def mass_stiffness_smooth(triangles, vertices, nb_iter=1, diffusion_step=1.0, flow_file=None): vertices_csc = csc_matrix(vertices) curvature_normal_mtx = mean_curvature_normal_matrix( triangles, vertices_csc, area_weighted=False) # mass_mtx = mass_matrix(triangles, vertices_csc).astype(np.float) if isinstance(diffusion_step, (int, float)): diffusion_step = diffusion_step * np.ones(len(vertices)) if flow_file is not None: mem_map = np.memmap(flow_file, dtype=G_DTYPE, mode='w+', shape=(nb_iter, vertices.shape[0], vertices.shape[1])) for i in range(nb_iter): stdout.write("\r step %d on %d done" % (i, nb_iter)) stdout.flush() if flow_file is not None: mem_map[i] = vertices_csc.toarray() # get curvature_normal_matrix mass_mtx = mass_matrix(triangles, vertices_csc).astype(np.float) # (D - d*L)*y = D*x = b A_matrix = mass_mtx - \ (diags(diffusion_step, 0).dot(curvature_normal_mtx)) b_matrix = mass_mtx.dot(vertices_csc) vertices_csc = spsolve(A_matrix, b_matrix) stdout.write("\r step %d on %d done \n" % (nb_iter, nb_iter)) # return next_vertices_csc return vertices_csc.toarray()
def curvature_normal_smooth(triangles, vertices, nb_iter=1, diffusion_step=1.0, area_weighted=False, backward_step=False, flow_file=None): if flow_file is not None: mem_map = np.memmap(flow_file, dtype=G_DTYPE, mode='w+', shape=(nb_iter, vertices.shape[0], vertices.shape[1])) vertices_csc = csc_matrix(vertices) if isinstance(diffusion_step, (int, float)): diffusion_step = diffusion_step * np.ones(len(vertices)) for i in range(nb_iter): stdout.write("\r step %d on %d done" % (i, nb_iter)) stdout.flush() if flow_file is not None: mem_map[i] = vertices_csc.toarray() # get curvature_normal_matrix curvature_normal_mtx = mean_curvature_normal_matrix( triangles, vertices_csc, area_weighted=area_weighted) next_vertices_csc = euler_step( curvature_normal_mtx, vertices_csc, diffusion_step, backward_step) vertices_csc = next_vertices_csc stdout.write("\r step %d on %d done \n" % (nb_iter, nb_iter)) # return next_vertices_csc return vertices_csc.toarray()
def volume_curvature_normal_smooth(triangles, vertices, nb_iter=1, diffusion_step=1.0, area_weighted=False, backward_step=False, flow_file=None): if isinstance(diffusion_step, (int, float)): diffusion_step = diffusion_step * np.ones(len(vertices)) if flow_file is not None: mem_map = np.memmap(flow_file, dtype=G_DTYPE, mode='w+', shape=(nb_iter, vertices.shape[0], vertices.shape[1])) for i in range(nb_iter): stdout.write("\r step %d on %d done" % (i, nb_iter)) stdout.flush() if flow_file is not None: mem_map[i] = vertices # get curvature_normal_matrix # todo not optimal, because operation done twice etc curvature_normal_mtx = mean_curvature_normal_matrix( triangles, vertices, area_weighted=area_weighted) # do the first step next_vertices = euler_step(curvature_normal_mtx, csc_matrix( vertices), diffusion_step, backward_step).toarray() # test if direction is positive direction = next_vertices - vertices normal_dir = vertices_cotan_normal(triangles, vertices, normalize=True) dotv = dot(normalize_vectors(direction), normal_dir, keepdims=True) vertices += direction * np.maximum(0.0, -dotv) stdout.write("\r step %d on %d done \n" % (nb_iter, nb_iter)) return vertices
def positive_curvature_normal_smooth(triangles, vertices, nb_iter=1, diffusion_step=1.0, area_weighted=False, backward_step=False, flow_file=None): if flow_file is not None: mem_map = np.memmap(flow_file, dtype=G_DTYPE, mode='w+', shape=(nb_iter, vertices.shape[0], vertices.shape[1])) if isinstance(diffusion_step, (int, float)): diffusion_step = diffusion_step * np.ones(len(vertices)) curvature_normal_mtx = mean_curvature_normal_matrix( triangles, vertices, area_weighted=area_weighted) for i in range(nb_iter): stdout.write("\r step %d on %d done" % (i, nb_iter)) stdout.flush() if flow_file is not None: mem_map[i] = vertices # do the first step next_vertices = euler_step(curvature_normal_mtx, csc_matrix( vertices), diffusion_step, backward_step).toarray() # test if direction is positive direction = next_vertices - vertices normal_dir = vertices_normal(triangles, next_vertices, normalize=False) pos_curv = dot(direction, normal_dir, keepdims=True) < 0 vertices += direction * pos_curv stdout.write("\r step %d on %d done \n" % (nb_iter, nb_iter)) return vertices
def positive_mass_stiffness_smooth(triangles, vertices, nb_iter=1, diffusion_step=1.0, flow_file=None, gaussian_threshold=0.2, angle_threshold=1.0): vertices_csc = csc_matrix(vertices) curvature_normal_mtx = mean_curvature_normal_matrix(triangles, vertices, area_weighted=False) # mass_mtx = mass_matrix(triangles, vertices_csc) if isinstance(diffusion_step, (int, long, float)): diffusion_step = diffusion_step * np.ones(len(vertices)) if flow_file is not None: mem_map = np.memmap(flow_file, dtype=G_DTYPE, mode='w+', shape=(nb_iter, vertices.shape[0], vertices.shape[1])) for i in range(nb_iter): stdout.write("\r step %d on %d done" % (i, nb_iter)) stdout.flush() if flow_file is not None: mem_map[i] = vertices_csc.todense() # third try mass_mtx = mass_matrix(triangles, vertices_csc) pos_curv = vertices_cotan_curvature(triangles, vertices_csc, False) > -G_ATOL if gaussian_threshold is not None: # Gaussian threshold: maximum value PI, cube corner = PI/2 # = 0.8 deg_vts = np.abs( vertices_gaussian_curvature(triangles, vertices_csc, False)) > gaussian_threshold pos_curv = np.logical_or(pos_curv, deg_vts) if angle_threshold is not None: # angle_threshold: PI, cube corner = PI/2 # = 1.7 deg_seg = edge_triangle_normal_angle( triangles, vertices_csc).max(1).toarray().squeeze() > angle_threshold pos_curv = np.logical_or(pos_curv, deg_seg) possitive_diffusion_step = pos_curv * diffusion_step # (D - d*L)*y = D*x = b A_matrix = mass_mtx - \ (diags(possitive_diffusion_step, 0).dot(curvature_normal_mtx)) b_matrix = mass_mtx.dot(vertices_csc) vertices_csc = spsolve(A_matrix, b_matrix) stdout.write("\r step %d on %d done \n" % (nb_iter, nb_iter)) return vertices_csc.toarray()
def vertices_cotan_direction(triangles, vertices, normalize=True, area_weighted=True): from trimeshpy.math.matrix import mean_curvature_normal_matrix curvature_normal_mtx = mean_curvature_normal_matrix( triangles, vertices, area_weighted=area_weighted) cotan_normal = curvature_normal_mtx.dot(vertices) if normalize: return normalize_vectors(cotan_normal) return cotan_normal
def volume_mass_stiffness_smooth(triangles, vertices, nb_iter=1, diffusion_step=1.0, flow_file=None): vertices_csc = csc_matrix(vertices) curvature_normal_mtx = mean_curvature_normal_matrix(triangles, vertices, area_weighted=False) if isinstance(diffusion_step, (int, long, float)): diffusion_step = diffusion_step * np.ones(len(vertices)) if flow_file is not None: mem_map = np.memmap(flow_file, dtype=G_DTYPE, mode='w+', shape=(nb_iter, vertices.shape[0], vertices.shape[1])) for i in range(nb_iter): stdout.write("\r step %d on %d done" % (i, nb_iter)) stdout.flush() if flow_file is not None: mem_map[i] = vertices_csc.toarray() # get curvature_normal_matrix mass_mtx = mass_matrix(triangles, vertices) raise NotImplementedError() # (D - d*L)*y = D*x = b A_matrix = mass_mtx - \ diags(diffusion_step, 0).dot(curvature_normal_mtx) b_matrix = mass_mtx.dot(csc_matrix(vertices_csc)) next_vertices = spsolve(A_matrix, b_matrix) # test if direction is positive direction = next_vertices.toarray() - vertices_csc normal_dir = vertices_cotan_normal(triangles, next_vertices, normalize=True) dotv = normalize_vectors(direction).multiply(normal_dir) vertices_csc += direction * np.maximum(0.0, -dotv) # vertices_csc += direction * sigmoid(-np.arctan(dotv)*np.pi - np.pi) # vertices_csc += direction * softplus(-dotv) stdout.write("\r step %d on %d done \n" % (nb_iter, nb_iter)) return vertices_csc.toarray()