Improve math module (#396)

* Improve math module

- Mark almost everything constexpr
- Equality (a == b) is now exact, down to the bit level. If you want approximate equality use the new ApproxEqual method/static method
- Rename Nz::Extend to Nz::Extent
- Removed Make[] and Set[] methods in favor of their static counterpart and operator=
This commit is contained in:
Jérôme Leclercq
2023-06-02 22:30:51 +02:00
committed by GitHub
parent de88873c35
commit 1a55b550fb
64 changed files with 2200 additions and 3758 deletions

View File

@@ -132,7 +132,7 @@ namespace Nz
m_library.alGetListenerfv(AL_ORIENTATION, orientation);
if (up)
up->Set(orientation[3], orientation[4], orientation[5]);
(*up) = Vector3f(orientation[3], orientation[4], orientation[5]);
return Vector3f(orientation[0], orientation[1], orientation[2]);
}

View File

@@ -386,7 +386,7 @@ namespace Nz
void BulletNullCollider3D::ComputeInertia(float /*mass*/, Vector3f* inertia) const
{
inertia->Set(1.f, 1.f, 1.f);
*inertia = Vector3f::Unit();
}
btCollisionShape* BulletNullCollider3D::GetShape() const

View File

@@ -117,7 +117,7 @@ namespace Nz
StackArray<Vector2f> nVertices = NazaraStackArray(Vector2f, vertexCount);
for (int i = 0; i < vertexCount; ++i)
nVertices[i].Set(float(vertices[i].x), float(vertices[i].y));
nVertices[i] = Vector2f(float(vertices[i].x), float(vertices[i].y));
callback(nVertices.data(), nVertices.size());
};
@@ -282,7 +282,7 @@ namespace Nz
{
m_vertices.resize(vertexCount);
for (std::size_t i = 0; i < vertexCount; ++i)
m_vertices[i].Set(*vertices++);
m_vertices[i] = Vector2<cpFloat>(*vertices++);
}
Vector2f ChipmunkConvexCollider2D::ComputeCenterOfMass() const

View File

