Skip to content

Commit ba1dfe4

Browse files
authored
Merge pull request #835 from A-G-D/master
Implemented cq.Vector.projectToLine() method
2 parents 94030ad + 1508f15 commit ba1dfe4

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

cadquery/occ_impl/geom.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ def __mul__(self, scale: float) -> "Vector":
155155
def __truediv__(self, denom: float) -> "Vector":
156156
return self.multiply(1.0 / denom)
157157

158+
def __rmul__(self, scale: float) -> "Vector":
159+
return self.multiply(scale)
160+
158161
def normalized(self) -> "Vector":
159162
"""Return a normalized version of this vector"""
160163
return Vector(self.wrapped.Normalized())
@@ -175,8 +178,18 @@ def getAngle(self, v: "Vector") -> float:
175178
def distanceToLine(self):
176179
raise NotImplementedError("Have not needed this yet, but OCCT supports it!")
177180

178-
def projectToLine(self):
179-
raise NotImplementedError("Have not needed this yet, but OCCT supports it!")
181+
def projectToLine(self, line: "Vector") -> "Vector":
182+
"""
183+
Returns a new vector equal to the projection of this Vector onto the line
184+
represented by Vector <line>
185+
186+
:param args: Vector
187+
188+
Returns the projected vector.
189+
"""
190+
lineLength = line.Length
191+
192+
return line * (self.dot(line) / (lineLength * lineLength))
180193

181194
def distanceToPlane(self):
182195
raise NotImplementedError("Have not needed this yet, but OCCT supports it!")

tests/test_cad_objects.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ def testVectorOperators(self):
289289
result = Vector(1, 2, 3) * 2
290290
self.assertEqual(Vector(2, 4, 6), result)
291291

292+
result = 3 * Vector(1, 2, 3)
293+
self.assertEqual(Vector(3, 6, 9), result)
294+
292295
result = Vector(2, 4, 6) / 2
293296
self.assertEqual(Vector(1, 2, 3), result)
294297

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

308311
def testVectorProject(self):
309312
"""
310-
Test method to project vector to plane.
313+
Test line projection and plane projection methods of cq.Vector
311314
"""
312315
decimal_places = 9
313316

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

327+
# test line projection
328+
vec = Vector(10, 10, 10)
329+
line = Vector(3, 4, 5)
330+
angle = vec.getAngle(line)
331+
332+
vecLineProjection = vec.projectToLine(line)
333+
334+
self.assertTupleAlmostEquals(
335+
vecLineProjection.normalized().toTuple(),
336+
line.normalized().toTuple(),
337+
decimal_places,
338+
)
339+
self.assertAlmostEqual(
340+
vec.Length * math.cos(angle), vecLineProjection.Length, decimal_places
341+
)
342+
324343
def testVectorNotImplemented(self):
325344
v = Vector(1, 2, 3)
326345
with self.assertRaises(NotImplementedError):
327346
v.distanceToLine()
328-
with self.assertRaises(NotImplementedError):
329-
v.projectToLine()
330347
with self.assertRaises(NotImplementedError):
331348
v.distanceToPlane()
332349

0 commit comments

Comments
 (0)