[Bf-blender-cvs] [a37164e11b7] temp-experimental-cpp-math-refactor: update float3
Jacques Lucke
noreply at git.blender.org
Mon Jan 11 18:40:52 CET 2021
Commit: a37164e11b7cf4fa7fbe37efa7ba0758dd7c4fc2
Author: Jacques Lucke
Date: Mon Jan 11 12:55:05 2021 +0100
Branches: temp-experimental-cpp-math-refactor
https://developer.blender.org/rBa37164e11b7cf4fa7fbe37efa7ba0758dd7c4fc2
update float3
===================================================================
M source/blender/blenlib/BLI_float3.hh
M source/blender/modifiers/intern/MOD_mesh_to_volume.cc
M source/blender/nodes/geometry/nodes/node_geo_transform.cc
M source/blender/nodes/intern/node_tree_multi_function.cc
M source/blender/nodes/shader/nodes/node_shader_vector_math.cc
===================================================================
diff --git a/source/blender/blenlib/BLI_float3.hh b/source/blender/blenlib/BLI_float3.hh
index 17b3f56453c..dcbc462cab4 100644
--- a/source/blender/blenlib/BLI_float3.hh
+++ b/source/blender/blenlib/BLI_float3.hh
@@ -20,7 +20,7 @@
#include "BLI_math_vector.h"
-namespace blender {
+namespace blender::math {
struct float3 {
float x, y, z;
@@ -57,151 +57,6 @@ struct float3 {
return &x;
}
- friend float3 operator+(const float3 &a, const float3 &b)
- {
- return {a.x + b.x, a.y + b.y, a.z + b.z};
- }
-
- float3 &operator+=(const float3 &b)
- {
- this->x += b.x;
- this->y += b.y;
- this->z += b.z;
- return *this;
- }
-
- friend float3 operator-(const float3 &a, const float3 &b)
- {
- return {a.x - b.x, a.y - b.y, a.z - b.z};
- }
-
- friend float3 operator-(const float3 &a)
- {
- return {-a.x, -a.y, -a.z};
- }
-
- float3 &operator-=(const float3 &b)
- {
- this->x -= b.x;
- this->y -= b.y;
- this->z -= b.z;
- return *this;
- }
-
- float3 &operator*=(float scalar)
- {
- this->x *= scalar;
- this->y *= scalar;
- this->z *= scalar;
- return *this;
- }
-
- float3 &operator*=(const float3 &other)
- {
- this->x *= other.x;
- this->y *= other.y;
- this->z *= other.z;
- return *this;
- }
-
- friend float3 operator*(const float3 &a, const float3 &b)
- {
- return {a.x * b.x, a.y * b.y, a.z * b.z};
- }
-
- friend float3 operator*(const float3 &a, float b)
- {
- return {a.x * b, a.y * b, a.z * b};
- }
-
- friend float3 operator*(float a, const float3 &b)
- {
- return b * a;
- }
-
- friend float3 operator/(const float3 &a, float b)
- {
- BLI_assert(b != 0.0f);
- return {a.x / b, a.y / b, a.z / b};
- }
-
- friend std::ostream &operator<<(std::ostream &stream, const float3 &v)
- {
- stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
- return stream;
- }
-
- friend bool operator==(const float3 &a, const float3 &b)
- {
- return a.x == b.x && a.y == b.y && a.z == b.z;
- }
-
- friend bool operator!=(const float3 &a, const float3 &b)
- {
- return !(a == b);
- }
-
- float normalize_and_get_length()
- {
- return normalize_v3(*this);
- }
-
- /**
- * Normalizes the vector in place.
- */
- void normalize()
- {
- normalize_v3(*this);
- }
-
- /**
- * Returns a normalized vector. The original vector is not changed.
- */
- float3 normalized() const
- {
- float3 result;
- normalize_v3_v3(result, *this);
- return result;
- }
-
- float length() const
- {
- return len_v3(*this);
- }
-
- float length_squared() const
- {
- return len_squared_v3(*this);
- }
-
- void reflect(const float3 &normal)
- {
- *this = this->reflected(normal);
- }
-
- float3 reflected(const float3 &normal) const
- {
- float3 result;
- reflect_v3_v3v3(result, *this, normal);
- return result;
- }
-
- static float3 safe_divide(const float3 &a, const float3 &b)
- {
- float3 result;
- result.x = (b.x == 0.0f) ? 0.0f : a.x / b.x;
- result.y = (b.y == 0.0f) ? 0.0f : a.y / b.y;
- result.z = (b.z == 0.0f) ? 0.0f : a.z / b.z;
- return result;
- }
-
- void invert()
- {
- x = -x;
- y = -y;
- z = -z;
- }
-
uint64_t hash() const
{
uint64_t x1 = *reinterpret_cast<const uint32_t *>(&x);
@@ -209,45 +64,188 @@ struct float3 {
uint64_t x3 = *reinterpret_cast<const uint32_t *>(&z);
return (x1 * 435109) ^ (x2 * 380867) ^ (x3 * 1059217);
}
-
- static float dot(const float3 &a, const float3 &b)
- {
- return a.x * b.x + a.y * b.y + a.z * b.z;
- }
-
- static float3 cross_high_precision(const float3 &a, const float3 &b)
- {
- float3 result;
- cross_v3_v3v3_hi_prec(result, a, b);
- return result;
- }
-
- static float3 project(const float3 &a, const float3 &b)
- {
- float3 result;
- project_v3_v3v3(result, a, b);
- return result;
- }
-
- static float distance(const float3 &a, const float3 &b)
- {
- return (a - b).length();
- }
-
- static float distance_squared(const float3 &a, const float3 &b)
- {
- return float3::dot(a, b);
- }
-
- static float3 interpolate(const float3 &a, const float3 &b, float t)
- {
- return a * (1 - t) + b * t;
- }
-
- static float3 abs(const float3 &a)
- {
- return float3(fabsf(a.x), fabsf(a.y), fabsf(a.z));
- }
};
-} // namespace blender
+inline float3 operator+(const float3 &a, const float3 &b)
+{
+ return {a.x + b.x, a.y + b.y, a.z + b.z};
+}
+
+inline float3 &operator+=(float3 &a, const float3 &b)
+{
+ a.x += b.x;
+ a.y += b.y;
+ a.z += b.z;
+ return a;
+}
+
+inline float3 operator-(const float3 &a, const float3 &b)
+{
+ return {a.x - b.x, a.y - b.y, a.z - b.z};
+}
+
+inline float3 &operator-=(float3 &a, const float3 &b)
+{
+ a.x -= b.x;
+ a.y -= b.y;
+ a.z -= b.z;
+ return a;
+}
+
+inline float3 &operator*=(float3 &a, float scalar)
+{
+ a.x *= scalar;
+ a.y *= scalar;
+ a.z *= scalar;
+ return a;
+}
+
+inline float3 &operator*=(float3 &a, const float3 &other)
+{
+ a.x *= other.x;
+ a.y *= other.y;
+ a.z *= other.z;
+ return a;
+}
+
+inline float3 operator*(const float3 &a, const float3 &b)
+{
+ return {a.x * b.x, a.y * b.y, a.z * b.z};
+}
+
+inline float3 operator/(const float3 &a, float b)
+{
+ BLI_assert(b != 0.0f);
+ return {a.x / b, a.y / b, a.z / b};
+}
+
+inline std::ostream &operator<<(std::ostream &stream, const float3 &v)
+{
+ stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
+ return stream;
+}
+
+inline float3 operator-(const float3 &a)
+{
+ return {-a.x, -a.y, -a.z};
+}
+
+inline bool operator==(const float3 &a, const float3 &b)
+{
+ return a.x == b.x && a.y == b.y && a.z == b.z;
+}
+
+inline bool operator!=(const float3 &a, const float3 &b)
+{
+ return !(a == b);
+}
+
+inline float3 operator*(const float3 &a, float b)
+{
+ return {a.x * b, a.y * b, a.z * b};
+}
+
+inline float3 operator*(float a, const float3 &b)
+{
+ return b * a;
+}
+
+inline float normalize_and_get_length(float3 &a)
+{
+ return normalize_v3(a);
+}
+
+inline void normalize(float3 &a)
+{
+ normalize_v3(a);
+}
+
+inline float3 normalized(const float3 &a)
+{
+ float3 result;
+ normalize_v3_v3(result, a);
+ return result;
+}
+
+inline float length(const float3 &a)
+{
+ return len_v3(a);
+}
+
+inline float length_squared(const float3 &a)
+{
+ return len_squared_v3(a);
+}
+
+inline float3 reflected(const float3 &a, const float3 &normal)
+{
+ float3 result;
+ reflect_v3_v3v3(result, a, normal);
+ return result;
+}
+
+inline void reflect(float3 &a, const float3 &normal)
+{
+ a = reflected(a, normal);
+}
+
+inline float3 safe_divide(const float3 &a, const float3 &b)
+{
+ float3 result;
+ result.x = (b.x == 0.0f) ? 0.0f : a.x / b.x;
+ result.y = (b.y == 0.0f) ? 0.0f : a.y / b.y;
+ result.z = (b.z == 0.0f) ? 0.0f : a.z / b.z;
+ return result;
+}
+
+inline void negate(float3 &a)
+{
+ a.x = -a.x;
+ a.y = -a.y;
+ a.z = -a.z;
+}
+
+inline float dot(const float3 &a, const float3 &b)
+{
+ return a.x * b.x + a.y * b.y + a.z * b.z;
+}
+
+inline float3 cross_high_precision(const float3 &a, const float3 &b)
+{
+ float3 result;
+ cross_v3_v3v3_hi_prec(result, a, b);
+ return result;
+}
+
+inline float3 project(const float3 &a, const float3 &b)
+{
+ float3 result;
+ project_v3_v3v3(result, a, b);
+ return result;
+}
+
+inline float distance(const float3 &a, const float3 &b)
+{
+ return length(a - b);
+}
+
+inline float distance_squared(const float3 &a, const float3 &b)
+{
+ return dot(a, b);
+}
+
+inline float3 lerp(const float3 &a, const float3 &b, float t)
+{
+ return a * (1 - t) + b * t;
+}
+
+inline float3 abs(const float3 &a)
+{
+ return float3(fabsf(a.x), fabsf(a.y), fabsf(a.z));
+}
+
+} // namespace blender::math
+
+namespace blender {
+using math::float3;
+}
diff --git a/source/blender/modifiers/intern/MOD_mesh_to_volume.cc b/source/blender/modifiers/intern/MOD_mesh_to_volume.cc
index cc007651733..3626928a8bb 100644
--- a/source/blender/modifiers/intern/MOD_mesh_to_volume.cc
+++ b/source/blender/modifiers/intern/MOD_mesh_to_volume.cc
@@ -196,8 +196,7 @@ static float compute_voxel_size(const ModifierEvalContext *ctx,
/* Compute the voxel size based on the desired number of voxels and the approximated bounding box
* of the volume. */
const BoundBox *bb = BKE_object_boundbox_get(mvmd->object);
- const float diagonal = float3::distance(transform * float3(bb->vec[6]),
- transform * float3(bb->vec[0]));
+ const float diagonal = distance(transform * float3(bb->vec[6]), transform * float3(bb->vec[0]));
const float approximate_volume_side_length = diagonal + mvmd->exterior_band_width * 2.0f;
const float voxel_size = approximate_volume_side_length / mvmd->voxel_amount / volume_simplify;
return voxel_size;
diff --git a/source/blender/nodes/geometry/nodes/node_geo_transform.cc b/source/blender/nodes/geometry/nodes/node_geo_transform.cc
index abfa603b584..37eb29979cf 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_transform.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_transform.cc
@@ -39,7 +39,7 @@ namespace blender::nodes {
static bool use_translate(const float3 rotation, const float3 scale)
{
- if (compare_ff(rotation.length_squared(), 0.0f, 1e-9f) != 1) {
+ if (compare_ff(length_squared(rotation), 0.0f, 1e-9f) != 1) {
return false;
}
if (compare_ff(scale.x, 1.0f, 1e-9f) != 1 || compare_ff(scale.y, 1.0f, 1e-9f) != 1 ||
diff --git a/source/blender/nodes/intern/node_tree_multi_function.cc b/source/blender/nodes/intern/node_tree_multi_function.cc
index 2e4196af156..7fac7553c04 100644
--- a/source/blender/nodes/intern/node_tree_multi_function.cc
+++ b/source/blender/nodes/intern/node_tree_multi_function.cc
@@ -197,7 +197,7 @@ static DataTypeConversions create_implicit_conversions()
add_implicit_conversion<float, bool>(conversions);
add_implicit_conversion<bool, float>(conversions);
add_implicit_conversion<float3, float>(
- conversions, "Vector Length", [](float3 a) { return a.length(); });
+ conversions, "Vector Length", [](float3 a) { return length(a); });
add_implicit_conversion<int32_t, float3>(
conversions, "int32 to float3", [](int32_t a) {
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list