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/io
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/io')
-rw-r--r--source/blender/io/alembic/intern/abc_reader_object.cc25
-rw-r--r--source/blender/io/gpencil/intern/gpencil_io_base.cc5
-rw-r--r--source/blender/io/gpencil/intern/gpencil_io_base.hh3
-rw-r--r--source/blender/io/gpencil/intern/gpencil_io_import_svg.cc2
-rw-r--r--source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh2
-rw-r--r--source/blender/io/wavefront_obj/exporter/obj_export_mtl.cc2
-rw-r--r--source/blender/io/wavefront_obj/exporter/obj_export_mtl.hh2
-rw-r--r--source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc2
-rw-r--r--source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc2
9 files changed, 31 insertions, 14 deletions
diff --git a/source/blender/io/alembic/intern/abc_reader_object.cc b/source/blender/io/alembic/intern/abc_reader_object.cc
index 86fa580bf1f..4a359c49d26 100644
--- a/source/blender/io/alembic/intern/abc_reader_object.cc
+++ b/source/blender/io/alembic/intern/abc_reader_object.cc
@@ -120,10 +120,29 @@ static Imath::M44d blend_matrices(const Imath::M44d &m0, const Imath::M44d &m1,
* the matrices manually.
*/
- convert_matrix_datatype(m0, mat0);
- convert_matrix_datatype(m1, mat1);
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ mat0[i][j] = static_cast<float>(m0[i][j]);
+ }
+ }
+
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ mat1[i][j] = static_cast<float>(m1[i][j]);
+ }
+ }
+
interp_m4_m4m4(ret, mat0, mat1, weight);
- return convert_matrix_datatype(ret);
+
+ Imath::M44d m;
+
+ for (int i = 0; i < 4; i++) {
+ for (int j = 0; j < 4; j++) {
+ m[i][j] = ret[i][j];
+ }
+ }
+
+ return m;
}
Imath::M44d get_matrix(const IXformSchema &schema, const float time)
diff --git a/source/blender/io/gpencil/intern/gpencil_io_base.cc b/source/blender/io/gpencil/intern/gpencil_io_base.cc
index f031648d2ed..7868bade8c1 100644
--- a/source/blender/io/gpencil/intern/gpencil_io_base.cc
+++ b/source/blender/io/gpencil/intern/gpencil_io_base.cc
@@ -23,9 +23,8 @@
* \ingroup bgpencil
*/
-#include "BLI_float2.hh"
-#include "BLI_float3.hh"
#include "BLI_float4x4.hh"
+#include "BLI_math_vec_types.hh"
#include "BLI_path_util.h"
#include "BLI_span.hh"
@@ -283,7 +282,7 @@ float GpencilIO::stroke_point_radius_get(bGPDlayer *gpl, bGPDstroke *gps)
const float2 screen_ex = gpencil_3D_point_to_2D(&pt->x);
const float2 v1 = screen_co - screen_ex;
- float radius = v1.length();
+ float radius = math::length(v1);
BKE_gpencil_free_stroke(gps_perimeter);
return MAX2(radius, 1.0f);
diff --git a/source/blender/io/gpencil/intern/gpencil_io_base.hh b/source/blender/io/gpencil/intern/gpencil_io_base.hh
index 09557cd7a4d..ae54d5056dc 100644
--- a/source/blender/io/gpencil/intern/gpencil_io_base.hh
+++ b/source/blender/io/gpencil/intern/gpencil_io_base.hh
@@ -22,9 +22,8 @@
* \ingroup bgpencil
*/
-#include "BLI_float2.hh"
-#include "BLI_float3.hh"
#include "BLI_float4x4.hh"
+#include "BLI_math_vec_types.hh"
#include "BLI_vector.hh"
#include "DNA_space_types.h" /* for FILE_MAX */
diff --git a/source/blender/io/gpencil/intern/gpencil_io_import_svg.cc b/source/blender/io/gpencil/intern/gpencil_io_import_svg.cc
index 941d1137f4d..455ebb7c3cb 100644
--- a/source/blender/io/gpencil/intern/gpencil_io_import_svg.cc
+++ b/source/blender/io/gpencil/intern/gpencil_io_import_svg.cc
@@ -21,8 +21,8 @@
* \ingroup bgpencil
*/
-#include "BLI_float3.hh"
#include "BLI_math.h"
+#include "BLI_math_vec_types.hh"
#include "BLI_span.hh"
#include "DNA_gpencil_types.h"
diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh
index 9a4dfe3efe3..e6d2853d040 100644
--- a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh
@@ -22,7 +22,7 @@
#include <optional>
-#include "BLI_float3.hh"
+#include "BLI_math_vec_types.hh"
#include "BLI_utility_mixins.hh"
#include "BLI_vector.hh"
diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_mtl.cc b/source/blender/io/wavefront_obj/exporter/obj_export_mtl.cc
index b99d41e0c72..5b710939e00 100644
--- a/source/blender/io/wavefront_obj/exporter/obj_export_mtl.cc
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_mtl.cc
@@ -21,8 +21,8 @@
#include "BKE_image.h"
#include "BKE_node.h"
-#include "BLI_float3.hh"
#include "BLI_map.hh"
+#include "BLI_math_vec_types.hh"
#include "BLI_path_util.h"
#include "DNA_material_types.h"
diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_mtl.hh b/source/blender/io/wavefront_obj/exporter/obj_export_mtl.hh
index 2f62d189bd1..a84dcb80a48 100644
--- a/source/blender/io/wavefront_obj/exporter/obj_export_mtl.hh
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_mtl.hh
@@ -20,8 +20,8 @@
#pragma once
-#include "BLI_float3.hh"
#include "BLI_map.hh"
+#include "BLI_math_vec_types.hh"
#include "BLI_string_ref.hh"
#include "BLI_vector.hh"
diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc b/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc
index 91aabd8fa76..ec690115115 100644
--- a/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc
@@ -18,9 +18,9 @@
* \ingroup obj
*/
-#include "BLI_float3.hh"
#include "BLI_listbase.h"
#include "BLI_math.h"
+#include "BLI_math_vec_types.hh"
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_query.h"
diff --git a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc
index f9151bb97f8..0feca806f35 100644
--- a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc
+++ b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc
@@ -236,7 +236,7 @@ TEST(obj_exporter_writer, mtllib)
static bool strings_equal_after_first_lines(const std::string &a, const std::string &b)
{
/* If `dbg_level > 0` then a failing test will print context around the first mismatch. */
- const int dbg_level = 0;
+ const bool dbg_level = 0;
const size_t a_len = a.size();
const size_t b_len = b.size();
const size_t a_next = a.find_first_of('\n');