1 // vector.cxx -- additional vector routines
3 // Written by Curtis Olson, started December 1997.
5 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
27 // #include <Include/fg_types.h>
32 // calculate the projection, p, of u along the direction of d.
33 void sgProjection(sgVec3 p, const sgVec3 u, const sgVec3 d){
34 double denom = sgScalarProductVec3(d,d);
35 if (denom == 0.) sgCopyVec3(p, u);
36 else sgScaleVec3(p, d, sgScalarProductVec3(u,d) / denom);
39 // Same thing, except using double precision
40 void sgProjection(sgdVec3 p, const sgdVec3 u, const sgdVec3 d){
41 double denom = sgdScalarProductVec3(d,d);
42 if (denom == 0.) sgdCopyVec3(p, u);
43 else sgdScaleVec3(p, d, sgdScalarProductVec3(u,d) / denom);
46 // Given a point p, and a line through p0 with direction vector d,
47 // find the closest point (p1) on the line
48 void sgClosestPointToLine( sgVec3 p1, const sgVec3 p, const sgVec3 p0,
56 // calculate the projection, u1, of u along d.
57 sgProjection(u1, u, d);
59 // calculate the point p1 along the line that is closest to p
61 sgAddVec3(p1, p0, u1);
65 // Given a point p, and a line through p0 with direction vector d,
66 // find the closest point (p1) on the line
67 void sgdClosestPointToLine( sgdVec3 p1, const sgdVec3 p, const sgdVec3 p0,
75 // calculate the projection, u1, of u along d.
76 sgProjection(u1, u, d);
78 // calculate the point p1 along the line that is closest to p
80 sgdAddVec3(p1, p0, u1);
84 // Given a point p, and a line through p0 with direction vector d,
85 // find the shortest distance (squared) from the point to the line
86 double sgClosestPointToLineDistSquared( const sgVec3 p, const sgVec3 p0,
94 // calculate the projection, u1, of u along d.
95 sgProjection(u1, u, d);
97 // v = u - u1 = vector from closest point on line, p1, to the
101 return ( sgScalarProductVec3(v, v) );
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 sgdClosestPointToLineDistSquared( const sgdVec3 p, const sgdVec3 p0,
113 sgdSubVec3(u, p, p0);
115 // calculate the projection, u1, of u along d.
116 sgProjection(u1, u, d);
118 // v = u - u1 = vector from closest point on line, p1, to the
119 // original point, p.
120 sgdSubVec3(v, u, u1);
122 return ( sgdScalarProductVec3(v, v) );
126 // This is a quicker form of
127 // sgMakeMatTrans4( sgMat4 sgTrans, sgVec3 trans )
128 // sgPostMultMat4( sgMat, sgTRANS );
129 void sgPostMultMat4ByTransMat4( sgMat4 src, const sgVec3 trans )
131 for( int i=0; i<4; i++) {
132 for( int j=0; j<3; j++ ) {
133 src[i][j] += (src[i][3] * trans[j]);