@@ -45,7 +45,7 @@ namespace Nz
StackArray<Vector2f> nVertices = NazaraStackArray(Vector2f, vertexCount);
for (int i = 0; i < vertexCount; ++i)
nVertices[i].Set(float(vertices[i].x), float(vertices[i].y));
nVertices[i] = Vector2f(float(vertices[i].x), float(vertices[i].y));
drawOptions->polygonCallback(nVertices.data(), vertexCount, float(radius), CpDebugColorToColor(outlineColor), CpDebugColorToColor(fillColor), drawOptions->userdata);
}
@@ -184,9 +184,9 @@ namespace Nz
if (cpSpacePointQueryNearest(m_handle, { from.x, from.y }, maxDistance, filter, &queryInfo))
{
result->closestPoint.Set(Vector2<cpFloat>(queryInfo.point.x, queryInfo.point.y));
result->closestPoint = Vector2f(Vector2<cpFloat>(queryInfo.point.x, queryInfo.point.y));
result->distance = float(queryInfo.distance);
result->fraction.Set(Vector2<cpFloat>(queryInfo.gradient.x, queryInfo.gradient.y));
result->fraction = Vector2f(Vector2<cpFloat>(queryInfo.gradient.x, queryInfo.gradient.y));
result->nearestBody = static_cast<ChipmunkRigidBody2D*>(cpShapeGetUserData(queryInfo.shape));
return true;
@@ -213,8 +213,8 @@ namespace Nz
RaycastHit hitInfo;
hitInfo.fraction = float(alpha);
hitInfo.hitNormal.Set(Vector2<cpFloat>(normal.x, normal.y));
hitInfo.hitPos.Set(Vector2<cpFloat>(point.x, point.y));
hitInfo.hitNormal = Vector2f(Vector2<cpFloat>(normal.x, normal.y));
hitInfo.hitPos = Vector2f(Vector2<cpFloat>(point.x, point.y));
hitInfo.nearestBody = static_cast<ChipmunkRigidBody2D*>(cpShapeGetUserData(shape));
callback(hitInfo);
@@ -234,8 +234,8 @@ namespace Nz
RaycastHit hitInfo;
hitInfo.fraction = float(alpha);
hitInfo.hitNormal.Set(Vector2<cpFloat>(normal.x, normal.y));
hitInfo.hitPos.Set(Vector2<cpFloat>(point.x, point.y));
hitInfo.hitNormal = Vector2f(Vector2<cpFloat>(normal.x, normal.y));
hitInfo.hitPos = Vector2f(Vector2<cpFloat>(point.x, point.y));
hitInfo.nearestBody = static_cast<ChipmunkRigidBody2D*>(cpShapeGetUserData(shape));
results->emplace_back(std::move(hitInfo));
@@ -260,8 +260,8 @@ namespace Nz
if (cpSpaceSegmentQueryFirst(m_handle, { from.x, from.y }, { to.x, to.y }, radius, filter, &queryInfo))
{
hitInfo->fraction = float(queryInfo.alpha);
hitInfo->hitNormal.Set(Vector2<cpFloat>(queryInfo.normal.x, queryInfo.normal.y));
hitInfo->hitPos.Set(Vector2<cpFloat>(queryInfo.point.x, queryInfo.point.y));
hitInfo->hitNormal = Vector2f(Vector2<cpFloat>(queryInfo.normal.x, queryInfo.normal.y));
hitInfo->hitPos = Vector2f(Vector2<cpFloat>(queryInfo.point.x, queryInfo.point.y));
hitInfo->nearestBody = static_cast<ChipmunkRigidBody2D*>(cpShapeGetUserData(queryInfo.shape));
return true;

View File

@@ -149,7 +149,7 @@ namespace Nz
float resultDistance = float(result.distance);
if (resultDistance < minDistance)
{
closest.Set(float(result.point.x), float(result.point.y));
closest = Vector2f(float(result.point.x), float(result.point.y));
minDistance = resultDistance;
}
}

View File

@@ -82,7 +82,7 @@ namespace Nz
{
NAZARA_USE_ANONYMOUS_NAMESPACE
fprintf(stderr, "%s: ", s_errorTypes[type]);
fprintf(stderr, "%s: ", s_errorTypes[type].data());
fwrite(error.data(), sizeof(char), error.size(), stdout);
if (line != 0 && file && function)

View File

@@ -206,7 +206,7 @@ namespace Nz
m_vertices[offset].position = glyph.corners[cornerIndex];
m_vertices[offset].position.y = bounds.height - m_vertices[offset].position.y;
m_vertices[offset].position *= scale;
m_vertices[offset].uv.Set(uvRect.GetCorner((glyph.flipped) ? flippedCorners[cornerIndex] : normalCorners[cornerIndex]));
m_vertices[offset].uv = uvRect.GetCorner((glyph.flipped) ? flippedCorners[cornerIndex] : normalCorners[cornerIndex]);
offset++;
}

View File

@@ -143,9 +143,9 @@ namespace Nz
Vector3f tileLeftBottom;
if (m_isometricModeEnabled)
tileLeftBottom.Set(x * m_tileSize.x + m_tileSize.x / 2.f * (y % 2), topCorner - y / 2.f * m_tileSize.y, 0.f);
tileLeftBottom = Vector3f(x * m_tileSize.x + m_tileSize.x / 2.f * (y % 2), topCorner - y / 2.f * m_tileSize.y, 0.f);
else
tileLeftBottom.Set(x * m_tileSize.x, topCorner - y * m_tileSize.y, 0.f);
tileLeftBottom = Vector3f(x * m_tileSize.x, topCorner - y * m_tileSize.y, 0.f);
for (RectCorner corner : { RectCorner::LeftBottom, RectCorner::RightBottom, RectCorner::LeftTop, RectCorner::RightTop })
{

View File

@@ -120,7 +120,7 @@ namespace Nz
triangles.emplace_back(triangle.y, b, a);
triangles.emplace_back(triangle.z, c, b);
triangle.Set(a, b, c); // Reuse triangle
triangle = { a, b, c }; // Reuse triangle
}
}
@@ -747,7 +747,7 @@ namespace Nz
Vector3f halfLengths = lengths/2.f;
// Face +X
transform.MakeTransform(Vector3f::UnitX() * halfLengths.x, EulerAnglesf(-90.f, -90.f, 180.f));
transform = Matrix4f::Transform(Vector3f::UnitX() * halfLengths.x, EulerAnglesf(-90.f, -90.f, 180.f));
GeneratePlane(Vector2ui(subdivision.z, subdivision.y), Vector2f(lengths.z, lengths.y), Matrix4f::ConcatenateTransform(matrix, transform), textureCoords, vertexPointers, indices, nullptr, indexOffset);
indexOffset += xVertexCount;
indices += xIndexCount;
@@ -764,7 +764,7 @@ namespace Nz
vertexPointers.uvPtr += xVertexCount;
// Face +Y
transform.MakeTransform(Vector3f::UnitY() * halfLengths.y, EulerAnglesf(0.f, 0.f, 0.f));
transform = Matrix4f::Transform(Vector3f::UnitY() * halfLengths.y, EulerAnglesf(0.f, 0.f, 0.f));
GeneratePlane(Vector2ui(subdivision.x, subdivision.z), Vector2f(lengths.x, lengths.z), Matrix4f::ConcatenateTransform(matrix, transform), textureCoords, vertexPointers, indices, nullptr, indexOffset);
indexOffset += yVertexCount;
indices += yIndexCount;
@@ -781,7 +781,7 @@ namespace Nz
vertexPointers.uvPtr += yVertexCount;
// Face +Z
transform.MakeTransform(Vector3f::UnitZ() * halfLengths.z, EulerAnglesf(90.f, 0.f, 0.f));
transform = Matrix4f::Transform(Vector3f::UnitZ() * halfLengths.z, EulerAnglesf(90.f, 0.f, 0.f));
GeneratePlane(Vector2ui(subdivision.x, subdivision.y), Vector2f(lengths.x, lengths.y), Matrix4f::ConcatenateTransform(matrix, transform), textureCoords, vertexPointers, indices, nullptr, indexOffset);
indexOffset += zVertexCount;
indices += zIndexCount;
@@ -798,7 +798,7 @@ namespace Nz
vertexPointers.uvPtr += zVertexCount;
// Face -X
transform.MakeTransform(-Vector3f::UnitX() * halfLengths.x, EulerAnglesf(-90.f, 90.f, 180.f));
transform = Matrix4f::Transform(-Vector3f::UnitX() * halfLengths.x, EulerAnglesf(-90.f, 90.f, 180.f));
GeneratePlane(Vector2ui(subdivision.z, subdivision.y), Vector2f(lengths.z, lengths.y), Matrix4f::ConcatenateTransform(matrix, transform), textureCoords, vertexPointers, indices, nullptr, indexOffset);
indexOffset += xVertexCount;
indices += xIndexCount;
@@ -815,7 +815,7 @@ namespace Nz
vertexPointers.uvPtr += xVertexCount;
// Face -Y
transform.MakeTransform(-Vector3f::UnitY() * halfLengths.y, EulerAnglesf(0.f, 180.f, 180.f));
transform = Matrix4f::Transform(-Vector3f::UnitY() * halfLengths.y, EulerAnglesf(0.f, 180.f, 180.f));
GeneratePlane(Vector2ui(subdivision.x, subdivision.z), Vector2f(lengths.x, lengths.z), Matrix4f::ConcatenateTransform(matrix, transform), textureCoords, vertexPointers, indices, nullptr, indexOffset);
indexOffset += yVertexCount;
indices += yIndexCount;
@@ -832,7 +832,7 @@ namespace Nz
vertexPointers.uvPtr += yVertexCount;
// Face -Z
transform.MakeTransform(-Vector3f::UnitZ() * halfLengths.z, EulerAnglesf(90.f, 180.f, 0.f));
transform = Matrix4f::Transform(-Vector3f::UnitZ() * halfLengths.z, EulerAnglesf(90.f, 180.f, 0.f));
GeneratePlane(Vector2ui(subdivision.x, subdivision.y), Vector2f(lengths.x, lengths.y), Matrix4f::ConcatenateTransform(matrix, transform), textureCoords, vertexPointers, indices, nullptr, indexOffset);
indexOffset += zVertexCount;
indices += zIndexCount;

