Elbeem: fix memory leak and add guarded allocator directives
[blender.git] / intern / elbeem / intern / attributes.h
1 /** \file elbeem/intern/attributes.h
2  *  \ingroup elbeem
3  */
4 /******************************************************************************
5  *
6  * El'Beem - Free Surface Fluid Simulation with the Lattice Boltzmann Method
7  * Copyright 2003-2006 Nils Thuerey
8  *
9  * DEPRECATED - replaced by elbeem API, only channels are still used
10  *
11  *****************************************************************************/
12
13
14 #ifndef NTL_ATTRIBUTES_H
15
16 #include "utilities.h"
17
18 #ifdef WITH_CXX_GUARDEDALLOC
19 #  include "MEM_guardedalloc.h"
20 #endif
21
22 template<class T> class ntlMatrix4x4;
23 class ntlSetVec3f;
24 std::ostream& operator<<( std::ostream& os, const ntlSetVec3f& i );
25
26
27
28 //! An animated attribute channel
29 template<class Scalar>
30 class AnimChannel
31 {
32         public:
33                 // default constructor
34                 AnimChannel() : 
35                         mValue(), mTimes() { mInited = false; debugPrintChannel(); }
36
37                 // null init constructor
38                 AnimChannel(Scalar null) : 
39                         mValue(1), mTimes(1) { mValue[0]=null; mTimes[0]=0.0; mInited = true; debugPrintChannel(); }
40
41                 // proper init
42                 AnimChannel(vector<Scalar> &v, vector<double> &t) : 
43                         mValue(v), mTimes(t) { mInited = true; debugPrintChannel(); }
44
45                 // desctructor, nothing to do
46                 ~AnimChannel() { };
47
48                 // get interpolated value at time t
49                 Scalar get(double t) const {
50                         if(!mInited) { Scalar null; null=(Scalar)(0.0); return null; }
51                         if(t<=mTimes[0])               { return mValue[0]; }
52                         if(t>=mTimes[mTimes.size()-1]) { return mValue[mTimes.size()-1]; }
53                         for(size_t i=0; i<mTimes.size()-1; i++) {
54                                 // find first time thats in between
55                                 if((mTimes[i]<=t)&&(mTimes[i+1]>t)) { 
56                                         // interpolate
57                                         double d = mTimes[i+1]-mTimes[i];
58                                         double f = (t-mTimes[i])/d;
59                                         //return (Scalar)(mValue[i] * (1.0-f) + mValue[i+1] * f);
60                                         Scalar ret,tmp;
61                                         ret = mValue[i];
62                                         ret *= 1.-f;
63                                         tmp = mValue[i+1];
64                                         tmp *= f;
65                                         ret += tmp;
66                                         return ret;
67                                 }
68                         }
69                         // whats this...?
70                         return mValue[0];
71                 };
72
73                 // get uninterpolated value at time t
74                 Scalar getConstant(double t) const {
75                         //errMsg("DEBB","getc"<<t<<" ");
76                         if(!mInited) { Scalar null; null=(Scalar)0.0; return null; }
77                         if(t<=mTimes[0])               { return mValue[0]; }
78                         if(t>=mTimes[mTimes.size()-1]) { return mValue[mTimes.size()-1]; }
79                         for(size_t i=0; i<mTimes.size()-1; i++) {
80                                 //errMsg("DEBB","getc i"<<i<<" "<<mTimes[i]);
81                                 // find first time thats in between
82                                 if((mTimes[i]<=t)&&(mTimes[i+1]>t)) { return mValue[i]; }
83                         }
84                         // whats this...?
85                         return mValue[0];
86                 };
87
88                 // reset to null value
89                 void reset(Scalar null) {
90                         mValue.clear();
91                         mTimes.clear();
92                         mValue.push_back(null);
93                         mTimes.push_back(0.0);
94                 }
95
96                 //! debug function, prints channel as string
97                 string printChannel();
98                 //! debug function, prints to stdout if DEBUG_CHANNELS flag is enabled, used in constructors
99                 void debugPrintChannel();
100                 //! valid init?
101                 bool isInited() const { return mInited; }
102
103                 //! get number of entries (value and time sizes have to be equal)
104                 int getSize() const { return mValue.size(); };
105                 //! raw access of value vector
106                 vector<Scalar> &accessValues() { return mValue; }
107                 //! raw access of time vector
108                 vector<double> &accessTimes() { return mTimes; }
109                 
110         protected:
111
112                 /*! inited at least once? */
113                 bool mInited;
114                 /*! anim channel attribute values */
115                 vector<Scalar> mValue;
116                 /*! anim channel attr times */
117                 vector<double> mTimes;
118
119 private:
120 #ifdef WITH_CXX_GUARDEDALLOC
121         MEM_CXX_CLASS_ALLOC_FUNCS("ELBEEM:AnimChannel")
122 #endif
123 };
124
125
126 // helper class (not templated) for animated meshes
127 class ntlSetVec3f {
128         public:
129                 ntlSetVec3f(): mVerts() {};
130                 ntlSetVec3f(double v);
131                 ntlSetVec3f(vector<ntlVec3f> &v) { mVerts = v; };
132
133                 const ntlSetVec3f& operator=(double v );
134                 ntlSetVec3f& operator+=( double v );
135                 ntlSetVec3f& operator+=( const ntlSetVec3f &v );
136                 ntlSetVec3f& operator*=( double v );
137                 ntlSetVec3f& operator*=( const ntlSetVec3f &v );
138
139                 vector<ntlVec3f> mVerts;
140
141 private:
142 #ifdef WITH_CXX_GUARDEDALLOC
143         MEM_CXX_CLASS_ALLOC_FUNCS("ELBEEM:ntlSetVec3f")
144 #endif
145 };
146
147
148 // warning: DEPRECATED - replaced by elbeem API
149 class Attribute
150 {
151         public:
152         Attribute(string mn, vector<string> &value, int setline,bool channel) { 
153                         mn = string(""); setline=0; channel=false; value.clear(); // remove warnings
154                 };
155         Attribute(Attribute &a) { a.getCompleteString(); };
156         ~Attribute() { };
157
158                 void setUsed(bool set){ set=false; }
159                 bool getUsed() { return true; }
160                 void setIsChannel(bool set){ set=false;  }
161                 bool getIsChannel() { return false; }
162
163                 string getAsString(bool debug=false);
164                 int getAsInt();
165                 bool getAsBool();
166                 double getAsFloat();
167                 ntlVec3d getAsVec3d();
168                 void getAsMat4Gfx(ntlMatrix4x4<gfxReal> *mat);
169
170                 AnimChannel<int> getChannelInt();
171                 AnimChannel<double> getChannelFloat();
172                 AnimChannel<ntlVec3d> getChannelVec3d();
173                 AnimChannel<ntlSetVec3f> getChannelSetVec3f();
174
175                 string getCompleteString();
176                 void print();
177                 
178         protected:
179
180                 bool initChannel(int elemSize);
181
182 private:
183 #ifdef WITH_CXX_GUARDEDALLOC
184         MEM_CXX_CLASS_ALLOC_FUNCS("ELBEEM:Attribute")
185 #endif
186 };
187
188
189 // warning: DEPRECATED - replaced by elbeem API
190 //! The list of configuration attributes
191 class AttributeList
192 {
193         public:
194         AttributeList(string name) { name=string(""); };
195         ~AttributeList();
196                 void addAttr(string name, vector<string> &value, int line, bool isChannel) { 
197                         name=string(""); value.clear(); line=0; isChannel=false; // remove warnings
198                 };
199                 bool exists(string name) { name=string(""); return false; }
200                 void setAllUsed();
201                 bool checkUnusedParams();
202                 void import(AttributeList *oal);
203                 int      readInt(string name, int defaultValue,         string source,string target, bool needed);
204                 bool     readBool(string name, bool defaultValue,       string source,string target, bool needed);
205                 double   readFloat(string name, double defaultValue,   string source,string target, bool needed);
206                 string   readString(string name, string defaultValue,   string source,string target, bool needed);
207                 ntlVec3d readVec3d(string name, ntlVec3d defaultValue,  string source,string target, bool needed);
208                 void readMat4Gfx(string name, ntlMatrix4x4<gfxReal> defaultValue,  string source,string target, bool needed, ntlMatrix4x4<gfxReal> *mat);
209                 AnimChannel<int>     readChannelInt(         string name, int defaultValue=0, string source=string("src"), string target=string("dst"), bool needed=false );
210                 AnimChannel<double>  readChannelFloat(       string name, double defaultValue=0, string source=string("src"), string target=string("dst"), bool needed=false );
211                 AnimChannel<ntlVec3d> readChannelVec3d(      string name, ntlVec3d defaultValue=ntlVec3d(0.), string source=string("src"), string target=string("dst"), bool needed=false );
212                 AnimChannel<ntlSetVec3f> readChannelSetVec3f(string name, ntlSetVec3f defaultValue=ntlSetVec3f(0.), string source=string("src"), string target=string("dst"), bool needed=false );
213                 AnimChannel<ntlVec3f> readChannelVec3f(           string name, ntlVec3f defaultValue=ntlVec3f(0.), string source=string("src"), string target=string("dst"), bool needed=false );
214                 AnimChannel<float>    readChannelSinglePrecFloat( string name, float defaultValue=0., string source=string("src"), string target=string("dst"), bool needed=false );
215                 bool ignoreParameter(string name, string source);
216                 void print();
217         protected:
218
219 private:
220 #ifdef WITH_CXX_GUARDEDALLOC
221         MEM_CXX_CLASS_ALLOC_FUNCS("ELBEEM:AttributeList")
222 #endif
223 };
224
225 ntlVec3f channelFindMaxVf (AnimChannel<ntlVec3f> channel);
226 ntlVec3d channelFindMaxVd (AnimChannel<ntlVec3d> channel);
227 int      channelFindMaxi  (AnimChannel<int     > channel);
228 float    channelFindMaxf  (AnimChannel<float   > channel);
229 double   channelFindMaxd  (AnimChannel<double  > channel);
230
231 // unoptimized channel simplification functions, use elbeem.cpp functions
232 bool channelSimplifyVf (AnimChannel<ntlVec3f> &channel);
233 bool channelSimplifyVd (AnimChannel<ntlVec3d> &channel);
234 bool channelSimplifyi  (AnimChannel<int     > &channel);
235 bool channelSimplifyf  (AnimChannel<float   > &channel);
236 bool channelSimplifyd  (AnimChannel<double  > &channel);
237
238 //! output channel values? on=1/off=0
239 #define DEBUG_PCHANNELS 0
240
241 //! debug function, prints to stdout if DEBUG_PCHANNELS flag is enabled, used in constructors
242 template<class Scalar>
243 void AnimChannel<Scalar>::debugPrintChannel() { }
244
245
246 #define NTL_ATTRIBUTES_H
247 #endif
248