Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 595 lines (498 sloc) 13.946 kB
ec56d04 header clean up
rtv authored
1 #include "region.hh"
2 #include "worldfile.hh"
ec21888 removed world size limitation
rtv authored
3
ec56d04 header clean up
rtv authored
4 using namespace Stg;
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
5 using std::vector;
6
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
7 static void canonicalize_winding(vector<point_t>& pts);
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
8
ec21888 removed world size limitation
rtv authored
9
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
10 /** Create a new block. A model's body is a list of these
cfa70a3 experimenting with wavefront planner
rtv authored
11 blocks. The point data is copied, so pts can safely be freed
12 after calling this.*/
9efe76f fixing some memory leaks
thjc authored
13 Block::Block( Model* mod,
e8d54f8 replacing some C style arrays with vectors and strings, for consistency
Richard Vaughan authored
14 const std::vector<point_t>& pts,
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
15 meters_t zmin,
16 meters_t zmax,
6ec6357 merged Apple/Linux conditional build from Rich via Jenny
Richard Vaughan authored
17 Color color,
18 bool inherit_color,
19 bool wheel ) :
89dcdaf major changes for 3.1
rtv authored
20 mod( mod ),
17f2908 cleaning up Block class
rtv authored
21 mpts(),
e8d54f8 replacing some C style arrays with vectors and strings, for consistency
Richard Vaughan authored
22 pts(pts),
ff51521 cleaning up and STLization of BlockGroup
rtv authored
23 local_z( zmin, zmax ),
89dcdaf major changes for 3.1
rtv authored
24 color( color ),
25 inherit_color( inherit_color ),
6ec6357 merged Apple/Linux conditional build from Rich via Jenny
Richard Vaughan authored
26 wheel(wheel),
55b9c56 @rtv simplified bitmap rendering (back to the old days) for speed and to a…
authored
27 rendered_cells(),
ff51521 cleaning up and STLization of BlockGroup
rtv authored
28 gpts()
89dcdaf major changes for 3.1
rtv authored
29 {
30 assert( mod );
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
31 canonicalize_winding(this->pts);
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
32 }
33
89dcdaf major changes for 3.1
rtv authored
34 /** A from-file constructor */
9efe76f fixing some memory leaks
thjc authored
35 Block::Block( Model* mod,
31e71c9 cleaning up
rtv authored
36 Worldfile* wf,
37 int entity)
89dcdaf major changes for 3.1
rtv authored
38 : mod( mod ),
1ce2551 cleaning up
rtv authored
39 mpts(),
17f2908 cleaning up Block class
rtv authored
40 pts(),
55b9c56 @rtv simplified bitmap rendering (back to the old days) for speed and to a…
authored
41 local_z(),
996c281 replaced some c-strings with std::strings
rtv authored
42 color(),
cfa70a3 experimenting with wavefront planner
rtv authored
43 inherit_color(true),
55b9c56 @rtv simplified bitmap rendering (back to the old days) for speed and to a…
authored
44 wheel(),
45 rendered_cells(),
46 gpts()
89dcdaf major changes for 3.1
rtv authored
47 {
48 assert(mod);
49 assert(wf);
50 assert(entity);
ff51521 cleaning up and STLization of BlockGroup
rtv authored
51
89dcdaf major changes for 3.1
rtv authored
52 Load( wf, entity );
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
53 canonicalize_winding(this->pts);
89dcdaf major changes for 3.1
rtv authored
54 }
55
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored
56 Block::~Block()
9efe76f fixing some memory leaks
thjc authored
57 {
89dcdaf major changes for 3.1
rtv authored
58 if( mapped ) UnMap();
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
59 }
60
41a7d4a grippers partially implemented. they don't lift anything yet
rtv authored
61 void Block::Translate( double x, double y )
62 {
aa783ff block refactoring
rtv authored
63 FOR_EACH( it, pts )
cfa70a3 experimenting with wavefront planner
rtv authored
64 {
aa783ff block refactoring
rtv authored
65 it->x += x;
66 it->y += y;
cfa70a3 experimenting with wavefront planner
rtv authored
67 }
ff51521 cleaning up and STLization of BlockGroup
rtv authored
68
41a7d4a grippers partially implemented. they don't lift anything yet
rtv authored
69 mod->blockgroup.BuildDisplayList( mod );
70 }
71
72 double Block::CenterY()
73 {
74 double min = billion;
75 double max = -billion;
ff51521 cleaning up and STLization of BlockGroup
rtv authored
76
aa783ff block refactoring
rtv authored
77 FOR_EACH( it, pts )
cfa70a3 experimenting with wavefront planner
rtv authored
78 {
aa783ff block refactoring
rtv authored
79 if( it->y > max ) max = it->y;
80 if( it->y < min ) min = it->y;
cfa70a3 experimenting with wavefront planner
rtv authored
81 }
ff51521 cleaning up and STLization of BlockGroup
rtv authored
82
41a7d4a grippers partially implemented. they don't lift anything yet
rtv authored
83 // return the value half way between max and min
84 return( min + (max - min)/2.0 );
85 }
86
87 double Block::CenterX()
88 {
89 double min = billion;
90 double max = -billion;
ff51521 cleaning up and STLization of BlockGroup
rtv authored
91
aa783ff block refactoring
rtv authored
92 FOR_EACH( it, pts )
cfa70a3 experimenting with wavefront planner
rtv authored
93 {
aa783ff block refactoring
rtv authored
94 if( it->x > max ) max = it->x;
95 if( it->x < min ) min = it->x;
cfa70a3 experimenting with wavefront planner
rtv authored
96 }
9efe76f fixing some memory leaks
thjc authored
97
41a7d4a grippers partially implemented. they don't lift anything yet
rtv authored
98 // return the value half way between maxx and min
99 return( min + (max - min)/2.0 );
100 }
101
102 void Block::SetCenter( double x, double y )
103 {
104 // move the block by the distance required to bring its center to
105 // the requested position
106 Translate( x-CenterX(), y-CenterY() );
107 }
108
109 void Block::SetCenterY( double y )
110 {
111 // move the block by the distance required to bring its center to
112 // the requested position
113 Translate( 0, y-CenterY() );
114 }
115
116 void Block::SetCenterX( double x )
117 {
118 // move the block by the distance required to bring its center to
119 // the requested position
120 Translate( x-CenterX(), 0 );
121 }
122
123 void Block::SetZ( double min, double max )
124 {
125 local_z.min = min;
126 local_z.max = max;
127
128 // force redraw
129 mod->blockgroup.BuildDisplayList( mod );
130 }
131
c5247e1 fixed alwayson and model property bugs. Fixed stage.pc to include GL …
rtv authored
132 const Color& Block::GetColor()
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
133 {
89dcdaf major changes for 3.1
rtv authored
134 return( inherit_color ? mod->color : color );
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
135 }
136
ff51521 cleaning up and STLization of BlockGroup
rtv authored
137 void Block::AppendTouchingModels( ModelPtrSet& touchers )
ff0dbdd working on charging model and demo
rtv authored
138 {
139 // for every cell we are rendered into
55b9c56 @rtv simplified bitmap rendering (back to the old days) for speed and to a…
authored
140 FOR_EACH( cell_it, rendered_cells )
141 // for every block rendered into that cell
142 FOR_EACH( block_it, (*cell_it)->GetBlocks() )
31e71c9 cleaning up
rtv authored
143 {
144 if( !mod->IsRelated( (*block_it)->mod ))
145 touchers.insert( (*block_it)->mod );
146 }
ff0dbdd working on charging model and demo
rtv authored
147 }
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
148
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored
149 Model* Block::TestCollision()
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
150 {
89dcdaf major changes for 3.1
rtv authored
151 //printf( "model %s block %p test collision...\n", mod->Token(), this );
152
153 // find the set of cells we would render into given the current global pose
55b9c56 @rtv simplified bitmap rendering (back to the old days) for speed and to a…
authored
154 //GenerateCandidateCells();
ff51521 cleaning up and STLization of BlockGroup
rtv authored
155
ff0dbdd working on charging model and demo
rtv authored
156 if( mod->vis.obstacle_return )
0e620eb added collision with ground (z=0 plane) using patch #2929553 from RAZOR
rtv authored
157 {
158 if ( global_z.min < 0 )
159 return this->mod->world->GetGround();
160
cfa70a3 experimenting with wavefront planner
rtv authored
161 // for every cell we may be rendered into
55b9c56 @rtv simplified bitmap rendering (back to the old days) for speed and to a…
authored
162 FOR_EACH( cell_it, rendered_cells )
cfa70a3 experimenting with wavefront planner
rtv authored
163 {
712d746 fixed Z collision detection for blocks
rtv authored
164 // for every block rendered into that cell
55b9c56 @rtv simplified bitmap rendering (back to the old days) for speed and to a…
authored
165 FOR_EACH( block_it, (*cell_it)->GetBlocks() )
712d746 fixed Z collision detection for blocks
rtv authored
166 {
167 Block* testblock = *block_it;
168 Model* testmod = testblock->mod;
169
170 //printf( " testing block %p of model %s\n", testblock, testmod->Token() );
171
172 // if the tested model is an obstacle and it's not attached to this model
173 if( (testmod != this->mod) &&
174 testmod->vis.obstacle_return &&
175 (!mod->IsRelated( testmod )) &&
176 // also must intersect in the Z range
177 testblock->global_z.min <= global_z.max &&
178 testblock->global_z.max >= global_z.min )
179 {
180 //puts( "HIT");
181 return testmod; // bail immediately with the bad news
182 }
183 }
184 }
0e620eb added collision with ground (z=0 plane) using patch #2929553 from RAZOR
rtv authored
185 }
712d746 fixed Z collision detection for blocks
rtv authored
186
89dcdaf major changes for 3.1
rtv authored
187 //printf( "model %s block %p collision done. no hits.\n", mod->Token(), this );
188 return NULL; // no hit
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
189 }
190
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored
191 void Block::Map()
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
192 {
55b9c56 @rtv simplified bitmap rendering (back to the old days) for speed and to a…
authored
193 // clear out of the old cells
194 RemoveFromCellArray( rendered_cells );
195
196 // now calculate the local coords of the block vertices
197 const unsigned int pt_count = pts.size();
198
199 if( mpts.size() == 0 )
200 {
201 // no valid cache of model coord points, so generate them
202 mpts.resize( pts.size() );
203
204 for( unsigned int i=0; i<pt_count; ++i )
205 mpts[i] = BlockPointToModelMeters( pts[i] );
206 }
207
208 // now calculate the global pixel coords of the block vertices
209 gpts.clear();
210 mod->LocalToPixels( mpts, gpts );
211
212 for( unsigned int i=0; i<pt_count; ++i )
213 MapLine( gpts[i],
214 gpts[(i+1)%pt_count] );
215
216 // update the block's absolute z bounds at this rendering
217 Pose gpose = mod->GetGlobalPose();
218 gpose.z += mod->geom.pose.z;
219 double scalez = mod->geom.size.z / mod->blockgroup.GetSize().z;
220 meters_t z = gpose.z - mod->blockgroup.GetOffset().z;
221 global_z.min = (scalez * local_z.min) + z;
222 global_z.max = (scalez * local_z.max) + z;
223
224 mapped = true;
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
225 }
226
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored
227 void Block::UnMap()
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
228 {
89dcdaf major changes for 3.1
rtv authored
229 RemoveFromCellArray( rendered_cells );
55b9c56 @rtv simplified bitmap rendering (back to the old days) for speed and to a…
authored
230 rendered_cells.clear();
89dcdaf major changes for 3.1
rtv authored
231 mapped = false;
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
232 }
233
0a5ba1d @rtv optmized FOR_EACH macro to assume container is not modified. other sm…
authored
234 #include <algorithm>
235 #include <functional>
236
55b9c56 @rtv simplified bitmap rendering (back to the old days) for speed and to a…
authored
237 inline void Block::RemoveFromCellArray( CellPtrVec& cells )
8aab383 moved bit to STL
rtv authored
238 {
0a5ba1d @rtv optmized FOR_EACH macro to assume container is not modified. other sm…
authored
239 // FOR_EACH( it, *cells )
240 // (*it)->RemoveBlock(this);
241
242 // this is equivalent to the above commented code - experimenting
243 // with optimizations
55b9c56 @rtv simplified bitmap rendering (back to the old days) for speed and to a…
authored
244 std::for_each( cells.begin(),
245 cells.end(),
0a5ba1d @rtv optmized FOR_EACH macro to assume container is not modified. other sm…
authored
246 std::bind2nd( std::mem_fun(&Cell::RemoveBlock), this));
8aab383 moved bit to STL
rtv authored
247 }
248
0075eb0 attempted bugfix in Player fiducials (untested)
rtv authored
249 void Block::SwitchToTestedCells()
8aab383 moved bit to STL
rtv authored
250 {
89dcdaf major changes for 3.1
rtv authored
251 }
22a1279 fixed indentation issues (converted to tabs)
alexcb authored
252
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
253 inline point_t Block::BlockPointToModelMeters( const point_t& bpt )
750e048 fixed raserizer scaling bug
rtv authored
254 {
255 Size bgsize = mod->blockgroup.GetSize();
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
256 point3_t bgoffset = mod->blockgroup.GetOffset();
9efe76f fixing some memory leaks
thjc authored
257
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
258 return point_t( (bpt.x - bgoffset.x) * (mod->geom.size.x/bgsize.x),
31e71c9 cleaning up
rtv authored
259 (bpt.y - bgoffset.y) * (mod->geom.size.y/bgsize.y));
750e048 fixed raserizer scaling bug
rtv authored
260 }
261
80cdf19 cleaning up code
rtv authored
262 void Block::InvalidateModelPointCache()
263 {
ff51521 cleaning up and STLization of BlockGroup
rtv authored
264 // this doesn't happen often, so this simple strategy isn't too wasteful
265 mpts.clear();
80cdf19 cleaning up code
rtv authored
266 }
eaba4cb geometry fixes & graphics tweaks
rtv authored
267
750e048 fixed raserizer scaling bug
rtv authored
268 void swap( int& a, int& b )
e96a997 added model rasterization
rtv authored
269 {
750e048 fixed raserizer scaling bug
rtv authored
270 int tmp = a;
271 a = b;
272 b = tmp;
e96a997 added model rasterization
rtv authored
273 }
274
9efe76f fixing some memory leaks
thjc authored
275 void Block::Rasterize( uint8_t* data,
31e71c9 cleaning up
rtv authored
276 unsigned int width,
277 unsigned int height,
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
278 meters_t cellwidth,
279 meters_t cellheight )
750e048 fixed raserizer scaling bug
rtv authored
280 {
281 //printf( "rasterize block %p : w: %u h: %u scale %.2f %.2f offset %.2f %.2f\n",
282 // this, width, height, scalex, scaley, offsetx, offsety );
e8d54f8 replacing some C style arrays with vectors and strings, for consistency
Richard Vaughan authored
283
284 const unsigned int pt_count = pts.size();
0a5ba1d @rtv optmized FOR_EACH macro to assume container is not modified. other sm…
authored
285 for( unsigned int i=0; i<pt_count; ++i )
750e048 fixed raserizer scaling bug
rtv authored
286 {
31e71c9 cleaning up
rtv authored
287 // convert points from local to model coords
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
288 point_t mpt1 = BlockPointToModelMeters( pts[i] );
289 point_t mpt2 = BlockPointToModelMeters( pts[(i+1)%pt_count] );
ff51521 cleaning up and STLization of BlockGroup
rtv authored
290
31e71c9 cleaning up
rtv authored
291 // record for debug visualization
292 mod->rastervis.AddPoint( mpt1.x, mpt1.y );
ff51521 cleaning up and STLization of BlockGroup
rtv authored
293
31e71c9 cleaning up
rtv authored
294 // shift to the bottom left of the model
295 mpt1.x += mod->geom.size.x/2.0;
296 mpt1.y += mod->geom.size.y/2.0;
297 mpt2.x += mod->geom.size.x/2.0;
298 mpt2.y += mod->geom.size.y/2.0;
ff51521 cleaning up and STLization of BlockGroup
rtv authored
299
31e71c9 cleaning up
rtv authored
300 // convert from meters to cells
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
301 point_int_t a( floor( mpt1.x / cellwidth ),
31e71c9 cleaning up
rtv authored
302 floor( mpt1.y / cellheight ));
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
303 point_int_t b( floor( mpt2.x / cellwidth ),
31e71c9 cleaning up
rtv authored
304 floor( mpt2.y / cellheight ) );
ff51521 cleaning up and STLization of BlockGroup
rtv authored
305
31e71c9 cleaning up
rtv authored
306 bool steep = abs( b.y-a.y ) > abs( b.x-a.x );
307 if( steep )
308 {
309 swap( a.x, a.y );
310 swap( b.x, b.y );
311 }
ff51521 cleaning up and STLization of BlockGroup
rtv authored
312
31e71c9 cleaning up
rtv authored
313 if( a.x > b.x )
314 {
315 swap( a.x, b.x );
316 swap( a.y, b.y );
317 }
ff51521 cleaning up and STLization of BlockGroup
rtv authored
318
31e71c9 cleaning up
rtv authored
319 double dydx = (double) (b.y - a.y) / (double) (b.x - a.x);
320 double y = a.y;
0a5ba1d @rtv optmized FOR_EACH macro to assume container is not modified. other sm…
authored
321 for(int x=a.x; x<=b.x; ++x)
31e71c9 cleaning up
rtv authored
322 {
323 if( steep )
324 {
325 if( ! (floor(y) >= 0) ) continue;
326 if( ! (floor(y) < (int)width) ) continue;
327 if( ! (x >= 0) ) continue;
328 if( ! (x < (int)height) ) continue;
329 }
330 else
331 {
332 if( ! (x >= 0) ) continue;
333 if( ! (x < (int)width) ) continue;
334 if( ! (floor(y) >= 0) ) continue;
335 if( ! (floor(y) < (int)height) ) continue;
336 }
ff51521 cleaning up and STLization of BlockGroup
rtv authored
337
31e71c9 cleaning up
rtv authored
338 if( steep )
339 data[ (int)floor(y) + (x * width)] = 1;
340 else
341 data[ x + ((int)floor(y) * width)] = 1;
342 y += dydx;
343 }
344 }
e96a997 added model rasterization
rtv authored
345 }
346
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored
347 void Block::DrawTop()
89dcdaf major changes for 3.1
rtv authored
348 {
349 // draw the top of the block - a polygon at the highest vertical
350 // extent
351 glBegin( GL_POLYGON);
31e71c9 cleaning up
rtv authored
352 FOR_EACH( it, pts )
353 glVertex3f( it->x, it->y, local_z.max );
89dcdaf major changes for 3.1
rtv authored
354 glEnd();
9efe76f fixing some memory leaks
thjc authored
355 }
22a1279 fixed indentation issues (converted to tabs)
alexcb authored
356
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored
357 void Block::DrawSides()
89dcdaf major changes for 3.1
rtv authored
358 {
9efe76f fixing some memory leaks
thjc authored
359 // construct a strip that wraps around the polygon
89dcdaf major changes for 3.1
rtv authored
360 glBegin(GL_QUAD_STRIP);
fec2ad5 cleaning up
rtv authored
361
31e71c9 cleaning up
rtv authored
362 FOR_EACH( it, pts )
363 {
fec2ad5 cleaning up
rtv authored
364 glVertex3f( it->x, it->y, local_z.max );
365 glVertex3f( it->x, it->y, local_z.min );
31e71c9 cleaning up
rtv authored
366 }
89dcdaf major changes for 3.1
rtv authored
367 // close the strip
368 glVertex3f( pts[0].x, pts[0].y, local_z.max );
369 glVertex3f( pts[0].x, pts[0].y, local_z.min );
370 glEnd();
371 }
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
372
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored
373 void Block::DrawFootPrint()
89dcdaf major changes for 3.1
rtv authored
374 {
fec2ad5 cleaning up
rtv authored
375 glBegin(GL_POLYGON);
31e71c9 cleaning up
rtv authored
376 FOR_EACH( it, pts )
377 glVertex2f( it->x, it->y );
89dcdaf major changes for 3.1
rtv authored
378 glEnd();
379 }
22a1279 fixed indentation issues (converted to tabs)
alexcb authored
380
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored
381 void Block::DrawSolid()
89dcdaf major changes for 3.1
rtv authored
382 {
6ec6357 merged Apple/Linux conditional build from Rich via Jenny
Richard Vaughan authored
383 // if( wheel )
384 // {
385 // glPushMatrix();
386
387 // glRotatef( 90,0,1,0 );
388 // glRotatef( 90,1,0,0 );
389
390 // glTranslatef( -local_z.max /2.0, 0, 0 );
391
392
393 // GLUquadric* quadric = gluNewQuadric();
394 // gluQuadricDrawStyle( quadric, GLU_FILL );
395 // gluCylinder( quadric, local_z.max, local_z.max, size.x, 16, 16 );
396 // gluDeleteQuadric( quadric );
397
398 // glPopMatrix();
399 // }
400 // else
401 {
402 DrawSides();
403 DrawTop();
404 }
89dcdaf major changes for 3.1
rtv authored
405 }
22a1279 fixed indentation issues (converted to tabs)
alexcb authored
406
3680786 API changes! removed the Stg prefix from all class names, and renamed…
rtv authored
407 void Block::Load( Worldfile* wf, int entity )
89dcdaf major changes for 3.1
rtv authored
408 {
e8d54f8 replacing some C style arrays with vectors and strings, for consistency
Richard Vaughan authored
409 const unsigned int pt_count = wf->ReadInt( entity, "points", 0);
410
9efe76f fixing some memory leaks
thjc authored
411 char key[128];
0a5ba1d @rtv optmized FOR_EACH macro to assume container is not modified. other sm…
authored
412 for( unsigned int p=0; p<pt_count; ++p )
31e71c9 cleaning up
rtv authored
413 {
414 snprintf(key, sizeof(key), "point[%d]", p );
415
e8d54f8 replacing some C style arrays with vectors and strings, for consistency
Richard Vaughan authored
416 pts.push_back( point_t( wf->ReadTupleLength(entity, key, 0, 0),
417 wf->ReadTupleLength(entity, key, 1, 0) ));
31e71c9 cleaning up
rtv authored
418 }
ff51521 cleaning up and STLization of BlockGroup
rtv authored
419
89dcdaf major changes for 3.1
rtv authored
420 local_z.min = wf->ReadTupleLength( entity, "z", 0, 0.0 );
421 local_z.max = wf->ReadTupleLength( entity, "z", 1, 1.0 );
ff51521 cleaning up and STLization of BlockGroup
rtv authored
422
6ec6357 merged Apple/Linux conditional build from Rich via Jenny
Richard Vaughan authored
423 const std::string& colorstr = wf->ReadString( entity, "color", "" );
424
996c281 replaced some c-strings with std::strings
rtv authored
425 if( colorstr != "" )
cfa70a3 experimenting with wavefront planner
rtv authored
426 {
4ccc5cd replaced stg_color_t with Color class and added static named construc…
rtv authored
427 color = Color( colorstr );
cfa70a3 experimenting with wavefront planner
rtv authored
428 inherit_color = false;
429 }
d60514d geometry & graphics fixes. added normalization of blocks to fit insid…
rtv authored
430 else
08e31b0 added option to prevent stacking of models, based on patch #2919630 f…
rtv authored
431 inherit_color = true;
6ec6357 merged Apple/Linux conditional build from Rich via Jenny
Richard Vaughan authored
432
433 wheel = wf->ReadInt( entity, "wheel", wheel );
36bfbdb first commit since shifting the Stage-3.0 development branch (opengl)…
rtv authored
434 }
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
435
55b9c56 @rtv simplified bitmap rendering (back to the old days) for speed and to a…
authored
436
437 void Block::MapLine( const point_int_t& start,
438 const point_int_t& end )
439 {
440 // line rasterization adapted from Cohen's 3D version in
441 // Graphics Gems II. Should be very fast.
442 const int32_t dx( end.x - start.x );
443 const int32_t dy( end.y - start.y );
444 const int32_t sx(sgn(dx));
445 const int32_t sy(sgn(dy));
446 const int32_t ax(abs(dx));
447 const int32_t ay(abs(dy));
448 const int32_t bx(2*ax);
449 const int32_t by(2*ay);
450 int32_t exy(ay-ax);
451 int32_t n(ax+ay);
452
453 int32_t globx(start.x);
454 int32_t globy(start.y);
455
456
457 World* w = mod->GetWorld();
458
459 while( n )
460 {
461 Region* reg( w->GetSuperRegion( GETSREG(globx), GETSREG(globy) )
462 ->GetRegion( GETREG(globx), GETREG(globy) ));
463
464 //printf( "REGION %p\n", reg );
465
466 // add all the required cells in this region before looking up
467 // another region
468 int32_t cx( GETCELL(globx) );
469 int32_t cy( GETCELL(globy) );
470
471 // need to call Region::GetCell() before using a Cell pointer
472 // directly, because the region allocates cells lazily, waiting
473 // for a call of this method
474 Cell* c( reg->GetCell( cx, cy ) );
475
476 // while inside the region, manipulate the Cell pointer directly
477 while( (cx>=0) && (cx<REGIONWIDTH) &&
478 (cy>=0) && (cy<REGIONWIDTH) &&
479 n > 0 )
480 {
481 c->AddBlock(this);
482
483 // cleverly skip to the next cell (now it's safe to
484 // manipulate the cell pointer)
485 if( exy < 0 )
486 {
487 globx += sx;
488 exy += by;
489 c += sx;
490 cx += sx;
491 }
492 else
493 {
494 globy += sy;
495 exy -= bx;
496 c += sy * REGIONWIDTH;
497 cy += sy;
498 }
499 --n;
500 }
501 }
502 }
503
08e31b0 added option to prevent stacking of models, based on patch #2919630 f…
rtv authored
504 /////////////////////////////////////////////////////////////////////////////////////////
505 // utility functions to ensure block winding is consistent and matches OpenGL's default
506
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
507 static
508 /// util; puts angle into [0, 2pi)
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
509 void positivize(radians_t& angle)
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
510 {
511 while (angle < 0) angle += 2 * M_PI;
512 }
513
514 static
515 /// util; puts angle into -pi/2, pi/2
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
516 void pi_ize(radians_t& angle)
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
517 {
518 while (angle < -M_PI) angle += 2 * M_PI;
519 while (M_PI < angle) angle -= 2 * M_PI;
520 }
521
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
522 typedef point_t V2;
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
523
524 static
525 /// util; How much was v1 rotated to get to v2?
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
526 radians_t angle_change(V2 v1, V2 v2)
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
527 {
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
528 radians_t a1 = atan2(v1.y, v1.x);
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
529 positivize(a1);
530
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
531 radians_t a2 = atan2(v2.y, v2.x);
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
532 positivize(a2);
533
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
534 radians_t angle_change = a2 - a1;
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
535 pi_ize(angle_change);
536
537 return angle_change;
538 }
539
540 static
541 /// util; find vectors between adjacent points, pts[next] - pts[cur]
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
542 vector<point_t> find_vectors(vector<point_t> const& pts)
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
543 {
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
544 vector<point_t> vs;
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
545 assert(2 <= pts.size());
546 for (unsigned i = 0, n = pts.size(); i < n; ++i)
547 {
548 unsigned j = (i + 1) % n;
549 vs.push_back(V2(pts[j].x - pts[i].x, pts[j].y - pts[i].y));
550 }
551 assert(vs.size() == pts.size());
552 return vs;
553 }
554
555 static
556 /// util; finds sum of angle changes, from each vertex to the
557 /// next one (in current ordering), wrapping around.
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
558 radians_t angles_sum(vector<point_t> const& vs)
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
559 {
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
560 radians_t angle_sum = 0;
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
561 for (unsigned i = 0, n = vs.size(); i < n; ++i)
562 {
563 unsigned j = (i + 1) % n;
564 angle_sum += angle_change(vs[i], vs[j]);
565 }
566 return angle_sum;
567 }
568
569 static
570 /// Util
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
571 bool is_canonical_winding(vector<point_t> const& ps)
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
572 {
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
573 // reuse point_t as vector
574 vector<point_t> vs = find_vectors(ps);
575 radians_t sum = angles_sum(vs);
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
576 bool bCanon = 0 < sum;
577
578 return bCanon;
579 }
580
581 static
582 /// util; sums angle changes to see whether it's 2pi or -2pi.
583 /// 2pi is counter-clockwise winding (which OpenGL requires),
584 /// -2pi is clockwise. Reverses <pts> when winding is clockwise.
585 // Note that a simple line that doubles back on itself has an
586 // angle sum of 0, but that's intrinsic to a line - its winding could
587 // be either way.
e45b70c combined laser and ranger functionality into ranger - still buggy
Richard Vaughan authored
588 void canonicalize_winding(vector<point_t>& ps)
e5a483c removing unused code. applied polygon winding patch from Jeff Donner
rtv authored
589 {
590 if (not is_canonical_winding(ps))
591 {
592 std::reverse(ps.begin(), ps.end());
593 }
594 }
Something went wrong with that request. Please try again.