-
Notifications
You must be signed in to change notification settings - Fork 0
/
myutils3d.hpp
227 lines (163 loc) · 3.87 KB
/
myutils3d.hpp
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
#ifndef MYUTILS3D_HPP
#define MYUTILS3D_HPP
#include "myutils.hpp"
typedef double F;
inline F maxF(F f1, F f2)
{
return std::max(f1, f2);
}
struct P3 {
explicit P3() {}
explicit P3(F f) : x(f), y(f), z(f) {}
constexpr explicit P3 ( F x, F y, F z ) : x ( x ), y ( y ), z( z ) {}
constexpr F dist ( P3 const &o ) const {
return sqrt ( sqr ( x - o.x ) + sqr ( y - o.y ) + sqr ( z - o.z ) );
}
constexpr F dist2 ( P3 const &o ) const {
return sqr ( x - o.x ) + sqr ( y - o.y ) + sqr ( z - o.z );
}
constexpr F len2() const {
return x*x + y*y + z*z;
}
constexpr F len() const {
return sqrt ( x*x + y*y + z*z);
}
constexpr P3 norm() const {
F l = len();
if ( l == 0.0 )
return P3 ( 1.0, 0.0, 0.0);
return P3 ( x / l, y / l, z / l );
}
constexpr P3 &operator += (const P3 &o)
{
x += o.x;
y += o.y;
z += o.z;
return *this;
}
constexpr P3 &operator -= (const P3 &o)
{
x -= o.x;
y -= o.y;
z -= o.z;
return *this;
}
constexpr P3 &operator *= (F v)
{
x *= v;
y *= v;
z *= v;
return *this;
}
constexpr P3 &operator /= (F v)
{
F r = 1.0 / v;
x *= r;
y *= r;
z *= r;
return *this;
}
constexpr bool operator == ( const P3 &o ) const {
return ( std::abs ( x - o.x ) + std::abs ( y - o.y ) + std::abs ( z - o.z ) ) < EPS;
}
constexpr bool operator != ( const P3 &o ) const {
return ( std::abs ( x - o.x ) + std::abs ( y - o.y ) + std::abs ( z - o.z ) ) >= EPS;
}
template<int ind>
F comp() {
if (ind == 0)
return x;
if (ind == 1)
return y;
return z;
}
P xz() const
{
return P(x, z);
}
F x, y, z;
};
inline P3 xz(const P &p, F y)
{
return P3(p.x, y, p.z);
}
constexpr inline P3 operator - ( P3 p1, P3 p2 )
{
return P3 ( p1.x - p2.x, p1.y - p2.y, p1.z - p2.z );
}
constexpr inline P3 operator + ( P3 p1, P3 p2 )
{
return P3 ( p1.x + p2.x, p1.y + p2.y, p1.z + p2.z );
}
constexpr inline P3 operator * ( P3 p1, F v )
{
return P3 ( p1.x * v, p1.y * v, p1.z * v );
}
constexpr inline P3 operator * ( F v, P3 p1 )
{
return P3 ( p1.x * v, p1.y * v, p1.z * v );
}
constexpr inline P3 operator / ( P3 p1, F v )
{
return P3 ( p1.x / v, p1.y / v, p1.z / v );
}
constexpr inline P3 operator / ( F v, P3 p1 )
{
return P3 ( v / p1.x, v / p1.y, v / p1.z );
}
constexpr inline P3 operator * ( P3 p1, P3 p2 )
{
return P3 ( p1.x * p2.x, p1.y * p2.y, p1.z * p2.z );
}
constexpr inline P3 operator / ( P3 p1, P3 p2 )
{
return P3 ( p1.x / p2.x, p1.y / p2.y, p1.z / p2.z );
}
constexpr inline F dot ( P3 p1, P3 p2 )
{
return p1.x * p2.x + p1.y * p2.y + p1.z * p2.z;
}
constexpr inline P3 cross ( P3 p1, P3 p2 )
{
return P3(
p1.y * p2.z - p1.z * p2.y,
p1.z * p2.x - p1.x * p2.z,
p1.x * p2.y - p1.y * p2.x
);
}
inline F length(const P3 p)
{
return p.len();
}
inline P3 clampV(const P3 &v, F len)
{
F l = v.len();
if (l > len)
return v * (len / l);
return v;
}
struct Rect {
P3 pos;
P3 t1, t2;
};
inline P3 closestPoint(const Rect &rect, const P3 &p)
{
P3 rp = p - rect.pos;
float t1 = dot(rect.t1, rp) / dot(rect.t1, rect.t1);
float t2 = dot(rect.t2, rp) / dot(rect.t2, rect.t2);
t1 = clampF(t1, -1, 1);
t2 = clampF(t2, -1, 1);
return rect.pos + rect.t1 * t1 + rect.t2 * t2;
}
#ifdef ENABLE_LOGGING
inline std::ostream &operator << ( std::ostream &str, const P3 &p )
{
str << "(" << p.x << "," << p.y << "," << p.z << ")";
return str;
}
#endif
inline F random(F vmin, F vmax)
{
return rand() / (F) RAND_MAX * (vmax - vmin) + vmin;
}
#endif // MYUTILS3D_HPP