11.. _compas_slicer_example_2 :
22
33************************************
4- Simple curved interpolation slicing
4+ Interpolation slicing
55************************************
66
77A general introduction of the concepts organization of compas_slicer can be found in the :ref: `introduction tutorial <compas_slicer_tutorial_1_introduction >`.
@@ -12,13 +12,25 @@ as it explains the main concepts of compas_slicer.
1212Having done that, in this example, we go through the basics of using the non-planar interpolation slicer, which generates
1313paths by interpolating user-defined boundaries.
1414This example uses the method described in `Print Paths KeyFraming <https://dl.acm.org/doi/fullHtml/10.1145/3424630.3425408 >`_.
15+ Its files can be found in the folder `/examples/2_curved_slicing/ `
1516
1617.. figure :: figures/02_curved_slicing.PNG
1718 :figclass: figure
1819 :class: figure-img img-fluid
1920
2021 *Result of simple curved slicing. *
2122
23+ Note that this example has three different data folders (`/data_costa_surface/ `, `/data_vase/ `, `/data_Y_shape/ `). Feel free
24+ to change the DATA_PATH parameter (in the code block below) to point to any of these folders so that you can slice its contents. To visualize the results,
25+ open the `curved_slicing_master.gh ` and select the desired folder in the inputs section (top left). You will only be able to visualize
26+ the results after you have run the python file that generates them.
27+
28+ .. figure :: figures/input_folder.png
29+ :figclass: figure
30+ :class: figure-img img-fluid
31+
32+ *Selection of input folder in Grasshopper (from `curved_slicing_master.gh`). *
33+
2234Imports and initialization
2335==========================
2436
@@ -29,23 +41,23 @@ Imports and initialization
2941 import logging
3042 import compas_slicer.utilities as utils
3143 from compas_slicer.slicers import InterpolationSlicer
32- from compas_slicer.post_processing import simplify_paths_rdp_igl
44+ from compas_slicer.post_processing import simplify_paths_rdp
3345 from compas_slicer.pre_processing import InterpolationSlicingPreprocessor
3446 from compas_slicer.print_organization import set_extruder_toggle, set_linear_velocity_by_range
3547 from compas_slicer.print_organization import add_safety_printpoints
3648 from compas_slicer.pre_processing import create_mesh_boundary_attributes
3749 from compas_slicer.print_organization import InterpolationPrintOrganizer
3850 from compas_slicer.post_processing import seams_smooth
3951 from compas_slicer.print_organization import smooth_printpoints_up_vectors, smooth_printpoints_layer_heights
40- from compas_slicer.post_processing import generate_brim
41- from compas_view2 import app
52+ import time
4253
4354 logger = logging.getLogger(' logger' )
4455 logging.basicConfig(format = ' %(levelname)s - %(message)s ' , level = logging.INFO )
4556
46- DATA_PATH = os.path.join(os.path.dirname(__file__ ), ' data_basic_example ' )
57+ DATA_PATH = os.path.join(os.path.dirname(__file__ ), ' data_Y_shape ' ) # set desired folder name
4758 OUTPUT_PATH = utils.get_output_directory(DATA_PATH )
48- OBJ_INPUT_NAME = os.path.join(DATA_PATH , ' vase.obj' )
59+ OBJ_INPUT_NAME = os.path.join(DATA_PATH , ' mesh.obj' )
60+
4961
5062 Slicing process
5163===============
@@ -56,9 +68,17 @@ Slicing process
5668 mesh = Mesh.from_obj(os.path.join(DATA_PATH , OBJ_INPUT_NAME ))
5769
5870
59- The interpolation slicer works by interpolating boundaries provided by the user. Each boundary is represented by a list
60- of vertex indices, that have been saved in the json files.
71+ The interpolation slicer works by interpolating two boundaries provided by the user. Each boundary is represented by a list
72+ of vertex indices, that have been saved in the json files. You can create these json files using the following grasshopper
73+ sequence from the file: `curved_slicing_master.gh `
6174
75+ .. figure :: figures/create_boundaries.png
76+ :figclass: figure
77+ :class: figure-img img-fluid
78+
79+ *Creation of boundary json files (from `curved_slicing_master.gh`). *
80+
81+ Then the boundary json files are loaded as follows:
6282.. code-block :: python
6383
6484 # --- Load targets (boundaries)
@@ -74,11 +94,9 @@ determines how dense the layers will be generated on the surface.
7494
7595.. code-block :: python
7696
77- avg_layer_height = 15 .0
97+ avg_layer_height = 2 .0
7898 parameters = {
7999 ' avg_layer_height' : avg_layer_height, # controls number of curves that will be generated
80- ' min_layer_height' : 0.3 ,
81- ' max_layer_height' : 5.0 # 2.0,
82100 }
83101
84102 The ``InterpolationSlicingPreprocessor `` sets up all the data that are necessary for the interpolation process.
@@ -102,9 +120,8 @@ options are available for all slicers.
102120 slicer.slice_model() # compute_norm_of_gradient contours
103121
104122 # post processing
105- generate_brim(slicer, layer_width = 3.0 , number_of_brim_offsets = 5 )
106- seams_smooth(slicer, smooth_distance = 10 )
107- simplify_paths_rdp_igl(slicer, threshold = 1.0 )
123+ simplify_paths_rdp(slicer, threshold = 0.25 )
124+ seams_smooth(slicer, smooth_distance = 3 )
108125 slicer.printout_info()
109126 utils.save_to_json(slicer.to_data(), OUTPUT_PATH , ' curved_slicer.json' )
110127
@@ -122,13 +139,14 @@ that is necessary for the print process.
122139 print_organizer = InterpolationPrintOrganizer(slicer, parameters, DATA_PATH )
123140 print_organizer.create_printpoints()
124141
142+ smooth_printpoints_up_vectors(print_organizer, strength = 0.5 , iterations = 10 )
143+ smooth_printpoints_layer_heights(print_organizer, strength = 0.5 , iterations = 5 )
144+
125145 set_linear_velocity_by_range(print_organizer, param_func = lambda ppt : ppt.layer_height,
126146 parameter_range = [avg_layer_height* 0.5 , avg_layer_height* 2.0 ],
127147 velocity_range = [150 , 70 ], bound_remapping = False )
128148 set_extruder_toggle(print_organizer, slicer)
129149 add_safety_printpoints(print_organizer, z_hop = 10.0 )
130- smooth_printpoints_up_vectors(print_organizer, strength = 0.5 , iterations = 10 )
131- smooth_printpoints_layer_heights(print_organizer, strength = 0.5 , iterations = 5 )
132150
133151 Output json file with printpoints.
134152
@@ -138,20 +156,9 @@ Output json file with printpoints.
138156 printpoints_data = print_organizer.output_printpoints_dict()
139157 utils.save_to_json(printpoints_data, OUTPUT_PATH , ' out_printpoints.json' )
140158
141- Visualize the result using compas_viewer2
142-
143- .. code-block :: python
144-
145- # ----- Visualize
146- viewer = app.App(width = 1600 , height = 1000 )
147- # slicer.visualize_on_viewer(viewer, visualize_mesh=False, visualize_paths=True)
148- print_organizer.visualize_on_viewer(viewer, visualize_printpoints = True )
149- viewer.show()
150-
151-
152- Once the slicing process is finished, you can use the compas_slicer grasshopper components to visualize the results,
153- described in the :ref: `grasshopper tutorial <compas_slicer_tutorial_2 >`.
154159
160+ Once the slicing process is finished, you can open the `curved_slicing_master.gh to visualize the results. More information on
161+ this visualization is given in :ref: `grasshopper tutorial <compas_slicer_tutorial_2>`.
155162
156163
157164Final script
@@ -166,82 +173,77 @@ The completed final script can be found below:
166173 import logging
167174 import compas_slicer.utilities as utils
168175 from compas_slicer.slicers import InterpolationSlicer
169- from compas_slicer.post_processing import simplify_paths_rdp_igl
176+ from compas_slicer.post_processing import simplify_paths_rdp
170177 from compas_slicer.pre_processing import InterpolationSlicingPreprocessor
171178 from compas_slicer.print_organization import set_extruder_toggle, set_linear_velocity_by_range
172179 from compas_slicer.print_organization import add_safety_printpoints
173180 from compas_slicer.pre_processing import create_mesh_boundary_attributes
174181 from compas_slicer.print_organization import InterpolationPrintOrganizer
175182 from compas_slicer.post_processing import seams_smooth
176183 from compas_slicer.print_organization import smooth_printpoints_up_vectors, smooth_printpoints_layer_heights
177- from compas_slicer.post_processing import generate_brim
178- from compas_view2 import app
179184 import time
180185
181186 logger = logging.getLogger(' logger' )
182187 logging.basicConfig(format = ' %(levelname)s - %(message)s ' , level = logging.INFO )
183188
184- DATA_PATH = os.path.join(os.path.dirname(__file__ ), ' data_basic_example ' )
189+ DATA_PATH = os.path.join(os.path.dirname(__file__ ), ' data_Y_shape ' )
185190 OUTPUT_PATH = utils.get_output_directory(DATA_PATH )
186- OBJ_INPUT_NAME = os.path.join(DATA_PATH , ' vase .obj' )
191+ OBJ_INPUT_NAME = os.path.join(DATA_PATH , ' mesh .obj' )
187192
188193
189- start_time = time.time()
194+ def main ():
195+ start_time = time.time()
190196
191- # --- Load initial_mesh
192- mesh = Mesh.from_obj(os.path.join(DATA_PATH , OBJ_INPUT_NAME ))
197+ # --- Load initial_mesh
198+ mesh = Mesh.from_obj(os.path.join(DATA_PATH , OBJ_INPUT_NAME ))
193199
194- # --- Load targets (boundaries)
195- low_boundary_vs = utils.load_from_json(DATA_PATH , ' boundaryLOW.json' )
196- high_boundary_vs = utils.load_from_json(DATA_PATH , ' boundaryHIGH.json' )
197- create_mesh_boundary_attributes(mesh, low_boundary_vs, high_boundary_vs)
200+ # --- Load targets (boundaries)
201+ low_boundary_vs = utils.load_from_json(DATA_PATH , ' boundaryLOW.json' )
202+ high_boundary_vs = utils.load_from_json(DATA_PATH , ' boundaryHIGH.json' )
203+ create_mesh_boundary_attributes(mesh, low_boundary_vs, high_boundary_vs)
198204
199- avg_layer_height = 15 .0
205+ avg_layer_height = 2 .0
200206
201- parameters = {
202- ' avg_layer_height' : avg_layer_height, # controls number of curves that will be generated
203- ' min_layer_height' : 0.3 ,
204- ' max_layer_height' : 5.0 # 2.0,
205- }
207+ parameters = {
208+ ' avg_layer_height' : avg_layer_height, # controls number of curves that will be generated
209+ }
206210
207- preprocessor = InterpolationSlicingPreprocessor(mesh, parameters, DATA_PATH )
208- preprocessor.create_compound_targets()
209- g_eval = preprocessor.create_gradient_evaluation(norm_filename = ' gradient_norm.json' , g_filename = ' gradient.json' ,
210- target_1 = preprocessor.target_LOW,
211- target_2 = preprocessor.target_HIGH)
212- preprocessor.find_critical_points(g_eval, output_filenames = [' minima.json' , ' maxima.json' , ' saddles.json' ])
211+ preprocessor = InterpolationSlicingPreprocessor(mesh, parameters, DATA_PATH )
212+ preprocessor.create_compound_targets()
213+ g_eval = preprocessor.create_gradient_evaluation(norm_filename = ' gradient_norm.json' , g_filename = ' gradient.json' ,
214+ target_1 = preprocessor.target_LOW,
215+ target_2 = preprocessor.target_HIGH)
216+ preprocessor.find_critical_points(g_eval, output_filenames = [' minima.json' , ' maxima.json' , ' saddles.json' ])
213217
214- # --- slicing
215- slicer = InterpolationSlicer(mesh, preprocessor, parameters)
216- slicer.slice_model() # compute_norm_of_gradient contours
217- generate_brim(slicer, layer_width = 3.0 , number_of_brim_offsets = 5 )
218- seams_smooth(slicer, smooth_distance = 10 )
218+ # --- slicing
219+ slicer = InterpolationSlicer(mesh, preprocessor, parameters)
220+ slicer.slice_model() # compute_norm_of_gradient contours
219221
220- simplify_paths_rdp_igl(slicer, threshold = 0.5 )
221- slicer.printout_info()
222- utils.save_to_json(slicer.to_data(), OUTPUT_PATH , ' curved_slicer.json' )
222+ simplify_paths_rdp(slicer, threshold = 0.25 )
223+ seams_smooth(slicer, smooth_distance = 3 )
224+ slicer.printout_info()
225+ utils.save_to_json(slicer.to_data(), OUTPUT_PATH , ' curved_slicer.json' )
223226
224- # --- Print organizer
225- print_organizer = InterpolationPrintOrganizer(slicer, parameters, DATA_PATH )
226- print_organizer.create_printpoints()
227+ # --- Print organizer
228+ print_organizer = InterpolationPrintOrganizer(slicer, parameters, DATA_PATH )
229+ print_organizer.create_printpoints()
227230
228- set_linear_velocity_by_range(print_organizer, param_func = lambda ppt : ppt.layer_height,
229- parameter_range = [avg_layer_height* 0.5 , avg_layer_height* 2.0 ],
230- velocity_range = [150 , 70 ], bound_remapping = False )
231- set_extruder_toggle(print_organizer, slicer)
232- add_safety_printpoints(print_organizer, z_hop = 10.0 )
233- smooth_printpoints_up_vectors(print_organizer, strength = 0.5 , iterations = 10 )
234- smooth_printpoints_layer_heights(print_organizer, strength = 0.5 , iterations = 5 )
231+ smooth_printpoints_up_vectors(print_organizer, strength = 0.5 , iterations = 10 )
232+ smooth_printpoints_layer_heights(print_organizer, strength = 0.5 , iterations = 5 )
235233
236- # --- Save printpoints dictionary to json file
237- printpoints_data = print_organizer.output_printpoints_dict()
238- utils.save_to_json(printpoints_data, OUTPUT_PATH , ' out_printpoints.json' )
234+ set_linear_velocity_by_range(print_organizer, param_func = lambda ppt : ppt.layer_height,
235+ parameter_range = [avg_layer_height* 0.5 , avg_layer_height* 2.0 ],
236+ velocity_range = [150 , 70 ], bound_remapping = False )
237+ set_extruder_toggle(print_organizer, slicer)
238+ add_safety_printpoints(print_organizer, z_hop = 10.0 )
239+
240+ # --- Save printpoints dictionary to json file
241+ printpoints_data = print_organizer.output_printpoints_dict()
242+ utils.save_to_json(printpoints_data, OUTPUT_PATH , ' out_printpoints.json' )
243+
244+ end_time = time.time()
245+ print (" Total elapsed time" , round (end_time - start_time, 2 ), " seconds" )
239246
240- # ----- Visualize
241- viewer = app.App(width = 1600 , height = 1000 )
242- # slicer.visualize_on_viewer(viewer, visualize_mesh=False, visualize_paths=True)
243- print_organizer.visualize_on_viewer(viewer, visualize_printpoints = True )
244- viewer.show()
245247
246- end_time = time.time()
247- print ( " Total elapsed time " , round (end_time - start_time, 2 ), " seconds " )
248+ if __name__ == " __main__ " :
249+ main( )
0 commit comments