15 #include <simgear/constants.h>
16 #include <simgear/sg_inlines.h>
17 #include <simgear/debug/logstream.hxx>
18 #include <simgear/math/point3d.hxx>
19 #include <simgear/math/sg_geodesy.hxx>
20 #include <simgear/math/vector.hxx>
22 #include <Main/globals.hxx>
23 #include <Main/viewer.hxx>
25 #include "hitlist.hxx"
28 extern ssgBranch *terrain;
31 // check to see if the intersection point is
32 // actually inside this face
33 static bool pointInTriangle( sgdVec3 point, sgdVec3 tri[3] )
35 double xmin, xmax, ymin, ymax, zmin, zmax;
37 // punt if outside bouding cube
38 if ( point[0] < (xmin = SG_MIN3 (tri[0][0], tri[1][0], tri[2][0])) ) {
40 } else if ( point[0] > (xmax = SG_MAX3 (tri[0][0], tri[1][0], tri[2][0])) ) {
42 } else if ( point[1] < (ymin = SG_MIN3 (tri[0][1], tri[1][1], tri[2][1])) ) {
44 } else if ( point[1] > (ymax = SG_MAX3 (tri[0][1], tri[1][1], tri[2][1])) ) {
46 } else if ( point[2] < (zmin = SG_MIN3 (tri[0][2], tri[1][2], tri[2][2])) ) {
48 } else if ( point[2] > (zmax = SG_MAX3 (tri[0][2], tri[1][2], tri[2][2])) ) {
52 // (finally) check to see if the intersection point is
53 // actually inside this face
55 //first, drop the smallest dimension so we only have to work
57 double dx = xmax - xmin;
58 double dy = ymax - ymin;
59 double dz = zmax - zmin;
60 double min_dim = SG_MIN3 (dx, dy, dz);
62 //first, drop the smallest dimension so we only have to work
64 double x1, y1, x2, y2, x3, y3, rx, ry;
65 if ( fabs(min_dim-dx) <= SG_EPSILON ) {
66 // x is the smallest dimension
75 } else if ( fabs(min_dim-dy) <= SG_EPSILON ) {
76 // y is the smallest dimension
85 } else if ( fabs(min_dim-dz) <= SG_EPSILON ) {
86 // z is the smallest dimension
96 // all dimensions are really small so lets call it close
97 // enough and return a successful match
101 // check if intersection point is on the same side of p1 <-> p2 as p3
102 double tmp = (y2 - y3) / (x2 - x3);
103 int side1 = SG_SIGN (tmp * (rx - x3) + y3 - ry);
104 int side2 = SG_SIGN (tmp * (x1 - x3) + y3 - y1);
105 if ( side1 != side2 ) {
106 // printf("failed side 1 check\n");
110 // check if intersection point is on correct side of p2 <-> p3 as p1
111 tmp = (y3 - ry) / (x3 - rx);
112 side1 = SG_SIGN (tmp * (x2 - rx) + ry - y2);
113 side2 = SG_SIGN (tmp * (x1 - rx) + ry - y1);
114 if ( side1 != side2 ) {
115 // printf("failed side 2 check\n");
119 // check if intersection point is on correct side of p1 <-> p3 as p2
120 tmp = (y2 - ry) / (x2 - rx);
121 side1 = SG_SIGN (tmp * (x3 - rx) + ry - y3);
122 side2 = SG_SIGN (tmp * (x1 - rx) + ry - y1);
123 if ( side1 != side2 ) {
124 // printf("failed side 3 check\n");
131 static int sgdIsectInfLinePlane( sgdVec3 dst, const sgdVec3 l_org,
132 const sgdVec3 l_vec, const sgdVec4 plane )
134 SGDfloat tmp = sgdScalarProductVec3 ( l_vec, plane ) ;
136 /* Is line parallel to plane? */
138 if ( fabs ( tmp ) < FLT_EPSILON )
141 sgdScaleVec3 ( dst, l_vec, -( sgdScalarProductVec3 ( l_org, plane )
142 + plane[3] ) / tmp ) ;
143 sgdAddVec3 ( dst, l_org ) ;
149 static void sgdXformPnt3 ( sgdVec3 dst, const sgVec3 src, const sgdMat4 mat )
151 SGDfloat t0 = src[ 0 ] ;
152 SGDfloat t1 = src[ 1 ] ;
153 SGDfloat t2 = src[ 2 ] ;
155 dst[0] = ( t0 * mat[ 0 ][ 0 ] +
160 dst[1] = ( t0 * mat[ 0 ][ 1 ] +
165 dst[2] = ( t0 * mat[ 0 ][ 2 ] +
172 Find the intersection of an infinite line with a leaf
173 the line being defined by a point and direction.
177 ssgLeaf pointer -- leaf
178 qualified matrix -- m
180 line direction -- dir
182 result -- intersection point
183 normal -- intersected tri's normal
186 true if intersection found
189 int FGHitList::IntersectLeaf( ssgLeaf *leaf, sgdMat4 m,
190 sgdVec3 orig, sgdVec3 dir )
193 for ( int i = 0; i < leaf->getNumTriangles(); ++i ) {
195 leaf->getTriangle( i, &i1, &i2, &i3 );
198 sgdXformPnt3( tri[0], leaf->getVertex( i1 ), m );
199 sgdXformPnt3( tri[1], leaf->getVertex( i2 ), m );
200 sgdXformPnt3( tri[2], leaf->getVertex( i3 ), m );
202 //avoid division by zero when two points are the same
203 if ( sgdEqualVec3(tri[0], tri[1]) ||
204 sgdEqualVec3(tri[1], tri[2]) ||
205 sgdEqualVec3(tri[2], tri[0]) ) {
210 sgdMakePlane( plane, tri[0], tri[1], tri[2] );
213 if( sgdIsectInfLinePlane( point, orig, dir, plane ) ) {
214 if( pointInTriangle( point, tri ) ) {
215 add(leaf,i,point,plane);
223 void FGHitList::IntersectBranch( ssgBranch *branch, sgdMat4 m,
224 sgdVec3 orig, sgdVec3 dir )
227 for ( ssgEntity *kid = branch->getKid( 0 );
229 kid = branch->getNextKid() )
231 if ( kid->getTraversalMask() & SSGTRAV_HOT ) {
232 bsphere = kid->getBSphere();
234 sgCopyVec3( fcenter, bsphere->getCenter() );
236 center[0] = fcenter[0];
237 center[1] = fcenter[1];
238 center[2] = fcenter[2];
239 sgdXformPnt3( center, m ) ;
240 // watch out for overflow
241 if ( sgdClosestPointToLineDistSquared( center, orig, dir ) <
242 double(bsphere->getRadius() * bsphere->getRadius()) )
244 // possible intersections
245 if ( kid->isAKindOf ( ssgTypeBranch() ) ) {
247 sgdCopyMat4(m_new, m);
248 if ( kid->isA( ssgTypeTransform() ) ) {
250 ((ssgTransform *)kid)->getTransform( fxform );
252 sgdSetMat4( xform, fxform );
253 sgdPreMultMat4( m_new, xform );
255 IntersectBranch( (ssgBranch *)kid, m_new, orig, dir );
256 } else if ( kid->isAKindOf ( ssgTypeLeaf() ) ) {
257 IntersectLeaf( (ssgLeaf *)kid, m, orig, dir );
260 // end of the line for this branch
263 // branch requested not to be traversed
269 // This expects the inital m to the identity transform
270 void ssgGetEntityTransform(ssgEntity *branch, sgMat4 m )
272 for ( ssgEntity *parent = branch->getParent(0);
274 parent = parent->getNextParent() )
276 // recurse till we are at the scene root
277 // then just unwinding the stack should
278 // give us our cumulative transform :-) NHV
279 ssgGetEntityTransform( parent, m );
280 if ( parent->isA( ssgTypeTransform() ) ) {
282 ((ssgTransform *)parent)->getTransform( xform );
283 sgPreMultMat4( m, xform );
289 // return the passed entitity's bsphere's center point radius and
290 // fully formed current model matrix for entity
291 void ssgGetCurrentBSphere( ssgEntity *entity, sgVec3 center,
292 float *radius, sgMat4 m )
294 sgSphere *bsphere = entity->getBSphere();
295 *radius = (double)bsphere->getRadius();
296 sgCopyVec3( center, bsphere->getCenter() );
297 sgMakeIdentMat4 ( m ) ;
298 ssgGetEntityTransform( entity, m );
302 void FGHitList::IntersectCachedLeaf( sgdMat4 m,
303 sgdVec3 orig, sgdVec3 dir)
309 // ssgEntity *ent = last_hit();
310 ssgGetCurrentBSphere( last_hit(), fcenter, &radius, fxform );
313 sgdSetMat4( m, fxform );
314 sgdXformPnt3( center, m );
316 if ( sgdClosestPointToLineDistSquared( center, orig, dir ) <
317 double(radius*radius) )
319 IntersectLeaf( (ssgLeaf *)last_hit(), m, orig, dir );
325 static void CurrentNormalInLocalPlane(sgVec3 dst, sgVec3 src) {
327 sgSetVec3(tmp, src[0], src[1], src[2] );
329 sgTransposeNegateMat4 ( TMP, globals->get_current_view()->get_UP() ) ;
330 sgXformVec3(tmp, tmp, TMP);
331 sgSetVec3(dst, tmp[2], tmp[1], tmp[0] );
335 // a temporary hack until we get everything rewritten with sgdVec3
336 static inline Point3D operator + (const Point3D& a, const sgdVec3 b)
338 return Point3D(a.x()+b[0], a.y()+b[1], a.z()+b[2]);
342 // Determine scenery altitude via ssg. Normally this just happens
343 // when we render the scene, but we'd also like to be able to do this
344 // explicitely. lat & lon are in radians. view_pos in current world
345 // coordinate translated near (0,0,0) (in meters.) Returns result in
347 bool fgCurrentElev( sgdVec3 abs_view_pos, sgdVec3 scenery_center,
349 double *terrain_elev, double *radius, double *normal)
352 sgdSubVec3( view_pos, abs_view_pos, scenery_center );
355 sgdCopyVec3(orig, view_pos );
356 sgdCopyVec3(dir, abs_view_pos );
358 hit_list->Intersect( terrain, orig, dir );
362 double result = -9999;
363 Point3D sc(scenery_center[0], scenery_center[1], scenery_center[2]) ;
365 int hitcount = hit_list->num_hits();
366 for ( int i = 0; i < hitcount; ++i ) {
367 geoc = sgCartToPolar3d( sc + hit_list->get_point(i) );
368 double lat_geod, alt, sea_level_r;
369 sgGeocToGeod(geoc.lat(), geoc.radius(), &lat_geod,
371 if ( alt > result && alt < 10000 ) {
377 if ( result > -9000 ) {
378 *terrain_elev = result;
379 *radius = geoc.radius();
381 sgSetVec3(tmp, hit_list->get_normal(this_hit));
382 // cout << "cur_normal: " << tmp[0] << " " << tmp[1] << " "
383 // << tmp[2] << endl;
384 /* ssgState *IntersectedLeafState =
385 ((ssgLeaf*)hit_list->get_entity(this_hit))->getState(); */
386 CurrentNormalInLocalPlane(tmp, tmp);
387 sgdSetVec3( normal, tmp );
388 // cout << "NED: " << tmp[0] << " " << tmp[1] << " " << tmp[2] << endl;
391 SG_LOG( SG_TERRAIN, SG_INFO, "no terrain intersection" );
393 float *up = globals->get_current_view()->get_world_up();
394 sgdSetVec3(normal, up[0], up[1], up[2]);