head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.41; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.59; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.05; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.22.02; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# vector.cxx

    00001 // vector.cxx -- additional vector routines
    00002 //
    00003 // Written by Curtis Olson, started December 1997.
    00004 //
    00005 // Copyright (C) 1997  Curtis L. Olson  - curt@@infoplane.com
    00006 //
    00007 // This library is free software; you can redistribute it and/or
    00008 // modify it under the terms of the GNU Library General Public
    00009 // License as published by the Free Software Foundation; either
    00010 // version 2 of the License, or (at your option) any later version.
    00011 //
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015 // Library General Public License for more details.
    00016 //
    00017 // You should have received a copy of the GNU Library General Public
    00018 // License along with this library; if not, write to the
    00019 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00020 // Boston, MA  02111-1307, USA.
    00021 //
    00022 // $Id: vector.cxx,v 1.5 2000/10/23 14:57:29 curt Exp $
    00023
    00024
    00025 #include <math.h>
    00026 #include <stdio.h>
    00027
    00028 // #include <Include/fg_types.h>
    00029
    00030 #include "vector.hxx"
    00031
    00032
    00033 // Given a point p, and a line through p0 with direction vector d,
    00034 // find the closest point (p1) on the line
    00035 void sgClosestPointToLine( sgVec3 p1, const sgVec3 p, const sgVec3 p0,
    00036                            const sgVec3 d ) {
    00037
    00038     sgVec3 u, u1;
    00039
    00040     // u = p - p0
    00041     sgSubVec3(u, p, p0);
    00042
    00043     // calculate the projection, u1, of u along d.
    00044     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
    00045     sgScaleVec3( u1, d, sgScalarProductVec3(u,d) / sgScalarProductVec3(d,d) );
    00046
    00047     // calculate the point p1 along the line that is closest to p
    00048     // p0 = p1 + u1
    00049     sgAddVec3(p1, p0, u1);
    00050 }
    00051
    00052
    00053 // Given a point p, and a line through p0 with direction vector d,
    00054 // find the closest point (p1) on the line
    00055 void sgdClosestPointToLine( sgdVec3 p1, const sgdVec3 p, const sgdVec3 p0,
    00056                             const sgdVec3 d ) {
    00057
    00058     sgdVec3 u, u1;
    00059
    00060     // u = p - p0
    00061     sgdSubVec3(u, p, p0);
    00062
    00063     // calculate the projection, u1, of u along d.
    00064     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
    00065     double ud = sgdScalarProductVec3(u, d);
    00066     double dd = sgdScalarProductVec3(d, d);
    00067     double tmp = ud / dd;
    00068
    00069     sgdScaleVec3(u1, d, tmp);;
    00070
    00071     // calculate the point p1 along the line that is closest to p
    00072     // p0 = p1 + u1
    00073     sgdAddVec3(p1, p0, u1);
    00074 }
    00075
    00076
    00077 // Given a point p, and a line through p0 with direction vector d,
    00078 // find the shortest distance (squared) from the point to the line
    00079 double sgClosestPointToLineDistSquared( const sgVec3 p, const sgVec3 p0,
    00080                                         const sgVec3 d ) {
    00081
    00082     sgVec3 u, u1, v;
    00083
    00084     // u = p - p0
    00085     sgSubVec3(u, p, p0);
    00086
    00087     // calculate the projection, u1, of u along d.
    00088     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
    00089     sgScaleVec3( u1, d, sgScalarProductVec3(u,d) / sgScalarProductVec3(d,d) );
    00090
    00091     // v = u - u1 = vector from closest point on line, p1, to the
    00092     // original point, p.
    00093     sgSubVec3(v, u, u1);
    00094
    00095     return ( sgScalarProductVec3(v, v) );
    00096 }
    00097
    00098
    00099 // Given a point p, and a line through p0 with direction vector d,
    00100 // find the shortest distance (squared) from the point to the line
    00101 double sgdClosestPointToLineDistSquared( const sgdVec3 p, const sgdVec3 p0,
    00102                                          const sgdVec3 d ) {
    00103
    00104     sgdVec3 u, u1, v;
    00105
    00106     // u = p - p0
    00107     sgdSubVec3(u, p, p0);
    00108
    00109     // calculate the projection, u1, of u along d.
    00110     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
    00111     double ud = sgdScalarProductVec3(u, d);
    00112     double dd = sgdScalarProductVec3(d, d);
    00113     double tmp = ud / dd;
    00114
    00115     sgdScaleVec3(u1, d, tmp);;
    00116
    00117     // v = u - u1 = vector from closest point on line, p1, to the
    00118     // original point, p.
    00119     sgdSubVec3(v, u, u1);
    00120
    00121     return ( sgdScalarProductVec3(v, v) );
    00122 }
    00123
    00124
    00125 // This is a quicker form of
    00126 // sgMakeMatTrans4( sgMat4 sgTrans, sgVec3 trans )
    00127 // sgPostMultMat4( sgMat, sgTRANS );
    00128 void sgPostMultMat4ByTransMat4( sgMat4 src, const sgVec3 trans )
    00129 {
    00130     for( int i=0; i<4; i++) {
    00131         for( int j=0; j<3; j++ ) {
    00132             src[i][j] += (src[i][3] * trans[j]);
    00133         }
    00134     }
    00135 }
    00136
    00137

@ 1.3 log @Oops, try again. @ text @d146 1 a146 1