View File

@@ -197,7 +197,7 @@ namespace Nz
Vector2f uv(texC.u, texC.v);
uv *= invSkinSize;
uvPtr[triangles[i].vertices[fixedIndex]].Set(parameters.texCoordOffset + uv * parameters.texCoordScale);
uvPtr[triangles[i].vertices[fixedIndex]] = parameters.texCoordOffset + uv * parameters.texCoordScale;
}
}
}

View File

@@ -83,7 +83,7 @@ namespace Nz
sequenceJoint.rotation = rotationQuat * frames[frameIndex].joints[jointIndex].orient;
}
sequenceJoint.scale.Set(1.f);
sequenceJoint.scale = Vector3f::Unit();
}
}

View File

@@ -10,7 +10,7 @@ namespace Nz
{
namespace
{
const unsigned int s_guillotineAtlasStartSize = 512;
constexpr Vector2ui s_guillotineAtlasStartSize(512);
}
GuillotineImageAtlas::GuillotineImageAtlas() :
@@ -119,7 +119,7 @@ namespace Nz
// Dernière couche, et le glyphe ne rentre pas, peut-on agrandir la taille de l'image ?
Vector2ui newSize = layer.binPack.GetSize()*2;
if (newSize == Vector2ui::Zero())
newSize.Set(s_guillotineAtlasStartSize);
newSize = s_guillotineAtlasStartSize;
// Limit image atlas size to prevent allocating too much contiguous memory blocks
if (newSize.x <= m_maxLayerSize && newSize.y <= m_maxLayerSize && ResizeLayer(layer, newSize))
@@ -133,7 +133,7 @@ namespace Nz
else
{
// On ne peut plus agrandir la dernière couche, il est temps d'en créer une nouvelle
newSize.Set(s_guillotineAtlasStartSize);
newSize = s_guillotineAtlasStartSize;
Layer newLayer;
if (!ResizeLayer(newLayer, newSize))

View File

@@ -367,7 +367,7 @@ namespace Nz
{
EnsureDerivedUpdate();
m_transformMatrix.MakeTransform(m_derivedPosition, m_derivedRotation, m_derivedScale);
m_transformMatrix = Matrix4f::Transform(m_derivedPosition, m_derivedRotation, m_derivedScale);
m_transformMatrixUpdated = true;
}
}

View File

@@ -330,7 +330,7 @@ namespace Nz
lastLine.bounds.width -= lastLine.bounds.GetMaximum().x - glyphPosition;
// Regenerate bounds
m_bounds.MakeZero();
m_bounds = Rectf::Zero();
for (auto& line : m_lines)
m_bounds.ExtendTo(line.bounds);
}
@@ -362,10 +362,10 @@ namespace Nz
float italicTop = italic * glyph.bounds.y;
float italicBottom = italic * glyph.bounds.GetMaximum().y;
glyph.corners[0].Set(glyph.bounds.x - italicTop - outlineThickness, glyph.bounds.y - outlineThickness);
glyph.corners[1].Set(glyph.bounds.x + glyph.bounds.width - italicTop - outlineThickness, glyph.bounds.y - outlineThickness);
glyph.corners[2].Set(glyph.bounds.x - italicBottom - outlineThickness, glyph.bounds.y + glyph.bounds.height - outlineThickness);
glyph.corners[3].Set(glyph.bounds.x + glyph.bounds.width - italicBottom - outlineThickness, glyph.bounds.y + glyph.bounds.height - outlineThickness);
glyph.corners[0] = Vector2f(glyph.bounds.x - italicTop - outlineThickness, glyph.bounds.y - outlineThickness);
glyph.corners[1] = Vector2f(glyph.bounds.x + glyph.bounds.width - italicTop - outlineThickness, glyph.bounds.y - outlineThickness);
glyph.corners[2] = Vector2f(glyph.bounds.x - italicBottom - outlineThickness, glyph.bounds.y + glyph.bounds.height - outlineThickness);
glyph.corners[3] = Vector2f(glyph.bounds.x + glyph.bounds.width - italicBottom - outlineThickness, glyph.bounds.y + glyph.bounds.height - outlineThickness);
if (advance)
*advance = fontGlyph.advance;
@@ -460,10 +460,10 @@ namespace Nz
glyph.atlas = nullptr;
glyph.bounds = Rectf(m_drawPos.x, m_lines.back().bounds.y, advance, lineHeight);
glyph.corners[0].Set(glyph.bounds.GetCorner(RectCorner::LeftTop));
glyph.corners[1].Set(glyph.bounds.GetCorner(RectCorner::RightTop));
glyph.corners[2].Set(glyph.bounds.GetCorner(RectCorner::LeftBottom));
glyph.corners[3].Set(glyph.bounds.GetCorner(RectCorner::RightBottom));
glyph.corners[0] = glyph.bounds.GetCorner(RectCorner::LeftTop);
glyph.corners[1] = glyph.bounds.GetCorner(RectCorner::RightTop);
glyph.corners[2] = glyph.bounds.GetCorner(RectCorner::LeftBottom);
glyph.corners[3] = glyph.bounds.GetCorner(RectCorner::RightBottom);
}
m_lines.back().bounds.ExtendTo(glyph.bounds);
@@ -566,7 +566,7 @@ namespace Nz
else
m_lines.emplace_back(Line{ Rectf::Zero(), 0 });
m_drawPos.Set(0, float(firstBlock.characterSize));
m_drawPos = Vector2f(0.f, SafeCast<float>(firstBlock.characterSize));
for (const Block& block : m_blocks)
{

View File

@@ -119,7 +119,7 @@ namespace Nz
lastLine.bounds.width -= lastLine.bounds.GetMaximum().x - glyphPosition;
// Regenerate bounds
m_bounds.MakeZero();
m_bounds = Rectf::Zero();
for (auto& line : m_lines)
m_bounds.ExtendTo(line.bounds);
}
@@ -127,9 +127,9 @@ namespace Nz
void SimpleTextDrawer::ClearGlyphs() const
{
m_bounds.MakeZero();
m_bounds = Rectf::Zero();
m_colorUpdated = true;
m_drawPos.Set(0, float(m_characterSize)); //< Our draw "cursor"
m_drawPos = Vector2f(0.f, SafeCast<float>(m_characterSize)); //< Our draw "cursor"
m_lastSeparatorGlyph = InvalidGlyph;
m_lines.clear();
m_glyphs.clear();
@@ -168,10 +168,10 @@ namespace Nz
float italicTop = italic * glyph.bounds.y;
float italicBottom = italic * glyph.bounds.GetMaximum().y;
glyph.corners[0].Set(glyph.bounds.x - italicTop - outlineThickness, glyph.bounds.y - outlineThickness);
glyph.corners[1].Set(glyph.bounds.x + glyph.bounds.width - italicTop - outlineThickness, glyph.bounds.y - outlineThickness);
glyph.corners[2].Set(glyph.bounds.x - italicBottom - outlineThickness, glyph.bounds.y + glyph.bounds.height - outlineThickness);
glyph.corners[3].Set(glyph.bounds.x + glyph.bounds.width - italicBottom - outlineThickness, glyph.bounds.y + glyph.bounds.height - outlineThickness);
glyph.corners[0] = Vector2f(glyph.bounds.x - italicTop - outlineThickness, glyph.bounds.y - outlineThickness);
glyph.corners[1] = Vector2f(glyph.bounds.x + glyph.bounds.width - italicTop - outlineThickness, glyph.bounds.y - outlineThickness);
glyph.corners[2] = Vector2f(glyph.bounds.x - italicBottom - outlineThickness, glyph.bounds.y + glyph.bounds.height - outlineThickness);
glyph.corners[3] = Vector2f(glyph.bounds.x + glyph.bounds.width - italicBottom - outlineThickness, glyph.bounds.y + glyph.bounds.height - outlineThickness);
if (advance)
*advance = fontGlyph.advance;
@@ -249,10 +249,10 @@ namespace Nz
glyph.atlas = nullptr;
glyph.bounds = Rectf(m_drawPos.x, m_lines.back().bounds.y, advance, GetLineHeight(sizeInfo));
glyph.corners[0].Set(glyph.bounds.GetCorner(RectCorner::LeftTop));
glyph.corners[1].Set(glyph.bounds.GetCorner(RectCorner::RightTop));
glyph.corners[2].Set(glyph.bounds.GetCorner(RectCorner::LeftBottom));
glyph.corners[3].Set(glyph.bounds.GetCorner(RectCorner::RightBottom));
glyph.corners[0] = glyph.bounds.GetCorner(RectCorner::LeftTop);
glyph.corners[1] = glyph.bounds.GetCorner(RectCorner::RightTop);
glyph.corners[2] = glyph.bounds.GetCorner(RectCorner::LeftBottom);
glyph.corners[3] = glyph.bounds.GetCorner(RectCorner::RightBottom);
}
m_lines.back().bounds.ExtendTo(glyph.bounds);

