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