Skip to content

Commit 2fe0681

Browse files
Copilotgonzalocasas
andcommitted
Add scale() implementations for Cone and Torus with tests
Co-authored-by: gonzalocasas <[email protected]>
1 parent 42c1b71 commit 2fe0681

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

src/compas/geometry/shapes/cone.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,22 @@ def to_brep(self):
300300
# Transformations
301301
# ==========================================================================
302302

303+
def scale(self, factor):
304+
"""Scale the cone by multiplying the radius and height by a factor.
305+
306+
Parameters
307+
----------
308+
factor : float
309+
The scaling factor.
310+
311+
Returns
312+
-------
313+
None
314+
315+
"""
316+
self.radius *= factor
317+
self.height *= factor
318+
303319
# ==========================================================================
304320
# Methods
305321
# ==========================================================================

src/compas/geometry/shapes/torus.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,20 @@ def transform(self, transformation):
278278
279279
"""
280280
self.frame.transform(transformation)
281+
282+
def scale(self, factor):
283+
"""Scale the torus by multiplying the axis radius and pipe radius by a factor.
284+
285+
Parameters
286+
----------
287+
factor : float
288+
The scaling factor.
289+
290+
Returns
291+
-------
292+
None
293+
294+
"""
295+
self.radius_axis *= factor
296+
self.radius_pipe *= factor
297+

tests/compas/geometry/test_cone.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,39 @@ def test_cone_discretization(cone):
1414
assert cone.edges
1515
assert cone.faces
1616
assert cone.vertices
17+
18+
19+
def test_cone_scaled():
20+
"""Test that Cone.scaled() returns a scaled copy without modifying the original."""
21+
cone = Cone(radius=5.0, height=10.0)
22+
23+
# Test uniform scaling
24+
scaled_cone = cone.scaled(0.5)
25+
26+
# Original should be unchanged
27+
assert cone.radius == 5.0
28+
assert cone.height == 10.0
29+
30+
# Scaled copy should have scaled dimensions
31+
assert scaled_cone.radius == 2.5
32+
assert scaled_cone.height == 5.0
33+
34+
# Test scaling with factor > 1
35+
scaled_cone_2 = cone.scaled(2.0)
36+
assert scaled_cone_2.radius == 10.0
37+
assert scaled_cone_2.height == 20.0
38+
assert cone.radius == 5.0 # Original still unchanged
39+
assert cone.height == 10.0
40+
41+
42+
def test_cone_scale():
43+
"""Test that Cone.scale() modifies the cone in place."""
44+
cone = Cone(radius=5.0, height=10.0)
45+
46+
# Test uniform scaling
47+
cone.scale(0.5)
48+
49+
# Cone should be modified
50+
assert cone.radius == 2.5
51+
assert cone.height == 5.0
52+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pytest
2+
3+
from compas.geometry import Torus
4+
5+
6+
@pytest.fixture
7+
def torus():
8+
return Torus(radius_axis=10.0, radius_pipe=2.0)
9+
10+
11+
def test_torus_discretization(torus):
12+
# just checking these don't break. Could not quickly find a formula that worked to test the actual values
13+
# as function of the resolution
14+
assert torus.edges
15+
assert torus.faces
16+
assert torus.vertices
17+
18+
19+
def test_torus_scaled():
20+
"""Test that Torus.scaled() returns a scaled copy without modifying the original."""
21+
torus = Torus(radius_axis=10.0, radius_pipe=2.0)
22+
23+
# Test uniform scaling
24+
scaled_torus = torus.scaled(0.5)
25+
26+
# Original should be unchanged
27+
assert torus.radius_axis == 10.0
28+
assert torus.radius_pipe == 2.0
29+
30+
# Scaled copy should have scaled dimensions
31+
assert scaled_torus.radius_axis == 5.0
32+
assert scaled_torus.radius_pipe == 1.0
33+
34+
# Test scaling with factor > 1
35+
scaled_torus_2 = torus.scaled(2.0)
36+
assert scaled_torus_2.radius_axis == 20.0
37+
assert scaled_torus_2.radius_pipe == 4.0
38+
assert torus.radius_axis == 10.0 # Original still unchanged
39+
assert torus.radius_pipe == 2.0
40+
41+
42+
def test_torus_scale():
43+
"""Test that Torus.scale() modifies the torus in place."""
44+
torus = Torus(radius_axis=10.0, radius_pipe=2.0)
45+
46+
# Test uniform scaling
47+
torus.scale(0.5)
48+
49+
# Torus should be modified
50+
assert torus.radius_axis == 5.0
51+
assert torus.radius_pipe == 1.0

0 commit comments

Comments
 (0)