Blender V4.3
mikk_float3.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
9#pragma once
10
11#include <cmath>
12
13namespace mikk {
14
15struct float3 {
16 float x, y, z;
17
18 float3() = default;
19
20 float3(const float *ptr) : x{ptr[0]}, y{ptr[1]}, z{ptr[2]} {}
21
22 float3(const float (*ptr)[3]) : float3((const float *)ptr) {}
23
24 explicit float3(float value) : x(value), y(value), z(value) {}
25
26 explicit float3(int value) : x((float)value), y((float)value), z((float)value) {}
27
28 float3(float x_, float y_, float z_) : x{x_}, y{y_}, z{z_} {}
29
30 static float3 zero()
31 {
32 return {0.0f, 0.0f, 0.0f};
33 }
34
35 friend float3 operator*(const float3 &a, float b)
36 {
37 return {a.x * b, a.y * b, a.z * b};
38 }
39
40 friend float3 operator*(float b, const float3 &a)
41 {
42 return {a.x * b, a.y * b, a.z * b};
43 }
44
45 friend float3 operator-(const float3 &a, const float3 &b)
46 {
47 return {a.x - b.x, a.y - b.y, a.z - b.z};
48 }
49
50 friend float3 operator-(const float3 &a)
51 {
52 return {-a.x, -a.y, -a.z};
53 }
54
55 friend bool operator==(const float3 &a, const float3 &b)
56 {
57 return a.x == b.x && a.y == b.y && a.z == b.z;
58 }
59
60 float length_squared() const
61 {
62 return x * x + y * y + z * z;
63 }
64
65 float length() const
66 {
67 return sqrtf(length_squared());
68 }
69
70 static float distance(const float3 &a, const float3 &b)
71 {
72 return (a - b).length();
73 }
74
75 friend float3 operator+(const float3 &a, const float3 &b)
76 {
77 return {a.x + b.x, a.y + b.y, a.z + b.z};
78 }
79
80 void operator+=(const float3 &b)
81 {
82 this->x += b.x;
83 this->y += b.y;
84 this->z += b.z;
85 }
86
87 friend float3 operator*(const float3 &a, const float3 &b)
88 {
89 return {a.x * b.x, a.y * b.y, a.z * b.z};
90 }
91
93 {
94 const float len = length();
95 return (len != 0.0f) ? *this * (1.0f / len) : *this;
96 }
97
98 float reduce_add() const
99 {
100 return x + y + z;
101 }
102};
103
104inline float dot(const float3 &a, const float3 &b)
105{
106 return a.x * b.x + a.y * b.y + a.z * b.z;
107}
108
109inline float distance(const float3 &a, const float3 &b)
110{
111 return float3::distance(a, b);
112}
113
114/* Projects v onto the surface with normal n. */
115inline float3 project(const float3 &n, const float3 &v)
116{
117 return (v - n * dot(n, v)).normalize();
118}
119
120} // namespace mikk
ATTR_WARN_UNUSED_RESULT const BMVert * v
local_group_size(16, 16) .push_constant(Type b
#define sqrtf(x)
int len
draw_view in_light_buf[] float
const Mat y_
const Mat x_
float dot(const float3 &a, const float3 &b)
float3 project(const float3 &n, const float3 &v)
float distance(const float3 &a, const float3 &b)
friend float3 operator*(const float3 &a, float b)
float3(const float *ptr)
friend float3 operator-(const float3 &a, const float3 &b)
float reduce_add() const
float length() const
friend bool operator==(const float3 &a, const float3 &b)
float3(float value)
float length_squared() const
friend float3 operator+(const float3 &a, const float3 &b)
float3(int value)
float3 normalize() const
float3(const float(*ptr)[3])
static float distance(const float3 &a, const float3 &b)
static float3 zero()
friend float3 operator*(const float3 &a, const float3 &b)
float3()=default
void operator+=(const float3 &b)
float3(float x_, float y_, float z_)
friend float3 operator-(const float3 &a)
friend float3 operator*(float b, const float3 &a)
PointerRNA * ptr
Definition wm_files.cc:4126