Revision: 7768
          http://playerstage.svn.sourceforge.net/playerstage/?rev=7768&view=rev
Author:   rtv
Date:     2009-06-03 07:38:57 +0000 (Wed, 03 Jun 2009)

Log Message:
-----------
tweaking ray tracing

Modified Paths:
--------------
    code/stage/trunk/libstage/CMakeLists.txt
    code/stage/trunk/libstage/model_laser.cc
    code/stage/trunk/libstage/stage.hh
    code/stage/trunk/libstage/world.cc

Modified: code/stage/trunk/libstage/CMakeLists.txt
===================================================================
--- code/stage/trunk/libstage/CMakeLists.txt    2009-06-03 06:28:26 UTC (rev 
7767)
+++ code/stage/trunk/libstage/CMakeLists.txt    2009-06-03 07:38:57 UTC (rev 
7768)
@@ -4,6 +4,7 @@
 include_directories(${PROJECT_BINARY_DIR})
 
 set( stageSrcs    
+       world.cc
    ancestor.cc
        block.cc
        blockgroup.cc
@@ -37,7 +38,6 @@
        typetable.cc
    vis_strip.cc
        waypoint.cc
-       world.cc
        worldfile.cc
    worldgui.cc 
    canvas.cc 

Modified: code/stage/trunk/libstage/model_laser.cc
===================================================================
--- code/stage/trunk/libstage/model_laser.cc    2009-06-03 06:28:26 UTC (rev 
7767)
+++ code/stage/trunk/libstage/model_laser.cc    2009-06-03 07:38:57 UTC (rev 
7768)
@@ -190,7 +190,7 @@
         vis( world ),
     sample_count( DEFAULT_SAMPLES ),
     samples(),
-               rays(),
+               //ray( this, ),
     range_max( DEFAULT_MAXRANGE ),
     fov( DEFAULT_FOV ),
     resolution( DEFAULT_RESOLUTION )
@@ -279,59 +279,43 @@
 void ModelLaser::SampleConfig()
 {
   samples.resize( sample_count );
-  rays.resize( sample_count );
-  
-  for( unsigned int t=0; t<sample_count; ++t )
-        {
-               // configure a normal ray with our laser-specific settings
-               rays[t].func = laser_raytrace_match; 
-               rays[t].arg = NULL;
-               rays[t].ztest = true;
-               rays[t].range = range_max;
-               rays[t].mod = this;
-        }
 }
 
 void ModelLaser::Update( void )
 {     
   assert( samples.size() == sample_count );
-  assert( rays.size() == sample_count );
+  //assert( rays.size() == sample_count );
   
   double bearing = -fov/2.0;
   // make the first and last rays exactly at the extremes of the FOV
   double sample_incr = fov / MAX(sample_count-1,1);
   
+       // find the global origin of our first emmitted ray
   Pose rayorg = geom.pose;
   rayorg.z += geom.size.z/2.0;
   rayorg.a = bearing;// + sample_incr/2.0;
   rayorg = LocalToGlobal(rayorg);
-  
-  // set up the ray origins in global coords
+
+       // set up a ray to trace
+       Ray ray( this, rayorg, range_max, laser_raytrace_match, NULL, true );
+       
+       // trace the ray, incrementing its heading for each sample
   for( unsigned int t=0; t<sample_count; t += resolution )
     {
-                       rays[t].origin = rayorg;                
-      rayorg.a += sample_incr;
-               }
-  
-  // do the raytracing of all rays in one go (allows parallel implementation)
-  world->Raytrace( rays );
-  
-  // now process the results and fill in our samples
-  for( unsigned int t=0; t<sample_count; t += resolution )
-    {
-               stg_raytrace_result_t* r = &rays[t].result;
-               assert( r );
-
-      samples[t].range = r->range;
-               
+                       stg_raytrace_result_t r = ray.Trace();  
+                       samples[t].range = r.range;
+                       
       // if we hit a model and it reflects brightly, we set
       // reflectance high, else low
-      if( r->mod && ( r->mod->vis.laser_return >= LaserBright ) )      
-                 samples[t].reflectance = 1;
+      if( r.mod && ( r.mod->vis.laser_return >= LaserBright ) )        
+                               samples[t].reflectance = 1;
       else
-                 samples[t].reflectance = 0;           
+                               samples[t].reflectance = 0;             
+
+                       // point the ray to the next angle
+                       ray.origin.a += sample_incr;
     }
-    
+       
   // we may need to interpolate the samples we skipped 
   if( resolution > 1 )
     {

Modified: code/stage/trunk/libstage/stage.hh
===================================================================
--- code/stage/trunk/libstage/stage.hh  2009-06-03 06:28:26 UTC (rev 7767)
+++ code/stage/trunk/libstage/stage.hh  2009-06-03 07:38:57 UTC (rev 7768)
@@ -851,13 +851,21 @@
   class Ray
   {
   public:
-               Model* mod;
+               Ray( const Model* mod, const Pose& origin, const stg_meters_t 
range, const stg_ray_test_func_t func, const void* arg, const bool ztest ) :
+                       mod(mod), origin(origin), range(range), func(func), 
arg(arg), ztest(ztest)
+               {}
+
+               Ray() : mod(NULL), origin(0,0,0,0), range(0), func(NULL), 
arg(NULL), ztest(true)
+               {}
+
+               const Model* mod;
                Pose origin;
                stg_meters_t range;
                stg_ray_test_func_t func;
-               void* arg;
+               const void* arg;
                bool ztest;             
-               RaytraceResult result;   
+
+               RaytraceResult Trace();
   };
                
   const uint32_t INTERVAL_LOG_LEN = 32;
@@ -1013,12 +1021,9 @@
         
     SuperRegion* CreateSuperRegion( stg_point_int_t origin );
     void DestroySuperRegion( SuperRegion* sr );
-        
-        /** trace a vector of rays all in one go. */
-        void Raytrace( std::vector<Ray>& rays );
-        
+                
         /** trace a ray. */
-        void Raytrace( Ray& ray );
+        stg_raytrace_result_t Raytrace( const Ray& ray );
 
     stg_raytrace_result_t Raytrace( const Pose& pose,                   
                                                                                
                const stg_meters_t range,
@@ -1737,6 +1742,7 @@
         friend class Region;
         friend class BlockGroup;
         friend class PowerPack;
+        friend class Ray;
 
   private:
         /** the number of models instatiated - used to assign unique IDs */
@@ -2499,8 +2505,9 @@
                
                unsigned int sample_count;
                std::vector<Sample> samples;
-       std::vector<Ray> rays;
-               
+       //std::vector<Ray> rays;
+               //Ray ray;
+
                stg_meters_t range_max;
                stg_radians_t fov;
                uint32_t resolution;

Modified: code/stage/trunk/libstage/world.cc
===================================================================
--- code/stage/trunk/libstage/world.cc  2009-06-03 06:28:26 UTC (rev 7767)
+++ code/stage/trunk/libstage/world.cc  2009-06-03 07:38:57 UTC (rev 7768)
@@ -54,6 +54,11 @@
 using namespace Stg;
 
 
+RaytraceResult Ray::Trace()
+{ 
+       return mod->world->Raytrace( origin, range, func, mod, arg, ztest );
+}
+
 // static data members
 unsigned int World::next_id = 0;
 bool World::quit_all = false;
@@ -597,24 +602,6 @@
 }
 
 
-void World::Raytrace( std::vector<Ray>& rays )
-{
-  rt_cells.clear();
-  rt_candidate_cells.clear();
-
-  //printf( "===================\n" );
-
-  for( std::vector<Ray>::iterator it = rays.begin();
-                it != rays.end();
-                ++it )
-        Raytrace( *it );
-}
-
-inline void World::Raytrace( Ray& r )
-{
-  r.result = Raytrace( r.origin, r.range, r.func, r.mod, r.arg, r.ztest );
-}
-
 void World::Raytrace( const Pose &gpose, // global pose
                                                         const stg_meters_t 
range,
                                                         const stg_radians_t 
fov,
@@ -643,33 +630,39 @@
 
 // Stage spends 50-99% of its time in this method.
 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 ) 
 {
+       Ray r( mod, gpose, range, func, arg, ztest );
+       return Raytrace( r );
+}
+               
+stg_raytrace_result_t World::Raytrace( const Ray& r )
+{
   //rt_cells.clear();
   //rt_candidate_cells.clear();
   
   // initialize the sample
-  RaytraceResult sample( gpose, range );
+  RaytraceResult sample( r.origin, r.range );
        
   // our global position in (floating point) cell coordinates
-  stg_point_t glob( gpose.x * ppm, gpose.y * ppm );
+  stg_point_t glob( r.origin.x * ppm, r.origin.y * ppm );
        
   // record our starting position
   const stg_point_int_t start( glob.x, glob.y );
   
   // eliminate a potential divide by zero
-  const double angle( gpose.a == 0.0 ? 1e-12 : gpose.a );
+  const double angle( r.origin.a == 0.0 ? 1e-12 : r.origin.a );
   const double cosa(cos(angle));
   const double sina(sin(angle));
   const double tana(sina/cosa); // = tan(angle)
 
   // and the x and y offsets of the ray
-  const int dx( ppm * range * cosa);
-  const int dy( ppm * range * sina);
+  const int dx( ppm * r.range * cosa);
+  const int dy( ppm * r.range * sina);
   
   // fast integer line 3d algorithm adapted from Cohen's code from
   // Graphics Gems IV  
@@ -719,9 +712,9 @@
                // coordinates of the region inside the superregion
                int32_t rx = GETREG(glob.x);
                int32_t ry = GETREG(glob.y);            
-               Region* r = &sr->regions[ rx + (ry*SuperRegion::WIDTH) ];
+               Region* reg = &sr->regions[ rx + (ry*SuperRegion::WIDTH) ];
 
-               if( r->count ) // if the region contains any objects
+               if( reg->count ) // if the region contains any objects
                  {
                         // invalidate the region crossing points used to jump 
over
                         // empty regions
@@ -731,7 +724,7 @@
                         int32_t cx = GETCELL(glob.x); 
                         int32_t cy = GETCELL(glob.y);
                         
-                        Cell* c = &r->cells[ cx + cy * Region::WIDTH ];
+                        Cell* c = &reg->cells[ cx + cy * Region::WIDTH ];
                         assert(c); // should be a cell there 
                         
                         // while within the bounds of this region and while 
some ray remains
@@ -749,13 +742,13 @@
                                                Block* block = *it;
                                                
                                                // skip if not in the right z 
range
-                                               if( ztest && 
-                                                        ( gpose.z < 
block->global_z.min || 
-                                                                        
gpose.z > block->global_z.max ) )
+                                               if( r.ztest && 
+                                                        ( r.origin.z < 
block->global_z.min || 
+                                                                        
r.origin.z > block->global_z.max ) )
                                                  continue; 
                                                
                                                // test the predicate we were 
passed
-                                               if( (*func)( block->mod, 
(Model*)mod, arg )) 
+                                               if( (*r.func)( block->mod, 
(Model*)r.mod, r.arg )) 
                                                  {
                                                         // a hit!
                                                         sample.color = 
block->GetColor();


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

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
looking to deploy the next generation of Solaris that includes the latest 
innovations from Sun and the OpenSource community. Download a copy and 
enjoy capabilities such as Networking, Storage and Virtualization. 
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to