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