1 // fragment.cxx -- routines to handle "atomic" display objects
3 // Written by Curtis Olson, started August 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
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.
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.
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.
24 #include <Include/fg_constants.h>
25 #include <Math/mat3.h>
26 #include <Math/point3d.hxx>
27 #include <Scenery/tileentry.hxx>
29 #include "fragment.hxx"
33 inline const int FG_SIGN(const T& x) {
34 return x < T(0) ? -1 : 1;
38 inline const T& FG_MIN(const T& a, const T& b) {
43 inline const T& FG_MAX(const T& a, const T& b) {
47 // return the minimum of the three values
49 inline const T& fg_min3( const T& a, const T& b, const T& c)
51 return (a > b ? FG_MIN (b, c) : FG_MIN (a, c));
55 // return the maximum of the three values
57 inline const T& fg_max3 (const T& a, const T& b, const T& c)
59 return (a < b ? FG_MAX (b, c) : FG_MAX (a, c));
62 // Add a face to the face list
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 ), */
74 fgFRAGMENT & fgFRAGMENT::operator = ( const fgFRAGMENT & rhs )
76 if(!(this == &rhs )) {
78 bounding_radius = rhs.bounding_radius;
79 material_ptr = rhs.material_ptr;
80 tile_ptr = rhs.tile_ptr;
81 // display_list = rhs.display_list;
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
94 int fgFRAGMENT::intersect( const Point3D& end0,
97 Point3D& result) const
100 MAT3vec v1, v2, n, center;
101 double p1[3], p2[3], p3[3];
102 double x, y, z; // temporary holding spot for result
104 double x0, y0, z0, x1, y1, z1, a1, b1, c1;
106 double xmin, xmax, ymin, ymax, zmin, zmax;
107 double dx, dy, dz, min_dim, x2, y2, x3, y3, rx, ry;
110 // find the associated tile
113 // printf("Intersecting\n");
115 // traverse the face list for this fragment
116 const_iterator last = faces.end();
117 for ( const_iterator current = faces.begin(); current != last; ++current )
121 // get face vertex coordinates
122 center[0] = t->center.x();
123 center[1] = t->center.y();
124 center[2] = t->center.z();
126 MAT3_ADD_VEC(p1, t->nodes[(*current).n1], center);
127 MAT3_ADD_VEC(p2, t->nodes[(*current).n2], center);
128 MAT3_ADD_VEC(p3, t->nodes[(*current).n3], center);
130 // printf("point 1 = %.2f %.2f %.2f\n", p1[0], p1[1], p1[2]);
131 // printf("point 2 = %.2f %.2f %.2f\n", p2[0], p2[1], p2[2]);
132 // printf("point 3 = %.2f %.2f %.2f\n", p3[0], p3[1], p3[2]);
134 // calculate two edge vectors, and the face normal
135 MAT3_SUB_VEC(v1, p2, p1);
136 MAT3_SUB_VEC(v2, p3, p1);
137 MAT3cross_product(n, v1, v2);
139 // calculate the plane coefficients for the plane defined by
140 // this face. If n is the normal vector, n = (a, b, c) and p1
141 // is a point on the plane, p1 = (x0, y0, z0), then the
142 // equation of the line is a(x-x0) + b(y-y0) + c(z-z0) = 0
146 d = a * p1[0] + b * p1[1] + c * p1[2];
147 // printf("a, b, c, d = %.2f %.2f %.2f %.2f\n", a, b, c, d);
149 // printf("p1(d) = %.2f\n", a * p1[0] + b * p1[1] + c * p1[2]);
150 // printf("p2(d) = %.2f\n", a * p2[0] + b * p2[1] + c * p2[2]);
151 // printf("p3(d) = %.2f\n", a * p3[0] + b * p3[1] + c * p3[2]);
153 // calculate the line coefficients for the specified line
154 x0 = end0.x(); x1 = end1.x();
155 y0 = end0.y(); y1 = end1.y();
156 z0 = end0.z(); z1 = end1.z();
158 if ( fabs(x1 - x0) > FG_EPSILON ) {
159 a1 = 1.0 / (x1 - x0);
161 // we got a big divide by zero problem here
167 // intersect the specified line with this plane
171 // printf("a = %.2f t1 = %.2f t2 = %.2f\n", a, t1, t2);
173 if ( fabs(a + t1 + t2) > FG_EPSILON ) {
174 x = (t1*x0 - b*y0 + t2*x0 - c*z0 + d) / (a + t1 + t2);
178 // printf("result(d) = %.2f\n", a * x + b * y + c * z);
180 // no intersection point
185 // check to see if end0 and end1 are on opposite sides of
187 if ( (x - x0) > FG_EPSILON ) {
191 } else if ( (y - y0) > FG_EPSILON ) {
195 } else if ( (z - z0) > FG_EPSILON ) {
200 // everything is too close together to tell the difference
201 // so the current intersection point should work as good
203 result = Point3D(x, y, z);
206 side1 = FG_SIGN (t1 - t2);
207 side2 = FG_SIGN (t1 - t3);
208 if ( side1 == side2 ) {
214 // check to see if intersection point is in the bounding
216 #ifdef XTRA_DEBUG_STUFF
217 xmin = fg_min3 (p1[0], p2[0], p3[0]);
218 xmax = fg_max3 (p1[0], p2[0], p3[0]);
219 ymin = fg_min3 (p1[1], p2[1], p3[1]);
220 ymax = fg_max3 (p1[1], p2[1], p3[1]);
221 zmin = fg_min3 (p1[2], p2[2], p3[2]);
222 zmax = fg_max3 (p1[2], p2[2], p3[2]);
223 printf("bounding cube = %.2f,%.2f,%.2f %.2f,%.2f,%.2f\n",
224 xmin, ymin, zmin, xmax, ymax, zmax);
226 // punt if outside bouding cube
227 if ( x < (xmin = fg_min3 (p1[0], p2[0], p3[0])) ) {
229 } else if ( x > (xmax = fg_max3 (p1[0], p2[0], p3[0])) ) {
231 } else if ( y < (ymin = fg_min3 (p1[1], p2[1], p3[1])) ) {
233 } else if ( y > (ymax = fg_max3 (p1[1], p2[1], p3[1])) ) {
235 } else if ( z < (zmin = fg_min3 (p1[2], p2[2], p3[2])) ) {
237 } else if ( z > (zmax = fg_max3 (p1[2], p2[2], p3[2])) ) {
241 // (finally) check to see if the intersection point is
242 // actually inside this face
244 //first, drop the smallest dimension so we only have to work
249 min_dim = fg_min3 (dx, dy, dz);
250 if ( fabs(min_dim - dx) <= FG_EPSILON ) {
251 // x is the smallest dimension
260 } else if ( fabs(min_dim - dy) <= FG_EPSILON ) {
261 // y is the smallest dimension
270 } else if ( fabs(min_dim - dz) <= FG_EPSILON ) {
271 // z is the smallest dimension
281 // all dimensions are really small so lets call it close
282 // enough and return a successful match
283 result = Point3D(x, y, z);
287 // check if intersection point is on the same side of p1 <-> p2 as p3
288 t1 = (y1 - y2) / (x1 - x2);
289 side1 = FG_SIGN (t1 * ((x3) - x2) + y2 - (y3));
290 side2 = FG_SIGN (t1 * ((rx) - x2) + y2 - (ry));
291 if ( side1 != side2 ) {
292 // printf("failed side 1 check\n");
296 // check if intersection point is on correct side of p2 <-> p3 as p1
297 t1 = (y2 - y3) / (x2 - x3);
298 side1 = FG_SIGN (t1 * ((x1) - x3) + y3 - (y1));
299 side2 = FG_SIGN (t1 * ((rx) - x3) + y3 - (ry));
300 if ( side1 != side2 ) {
301 // printf("failed side 2 check\n");
305 // check if intersection point is on correct side of p1 <-> p3 as p2
306 t1 = (y1 - y3) / (x1 - x3);
307 side1 = FG_SIGN (t1 * ((x2) - x3) + y3 - (y2));
308 side2 = FG_SIGN (t1 * ((rx) - x3) + y3 - (ry));
309 if ( side1 != side2 ) {
310 // printf("failed side 3 check\n");
314 // printf( "intersection point = %.2f %.2f %.2f\n", x, y, z);
315 result = Point3D(x, y, z);