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