]> git.mxchange.org Git - simgear.git/blob - simgear/math/vector.hxx
Allow geocentric distance computations to return radians.
[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  - http://www.flightgear.org/~curt
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 General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 //
24 // $Id$
25
26
27 #ifndef _VECTOR_HXX
28 #define _VECTOR_HXX
29
30
31 #ifndef __cplusplus
32 # error This library requires C++
33 #endif
34
35 #include <simgear/compiler.h>
36
37 #include <plib/sg.h>
38
39
40 /**
41  * calculate the projection, p, of u along the direction of d.
42  * @param p (out) the projection
43  * @param u (in) the vector to be projected
44  * @param d (in) the direction onto which we project
45  */
46 void sgProjection(sgVec3 p, const sgVec3 u, const sgVec3 d);
47 void sgProjection(sgdVec3 p, const sgdVec3 u, const sgdVec3 d);
48
49
50 /**
51  * Map i.e. project a vector onto a plane.
52  * @param normal (in) normal vector for the plane
53  * @param v0 (in) a point on the plane
54  * @param vec (in) the vector to map onto the plane
55  * @param result (out) the result vector
56  */
57 inline void sgmap_vec_onto_cur_surface_plane( sgVec3 normal, 
58                                               sgVec3 v0, 
59                                               sgVec3 vec,
60                                               sgVec3 result )
61 {
62     sgVec3 u1, v, tmp;
63
64     // calculate a vector "u1" representing the shortest distance from
65     // the plane specified by normal and v0 to a point specified by
66     // "vec".  "u1" represents both the direction and magnitude of
67     // this desired distance.
68
69     // u1 = ( (normal <dot> vec) / (normal <dot> normal) ) * normal
70
71     sgScaleVec3( u1,
72                  normal,
73                  ( sgScalarProductVec3(normal, vec) /
74                    sgScalarProductVec3(normal, normal)
75                    )
76                  );
77
78     // printf("  vec = %.2f, %.2f, %.2f\n", vec[0], vec[1], vec[2]);
79     // printf("  v0 = %.2f, %.2f, %.2f\n", v0[0], v0[1], v0[2]);
80     // printf("  u1 = %.2f, %.2f, %.2f\n", u1[0], u1[1], u1[2]);
81    
82     // calculate the vector "v" which is the vector "vec" mapped onto
83     // the plane specified by "normal" and "v0".
84
85     // v = v0 + vec - u1
86
87     sgAddVec3(tmp, v0, vec);
88     sgSubVec3(v, tmp, u1);
89     // printf("  v = %.2f, %.2f, %.2f\n", v[0], v[1], v[2]);
90
91     // Calculate the vector "result" which is "v" - "v0" which is a
92     // directional vector pointing from v0 towards v
93
94     // result = v - v0
95
96     sgSubVec3(result, v, v0);
97     // printf("  result = %.2f, %.2f, %.2f\n", 
98     // result[0], result[1], result[2]);
99 }
100
101
102 /**
103  * Copy and negate a vector.
104  * @param dst (out) result vector
105  * @param src (in) input vector
106  */
107 inline void sgCopyNegateVec4( sgVec4 dst, sgVec4 src )
108 {
109         dst [ 0 ] = -src [ 0 ] ;
110         dst [ 1 ] = -src [ 1 ] ;
111         dst [ 2 ] = -src [ 2 ] ;
112         dst [ 3 ] = -src [ 3 ] ;
113 }
114
115 /**
116  * Given a point p, and a line through p0 with direction vector d,
117  * find the closest point (p1) on the line (float version).
118  * @param p1 (out) closest point to p on the line
119  * @param p (in) original point
120  * @param p0 (in) point on the line
121  * @param d (in) vector defining line direction
122  */
123 void sgClosestPointToLine( sgVec3 p1, const sgVec3 p, const sgVec3 p0,
124                            const sgVec3 d );
125
126 /**
127  * Given a point p, and a line through p0 with direction vector d,
128  * find the closest point (p1) on the line (double version).
129  * @param p1 (out) closest point to p on the line
130  * @param p (in) original point
131  * @param p0 (in) point on the line
132  * @param d (in) vector defining line direction
133  */
134 void sgdClosestPointToLine( sgdVec3 p1, const sgdVec3 p, const sgdVec3 p0,
135                             const sgdVec3 d );
136
137 /**
138  * Given a point p, and a line through p0 with direction vector d,
139  * find the shortest distance (squared) from the point to the line (float
140  * version.)
141  * @param p (in) original point
142  * @param p0 (in) point on the line
143  * @param d (in) vector defining line direction
144  * @return shortest distance (squared) from p to line
145  */
146 double sgClosestPointToLineDistSquared( const sgVec3 p, const sgVec3 p0,
147                                         const sgVec3 d );
148
149 /**
150  * Given a point p, and a line through p0 with direction vector d,
151  * find the shortest distance (squared) from the point to the line (double
152  * version.)
153  * @param p (in) original point
154  * @param p0 (in) point on the line
155  * @param d (in) vector defining line direction
156  * @return shortest distance (squared) from p to line
157  */
158 double sgdClosestPointToLineDistSquared( const sgdVec3 p, const sgdVec3 p0,
159                                          const sgdVec3 d );
160
161 /**
162  * This is same as:
163  *
164  * <li> sgMakeMatTrans4( sgMat4 sgTrans, sgVec3 trans )
165  * <li> sgPostMultMat4( sgMat4 src, sgTRANS );
166  *
167  * @param src starting sgMat4 matrix
168  * @param trans translation vector
169  */
170 void sgPostMultMat4ByTransMat4( sgMat4 src, const sgVec3 trans );
171
172
173 #endif // _VECTOR_HXX
174
175