2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_MATH_LINE_H)
23 #define INCLUDED_MATH_LINE_H
26 /// \brief Line data types and related operations.
28 #include "math/vector.h"
29 #include "math/plane.h"
31 /// \brief A line segment defined by a start point and and end point.
40 Line(const Vector3& start_, const Vector3& end_) : start(start_), end(end_)
45 inline Vector3 line_closest_point(const Line& line, const Vector3& point)
47 Vector3 v = line.end - line.start;
48 Vector3 w = point - line.start;
50 double c1 = vector3_dot(w,v);
54 double c2 = vector3_dot(v,v);
58 return Vector3(line.start + v * (c1 / c2));
65 Vector3 origin, extents;
70 Segment(const Vector3& origin_, const Vector3& extents_) :
71 origin(origin_), extents(extents_)
77 inline Segment segment_for_startend(const Vector3& start, const Vector3& end)
80 segment.origin = vector3_mid(start, end);
81 segment.extents = vector3_subtracted(end, segment.origin);
85 inline unsigned int segment_classify_plane(const Segment& segment, const Plane3& plane)
87 double distance_origin = vector3_dot(plane.normal(), segment.origin) + plane.dist();
89 if (fabs(distance_origin) < fabs(vector3_dot(plane.normal(), segment.extents)))
91 return 1; // partially inside
93 else if (distance_origin < 0)
95 return 2; // totally inside
97 return 0; // totally outside
104 Vector3 origin, direction;
109 Ray(const Vector3& origin_, const Vector3& direction_) :
110 origin(origin_), direction(direction_)
115 inline Ray ray_for_points(const Vector3& origin, const Vector3& p2)
117 return Ray(origin, vector3_normalised(vector3_subtracted(p2, origin)));
120 inline void ray_transform(Ray& ray, const Matrix4& matrix)
122 matrix4_transform_point(matrix, ray.origin);
123 matrix4_transform_direction(matrix, ray.direction);
126 // closest-point-on-line
127 inline double ray_squared_distance_to_point(const Ray& ray, const Vector3& point)
129 return vector3_length_squared(
137 vector3_subtracted(point, ray.origin),
146 inline double ray_distance_to_plane(const Ray& ray, const Plane3& plane)
148 return -(vector3_dot(plane.normal(), ray.origin) - plane.dist()) / vector3_dot(ray.direction, plane.normal());