]> git.mxchange.org Git - flightgear.git/blob - src/Objects/fragment.cxx
Replaced Durk's mymath.* with plib/sg.h (contributed by Durk).
[flightgear.git] / src / Objects / fragment.cxx
1 // fragment.cxx -- routines to handle "atomic" display objects
2 //
3 // Written by Curtis Olson, started August 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <simgear/constants.h>
25 #include <simgear/math/point3d.hxx>
26
27 #include <Scenery/tileentry.hxx>
28
29 #include "fragment.hxx"
30
31
32 template <class T>
33 inline const int FG_SIGN(const T& x) {
34     return x < T(0) ? -1 : 1;
35 }
36
37 template <class T>
38 inline const T& FG_MIN(const T& a, const T& b) {
39     return b < a ? b : a;
40 }
41
42 template <class T>
43 inline const T& FG_MAX(const T& a, const T& b) {
44     return  a < b ? b : a;
45 }
46
47 // return the minimum of the three values
48 template <class T>
49 inline const T& fg_min3( const T& a, const T& b, const T& c)
50 {
51     return (a > b ? FG_MIN (b, c) : FG_MIN (a, c));
52 }
53
54
55 // return the maximum of the three values
56 template <class T>
57 inline const T& fg_max3 (const T& a, const T& b, const T& c)
58 {
59     return (a < b ? FG_MAX (b, c) : FG_MAX (a, c));
60 }
61
62 // Add a face to the face list
63 // Copy constructor
64 fgFRAGMENT::fgFRAGMENT ( const fgFRAGMENT & rhs ) :
65     center         ( rhs.center          ),
66     bounding_radius( rhs.bounding_radius ),
67     material_ptr   ( rhs.material_ptr    ),
68     tile_ptr       ( rhs.tile_ptr        ),
69     /* display_list   ( rhs.display_list    ), */
70     faces          ( rhs.faces           )
71 {
72 }
73
74 fgFRAGMENT & fgFRAGMENT::operator = ( const fgFRAGMENT & rhs )
75 {
76     if(!(this == &rhs )) {
77         center          = rhs.center;
78         bounding_radius = rhs.bounding_radius;
79         material_ptr    = rhs.material_ptr;
80         tile_ptr        = rhs.tile_ptr;
81         // display_list    = rhs.display_list;
82         faces           = rhs.faces;
83     }
84     return *this;
85 }
86
87
88 // test if line intesects with this fragment.  p0 and p1 are the two
89 // line end points of the line.  If side_flag is true, check to see
90 // that end points are on opposite sides of face.  Returns 1 if it
91 // intersection found, 0 otherwise.  If it intesects, result is the
92 // point of intersection
93
94 int fgFRAGMENT::intersect( const Point3D& end0,
95                            const Point3D& end1,
96                            int side_flag,
97                            Point3D& result) const
98 {
99     FGTileEntry *t;
100     sgVec3 v1, v2, n, center;
101     sgVec3 p1, p2, p3;
102     sgVec3 temp;
103     double x, y, z;  // temporary holding spot for result
104     double a, b, c, d;
105     double x0, y0, z0, x1, y1, z1, a1, b1, c1;
106     double t1, t2, t3;
107     double xmin, xmax, ymin, ymax, zmin, zmax;
108     double dx, dy, dz, min_dim, x2, y2, x3, y3, rx, ry;
109     int side1, side2;
110
111     // find the associated tile
112     t = tile_ptr;
113
114     // printf("Intersecting\n");
115
116     // traverse the face list for this fragment
117     const_iterator last = faces.end();
118     for ( const_iterator current = faces.begin(); current != last; ++current )
119     {
120         // printf(".");
121
122         // get face vertex coordinates
123         sgSetVec3( center, t->center.x(), t->center.y(), t->center.z() );
124
125         sgSetVec3( temp, t->nodes[(*current).n1].x(), 
126                    t->nodes[(*current).n1].y(), t->nodes[(*current).n1].z() );
127         sgAddVec3( p1, temp, center );
128
129         sgSetVec3( temp, t->nodes[(*current).n2].x(), 
130                    t->nodes[(*current).n2].y(), t->nodes[(*current).n2].z() );
131         sgAddVec3( p2, temp, center );
132
133         sgSetVec3( temp, t->nodes[(*current).n3].x(), 
134                    t->nodes[(*current).n3].y(), t->nodes[(*current).n3].z() );
135         sgAddVec3( p3, temp, center );
136
137         // printf("point 1 = %.2f %.2f %.2f\n", p1[0], p1[1], p1[2]);
138         // printf("point 2 = %.2f %.2f %.2f\n", p2[0], p2[1], p2[2]);
139         // printf("point 3 = %.2f %.2f %.2f\n", p3[0], p3[1], p3[2]);
140
141         // calculate two edge vectors, and the face normal
142         sgSubVec3( v1, p2, p1 );
143         sgSubVec3( v2, p3, p1 );
144         sgVectorProductVec3( n, v1, v2 );
145
146         // calculate the plane coefficients for the plane defined by
147         // this face.  If n is the normal vector, n = (a, b, c) and p1
148         // is a point on the plane, p1 = (x0, y0, z0), then the
149         // equation of the line is a(x-x0) + b(y-y0) + c(z-z0) = 0
150         a = n[0];
151         b = n[1];
152         c = n[2];
153         d = a * p1[0] + b * p1[1] + c * p1[2];
154         // printf("a, b, c, d = %.2f %.2f %.2f %.2f\n", a, b, c, d);
155
156         // printf("p1(d) = %.2f\n", a * p1[0] + b * p1[1] + c * p1[2]);
157         // printf("p2(d) = %.2f\n", a * p2[0] + b * p2[1] + c * p2[2]);
158         // printf("p3(d) = %.2f\n", a * p3[0] + b * p3[1] + c * p3[2]);
159
160         // calculate the line coefficients for the specified line
161         x0 = end0.x();  x1 = end1.x();
162         y0 = end0.y();  y1 = end1.y();
163         z0 = end0.z();  z1 = end1.z();
164
165         if ( fabs(x1 - x0) > FG_EPSILON ) {
166             a1 = 1.0 / (x1 - x0);
167         } else {
168             // we got a big divide by zero problem here
169             a1 = 0.0;
170         }
171         b1 = y1 - y0;
172         c1 = z1 - z0;
173
174         // intersect the specified line with this plane
175         t1 = b * b1 * a1;
176         t2 = c * c1 * a1;
177
178         // printf("a = %.2f  t1 = %.2f  t2 = %.2f\n", a, t1, t2);
179
180         if ( fabs(a + t1 + t2) > FG_EPSILON ) {
181             x = (t1*x0 - b*y0 + t2*x0 - c*z0 + d) / (a + t1 + t2);
182             t3 = a1 * (x - x0);
183             y = b1 * t3 + y0;
184             z = c1 * t3 + z0;       
185             // printf("result(d) = %.2f\n", a * x + b * y + c * z);
186         } else {
187             // no intersection point
188             continue;
189         }
190
191         if ( side_flag ) {
192             // check to see if end0 and end1 are on opposite sides of
193             // plane
194             if ( (x - x0) > FG_EPSILON ) {
195                 t1 = x;
196                 t2 = x0;
197                 t3 = x1;
198             } else if ( (y - y0) > FG_EPSILON ) {
199                 t1 = y;
200                 t2 = y0;
201                 t3 = y1;
202             } else if ( (z - z0) > FG_EPSILON ) {
203                 t1 = z;
204                 t2 = z0;
205                 t3 = z1;
206             } else {
207                 // everything is too close together to tell the difference
208                 // so the current intersection point should work as good
209                 // as any
210                 result = Point3D(x, y, z);
211                 return(1);
212             }
213             side1 = FG_SIGN (t1 - t2);
214             side2 = FG_SIGN (t1 - t3);
215             if ( side1 == side2 ) {
216                 // same side, punt
217                 continue;
218             }
219         }
220
221         // check to see if intersection point is in the bounding
222         // cube of the face
223 #ifdef XTRA_DEBUG_STUFF
224         xmin = fg_min3 (p1[0], p2[0], p3[0]);
225         xmax = fg_max3 (p1[0], p2[0], p3[0]);
226         ymin = fg_min3 (p1[1], p2[1], p3[1]);
227         ymax = fg_max3 (p1[1], p2[1], p3[1]);
228         zmin = fg_min3 (p1[2], p2[2], p3[2]);
229         zmax = fg_max3 (p1[2], p2[2], p3[2]);
230         printf("bounding cube = %.2f,%.2f,%.2f  %.2f,%.2f,%.2f\n",
231                xmin, ymin, zmin, xmax, ymax, zmax);
232 #endif
233         // punt if outside bouding cube
234         if ( x < (xmin = fg_min3 (p1[0], p2[0], p3[0])) ) {
235             continue;
236         } else if ( x > (xmax = fg_max3 (p1[0], p2[0], p3[0])) ) {
237             continue;
238         } else if ( y < (ymin = fg_min3 (p1[1], p2[1], p3[1])) ) {
239             continue;
240         } else if ( y > (ymax = fg_max3 (p1[1], p2[1], p3[1])) ) {
241             continue;
242         } else if ( z < (zmin = fg_min3 (p1[2], p2[2], p3[2])) ) {
243             continue;
244         } else if ( z > (zmax = fg_max3 (p1[2], p2[2], p3[2])) ) {
245             continue;
246         }
247
248         // (finally) check to see if the intersection point is
249         // actually inside this face
250
251         //first, drop the smallest dimension so we only have to work
252         //in 2d.
253         dx = xmax - xmin;
254         dy = ymax - ymin;
255         dz = zmax - zmin;
256         min_dim = fg_min3 (dx, dy, dz);
257         if ( fabs(min_dim - dx) <= FG_EPSILON ) {
258             // x is the smallest dimension
259             x1 = p1[1];
260             y1 = p1[2];
261             x2 = p2[1];
262             y2 = p2[2];
263             x3 = p3[1];
264             y3 = p3[2];
265             rx = y;
266             ry = z;
267         } else if ( fabs(min_dim - dy) <= FG_EPSILON ) {
268             // y is the smallest dimension
269             x1 = p1[0];
270             y1 = p1[2];
271             x2 = p2[0];
272             y2 = p2[2];
273             x3 = p3[0];
274             y3 = p3[2];
275             rx = x;
276             ry = z;
277         } else if ( fabs(min_dim - dz) <= FG_EPSILON ) {
278             // z is the smallest dimension
279             x1 = p1[0];
280             y1 = p1[1];
281             x2 = p2[0];
282             y2 = p2[1];
283             x3 = p3[0];
284             y3 = p3[1];
285             rx = x;
286             ry = y;
287         } else {
288             // all dimensions are really small so lets call it close
289             // enough and return a successful match
290             result = Point3D(x, y, z);
291             return 1;
292         }
293
294         // check if intersection point is on the same side of p1 <-> p2 as p3
295         t1 = (y1 - y2) / (x1 - x2);
296         side1 = FG_SIGN (t1 * ((x3) - x2) + y2 - (y3));
297         side2 = FG_SIGN (t1 * ((rx) - x2) + y2 - (ry));
298         if ( side1 != side2 ) {
299             // printf("failed side 1 check\n");
300             continue;
301         }
302
303         // check if intersection point is on correct side of p2 <-> p3 as p1
304         t1 = (y2 - y3) / (x2 - x3);
305         side1 = FG_SIGN (t1 * ((x1) - x3) + y3 - (y1));
306         side2 = FG_SIGN (t1 * ((rx) - x3) + y3 - (ry));
307         if ( side1 != side2 ) {
308             // printf("failed side 2 check\n");
309             continue;
310         }
311
312         // check if intersection point is on correct side of p1 <-> p3 as p2
313         t1 = (y1 - y3) / (x1 - x3);
314         side1 = FG_SIGN (t1 * ((x2) - x3) + y3 - (y2));
315         side2 = FG_SIGN (t1 * ((rx) - x3) + y3 - (ry));
316         if ( side1 != side2 ) {
317             // printf("failed side 3  check\n");
318             continue;
319         }
320
321         // printf( "intersection point = %.2f %.2f %.2f\n", x, y, z);
322         result = Point3D(x, y, z);
323         return 1;
324     }
325
326     // printf("\n");
327
328     return 0;
329 }
330