1 // cache.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.cache;
23 
24 private {
25     import noise.mod.modulebase;
26 }
27 
28 /// @addtogroup libnoise
29 /// @{
30 
31 /// @addtogroup modules
32 /// @{
33 
34 /// @defgroup miscmodules Miscellaneous Mods
35 /// @addtogroup miscmodules
36 /// @{
37 
38 /// Noise module that caches the last output value generated by a source
39 /// module.
40 ///
41 /// If an application passes an input value to the GetValue() method that
42 /// differs from the previously passed-in input value, this noise module
43 /// instructs the source module to calculate the output value.  This
44 /// value, as well as the ( @a x, @a y, @a z ) coordinates of the input
45 /// value, are stored (cached) in this noise module.
46 ///
47 /// If the application passes an input value to the GetValue() method
48 /// that is equal to the previously passed-in input value, this noise
49 /// module returns the cached output value without having the source
50 /// module recalculate the output value.
51 ///
52 /// If an application passes a new source module to the SetSourceMod()
53 /// method, the cache is invalidated.
54 ///
55 /// Caching a noise module is useful if it is used as a source module for
56 /// multiple noise modules.  If a source module is not cached, the source
57 /// module will redundantly calculate the same output value once for each
58 /// noise module in which it is included.
59 ///
60 /// This noise module requires one source module.
61 class Cache : Mod
62 {
63 
64   public:
65 
66     /// Constructor.
67     this ()
68     {
69         super(this.GetSourceModCount());
70         m_isCached = false;
71     }
72 
73     override int GetSourceModCount () const
74     {
75       return 1;
76     }
77 
78     override double GetValue (double x, double y, double z) const
79     {
80       assert (m_pSourceMod[0] !is null);
81 
82       if (!(m_isCached && x == m_xCache && y == m_yCache && z == m_zCache)) {
83         m_cachedValue = m_pSourceMod[0].GetValue (x, y, z);
84         m_xCache = x;
85         m_yCache = y;
86         m_zCache = z;
87       }
88       m_isCached = true;
89       return m_cachedValue;
90     }
91 
92 
93     override void SetSourceMod (int index, const(Mod)* sourceMod)
94     {
95       super.SetSourceMod (index, sourceMod);
96       m_isCached = false;
97     }
98 
99   protected:
100 
101     /// The cached output value at the cached input value.
102     double m_cachedValue;
103 
104     /// Determines if a cached output value is stored in this noise
105     /// module.
106     double m_isCached;
107 
108     /// @a x coordinate of the cached input value.
109     double m_xCache;
110 
111     /// @a y coordinate of the cached input value.
112     double m_yCache;
113 
114     /// @a z coordinate of the cached input value.
115     double m_zCache;
116 
117 }
118 
119 /// @}
120 
121 /// @}
122 
123 /// @}