Blender  V2.93
BLI_float3.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 
19 #include <iostream>
20 
21 #include "BLI_math_vector.h"
22 
23 namespace blender {
24 
25 struct float3 {
26  float x, y, z;
27 
28  float3() = default;
29 
30  float3(const float *ptr) : x{ptr[0]}, y{ptr[1]}, z{ptr[2]}
31  {
32  }
33 
34  float3(const float (*ptr)[3]) : float3(static_cast<const float *>(ptr[0]))
35  {
36  }
37 
38  explicit float3(float value) : x(value), y(value), z(value)
39  {
40  }
41 
42  explicit float3(int value) : x(value), y(value), z(value)
43  {
44  }
45 
46  float3(float x, float y, float z) : x{x}, y{y}, z{z}
47  {
48  }
49 
50  operator const float *() const
51  {
52  return &x;
53  }
54 
55  operator float *()
56  {
57  return &x;
58  }
59 
60  friend float3 operator+(const float3 &a, const float3 &b)
61  {
62  return {a.x + b.x, a.y + b.y, a.z + b.z};
63  }
64 
66  {
67  this->x += b.x;
68  this->y += b.y;
69  this->z += b.z;
70  return *this;
71  }
72 
73  friend float3 operator-(const float3 &a, const float3 &b)
74  {
75  return {a.x - b.x, a.y - b.y, a.z - b.z};
76  }
77 
78  friend float3 operator-(const float3 &a)
79  {
80  return {-a.x, -a.y, -a.z};
81  }
82 
84  {
85  this->x -= b.x;
86  this->y -= b.y;
87  this->z -= b.z;
88  return *this;
89  }
90 
91  float3 &operator*=(float scalar)
92  {
93  this->x *= scalar;
94  this->y *= scalar;
95  this->z *= scalar;
96  return *this;
97  }
98 
99  float3 &operator*=(const float3 &other)
100  {
101  this->x *= other.x;
102  this->y *= other.y;
103  this->z *= other.z;
104  return *this;
105  }
106 
107  friend float3 operator*(const float3 &a, const float3 &b)
108  {
109  return {a.x * b.x, a.y * b.y, a.z * b.z};
110  }
111 
112  friend float3 operator*(const float3 &a, float b)
113  {
114  return {a.x * b, a.y * b, a.z * b};
115  }
116 
117  friend float3 operator*(float a, const float3 &b)
118  {
119  return b * a;
120  }
121 
122  friend float3 operator/(const float3 &a, float b)
123  {
124  BLI_assert(b != 0.0f);
125  return {a.x / b, a.y / b, a.z / b};
126  }
127 
128  friend std::ostream &operator<<(std::ostream &stream, const float3 &v)
129  {
130  stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
131  return stream;
132  }
133 
134  friend bool operator==(const float3 &a, const float3 &b)
135  {
136  return a.x == b.x && a.y == b.y && a.z == b.z;
137  }
138 
139  friend bool operator!=(const float3 &a, const float3 &b)
140  {
141  return !(a == b);
142  }
143 
145  {
146  return normalize_v3(*this);
147  }
148 
152  void normalize()
153  {
154  normalize_v3(*this);
155  }
156 
161  {
162  float3 result;
163  normalize_v3_v3(result, *this);
164  return result;
165  }
166 
167  float length() const
168  {
169  return len_v3(*this);
170  }
171 
172  float length_squared() const
173  {
174  return len_squared_v3(*this);
175  }
176 
177  bool is_zero() const
178  {
179  return this->x == 0.0f && this->y == 0.0f && this->z == 0.0f;
180  }
181 
182  void reflect(const float3 &normal)
183  {
184  *this = this->reflected(normal);
185  }
186 
188  {
189  float3 result;
190  reflect_v3_v3v3(result, *this, normal);
191  return result;
192  }
193 
194  static float3 refract(const float3 &incident, const float3 &normal, const float eta)
195  {
196  float3 result;
197  float k = 1.0f - eta * eta * (1.0f - dot(normal, incident) * dot(normal, incident));
198  if (k < 0.0f) {
199  result = float3(0.0f);
200  }
201  else {
202  result = eta * incident - (eta * dot(normal, incident) + sqrt(k)) * normal;
203  }
204  return result;
205  }
206 
207  static float3 faceforward(const float3 &vector, const float3 &incident, const float3 &reference)
208  {
209  return dot(reference, incident) < 0.0f ? vector : -vector;
210  }
211 
212  static float3 safe_divide(const float3 &a, const float3 &b)
213  {
214  float3 result;
215  result.x = (b.x == 0.0f) ? 0.0f : a.x / b.x;
216  result.y = (b.y == 0.0f) ? 0.0f : a.y / b.y;
217  result.z = (b.z == 0.0f) ? 0.0f : a.z / b.z;
218  return result;
219  }
220 
221  void invert()
222  {
223  x = -x;
224  y = -y;
225  z = -z;
226  }
227 
228  uint64_t hash() const
229  {
230  uint64_t x1 = *reinterpret_cast<const uint32_t *>(&x);
231  uint64_t x2 = *reinterpret_cast<const uint32_t *>(&y);
232  uint64_t x3 = *reinterpret_cast<const uint32_t *>(&z);
233  return (x1 * 435109) ^ (x2 * 380867) ^ (x3 * 1059217);
234  }
235 
236  static float dot(const float3 &a, const float3 &b)
237  {
238  return a.x * b.x + a.y * b.y + a.z * b.z;
239  }
240 
241  static float3 cross_high_precision(const float3 &a, const float3 &b)
242  {
243  float3 result;
245  return result;
246  }
247 
248  static float3 project(const float3 &a, const float3 &b)
249  {
250  float3 result;
251  project_v3_v3v3(result, a, b);
252  return result;
253  }
254 
255  static float distance(const float3 &a, const float3 &b)
256  {
257  return (a - b).length();
258  }
259 
260  static float distance_squared(const float3 &a, const float3 &b)
261  {
262  float3 diff = a - b;
263  return float3::dot(diff, diff);
264  }
265 
266  static float3 interpolate(const float3 &a, const float3 &b, float t)
267  {
268  return a * (1 - t) + b * t;
269  }
270 
271  static float3 abs(const float3 &a)
272  {
273  return float3(fabsf(a.x), fabsf(a.y), fabsf(a.z));
274  }
275 };
276 
277 } // namespace blender
typedef float(TangentPoint)[2]
#define BLI_assert(a)
Definition: BLI_assert.h:58
sqrt(x)+1/max(0
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
void reflect_v3_v3v3(float out[3], const float vec[3], const float normal[3])
Definition: math_vector.c:818
MINLINE float normalize_v3(float r[3])
MINLINE void cross_v3_v3v3_hi_prec(float r[3], const float a[3], const float b[3])
void project_v3_v3v3(float out[3], const float p[3], const float v_proj[3])
Definition: math_vector.c:674
MINLINE float normalize_v3_v3(float r[3], const float a[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
_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 x2
_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
ATTR_WARN_UNUSED_RESULT const BMVert * v
IconTextureDrawCall normal
#define fabsf(x)
static unsigned a[3]
Definition: RandGen.cpp:92
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt=1)
std::vector< ElementType, Eigen::aligned_allocator< ElementType > > vector
Definition: vector.h:39
unsigned int uint32_t
Definition: stdint.h:83
unsigned __int64 uint64_t
Definition: stdint.h:93
friend float3 operator*(const float3 &a, float b)
Definition: BLI_float3.hh:112
static float3 cross_high_precision(const float3 &a, const float3 &b)
Definition: BLI_float3.hh:241
static float distance_squared(const float3 &a, const float3 &b)
Definition: BLI_float3.hh:260
float length_squared() const
Definition: BLI_float3.hh:172
friend float3 operator-(const float3 &a, const float3 &b)
Definition: BLI_float3.hh:73
float3 normalized() const
Definition: BLI_float3.hh:160
float3 & operator-=(const float3 &b)
Definition: BLI_float3.hh:83
static float3 project(const float3 &a, const float3 &b)
Definition: BLI_float3.hh:248
float3 & operator*=(const float3 &other)
Definition: BLI_float3.hh:99
float length() const
Definition: BLI_float3.hh:167
float3(int value)
Definition: BLI_float3.hh:42
float3(const float *ptr)
Definition: BLI_float3.hh:30
float3(float x, float y, float z)
Definition: BLI_float3.hh:46
float normalize_and_get_length()
Definition: BLI_float3.hh:144
void reflect(const float3 &normal)
Definition: BLI_float3.hh:182
static float3 abs(const float3 &a)
Definition: BLI_float3.hh:271
friend bool operator==(const float3 &a, const float3 &b)
Definition: BLI_float3.hh:134
friend std::ostream & operator<<(std::ostream &stream, const float3 &v)
Definition: BLI_float3.hh:128
friend float3 operator+(const float3 &a, const float3 &b)
Definition: BLI_float3.hh:60
static float3 interpolate(const float3 &a, const float3 &b, float t)
Definition: BLI_float3.hh:266
float3()=default
static float dot(const float3 &a, const float3 &b)
Definition: BLI_float3.hh:236
friend float3 operator/(const float3 &a, float b)
Definition: BLI_float3.hh:122
float3 & operator+=(const float3 &b)
Definition: BLI_float3.hh:65
static float3 faceforward(const float3 &vector, const float3 &incident, const float3 &reference)
Definition: BLI_float3.hh:207
static float3 safe_divide(const float3 &a, const float3 &b)
Definition: BLI_float3.hh:212
friend float3 operator*(float a, const float3 &b)
Definition: BLI_float3.hh:117
float3(float value)
Definition: BLI_float3.hh:38
float3(const float(*ptr)[3])
Definition: BLI_float3.hh:34
friend bool operator!=(const float3 &a, const float3 &b)
Definition: BLI_float3.hh:139
friend float3 operator*(const float3 &a, const float3 &b)
Definition: BLI_float3.hh:107
uint64_t hash() const
Definition: BLI_float3.hh:228
float3 reflected(const float3 &normal) const
Definition: BLI_float3.hh:187
static float distance(const float3 &a, const float3 &b)
Definition: BLI_float3.hh:255
static float3 refract(const float3 &incident, const float3 &normal, const float eta)
Definition: BLI_float3.hh:194
bool is_zero() const
Definition: BLI_float3.hh:177
float3 & operator*=(float scalar)
Definition: BLI_float3.hh:91
friend float3 operator-(const float3 &a)
Definition: BLI_float3.hh:78
PointerRNA * ptr
Definition: wm_files.c:3157