Geometry.h

Go to the documentation of this file.
00001 
00005 /* Copyright © 2010 James Legg.
00006     This program is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 */
00011 
00012 #ifndef LIBTRACK_GEOMETRY_H
00013 #define LIBTRACK_GEOMETRY_H
00014 
00015 #include <LinearMath/btVector3.h>
00016 #include <cmath>
00017 
00018 namespace Track
00019 {
00020 
00022 class Plane
00023 {
00024     public:
00031         Plane(btVector3 vector, btScalar distance)
00032             :   m_vector(vector)
00033             ,   m_distance(distance)
00034         {
00035             normalise();
00036         }
00038         Plane()
00039         {
00040         };
00042         inline bool find_side(btVector3 point) const
00043         {
00044             return find_signed_distance(point) < 0.0;
00045         }
00047         inline btScalar find_distance(btVector3 point) const
00048         {
00049             return std::abs(find_signed_distance(point));
00050         }
00052         inline btScalar find_signed_distance(btVector3 point) const
00053         {
00054             return point.dot(m_vector) + m_distance;
00055         }
00057         inline btVector3 find_nearest_point(btVector3 point) const
00058         {
00059             return point - m_vector * find_signed_distance(point);
00060 
00061         }
00062     protected:
00063         btVector3 m_vector;
00064         btScalar m_distance;
00070         inline void normalise()
00071         {
00072             btScalar length = m_vector.length();
00073             m_vector = m_vector / length;
00074             m_distance /= length;
00075         }
00076 };
00077 
00079 class Line
00080 {
00081     public:
00082         Line(btVector3 begin, btVector3 end)
00083             :   m_begin(begin)
00084             ,   m_end(end)
00085             ,   m_vector(end - begin)
00086             ,   m_length2(m_vector.length2())
00087         {
00088         }
00089         Line()
00090         {}
00096         inline btScalar closest_parametric(btVector3 point) const
00097         {
00098             return (point - m_begin).dot(m_vector) / m_length2;
00099         }
00103         inline btScalar closest_parametric_segment(btVector3 point) const
00104         {
00105             btScalar d = closest_parametric(point);
00106             if (d < 0.0) return 0.0;
00107             if (d > 1.0) return 1.0;
00108             return d;
00109         }
00110         
00111         inline btVector3 closest_point_segment(btVector3 point) const
00112         {
00113             return m_begin + m_vector * closest_parametric_segment(point);
00114         }
00117         inline Plane get_plane_including(btVector3 vector) const
00118         {
00119             // Find vector for plane normal.
00120             btVector3 plane_normal = m_vector.cross(vector);
00121             plane_normal = plane_normal.normalized();
00122             return Plane(plane_normal, -(plane_normal.dot(m_begin)));
00123         }
00124     protected:
00125         btVector3 m_begin;
00126         btVector3 m_end;
00128         btVector3 m_vector;
00130         btScalar m_length2;
00131 };
00132 
00133 } // namespace Track
00134 
00135 #endif // LIBTRACK_GEOMETRY_H

Get Racer at SourceForge.net. Fast, secure and Free Open Source software downloads

Generated at Mon Sep 6 00:41:11 2010 by Doxygen version 1.4.7 for Racer version svn335.