Revision: 7707
          http://playerstage.svn.sourceforge.net/playerstage/?rev=7707&view=rev
Author:   rtv
Date:     2009-05-22 02:52:17 +0000 (Fri, 22 May 2009)

Log Message:
-----------
moved  bit to STL

Modified Paths:
--------------
    code/stage/trunk/libstage/block.cc
    code/stage/trunk/libstage/region.cc
    code/stage/trunk/libstage/stage.hh
    code/stage/trunk/libstage/world.cc

Modified: code/stage/trunk/libstage/block.cc
===================================================================
--- code/stage/trunk/libstage/block.cc  2009-05-21 16:46:22 UTC (rev 7706)
+++ code/stage/trunk/libstage/block.cc  2009-05-22 02:52:17 UTC (rev 7707)
@@ -21,8 +21,8 @@
   pts( (stg_point_t*)g_memdup( pts, pt_count * sizeof(stg_point_t)) ),
   color( color ),
   inherit_color( inherit_color ),
-  rendered_cells( g_ptr_array_sized_new(32) ),
-  candidate_cells( g_ptr_array_sized_new(32) )
+  rendered_cells( new std::vector<Cell*> ), 
+  candidate_cells( new std::vector<Cell*> ) 
 {
   assert( mod );
   assert( pt_count > 0 );
@@ -42,8 +42,8 @@
     pts(NULL),
     color(0),
     inherit_color(true),
-    rendered_cells( g_ptr_array_sized_new(32) ),
-    candidate_cells( g_ptr_array_sized_new(32) )
+        rendered_cells( new std::vector<Cell*> ), 
+        candidate_cells( new std::vector<Cell*> ) 
 {
   assert(mod);
   assert(wf);
@@ -59,8 +59,8 @@
   if( pts ) delete[] pts;
   InvalidateModelPointCache();
 
-  g_ptr_array_free( rendered_cells, TRUE );
-  g_ptr_array_free( candidate_cells, TRUE );
+  delete rendered_cells;
+  delete candidate_cells;
 }
 
 void Block::Translate( double x, double y )
@@ -143,9 +143,10 @@
 GList* Block::AppendTouchingModels( GList* l )
 {
   // for every cell we are rendered into
-  for( unsigned int i=0; i<rendered_cells->len; i++ )
+  for( unsigned int i=0; i<rendered_cells->size(); i++ )
     {
-      Cell* c = (Cell*)g_ptr_array_index( rendered_cells, i);
+      //Cell* c = (Cell*)g_ptr_array_index( rendered_cells, i);
+               Cell* c = (*rendered_cells)[i];
 
       // for every block rendered into that cell
                for( std::list<Block*>::iterator it = c->blocks.begin();
@@ -173,10 +174,11 @@
 
   if( mod->vis.obstacle_return )
     // for every cell we may be rendered into
-    for( unsigned int i=0; i<candidate_cells->len; i++ )
+    for( unsigned int i=0; i<candidate_cells->size(); i++ )
       {
-                 Cell* c = (Cell*)g_ptr_array_index(candidate_cells, i);
-
+                 //Cell* c = (Cell*)g_ptr_array_index(candidate_cells, i);
+                 Cell* c = (*candidate_cells)[i];
+                 
                  // for every rendered into that cell
                  for( std::list<Block*>::iterator it = c->blocks.begin();
                                 it != c->blocks.end();
@@ -202,26 +204,9 @@
 }
 
 
-void Block::RemoveFromCellArray( GPtrArray* ptrarray )
-{
-  for( unsigned int i=0; i<ptrarray->len; i++ )
-    ((Cell*)g_ptr_array_index(ptrarray, i))->RemoveBlock( this );
-}
 
-void Block::AddToCellArray( GPtrArray* ptrarray )
-{
-  for( unsigned int i=0; i<ptrarray->len; i++ )
-    ((Cell*)g_ptr_array_index(ptrarray, i))->AddBlock( this );
-}
 
-
 // used as a callback to gather an array of cells in a polygon
-void AppendCellToPtrArray( Cell* c, GPtrArray* a )
-{
-  g_ptr_array_add( a, c );
-}
-
-// used as a callback to gather an array of cells in a polygon
 void AddBlockToCell( Cell* c, Block* block )
 {
   c->AddBlock( block );
@@ -241,17 +226,34 @@
 {
   RemoveFromCellArray( rendered_cells );
 
-  g_ptr_array_set_size( rendered_cells, 0 );
+  //g_ptr_array_set_size( rendered_cells, 0 );
+  rendered_cells->clear();
   mapped = false;
 }
 
+void Block::RemoveFromCellArray( std::vector<Cell*> * cells )
+{
+  for( std::vector<Cell*>::iterator it = cells->begin();
+                it != cells->end();
+                ++it )  
+        (*it)->RemoveBlock( this);
+}
+
+void Block::AddToCellArray( std::vector<Cell*> * cells )
+{
+  for( std::vector<Cell*>::iterator it = cells->begin();
+                it != cells->end();
+                ++it )  
+        (*it)->AddBlock( this);
+}
+
 void Block::SwitchToTestedCells()
 {
   RemoveFromCellArray( rendered_cells );
   AddToCellArray( candidate_cells );
 
   // switch current and candidate cell pointers
-  GPtrArray* tmp = rendered_cells;
+  std::vector<Cell*> * tmp = rendered_cells;
   rendered_cells = candidate_cells;
   candidate_cells = tmp;
 
@@ -291,6 +293,12 @@
         }
 }
 
+// callback used below
+static void AppendCellToVector( Cell* c, std::vector<Cell*> * a )
+{
+  a->push_back( c );
+}
+
 void Block::GenerateCandidateCells()
 {
   stg_point_t* mpts = GetPointsInModelCoords();
@@ -300,11 +308,12 @@
   for( unsigned int i=0; i<pt_count; i++ )
         gpts[i] = mod->LocalToGlobal( mpts[i] );
 
-  g_ptr_array_set_size( candidate_cells, 0 );
+  //g_ptr_array_set_size( candidate_cells, 0 );
+  candidate_cells->clear();
 
   mod->world->
         ForEachCellInPolygon( gpts, pt_count,
-                                                                 
(stg_cell_callback_t)AppendCellToPtrArray,
+                                                                 
(stg_cell_callback_t)AppendCellToVector,
                                                                  
candidate_cells );
   delete[] gpts;
 

Modified: code/stage/trunk/libstage/region.cc
===================================================================
--- code/stage/trunk/libstage/region.cc 2009-05-21 16:46:22 UTC (rev 7706)
+++ code/stage/trunk/libstage/region.cc 2009-05-22 02:52:17 UTC (rev 7707)
@@ -69,7 +69,7 @@
                         // outline regions with contents
                         glRecti( x<<RBITS, y<<RBITS, 
                                                 (x+1)<<RBITS, (y+1)<<RBITS );
-                 else// if( r->cells )
+                 else if( r->cells )
                         {
                                double left = x << RBITS;
                                double right = (x+1) << RBITS;

Modified: code/stage/trunk/libstage/stage.hh
===================================================================
--- code/stage/trunk/libstage/stage.hh  2009-05-21 16:46:22 UTC (rev 7706)
+++ code/stage/trunk/libstage/stage.hh  2009-05-22 02:52:17 UTC (rev 7707)
@@ -902,7 +902,6 @@
   {
     friend class Model; // allow access to private members
     friend class Block;
-    //friend class StgTime;
     friend class Canvas;
 
   private:
@@ -1197,7 +1196,9 @@
         void SetZ( double min, double max );
 
     void RecordRendering( Cell* cell )
-    { g_ptr_array_add( rendered_cells, (gpointer)cell ); };
+    { //g_ptr_array_add( rendered_cells, (gpointer)cell ); };
+               rendered_cells->push_back( cell );
+        }
   
     stg_point_t* Points( unsigned int *count )
     { if( count ) *count = pt_count; return pts; };           
@@ -1205,11 +1206,8 @@
     //bool IntersectGlobalZ( stg_meters_t z )
     //{ return( z >= global_zmin &&  z <= global_zmax ); }
   
-    void AddToCellArray( GPtrArray* ptrarray );
-    void RemoveFromCellArray( GPtrArray* ptrarray );
-        
-    //void AddToCellArray( std::vector<Cell*>& blocks );
-    //void RemoveFromCellArray( std::vector<Cell*>& blocks );
+    void AddToCellArray( std::vector<Cell*>* blocks );
+    void RemoveFromCellArray( std::vector<Cell*>* blocks );
 
     void GenerateCandidateCells();
   
@@ -1261,18 +1259,18 @@
         
     bool mapped;
         
-    /** an array of pointers to cells into which this block has been
-                 rendered (speeds up UnMapping) */  
-    GPtrArray* rendered_cells;
-  
+    /** record the cells into which this block has been rendered to
+                 UnMapping them very quickly. */  
+        std::vector<Cell*> * rendered_cells;
+
     /** When moving a model, we test for collisions by generating, for
                  each block, a list of the cells in which it would be rendered 
if the
                  move were to be successful. If no collision occurs, the move 
is
                  allowed - the rendered cells are cleared, the potential cells 
are
                  written, and the pointers to the rendered and potential cells 
are
                  switched for next time (avoiding a memory copy).*/
-    GPtrArray* candidate_cells;
-        
+        std::vector<Cell*> * candidate_cells;
+
         /** find the position of a block's point in model coordinates
                  (m) */
         stg_point_t BlockPointToModelMeters( const stg_point_t& bpt );

Modified: code/stage/trunk/libstage/world.cc
===================================================================
--- code/stage/trunk/libstage/world.cc  2009-05-21 16:46:22 UTC (rev 7706)
+++ code/stage/trunk/libstage/world.cc  2009-05-22 02:52:17 UTC (rev 7707)
@@ -1006,6 +1006,9 @@
                         ->GetCellGlobalCreate( glob )) ;
 }
 
+// TODO - each line end point is processed twice here - could save a
+// teeny bit of time if we did this more cleverly
+// also - replace C arrays with vectors?
 void World::ForEachCellInPolygon( const stg_point_t pts[], 
                                                                                
         const unsigned int pt_count,
                                                                                
         stg_cell_callback_t cb,
@@ -1048,6 +1051,10 @@
     {                          
       // find or create the cell at this location, then call the callback
       // with the cell, block and user-defined argument
+
+               // TODO - could get rid of this callback, as we only ever use
+               // one function here we can speed it up a bit by having the code
+               // inline
       (*cb)( GetCellCreate( x,y ), cb_arg );
                
       // cleverly skip to the next cell                         


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, & 
iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian
Group, R/GA, & Big Spaceship. http://www.creativitycat.com 
_______________________________________________
Playerstage-commit mailing list
Playerstage-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to