2
* Copyright (C) 1999-2005 Terence M. Welsh
4
* This file is part of rsMath.
6
* rsMath is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License version 2.1 as published by the Free Software Foundation.
10
* rsMath is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include <rsMath/rsMath.h>
29
rsVec::rsVec(float xx, float yy, float zz){
38
void rsVec::set(float xx, float yy, float zz){
44
float rsVec::length(){
45
return(float(sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])));
48
float rsVec::normalize(){
49
float length = sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
54
const float normalizer(1.0f / length);
61
float rsVec::dot(rsVec vec1){
62
return(v[0] * vec1[0] + v[1] * vec1[1] + v[2] * vec1[2]);
65
void rsVec::cross(rsVec vec1, rsVec vec2){
66
v[0] = vec1[1] * vec2[2] - vec2[1] * vec1[2];
67
v[1] = vec1[2] * vec2[0] - vec2[2] * vec1[0];
68
v[2] = vec1[0] * vec2[1] - vec2[0] * vec1[1];
71
void rsVec::scale(float scale){
77
void rsVec::transPoint(const rsMatrix &m){
81
v[0] = x * m[0] + y * m[4] + z * m[8] + m[12];
82
v[1] = x * m[1] + y * m[5] + z * m[9] + m[13];
83
v[2] = x * m[2] + y * m[6] + z * m[10] + m[14];
87
void rsVec::transVec(const rsMatrix &m){
91
v[0] = x * m[0] + y * m[4] + z * m[8];
92
v[1] = x * m[1] + y * m[5] + z * m[9];
93
v[2] = x * m[2] + y * m[6] + z * m[10];
97
int rsVec::almostEqual(rsVec vec, float tolerance){
98
if(sqrtf((v[0]-vec[0])*(v[0]-vec[0])
99
+ (v[1]-vec[1])*(v[1]-vec[1])
100
+ (v[2]-vec[2])*(v[2]-vec[2]))
111
// Generic vector math functions
112
float rsLength(float *xyz){
113
return(float(sqrt(xyz[0] * xyz[0] + xyz[1] * xyz[1] + xyz[2] * xyz[2])));
117
float rsNormalize(float *xyz){
118
float length = float(sqrt(xyz[0] * xyz[0] + xyz[1] * xyz[1] + xyz[2] * xyz[2]));
121
float reciprocal = 1.0f / length;
122
xyz[0] *= reciprocal;
123
xyz[1] *= reciprocal;
124
xyz[2] *= reciprocal;
125
// Really freakin' stupid compiler bug fix for VC++ 5.0
133
float rsDot(float *xyz1, float *xyz2){
134
return(xyz1[0] * xyz2[0] + xyz1[1] * xyz2[1] + xyz1[2] * xyz2[2]);
138
void rsCross(float *xyz1, float *xyz2, float *xyzOut){
139
xyzOut[0] = xyz1[1] * xyz2[2] - xyz2[1] * xyz1[2];
140
xyzOut[1] = xyz1[2] * xyz2[0] - xyz2[2] * xyz1[0];
141
xyzOut[2] = xyz1[0] * xyz2[1] - xyz2[0] * xyz1[1];
145
void rsScaleVec(float *xyz, float scale){