]> git.mxchange.org Git - flightgear.git/blob - Scenery/tile.cxx
material.cxx: whups, double method declaration with no definition.
[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 fgFACE :: fgFACE () : 
41     n1(0), n2(0), n3(0)
42 {
43 }
44
45 fgFACE :: ~fgFACE()
46 {
47 }
48
49 fgFACE :: fgFACE( const fgFACE & image ) :
50     n1( image.n1), n2( image.n2), n3( image.n3)
51 {
52 }
53
54 bool fgFACE :: operator < (const fgFACE & rhs )
55 {
56     return ( n1 < rhs.n1 ? true : false);
57 }
58
59 bool fgFACE :: operator == (const fgFACE & rhs )
60 {
61     return ((n1 == rhs.n1) && (n2 == rhs.n2) && ( n3 == rhs.n3));
62 }
63
64
65 // Constructor
66 fgFRAGMENT::fgFRAGMENT ( void ) {
67 }
68
69
70 // Copy constructor
71 fgFRAGMENT ::   fgFRAGMENT ( const fgFRAGMENT & rhs ) :
72     center         ( rhs.center          ),
73     bounding_radius( rhs.bounding_radius ),
74     material_ptr   ( rhs.material_ptr    ),
75     tile_ptr       ( rhs.tile_ptr        ),
76     display_list   ( rhs.display_list    ),
77     faces          ( rhs.faces           )
78 {
79 }
80
81 fgFRAGMENT & fgFRAGMENT :: operator = ( const fgFRAGMENT & rhs )
82 {
83     if(!(this == &rhs )) {
84         center          = rhs.center;
85         bounding_radius = rhs.bounding_radius;
86         material_ptr    = rhs.material_ptr;
87         tile_ptr        = rhs.tile_ptr;
88         // display_list    = rhs.display_list;
89         faces           = rhs.faces;
90     }
91     return *this;
92 }
93
94
95 // Add a face to the face list
96 void fgFRAGMENT::add_face(int n1, int n2, int n3) {
97     fgFACE face;
98
99     face.n1 = n1;
100     face.n2 = n2;
101     face.n3 = n3;
102
103     faces.push_back(face);
104 }
105
106
107 /*
108 // return the sign of a value
109 static int fg_sign( double x ) {
110     if ( x >= 0 ) {
111         return(1);
112     } else {
113         return(-1);
114     }
115 }
116
117
118 // return the minimum of the three values
119 static double fg_min( double a, double b, double c ) {
120     double result;
121     result = a;
122     if (result > b) result = b;
123     if (result > c) result = c;
124
125     return(result);
126 }
127
128
129 // return the maximum of the three values
130 static double fg_max( double a, double b, double c ) {
131     double result;
132     result = a;
133     if (result < b) result = b;
134     if (result < c) result = c;
135
136     return(result);
137 }
138 */
139
140
141 // return the minimum of the three values
142 static double fg_min3 (double a, double b, double c)
143 {
144     return (a > b ? FG_MIN (b, c) : FG_MIN (a, c));
145 }
146
147
148 // return the maximum of the three values
149 static double fg_max3 (double a, double b, double c)
150 {
151   return (a < b ? FG_MAX (b, c) : FG_MAX (a, c));
152 }
153
154
155 // test if line intesects with this fragment.  p0 and p1 are the two
156 // line end points of the line.  If side_flag is true, check to see
157 // that end points are on opposite sides of face.  Returns 1 if it
158 // does, 0 otherwise.  If it intesects, result is the point of
159 // intersection
160
161 int fgFRAGMENT::intersect( fgPoint3d *end0, fgPoint3d *end1, int side_flag,
162                            fgPoint3d *result)
163 {
164     fgTILE *t;
165     fgFACE face;
166     MAT3vec v1, v2, n, center;
167     double p1[3], p2[3], p3[3];
168     double x, y, z;  // temporary holding spot for result
169     double a, b, c, d;
170     double x0, y0, z0, x1, y1, z1, a1, b1, c1;
171     double t1, t2, t3;
172     double xmin, xmax, ymin, ymax, zmin, zmax;
173     double dx, dy, dz, min_dim, x2, y2, x3, y3, rx, ry;
174     int side1, side2;
175     list < fgFACE > :: iterator current;
176     list < fgFACE > :: iterator last;
177
178     // find the associated tile
179     t = (fgTILE *)tile_ptr;
180
181     // printf("Intersecting\n");
182
183     // traverse the face list for this fragment
184     current = faces.begin();
185     last = faces.end();
186     while ( current != last ) {
187         face = *current;
188         current++;
189
190         // printf(".");
191
192         // get face vertex coordinates
193         center[0] = t->center.x;
194         center[1] = t->center.y;
195         center[2] = t->center.z;
196
197         MAT3_ADD_VEC(p1, t->nodes[face.n1], center);
198         MAT3_ADD_VEC(p2, t->nodes[face.n2], center);
199         MAT3_ADD_VEC(p3, t->nodes[face.n3], center);
200
201         // printf("point 1 = %.2f %.2f %.2f\n", p1[0], p1[1], p1[2]);
202         // printf("point 2 = %.2f %.2f %.2f\n", p2[0], p2[1], p2[2]);
203         // printf("point 3 = %.2f %.2f %.2f\n", p3[0], p3[1], p3[2]);
204
205         // calculate two edge vectors, and the face normal
206         MAT3_SUB_VEC(v1, p2, p1);
207         MAT3_SUB_VEC(v2, p3, p1);
208         MAT3cross_product(n, v1, v2);
209
210         // calculate the plane coefficients for the plane defined by
211         // this face.  If n is the normal vector, n = (a, b, c) and p1
212         // is a point on the plane, p1 = (x0, y0, z0), then the
213         // equation of the line is a(x-x0) + b(y-y0) + c(z-z0) = 0
214         a = n[0];
215         b = n[1];
216         c = n[2];
217         d = a * p1[0] + b * p1[1] + c * p1[2];
218         // printf("a, b, c, d = %.2f %.2f %.2f %.2f\n", a, b, c, d);
219
220         // printf("p1(d) = %.2f\n", a * p1[0] + b * p1[1] + c * p1[2]);
221         // printf("p2(d) = %.2f\n", a * p2[0] + b * p2[1] + c * p2[2]);
222         // printf("p3(d) = %.2f\n", a * p3[0] + b * p3[1] + c * p3[2]);
223
224         // calculate the line coefficients for the specified line
225         x0 = end0->x;  x1 = end1->x;
226         y0 = end0->y;  y1 = end1->y;
227         z0 = end0->z;  z1 = end1->z;
228
229         if ( fabs(x1 - x0) > FG_EPSILON ) {
230             a1 = 1.0 / (x1 - x0);
231         } else {
232             // we got a big divide by zero problem here
233             a1 = 0.0;
234         }
235         b1 = y1 - y0;
236         c1 = z1 - z0;
237
238         // intersect the specified line with this plane
239         t1 = b * b1 * a1;
240         t2 = c * c1 * a1;
241
242         // printf("a = %.2f  t1 = %.2f  t2 = %.2f\n", a, t1, t2);
243
244         if ( fabs(a + t1 + t2) > FG_EPSILON ) {
245             x = (t1*x0 - b*y0 + t2*x0 - c*z0 + d) / (a + t1 + t2);
246             t3 = a1 * (x - x0);
247             y = b1 * t3 + y0;
248             z = c1 * t3 + z0;       
249             // printf("result(d) = %.2f\n", a * x + b * y + c * z);
250         } else {
251             // no intersection point
252             continue;
253         }
254
255         if ( side_flag ) {
256             // check to see if end0 and end1 are on opposite sides of
257             // plane
258             if ( (x - x0) > FG_EPSILON ) {
259                 t1 = x;
260                 t2 = x0;
261                 t3 = x1;
262             } else if ( (y - y0) > FG_EPSILON ) {
263                 t1 = y;
264                 t2 = y0;
265                 t3 = y1;
266             } else if ( (z - z0) > FG_EPSILON ) {
267                 t1 = z;
268                 t2 = z0;
269                 t3 = z1;
270             } else {
271                 // everything is too close together to tell the difference
272                 // so the current intersection point should work as good
273                 // as any
274                 result->x = x;
275                 result->y = y;
276                 result->z = z;
277                 return(1);
278             }
279             side1 = FG_SIGN (t1 - t2);
280             side2 = FG_SIGN (t1 - t3);
281             if ( side1 == side2 ) {
282                 // same side, punt
283                 continue;
284             }
285         }
286
287         // check to see if intersection point is in the bounding
288         // cube of the face
289         xmin = fg_min3 (p1[0], p2[0], p3[0]);
290         xmax = fg_max3 (p1[0], p2[0], p3[0]);
291         ymin = fg_min3 (p1[1], p2[1], p3[1]);
292         ymax = fg_max3 (p1[1], p2[1], p3[1]);
293         zmin = fg_min3 (p1[2], p2[2], p3[2]);
294         zmax = fg_max3 (p1[2], p2[2], p3[2]);
295         // printf("bounding cube = %.2f,%.2f,%.2f  %.2f,%.2f,%.2f\n",
296         //        xmin, ymin, zmin, xmax, ymax, zmax);
297         // punt if outside bouding cube
298         if ( x < (xmin = fg_min3 (p1[0], p2[0], p3[0])) ) {
299             continue;
300         } else if ( x > (xmax = fg_max3 (p1[0], p2[0], p3[0])) ) {
301             continue;
302         } else if ( y < (ymin = fg_min3 (p1[1], p2[1], p3[1])) ) {
303             continue;
304         } else if ( y > (ymax = fg_max3 (p1[1], p2[1], p3[1])) ) {
305             continue;
306         } else if ( z < (zmin = fg_min3 (p1[2], p2[2], p3[2])) ) {
307             continue;
308         } else if ( z > (zmax = fg_max3 (p1[2], p2[2], p3[2])) ) {
309             continue;
310         }
311
312         // (finally) check to see if the intersection point is
313         // actually inside this face
314
315         //first, drop the smallest dimension so we only have to work
316         //in 2d.
317         dx = xmax - xmin;
318         dy = ymax - ymin;
319         dz = zmax - zmin;
320         min_dim = fg_min3 (dx, dy, dz);
321         if ( fabs(min_dim - dx) <= FG_EPSILON ) {
322             // x is the smallest dimension
323             x1 = p1[1];
324             y1 = p1[2];
325             x2 = p2[1];
326             y2 = p2[2];
327             x3 = p3[1];
328             y3 = p3[2];
329             rx = y;
330             ry = z;
331         } else if ( fabs(min_dim - dy) <= FG_EPSILON ) {
332             // y is the smallest dimension
333             x1 = p1[0];
334             y1 = p1[2];
335             x2 = p2[0];
336             y2 = p2[2];
337             x3 = p3[0];
338             y3 = p3[2];
339             rx = x;
340             ry = z;
341         } else if ( fabs(min_dim - dz) <= FG_EPSILON ) {
342             // z is the smallest dimension
343             x1 = p1[0];
344             y1 = p1[1];
345             x2 = p2[0];
346             y2 = p2[1];
347             x3 = p3[0];
348             y3 = p3[1];
349             rx = x;
350             ry = y;
351         } else {
352             // all dimensions are really small so lets call it close
353             // enough and return a successful match
354             result->x = x;
355             result->y = y;
356             result->z = z;
357             return(1);
358         }
359
360         // check if intersection point is on the same side of p1 <-> p2 as p3
361         t1 = (y1 - y2) / (x1 - x2);
362         side1 = FG_SIGN (t1 * ((x3) - x2) + y2 - (y3));
363         side2 = FG_SIGN (t1 * ((rx) - x2) + y2 - (ry));
364         if ( side1 != side2 ) {
365             // printf("failed side 1 check\n");
366             continue;
367         }
368
369         // check if intersection point is on correct side of p2 <-> p3 as p1
370         t1 = (y2 - y3) / (x2 - x3);
371         side1 = FG_SIGN (t1 * ((x1) - x3) + y3 - (y1));
372         side2 = FG_SIGN (t1 * ((rx) - x3) + y3 - (ry));
373         if ( side1 != side2 ) {
374             // printf("failed side 2 check\n");
375             continue;
376         }
377
378         // check if intersection point is on correct side of p1 <-> p3 as p2
379         t1 = (y1 - y3) / (x1 - x3);
380         side1 = FG_SIGN (t1 * ((x2) - x3) + y3 - (y2));
381         side2 = FG_SIGN (t1 * ((rx) - x3) + y3 - (ry));
382         if ( side1 != side2 ) {
383             // printf("failed side 3  check\n");
384             continue;
385         }
386
387         // printf( "intersection point = %.2f %.2f %.2f\n", x, y, z);
388         result->x = x;
389         result->y = y;
390         result->z = z;
391         return(1);
392     }
393
394     // printf("\n");
395
396     return(0);
397 }
398
399
400 // Destructor
401 fgFRAGMENT::~fgFRAGMENT ( void ) {
402     // Step through the face list deleting the items until the list is
403     // empty
404
405     // printf("destructing a fragment with %d faces\n", faces.size());
406
407     while ( faces.size() ) {
408         //  printf("emptying face list\n");
409         faces.pop_front();
410     }
411 }
412
413
414 // equality operator
415 bool  fgFRAGMENT :: operator == ( const fgFRAGMENT & rhs)
416 {
417     if(( center.x - rhs.center.x ) < FG_EPSILON) {
418         if(( center.y - rhs.center.y) < FG_EPSILON) {
419             if(( center.z - rhs.center.z) < FG_EPSILON) {
420                 return true;
421             }
422         }
423     }
424     return false;
425 }
426
427 // comparison operator
428 bool  fgFRAGMENT :: operator < ( const fgFRAGMENT &rhs)
429 {
430     // This is completely arbitrary. It satisfies RW's STL implementation
431
432     return bounding_radius < rhs.bounding_radius;
433 }
434
435
436 // Constructor
437 fgTILE::fgTILE ( void ) {
438     nodes = new double[MAX_NODES][3];
439 }
440
441
442 // Destructor
443 fgTILE::~fgTILE ( void ) {
444     free(nodes);
445 }
446
447
448 // $Log$
449 // Revision 1.5  1998/07/24 21:42:08  curt
450 // material.cxx: whups, double method declaration with no definition.
451 // obj.cxx: tweaks to avoid errors in SGI's CC.
452 // tile.cxx: optimizations by Norman Vine.
453 // tilemgr.cxx: optimizations by Norman Vine.
454 //
455 // Revision 1.4  1998/07/22 21:41:42  curt
456 // Add basic fgFACE methods contributed by Charlie Hotchkiss.
457 // intersect optimization from Norman Vine.
458 //
459 // Revision 1.3  1998/07/16 17:34:24  curt
460 // Ground collision detection optimizations contributed by Norman Vine.
461 //
462 // Revision 1.2  1998/07/12 03:18:28  curt
463 // Added ground collision detection.  This involved:
464 // - saving the entire vertex list for each tile with the tile records.
465 // - saving the face list for each fragment with the fragment records.
466 // - code to intersect the current vertical line with the proper face in
467 //   an efficient manner as possible.
468 // Fixed a bug where the tiles weren't being shifted to "near" (0,0,0)
469 //
470 // Revision 1.1  1998/05/23 14:09:21  curt
471 // Added tile.cxx and tile.hxx.
472 // Working on rewriting the tile management system so a tile is just a list
473 // fragments, and the fragment record contains the display list for that fragment.
474 //