Moved documentation, and added more operators to Vertex

- added __neg__
    - added cross for Vertex * Vertex
    - added tests for these
    - moved the cube docstring up into the cube generating function
This commit is contained in:
Stephen M. McQuay 2012-03-20 07:16:15 -06:00
parent 9a5fc54f12
commit 93891b917b
3 changed files with 123 additions and 73 deletions

View File

@ -15,77 +15,14 @@ to store the vertex, edge and face data. These include:
We have chosen to use a winged-edge style mesh for our purpopses. We have chosen to use a winged-edge style mesh for our purpopses.
v4 v5
*-----e8-----*
| |
| |
e|4 f4 e|5
| |
v4 v|0 v|1 v5
*-----e4-----*-----e0-----*-----e5-----*
| | | |
| | | |
e11| f3 e|3 f0 e|1 f1 e|9
| | | |
| | | |
*-----e7-----*-----e2-----*-----e6-----*
v7 v|3 v|2 v6
| |
e|7 f2 e|6
| |
v|7 v|6
*-----e10----*
| |
| |
e|11 f5 e|9
| |
| |
*-----e8-----*
v4 v5
v0 - <0,1,0>
v1 - <1,1,0>
v2 - <1,0,0>
v3 - <0,0,0>
v4 - <0,1,1>
v5 - <1,1,1>
v6 - <1,0,1>
v7 - <0,0,1>
face list
f0 - e0, e1, e2, e3
f1 - e1, e5, e9, e6
f2 - e2, e6, e10, e7
f3 - e4, e3, e7, e11
f4 - e8, e5, e0, e4
f5 - e10, e9, e8, e11
winged edges ordered by face, then by vertex reference
edge list
e0 - v0, v1; f0, f4; e3, e1, e4, e5
e1 - v1, v2; f0, f1; e0, e2, e5, e6
e2 - v3, v2; f0, f2; e3, e7, e1, e6
e3 - v0, v3; f3, f0; e4, e7, e0, e2
e4 - v4, v0; f3, f4; e11, e3, e0 e8
e5 - v5, v1; f4, f1; e8, e0, e9, e1
e6 - v2, v6; f1, f2; e1, e9, e2, e10
e7 - v7, v3; f3, f2; e11, e3, e10, e2
e8 - v4, v5, f4, f5; e4, e5, e11, e9
e9 - v5, v6; f1, f5; e5, e6, e8, e10
e10 - v7, v6; f2, f5; e7, e6, e11, e9
e11 - v4, v7; f3, f5; e4, e7, e8, 10
vertex list
v0 - e0, e3, e4
v1 - e0, e5, e1
v2 - e1, e6, e2
v3 - e2, e7, e3
v4 - e4, e11, e8
v5 - e5, e9, e8
v6 - e2, e9, e10
v7 - e7, e10, e11
''' '''
def cross(a, b):
i = a.y * b.z - a.z * b.y
j = a.z * b.x - a.x * b.z
k = a.x * b.y - a.y * b.x
return Vertex(i, j, k)
class Vertex(object): class Vertex(object):
''' '''
@ -112,11 +49,17 @@ class Vertex(object):
return Vertex(self.x + other.x, self.y + other.y, self.z + other.z) return Vertex(self.x + other.x, self.y + other.y, self.z + other.z)
def __radd__(self, other): def __radd__(self, other):
return self.__add__(other) return other + self
# return self.__add__(other)
def __mul__(self, other): def __mul__(self, other):
# for now just assume type(other) = int or float if isinstance(other, Vertex):
return cross(self, other)
elif isinstance(other, (float, int)):
return Vertex(self.x * other, self.y * other, self.z * other) return Vertex(self.x * other, self.y * other, self.z * other)
else:
raise TypeError("{0} has an unexpected type: {1}".format(
other, type(other)))
def __rmul__(self, other): def __rmul__(self, other):
return self.__mul__(other) return self.__mul__(other)
@ -126,6 +69,9 @@ class Vertex(object):
other = float(other) other = float(other)
return Vertex(self.x / other, self.y / other, self.z / other) return Vertex(self.x / other, self.y / other, self.z / other)
def __neg__(self):
return Vertex(-self.x, -self.y, -self.z)
class Edge(object): class Edge(object):
''' '''

View File

