projectToPlane returns self and added tests

The projectToPlane method of Vector returns self to allow for a fluent interface. There is now full test converge of projectToPlane method. Bug fixed in projectToPlane when plane is passed instead of base and normal.
This commit is contained in:
Michael Greminger
2019-05-27 14:51:32 -05:00
parent 8baafc719b
commit 1d05eea6c0
4 changed files with 42 additions and 5 deletions

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"python.linting.flake8Enabled": false,
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.pythonPath": "C:\\Users\\micha\\Anaconda3\\envs\\cq\\python.exe"
}

View File

@ -376,9 +376,7 @@ class CQ(object):
# update center to projected origin if desired
if centerOption == 'ProjectedOrigin':
projected_center = Vector(0, 0, 0)
projected_center.projectToPlane(center, normal)
center = projected_center
center = Vector(0, 0, 0).projectToPlane(center, normal)
# invert if requested
if invert:

View File

@ -154,9 +154,12 @@ class Vector(object):
if len(args) == 2:
base = args[0]
normal = args[1]
else:
elif len(args) == 1:
base = args[0].origin
normal = args[1].zDir
normal = args[0].zDir
else:
raise TypeError("Expected 1 or 2 arguments consisting of 1 Plane object \
or a base Vector and a normal Vector object.")
result = self-normal*(((self-base).dot(normal))/normal.Length**2)
@ -164,6 +167,8 @@ class Vector(object):
self.y = result.y
self.z = result.z
return self
def __neg__(self):
return self * -1

View File

@ -151,6 +151,34 @@ class TestCadObjects(BaseTest):
self.assertEqual(a, b)
self.assertEqual(a, c)
def testVectorProject(self):
"""
Test method to project vector to plane.
"""
decimal_places = 9
normal = Vector(1, 2, 3)
base = Vector(5, 7, 9)
x_dir = Vector(1, 0, 0)
# test passing plane defined by base and normal
point = Vector(10, 11, 12).projectToPlane(base, normal)
self.assertTupleAlmostEquals(point.toTuple(), (59/7, 55/7, 51/7),
decimal_places)
point = Vector(10, 11, 12).projectToPlane(base, normal.normalized())
self.assertTupleAlmostEquals(point.toTuple(), (59/7, 55/7, 51/7),
decimal_places)
# test passing Plane object
point = Vector(10, 11, 12).projectToPlane(Plane(base, x_dir, normal))
self.assertTupleAlmostEquals(point.toTuple(), (59/7, 55/7, 51/7),
decimal_places)
# test wrong number of input arguments
with self.assertRaises(TypeError):
Vector(10,11.12).projectToPlane(1,1,1)
def testMatrixCreationAndAccess(self):
def matrix_vals(m):
return [[m[r,c] for c in range(4)] for r in range(4)]