Skip to content

Commit e809e05

Browse files
committed
rebase shape and add boolean operators
1 parent 4e323c0 commit e809e05

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

src/compas/geometry/shapes/_shape.py

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,77 @@
33
from __future__ import division
44

55
import abc
6-
from compas.geometry import Primitive
6+
from ..geometry import Geometry
77

88

9-
class Shape(Primitive):
9+
class Shape(Geometry):
1010
"""Base class for geometric shapes."""
1111

1212
@abc.abstractmethod
1313
def to_vertices_and_faces(self):
1414
pass
15+
16+
def __add__(self, other):
17+
"""Compute the boolean union using the "+" operator of this shape and another.
18+
19+
Parameters
20+
----------
21+
other : :class:`Solid`
22+
The solid to add.
23+
24+
Returns
25+
-------
26+
:class:`Solid`
27+
The resulting solid.
28+
29+
Examples
30+
--------
31+
>>> from compas.geometry import Box, Sphere
32+
>>> A = Box.from_width_height_depth(2, 2, 2)
33+
>>> B = Sphere([1, 1, 1], 1.0)
34+
>>> C = A + B
35+
"""
36+
from compas.geometry import boolean_union_mesh_mesh
37+
from compas.geometry import Polyhedron
38+
A = self.to_vertices_and_faces(triangulated=True)
39+
B = other.to_vertices_and_faces(triangulated=True)
40+
V, F = boolean_union_mesh_mesh(A, B)
41+
return Polyhedron(V, F)
42+
43+
def __sub__(self, other):
44+
"""Compute the boolean difference using the "-" operator of this shape and another.
45+
46+
Parameters
47+
----------
48+
other : :class:`Solid`
49+
The solid to subtract.
50+
51+
Returns
52+
-------
53+
:class:`Solid`
54+
The resulting solid.
55+
56+
Examples
57+
--------
58+
>>> from compas.geometry import Box, Sphere
59+
>>> A = Box.from_width_height_depth(2, 2, 2)
60+
>>> B = Sphere([1, 1, 1], 1.0)
61+
>>> C = A - B
62+
"""
63+
from compas.geometry import boolean_difference_mesh_mesh
64+
from compas.geometry import Polyhedron
65+
A = self.to_vertices_and_faces(triangulated=True)
66+
B = other.to_vertices_and_faces(triangulated=True)
67+
V, F = boolean_difference_mesh_mesh(A, B)
68+
return Polyhedron(V, F)
69+
70+
def __and__(self, other):
71+
from compas.geometry import boolean_intersection_mesh_mesh
72+
from compas.geometry import Polyhedron
73+
A = self.to_vertices_and_faces(triangulated=True)
74+
B = other.to_vertices_and_faces(triangulated=True)
75+
V, F = boolean_intersection_mesh_mesh(A, B)
76+
return Polyhedron(V, F)
77+
78+
def __or__(self, other):
79+
return self.__add__(other)

0 commit comments

Comments
 (0)