Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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