Documentation


DeformableOctreeNode.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           deformableOctreeNode.cpp  -  description
00003                              -------------------
00004     begin                : Tue Mar 23 2004
00005     copyright            : (C) 2004 by Mathieu Coquerelle
00006     email                : mathieu.coquerelle@imag.fr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "DeformableOctreeNode.h"
00019 #include "OctreeODEEngine.h"
00020 
00021 #include <X3DTK/kernel.h>
00022 #include <X3DTK/private/X3D_Coordinate.h>
00023 
00024 #include <GL/glut.h>
00025 #include "ConstrainedVertex.h"
00026 #include "SFVec3fCellConstrained.h"
00027 
00028 #include "assertions.h"
00029 #include <animal/X3DTK/Qt/mainControllerGUI/MainController.h>
00030 
00031 #include "DeformableOctreeShapeNode.h"
00032 
00033 #include "ManipulatedMeshVertex.h"
00034 
00035 #include <X3DTK/private/SFVec3f.h>
00036 #include <animal/picking.h>
00037 #include <animal/X3DTK/linearX3DTK.h>
00038 
00039 #include <iostream>
00040 using std::cerr;
00041 using std::endl;
00042 
00043 
00044 
00045 
00046 
00047 namespace X3DTK
00048 {
00049 
00050   namespace X3D
00051   {
00052 
00056     void drawLeaf( Cell *cell );
00057     void drawCell( Cell *cell );
00058 
00059 
00060     DeformableOctreeNode::DeformableOctreeNode() :
00061         _dosn(NULL)
00062     {
00063       // Defines the tag of the node. This string must be the same than the one
00064       // entered for the creation function, otherwise the node won't be loaded.
00065       // Node
00066       define(Recorder<DeformableOctreeNode>::getTypeName("DeformableOctreeNode"));
00067       // Attributes
00068       define(Recorder<DeformableOctreeNode, SFString>::getAttribute("engine", &DeformableOctreeNode::_engineType, SFString()));
00069 
00070       _octree = NULL;
00071       _selectedCell = NULL;
00072       _selectedVertexId = 0;
00073       _selectionId = 0;
00074 
00075       initOptions();
00076 
00077     }
00078 
00079     DeformableOctreeNode::~DeformableOctreeNode()
00080     {
00081       //removeParentFromChild(this, _coordinate);
00082       delete _octree;
00083     }
00084 
00085     void DeformableOctreeNode::declareOutputs( X3D_X3DNodeList& )
00086     {}
00087 
00088     void DeformableOctreeNode::animate( float dt )
00089     {
00090       //assert( _octree );
00091       if( animal::octree::OctreeEngine* engine = dynamic_cast<animal::octree::OctreeEngine*>(_octree) )
00092       {
00093         engine->move(dt);
00094       }
00095     }
00096 
00097     void DeformableOctreeNode::postAnimate( float dt )
00098     {
00099       if( animal::octree::OctreeEngine* engine = dynamic_cast<animal::octree::OctreeEngine*>(_octree) )
00100       {
00101         engine->postMove(dt);
00102       }
00103     }
00104 
00106     void DeformableOctreeNode::setDeformableOctreeShapeNode( DeformableOctreeShapeNode *dosn)
00107     {
00108       _dosn = dosn;
00109       createOctree();
00110       if( _octree )
00111       {
00112         _dosn->setOctree( _octree );
00113       }
00114     }
00115 
00116     void DeformableOctreeNode::getBoundingBox( float & minX, float & minY, float & minZ, float & maxX, float & maxY, float & maxZ)
00117     {
00118       if( _octree != NULL )
00119       {
00120         Cell *cell = _octree->root();
00121         Vec3d min = cell->vertex(0)->getPosition();
00122         Vec3d max = min;
00123         for( unsigned short i=1 ; i<8 ; ++i )
00124         {
00125           Vec3d pos = cell->vertex(i)->getPosition();
00126 
00127           if( pos[0] < min[0] )
00128             min[0] = pos[0];
00129           if( pos[0] > max[0] )
00130             max[0] = pos[0];
00131 
00132           if( pos[1] < min[1] )
00133             min[1] = pos[1];
00134           if( pos[1] > max[1] )
00135             max[1] = pos[1];
00136 
00137           if( pos[2] < min[2] )
00138             min[2] = pos[2];
00139           if( pos[2] > max[2] )
00140             max[2] = pos[2];
00141         }
00142         minX = min[0];
00143         minY = min[1];
00144         minZ = min[2];
00145         maxX = max[0];
00146         maxY = max[1];
00147         maxZ = max[2];
00148       }
00149 
00150     }
00151 
00152     void DeformableOctreeNode::updateBoundingBox()
00153     {
00154       float xmin, ymin, zmin, xmax, ymax, zmax;
00155       getBoundingBox(xmin, ymin, zmin, xmax, ymax, zmax);
00156       setBBoxCenter( SFVec3f( (xmin+xmax)/2, (ymin+ymax)/2, (zmin+zmax)/2 ) );
00157       setBBoxSize( SFVec3f( (xmax-xmin), (ymax-ymin), (zmax-zmin) ) );
00158     }
00159 
00160 
00161 
00162 
00163 
00164 
00165 
00166 
00167     void DeformableOctreeNode::createOctree()
00168     {
00169       std::cerr << "DeformableOctreeNode::createOctree()\n";
00170 
00171       X3D::Coordinate * coord = _dosn->getCoordinate();
00172 
00173       // get the coordinates
00174       /*
00175       std::cerr << "My shape " << _x3dshape->getTypeName() << "\n";
00176       MFNode childList = _x3dshape->getChildList();
00177       for( MFNode::iterator it=childList.begin() ; it != childList.end() ; ++it )
00178       {
00179         std::cerr << "My child is " << (*it)->getTypeName() << "\n";
00180         if( dynamic_cast<Coordinate *>(*it) != 0 )
00181         {
00182             // Should be a class Coordinate
00183             coord = static_cast<Coordinate*> (*it);
00184         }
00185       }
00186       */
00187 
00188       if (!coord)
00189       {
00190         std::cerr<< "Warning: impossible to init the octree. In the file .x3d, there is no Coordinate child of the ShapeNode."<< std::endl;
00191         return;
00192       }
00193       if ( coord->getPoint().size() == 0)
00194       {
00195         std::cerr << "Warning: no vertex in the mesh !\n";
00196         return;
00197       }
00198 
00199 
00200 
00201       //std::cerr << "createOctree() : there are " << coord->getPoint().size() << " vertices in the mesh\n";
00202       // Get the points
00203       const MFVec3f * pointsTmp = &(coord->getPoint());
00204       MFVec3f * points = const_cast<MFVec3f *>(pointsTmp);
00205 
00206       Vec3d minVec, maxVec;
00207       minVec.x = (*points)[0].x;
00208       maxVec.x = (*points)[0].x;
00209       minVec.y = (*points)[0].y;
00210       maxVec.y = (*points)[0].y;
00211       minVec.z = (*points)[0].z;
00212       maxVec.z = (*points)[0].z;
00213 
00214       for( unsigned int i=0 ; i< points->size() ; ++i )
00215       {
00216         SFVec3f p = (*points)[i];
00217 
00218         if( p.x < minVec.x )
00219           minVec.x = p.x;
00220         if( p.y < minVec.y )
00221           minVec.y = p.y;
00222         if( p.z < minVec.z )
00223           minVec.z = p.z;
00224 
00225         if( p.x > maxVec.x )
00226           maxVec.x = p.x;
00227         if( p.y > maxVec.y )
00228           maxVec.y = p.y;
00229         if( p.z > maxVec.z )
00230           maxVec.z = p.z;
00231       }
00232 
00233 
00234 
00235       Vec3d sizeVec = maxVec - minVec;
00236 
00237       // Give the octree a little larger BBox so that every points are SURE to be inside
00238       minVec -= sizeVec/100.0;
00239       maxVec += sizeVec/100.0;
00240 
00241       minVec.z -= 0.5;
00242       maxVec.z += 1;
00243 
00244       sizeVec = maxVec - minVec;
00245 
00246       MFVec3f normals = _dosn->computeNormals();
00247 
00248 
00249     if( _engineType == string("engine") )
00250     {
00251         cerr<<"DeformableOctreeNode::createOctree create a basic engine octree"<<endl;
00252         _octree = new OctreeEngine( minVec, maxVec, points, normals, 10000 );
00253     }
00254 //     else if( _engineType == string("physical") )
00255 //     {
00256 //         cerr<<"DeformableOctreeNode::createOctree create a physical octree"<<endl;
00257 //         _octree = new OctreeODEEngine( minVec, maxVec, points, normals, 10000 );
00258 //     }
00259     else
00260     {
00261         cerr<<"DeformableOctreeNode::createOctree create default octree"<<endl;
00262         _octree = new Octree( minVec, maxVec, points, normals, 10000 );
00263       }
00264       _selectedCell = _octree->root();
00265     }
00266 
00267     void DeformableOctreeNode::postInit()
00268     {
00269       // std::cerr << "DON::postInit\n";
00270       //glEnable( GL_COLOR_MATERIAL );
00271       //glColorMaterial( GL_FRONT_AND_BACK,  GL_SPECULAR );
00272 
00273       MFNode children = getChildList();
00274 
00275       for( MFNode::iterator it = children.begin() ; it != children.end() ; ++it )
00276       {
00277         std::cerr << "\tGot one child : " << (*it)->getName() << "\n";
00278 
00279         if( DeformableOctreeShapeNode *tmpNode =dynamic_cast<DeformableOctreeShapeNode *>(*it) )
00280         {
00281           Require( !_dosn );
00282 
00283           // anciennes verifications
00284           /*
00285           if(_dosn)
00286             return false;
00287           */
00288 
00289           // Find a Coordinate Node if any
00290           if( !tmpNode->getCoordinate() )
00291           {
00292             std::cerr << "DON::init : can't get coordinates from DOSN\n";
00293           }
00294 
00295           setDeformableOctreeShapeNode( tmpNode );
00296         }
00297       }
00298 
00299       if( animal::octree::OctreeEngine* engine = dynamic_cast<animal::octree::OctreeEngine*>(_octree) )
00300       {
00301         engine->init();
00302       }
00303 
00304 
00305 
00306     }
00307 
00308 
00309 
00310     void DeformableOctreeNode::draw()
00311     {
00312       //std::cerr << "Drawing octree\n";
00313 
00314       if( _dosn )
00315       {
00316         _dosn->draw();
00317 
00318         glPushAttrib( GL_ALL_ATTRIB_BITS );
00319 
00320         glDisable( GL_LIGHTING );
00321         glPolygonMode ( GL_FRONT_AND_BACK, GL_LINE );
00322         glEnable (GL_BLEND);
00323         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00324 
00325 
00326         if( _drawOctree )
00327         {
00328           drawCells( _octree->root() );
00329           drawOctreeVertices();
00330         }
00331 
00332 
00333         if( _drawSelection )
00334         {
00335           drawSelection();
00336         }
00337 
00338         if( _drawFrames )
00339         {
00340           drawFrames( getSelectedCell() );
00341         }
00342 
00343 
00344         if( getOptionDrawSelectedCellsPoints() )
00345         {
00346           drawCellsPoints( getSelectedCell() );
00347         }
00348 
00349         if( getOptionDrawNeighbours() )
00350         {
00351           drawNeighbours( getSelectedCell() );
00352         }
00353 
00354 
00355         glPopAttrib();
00356       }
00357 
00358     }
00359 
00360 
00361 
00362 
00363     void DeformableOctreeNode::drawNeighbours( Cell *cell )
00364     {
00365       std::cerr << "void DeformableOctreeNode::drawNeighbours( Cell *" << *cell << " )\n";
00366       Require( cell );
00367 
00368       glColor4f( 0.0, 0.0, 1.0, 0.2 );
00369       for( unsigned int i=0 ; i<6 ; ++i )
00370       {
00371         if( cell->getNeighbour(i) )
00372         {
00373           std::cerr << "Drawing neighbour for " << i << "\n";
00374           drawLeaf( cell->getNeighbour(i) );
00375         }
00376       }
00377     }
00378 
00379     void DeformableOctreeNode::drawOctreeVertices()
00380     {
00381       Cell::vertex_iterator it( _octree->root() );
00382 
00383       ConstrainedVertex *cv;
00384       while( !it.empty() )
00385       {
00386         cv = ++it;
00387         if( cv->isFree() )
00388         {
00389           glColor4f( 0.0, 1.0, 0.0, 1.0 );
00390         }
00391         else
00392         {
00393           glColor4f( 1.0, 0.0, 0.0, 1.0 );
00394         }
00395         drawVertex( cv, this->getToolsSize(), 10 );
00396       }
00397     }
00398 
00399     void DeformableOctreeNode::drawVertex( OctreeVertex *vertex, float size, unsigned int details )
00400     {
00401       glPushMatrix();
00402       Vec3d v = vertex->getPosition();
00403 
00404       glTranslatef( v[0], v[1], v[2] );
00405       glutSolidSphere( size, details, details );
00406 
00407       glPopMatrix();
00408     }
00409 
00410 
00411     void DeformableOctreeNode::drawCells( Cell *cell )
00412     {
00413       for( Octree::dfs_iterator it( cell, isLeaf, NULL, drawCell ) ; !it.empty() ; ++it )
00414         ;
00415     }
00416 
00417 
00418 
00419 
00420 
00421     void DeformableOctreeNode::drawSelection()
00422     {
00423       //
00424       // Some fuzzy stuff
00425       //
00426       static float minColor = 0.0;
00427       static float maxColor = 0.5;
00428       static float curColor = 0.0;
00429       static float stepColor = 0.015;
00430 
00431       glLineWidth( 5 );
00432       glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
00433       glColor4f( 1.0 , 1.0, 1.0, curColor );
00434       drawLeaf( getSelectedCell() );
00435 
00436       glEnable( GL_CULL_FACE );
00437       glColor4f( 1.0, 1.0, 1.0, curColor+maxColor );
00438       drawVertex( getSelectedVertex(), getToolsSize(), 30 );
00439       glDisable( GL_CULL_FACE );
00440 
00441       if( curColor >= maxColor )
00442       {
00443         curColor = maxColor;
00444         stepColor *= -1;
00445         curColor += stepColor;
00446       }
00447       else if( curColor <= minColor )
00448       {
00449         curColor = minColor;
00450         stepColor *= -1;
00451         curColor += stepColor;
00452       }
00453       else
00454       {
00455         curColor += stepColor;
00456       }
00457 
00458       //CellInfluenceData cid = getSelectedCell()->getData()._influence;
00459       /*
00460       // Then draw the vertices influencing this vertex
00461       for( unsigned int i=0 ; i<cid.influenceMaps.size() ; ++i )
00462       {
00463         Vec3d w = cid.influenceMaps[i][getSelectedVertex()];
00464         Vec3d pos = cid.vertices[i]->getFrame().getOrigin();
00465         
00466         glPushMatrix();
00467         glTranslatef( pos.x, pos.y, pos.z );
00468         glColor4f( 1.0, 0.0, 0.0, 1.0 );
00469         //glScalef( w[0], w[1], w[2] );
00470         //glutSolidSphere( 1.0, 10, 10 );
00471         glutSolidSphere( (1.0-w[0])*(1.0-w[1])*(1.0-w[2])/3.0, 10, 10 );
00472         glPopMatrix();      
00473       }
00474       */
00475       /*
00476        // Show the vertices and influences depending on the current vertex
00477       for( TempOctree::dfs_iterator it( _octree->root() ) ; !it.empty() ;)
00478       {
00479         Cell * cell = ++it;
00480         
00481         if( cell->isLeaf() )
00482         {
00483             CellInfluenceData cid = cell->getData()._influence;
00484 
00485             int vertexId = -1;
00486             for( unsigned int i=0 ; i<cid.vertices.size() ; ++i )
00487             {
00488                 if( cid.vertices[i] == getSelectedVertex() )
00489                 {
00490                     vertexId = i;
00491                 }
00492             }
00493 
00494             if( vertexId != -1 )
00495             {
00496                 for( unsigned int i=0 ; i<8 ; ++i )
00497                 {
00498                     //std::cerr << "\t++it";
00499                     ConstrainedVertex *cv = cell->vertex(i);
00500 
00501                     if( cid.influenceMaps[vertexId].find(cv) != cid.influenceMaps[vertexId].end() )
00502                     {
00503                         //std::cerr << " : ok got one";
00504                         Vec3d w = cid.influenceMaps[vertexId][cv];
00505                         Vec3d pos = cv->getPosition();
00506 
00507                         glEnable( GL_LIGHTING );
00508                         glPushMatrix();
00509                         glTranslatef( pos.x, pos.y, pos.z );
00510                         glColor4f( 0.0, 1.0, 0.0, 1.0 );
00511                         glScalef( 2.0-w[0], 2.0-w[1], 2.0-w[2] );
00512                         glutSolidSphere( 1.0/10.0, 20, 20 );
00513                         //glutSolidSphere( (1.0-w[0])*(1.0-w[1])*(1.0-w[2])/3.0, 10, 10 );
00514                         glPopMatrix();      
00515                     }
00516                     //std::cerr << "\n";
00517 
00518                 }
00519         //std::cerr << "end\n";     
00520             }
00521         }
00522       }
00523       */
00524 
00525     }
00526 
00527     /*
00528     void DeformableOctreeNode::drawFrame( Cell *cell, unsigned int vId )
00529     {
00530         
00531         Frame f = cell->vertex(vId)->getFrame();
00532         Vec3d v = f.getOrigin();
00533                 
00534         
00535         glColor4f( 0.0, 1.0, 1.0, 1.0 );
00536         drawVector(v,v+f.getVector(0));
00537      
00538         // y vectors
00539         glColor4f( 1.0, 0.0, 1.0, 1.0 );
00540         drawVector(v,v+f.getVector(1));
00541         
00542         // z vectors
00543         glColor4f( 1.0, 1.0, 0.0, 1.0 );
00544         drawVector(v,v+f.getVector(2));
00545     }
00546     */
00547 
00548 
00549     void DeformableOctreeNode::drawFrame( Frame f )
00550     {
00551       Vec3d v = f.getOrigin();
00552 
00553       glColor4f( 0.0, 1.0, 1.0, 1.0 );
00554       drawVector(v,v+f.getVector(0));
00555 
00556       // y vectors
00557       glColor4f( 1.0, 0.0, 1.0, 1.0 );
00558       drawVector(v,v+f.getVector(1));
00559 
00560       // z vectors
00561       glColor4f( 1.0, 1.0, 0.0, 1.0 );
00562       drawVector(v,v+f.getVector(2));
00563     }
00564 
00565 
00566     void DeformableOctreeNode::drawFrames( Cell *cell )
00567     {
00568       for( unsigned int i=0 ; i<8 ; ++i )
00569       {
00570         drawFrame( cell->vertex(i)->getFrame() );
00571       }
00572     }
00573 
00574     void DeformableOctreeNode::drawVector( Vec3d p1, Vec3d p2 )
00575     {
00576       glLineWidth(10);
00577       glBegin( GL_LINES );
00578       glVertex3fv( p1 );
00579       glVertex3fv( p2 );
00580       glEnd();
00581       glLineWidth(1);
00582     }
00583 
00584 
00585 
00586 
00587 
00588     void DeformableOctreeNode::drawCellsPoints( Cell *cell )
00589     {
00590 
00591       if( cell->isLeaf() )
00592       {
00593         //
00594         // This was used to shaw some debug information
00595         //
00596         if( (cell == getSelectedCell()) && cell->getData()._points.size() != 0 )
00597         {
00598           SFVec3fCellConstrained *point = cell->getData()._points[ _selectionId%(cell->getData()._points.size()) ];
00599           SFVec3f v = *(point->getSFVec3f());
00600 
00601           glPointSize( 10 );
00602           glColor4f( 1.0, 0.0, 0.0, 1.0 );
00603 
00604           glBegin( GL_POINTS );
00605           glVertex3f( v.x, v.y, v.z );
00606           glEnd();
00607 
00608 
00609 
00610           CellInfluenceData cid = cell->getData()._influence;
00611 
00612 
00613           for( unsigned int i=0 ; i<cid.vertices.size() ; ++i )
00614           {
00615             Vec3d pos = cid.vertices[i]->getFrame().computePosition( point->getRelativePosition(cid.vertices[i]) );
00616 
00617             glColor4f( (i+1)/(float)cid.vertices.size(), 1.0-(i+1)/(float)cid.vertices.size(), 1.0, 1.0 );
00618             glPushMatrix();
00619             glTranslatef( pos.x, pos.y, pos.z );
00620             glutSolidSphere( point->getRelativeWeight( cid.vertices[i] ), 10, 10 );
00621             glPopMatrix();
00622 
00623             drawVector( cid.vertices[i]->getPosition(), pos );
00624             drawFrame( cid.vertices[i]->getFrame() );
00625           }
00626 
00627           /*
00628           //std::cerr << "There are " << cid.vertices.size() << " vertices\n";
00629           FloatingPointType sumW = 0.0;
00630           for( unsigned int i=0 ; i<cid.vertices.size() ; ++i )
00631           {
00632             sumW += point->getRelativeWeight( cid.vertices[i] );
00633           }
00634 
00635           // Scaling factor...
00636           sumW *= 3;
00637                     
00638           for( unsigned int i=0 ; i<cid.vertices.size() ; ++i )
00639           {
00640             drawFrame( cid.vertices[i]->getFrame() );
00641             
00642             Vec3d pos = cid.vertices[i]->getFrame().computePosition( point->getRelativePosition(cid.vertices[i]) );
00643             
00644             glColor4f( 0.0, 1.0, 1.0, 1.0 );
00645             glBegin( GL_POINTS );
00646             glVertex3f( pos.x, pos.y, pos.z );
00647             glEnd();
00648             
00649             pos = cid.vertices[i]->getFrame().getOrigin();
00650             glPushMatrix();
00651             glTranslatef( pos.x, pos.y, pos.z );
00652             glColor4f( 0.0, 0.0, 1.0, 1.0 );
00653             glutSolidSphere( point->getRelativeWeight( cid.vertices[i] )/sumW, 10, 10 );
00654             glPopMatrix();
00655           } 
00656 
00657           Vec3d vRes;
00658           FloatingPointType wSum = 0.0;
00659           for( unsigned int i=0 ; i<cid.vertices.size() ; ++i )
00660           {
00661             //vRes += weights[i] * Ps[i];
00662             //wSum += weights[i];
00663             
00664             FloatingPointType w = getParameter( cid.vertices[i] );
00665             //std::cerr << "W is " << w << "\n";
00666             vRes += Ps[i] * w;
00667             wSum += w;
00668             
00669           }
00670 
00671           */
00672         }
00673 
00674         OctreeDataPoints *points = &(cell->getData()._points);
00675 
00676         glPointSize( 5 );
00677         glColor4f( 1.0, 0.0, 0.0, 1.0 );
00678         glBegin( GL_POINTS );
00679         for( OctreeDataPoints::iterator it = points->begin() ; it != points->end() ; ++it )
00680         {
00681           SFVec3f v = *(*it)->getSFVec3f();
00682           glVertex3f( v.x, v.y, v.z );
00683         }
00684 
00685         glEnd();
00686       }
00687       else
00688       {
00689         for( unsigned int i=0 ; i<8 ; ++i )
00690           drawCellsPoints( cell->child(i) );
00691       }
00692     }
00693 
00694 
00695 
00696 
00697 
00698 
00699 
00700     void testEnterCell(Cell* cell)
00701     {
00702       for( unsigned int i=0 ; i<cell->getData()._depth ; ++i )
00703       {
00704         fputc( '\t', stderr );
00705       }
00706       std::cerr << "entering cell : " << *cell << " (depth = " << cell->getData()._depth << ")\n";
00707     }
00708 
00709     void testLeaveCell(Cell* cell)
00710     {
00711       for( unsigned int i=0 ; i<cell->getData()._depth ; ++i )
00712       {
00713         fputc( '\t', stderr );
00714       }
00715       std::cerr << "leaving cell : " << *cell << " (depth = " << cell->getData()._depth << "\n";
00716     }
00717 
00718 
00719 
00720     void DeformableOctreeNode::slotButtonTest()
00721     {
00722       _selectionId++;
00723       /*
00724         
00725         std::cerr << "DeformableOctreeNode::slotButtonTest() : exiting\n";
00726         exit(0);
00727         std::cerr << "Test button\n";
00728         if( _octree->root()->isLeaf() )
00729         {
00730             std::cerr << "isLeaf()\n";
00731         }
00732         TempOctree::dfs_iterator it( _octree->root(), NULL, testEnterCell, testLeaveCell );
00733         while(!it.empty())
00734         {
00735             ++it;
00736         }
00737         std::cerr << "Test button end\n";
00738         */
00739     }
00740 
00741 
00742 
00743 
00744 
00745     void DeformableOctreeNode::slotSubdivide()
00746     {
00747       _octree->subdivideRecDeformed( getSelectedCell() );
00748       updateGL();
00749     }
00750     void DeformableOctreeNode::slotSimplify()
00751     {
00752       _octree->simplify(getSelectedCell());
00753       updateGL();
00754     }
00755 
00756 
00757     void DeformableOctreeNode::slotMoveXPlus()
00758     {
00759       Vec3d v = getSelectedVertex()->getPosition();
00760       v[0] += _toolsSize;
00761       getSelectedVertex()->setPositionAndPropagateAll( v, getSelectedCell()->getData()._depth );
00762       //getSelectedVertex()->moveIndependent( v );
00763 
00764       _octree->movedRecLeaves(_octree->root() );
00765 
00766       updateGL();
00767     }
00768     void DeformableOctreeNode::slotMoveXMinus()
00769     {
00770       Vec3d v = getSelectedVertex()->getPosition();
00771       v[0] -= _toolsSize;
00772       getSelectedVertex()->setPositionAndPropagateAll
00773       ( v, getSelectedCell()->getData()._depth );
00774 
00775       _octree->movedRecLeaves(_octree->root() );
00776 
00777       updateGL();
00778     }
00779 
00780 
00781     void DeformableOctreeNode::slotMoveYPlus()
00782     {
00783       Vec3d v = getSelectedVertex()->getPosition();
00784       v[1] += _toolsSize;
00785       getSelectedVertex()->setPositionAndPropagateAll
00786       ( v, getSelectedCell()->getData()._depth );
00787 
00788       _octree->movedRecLeaves(_octree->root() );
00789 
00790       updateGL();
00791     }
00792     void DeformableOctreeNode::slotMoveYMinus()
00793     {
00794       Vec3d v = getSelectedVertex()->getPosition();
00795       v[1] -= _toolsSize;
00796       getSelectedVertex()->setPositionAndPropagateAll
00797       ( v, getSelectedCell()->getData()._depth );
00798 
00799       _octree->movedRecLeaves(_octree->root() );
00800 
00801       updateGL();
00802     }
00803     void DeformableOctreeNode::slotMoveZPlus()
00804     {
00805       Vec3d v = getSelectedVertex()->getPosition();
00806       v[2] += _toolsSize;
00807       getSelectedVertex()->setPositionAndPropagateAll
00808       ( v, getSelectedCell()->getData()._depth );
00809 
00810       _octree->movedRecLeaves(_octree->root() );
00811 
00812       updateGL();
00813     }
00814     void DeformableOctreeNode::slotMoveZMinus()
00815     {
00816       Vec3d v = getSelectedVertex()->getPosition();
00817       v[2] -= _toolsSize;
00818       getSelectedVertex()->setPositionAndPropagateAll
00819       ( v, getSelectedCell()->getData()._depth );
00820 
00821       _octree->movedRecLeaves(_octree->root() );
00822 
00823       updateGL();
00824     }
00825 
00826 
00827     void DeformableOctreeNode::slotCycleCell()
00828     {
00829       if( !_selectedCell->isRoot() )
00830         // Has a father, not root cell
00831       {
00832         _selectedCell = _selectedCell->father()->child( (_selectedCell->fatherPos()+1)%8 );
00833         std::cerr << "This cell (" << *(getSelectedCell()) << ")  has " << getSelectedCell()->getData()._points.size() << " points\n";
00834         updateGL();
00835       }
00836     }
00837 
00838     void DeformableOctreeNode::slotCyclePoint()
00839     {
00840       _selectedVertexId = (_selectedVertexId+1)%8;
00841       std::cerr << "\tHaving vertex " << _selectedVertexId << "\n";
00842       updateGL();
00843     }
00844 
00845     void DeformableOctreeNode::slotGoFather()
00846     {
00847       if( !_selectedCell->isRoot() )
00848       {
00849         _selectedCell = _selectedCell->father();
00850         std::cerr << "This cell (" << *(getSelectedCell()) << ") has " << getSelectedCell()->getData()._points.size() << " points\n";
00851         updateGL();
00852       }
00853     }
00854 
00855     void DeformableOctreeNode::slotGoChild()
00856     {
00857       if( !_selectedCell->isLeaf() )
00858       {
00859         _selectedCell = _selectedCell->child(0);
00860         std::cerr << "This cell (" << *(getSelectedCell()) << ")  has " << getSelectedCell()->getData()._points.size() << " points\n";
00861         updateGL();
00862       }
00863     }
00864 
00865     void DeformableOctreeNode::updateGL()
00866     {
00867       //getMainController()->getViewer()->updateGL();
00868       //            getMainController()->display();
00869       //          this->draw();
00870       //          _coordinate->draw();
00871     }
00872 
00873 
00874     OctreeVertex* DeformableOctreeNode::getSelectedVertex()
00875     {
00876       return _selectedCell->vertex(_selectedVertexId);
00877     }
00878     Cell* DeformableOctreeNode::getSelectedCell()
00879     {
00880       return _selectedCell;
00881     }
00882 
00883 
00884 
00885 
00886 
00887 
00888 
00889     void DeformableOctreeNode::initOptions()
00890     {
00891       _drawEmptyCells = false;
00892       _drawOctree = false;
00893       _drawSelection = false;
00894       _drawSelectedCellsPoints = false;
00895       _drawFrames = false;
00896       _drawNeighbours = false;
00897     }
00898 
00899     // Options
00900     void DeformableOctreeNode::slotSetDrawEmptyCells(bool b)
00901     {
00902       _drawEmptyCells = b;
00903       updateGL();
00904     }
00905     bool DeformableOctreeNode::getOptionDrawEmptyCells()
00906     {
00907       return _drawEmptyCells;
00908     }
00909 
00910     void DeformableOctreeNode::slotSetDrawOctree(bool b)
00911     {
00912       _drawOctree = b;
00913       updateGL();
00914     }
00915     bool DeformableOctreeNode::getOptionDrawOctree()
00916     {
00917       return _drawOctree;
00918     }
00919 
00920     void DeformableOctreeNode::slotSetDrawSelection(bool b)
00921     {
00922       _drawSelection = b;
00923       updateGL();
00924     }
00925     bool DeformableOctreeNode::getOptionDrawSelection()
00926     {
00927       return _drawSelection;
00928     }
00929 
00930     void DeformableOctreeNode::slotSetNMaxPointsPerCell( unsigned int n )
00931     {
00932       _octree->setNMaxPointsPerCell(n);
00933       //_octree->structureChangedRecLeaves();
00934       updateGL();
00935     }
00936     unsigned int DeformableOctreeNode::getOptionNMaxPointsPerCell( )
00937     {
00938       return _octree->getNMaxPointsPerCell();
00939     }
00940 
00941     void DeformableOctreeNode::slotSetDrawSelectedCellsPoints(bool b)
00942     {
00943       _drawSelectedCellsPoints = b;
00944       updateGL();
00945     }
00946     bool DeformableOctreeNode::getOptionDrawSelectedCellsPoints()
00947     {
00948       return _drawSelectedCellsPoints;
00949     }
00950 
00951     void DeformableOctreeNode::slotSetInterpolationMethod( int index )
00952     {
00953       _octree->setPositionMethod( index );
00954       _octree->movedRecLeaves( _octree->root() );
00955       updateGL();
00956     }
00957 
00958     void DeformableOctreeNode::slotChangeToolsSize( int size )
00959     {
00960       _toolsSize = 10e-4 * pow(10,size/10) * ((size%10) + 1);
00961     }
00962 
00963     FloatingPointType DeformableOctreeNode::getToolsSize()
00964     {
00965       return _toolsSize;
00966     }
00967 
00968 
00969     void DeformableOctreeNode::slotSetDrawNeighbours(bool b)
00970     {
00971       _drawNeighbours = b;
00972       updateGL();
00973     }
00974     bool DeformableOctreeNode::getOptionDrawNeighbours() const
00975     {
00976       return _drawNeighbours;
00977     }
00978 
00979 
00980 
00981 
00982 
00983 
00984 
00985 
00986 
00987 
00988     void DeformableOctreeNode::slotButtonDirectManipulation( )
00989     {
00990       //exit(0);
00991 
00992       Cell *cell = getSelectedCell();
00993 
00994       SFVec3fCellConstrained *point = cell->getData()._points[ _selectionId%(cell->getData()._points.size()) ];
00995       SFVec3f *v = point->getSFVec3f();
00996       Vec3d pos(v->x,v->y,v->z);
00997 
00998       Vec3d delta( getToolsSize(), -getToolsSize(), 2*getToolsSize() );
00999 
01000       std::cerr << "DON : " << delta << "\n";
01001 
01002       _octree->directManipulation( point, pos + delta );
01003       _octree->movedRecLeaves(_octree->root() );
01004       updateGL();
01005     }
01006 
01007 
01008 
01009     animal::ConstrainedItem* DeformableOctreeNode::pickPoint( float* origin, float* direction, float threshold )
01010     {
01011       //std::cerr << "DeformableOctreeNode::pickPoint()\n";
01012 
01013       if( _dosn )
01014       {
01015         X3D::Coordinate* coord = _dosn->getCoordinate();
01016 
01017         int index;
01018         float distance;
01019         SFVec3f lineOrigin(origin[0], origin[1], origin[2]);
01020         SFVec3f lineDirection(direction[0],direction[1],direction[2]);
01021         //cout<<"ParticleSystemNode::pickPoint"<<endl;
01022 
01023         if( animal::findClosestPointToLine( index, distance, coord->getPoint(), lineOrigin, lineDirection, threshold ) )
01024         {
01025           ManipulatedMeshVertex* c = new ManipulatedMeshVertex( this, &coord->getPoint()[index]);
01026           return c;
01027         }
01028         else
01029         {
01030           return NULL;
01031         }
01032       }
01033       else
01034       {
01035         return NULL;
01036       }
01037 
01038     }
01039 
01040 
01041 
01042 
01043 
01044     void drawCell( Cell *cell )
01045     {
01046       Require( cell != NULL );
01047 
01048       if( cell->isLeaf() )
01049       {
01050         if( cell->getData()._points.size() != 0 )
01051         {
01052           glColor4f(0.0,0.0,1.0,1.0);
01053           drawLeaf( cell );
01054         }
01055         else
01056         {
01057           glColor4f(1.0,1.0,0.0,1.0);
01058           drawLeaf( cell );
01059         }
01060         /*
01061         else if( DeformableOctreeNode::_drawEmptyCells )
01062         {
01063             Require( cell->getData()._points.size()  == 0 );
01064             glColor4f(1.0,1.0,0.0,1.0);
01065             drawLeaf( cell );
01066         }
01067         */
01068       }
01069     }
01070 
01071     void drawLeaf( Cell *cell )
01072     {
01073       glBegin( GL_QUADS );
01074       for( unsigned short f=0 ; f<6 ; ++f )
01075       {
01076         for( unsigned short i=0 ; i<4 ; ++i )
01077         {
01078           glVertex3fv( (cell->vertex(X3DTK::X3D::Octree::faceVertices[f][i])->getPosition()) );
01079         }
01080       }
01081       glEnd();
01082     }
01083 
01084 
01085 
01086   }//X3D
01087 }//X3DTK
01088 
01089 
01090 
01091 

Generated on Thu Dec 23 13:52:24 2004 by doxygen 1.3.6