Skip to content

HTTPS clone URL

Subversion checkout URL

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