Cleanup: bring operator overloads closer together

This commit is contained in:
Jacques Lucke 2020-07-03 14:31:26 +02:00
parent 395b294b61
commit 7704e6a678
5 changed files with 140 additions and 140 deletions

View File

@ -201,6 +201,18 @@ class Array {
return *this;
}
T &operator[](uint index)
{
BLI_assert(index < size_);
return data_[index];
}
const T &operator[](uint index) const
{
BLI_assert(index < size_);
return data_[index];
}
operator Span<T>() const
{
return Span<T>(data_, size_);
@ -221,18 +233,6 @@ class Array {
return *this;
}
T &operator[](uint index)
{
BLI_assert(index < size_);
return data_[index];
}
const T &operator[](uint index) const
{
BLI_assert(index < size_);
return data_[index];
}
/**
* Returns the number of elements in the array.
*/

View File

@ -58,56 +58,6 @@ struct float3 {
return &x;
}
float normalize_and_get_length()
{
return normalize_v3(*this);
}
float3 normalized() const
{
float3 result;
normalize_v3_v3(result, *this);
return result;
}
float length() const
{
return len_v3(*this);
}
float length_squared() const
{
return len_squared_v3(*this);
}
void reflect(const float3 &normal)
{
*this = this->reflected(normal);
}
float3 reflected(const float3 &normal) const
{
float3 result;
reflect_v3_v3v3(result, *this, normal);
return result;
}
static float3 safe_divide(const float3 &a, const float3 &b)
{
float3 result;
result.x = (b.x == 0.0f) ? 0.0f : a.x / b.x;
result.y = (b.y == 0.0f) ? 0.0f : a.y / b.y;
result.z = (b.z == 0.0f) ? 0.0f : a.z / b.z;
return result;
}
void invert()
{
x = -x;
y = -y;
z = -z;
}
friend float3 operator+(const float3 &a, const float3 &b)
{
return {a.x + b.x, a.y + b.y, a.z + b.z};
@ -178,6 +128,56 @@ struct float3 {
return stream;
}
float normalize_and_get_length()
{
return normalize_v3(*this);
}
float3 normalized() const
{
float3 result;
normalize_v3_v3(result, *this);
return result;
}
float length() const
{
return len_v3(*this);
}
float length_squared() const
{
return len_squared_v3(*this);
}
void reflect(const float3 &normal)
{
*this = this->reflected(normal);
}
float3 reflected(const float3 &normal) const
{
float3 result;
reflect_v3_v3v3(result, *this, normal);
return result;
}
static float3 safe_divide(const float3 &a, const float3 &b)
{
float3 result;
result.x = (b.x == 0.0f) ? 0.0f : a.x / b.x;
result.y = (b.y == 0.0f) ? 0.0f : a.y / b.y;
result.z = (b.z == 0.0f) ? 0.0f : a.z / b.z;
return result;
}
void invert()
{
x = -x;
y = -y;
z = -z;
}
static float dot(const float3 &a, const float3 &b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;

View File

@ -46,23 +46,6 @@ struct float4x4 {
return (const float *)this;
}
float4x4 inverted() const
{
float result[4][4];
invert_m4_m4(result, values);
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();
}
friend float4x4 operator*(const float4x4 &a, const float4x4 &b)
{
float4x4 result;
@ -86,6 +69,23 @@ struct float4x4 {
return m * float3(v);
}
float4x4 inverted() const
{
float result[4][4];
invert_m4_m4(result, values);
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();
}
struct float3x3_ref {
const float4x4 &data;

View File

@ -263,26 +263,6 @@ class Vector {
}
}
operator Span<T>() const
{
return Span<T>(begin_, this->size());
}
operator MutableSpan<T>()
{
return MutableSpan<T>(begin_, this->size());
}
Span<T> as_span() const
{
return *this;
}
MutableSpan<T> as_mutable_span()
{
return *this;
}
Vector &operator=(const Vector &other)
{
if (this == &other) {
@ -309,6 +289,42 @@ class Vector {
return *this;
}
/**
* Get the value at the given index. This invokes undefined behavior when the index is out of
* bounds.
*/
const T &operator[](uint index) const
{
BLI_assert(index < this->size());
return begin_[index];
}
T &operator[](uint index)
{
BLI_assert(index < this->size());
return begin_[index];
}
operator Span<T>() const
{
return Span<T>(begin_, this->size());
}
operator MutableSpan<T>()
{
return MutableSpan<T>(begin_, this->size());
}
Span<T> as_span() const
{
return *this;
}
MutableSpan<T> as_mutable_span()
{
return *this;
}
/**
* Make sure that enough memory is allocated to hold min_capacity elements.
* This won't necessarily make an allocation when min_capacity is small.
@ -673,22 +689,6 @@ class Vector {
return this->first_index_of_try(value) != -1;
}
/**
* Get the value at the given index. This invokes undefined behavior when the index is out of
* bounds.
*/
const T &operator[](uint index) const
{
BLI_assert(index < this->size());
return begin_[index];
}
T &operator[](uint index)
{
BLI_assert(index < this->size());
return begin_[index];
}
/**
* Get access to the underlying array.
*/

View File

@ -235,6 +235,31 @@ class VectorSet {
return *this;
}
/**
* Get the key stored at the given position in the vector.
*/
const Key &operator[](uint32_t index) const
{
BLI_assert(index <= this->size());
return keys_[index];
}
operator Span<Key>() const
{
return Span<Key>(keys_, this->size());
}
/**
* Get an Span referencing the keys vector. The referenced memory buffer is only valid as
* long as the vector set is not changed.
*
* The keys must not be changed, because this would change their hash value.
*/
Span<Key> as_span() const
{
return *this;
}
/**
* Add a new key to the vector set. This invokes undefined behavior when the key is in the set
* already. When you know for certain that a key is not in the set yet, use this method for
@ -402,31 +427,6 @@ class VectorSet {
return keys_ + this->size();
}
/**
* Get the key stored at the given position in the vector.
*/
const Key &operator[](uint32_t index) const
{
BLI_assert(index <= this->size());
return keys_[index];
}
operator Span<Key>() const
{
return Span<Key>(keys_, this->size());
}
/**
* Get an Span referencing the keys vector. The referenced memory buffer is only valid as
* long as the vector set is not changed.
*
* The keys must not be changed, because this would change their hash value.
*/
Span<Key> as_span() const
{
return *this;
}
/**
* Print common statistics like size and collision count. This is useful for debugging purposes.
*/