Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit methods for geometry and separate methods for doc objects #1378

Merged
merged 7 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added `compas.geometry.curves.curve.Curve.from_native`.
* Added `compas_rhino.geometry.curves.curve.Curve.from_native`.
* Added `compas_rhino.geometry.curves.nurbs.NurbsCurve.from_native`.
* Added `compas_rhino.conversions.breps.brep_to_compas_mesh`.
* Added `compas_rhino.conversions.docobjects.brepobject_to_compas`.
* Added `compas_rhino.conversions.docobjects.curveobject_to_compas`.
* Added `compas_rhino.conversions.docobjects.meshobject_to_compas`.
* Added `compas_rhino.conversions.docobjects.pointobject_to_compas`.
* Added `compas_rhino.conversions.docobjects.surfaceobject_to_compas`.
* Added `compas.datastructures.HashTree` and `compas.datastructures.HashNode`.

### Changed
Expand All @@ -47,7 +53,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Changed `compas.geometry.curves.nurbs.NurbsCurve.__new__` to prevent instantiation of `NurbsCurve` directly.
* Changed `compas_rhino.geometry.curves.new_nurbscurve_from_...` to `nurbscurve_from_...`.
* Fixed `compas_ghpython` Grasshopper components not included in published pakcage.
* Chnaged `compas.colors.Color.coerce` to take color as is, if it is already aninstance of `compas.colors.Color`.
* Changed `compas.colors.Color.coerce` to take color as is, if it is already aninstance of `compas.colors.Color`.
tomvanmele marked this conversation as resolved.
Show resolved Hide resolved
* Changed `compas_rhino.conversions.surfaces.surface_to_compas` to work only with surface geometry.
* Changed `compas_rhino.conversions.curves.curve_to_compas_line` to work only with geometry.
* Changed `compas_rhino.conversions.curves.curve_to_compas_circle` to work only with geometry.
* Changed `compas_rhino.conversions.curves.curve_to_compas_ellipse` to work only with geometry.
* Changed `compas_rhino.conversions.curves.curve_to_compas_polyline` to work only with geometry.
* Changed `compas_rhino.objects.get_point_coordinates` to deprecated (removed in v2.3).
* Changed `compas_rhino.objects.get_line_coordinates` to deprecated (removed in v2.3).
* Changed `compas_rhino.objects.get_polyline_coordinates` to deprecated (removed in v2.3).
* Changed `compas_rhino.objects.get_polygon_coordinates` to deprecated (removed in v2.3).
* Fixed a bug in `worldtransformation` of `compas.scene.SceneObject` to include the object's own frame.

### Removed
Expand Down Expand Up @@ -77,6 +92,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Removed `compas.geometry.curves.curve.new_nurbscurve`.
* Removed `compas_rhino.geometry.curves.new_curve`.
* Removed `compas_rhino.geometry.curves.new_nurbscurve`.
* Removed `compas_rhino.conversions.surfaces.data_to_rhino_surface`.
* Removed `compas_rhino.conversions.surfaces.surface_to_compas_data`.
* Removed `compas_rhino.conversions.surfaces.surface_to_compas_quadmesh`.
* Removed `compas_rhino.conversions.curves.curve_to_compas_data`.

## [2.2.1] 2024-06-25

Expand Down
22 changes: 16 additions & 6 deletions src/compas_rhino/conversions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@
)
from .surfaces import (
surface_to_rhino,
data_to_rhino_surface,
surface_to_compas_data,
surface_to_compas,
surface_to_compas_mesh,
surface_to_compas_quadmesh,
)
from .shapes import (
box_to_rhino,
Expand Down Expand Up @@ -77,6 +74,7 @@
brep_to_compas_cone,
brep_to_compas_cylinder,
brep_to_compas_sphere,
brep_to_compas_mesh,
)
from .extrusions import (
extrusion_to_compas_box,
Expand All @@ -89,6 +87,14 @@
transformation_matrix_to_rhino,
)

from .docobjects import (
brepobject_to_compas,
curveobject_to_compas,
meshobject_to_compas,
pointobject_to_compas,
surfaceobject_to_compas,
)


