]> git.mxchange.org Git - flightgear.git/blob - Objects/fragment.cxx
Updates from Bernie Bright.
[flightgear.git] / 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 // (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 #include <Scenery/tile.hxx>
29
30 #include "fragment.hxx"
31
32
33 template <class T>
34 inline const int FG_SIGN(const T& x) {
35     return x < T(0) ? -1 : 1;
36 }
37
38 template <class T>
39 inline const T& FG_MIN(const T& a, const T& b) {
40     return b < a ? b : a;
41 }
42
43 template <class T>
44 inline const T& FG_MAX(const T& a, const T& b) {
45     return  a < b ? b : a;
46 }
47
48 // return the minimum of the three values
49 template <class T>
50 inline const T& fg_min3( const T& a, const T& b, const T& c)
51 {
52     return (a > b ? FG_MIN (b, c) : FG_MIN (a, c));
53 }
54
55
56 // return the maximum of the three values
57 template <class T>
58 inline const T& fg_max3 (const T& a, const T& b, const T& c)
59 {
60     return (a < b ? FG_MAX (b, c) : FG_MAX (a, c));
61 }
62
63 // Add a face to the face list
64 // Copy constructor
65 fgFRAGMENT::fgFRAGMENT ( const fgFRAGMENT & rhs ) :
66     center         ( rhs.center          ),
67     bounding_radius( rhs.bounding_radius ),
68     material_ptr   ( rhs.material_ptr    ),
69     tile_ptr       ( rhs.tile_ptr        ),
70     display_list   ( rhs.display_list    ),
71     faces          ( rhs.faces           ),
72     num_faces      ( rhs.num_faces       )
73 {
74 }
75
76 fgFRAGMENT & fgFRAGMENT::operator = ( const fgFRAGMENT & rhs )
77 {
78     if(!(this == &rhs )) {
79         center          = rhs.center;
80         bounding_radius = rhs.bounding_radius;
81         material_ptr    = rhs.material_ptr;
82         tile_ptr        = rhs.tile_ptr;
83         // display_list    = rhs.display_list;
84         faces           = rhs.faces;
85     }
86     return *this;
87 }
88
89
90 // test if line intesects with this fragment.  p0 and p1 are the two
91 // line end points of the line.  If side_flag is true, check to see
92 // that end points are on opposite sides of face.  Returns 1 if it
93 // intersection found, 0 otherwise.  If it intesects, result is the
94 // point of intersection
95
96 int fgFRAGMENT::intersect( const fgPoint3d *end0,
97                            const fgPoint3d *end1,
98                            int side_flag,
99                            fgPoint3d *result) const
100 {
101     fgTILE *t;
102     MAT3vec v1, v2, n, center;
103     double p1[3], p2[3], p3[3];
104     double x, y, z;  // temporary holding spot for result
105     double a, b, c, d;
106     double x0, y0, z0, x1, y1, z1, a1, b1, c1;
107     double t1, t2, t3;
108     double xmin, xmax, ymin, ymax, zmin, zmax;
109     double dx, dy, dz, min_dim, x2, y2, x3, y3, rx, ry;
110     int side1, side2;
111
112     // find the associated tile
113     t = tile_ptr;
114
115     // printf("Intersecting\n");
116
117     // traverse the face list for this fragment
118     const_iterator last = faces.end();
119     for ( const_iterator current = faces.begin(); current != last; ++current )
120     {
121         // printf(".");
122
123         // get face vertex coordinates
124         center[0] = t->center.x;
125         center[1] = t->center.y;
126         center[2] = t->center.z;
127
128         MAT3_ADD_VEC(p1, t->nodes[(*current).n1], center);
129         MAT3_ADD_VEC(p2, t->nodes[(*current).n2], center);
130         MAT3_ADD_VEC(p3, t->nodes[(*current).n3], center);
131
132         // printf("point 1 = %.2f %.2f %.2f\n", p1[0], p1[1], p1[2]);
133         // printf("point 2 = %.2f %.2f %.2f\n", p2[0], p2[1], p2[2]);
134         // printf("point 3 = %.2f %.2f %.2f\n", p3[0], p3[1], p3[2]);
135
136         // calculate two edge vectors, and the face normal
137         MAT3_SUB_VEC(v1, p2, p1);
138         MAT3_SUB_VEC(v2, p3, p1);
139         MAT3cross_product(n, v1, v2);
140
141         // calculate the plane coefficients for the plane defined by
142         // this face.  If n is the normal vector, n = (a, b, c) and p1
143         // is a point on the plane, p1 = (x0, y0, z0), then the
144         // equation of the line is a(x-x0) + b(y-y0) + c(z-z0) = 0
145         a = n[0];
146         b = n[1];
147         c = n[2];
148         d = a * p1[0] + b * p1[1] + c * p1[2];
149         // printf("a, b, c, d = %.2f %.2f %.2f %.2f\n", a, b, c, d);
150
151         // printf("p1(d) = %.2f\n", a * p1[0] + b * p1[1] + c * p1[2]);
152         // printf("p2(d) = %.2f\n", a * p2[0] + b * p2[1] + c * p2[2]);
153         // printf("p3(d) = %.2f\n", a * p3[0] + b * p3[1] + c * p3[2]);
154
155         // calculate the line coefficients for the specified line
156         x0 = end0->x;  x1 = end1->x;
157         y0 = end0->y;  y1 = end1->y;
158         z0 = end0->z;  z1 = end1->z;
159
160         if ( fabs(x1 - x0) > FG_EPSILON ) {
161             a1 = 1.0 / (x1 - x0);
162         } else {
163             // we got a big divide by zero problem here
164             a1 = 0.0;
165         }
166         b1 = y1 - y0;
167         c1 = z1 - z0;
168
169         // intersect the specified line with this plane
170         t1 = b * b1 * a1;
171         t2 = c * c1 * a1;
172
173         // printf("a = %.2f  t1 = %.2f  t2 = %.2f\n", a, t1, t2);
174
175         if ( fabs(a + t1 + t2) > FG_EPSILON ) {
176             x = (t1*x0 - b*y0 + t2*x0 - c*z0 + d) / (a + t1 + t2);
177             t3 = a1 * (x - x0);
178             y = b1 * t3 + y0;
179             z = c1 * t3 + z0;       
180             // printf("result(d) = %.2f\n", a * x + b * y + c * z);
181         } else {
182             // no intersection point
183             continue;
184         }
185
186         if ( side_flag ) {
187             // check to see if end0 and end1 are on opposite sides of
188             // plane
189             if ( (x - x0) > FG_EPSILON ) {
190                 t1 = x;
191                 t2 = x0;
192                 t3 = x1;
193             } else if ( (y - y0) > FG_EPSILON ) {
194                 t1 = y;
195                 t2 = y0;
196                 t3 = y1;
197             } else if ( (z - z0) > FG_EPSILON ) {
198                 t1 = z;
199                 t2 = z0;
200                 t3 = z1;
201             } else {
202                 // everything is too close together to tell the difference
203                 // so the current intersection point should work as good
204                 // as any
205                 result->x = x;
206                 result->y = y;
207                 result->z = z;
208                 return(1);
209             }
210             side1 = FG_SIGN (t1 - t2);
211             side2 = FG_SIGN (t1 - t3);
212             if ( side1 == side2 ) {
213                 // same side, punt
214                 continue;
215             }
216         }
217
218         // check to see if intersection point is in the bounding
219         // cube of the face
220 #ifdef XTRA_DEBUG_STUFF
221         xmin = fg_min3 (p1[0], p2[0], p3[0]);
222         xmax = fg_max3 (p1[0], p2[0], p3[0]);
223         ymin = fg_min3 (p1[1], p2[1], p3[1]);
224         ymax = fg_max3 (p1[1], p2[1], p3[1]);
225         zmin = fg_min3 (p1[2], p2[2], p3[2]);
226         zmax = fg_max3 (p1[2], p2[2], p3[2]);
227         printf("bounding cube = %.2f,%.2f,%.2f  %.2f,%.2f,%.2f\n",
228                xmin, ymin, zmin, xmax, ymax, zmax);
229 #endif
230         // punt if outside bouding cube
231         if ( x < (xmin = fg_min3 (p1[0], p2[0], p3[0])) ) {
232             continue;
233         } else if ( x > (xmax = fg_max3 (p1[0], p2[0], p3[0])) ) {
234             continue;
235         } else if ( y < (ymin = fg_min3 (p1[1], p2[1], p3[1])) ) {
236             continue;
237         } else if ( y > (ymax = fg_max3 (p1[1], p2[1], p3[1])) ) {
238             continue;
239         } else if ( z < (zmin = fg_min3 (p1[2], p2[2], p3[2])) ) {
240             continue;
241         } else if ( z > (zmax = fg_max3 (p1[2], p2[2], p3[2])) ) {
242             continue;
243         }
244
245         // (finally) check to see if the intersection point is
246         // actually inside this face
247
248         //first, drop the smallest dimension so we only have to work
249         //in 2d.
250         dx = xmax - xmin;
251         dy = ymax - ymin;
252         dz = zmax - zmin;
253         min_dim = fg_min3 (dx, dy, dz);
254         if ( fabs(min_dim - dx) <= FG_EPSILON ) {
255             // x is the smallest dimension
256             x1 = p1[1];
257             y1 = p1[2];
258             x2 = p2[1];
259             y2 = p2[2];
260             x3 = p3[1];
261             y3 = p3[2];
262             rx = y;
263             ry = z;
264         } else if ( fabs(min_dim - dy) <= FG_EPSILON ) {
265             // y is the smallest dimension
266             x1 = p1[0];
267             y1 = p1[2];
268             x2 = p2[0];
269             y2 = p2[2];
270             x3 = p3[0];
271             y3 = p3[2];
272             rx = x;
273             ry = z;
274         } else if ( fabs(min_dim - dz) <= FG_EPSILON ) {
275             // z is the smallest dimension
276             x1 = p1[0];
277             y1 = p1[1];
278             x2 = p2[0];
279             y2 = p2[1];
280             x3 = p3[0];
281             y3 = p3[1];
282             rx = x;
283             ry = y;
284         } else {
285             // all dimensions are really small so lets call it close
286             // enough and return a successful match
287             result->x = x;
288             result->y = y;
289             result->z = z;
290             return(1);
291         }
292
293         // check if intersection point is on the same side of p1 <-> p2 as p3
294         t1 = (y1 - y2) / (x1 - x2);
295         side1 = FG_SIGN (t1 * ((x3) - x2) + y2 - (y3));
296         side2 = FG_SIGN (t1 * ((rx) - x2) + y2 - (ry));
297         if ( side1 != side2 ) {
298             // printf("failed side 1 check\n");
299             continue;
300         }
301
302         // check if intersection point is on correct side of p2 <-> p3 as p1
303         t1 = (y2 - y3) / (x2 - x3);
304         side1 = FG_SIGN (t1 * ((x1) - x3) + y3 - (y1));
305         side2 = FG_SIGN (t1 * ((rx) - x3) + y3 - (ry));
306         if ( side1 != side2 ) {
307             // printf("failed side 2 check\n");
308             continue;
309         }
310
311         // check if intersection point is on correct side of p1 <-> p3 as p2
312         t1 = (y1 - y3) / (x1 - x3);
313         side1 = FG_SIGN (t1 * ((x2) - x3) + y3 - (y2));
314         side2 = FG_SIGN (t1 * ((rx) - x3) + y3 - (ry));
315         if ( side1 != side2 ) {
316             // printf("failed side 3  check\n");
317             continue;
318         }
319
320         // printf( "intersection point = %.2f %.2f %.2f\n", x, y, z);
321         result->x = x;
322         result->y = y;
323         result->z = z;
324         return(1);
325     }
326
327     // printf("\n");
328
329     return(0);
330 }
331
332 // $Log$
333 // Revision 1.3  1998/09/08 21:40:42  curt
334 // Updates from Bernie Bright.
335 //
336 // Revision 1.2  1998/09/01 19:03:07  curt
337 // Changes contributed by Bernie Bright <bbright@c031.aone.net.au>
338 //  - The new classes in libmisc.tgz define a stream interface into zlib.
339 //    I've put these in a new directory, Lib/Misc.  Feel free to rename it
340 //    to something more appropriate.  However you'll have to change the
341 //    include directives in all the other files.  Additionally you'll have
342 //    add the library to Lib/Makefile.am and Simulator/Main/Makefile.am.
343 //
344 //    The StopWatch class in Lib/Misc requires a HAVE_GETRUSAGE autoconf
345 //    test so I've included the required changes in config.tgz.
346 //
347 //    There are a fair few changes to Simulator/Objects as I've moved
348 //    things around.  Loading tiles is quicker but thats not where the delay
349 //    is.  Tile loading takes a few tenths of a second per file on a P200
350 //    but it seems to be the post-processing that leads to a noticeable
351 //    blip in framerate.  I suppose its time to start profiling to see where
352 //    the delays are.
353 //
354 //    I've included a brief description of each archives contents.
355 //
356 // Lib/Misc/
357 //   zfstream.cxx
358 //   zfstream.hxx
359 //     C++ stream interface into zlib.
360 //     Taken from zlib-1.1.3/contrib/iostream/.
361 //     Minor mods for STL compatibility.
362 //     There's no copyright associated with these so I assume they're
363 //     covered by zlib's.
364 //
365 //   fgstream.cxx
366 //   fgstream.hxx
367 //     FlightGear input stream using gz_ifstream.  Tries to open the
368 //     given filename.  If that fails then filename is examined and a
369 //     ".gz" suffix is removed or appended and that file is opened.
370 //
371 //   stopwatch.hxx
372 //     A simple timer for benchmarking.  Not used in production code.
373 //     Taken from the Blitz++ project.  Covered by GPL.
374 //
375 //   strutils.cxx
376 //   strutils.hxx
377 //     Some simple string manipulation routines.
378 //
379 // Simulator/Airports/
380 //   Load airports database using fgstream.
381 //   Changed fgAIRPORTS to use set<> instead of map<>.
382 //   Added bool fgAIRPORTS::search() as a neater way doing the lookup.
383 //   Returns true if found.
384 //
385 // Simulator/Astro/
386 //   Modified fgStarsInit() to load stars database using fgstream.
387 //
388 // Simulator/Objects/
389 //   Modified fgObjLoad() to use fgstream.
390 //   Modified fgMATERIAL_MGR::load_lib() to use fgstream.
391 //   Many changes to fgMATERIAL.
392 //   Some changes to fgFRAGMENT but I forget what!
393 //
394 // Revision 1.1  1998/08/25 16:51:23  curt
395 // Moved from ../Scenery
396 //
397 //
398