Revision: 7605
http://playerstage.svn.sourceforge.net/playerstage/?rev=7605&view=rev
Author: rtv
Date: 2009-04-23 01:44:18 +0000 (Thu, 23 Apr 2009)
Log Message:
-----------
changed from GSList to std::list in raytracing
Modified Paths:
--------------
code/stage/trunk/libstage/block.cc
code/stage/trunk/libstage/region.cc
code/stage/trunk/libstage/region.hh
code/stage/trunk/libstage/world.cc
Modified: code/stage/trunk/libstage/block.cc
===================================================================
--- code/stage/trunk/libstage/block.cc 2009-04-22 02:48:52 UTC (rev 7604)
+++ code/stage/trunk/libstage/block.cc 2009-04-23 01:44:18 UTC (rev 7605)
@@ -139,26 +139,28 @@
return( inherit_color ? mod->color : color );
}
-GList* Block::AppendTouchingModels( GList* list )
+GList* Block::AppendTouchingModels( GList* l )
{
// for every cell we are rendered into
for( unsigned int i=0; i<rendered_cells->len; i++ )
{
- Cell* cell = (Cell*)g_ptr_array_index( rendered_cells, i);
+ Cell* c = (Cell*)g_ptr_array_index( rendered_cells, i);
// for every block rendered into that cell
- for( GSList* it = cell->list; it; it=it->next )
- {
- Block* testblock = (Block*)it->data;
- Model* testmod = testblock->mod;
+ for( std::list<Block*>::iterator it = c->blocks.begin();
+ it != c->blocks.end();
+ ++it )
+ {
+ //Block* testblock = *it;
+ Model* testmod = (*it)->mod;
- if( !mod->IsRelated( testmod ))
- if( ! g_list_find( list, testmod ) )
- list = g_list_append( list, testmod );
- }
+ if( !mod->IsRelated( testmod ))
+ if( ! g_list_find( l, testmod ) )
+ l = g_list_append( l, testmod );
+ }
}
-
- return list;
+
+ return l;
}
Model* Block::TestCollision()
@@ -172,25 +174,26 @@
// for every cell we may be rendered into
for( unsigned int i=0; i<candidate_cells->len; i++ )
{
- Cell* cell = (Cell*)g_ptr_array_index(candidate_cells, i);
-
- // for every rendered into that cell
- for( GSList* it = cell->list; it; it=it->next )
- {
- Block* testblock = (Block*)it->data;
- Model* testmod = testblock->mod;
-
- //printf( " testing block %p of model %s\n", testblock,
testmod->Token() );
-
- // if the tested model is an obstacle and it's not attached to this
model
- if( (testmod != this->mod) &&
- testmod->vis.obstacle_return &&
- !mod->IsRelated( testmod ))
- {
- //puts( "HIT");
- return testmod; // bail immediately with the bad news
- }
- }
+ Cell* c = (Cell*)g_ptr_array_index(candidate_cells, i);
+
+ // for every rendered into that cell
+ for( std::list<Block*>::iterator it = c->blocks.begin();
+ it != c->blocks.end();
+ ++it )
+ {
+ Model* testmod = (*it)->mod;
+
+ //printf( " testing block %p of model %s\n",
testblock, testmod->Token() );
+
+ // if the tested model is an obstacle and it's
not attached to this model
+ if( (testmod != this->mod) &&
+ testmod->vis.obstacle_return &&
+ !mod->IsRelated( testmod ))
+ {
+ //puts( "HIT");
+ return testmod; // bail immediately
with the bad news
+ }
+ }
}
//printf( "model %s block %p collision done. no hits.\n", mod->Token(), this
);
Modified: code/stage/trunk/libstage/region.cc
===================================================================
--- code/stage/trunk/libstage/region.cc 2009-04-22 02:48:52 UTC (rev 7604)
+++ code/stage/trunk/libstage/region.cc 2009-04-23 01:44:18 UTC (rev 7605)
@@ -129,11 +129,10 @@
snprintf( buf, 15, "%lu", r->count );
Gl::draw_string( x<<RBITS, y<<RBITS, 0, buf );
-
for( unsigned int p=0; p<Region::WIDTH; p++ )
for( unsigned int q=0; q<Region::WIDTH; q++ )
- if( r->cells[p+(q*Region::WIDTH)].list )
+ if( r->cells[p+(q*Region::WIDTH)].blocks.size()
)
{
GLfloat xx = p+(x<<RBITS);
GLfloat yy = q+(y<<RBITS);
@@ -145,11 +144,12 @@
}
else // draw a rectangular solid
{
- for( GSList* it =
r->cells[p+(q*Region::WIDTH)].list;
- it;
- it=it->next )
+ Cell* c =
&r->cells[p+(q*Region::WIDTH)];
+ for(
std::list<Block*>::iterator it = c->blocks.begin();
+ it !=
c->blocks.end();
+ ++it )
{
- Block* block =
(Block*)it->data;
+ Block* block =
*it;//(Block*)it->data;
//printf( "zb
%.2f %.2f\n", ent->zbounds.min, ent->zbounds.max );
Modified: code/stage/trunk/libstage/region.hh
===================================================================
--- code/stage/trunk/libstage/region.hh 2009-04-22 02:48:52 UTC (rev 7604)
+++ code/stage/trunk/libstage/region.hh 2009-04-23 01:44:18 UTC (rev 7605)
@@ -5,6 +5,9 @@
Copyright Richard Vaughan 2008
*/
+#include <list> // STL containers
+// #include <vector>
+
#include "stage.hh"
namespace Stg
@@ -21,6 +24,7 @@
#define SUPERREGIONWIDTH (1<<SBITS)
#define SUPERREGIONSIZE SUPERREGIONWIDTH*SUPERREGIONWIDTH
+
class Cell
{
friend class Region;
@@ -29,13 +33,14 @@
friend class Block;
private:
- Region* region;
- GSList* list;
+ Region* region;
+ std::list<Block*> blocks;
+
public:
Cell()
: region( NULL),
- list(NULL)
- { /* do nothing */ }
+ blocks()
+ { /* empty */ }
inline void RemoveBlock( Block* b );
inline void AddBlock( Block* b );
@@ -51,17 +56,16 @@
static const uint32_t WIDTH;
static const uint32_t SIZE;
- //Cell cells[REGIONSIZE];
Cell* cells;
SuperRegion* superregion;
-
+
public:
unsigned long count; // number of blocks rendered into these cells
Region();
~Region();
- Cell* GetCell( int32_t x, int32_t y )
+ Cell* GetCellCreate( int32_t x, int32_t y )
{
if( ! cells )
{
@@ -73,6 +77,11 @@
};
+ Cell* GetCellNoCreate( int32_t x, int32_t y )
+ {
+ return( &cells[x + (y*Region::WIDTH)] );
+ };
+
void DecrementOccupancy();
void IncrementOccupancy();
};
@@ -127,23 +136,18 @@
inline void Cell::RemoveBlock( Block* b )
{
// linear time removal, but these lists should be very short.
- list = g_slist_remove( list, b );
+ blocks.remove( b );
+
region->DecrementOccupancy();
}
inline void Cell::AddBlock( Block* b )
{
// constant time prepend
- list = g_slist_prepend( list, b );
+ blocks.push_front( b );
region->IncrementOccupancy();
b->RecordRendering( this );
}
-inline void Cell::AddBlockNoRecord( Block* b )
-{
- list = g_slist_prepend( list, b );
- // don't add this cell to the block - we assume it's already there
-}
-
}; // namespace Stg
Modified: code/stage/trunk/libstage/world.cc
===================================================================
--- code/stage/trunk/libstage/world.cc 2009-04-22 02:48:52 UTC (rev 7604)
+++ code/stage/trunk/libstage/world.cc 2009-04-23 01:44:18 UTC (rev 7605)
@@ -71,8 +71,8 @@
World::World( const char* token,
- stg_msec_t interval_sim,
- double ppm )
+ stg_msec_t interval_sim,
+ double ppm )
:
// private
charge_list( NULL ),
@@ -103,7 +103,7 @@
ray_list( NULL ),
sim_time( 0 ),
superregions( g_hash_table_new( (GHashFunc)PointIntHash,
- (GEqualFunc)PointIntEqual ) ),
+
(GEqualFunc)PointIntEqual ) ),
sr_cached(NULL),
reentrant_update_list( NULL ),
nonreentrant_update_list( NULL ),
@@ -154,7 +154,7 @@
for( GList* it = World::world_list; it; it=it->next )
{
if( ((World*)it->data)->Update() == false )
- quit = false;
+ quit = false;
}
return quit;
}
@@ -189,7 +189,7 @@
{
// lookup the group in which this was defined
Model* mod = (Model*)g_hash_table_lookup( entitytable,
- (gpointer)wf->GetEntityParent(
entity ) );
+
(gpointer)wf->GetEntityParent( entity ) );
if( ! mod )
PRINT_ERR( "block has no model for a parent" );
@@ -247,7 +247,7 @@
int parent_entity = wf->GetEntityParent( entity );
PRINT_DEBUG2( "wf entity %d parent entity %d\n",
- entity, parent_entity );
+ entity, parent_entity );
Model* parent = (Model*)g_hash_table_lookup( entitytable,
(gpointer)parent_entity );
@@ -299,11 +299,11 @@
this->interval_sim = (stg_usec_t)thousand *
wf->ReadInt( entity, "interval_sim",
- (int)(this->interval_sim/thousand) );
+ (int)(this->interval_sim/thousand) );
if( wf->PropertyExists( entity, "quit_time" ) ) {
this->quit_time = (stg_usec_t) ( million *
- wf->ReadFloat( entity, "quit_time", 0 ) );
+
wf->ReadFloat( entity, "quit_time", 0 ) );
}
if( wf->PropertyExists( entity, "resolution" ) )
@@ -317,22 +317,22 @@
int count = wf->ReadInt( entity, "threadpool", worker_threads );
if( count && (count != (int)worker_threads) )
- {
- worker_threads = count;
+ {
+ worker_threads = count;
- if( threadpool == NULL )
- threadpool = g_thread_pool_new( (GFunc)update_thread_entry,
- this,
- worker_threads,
- true,
- NULL );
- else
- g_thread_pool_set_max_threads( threadpool,
- worker_threads,
- NULL );
+ if( threadpool == NULL )
+ threadpool = g_thread_pool_new(
(GFunc)update_thread_entry,
+
this,
+
worker_threads,
+
true,
+
NULL );
+ else
+ g_thread_pool_set_max_threads( threadpool,
+
worker_threads,
+
NULL );
- printf( "[threadpool %u]", worker_threads );
- }
+ printf( "[threadpool %u]", worker_threads );
+ }
}
// Iterate through entitys and create objects of the appropriate type
@@ -347,8 +347,8 @@
}
else if( strcmp( typestr, "block" ) == 0 )
LoadBlock( wf, entity, entitytable );
-// else if( strcmp( typestr, "puck" ) == 0 )
-// LoadPuck( wf, entity, entitytable );
+ // else if( strcmp( typestr, "puck" ) == 0 )
+ // LoadPuck( wf, entity, entitytable );
else
LoadModel( wf, entity, entitytable );
}
@@ -366,7 +366,7 @@
if( debug )
printf( "[Load time %.3fsec]\n",
- (load_end_time - load_start_time) / 1000000.0 );
+ (load_end_time - load_start_time) / 1000000.0 );
else
putchar( '\n' );
}
@@ -629,11 +629,11 @@
}
stg_raytrace_result_t World::Raytrace( const Pose &gpose,
- const stg_meters_t range,
- const stg_ray_test_func_t func,
- const Model* mod,
- const void* arg,
- const bool ztest )
+
const stg_meters_t range,
+
const stg_ray_test_func_t func,
+
const Model* mod,
+
const void* arg,
+
const bool ztest )
{
stg_raytrace_result_t sample;
@@ -711,71 +711,73 @@
// find the starting superregion
sr = GetSuperRegionCached( sup ); // possibly NULL, but unlikely
-
+
while ( n-- )
{
//printf( "pixel [%d %d]\tS[ %d %d ]\t",
// x, y, sup.x, sup.y );
if( sr )
- {
- // printf( "R[ %d %d ]\t", reg.x, reg.y );
-
- if( reg.x != lastreg.x || reg.y != lastreg.y )
- {
- r = sr->GetRegion( reg.x, reg.y );
- lastreg = reg;
- nonempty_region = ( r && r->count );
- }
+ {
+ // printf( "R[ %d %d ]\t", reg.x, reg.y );
+
+ // if the region coordinate has changed since the last
loop
+ if( reg.x != lastreg.x || reg.y != lastreg.y )
+ {
+ // lookup the new region
+ r = sr->GetRegion( reg.x, reg.y );
+ lastreg = reg;
+ nonempty_region = ( r && r->count );
+ }
- if( nonempty_region )
- {
- //printf( "C[ %d %d ]\t", cell.x, cell.y );
-
- Cell* c = r->GetCell( cell.x, cell.y );
- assert(c);
-
- for( GSList* list = c->list;
- list;
- list = list->next )
- {
- Block* block = (Block*)list->data;
- assert( block );
-
- //printf( "ENT %p mod %p z [%.2f %.2f] color %X\n",
- // ent,
- // ent->mod,
- // ent->zbounds.min,
- // ent->zbounds.max,
- // ent->color );
-
- // skip if not in the right z range
- if( ztest &&
- ( gpose.z < block->global_z.min ||
- gpose.z > block->global_z.max ) )
- {
- //max( "failed ztest: ray at %.2f block at [%.2f %.2f]\n",
- // pose.z, ent->zbounds.min, ent->zbounds.max );
- continue;
- }
+ if( nonempty_region )
+ {
+ //printf( "C[ %d %d ]\t", cell.x, cell.y );
+
+ Cell* c = r->GetCellNoCreate( cell.x, cell.y
);
+ assert(c);
+
+ for( std::list<Block*>::iterator it =
c->blocks.begin();
+ it != c->blocks.end();
+ ++it )
+ {
+ Block* block = *it;
+ assert( block );
- //printf( "RT: mod %p testing mod %p at %d %d\n",
- // mod, ent->mod, x, y );
- // printf( "RT: mod %p %s testing mod %p %s at %d %d\n",
- // mod, mod->Token(), ent->mod, ent->mod->Token(),
x, y );
+ //printf( "ENT %p mod %p z
[%.2f %.2f] color %X\n",
+ // ent,
+ // ent->mod,
+ //
ent->zbounds.min,
+ //
ent->zbounds.max,
+ // ent->color );
- // test the predicate we were passed
- if( (*func)( block->mod, (Model*)mod, arg )) // TODO
- {
- // a hit!
- sample.color = block->GetColor();
- sample.mod = block->mod;
- sample.range = hypot( (glob.x-start.x)/ppm,
(glob.y-start.y)/ppm );
- return sample;
- }
- }
- }
- }
+ // skip if not in the right z
range
+ if( ztest &&
+ ( gpose.z <
block->global_z.min ||
+ gpose.z >
block->global_z.max ) )
+ {
+ //max( "failed ztest:
ray at %.2f block at [%.2f %.2f]\n",
+ // pose.z,
ent->zbounds.min, ent->zbounds.max );
+ continue;
+ }
+
+ //printf( "RT: mod %p testing
mod %p at %d %d\n",
+ // mod, ent->mod,
x, y );
+ // printf( "RT: mod %p %s
testing mod %p %s at %d %d\n",
+ // mod,
mod->Token(), ent->mod, ent->mod->Token(), x, y );
+
+ // test the predicate we were
passed
+ if( (*func)( block->mod,
(Model*)mod, arg ))
+ {
+ // a hit!
+ sample.color =
block->GetColor();
+ sample.mod =
block->mod;
+ sample.range = hypot(
(glob.x-start.x)/ppm, (glob.y-start.y)/ppm );
+ return sample;
+ }
+ }
+ }
+ }
// printf( "\t step %d n %d pixel [ %d, %d ] block [ %d %d ]
index [ %d %d ] \n",
// //coarse [ %d %d ]\n",
@@ -783,41 +785,41 @@
// increment our pixel in the correct direction
if( exy < 0 ) // we're iterating along X
- {
- glob.x += sx;
- exy += by;
+ {
+ glob.x += sx;
+ exy += by;
- sup.x = SUPERREGION( glob.x );
- if( sup.x != lastsup.x )
- {
- sr = (SuperRegion*)g_hash_table_lookup( superregions, (void*)&sup
);
- lastsup = sup; // remember these coords
- }
+ sup.x = SUPERREGION( glob.x );
+ if( sup.x != lastsup.x )
+ {
+ sr = (SuperRegion*)g_hash_table_lookup(
superregions, (void*)&sup );
+ lastsup = sup; // remember these coords
+ }
- // recompute current region and cell (we can skip these if
- // sr==NULL, but profiling suggests it's faster to do them
- // than add a conditional)
- reg.x = REGION( glob.x);
- cell.x = CELL( glob.x );
- }
+ // recompute current region and cell (we can skip
these if
+ // sr==NULL, but profiling suggests it's faster to do
them
+ // than add a conditional)
+ reg.x = REGION( glob.x);
+ cell.x = CELL( glob.x );
+ }
else // we're iterating along Y
- {
- glob.y += sy;
- exy -= bx;
+ {
+ glob.y += sy;
+ exy -= bx;
- sup.y = SUPERREGION( glob.y );
- if( sup.y != lastsup.y )
- {
- sr = (SuperRegion*)g_hash_table_lookup( superregions, (void*)&sup
);
- lastsup = sup; // remember these coords
- }
+ sup.y = SUPERREGION( glob.y );
+ if( sup.y != lastsup.y )
+ {
+ sr = (SuperRegion*)g_hash_table_lookup(
superregions, (void*)&sup );
+ lastsup = sup; // remember these coords
+ }
- // recompute current region and cell (we can skip these if
- // sr==NULL, but profiling suggests it's faster to do them
- // than add a conditional)
- reg.y = REGION( glob.y );
- cell.y = CELL( glob.y );
- }
+ // recompute current region and cell (we can skip
these if
+ // sr==NULL, but profiling suggests it's faster to do
them
+ // than add a conditional)
+ reg.y = REGION( glob.y );
+ cell.y = CELL( glob.y );
+ }
}
// hit nothing
@@ -911,8 +913,8 @@
//printf( "GC[ %d %d ] ", glob.x, glob.y );
return( GetSuperRegionCached( SUPERREGION(glob) )
- ->GetRegion( REGION(x), REGION(y) )
- ->GetCell( CELL(x), CELL(y) )) ;
+ ->GetRegion( REGION(x), REGION(y) )
+ ->GetCellCreate( CELL(x), CELL(y) )) ;
}
void World::ForEachCellInPolygon( const stg_point_t pts[],
@@ -961,15 +963,15 @@
// cleverly skip to the next cell
if( exy < 0 )
- {
- x += sx;
- exy += by;
- }
+ {
+ x += sx;
+ exy += by;
+ }
else
- {
- y += sy;
- exy -= bx;
- }
+ {
+ y += sy;
+ exy -= bx;
+ }
}
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today.
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit