Skip to content
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

Implemented cq.Vector.projectToLine() method #835

Merged
merged 5 commits into from
Sep 8, 2021
Merged
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
17 changes: 15 additions & 2 deletions cadquery/occ_impl/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ def __mul__(self, scale: float) -> "Vector":
def __truediv__(self, denom: float) -> "Vector":
return self.multiply(1.0 / denom)

def __rmul__(self, scale: float) -> "Vector":
return self.multiply(scale)

def normalized(self) -> "Vector":
"""Return a normalized version of this vector"""
return Vector(self.wrapped.Normalized())
Expand All @@ -175,8 +178,18 @@ def getAngle(self, v: "Vector") -> float:
def distanceToLine(self):
raise NotImplementedError("Have not needed this yet, but OCCT supports it!")

def projectToLine(self):
raise NotImplementedError("Have not needed this yet, but OCCT supports it!")
def projectToLine(self, line: "Vector") -> "Vector":
"""
Returns a new vector equal to the projection of this Vector onto the line
represented by Vector <line>

:param args: Vector

Returns the projected vector.
"""
lineLength = line.Length

return line * (self.dot(line) / (lineLength * lineLength))

def distanceToPlane(self):
raise NotImplementedError("Have not needed this yet, but OCCT supports it!")
Expand Down
23 changes: 20 additions & 3 deletions tests/test_cad_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ def testVectorOperators(self):
result = Vector(1, 2, 3) * 2
self.assertEqual(Vector(2, 4, 6), result)

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

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

Expand All @@ -307,7 +310,7 @@ def testVectorEquals(self):

def testVectorProject(self):
"""
Test method to project vector to plane.
Test line projection and plane projection methods of cq.Vector
"""
decimal_places = 9

Expand All @@ -321,12 +324,26 @@ def testVectorProject(self):
point.toTuple(), (59 / 7, 55 / 7, 51 / 7), decimal_places
)

# test line projection
vec = Vector(10, 10, 10)
line = Vector(3, 4, 5)
angle = vec.getAngle(line)

vecLineProjection = vec.projectToLine(line)

self.assertTupleAlmostEquals(
vecLineProjection.normalized().toTuple(),
line.normalized().toTuple(),
decimal_places,
)
self.assertAlmostEqual(
vec.Length * math.cos(angle), vecLineProjection.Length, decimal_places
)

def testVectorNotImplemented(self):
v = Vector(1, 2, 3)
with self.assertRaises(NotImplementedError):
v.distanceToLine()
with self.assertRaises(NotImplementedError):
v.projectToLine()
with self.assertRaises(NotImplementedError):
v.distanceToPlane()

Expand Down