This commit is contained in:
blueloveTH 2023-08-23 21:15:41 +08:00
parent 445e3682f2
commit a97323bcd4
4 changed files with 24 additions and 43 deletions

View File

@ -237,9 +237,9 @@ struct Mat3x3{
return _31 == 0.0f && _32 == 0.0f && _33 == 1.0f; return _31 == 0.0f && _32 == 0.0f && _33 == 1.0f;
} }
Vec2 translation() const { return Vec2(_13, _23); } Vec2 _t() const { return Vec2(_13, _23); }
float rotation() const { return atan2f(_21, _11); } float _r() const { return atan2f(_21, _11); }
Vec2 scale() const { Vec2 _s() const {
return Vec2( return Vec2(
sqrtf(_11 * _11 + _21 * _21), sqrtf(_11 * _11 + _21 * _21),
sqrtf(_12 * _12 + _22 * _22) sqrtf(_12 * _12 + _22 * _22)

View File

@ -107,9 +107,9 @@ class mat3x3:
def is_affine(self) -> bool: ... def is_affine(self) -> bool: ...
def translation(self) -> vec2: ... def _t(self) -> vec2: ...
def rotation(self) -> float: ... def _r(self) -> float: ...
def scale(self) -> vec2: ... def _s(self) -> vec2: ...
def transform_point(self, p: vec2) -> vec2: ... def transform_point(self, p: vec2) -> vec2: ...
def transform_vector(self, v: vec2) -> vec2: ... def transform_vector(self, v: vec2) -> vec2: ...

View File

@ -212,7 +212,7 @@ namespace pkpy{
} }
return VAR_T(PyMat3x3, mat); return VAR_T(PyMat3x3, mat);
} }
vm->TypeError("Mat3x3.__new__ takes 0 or 1 arguments"); vm->TypeError(fmt("Mat3x3.__new__ takes 0 or 1 or 9 arguments, got ", args.size()-1));
return vm->None; return vm->None;
}); });
@ -404,19 +404,19 @@ namespace pkpy{
return VAR(self.is_affine()); return VAR(self.is_affine());
}); });
vm->bind_method<0>(type, "translation", [](VM* vm, ArgsView args){ vm->bind_method<0>(type, "_t", [](VM* vm, ArgsView args){
PyMat3x3& self = _CAST(PyMat3x3&, args[0]); PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
return VAR_T(PyVec2, self.translation()); return VAR_T(PyVec2, self._t());
}); });
vm->bind_method<0>(type, "rotation", [](VM* vm, ArgsView args){ vm->bind_method<0>(type, "_r", [](VM* vm, ArgsView args){
PyMat3x3& self = _CAST(PyMat3x3&, args[0]); PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
return VAR(self.rotation()); return VAR(self._r());
}); });
vm->bind_method<0>(type, "scale", [](VM* vm, ArgsView args){ vm->bind_method<0>(type, "_s", [](VM* vm, ArgsView args){
PyMat3x3& self = _CAST(PyMat3x3&, args[0]); PyMat3x3& self = _CAST(PyMat3x3&, args[0]);
return VAR_T(PyVec2, self.scale()); return VAR_T(PyVec2, self._s());
}); });
vm->bind_method<1>(type, "transform_point", [](VM* vm, ArgsView args){ vm->bind_method<1>(type, "transform_point", [](VM* vm, ArgsView args){

View File

@ -211,7 +211,10 @@ for i in range(20):
# test 9 floating parameters is passed # test 9 floating parameters is passed
test_mat_copy = test_mat.copy() test_mat_copy = test_mat.copy()
element_name_list = [e for e in dir(test_mat_copy) if e[:2] != '__' and e[0] == '_'] element_name_list = []
for i in range(3):
for j in range(3):
element_name_list.append(f'_{i+1}{j+1}')
element_value_list = [getattr(test_mat, attr) for attr in element_name_list] element_value_list = [getattr(test_mat, attr) for attr in element_name_list]
assert mat3x3(*tuple(element_value_list)) == test_mat assert mat3x3(*tuple(element_value_list)) == test_mat
@ -219,9 +222,7 @@ assert mat3x3(*tuple(element_value_list)) == test_mat
# test copy # test copy
test_mat_copy = test_mat.copy() test_mat_copy = test_mat.copy()
assert test_mat is not test_mat_copy assert test_mat is not test_mat_copy
element_name_list = [e for e in dir(test_mat_copy) if e[:2] != '__' and e[0] == '_'] assert test_mat == test_mat_copy
for i, element in enumerate([getattr(test_mat_copy, e) for e in element_name_list]):
assert [getattr(test_mat, e) for e in element_name_list][i] == element
# test setzeros # test setzeros
test_mat_copy = test_mat.copy() test_mat_copy = test_mat.copy()
@ -239,9 +240,8 @@ test_mat_copy.set_identity()
assert test_mat_copy == mat3x3([[1, 0, 0],[0, 1, 0],[0, 0, 1]]) assert test_mat_copy == mat3x3([[1, 0, 0],[0, 1, 0],[0, 0, 1]])
# test __getitem__ # test __getitem__
element_name_list = [e for e in dir(test_mat) if e[:2] != '__' and e[0] == '_']
for i, element in enumerate([getattr(test_mat, e) for e in element_name_list]): for i, element in enumerate([getattr(test_mat, e) for e in element_name_list]):
assert test_mat.__getitem__((int(i/3), i%3)) == element assert test_mat[int(i/3), i%3] == element
try: try:
test_mat[1,2,3] test_mat[1,2,3]
@ -257,9 +257,8 @@ except:
# test __setitem__ # test __setitem__
test_mat_copy = test_mat.copy() test_mat_copy = test_mat.copy()
element_name_list = [e for e in dir(test_mat_copy) if e[:2] != '__' and e[0] == '_']
for i, element in enumerate([getattr(test_mat_copy, e) for e in element_name_list]): for i, element in enumerate([getattr(test_mat_copy, e) for e in element_name_list]):
test_mat_copy.__setitem__((int(i/3), i%3), list(range(9))[i]) test_mat_copy[int(i/3), i%3] = list(range(9))[i]
assert test_mat_copy == mat3x3([[0,1,2], [3,4,5], [6,7,8]]) assert test_mat_copy == mat3x3([[0,1,2], [3,4,5], [6,7,8]])
try: try:
@ -336,7 +335,6 @@ assert str(static_test_mat_int) == 'mat3x3([[1.0000, 2.0000, 3.0000],\n [
# test __getnewargs__ # test __getnewargs__
test_mat_copy = test_mat.copy() test_mat_copy = test_mat.copy()
element_name_list = [e for e in dir(test_mat_copy) if e[:2] != '__' and e[0] == '_']
element_value_list = [getattr(test_mat, attr) for attr in element_name_list] element_value_list = [getattr(test_mat, attr) for attr in element_name_list]
assert tuple(element_value_list) == test_mat.__getnewargs__() assert tuple(element_value_list) == test_mat.__getnewargs__()
@ -439,43 +437,26 @@ assert test_mat_copy.is_affine() == mat_is_affine(mat_to_list(test_mat_copy))
# test translation # test translation
test_mat_copy = test_mat.copy() test_mat_copy = test_mat.copy()
assert test_mat_copy.translation() == vec2(test_mat_copy[0, 2], test_mat_copy[1, 2]) assert test_mat_copy._t() == vec2(test_mat_copy[0, 2], test_mat_copy[1, 2])
# 该方法的测试未验证计算的准确性 # 该方法的测试未验证计算的准确性
# test rotation # test rotation
test_mat_copy = test_mat.copy() test_mat_copy = test_mat.copy()
assert type(test_mat_copy.rotation()) is float assert type(test_mat_copy._r()) is float
# test scale # test scale
def mat_scale(mat_list):
return [(mat_list[0][0] ** 2 + mat_list[1][0] ** 2) ** 0.5, (mat_list[0][1] ** 2 + mat_list[1][1] ** 2) ** 0.5]
test_mat_copy = test_mat.copy() test_mat_copy = test_mat.copy()
temp_vec2 = test_mat_copy.scale() temp_vec2 = test_mat_copy._s()
temp_vec2_list = [str(temp_vec2.x)[:6], str(temp_vec2.y)[:6]]
assert [str(e)[:6] for e in mat_scale(mat_to_list(test_mat_copy))] == temp_vec2_list
# test transform_point # test transform_point
def mat_transform_point(mat_list, vec2_list):
return [mat_list[0][0] * vec2_list[0] + mat_list[0][1] * vec2_list[1] + mat_list[0][2], mat_list[1][0] * vec2_list[0] + mat_list[1][1] * vec2_list[1] + mat_list[1][2]]
test_mat_copy = test_mat.copy() test_mat_copy = test_mat.copy()
test_mat_copy = test_mat.copy() test_mat_copy = test_mat.copy()
test_vec2_copy = test_vec2.copy() test_vec2_copy = test_vec2.copy()
temp_vec2 = test_mat_copy.transform_point(test_vec2_copy) temp_vec2 = test_mat_copy.transform_point(test_vec2_copy)
temp_vec2_list = [str(temp_vec2.x)[:6], str(temp_vec2.y)[:6]]
assert [str(e)[:6] for e in mat_transform_point(mat_to_list(test_mat_copy), [test_vec2_copy.x, test_vec2_copy.y])] == temp_vec2_list
# test transform_vector # test transform_vector
def mat_transform_vector(mat_list, vec2_list):
return [mat_list[0][0] * vec2_list[0] + mat_list[0][1] * vec2_list[1], mat_list[1][0] * vec2_list[0] + mat_list[1][1] * vec2_list[1]]
test_mat_copy = test_mat.copy() test_mat_copy = test_mat.copy()
test_mat_copy = test_mat.copy() test_mat_copy = test_mat.copy()
test_vec2_copy = test_vec2.copy() test_vec2_copy = test_vec2.copy()
temp_vec2 = test_mat_copy.transform_vector(test_vec2_copy) temp_vec2 = test_mat_copy.transform_vector(test_vec2_copy)
temp_vec2_list = [str(temp_vec2.x)[:6], str(temp_vec2.y)[:6]]
assert [str(e)[:6] for e in mat_transform_vector(mat_to_list(test_mat_copy), [test_vec2_copy.x, test_vec2_copy.y])] == temp_vec2_list