17 #include <simgear/constants.h>
18 #include <simgear/sg_inlines.h>
19 #include <simgear/debug/logstream.hxx>
20 #include <simgear/math/point3d.hxx>
21 #include <simgear/math/sg_geodesy.hxx>
22 #include <simgear/math/vector.hxx>
24 #include <Main/globals.hxx>
25 #include <Main/viewer.hxx>
27 #include "hitlist.hxx"
30 extern ssgBranch *terrain;
33 // check to see if the intersection point is
34 // actually inside this face
35 static bool pointInTriangle( sgdVec3 point, sgdVec3 tri[3] )
37 double xmin, xmax, ymin, ymax, zmin, zmax;
39 // punt if outside bouding cube
40 if ( point[0] < (xmin = SG_MIN3 (tri[0][0], tri[1][0], tri[2][0])) ) {
42 } else if ( point[0] > (xmax = SG_MAX3 (tri[0][0], tri[1][0], tri[2][0])) ) {
44 } else if ( point[1] < (ymin = SG_MIN3 (tri[0][1], tri[1][1], tri[2][1])) ) {
46 } else if ( point[1] > (ymax = SG_MAX3 (tri[0][1], tri[1][1], tri[2][1])) ) {
48 } else if ( point[2] < (zmin = SG_MIN3 (tri[0][2], tri[1][2], tri[2][2])) ) {
50 } else if ( point[2] > (zmax = SG_MAX3 (tri[0][2], tri[1][2], tri[2][2])) ) {
54 // (finally) check to see if the intersection point is
55 // actually inside this face
57 //first, drop the smallest dimension so we only have to work
59 double dx = xmax - xmin;
60 double dy = ymax - ymin;
61 double dz = zmax - zmin;
62 double min_dim = SG_MIN3 (dx, dy, dz);
64 //first, drop the smallest dimension so we only have to work
66 double x1, y1, x2, y2, x3, y3, rx, ry;
67 if ( fabs(min_dim-dx) <= SG_EPSILON ) {
68 // x is the smallest dimension
77 } else if ( fabs(min_dim-dy) <= SG_EPSILON ) {
78 // y is the smallest dimension
87 } else if ( fabs(min_dim-dz) <= SG_EPSILON ) {
88 // z is the smallest dimension
98 // all dimensions are really small so lets call it close
99 // enough and return a successful match
103 // check if intersection point is on the same side of p1 <-> p2 as p3
104 double tmp = (y2 - y3) / (x2 - x3);
105 int side1 = SG_SIGN (tmp * (rx - x3) + y3 - ry);
106 int side2 = SG_SIGN (tmp * (x1 - x3) + y3 - y1);
107 if ( side1 != side2 ) {
108 // printf("failed side 1 check\n");
112 // check if intersection point is on correct side of p2 <-> p3 as p1
113 tmp = (y3 - ry) / (x3 - rx);
114 side1 = SG_SIGN (tmp * (x2 - rx) + ry - y2);
115 side2 = SG_SIGN (tmp * (x1 - rx) + ry - y1);
116 if ( side1 != side2 ) {
117 // printf("failed side 2 check\n");
121 // check if intersection point is on correct side of p1 <-> p3 as p2
122 tmp = (y2 - ry) / (x2 - rx);
123 side1 = SG_SIGN (tmp * (x3 - rx) + ry - y3);
124 side2 = SG_SIGN (tmp * (x1 - rx) + ry - y1);
125 if ( side1 != side2 ) {
126 // printf("failed side 3 check\n");
133 static int sgdIsectInfLinePlane( sgdVec3 dst, const sgdVec3 l_org,
134 const sgdVec3 l_vec, const sgdVec4 plane )
136 SGDfloat tmp = sgdScalarProductVec3 ( l_vec, plane ) ;
138 /* Is line parallel to plane? */
140 if ( fabs ( tmp ) < FLT_EPSILON )
143 sgdScaleVec3 ( dst, l_vec, -( sgdScalarProductVec3 ( l_org, plane )
144 + plane[3] ) / tmp ) ;
145 sgdAddVec3 ( dst, l_org ) ;
151 static void sgdXformPnt3 ( sgdVec3 dst, const sgVec3 src, const sgdMat4 mat )
153 SGDfloat t0 = src[ 0 ] ;
154 SGDfloat t1 = src[ 1 ] ;
155 SGDfloat t2 = src[ 2 ] ;
157 dst[0] = ( t0 * mat[ 0 ][ 0 ] +
162 dst[1] = ( t0 * mat[ 0 ][ 1 ] +
167 dst[2] = ( t0 * mat[ 0 ][ 2 ] +
176 Find the intersection of an infinite line with a plane
177 (the line being defined by a point and direction).
179 Norman Vine -- nhv@yahoo.com (with hacks by Steve)
182 int sgdIsectInfLinePlane( sgdVec3 dst, sgdVec3 l_org,
183 sgdVec3 l_vec, sgdVec4 plane )
185 SGDfloat tmp = sgdScalarProductVec3 ( l_vec, plane ) ;
187 /* Is line parallel to plane? */
189 if ( sgdAbs ( tmp ) < DBL_EPSILON )
192 sgdScaleVec3 ( dst, l_vec, -( sgdScalarProductVec3 ( l_org, plane )
193 + plane[3] ) / tmp ) ;
194 sgdAddVec3 ( dst, l_org ) ;
201 * Given a point and a triangle lying on the same plane
202 * check to see if the point is inside the triangle
204 bool sgdPointInTriangle( sgdVec3 point, sgdVec3 tri[3] )
209 for( i=0; i<3; i++ ) {
211 SG_MIN_MAX3 ( min, max, tri[0][i], tri[1][i], tri[2][i] );
212 // punt if outside bouding cube
213 if( (point[i] < min) || (point[i] > max) )
218 // drop the smallest dimension so we only have to work in 2d.
219 SGDfloat min_dim = SG_MIN3 (dif[0], dif[1], dif[2]);
220 SGDfloat x1, y1, x2, y2, x3, y3, rx, ry;
221 if ( fabs(min_dim-dif[0]) <= DBL_EPSILON ) {
222 // x is the smallest dimension
231 } else if ( fabs(min_dim-dif[1]) <= DBL_EPSILON ) {
232 // y is the smallest dimension
241 } else if ( fabs(min_dim-dif[2]) <= DBL_EPSILON ) {
242 // z is the smallest dimension
252 // all dimensions are really small so lets call it close
253 // enough and return a successful match
257 // check if intersection point is on the same side of p1 <-> p2 as p3
258 SGDfloat tmp = (y2 - y3) / (x2 - x3);
259 int side1 = SG_SIGN (tmp * (rx - x3) + y3 - ry);
260 int side2 = SG_SIGN (tmp * (x1 - x3) + y3 - y1);
261 if ( side1 != side2 ) {
262 // printf("failed side 1 check\n");
266 // check if intersection point is on correct side of p2 <-> p3 as p1
267 tmp = (y3 - ry) / (x3 - rx);
268 side1 = SG_SIGN (tmp * (x2 - rx) + ry - y2);
269 side2 = SG_SIGN (tmp * (x1 - rx) + ry - y1);
270 if ( side1 != side2 ) {
271 // printf("failed side 2 check\n");
275 // check if intersection point is on correct side of p1 <-> p3 as p2
276 tmp = (y2 - ry) / (x2 - rx);
277 side1 = SG_SIGN (tmp * (x3 - rx) + ry - y3);
278 side2 = SG_SIGN (tmp * (x1 - rx) + ry - y1);
279 if ( side1 != side2 ) {
280 // printf("failed side 3 check\n");
289 Find the intersection of an infinite line with a leaf
290 the line being defined by a point and direction.
294 ssgLeaf pointer -- leaf
295 qualified matrix -- m
297 line direction -- dir
299 result -- intersection point
300 normal -- intersected tri's normal
303 true if intersection found
306 int FGHitList::IntersectLeaf( ssgLeaf *leaf, sgdMat4 m,
307 sgdVec3 orig, sgdVec3 dir )
310 for ( int i = 0; i < leaf->getNumTriangles(); ++i ) {
312 leaf->getTriangle( i, &i1, &i2, &i3 );
315 sgdSetVec3( tri[0], leaf->getVertex( i1 ) );
316 sgdSetVec3( tri[1], leaf->getVertex( i2 ) );
317 sgdSetVec3( tri[2], leaf->getVertex( i3 ) );
319 //avoid division by zero when two points are the same
320 if ( sgdEqualVec3(tri[0], tri[1]) ||
321 sgdEqualVec3(tri[1], tri[2]) ||
322 sgdEqualVec3(tri[2], tri[0]) ) {
327 sgdMakePlane( plane, tri[0], tri[1], tri[2] );
330 if( sgdIsectInfLinePlane( point, orig, dir, plane ) ) {
332 if( pointInTriangle( point, tri ) ) {
333 add(leaf,i,point,plane);
337 if( sgdPointInTriangle( point, tri ) ) {
338 // transform point into passed into desired coordinate frame
339 sgdXformPnt3( point, point, m );
340 add(leaf,i,point,plane);
348 void FGHitList::IntersectBranch( ssgBranch *branch, sgdMat4 m,
349 sgdVec3 orig, sgdVec3 dir )
353 // lookat vector in branch's coordinate frame
356 sgdTransposeNegateMat4( _m, m);
357 sgdXformPnt3( _orig, orig, _m );
358 sgdXformPnt3( _dir, dir, _m );
360 for ( ssgEntity *kid = branch->getKid( 0 );
362 kid = branch->getNextKid() )
364 if ( kid->getTraversalMask() & SSGTRAV_HOT ) {
365 bsphere = kid->getBSphere();
367 sgCopyVec3( fcenter, bsphere->getCenter() );
369 sgdSetVec3( center, fcenter );
370 sgdXformPnt3( center, m ) ;
371 // watch out for overflow
372 if ( sgdClosestPointToLineDistSquared( center, orig, dir ) <
373 double(bsphere->getRadius() * bsphere->getRadius()) )
375 // possible intersections
376 if ( kid->isAKindOf ( ssgTypeBranch() ) ) {
378 sgdCopyMat4(m_new, m);
379 if ( kid->isA( ssgTypeTransform() ) ) {
381 ((ssgTransform *)kid)->getTransform( fxform );
383 sgdSetMat4( xform, fxform );
384 sgdPreMultMat4( m_new, xform );
386 IntersectBranch( (ssgBranch *)kid, m_new, orig, dir );
387 } else if ( kid->isAKindOf( ssgTypeLeaf() ) ) {
388 IntersectLeaf( (ssgLeaf *)kid, m, _orig, _dir );
391 // end of the line for this branch
394 // branch requested not to be traversed
400 // This expects the inital m to the identity transform
401 void ssgGetEntityTransform(ssgEntity *branch, sgMat4 m )
403 for ( ssgEntity *parent = branch->getParent(0);
405 parent = parent->getNextParent() )
407 // recurse till we are at the scene root
408 // then just unwinding the stack should
409 // give us our cumulative transform :-) NHV
410 ssgGetEntityTransform( parent, m );
411 if ( parent->isA( ssgTypeTransform() ) ) {
413 ((ssgTransform *)parent)->getTransform( xform );
414 sgPreMultMat4( m, xform );
420 // return the passed entitity's bsphere's center point radius and
421 // fully formed current model matrix for entity
422 void ssgGetCurrentBSphere( ssgEntity *entity, sgVec3 center,
423 float *radius, sgMat4 m )
425 sgSphere *bsphere = entity->getBSphere();
426 *radius = (double)bsphere->getRadius();
427 sgCopyVec3( center, bsphere->getCenter() );
428 sgMakeIdentMat4 ( m ) ;
429 ssgGetEntityTransform( entity, m );
433 void FGHitList::IntersectCachedLeaf( sgdMat4 m,
434 sgdVec3 orig, sgdVec3 dir)
440 // ssgEntity *ent = last_hit();
441 ssgGetCurrentBSphere( last_hit(), fcenter, &radius, fxform );
444 sgdSetMat4( m, fxform );
445 sgdXformPnt3( center, m );
447 if ( sgdClosestPointToLineDistSquared( center, orig, dir ) <
448 double(radius*radius) )
450 IntersectLeaf( (ssgLeaf *)last_hit(), m, orig, dir );
456 void FGHitList::Intersect( ssgBranch *scene, sgdVec3 orig, sgdVec3 dir ) {
459 // #define USE_CACHED_HIT
461 #ifdef USE_CACHED_HIT
462 // This optimization gives a slight speedup
463 // but it precludes using the hitlist for dynamic
467 sgdMakeIdentMat4 ( m ) ;
468 IntersectCachedLeaf(m, orig, dir);
474 sgdMakeIdentMat4 ( m ) ;
475 IntersectBranch( scene, m, orig, dir);
477 #ifdef USE_CACHED_HIT
483 static void CurrentNormalInLocalPlane(sgVec3 dst, sgVec3 src) {
485 sgSetVec3(tmp, src[0], src[1], src[2] );
487 sgTransposeNegateMat4 ( TMP, globals->get_current_view()->get_UP() ) ;
488 sgXformVec3(tmp, tmp, TMP);
489 sgSetVec3(dst, tmp[2], tmp[1], tmp[0] );
493 // a temporary hack until we get everything rewritten with sgdVec3
494 static inline Point3D operator + (const Point3D& a, const sgdVec3 b)
496 return Point3D(a.x()+b[0], a.y()+b[1], a.z()+b[2]);
500 // Determine scenery altitude via ssg. Normally this just happens
501 // when we render the scene, but we'd also like to be able to do this
502 // explicitely. lat & lon are in radians. view_pos in current world
503 // coordinate translated near (0,0,0) (in meters.) Returns result in
505 bool fgCurrentElev( sgdVec3 abs_view_pos, sgdVec3 scenery_center,
507 double *terrain_elev, double *radius, double *normal)
510 sgdSubVec3( view_pos, abs_view_pos, scenery_center );
513 sgdCopyVec3(orig, view_pos );
514 sgdCopyVec3(dir, abs_view_pos );
516 hit_list->Intersect( terrain, orig, dir );
520 double result = -9999;
521 Point3D sc(scenery_center[0], scenery_center[1], scenery_center[2]) ;
523 int hitcount = hit_list->num_hits();
524 for ( int i = 0; i < hitcount; ++i ) {
525 geoc = sgCartToPolar3d( sc + hit_list->get_point(i) );
526 double lat_geod, alt, sea_level_r;
527 sgGeocToGeod(geoc.lat(), geoc.radius(), &lat_geod,
529 if ( alt > result && alt < 10000 ) {
535 if ( result > -9000 ) {
536 *terrain_elev = result;
537 *radius = geoc.radius();
539 sgSetVec3(tmp, hit_list->get_normal(this_hit));
540 // cout << "cur_normal: " << tmp[0] << " " << tmp[1] << " "
541 // << tmp[2] << endl;
542 /* ssgState *IntersectedLeafState =
543 ((ssgLeaf*)hit_list->get_entity(this_hit))->getState(); */
544 CurrentNormalInLocalPlane(tmp, tmp);
545 sgdSetVec3( normal, tmp );
546 // cout << "NED: " << tmp[0] << " " << tmp[1] << " " << tmp[2] << endl;
549 SG_LOG( SG_TERRAIN, SG_INFO, "no terrain intersection" );
551 float *up = globals->get_current_view()->get_world_up();
552 sgdSetVec3(normal, up[0], up[1], up[2]);