Skip to content

Commit

Permalink
component safety fixes (#108)
Browse files Browse the repository at this point in the history
* FIX: add default values and throw standard warning if no PC or assembly is given

* UPTDATE: better standard values and info added to metadata

* FIX: avoid error created by ghcomponentizer by importing Rhino and System in advance

* FIX: name of output changed from o_clusters to o_cloud

* FIX: missing rg

---------

Co-authored-by: Andrea Settimi <[email protected]>
  • Loading branch information
DamienGilliard and 9and3 authored Sep 20, 2024
1 parent 6744eed commit ec0c5cf
Show file tree
Hide file tree
Showing 19 changed files with 88 additions and 114 deletions.
25 changes: 16 additions & 9 deletions src/gh/components/DF_CAD_segmentator/code.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#! python3

import typing
import System

import Rhino.Geometry as rg
import Rhino
from ghpythonlib.componentbase import executingcomponent as component
from Grasshopper.Kernel import GH_RuntimeMessageLevel as RML


import diffCheck
import diffCheck.df_geometries
from diffCheck.diffcheck_bindings import dfb_segmentation

from diffCheck import df_cvt_bindings
Expand All @@ -16,11 +15,19 @@

class DFCADSegmentator(component):
def RunScript(self,
i_clouds : typing.List[rg.PointCloud],
i_assembly : diffCheck.df_geometries.DFAssembly,
i_angle_threshold : float,
i_association_threshold : float
) -> rg.PointCloud:
i_clouds: System.Collections.Generic.IList[Rhino.Geometry.PointCloud],
i_assembly,
i_angle_threshold: float = 0.1,
i_association_threshold: float = 0.1) -> rg.PointCloud:

if i_clouds is None or i_assembly is None:
self.AddRuntimeMessage(RML.Warning, "Please provide a cloud and an assembly to segmentate")
return None
if i_angle_threshold is None:
i_angle_threshold = 0.1
if i_association_threshold is None:
i_association_threshold = 0.1

o_clusters = []
df_clusters = []
# we make a deepcopy of the input clouds
Expand Down
4 changes: 2 additions & 2 deletions src/gh/components/DF_CAD_segmentator/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
{
"name": "i_angle_threshold",
"nickname": "i_angle_threshold",
"description": "From 0 to 1, it's the sin value. The closer to 0 the less permissive and viceversa to 1.",
"description": "From 0 to 1, it's the sin value. By default 0.1. The closer to 0 the less permissive and viceversa to 1.",
"optional": false,
"allowTreeAccess": true,
"showTypeHints": true,
Expand All @@ -52,7 +52,7 @@
{
"name": "i_association_threshold",
"nickname": "i_association_threshold",
"description": "From 0 to infinite. By default 0.5. The closer to 0 the less permissive your point.",
"description": "From 0 to infinite. By default 0.1. The closer to 0 the less permissive your point.",
"optional": false,
"allowTreeAccess": true,
"showTypeHints": true,
Expand Down
8 changes: 4 additions & 4 deletions src/gh/components/DF_cloud_cloud_distance/code.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#! python3

import typing
import System

import Rhino.Geometry as rg
import Rhino
from ghpythonlib.componentbase import executingcomponent as component

from Grasshopper.Kernel import GH_RuntimeMessageLevel as RML
Expand All @@ -14,8 +14,8 @@

class DFCloudCloudDistance(component):
def RunScript(self,
i_cloud_source: typing.List[rg.PointCloud],
i_cloud_target: typing.List[rg.PointCloud],
i_cloud_source: System.Collections.Generic.List[Rhino.Geometry.PointCloud],
i_cloud_target: System.Collections.Generic.List[Rhino.Geometry.PointCloud],
i_swap: bool):
if i_cloud_source is None or i_cloud_target is None:
ghenv.Component.AddRuntimeMessage(RML.Warning, "Please provide both objects of type point clouds to compare") # noqa: F821
Expand Down
15 changes: 7 additions & 8 deletions src/gh/components/DF_cloud_mesh_distance/code.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
#! python3

import typing
import System

import Rhino.Geometry as rg
import Rhino
from ghpythonlib.componentbase import executingcomponent as component
from Grasshopper.Kernel import GH_RuntimeMessageLevel as RML

import diffCheck
from diffCheck import df_cvt_bindings
from diffCheck import df_error_estimation
from diffCheck.df_geometries import DFAssembly


class DFCloudMeshDistance(component):
def RunScript(self,
i_cloud_source: typing.List[rg.PointCloud],
i_assembly: DFAssembly,
i_signed_flag: bool,
i_swap: bool,
i_analysis_resolution):
i_cloud_source: System.Collections.Generic.List[Rhino.Geometry.PointCloud],
i_assembly,
i_signed_flag: bool,
i_swap: bool,
i_analysis_resolution: float):

if i_analysis_resolution is None:
scalef = diffCheck.df_util.get_doc_2_meters_unitf()
Expand Down
14 changes: 6 additions & 8 deletions src/gh/components/DF_cloud_normal_estimator/code.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#! python3


import Rhino.Geometry as rg
import Rhino
from ghpythonlib.componentbase import executingcomponent as component

from diffCheck import df_cvt_bindings

class DFCloudNormalEstimator(component):
def RunScript(self,
i_cloud : rg.PointCloud = None,
i_knn : int = None,
i_radius : float = None,
i_switch_mode : bool = True
):
o_cloud = rg.PointCloud()
i_cloud: Rhino.Geometry.PointCloud,
i_knn: int,
i_radius: int,
i_switch_mode: bool):
o_cloud = Rhino.Geometry.PointCloud()

