1 // spheres.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.spheres;
23 
24 private {
25   import noise.mod.modulebase;
26   import noise.misc;
27 }
28 
29 /// @addtogroup libnoise
30 /// @{
31 
32 /// @addtogroup modules
33 /// @{
34 
35 /// @addtogroup generatormodules
36 /// @{
37 
38 /// Default frequency value for the module::Spheres noise module.
39 const double DEFAULT_SPHERES_FREQUENCY = 1.0;
40 
41 /// Noise module that outputs concentric spheres.
42 ///
43 /// @image html modulespheres.png
44 ///
45 /// This noise module outputs concentric spheres centered on the origin
46 /// like the concentric rings of an onion.
47 ///
48 /// The first sphere has a radius of 1.0.  Each subsequent sphere has a
49 /// radius that is 1.0 unit larger than the previous sphere.
50 ///
51 /// The output value from this noise module is determined by the distance
52 /// between the input value and the the nearest spherical surface.  The
53 /// input values that are located on a spherical surface are given the
54 /// output value 1.0 and the input values that are equidistant from two
55 /// spherical surfaces are given the output value -1.0.
56 ///
57 /// An application can change the frequency of the concentric spheres.
58 /// Increasing the frequency reduces the distances between spheres.  To
59 /// specify the frequency, call the SetFrequency() method.
60 ///
61 /// This noise module, modified with some low-frequency, low-power
62 /// turbulence, is useful for generating agate-like textures.
63 ///
64 /// This noise module does not require any source modules.    
65 class Spheres : Mod
66 {
67 
68   public:
69 
70     /// Constructor.
71     ///
72     /// The default frequency is set to
73     /// module::DEFAULT_SPHERES_FREQUENCY.
74     this()
75     {
76         super(this.GetSourceModCount ());
77         m_frequency = DEFAULT_SPHERES_FREQUENCY;
78     }
79 
80     /// Returns the frequency of the concentric spheres.
81     ///
82     /// @returns The frequency of the concentric spheres.
83     ///
84     /// Increasing the frequency increases the density of the concentric
85     /// spheres, reducing the distances between them.
86     double GetFrequency () const
87     {
88       return m_frequency;
89     }
90 
91     override int GetSourceModCount () const
92     {
93       return 0;
94     }
95 
96     override double GetValue (double x, double y, double z) const
97     {
98       x *= m_frequency;
99       y *= m_frequency;
100       z *= m_frequency;
101 
102       double distFromCenter = sqrt (x * x + y * y + z * z);
103       double distFromSmallerSphere = distFromCenter - floor (distFromCenter);
104       double distFromLargerSphere = 1.0 - distFromSmallerSphere;
105       double nearestDist = GetMin (distFromSmallerSphere, distFromLargerSphere);
106       return 1.0 - (nearestDist * 4.0); // Puts it in the -1.0 to +1.0 range.
107     }
108 
109     /// Sets the frequenct of the concentric spheres.
110     ///
111     /// @param frequency The frequency of the concentric spheres.
112     ///
113     /// Increasing the frequency increases the density of the concentric
114     /// spheres, reducing the distances between them.
115     void SetFrequency (double frequency)
116     {
117       m_frequency = frequency;
118     }
119 
120   protected:
121 
122     /// Frequency of the concentric spheres.
123     double m_frequency;
124 
125 };
126 
127 /// @}
128 
129 /// @}
130 
131 /// @}