]> git.mxchange.org Git - simgear.git/blob - simgear/math/vector.cxx
Update a few more instances of my email address.
[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 Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA  02111-1307, USA.
21 //
22 // $Id$
23
24
25 #include <math.h>
26 #include <stdio.h>
27
28 // #include <Include/fg_types.h>
29
30 #include "vector.hxx"
31
32
33 // Given a point p, and a line through p0 with direction vector d,
34 // find the closest point (p1) on the line
35 void sgClosestPointToLine( sgVec3 p1, const sgVec3 p, const sgVec3 p0,
36                            const sgVec3 d ) {
37
38     sgVec3 u, u1;
39     
40     // u = p - p0
41     sgSubVec3(u, p, p0);
42
43     // calculate the projection, u1, of u along d.
44     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
45     sgScaleVec3( u1, d, sgScalarProductVec3(u,d) / sgScalarProductVec3(d,d) );
46
47     // calculate the point p1 along the line that is closest to p
48     // p0 = p1 + u1
49     sgAddVec3(p1, p0, u1);
50 }
51
52
53 // Given a point p, and a line through p0 with direction vector d,
54 // find the closest point (p1) on the line
55 void sgdClosestPointToLine( sgdVec3 p1, const sgdVec3 p, const sgdVec3 p0,
56                             const sgdVec3 d ) {
57
58     sgdVec3 u, u1;
59     
60     // u = p - p0
61     sgdSubVec3(u, p, p0);
62
63     // calculate the projection, u1, of u along d.
64     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
65     double ud = sgdScalarProductVec3(u, d);
66     double dd = sgdScalarProductVec3(d, d);
67     double tmp = ud / dd;
68
69     sgdScaleVec3(u1, d, tmp);;
70
71     // calculate the point p1 along the line that is closest to p
72     // p0 = p1 + u1
73     sgdAddVec3(p1, p0, u1);
74 }
75
76
77 // Given a point p, and a line through p0 with direction vector d,
78 // find the shortest distance (squared) from the point to the line
79 double sgClosestPointToLineDistSquared( const sgVec3 p, const sgVec3 p0,
80                                         const sgVec3 d ) {
81
82     sgVec3 u, u1, v;
83     
84     // u = p - p0
85     sgSubVec3(u, p, p0);
86
87     // calculate the projection, u1, of u along d.
88     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
89     sgScaleVec3( u1, d, sgScalarProductVec3(u,d) / sgScalarProductVec3(d,d) );
90
91     // v = u - u1 = vector from closest point on line, p1, to the
92     // original point, p.
93     sgSubVec3(v, u, u1);
94
95     return ( sgScalarProductVec3(v, v) );
96 }
97
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 sgdClosestPointToLineDistSquared( const sgdVec3 p, const sgdVec3 p0,
102                                          const sgdVec3 d ) {
103
104     sgdVec3 u, u1, v;
105     
106     // u = p - p0
107     sgdSubVec3(u, p, p0);
108
109     // calculate the projection, u1, of u along d.
110     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
111     double ud = sgdScalarProductVec3(u, d);
112     double dd = sgdScalarProductVec3(d, d);
113     double tmp = ud / dd;
114
115     sgdScaleVec3(u1, d, tmp);;
116
117     // v = u - u1 = vector from closest point on line, p1, to the
118     // original point, p.
119     sgdSubVec3(v, u, u1);
120
121     return ( sgdScalarProductVec3(v, v) );
122 }
123
124
125 // This is a quicker form of
126 // sgMakeMatTrans4( sgMat4 sgTrans, sgVec3 trans )
127 // sgPostMultMat4( sgMat, sgTRANS );
128 void sgPostMultMat4ByTransMat4( sgMat4 src, const sgVec3 trans )
129 {
130     for( int i=0; i<4; i++) {
131         for( int j=0; j<3; j++ ) {
132             src[i][j] += (src[i][3] * trans[j]);
133         }
134     }
135 }
136
137