add: Node::GetBoundingBox function

minor

minor

minor
This commit is contained in:
Nomango 2019-01-27 13:08:56 +08:00 committed by Nomango
parent 1e16f9d815
commit e9127832d7
15 changed files with 228 additions and 135 deletions

View File

@ -73,7 +73,7 @@
<ClInclude Include="..\..\src\easy2d.h" />
<ClInclude Include="..\..\src\math\constants.hpp" />
<ClInclude Include="..\..\src\math\ease.hpp" />
<ClInclude Include="..\..\src\math\Matrix.hpp" />
<ClInclude Include="..\..\src\math\Matrix.h" />
<ClInclude Include="..\..\src\math\rand.h" />
<ClInclude Include="..\..\src\math\Rect.hpp" />
<ClInclude Include="..\..\src\math\scalar.hpp" />
@ -123,6 +123,7 @@
<ClCompile Include="..\..\src\core\time.cpp" />
<ClCompile Include="..\..\src\core\Transition.cpp" />
<ClCompile Include="..\..\src\core\window.cpp" />
<ClCompile Include="..\..\src\math\Matrix.cpp" />
<ClCompile Include="..\..\src\math\rand.cpp" />
<ClCompile Include="..\..\src\ui\Button.cpp" />
<ClCompile Include="..\..\src\ui\Menu.cpp" />

View File

@ -62,9 +62,6 @@
<ClInclude Include="..\..\src\core\window.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\math\Matrix.hpp">
<Filter>math</Filter>
</ClInclude>
<ClInclude Include="..\..\src\math\rand.h">
<Filter>math</Filter>
</ClInclude>
@ -203,6 +200,9 @@
<ClInclude Include="..\..\src\core\ActionHelper.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\math\Matrix.h">
<Filter>math</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="core">
@ -351,5 +351,8 @@
<ClCompile Include="..\..\src\core\ActionGroup.cpp">
<Filter>core</Filter>
</ClCompile>
<ClCompile Include="..\..\src\math\Matrix.cpp">
<Filter>math</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -73,7 +73,7 @@
<ClInclude Include="..\..\src\easy2d.h" />
<ClInclude Include="..\..\src\math\constants.hpp" />
<ClInclude Include="..\..\src\math\ease.hpp" />
<ClInclude Include="..\..\src\math\Matrix.hpp" />
<ClInclude Include="..\..\src\math\Matrix.h" />
<ClInclude Include="..\..\src\math\rand.h" />
<ClInclude Include="..\..\src\math\Rect.hpp" />
<ClInclude Include="..\..\src\math\scalar.hpp" />
@ -123,6 +123,7 @@
<ClCompile Include="..\..\src\core\time.cpp" />
<ClCompile Include="..\..\src\core\Transition.cpp" />
<ClCompile Include="..\..\src\core\window.cpp" />
<ClCompile Include="..\..\src\math\Matrix.cpp" />
<ClCompile Include="..\..\src\math\rand.cpp" />
<ClCompile Include="..\..\src\ui\Button.cpp" />
<ClCompile Include="..\..\src\ui\Menu.cpp" />

View File

@ -62,9 +62,6 @@
<ClInclude Include="..\..\src\core\window.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\math\Matrix.hpp">
<Filter>math</Filter>
</ClInclude>
<ClInclude Include="..\..\src\math\rand.h">
<Filter>math</Filter>
</ClInclude>
@ -203,6 +200,9 @@
<ClInclude Include="..\..\src\core\ActionHelper.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\math\Matrix.h">
<Filter>math</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="core">
@ -351,5 +351,8 @@
<ClCompile Include="..\..\src\core\ActionGroup.cpp">
<Filter>core</Filter>
</ClCompile>
<ClCompile Include="..\..\src\math\Matrix.cpp">
<Filter>math</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -73,7 +73,7 @@
<ClInclude Include="..\..\src\easy2d.h" />
<ClInclude Include="..\..\src\math\constants.hpp" />
<ClInclude Include="..\..\src\math\ease.hpp" />
<ClInclude Include="..\..\src\math\Matrix.hpp" />
<ClInclude Include="..\..\src\math\Matrix.h" />
<ClInclude Include="..\..\src\math\rand.h" />
<ClInclude Include="..\..\src\math\Rect.hpp" />
<ClInclude Include="..\..\src\math\scalar.hpp" />
@ -123,6 +123,7 @@
<ClCompile Include="..\..\src\core\time.cpp" />
<ClCompile Include="..\..\src\core\Transition.cpp" />
<ClCompile Include="..\..\src\core\window.cpp" />
<ClCompile Include="..\..\src\math\Matrix.cpp" />
<ClCompile Include="..\..\src\math\rand.cpp" />
<ClCompile Include="..\..\src\ui\Button.cpp" />
<ClCompile Include="..\..\src\ui\Menu.cpp" />

