2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-04-21 16:55:00 +02:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "BLI_math_matrix.h"
|
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
2022-01-12 12:57:07 +01:00
|
|
|
#include "BLI_math_vec_types.hh"
|
|
|
|
|
#include "BLI_math_vector.h"
|
2022-02-15 10:27:03 -06:00
|
|
|
#include "BLI_math_vector.hh"
|
2020-04-21 16:55:00 +02:00
|
|
|
|
2020-06-09 10:27:24 +02:00
|
|
|
namespace blender {
|
2020-04-21 16:55:00 +02:00
|
|
|
|
|
|
|
|
struct float4x4 {
|
|
|
|
|
float values[4][4];
|
|
|
|
|
|
|
|
|
|
float4x4() = default;
|
|
|
|
|
|
|
|
|
|
float4x4(const float *matrix)
|
|
|
|
|
{
|
|
|
|
|
memcpy(values, matrix, sizeof(float) * 16);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 18:24:59 +02:00
|
|
|
float4x4(const float matrix[4][4]) : float4x4(static_cast<const float *>(matrix[0]))
|
2020-04-21 16:55:00 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 17:29:39 -04:00
|
|
|
/* Assumes an XYZ euler order. */
|
|
|
|
|
static float4x4 from_loc_eul_scale(const float3 location,
|
|
|
|
|
const float3 rotation,
|
|
|
|
|
const float3 scale)
|
|
|
|
|
{
|
|
|
|
|
float4x4 mat;
|
|
|
|
|
loc_eul_size_to_mat4(mat.values, location, rotation, scale);
|
|
|
|
|
return mat;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-24 12:41:49 -05:00
|
|
|
static float4x4 from_location(const float3 location)
|
|
|
|
|
{
|
|
|
|
|
float4x4 mat = float4x4::identity();
|
|
|
|
|
copy_v3_v3(mat.values[3], location);
|
|
|
|
|
return mat;
|
|
|
|
|
}
|
|
|
|
|
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
static float4x4 from_normalized_axis_data(const float3 location,
|
|
|
|
|
const float3 forward,
|
|
|
|
|
const float3 up)
|
|
|
|
|
{
|
|
|
|
|
BLI_ASSERT_UNIT_V3(forward);
|
|
|
|
|
BLI_ASSERT_UNIT_V3(up);
|
2021-07-23 16:09:57 +02:00
|
|
|
|
|
|
|
|
/* Negate the cross product so that the resulting matrix has determinant 1 (instead of -1).
|
|
|
|
|
* Without the negation, the result would be a so called improper rotation. That means it
|
|
|
|
|
* contains a reflection. Such an improper rotation matrix could not be converted to another
|
|
|
|
|
* representation of a rotation such as euler angles. */
|
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
2022-01-12 12:57:07 +01:00
|
|
|
const float3 cross = -math::cross(forward, up);
|
2021-07-23 16:09:57 +02:00
|
|
|
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
float4x4 matrix;
|
|
|
|
|
matrix.values[0][0] = forward.x;
|
|
|
|
|
matrix.values[1][0] = cross.x;
|
|
|
|
|
matrix.values[2][0] = up.x;
|
|
|
|
|
matrix.values[3][0] = location.x;
|
|
|
|
|
|
|
|
|
|
matrix.values[0][1] = forward.y;
|
|
|
|
|
matrix.values[1][1] = cross.y;
|
|
|
|
|
matrix.values[2][1] = up.y;
|
|
|
|
|
matrix.values[3][1] = location.y;
|
|
|
|
|
|
|
|
|
|
matrix.values[0][2] = forward.z;
|
|
|
|
|
matrix.values[1][2] = cross.z;
|
|
|
|
|
matrix.values[2][2] = up.z;
|
|
|
|
|
matrix.values[3][2] = location.z;
|
|
|
|
|
|
|
|
|
|
matrix.values[0][3] = 0.0f;
|
|
|
|
|
matrix.values[1][3] = 0.0f;
|
|
|
|
|
matrix.values[2][3] = 0.0f;
|
|
|
|
|
matrix.values[3][3] = 1.0f;
|
|
|
|
|
|
|
|
|
|
return matrix;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 11:52:44 -04:00
|
|
|
static float4x4 identity()
|
|
|
|
|
{
|
|
|
|
|
float4x4 mat;
|
|
|
|
|
unit_m4(mat.values);
|
|
|
|
|
return mat;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 16:55:00 +02:00
|
|
|
operator float *()
|
|
|
|
|
{
|
2020-08-07 18:24:59 +02:00
|
|
|
return &values[0][0];
|
2020-04-21 16:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operator const float *() const
|
|
|
|
|
{
|
2020-08-07 18:24:59 +02:00
|
|
|
return &values[0][0];
|
2020-04-21 16:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-27 21:35:36 +01:00
|
|
|
float *operator[](const int64_t index)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(index >= 0);
|
|
|
|
|
BLI_assert(index < 4);
|
|
|
|
|
return &values[index][0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const float *operator[](const int64_t index) const
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(index >= 0);
|
|
|
|
|
BLI_assert(index < 4);
|
|
|
|
|
return &values[index][0];
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 18:21:12 +01:00
|
|
|
using c_style_float4x4 = float[4][4];
|
|
|
|
|
c_style_float4x4 &ptr()
|
|
|
|
|
{
|
|
|
|
|
return values;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const c_style_float4x4 &ptr() const
|
|
|
|
|
{
|
|
|
|
|
return values;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 16:55:00 +02:00
|
|
|
friend float4x4 operator*(const float4x4 &a, const float4x4 &b)
|
|
|
|
|
{
|
|
|
|
|
float4x4 result;
|
|
|
|
|
mul_m4_m4m4(result.values, a.values, b.values);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 16:59:06 -05:00
|
|
|
void operator*=(const float4x4 &other)
|
|
|
|
|
{
|
|
|
|
|
mul_m4_m4_post(values, other.values);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 16:55:00 +02:00
|
|
|
/**
|
|
|
|
|
* This also applies the translation on the vector. Use `m.ref_3x3() * v` if that is not
|
|
|
|
|
* intended.
|
|
|
|
|
*/
|
|
|
|
|
friend float3 operator*(const float4x4 &m, const float3 &v)
|
|
|
|
|
{
|
|
|
|
|
float3 result;
|
|
|
|
|
mul_v3_m4v3(result, m.values, v);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend float3 operator*(const float4x4 &m, const float (*v)[3])
|
|
|
|
|
{
|
|
|
|
|
return m * float3(v);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-19 15:28:58 -04:00
|
|
|
float3 translation() const
|
|
|
|
|
{
|
|
|
|
|
return float3(values[3]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Assumes XYZ rotation order. */
|
|
|
|
|
float3 to_euler() const
|
|
|
|
|
{
|
|
|
|
|
float3 euler;
|
|
|
|
|
mat4_to_eul(euler, values);
|
|
|
|
|
return euler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float3 scale() const
|
|
|
|
|
{
|
|
|
|
|
float3 scale;
|
|
|
|
|
mat4_to_size(scale, values);
|
|
|
|
|
return scale;
|
|
|
|
|
}
|
|
|
|
|
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
void apply_scale(const float scale)
|
|
|
|
|
{
|
|
|
|
|
values[0][0] *= scale;
|
|
|
|
|
values[0][1] *= scale;
|
|
|
|
|
values[0][2] *= scale;
|
|
|
|
|
values[1][0] *= scale;
|
|
|
|
|
values[1][1] *= scale;
|
|
|
|
|
values[1][2] *= scale;
|
|
|
|
|
values[2][0] *= scale;
|
|
|
|
|
values[2][1] *= scale;
|
|
|
|
|
values[2][2] *= scale;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-03 14:31:26 +02:00
|
|
|
float4x4 inverted() const
|
|
|
|
|
{
|
2020-07-23 22:30:05 +02:00
|
|
|
float4x4 result;
|
|
|
|
|
invert_m4_m4(result.values, values);
|
2020-07-03 14:31:26 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Matrix inversion can be implemented more efficiently for affine matrices.
|
|
|
|
|
*/
|
|
|
|
|
float4x4 inverted_affine() const
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(values[0][3] == 0.0f && values[1][3] == 0.0f && values[2][3] == 0.0f &&
|
|
|
|
|
values[3][3] == 1.0f);
|
|
|
|
|
return this->inverted();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-23 22:30:05 +02:00
|
|
|
float4x4 transposed() const
|
|
|
|
|
{
|
|
|
|
|
float4x4 result;
|
|
|
|
|
transpose_m4_m4(result.values, values);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 14:51:15 +02:00
|
|
|
float4x4 inverted_transposed_affine() const
|
|
|
|
|
{
|
|
|
|
|
return this->inverted_affine().transposed();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 16:55:00 +02:00
|
|
|
struct float3x3_ref {
|
|
|
|
|
const float4x4 &data;
|
|
|
|
|
|
|
|
|
|
friend float3 operator*(const float3x3_ref &m, const float3 &v)
|
|
|
|
|
{
|
|
|
|
|
float3 result;
|
|
|
|
|
mul_v3_mat3_m4v3(result, m.data.values, v);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
float3x3_ref ref_3x3() const
|
|
|
|
|
{
|
|
|
|
|
return {*this};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static float4x4 interpolate(const float4x4 &a, const float4x4 &b, float t)
|
|
|
|
|
{
|
|
|
|
|
float result[4][4];
|
|
|
|
|
interp_m4_m4m4(result, a.values, b.values, t);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2020-07-10 12:53:50 +02:00
|
|
|
|
2021-07-25 13:29:45 -04:00
|
|
|
bool is_negative() const
|
|
|
|
|
{
|
|
|
|
|
return is_negative_m4(ptr());
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
uint64_t hash() const
|
2020-07-10 12:53:50 +02:00
|
|
|
{
|
2020-07-20 12:16:20 +02:00
|
|
|
uint64_t h = 435109;
|
|
|
|
|
for (int i = 0; i < 16; i++) {
|
2020-08-07 18:24:59 +02:00
|
|
|
float value = (static_cast<const float *>(values[0]))[i];
|
|
|
|
|
h = h * 33 + *reinterpret_cast<const uint32_t *>(&value);
|
2020-07-10 12:53:50 +02:00
|
|
|
}
|
|
|
|
|
return h;
|
|
|
|
|
}
|
2020-04-21 16:55:00 +02:00
|
|
|
};
|
|
|
|
|
|
2020-06-09 10:27:24 +02:00
|
|
|
} // namespace blender
|