13 #include <simgear/constants.h>
14 #include <simgear/inlines.h>
15 #include <simgear/math/vector.hxx>
16 #include <simgear/xgl/xgl.h>
18 #include "hitlist.hxx"
21 // check to see if the intersection point is
22 // actually inside this face
23 static bool sgdPointInTriangle( sgdVec3 point, sgdVec3 tri[3] )
25 double xmin, xmax, ymin, ymax, zmin, zmax;
27 // punt if outside bouding cube
28 if ( point[0] < (xmin = SG_MIN3 (tri[0][0], tri[1][0], tri[2][0])) ) {
30 } else if ( point[0] > (xmax = SG_MAX3 (tri[0][0], tri[1][0], tri[2][0])) ) {
32 } else if ( point[1] < (ymin = SG_MIN3 (tri[0][1], tri[1][1], tri[2][1])) ) {
34 } else if ( point[1] > (ymax = SG_MAX3 (tri[0][1], tri[1][1], tri[2][1])) ) {
36 } else if ( point[2] < (zmin = SG_MIN3 (tri[0][2], tri[1][2], tri[2][2])) ) {
38 } else if ( point[2] > (zmax = SG_MAX3 (tri[0][2], tri[1][2], tri[2][2])) ) {
42 // (finally) check to see if the intersection point is
43 // actually inside this face
45 //first, drop the smallest dimension so we only have to work
47 double dx = xmax - xmin;
48 double dy = ymax - ymin;
49 double dz = zmax - zmin;
50 double min_dim = SG_MIN3 (dx, dy, dz);
52 //first, drop the smallest dimension so we only have to work
54 double x1, y1, x2, y2, x3, y3, rx, ry;
55 if ( fabs(min_dim-dx) <= FG_EPSILON ) {
56 // x is the smallest dimension
65 } else if ( fabs(min_dim-dy) <= FG_EPSILON ) {
66 // y is the smallest dimension
75 } else if ( fabs(min_dim-dz) <= FG_EPSILON ) {
76 // z is the smallest dimension
86 // all dimensions are really small so lets call it close
87 // enough and return a successful match
91 // check if intersection point is on the same side of p1 <-> p2 as p3
92 double tmp = (y2 - y3) / (x2 - x3);
93 int side1 = SG_SIGN (tmp * (rx - x3) + y3 - ry);
94 int side2 = SG_SIGN (tmp * (x1 - x3) + y3 - y1);
95 if ( side1 != side2 ) {
96 // printf("failed side 1 check\n");
100 // check if intersection point is on correct side of p2 <-> p3 as p1
101 tmp = (y3 - ry) / (x3 - rx);
102 side1 = SG_SIGN (tmp * (x2 - rx) + ry - y2);
103 side2 = SG_SIGN (tmp * (x1 - rx) + ry - y1);
104 if ( side1 != side2 ) {
105 // printf("failed side 2 check\n");
109 // check if intersection point is on correct side of p1 <-> p3 as p2
110 tmp = (y2 - ry) / (x2 - rx);
111 side1 = SG_SIGN (tmp * (x3 - rx) + ry - y3);
112 side2 = SG_SIGN (tmp * (x1 - rx) + ry - y1);
113 if ( side1 != side2 ) {
114 // printf("failed side 3 check\n");
121 static int sgdIsectInfLinePlane( sgdVec3 dst, const sgdVec3 l_org,
122 const sgdVec3 l_vec, const sgdVec4 plane )
124 SGDfloat tmp = sgdScalarProductVec3 ( l_vec, plane ) ;
126 /* Is line parallel to plane? */
128 if ( fabs ( tmp ) < FLT_EPSILON )
131 sgdScaleVec3 ( dst, l_vec, -( sgdScalarProductVec3 ( l_org, plane )
132 + plane[3] ) / tmp ) ;
133 sgdAddVec3 ( dst, l_org ) ;
139 static void sgdXformPnt3 ( sgdVec3 dst, const sgVec3 src, const sgdMat4 mat )
141 SGDfloat t0 = src[ 0 ] ;
142 SGDfloat t1 = src[ 1 ] ;
143 SGDfloat t2 = src[ 2 ] ;
145 dst[0] = ( t0 * mat[ 0 ][ 0 ] +
150 dst[1] = ( t0 * mat[ 0 ][ 1 ] +
155 dst[2] = ( t0 * mat[ 0 ][ 2 ] +
162 Find the intersection of an infinite line with a leaf
163 the line being defined by a point and direction.
167 ssgLeaf pointer -- leaf
168 qualified matrix -- m
170 line direction -- dir
172 result -- intersection point
173 normal -- intersected tri's normal
176 true if intersection found
179 int FGHitList::IntersectLeaf( ssgLeaf *leaf, sgdMat4 m,
180 sgdVec3 orig, sgdVec3 dir )
183 for ( int i = 0; i < leaf->getNumTriangles(); ++i ) {
185 leaf->getTriangle( i, &i1, &i2, &i3 );
188 sgdXformPnt3( tri[0], leaf->getVertex( i1 ), m );
189 sgdXformPnt3( tri[1], leaf->getVertex( i2 ), m );
190 sgdXformPnt3( tri[2], leaf->getVertex( i3 ), m );
193 sgdMakePlane( plane, tri[0], tri[1], tri[2] );
196 if( sgdIsectInfLinePlane( point, orig, dir, plane ) ) {
197 if( sgdPointInTriangle( point, tri ) ) {
198 add(leaf,i,point,plane);
206 void FGHitList::IntersectBranch( ssgBranch *branch, sgdMat4 m,
207 sgdVec3 orig, sgdVec3 dir )
210 for ( ssgEntity *kid = branch->getKid( 0 );
212 kid = branch->getNextKid() )
214 if ( kid->getTraversalMask() & SSGTRAV_HOT ) {
215 bsphere = kid->getBSphere();
217 sgCopyVec3( fcenter, bsphere->getCenter() );
219 center[0] = fcenter[0];
220 center[1] = fcenter[1];
221 center[2] = fcenter[2];
222 sgdXformPnt3( center, m ) ;
223 double radius_sqd = bsphere->getRadius() * bsphere->getRadius();
224 if ( sgdPointLineDistSquared( center, orig, dir ) < radius_sqd ) {
225 // possible intersections
226 if ( kid->isAKindOf ( ssgTypeBranch() ) ) {
228 sgdCopyMat4(m_new, m);
229 if ( kid->isA( ssgTypeTransform() ) ) {
231 ((ssgTransform *)kid)->getTransform( fxform );
233 sgdSetMat4( xform, fxform );
234 sgdPreMultMat4( m_new, xform );
236 IntersectBranch( (ssgBranch *)kid, m_new, orig, dir );
237 } else if ( kid->isAKindOf ( ssgTypeLeaf() ) ) {
238 IntersectLeaf( (ssgLeaf *)kid, m, orig, dir );
241 // end of the line for this branch
244 // branch requested not to be traversed
250 // This expects the inital m to the identity transform
251 void ssgGetEntityTransform(ssgEntity *branch, sgMat4 m )
253 for ( ssgEntity *parent = branch->getParent(0);
255 parent = parent->getNextParent() )
257 // recurse till we are at the scene root
258 // then just unwinding the stack should
259 // give us our cumulative transform :-) NHV
260 ssgGetEntityTransform( parent, m );
261 if ( parent->isA( ssgTypeTransform() ) ) {
263 ((ssgTransform *)parent)->getTransform( xform );
264 sgPreMultMat4( m, xform );
270 // return the passed entitity's bsphere's center point radius and
271 // fully formed current model matrix for entity
272 void ssgGetCurrentBSphere( ssgEntity *entity, sgVec3 center,
273 float *radius, sgMat4 m )
275 sgSphere *bsphere = entity->getBSphere();
276 *radius = (double)bsphere->getRadius();
277 sgCopyVec3( center, bsphere->getCenter() );
278 sgMakeIdentMat4 ( m ) ;
279 ssgGetEntityTransform( entity, m );
283 void FGHitList::IntersectCachedLeaf( sgdMat4 m,
284 sgdVec3 orig, sgdVec3 dir)
290 // ssgEntity *ent = last_hit();
291 ssgGetCurrentBSphere( last_hit(), fcenter, &radius, fxform );
294 sgdSetMat4( m, fxform );
295 sgdXformPnt3( center, m );
297 if ( sgdPointLineDistSquared( center, orig, dir ) < radius*radius ) {
298 IntersectLeaf( (ssgLeaf *)last_hit(), m, orig, dir );