]> git.mxchange.org Git - simgear.git/blob - simgear/math/vector.hxx
Added some convenience functions to point3d.
[simgear.git] / simgear / math / vector.hxx
1 /**
2  * \file vector.hxx
3  * Additional vector routines.
4  */
5
6 // Written by Curtis Olson, started December 1997.
7 //
8 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU Library General Public
21 // License along with this library; if not, write to the
22 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 // Boston, MA  02111-1307, USA.
24 //
25 // $Id$
26
27
28 #ifndef _VECTOR_HXX
29 #define _VECTOR_HXX
30
31
32 #ifndef __cplusplus                                                          
33 # error This library requires C++
34 #endif                                   
35
36 #ifdef HAVE_CONFIG_H
37 #  include <config.h>
38 #endif
39
40 #include <simgear/compiler.h>
41
42 #include <plib/sg.h>
43
44
45 /**
46  * Map a vector onto a plane.
47  * @param normal (in) normal vector for the plane
48  * @param v0 (in) a point on the plane
49  * @param vec (in) the vector to map onto the plane
50  * @param result (out) the result vector
51  */
52 inline void sgmap_vec_onto_cur_surface_plane( sgVec3 normal, 
53                                               sgVec3 v0, 
54                                               sgVec3 vec,
55                                               sgVec3 result )
56 {
57     sgVec3 u1, v, tmp;
58
59     // calculate a vector "u1" representing the shortest distance from
60     // the plane specified by normal and v0 to a point specified by
61     // "vec".  "u1" represents both the direction and magnitude of
62     // this desired distance.
63
64     // u1 = ( (normal <dot> vec) / (normal <dot> normal) ) * normal
65
66     sgScaleVec3( u1,
67                  normal,
68                  ( sgScalarProductVec3(normal, vec) /
69                    sgScalarProductVec3(normal, normal)
70                    )
71                  );
72
73     // printf("  vec = %.2f, %.2f, %.2f\n", vec[0], vec[1], vec[2]);
74     // printf("  v0 = %.2f, %.2f, %.2f\n", v0[0], v0[1], v0[2]);
75     // printf("  u1 = %.2f, %.2f, %.2f\n", u1[0], u1[1], u1[2]);
76    
77     // calculate the vector "v" which is the vector "vec" mapped onto
78     // the plane specified by "normal" and "v0".
79
80     // v = v0 + vec - u1
81
82     sgAddVec3(tmp, v0, vec);
83     sgSubVec3(v, tmp, u1);
84     // printf("  v = %.2f, %.2f, %.2f\n", v[0], v[1], v[2]);
85
86     // Calculate the vector "result" which is "v" - "v0" which is a
87     // directional vector pointing from v0 towards v
88
89     // result = v - v0
90
91     sgSubVec3(result, v, v0);
92     // printf("  result = %.2f, %.2f, %.2f\n", 
93     // result[0], result[1], result[2]);
94 }
95
96
97 /**
98  * Copy and negate a vector.
99  * @param dst (out) result vector
100  * @param src (in) input vector
101  */
102 inline void sgCopyNegateVec4( sgVec4 dst, sgVec4 src )
103 {
104         dst [ 0 ] = -src [ 0 ] ;
105         dst [ 1 ] = -src [ 1 ] ;
106         dst [ 2 ] = -src [ 2 ] ;
107         dst [ 3 ] = -src [ 3 ] ;
108 }
109
110 /**
111  * Given a point p, and a line through p0 with direction vector d,
112  * find the closest point (p1) on the line (float version).
113  * @param p1 (out) closest point to p on the line
114  * @param p (in) original point
115  * @param p0 (in) point on the line
116  * @param d (in) vector defining line direction
117  */
118 void sgClosestPointToLine( sgVec3 p1, const sgVec3 p, const sgVec3 p0,
119                            const sgVec3 d );
120
121 /**
122  * Given a point p, and a line through p0 with direction vector d,
123  * find the closest point (p1) on the line (double version).
124  * @param p1 (out) closest point to p on the line
125  * @param p (in) original point
126  * @param p0 (in) point on the line
127  * @param d (in) vector defining line direction
128  */
129 void sgdClosestPointToLine( sgdVec3 p1, const sgdVec3 p, const sgdVec3 p0,
130                             const sgdVec3 d );
131
132 /**
133  * Given a point p, and a line through p0 with direction vector d,
134  * find the shortest distance (squared) from the point to the line (float
135  * version.)
136  * @param p (in) original point
137  * @param p0 (in) point on the line
138  * @param d (in) vector defining line direction
139  * @return shortest distance (squared) from p to line
140  */
141 double sgClosestPointToLineDistSquared( const sgVec3 p, const sgVec3 p0,
142                                         const sgVec3 d );
143
144 /**
145  * Given a point p, and a line through p0 with direction vector d,
146  * find the shortest distance (squared) from the point to the line (double
147  * version.)
148  * @param p (in) original point
149  * @param p0 (in) point on the line
150  * @param d (in) vector defining line direction
151  * @return shortest distance (squared) from p to line
152  */
153 double sgdClosestPointToLineDistSquared( const sgdVec3 p, const sgdVec3 p0,
154                                          const sgdVec3 d );
155
156 /**
157  * This is same as:
158  *
159  * <li> sgMakeMatTrans4( sgMat4 sgTrans, sgVec3 trans )
160  * <li> sgPostMultMat4( sgMat4 src, sgTRANS );
161  *
162  * @param src starting sgMat4 matrix
163  * @param trans translation vector
164  */
165 void sgPostMultMat4ByTransMat4( sgMat4 src, const sgVec3 trans );
166
167
168 #endif // _VECTOR_HXX
169
170