From 133d379f2917edc5e45877082ec7b4152d195afe Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 24 May 2024 10:33:56 +0800 Subject: [PATCH] cleanup math related codes. --- Source/GUI/ImGuiBinding.h | 4 +- Source/Node/DragonBone.cpp | 4 +- Source/Node/Model.cpp | 4 +- Source/Node/Node.cpp | 48 +++++++++++------------- Source/Support/Geometry.cpp | 73 +++++++++++++++++-------------------- Source/Support/Geometry.h | 18 ++++----- Tools/tolua++/ImGui.h | 2 +- 7 files changed, 71 insertions(+), 82 deletions(-) diff --git a/Source/GUI/ImGuiBinding.h b/Source/GUI/ImGuiBinding.h index 9031644ad..ee01247c7 100644 --- a/Source/GUI/ImGuiBinding.h +++ b/Source/GUI/ImGuiBinding.h @@ -407,7 +407,7 @@ bool DragInt2( float v_speed, int v_min, int v_max, - const char* display_format = "%.0f", + const char* display_format = "%d", Slice* flags = nullptr, int flagCount = 0); @@ -417,7 +417,7 @@ bool DragInt2( float v_speed, int v_min, int v_max, - const std::string& display_format = "%.0f"s, + const std::string& display_format = "%d"s, uint32_t sliderFlags = 0); // bool InputFloat( diff --git a/Source/Node/DragonBone.cpp b/Source/Node/DragonBone.cpp index 9d434fcef..f4f9ee374 100644 --- a/Source/Node/DragonBone.cpp +++ b/Source/Node/DragonBone.cpp @@ -53,7 +53,7 @@ const Matrix& DBSlotNode::getWorld() { if (_flags.isOn(DBSlotNode::TransformDirty) || _flags.isOn(Node::WorldDirty)) { _flags.setOff(DBSlotNode::TransformDirty); Matrix mat; - AffineTransform::toMatrix(_transform, mat); + _transform.toMatrix(mat); Matrix::mulMtx(_matrix, Node::getWorld(), mat); } return _matrix; @@ -548,7 +548,7 @@ void DragonBone::render() { for (int i = 0; i < vertSize; i++) { float x = verts[i * 2] * scale; float y = verts[i * 2 + 1] * scale; - vertices[i] = AffineTransform::applyPoint(*transform, {x, y}); + vertices[i] = transform->applyPoint({x, y}); } vertices[vertSize] = vertices[0]; _debugLine->add(vertices, Color(0xff00ffff)); diff --git a/Source/Node/Model.cpp b/Source/Node/Model.cpp index d1d931f65..529f9a8b9 100644 --- a/Source/Node/Model.cpp +++ b/Source/Node/Model.cpp @@ -335,7 +335,7 @@ Rect Model::getBoundingBox() { Rect box = child->getBoundingBox(); if (box.size != Size::zero) { for (Node* parent = child->getParent(); parent != this; parent = parent->getParent()) { - box = AffineTransform::applyRect(parent->getLocalTransform(), box); + box = parent->getLocalTransform().applyRect(box); } if (firstBox) { firstBox = false; @@ -352,7 +352,7 @@ Rect Model::getBoundingBox() { }); _flags.set(Node::TraverseEnabled, traverseEnabled); Rect rect(lower.x, lower.y, upper.x - lower.x, upper.y - lower.y); - return AffineTransform::applyRect(getLocalTransform(), rect); + return getLocalTransform().applyRect(rect); } Model* Model::dummy() { diff --git a/Source/Node/Node.cpp b/Source/Node/Node.cpp index 1d3060bae..09a3cc713 100644 --- a/Source/Node/Node.cpp +++ b/Source/Node/Node.cpp @@ -351,7 +351,7 @@ Node* Node::getTargetParent() const { Rect Node::getBoundingBox() { Rect rect(0, 0, _size.width, _size.height); - return AffineTransform::applyRect(getLocalTransform(), rect); + return getLocalTransform().applyRect(rect); } void Node::setRenderOrder(int var) { @@ -899,34 +899,29 @@ const AffineTransform& Node::getLocalTransform() { float c = 1, s = 0; if (_angle) { float radians = -bx::toRad(_angle); - c = std::cos(radians); - s = std::sin(radians); + c = bx::cos(radians); + s = bx::sin(radians); } - if (_skewX || _skewY) { - /* translateXY, rotateZ, scaleXY */ - _transform = {c * _scaleX, s * _scaleX, -s * _scaleY, c * _scaleY, _position.x, _position.y}; + if (_skewX || _skewY) { /* skewXY */ - AffineTransform skewMatrix{ - 1.0f, std::tan(bx::toRad(_skewY)), - std::tan(bx::toRad(_skewX)), 1.0f, + _transform = { + 1.0f, bx::tan(bx::toRad(_skewY)), + bx::tan(bx::toRad(_skewX)), 1.0f, 0.0f, 0.0f}; - _transform = AffineTransform::concat(skewMatrix, _transform); - /* translateAnchorXY */ - if (_anchorPoint != Vec2::zero) { - _transform = AffineTransform::translate(_transform, -_anchorPoint.x, -_anchorPoint.y); - } + /* scaleXY, rotateZ, translateXY */ + _transform.concat({c * _scaleX, s * _scaleX, -s * _scaleY, c * _scaleY, _position.x, _position.y}); } else { - /* translateXY, scaleXY, rotateZ, translateAnchorXY */ - float x = _position.x; - float y = _position.y; - if (_anchorPoint != Vec2::zero) { - x += c * -_anchorPoint.x * _scaleX + -s * -_anchorPoint.y * _scaleY; - y += s * -_anchorPoint.x * _scaleX + c * -_anchorPoint.y * _scaleY; - } - _transform = {c * _scaleX, s * _scaleX, -s * _scaleY, c * _scaleY, x, y}; + /* scaleXY, rotateZ, translateXY */ + _transform = {c * _scaleX, s * _scaleX, -s * _scaleY, c * _scaleY, _position.x, _position.y}; } + + /* translateAnchorXY */ + if (_anchorPoint != Vec2::zero) { + _transform.translate(-_anchorPoint.x, -_anchorPoint.y); + } + _flags.setOff(Node::TransformDirty); } return _transform; @@ -934,14 +929,14 @@ const AffineTransform& Node::getLocalTransform() { void Node::getLocalWorld(Matrix& localWorld) { if (_angleX || _angleY) { - AffineTransform transform = getLocalTransform(); /* translateXY, scaleXY, rotateZ, translateAnchorXY */ if (_anchorPoint != Vec2::zero) { + AffineTransform transform = getLocalTransform(); Matrix mtxRoted; { /* -translateAnchorXY */ Matrix mtxBase; - AffineTransform::toMatrix(AffineTransform::translate(transform, _anchorPoint.x, _anchorPoint.y), mtxBase); + transform.translate(_anchorPoint.x, _anchorPoint.y).toMatrix(mtxBase); /* translateZ */ mtxBase.m[14] = _positionZ; @@ -958,7 +953,7 @@ void Node::getLocalWorld(Matrix& localWorld) { Matrix::mulMtx(localWorld, mtxRoted, mtxAnchor); } else { Matrix mtxBase; - AffineTransform::toMatrix(transform, mtxBase); + getLocalTransform().toMatrix(mtxBase); /* translateZ */ mtxBase.m[14] = _positionZ; @@ -970,8 +965,7 @@ void Node::getLocalWorld(Matrix& localWorld) { } } else { /* translateXY, scaleXY, rotateZ, translateAnchorXY */ - AffineTransform transform = getLocalTransform(); - AffineTransform::toMatrix(transform, localWorld); + getLocalTransform().toMatrix(localWorld); /* translateZ */ localWorld.m[14] = _positionZ; diff --git a/Source/Support/Geometry.cpp b/Source/Support/Geometry.cpp index 7f72bc447..918f624b0 100644 --- a/Source/Support/Geometry.cpp +++ b/Source/Support/Geometry.cpp @@ -332,73 +332,68 @@ bool Rect::intersectsRect(const Rect& rect) const { // AffineTransform AffineTransform AffineTransform::Indentity = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0}; -Vec2 AffineTransform::applyPoint(const AffineTransform& t, const Vec2& point) { - const ktm::faffine2d& ktm_affine = r_cast(t); - ktm::fvec2 ret = r_cast(ktm_affine.m) * r_cast(point) + ktm_affine.m[2]; - return r_cast(ret); +Vec2 AffineTransform::applyPoint(const Vec2& point) const { + const ktm::faffine2d& ktm_affine = *r_cast(r_cast(this)); + ktm::fvec2 ret = r_cast(ktm_affine.m) * r_cast(point) + ktm_affine.m[2]; + return r_cast(ret); } -Size AffineTransform::applySize(const AffineTransform& t, const Size& size) { - ktm::fvec2 ret = r_cast(t) * r_cast(size); - return r_cast(ret); +Size AffineTransform::applySize(const Size& size) const { + ktm::fvec2 ret = *r_cast(r_cast(this)) * r_cast(size); + return r_cast(ret); } -Rect AffineTransform::applyRect(const AffineTransform& t, const Rect& rect) { +Rect AffineTransform::applyRect(const Rect& rect) const { float bottom = rect.getBottom(); float left = rect.getLeft(); float right = rect.getRight(); float top = rect.getTop(); - Vec2 topLeft = applyPoint(t, Vec2{left, top}); - Vec2 topRight = applyPoint(t, Vec2{right, top}); - Vec2 bottomLeft = applyPoint(t, Vec2{left, bottom}); - Vec2 bottomRight = applyPoint(t, Vec2{right, bottom}); + Vec2 topLeft = applyPoint({left, top}); + Vec2 topRight = applyPoint({right, top}); + Vec2 bottomLeft = applyPoint({left, bottom}); + Vec2 bottomRight = applyPoint({right, bottom}); - float minX = ktm::reduce_min(ktm::fvec4{topLeft.x, topRight.x, bottomLeft.x, bottomRight.x}); - float maxX = ktm::reduce_max(ktm::fvec4{topLeft.x, topRight.x, bottomLeft.x, bottomRight.x}); - float minY = ktm::reduce_min(ktm::fvec4{topLeft.y, topRight.y, bottomLeft.y, bottomRight.y}); - float maxY = ktm::reduce_max(ktm::fvec4{topLeft.y, topRight.y, bottomLeft.y, bottomRight.y}); + float minX = ktm::reduce_min(ktm::fvec4{topLeft.x, topRight.x, bottomLeft.x, bottomRight.x}); + float maxX = ktm::reduce_max(ktm::fvec4{topLeft.x, topRight.x, bottomLeft.x, bottomRight.x}); + float minY = ktm::reduce_min(ktm::fvec4{topLeft.y, topRight.y, bottomLeft.y, bottomRight.y}); + float maxY = ktm::reduce_max(ktm::fvec4{topLeft.y, topRight.y, bottomLeft.y, bottomRight.y}); return Rect(minX, minY, (maxX - minX), (maxY - minY)); } -AffineTransform AffineTransform::translate(const AffineTransform& t, float tx, float ty) { - AffineTransform affine = t; - r_cast(affine).translate(tx, ty); - return affine; +AffineTransform& AffineTransform::translate(float tx, float ty) { + r_cast(r_cast(this))->translate(tx, ty); + return *this; } -AffineTransform AffineTransform::rotate(const AffineTransform& t, float angle) { - AffineTransform affine = t; - r_cast(affine).rotate(angle); - return affine; +AffineTransform& AffineTransform::rotate(float angle) { + r_cast(r_cast(this))->rotate(angle); + return *this; } -AffineTransform AffineTransform::scale(const AffineTransform& t, float sx, float sy) { - AffineTransform affine = t; - r_cast(affine).scale(sx, sy); - return affine; +AffineTransform& AffineTransform::scale(float sx, float sy) { + r_cast(r_cast(this))->scale(sx, sy); + return *this; } -AffineTransform AffineTransform::concat(const AffineTransform& t1, const AffineTransform& t2) { - AffineTransform affine = t1; - r_cast(affine).concat(r_cast(t2)); - return affine; +AffineTransform& AffineTransform::concat(const AffineTransform& t2) { + r_cast(r_cast(this))->concat(r_cast(t2)); + return *this; } -AffineTransform AffineTransform::invert(const AffineTransform& t) { - AffineTransform affine = t; - r_cast(affine).invert(); - return affine; +AffineTransform& AffineTransform::invert() { + r_cast(r_cast(this))->invert(); + return *this; } -void AffineTransform::toMatrix(const AffineTransform& t, float* m) { +void AffineTransform::toMatrix(float* m) const { // | m[0] m[4] m[8] m[12] | | m11 m21 m31 m41 | | a c 0 tx | // | m[1] m[5] m[9] m[13] | | m12 m22 m32 m42 | | b d 0 ty | // | m[2] m[6] m[10] m[14] | => | m13 m23 m33 m43 | => | 0 0 1 0 | // | m[3] m[7] m[11] m[15] | | m14 m24 m34 m44 | | 0 0 0 1 | - ktm::fmat4x4& mat = *r_cast(r_cast(m)); - r_cast(t) >> mat; + ktm::fmat4x4& mat = *r_cast(r_cast(m)); + *r_cast(r_cast(this)) >> mat; } const Matrix Matrix::Indentity = { diff --git a/Source/Support/Geometry.h b/Source/Support/Geometry.h index 515ba5b2f..3e5d9f78c 100644 --- a/Source/Support/Geometry.h +++ b/Source/Support/Geometry.h @@ -97,15 +97,15 @@ struct Rect { struct AffineTransform { float a, b, c, d; float tx, ty; - static Vec2 applyPoint(const AffineTransform& t, const Vec2& point); - static Size applySize(const AffineTransform& t, const Size& size); - static Rect applyRect(const AffineTransform& t, const Rect& size); - static AffineTransform translate(const AffineTransform& t, float tx, float ty); - static AffineTransform rotate(const AffineTransform& t, float angle); - static AffineTransform scale(const AffineTransform& t, float sx, float sy); - static AffineTransform concat(const AffineTransform& t1, const AffineTransform& t2); - static AffineTransform invert(const AffineTransform& t); - static void toMatrix(const AffineTransform& t, float* matrix); + Vec2 applyPoint(const Vec2& point) const; + Size applySize(const Size& size) const; + Rect applyRect(const Rect& size) const; + AffineTransform& translate(float tx, float ty); + AffineTransform& rotate(float angle); + AffineTransform& scale(float sx, float sy); + AffineTransform& concat(const AffineTransform& t2); + AffineTransform& invert(); + void toMatrix(float* matrix) const; static AffineTransform Indentity; }; diff --git a/Tools/tolua++/ImGui.h b/Tools/tolua++/ImGui.h index bb691927a..4e894cffd 100644 --- a/Tools/tolua++/ImGui.h +++ b/Tools/tolua++/ImGui.h @@ -104,7 +104,7 @@ namespace ImGui bool Binding::DragFloat2 @ DragFloat2(CString label, float* v1, float* v2, float v_speed, float v_min, float v_max, CString display_format, String flags[tolua_len]); bool Binding::DragInt @ DragInt(CString label, int* v, float v_speed, int v_min, int v_max, CString display_format = "%d"); bool Binding::DragInt @ DragInt(CString label, int* v, float v_speed, int v_min, int v_max, CString display_format, String flags[tolua_len]); - bool Binding::DragInt2 @ DragInt2(CString label, int* v1, int* v2, float v_speed = 1.0f, int v_min = 0, int v_max = 0, CString display_format = "%.0f"); + bool Binding::DragInt2 @ DragInt2(CString label, int* v1, int* v2, float v_speed = 1.0f, int v_min = 0, int v_max = 0, CString display_format = "%d"); bool Binding::DragInt2 @ DragInt2(CString label, int* v1, int* v2, float v_speed, int v_min, int v_max, CString display_format, String flags[tolua_len]); bool Binding::InputFloat @ InputFloat(CString label, float* v, float step = 0.0f, float step_fast = 0.0f, CString format = "%.3f"); bool Binding::InputFloat @ InputFloat(CString label, float* v, float step, float step_fast, CString format, String flags[tolua_len]);