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

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClment Foucault <fclem>2022-01-12 14:46:52 +0300
committerClément Foucault <foucault.clem@gmail.com>2022-01-12 14:47:43 +0300
commit46e049d0ce2bce2f53ddc41a0dbbea2969d00a5d (patch)
tree2b814f1b737ebe17364de1a5938deb9a7066d2a9 /source/blender/blenlib/BLI_float3.hh
parente5766752d04794c2693dedad75baeb8c7d68f4cf (diff)
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:`float2`) by making heavy usage of templating. All vector functions are now outside of the vector classes (inside the `blender::math` namespace) and are not vector size dependent for the most part. In the ongoing effort to make shaders less GL centric, we are aiming to share more code between GLSL and C++ to avoid code duplication. ####Motivations: - We are aiming to share UBO and SSBO structures between GLSL and C++. This means we will use many of the existing vector types and others we currently don't have (uintX, intX). All these variations were asking for many more code duplication. - Deduplicate existing code which is duplicated for each vector size. - We also want to share small functions. Which means that vector functions should be static and not in the class namespace. - Reduce friction to use these types in new projects due to their incompleteness. - The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a bit of a let down. Most clases are incomplete, out of sync with each others with different codestyles, and some functions that should be static are not (i.e: `float3::reflect()`). ####Upsides: - Still support `.x, .y, .z, .w` for readability. - Compact, readable and easilly extendable. - All of the vector functions are available for all the vectors types and can be restricted to certain types. Also template specialization let us define exception for special class (like mpq). - With optimization ON, the compiler unroll the loops and performance is the same. ####Downsides: - Might impact debugability. Though I would arge that the bugs are rarelly caused by the vector class itself (since the operations are quite trivial) but by the type conversions. - Might impact compile time. I did not saw a significant impact since the usage is not really widespread. - Functions needs to be rewritten to support arbitrary vector length. For instance, one can't call `len_squared_v3v3` in `math::length_squared()` and call it a day. - Type cast does not work with the template version of the `math::` vector functions. Meaning you need to manually cast `float *` and `(float *)[3]` to `float3` for the function calls. i.e: `math::distance_squared(float3(nearest.co), positions[i]);` - Some parts might loose in readability: `float3::dot(v1.normalized(), v2.normalized())` becoming `math::dot(math::normalize(v1), math::normalize(v2))` But I propose, when appropriate, to use `using namespace blender::math;` on function local or file scope to increase readability. `dot(normalize(v1), normalize(v2))` ####Consideration: - Include back `.length()` method. It is quite handy and is more C++ oriented. - I considered the GLM library as a candidate for replacement. It felt like too much for what we need and would be difficult to extend / modify to our needs. - I used Macros to reduce code in operators declaration and potential copy paste bugs. This could reduce debugability and could be reverted. - This touches `delaunay_2d.cc` and the intersection code. I would like to know @howardt opinion on the matter. - The `noexcept` on the copy constructor of `mpq(2|3)` is being removed. But according to @JacquesLucke it is not a real problem for now. I would like to give a huge thanks to @JacquesLucke who helped during this and pushed me to reduce the duplication further. Reviewed By: brecht, sergey, JacquesLucke Differential Revision: https://developer.blender.org/D13791
Diffstat (limited to 'source/blender/blenlib/BLI_float3.hh')
-rw-r--r--source/blender/blenlib/BLI_float3.hh320
1 files changed, 0 insertions, 320 deletions
diff --git a/source/blender/blenlib/BLI_float3.hh b/source/blender/blenlib/BLI_float3.hh
deleted file mode 100644
index 765f524fb31..00000000000
--- a/source/blender/blenlib/BLI_float3.hh
+++ /dev/null
@@ -1,320 +0,0 @@
-/*
- * 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
-
-#include <iostream>
-
-#include "BLI_math_vector.h"
-
-namespace blender {
-
-struct float3 {
- float x, y, z;
-
- float3() = default;
-
- float3(const float *ptr) : x{ptr[0]}, y{ptr[1]}, z{ptr[2]}
- {
- }
-
- float3(const float (*ptr)[3]) : float3(static_cast<const float *>(ptr[0]))
- {
- }
-
- explicit float3(float value) : x(value), y(value), z(value)
- {
- }
-
- explicit float3(int value) : x(value), y(value), z(value)
- {
- }
-
- float3(float x, float y, float z) : x{x}, y{y}, z{z}
- {
- }
-
- operator const float *() const
- {
- return &x;
- }
-
- operator float *()
- {
- return &x;
- }
-
- 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, const float &b)
- {
- return {a.x + b, a.y + b, a.z + b};
- }
-
- 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};
- }
-
- friend float3 operator-(const float3 &a, const float &b)
- {
- return {a.x - b, a.y - b, a.z - b};
- }
-
- 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);
- }
-
- bool is_zero() const
- {
- return this->x == 0.0f && this->y == 0.0f && this->z == 0.0f;
- }
-
- 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 refract(const float3 &incident, const float3 &normal, const float eta)
- {
- float3 result;
- float k = 1.0f - eta * eta * (1.0f - dot(normal, incident) * dot(normal, incident));
- if (k < 0.0f) {
- result = float3(0.0f);
- }
- else {
- result = eta * incident - (eta * dot(normal, incident) + sqrt(k)) * normal;
- }
- return result;
- }
-
- static float3 faceforward(const float3 &vector, const float3 &incident, const float3 &reference)
- {
- return dot(reference, incident) < 0.0f ? vector : -vector;
- }
-
- 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;
- }
-
- static float3 min(const float3 &a, const float3 &b)
- {
- return {a.x < b.x ? a.x : b.x, a.y < b.y ? a.y : b.y, a.z < b.z ? a.z : b.z};
- }
-
- static float3 max(const float3 &a, const float3 &b)
- {
- return {a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y, a.z > b.z ? a.z : b.z};
- }
-
- static void min_max(const float3 &vector, float3 &min, float3 &max)
- {
- min = float3::min(vector, min);
- max = float3::max(vector, max);
- }
-
- static float3 safe_divide(const float3 &a, const float b)
- {
- return (b != 0.0f) ? a / b : float3(0.0f);
- }
-
- static float3 floor(const float3 &a)
- {
- return float3(floorf(a.x), floorf(a.y), floorf(a.z));
- }
-
- void invert()
- {
- x = -x;
- y = -y;
- z = -z;
- }
-
- uint64_t hash() const
- {
- uint64_t x1 = *reinterpret_cast<const uint32_t *>(&x);
- uint64_t x2 = *reinterpret_cast<const uint32_t *>(&y);
- 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 cross(const float3 &a, const float3 &b)
- {
- float3 result;
- cross_v3_v3v3(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)
- {
- float3 diff = a - b;
- return float3::dot(diff, diff);
- }
-
- 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