View File

@ -62,9 +62,6 @@
<ClInclude Include="..\..\src\core\window.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\math\Matrix.hpp">
<Filter>math</Filter>
</ClInclude>
<ClInclude Include="..\..\src\math\rand.h">
<Filter>math</Filter>
</ClInclude>
@ -203,6 +200,9 @@
<ClInclude Include="..\..\src\core\ActionHelper.h">
<Filter>core</Filter>
</ClInclude>
<ClInclude Include="..\..\src\math\Matrix.h">
<Filter>math</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="core">
@ -351,5 +351,8 @@
<ClCompile Include="..\..\src\core\ActionGroup.cpp">
<Filter>core</Filter>
</ClCompile>
<ClCompile Include="..\..\src\math\Matrix.cpp">
<Filter>math</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -500,6 +500,11 @@ namespace easy2d
return Rect(Point{}, size_);
}
Rect Node::GetBoundingBox() const
{
return GetTransformMatrix().Transform(GetBounds());
}
Array<NodePtr> Node::GetChildren(String const& name) const
{
Array<NodePtr> children;

View File

@ -127,6 +127,9 @@ namespace easy2d
// 삿혤관鍋분
Rect GetBounds() const;
// 삿혤棍학관鍋분
Rect GetBoundingBox() const;
// 삿혤랗郭긴뻣앤黎
Matrix const& GetTransformMatrix() const;

View File

@ -99,7 +99,7 @@ namespace easy2d
{
if (image_)
{
RenderSystem::Instance()->DrawImage(image_);
RenderSystem::Instance()->DrawImage(image_, GetBounds());
}
}
}

View File

@ -24,7 +24,7 @@
#include "closure.hpp"
#include "../math/vector.hpp"
#include "../math/Rect.hpp"
#include "../math/Matrix.hpp"
#include "../math/Matrix.h"
#include <set>
#include <map>
#include <list>

View File

