]> git.mxchange.org Git - simgear.git/blob - simgear/math/vector.cxx
7c6f2c3cb46296eca6978968682cee5d4ea666ed
[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  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // 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., 675 Mass Ave, Cambridge, MA 02139, 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 shortest distance (squared) from the point to the line
34 double sgPointLineDistSquared( const sgVec3 p, const sgVec3 p0,
35                                const sgVec3 d ) {
36
37     sgVec3 u, u1, v;
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     // v = u - u1 = vector from closest point on line, p1, to the
47     // original point, p.
48     sgSubVec3(v, u, u1);
49
50     return ( sgScalarProductVec3(v, v) );
51 }
52
53
54 // Given a point p, and a line through p0 with direction vector d,
55 // find the shortest distance (squared) from the point to the line
56 double sgdPointLineDistSquared( const sgdVec3 p, const sgdVec3 p0,
57                                 const sgdVec3 d ) {
58
59     sgdVec3 u, u1, v;
60     double ud, dd, tmp;
61     
62     // u = p - p0
63     sgdSubVec3(u, p, p0);
64
65     // calculate the projection, u1, of u along d.
66     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
67     ud = sgdScalarProductVec3(u, d);
68     dd = sgdScalarProductVec3(d, d);
69     tmp = ud / dd;
70
71     sgdScaleVec3(u1, d, tmp);;
72
73     // v = u - u1 = vector from closest point on line, p1, to the
74     // original point, p.
75     sgdSubVec3(v, u, u1);
76
77     return ( sgdScalarProductVec3(v, v) );
78 }
79
80
81 // This is a quicker form of
82 // sgMakeMatTrans4( sgMat4 sgTrans, sgVec3 trans )
83 // sgPostMultMat4( sgMat, sgTRANS );
84 void sgPostMultMat4ByTransMat4( sgMat4 src, const sgVec3 trans )
85 {
86     for( int i=0; i<4; i++) {
87         for( int j=0; j<3; j++ ) {
88             src[i][j] += (src[i][3] * trans[j]);
89         }
90     }
91 }
92
93