Skip to content

Commit 9b2e945

Browse files
committed
Merge branch 'wip_process' of https://github.com/compas-dev/compas_fab into wip_process
2 parents ffb214d + ede5ae9 commit 9b2e945

8 files changed

Lines changed: 81 additions & 201 deletions

src/compas_fab/robots/constraints.py

Lines changed: 54 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@
22
from __future__ import division
33
from __future__ import print_function
44

5-
from compas import IPY
65
from compas.data import Data
76
from compas.geometry import Rotation
87
from compas.geometry import Scale
98
from compas.geometry import Sphere
9+
from compas.datastructures import Mesh # noqa: F401
10+
from compas.geometry import Box # noqa: F401
11+
from compas.geometry import Frame # noqa: F401
12+
from compas.geometry import Point # noqa: F401
13+
from compas.geometry import Transformation # noqa: F401
14+
from compas_robots import Configuration # noqa: F401
1015

11-
if not IPY:
12-
from typing import TYPE_CHECKING
16+
from typing import TYPE_CHECKING
1317

14-
if TYPE_CHECKING: # pragma: no cover
15-
from typing import Optional # noqa: F401
16-
from typing import Union # noqa: F401
18+
from typing import Optional # noqa: F401
19+
from typing import Union # noqa: F401
1720

18-
from compas.datastructures import Mesh # noqa: F401
19-
from compas.geometry import Box # noqa: F401
20-
from compas.geometry import Frame # noqa: F401
21-
from compas.geometry import Point # noqa: F401
22-
from compas.geometry import Transformation # noqa: F401
23-
from compas_robots import Configuration # noqa: F401
2421

2522
__all__ = [
2623
"BoundingVolume",
@@ -88,8 +85,7 @@ def __data__(self):
8885
}
8986

9087
@classmethod
91-
def from_box(cls, box):
92-
# type: (Box) -> BoundingVolume
88+
def from_box(cls, box: Box) -> "BoundingVolume":
9389
"""Create a :class:`BoundingVolume` from a :class:`compas.geometry.Box`.
9490
9591
Parameters
@@ -114,8 +110,7 @@ def from_box(cls, box):
114110
return cls(cls.BOX, box)
115111

116112
@classmethod
117-
def from_sphere(cls, sphere):
118-
# type: (Sphere) -> BoundingVolume
113+
def from_sphere(cls, sphere: Sphere) -> "BoundingVolume":
119114
"""Create a :class:`BoundingVolume` from a :class:`compas.geometry.Sphere`.
120115
121116
Parameters
@@ -131,8 +126,7 @@ def from_sphere(cls, sphere):
131126
return cls(cls.SPHERE, sphere)
132127

133128
@classmethod
134-
def from_mesh(cls, mesh):
135-
# type: (Mesh) -> BoundingVolume
129+
def from_mesh(cls, mesh: Mesh) -> "BoundingVolume":
136130
"""Create a :class:`BoundingVolume` from a :class:`compas.datastructures.Mesh`.
137131
138132
Parameters
@@ -156,8 +150,7 @@ def from_mesh(cls, mesh):
156150
"""
157151
return cls(cls.MESH, mesh)
158152

159-
def scale(self, scale_factor):
160-
# type: (float) -> None
153+
def scale(self, scale_factor: float) -> None:
161154
"""Scale the volume uniformly.
162155
163156
Parameters
@@ -168,8 +161,7 @@ def scale(self, scale_factor):
168161
S = Scale.from_factors([scale_factor] * 3)
169162
self.transform(S)
170163

171-
def transform(self, transformation):
172-
# type: (Transformation) -> None
164+
def transform(self, transformation: Transformation) -> None:
173165
"""Transform the volume using a :class:`compas.geometry.Transformation`.
174166
175167
Parameters
@@ -183,8 +175,7 @@ def __repr__(self):
183175
"""Printable representation of :class:`BoundingVolume`."""
184176
return "BoundingVolume({!r}, {!r})".format(self.type, self.volume)
185177

186-
def copy(self):
187-
# type: () -> BoundingVolume
178+
def copy(self) -> "BoundingVolume":
188179
"""Make a copy of this :class:`BoundingVolume`.
189180
190181
Returns
@@ -238,8 +229,7 @@ class Constraint(Data):
238229
#: List of possible constraint types.
239230
CONSTRAINT_TYPES = (JOINT, POSITION, ORIENTATION)
240231

241-
def __init__(self, constraint_type, weight=1.0):
242-
# type: (int, Optional[float]) -> None
232+
def __init__(self, constraint_type: int, weight: Optional[float] = 1.0) -> None:
243233
if constraint_type not in self.CONSTRAINT_TYPES:
244234
raise ValueError("Type must be %d, %d or %d" % self.CONSTRAINT_TYPES)
245235
self.type = constraint_type
@@ -251,18 +241,15 @@ def __data__(self):
251241
"weight": self.weight,
252242
}
253243

254-
def transform(self, transformation):
255-
# type: (Transformation) -> None
244+
def transform(self, transformation: Transformation) -> None:
256245
"""Transform the :class:`Constraint`."""
257246
pass
258247

259-
def scale(self, scale_factor):
260-
# type: (float) -> None
248+
def scale(self, scale_factor: float) -> None:
261249
"""Scale the :class:`Constraint`."""
262250
pass
263251

264-
def scaled(self, scale_factor):
265-
# type: (float) -> Constraint
252+
def scaled(self, scale_factor: float) -> "Constraint":
266253
"""Get a scaled copy of this :class:`Constraint`.
267254
268255
Parameters
@@ -274,8 +261,7 @@ def scaled(self, scale_factor):
274261
c.scale(scale_factor)
275262
return c
276263

277-
def copy(self):
278-
# type: () -> Constraint
264+
def copy(self) -> "Constraint":
279265
"""Create a copy of this :class:`Constraint`.
280266
281267
Returns
@@ -320,8 +306,14 @@ class JointConstraint(Constraint):
320306
321307
"""
322308

323-
def __init__(self, joint_name, value, tolerance_above=0.0, tolerance_below=0.0, weight=1.0):
324-
# type: (str, float, Optional[float], Optional[float], Optional[float]) -> None
309+
def __init__(
310+
self,
311+
joint_name: str,
312+
value: float,
313+
tolerance_above: Optional[float] = 0.0,
314+
tolerance_below: Optional[float] = 0.0,
315+
weight: Optional[float] = 1.0,
316+
) -> None:
325317
super(JointConstraint, self).__init__(self.JOINT, weight)
326318
self.joint_name = joint_name
327319
self.value = value
@@ -337,8 +329,7 @@ def __data__(self):
337329
"weight": self.weight,
338330
}
339331

340-
def scale(self, scale_factor):
341-
# type: (float) -> None
332+
def scale(self, scale_factor: float) -> None:
342333
"""Scale (multiply) the constraint with a factor.
343334
344335
Parameters
@@ -356,8 +347,7 @@ def __repr__(self):
356347
self.joint_name, self.value, self.tolerance_above, self.tolerance_below, self.weight
357348
)
358349

359-
def copy(self):
360-
# type: () -> JointConstraint
350+
def copy(self) -> "JointConstraint":
361351
"""Create a copy of this :class:`JointConstraint`.
362352
363353
Returns
@@ -368,8 +358,9 @@ def copy(self):
368358
return cls(self.joint_name, self.value, self.tolerance_above, self.tolerance_below, self.weight)
369359

370360
@classmethod
371-
def joint_constraints_from_configuration(cls, configuration, tolerances_above, tolerances_below):
372-
# type: (Configuration, list, list) -> list[JointConstraint]
361+
def joint_constraints_from_configuration(
362+
cls, configuration: Configuration, tolerances_above: list[float], tolerances_below: list[float]
363+
) -> list["JointConstraint"]:
373364
"""Create joint constraints for all joints of the configuration.
374365
One constraint is created for each joint.
375366
@@ -495,8 +486,9 @@ class OrientationConstraint(Constraint):
495486
496487
"""
497488

498-
def __init__(self, link_name, quaternion, tolerances=None, weight=1.0):
499-
# type: (str, list[float], Optional[list[float]], Optional[float]) -> None
489+
def __init__(
490+
self, link_name: str, quaternion: list[float], tolerances: list[float] = None, weight: list[float] = 1.0
491+
) -> None:
500492
super(OrientationConstraint, self).__init__(self.ORIENTATION, weight)
501493
self.link_name = link_name
502494
self.quaternion = [float(a) for a in list(quaternion)]
@@ -515,8 +507,7 @@ def __data__(self):
515507
"weight": self.weight,
516508
}
517509

518-
def transform(self, transformation):
519-
# type: (Transformation) -> None
510+
def transform(self, transformation: Transformation) -> None:
520511
"""Transform the volume using a :class:`compas.geometry.Transformation`.
521512
522513
Parameters
@@ -534,8 +525,7 @@ def __repr__(self):
534525
self.link_name, self.quaternion, self.tolerances, self.weight
535526
)
536527

537-
def copy(self):
538-
# type: () -> OrientationConstraint
528+
def copy(self) -> "OrientationConstraint":
539529
"""Create a copy of this :class:`OrientationConstraint`.
540530
541531
Returns
@@ -546,8 +536,9 @@ def copy(self):
546536
return cls(self.link_name, self.quaternion[:], self.tolerances[:], self.weight)
547537

548538
@classmethod
549-
def from_frame(cls, pcf_frame, tolerances_orientation, link_name, weight=1.0):
550-
# type: (Frame, list[float], str, Optional[Frame], Optional[float]) -> OrientationConstraint
539+
def from_frame(
540+
cls, pcf_frame: Frame, tolerances_orientation: list[float], link_name: str, weight: Optional[float] = 1.0
541+
) -> "OrientationConstraint":
551542
"""Create an :class:`OrientationConstraint` from a frame on the group's end-effector link.
552543
Only the orientation of the frame is considered for the constraint, expressed
553544
as a quaternion.
@@ -628,8 +619,7 @@ class PositionConstraint(Constraint):
628619
>>> pc = PositionConstraint("link_0", bv, weight=1.0)
629620
"""
630621

631-
def __init__(self, link_name, bounding_volume, weight=1.0):
632-
# type: (str, BoundingVolume, Optional[float]) -> None
622+
def __init__(self, link_name: str, bounding_volume: BoundingVolume, weight: Optional[float] = 1.0) -> None:
633623
super(PositionConstraint, self).__init__(self.POSITION, weight)
634624
self.link_name = link_name
635625
self.bounding_volume = bounding_volume
@@ -643,8 +633,9 @@ def __data__(self):
643633
}
644634

645635
@classmethod
646-
def from_frame(cls, pcf_frame, tolerance_position, link_name, weight=1.0):
647-
# type: (Frame, float, str, Optional[float]) -> PositionConstraint
636+
def from_frame(
637+
cls, pcf_frame: Frame, tolerance_position: float, link_name: str, weight: Optional[float] = 1.0
638+
) -> "PositionConstraint":
648639
"""Create a :class:`PositionConstraint` from a frame on the group's end-effector link.
649640
Only the position of the frame is considered for the constraint.
650641
@@ -678,8 +669,7 @@ def from_frame(cls, pcf_frame, tolerance_position, link_name, weight=1.0):
678669
return cls.from_sphere(link_name, sphere, weight)
679670

680671
@classmethod
681-
def from_box(cls, link_name, box, weight=1.0):
682-
# type: (str, Box, Optional[float]) -> PositionConstraint
672+
def from_box(cls, link_name: str, box: Box, weight: Optional[float] = 1.0) -> "PositionConstraint":
683673
"""Create a :class:`PositionConstraint` from a :class:`compas.geometry.Box`.
684674
685675
Parameters
@@ -707,8 +697,7 @@ def from_box(cls, link_name, box, weight=1.0):
707697
return cls(link_name, bounding_volume, weight)
708698

709699
@classmethod
710-
def from_sphere(cls, link_name, sphere, weight=1.0):
711-
# type: (str, Sphere, Optional[float]) -> PositionConstraint
700+
def from_sphere(cls, link_name: str, sphere: Sphere, weight: Optional[float] = 1.0) -> "PositionConstraint":
712701
"""Create a :class:`PositionConstraint` from a :class:`compas.geometry.Sphere`.
713702
714703
Parameters
@@ -736,8 +725,9 @@ def from_sphere(cls, link_name, sphere, weight=1.0):
736725
return cls(link_name, bounding_volume, weight)
737726

738727
@classmethod
739-
def from_point(cls, link_name, point, tolerance_position, weight=1.0):
740-
# type: (str, Point, float, Optional[float]) -> PositionConstraint
728+
def from_point(
729+
cls, link_name: str, point: Point, tolerance_position: float, weight: Optional[float] = 1.0
730+
) -> "PositionConstraint":
741731
"""Create a :class:`PositionConstraint` from a point.
742732
743733
Parameters
@@ -762,8 +752,7 @@ def from_point(cls, link_name, point, tolerance_position, weight=1.0):
762752
return cls.from_sphere(link_name, sphere, weight)
763753

764754
@classmethod
765-
def from_mesh(cls, link_name, mesh, weight=1.0):
766-
# type: (str, Mesh, Optional[float]) -> PositionConstraint
755+
def from_mesh(cls, link_name: str, mesh: Mesh, weight: Optional[float] = 1.0) -> "PositionConstraint":
767756
"""Create a :class:`PositionConstraint` from a :class:`compas.datastructures.Mesh`.
768757
769758
Parameters
@@ -791,8 +780,7 @@ def from_mesh(cls, link_name, mesh, weight=1.0):
791780
bounding_volume = BoundingVolume.from_mesh(mesh)
792781
return cls(link_name, bounding_volume, weight)
793782

794-
def scale(self, scale_factor):
795-
# type: (float) -> None
783+
def scale(self, scale_factor: float) -> None:
796784
"""Scale the :attr:`bounding_volume` uniformly.
797785
798786
Parameters
@@ -802,8 +790,7 @@ def scale(self, scale_factor):
802790
"""
803791
self.bounding_volume.scale(scale_factor)
804792

805-
def transform(self, transformation):
806-
# type: (Transformation) -> None
793+
def transform(self, transformation: Transformation) -> None:
807794
"""Transform the :attr:`bounding_volume` using a :class:`compas.geometry.Transformation`.
808795
809796
Parameters
@@ -816,8 +803,7 @@ def __repr__(self):
816803
"""Printable representation of :class:`PositionConstraint`."""
817804
return "PositionConstraint({!r}, {!r}, {!r})".format(self.link_name, self.bounding_volume, self.weight)
818805

819-
def copy(self):
820-
# type: () -> PositionConstraint
806+
def copy(self) -> "PositionConstraint":
821807
"""Create a copy of this :class:`PositionConstraint`.
822808
823809
Returns

0 commit comments

Comments
 (0)