Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_mpq3.hh « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b9eda2ad7e1e98449a3137b63063ae01b07bf88e (plain)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * 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

/** \file
 * \ingroup bli
 */

#ifdef WITH_GMP

#  include <iostream>

#  include "BLI_math.h"
#  include "BLI_math_mpq.hh"
#  include "BLI_span.hh"

namespace blender {

struct mpq3 {
  mpq_class x, y, z;

  mpq3() = default;

  mpq3(const mpq_class *ptr) : x{ptr[0]}, y{ptr[1]}, z{ptr[2]}
  {
  }

  mpq3(const mpq_class (*ptr)[3]) : mpq3((const mpq_class *)ptr)
  {
  }

  explicit mpq3(mpq_class value) : x(value), y(value), z(value)
  {
  }

  explicit mpq3(int value) : x(value), y(value), z(value)
  {
  }

  mpq3(mpq_class x, mpq_class y, mpq_class z) : x{x}, y{y}, z{z}
  {
  }

  operator const mpq_class *() const
  {
    return &x;
  }

  operator mpq_class *()
  {
    return &x;
  }

  /* Cannot do this exactly in rational arithmetic!
   * Approximate by going in and out of doubles.
   */
  mpq_class normalize_and_get_length()
  {
    double dv[3] = {x.get_d(), y.get_d(), z.get_d()};
    double len = normalize_v3_db(dv);
    this->x = mpq_class(dv[0]);
    this->y = mpq_class(dv[1]);
    this->z = mpq_class(dv[2]);
    return len;
  }

  mpq3 normalized() const
  {
    double dv[3] = {x.get_d(), y.get_d(), z.get_d()};
    double dr[3];
    normalize_v3_v3_db(dr, dv);
    return mpq3(mpq_class(dr[0]), mpq_class(dr[1]), mpq_class(dr[2]));
  }

  /* Cannot do this exactly in rational arithmetic!
   * Approximate by going in and out of double.
   */
  mpq_class length() const
  {
    mpq_class lsquared = this->length_squared();
    double dsquared = lsquared.get_d();
    double d = sqrt(dsquared);
    return mpq_class(d);
  }

  mpq_class length_squared() const
  {
    return x * x + y * y + z * z;
  }

  void reflect(const mpq3 &normal)
  {
    *this = this->reflected(normal);
  }

  mpq3 reflected(const mpq3 &normal) const
  {
    mpq3 result;
    const mpq_class dot2 = 2 * dot(*this, normal);
    result.x = this->x - (dot2 * normal.x);
    result.y = this->y - (dot2 * normal.y);
    result.z = this->z - (dot2 * normal.z);
    return result;
  }

  static mpq3 safe_divide(const mpq3 &a, const mpq3 &b)
  {
    mpq3 result;
    result.x = (b.x == 0) ? mpq_class(0) : a.x / b.x;
    result.y = (b.y == 0) ? mpq_class(0) : a.y / b.y;
    result.z = (b.z == 0) ? mpq_class(0) : a.z / b.z;
    return result;
  }

  void invert()
  {
    x = -x;
    y = -y;
    z = -z;
  }

  friend mpq3 operator+(const mpq3 &a, const mpq3 &b)
  {
    return mpq3(a.x + b.x, a.y + b.y, a.z + b.z);
  }

  void operator+=(const mpq3 &b)
  {
    this->x += b.x;
    this->y += b.y;
    this->z += b.z;
  }

  friend mpq3 operator-(const mpq3 &a, const mpq3 &b)
  {
    return mpq3(a.x - b.x, a.y - b.y, a.z - b.z);
  }

  friend mpq3 operator-(const mpq3 &a)
  {
    return mpq3(-a.x, -a.y, -a.z);
  }

  void operator-=(const mpq3 &b)
  {
    this->x -= b.x;
    this->y -= b.y;
    this->z -= b.z;
  }

  void operator*=(mpq_class scalar)
  {
    this->x *= scalar;
    this->y *= scalar;
    this->z *= scalar;
  }

