1 // blend.h
2 //
3 // Copyright (C) 2003, 2004 Jason Bevins
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or (at
8 // your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
13 // License (COPYING.txt) for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation,
17 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 //
19 // The developer's email is jlbezigvins@gmzigail.com (for great email, take
20 // off every 'zig'.)
21 //
22 module noise.mod.blend;
23 
24 private {
25     import noise.mod.modulebase;
26     import noise.interp;
27 }
28 
29 /// @addtogroup libnoise
30 /// @{
31 
32 /// @addtogroup modules
33 /// @{
34 
35 /// @defgroup selectormodules Selector Mods
36 /// @addtogroup selectormodules
37 /// @{
38 
39 /// Noise module that outputs a weighted blend of the output values from
40 /// two source modules given the output value supplied by a control module.
41 ///
42 /// @image html moduleblend.png
43 ///
44 /// Unlike most other noise modules, the index value assigned to a source
45 /// module determines its role in the blending operation:
46 /// - Source module 0 (upper left in the diagram) outputs one of the
47 ///   values to blend.
48 /// - Source module 1 (lower left in the diagram) outputs one of the
49 ///   values to blend.
50 /// - Source module 2 (bottom of the diagram) is known as the <i>control
51 ///   module</i>.  The control module determines the weight of the
52 ///   blending operation.  Negative values weigh the blend towards the
53 ///   output value from the source module with an index value of 0.
54 ///   Positive values weigh the blend towards the output value from the
55 ///   source module with an index value of 1.
56 ///
57 /// An application can pass the control module to the SetControlMod()
58 /// method instead of the SetSourceMod() method.  This may make the
59 /// application code easier to read.
60 ///
61 /// This noise module uses linear interpolation to perform the blending
62 /// operation.
63 ///
64 /// This noise module requires three source modules.
65 class Blend : Mod
66 {
67 
68   public:
69 
70     /// Constructor.
71     this () {
72         super(this.GetSourceModCount());
73     }
74 
75     /// Returns the control module.
76     ///
77     /// @returns A reference to the control module.
78     ///
79     /// @pre A control module has been added to this noise module via a
80     /// call to SetSourceMod() or SetControlMod().
81     ///
82     /// @throw new noise::ExceptionNoMod See the preconditions for more
83     /// information.
84     ///
85     /// The control module determines the weight of the blending
86     /// operation.  Negative values weigh the blend towards the output
87     /// value from the source module with an index value of 0.  Positive
88     /// values weigh the blend towards the output value from the source
89     /// module with an index value of 1.
90     ref const(Mod) GetControlMod () const
91     {
92       if (m_pSourceMod is null || m_pSourceMod[2] is null) {
93         throw new ExceptionNoMod ();
94       }
95       return *m_pSourceMod[2];
96     }
97 
98     override int GetSourceModCount () const
99     {
100       return 3;
101     }
102 
103     override double GetValue (double x, double y, double z) const
104     {
105       assert (m_pSourceMod[0] !is null);
106       assert (m_pSourceMod[1] !is null);
107       assert (m_pSourceMod[2] !is null);
108 
109       double v0 = m_pSourceMod[0].GetValue (x, y, z);
110       double v1 = m_pSourceMod[1].GetValue (x, y, z);
111       double alpha = (m_pSourceMod[2].GetValue (x, y, z) + 1.0) / 2.0;
112       return LinearInterp (v0, v1, alpha);
113     }   
114 
115 
116     /// Sets the control module.
117     ///
118     /// @param controlMod The control module.
119     ///
120     /// The control module determines the weight of the blending
121     /// operation.  Negative values weigh the blend towards the output
122     /// value from the source module with an index value of 0.  Positive
123     /// values weigh the blend towards the output value from the source
124     /// module with an index value of 1.
125     ///
126     /// This method assigns the control module an index value of 2.
127     /// Passing the control module to this method produces the same
128     /// results as passing the control module to the SetSourceMod()
129     /// method while assigning that noise module an index value of 2.
130     ///
131     /// This control module must exist throughout the lifetime of this
132     /// noise module unless another control module replaces that control
133     /// module.
134     void SetControlMod (const(Mod)* controlMod)
135     {
136       assert (m_pSourceMod !is null);
137       m_pSourceMod[2] = controlMod;
138     }
139 
140 }
141 
142 /// @}
143 
144 /// @}
145 
146 /// @}