]> git.mxchange.org Git - simgear.git/blob - simgear/math/vector.cxx
Modified Files:
[simgear.git] / simgear / math / vector.cxx
1 // vector.cxx -- additional vector routines
2 //
3 // Written by Curtis Olson, started December 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library 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 GNU
15 // Library 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #include <math.h>
25 #include <stdio.h>
26
27 // #include <Include/fg_types.h>
28
29 #include "vector.hxx"
30
31
32 // Given a point p, and a line through p0 with direction vector d,
33 // find the closest point (p1) on the line
34 void sgClosestPointToLine( sgVec3 p1, const sgVec3 p, const sgVec3 p0,
35                            const sgVec3 d ) {
36
37     sgVec3 u, u1;
38     
39     // u = p - p0
40     sgSubVec3(u, p, p0);
41
42     // calculate the projection, u1, of u along d.
43     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
44     sgScaleVec3( u1, d, sgScalarProductVec3(u,d) / sgScalarProductVec3(d,d) );
45
46     // calculate the point p1 along the line that is closest to p
47     // p0 = p1 + u1
48     sgAddVec3(p1, p0, u1);
49 }
50
51
52 // Given a point p, and a line through p0 with direction vector d,
53 // find the closest point (p1) on the line
54 void sgdClosestPointToLine( sgdVec3 p1, const sgdVec3 p, const sgdVec3 p0,
55                             const sgdVec3 d ) {
56
57     sgdVec3 u, u1;
58     
59     // u = p - p0
60     sgdSubVec3(u, p, p0);
61
62     // calculate the projection, u1, of u along d.
63     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
64     double ud = sgdScalarProductVec3(u, d);
65     double dd = sgdScalarProductVec3(d, d);
66     double tmp = ud / dd;
67
68     sgdScaleVec3(u1, d, tmp);;
69
70     // calculate the point p1 along the line that is closest to p
71     // p0 = p1 + u1
72     sgdAddVec3(p1, p0, u1);
73 }
74
75
76 // Given a point p, and a line through p0 with direction vector d,
77 // find the shortest distance (squared) from the point to the line
78 double sgClosestPointToLineDistSquared( const sgVec3 p, const sgVec3 p0,
79                                         const sgVec3 d ) {
80
81     sgVec3 u, u1, v;
82     
83     // u = p - p0
84     sgSubVec3(u, p, p0);
85
86     // calculate the projection, u1, of u along d.
87     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
88     sgScaleVec3( u1, d, sgScalarProductVec3(u,d) / sgScalarProductVec3(d,d) );
89
90     // v = u - u1 = vector from closest point on line, p1, to the
91     // original point, p.
92     sgSubVec3(v, u, u1);
93
94     return ( sgScalarProductVec3(v, v) );
95 }
96
97
98 // Given a point p, and a line through p0 with direction vector d,
99 // find the shortest distance (squared) from the point to the line
100 double sgdClosestPointToLineDistSquared( const sgdVec3 p, const sgdVec3 p0,
101                                          const sgdVec3 d ) {
102
103     sgdVec3 u, u1, v;
104     
105     // u = p - p0
106     sgdSubVec3(u, p, p0);
107
108     // calculate the projection, u1, of u along d.
109     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
110     double ud = sgdScalarProductVec3(u, d);
111     double dd = sgdScalarProductVec3(d, d);
112     double tmp = ud / dd;
113
114     sgdScaleVec3(u1, d, tmp);;
115
116     // v = u - u1 = vector from closest point on line, p1, to the
117     // original point, p.
118     sgdSubVec3(v, u, u1);
119
120     return ( sgdScalarProductVec3(v, v) );
121 }
122
123
124 // This is a quicker form of
125 // sgMakeMatTrans4( sgMat4 sgTrans, sgVec3 trans )
126 // sgPostMultMat4( sgMat, sgTRANS );
127 void sgPostMultMat4ByTransMat4( sgMat4 src, const sgVec3 trans )
128 {
129     for( int i=0; i<4; i++) {
130         for( int j=0; j<3; j++ ) {
131             src[i][j] += (src[i][3] * trans[j]);
132         }
133     }
134 }
135
136