diff options
author | Clment Foucault <fclem> | 2022-01-12 14:46:52 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2022-01-12 14:47:43 +0300 |
commit | 46e049d0ce2bce2f53ddc41a0dbbea2969d00a5d (patch) | |
tree | 2b814f1b737ebe17364de1a5938deb9a7066d2a9 /source/blender/blenlib/BLI_mpq3.hh | |
parent | e5766752d04794c2693dedad75baeb8c7d68f4cf (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_mpq3.hh')
-rw-r--r-- | source/blender/blenlib/BLI_mpq3.hh | 297 |
1 files changed, 0 insertions, 297 deletions
diff --git a/source/blender/blenlib/BLI_mpq3.hh b/source/blender/blenlib/BLI_mpq3.hh deleted file mode 100644 index b9eda2ad7e1..00000000000 --- a/source/blender/blenlib/BLI_mpq3.hh +++ /dev/null @@ -1,297 +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 - -/** \file - * \ingroup bli - */ - -#ifdef WITH_GMP - -# include <iostream> - -# include "BLI_math.h" -# include "BLI_math_mpq.hh" -# include "BLI_span.hh" - -namespace blender { - -struct mpq3 { - mpq_class x, y, z; - - mpq3() = default; - - mpq3(const mpq_class *ptr) : x{ptr[0]}, y{ptr[1]}, z{ptr[2]} - { - } - - mpq3(const mpq_class (*ptr)[3]) : mpq3((const mpq_class *)ptr) - { - } - - explicit mpq3(mpq_class value) : x(value), y(value), z(value) - { - } - - explicit mpq3(int value) : x(value), y(value), z(value) - { - } - - mpq3(mpq_class x, mpq_class y, mpq_class z) : x{x}, y{y}, z{z} - { - } - - operator const mpq_class *() const - { - return &x; - } - - operator mpq_class *() - { - return &x; - } - - /* Cannot do this exactly in rational arithmetic! - * Approximate by going in and out of doubles. - */ - mpq_class normalize_and_get_length() - { - double dv[3] = {x.get_d(), y.get_d(), z.get_d()}; - double len = normalize_v3_db(dv); - this->x = mpq_class(dv[0]); - this->y = mpq_class(dv[1]); - this->z = mpq_class(dv[2]); - return len; - } - - mpq3 normalized() const - { - double dv[3] = {x.get_d(), y.get_d(), z.get_d()}; - double dr[3]; - normalize_v3_v3_db(dr, dv); - return mpq3(mpq_class(dr[0]), mpq_class(dr[1]), mpq_class(dr[2])); - } - - /* Cannot do this exactly in rational arithmetic! - * Approximate by going in and out of double. - */ - mpq_class length() const - { - mpq_class lsquared = this->length_squared(); - double dsquared = lsquared.get_d(); - double d = sqrt(dsquared); - return mpq_class(d); - } - - mpq_class length_squared() const - { - return x * x + y * y + z * z; - } - - void reflect(const mpq3 &normal) - { - *this = this->reflected(normal); - } - - mpq3 reflected(const mpq3 &normal) const - { - mpq3 result; - const mpq_class dot2 = 2 * dot(*this, normal); - result.x = this->x - (dot2 * normal.x); - result.y = this->y - (dot2 * normal.y); - result.z = this->z - (dot2 * normal.z); - return result; - } - - static mpq3 safe_divide(const mpq3 &a, const mpq3 &b) - { - mpq3 result; - result.x = (b.x == 0) ? mpq_class(0) : a.x / b.x; - result.y = (b.y == 0) ? mpq_class(0) : a.y / b.y; - result.z = (b.z == 0) ? mpq_class(0) : a.z / b.z; - return result; - } - - void invert() - { - x = -x; - y = -y; - z = -z; - } - - friend mpq3 operator+(const mpq3 &a, const mpq3 &b) - { - return mpq3(a.x + b.x, a.y + b.y, a.z + b.z); - } - - void operator+=(const mpq3 &b) - { - this->x += b.x; - this->y += b.y; - this->z += b.z; - } - - friend mpq3 operator-(const mpq3 &a, const mpq3 &b) - { - return mpq3(a.x - b.x, a.y - b.y, a.z - b.z); - } - - friend mpq3 operator-(const mpq3 &a) - { - return mpq3(-a.x, -a.y, -a.z); - } - - void operator-=(const mpq3 &b) - { - this->x -= b.x; - this->y -= b.y; - this->z -= b.z; - } - - void operator*=(mpq_class scalar) - { - this->x *= scalar; - this->y *= scalar; - this->z *= scalar; - } - - void operator*=(const mpq3 &other) - { - this->x *= other.x; - this->y *= other.y; - this->z *= other.z; - } - - friend mpq3 operator*(const mpq3 &a, const mpq3 &b) - { - return {a.x * b.x, a.y * b.y, a.z * b.z}; - } - - friend mpq3 operator*(const mpq3 &a, const mpq_class &b) - { - return mpq3(a.x * b, a.y * b, a.z * b); - } - - friend mpq3 operator*(const mpq_class &a, const mpq3 &b) - { - return mpq3(a * b.x, a * b.y, a * b.z); - } - - friend mpq3 operator/(const mpq3 &a, const mpq_class &b) - { - BLI_assert(b != 0); - return mpq3(a.x / b, a.y / b, a.z / b); - } - - friend bool operator==(const mpq3 &a, const mpq3 &b) - { - return a.x == b.x && a.y == b.y && a.z == b.z; - } - - friend bool operator!=(const mpq3 &a, const mpq3 &b) - { - return a.x != b.x || a.y != b.y || a.z != b.z; - } - - friend std::ostream &operator<<(std::ostream &stream, const mpq3 &v) - { - stream << "(" << v.x << ", " << v.y << ", " << v.z << ")"; - return stream; - } - - static mpq_class dot(const mpq3 &a, const mpq3 &b) - { - return a.x * b.x + a.y * b.y + a.z * b.z; - } - - static mpq_class dot_with_buffer(const mpq3 &a, const mpq3 &b, mpq3 &buffer) - { - buffer = a; - buffer *= b; - buffer.x += buffer.y; - buffer.x += buffer.z; - return buffer.x; - } - - static mpq3 cross(const mpq3 &a, const mpq3 &b) - { - 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]); - } - - static mpq3 cross_high_precision(const mpq3 &a, const mpq3 &b) - { - return cross(a, b); - } - - static mpq3 project(const mpq3 &a, const mpq3 &b) - { - const mpq_class mul = mpq3::dot(a, b) / mpq3::dot(b, b); - return mpq3(mul * b[0], mul * b[1], mul * b[2]); - } - - static mpq_class distance(const mpq3 &a, const mpq3 &b) - { - mpq3 diff(a.x - b.x, a.y - b.y, a.z - b.z); - return diff.length(); - } - - static mpq_class distance_squared(const mpq3 &a, const mpq3 &b) - { - mpq3 diff(a.x - b.x, a.y - b.y, a.z - b.z); - return mpq3::dot(diff, diff); - } - - static mpq_class distance_squared_with_buffer(const mpq3 &a, const mpq3 &b, mpq3 &buffer) - { - buffer = a; - buffer -= b; - return mpq3::dot(buffer, buffer); - } - - static mpq3 interpolate(const mpq3 &a, const mpq3 &b, mpq_class t) - { - mpq_class s = 1 - t; - return mpq3(a.x * s + b.x * t, a.y * s + b.y * t, a.z * s + b.z * t); - } - - static mpq3 abs(const mpq3 &a) - { - mpq_class abs_x = (a.x >= 0) ? a.x : -a.x; - mpq_class abs_y = (a.y >= 0) ? a.y : -a.y; - mpq_class abs_z = (a.z >= 0) ? a.z : -a.z; - return mpq3(abs_x, abs_y, abs_z); - } - - static int dominant_axis(const mpq3 &a) - { - mpq_class x = (a.x >= 0) ? a.x : -a.x; - mpq_class y = (a.y >= 0) ? a.y : -a.y; - mpq_class z = (a.z >= 0) ? a.z : -a.z; - return ((x > y) ? ((x > z) ? 0 : 2) : ((y > z) ? 1 : 2)); - } - - static mpq3 cross_poly(Span<mpq3> poly); - - /** There is a sensible use for hashing on exact arithmetic types. */ - uint64_t hash() const; -}; - -uint64_t hash_mpq_class(const mpq_class &value); - -} // namespace blender - -#endif /* WITH_GMP */ |