1 /*
2  * Copyright (C) 1999-2005  Terence M. Welsh
3  *
4  * This file is part of rsMath.
5  *
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.
9  *
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.
14  *
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
18  */
19 
20 
21 #ifndef RSVEC_H
22 #define RSVEC_H
23 
24 
25 
26 class rsMatrix;
27 
28 
29 
30 class rsVec{
31 public:
32 	float v[3];
33 
34 	rsVec();
35 	rsVec(const float &xx, const float &yy, const float &zz);
36 	virtual ~rsVec();
37 	void set(const float &xx, const float &yy, const float &zz);
38 	float length() const;
39 	float length2() const;
40 	float normalize();
41 	float dot(const rsVec &) const;
42 	void cross(const rsVec &, const rsVec &);
43 	void scale(const float &);
44 	void transPoint(const rsMatrix &m);
45 	void transVec(const rsMatrix &m);
46 	int almostEqual(const rsVec &vec, const float &tolerance) const;
47 	rsVec & linearInterp(const rsVec &, const rsVec &, const float &);
48 
49 	float & operator [] (const int i) {return v[i];}
50 	const float & operator [] (const int i) const {return v[i];}
x()51 	float & x() {return v[0];}
y()52 	float & y() {return v[1];}
z()53 	float & z() {return v[2];}
zero()54 	rsVec & zero ()
55 		{v[0]=0;v[1]=0;v[2]=0;return *this;}
56 	rsVec & operator = (const rsVec &vec)
57 		{v[0]=vec[0];v[1]=vec[1];v[2]=vec[2];return *this;}
58 	rsVec operator + (const rsVec &vec) const
59 		{return(rsVec(v[0]+vec[0], v[1]+vec[1], v[2]+vec[2]));}
60 	rsVec operator - (const rsVec &vec) const
61 		{return(rsVec(v[0]-vec[0], v[1]-vec[1], v[2]-vec[2]));}
62 	rsVec operator * (const float &mul) const
63 		{return(rsVec(v[0]*mul, v[1]*mul, v[2]*mul));}
64 	rsVec operator / (const float &div) const
65 		{float rec = 1.0f/div; return(rsVec(v[0]*rec, v[1]*rec, v[2]*rec));}
66 	rsVec & operator += (const rsVec &vec)
67 		{v[0]+=vec[0];v[1]+=vec[1];v[2]+=vec[2];return *this;}
68 	rsVec & operator -= (const rsVec &vec)
69 		{v[0]-=vec[0];v[1]-=vec[1];v[2]-=vec[2];return *this;}
70 	rsVec & operator *= (const rsVec &vec)
71 		{v[0]*=vec[0];v[1]*=vec[1];v[2]*=vec[2];return *this;}
72 	rsVec & operator *= (const float &mul)
73 		{v[0]*=mul;v[1]*=mul;v[2]*=mul;return *this;}
74 };
75 
76 
77 
78 #endif
79