]> git.mxchange.org Git - simgear.git/blob - simgear/math/vector.hxx
2d3feedcf99108bf3855ec1b2b0afb51dd41fdeb
[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
41 inline void sgmap_vec_onto_cur_surface_plane( sgVec3 normal, 
42                                               sgVec3 v0, 
43                                               sgVec3 vec,
44                                               sgVec3 result )
45 {
46     sgVec3 u1, v, tmp;
47
48     // calculate a vector "u1" representing the shortest distance from
49     // the plane specified by normal and v0 to a point specified by
50     // "vec".  "u1" represents both the direction and magnitude of
51     // this desired distance.
52
53     // u1 = ( (normal <dot> vec) / (normal <dot> normal) ) * normal
54
55     sgScaleVec3( u1,
56                  normal,
57                  ( sgScalarProductVec3(normal, vec) /
58                    sgScalarProductVec3(normal, normal)
59                    )
60                  );
61
62     // printf("  vec = %.2f, %.2f, %.2f\n", vec[0], vec[1], vec[2]);
63     // printf("  v0 = %.2f, %.2f, %.2f\n", v0[0], v0[1], v0[2]);
64     // printf("  u1 = %.2f, %.2f, %.2f\n", u1[0], u1[1], u1[2]);
65    
66     // calculate the vector "v" which is the vector "vec" mapped onto
67     // the plane specified by "normal" and "v0".
68
69     // v = v0 + vec - u1
70
71     sgAddVec3(tmp, v0, vec);
72     sgSubVec3(v, tmp, u1);
73     // printf("  v = %.2f, %.2f, %.2f\n", v[0], v[1], v[2]);
74
75     // Calculate the vector "result" which is "v" - "v0" which is a
76     // directional vector pointing from v0 towards v
77
78     // result = v - v0
79
80     sgSubVec3(result, v, v0);
81     // printf("  result = %.2f, %.2f, %.2f\n", 
82     // result[0], result[1], result[2]);
83 }
84
85
86 inline void sgCopyNegateVec4( sgVec4 dst, sgVec4 src )
87 {
88         dst [ 0 ] = -src [ 0 ] ;
89         dst [ 1 ] = -src [ 1 ] ;
90         dst [ 2 ] = -src [ 2 ] ;
91         dst [ 3 ] = -src [ 3 ] ;
92 }
93
94 // Given a point p, and a line through p0 with direction vector d,
95 // find the shortest distance (squared) from the point to the line
96 double sgPointLineDistSquared( const sgVec3 p, const sgVec3 p0,
97                                const sgVec3 d );
98
99 // Given a point p, and a line through p0 with direction vector d,
100 // find the shortest distance (squared) from the point to the line
101 double sgdPointLineDistSquared( const sgdVec3 p, const sgdVec3 p0,
102                                 const sgdVec3 d );
103
104 // This is same as
105 // sgMakeMatTrans4( sgMat4 sgTrans, sgVec3 trans )
106 // sgPostMultMat4( sgMat, sgTRANS );
107 void sgPostMultMat4ByTransMat4( sgMat4 src, const sgVec3 trans );
108
109 #endif // _VECTOR_HXX
110
111