geometry.py

changeset 24
f8080ffceaa9
parent 22
9bb00170780b
child 28
5933250813a3
--- a/geometry.py	Mon Jan 22 18:23:25 2018 +0200
+++ b/geometry.py	Mon Jan 22 21:00:45 2018 +0200
@@ -67,16 +67,18 @@
     def __matmul__(self, transformation_matrix):
         return transform(self, transformation_matrix)
     def __eq__(self, other):
-        return all(
-            abs(a - b) < 1e-8
-            for a, b in zip(self.coordinates, other.coordinates)
-        )
+        return self.is_close(other, threshold = 1e-10)
     def __ne__(self, other):
         return not self.__eq__(other)
     def __lt__(self, other):
         return self.coordinates < other.coordinates
     def __hash__(self):
         return hash(self.coordinates)
+    def is_close(self, other, *, threshold):
+        return all(
+            abs(a - b) < threshold
+            for a, b in zip(self.coordinates, other.coordinates)
+        )
 
 class VertexOp:
     def __init__(self, callable):
@@ -263,7 +265,6 @@
             -(self[0, 1] * self[1, 0] * self[2, 2]),
             -(self[0, 0] * self[1, 2] * self[2, 1]),
         ])
-    @property
     def scaling_vector(self):
         '''
             Extracts scaling factors from this transformation matrix.
@@ -274,7 +275,6 @@
             y = sqrt(self[0, 1] ** 2 + self[1, 1] ** 2 + self[2, 1] ** 2),
             z = sqrt(self[0, 2] ** 2 + self[1, 2] ** 2 + self[2, 2] ** 2),
         )
-    @property
     def rotation_component(self):
         '''
             Extracts rotation from this matrix.
@@ -284,24 +284,22 @@
         '''
             Extracts the rotation matrix and scaling vector.
         '''
-        vec = self.scaling_vector
+        vec = self.scaling_vector()
         return Matrix3x3([
             self[i, j] / vec.coordinates[j]
             for i, j in matrix_indices()
         ]), vec
-    @property
     def contains_scaling(self):
         '''
             Returns whether this matrix contains scaling factors.
         '''
-        vec = self.scaling_vector
+        vec = self.scaling_vector()
         return abs((vec.x * vec.y * vec.z) - 1) >= 1e-10
-    @property
     def contains_rotation(self):
         '''
             Returns whether this matrix contains rotation factors.
         '''
-        return self.rotation_component != Matrix3x3()
+        return self.rotation_component() != Matrix3x3()
     def __eq__(self, other):
         '''
             Returns whether two matrices are equivalent.

mercurial