blender/intern
Clément Foucault a2c1c368af 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 @Howard Trickey (howardt) opinion on the matter.
- The noexcept on the copy constructor of mpq(2|3) is being removed.
  But according to @Jacques Lucke (JacquesLucke) it is not a real problem
  for now.

I would like to give a huge thanks to @Jacques Lucke (JacquesLucke) who
helped during this and pushed me to reduce the duplication further.

Reviewed By: brecht, sergey, JacquesLucke

Differential Revision: http://developer.blender.org/D13791
2022-01-12 12:19:39 +01:00
..
atomic Cleanup: reorganize doxygen modules 2021-12-14 20:56:11 +11:00
audaspace Cleanup: make format after SortedIncludes change 2020-03-19 09:33:58 +01:00
clog Cleanup: reorganize doxygen modules 2021-12-14 20:56:11 +11:00
cycles Fix T93727: Tiled render error in Cycles after changing temp directory 2022-01-10 16:54:12 +01:00
dualcon Cleanup: capitalize NOTE tag 2021-11-30 10:15:17 +11:00
eigen Cleanup: reorganize doxygen modules 2021-12-14 20:56:11 +11:00
ffmpeg Fix: VSE search in mpegts files would fail 2021-06-11 14:05:07 +02:00
ghost BLI: Refactor vector types & functions to use templates 2022-01-12 12:19:39 +01:00
glew-mx Cleanup: reorganize doxygen modules 2021-12-14 20:56:11 +11:00
guardedalloc Revert "Cleanup: remove declaration for removed function" 2022-01-11 18:16:00 +11:00
iksolver Cleanup: fix typos in source code in intern/ 2022-01-05 15:35:30 +01:00
itasc Cleanup: reorganize doxygen modules 2021-12-14 20:56:11 +11:00
libc_compat Cleanup: reorganize doxygen modules 2021-12-14 20:56:11 +11:00
libmv Cleanup: fix typos in source code in intern/ 2022-01-05 15:35:30 +01:00
locale Cleanup: reorganize doxygen modules 2021-12-14 20:56:11 +11:00
mantaflow Cleanup: reorganize doxygen modules 2021-12-14 20:56:11 +11:00
memutil Cleanup: fix typos in source code in intern/ 2022-01-05 15:35:30 +01:00
mikktspace Cleanup: pass arguments as const 2021-10-04 13:15:15 +11:00
opencolorio Cleanup: fix typos in source code in intern/ 2022-01-05 15:35:30 +01:00
opensubdiv Cleanup: fix typos in source code in intern/ 2022-01-05 15:35:30 +01:00
openvdb Cleanup: Remove unused/unecessary OpenVDB C API 2021-08-02 12:26:28 -04:00
quadriflow Cleanup: Simplify logic, follow style guide for integer types 2021-07-30 15:08:43 -04:00
rigidbody Cleanup: reorganize doxygen modules 2021-12-14 20:56:11 +11:00
sky Cleanup: reorganize doxygen modules 2021-12-14 20:56:11 +11:00
utfconv Cleanup: fix typos in source code in intern/ 2022-01-05 15:35:30 +01:00
CMakeLists.txt Cleanup: Remove unused numapi library 2022-01-07 12:19:02 +01:00