@ -187,7 +187,7 @@ namespace easy2d
return S_OK;
}
HRESULT RenderSystem::DrawImage(ImagePtr const & image)
HRESULT RenderSystem::DrawImage(ImagePtr const & image, Rect const& dest_rect)
{
if (!render_target_)
return E_UNEXPECTED;
@ -200,7 +200,7 @@ namespace easy2d
render_target_->DrawBitmap(
image->GetBitmap().Get(),
D2D1::RectF(0.f, 0.f, image->GetWidth(), image->GetHeight()),
dest_rect,
opacity_,
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
image->GetCropRect()

View File

@ -26,7 +26,7 @@
#include "TextRenderer.h"
#include "TextStyle.hpp"
#include "Singleton.hpp"
#include "../math/Matrix.hpp"
#include "../math/Matrix.h"
#include <unordered_map>
namespace easy2d
@ -128,7 +128,8 @@ namespace easy2d
);
HRESULT DrawImage(
ImagePtr const& image
ImagePtr const& image,
Rect const& dest_rect
);
HRESULT DrawBitmap(

View File

@ -91,7 +91,7 @@
#include "math/ease.hpp"
#include "math/vector.hpp"
#include "math/rand.h"
#include "math/Matrix.hpp"
#include "math/Matrix.h"
//

152
src/math/Matrix.cpp Normal file
View File

@ -0,0 +1,152 @@
// Copyright (c) 2016-2018 Easy2D - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "Matrix.h"
namespace easy2d
{
namespace math
{
Matrix::Matrix()
: _11(1.f), _12(0.f)
, _21(0.f), _22(1.f)
, _31(0.f), _32(0.f)
{
}
Matrix::Matrix(float _11, float _12, float _21, float _22, float _31, float _32)
: _11(_11), _12(_12), _21(_21), _22(_22), _31(_31), _32(_32)
{
}
Matrix::Matrix(const float* p)
{
for (int i = 0; i < 6; i++)
m[i] = p[i];
}
Matrix::Matrix(Matrix const& other)
: _11(other._11), _12(other._12)
, _21(other._21), _22(other._22)
, _31(other._31), _32(other._32)
{
}
void Matrix::Identity()
{
_11 = 1.f; _12 = 0.f;
_21 = 0.f; _12 = 1.f;
_31 = 0.f; _32 = 0.f;
}
Rect Matrix::Transform(const Rect & rect) const
{
Vector2 top_left = Transform(rect.GetLeftTop());
Vector2 top_right = Transform(rect.GetRightTop());
Vector2 bottom_left = Transform(rect.GetLeftBottom());
Vector2 bottom_right = Transform(rect.GetRightBottom());
float left = min(min(top_left.x, top_right.x), min(bottom_left.x, bottom_right.x));
float right = max(max(top_left.x, top_right.x), max(bottom_left.x, bottom_right.x));
float top = min(min(top_left.y, top_right.y), min(bottom_left.y, bottom_right.y));
float bottom = max(max(top_left.y, top_right.y), max(bottom_left.y, bottom_right.y));
return Rect{ left, top, (right - left), (bottom - top) };
}
Matrix Matrix::Translation(const Vector2& v)
{
return Matrix(
1.f, 0.f,
0.f, 1.f,
v.x, v.y
);
}
Matrix Matrix::Translation(
float x,
float y)
{
return Translation(Vector2(x, y));
}
Matrix Matrix::Scaling(
const Vector2& v,
const Vector2& center)
{
return Matrix(
v.x, 0.f,
0.f, v.y,
center.x - v.x * center.x,
center.y - v.y * center.y
);
}
Matrix Matrix::Scaling(
float x,
float y,
const Vector2& center)
{
return Scaling(Vector2(x, y), center);
}
Matrix Matrix::Rotation(
float angle,
const Vector2& center)
{
float s = math::Sin(angle);
float c = math::Cos(angle);
return Matrix(
c, s,
-s, c,
center.x * (1 - c) + center.y * s,
center.y * (1 - c) - center.x * s
);
}
Matrix Matrix::Skewing(
float angle_x,
float angle_y,
const Vector2& center)
{
float tx = math::Tan(angle_x);
float ty = math::Tan(angle_y);
return Matrix(
1.f, -ty,
-tx, 1.f,
center.y * tx, center.x * ty
);
}
Matrix Matrix::Invert(Matrix const& matrix)
{
float det = 1.f / matrix.Determinant();
return Matrix(
det * matrix._22,
-det * matrix._12,
-det * matrix._21,
det * matrix._11,
det * (matrix._21 * matrix._32 - matrix._22 * matrix._31),
det * (matrix._12 * matrix._31 - matrix._11 * matrix._32)
);
}
}
}

View File

@ -20,25 +20,13 @@
#pragma once
#include "vector.hpp"
#include "Rect.hpp"
#include <d2d1.h>
namespace easy2d
{
namespace math
{
struct Matrix;
template <typename L, typename R>
struct MatrixMultiply;
inline MatrixMultiply<Matrix, Matrix>
operator *(Matrix const& lhs, Matrix const& rhs);
template <typename L, typename R>
inline MatrixMultiply<MatrixMultiply<L, R>, Matrix>
operator *(MatrixMultiply<L, R> const& lhs, Matrix const& rhs);
struct Matrix
{
union
@ -57,31 +45,13 @@ namespace easy2d
};
};
public:
Matrix()
: _11(1.f), _12(0.f)
, _21(0.f), _22(1.f)
, _31(0.f), _32(0.f)
{
}
Matrix();
Matrix(float _11, float _12, float _21, float _22, float _31, float _32)
: _11(_11), _12(_12), _21(_21), _22(_22), _31(_31), _32(_32)
{
}
Matrix(float _11, float _12, float _21, float _22, float _31, float _32);
Matrix(const float* p)
{
for (int i = 0; i < 6; i++)
m[i] = p[i];
}
Matrix(const float* p);
Matrix(Matrix const& other)
: _11(other._11), _12(other._12)
, _21(other._21), _22(other._22)
, _31(other._31), _32(other._32)
{
}
Matrix(Matrix const& other);
template <typename T>
Matrix(T const& other)
@ -90,6 +60,24 @@ namespace easy2d
m[i] = other[i];
}
void Identity();
inline Vector2 Transform(const Vector2& v) const
{
return Vector2(
v.x * _11 + v.y * _21 + _31,
v.x * _12 + v.y * _22 + _32
);
}
Rect Transform(const Rect& rect) const;
inline void Translate(const Vector2& v)
{
_31 += _11 * v.x + _21 * v.y;
_32 += _12 * v.x + _22 * v.y;
}
inline float operator [](unsigned int index) const
{
return m[index];
@ -103,14 +91,6 @@ namespace easy2d
return *this;
}
inline Matrix& Identity()
{
_11 = 1.f; _12 = 0.f;
_21 = 0.f; _12 = 1.f;
_31 = 0.f; _32 = 0.f;
return *this;
}
inline float Determinant() const
{
return (_11 * _22) - (_12 * _21);
@ -128,20 +108,6 @@ namespace easy2d
return 0 != Determinant();
}
Vector2 Transform(const Vector2& v) const
{
return Vector2(
v.x * _11 + v.y * _21 + _31,
v.x * _12 + v.y * _22 + _32
);
}
void Translate(const Vector2& v)
{
_31 += _11 * v.x + _21 * v.y;
_32 += _12 * v.x + _22 * v.y;
}
inline operator D2D1_MATRIX_3X2_F const& () const
{
return reinterpret_cast<D2D1_MATRIX_3X2_F const&>(*this);
@ -152,86 +118,40 @@ namespace easy2d
return reinterpret_cast<D2D1_MATRIX_3X2_F&>(*this);
}
static Matrix Translation(const Vector2& v)
{
return Matrix(
1.f, 0.f,
0.f, 1.f,
v.x, v.y
);
}
static Matrix Translation(const Vector2& v);
static Matrix Translation(
float x,
float y)
{
return Translation(Vector2(x, y));
}
float y
);
static Matrix Scaling(
const Vector2& v,
const Vector2& center = Vector2())
{
return Matrix(
v.x, 0.f,
0.f, v.y,
center.x - v.x * center.x,
center.y - v.y * center.y
);
}
const Vector2& center = Vector2()
);
static Matrix Scaling(
float x,
float y,
const Vector2& center = Vector2())
{
return Scaling(Vector2(x, y), center);
}
const Vector2& center = Vector2()
);
static Matrix Rotation(
float angle,
const Vector2& center = Vector2())
{
float s = math::Sin(angle);
float c = math::Cos(angle);
return Matrix(
c, s,
-s, c,
center.x * (1 - c) + center.y * s,
center.y * (1 - c) - center.x * s
);
}
const Vector2& center = Vector2()
);
static Matrix Skewing(
float angle_x,
float angle_y,
const Vector2& center = Vector2())
{
float tx = math::Tan(angle_x);
float ty = math::Tan(angle_y);
return Matrix(
1.f, -ty,
-tx, 1.f,
center.y * tx, center.x * ty
);
}
const Vector2& center = Vector2()
);
static Matrix Invert(Matrix const& matrix)
{
float det = 1.f / matrix.Determinant();
return Matrix(
det * matrix._22,
-det * matrix._12,
-det * matrix._21,
det * matrix._11,
det * (matrix._21 * matrix._32 - matrix._22 * matrix._31),
det * (matrix._12 * matrix._31 - matrix._11 * matrix._32)
);
}
static Matrix Invert(Matrix const& matrix);
};
// Use template expression to optimize matrix multiply
template <typename L, typename R>
struct MatrixMultiply
{