-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic.h
More file actions
228 lines (189 loc) · 4.59 KB
/
basic.h
File metadata and controls
228 lines (189 loc) · 4.59 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#ifndef _BASIC_H_
#define _BASIC_H_
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <cmath>
#include <cassert>
#include <GfxTL/VectorXD.h>
#ifndef DLL_LINKAGE
#define DLL_LINKAGE
#endif
typedef float Scalar;
class DLL_LINKAGE Vec3f {
public:
typedef float ScalarType;
enum { Dim = 3 };
Vec3f () {}
Vec3f (float x, float y, float z) { setValue(x, y, z); }
Vec3f(const GfxTL::Vector3Df &v)
{
for(unsigned int i = 0; i < 3; ++i)
vec[i] = v[i];
}
explicit Vec3f (const float v[3])
{
setValue(v);
}
Vec3f& setValue (const float v[3])
{
for (int i = 0; i < 3; i++)
vec[i] = v[i];
return *this;
}
const float* getValue () const
{
return vec;
}
Vec3f& setValue (float x, float y, float z)
{
vec[0] = x; vec[1] = y; vec[2] = z;
return *this;
}
void getValue (float& x, float& y, float& z) const
{
x = vec[0]; y = vec[1]; z = vec[2];
}
operator const float* () const
{
return vec;
}
operator float *()
{
return vec;
}
/*float& operator [] (int i)
{
return vec[i];
}
const float& operator [] (int i) const
{
return vec[i];
}*/
float dot (const Vec3f& v) const
{
float s = vec[0] * v.vec[0];
for (int i = 1; i < 3; i++)
s += vec[i] * v.vec[i];
return s;
}
float dot(const float *v) const
{
return vec[0] * v[0] + vec[1] * v[1] + vec[2] * v[2];
}
Vec3f cross( const Vec3f& v ) const
{
return Vec3f(vec[1] * v.vec[2] - vec[2] * v.vec[1],
vec[2] * v.vec[0] - vec[0] * v.vec[2],
vec[0] * v.vec[1] - vec[1] * v.vec[0]);
}
float sqrLength () const
{
return dot(*this);
}
float length () const
{
return (float) std::sqrt(dot(*this));
}
float normalize ()
{
float len = length();
if (len > 0)
*this /= len;
return len;
}
bool equals (const Vec3f& v, float tolerance) const
{
return ((*this - v).sqrLength() <= tolerance);
}
friend bool operator == (const Vec3f& v1, const Vec3f& v2)
{
for (int i = 0; i < 3; i++)
if (v1.vec[i] != v2.vec[i])
return false;
return true;
}
friend bool operator < (const Vec3f& v1, const Vec3f& v2)
{
for (int i = 0; i < 3; i++)
if (v1.vec[i] >= v2.vec[i])
return false;
return true;
}
friend bool operator <= (const Vec3f& v1, const Vec3f& v2)
{
for (int i = 0; i < 3; i++)
if (v1.vec[i] > v2.vec[i])
return false;
return true;
}
friend bool operator > (const Vec3f& v1, const Vec3f& v2)
{
for (int i = 0; i < 3; i++)
if (v1.vec[i] <= v2.vec[i])
return false;
return true;
}
friend bool operator >= (const Vec3f& v1, const Vec3f& v2)
{
for (int i = 0; i < 3; i++)
if (v1.vec[i] < v2.vec[i])
return false;
return true;
}
friend bool operator != (const Vec3f& v1, const Vec3f& v2)
{
return !(v1 == v2);
}
Vec3f& operator *= (float s)
{
for (int i = 0; i < 3; i++)
vec[i] *= s;
return *this;
}
Vec3f& operator /= (float s)
{
for (int i = 0; i < 3; i++)
vec[i] /= s;
return *this;
}
Vec3f& operator += (const Vec3f& v)
{
for (int i = 0; i < 3; i++)
vec[i] += v.vec[i];
return *this;
}
Vec3f& operator -= (const Vec3f& v)
{
for (int i = 0; i < 3; i++)
vec[i] -= v.vec[i];
return *this;
}
void negate ()
{
for (int i = 0; i < 3; i++)
vec[i] = -vec[i];
}
friend Vec3f operator * (float s, const Vec3f& v) { return Vec3f(v) *= s; }
friend Vec3f operator * (const Vec3f& v, float s) { return Vec3f(v) *= s; }
friend Vec3f operator / (const Vec3f& v, float s) { return Vec3f(v) /= s; }
friend Vec3f operator + (const Vec3f& v1, const Vec3f& v2) { return Vec3f(v1) += v2; }
friend Vec3f operator - (const Vec3f& v1, const Vec3f& v2) { return Vec3f(v1) -= v2; }
friend Vec3f operator - (const Vec3f& v1) { Vec3f v2 = v1; v2.negate(); return v2; }
Vec3f operator*(const Vec3f &v) const { return Vec3f(vec[0] * v.vec[0], vec[1] * v.vec[1], vec[2] * v.vec[2]); }
// Konvertierung:
Vec3f& setValue (const Vec3f& v)
{
for (int i = 0; i < 3; i++)
vec[i] = (float) v[i];
return *this;
}
void getValue (Vec3f& v) const
{
for (int i = 0; i < 3; i++)
v[i] = (Scalar) vec[i];
}
private:
float vec[3];
};
#endif //_BASIC_H_