-
Notifications
You must be signed in to change notification settings - Fork 2
/
vect3.h
executable file
·110 lines (86 loc) · 4.64 KB
/
vect3.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Copyright (C) 2010 Matthew Baranowski, Sander van Rossen & Raven software.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined(VECT3_INC)
#define VECT3_INC
#include <math.h>
#include <assert.h>
class Vect3
{
float v[3];
public:
Vect3(const float val) {v[0]=val;v[1]=val;v[2]=val;}
Vect3() {}//never put anything in here! too slow}
Vect3(const float x,const float y,const float z) {v[0]=x;v[1]=y;v[2]=z;}
Vect3(const Vect3& t) {v[0]=t.v[0];v[1]=t.v[1];v[2]=t.v[2];}
Vect3(const float *t) {v[0]=t[0];v[1]=t[1];v[2]=t[2];}
float& operator[](int i) {return v[i];}
float& x() {return v[0];}
float& y() {return v[1];}
float& z() {return v[2];}
const float& operator[](int i) const {return v[i];}
const float& x() const {return v[0];}
const float& y() const {return v[1];}
const float& z() const {return v[2];}
void Set(const float x,const float y,const float z) {v[0]=x;v[1]=y;v[2]=z;}
float Len() const {return (float)sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);}
float Len2() const {return v[0]*v[0]+v[1]*v[1]+v[2]*v[2];}
void Norm() {(*this)/=this->Len();}
bool ZeroNorm() {float d=this->Len();if (d>1E-10) {(*this)/=d;return true;} (*this)=0.0f; return false;}
void SafeNorm() {assert(this->Len()>1E-10);(*this)/=this->Len();}
const Vect3 &operator= (const float d) {v[0]=d;v[1]=d;v[2]=d; return *this; }
const Vect3 &operator= (const Vect3& t) {v[0]=t.v[0];v[1]=t.v[1];v[2]=t.v[2]; return *this; }
const Vect3 &operator+= (const float d) {v[0]+=d;v[1]+=d;v[2]+=d; return *this; }
const Vect3 &operator+= (const Vect3& t) {v[0]+=t.v[0];v[1]+=t.v[1];v[2]+=t.v[2]; return *this; }
const Vect3 &operator-= (const float d) {v[0]-=d;v[1]-=d;v[2]-=d; return *this; }
const Vect3 &operator-= (const Vect3& t) {v[0]-=t.v[0];v[1]-=t.v[1];v[2]-=t.v[2]; return *this; }
const Vect3 &operator*= (const float d) {v[0]*=d;v[1]*=d;v[2]*=d; return *this; }
const Vect3 &operator*= (const Vect3& t) {v[0]*=t.v[0];v[1]*=t.v[1];v[2]*=t.v[2]; return *this; }
const Vect3 &operator/= (const float d);
const Vect3 &operator/= (const Vect3& t) {v[0]/=t.v[0];v[1]/=t.v[1];v[2]/=t.v[2]; return *this; }
float operator^ (const Vect3& t) const {return v[0]*t.v[0]+v[1]*t.v[1]+v[2]*t.v[2];}
float Dist(const Vect3&) const;
float Dist2(const Vect3&) const;
void Cross(const Vect3&);
void NegCross(const Vect3&);
void Perp();
void Min(const Vect3&);
void Max(const Vect3&);
float MaxElement() const;
int MaxElementIndex() const;
void Interp(const Vect3 &v1,const Vect3 &v2,float t) {*this=v1;*this-=v2;*this*=t;*this+=v2;}
// bool operator== (const Vect3& t) const {return v[0]==t.v[0]&&v[1]==t.v[1]&&v[2]==t.v[2];}
bool operator== (const Vect3& t) const {return fabs(v[0]-t.v[0])<.001f&&fabs(v[1]-t.v[1])<.001f&&fabs(v[2]-t.v[2])<.001f;}
bool operator< (const Vect3& t) const {assert(0);return false;}
bool operator!= (const Vect3& t) const {return !(v[0]==t.v[0]&&v[1]==t.v[1]&&v[2]==t.v[2]);}
bool operator> (const Vect3& t) const {assert(0);return false;}
inline Vect3 operator +(const Vect3 &rhs) const { return Vect3(v[0]+rhs.v[0], v[1]+rhs.v[1], v[2]+rhs.v[2]); }
inline Vect3 operator -(const Vect3 &rhs) const { return Vect3(v[0]-rhs.v[0], v[1]-rhs.v[1], v[2]-rhs.v[2]); }
inline Vect3 operator *(const Vect3 &rhs) const { return Vect3(v[0]*rhs.v[0], v[1]*rhs.v[1], v[2]*rhs.v[2]); }
inline Vect3 operator *(const float scalar) const { return Vect3(v[0]*scalar, v[1]*scalar, v[2]*scalar); }
inline friend Vect3 operator *(const float scalar, const Vect3 &rhs);
inline Vect3 operator /(const Vect3 &rhs) const { return Vect3(v[0]/rhs.v[0], v[1]/rhs.v[1], v[2]/rhs.v[2]); }
inline Vect3 operator /(const float scalar) const { return Vect3(v[0]/scalar, v[1]/scalar, v[2]/scalar); }
};
inline Vect3 operator *(const float scalar, const Vect3 &rhs)
{
return Vect3(scalar*rhs.v[0], scalar*rhs.v[1], scalar*rhs.v[2]);
}
extern const Vect3 Vect3X;
extern const Vect3 Vect3Y;
extern const Vect3 Vect3Z;
extern const Vect3 Vect3negX;
extern const Vect3 Vect3negY;
extern const Vect3 Vect3negZ;
extern const Vect3 Vect3Zero;
#endif