  void operator*=(const mpq3 &other)
  {
    this->x *= other.x;
    this->y *= other.y;
    this->z *= other.z;
  }

  friend mpq3 operator*(const mpq3 &a, const mpq3 &b)
  {
    return {a.x * b.x, a.y * b.y, a.z * b.z};
  }

  friend mpq3 operator*(const mpq3 &a, const mpq_class &b)
  {
    return mpq3(a.x * b, a.y * b, a.z * b);
  }

  friend mpq3 operator*(const mpq_class &a, const mpq3 &b)
  {
    return mpq3(a * b.x, a * b.y, a * b.z);
  }

  friend mpq3 operator/(const mpq3 &a, const mpq_class &b)
  {
    BLI_assert(b != 0);
    return mpq3(a.x / b, a.y / b, a.z / b);
  }

  friend bool operator==(const mpq3 &a, const mpq3 &b)
  {
    return a.x == b.x && a.y == b.y && a.z == b.z;
  }

  friend bool operator!=(const mpq3 &a, const mpq3 &b)
  {
    return a.x != b.x || a.y != b.y || a.z != b.z;
  }

  friend std::ostream &operator<<(std::ostream &stream, const mpq3 &v)
  {
    stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
    return stream;
  }

  static mpq_class dot(const mpq3 &a, const mpq3 &b)
  {
    return a.x * b.x + a.y * b.y + a.z * b.z;
  }

  static mpq_class dot_with_buffer(const mpq3 &a, const mpq3 &b, mpq3 &buffer)
  {
    buffer = a;
    buffer *= b;
    buffer.x += buffer.y;
    buffer.x += buffer.z;
    return buffer.x;
  }

  static mpq3 cross(const mpq3 &a, const mpq3 &b)
  {
    return mpq3(a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]);
  }

  static mpq3 cross_high_precision(const mpq3 &a, const mpq3 &b)
  {
    return cross(a, b);
  }

  static mpq3 project(const mpq3 &a, const mpq3 &b)
  {
    const mpq_class mul = mpq3::dot(a, b) / mpq3::dot(b, b);
    return mpq3(mul * b[0], mul * b[1], mul * b[2]);
  }

  static mpq_class distance(const mpq3 &a, const mpq3 &b)
  {
    mpq3 diff(a.x - b.x, a.y - b.y, a.z - b.z);
    return diff.length();
  }

  static mpq_class distance_squared(const mpq3 &a, const mpq3 &b)
  {
    mpq3 diff(a.x - b.x, a.y - b.y, a.z - b.z);
    return mpq3::dot(diff, diff);
  }

  static mpq_class distance_squared_with_buffer(const mpq3 &a, const mpq3 &b, mpq3 &buffer)
  {
    buffer = a;
    buffer -= b;
    return mpq3::dot(buffer, buffer);
  }

  static mpq3 interpolate(const mpq3 &a, const mpq3 &b, mpq_class t)
  {
    mpq_class s = 1 - t;
    return mpq3(a.x * s + b.x * t, a.y * s + b.y * t, a.z * s + b.z * t);
  }

  static mpq3 abs(const mpq3 &a)
  {
    mpq_class abs_x = (a.x >= 0) ? a.x : -a.x;
    mpq_class abs_y = (a.y >= 0) ? a.y : -a.y;
    mpq_class abs_z = (a.z >= 0) ? a.z : -a.z;
    return mpq3(abs_x, abs_y, abs_z);
  }

  static int dominant_axis(const mpq3 &a)
  {
    mpq_class x = (a.x >= 0) ? a.x : -a.x;
    mpq_class y = (a.y >= 0) ? a.y : -a.y;
    mpq_class z = (a.z >= 0) ? a.z : -a.z;
    return ((x > y) ? ((x > z) ? 0 : 2) : ((y > z) ? 1 : 2));
  }

  static mpq3 cross_poly(Span<mpq3> poly);

  /** There is a sensible use for hashing on exact arithmetic types. */
  uint64_t hash() const;
};

uint64_t hash_mpq_class(const mpq_class &value);

}  // namespace blender

#endif /* WITH_GMP */