line.h

Go to the documentation of this file.
00001 
00006 /*
00007  Copyright (C) 2001-2006, William Joseph.
00008  All Rights Reserved.
00009 
00010  This file is part of GtkRadiant.
00011 
00012  GtkRadiant is free software; you can redistribute it and/or modify
00013  it under the terms of the GNU General Public License as published by
00014  the Free Software Foundation; either version 2 of the License, or
00015  (at your option) any later version.
00016 
00017  GtkRadiant is distributed in the hope that it will be useful,
00018  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  GNU General Public License for more details.
00021 
00022  You should have received a copy of the GNU General Public License
00023  along with GtkRadiant; if not, write to the Free Software
00024  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00025  */
00026 
00027 #if !defined(INCLUDED_MATH_LINE_H)
00028 #define INCLUDED_MATH_LINE_H
00029 
00030 #include "math/plane.h"
00031 
00033 class Line
00034 {
00035     public:
00036         Vector3 start, end;
00037 
00038         Line ()
00039         {
00040         }
00041         Line (const Vector3& start_, const Vector3& end_) :
00042             start(start_), end(end_)
00043         {
00044         }
00045 };
00046 
00047 inline Vector3 line_closest_point (const Line& line, const Vector3& point)
00048 {
00049     Vector3 v = line.end - line.start;
00050     Vector3 w = point - line.start;
00051 
00052     double c1 = w.dot(v);
00053     if (c1 <= 0)
00054         return line.start;
00055 
00056     double c2 = v.dot(v);
00057     if (c2 <= c1)
00058         return line.end;
00059 
00060     return Vector3(line.start + v * (c1 / c2));
00061 }
00062 
00063 class Segment
00064 {
00065     public:
00066         Vector3 origin, extents;
00067 
00068         Segment ()
00069         {
00070         }
00071         Segment (const Vector3& origin_, const Vector3& extents_) :
00072             origin(origin_), extents(extents_)
00073         {
00074         }
00075 };
00076 
00077 inline Segment segment_for_startend (const Vector3& start, const Vector3& end)
00078 {
00079     Segment segment;
00080     segment.origin = vector3_mid(start, end);
00081     segment.extents = end - segment.origin;
00082     return segment;
00083 }
00084 
00085 inline unsigned int segment_classify_plane (const Segment& segment, const Plane3& plane)
00086 {
00087     double distance_origin = plane.normal().dot(segment.origin) + plane.dist();
00088 
00089     if (fabs(distance_origin) < fabs(plane.normal().dot(segment.extents))) {
00090         return 1; // partially inside
00091     } else if (distance_origin < 0) {
00092         return 2; // totally inside
00093     }
00094     return 0; // totally outside
00095 }
00096 
00097 class Ray
00098 {
00099     public:
00100         Vector3 origin, direction;
00101 
00102         Ray ()
00103         {
00104         }
00105         Ray (const Vector3& origin_, const Vector3& direction_) :
00106             origin(origin_), direction(direction_)
00107         {
00108         }
00109 };
00110 
00111 inline Ray ray_for_points (const Vector3& origin, const Vector3& p2)
00112 {
00113     return Ray(origin, (p2 - origin).getNormalised());
00114 }
00115 
00116 inline void ray_transform (Ray& ray, const Matrix4& matrix)
00117 {
00118     matrix4_transform_point(matrix, ray.origin);
00119     matrix4_transform_direction(matrix, ray.direction);
00120 }
00121 
00122 // closest-point-on-line
00123 inline double ray_squared_distance_to_point (const Ray& ray, const Vector3& point)
00124 {
00125     return (point - (ray.origin + ray.direction * (point - ray.origin).dot(ray.direction))).getLengthSquared();
00126 }
00127 
00128 inline double ray_distance_to_plane (const Ray& ray, const Plane3& plane)
00129 {
00130     return -(plane.normal().dot(ray.origin) - plane.dist()) / ray.direction.dot(plane.normal());
00131 }
00132 
00133 #endif

Generated by  doxygen 1.6.2