df_cloud = df_cvt_bindings.cvt_rhcloud_2_dfcloud(i_cloud)

Expand Down
7 changes: 2 additions & 5 deletions src/gh/components/DF_cloud_size_downsample/code.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#! python3


import Rhino.Geometry as rg
import Rhino
from ghpythonlib.componentbase import executingcomponent as component


from diffCheck import df_cvt_bindings

class DFCloudSizeDownsample(component):
def RunScript(self,
i_cloud: rg.PointCloud,
i_size: int,
) -> rg.PointCloud:
def RunScript(self, i_cloud: Rhino.Geometry.PointCloud, i_size: float) -> Rhino.Geometry.PointCloud:
df_cloud = df_cvt_bindings.cvt_rhcloud_2_dfcloud(i_cloud)
df_cloud.downsample_by_size(i_size)
o_cloud = df_cvt_bindings.cvt_dfcloud_2_rhcloud(df_cloud)
Expand Down
7 changes: 2 additions & 5 deletions src/gh/components/DF_cloud_uniform_downsample/code.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#! python3


import Rhino.Geometry as rg
import Rhino
from ghpythonlib.componentbase import executingcomponent as component


from diffCheck import df_cvt_bindings

class DFCloudUniformDownsample(component):
def RunScript(self,
i_cloud: rg.PointCloud,
i_every_k_points: int,
) -> rg.PointCloud:
def RunScript(self, i_cloud: Rhino.Geometry.PointCloud, i_every_k_points: int) -> Rhino.Geometry.PointCloud:
df_cloud = df_cvt_bindings.cvt_rhcloud_2_dfcloud(i_cloud)
df_cloud.uniform_downsample(i_every_k_points)
o_cloud = df_cvt_bindings.cvt_dfcloud_2_rhcloud(df_cloud)
Expand Down
7 changes: 2 additions & 5 deletions src/gh/components/DF_cloud_voxel_downsample/code.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#! python3


import Rhino.Geometry as rg
import Rhino
from ghpythonlib.componentbase import executingcomponent as component


from diffCheck import df_cvt_bindings