__all__ = [
"ConversionError",
Expand Down Expand Up @@ -127,11 +133,8 @@
"curve_to_compas",
# surfaces
"surface_to_rhino",
"surface_to_compas_data",
"data_to_rhino_surface",
"surface_to_compas",
"surface_to_compas_mesh",
"surface_to_compas_quadmesh",
# shapes
"box_to_rhino",
"sphere_to_rhino",
Expand All @@ -158,11 +161,18 @@
"brep_to_compas_cone",
"brep_to_compas_cylinder",
"brep_to_compas_sphere",
"brep_to_compas_mesh",
# extrusions
"extrusion_to_compas_box",
"extrusion_to_compas_cylinder",
"extrusion_to_compas_torus",
# transformations
"transformation_to_rhino",
"transformation_matrix_to_rhino",
# docobjects
"brepobject_to_compas",
"curveobject_to_compas",
"meshobject_to_compas",
"pointobject_to_compas",
"surfaceobject_to_compas",
]
113 changes: 112 additions & 1 deletion src/compas_rhino/conversions/breps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

import scriptcontext as sc # type: ignore

from compas.datastructures import Mesh
from compas.geometry import Brep
from compas.tolerance import TOL

from .exceptions import ConversionError
from .geometry import point_to_compas
from .shapes import cone_to_compas
from .shapes import cylinder_to_compas
from .shapes import sphere_to_compas
Expand All @@ -15,7 +20,7 @@


def brep_to_rhino(brep):
"""Convert a COMPAS brep to a Rhino brep.
"""Convert a COMPAS Brep to a Rhino Brep.

Parameters
----------
Expand All @@ -34,6 +39,26 @@ def brep_to_rhino(brep):
# =============================================================================


def brep_to_compas(brep):
"""Convert a Rhino Brep to a COMPAS Brep.

Parameters
----------
brep : :rhino:`Rhino.Geometry.Brep`

Returns
-------
:class:`compas.geometry.Brep`

"""
return Brep.from_native(brep)


# =============================================================================
# To COMPAS Shapes
# =============================================================================


def brep_to_compas_box(brep):
"""Convert a Rhino brep to a COMPAS box.

Expand Down Expand Up @@ -120,3 +145,89 @@ def brep_to_compas_sphere(brep):
result, sphere = face.TryGetSphere()
if result:
return sphere_to_compas(sphere)


# =============================================================================
# To COMPAS Mesh
# =============================================================================


def brep_to_compas_mesh(brep, facefilter=None, cleanup=False, cls=None):
"""Convert the face loops of a Rhino brep to a COMPAS mesh.

Parameters
----------
brep : :class:`Rhino.Geometry.Brep`
A Rhino brep.
facefilter : callable, optional
A filter for selection which Brep faces to include.
If provided, the filter should return True or False per face.
A very simple filter that includes all faces is ``def facefilter(face): return True``.
Default parameter value is None in which case all faces are included.
cleanup : bool, optional
Flag indicating to clean up the result.
Cleaning up means to remove isolated faces and unused vertices.
Default is False.
cls : :class:`compas.datastructures.Mesh`, optional
The type of COMPAS mesh.

Returns
-------
:class:`compas.datastructures.Mesh`
The resulting mesh.

"""
if facefilter and callable(facefilter):
brepfaces = [face for face in brep.Faces if facefilter(face)]
else:
brepfaces = brep.Faces

# vertex maps and face lists
gkey_xyz = {}
faces = []
for face in brepfaces:
loop = face.OuterLoop
curve = loop.To3dCurve()
segments = list(curve.Explode())
a = point_to_compas(segments[0].PointAtStart)
b = point_to_compas(segments[0].PointAtEnd)
a_gkey = TOL.geometric_key(a)
b_gkey = TOL.geometric_key(b)
gkey_xyz[a_gkey] = a
gkey_xyz[b_gkey] = b
face = [a_gkey, b_gkey]
for segment in segments[1:-1]:
b = point_to_compas(segment.PointAtEnd)
b_gkey = TOL.geometric_key(b)
face.append(b_gkey)
gkey_xyz[b_gkey] = b
faces.append(face)

# vertices and faces
gkey_index = {gkey: index for index, gkey in enumerate(gkey_xyz)}
vertices = [list(xyz) for gkey, xyz in gkey_xyz.items()]
faces = [[gkey_index[gkey] for gkey in face] for face in faces]

# remove duplicates from vertexlist
polygons = []
for temp in faces:
face = []
for vertex in temp:
if vertex not in face:
face.append(vertex)
polygons.append(face)

# define mesh type
cls = cls or Mesh
# create mesh
mesh = cls.from_vertices_and_faces(vertices, polygons)

# remove isolated faces
if cleanup:
if mesh.number_of_faces() > 1:
for face in list(mesh.faces()):
if not mesh.face_neighbors(face):
mesh.delete_face(face)
mesh.remove_unused_vertices()

return mesh
Loading
Loading