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:
authorClément Foucault <foucault.clem@gmail.com>2022-01-12 14:57:07 +0300
committerClément Foucault <foucault.clem@gmail.com>2022-01-12 14:57:07 +0300
commitd43b5791e0c1f6581a539c2663ec8200e107740a (patch)
tree422c3f78cae6ca3560d8ebbbbd243235b5ea067f /source/blender/blenlib/BLI_mpq2.hh
parentfb6bd8864411ee27db05ceadcb80f690f44e48dd (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_mpq2.hh')
-rw-r--r--source/blender/blenlib/BLI_mpq2.hh184
1 files changed, 0 insertions, 184 deletions
diff --git a/source/blender/blenlib/BLI_mpq2.hh b/source/blender/blenlib/BLI_mpq2.hh
deleted file mode 100644
index 18bc8821d9c..00000000000
--- a/source/blender/blenlib/BLI_mpq2.hh
+++ /dev/null
@@ -1,184 +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 "BLI_math_mpq.hh"
-# include "BLI_mpq3.hh"
-
-namespace blender {
-
-struct mpq2 {
- mpq_class x, y;
-
- mpq2() = default;
-
- mpq2(const mpq_class *ptr) : x{ptr[0]}, y{ptr[1]}
- {
- }
-
- mpq2(mpq_class x, mpq_class y) : x(x), y(y)
- {
- }
-
- mpq2(const mpq2 &other) : x(other.x), y(other.y)
- {
- }
-
- mpq2(mpq2 &&other) noexcept : x(std::move(other.x)), y(std::move(other.y))
- {
- }
-
- ~mpq2() = default;
-
- mpq2 &operator=(const mpq2 &other)
- {
- if (this != &other) {
- x = other.x;
- y = other.y;
- }
- return *this;
- }
-
- mpq2 &operator=(mpq2 &&other) noexcept
- {
- x = std::move(other.x);
- y = std::move(other.y);
- return *this;
- }
-
- mpq2(const mpq3 &other) : x(other.x), y(other.y)
- {
- }
-
- operator mpq_class *()
- {
- return &x;
- }
-
- operator const mpq_class *() const
- {
- return &x;
- }
-
- /**
- * Cannot do this exactly in rational arithmetic!
- * Approximate by going in and out of doubles.
- */
- mpq_class length() const
- {
- mpq_class lsquared = dot(*this, *this);
- return mpq_class(sqrt(lsquared.get_d()));
- }
-
- friend mpq2 operator+(const mpq2 &a, const mpq2 &b)
- {
- return {a.x + b.x, a.y + b.y};
- }
-
- friend mpq2 operator-(const mpq2 &a, const mpq2 &b)
- {
- return {a.x - b.x, a.y - b.y};
- }
-
- friend mpq2 operator*(const mpq2 &a, mpq_class b)
- {
- return {a.x * b, a.y * b};
- }
-
- friend mpq2 operator/(const mpq2 &a, mpq_class b)
- {
- BLI_assert(b != 0);
- return {a.x / b, a.y / b};
- }
-
- friend mpq2 operator*(mpq_class a, const mpq2 &b)
- {
- return b * a;
- }
-
- friend bool operator==(const mpq2 &a, const mpq2 &b)
- {
- return a.x == b.x && a.y == b.y;
- }
-
- friend bool operator!=(const mpq2 &a, const mpq2 &b)
- {
- return a.x != b.x || a.y != b.y;
- }
-
- friend std::ostream &operator<<(std::ostream &stream, const mpq2 &v)
- {
- stream << "(" << v.x << ", " << v.y << ")";
- return stream;
- }
-
- static mpq_class dot(const mpq2 &a, const mpq2 &b)
- {
- return a.x * b.x + a.y * b.y;
- }
-
- static mpq2 interpolate(const mpq2 &a, const mpq2 &b, mpq_class t)
- {
- return a * (1 - t) + b * t;
- }
-
- static mpq2 abs(const mpq2 &a)
- {
- mpq_class abs_x = (a.x >= 0) ? a.x : -a.x;
- mpq_class abs_y = (a.y >= 0) ? a.y : -a.y;
- return mpq2(abs_x, abs_y);
- }
-
- static mpq_class distance(const mpq2 &a, const mpq2 &b)
- {
- return (a - b).length();
- }
-
- static mpq_class distance_squared(const mpq2 &a, const mpq2 &b)
- {
- mpq2 diff = a - b;
- return dot(diff, diff);
- }
-
- struct isect_result {
- enum {
- LINE_LINE_COLINEAR = -1,
- LINE_LINE_NONE = 0,
- LINE_LINE_EXACT = 1,
- LINE_LINE_CROSS = 2,
- } kind;
- mpq_class lambda;
- };
-
- static isect_result isect_seg_seg(const mpq2 &v1,
- const mpq2 &v2,
- const mpq2 &v3,
- const mpq2 &v4);
-
- /** There is a sensible use for hashing on exact arithmetic types. */
- uint64_t hash() const;
-};
-
-} // namespace blender
-
-#endif /* WITH_GMP */