class DFCloudVoxelDownsample(component):
def RunScript(self,
i_cloud: rg.PointCloud,
i_voxel_size: float,
) -> rg.PointCloud:
def RunScript(self, i_cloud: Rhino.Geometry.PointCloud, i_voxel_size: float) -> Rhino.Geometry.PointCloud:
df_cloud = df_cvt_bindings.cvt_rhcloud_2_dfcloud(i_cloud)
df_cloud.voxel_downsample(i_voxel_size)
o_cloud = df_cvt_bindings.cvt_dfcloud_2_rhcloud(df_cloud)
Expand Down
2 changes: 1 addition & 1 deletion src/gh/components/DF_csv_exporter/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
{
"name": "i_export_seperate_files",
"nickname": "i_export_seperate_file",
"nickname": "i_export_seperate_files",
"description": "Whether to export one single file or seperate files per element.",
"optional": true,
"allowTreeAccess": true,
Expand Down
8 changes: 2 additions & 6 deletions src/gh/components/DF_deconstruct_beam/code.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#! python3

import typing

import System

from ghpythonlib.componentbase import executingcomponent as component

from diffCheck.df_geometries import DFBeam


class DFDeconstructBeam(component):
def RunScript(self,
i_beams : typing.List[DFBeam]):
def RunScript(self, i_beams: System.Collections.Generic.List[object]):
o_side_faces, o_joint_faces, o_joint_ids = [], [], []

for i_b in i_beams:
Expand Down
22 changes: 10 additions & 12 deletions src/gh/components/DF_fast_global_registration/code.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! python3


import Rhino.Geometry as rg
import Rhino
from ghpythonlib.componentbase import executingcomponent as component

from Grasshopper.Kernel import GH_RuntimeMessageLevel as RML
Expand All @@ -12,15 +12,13 @@

class DFFastGlobalRegistration(component):
def RunScript(self,
i_cloud_source: rg.PointCloud,
i_cloud_target: rg.PointCloud,
i_radius_kd_search: float,
i_neighbours_kd_search: int,
i_max_corrspondence_dist: float,
i_iteration_number: int,
i_max_tuple_count: int
) -> rg.Transform:
# preliminary checks
i_cloud_source: Rhino.Geometry.PointCloud,
i_cloud_target: Rhino.Geometry.PointCloud,
i_radius_kd_search: float,
i_neighbours_kd_search: int,
i_max_corrspondence_dist: float,
i_iteration_number: int,
i_max_tuple_count: int) -> Rhino.Geometry.Transform:
if i_cloud_source is None or i_cloud_target is None:
ghenv.Component.AddRuntimeMessage(RML.Warning, "Please provide both objects of type point clouds to align") # noqa: F821
return None
Expand Down Expand Up @@ -60,11 +58,11 @@ def RunScript(self,

# cvt df xform to rhino xform
df_xform_matrix = df_xform.transformation_matrix
rh_form = rg.Transform()
rh_form = Rhino.Geometry.Transform()
for i in range(4):
for j in range(4):
rh_form[i, j] = df_xform_matrix[i, j]
if rh_form == rg.Transform.Identity:
if rh_form == Rhino.Geometry.Transform.Identity:
ghenv.Component.AddRuntimeMessage(RML.Warning, "The transformation matrix is identity, no transformation is applied") # noqa: F821
return None

Expand Down
25 changes: 10 additions & 15 deletions src/gh/components/DF_icp_registration/code.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! python3


import Rhino.Geometry as rg
import Rhino
from ghpythonlib.componentbase import executingcomponent as component

from Grasshopper.Kernel import GH_RuntimeMessageLevel as RML
Expand All @@ -12,18 +12,13 @@

class DFICPRegistration(component):
def RunScript(self,
i_cloud_source: rg.PointCloud,
i_cloud_target: rg.PointCloud,

i_use_generalized_icp: bool,

i_max_corrspondence_dist: float,
i_max_iteration: int,

is_t_estimate_pt2pt: bool, # valid only for 03dicp
i_use_point_to_plane: bool # valid only for 03dicp
) -> rg.Transform:
# preliminary checks
i_use_generalized_icp: bool,
i_cloud_source: Rhino.Geometry.PointCloud,
i_cloud_target: Rhino.Geometry.PointCloud,
i_max_corrspondence_dist: float,
i_max_iteration: int,
is_t_estimate_pt2pt: bool,
i_use_point_to_plane: bool) -> Rhino.Geometry.Transform:
if i_cloud_source is None or i_cloud_target is None:
ghenv.Component.AddRuntimeMessage(RML.Warning, "Please provide both objects of type point clouds to align") # noqa: F821
return None
Expand Down Expand Up @@ -75,11 +70,11 @@ def RunScript(self,

# cvt df xform to rhino xform
df_xform_matrix = df_xform.transformation_matrix
rh_form = rg.Transform()
rh_form = Rhino.Geometry.Transform()
for i in range(4):
for j in range(4):
rh_form[i, j] = df_xform_matrix[i, j]
if rh_form == rg.Transform.Identity:
if rh_form == Rhino.Geometry.Transform.Identity:
ghenv.Component.AddRuntimeMessage(RML.Warning, "The transformation matrix is identity, no transformation is applied") # noqa: F821
return None

Expand Down
4 changes: 2 additions & 2 deletions src/gh/components/DF_joint_segmentator/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
from Grasshopper.Kernel import GH_RuntimeMessageLevel as RML
from ghpythonlib.componentbase import executingcomponent as component

import typing
import System

ABSTOL = Rhino.RhinoDoc.ActiveDoc.ModelAbsoluteTolerance

class DFJointSegmentator(component):
def __init__(self):
super(DFJointSegmentator, self).__init__()
def RunScript(self,
i_clusters: typing.List[Rhino.Geometry.PointCloud],
i_clusters: System.Collections.Generic.List[Rhino.Geometry.PointCloud],
i_assembly: diffCheck.df_geometries.DFAssembly,
i_angle_threshold: float,
i_distance_threshold: float):
Expand Down
14 changes: 5 additions & 9 deletions src/gh/components/DF_mesh_to_cloud/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,9 @@


class DFMeshToCloud(component):
def RunScript(self,
i_meshes:System.Collections.Generic.IList[Rhino.Geometry.Mesh],
i_points: int) -> Rhino.Geometry.PointCloud:

if i_meshes is None:
return None

if i_points is None:
i_points = 1000
def RunScript(self, i_mesh: Rhino.Geometry.Mesh, i_points: int) -> Rhino.Geometry.PointCloud:
df_mesh = diffCheck.df_cvt_bindings.cvt_rhmesh_2_dfmesh(i_mesh)
df_cloud = df_mesh.sample_points_uniformly(i_points)

rh_mesh = i_meshes[0]
for i in range(1, len(i_meshes)):
Expand All @@ -33,6 +27,8 @@ def RunScript(self,
df_cloud = df_mesh.sample_points_uniformly(i_points)
rgpoints = [Rhino.Geometry.Point3d(p[0], p[1], p[2]) for p in df_cloud.points]
# convert the df_cloud to a rhino cloud

rgpoints = [Rhino.Geometry.Point3d(pt[0], pt[1], pt[2]) for pt in df_cloud.points]
rh_cloud = Rhino.Geometry.PointCloud(rgpoints)

return [rh_cloud]
6 changes: 3 additions & 3 deletions src/gh/components/DF_normal_segmentator/code.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! python3


import Rhino.Geometry as rg
import Rhino
from ghpythonlib.componentbase import executingcomponent as component


Expand All @@ -12,13 +12,13 @@

class DFCloudNormalSegmentator(component):
def RunScript(self,
i_cloud,
i_cloud: Rhino.Geometry.PointCloud,
i_normal_threshold_degree=None,
i_min_cluster_size=None,
i_use_knn_neighborhood=None,
i_knn_neighborhood_size=None,
i_radius_neighborhood_size=None
) -> rg.PointCloud:
) -> Rhino.Geometry.PointCloud:
o_clusters = []
df_cloud = df_cvt_bindings.cvt_rhcloud_2_dfcloud(i_cloud)

Expand Down
Loading

0 comments on commit ec0c5cf

Please sign in to comment.