Revision: 7157
http://playerstage.svn.sourceforge.net/playerstage/?rev=7157&view=rev
Author: rtv
Date: 2008-11-18 02:48:05 +0000 (Tue, 18 Nov 2008)
Log Message:
-----------
small speedups and const correctness additions
Modified Paths:
--------------
code/stage/trunk/libstage/model.cc
code/stage/trunk/libstage/region.cc
code/stage/trunk/libstage/stage.cc
code/stage/trunk/libstage/stage.hh
code/stage/trunk/libstage/stage_internal.hh
code/stage/trunk/libstage/world.cc
Modified: code/stage/trunk/libstage/model.cc
===================================================================
--- code/stage/trunk/libstage/model.cc 2008-11-17 23:06:46 UTC (rev 7156)
+++ code/stage/trunk/libstage/model.cc 2008-11-18 02:48:05 UTC (rev 7157)
@@ -361,11 +361,11 @@
}
-stg_raytrace_result_t StgModel::Raytrace( stg_pose_t pose,
-
stg_meters_t range,
-
stg_ray_test_func_t func,
+stg_raytrace_result_t StgModel::Raytrace( const stg_pose_t &pose,
+
const stg_meters_t range,
+
const stg_ray_test_func_t func,
const void* arg,
-
bool ztest )
+
const bool ztest )
{
return world->Raytrace( LocalToGlobal(pose),
range,
@@ -375,11 +375,11 @@
ztest );
}
-stg_raytrace_result_t StgModel::Raytrace( stg_radians_t bearing,
-
stg_meters_t range,
-
stg_ray_test_func_t func,
+stg_raytrace_result_t StgModel::Raytrace( const stg_radians_t bearing,
+
const stg_meters_t range,
+
const stg_ray_test_func_t func,
const void* arg,
-
bool ztest )
+
const bool ztest )
{
stg_pose_t raystart;
bzero( &raystart, sizeof(raystart));
@@ -394,14 +394,14 @@
}
-void StgModel::Raytrace( stg_radians_t bearing,
- stg_meters_t
range,
- stg_radians_t
fov,
-
stg_ray_test_func_t func,
+void StgModel::Raytrace( const stg_radians_t bearing,
+ const
stg_meters_t range,
+ const
stg_radians_t fov,
+ const
stg_ray_test_func_t func,
const void*
arg,
stg_raytrace_result_t* samples,
- uint32_t
sample_count,
- bool ztest )
+ const uint32_t
sample_count,
+ const bool
ztest )
{
stg_pose_t raystart;
bzero( &raystart, sizeof(raystart));
@@ -550,22 +550,17 @@
// get the model's position in the global frame
stg_pose_t StgModel::GetGlobalPose()
{
- //printf( "model %s global pose ", token );
-
- stg_pose_t parent_pose;
+ // if I'm a top level model, my global pose is my local pose
+ if( parent == NULL )
+ return pose;
- // find my parent's pose
- if( this->parent )
- {
- parent_pose = parent->GetGlobalPose();
- stg_pose_sum( &global_pose, &parent_pose, &pose );
-
- // we are on top of our parent
- global_pose.z += parent->geom.size.z;
- }
- else
- memcpy( &global_pose, &pose, sizeof(stg_pose_t));
+ // otherwise
+ stg_pose_t global_pose = pose_sum( parent->GetGlobalPose(), pose );
+
+ // we are on top of our parent
+ global_pose.z += parent->geom.size.z;
+
// PRINT_DEBUG4( "GET GLOBAL POSE [x:%.2f y:%.2f z:%.2f a:%.2f]",
// global_pose.x,
// global_pose.y,
@@ -579,27 +574,27 @@
// convert a pose in this model's local coordinates into global
// coordinates
// should one day do all this with affine transforms for neatness?
-stg_pose_t StgModel::LocalToGlobal( stg_pose_t pose )
+inline stg_pose_t StgModel::LocalToGlobal( stg_pose_t pose )
{
return pose_sum( pose_sum( GetGlobalPose(), geom.pose ), pose );
}
-stg_point3_t StgModel::LocalToGlobal( stg_point3_t point )
-{
- stg_pose_t pose;
- pose.x = point.x;
- pose.y = point.y;
- pose.z = point.z;
- pose.a = 0;
+// stg_point3_t StgModel::LocalToGlobal( stg_point3_t point )
+// {
+// stg_pose_t pose;
+// pose.x = point.x;
+// pose.y = point.y;
+// pose.z = point.z;
+// pose.a = 0;
- pose = LocalToGlobal( pose );
+// pose = LocalToGlobal( pose );
- point.x = pose.x;
- point.y = pose.y;
- point.z = pose.z;
+// point.x = pose.x;
+// point.y = pose.y;
+// point.z = pose.z;
- return point;
-}
+// return point;
+// }
void StgModel::MapWithChildren()
{
@@ -623,28 +618,28 @@
// given an input point array in model local coordinates, return
// an array with the same points in global coordinates. caller must
// delete[] the points.
-stg_point_t* StgModel::LocalToGlobal( double scalex,
-
double scaley,
-
stg_point_t pts[],
-
uint32_t pt_count )
-{
- stg_point_t* glob = new stg_point_t[pt_count];
+// stg_point_t* StgModel::LocalToGlobal( double scalex,
+//
double scaley,
+//
stg_point_t pts[],
+//
uint32_t pt_count )
+// {
+// stg_point_t* glob = new stg_point_t[pt_count];
- stg_pose_t global_pose = GetGlobalPose();
+// stg_pose_t global_pose = GetGlobalPose();
- for( int p=0; p<pt_count; p++ )
- {
- stg_pose_t local( pts[p].x * scalex,
- pts[p].y *
scaley,
- 0, 0 );
- stg_pose_t global = pose_sum( global_pose, local );
+// for( int p=0; p<pt_count; p++ )
+// {
+// stg_pose_t local( pts[p].x * scalex,
+// pts[p].y *
scaley,
+// 0, 0 );
+// stg_pose_t global = pose_sum( global_pose, local );
- glob[p].x = global.x;
- glob[p].y = global.y;
- }
+// glob[p].x = global.x;
+// glob[p].y = global.y;
+// }
- return glob;
-}
+// return glob;
+// }
void StgModel::Map()
Modified: code/stage/trunk/libstage/region.cc
===================================================================
--- code/stage/trunk/libstage/region.cc 2008-11-17 23:06:46 UTC (rev 7156)
+++ code/stage/trunk/libstage/region.cc 2008-11-18 02:48:05 UTC (rev 7157)
@@ -12,33 +12,7 @@
const uint32_t SuperRegion::WIDTH = SUPERREGIONWIDTH;
const uint32_t SuperRegion::SIZE = SUPERREGIONSIZE;
-//inline
-void Cell::RemoveBlock( StgBlock* b )
-{
- // linear time removal, but these lists should be very short.
- list = g_slist_remove( list, b );
-
- assert( region );
- region->DecrementOccupancy();
-}
-void Cell::AddBlock( StgBlock* b )
-{
- // constant time prepend
- list = g_slist_prepend( list, b );
-
- region->IncrementOccupancy();
- b->RecordRendering( this );
-}
-
-void Cell::AddBlockNoRecord( StgBlock* b )
-{
- list = g_slist_prepend( list, b );
-
- // don't add this cell to the block - we assume it's already there
-}
-
-
Region::Region()
: count(0)
{
@@ -51,30 +25,7 @@
delete[] cells;
}
-inline void Region::DecrementOccupancy()
-{
- assert( superregion );
- superregion->DecrementOccupancy();
- --count;
-}
-inline void Region::IncrementOccupancy()
-{
- assert( superregion );
- superregion->IncrementOccupancy();
- ++count;
-}
-
-Cell* Region::GetCell( int32_t x, int32_t y )
-{
- uint32_t index = x + (y*Region::WIDTH);
- assert( x < Region::WIDTH );
- assert( index >=0 );
- assert( index < Region::SIZE );
- return &cells[index];
-}
-
-
SuperRegion::SuperRegion( int32_t x, int32_t y )
: count(0)
{
@@ -91,25 +42,6 @@
// nothing to do
}
-// get the region x,y from the region array
-Region* SuperRegion::GetRegion( int32_t x, int32_t y )
-{
- int32_t index = x + (y*SuperRegion::WIDTH);
- assert( index >=0 );
- assert( index < (int)SuperRegion::SIZE );
- return ®ions[ index ];
-}
-
-inline void SuperRegion::DecrementOccupancy()
-{
- --count;
-}
-
-inline void SuperRegion::IncrementOccupancy()
-{
- ++count;
-}
-
void SuperRegion::Draw( bool drawall )
{
glEnable( GL_DEPTH_TEST );
Modified: code/stage/trunk/libstage/stage.cc
===================================================================
--- code/stage/trunk/libstage/stage.cc 2008-11-17 23:06:46 UTC (rev 7156)
+++ code/stage/trunk/libstage/stage.cc 2008-11-18 02:48:05 UTC (rev 7157)
@@ -197,22 +197,9 @@
}
}
-// sets [result] to the pose of [p2] in [p1]'s coordinate system
-void Stg::stg_pose_sum( stg_pose_t* result, stg_pose_t* p1, stg_pose_t* p2 )
-{
- double cosa = cos(p1->a);
- double sina = sin(p1->a);
- result->x = p1->x + p2->x * cosa - p2->y * sina;
- result->y = p1->y + p2->x * sina + p2->y * cosa;
- result->z = p1->z + p2->z;
- result->a = normalize(p1->a + p2->a);
-
- // printf( "pose z %.2f\n", result->z );
-}
-
-// returns the resultant of vector [p1] and [p2]
-stg_pose_t Stg::pose_sum( stg_pose_t p1, stg_pose_t p2 )
+// returns the pose of p2 in p1's coordinate system
+stg_pose_t Stg::pose_sum( const stg_pose_t& p1, const stg_pose_t& p2 )
{
double cosa = cos(p1.a);
double sina = sin(p1.a);
@@ -227,12 +214,15 @@
}
// returns the resultant of vector [p1] and [p2]
-stg_pose_t Stg::pose_scale( stg_pose_t p1, double sx, double sy, double sz )
+stg_pose_t Stg::pose_scale( const stg_pose_t& p1, const double sx, const
double sy, const double sz )
{
- p1.x *= sx;
- p1.y *= sy;
- p1.z *= sz;
- return p1;
+ stg_pose_t scaled;
+ scaled.x = p1.x * sx;
+ scaled.y = p1.y * sy;
+ scaled.z = p1.z * sz;
+ scaled.a = p1.a;
+
+ return scaled;
}
Modified: code/stage/trunk/libstage/stage.hh
===================================================================
--- code/stage/trunk/libstage/stage.hh 2008-11-17 23:06:46 UTC (rev 7156)
+++ code/stage/trunk/libstage/stage.hh 2008-11-18 02:48:05 UTC (rev 7157)
@@ -545,15 +545,11 @@
*/
stg_color_t stg_lookup_color(const char *name);
- /** calculate the sum of [p1] and [p2], in [p1]'s coordinate system, and
- copy the result into result. */
- void stg_pose_sum( stg_pose_t* result, stg_pose_t* p1, stg_pose_t* p2 );
-
/** returns the sum of [p1] + [p2], in [p1]'s coordinate system */
- stg_pose_t pose_sum( stg_pose_t p1, stg_pose_t p2 );
+ stg_pose_t pose_sum( const stg_pose_t& p1, const stg_pose_t& p2 );
/** returns a new pose, with each axis scaled */
- stg_pose_t pose_scale( stg_pose_t p1, double x, double y, double z );
+ stg_pose_t pose_scale( const stg_pose_t& p1, const double x, const double y,
const double z );
// PRETTY PRINTING -------------------------------------------------
@@ -929,22 +925,22 @@
SuperRegion* CreateSuperRegion( int32_t x, int32_t y );
void DestroySuperRegion( SuperRegion* sr );
- stg_raytrace_result_t Raytrace( stg_pose_t pose,
-
stg_meters_t range,
-
stg_ray_test_func_t func,
-
StgModel* finder,
+ stg_raytrace_result_t Raytrace( const stg_pose_t& pose,
+
const stg_meters_t range,
+
const stg_ray_test_func_t func,
+
const StgModel* finder,
const void* arg,
-
bool ztest );
+
const bool ztest );
- void Raytrace( stg_pose_t pose,
- stg_meters_t range,
- stg_radians_t fov,
- stg_ray_test_func_t func,
- StgModel* finder,
+ void Raytrace( const stg_pose_t &pose,
+ const stg_meters_t range,
+ const stg_radians_t fov,
+ const stg_ray_test_func_t func,
+ const StgModel* finder,
const void* arg,
stg_raytrace_result_t* samples,
- uint32_t sample_count,
- bool ztest );
+ const uint32_t sample_count,
+ const bool ztest );
protected:
@@ -1057,9 +1053,9 @@
/** Return the number of times the world has been updated. */
long unsigned int GetUpdateCount() { return updates; }
- stg_point_t* LocalToGlobal( double scalex, double scaley,
-
stg_point_t pts[],
-
uint32_t pt_count );
+// stg_point_t* LocalToGlobal( double scalex, double scaley,
+//
stg_point_t pts[],
+//
uint32_t pt_count );
};
class StgBlock
@@ -1333,37 +1329,37 @@
/** raytraces a single ray from the point and heading identified by
pose, in local coords */
- stg_raytrace_result_t Raytrace( stg_pose_t pose,
-
stg_meters_t range,
-
stg_ray_test_func_t func,
+ stg_raytrace_result_t Raytrace( const stg_pose_t &pose,
+
const stg_meters_t range,
+
const stg_ray_test_func_t func,
const void* arg,
-
bool ztest = true );
+
const bool ztest = true );
/** raytraces multiple rays around the point and heading identified
by pose, in local coords */
- void Raytrace( stg_pose_t pose,
- stg_meters_t range,
- stg_radians_t fov,
- stg_ray_test_func_t func,
+ void Raytrace( const stg_pose_t &pose,
+ const stg_meters_t range,
+ const stg_radians_t fov,
+ const stg_ray_test_func_t func,
const void* arg,
stg_raytrace_result_t* samples,
- uint32_t sample_count,
- bool ztest = true );
+ const uint32_t sample_count,
+ const bool ztest = true );
- stg_raytrace_result_t Raytrace( stg_radians_t bearing,
-
stg_meters_t range,
-
stg_ray_test_func_t func,
+ stg_raytrace_result_t Raytrace( const stg_radians_t bearing,
+
const stg_meters_t range,
+
const stg_ray_test_func_t func,
const void* arg,
-
bool ztest = true );
+
const bool ztest = true );
- void Raytrace( stg_radians_t bearing,
- stg_meters_t range,
- stg_radians_t fov,
- stg_ray_test_func_t func,
+ void Raytrace( const stg_radians_t bearing,
+ const stg_meters_t range,
+ const stg_radians_t fov,
+ const stg_ray_test_func_t func,
const void* arg,
stg_raytrace_result_t* samples,
- uint32_t sample_count,
- bool ztest = true );
+ const uint32_t sample_count,
+ const bool ztest = true );
/** Causes this model and its children to recompute their global
@@ -1430,10 +1426,10 @@
virtual void PopColor(){ world->PopColor(); }
void DrawFlagList();
- stg_point_t* LocalToGlobal( double scalex,
-
double scaley,
-
stg_point_t pts[],
-
uint32_t pt_count );
+// stg_point_t* LocalToGlobal( double scalex,
+//
double scaley,
+//
stg_point_t pts[],
+//
uint32_t pt_count );
void DrawPose( stg_pose_t pose );
Modified: code/stage/trunk/libstage/stage_internal.hh
===================================================================
--- code/stage/trunk/libstage/stage_internal.hh 2008-11-17 23:06:46 UTC (rev
7156)
+++ code/stage/trunk/libstage/stage_internal.hh 2008-11-18 02:48:05 UTC (rev
7157)
@@ -135,12 +135,12 @@
{ colorstack.Push( r,g,b,a ); }
void PopColor(){ colorstack.Pop(); }
-
- void InvertView( uint32_t invertflags );
-
- static void TimerCallback( StgCanvas* canvas );
- static void perspectiveCb( Fl_Widget* w, void* p );
+ void InvertView( uint32_t invertflags );
+
+ static void TimerCallback( StgCanvas* canvas );
+ static void perspectiveCb( Fl_Widget* w, void* p );
+
void Load( Worldfile* wf, int section );
void Save( Worldfile* wf, int section );
};
@@ -152,7 +152,7 @@
friend class SuperRegion;
friend class StgWorld;
friend class StgBlock;
-
+
private:
Region* region;
GSList* list;
@@ -161,10 +161,10 @@
: region( NULL),
list(NULL)
{ /* do nothing */ }
-
- void RemoveBlock( StgBlock* b );
- void AddBlock( StgBlock* b );
- void AddBlockNoRecord( StgBlock* b );
+
+ inline void RemoveBlock( StgBlock* b );
+ inline void AddBlock( StgBlock* b );
+ inline void AddBlockNoRecord( StgBlock* b );
};
// a bit of experimenting suggests that these values are fast. YMMV.
@@ -188,14 +188,18 @@
Cell cells[REGIONSIZE];
SuperRegion* superregion;
-
+
public:
unsigned long count; // number of blocks rendered into these cells
Region();
~Region();
- Cell* GetCell( int32_t x, int32_t y );
+ Cell* GetCell( int32_t x, int32_t y )
+ {
+ return( &cells[x + (y*Region::WIDTH)] );
+ };
+
void DecrementOccupancy();
void IncrementOccupancy();
};
@@ -220,14 +224,58 @@
SuperRegion( int32_t x, int32_t y );
~SuperRegion();
- Region* GetRegion( int32_t x, int32_t y );
+ Region* GetRegion( int32_t x, int32_t y )
+ {
+ return( ®ions[ x + (y*SuperRegion::WIDTH) ] );
+ };
void Draw( bool drawall );
void Floor();
- void DecrementOccupancy();
- void IncrementOccupancy();
+
+ void DecrementOccupancy(){ --count; };
+ void IncrementOccupancy(){ ++count; };
};
+
+// INLINE METHOD DEFITIONS
+
+inline void Region::DecrementOccupancy()
+{
+ assert( superregion );
+ superregion->DecrementOccupancy();
+ --count;
+}
+
+inline void Region::IncrementOccupancy()
+{
+ assert( superregion );
+ superregion->IncrementOccupancy();
+ ++count;
+}
+
+inline void Cell::RemoveBlock( StgBlock* b )
+{
+ // linear time removal, but these lists should be very short.
+ list = g_slist_remove( list, b );
+ region->DecrementOccupancy();
+}
+
+inline void Cell::AddBlock( StgBlock* b )
+{
+ // constant time prepend
+ list = g_slist_prepend( list, b );
+ region->IncrementOccupancy();
+ b->RecordRendering( this );
+}
+
+inline void Cell::AddBlockNoRecord( StgBlock* b )
+{
+ list = g_slist_prepend( list, b );
+ // don't add this cell to the block - we assume it's already there
+}
+
}; // namespace Stg
+
+
#endif // STG_INTERNAL_H
Modified: code/stage/trunk/libstage/world.cc
===================================================================
--- code/stage/trunk/libstage/world.cc 2008-11-17 23:06:46 UTC (rev 7156)
+++ code/stage/trunk/libstage/world.cc 2008-11-18 02:48:05 UTC (rev 7157)
@@ -143,10 +143,10 @@
PRINT_WARN( "Stg::Init() must be called before a StgWorld is
created." );
exit(-1);
}
+
+ bzero( &extent, sizeof(extent) );
StgWorld::world_list = g_list_append( StgWorld::world_list, this );
-
- bzero( &this->extent, sizeof(this->extent));
}
@@ -483,24 +483,28 @@
}
-void StgWorld::Raytrace( stg_pose_t pose, // global pose
- stg_meters_t range,
- stg_radians_t fov,
- stg_ray_test_func_t func,
- StgModel* model,
- const void* arg,
- stg_raytrace_result_t* samples, // preallocated storage for
samples
- uint32_t sample_count,
- bool ztest ) // number of samples
+void StgWorld::Raytrace( const stg_pose_t &pose, // global pose
+ const
stg_meters_t range,
+ const
stg_radians_t fov,
+ const
stg_ray_test_func_t func,
+ const
StgModel* model,
+ const void*
arg,
+
stg_raytrace_result_t* samples, // preallocated storage for samples
+ const uint32_t
sample_count,
+ const bool
ztest ) // number of samples
{
- pose.a -= fov/2.0; // direction of first ray
- stg_radians_t angle_incr = fov/(double)sample_count;
-
- for( uint32_t s=0; s < sample_count; s++ )
- {
- samples[s] = Raytrace( pose, range, func, model, arg, ztest );
- pose.a += angle_incr;
- }
+ // find the direction of the first ray
+ stg_pose_t raypose = pose;
+ raypose.a -= fov/2.0;
+
+ // increment the ray direction by this much for each sample
+ stg_radians_t angle_incr = fov/(double)sample_count;
+
+ for( uint32_t s=0; s < sample_count; s++ )
+ {
+ samples[s] = Raytrace( raypose, range, func, model, arg, ztest
);
+ raypose.a += angle_incr;
+ }
}
@@ -548,12 +552,12 @@
return c;
}
-stg_raytrace_result_t StgWorld::Raytrace( stg_pose_t gpose,
-
stg_meters_t range,
-
stg_ray_test_func_t func,
-
StgModel* mod,
+stg_raytrace_result_t StgWorld::Raytrace( const stg_pose_t &gpose,
+
const stg_meters_t range,
+
const stg_ray_test_func_t func,
+
const StgModel* mod,
const void* arg,
-
bool ztest )
+
const bool ztest )
{
stg_raytrace_result_t sample;
@@ -603,14 +607,9 @@
// x,y, dx,dy, n );
// superregion coords
- stg_point_int_t lastsup;
- lastsup.x = INT_MAX; // an unlikely first raytrace
- lastsup.y = INT_MAX;
+ stg_point_int_t lastsup = {INT_MAX, INT_MAX };
+ stg_point_int_t lastreg = {INT_MAX, INT_MAX };
- stg_point_int_t lastreg;
- lastreg.x = INT_MAX; // an unlikely first raytrace
- lastreg.y = INT_MAX;
-
SuperRegion* sr = NULL;
Region* r = NULL;
bool nonempty_region = false;
@@ -637,8 +636,7 @@
sr = GetSuperRegionCached( sup ); // possibly NULL, but unlikely
while ( n-- )
- {
-
+ {
//printf( "pixel [%d %d]\tS[ %d %d ]\t",
// x, y, sup.x, sup.y );
@@ -690,7 +688,7 @@
// mod, mod->Token(), ent->mod,
ent->mod->Token(), x, y );
// test the predicate we were passed
- if( (*func)( block->mod, mod, arg ))
// TODO
+ if( (*func)( block->mod,
(StgModel*)mod, arg )) // TODO
{
// a hit!
sample.color =
block->GetColor();
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit