Skip to content

Commit b812aea

Browse files
committed
convenience methods
1 parent 197d491 commit b812aea

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3434
* Added `compas_ghpython.unload_modules`.
3535
* Added `compas_ghpython.sets`.
3636
* Added `compas_ghpython.timer`.
37+
* Added `scale` and `scaled` to `compas.datastructures.Datastructure`.
38+
* Added `rotate` and `rotated` to `compas.datastructures.Datastructure`.
39+
* Added `translate` and `translated` to `compas.datastructures.Datastructure`.
3740

3841
### Changed
3942

src/compas/datastructures/datastructure.py

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
from __future__ import division
33
from __future__ import print_function
44

5+
try:
6+
from typing import TypeVar # noqa: F401
7+
except ImportError:
8+
pass
9+
else:
10+
G = TypeVar("G", bound="Datastructure")
11+
512
from compas.data import Data
613

714

@@ -77,3 +84,192 @@ def transformed_numpy(self, transformation):
7784
datastructure = self.copy()
7885
datastructure.transform_numpy(transformation)
7986
return datastructure
87+
88+
def scale(self, x, y=None, z=None):
89+
"""Scale the datastructure.
90+
91+
Parameters
92+
----------
93+
x : float
94+
The scaling factor in the x-direction.
95+
y : float, optional
96+
The scaling factor in the y-direction.
97+
Defaults to ``x``.
98+
z : float, optional
99+
The scaling factor in the z-direction.
100+
Defaults to ``x``.
101+
102+
Returns
103+
-------
104+
None
105+
106+
See Also
107+
--------
108+
scaled
109+
translate
110+
rotate
111+
transform
112+
113+
"""
114+
from compas.geometry import Scale
115+
116+
if y is None:
117+
y = x
118+
119+
if z is None:
120+
z = x
121+
122+
self.transform(Scale.from_factors([x, y, z]))
123+
124+
def scaled(self, x, y=None, z=None): # type: (...) -> G
125+
"""Returns a scaled copy of this geometry.
126+
127+
Parameters
128+
----------
129+
x : float
130+
The scaling factor in the x-direction.
131+
y : float, optional
132+
The scaling factor in the y-direction.
133+
Defaults to ``x``.
134+
z : float, optional
135+
The scaling factor in the z-direction.
136+
Defaults to ``x``.
137+
138+
Returns
139+
-------
140+
:class:`Geometry`
141+
The scaled geometry.
142+
143+
See Also
144+
--------
145+
scale
146+
translated
147+
rotated
148+
transformed
149+
150+
"""
151+
from compas.geometry import Scale
152+
153+
if y is None:
154+
y = x
155+
156+
if z is None:
157+
z = x
158+
159+
return self.transformed(Scale.from_factors([x, y, z]))
160+
161+
def translate(self, vector):
162+
"""Translate the datastructure.
163+
164+
Parameters
165+
----------
166+
vector : :class:`compas.geometry.Vector`
167+
The vector used to translate the datastructure.
168+
169+
Returns
170+
-------
171+
None
172+
173+
See Also
174+
--------
175+
translated
176+
rotate
177+
scale
178+
transform
179+
180+
"""
181+
from compas.geometry import Translation
182+
183+
self.transform(Translation.from_vector(vector))
184+
185+
def translated(self, vector): # type: (...) -> G
186+
"""Returns a translated copy of this geometry.
187+
188+
Parameters
189+
----------
190+
vector : :class:`compas.geometry.Vector`
191+
The vector used to translate the datastructure.
192+
193+
Returns
194+
-------
195+
:class:`Geometry`
196+
The translated geometry.
197+
198+
See Also
199+
--------
200+
translate
201+
rotated
202+
scaled
203+
transformed
204+
205+
"""
206+
from compas.geometry import Translation
207+
208+
return self.transformed(Translation.from_vector(vector))
209+
210+
def rotate(self, angle, axis=None, point=None):
211+
"""Rotate the datastructure.
212+
213+
Parameters
214+
----------
215+
angle : float
216+
The angle of rotation in radians.
217+
axis : :class:`compas.geometry.Vector`, optional
218+
The axis of rotation.
219+
Defaults to the z-axis.
220+
point : :class:`compas.geometry.Point`, optional
221+
The base point of the rotation axis.
222+
Defaults to the origin.
223+
224+
Returns
225+
-------
226+
None
227+
228+
See Also
229+
--------
230+
rotated
231+
translate
232+
scale
233+
transform
234+
235+
"""
236+
from compas.geometry import Rotation
237+
238+
if axis is None:
239+
axis = [0.0, 0.0, 1.0]
240+
241+
self.transform(Rotation.from_axis_and_angle(axis, angle, point))
242+
243+
def rotated(self, angle, axis=None, point=None): # type: (...) -> G
244+
"""Returns a rotated copy of this geometry.
245+
246+
Parameters
247+
----------
248+
angle : float
249+
The angle of rotation in radians.
250+
axis : :class:`compas.geometry.Vector`, optional
251+
The axis of rotation.
252+
Defaults to the z-axis.
253+
point : :class:`compas.geometry.Point`, optional
254+
The base point of the rotation axis.
255+
Defaults to the origin.
256+
257+
Returns
258+
-------
259+
:class:`Geometry`
260+
The rotated geometry.
261+
262+
See Also
263+
--------
264+
rotate
265+
translated
266+
scaled
267+
transformed
268+
269+
"""
270+
from compas.geometry import Rotation
271+
272+
if axis is None:
273+
axis = [0.0, 0.0, 1.0]
274+
275+
return self.transformed(Rotation.from_axis_and_angle(axis, angle, point))

0 commit comments

Comments
 (0)