[svn] / trunk / src / examples / eqPly / plyModel.cpp Repository:
ViewVC logotype

Diff of /trunk/src/examples/eqPly/plyModel.cpp

Parent Directory Parent Directory | Revision Log Revision Log | Patch

revision 861, Mon Mar 5 14:28:34 2007 UTC revision 862, Mon Mar 5 16:38:43 2007 UTC
# Line 1  Line 1 
1    
2  /* Copyright (c) 2006-2007, Stefan Eilemann <eile@equalizergraphics.com>  /* Copyright (c) 2006-2007, Stefan Eilemann <eile@equalizergraphics.com>
3     All rights reserved.     All rights reserved.
4     Adapted code for Equalizer usage.     - Adapted code for Equalizer usage.
5       - Improved malloc fragmentation behaviour
6   */   */
7    
8  /******************************************************************************  /******************************************************************************
# Line 54  Line 55 
55  #include <float.h>  #include <float.h>
56  #include <math.h>  #include <math.h>
57    
 #define CHUNKSIZE 4096  
58  #define MAXDEPTH  256  #define MAXDEPTH  256
59    
60  #define PLYFILEVERSION 20  #define PLYFILEVERSION 20
# Line 67  Line 67 
67  template<class FaceType>  template<class FaceType>
68  PlyModel<FaceType>::PlyModel( void )  PlyModel<FaceType>::PlyModel( void )
69          : _nFaces(0),          : _nFaces(0),
70            _faces(NULL)            _faces(0)
71  {  {
72      _bbox.parent   = NULL;      _bbox.parent   = 0;
73      _bbox.next     = NULL;      _bbox.next     = 0;
74      _bbox.children = NULL;      _bbox.children = 0;
75      _bbox.faces    = NULL;      _bbox.faces    = 0;
76      _bbox.nFaces   = 0;      _bbox.nFaces   = 0;
77  }  }
78    
# Line 82  Line 82 
82  template<class FaceType>  template<class FaceType>
83  PlyModel<FaceType>::~PlyModel()  PlyModel<FaceType>::~PlyModel()
84  {  {
85      if( _faces != NULL )      if( _faces != 0 )
86          free( _faces );          free( _faces );
87      _faces = NULL;      _faces = 0;
88    
89      freeBBoxes( _bbox );      freeBBoxes( _bbox );
90  }  }
# Line 96  Line 96 
96  void PlyModel<FaceType>::setFaces( size_t nFaces, FaceType *faces,  void PlyModel<FaceType>::setFaces( size_t nFaces, FaceType *faces,
97      size_t bboxFaceThreshold )      size_t bboxFaceThreshold )
98  {  {
99      if( _faces != NULL )      if( _faces != 0 )
100          free( _faces );          free( _faces );
101    
102      if( nFaces == 0 )      if( nFaces == 0 )
# Line 115  Line 115 
115    
116      EQINFO << eqBase::disableHeader << "Filling bounding boxes";      EQINFO << eqBase::disableHeader << "Filling bounding boxes";
117    
118      _bbox.children = NULL;      _bbox.children = 0;
119      _bbox.parent   = NULL;      _bbox.parent   = 0;
120      _bbox.next     = NULL;      _bbox.next     = 0;
121      _bbox.range[0] = 0.0f;      _bbox.range[0] = 0.0f;
122      _bbox.range[1] = 1.0f - numeric_limits<float>::epsilon();      _bbox.range[1] = 1.0f - numeric_limits<float>::epsilon();
123    
# Line 195  Line 195 
195      ++filled;      ++filled;
196  #endif  #endif
197    
198      bbox.nFaces   = 0;      vector<FaceType> bboxFaces;
     bbox.faces    = NULL;  
199    
200      for( size_t i=0; i<nFaces; i++ )      for( size_t i=0; i<nFaces; i++ )
201      {      {
# Line 207  Line 206 
206          if( faceInBBox( bbox.pos, faceBBox ))          if( faceInBBox( bbox.pos, faceBBox ))
207          {          {
208              expandBox( bbox.cullBox, faceBBox );              expandBox( bbox.cullBox, faceBBox );
209              addFaceToBBox( bbox, face );              bboxFaces.push_back( face );
210          }          }
211      }      }
212    
213      calculateCullSphere( bbox );      calculateCullSphere( bbox );
214    
215      // update range      // update range
216      if( nFaces == 0 ) // end pos      if( bboxFaces.empty( )) // end pos
217          bbox.range[1] = bbox.range[0];          bbox.range[1] = bbox.range[0];
218      else      else
219      {      {
220          const float start = bbox.range[0]; // start pos set by previous or init          const float start = bbox.range[0]; // start pos set by previous or init
221          const float range = bbox.range[1]; // parents range span set in init          const float range = bbox.range[1]; // parents range span set in init
222    
223          bbox.range[1] = start + range * bbox.nFaces / nFaces;          bbox.range[1] = start + range * bboxFaces.size() / nFaces;
224      }      }
225    
226      if( bbox.next != NULL ) // start range position of next child      if( bbox.next != 0 ) // start range position of next child
227          bbox.next->range[0] = bbox.range[1];          bbox.next->range[0] = bbox.range[1];
228    
229        bbox.faces  = &_faces[_nFaces];
230        bbox.nFaces = bboxFaces.size();
231    
232      if( bbox.nFaces < bboxFaceThreshold || depth > MAXDEPTH )      if( bbox.nFaces < bboxFaceThreshold || depth > MAXDEPTH )
233      {      {
234          if( bbox.nFaces == 0 )          if( bboxFaces.empty( ))
235              return;              return;
236    
237          memcpy( &_faces[_nFaces], bbox.faces, bbox.nFaces * sizeof(FaceType) );          memcpy( &_faces[_nFaces], &bboxFaces.front(),
238          free( bbox.faces );                  bbox.nFaces * sizeof( FaceType ));
239          bbox.faces = &_faces[_nFaces];  
240          _nFaces += bbox.nFaces;          _nFaces += bbox.nFaces;
241      }      }
242      else      else
# Line 242  Line 244 
244          // recursive-fill children bboxes          // recursive-fill children bboxes
245          createBBoxChildren( bbox );          createBBoxChildren( bbox );
246    
         FaceType *faces = bbox.faces;  
         bbox.faces = &_faces[_nFaces];  
   
247          size_t cnf = 0;          size_t cnf = 0;
   
248          for( int j=0; j<8; j++ )          for( int j=0; j<8; j++ )
249          {          {
250              fillBBox( bbox.nFaces, faces, bbox.children[j], bboxFaceThreshold,              fillBBox( bboxFaces.size(), &bboxFaces.front(), bbox.children[j],
251                        depth+1 );                        bboxFaceThreshold, depth+1 );
252              cnf +=  bbox.children[j].nFaces;              cnf +=  bbox.children[j].nFaces;
253          }          }
   
254          EQASSERT( cnf == bbox.nFaces );          EQASSERT( cnf == bbox.nFaces );
         bbox.nFaces = cnf;  
   
         free( faces );  
255      }      }
256  }  }
257    
# Line 278  Line 272 
272          if( i<7 )          if( i<7 )
273              bbox.children[i].next = &bbox.children[i+1];              bbox.children[i].next = &bbox.children[i+1];
274          else          else
275              bbox.children[i].next = NULL;              bbox.children[i].next = 0;
276      }      }
277    
278      // range start (child updates next child when filling bbox)      // range start (child updates next child when filling bbox)
# Line 463  Line 457 
457  }  }
458    
459  //---------------------------------------------------------------------------  //---------------------------------------------------------------------------
 // addFaceToBBox  
 //---------------------------------------------------------------------------  
 template<class FaceType>  
 void PlyModel<FaceType>::addFaceToBBox( BBox &bbox, FaceType &face )  
 {  
     if( bbox.nFaces%CHUNKSIZE == 0 )  
     {  
         if( bbox.faces == NULL )  
             bbox.faces = (FaceType *)malloc( CHUNKSIZE*sizeof(FaceType) );  
         else  
         {  
             size_t newSize = (bbox.nFaces/CHUNKSIZE + 1) *  
                 CHUNKSIZE*sizeof(FaceType);  
   
             bbox.faces = (FaceType *)realloc( bbox.faces, newSize );  
         }  
     }  
   
     memcpy( &bbox.faces[bbox.nFaces], &face, sizeof( FaceType ));  
     bbox.nFaces++;  
 }  
   
 //---------------------------------------------------------------------------  
460  // scale  // scale
461  //---------------------------------------------------------------------------  //---------------------------------------------------------------------------
462  struct ScaleData  struct ScaleData
# Line 505  Line 476 
476      };      };
477    
478      scaleModel( data.scale, data.offset );      scaleModel( data.scale, data.offset );
479      traverseBBox( &_bbox, scaleBBoxCB, scaleBBoxCB, NULL, &data );      traverseBBox( &_bbox, scaleBBoxCB, scaleBBoxCB, 0, &data );
480  }  }
481    
482  template<class FaceType>  template<class FaceType>
# Line 590  Line 561 
561  template<class FaceType>  template<class FaceType>
562  void PlyModel<FaceType>::freeBBoxes( BBox &bbox )  void PlyModel<FaceType>::freeBBoxes( BBox &bbox )
563  {  {
564      bbox.faces = NULL;      bbox.faces = 0;
565    
566      if( bbox.children == NULL )      if( bbox.children == 0 )
567          return;          return;
568    
569      for( int i=0; i<8; i++ )      for( int i=0; i<8; i++ )
570          freeBBoxes( bbox.children[i] );          freeBBoxes( bbox.children[i] );
571    
572      free( bbox.children );      free( bbox.children );
573      bbox.children = NULL;      bbox.children = 0;
574  }  }
575    
576  //---------------------------------------------------------------------------  //---------------------------------------------------------------------------
# Line 617  Line 588 
588  void PlyModel<FaceType>::traverseBBox( BBox *top, TraverseCB preCB,  void PlyModel<FaceType>::traverseBBox( BBox *top, TraverseCB preCB,
589      TraverseCB leafCB, TraverseCB postCB, void *userData )      TraverseCB leafCB, TraverseCB postCB, void *userData )
590  {  {
591      if ( top->children == NULL )      if ( top->children == 0 )
592      {      {
593          if ( leafCB ) leafCB( top, userData );          if ( leafCB ) leafCB( top, userData );
594          return;          return;
# Line 630  Line 601 
601      {      {
602          parent = bbox->parent;          parent = bbox->parent;
603          next   = bbox->next;          next   = bbox->next;
604          child  = (bbox->children==NULL) ? NULL : &bbox->children[0];          child  = (bbox->children==0) ? 0 : &bbox->children[0];
605    
606          //---------- down-right traversal          //---------- down-right traversal
607          if ( child == NULL ) // leaf          if ( child == 0 ) // leaf
608          {          {
609              if ( leafCB ) leafCB( bbox, userData );              if ( leafCB ) leafCB( bbox, userData );
610    
# Line 641  Line 612 
612          }          }
613          else // node          else // node
614          {          {
615              if( preCB != NULL ) preCB( bbox, userData );              if( preCB != 0 ) preCB( bbox, userData );
616    
617              bbox = child;              bbox = child;
618          }          }
619    
620          //---------- up-right traversal          //---------- up-right traversal
621          if( bbox == NULL && parent == NULL ) return;          if( bbox == 0 && parent == 0 ) return;
622    
623          while ( bbox == NULL )          while ( bbox == 0 )
624          {          {
625              bbox   = parent;              bbox   = parent;
626              parent = bbox->parent;              parent = bbox->parent;
# Line 740  Line 711 
711      os.write( (char *)bbox.range, 2*sizeof(float) );      os.write( (char *)bbox.range, 2*sizeof(float) );
712    
713      int nChildren = 8;      int nChildren = 8;
714      if( bbox.children == NULL )      if( bbox.children == 0 )
715          nChildren = 0;          nChildren = 0;
716    
717      os.write( (char *)&nChildren, sizeof(int) );      os.write( (char *)&nChildren, sizeof(int) );

Legend:
Removed from v.861  
changed lines
  Added in v.862

Back to Equalizer website
ViewVC Help
Powered by ViewVC 1.0.3