1 /**
2  * Copyright (c) 2006-2019 LOVE Development Team
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty.  In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  *    claim that you wrote the original software. If you use this software
14  *    in a product, an acknowledgment in the product documentation would be
15  *    appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  *    misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  **/
20 
21 #include "Transform.h"
22 
23 namespace love
24 {
25 namespace math
26 {
27 
28 love::Type Transform::type("Transform", &Object::type);
29 
Transform()30 Transform::Transform()
31 	: matrix()
32 	, inverseDirty(true)
33 	, inverseMatrix()
34 {
35 }
36 
Transform(const Matrix4 & m)37 Transform::Transform(const Matrix4 &m)
38 	: matrix(m)
39 	, inverseDirty(true)
40 	, inverseMatrix()
41 {
42 }
43 
Transform(float x,float y,float a,float sx,float sy,float ox,float oy,float kx,float ky)44 Transform::Transform(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky)
45 	: matrix(x, y, a, sx, sy, ox, oy, kx, ky)
46 	, inverseDirty(true)
47 	, inverseMatrix()
48 {
49 }
50 
~Transform()51 Transform::~Transform()
52 {
53 }
54 
clone()55 Transform *Transform::clone()
56 {
57 	return new Transform(*this);
58 }
59 
inverse()60 Transform *Transform::inverse()
61 {
62 	return new Transform(getInverseMatrix());
63 }
64 
apply(Transform * other)65 void Transform::apply(Transform *other)
66 {
67 	matrix *= other->getMatrix();
68 	inverseDirty = true;
69 }
70 
translate(float x,float y)71 void Transform::translate(float x, float y)
72 {
73 	matrix.translate(x, y);
74 	inverseDirty = true;
75 }
76 
rotate(float angle)77 void Transform::rotate(float angle)
78 {
79 	matrix.rotate(angle);
80 	inverseDirty = true;
81 }
82 
scale(float x,float y)83 void Transform::scale(float x, float y)
84 {
85 	matrix.scale(x, y);
86 	inverseDirty = true;
87 }
88 
shear(float x,float y)89 void Transform::shear(float x, float y)
90 {
91 	matrix.shear(x, y);
92 	inverseDirty = true;
93 }
94 
reset()95 void Transform::reset()
96 {
97 	matrix.setIdentity();
98 	inverseDirty = true;
99 }
100 
setTransformation(float x,float y,float a,float sx,float sy,float ox,float oy,float kx,float ky)101 void Transform::setTransformation(float x, float y, float a, float sx, float sy, float ox, float oy, float kx, float ky)
102 {
103 	matrix.setTransformation(x, y, a, sx, sy, ox, oy, kx, ky);
104 	inverseDirty = true;
105 }
106 
transformPoint(love::Vector2 p) const107 love::Vector2 Transform::transformPoint(love::Vector2 p) const
108 {
109 	love::Vector2 result;
110 	matrix.transformXY(&result, &p, 1);
111 	return result;
112 }
113 
inverseTransformPoint(love::Vector2 p)114 love::Vector2 Transform::inverseTransformPoint(love::Vector2 p)
115 {
116 	love::Vector2 result;
117 	getInverseMatrix().transformXY(&result, &p, 1);
118 	return result;
119 }
120 
getMatrix() const121 const Matrix4 &Transform::getMatrix() const
122 {
123 	return matrix;
124 }
125 
setMatrix(const Matrix4 & m)126 void Transform::setMatrix(const Matrix4 &m)
127 {
128 	matrix = m;
129 	inverseDirty = true;
130 }
131 
getConstant(const char * in,MatrixLayout & out)132 bool Transform::getConstant(const char *in, MatrixLayout &out)
133 {
134 	return matrixLayouts.find(in, out);
135 }
136 
getConstant(MatrixLayout in,const char * & out)137 bool Transform::getConstant(MatrixLayout in, const char *&out)
138 {
139 	return matrixLayouts.find(in, out);
140 }
141 
getConstants(MatrixLayout)142 std::vector<std::string> Transform::getConstants(MatrixLayout)
143 {
144 	return matrixLayouts.getNames();
145 }
146 
147 StringMap<Transform::MatrixLayout, Transform::MATRIX_MAX_ENUM>::Entry Transform::matrixLayoutEntries[] =
148 {
149 	{ "row",    MATRIX_ROW_MAJOR    },
150 	{ "column", MATRIX_COLUMN_MAJOR },
151 };
152 
153 StringMap<Transform::MatrixLayout, Transform::MATRIX_MAX_ENUM> Transform::matrixLayouts(Transform::matrixLayoutEntries, sizeof(Transform::matrixLayoutEntries));
154 
155 } // math
156 } // love
157