Cleanup: remove redundant doxygen \file argument
[blender.git] / intern / elbeem / intern / ntl_geometryobject.cpp
1 /** \file \ingroup elbeem
2  */
3 /******************************************************************************
4  *
5  * El'Beem - Free Surface Fluid Simulation with the Lattice Boltzmann Method
6  * Copyright 2003-2006 Nils Thuerey
7  *
8  * a geometry object
9  * all other geometry objects are derived from this one
10  *
11  *****************************************************************************/
12
13
14 #include "ntl_geometryobject.h"
15 #include "ntl_world.h"
16 #include "ntl_matrices.h"
17
18 // for FGI
19 #include "elbeem.h"
20
21 #define TRI_UVOFFSET (1./4.)
22 //#define TRI_UVOFFSET (1./3.)
23
24
25 /*****************************************************************************/
26 /* Default constructor */
27 /*****************************************************************************/
28 ntlGeometryObject::ntlGeometryObject() :
29         mIsInitialized(false), mpMaterial( NULL ),
30         mMaterialName( "default" ),
31         mCastShadows( 1 ), mReceiveShadows( 1 ),
32         mGeoInitType( 0 ), 
33         mInitialVelocity(0.0), mcInitialVelocity(0.0), mLocalCoordInivel(false),
34         mGeoInitIntersect(false),
35         mGeoPartSlipValue(0.0),
36         mcGeoImpactFactor(1.),
37         mVolumeInit(VOLUMEINIT_VOLUME),
38         mInitialPos(0.),
39         mcTrans(0.), mcRot(0.), mcScale(1.),
40         mIsAnimated(false),
41         mMovPoints(), mMovNormals(),
42         mHaveCachedMov(false),
43         mCachedMovPoints(), mCachedMovNormals(),
44         mTriangleDivs1(), mTriangleDivs2(),
45         mMovPntsInited(-100.0), mMaxMovPnt(-1),
46         mcGeoActive(1.),
47         mCpsTimeStart(0.), mCpsTimeEnd(1.0), mCpsQuality(10.),
48         mcAttrFStr(0.),mcAttrFRad(0.), mcVelFStr(0.), mcVelFRad(0.)
49
50 };
51
52
53 /*****************************************************************************/
54 /* Default destructor */
55 /*****************************************************************************/
56 ntlGeometryObject::~ntlGeometryObject() 
57 {
58 }
59
60 /*! is the mesh animated? */
61 bool ntlGeometryObject::getMeshAnimated() {
62         // off by default, on for e.g. ntlGeometryObjModel
63         return false; 
64 }
65
66 /*! init object anim flag */
67 bool ntlGeometryObject::checkIsAnimated() {
68         if(    (mcTrans.accessValues().size()>1)  // VALIDATE
69             || (mcRot.accessValues().size()>1) 
70             || (mcScale.accessValues().size()>1) 
71             || (mcGeoActive.accessValues().size()>1) 
72                         // mcGeoImpactFactor only needed when moving
73             || (mcInitialVelocity.accessValues().size()>1) 
74                 ) {
75                 mIsAnimated = true;
76         }
77
78         // fluid objects always have static init!
79         if(mGeoInitType==FGI_FLUID) {
80                 mIsAnimated=false;
81         }
82         //errMsg("ntlGeometryObject::checkIsAnimated","obj="<<getName()<<" debug: trans:"<<mcTrans.accessValues().size()<<" rot:"<<mcRot.accessValues().size()<<" scale:"<<mcScale.accessValues().size()<<" active:"<<mcGeoActive.accessValues().size()<<" inivel:"<<mcInitialVelocity.accessValues().size()<<". isani?"<<mIsAnimated ); // DEBUG
83         return mIsAnimated;
84 }
85
86 /*****************************************************************************/
87 /* Init attributes etc. of this object */
88 /*****************************************************************************/
89 #define GEOINIT_STRINGS  10
90 static const char *initStringStrs[GEOINIT_STRINGS] = {
91         "fluid",
92         "bnd_no","bnd_noslip",
93         "bnd_free","bnd_freeslip",
94         "bnd_part","bnd_partslip",
95         "inflow", "outflow", "control",
96 };
97 static int initStringTypes[GEOINIT_STRINGS] = {
98         FGI_FLUID,
99         FGI_BNDNO, FGI_BNDNO,
100         FGI_BNDFREE, FGI_BNDFREE,
101         FGI_BNDPART, FGI_BNDPART,
102         FGI_MBNDINFLOW, FGI_MBNDOUTFLOW, 
103         FGI_CONTROL
104 };
105 void ntlGeometryObject::initialize(ntlRenderGlobals *glob) 
106 {
107         //debugOut("ntlGeometryObject::initialize: '"<<getName()<<"' ", 10);
108         // initialize only once...
109         if(mIsInitialized) return;
110         
111         // init material, always necessary
112         searchMaterial( glob->getMaterials() );
113         
114         this->mGeoInitId = mpAttrs->readInt("geoinitid", this->mGeoInitId,"ntlGeometryObject", "mGeoInitId", false);
115         mGeoInitIntersect = mpAttrs->readInt("geoinit_intersect", mGeoInitIntersect,"ntlGeometryObject", "mGeoInitIntersect", false);
116         string ginitStr = mpAttrs->readString("geoinittype", "", "ntlGeometryObject", "mGeoInitType", false);
117         if(this->mGeoInitId>=0) {
118                 bool gotit = false;
119                 for(int i=0; i<GEOINIT_STRINGS; i++) {
120                         if(ginitStr== initStringStrs[i]) {
121                                 gotit = true;
122                                 mGeoInitType = initStringTypes[i];
123                         }
124                 }
125
126                 if(!gotit) {
127                         errFatal("ntlGeometryObject::initialize","Obj '"<<mName<<"', Unknown 'geoinittype' value: '"<< ginitStr <<"' ", SIMWORLD_INITERROR);
128                         return;
129                 }
130         }
131
132         int geoActive = mpAttrs->readInt("geoinitactive", 1,"ntlGeometryObject", "geoActive", false);
133         if(!geoActive) {
134                 // disable geo init again...
135                 this->mGeoInitId = -1;
136         }
137         mInitialVelocity  = vec2G( mpAttrs->readVec3d("initial_velocity", vec2D(mInitialVelocity),"ntlGeometryObject", "mInitialVelocity", false));
138         if(getAttributeList()->exists("initial_velocity") || (!mcInitialVelocity.isInited()) ) {
139                 mcInitialVelocity = mpAttrs->readChannelVec3f("initial_velocity");
140         }
141         // always use channel
142         if(!mcInitialVelocity.isInited()) { mcInitialVelocity = AnimChannel<ntlVec3Gfx>(mInitialVelocity); }
143         mLocalCoordInivel = mpAttrs->readBool("geoinit_localinivel", mLocalCoordInivel,"ntlGeometryObject", "mLocalCoordInivel", false);
144
145         mGeoPartSlipValue = mpAttrs->readFloat("geoinit_partslip", mGeoPartSlipValue,"ntlGeometryObject", "mGeoPartSlipValue", false);
146         bool mOnlyThinInit = false; // deprecated!
147         mOnlyThinInit     = mpAttrs->readBool("geoinit_onlythin", mOnlyThinInit,"ntlGeometryObject", "mOnlyThinInit", false);
148         if(mOnlyThinInit) mVolumeInit = VOLUMEINIT_SHELL;
149         mVolumeInit     = mpAttrs->readInt("geoinit_volumeinit", mVolumeInit,"ntlGeometryObject", "mVolumeInit", false);
150         if((mVolumeInit<VOLUMEINIT_VOLUME)||(mVolumeInit>VOLUMEINIT_BOTH)) mVolumeInit = VOLUMEINIT_VOLUME;
151
152         // moving obs correction factor
153         float impactfactor=1.;
154         impactfactor = (float)mpAttrs->readFloat("impactfactor", impactfactor,"ntlGeometryObject", "impactfactor", false);
155         if(getAttributeList()->exists("impactfactor") || (!mcGeoImpactFactor.isInited()) ) {
156                 mcGeoImpactFactor = mpAttrs->readChannelSinglePrecFloat("impactfactor");
157         }
158
159         // override cfg types
160         mVisible = mpAttrs->readBool("visible", mVisible,"ntlGeometryObject", "mVisible", false);
161         mReceiveShadows = mpAttrs->readBool("recv_shad", mReceiveShadows,"ntlGeometryObject", "mReceiveShadows", false);
162         mCastShadows = mpAttrs->readBool("cast_shad", mCastShadows,"ntlGeometryObject", "mCastShadows", false);
163
164         // read mesh animation channels
165         ntlVec3d translation(0.0);
166         translation = mpAttrs->readVec3d("translation", translation,"ntlGeometryObject", "translation", false);
167         if(getAttributeList()->exists("translation") || (!mcTrans.isInited()) ) {
168                 mcTrans = mpAttrs->readChannelVec3f("translation");
169         }
170         ntlVec3d rotation(0.0);
171         rotation = mpAttrs->readVec3d("rotation", rotation,"ntlGeometryObject", "rotation", false);
172         if(getAttributeList()->exists("rotation") || (!mcRot.isInited()) ) {
173                 mcRot = mpAttrs->readChannelVec3f("rotation");
174         }
175         ntlVec3d scale(1.0);
176         scale = mpAttrs->readVec3d("scale", scale,"ntlGeometryObject", "scale", false);
177         if(getAttributeList()->exists("scale") || (!mcScale.isInited()) ) {
178                 mcScale = mpAttrs->readChannelVec3f("scale");
179         }
180
181         float geoactive=1.;
182         geoactive = (float)mpAttrs->readFloat("geoactive", geoactive,"ntlGeometryObject", "geoactive", false);
183         if(getAttributeList()->exists("geoactive") || (!mcGeoActive.isInited()) ) {
184                 mcGeoActive = mpAttrs->readChannelSinglePrecFloat("geoactive");
185         }
186         // always use channel
187         if(!mcGeoActive.isInited()) { mcGeoActive = AnimChannel<float>(geoactive); }
188
189         checkIsAnimated();
190
191         mIsInitialized = true;
192         debMsgStd("ntlGeometryObject::initialize",DM_MSG,"GeoObj '"<<this->getName()<<"': visible="<<this->mVisible<<" gid="<<this->mGeoInitId<<" gtype="<<mGeoInitType<<","<<ginitStr<<
193                         " gvel="<<mInitialVelocity<<" gisect="<<mGeoInitIntersect, 10); // debug
194 }
195
196 /*! notify object that dump is in progress (e.g. for particles) */
197 // default action - do nothing...
198 void ntlGeometryObject::notifyOfDump(int dumtp, int frameNr,char *frameNrStr,string outfilename, double simtime) {
199   bool debugOut=false;
200         if(debugOut) debMsgStd("ntlGeometryObject::notifyOfDump",DM_MSG," dt:"<<dumtp<<" obj:"<<this->getName()<<" frame:"<<frameNrStr<<","<<frameNr<<",t"<<simtime<<" to "<<outfilename, 10); // DEBUG
201 }
202
203 /*****************************************************************************/
204 /* Search the material for this object from the material list */
205 /*****************************************************************************/
206 void ntlGeometryObject::searchMaterial(vector<ntlMaterial *> *mat)
207 {
208         /* search the list... */
209         int i=0;
210         for (vector<ntlMaterial*>::iterator iter = mat->begin();
211          iter != mat->end(); iter++) {
212                 if( mMaterialName == (*iter)->getName() ) {
213                         //warnMsg("ntlGeometryObject::searchMaterial","for obj '"<<getName()<<"' found - '"<<(*iter)->getName()<<"' "<<i); // DEBUG
214                         mpMaterial = (*iter);
215                         return;
216                 }
217                 i++;
218         }
219         errFatal("ntlGeometryObject::searchMaterial","Unknown material '"<<mMaterialName<<"' ! ", SIMWORLD_INITERROR);
220         mpMaterial = new ntlMaterial();
221         return;
222 }
223
224 /******************************************************************************
225  * static add triangle function
226  *****************************************************************************/
227 void ntlGeometryObject::sceneAddTriangle(
228                 ntlVec3Gfx  p1,ntlVec3Gfx  p2,ntlVec3Gfx  p3,
229                 ntlVec3Gfx pn1,ntlVec3Gfx pn2,ntlVec3Gfx pn3,
230                 ntlVec3Gfx trin, bool smooth,
231                 vector<ntlTriangle> *triangles,
232                 vector<ntlVec3Gfx>  *vertices,
233                 vector<ntlVec3Gfx>  *normals) {
234         ntlTriangle tri;
235         int tempVert;
236   
237         if(normals->size() != vertices->size()) {
238                 errFatal("ntlGeometryObject::sceneAddTriangle","For '"<<this->mName<<"': Vertices and normals sizes to not match!!!",SIMWORLD_GENERICERROR);
239
240         } else {
241                 
242                 vertices->push_back( p1 ); 
243                 normals->push_back( pn1 ); 
244                 tempVert = normals->size()-1;
245                 tri.getPoints()[0] = tempVert;
246                 
247                 vertices->push_back( p2 ); 
248                 normals->push_back( pn2 ); 
249                 tempVert = normals->size()-1;
250                 tri.getPoints()[1] = tempVert;
251                 
252                 vertices->push_back( p3 ); 
253                 normals->push_back( pn3 ); 
254                 tempVert = normals->size()-1;
255                 tri.getPoints()[2] = tempVert;
256                 
257                 
258                 /* init flags from ntl_ray.h */
259                 int flag = 0; 
260                 if(getVisible()){ flag |= TRI_GEOMETRY; }
261                 if(getCastShadows() ) { 
262                         flag |= TRI_CASTSHADOWS; } 
263                 
264                 /* init geo init id */
265                 int geoiId = getGeoInitId(); 
266                 //if((geoiId > 0) && (mVolumeInit&VOLUMEINIT_VOLUME) && (!mIsAnimated)) { 
267                 if((geoiId > 0) && (mVolumeInit&VOLUMEINIT_VOLUME)) { 
268                         flag |= (1<< (geoiId+4)); 
269                         flag |= mGeoInitType; 
270                 } 
271                 /*errMsg("ntlScene::addTriangle","DEBUG flag="<<convertFlags2String(flag) ); */ 
272                 tri.setFlags( flag );
273                 
274                 /* triangle normal missing */
275                 tri.setNormal( trin );
276                 tri.setSmoothNormals( smooth );
277                 tri.setObjectId( this->mObjectId );
278                 triangles->push_back( tri ); 
279         } /* normals check*/ 
280 }
281
282 void ntlGeometryObject::sceneAddTriangleNoVert(int *trips,
283                 ntlVec3Gfx trin, bool smooth,
284                 vector<ntlTriangle> *triangles) {
285         ntlTriangle tri;
286                 
287         tri.getPoints()[0] = trips[0];
288         tri.getPoints()[1] = trips[1];
289         tri.getPoints()[2] = trips[2];
290
291         // same as normal sceneAddTriangle
292
293         /* init flags from ntl_ray.h */
294         int flag = 0; 
295         if(getVisible()){ flag |= TRI_GEOMETRY; }
296         if(getCastShadows() ) { 
297                 flag |= TRI_CASTSHADOWS; } 
298
299         /* init geo init id */
300         int geoiId = getGeoInitId(); 
301         if((geoiId > 0) && (mVolumeInit&VOLUMEINIT_VOLUME)) { 
302                 flag |= (1<< (geoiId+4)); 
303                 flag |= mGeoInitType; 
304         } 
305         /*errMsg("ntlScene::addTriangle","DEBUG flag="<<convertFlags2String(flag) ); */ 
306         tri.setFlags( flag );
307
308         /* triangle normal missing */
309         tri.setNormal( trin );
310         tri.setSmoothNormals( smooth );
311         tri.setObjectId( this->mObjectId );
312         triangles->push_back( tri ); 
313 }
314
315
316 /******************************************************************************/
317 /* Init channels from float arrays (for elbeem API) */
318 /******************************************************************************/
319
320 #define ADD_CHANNEL_VEC(dst,nvals,val) \
321                 vals.clear(); time.clear(); elbeemSimplifyChannelVec3(val,&nvals); \
322                 for(int i=0; i<(nvals); i++) { \
323                         vals.push_back(ntlVec3Gfx((val)[i*4+0], (val)[i*4+1],(val)[i*4+2] )); \
324                         time.push_back( (val)[i*4+3] ); \
325                 } \
326                 (dst) = AnimChannel< ntlVec3Gfx >(vals,time); 
327
328 #define ADD_CHANNEL_FLOAT(dst,nvals,val) \
329                 valsfloat.clear(); time.clear(); elbeemSimplifyChannelFloat(val,&nvals); \
330                 for(int i=0; i<(nvals); i++) { \
331                         valsfloat.push_back( (val)[i*2+0] ); \
332                         time.push_back( (val)[i*2+1] ); \
333                 } \
334                 (dst) = AnimChannel< float >(valsfloat,time); 
335
336 void ntlGeometryObject::initChannels(
337                 int nTrans, float *trans, int nRot, float *rot, int nScale, float *scale,
338                 int nAct, float *act, int nIvel, float *ivel,
339                 int nAttrFStr, float *attrFStr,
340                 int nAttrFRad, float *attrFRad,
341                 int nVelFStr, float *velFStr,
342                 int nVelFRad, float *velFRad
343                 ) {
344         const bool debugInitc=true;
345         if(debugInitc) { debMsgStd("ntlGeometryObject::initChannels",DM_MSG,"nt:"<<nTrans<<" nr:"<<nRot<<" ns:"<<nScale, 10); 
346                          debMsgStd("ntlGeometryObject::initChannels",DM_MSG,"na:"<<nAct<<" niv:"<<nIvel<<" ", 10); }
347         vector<ntlVec3Gfx> vals;
348         vector<float>  valsfloat;
349         vector<double> time;
350         if((trans)&&(nTrans>0)) {  ADD_CHANNEL_VEC(mcTrans, nTrans, trans); }
351         if((rot)&&(nRot>0)) {      ADD_CHANNEL_VEC(mcRot, nRot, rot); }
352         if((scale)&&(nScale>0)) {  ADD_CHANNEL_VEC(mcScale, nScale, scale); }
353         if((act)&&(nAct>0)) {      ADD_CHANNEL_FLOAT(mcGeoActive, nAct, act); }
354         if((ivel)&&(nIvel>0)) {    ADD_CHANNEL_VEC(mcInitialVelocity, nIvel, ivel); }
355         
356         /* fluid control channels */
357         if((attrFStr)&&(nAttrFStr>0)) { ADD_CHANNEL_FLOAT(mcAttrFStr, nAttrFStr, attrFStr); }
358         if((attrFRad)&&(nAttrFRad>0)) { ADD_CHANNEL_FLOAT(mcAttrFRad, nAttrFRad, attrFRad); }
359         if((velFStr)&&(nVelFStr>0)) {   ADD_CHANNEL_FLOAT(mcVelFStr, nAct, velFStr); }
360         if((velFRad)&&(nVelFRad>0)) {   ADD_CHANNEL_FLOAT(mcVelFRad, nVelFRad, velFRad); }
361
362         checkIsAnimated();
363         
364         if(debugInitc) { 
365                 debMsgStd("ntlGeometryObject::initChannels",DM_MSG,getName()<<
366                                 " nt:"<<mcTrans.accessValues().size()<<" nr:"<<mcRot.accessValues().size()<<
367                                 " ns:"<<mcScale.accessValues().size()<<" isAnim:"<<mIsAnimated, 10); }
368
369         if(debugInitc) {
370                 std::ostringstream ostr;
371                 ostr << "trans: ";
372                 for(size_t i=0; i<mcTrans.accessValues().size(); i++) {
373                         ostr<<" "<<mcTrans.accessValues()[i]<<"@"<<mcTrans.accessTimes()[i]<<" ";
374                 } ostr<<";   ";
375                 ostr<<"rot: ";
376                 for(size_t i=0; i<mcRot.accessValues().size(); i++) {
377                         ostr<<" "<<mcRot.accessValues()[i]<<"@"<<mcRot.accessTimes()[i]<<" ";
378                 } ostr<<";   ";
379                 ostr<<"scale: ";
380                 for(size_t i=0; i<mcScale.accessValues().size(); i++) {
381                         ostr<<" "<<mcScale.accessValues()[i]<<"@"<<mcScale.accessTimes()[i]<<" ";
382                 } ostr<<";   ";
383                 ostr<<"act: ";
384                 for(size_t i=0; i<mcGeoActive.accessValues().size(); i++) {
385                         ostr<<" "<<mcGeoActive.accessValues()[i]<<"@"<<mcGeoActive.accessTimes()[i]<<" ";
386                 } ostr<<";   ";
387                 ostr<<"ivel: ";
388                 for(size_t i=0; i<mcInitialVelocity.accessValues().size(); i++) {
389                         ostr<<" "<<mcInitialVelocity.accessValues()[i]<<"@"<<mcInitialVelocity.accessTimes()[i]<<" ";
390                 } ostr<<";   ";
391                 debMsgStd("ntlGeometryObject::initChannels",DM_MSG,"Inited "<<ostr.str(),10);
392         }
393 }
394 #undef ADD_CHANNEL
395
396
397 /*****************************************************************************/
398 /* apply object translation at time t*/
399 /*****************************************************************************/
400 void ntlGeometryObject::applyTransformation(double t, vector<ntlVec3Gfx> *verts, vector<ntlVec3Gfx> *norms, int vstart, int vend, int forceTrafo) {
401         if(    (mcTrans.accessValues().size()>1)  // VALIDATE
402             || (mcRot.accessValues().size()>1) 
403             || (mcScale.accessValues().size()>1) 
404             || (forceTrafo)
405             || (!mHaveCachedMov)
406                 ) {
407                 // transformation is animated, continue
408                 ntlVec3Gfx pos = getTranslation(t); 
409                 ntlVec3Gfx scale = mcScale.get(t);
410                 ntlVec3Gfx rot = mcRot.get(t);
411                 ntlMat4Gfx rotMat;
412                 rotMat.initRotationXYZ(rot[0],rot[1],rot[2]);
413                 pos += mInitialPos;
414                 errMsg("ntlGeometryObject::applyTransformation","obj="<<getName()<<" t"<<pos<<" r"<<rot<<" s"<<scale);
415                 for(int i=vstart; i<vend; i++) {
416                         (*verts)[i] *= scale;
417                         (*verts)[i] = rotMat * (*verts)[i];
418                         (*verts)[i] += pos;
419                         //if(i<10) errMsg("ntlGeometryObject::applyTransformation"," v"<<i<<"/"<<vend<<"="<<(*verts)[i]);
420                 }
421                 if(norms) {
422                         for(int i=vstart; i<vend; i++) {
423                                 (*norms)[i] = rotMat * (*norms)[i];
424                         }
425                 }
426         } else {
427                 // not animated, cached points were already returned
428                 errMsg ("ntlGeometryObject::applyTransformation","Object "<<getName()<<" used cached points ");
429         }
430 }
431
432 /*! init triangle divisions */
433 void ntlGeometryObject::calcTriangleDivs(vector<ntlVec3Gfx> &verts, vector<ntlTriangle> &tris, gfxReal fsTri) {
434         mTriangleDivs1.resize( tris.size() );
435         mTriangleDivs2.resize( tris.size() );
436
437         //fsTri *= 2.; // DEBUG! , wrong init!
438
439         for(size_t i=0; i<tris.size(); i++) {
440                 const ntlVec3Gfx p0 = verts[ tris[i].getPoints()[0] ];
441                 const ntlVec3Gfx p1 = verts[ tris[i].getPoints()[1] ];
442                 const ntlVec3Gfx p2 = verts[ tris[i].getPoints()[2] ];
443                 const ntlVec3Gfx side1 = p1 - p0;
444                 const ntlVec3Gfx side2 = p2 - p0;
445                 int divs1=0, divs2=0;
446                 if(normNoSqrt(side1) > fsTri*fsTri) { divs1 = (int)(norm(side1)/fsTri); }
447                 if(normNoSqrt(side2) > fsTri*fsTri) { divs2 = (int)(norm(side2)/fsTri); }
448
449                 mTriangleDivs1[i] = divs1;
450                 mTriangleDivs2[i] = divs2;
451         }
452 }
453
454 /*! Prepare points for moving objects */
455 void ntlGeometryObject::initMovingPoints(double time, gfxReal featureSize) {
456         if((mMovPntsInited==featureSize)&&(!getMeshAnimated())) return;
457         const bool debugMoinit=false;
458
459         vector<ntlTriangle> triangles; 
460         vector<ntlVec3Gfx> vertices; 
461         vector<ntlVec3Gfx> vnormals; 
462         int objectId = 1;
463         this->getTriangles(time, &triangles,&vertices,&vnormals,objectId);
464         
465         mMovPoints.clear();
466         mMovNormals.clear();
467         if(debugMoinit) errMsg("ntlGeometryObject::initMovingPoints","Object "<<getName()<<" has v:"<<vertices.size()<<" t:"<<triangles.size() );
468         // no points?
469         if(vertices.size()<1) {
470                 mMaxMovPnt=-1;
471                 return; 
472         }
473         ntlVec3f maxscale = channelFindMaxVf(mcScale);
474         float maxpart = ABS(maxscale[0]);
475         if(ABS(maxscale[1])>maxpart) maxpart = ABS(maxscale[1]);
476         if(ABS(maxscale[2])>maxpart) maxpart = ABS(maxscale[2]);
477         float scaleFac = 1.0/(maxpart);
478         // TODO - better reinit from time to time?
479         const gfxReal fsTri = featureSize*0.5 *scaleFac;
480         if(debugMoinit) errMsg("ntlGeometryObject::initMovingPoints","maxscale:"<<maxpart<<" featureSize:"<<featureSize<<" fsTri:"<<fsTri );
481
482         if(mTriangleDivs1.size()!=triangles.size()) {
483                 calcTriangleDivs(vertices,triangles,fsTri);
484         }
485
486         // debug: count points to init
487         /*if(debugMoinit) {
488                 errMsg("ntlGeometryObject::initMovingPoints","Object "<<getName()<<" estimating...");
489                 int countp=vertices.size()*2;
490                 for(size_t i=0; i<triangles.size(); i++) {
491                         ntlVec3Gfx p0 = vertices[ triangles[i].getPoints()[0] ];
492                         ntlVec3Gfx side1 = vertices[ triangles[i].getPoints()[1] ] - p0;
493                         ntlVec3Gfx side2 = vertices[ triangles[i].getPoints()[2] ] - p0;
494                         int divs1=0, divs2=0;
495                         if(normNoSqrt(side1) > fsTri*fsTri) { divs1 = (int)(norm(side1)/fsTri); }
496                         if(normNoSqrt(side2) > fsTri*fsTri) { divs2 = (int)(norm(side2)/fsTri); }
497                         errMsg("ntlGeometryObject::initMovingPoints","tri:"<<i<<" p:"<<p0<<" s1:"<<side1<<" s2:"<<side2<<" -> "<<divs1<<","<<divs2 );
498                         if(divs1+divs2 > 0) {
499                                 for(int u=0; u<=divs1; u++) {
500                                         for(int v=0; v<=divs2; v++) {
501                                                 const gfxReal uf = (gfxReal)(u+TRI_UVOFFSET) / (gfxReal)(divs1+0.0);
502                                                 const gfxReal vf = (gfxReal)(v+TRI_UVOFFSET) / (gfxReal)(divs2+0.0);
503                                                 if(uf+vf>1.0) continue;
504                                                 countp+=2;
505                                         }
506                                 }
507                         }
508                 }
509                 errMsg("ntlGeometryObject::initMovingPoints","Object "<<getName()<<" requires:"<<countp*2);
510         } // */
511
512         bool discardInflowBack = false;
513         if( (mGeoInitType==FGI_MBNDINFLOW) && (mcInitialVelocity.accessValues().size()<1) ) discardInflowBack = true;
514         discardInflowBack = false; // DEBUG disable for now
515
516
517         // init std points
518         for(size_t i=0; i<vertices.size(); i++) {
519                 ntlVec3Gfx p = vertices[ i ];
520                 ntlVec3Gfx n = vnormals[ i ];
521                 // discard inflow backsides
522                 //if( (mGeoInitType==FGI_MBNDINFLOW) && (!mIsAnimated)) {
523                 if(discardInflowBack) { //if( (mGeoInitType==FGI_MBNDINFLOW) && (!mIsAnimated)) {
524                         if(dot(mInitialVelocity,n)<0.0) continue;
525                 }
526                 mMovPoints.push_back(p);
527                 mMovNormals.push_back(n);
528                 if(debugMoinit) errMsg("ntlGeometryObject::initMovingPoints","std"<<i<<" p"<<p<<" n"<<n<<" ");
529         }
530         // init points & refine...
531         for(size_t i=0; i<triangles.size(); i++) {
532                 int *trips = triangles[i].getPoints();
533                 const ntlVec3Gfx p0 = vertices[ trips[0] ];
534                 const ntlVec3Gfx side1 = vertices[ trips[1] ] - p0;
535                 const ntlVec3Gfx side2 = vertices[ trips[2] ] - p0;
536                 int divs1=mTriangleDivs1[i], divs2=mTriangleDivs2[i];
537                 
538                 const ntlVec3Gfx trinormOrg = getNormalized(cross(side1,side2));
539                 const ntlVec3Gfx trinorm = trinormOrg*0.25*featureSize;
540                 if(discardInflowBack) { 
541                         if(dot(mInitialVelocity,trinorm)<0.0) continue;
542                 }
543                 if(debugMoinit) errMsg("ntlGeometryObject::initMovingPoints","Tri1 "<<vertices[trips[0]]<<","<<vertices[trips[1]]<<","<<vertices[trips[2]]<<" "<<divs1<<","<<divs2 );
544                 if(divs1+divs2 > 0) {
545                         for(int u=0; u<=divs1; u++) {
546                                 for(int v=0; v<=divs2; v++) {
547                                         const gfxReal uf = (gfxReal)(u+TRI_UVOFFSET) / (gfxReal)(divs1+0.0);
548                                         const gfxReal vf = (gfxReal)(v+TRI_UVOFFSET) / (gfxReal)(divs2+0.0);
549                                         if(uf+vf>1.0) continue;
550                                         ntlVec3Gfx p = 
551                                                 vertices[ trips[0] ] * (1.0-uf-vf)+
552                                                 vertices[ trips[1] ] * uf +
553                                                 vertices[ trips[2] ] * vf;
554                                         //ntlVec3Gfx n = vnormals[ 
555                                                 //trips[0] ] * (1.0-uf-vf)+
556                                                 //vnormals[ trips[1] ]*uf +
557                                                 //vnormals[ trips[2] ]*vf;
558                                         //normalize(n);
559                                         // discard inflow backsides
560
561                                         mMovPoints.push_back(p + trinorm); // NEW!?
562                                         mMovPoints.push_back(p - trinorm);
563                                         mMovNormals.push_back(trinormOrg);
564                                         mMovNormals.push_back(trinormOrg); 
565                                         //errMsg("TRINORM","p"<<p<<" n"<<n<<" trin"<<trinorm);
566                                 }
567                         }
568                 }
569         }
570
571         // find max point
572         mMaxMovPnt = 0;
573         gfxReal dist = normNoSqrt(mMovPoints[0]);
574         for(size_t i=0; i<mMovPoints.size(); i++) {
575                 if(normNoSqrt(mMovPoints[i])>dist) {
576                         mMaxMovPnt = i;
577                         dist = normNoSqrt(mMovPoints[0]);
578                 }
579         }
580
581         if(    (this->getMeshAnimated())
582       || (mcTrans.accessValues().size()>1)  // VALIDATE
583             || (mcRot.accessValues().size()>1) 
584             || (mcScale.accessValues().size()>1) 
585                 ) {
586                 // also do trafo...
587         } else {
588                 mCachedMovPoints = mMovPoints;
589                 mCachedMovNormals = mMovNormals;
590                 //applyTransformation(time, &mCachedMovPoints, &mCachedMovNormals, 0, mCachedMovPoints.size(), true);
591                 applyTransformation(time, &mCachedMovPoints, NULL, 0, mCachedMovPoints.size(), true);
592                 mHaveCachedMov = true;
593                 debMsgStd("ntlGeometryObject::initMovingPoints",DM_MSG,"Object "<<getName()<<" cached points ", 7);
594         }
595
596         mMovPntsInited = featureSize;
597         debMsgStd("ntlGeometryObject::initMovingPoints",DM_MSG,"Object "<<getName()<<" inited v:"<<vertices.size()<<"->"<<mMovPoints.size() , 5);
598 }
599 /*! Prepare points for animated objects,
600  * init both sets, never used cached points 
601  * discardInflowBack ignore */
602 void ntlGeometryObject::initMovingPointsAnim(
603                  double srctime, vector<ntlVec3Gfx> &srcmovPoints,
604                  double dsttime, vector<ntlVec3Gfx> &dstmovPoints,
605                  vector<ntlVec3Gfx> *dstmovNormals,
606                  gfxReal featureSize,
607                  ntlVec3Gfx geostart, ntlVec3Gfx geoend
608                  ) {
609         const bool debugMoinit=false;
610
611         vector<ntlTriangle> srctriangles; 
612         vector<ntlVec3Gfx> srcvertices; 
613         vector<ntlVec3Gfx> unused_normals; 
614         vector<ntlTriangle> dsttriangles; 
615         vector<ntlVec3Gfx> dstvertices; 
616         vector<ntlVec3Gfx> dstnormals; 
617         int objectId = 1;
618         // TODO optimize? , get rid of normals?
619         unused_normals.clear();
620         this->getTriangles(srctime, &srctriangles,&srcvertices,&unused_normals,objectId);
621         unused_normals.clear();
622         this->getTriangles(dsttime, &dsttriangles,&dstvertices,&dstnormals,objectId);
623         
624         srcmovPoints.clear();
625         dstmovPoints.clear();
626         if(debugMoinit) errMsg("ntlGeometryObject::initMovingPointsAnim","Object "<<getName()<<" has srcv:"<<srcvertices.size()<<" srct:"<<srctriangles.size() );
627         if(debugMoinit) errMsg("ntlGeometryObject::initMovingPointsAnim","Object "<<getName()<<" has dstv:"<<dstvertices.size()<<" dstt:"<<dsttriangles.size() );
628         // no points?
629         if(srcvertices.size()<1) {
630                 mMaxMovPnt=-1;
631                 return; 
632         }
633         if((srctriangles.size() != dsttriangles.size()) ||
634            (srcvertices.size() != dstvertices.size()) ) {
635                 errMsg("ntlGeometryObject::initMovingPointsAnim","Invalid triangle numbers! Aborting...");
636                 return;
637         }
638         ntlVec3f maxscale = channelFindMaxVf(mcScale);
639         float maxpart = ABS(maxscale[0]);
640         if(ABS(maxscale[1])>maxpart) maxpart = ABS(maxscale[1]);
641         if(ABS(maxscale[2])>maxpart) maxpart = ABS(maxscale[2]);
642         float scaleFac = 1.0/(maxpart);
643         // TODO - better reinit from time to time?
644         const gfxReal fsTri = featureSize*0.5 *scaleFac;
645         if(debugMoinit) errMsg("ntlGeometryObject::initMovingPointsAnim","maxscale:"<<maxpart<<" featureSize:"<<featureSize<<" fsTri:"<<fsTri );
646
647         if(mTriangleDivs1.size()!=srctriangles.size()) {
648                 calcTriangleDivs(srcvertices,srctriangles,fsTri);
649         }
650
651
652         // init std points
653         for(size_t i=0; i<srcvertices.size(); i++) {
654                 srcmovPoints.push_back(srcvertices[i]);
655                 //srcmovNormals.push_back(srcnormals[i]);
656         }
657         for(size_t i=0; i<dstvertices.size(); i++) {
658                 dstmovPoints.push_back(dstvertices[i]);
659                 if(dstmovNormals) (*dstmovNormals).push_back(dstnormals[i]);
660         }
661         if(debugMoinit) errMsg("ntlGeometryObject::initMovingPointsAnim","stats src:"<<srcmovPoints.size()<<" dst:"<<dstmovPoints.size()<<" " );
662         // init points & refine...
663         for(size_t i=0; i<srctriangles.size(); i++) {
664                 const int divs1=mTriangleDivs1[i];
665                 const int       divs2=mTriangleDivs2[i];
666                 if(divs1+divs2 > 0) {
667                         int *srctrips = srctriangles[i].getPoints();
668                         int *dsttrips = dsttriangles[i].getPoints();
669                         const ntlVec3Gfx srcp0 =    srcvertices[ srctrips[0] ];
670                         const ntlVec3Gfx srcside1 = srcvertices[ srctrips[1] ] - srcp0;
671                         const ntlVec3Gfx srcside2 = srcvertices[ srctrips[2] ] - srcp0;
672                         const ntlVec3Gfx dstp0 =    dstvertices[ dsttrips[0] ];
673                         const ntlVec3Gfx dstside1 = dstvertices[ dsttrips[1] ] - dstp0;
674                         const ntlVec3Gfx dstside2 = dstvertices[ dsttrips[2] ] - dstp0;
675                         const ntlVec3Gfx src_trinorm    = getNormalized(cross(srcside1,srcside2))*0.25*featureSize;
676                         const ntlVec3Gfx dst_trinormOrg = getNormalized(cross(dstside1,dstside2));
677                         const ntlVec3Gfx dst_trinorm    = dst_trinormOrg                         *0.25*featureSize;
678                         //errMsg("ntlGeometryObject::initMovingPointsAnim","Tri1 "<<srcvertices[srctrips[0]]<<","<<srcvertices[srctrips[1]]<<","<<srcvertices[srctrips[2]]<<" "<<divs1<<","<<divs2 );
679                         for(int u=0; u<=divs1; u++) {
680                                 for(int v=0; v<=divs2; v++) {
681                                         const gfxReal uf = (gfxReal)(u+TRI_UVOFFSET) / (gfxReal)(divs1+0.0);
682                                         const gfxReal vf = (gfxReal)(v+TRI_UVOFFSET) / (gfxReal)(divs2+0.0);
683                                         if(uf+vf>1.0) continue;
684                                         ntlVec3Gfx srcp = 
685                                                 srcvertices[ srctrips[0] ] * (1.0-uf-vf)+
686                                                 srcvertices[ srctrips[1] ] * uf +
687                                                 srcvertices[ srctrips[2] ] * vf;
688                                         ntlVec3Gfx dstp = 
689                                                 dstvertices[ dsttrips[0] ] * (1.0-uf-vf)+
690                                                 dstvertices[ dsttrips[1] ] * uf +
691                                                 dstvertices[ dsttrips[2] ] * vf;
692
693                                         // cutoffDomain
694                                         if((srcp[0]<geostart[0]) && (dstp[0]<geostart[0])) continue;
695                                         if((srcp[1]<geostart[1]) && (dstp[1]<geostart[1])) continue;
696                                         if((srcp[2]<geostart[2]) && (dstp[2]<geostart[2])) continue;
697                                         if((srcp[0]>geoend[0]  ) && (dstp[0]>geoend[0]  )) continue;
698                                         if((srcp[1]>geoend[1]  ) && (dstp[1]>geoend[1]  )) continue;
699                                         if((srcp[2]>geoend[2]  ) && (dstp[2]>geoend[2]  )) continue;
700                                         
701                                         srcmovPoints.push_back(srcp+src_trinorm); // SURFENHTEST
702                                         srcmovPoints.push_back(srcp-src_trinorm);
703
704                                         dstmovPoints.push_back(dstp+dst_trinorm); // SURFENHTEST
705                                         dstmovPoints.push_back(dstp-dst_trinorm);
706                                         if(dstmovNormals) {
707                                                 (*dstmovNormals).push_back(dst_trinormOrg);
708                                                 (*dstmovNormals).push_back(dst_trinormOrg); }
709                                 }
710                         }
711                 }
712         }
713
714         // find max point not necessary
715         debMsgStd("ntlGeometryObject::initMovingPointsAnim",DM_MSG,"Object "<<getName()<<" inited v:"<<srcvertices.size()<<"->"<<srcmovPoints.size()<<","<<dstmovPoints.size() , 5);
716 }
717
718 /*! Prepare points for moving objects */
719 void ntlGeometryObject::getMovingPoints(vector<ntlVec3Gfx> &ret, vector<ntlVec3Gfx> *norms) {
720         if(mHaveCachedMov) {
721                 ret = mCachedMovPoints;
722                 if(norms) { 
723                         *norms = mCachedMovNormals; 
724                         //errMsg("ntlGeometryObject","getMovingPoints - Normals currently unused!");
725                 }
726                 //errMsg ("ntlGeometryObject::getMovingPoints","Object "<<getName()<<" used cached points "); // DEBUG
727                 return;
728         }
729
730         ret = mMovPoints;
731         if(norms) { 
732                 //errMsg("ntlGeometryObject","getMovingPoints - Normals currently unused!");
733                 *norms = mMovNormals; 
734         }
735 }
736
737
738 /*! Calculate max. velocity on object from t1 to t2 */
739 ntlVec3Gfx ntlGeometryObject::calculateMaxVel(double t1, double t2) {
740         ntlVec3Gfx vel(0.);
741         if(mMaxMovPnt<0) return vel;
742                 
743         vector<ntlVec3Gfx> verts1,verts2;
744         verts1.push_back(mMovPoints[mMaxMovPnt]);
745         verts2 = verts1;
746         applyTransformation(t1,&verts1,NULL, 0,verts1.size(), true);
747         applyTransformation(t2,&verts2,NULL, 0,verts2.size(), true);
748
749         vel = (verts2[0]-verts1[0]); // /(t2-t1);
750         //errMsg("ntlGeometryObject::calculateMaxVel","t1="<<t1<<" t2="<<t2<<" p1="<<verts1[0]<<" p2="<<verts2[0]<<" v="<<vel);
751         return vel;
752 }
753
754 /*! get translation at time t*/
755 ntlVec3Gfx ntlGeometryObject::getTranslation(double t) {
756         ntlVec3Gfx pos = mcTrans.get(t);
757   // DEBUG CP_FORCECIRCLEINIT 1
758         /*if( 
759                         (mName.compare(string("0__ts1"))==0) ||
760                         (mName.compare(string("1__ts1"))==0) ||
761                         (mName.compare(string("2__ts1"))==0) ||
762                         (mName.compare(string("3__ts1"))==0) ||
763                         (mName.compare(string("4__ts1"))==0) ||
764                         (mName.compare(string("5__ts1"))==0) ||
765                         (mName.compare(string("6__ts1"))==0) ||
766                         (mName.compare(string("7__ts1"))==0) ||
767                         (mName.compare(string("8__ts1"))==0) ||
768                         (mName.compare(string("9__ts1"))==0) 
769                         ) { int j=mName[0]-'0';
770                         ntlVec3Gfx ppos(0.); { // DEBUG
771                         const float tscale=10.;
772                         const float tprevo = 0.33;
773                         const ntlVec3Gfx   toff(50,50,0);
774                         const ntlVec3Gfx oscale(30,30,0);
775                         ppos[0] =  cos(tscale* t - tprevo*(float)j + M_PI -0.1) * oscale[0] + toff[0];
776                         ppos[1] = -sin(tscale* t - tprevo*(float)j + M_PI -0.1) * oscale[1] + toff[1];
777                         ppos[2] =                               toff[2]; } // DEBUG
778                         pos = ppos;
779                         pos[2] = 0.15;
780         }
781   // DEBUG CP_FORCECIRCLEINIT 1 */
782         return pos;
783 }
784 /*! get active flag time t*/
785 float ntlGeometryObject::getGeoActive(double t) {
786         float act = (mcGeoActive.get(t) >= 1.) ? 1.0 : 0.0; 
787         return act;
788 }
789
790 void ntlGeometryObject::setInitialVelocity(ntlVec3Gfx set) { 
791         mInitialVelocity=set; 
792         mcInitialVelocity = AnimChannel<ntlVec3Gfx>(set); 
793 }
794 ntlVec3Gfx ntlGeometryObject::getInitialVelocity(double t) { 
795         ntlVec3Gfx v =  mcInitialVelocity.get(t); //return mInitialVelocity; 
796         if(!mLocalCoordInivel) return v;
797
798         ntlVec3Gfx rot = mcRot.get(t);
799         ntlMat4Gfx rotMat;
800         rotMat.initRotationXYZ(rot[0],rot[1],rot[2]);
801         v = rotMat * v;
802         return v;
803 }
804         
805