1 // vector.cxx -- additional vector routines
3 // Written by Curtis Olson, started December 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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 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.
28 // #include <Include/fg_types.h>
33 // Given a point p, and a line through p0 with direction vector d,
34 // find the shortest distance (squared) from the point to the line
35 double sgPointLineDistSquared( const sgVec3 p, const sgVec3 p0,
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) );
47 // v = u - u1 = vector from closest point on line, p1, to the
51 return ( sgScalarProductVec3(v, v) );
55 // Given a point p, and a line through p0 with direction vector d,
56 // find the shortest distance (squared) from the point to the line
57 double sgdPointLineDistSquared( const sgdVec3 p, const sgdVec3 p0,
66 // calculate the projection, u1, of u along d.
67 // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
68 ud = sgdScalarProductVec3(u, d);
69 dd = sgdScalarProductVec3(d, d);
72 sgdScaleVec3(u1, d, tmp);;
74 // v = u - u1 = vector from closest point on line, p1, to the
78 return ( sgdScalarProductVec3(v, v) );
82 // This is a quicker form of
83 // sgMakeMatTrans4( sgMat4 sgTrans, sgVec3 trans )
84 // sgPostMultMat4( sgMat, sgTRANS );
85 void sgPostMultMat4ByTransMat4( sgMat4 src, const sgVec3 trans )
87 for( int i=0; i<4; i++) {
88 for( int j=0; j<3; j++ ) {
89 src[i][j] += (src[i][3] * trans[j]);