22from __future__ import absolute_import
33from __future__ import division
44
5+ from copy import deepcopy
56
67from compas .geometry import centroid_points
78from compas .geometry import center_of_mass_polygon
@@ -118,7 +119,8 @@ def smooth_centroid(vertices,
118119 fixed = set (fixed )
119120
120121 for k in range (kmax ):
121- vertices_0 = {key : xyz for key , xyz in iter (vertices .items ())}
122+ vertices_0 = {key : xyz [:] for key , xyz in iter (vertices .items ())}
123+ # vertices_0 = deepcopy(vertices)
122124
123125 for key , point in iter (vertices_0 .items ()):
124126 if key in fixed :
@@ -131,6 +133,10 @@ def smooth_centroid(vertices,
131133 vertices [key ][1 ] += damping * (centroid [1 ] - point [1 ])
132134 vertices [key ][2 ] += damping * (centroid [2 ] - point [2 ])
133135
136+ # vertices[key][0] = centroid[0]
137+ # vertices[key][1] = centroid[1]
138+ # vertices[key][2] = centroid[2]
139+
134140 if callback :
135141 callback (vertices , k , callback_args )
136142
@@ -236,7 +242,7 @@ def smooth_centerofmass(vertices,
236242 raise Exception ('The callback is not callable.' )
237243
238244 for k in range (kmax ):
239- vertices_0 = {key : xyz for key , xyz in iter (vertices .items ())}
245+ vertices_0 = {key : xyz [:] for key , xyz in iter (vertices .items ())}
240246
241247 for key , point in iter (vertices_0 .items ()):
242248 if key in fixed :
@@ -341,7 +347,7 @@ def smooth_resultant(vertices,
341347 fixed = set (fixed )
342348
343349 for k in range (kmax ):
344- vertices_0 = {key : xyz for key , xyz in iter (vertices .items ())}
350+ vertices_0 = {key : xyz [:] for key , xyz in iter (vertices .items ())}
345351
346352 for key , point in iter (vertices_0 .items ()):
347353 if key in fixed :
@@ -453,7 +459,7 @@ def smooth_area(vertices,
453459 fixed = set (fixed )
454460
455461 for k in range (kmax ):
456- vertices_0 = {key : point for key , point in iter (vertices .items ())}
462+ vertices_0 = {key : point [:] for key , point in iter (vertices .items ())}
457463
458464 face_centroid = {fkey : centroid_points ([vertices [key ] for key in keys ]) for fkey , keys in iter (faces .items ())}
459465 face_area = {fkey : area_polygon ([vertices [key ] for key in keys ]) for fkey , keys in iter (faces .items ())}
@@ -742,17 +748,24 @@ def network_smooth_resultant(network, fixed=None, kmax=100, damping=0.05, callba
742748
743749if __name__ == "__main__" :
744750
751+ import os
752+ import sys
753+
745754 import compas
746755
747756 from compas .datastructures import Mesh
748757 from compas .plotters import MeshPlotter
749758
750759 mesh = Mesh .from_obj (compas .get ('faces.obj' ))
751760
761+ edges = list (mesh .edges ())
762+
752763 fixed = [key for key in mesh .vertices () if mesh .vertex_degree (key ) == 2 ]
753764
754765 plotter = MeshPlotter (mesh , figsize = (10 , 7 ))
755766
767+ images = []
768+
756769 lines = []
757770 for u , v in mesh .edges ():
758771 lines .append ({
@@ -763,20 +776,32 @@ def network_smooth_resultant(network, fixed=None, kmax=100, damping=0.05, callba
763776 })
764777 plotter .draw_lines (lines )
765778
766- plotter .draw_vertices (facecolor = {key : '#ff0000' for key in fixed })
779+ plotter .draw_vertices (facecolor = {key : '#ff0000' for key in fixed }, text = { key : key for key in fixed } )
767780 plotter .draw_faces ()
768781 plotter .draw_edges ()
769782
770783 plotter .update (pause = 1.0 )
771784
785+ # filepath = os.path.join(compas.TEMP, 'image_{}.jpg'.format(0))
786+ # plotter.save(filepath)
787+ # images.append(filepath)
788+
772789 def callback (mesh , k , args ):
773790 print (k )
774791 plotter .update_vertices ()
775792 plotter .update_faces ()
776793 plotter .update_edges ()
777794 plotter .update (pause = 0.001 )
778795
796+ # filepath = os.path.join(compas.TEMP, 'image_{}.jpg'.format(k + 1))
797+ # plotter.save(filepath)
798+ # images.append(filepath)
799+
779800 mesh_smooth_centroid (mesh , kmax = 50 , fixed = fixed , callback = callback )
780801
781- plotter .update (pause = 1.0 )
802+ # plotter.saveas_gif(os.path.join(compas.TEMP, 'smoothing.gif'), images)
803+
804+ for filepath in images :
805+ os .remove (filepath )
806+
782807 plotter .show ()
0 commit comments