Skip to content

Copy Plane.__eq__() from main dcowden/cadquery #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions cadquery/occ_impl/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,25 @@ def dot(self, v):
def sub(self, v):
return Vector(self.wrapped.Subtracted(v.wrapped))

def __sub__(self, v):
return self.sub(v)

def add(self, v):
return Vector(self.wrapped.Added(v.wrapped))

def __add__(self, v):
return self.add(v)

def multiply(self, scale):
"""Return a copy multiplied by the provided scalar"""
return Vector(self.wrapped.Multiplied(scale))

def __mul__(self, scale):
return self.multiply(scale)

def __truediv__(self, denom):
return self.multiply(1.0 / denom)

def normalized(self):
"""Return a normalized version of this vector"""
return Vector(self.wrapped.Normalized())
Expand Down Expand Up @@ -126,6 +138,12 @@ def __add__(self, v):
def __sub__(self, v):
return self.sub(v)

def __neg__(self):
return self * -1

def __abs__(self):
return self.Length

def __repr__(self):
return 'Vector: ' + str((self.x, self.y, self.z))

Expand Down Expand Up @@ -227,6 +245,11 @@ class Plane(object):
created automatically from faces.
"""

# equality tolerances
_eq_tolerance_origin = 1e-6
_eq_tolerance_dot = 1e-6


@classmethod
def named(cls, stdName, origin=(0, 0, 0)):
"""Create a predefined Plane based on the conventional names.
Expand Down Expand Up @@ -632,6 +655,22 @@ def _calcTransforms(self):
self.rG = inverse
self.fG = forward

# Equality
def __eq__(self, other):
def equality_iter():
cls = type(self)
yield isinstance(other, Plane) # comparison is with another Plane
# origins are the same
yield abs(self.origin - other.origin) < cls._eq_tolerance_origin
# z-axis vectors are parallel (assumption: both are unit vectors)
yield abs(self.zDir.dot(other.zDir) - 1) < cls._eq_tolerance_dot
# x-axis vectors are parallel (assumption: both are unit vectors)
yield abs(self.xDir.dot(other.xDir) - 1) < cls._eq_tolerance_dot
return all(equality_iter())

def __ne__(self, other):
return not self.__eq__(other)


class BoundBox(object):
"""A BoundingBox for an object or set of objects. Wraps the OCC one"""
Expand Down
64 changes: 64 additions & 0 deletions tests/TestCadObjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ def testVectorAdd(self):
result = Vector(1, 2, 0) + Vector(0, 0, 3)
self.assertTupleAlmostEquals((1.0, 2.0, 3.0), result.toTuple(), 3)

def testVectorOperators(self):
result = Vector(1, 1, 1) + Vector(2, 2, 2)
self.assertEqual(Vector(3, 3, 3), result)

result = Vector(1, 2, 3) - Vector(3, 2, 1)
self.assertEqual(Vector(-2, 0, 2), result)

result = Vector(1, 2, 3) * 2
self.assertEqual(Vector(2, 4, 6), result)

result = Vector(2, 4, 6) / 2
self.assertEqual(Vector(1, 2, 3), result)

self.assertEqual(Vector(-1, -1, -1), -Vector(1, 1, 1))

self.assertEqual(0, abs(Vector(0, 0, 0)))
self.assertEqual(1, abs(Vector(1, 0, 0)))
self.assertEqual((1+4+9)**0.5, abs(Vector(1, 2, 3)))

def testVectorEquals(self):
a = Vector(1, 2, 3)
b = Vector(1, 2, 3)
Expand All @@ -126,6 +145,51 @@ def testVertices(self):
gp_Pnt(1, 1, 0)).Edge())
self.assertEqual(2, len(e.Vertices()))

def testPlaneEqual(self):
# default orientation
self.assertEqual(
Plane(origin=(0,0,0), xDir=(1,0,0), normal=(0,0,1)),
Plane(origin=(0,0,0), xDir=(1,0,0), normal=(0,0,1))
)
# moved origin
self.assertEqual(
Plane(origin=(2,1,-1), xDir=(1,0,0), normal=(0,0,1)),
Plane(origin=(2,1,-1), xDir=(1,0,0), normal=(0,0,1))
)
# moved x-axis
self.assertEqual(
Plane(origin=(0,0,0), xDir=(1,1,0), normal=(0,0,1)),
Plane(origin=(0,0,0), xDir=(1,1,0), normal=(0,0,1))
)
# moved z-axis
self.assertEqual(
Plane(origin=(0,0,0), xDir=(1,0,0), normal=(0,1,1)),
Plane(origin=(0,0,0), xDir=(1,0,0), normal=(0,1,1))
)

def testPlaneNotEqual(self):
# type difference
for value in [None, 0, 1, 'abc']:
self.assertNotEqual(
Plane(origin=(0,0,0), xDir=(1,0,0), normal=(0,0,1)),
value
)
# origin difference
self.assertNotEqual(
Plane(origin=(0,0,0), xDir=(1,0,0), normal=(0,0,1)),
Plane(origin=(0,0,1), xDir=(1,0,0), normal=(0,0,1))
)
# x-axis difference
self.assertNotEqual(
Plane(origin=(0,0,0), xDir=(1,0,0), normal=(0,0,1)),
Plane(origin=(0,0,0), xDir=(1,1,0), normal=(0,0,1))
)
# z-axis difference
self.assertNotEqual(
Plane(origin=(0,0,0), xDir=(1,0,0), normal=(0,0,1)),
Plane(origin=(0,0,0), xDir=(1,0,0), normal=(0,1,1))
)


if __name__ == '__main__':
unittest.main()