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
229
230
231
232
233
234
235
236
237
238
239
240
|
/*
* 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 2
* 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, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
#include <iostream>
#include "BLI_math_vector.h"
namespace blender {
struct double3 {
double x, y, z;
double3() = default;
double3(const double *ptr) : x{ptr[0]}, y{ptr[1]}, z{ptr[2]}
{
}
double3(const double (*ptr)[3]) : double3((const double *)ptr)
{
}
explicit double3(double value) : x(value), y(value), z(value)
{
}
explicit double3(int value) : x(value), y(value), z(value)
{
}
double3(double x, double y, double z) : x{x}, y{y}, z{z}
{
}
operator const double *() const
{
return &x;
}
operator double *()
{
return &x;
}
double normalize_and_get_length()
{
return normalize_v3_db(*this);
}
double3 normalized() const
{
double3 result;
normalize_v3_v3_db(result, *this);
return result;
}
double length() const
{
return len_v3_db(*this);
}
double length_squared() const
{
return len_squared_v3_db(*this);
}
void reflect(const double3 &normal)
{
*this = this->reflected(normal);
}
double3 reflected(const double3 &normal) const
{
double3 result;
reflect_v3_v3v3_db(result, *this, normal);
return result;
}
static double3 safe_divide(const double3 &a, const double3 &b)
{
double3 result;
result.x = (b.x == 0.0) ? 0.0 : a.x / b.x;
result.y = (b.y == 0.0) ? 0.0 : a.y / b.y;
result.z = (b.z == 0.0) ? 0.0 : a.z / b.z;
return result;
}
void invert()
{
x = -x;
y = -y;
z = -z;
}
friend double3 operator+(const double3 &a, const double3 &b)
{
return {a.x + b.x, a.y + b.y, a.z + b.z};
}
void operator+=(const double3 &b)
{
this->x += b.x;
this->y += b.y;
this->z += b.z;
}
friend double3 operator-(const double3 &a, const double3 &b)
{
return {a.x - b.x, a.y - b.y, a.z - b.z};
}
friend double3 operator-(const double3 &a)
{
return {-a.x, -a.y, -a.z};
}
void operator-=(const double3 &b)
{
this->x -= b.x;
this->y -= b.y;
this->z -= b.z;
}
void operator*=(const double &scalar)
{
this->x *= scalar;
this->y *= scalar;
this->z *= scalar;
}
void operator*=(const double3 &other)
{
this->x *= other.x;
this->y *= other.y;
this->z *= other.z;
}
friend double3 operator*(const double3 &a, const double3 &b)
{
return {a.x * b.x, a.y * b.y, a.z * b.z};
}
friend double3 operator*(const double3 &a, const double &b)
{
return {a.x * b, a.y * b, a.z * b};
}
friend double3 operator*(const double &a, const double3 &b)
{
return b * a;
}
friend double3 operator/(const double3 &a, const double &b)
{
BLI_assert(b != 0.0);
return {a.x / b, a.y / b, a.z / b};
}
friend bool operator==(const double3 &a, const double3 &b)
{
return a.x == b.x && a.y == b.y && a.z == b.z;
}
friend std::ostream &operator<<(std::ostream &stream, const double3 &v)
{
stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
return stream;
}
static double dot(const double3 &a, const double3 &b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
static double3 cross_high_precision(const double3 &a, const double3 &b)
{
double3 result;
cross_v3_v3v3_db(result, a, b);
return result;
}
static double3 project(const double3 &a, const double3 &b)
{
double3 result;
project_v3_v3v3_db(result, a, b);
return result;
}
static double distance(const double3 &a, const double3 &b)
{
return (a - b).length();
}
static double distance_squared(const double3 &a, const double3 &b)
{
return double3::dot(a, b);
}
static double3 interpolate(const double3 &a, const double3 &b, double t)
{
return a * (1 - t) + b * t;
}
static double3 abs(const double3 &a)
{
return double3(fabs(a.x), fabs(a.y), fabs(a.z));
}
/* orient3d gives the exact result, using multiprecision artihmetic when result
* is close to zero. orient3d_fast just uses double arithmetic, so may be
* wrong if the answer is very close to zero.
* Similarly, for insphere and insphere_fast.
*/
static int orient3d(const double3 &a, const double3 &b, const double3 &c, const double3 &d);
static int orient3d_fast(const double3 &a, const double3 &b, const double3 &c, const double3 &d);
static int insphere(
const double3 &a, const double3 &b, const double3 &c, const double3 &d, const double3 &e);
static int insphere_fast(
const double3 &a, const double3 &b, const double3 &c, const double3 &d, const double3 &e);
};
} // namespace blender
|