Blender  V2.93
BLI_mpq3.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
23 #ifdef WITH_GMP
24 
25 # include <iostream>
26 
27 # include "BLI_math.h"
28 # include "BLI_math_mpq.hh"
29 # include "BLI_span.hh"
30 
31 namespace blender {
32 
33 struct mpq3 {
34  mpq_class x, y, z;
35 
36  mpq3() = default;
37 
38  mpq3(const mpq_class *ptr) : x{ptr[0]}, y{ptr[1]}, z{ptr[2]}
39  {
40  }
41 
42  mpq3(const mpq_class (*ptr)[3]) : mpq3((const mpq_class *)ptr)
43  {
44  }
45 
46  explicit mpq3(mpq_class value) : x(value), y(value), z(value)
47  {
48  }
49 
50  explicit mpq3(int value) : x(value), y(value), z(value)
51  {
52  }
53 
54  mpq3(mpq_class x, mpq_class y, mpq_class z) : x{x}, y{y}, z{z}
55  {
56  }
57 
58  operator const mpq_class *() const
59  {
60  return &x;
61  }
62 
63  operator mpq_class *()
64  {
65  return &x;
66  }
67 
68  /* Cannot do this exactly in rational arithmetic!
69  * Approximate by going in and out of doubles.
70  */
71  mpq_class normalize_and_get_length()
72  {
73  double dv[3] = {x.get_d(), y.get_d(), z.get_d()};
74  double len = normalize_v3_db(dv);
75  this->x = mpq_class(dv[0]);
76  this->y = mpq_class(dv[1]);
77  this->z = mpq_class(dv[2]);
78  return len;
79  }
80 
81  mpq3 normalized() const
82  {
83  double dv[3] = {x.get_d(), y.get_d(), z.get_d()};
84  double dr[3];
85  normalize_v3_v3_db(dr, dv);
86  return mpq3(mpq_class(dr[0]), mpq_class(dr[1]), mpq_class(dr[2]));
87  }
88 
89  /* Cannot do this exactly in rational arithmetic!
90  * Approximate by going in and out of double.
91  */
92  mpq_class length() const
93  {
94  mpq_class lsquared = this->length_squared();
95  double dsquared = lsquared.get_d();
96  double d = sqrt(dsquared);
97  return mpq_class(d);
98  }
99 
100  mpq_class length_squared() const
101  {
102  return x * x + y * y + z * z;
103  }
104 
105  void reflect(const mpq3 &normal)
106  {
107  *this = this->reflected(normal);
108  }
109 
110  mpq3 reflected(const mpq3 &normal) const
111  {
112  mpq3 result;
113  const mpq_class dot2 = 2 * dot(*this, normal);
114  result.x = this->x - (dot2 * normal.x);
115  result.y = this->y - (dot2 * normal.y);
116  result.z = this->z - (dot2 * normal.z);
117  return result;
118  }
119 
120  static mpq3 safe_divide(const mpq3 &a, const mpq3 &b)
121  {
122  mpq3 result;
123  result.x = (b.x == 0) ? mpq_class(0) : a.x / b.x;
124  result.y = (b.y == 0) ? mpq_class(0) : a.y / b.y;
125  result.z = (b.z == 0) ? mpq_class(0) : a.z / b.z;
126  return result;
127  }
128 
129  void invert()
130  {
131  x = -x;
132  y = -y;
133  z = -z;
134  }
135 
136  friend mpq3 operator+(const mpq3 &a, const mpq3 &b)
137  {
138  return mpq3(a.x + b.x, a.y + b.y, a.z + b.z);
139  }
140 
141  void operator+=(const mpq3 &b)
142  {
143  this->x += b.x;
144  this->y += b.y;
145  this->z += b.z;
146  }
147 
148  friend mpq3 operator-(const mpq3 &a, const mpq3 &b)
149  {
150  return mpq3(a.x - b.x, a.y - b.y, a.z - b.z);
151  }
152 
153  friend mpq3 operator-(const mpq3 &a)
154  {
155  return mpq3(-a.x, -a.y, -a.z);
156  }
157 
158  void operator-=(const mpq3 &b)
159  {
160  this->x -= b.x;
161  this->y -= b.y;
162  this->z -= b.z;
163  }
164 
165  void operator*=(mpq_class scalar)
166  {
167  this->x *= scalar;
168  this->y *= scalar;
169  this->z *= scalar;
170  }
171 
172  void operator*=(const mpq3 &other)
173  {
174  this->x *= other.x;
175  this->y *= other.y;
176  this->z *= other.z;
177  }
178 
179  friend mpq3 operator*(const mpq3 &a, const mpq3 &b)
180  {
181  return {a.x * b.x, a.y * b.y, a.z * b.z};
182  }
183 
184  friend mpq3 operator*(const mpq3 &a, const mpq_class &b)
185  {
186  return mpq3(a.x * b, a.y * b, a.z * b);
187  }
188 
189  friend mpq3 operator*(const mpq_class &a, const mpq3 &b)
190  {
191  return mpq3(a * b.x, a * b.y, a * b.z);
192  }
193 
194  friend mpq3 operator/(const mpq3 &a, const mpq_class &b)
195  {
196  BLI_assert(b != 0);
197  return mpq3(a.x / b, a.y / b, a.z / b);
198  }
199 
200  friend bool operator==(const mpq3 &a, const mpq3 &b)
201  {
202  return a.x == b.x && a.y == b.y && a.z == b.z;
203  }
204 
205  friend bool operator!=(const mpq3 &a, const mpq3 &b)
206  {
207  return a.x != b.x || a.y != b.y || a.z != b.z;
208  }
209 
210  friend std::ostream &operator<<(std::ostream &stream, const mpq3 &v)
211  {
212  stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
213  return stream;
214  }
215 
216  static mpq_class dot(const mpq3 &a, const mpq3 &b)
217  {
218  return a.x * b.x + a.y * b.y + a.z * b.z;
219  }
220 
221  static mpq3 cross(const mpq3 &a, const mpq3 &b)
222  {
223  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]);
224  }
225 
226  static mpq3 cross_high_precision(const mpq3 &a, const mpq3 &b)
227  {
228  return cross(a, b);
229  }
230 
231  static mpq3 project(const mpq3 &a, const mpq3 &b)
232  {
233  const mpq_class mul = mpq3::dot(a, b) / mpq3::dot(b, b);
234  return mpq3(mul * b[0], mul * b[1], mul * b[2]);
235  }
236 
237  static mpq_class distance(const mpq3 &a, const mpq3 &b)
238  {
239  mpq3 diff(a.x - b.x, a.y - b.y, a.z - b.z);
240  return diff.length();
241  }
242 
243  static mpq_class distance_squared(const mpq3 &a, const mpq3 &b)
244  {
245  mpq3 diff(a.x - b.x, a.y - b.y, a.z - b.z);
246  return mpq3::dot(diff, diff);
247  }
248 
249  static mpq3 interpolate(const mpq3 &a, const mpq3 &b, mpq_class t)
250  {
251  mpq_class s = 1 - t;
252  return mpq3(a.x * s + b.x * t, a.y * s + b.y * t, a.z * s + b.z * t);
253  }
254 
255  static mpq3 abs(const mpq3 &a)
256  {
257  mpq_class abs_x = (a.x >= 0) ? a.x : -a.x;
258  mpq_class abs_y = (a.y >= 0) ? a.y : -a.y;
259  mpq_class abs_z = (a.z >= 0) ? a.z : -a.z;
260  return mpq3(abs_x, abs_y, abs_z);
261  }
262 
263  static int dominant_axis(const mpq3 &a)
264  {
265  mpq_class x = (a.x >= 0) ? a.x : -a.x;
266  mpq_class y = (a.y >= 0) ? a.y : -a.y;
267  mpq_class z = (a.z >= 0) ? a.z : -a.z;
268  return ((x > y) ? ((x > z) ? 0 : 2) : ((y > z) ? 1 : 2));
269  }
270 
271  static mpq3 cross_poly(Span<mpq3> poly);
272 
274  uint64_t hash() const;
275 };
276 
277 uint64_t hash_mpq_class(const mpq_class &value);
278 
279 } // namespace blender
280 
281 #endif /* WITH_GMP */
#define BLI_assert(a)
Definition: BLI_assert.h:58
sqrt(x)+1/max(0
MINLINE float safe_divide(float a, float b)
MINLINE double normalize_v3_db(double n[3])
MINLINE double normalize_v3_v3_db(double r[3], const double a[3])
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble z
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
return(oflags[bm->toolflag_index].f &oflag) !=0
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:895
static void mul(btAlignedObjectArray< T > &items, const Q &value)
SIMD_FORCE_INLINE btVector3 normalized() const
Return a normalized version of this vector.
IconTextureDrawCall normal
Matrix< T, M, N > operator-(const Matrix< T, M, N > &m1, const Matrix< T, M, N > &m2)
Definition: VecMat.h:922
Matrix< T, M, N > operator/(const Matrix< T, M, N > &m1, const typename Matrix< T, M, N >::value_type lambda)
Definition: VecMat.h:948
Vec< T, N > operator*(const typename Vec< T, N >::value_type r, const Vec< T, N > &v)
Definition: VecMat.h:858
static unsigned a[3]
Definition: RandGen.cpp:92
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt=1)
constexpr bool operator!=(StringRef a, StringRef b)
constexpr bool operator==(StringRef a, StringRef b)
std::ostream & operator<<(std::ostream &stream, StringRef ref)
std::string operator+(StringRef a, StringRef b)
vector project(vector v, vector v_proj)
Definition: node_math.h:66
#define hash
Definition: noise.c:169
unsigned __int64 uint64_t
Definition: stdint.h:93
CCL_NAMESPACE_BEGIN ccl_device float invert(float color, float factor)
Definition: svm_invert.h:19
__forceinline avxf cross(const avxf &a, const avxf &b)
Definition: util_avxf.h:119
__forceinline avxi & operator-=(avxi &a, const avxi &b)
Definition: util_avxi.h:407
__forceinline const avxi abs(const avxi &a)
Definition: util_avxi.h:186
__forceinline avxi & operator+=(avxi &a, const avxi &b)
Assignment Operators.
Definition: util_avxi.h:398
__forceinline avxi & operator*=(avxi &a, const avxi &b)
Definition: util_avxi.h:416
ccl_device_inline float distance(const float2 &a, const float2 &b)
ccl_device_inline float dot(const float2 &a, const float2 &b)
ccl_device_inline float3 reflect(const float3 incident, const float3 normal)
uint len
PointerRNA * ptr
Definition: wm_files.c:3157