]> git.mxchange.org Git - flightgear.git/blob - Scenery/tile.cxx
Ground collision detection optimizations contributed by Norman Vine.
[flightgear.git] / Scenery / tile.cxx
1 // tile.cxx -- routines to handle a scenery tile
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@infoplane.com
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 // (Log is kept at end of this file)
23
24
25 #include <Include/fg_constants.h>
26 #include <Include/fg_types.h>
27 #include <Math/mat3.h>
28
29 #include "tile.hxx"
30
31
32 // return the sign of a value
33 #define FG_SIGN( x )  ((x) >= 0 ? 1 : -1)
34
35 // return min or max of two values
36 #define FG_MIN(A,B)     ((A) < (B) ? (A) :  (B))
37 #define FG_MAX(A,B)     ((A) > (B) ? (A) :  (B))
38
39
40 // Constructor
41 fgFRAGMENT::fgFRAGMENT ( void ) {
42 }
43
44
45 // Add a face to the face list
46 void fgFRAGMENT::add_face(int n1, int n2, int n3) {
47     fgFACE face;
48
49     face.n1 = n1;
50     face.n2 = n2;
51     face.n3 = n3;
52
53     faces.push_back(face);
54 }
55
56
57 /*
58 // return the sign of a value
59 static int fg_sign( double x ) {
60     if ( x >= 0 ) {
61         return(1);
62     } else {
63         return(-1);
64     }
65 }
66
67
68 // return the minimum of the three values
69 static double fg_min( double a, double b, double c ) {
70     double result;
71     result = a;
72     if (result > b) result = b;
73     if (result > c) result = c;
74
75     return(result);
76 }
77
78
79 // return the maximum of the three values
80 static double fg_max( double a, double b, double c ) {
81     double result;
82     result = a;
83     if (result < b) result = b;
84     if (result < c) result = c;
85
86     return(result);
87 }
88 */
89
90
91 // return the minimum of the three values
92 static double fg_min3 (double a, double b, double c)
93 {
94     return (a > b ? FG_MIN (b, c) : FG_MIN (a, c));
95 }
96
97
98 // return the maximum of the three values
99 static double fg_max3 (double a, double b, double c)
100 {
101   return (a < b ? FG_MAX (b, c) : FG_MAX (a, c));
102 }
103
104
105 // test if line intesects with this fragment.  p0 and p1 are the two
106 // line end points of the line.  If side_flag is true, check to see
107 // that end points are on opposite sides of face.  Returns 1 if it
108 // does, 0 otherwise.  If it intesects, result is the point of
109 // intersection
110
111 int fgFRAGMENT::intersect( fgPoint3d *end0, fgPoint3d *end1, int side_flag,
112                            fgPoint3d *result)
113 {
114     fgTILE *t;
115     fgFACE face;
116     MAT3vec v1, v2, n, center;
117     double p1[3], p2[3], p3[3];
118     double a, b, c, d;
119     double x0, y0, z0, x1, y1, z1, a1, b1, c1;
120     double t1, t2, t3;
121     double xmin, xmax, ymin, ymax, zmin, zmax;
122     double dx, dy, dz, min_dim, x2, y2, x3, y3, rx, ry;
123     int side1, side2;
124     list < fgFACE > :: iterator current;
125     list < fgFACE > :: iterator last;
126
127     // find the associated tile
128     t = (fgTILE *)tile_ptr;
129
130     // printf("Intersecting\n");
131
132     // traverse the face list for this fragment
133     current = faces.begin();
134     last = faces.end();
135     while ( current != last ) {
136         face = *current;
137         current++;
138
139         // printf(".");
140
141         // get face vertex coordinates
142         center[0] = t->center.x;
143         center[1] = t->center.y;
144         center[2] = t->center.z;
145
146         MAT3_ADD_VEC(p1, t->nodes[face.n1], center);
147         MAT3_ADD_VEC(p2, t->nodes[face.n2], center);
148         MAT3_ADD_VEC(p3, t->nodes[face.n3], center);
149
150         // printf("point 1 = %.2f %.2f %.2f\n", p1[0], p1[1], p1[2]);
151         // printf("point 2 = %.2f %.2f %.2f\n", p2[0], p2[1], p2[2]);
152         // printf("point 3 = %.2f %.2f %.2f\n", p3[0], p3[1], p3[2]);
153
154         // calculate two edge vectors, and the face normal
155         MAT3_SUB_VEC(v1, p2, p1);
156         MAT3_SUB_VEC(v2, p3, p1);
157         MAT3cross_product(n, v1, v2);
158
159         // calculate the plane coefficients for the plane defined by
160         // this face.  If n is the normal vector, n = (a, b, c) and p1
161         // is a point on the plane, p1 = (x0, y0, z0), then the
162         // equation of the line is a(x-x0) + b(y-y0) + c(z-z0) = 0
163         a = n[0];
164         b = n[1];
165         c = n[2];
166         d = a * p1[0] + b * p1[1] + c * p1[2];
167         // printf("a, b, c, d = %.2f %.2f %.2f %.2f\n", a, b, c, d);
168
169         // printf("p1(d) = %.2f\n", a * p1[0] + b * p1[1] + c * p1[2]);
170         // printf("p2(d) = %.2f\n", a * p2[0] + b * p2[1] + c * p2[2]);
171         // printf("p3(d) = %.2f\n", a * p3[0] + b * p3[1] + c * p3[2]);
172
173         // calculate the line coefficients for the specified line
174         x0 = end0->x;  x1 = end1->x;
175         y0 = end0->y;  y1 = end1->y;
176         z0 = end0->z;  z1 = end1->z;
177
178         if ( fabs(x1 - x0) > FG_EPSILON ) {
179             a1 = 1.0 / (x1 - x0);
180         } else {
181             // we got a big divide by zero problem here
182             a1 = 0.0;
183         }
184         b1 = y1 - y0;
185         c1 = z1 - z0;
186
187         // intersect the specified line with this plane
188         t1 = b * b1 * a1;
189         t2 = c * c1 * a1;
190
191         // printf("a = %.2f  t1 = %.2f  t2 = %.2f\n", a, t1, t2);
192
193         if ( fabs(a + t1 + t2) > FG_EPSILON ) {
194             result->x = (t1*x0 - b*y0 + t2*x0 - c*z0 + d) / (a + t1 + t2);
195             t3 = a1 * (result->x - x0);
196             result->y = b1 * t3 + y0;
197             result->z = c1 * t3 + z0;       
198             // printf("result(d) = %.2f\n", 
199             //        a * result->x + b * result->y + c * result->z);
200         } else {
201             // no intersection point
202             continue;
203         }
204
205         if ( side_flag ) {
206             // check to see if end0 and end1 are on opposite sides of
207             // plane
208             if ( (result->x - x0) > FG_EPSILON ) {
209                 t1 = result->x; t2 = x0; t3 = x1;
210             } else if ( (result->y - y0) > FG_EPSILON ) {
211                 t1 = result->y; t2 = y0; t3 = y1;
212             } else if ( (result->z - z0) > FG_EPSILON ) {
213                 t1 = result->z; t2 = z0; t3 = z1;
214             } else {
215                 // everything is too close together to tell the difference
216                 // so the current intersection point should work as good
217                 // as any
218                 return(1);
219             }
220             side1 = FG_SIGN (t1 - t2);
221             side2 = FG_SIGN (t1 - t3);
222             if ( side1 == side2 ) {
223                 // same side, punt
224                 continue;
225             }
226         }
227
228         // check to see if intersection point is in the bounding
229         // cube of the face
230         xmin = fg_min3 (p1[0], p2[0], p3[0]);
231         xmax = fg_max3 (p1[0], p2[0], p3[0]);
232         ymin = fg_min3 (p1[1], p2[1], p3[1]);
233         ymax = fg_max3 (p1[1], p2[1], p3[1]);
234         zmin = fg_min3 (p1[2], p2[2], p3[2]);
235         zmax = fg_max3 (p1[2], p2[2], p3[2]);
236         // printf("bounding cube = %.2f,%.2f,%.2f  %.2f,%.2f,%.2f\n",
237         //        xmin, ymin, zmin, xmax, ymax, zmax);
238         // punt if outside bouding cube
239         if ( result->x < xmin ) {
240             continue;
241         } else if ( result->x > xmax ) {
242             continue;
243         } else if ( result->y < ymin ) {
244             continue;
245         } else if ( result->y > ymax ) {
246             continue;
247         } else if ( result->z < zmin ) {
248             continue;
249         } else if ( result->z > zmax ) {
250             continue;
251         }
252
253         // (finally) check to see if the intersection point is
254         // actually inside this face
255
256         //first, drop the smallest dimension so we only have to work
257         //in 2d.
258         dx = xmax - xmin;
259         dy = ymax - ymin;
260         dz = zmax - zmin;
261         min_dim = fg_min3 (dx, dy, dz);
262         if ( fabs(min_dim - dx) <= FG_EPSILON ) {
263             // x is the smallest dimension
264             x1 = p1[1]; y1 = p1[2];
265             x2 = p2[1]; y2 = p2[2];
266             x3 = p3[1]; y3 = p3[2];
267             rx = result->y; ry = result->z;
268         } else if ( fabs(min_dim - dy) <= FG_EPSILON ) {
269             // y is the smallest dimension
270             x1 = p1[0]; y1 = p1[2];
271             x2 = p2[0]; y2 = p2[2];
272             x3 = p3[0]; y3 = p3[2];
273             rx = result->x; ry = result->z;
274         } else if ( fabs(min_dim - dz) <= FG_EPSILON ) {
275             // z is the smallest dimension
276             x1 = p1[0]; y1 = p1[1];
277             x2 = p2[0]; y2 = p2[1];
278             x3 = p3[0]; y3 = p3[1];
279             rx = result->x; ry = result->y;
280         } else {
281             // all dimensions are really small so lets call it close
282             // enough and return a successful match
283             return(1);
284         }
285
286         // check if intersection point is on the same side of p1 <-> p2 as p3
287         side1 = FG_SIGN ((y1 - y2) * ((x3) - x2) / (x1 - x2) + y2 - (y3));
288         side2 = FG_SIGN ((y1 - y2) * ((rx) - x2) / (x1 - x2) + y2 - (ry));
289         if ( side1 != side2 ) {
290             // printf("failed side 1 check\n");
291             continue;
292         }
293
294         // check if intersection point is on correct side of p2 <-> p3 as p1
295         side1 = FG_SIGN ((y2 - y3) * ((x1) - x3) / (x2 - x3) + y3 - (y1));
296         side2 = FG_SIGN ((y2 - y3) * ((rx) - x3) / (x2 - x3) + y3 - (ry));
297         if ( side1 != side2 ) {
298             // printf("failed side 2 check\n");
299             continue;
300         }
301
302         // check if intersection point is on correct side of p1 <-> p3 as p2
303         side1 = FG_SIGN ((y1 - y3) * ((x2) - x3) / (x1 - x3) + y3 - (y2));
304         side2 = FG_SIGN ((y1 - y3) * ((rx) - x3) / (x1 - x3) + y3 - (ry));
305         if ( side1 != side2 ) {
306             // printf("failed side 3  check\n");
307             continue;
308         }
309
310         // printf( "intersection point = %.2f %.2f %.2f\n", 
311         //         result->x, result->y, result->z);
312         return(1);
313     }
314
315     // printf("\n");
316
317     return(0);
318 }
319
320
321 // Destructor
322 fgFRAGMENT::~fgFRAGMENT ( void ) {
323     // Step through the face list deleting the items until the list is
324     // empty
325
326     // printf("destructing a fragment with %d faces\n", faces.size());
327
328     while ( faces.size() ) {
329         //  printf("emptying face list\n");
330         faces.pop_front();
331     }
332 }
333
334
335 // Constructor
336 fgTILE::fgTILE ( void ) {
337     nodes = new double[MAX_NODES][3];
338 }
339
340
341 // Destructor
342 fgTILE::~fgTILE ( void ) {
343     free(nodes);
344 }
345
346
347 // $Log$
348 // Revision 1.3  1998/07/16 17:34:24  curt
349 // Ground collision detection optimizations contributed by Norman Vine.
350 //
351 // Revision 1.2  1998/07/12 03:18:28  curt
352 // Added ground collision detection.  This involved:
353 // - saving the entire vertex list for each tile with the tile records.
354 // - saving the face list for each fragment with the fragment records.
355 // - code to intersect the current vertical line with the proper face in
356 //   an efficient manner as possible.
357 // Fixed a bug where the tiles weren't being shifted to "near" (0,0,0)
358 //
359 // Revision 1.1  1998/05/23 14:09:21  curt
360 // Added tile.cxx and tile.hxx.
361 // Working on rewriting the tile management system so a tile is just a list
362 // fragments, and the fragment record contains the display list for that fragment.
363 //