Skip to content

Commit

Permalink
cleanup math related codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed May 24, 2024
1 parent 06f33a9 commit 133d379
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 82 deletions.
4 changes: 2 additions & 2 deletions Source/GUI/ImGuiBinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions Source/Node/DragonBone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions Source/Node/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
48 changes: 21 additions & 27 deletions Source/Node/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -899,49 +899,44 @@ 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;
}

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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
73 changes: 34 additions & 39 deletions Source/Support/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const ktm::faffine2d&>(t);
ktm::fvec2 ret = r_cast<const ktm::fmat2x2&>(ktm_affine.m) * r_cast<const ktm::fvec2&>(point) + ktm_affine.m[2];
return r_cast<Vec2&>(ret);
Vec2 AffineTransform::applyPoint(const Vec2& point) const {
const ktm::faffine2d& ktm_affine = *r_cast<const ktm::faffine2d*>(r_cast<const void*>(this));
ktm::fvec2 ret = r_cast<const ktm::fmat2x2&>(ktm_affine.m) * r_cast<const ktm::fvec2&>(point) + ktm_affine.m[2];
return r_cast<Vec2&>(ret);
}

Size AffineTransform::applySize(const AffineTransform& t, const Size& size) {
ktm::fvec2 ret = r_cast<const ktm::fmat2x2&>(t) * r_cast<const ktm::fvec2&>(size);
return r_cast<Size&>(ret);
Size AffineTransform::applySize(const Size& size) const {
ktm::fvec2 ret = *r_cast<const ktm::fmat2x2*>(r_cast<const void*>(this)) * r_cast<const ktm::fvec2&>(size);
return r_cast<Size&>(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<ktm::faffine2d&>(affine).translate(tx, ty);
return affine;
AffineTransform& AffineTransform::translate(float tx, float ty) {
r_cast<ktm::faffine2d*>(r_cast<void*>(this))->translate(tx, ty);
return *this;
}

AffineTransform AffineTransform::rotate(const AffineTransform& t, float angle) {
AffineTransform affine = t;
r_cast<ktm::faffine2d&>(affine).rotate(angle);
return affine;
AffineTransform& AffineTransform::rotate(float angle) {
r_cast<ktm::faffine2d*>(r_cast<void*>(this))->rotate(angle);
return *this;
}

AffineTransform AffineTransform::scale(const AffineTransform& t, float sx, float sy) {
AffineTransform affine = t;
r_cast<ktm::faffine2d&>(affine).scale(sx, sy);
return affine;
AffineTransform& AffineTransform::scale(float sx, float sy) {
r_cast<ktm::faffine2d*>(r_cast<void*>(this))->scale(sx, sy);
return *this;
}

AffineTransform AffineTransform::concat(const AffineTransform& t1, const AffineTransform& t2) {
AffineTransform affine = t1;
r_cast<ktm::faffine2d&>(affine).concat(r_cast<const ktm::faffine2d&>(t2));
return affine;
AffineTransform& AffineTransform::concat(const AffineTransform& t2) {
r_cast<ktm::faffine2d*>(r_cast<void*>(this))->concat(r_cast<const ktm::faffine2d&>(t2));
return *this;
}

AffineTransform AffineTransform::invert(const AffineTransform& t) {
AffineTransform affine = t;
r_cast<ktm::faffine2d&>(affine).invert();
return affine;
AffineTransform& AffineTransform::invert() {
r_cast<ktm::faffine2d*>(r_cast<void*>(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<ktm::fmat4x4*>(r_cast<void*>(m));
r_cast<const ktm::faffine2d&>(t) >> mat;
ktm::fmat4x4& mat = *r_cast<ktm::fmat4x4*>(r_cast<void*>(m));
*r_cast<const ktm::faffine2d*>(r_cast<const void*>(this)) >> mat;
}

const Matrix Matrix::Indentity = {
Expand Down
18 changes: 9 additions & 9 deletions Source/Support/Geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion Tools/tolua++/ImGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 133d379

Please sign in to comment.