geometry.H

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 #ifndef ARCHON_MATH_GEOMETRY_H
00021 #define ARCHON_MATH_GEOMETRY_H
00022 
00023 #include <archon/math/vector.H>
00024 #include <archon/math/matrix.H>
00025 
00026 namespace Archon
00027 {
00028   namespace Math
00029   {
00030     struct Line3
00031     {
00032       Vector3 point;
00033       Vector3 direction; // Must be a unit vector
00034 
00035       Line3() {}
00036       Line3(Vector3 point, Vector3 direction): point(point), direction(direction) {}
00037     };
00038 
00046     struct Plane3
00047     {
00054       Vector3 normal;
00055 
00061       double dist;
00062 
00063       Plane3() {}
00064       Plane3(Vector3 normal, double dist): normal(normal), dist(dist) {}
00065       Plane3(Vector3 normal, Vector3 point): normal(normal), dist(-dot(normal, point)) {}
00066     };
00067 
00068     Vector3 intersect(const Plane3 &, const Plane3 &, const Plane3 &);
00069 
00070     struct Ray3
00071     {
00072       Vector3 origin;
00073       Vector3 direction; // Must be a unit vector
00074 
00075       Ray3() {}
00076       Ray3(const Vector3 &origin, const Vector3 &direction):
00077         origin(origin), direction(direction) {}
00078       Ray3(const Line3 &l): origin(l.point), direction(l.direction) {}
00079     };
00080 
00084     struct Box3
00085     {
00086       Vector3 lowerCorner; // Lower-left-back corner in 3D
00087       Vector3 upperCorner; // Upper-right-front corner in 3D
00088 
00089       Box3() {}
00090       Box3(const Vector3 &lowerCorner, const Vector3 &upperCorner):
00091         lowerCorner(lowerCorner), upperCorner(upperCorner) {}
00092       Box3(const Vector3 &size):
00093         lowerCorner(-size/2), upperCorner(size/2) {}
00094 
00095       Vector3 getCenter() const { return (lowerCorner + upperCorner)/2; }
00096       Vector3 getSize() const
00097       {
00098         Vector3 v = lowerCorner;
00099         v -= upperCorner;
00100         if(v[0] < 0) v[0] = -v[0];
00101         if(v[1] < 0) v[1] = -v[1];
00102         if(v[2] < 0) v[2] = -v[2];
00103         return v;
00104       }
00105     };
00106 
00107     struct Sphere3
00108     {
00109       Vector3 center;
00110       double radius;
00111 
00112       Sphere3() {}
00113       Sphere3(Vector3 center, double radius):
00114         center(center), radius(radius) {}
00115       Sphere3(double radius):
00116         center(Vector3::zero()), radius(radius) {}
00117     };
00118 
00135     bool intersect(const Ray3 &, const Plane3 &, bool frontToBackOnly, double &dist);
00136 
00163     int intersect(const Ray3 &, const Box3 &, double &dist);
00164 
00185     bool intersect(const Ray3 &, const Sphere3 &, double &dist);
00186 
00312     int intersectCylinder(const Ray3 &, double height, double radius,
00313                           double &dist, bool side=true, bool top=true,
00314                           bool bottom=true, bool enterOnly = true);
00315 
00589     int intersectCone(const Ray3 &, double height, double bottomRadius,
00590                       double &dist, bool side=true, bool bottom=true,
00591                       bool enterOnly = true);
00592 
00616     bool intersectTorus(const Ray3 &, double majorTorusRadius,
00617                         double minorTorusRadius, double &dist,
00618                         bool surfaceOrigin = false, bool extToIntOnly = true);
00619   }
00620 }
00621 
00622 #endif // ARCHON_MATH_GEOMETRY_H

Generated on Sun Jul 30 22:55:43 2006 for Archon by  doxygen 1.4.4