geometry.py

Tue, 12 Dec 2017 13:46:17 +0200

author
Santeri Piippo
date
Tue, 12 Dec 2017 13:46:17 +0200
changeset 5
e340e35d6e4c
parent 3
1dc58f44d556
child 6
6da1e81c5652
permissions
-rw-r--r--

Added vertex operators

0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
1 class Vertex:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
2 def __init__(self, x, y, z):
5
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
3 if not all(is_real(coordinate) for coordinate in (x, y, z)):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
4 raise ValueError(str.format('Bad vertex coordinates: {!r}',
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
5 (x, y, z),
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
6 ))
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
7 self.x, self.y, self.z = x, y, z
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
8 def __repr__(self):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
9 return str.format('Vertex({!r}, {!r}, {!r})', self.x, self.y, self.z)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
10 def distance_to(self, other):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
11 # can't use hypot because of 3 arguments
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
12 from math import sqrt
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
13 return sqrt(
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
14 (self.x - other.x) ** 2 +
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
15 (self.y - other.y) ** 2 +
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
16 (self.z - other.z) ** 2
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
17 )
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
18 @property
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
19 def coordinates(self):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
20 return self.x, self.y, self.z
5
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
21 def __add__(self, other):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
22 return Vertex(self.x + other.x, self.y + other.y, self.z + other.z)
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
23 def __neg__(self):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
24 return Vertex(-self.x, -self.y, -self.z)
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
25 def __sub__(self, other):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
26 return self + (-other)
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
27 def __mul__(self, scalar):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
28 return Vertex(self.x * scalar, self.y * scalar, self.z * scalar)
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
29 def __rmul__(self, other):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
30 return self * other
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
31 def __truediv__(self, scalar):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
32 return Vertex(self.x / scalar, self.y / scalar, self.z / scalar)
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
33 def __floordiv__(self, scalar):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
34 return Vertex(self.x // scalar, self.y // scalar, self.z // scalar)
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
35 def __matmul__(self, transformation_matrix):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
36 return transform(self, transformation_matrix)
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
37 def __eq__(self, other):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
38 return self.coordinates == other.coordinates
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
39 def __lt__(self, other):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
40 return self.coordinates < other.coordinates
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
41 def __hash__(self):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
42 return hash(self.coordinates)
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
43
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
44 class VertexOp:
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
45 def __init__(self, callable):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
46 self.callable = callable
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
47 def __rmul__(self, coordinate):
e340e35d6e4c Added vertex operators
Santeri Piippo
parents: 3
diff changeset
48 return self.callable(coordinate)
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
49
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
50 class LineSegment:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
51 def __init__(self, v1, v2):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
52 self.v1, self.v2 = v1, v2
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
53 def __repr__(self):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
54 return str.format('LineSegment({!r}, {!r})', self.v1, self.v2)
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
55 @property
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
56 def vertices(self):
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 1
diff changeset
57 return self.v1, self.v2
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
58
1
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
59 class Polygon:
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
60 def __init__(self, vertices):
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
61 self.vertices = vertices
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
62 def __repr__(self):
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
63 return str.format('Polygon({!r})', self.vertices)
5411a25cfca7 Parsing function complete
Santeri Piippo
parents: 0
diff changeset
64
0
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
65 def is_real(number):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
66 return isinstance(number, int) or isinstance(number, float)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
67
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
68 class TransformationMatrix:
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
69 '''
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
70 A 3×3 matrix forming the top-left portion of a full 4×4 transformation
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
71 matrix.
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
72 '''
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
73 def __init__(self, values):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
74 assert(all(is_real(x) for x in values))
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
75 assert len(values) == 9
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
76 self.values = values
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
77 def __repr__(self):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
78 return str.format('TransformationMatrix({!r})', self.values)
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
79 def __getitem__(self, index):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
80 return self.values[index]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
81
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
82 def complete_matrix(matrix, anchor):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
83 '''
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
84 Combines a 3×3 matrix and an anchor vertex into a full 4×4
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
85 transformation matrix.
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
86 '''
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
87 return [
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
88 matrix[0], matrix[1], matrix[2], anchor.x,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
89 matrix[3], matrix[4], matrix[5], anchor.y,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
90 matrix[6], matrix[7], matrix[8], anchor.z,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
91 0, 0, 0, 1,
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
92 ]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
93
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
94 def transform(vertex, transformation_matrix):
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
95 '''
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
96 Transforms a vertex by a 4×4 transformation matrix.
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
97 '''
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
98 u = transformation_matrix[0] * vertex.x \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
99 + transformation_matrix[1] * vertex.y \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
100 + transformation_matrix[2] * vertex.z \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
101 + transformation_matrix[3]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
102 v = transformation_matrix[4] * vertex.x \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
103 + transformation_matrix[5] * vertex.y \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
104 + transformation_matrix[6] * vertex.z \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
105 + transformation_matrix[7]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
106 w = transformation_matrix[8] * vertex.x \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
107 + transformation_matrix[9] * vertex.y \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
108 + transformation_matrix[10] * vertex.z \
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
109 + transformation_matrix[11]
55b4c97d44c5 Initial commit with half-done parsing function
Santeri Piippo
parents:
diff changeset
110 return Vertex(u, v, w)

mercurial