Math: Rework Box and Rect classes

This commit is contained in:
SirLynix
2022-12-18 14:57:14 +01:00
parent 811194bb97
commit 830eee78a8
28 changed files with 242 additions and 642 deletions

View File

@@ -238,7 +238,7 @@ namespace Nz
m_aspectRatio = fViewport.width / fViewport.height;
// Convert it back to int
m_viewport.Set(fViewport);
m_viewport = Recti(fViewport);
m_viewerInstance.UpdateTargetSize(fViewport.GetLengths());

View File

@@ -150,8 +150,8 @@ namespace Nz
inline void LinearSlicedSprite::SetTextureRect(const Rectf& textureRect)
{
Vector2ui textureSize(GetTextureSize());
return SetTextureCoords(textureRect / Vector2f(textureSize));
Vector2f invTextureSize = 1.f / Vector2f(Vector2ui(GetTextureSize()));
return SetTextureCoords(Rectf(textureRect.x * invTextureSize.x, textureRect.y * invTextureSize.y, textureRect.width * invTextureSize.x, textureRect.height * invTextureSize.y));
}
}

View File

@@ -105,8 +105,8 @@ namespace Nz
inline void SlicedSprite::SetTextureRect(const Rectf& textureRect)
{
Vector2ui textureSize(GetTextureSize());
return SetTextureCoords(textureRect / Vector2f(textureSize));
Vector2f invTextureSize = 1.f / Vector2f(Vector2ui(GetTextureSize()));
return SetTextureCoords(Rectf(textureRect.x * invTextureSize.x, textureRect.y * invTextureSize.y, textureRect.width * invTextureSize.x, textureRect.height * invTextureSize.y));
}
}

View File

@@ -140,7 +140,7 @@ namespace Nz
inline void SpotLight::UpdateBoundingVolume()
{
// We make a box center in the origin
Boxf box(Vector3f::Zero());
Boxf box = Boxf::Zero();
// We compute the other points
Vector3f base(Vector3f::Forward() * m_radius);

View File

@@ -89,8 +89,6 @@ namespace Nz
inline void Sprite::UpdateVertices()
{
Boxf aabb(-1.f, -1.f, -1.f);
VertexStruct_XYZ_Color_UV* vertices = m_vertices.data();
std::array<Vector2f, RectCornerCount> cornerExtent;
@@ -107,15 +105,10 @@ namespace Nz
vertices->position = Vector3f(m_size * cornerExtent[UnderlyingCast(corner)], 0.f) - originShift;
vertices->uv = m_textureCoords.GetCorner(corner);
if (aabb.IsValid())
aabb.ExtendTo(vertices->position);
else
aabb.Set(vertices->position);
vertices++;
}
UpdateAABB(aabb);
UpdateAABB(Rectf(-originShift.x, -originShift.y, m_size.x, m_size.y));
OnElementInvalidated(this);
}
}

View File

@@ -54,11 +54,6 @@ namespace Nz
NazaraSlot(AbstractAtlas, OnAtlasRelease, releaseSlot);
};
struct RenderData
{
};
struct RenderKey
{
Texture* texture;
@@ -93,9 +88,7 @@ namespace Nz
std::unordered_map<const AbstractAtlas*, AtlasSlots> m_atlases;
mutable std::unordered_map<RenderKey, RenderIndices, HashRenderKey> m_renderInfos;
std::shared_ptr<MaterialInstance> m_material;
std::vector<RenderData> m_data;
std::vector<VertexStruct_XYZ_Color_UV> m_vertices;
Recti m_scissorBox;
};
}

View File

