|
3 | 3 | from __future__ import division |
4 | 4 |
|
5 | 5 | import abc |
6 | | -from compas.geometry import Primitive |
| 6 | +from ..geometry import Geometry |
7 | 7 |
|
8 | 8 |
|
9 | | -class Shape(Primitive): |
| 9 | +class Shape(Geometry): |
10 | 10 | """Base class for geometric shapes.""" |
11 | 11 |
|
12 | 12 | @abc.abstractmethod |
13 | 13 | def to_vertices_and_faces(self): |
14 | 14 | 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 | + """Compute the boolean intersection using the "&" operator of this shape and another. |
| 72 | +
|
| 73 | + Parameters |
| 74 | + ---------- |
| 75 | + other : :class:`Solid` |
| 76 | + The solid to intersect with. |
| 77 | +
|
| 78 | + Returns |
| 79 | + ------- |
| 80 | + :class:`Solid` |
| 81 | + The resulting solid. |
| 82 | +
|
| 83 | + Examples |
| 84 | + -------- |
| 85 | + >>> from compas.geometry import Box, Sphere |
| 86 | + >>> A = Box.from_width_height_depth(2, 2, 2) |
| 87 | + >>> B = Sphere([1, 1, 1], 1.0) |
| 88 | + >>> C = A & B |
| 89 | + """ |
| 90 | + from compas.geometry import boolean_intersection_mesh_mesh |
| 91 | + from compas.geometry import Polyhedron |
| 92 | + A = self.to_vertices_and_faces(triangulated=True) |
| 93 | + B = other.to_vertices_and_faces(triangulated=True) |
| 94 | + V, F = boolean_intersection_mesh_mesh(A, B) |
| 95 | + return Polyhedron(V, F) |
| 96 | + |
| 97 | + def __or__(self, other): |
| 98 | + """Compute the boolean union using the "|" operator of this shape and another. |
| 99 | +
|
| 100 | + Parameters |
| 101 | + ---------- |
| 102 | + other : :class:`Solid` |
| 103 | + The solid to add. |
| 104 | +
|
| 105 | + Returns |
| 106 | + ------- |
| 107 | + :class:`Solid` |
| 108 | + The resulting solid. |
| 109 | +
|
| 110 | + Examples |
| 111 | + -------- |
| 112 | + >>> from compas.geometry import Box, Sphere |
| 113 | + >>> A = Box.from_width_height_depth(2, 2, 2) |
| 114 | + >>> B = Sphere([1, 1, 1], 1.0) |
| 115 | + >>> C = A | B |
| 116 | + """ |
| 117 | + return self.__add__(other) |
0 commit comments