From c1da3844b4cb89d45cd8ea32717f4656b3c13e06 Mon Sep 17 00:00:00 2001 From: tomvanmele Date: Thu, 22 Aug 2024 11:41:58 +0200 Subject: [PATCH] re-added updated version of mesh normals drawing methods --- CHANGELOG.md | 4 + src/compas_rhino/scene/meshobject.py | 139 +++++++++++++++------------ 2 files changed, 80 insertions(+), 63 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9e4abb584e..50be02b4b14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Changed `compas_rhino.scene.RhinoMeshObject` to keep track of element-guid pairs in dicts. * Changed `compas.scene.Scene._guids` to a default value of `[]`. * Fixed bug due to missing import in `compas_rhino.scene.graphobject`. +* Changed `compas_rhino.scene.RhinoMeshObject.draw_vertexnormals` to use the same selection of vertices as `draw_vertices`. +* Changed `compas_rhino.scene.RhinoMeshObject.draw_vertexnormals` to use the corresponding vertex color if no color is specified. +* Changed `compas_rhino.scene.RhinoMeshObject.draw_facenormals` to use the same selection of vertices as `draw_faces`. +* Changed `compas_rhino.scene.RhinoMeshObject.draw_facenormals` to use the corresponding face color if no color is specified. ### Removed diff --git a/src/compas_rhino/scene/meshobject.py b/src/compas_rhino/scene/meshobject.py index 18b945bfe38..2413f13980f 100644 --- a/src/compas_rhino/scene/meshobject.py +++ b/src/compas_rhino/scene/meshobject.py @@ -6,6 +6,9 @@ import scriptcontext as sc # type: ignore import compas_rhino.objects +from compas.colors import Color +from compas.geometry import Line +from compas.geometry import Point from compas.geometry import centroid_points from compas.scene import MeshObject from compas_rhino.conversions import line_to_rhino @@ -447,86 +450,96 @@ def draw_facelabels(self, text, color=None, group=None, fontheight=10, fontface= # draw normals # ========================================================================== - # def draw_vertexnormals(self, vertices=None, color=(0, 255, 0), scale=1.0, group=None): - # """Draw the normals at the vertices of the mesh. + def draw_vertexnormals(self, color=None, scale=1.0, group=None): + """Draw the normals at the vertices of the mesh. - # Parameters - # ---------- - # vertices : list[int], optional - # A selection of vertex normals to draw. - # Default is to draw all vertex normals. - # color : tuple[int, int, int] | tuple[float, float, float] | :class:`compas.colors.Color`, optional - # The color specification of the normal vectors. - # scale : float, optional - # Scale factor for the vertex normals. - # group : str, optional - # The name of a group to join the created Rhino objects in. + Parameters + ---------- + color : tuple[int, int, int] | tuple[float, float, float] | :class:`compas.colors.Color`, optional + The color specification of the normal vectors. + If no color is specified, the color of the corresponding vertex is used. + scale : float, optional + Scale factor for the vertex normals. + group : str, optional + The name of a group to join the created Rhino objects in. - # Returns - # ------- - # list[System.Guid] - # The GUIDs of the created Rhino objects. + Returns + ------- + list[System.Guid] + The GUIDs of the created Rhino objects. - # """ - # guids = [] + """ + guids = [] - # color = Color.coerce(color) + vertices = list(self.mesh.vertices()) if self.show_vertices is True else self.show_vertices or [] + transformation = transformation_to_rhino(self.worldtransformation) - # for vertex in vertices or self.mesh.vertices(): # type: ignore - # name = "{}.vertex.{}.normal".format(self.mesh.name, vertex) # type: ignore - # attr = self.compile_attributes(name=name, color=color) + color = Color.coerce(color) - # point = self.mesh.vertex_point(vertex) - # normal = self.mesh.vertex_normal(vertex) # type: ignore + for vertex in vertices: + name = "{}.vertex.{}.normal".format(self.mesh.name, vertex) # type: ignore + attr = self.compile_attributes(name=name, color=color or self.vertexcolor[vertex]) # type: ignore - # guid = sc.doc.Objects.AddLine(point_to_rhino(point), point_to_rhino(point + normal * scale), attr) - # guids.append(guid) + point = self.mesh.vertex_point(vertex) + normal = self.mesh.vertex_normal(vertex) # type: ignore + line = Line.from_point_and_vector(point, normal) - # if group: - # self.add_to_group(group, guids) + geometry = line_to_rhino(line) + geometry.Transform(transformation) - # self._guids_vertexnormals = guids - # self._guids += guids - # return guids + guid = sc.doc.Objects.AddLine(geometry, attr) + guids.append(guid) - # def draw_facenormals(self, faces=None, color=(0, 255, 255), scale=1.0, group=None): - # """Draw the normals of the faces. + if group: + self.add_to_group(group, guids) - # Parameters - # ---------- - # faces : list[int], optional - # A selection of face normals to draw. - # Default is to draw all face normals. - # color : tuple[int, int, int] | tuple[float, float, float] | :class:`compas.colors.Color`, optional - # The color specification of the normal vectors. - # scale : float, optional - # Scale factor for the face normals. - # group : str, optional - # The name of a group to join the created Rhino objects in. + self._guids_vertexnormals = guids + self._guids += guids + return guids - # Returns - # ------- - # list[System.Guid] - # The GUIDs of the created Rhino objects. + def draw_facenormals(self, color=None, scale=1.0, group=None): + """Draw the normals of the faces. - # """ - # guids = [] + Parameters + ---------- + color : tuple[int, int, int] | tuple[float, float, float] | :class:`compas.colors.Color`, optional + The color specification of the normal vectors. + If no color is specified, the color of the corresponding face is used. + scale : float, optional + Scale factor for the face normals. + group : str, optional + The name of a group to join the created Rhino objects in. + + Returns + ------- + list[System.Guid] + The GUIDs of the created Rhino objects. + + """ + guids = [] - # color = Color.coerce(color) + faces = list(self.mesh.faces()) if self.show_faces is True else self.show_faces or [] + transformation = transformation_to_rhino(self.worldtransformation) - # for face in faces or self.mesh.faces(): # type: ignore - # name = "{}.face.{}.normal".format(self.mesh.name, face) # type: ignore - # attr = self.compile_attributes(name=name, color=color) + color = Color.coerce(color) - # point = Point(*centroid_points([self.vertex_xyz[vertex] for vertex in self.mesh.face_vertices(face)])) # type: ignore - # normal = self.mesh.face_normal(face) # type: ignore + for face in faces: + name = "{}.face.{}.normal".format(self.mesh.name, face) # type: ignore + attr = self.compile_attributes(name=name, color=color or self.facecolor[face]) # type: ignore - # guid = sc.doc.Objects.AddLine(point_to_rhino(point), point_to_rhino(point + normal * scale), attr) - # guids.append(guid) + point = self.mesh.face_centroid(face) + normal = self.mesh.face_normal(face) + line = Line.from_point_and_vector(point, normal) - # if group: - # self.add_to_group(group, guids) + geometry = line_to_rhino(line) + geometry.Transform(transformation) - # self._guids_facenormals = guids + guid = sc.doc.Objects.AddLine(geometry, attr) + guids.append(guid) - # return guids + if group: + self.add_to_group(group, guids) + + self._guids_facenormals = guids + + return guids