]> git.mxchange.org Git - simgear.git/blob - simgear/math/vector.hxx
Initial revision
[simgear.git] / simgear / math / vector.hxx
1 // vector.hxx -- additional vector routines
2 //
3 // Written by Curtis Olson, started December 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifndef _VECTOR_HXX
25 #define _VECTOR_HXX
26
27
28 #ifndef __cplusplus                                                          
29 # error This library requires C++
30 #endif                                   
31
32 #ifdef HAVE_CONFIG_H
33 #  include <config.h>
34 #endif
35
36 #include <simgear/compiler.h>
37
38 #include <plib/sg.h>
39
40 #include <simgear/mat3.h>
41
42
43 // Map a vector onto the plane specified by normal
44 void map_vec_onto_cur_surface_plane( MAT3vec normal,
45                                      MAT3vec v0,
46                                      MAT3vec vec,
47                                      MAT3vec result );
48
49
50 inline void sgmap_vec_onto_cur_surface_plane( sgVec3 normal, 
51                                               sgVec3 v0, 
52                                               sgVec3 vec,
53                                               sgVec3 result )
54 {
55     sgVec3 u1, v, tmp;
56
57     // calculate a vector "u1" representing the shortest distance from
58     // the plane specified by normal and v0 to a point specified by
59     // "vec".  "u1" represents both the direction and magnitude of
60     // this desired distance.
61
62     // u1 = ( (normal <dot> vec) / (normal <dot> normal) ) * normal
63
64     sgScaleVec3( u1,
65                  normal,
66                  ( sgScalarProductVec3(normal, vec) /
67                    sgScalarProductVec3(normal, normal)
68                    )
69                  );
70
71     // printf("  vec = %.2f, %.2f, %.2f\n", vec[0], vec[1], vec[2]);
72     // printf("  v0 = %.2f, %.2f, %.2f\n", v0[0], v0[1], v0[2]);
73     // printf("  u1 = %.2f, %.2f, %.2f\n", u1[0], u1[1], u1[2]);
74    
75     // calculate the vector "v" which is the vector "vec" mapped onto
76     // the plane specified by "normal" and "v0".
77
78     // v = v0 + vec - u1
79
80     sgAddVec3(tmp, v0, vec);
81     sgSubVec3(v, tmp, u1);
82     // printf("  v = %.2f, %.2f, %.2f\n", v[0], v[1], v[2]);
83
84     // Calculate the vector "result" which is "v" - "v0" which is a
85     // directional vector pointing from v0 towards v
86
87     // result = v - v0
88
89     sgSubVec3(result, v, v0);
90     // printf("  result = %.2f, %.2f, %.2f\n", 
91     // result[0], result[1], result[2]);
92 }
93
94
95 // Given a point p, and a line through p0 with direction vector d,
96 // find the shortest distance from the point to the line
97 double fgPointLine(MAT3vec p, MAT3vec p0, MAT3vec d);
98
99
100 // Given a point p, and a line through p0 with direction vector d,
101 // find the shortest distance (squared) from the point to the line
102 double fgPointLineSquared(MAT3vec p, MAT3vec p0, MAT3vec d);
103
104
105 // Given a point p, and a line through p0 with direction vector d,
106 // find the shortest distance (squared) from the point to the line
107 double sgPointLineDistSquared( const sgVec3 p, const sgVec3 p0,
108                                const sgVec3 d );
109
110 // Given a point p, and a line through p0 with direction vector d,
111 // find the shortest distance (squared) from the point to the line
112 double sgdPointLineDistSquared( const sgdVec3 p, const sgdVec3 p0,
113                                 const sgdVec3 d );
114
115
116 #endif // _VECTOR_HXX
117
118