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

Log Message:
-----------
optimizing block.cc

Modified Paths:
--------------
    code/stage/trunk/libstage/block.cc
    code/stage/trunk/libstage/model.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-22 02:52:17 UTC (rev 7707)
+++ code/stage/trunk/libstage/block.cc  2009-05-22 08:06:08 UTC (rev 7708)
@@ -293,30 +293,22 @@
         }
 }
 
-// callback used below
-static void AppendCellToVector( Cell* c, std::vector<Cell*> * a )
-{
-  a->push_back( c );
-}
-
 void Block::GenerateCandidateCells()
 {
-  stg_point_t* mpts = GetPointsInModelCoords();
-
-  // convert the mpts in model coords into global coords
-  stg_point_t* gpts = new stg_point_t[pt_count];
-  for( unsigned int i=0; i<pt_count; i++ )
-        gpts[i] = mod->LocalToGlobal( mpts[i] );
-
-  //g_ptr_array_set_size( candidate_cells, 0 );
   candidate_cells->clear();
 
-  mod->world->
-        ForEachCellInPolygon( gpts, pt_count,
-                                                                 
(stg_cell_callback_t)AppendCellToVector,
-                                                                 
candidate_cells );
-  delete[] gpts;
+  stg_point_t* mpts = GetPointsInModelCoords();
 
+  // convert the mpts in model coords into global pixel coords
+  stg_point_int_t gpts[pt_count];
+  for( unsigned int i=0; i<pt_count; i++ )
+               gpts[i] = mod->world->MetersToPixels( mod->LocalToGlobal( 
mpts[i] ));
+  
+  for( unsigned int i=0; i<pt_count; i++ )
+        mod->world->ForEachCellInLine( gpts[i], 
+                                                                               
          gpts[(i+1)%pt_count], 
+                                                                               
          *candidate_cells );  
+  
   // set global Z
   Pose gpose = mod->GetGlobalPose();
   gpose.z += mod->geom.pose.z;

Modified: code/stage/trunk/libstage/model.cc
===================================================================
--- code/stage/trunk/libstage/model.cc  2009-05-22 02:52:17 UTC (rev 7707)
+++ code/stage/trunk/libstage/model.cc  2009-05-22 08:06:08 UTC (rev 7708)
@@ -117,9 +117,9 @@
 
 // static members
 uint32_t Model::count = 0;
-GHashTable* Model::modelsbyid = g_hash_table_new( NULL, NULL );
+//GHashTable* Model::modelsbyid = g_hash_table_new( NULL, NULL );
+std::map<stg_id_t,Model*> Model::modelsbyid;
 
-
 void Size::Load( Worldfile* wf, int section, const char* keyword )
 {
   x = wf->ReadTupleLength( section, keyword, 0, x );
@@ -237,7 +237,7 @@
     world(world),
         world_gui( dynamic_cast<WorldGui*>( world ) )
 {
-  assert( modelsbyid );
+  //assert( modelsbyid );
   assert( world );
   
   PRINT_DEBUG3( "Constructing model world: %s parent: %s type: %d ",
@@ -245,7 +245,8 @@
                parent ? parent->Token() : "(null)",
                type );
   
-  g_hash_table_insert( modelsbyid, (void*)id, this );
+  //g_hash_table_insert( modelsbyid, (void*)id, this );
+  modelsbyid[id] = this;
   
   // Adding this model to its ancestor also gives this model a
   // sensible default name
@@ -286,7 +287,8 @@
        
   g_datalist_clear( &props );
 
-  g_hash_table_remove( Model::modelsbyid, (void*)id );
+  //g_hash_table_remove( Model::modelsbyid, (void*)id );
+  modelsbyid.erase(id);
 
   world->RemoveModel( this );
 }

Modified: code/stage/trunk/libstage/stage.hh
===================================================================
--- code/stage/trunk/libstage/stage.hh  2009-05-22 02:52:17 UTC (rev 7707)
+++ code/stage/trunk/libstage/stage.hh  2009-05-22 08:06:08 UTC (rev 7708)
@@ -46,7 +46,6 @@
 #include <vector>
 #include <list>
 #include <map>
-//#include <pair>
 
 // we use GLib's data structures extensively. Perhaps we'll move to
 // C++ STL types to lose this dependency one day.
@@ -996,22 +995,22 @@
 
     inline Cell* GetCellNoCreate( const stg_point_int_t& glob );
     inline Cell* GetCellNoCreate( const int32_t x, const int32_t y );
-    inline Cell* GetCellCreate( const int32_t x, const int32_t y );
+    //inline Cell* GetCellCreate( const int32_t x, const int32_t y );
+    inline Cell* GetCellCreate( const stg_point_int_t& glob );
         
-    void ForEachCellInPolygon( const stg_point_t pts[], 
-                                                                               
 const unsigned int pt_count,
-                                                                               
 stg_cell_callback_t cb,
-                                                                               
 void* cb_arg );
-  
-    void ForEachCellInLine( const stg_point_t& pt1,
-                                                                        const 
stg_point_t& pt2, 
-                                                                        
stg_cell_callback_t cb,
-                                                                        void* 
cb_arg );
-               
+        /** add a Cell pointer to the vector for each cell on the line from
+                 pt1 to pt2 inclusive */
+    void ForEachCellInLine( const stg_point_int_t& pt1,
+                                                                        const 
stg_point_int_t& pt2, 
+                                                                        
std::vector<Cell*>& cells );
+
     /** convert a distance in meters to a distance in world occupancy
                  grid pixels */
     int32_t MetersToPixels( stg_meters_t x )
     { return (int32_t)floor(x * ppm); };
+
+    stg_point_int_t MetersToPixels( const stg_point_t& pt )
+    { return stg_point_int_t( MetersToPixels(pt.x), MetersToPixels(pt.y)); };
         
     // dummy implementations to be overloaded by GUI subclasses
     virtual void PushColor( stg_color_t col ) { /* do nothing */  };
@@ -1748,7 +1747,8 @@
   private:
         /** the number of models instatiated - used to assign unique IDs */
         static uint32_t count;
-        static GHashTable*  modelsbyid;
+        //static GHashTable*  modelsbyid;
+        static std::map<stg_id_t,Model*> modelsbyid;
         std::vector<Option*> drawOptions;
         const std::vector<Option*>& getOptions() const { return drawOptions; }
         
@@ -2046,7 +2046,6 @@
         { world->PushColor( r,g,b,a ); }
        
         virtual void PopColor(){ world->PopColor(); }
-       
 
         PowerPack* FindPowerPack() const;
 
@@ -2060,7 +2059,8 @@
 
         /** Look up a model pointer by a unique model ID */
         static Model* LookupId( uint32_t id )
-        { return (Model*)g_hash_table_lookup( modelsbyid, (void*)id ); }
+        //{ return (Model*)g_hash_table_lookup( modelsbyid, (void*)id ); }
+        { return modelsbyid[id]; }
        
         /** Constructor */
         Model( World* world, 

Modified: code/stage/trunk/libstage/world.cc
===================================================================
--- code/stage/trunk/libstage/world.cc  2009-05-22 02:52:17 UTC (rev 7707)
+++ code/stage/trunk/libstage/world.cc  2009-05-22 08:06:08 UTC (rev 7708)
@@ -995,41 +995,35 @@
   return NULL;
 }
 
-inline Cell* World::GetCellCreate( const int32_t x, const int32_t y )
-{
-  stg_point_int_t glob( x, y );
+// inline Cell* World::GetCellCreate( const int32_t x, const int32_t y )
+// {
+//   stg_point_int_t glob( x, y );
 
-  //printf( "GC[ %d %d ] ", glob.x, glob.y );
+//   //printf( "GC[ %d %d ] ", glob.x, glob.y );
   
-  return( GetSuperRegionCached(  GETSREG(x), GETSREG(y)  )
+//   return( GetSuperRegionCached(  GETSREG(x), GETSREG(y)  )
+//                      ->GetRegionGlobal( glob )
+//                      ->GetCellGlobalCreate( glob )) ;
+// }
+
+inline Cell* World::GetCellCreate( const stg_point_int_t& glob )
+{
+  return( GetSuperRegionCached(  GETSREG(glob.x), GETSREG(glob.y)  )
                         ->GetRegionGlobal( glob )
                         ->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,
-                                                                               
         void* cb_arg )
-{
-  for( unsigned int i=0; i<pt_count; i++ )
-        ForEachCellInLine( pts[i], pts[(i+1)%pt_count], cb, cb_arg );
-
-}
   
-void World::ForEachCellInLine( const stg_point_t& pt1,
-                                                                               
 const stg_point_t& pt2,
-                                                                               
 stg_cell_callback_t cb,
-                                                                               
 void* cb_arg )
+void World::ForEachCellInLine( const stg_point_int_t& start,
+                                                                               
 const stg_point_int_t& end,
+                                                                               
 std::vector<Cell*>& cells )
 {  
-  int x = MetersToPixels( pt1.x ); // global pixel coords
-  int y = MetersToPixels( pt1.y );
+
+  int dx = end.x - start.x;
+  int dy = end.y - start.y;
+
+  stg_point_int_t cell = start;
   
-  int dx = MetersToPixels( pt2.x - pt1.x );
-  int dy = MetersToPixels( pt2.y - pt1.y );
-  
   // line rasterization adapted from Cohen's 3D version in
   // Graphics Gems II. Should be very fast.
   
@@ -1049,23 +1043,18 @@
   
   while( n-- ) 
     {                          
-      // find or create the cell at this location, then call the callback
-      // with the cell, block and user-defined argument
+      // find or create the cell at this location, then add it to the vector
+               cells.push_back( GetCellCreate( cell ) );
 
-               // 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                         
       if( exy < 0 ) 
                  {
-                        x += sx;
+                        cell.x += sx;
                         exy += by;
                  }
       else 
                  {
-                        y += sy;
+                        cell.y += sy;
                         exy -= bx; 
                  }
     }


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
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to