@@ -15,7 +15,7 @@ namespace Nz
{
/*!
* \ingroup math
* \ingroup math
* \class Nz::BoundingVolume
* \brief Math class that represents a bounding volume, a combination of a box and an oriented box
*
@@ -396,7 +396,7 @@ namespace Nz
{
obb.Update(transformMatrix);
aabb.Set(obb(0), obb(1));
aabb = Boxf::FromExtends(obb(0), obb(1));
for (unsigned int i = 2; i < 8; ++i)
aabb.ExtendTo(obb(i));
}
@@ -412,7 +412,7 @@ namespace Nz
{
obb.Update(translation);
aabb.Set(obb(0), obb(1));
aabb = Boxf::FromExtends(obb(0), obb(1));
for (unsigned int i = 2; i < 8; ++i)
aabb.ExtendTo(obb(i));
}

View File

@@ -25,10 +25,8 @@ namespace Nz
Box() = default;
Box(T Width, T Height, T Depth);
Box(T X, T Y, T Z, T Width, T Height, T Depth);
Box(const T box[6]);
Box(const Rect<T>& rect);
Box(const Vector3<T>& lengths);
Box(const Vector3<T>& vec1, const Vector3<T>& vec2);
explicit Box(const Vector3<T>& pos, const Vector3<T>& lengths);
template<typename U> explicit Box(const Box<U>& box);
Box(const Box& box) = default;
~Box() = default;
@@ -56,17 +54,11 @@ namespace Nz
bool Intersect(const Box& box, Box* intersection = nullptr) const;
bool IsNull() const;
bool IsValid() const;
Box& MakeZero();
Box& Set(T Width, T Height, T Depth);
Box& Set(T X, T Y, T Z, T Width, T Height, T Depth);
Box& Set(const T box[6]);
Box& Set(const Rect<T>& rect);
Box& Set(const Vector3<T>& lengths);
Box& Set(const Vector3<T>& vec1, const Vector3<T>& vec2);
template<typename U> Box& Set(const Box<U>& box);
Box& Scale(T scalar);
Box& Scale(const Vector3<T>& vec);
std::string ToString() const;
@@ -74,19 +66,17 @@ namespace Nz
Box& Translate(const Vector3<T>& translation);
T& operator[](std::size_t i);
T operator[](std::size_t i) const;
const T& operator[](std::size_t i) const;
Box operator*(T scalar) const;
Box operator*(const Vector3<T>& vec) const;
Box& operator=(const Box& other) = default;
Box& operator*=(T scalar);
Box& operator*=(const Vector3<T>& vec);
Box& operator=(const Box&) = default;
Box& operator=(Box&&) = default;
bool operator==(const Box& box) const;
bool operator!=(const Box& box) const;
static Box FromExtends(const Vector3<T>& vec1, const Vector3<T>& vec2);
static Box Lerp(const Box& from, const Box& to, T interpolation);
static Box Invalid();
static Box Zero();
T x, y, z, width, height, depth;

View File

@@ -29,9 +29,14 @@ namespace Nz
*/
template<typename T>
Box<T>::Box(T Width, T Height, T Depth)
Box<T>::Box(T Width, T Height, T Depth) :
x(0),
y(0),
z(0),
width(Width),
height(Height),
depth(Depth)
{
Set(Width, Height, Depth);
}
/*!
@@ -44,23 +49,15 @@ namespace Nz
* \param Height Height of the box (following Y)
* \param Depth Depth of the box (following Z)
*/
template<typename T>
Box<T>::Box(T X, T Y, T Z, T Width, T Height, T Depth)
Box<T>::Box(T X, T Y, T Z, T Width, T Height, T Depth) :
x(X),
y(Y),
z(Z),
width(Width),
height(Height),
depth(Depth)
{
Set(X, Y, Z, Width, Height, Depth);
}
/*!
* \brief Constructs a Box object from an array of six elements
*
* \param vec[6] vec[0] is X position, vec[1] is Y position, vec[2] is Z position, vec[3] is width, vec[4] is height and vec[5] is depth
*/
template<typename T>
Box<T>::Box(const T vec[6])
{
Set(vec);
}
/*!
@@ -70,39 +67,32 @@ namespace Nz
*
* \remark Z position is 0 and depth is 1
*/
template<typename T>
Box<T>::Box(const Rect<T>& rect)
Box<T>::Box(const Rect<T>& rect) :
x(rect.x),
y(rect.y),
z(0),
width(rect.width),
height(rect.height),
depth(0)
{
Set(rect);
}
/*!
* \brief Constructs a Box object from a vector representing width, height and depth
* \brief Constructs a Box object from two vector representing position and lengths
*
* \param pos (X, Y, Z) of the box
* \param lengths (Width, Height, Depth) of the box
*
* \remark Positions will be (0, 0, 0)
*/
template<typename T>
Box<T>::Box(const Vector3<T>& lengths)
Box<T>::Box(const Vector3<T>& pos, const Vector3<T>& lengths) :
x(pos.x),
y(pos.y),
z(pos.z),
width(lengths.x),
height(lengths.y),
depth(lengths.z)
{
Set(lengths);
}
/*!
* \brief Constructs a Box object from two vectors representing point of the space
* (X, Y, Z) will be the components minimum of the two vectors and the (width, height, depth) will be the components maximum - minimum
*
* \param vec1 First point
* \param vec2 Second point
*/
template<typename T>
Box<T>::Box(const Vector3<T>& vec1, const Vector3<T>& vec2)
{
Set(vec1, vec2);
}
/*!
@@ -113,9 +103,14 @@ namespace Nz
template<typename T>
template<typename U>
Box<T>::Box(const Box<U>& box)
Box<T>::Box(const Box<U>& box) :
x(static_cast<T>(box.x)),
y(static_cast<T>(box.y)),
z(static_cast<T>(box.z)),
width(static_cast<T>(box.width)),
height(static_cast<T>(box.height)),
depth(static_cast<T>(box.depth))
{
Set(box);
}
/*!
@@ -482,179 +477,52 @@ namespace Nz
/*!
* \brief Checks whether this box is valid
* \return true if the box has a strictly positive width, height and depth
* \return true if the box has a negative or zero width, height and depth
*/
template<typename T>
bool Box<T>::IsNull() const
{
return width <= T(0.0) && height <= T(0.0) && depth <= T(0.0);
}
/*!
* \brief Checks whether this box is valid
* \return true if the box has a positive width, height and depth
*/
template<typename T>
bool Box<T>::IsValid() const
{
return width > T(0.0) && height > T(0.0) && depth > T(0.0);
return width >= T(0.0) && height >= T(0.0) && depth >= T(0.0);
}
/*!
* \brief Makes the box position (0, 0, 0) and lengths (0, 0, 0)
* \return A reference to this box with position (0, 0, 0) and lengths (0, 0, 0)
* \brief Multiplies the lengths of this box with the scalar
* \return A reference to this box where lengths are the product of these lengths and the scalar
*
* \see Zero
* \param scalar The scalar to multiply width, height and depth with
*/
template<typename T>
Box<T>& Box<T>::MakeZero()
Box<T>& Box<T>::Scale(T scalar)
{
x = T(0.0);
y = T(0.0);
z = T(0.0);
width = T(0.0);
height = T(0.0);
depth = T(0.0);
width *= scalar;
height *= scalar;
depth *= scalar;
return *this;
}
/*!
* \brief Sets the components of the box with width, height and depth
* \return A reference to this box
* \brief Multiplies the lengths of this box with the vector
* \return A reference to this box where width, height and depth are the product of the old width, height and depth with the vec
*
* \param Width Width of the box (following X)
* \param Height Height of the box (following Y)
* \param Depth Depth of the box (following Z)
*
* \remark Position will be (0, 0, 0)
* \param vec The vector where component one multiply width, two height and three depth
*/
template<typename T>
Box<T>& Box<T>::Set(T Width, T Height, T Depth)
Box<T>& Box<T>::Scale(const Vector3<T>& vec)
{
x = T(0.0);
y = T(0.0);
z = T(0.0);
width = Width;
height = Height;
depth = Depth;
return *this;
}
/*!
* \brief Constructs a Box object from its position and sizes
*
* \param X X component of position
* \param Y Y component of position
* \param Z Z component of position
* \param Width Width of the box (following X)
* \param Height Height of the box (following Y)
* \param Depth Depth of the box (following Z)
*/
template<typename T>
Box<T>& Box<T>::Set(T X, T Y, T Z, T Width, T Height, T Depth)
{
x = X;
y = Y;
z = Z;
width = Width;
height = Height;
depth = Depth;
return *this;
}
/*!
* \brief Sets the components of the box from an array of six elements
* \return A reference to this box
*
* \param box[6] box[0] is X position, box[1] is Y position, box[2] is Z position, box[3] is width, box[4] is height and box[5] is depth
*/
template<typename T>
Box<T>& Box<T>::Set(const T box[6])
{
x = box[0];
y = box[1];
z = box[2];
width = box[3];
height = box[4];
depth = box[5];
return *this;
}
/*!
* \brief Sets the components of the box with components from a Rect
* \return A reference to this box
*
* \param rect Rectangle which describes (X, Y) position and (width, height) lenghts
*
* \remark Z position is 0 and depth is 1
*/
template<typename T>
Box<T>& Box<T>::Set(const Rect<T>& rect)
{
x = rect.x;
y = rect.y;
z = T(0.0);
width = rect.width;
height = rect.height;
depth = T(1.0);
return *this;
}
/*!
* \brief Sets the components of the box from a vector representing width, height and depth
* \return A reference to this box
*
* \param lengths (Width, Height, depth) of the box
*
* \remark Position will be (0, 0, 0)
*/
template<typename T>
Box<T>& Box<T>::Set(const Vector3<T>& lengths)
{
return Set(lengths.x, lengths.y, lengths.z);
}
/*!
* \brief Sets the components of the box from two vectors representing point of the space
* (X, Y, Z) will be the components minimum of the two vectors and the (width, height, depth) will be the components maximum - minimum
* \return A reference to this box
*
* \param vec1 First point
* \param vec2 Second point
*/
template<typename T>
Box<T>& Box<T>::Set(const Vector3<T>& vec1, const Vector3<T>& vec2)
{
x = std::min(vec1.x, vec2.x);
y = std::min(vec1.y, vec2.y);
z = std::min(vec1.z, vec2.z);
width = (vec2.x > vec1.x) ? vec2.x - vec1.x : vec1.x - vec2.x;
height = (vec2.y > vec1.y) ? vec2.y - vec1.y : vec1.y - vec2.y;
depth = (vec2.z > vec1.z) ? vec2.z - vec1.z : vec1.z - vec2.z;
return *this;
}
/*!
* \brief Sets the components of the box from another type of Box
* \return A reference to this box
*
* \param box Box of type U to convert its components
*/
template<typename T>
template<typename U>
Box<T>& Box<T>::Set(const Box<U>& box)
{
x = T(box.x);
y = T(box.y);
z = T(box.z);
width = T(box.width);
height = T(box.height);
depth = T(box.depth);
width *= vec.x;
height *= vec.y;
depth *= vec.z;
return *this;
}
@@ -663,7 +531,6 @@ namespace Nz
* \brief Gives a string representation
* \return A string representation of the object: "Box(x, y, z, width, height, depth)"
*/
template<typename T>
std::string Box<T>::ToString() const
{
@@ -691,7 +558,7 @@ namespace Nz
std::abs(matrix(0,1)) * halfSize.x + std::abs(matrix(1,1)) * halfSize.y + std::abs(matrix(2,1)) * halfSize.z,
std::abs(matrix(0,2)) * halfSize.x + std::abs(matrix(1,2)) * halfSize.y + std::abs(matrix(2,2)) * halfSize.z);
return Set(center - halfSize, center + halfSize);
return operator=(Boxf::FromExtends(center - halfSize, center + halfSize));
}
/*!
@@ -738,73 +605,13 @@ namespace Nz
*/
template<typename T>
T Box<T>::operator[](std::size_t i) const
const T& Box<T>::operator[](std::size_t i) const
{
NazaraAssert(i < 6, "Index out of range");
return *(&x+i);
}
/*!
* \brief Multiplies the lengths with the scalar
* \return A box where the position is the same and width, height and depth are the product of the old width, height and depth and the scalar
*
* \param scalar The scalar to multiply width, height and depth with
*/
template<typename T>
Box<T> Box<T>::operator*(T scalar) const
{
return Box(x, y, z, width * scalar, height * scalar, depth * scalar);
}
/*!
* \brief Multiplies the lengths with the vector
* \return A box where the position is the same and width, height and depth are the product of the old width, height and depth with the vec
*
* \param vec The vector where component one multiply width, two height and three depth
*/
template<typename T>
Box<T> Box<T>::operator*(const Vector3<T>& vec) const
{
return Box(x, y, z, width * vec.x, height * vec.y, depth * vec.z);
}
/*!
* \brief Multiplies the lengths of this box with the scalar
* \return A reference to this box where lengths are the product of these lengths and the scalar
*
* \param scalar The scalar to multiply width, height and depth with
*/
template<typename T>
Box<T>& Box<T>::operator*=(T scalar)
{
width *= scalar;
height *= scalar;
depth *= scalar;
return *this;
}
/*!
* \brief Multiplies the lengths of this box with the vector
* \return A reference to this box where width, height and depth are the product of the old width, height and depth with the vec
*
* \param vec The vector where component one multiply width, two height and three depth
*/
template<typename T>
Box<T>& Box<T>::operator*=(const Vector3<T>& vec)
{
width *= vec.x;
height *= vec.y;
depth *= vec.z;
return *this;
}
/*!
* \brief Compares the box to other one
* \return true if the boxes are the same
@@ -845,18 +652,33 @@ namespace Nz
*
* \see Lerp
*/
template<typename T>
Box<T> Box<T>::FromExtends(const Vector3<T>& vec1, const Vector3<T>& vec2)
{
Box box;
box.x = std::min(vec1.x, vec2.x);
box.y = std::min(vec1.y, vec2.y);
box.z = std::min(vec1.z, vec2.z);
box.width = (vec2.x > vec1.x) ? vec2.x - vec1.x : vec1.x - vec2.x;
box.height = (vec2.y > vec1.y) ? vec2.y - vec1.y : vec1.y - vec2.y;
box.depth = (vec2.z > vec1.z) ? vec2.z - vec1.z : vec1.z - vec2.z;
return box;
}
/*!
* \brief Interpolates the box to other one with a factor of interpolation
* \return A new box which is the interpolation of two rectangles
*
* \param from Initial box
* \param to Target box
* \param interpolation Factor of interpolation
*
* \see Lerp
*/
template<typename T>
Box<T> Box<T>::Lerp(const Box& from, const Box& to, T interpolation)
{
#ifdef NAZARA_DEBUG
if (interpolation < T(0.0) || interpolation > T(1.0))
{
NazaraError("Interpolation must be in range [0..1] (Got " + NumberToString(interpolation) + ')');
return Zero();
}
#endif
Box box;
box.x = Nz::Lerp(from.x, to.x, interpolation);
box.y = Nz::Lerp(from.y, to.y, interpolation);
@@ -874,14 +696,22 @@ namespace Nz
*
* \see MakeZero
*/
template<typename T>
Box<T> Box<T>::Invalid()
{
return Box(-1, -1, -1, -1, -1, -1);
}
/*!
* \brief Shorthand for the box (0, 0, 0, 0, 0, 0)
* \return A box with position (0, 0, 0) and lengths (0, 0, 0)
*
* \see MakeZero
*/
template<typename T>
Box<T> Box<T>::Zero()
{
Box box;
box.MakeZero();
return box;
return Box(Vector3<T>::Zero(), Vector3<T>::Zero());
}
/*!

View File

@@ -14,7 +14,7 @@
namespace Nz
{
/*!
* \ingroup math
* \ingroup math
* \class Nz::OrientedBox
* \brief Math class that represents an oriented three dimensional box
*
@@ -164,7 +164,7 @@ namespace Nz
template<typename T>
OrientedBox<T>& OrientedBox<T>::Set(const Box<T>& box)
{
localBox.Set(box);
localBox = box;
return *this;
}

View File

@@ -22,9 +22,7 @@ namespace Nz
Rect() = default;
Rect(T Width, T Height);
Rect(T X, T Y, T Width, T Height);
Rect(const T rect[4]);
Rect(const Vector2<T>& lengths);
Rect(const Vector2<T>& vec1, const Vector2<T>& vec2);
explicit Rect(const Vector2<T>& vec1, const Vector2<T>& vec2);
template<typename U> explicit Rect(const Rect<U>& rect);
Rect(const Rect& rect) = default;
~Rect() = default;
@@ -48,39 +46,30 @@ namespace Nz
bool Intersect(const Rect& rect, Rect* intersection = nullptr) const;
bool IsNull() const;
bool IsValid() const;
Rect& MakeZero();
Rect& Set(T Width, T Height);
Rect& Set(T X, T Y, T Width, T Height);
Rect& Set(const T rect[4]);
Rect& Set(const Vector2<T>& lengths);
Rect& Set(const Vector2<T>& vec1, const Vector2<T>& vec2);
template<typename U> Rect& Set(const Rect<U>& rect);
Rect& Scale(T scalar);
Rect& Scale(const Vector2<T>& vec);
std::string ToString() const;
Rect& Translate(const Vector2<T>& translation);
T& operator[](std::size_t i);
T operator[](std::size_t i) const;
const T& operator[](std::size_t i) const;
Rect operator*(T scalar) const;
Rect operator*(const Vector2<T>& vec) const;
Rect operator/(T scalar) const;
Rect operator/(const Vector2<T>& vec) const;
Rect& operator=(const Rect& other) = default;
Rect& operator*=(T scalar);
Rect& operator*=(const Vector2<T>& vec);
Rect& operator/=(T scalar);
Rect& operator/=(const Vector2<T>& vec);
Rect& operator=(const Rect&) = default;
Rect& operator=(Rect&&) = default;
bool operator==(const Rect& rect) const;
bool operator!=(const Rect& rect) const;
static Rect FromExtends(const Vector2<T>& vec1, const Vector2<T>& vec2);
static Rect Lerp(const Rect& from, const Rect& to, T interpolation);
static Rect Invalid();
static Rect Zero();
T x, y, width, height;

View File

@@ -30,9 +30,12 @@ namespace Nz
*/
template<typename T>
Rect<T>::Rect(T Width, T Height)
Rect<T>::Rect(T Width, T Height) :
x(0),
y(0),
width(Width),
height(Height)
{
Set(Width, Height);
}
/*!
@@ -43,53 +46,29 @@ namespace Nz
* \param Width Width of the rectangle (following X)
* \param Height Height of the rectangle (following Y)
*/
template<typename T>
Rect<T>::Rect(T X, T Y, T Width, T Height)
Rect<T>::Rect(T X, T Y, T Width, T Height) :
x(X),
y(Y),
width(Width),
height(Height)
{
Set(X, Y, Width, Height);
}
/*!
* \brief Constructs a Rect object from an array of four elements
* \brief Constructs a Rect object from two vector representing position and lengths
*
* \param vec[4] vec[0] is X position, vec[1] is Y position, vec[2] is width and vec[3] is height
* \param pos (X, Y) of the rect
* \param lengths (Width, Height) of the rect
*/
template<typename T>
Rect<T>::Rect(const T vec[4])
Rect<T>::Rect(const Vector2<T>& pos, const Vector2<T>& lengths) :
x(pos.x),
y(pos.y),
width(lengths.x),
height(lengths.y)
{
Set(vec);
}
/*!
* \brief Constructs a Rect object from a vector representing width and height
*
* \param lengths (Width, Height) of the rectangle
*
* \remark X and Y will be (0, 0)
*/
template<typename T>
Rect<T>::Rect(const Vector2<T>& lengths)
{
Set(lengths);
}
/*!
* \brief Constructs a Rect object from two vectors representing point of the space
* (X, Y) will be the components minimum of the two vectors and the width and height will be the components maximum - minimum
*
* \param vec1 First point
* \param vec2 Second point
*/
template<typename T>
Rect<T>::Rect(const Vector2<T>& vec1, const Vector2<T>& vec2)
{
Set(vec1, vec2);
}
/*!
* \brief Constructs a Rect object from another type of Rect
*
@@ -98,9 +77,12 @@ namespace Nz
template<typename T>
template<typename U>
Rect<T>::Rect(const Rect<U>& rect)
Rect<T>::Rect(const Rect<U>& rect) :
x(static_cast<T>(rect.x)),
y(static_cast<T>(rect.y)),
width(static_cast<T>(rect.width)),
height(static_cast<T>(rect.height))
{
Set(rect);
}
/*!
@@ -128,7 +110,6 @@ namespace Nz
*
* \see Contains
*/
template<typename T>
bool Rect<T>::Contains(const Rect<T>& rect) const
{
@@ -144,7 +125,6 @@ namespace Nz
*
* \see Contains
*/
template<typename T>
bool Rect<T>::Contains(const Vector2<T>& point) const
{
@@ -160,7 +140,6 @@ namespace Nz
*
* \see ExtendTo
*/
template<typename T>
Rect<T>& Rect<T>::ExtendTo(T X, T Y)
{
@@ -184,7 +163,6 @@ namespace Nz
*
* \see ExtendTo
*/
template<typename T>
Rect<T>& Rect<T>::ExtendTo(const Rect& rect)
{
@@ -385,13 +363,22 @@ namespace Nz
/*!
* \brief Checks whether this rectangle is valid
* \return true if the rectangle has a strictly positive width and height
* \return true if the rectangle has a positive width and height
*/
template<typename T>
bool Rect<T>::IsNull() const
{
return width <= T(0.0) && height <= T(0.0);
}
/*!
* \brief Checks whether this rectangle is valid
* \return true if the rectangle has a positive width and height
*/
template<typename T>
bool Rect<T>::IsValid() const
{
return width > T(0.0) && height > T(0.0);
return width >= T(0.0) && height >= T(0.0);
}
/*!
@@ -413,115 +400,33 @@ namespace Nz
}
/*!
* \brief Sets the components of the rectangle with width and height
* \return A reference to this rectangle
* \brief Multiplies the lengths of this rectangle with the scalar
* \return A reference to this rectangle where lengths are the product of these lengths and the scalar
*
* \param Width Width of the rectangle (following X)
* \param Height Height of the rectangle (following Y)
*
* \remark Position will be (0, 0)
* \param scalar The scalar to multiply width and height with
*/
template<typename T>
Rect<T>& Rect<T>::Set(T Width, T Height)
Rect<T>& Rect<T>::Scale(T scalar)
{
x = T(0.0);
y = T(0.0);
width = Width;
height = Height;
width *= scalar;
height *= scalar;
return *this;
}
/*!
* \brief Sets the components of the rectangle
* \return A reference to this rectangle
* \brief Multiplies the lengths of this rectangle with the vector
* \return A reference to this rectangle where width and height are the product of the old width and height with the vec
*
* \param X X position
* \param Y Y position
* \param Width Width of the rectangle (following X)
* \param Height Height of the rectangle (following Y)
* \param vec The vector where component one multiply width and two height
*/
template<typename T>
Rect<T>& Rect<T>::Set(T X, T Y, T Width, T Height)
Rect<T>& Rect<T>::Scale(const Vector2<T>& vec)
{
x = X;
y = Y;
width = Width;
height = Height;
return *this;
}
/*!
* \brief Sets the components of the rectangle from an array of four elements
* \return A reference to this rectangle
*
* \param rect[4] rect[0] is X position, rect[1] is Y position, rect[2] is width and rect[3] is height
*/
template<typename T>
Rect<T>& Rect<T>::Set(const T rect[4])
{
x = rect[0];
y = rect[1];
width = rect[2];
height = rect[3];
return *this;
}
/*!
* \brief Sets the components of the rectange from a vector representing width and height
* \return A reference to this rectangle
*
* \param lengths (Width, Height) of the rectangle
*
* \remark Position will be (0, 0)
*/
template<typename T>
Rect<T>& Rect<T>::Set(const Vector2<T>& lengths)
{
return Set(lengths.x, lengths.y);
}
/*!
* \brief Sets a Rect object from two vectors representing point of the space
* (X, Y) will be the components minimum of the two vectors and the width and height will be the components maximum - minimum
* \return A reference to this rectangle
*
* \param vec1 First point
* \param vec2 Second point
*/
template<typename T>
Rect<T>& Rect<T>::Set(const Vector2<T>& vec1, const Vector2<T>& vec2)
{
x = std::min(vec1.x, vec2.x);
y = std::min(vec1.y, vec2.y);
width = (vec2.x > vec1.x) ? vec2.x - vec1.x : vec1.x - vec2.x;
height = (vec2.y > vec1.y) ? vec2.y - vec1.y : vec1.y - vec2.y;
return *this;
}
/*!
* \brief Sets the components of the rectangle from another type of Rect
* \return A reference to this rectangle
*
* \param rect Rectangle of type U to convert its components
*/
template<typename T>
template<typename U>
Rect<T>& Rect<T>::Set(const Rect<U>& rect)
{
x = T(rect.x);
y = T(rect.y);
width = T(rect.width);
height = T(rect.height);
width *= vec.x;
height *= vec.y;
return *this;
}
@@ -583,129 +488,13 @@ namespace Nz
*/
template<typename T>
T Rect<T>::operator[](std::size_t i) const
const T& Rect<T>::operator[](std::size_t i) const
{
NazaraAssert(i < 4, "Index out of range");
return *(&x+i);
}
/*!
* \brief Multiplies the lengths with the scalar
* \return A rectangle where the position is the same and width and height are the product of the old width and height and the scalar
*
* \param scalar The scalar to multiply width and height with
*/
template<typename T>
Rect<T> Rect<T>::operator*(T scalar) const
{
return Rect(x, y, width * scalar, height * scalar);
}
/*!
* \brief Multiplies the lengths with the vector
* \return A rectangle where the position is the same and width and height are the product of the old width and height with the vec
*
* \param vec The vector where component one multiply width and two height
*/
template<typename T>
Rect<T> Rect<T>::operator*(const Vector2<T>& vec) const
{
return Rect(x, y, width*vec.x, height*vec.y);
}
/*!
* \brief Divides the lengths with the scalar
* \return A rectangle where the position is the same and width and height are the quotient of the old width and height and the scalar
*
* \param scalar The scalar to divide width and height with
*/
template<typename T>
Rect<T> Rect<T>::operator/(T scalar) const
{
return Rect(x, y, width / scalar, height / scalar);
}
/*!
* \brief Divides the lengths with the vector
* \return A rectangle where the position is the same and width and height are the quotient of the old width and height with the vec
*
* \param vec The vector where component one divide width and two height
*/
template<typename T>
Rect<T> Rect<T>::operator/(const Vector2<T>& vec) const
{
return Rect(x, y, width/vec.x, height/vec.y);
}
/*!
* \brief Multiplies the lengths of this rectangle with the scalar
* \return A reference to this rectangle where lengths are the product of these lengths and the scalar
*
* \param scalar The scalar to multiply width and height with
*/
template<typename T>
Rect<T>& Rect<T>::operator*=(T scalar)
{
width *= scalar;
height *= scalar;
return *this;
}
/*!
* \brief Multiplies the lengths of this rectangle with the vector
* \return A reference to this rectangle where width and height are the product of the old width and height with the vec
*
* \param vec The vector where component one multiply width and two height
*/
template<typename T>
Rect<T>& Rect<T>::operator*=(const Vector2<T>& vec)
{
width *= vec.x;
height *= vec.y;
return *this;
}
/*!
* \brief Divides the lengths of this rectangle with the scalar
* \return A reference to this rectangle where lengths are the quotient of these lengths and the scalar
*
* \param scalar The scalar to divide width and height with
*/
template<typename T>
Rect<T>& Rect<T>::operator/=(T scalar)
{
width /= scalar;
height /= scalar;
return *this;
}
/*!
* \brief Divives the lengths of this rectangle with the vector
* \return A reference to this rectangle where width and height are the quotient of the old width and height with the vec
*
* \param vec The vector where component one divide width and two height
*/
template<typename T>
Rect<T>& Rect<T>::operator/=(const Vector2<T>& vec)
{
width /= vec.x;
height /= vec.y;
return *this;
}
/*!
* \brief Compares the rectangle to other one
* \return true if the rectangles are the same
@@ -733,6 +522,26 @@ namespace Nz
return !operator==(rect);
}
/*!
* \brief Sets a Rect object from two vectors representing point of the space
* (X, Y) will be the components minimum of the two vectors and the width and height will be the components maximum - minimum
* \return A reference to this rectangle
*
* \param vec1 First point
* \param vec2 Second point
*/
template<typename T>
Rect<T> Rect<T>::FromExtends(const Vector2<T>& vec1, const Vector2<T>& vec2)
{
Rect rect;
rect.x = std::min(vec1.x, vec2.x);
rect.y = std::min(vec1.y, vec2.y);
rect.width = (vec2.x > vec1.x) ? vec2.x - vec1.x : vec1.x - vec2.x;
rect.height = (vec2.y > vec1.y) ? vec2.y - vec1.y : vec1.y - vec2.y;
return rect;
}
/*!
* \brief Interpolates the rectangle to other one with a factor of interpolation
* \return A new rectangle which is the interpolation of two rectangles
@@ -741,23 +550,11 @@ namespace Nz
* \param to Target rectangle
* \param interpolation Factor of interpolation
*
* \remark interpolation is meant to be between 0 and 1, other values are potentially undefined behavior
* \remark With NAZARA_DEBUG, a NazaraError is thrown and Zero() is returned
*
* \see Lerp
*/
template<typename T>
Rect<T> Rect<T>::Lerp(const Rect& from, const Rect& to, T interpolation)
{
#ifdef NAZARA_DEBUG
if (interpolation < T(0.0) || interpolation > T(1.0))
{
NazaraError("Interpolation must be in range [0..1] (Got " + NumberToString(interpolation) + ')');
return Zero();
}
#endif
Rect rect;
rect.x = Nz::Lerp(from.x, to.x, interpolation);
rect.y = Nz::Lerp(from.y, to.y, interpolation);
@@ -773,14 +570,22 @@ namespace Nz
*
* \see MakeZero
*/
template<typename T>
Rect<T> Rect<T>::Invalid()
{
return Rect(-1, -1, -1, -1);
}
/*!
* \brief Shorthand for the rectangle (0, 0, 0, 0)
* \return A rectangle with position (0, 0) and lengths (0, 0)
*
* \see MakeZero
*/
template<typename T>
Rect<T> Rect<T>::Zero()
{
Rect rect;
rect.MakeZero();
return rect;
return Rect(0, 0, 0, 0);
}
/*!

View File

@@ -9,7 +9,7 @@ namespace Nz
{
inline bool AbstractImage::Update(const void* pixels, unsigned int srcWidth, unsigned int srcHeight, UInt8 level)
{
return Update(pixels, GetSize(level), srcWidth, srcHeight, level);
return Update(pixels, Boxui(Vector3ui::Zero(), GetSize(level)), srcWidth, srcHeight, level);
}
inline bool AbstractImage::Update(const void* pixels, const Rectui& rect, unsigned int z, unsigned int srcWidth, unsigned int srcHeight, UInt8 level)

View File

@@ -69,7 +69,7 @@ namespace Nz
Nz::Vector3f pos = entry.widget->GetPosition(Nz::CoordSys::Global);
Nz::Vector2f size = entry.widget->GetSize();
entry.box.Set(pos.x, pos.y, pos.z, size.x, size.y, 1.f);
entry.box = Boxf(pos.x, pos.y, pos.z, size.x, size.y, 1.f);
}
inline void Canvas::NotifyWidgetCursorUpdate(std::size_t index)