@ -2,6 +2,80 @@ from surf.geometry import Vertex, Edge, Face, Polygon
def cube(): def cube():
"""
This function returns a populated Polygon object. The object is a cube with
a vertex at the origin, and another at (1, 1, 1).
v4 v5
*-----e8-----*
| |
| |
e|4 f4 e|5
| |
v4 v|0 v|1 v5
*-----e4-----*-----e0-----*-----e5-----*
| | | |
| | | |
e11| f3 e|3 f0 e|1 f1 e|9
| | | |
| | | |
*-----e7-----*-----e2-----*-----e6-----*
v7 v|3 v|2 v6
| |
e|7 f2 e|6
| |
v|7 v|6
*-----e10----*
| |
| |
e|11 f5 e|9
| |
| |
*-----e8-----*
v4 v5
v0 - <0,1,0>
v1 - <1,1,0>
v2 - <1,0,0>
v3 - <0,0,0>
v4 - <0,1,1>
v5 - <1,1,1>
v6 - <1,0,1>
v7 - <0,0,1>
face list
f0 - e0, e1, e2, e3
f1 - e1, e5, e9, e6
f2 - e2, e6, e10, e7
f3 - e4, e3, e7, e11
f4 - e8, e5, e0, e4
f5 - e10, e9, e8, e11
winged edges ordered by face, then by vertex reference
edge list
e0 - v0, v1; f0, f4; e3, e1, e4, e5
e1 - v1, v2; f0, f1; e0, e2, e5, e6
e2 - v3, v2; f0, f2; e3, e7, e1, e6
e3 - v0, v3; f3, f0; e4, e7, e0, e2
e4 - v4, v0; f3, f4; e11, e3, e0 e8
e5 - v5, v1; f4, f1; e8, e0, e9, e1
e6 - v2, v6; f1, f2; e1, e9, e2, e10
e7 - v7, v3; f3, f2; e11, e3, e10, e2
e8 - v4, v5, f4, f5; e4, e5, e11, e9
e9 - v5, v6; f1, f5; e5, e6, e8, e10
e10 - v7, v6; f2, f5; e7, e6, e11, e9
e11 - v4, v7; f3, f5; e4, e7, e8, 10
vertex list
v0 - e0, e3, e4
v1 - e0, e5, e1
v2 - e1, e6, e2
v3 - e2, e7, e3
v4 - e4, e11, e8
v5 - e5, e9, e8
v6 - e2, e9, e10
v7 - e7, e10, e11
"""
v = [] v = []
v.append(Vertex(0.0, 1.0, 0.0)) v.append(Vertex(0.0, 1.0, 0.0))
v.append(Vertex(1.0, 1.0, 0.0)) v.append(Vertex(1.0, 1.0, 0.0))

View File

@ -1,6 +1,6 @@
import unittest import unittest
from surf.geometry import Vertex from surf.geometry import Vertex, cross
class TestVertexOperations(unittest.TestCase): class TestVertexOperations(unittest.TestCase):
@ -30,6 +30,36 @@ class TestVertexOperations(unittest.TestCase):
v2 = self.v1 / 2.0 v2 = self.v1 / 2.0
self.assertEqual(v1, v2) self.assertEqual(v1, v2)
def test_multiply(self):
self.assertEqual(type(self.v1 * 2), Vertex)
self.assertEqual(type(self.v1 * self.v1), Vertex)
self.assertEqual(self.v1 * 2, Vertex(-2, -2, -2))
self.assertEqual(self.v1 * self.v1, Vertex(0, 0, 0))
self.assertEqual(self.v1 * self.v2, Vertex(0, 0, 0))
self.assertEqual(self.v3 * self.v4, Vertex(58, -35, -50))
def test_cross(self):
i = Vertex(1, 0, 0)
j = Vertex(0, 1, 0)
k = Vertex(0, 0, 1)
self.assertEqual(i * j, k)
self.assertEqual(j * k, i)
self.assertEqual(k * i, j)
self.assertEqual(j * i, -k)
self.assertEqual(k * j, -i)
self.assertEqual(i * k, -j)
self.assertEqual(i * i, self.origin)
self.assertEqual(j * j, self.origin)
self.assertEqual(k * k, self.origin)
def test_negative(self):
self.assertEqual(-self.v1, self.v2)
self.assertEqual(-self.v2, self.v1)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main(verbosity=3) unittest.main(verbosity=3)