def test_centroids_projection(self): examples_list = example_objects.get_all_examples([2]) for example_name in examples_list: sys.stderr.write("example_name = ", example_name) iobj = example_objects.make_example_vectorized(example_name) from example_objects import make_example_vectorized iobj = make_example_vectorized(example_name) self.check_centroids_projection(iobj, objname=example_name)
def test_mesh_correctness(self): examples_list = example_objects.get_all_examples([2]) for example_name in examples_list: sys.stderr.write("example_name = ", example_name) iobj = example_objects.make_example_vectorized(example_name) from example_objects import make_example_vectorized iobj = make_example_vectorized(example_name) self.check_meshs(iobj, objname=example_name)
def test_gradients_using_numerical_gradient(self): def blend2(): m1 = np.eye(4) * 1 m1[0:3, 3] = [0, 1, 0] m1[3, 3] = 1 m2 = np.eye(4) * 2 m2[0:3, 3] = [2.5, 0, 0] m2[3, 3] = 1 iobj_v = vector3.SimpleBlend(vector3.Ellipsoid(m1), vector3.Ellipsoid(m2)) return iobj_v iobj_v = blend2() self.check_gradient_function(iobj_v, objname="blend2") examples_list = example_objects.get_all_examples([2]) for example_name in examples_list: sys.stderr.write("example_name = ", example_name) iobj = example_objects.make_example_vectorized(example_name) self.check_gradient_function(iobj, objname=example_name) """ numerical """ x = make_vector3(0, 2, 0) g2 = numerical_gradient(iobj_v, x) g = iobj_v.implicitGradient(repeat_vect3(1, x)) np.set_printoptions( formatter={'all': lambda x: '' + ("%2.19f" % (x, ))}) err = np.sum(np.abs(g - g2), axis=1) err_max = np.max(np.abs(g - g2), axis=1) err_rel = np.sum(np.abs(g - g2), axis=1) / np.mean(np.abs(g)) sys.stderr.write(err, err_max, err_rel) # print(err) self.assertTrue(np.all(err < NUMERICAL_GRADIENT_TOLERANCE))
def load_example(self, example_name="ell_example1", res=1): """ res: controls step size of mc grid example_name: name of example to build, available names can be found in the file example_objects """ print "[Start] Loading example: %s" % example_name + ' ...' from example_objects import make_example_vectorized from stl_tests import make_mc_mesh_scikit exname = example_name self.iobj = make_example_vectorized(exname) self.register_function(self.iobj.implicitFunction) self.register_gradient(self.iobj.implicitGradient) (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-2., 2., 0.1 / res) # non-spiky!! self.vertices, self.faces = make_mc_mesh_scikit( self.iobj, RANGE_MIN, RANGE_MAX, STEPSIZE) self.centroids = np.mean(self.vertices[self.faces[:], :], axis=1) self.centroids = np.c_[self.centroids, np.ones((len(self.centroids), 1))] self.triangles = self.vertices[self.faces] self.build_neighbours() self.name = exname self.lamda = calc_avg_triangle_length(self.triangles) / 2 print "[End] Loading example: %s" % example_name + ' .'
def vtk_mc_test(): #set_trace() dicesize = 16. exname = "udice_vec" # "blend_example2" import example_objects iobj = example_objects.make_example_vectorized(exname, dicesize) (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-22, +20., 0.8) from example_objects import cyl4 iobj, (RANGE_MIN, RANGE_MAX, STEPSIZE) = cyl4() from stl_tests import make_mc_values_grid numpy_array = make_mc_values_grid(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE, old=False) gridvals = numpy_array #gridvals = make_npy_file_dice() verts, faces = vtk_mc(gridvals, (RANGE_MIN, RANGE_MAX, STEPSIZE)) print("MC calculated") sys.stdout.flush() from mesh_utils import mesh_invariant mesh_invariant(faces) #from stl_tests import make_mc_mesh_scikit #verts, faces = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE) display_simple_using_mayavi_vf1(verts, faces)
def test8_bigdice(): """ A larger dice. May need support though. Neat, high resolution, a bit heavy. """ from stl import mesh import math # -8.8, 7.2 dicesize = 16. exname = "udice_vec" # "blend_example2" import example_objects iobj = example_objects.make_example_vectorized(exname, dicesize) # (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-22, +20., 0.8/2) # non-spiky!! # will look perfect. Neat, high resolution, a bit heavy. (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-22, +20., 0.8) verts, faces = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE) verts = optimise_mesh(verts, faces, iobj) m = m2stl_mesh(verts, faces) if ACTUALLY_SAVE: m.save('stl/implicit8-bigdice-.stl') # wow display_simple_using_mayavi_vf1(verts, faces) print(np.min(verts.ravel()), np.max(verts.ravel())) plot_stlmesh(m)
def test3(): exname = "screw3" import example_objects iobj = example_objects.make_example_vectorized(exname) (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-2.5, +2.5, 0.1) verts, faces = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE) display_simple_using_mayavi_vf1(verts, faces)
def test_ohtake1(): global count_not_converged global count_converged count_converged = 0 count_not_converged = 0 num_pairs = 20 import example_objects iobj = example_objects.make_example_vectorized("first_csg") from basic_types import make_vector4 counter = 0 while counter < num_pairs: from basic_types import make_random_vector_vectorized x1 = make_random_vector_vectorized(1, 9/4, 3, "randn", normalize=False) x2 = make_random_vector_vectorized(1, 9/4, 3, "randn", normalize=False) f1 = iobj.implicitFunction(x1) f2 = iobj.implicitFunction(x2) if f1*f2 < 0 and f2 < 0: (f1, x1, f2, x2) = (f2, x2, f1, x1) # not tested if f1*f2 < 0 and f1 < 0: #print "*", f1, f2 start_x = x1 dr = x2 - x1 dr = dr / np.linalg.norm(dr) lambda_val = 0.1 # 1.0 #max_dist = 9 #p = project_single_point2_ohtake(iobj, start_x, lambda_val, max_dist ) MAX_ITER = 20 #p, p2 = search_near__ohtake(iobj, start_x, dr, lambda_val, MAX_ITER) #success = p2 is None #if not success: # p is None: # #print "not" # count_not_converged += 1 #else: # assert p is not None # #print "yes" # count_converged += 1 # print("Total distance travelled: ", np.linalg.norm(start_x - p), np.linalg.norm(sp2 - p) ) #print("***********************") is_1d = False p = search_near__ohtake(iobj, start_x, dr if is_1d else None, lambda_val, MAX_ITER) #print p #print "==============" if p is None: count_not_converged += 1 print "Did not converge ", print "*", f1, f2, print("***********************************=*=*=*=") #341 iterations when it doesn't find it. Such a waste of O(.) print 100.0*float(count_converged)/float(count_not_converged+count_converged), "%", "converged" #Result: only %19.74% will converge else: count_converged += 1 print("cccc +")
def test_proj_ohtak1(): """ Tests projection of centroids using Ohtake's original method. Projects point-wise. Repeats points by applying a small perturbation. Blend disc object. Uses a high resolution MC for checking the new projected points (black dots). The original centroids are shon in red. The failed projections are shown in Yello. Their number is writtn in output (9 points)""" from stl_tests import make_mc_mesh_scikit exname = "blend_example2_discs" # "blend_example2" import example_objects iobj = example_objects.make_example_vectorized(exname, 8.0) #(RANGE_MIN, RANGE_MAX, STEPSIZE) = (-2.*8, +4.*8, 0.4*8/5) (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-16, +32, 0.64*3*2/2) verts, faces = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE) print verts.shape, faces.shape average_edge = STEPSIZE # verts = optimise_mesh(verts, faces, iobj) c3 = np.mean(verts[faces[:], :], axis=1) # add extra points c3 = np.concatenate((c3, c3+STEPSIZE*0.1, c3+STEPSIZE*(-0.1)), axis=0) c3 = np.concatenate((c3,), axis=0) centroids = np.concatenate((c3, np.ones((c3.shape[0], 1))), axis=1) new_centroids = centroids.copy() set_centers_on_surface__ohtake(iobj, new_centroids, average_edge) print #print np.arange(nones_map.shape[0])[np.logical_not(nones_map)].shape print np.sum(np.logical_not(nones_map)), "(success) + ", np.sum(nones_map), " (failed)" new_centroids2 = new_centroids[np.logical_not(nones_map), :] #todo: Use "None" for non-converging ones. (and visualise them) #(RANGE_MIN, RANGE_MAX, STEPSIZE) = (-16, +32, 0.64*3*2/2) #verts2, faces2 = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE * 0.2/2.0) #15 million! verts2, faces2 = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE * 0.2) #m = m2stl_mesh(verts, faces) #if ACTUALLY_SAVE: # m.save('implicit6-blend.stl') # wow centroids_failed = centroids[nones_map, :] #green ones #display_simple_using_mayavi_([(verts, faces), (verts2, faces2)], [centroids, new_centroids2], minmax=(RANGE_MIN, RANGE_MAX)) #display_simple_using_mayavi_([(verts, faces), (verts2, faces2)], [centroids, new_centroids2, centroids_failed], minmax=(RANGE_MIN, RANGE_MAX)) #display_simple_using_mayavi_([(verts, faces), (verts2, faces2)], [centroids_failed, new_centroids2], minmax=(RANGE_MIN, RANGE_MAX)) #best: #display_simple_using_mayavi_([(verts, faces), (verts2, faces2)], [centroids, new_centroids], minmax=(RANGE_MIN, RANGE_MAX)) display_simple_using_mayavi_([(verts, faces), (verts2, faces2)], [centroids, new_centroids, centroids_failed], minmax=(RANGE_MIN, RANGE_MAX))
def test4(): # simply marching cubes exname = "screw3" import example_objects iobj = example_objects.make_example_vectorized(exname) (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-2.5, +2.5, 0.1) verts, faces = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE) m = m2stl_mesh(verts, faces) m.save('m.stl') plot_stlmesh(m)
def make_npy_file_dice(): dicesize = 16. exname = "udice_vec" # "blend_example2" import example_objects iobj = example_objects.make_example_vectorized(exname, dicesize) from stl_tests import make_mc_values_grid (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-22, +20., 0.8) #Caution: The only place in which old=True #fixme #todo assert False, "Caution: The only place in which old=True. Use old=False or the new form" numpy_array = make_mc_values_grid(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE, old=True) return numpy_array
def test_profiler(): #use for profiling only exname = "blend_example2_discs" # "blend_example2" import example_objects iobj = example_objects.make_example_vectorized(exname, 8.0) (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-16, +32, 0.64*3*2/2) verts, faces = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE) print verts.shape, faces.shape average_edge = STEPSIZE c3 = np.mean(verts[faces[:], :], axis=1) c3 = np.concatenate( (c3,c3+STEPSIZE*0.1,c3+STEPSIZE*(-0.1)), axis=0) centroids = np.concatenate((c3, np.ones((c3.shape[0], 1))), axis=1) nones_map = centroids[:,0]*0 > 100 new_centroids = centroids.copy() set_centers_on_surface__ohtake(iobj, new_centroids, average_edge, nones_map) new_centroids2 = new_centroids[np.logical_not(nones_map),:] verts2, faces2 = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE * 0.2/1.0)
def test_bisection_vectorized(prop, num_pairs, max_iter_count): import example_objects iobj = example_objects.make_example_vectorized("first_csg") from basic_types import make_vector4 func_test_bisection_p_vectorized(iobj, make_vector4_vectorized(1, 1, 1), make_vector4_vectorized(0, 0, 0), prop, max_iter_count=max_iter_count) if VERBOSE: print("=============") func_test_bisection_p_vectorized(iobj, make_vector4_vectorized(10, 10, 10), make_vector4_vectorized(0, 0, 0), prop, max_iter_count=max_iter_count) if VERBOSE: print("=============") counter = 0 while counter < num_pairs: from basic_types import make_random_vector_vectorized x1 = make_random_vector_vectorized(1, 90 / 10, 3, "randn", normalize=False) x2 = make_random_vector_vectorized(1, 90 / 10, 3, "randn", normalize=False) f1 = iobj.implicitFunction(x1) f2 = iobj.implicitFunction(x2) if f1 * f2 < 0 and f1 < 0: func_test_bisection_p_vectorized(iobj, x1[:, :], x2[:, :], prop, max_iter_count=max_iter_count) if VERBOSE: print("======================") import sys sys.stdout.flush() counter += 1
def test_search1(): import math from stl_tests import make_mc_mesh_scikit dicesize = 8. exname = "udice_vec" import example_objects iobj = example_objects.make_example_vectorized(exname, dicesize) (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-11, +10., 0.8) # non-spiky!! verts, faces = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE) verts = my_process_1(verts, faces, iobj) from stl_tests import display_simple_using_mayavi_vf1 display_simple_using_mayavi_vf1(verts, faces) print(np.min(verts.ravel()), np.max(verts.ravel())) plot_stlmesh(m)
def test6_blend(): """Printed nicely.""" exname = "blend_example2_discs" # "blend_example2" import example_objects iobj = example_objects.make_example_vectorized(exname, 8.0) (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-2. * 8, +4. * 8, 0.4 * 8 / 5) verts, faces = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE) # verts = optimise_mesh(verts, faces, iobj) m = m2stl_mesh(verts, faces) if ACTUALLY_SAVE: m.save('implicit6-blend.stl') # wow display_simple_using_mayavi_vf1(verts, faces) plot_stlmesh(m)
def test5_screw(): # simply marching cubes exname = "screw3" import example_objects iobj = example_objects.make_example_vectorized(exname, 8.0) (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-2.5 * 8, +2.5 * 8, 0.1 * 8) verts, faces = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE) # verts = optimise_mesh(verts, faces, iobj) m = m2stl_mesh(verts, faces) if ACTUALLY_SAVE: m.save('m-optim2.stl') display_simple_using_mayavi_vf1(verts, faces) plot_stlmesh(m)
def test7_dice(): """ Dice prints well. I used NEtfabb to correct the STL though. """ from stl import mesh import math """ It is interesting that the result DOES depend on size (Scaling everything inslucing the grid step size). It works well when scale is x1 and works perfect when scale is x2. But if x3, it starts to look rubbish. Because the numerical methods use absolute sizes (lambda, etc?). """ # -8.8, 7.2 rescale = 1. dicesize = rescale * 8. exname = "udice_vec" # "blend_example2" import example_objects iobj = example_objects.make_example_vectorized(exname, dicesize) # (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-2.*8, +4.*8, 0.4*8/4) # (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-16., +32., 0.8) #non-spiky # (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-10, +9., 0.8) # spikes at bottom! (RANGE_MIN, RANGE_MAX, STEPSIZE) = \ (-11 * rescale, +10. * rescale, 0.8 * rescale) # non-spiky!! # (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-8, +8., 0.8) # verts, faces = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE) from numerical_utils import average_edge_size print "average_edge_size", average_edge_size(verts, faces) # When rescale is doubled (=2.), average edge size 0.824 -> 1.647 . # Seems the latter is when it works very well. verts = optimise_mesh(verts, faces, iobj) print "average_edge_size", average_edge_size(verts, faces) # 0.86 -> 1.72 m = m2stl_mesh(verts, faces) if ACTUALLY_SAVE: m.save('stl/implicit7-dice.stl') # wow display_simple_using_mayavi_vf1(verts, faces) print(np.min(verts.ravel()), np.max(verts.ravel())) plot_stlmesh(m)
from mayavi import mlab from skimage import measure # from basic_types import normalize_vector4_vectorized import mc_utils import example_objects # 120 2 RANGE_MIN = -2 RANGE_MAX = 2 STEPSIZE = 0.2 rng = np.arange(RANGE_MIN, RANGE_MAX, STEPSIZE) """ Choose the object """ iobj = example_objects.make_example_vectorized("bowl_15_holes") print("Starting evaluation of implicit on the Grid.") sys.stdout.flush() t1s = dtimer() vgrid = mc_utils.make_grid(iobj, rng, old=True) assert vgrid.shape == (len(rng), len(rng), len(rng)) t1 = dtimer() - t1s print('done grid') sys.stdout.flush() """ Use the marching cubes: Usually very fast """ t2s = dtimer() verts, faces = measure.marching_cubes(vgrid, 0) verts = (verts) * STEPSIZE + RANGE_MIN
plot_rank3 = False plot_random_interior_points = False plot_raycasts = False #do_project_centroids = False #do_qem = True #qem_breakdown = True mayavi_wireframe = False """ Choose the object """ #exname = "bowl_15_holes" # "blend_example2_discs" "french_fries_vectorized" "cube_example" #exname = "blend_example2_discs" # #exname ="ell_example1" # #exname = "first_csg" #exname = "bowl_15_holes" iobj = example_objects.make_example_vectorized(exname) #iobj = example_objects.make_example_nonvec(exname) #import vectorized #iobj = cube1(vectorized) print("Starting evaluation of implicit on the Grid.") sys.stdout.flush() t1s = dtimer() vgrid = mc_utils.make_grid(iobj, rng, old=True) #vgrid = mc_utils.make_grid_pointwise(iobj, rng) assert vgrid.shape == (len(rng), len(rng), len(rng)) t1 = dtimer() - t1s print('done grid') sys.stdout.flush() """ Use the marching cubes: Usually very fast """
def demo_combination_plus_qem(): """ Now with QEM """ curvature_epsilon = 1. / 1000. # a>eps 1/a > 1/eps = 2000 # curvature_epsilon = 1. / 10000. VERTEX_RELAXATION_ITERATIONS_COUNT = 0 SUBDIVISION_ITERATIONS_COUNT = 0 # 2 # 5+4 from example_objects import make_example_vectorized object_name = "cube_with_cylinders" # "sphere_example" #or "rcube_vec" work well #"ell_example1"#"cube_with_cylinders"#"ell_example1" " #"rdice_vec" #"cube_example" iobj = make_example_vectorized(object_name) (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-3, +5, 0.2) if object_name == "cube_with_cylinders" or object_name == "twist_object" or object_name == "french_fries" or object_name == "rdice_vec" or object_name == "rods" or object_name == "bowl_15_holes": VERTEX_RELAXATION_ITERATIONS_COUNT = 1 if object_name == "cyl4": (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-32 / 2, +32 / 2, 1.92 / 4.0) elif object_name == "french_fries" or object_name == "rods": (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-3, +5, 0.11) # 0.05 elif object_name == "bowl_15_holes": (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-3, +5, 0.15) elif object_name == "cyl3": (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-32 / 2, +32 / 2, 1.92 / 4.0) elif object_name == "cyl1": (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-16, +32, 1.92 * 0.2 * 10 / 2.0) from stl_tests import make_mc_values_grid gridvals = make_mc_values_grid(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE, old=False) verts, facets = vtk_mc(gridvals, (RANGE_MIN, RANGE_MAX, STEPSIZE)) print("MC calculated.") sys.stdout.flush() # display_simple_using_mayavi_2([(verts, facets), ], # pointcloud_list=[], # mayavi_wireframe=[False], opacity=[1], gradients_at=None, separate=False, gradients_from_iobj=None, # minmax=(RANGE_MIN, RANGE_MAX)) # exit() for i in range(VERTEX_RELAXATION_ITERATIONS_COUNT): verts, facets_not_used, centroids = process2_vertex_resampling_relaxation( verts, facets, iobj) assert not np.any(np.isnan(verts.ravel())) # fails print("Vertex relaxation applied.") sys.stdout.flush() average_edge = compute_average_edge_length(verts, facets) old_centroids = np.mean(verts[facets[:], :], axis=1) new_centroids = old_centroids.copy() set_centers_on_surface__ohtake_v3s_002(iobj, new_centroids, average_edge) # neighbour_faces_of_vertex vertex_neighbours_list = mesh_utils.make_neighbour_faces_of_vertex(facets) centroid_gradients = compute_centroid_gradients(new_centroids, iobj) new_verts_qem = \ vertices_apply_qem3(verts, facets, new_centroids, vertex_neighbours_list, centroid_gradients) verts_before_qem = verts highlighted_vertices = np.array([131, 71, 132]) # np.arange(100, 200) hv = new_verts_qem[highlighted_vertices, :] total_subdivided_facets = [] for i in range(SUBDIVISION_ITERATIONS_COUNT): e_array, which_facets = compute_facets_subdivision_curvatures( new_verts_qem, facets, iobj, curvature_epsilon) # which_facets = np.arange(facets.shape[0])[e_array > curvature_epsilon] print "Curvature epsilon:", curvature_epsilon, "which facets need to be subdivided", which_facets.shape verts4_subdivided, facets3_subdivided = subdivide_multiple_facets( new_verts_qem, facets, which_facets) global trace_subdivided_facets # third implicit output verts, facets = verts4_subdivided, facets3_subdivided print("Subdivision applied.") sys.stdout.flush() # total_subdivided_facets += trace_subdivided_facets # old face indices remain valid # new_verts_qem_alpha = (new_verts_qem * alpha + verts * (1-alpha)) highlighted_vertices = np.array([131, 71, 132]) # np.arange(100, 200) hv = verts[highlighted_vertices, :] chosen_facet_indices = np.array(total_subdivided_facets, dtype=int) # move the following code into subdivide_multiple_facets() (?) if chosen_facet_indices.size == 0: chosen_subset_of_facets = np.zeros((0, ), dtype=int) else: chosen_subset_of_facets = facets[chosen_facet_indices, :] # display_simple_using_mayavi_2([(new_verts_final, facets), (new_verts_qem, facets), ], # pointcloud_list=[hv], pointcloud_opacity=0.2, # mayavi_wireframe=[False, True], opacity=[0.2, 0.5, 0.9], gradients_at=None, separate=False, gradients_from_iobj=None, # minmax=(RANGE_MIN, RANGE_MAX)) # exit() # display_simple_using_mayavi_2([ (verts_before_qem, facets), (new_verts_qem, facets), ], pointcloud_list=[hv], pointcloud_opacity=0.2, mayavi_wireframe=[False, False], opacity=[0.4 * 0, 1, 0.9], gradients_at=None, separate=False, gradients_from_iobj=None, minmax=(RANGE_MIN, RANGE_MAX))
check_vector4_vectorized(v4) return v4 objname = "bowl_15_holes" #"rcube_vec" #"rdice_vec" # #"cube_example" # problem: zero facet areas. otherwise, it works. #"ell_example1" #+ #"bowl_15_holes" # works too. But too many faces => too slow, too much memory. 32K? #"french_fries_vectorized" global STEPSIZE from example_objects import make_example_vectorized iobj = make_example_vectorized(objname) (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-3, +5, 0.2 / 2.) #STEPSIZE = STEPSIZE / 2. rng = np.arange(RANGE_MIN, RANGE_MAX, STEPSIZE) #vgrid = mc_utils.make_grid(iobj, rng, old=old) gridvals, xyz = mc_utils.make_grid(iobj, rng, old=False, return_xyz=True) v_xyz = gridvals.ravel() print "grid size", gridvals.shape, np.prod(gridvals.shape) #from stl_tests import make_mc_values_grid #gridvals = make_mc_values_grid(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE, old="3") verts, facets = vtk_mc(gridvals, (RANGE_MIN, RANGE_MAX, STEPSIZE)) print("MC calculated.") sys.stdout.flush()
import sys sys.path.append("..") import matplotlib.pyplot as plt import numpy as np global STEPSIZE from example_objects import make_example_vectorized ifunc = make_example_vectorized("cube_example") (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-3, +5, 0.2 * 1.5 / 1.5 * 2. / 2.) global x def test1(): f = ifunc.implicitFunction(x) def test2(): for i in range(x.shape[0]): f = ifunc.implicitFunction(x[i, np.newaxis, :]) def experiment1(): #asymp = (10.**4, 2.37+03/10.**4 / 100) #print asymp #asymp_ratio1 = (2.37e+03) /(10. * 4)/100000000. # 0.592 microseconds asymp_ratio1 = 0.59 / (10.**6) # 0.592 microseconds #asymp_ratio2 = 0.50 / (10. ** 6)
def test_proj_ohtak2(): from stl_tests import make_mc_mesh_scikit exname = "french_fries_vectorized" import example_objects iobj = example_objects.make_example_vectorized(exname, 8.0) #(RANGE_MIN, RANGE_MAX, STEPSIZE) = (-2.*8, +4.*8, 0.4*8/5) #(RANGE_MIN, RANGE_MAX, STEPSIZE) = (-16, +32, 0.64*3*2/2) (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-2 * 8, +4 * 8, 0.64 * 3 * 2 / 2 / 4. * 8 * 0.2 ) # / 8. #0.768 #STEPSIZE2 = STEPSIZE * 0.2 STEPSIZE, STEPSIZE2 = (1.5, 0.384) verts, faces = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE) print verts.shape, faces.shape average_edge = STEPSIZE # verts = optimise_mesh(verts, faces, iobj) c3_0 = np.mean(verts[faces[:], :], axis=1) # add extra points offsets = [0] print c3_0.shape #offsets = [0,+0.1,-0.1] #c3 = np.concatenate((c3, c3+STEPSIZE*0.1, c3+STEPSIZE*(-0.1)), axis=0) c3 = np.zeros((0, 3)) for x in offsets: c3 = np.concatenate(( c3, c3_0 + STEPSIZE * x, ), axis=0) #c3 = np.concatenate((c3,), axis=0) centroids = np.concatenate((c3, np.ones((c3.shape[0], 1))), axis=1) nones_map = centroids[:, 0] * 0 > 100 new_centroids = centroids.copy() set_centers_on_surface__ohtake(iobj, new_centroids, average_edge, nones_map) print #print np.arange(nones_map.shape[0])[np.logical_not(nones_map)].shape print np.sum(np.logical_not(nones_map)), "(success) + ", np.sum( nones_map), " (failed)" new_centroids2 = new_centroids[np.logical_not(nones_map), :] #todo: Use "None" for non-converging ones. (and visualise them) if False: #(RANGE_MIN, RANGE_MAX, STEPSIZE) = (-16, +32, 0.64*3*2/2) #verts2, faces2 = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE * 0.2/2.0) #15 million! verts2, faces2 = make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE2) verts2, faces2 = None, None #m = m2stl_mesh(verts, faces) #if ACTUALLY_SAVE: # m.save('implicit6-blend.stl') # wow centroids_failed = centroids[nones_map, :] #green ones #display_simple_using_mayavi_([(verts, faces), (verts2, faces2)], [centroids, new_centroids2], minmax=(RANGE_MIN, RANGE_MAX)) #display_simple_using_mayavi_([(verts, faces), (verts2, faces2)], [centroids, new_centroids2, centroids_failed], minmax=(RANGE_MIN, RANGE_MAX)) #display_simple_using_mayavi_([(verts, faces), (verts2, faces2)], [centroids_failed, new_centroids2], minmax=(RANGE_MIN, RANGE_MAX)) #best: #display_simple_using_mayavi_([(verts, faces), (verts2, faces2)], [centroids, new_centroids], minmax=(RANGE_MIN, RANGE_MAX)) display_simple_using_mayavi_([(verts, faces), (verts2, faces2)], [centroids, new_centroids, centroids_failed], minmax=(RANGE_MIN, RANGE_MAX))
def getifunc(): from example_objects import make_example_vectorized ifunc = make_example_vectorized("cube_example") (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-3 / 5., +5 / 5., 0.2 * 1.5 / 1.5 * 2. / 2.) return ifunc, (RANGE_MIN, RANGE_MAX, STEPSIZE)