View File

@@ -30,7 +30,7 @@ namespace Nz
return;
for (UInt32 i = 0; i < vertexCount; ++i)
normals[i].MakeZero();
normals[i] = Vector3f::Zero();
TriangleIterator iterator(*this);
do
@@ -66,8 +66,8 @@ namespace Nz
for (UInt32 i = 0; i < vertexCount; ++i)
{
normals[i].MakeZero();
tangents[i].MakeZero();
normals[i] = Vector3f::Zero();
tangents[i] = Vector3f::Zero();
}
TriangleIterator iterator(*this);

View File

@@ -56,8 +56,8 @@ namespace Nz
textDrawer.Clear();
UpdateTextSprite();
m_cursorPositionBegin.MakeZero();
m_cursorPositionEnd.MakeZero();
m_cursorPositionBegin = Vector2ui::Zero();
m_cursorPositionEnd = Vector2ui::Zero();
RefreshCursor();
}

View File

@@ -33,8 +33,8 @@ namespace Nz
UInt32 renderMask = GetRenderMask();
SlicedSprite::Corner corner;
corner.size.Set(config.cornerSize);
corner.textureCoords.Set(config.cornerTexCoords);
corner.size = Vector2f(config.cornerSize);
corner.textureCoords = Vector2f(config.cornerTexCoords);
m_sprite = std::make_shared<SlicedSprite>(m_material);
m_sprite->SetCorners(corner, corner);
@@ -122,8 +122,8 @@ namespace Nz
UInt32 renderMask = GetRenderMask();
SlicedSprite::Corner corner;
corner.size.Set(config.backgroundCornerSize);
corner.textureCoords.Set(config.backgroundCornerTexCoords);
corner.size = Vector2f(config.backgroundCornerSize);
corner.textureCoords = Vector2f(config.backgroundCornerTexCoords);
m_backgroundSprite = std::make_shared<SlicedSprite>(m_material);
m_backgroundSprite->SetCorners(corner, corner);
@@ -447,8 +447,8 @@ namespace Nz
UInt32 renderMask = GetRenderMask();
SlicedSprite::Corner corner;
corner.size.Set(config.cornerSize);
corner.textureCoords.Set(config.cornerTexCoords);
corner.size = Vector2f(config.cornerSize);
corner.textureCoords = Vector2f(config.cornerTexCoords);
m_sprite = std::make_shared<SlicedSprite>(m_material);
m_sprite->SetCorners(corner, corner);