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.
39 Line( const Vector3& start_, const Vector3& end_ ) : start( start_ ), end( end_ ){
43 inline Vector3 line_closest_point( const Line& line, const Vector3& point ){
44 Vector3 v = line.end - line.start;
45 Vector3 w = point - line.start;
47 double c1 = vector3_dot( w,v );
52 double c2 = vector3_dot( v,v );
57 return Vector3( line.start + v * ( c1 / c2 ) );
64 Vector3 origin, extents;
68 Segment( const Vector3& origin_, const Vector3& extents_ ) :
69 origin( origin_ ), extents( extents_ ){
74 inline Segment segment_for_startend( const Vector3& start, const Vector3& end ){
76 segment.origin = vector3_mid( start, end );
77 segment.extents = vector3_subtracted( end, segment.origin );
81 inline unsigned int segment_classify_plane( const Segment& segment, const Plane3& plane ){
82 double distance_origin = vector3_dot( plane.normal(), segment.origin ) + plane.dist();
84 if ( fabs( distance_origin ) < fabs( vector3_dot( plane.normal(), segment.extents ) ) ) {
85 return 1; // partially inside
87 else if ( distance_origin < 0 ) {
88 return 2; // totally inside
90 return 0; // totally outside
97 Vector3 origin, direction;
101 Ray( const Vector3& origin_, const Vector3& direction_ ) :
102 origin( origin_ ), direction( direction_ ){
106 inline Ray ray_for_points( const Vector3& origin, const Vector3& p2 ){
107 return Ray( origin, vector3_normalised( vector3_subtracted( p2, origin ) ) );
110 inline void ray_transform( Ray& ray, const Matrix4& matrix ){
111 matrix4_transform_point( matrix, ray.origin );
112 matrix4_transform_direction( matrix, ray.direction );
115 // closest-point-on-line
116 inline double ray_squared_distance_to_point( const Ray& ray, const Vector3& point ){
117 return vector3_length_squared(
125 vector3_subtracted( point, ray.origin ),
134 inline double ray_distance_to_plane( const Ray& ray, const Plane3& plane ){
135 return -( vector3_dot( plane.normal(), ray.origin ) - plane.dist() ) / vector3_dot( ray.direction, plane.normal() );