|
| 1 | +*********************** |
| 2 | +Boundary Representation |
| 3 | +*********************** |
| 4 | + |
| 5 | +.. rst-class:: lead |
| 6 | + |
| 7 | +Boundary representation (Brep) support is realized in COMPAS using its `plugin` system. |
| 8 | +The expected interface for Brep related classes is defined in the :mod:`compas.geometry.brep` module |
| 9 | +whereas the actual implementation is context dependent and implemented using plugins. |
| 10 | + |
| 11 | +.. currentmodule:: compas.geometry |
| 12 | + |
| 13 | +.. highlight:: python |
| 14 | + |
| 15 | +Brep Basics |
| 16 | +=========== |
| 17 | +Brep is a data structure used to describe a shape by means of recording topological and geometrical information of the shape's boundaries. |
| 18 | +Some topological properties are associated with an underlying geometry, while others are purely topological. |
| 19 | + |
| 20 | +A Brep is comprised of the following: |
| 21 | + |
| 22 | +.. rst-class:: table table-bordered |
| 23 | + |
| 24 | +.. list-table:: |
| 25 | + :widths: auto |
| 26 | + :header-rows: 1 |
| 27 | + |
| 28 | + * - Topology |
| 29 | + - Geometry |
| 30 | + - Description |
| 31 | + * - Vertex |
| 32 | + - 3D Point |
| 33 | + - The most basic element of a Brep, geometrically described as a point in 3D space. |
| 34 | + * - Edge |
| 35 | + - 3D Curve |
| 36 | + - An edge has a start vertex and an end vertex. The underlying 3D curve describes the geometry of the edge (Line, Circle etc.). Closed edges feature start_vertex == end_vertex. |
| 37 | + * - Loop |
| 38 | + - None |
| 39 | + - A collection of trims which define the inner or outer boundary of a face. |
| 40 | + * - Face |
| 41 | + - Surface |
| 42 | + - Defines the geometry of one of the shape's faces using a surface. Associated with at least one loop which describes the trimmed outer boundary of the surface. Inner loops are referred to as holes in the face. |
| 43 | + * - Trim |
| 44 | + - 2D Curve |
| 45 | + - A 2D curve which trims a face. Trims are associated with a corresponding edge. |
| 46 | + |
| 47 | + |
| 48 | +Getting Started with COMPAS Brep |
| 49 | +================================ |
| 50 | + |
| 51 | +To create an empty `Brep` |
| 52 | + |
| 53 | +.. code-block:: |
| 54 | +
|
| 55 | + >>> from compas.geometry import Brep |
| 56 | + >>> brep = Brep() |
| 57 | +
|
| 58 | +
|
| 59 | +Notice that the type of the actual instance created by `Brep()` will differ depending on the currently available backend. |
| 60 | +For example, when in Rhino |
| 61 | + |
| 62 | +.. code-block:: |
| 63 | +
|
| 64 | + >>> type(brep) |
| 65 | + compas_rhino.geometry.RhinoBrep |
| 66 | +
|
| 67 | +Every backend is expected to implement some alternative constructors |
| 68 | + |
| 69 | +.. code-block:: |
| 70 | +
|
| 71 | + >>> from compas.geometry import Box |
| 72 | + >>> from compas.geometry import Brep |
| 73 | + >>> ... |
| 74 | + >>> box = Box.from_width_height_depth(5., 5., 5.) |
| 75 | + >>> brep_box = Brep.from_box(box) |
| 76 | +
|
| 77 | +
|
| 78 | +`Brep` can also be instantiated from an instance of a backend native `Brep` |
| 79 | + |
| 80 | +.. code-block:: |
| 81 | +
|
| 82 | + >>> import Rhino |
| 83 | + >>> from compas.geometry import Brep |
| 84 | + >>> ... |
| 85 | + >>> Brep.from_brep(Rhino.Geometry.Brep()) |
| 86 | +
|
| 87 | +Brep operations |
| 88 | +=============== |
| 89 | + |
| 90 | +Trimming a Brep in Grasshopper |
| 91 | + |
| 92 | +.. code-block:: |
| 93 | +
|
| 94 | + from compas.geometry import Frame |
| 95 | + from compas.geometry import Point |
| 96 | + from compas.geometry import Brep |
| 97 | +
|
| 98 | + box = Box.from_width_height_depth(5, 5, 10) |
| 99 | +
|
| 100 | + brep = Brep.from_box(box) |
| 101 | + cutting_plane = Frame(Point(0, 2.5, 0), [1, 0, 0], [0, 1, 1.5]) |
| 102 | +
|
| 103 | + brep.trim(cutting_plane) |
| 104 | +
|
| 105 | +|pic1| |pic2| |
| 106 | + |
| 107 | +.. |pic1| image:: files/box_w_plane.png |
| 108 | + :width: 48% |
| 109 | + |
| 110 | +.. |pic2| image:: files/trimmed_box.png |
| 111 | + :width: 48% |
| 112 | + |
| 113 | + |
| 114 | +Implementing a new backend |
| 115 | +========================== |
| 116 | + |
| 117 | +If you wish to create an additional backend to `Brep` in your package, this can be done using the plugin system of COMPAS. |
| 118 | + |
| 119 | +Create a Brep type in your package which inherits from :class:`compas.geometry.Brep` and override the `__new__` dundle as follows: |
| 120 | + |
| 121 | +.. code-block:: |
| 122 | +
|
| 123 | + from compas.geometry import Brep |
| 124 | +
|
| 125 | + class OccBrep(Brep): |
| 126 | +
|
| 127 | + def __new__(cls, *args, **kwargs): |
| 128 | + # This breaks the endless recursion when calling `compas.geometry.Brep()` and allows |
| 129 | + # having Brep here as the parent class. Otherwise OccBrep() calls Brep.__new__() |
| 130 | + # which calls OccBrep() and so on... |
| 131 | + return object.__new__(cls, *args, **kwargs) |
| 132 | +
|
| 133 | +Whenever instantiating `compas.geometry.Brep`, the actual instantiation is delegated to the available factory plugin |
| 134 | + |
| 135 | +.. code-block:: |
| 136 | +
|
| 137 | + @plugin(category="factories", requires=["OCC"]) |
| 138 | + def new_brep(*args, **kwargs): |
| 139 | + # Note: this is called inside Brep.__new__, thus Brep.__init__ will be ran by the interpreter |
| 140 | + # upon returning from __new__. This means any fully initialized instance returned here will be overwritten! |
| 141 | + return object.__new__(OccBrep, *args, **kwargs) |
| 142 | +
|
| 143 | +Now, a call to `compas.geometry.Brep()` will result in an instance of `your.package.OccBrep`, given that the plugin is available and loaded. |
| 144 | +`OccBrep` encapsulates the native Brep object available by the underlying implementation. If necessary, it can be accessed using `occ_brep_instance.native_brep`. |
| 145 | + |
| 146 | +Implementing the `compas.data.Data` interface |
| 147 | +--------------------------------------------- |
| 148 | +A powerful feature of this approach is to be able to serialize a Brep created in one backend and de-serialize it using another. |
| 149 | +For that, it is required that `your.package.OccBrep` implements the :class:`compas.data.Data` interface and follows the unified serialization protocol. |
| 150 | + |
0 commit comments