diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2022-01-12 14:19:00 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2022-01-12 14:19:39 +0300 |
commit | a2c1c368af48644fa8995ecbe7138cc0d7900c30 (patch) | |
tree | 39cd435d2033f2952d2c39ce302816b037263c63 /source/blender/blenkernel/intern/spline_bezier.cc | |
parent | d320f3677e2a98b12d386a3e6f3da2d7b7018463 (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 @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
Diffstat (limited to 'source/blender/blenkernel/intern/spline_bezier.cc')
-rw-r--r-- | source/blender/blenkernel/intern/spline_bezier.cc | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/source/blender/blenkernel/intern/spline_bezier.cc b/source/blender/blenkernel/intern/spline_bezier.cc index b24c8960857..980437014b1 100644 --- a/source/blender/blenkernel/intern/spline_bezier.cc +++ b/source/blender/blenkernel/intern/spline_bezier.cc @@ -199,11 +199,13 @@ void BezierSpline::ensure_auto_handles() const } for (const int i : IndexRange(this->size())) { + using namespace blender; + if (ELEM(HandleType::Auto, handle_types_left_[i], handle_types_right_[i])) { const float3 prev_diff = positions_[i] - previous_position(positions_, is_cyclic_, i); const float3 next_diff = next_position(positions_, is_cyclic_, i) - positions_[i]; - float prev_len = prev_diff.length(); - float next_len = next_diff.length(); + float prev_len = math::length(prev_diff); + float next_len = math::length(next_diff); if (prev_len == 0.0f) { prev_len = 1.0f; } @@ -213,7 +215,7 @@ void BezierSpline::ensure_auto_handles() const const float3 dir = next_diff / next_len + prev_diff / prev_len; /* This magic number is unfortunate, but comes from elsewhere in Blender. */ - const float len = dir.length() * 2.5614f; + const float len = math::length(dir) * 2.5614f; if (len != 0.0f) { if (handle_types_left_[i] == HandleType::Auto) { const float prev_len_clamped = std::min(prev_len, next_len * 5.0f); @@ -228,12 +230,12 @@ void BezierSpline::ensure_auto_handles() const if (handle_types_left_[i] == HandleType::Vector) { const float3 prev = previous_position(positions_, is_cyclic_, i); - handle_positions_left_[i] = float3::interpolate(positions_[i], prev, 1.0f / 3.0f); + handle_positions_left_[i] = math::interpolate(positions_[i], prev, 1.0f / 3.0f); } if (handle_types_right_[i] == HandleType::Vector) { const float3 next = next_position(positions_, is_cyclic_, i); - handle_positions_right_[i] = float3::interpolate(positions_[i], next, 1.0f / 3.0f); + handle_positions_right_[i] = math::interpolate(positions_[i], next, 1.0f / 3.0f); } } @@ -275,6 +277,8 @@ static void set_handle_position(const float3 &position, float3 &handle, float3 &handle_other) { + using namespace blender::math; + /* Don't bother when the handle positions are calculated automatically anyway. */ if (ELEM(type, BezierSpline::HandleType::Auto, BezierSpline::HandleType::Vector)) { return; @@ -283,9 +287,9 @@ static void set_handle_position(const float3 &position, handle = new_value; if (type_other == BezierSpline::HandleType::Align) { /* Keep track of the old length of the opposite handle. */ - const float length = float3::distance(handle_other, position); + const float length = distance(handle_other, position); /* Set the other handle to directly opposite from the current handle. */ - const float3 dir = (handle - position).normalized(); + const float3 dir = normalize(handle - position); handle_other = position - dir * length; } } @@ -353,6 +357,7 @@ int BezierSpline::evaluated_points_size() const void BezierSpline::correct_end_tangents() const { + using namespace blender::math; if (is_cyclic_) { return; } @@ -360,10 +365,10 @@ void BezierSpline::correct_end_tangents() const MutableSpan<float3> tangents(evaluated_tangents_cache_); if (handle_positions_right_.first() != positions_.first()) { - tangents.first() = (handle_positions_right_.first() - positions_.first()).normalized(); + tangents.first() = normalize(handle_positions_right_.first() - positions_.first()); } if (handle_positions_left_.last() != positions_.last()) { - tangents.last() = (positions_.last() - handle_positions_left_.last()).normalized(); + tangents.last() = normalize(positions_.last() - handle_positions_left_.last()); } } @@ -371,20 +376,22 @@ BezierSpline::InsertResult BezierSpline::calculate_segment_insertion(const int i const int next_index, const float parameter) { + using namespace blender::math; + BLI_assert(parameter <= 1.0f && parameter >= 0.0f); BLI_assert(next_index == 0 || next_index == index + 1); const float3 &point_prev = positions_[index]; const float3 &handle_prev = handle_positions_right_[index]; const float3 &handle_next = handle_positions_left_[next_index]; const float3 &point_next = positions_[next_index]; - const float3 center_point = float3::interpolate(handle_prev, handle_next, parameter); + const float3 center_point = interpolate(handle_prev, handle_next, parameter); BezierSpline::InsertResult result; - result.handle_prev = float3::interpolate(point_prev, handle_prev, parameter); - result.handle_next = float3::interpolate(handle_next, point_next, parameter); - result.left_handle = float3::interpolate(result.handle_prev, center_point, parameter); - result.right_handle = float3::interpolate(center_point, result.handle_next, parameter); - result.position = float3::interpolate(result.left_handle, result.right_handle, parameter); + result.handle_prev = interpolate(point_prev, handle_prev, parameter); + result.handle_next = interpolate(handle_next, point_next, parameter); + result.left_handle = interpolate(result.handle_prev, center_point, parameter); + result.right_handle = interpolate(center_point, result.handle_next, parameter); + result.position = interpolate(result.left_handle, result.right_handle, parameter); return result; } |