Exemplo n.º 1
0
 def build_marching_cubes(self):
     minn, maxx, step = -3, 3, 0.05
     rng = np.arange(-3, 3, 0.05)
     x, y, z = np.mgrid[minn:maxx:step, minn:maxx:step, minn:maxx:step]
     xyza = _prepare_grid(rng)
     vgrid_v = self.function.fnc_numpy(xyza[:, 0], xyza[:, 1], xyza[:, 2])
     vgrid = np.reshape(vgrid_v, (len(rng), len(rng), len(rng)), order='C')
     self.vertices, self.faces = vtk_mc(vgrid, (minn, maxx, step))
     from mayavi import mlab
Exemplo n.º 2
0
def make_mc_mesh_scikit(iobj, RANGE_MIN, RANGE_MAX, STEPSIZE):
    """ Uses Scikit's MC algorithm,which has minor bugs. """
    rng = np.arange(RANGE_MIN, RANGE_MAX, STEPSIZE)
    import mc_utils
    vgrid = mc_utils.make_grid(iobj, rng, old=True)
    verts, faces = vtk_mc(vgrid, (RANGE_MIN, RANGE_MAX, STEPSIZE))
    verts = ((verts) * STEPSIZE + rng[0])
    print("OLD: swapping x,y")
    verts = np.concatenate(
        (verts[:, 1, np.newaxis], verts[:, 0, np.newaxis], verts[:, 2,
                                                                 np.newaxis]),
        axis=1)
    return verts, faces
Exemplo n.º 3
0
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()

if False:
    import visual5
    visual5.display_simple_using_mayavi_2(
        [(verts, facets), (verts, facets)],
        mayavi_wireframe=[False, True],
        opacity=[0.4, 0.3],
        #gradients_at=c3,
        separate_panels=False,
        #gradients_from_iobj=iobj,
        minmax=(RANGE_MIN, RANGE_MAX),
        #pointcloud_list=[],
        #add_noise=[0, 0], noise_added_before_broadcast=True,
    def check_meshs(self, iobj, objname=None):

        """Do the algorithm and then do the test"""
        if iobj is not None:
            (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-3, +5, 0.2)
            curvature_epsilon = 1. / 1000.
            VERTEX_RELAXATION_ITERATIONS_COUNT = 1
            SUBDIVISION_ITERATIONS_COUNT = 1

            if objname == "cyl4":
                (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-32 / 2, +32 / 2, 1.92 / 4.0)

            elif objname == "french_fries" or objname == "rods":
                (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-3, +5, 0.11)  # 0.05

            elif objname == "bowl_15_holes":
                (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-3, +5, 0.15)

            elif objname == "cyl3":
                (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-32 / 2, +32 / 2, 1.92 / 4.0)

            elif objname == "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)
            vertex, faces = vtk_mc(gridvals, (RANGE_MIN, RANGE_MAX, STEPSIZE))
            sys.stderr.write("MC calculated.")
            sys.stdout.flush()

            from ohtake_belyaev_demo_subdivision_projection_qem import process2_vertex_resampling_relaxation
            from ohtake_belyaev_demo_subdivision_projection_qem import compute_average_edge_length, set_centers_on_surface__ohtake_v3s
            from ohtake_belyaev_demo_subdivision_projection_qem import compute_centroid_gradients, vertices_apply_qem3, do_subdivision

            for i in range(VERTEX_RELAXATION_ITERATIONS_COUNT):
                vertex, faces_not_used, centroids = process2_vertex_resampling_relaxation(vertex, faces, iobj)
                assert not np.any(np.isnan(vertex.ravel()))  # fails
                sys.stderr.write("Vertex relaxation applied.")
                sys.stdout.flush()

            average_edge = compute_average_edge_length(vertex, faces)

            old_centroids = np.mean(vertex[faces[:], :], axis=1)

            new_centroids = old_centroids.copy()

            set_centers_on_surface__ohtake_v3s(iobj, new_centroids, average_edge)

            # neighbour_faces_of_vertex
            vertex_neighbours_list = mesh_utils.make_neighbour_faces_of_vertex(faces)
            centroid_gradients = compute_centroid_gradients(new_centroids, iobj)

            new_vertex_qem = \
                vertices_apply_qem3(vertex, faces, new_centroids, vertex_neighbours_list, centroid_gradients)

            for i in range(SUBDIVISION_ITERATIONS_COUNT):

                print "subdivision:"
                vertex, facets = do_subdivision(new_vertex_qem, faces, iobj, curvature_epsilon)
                global trace_subdivided_facets  # third implicit output

                print "subdivision done."

            """Testing the mesh correctness"""
            check_mesh(facets)
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))
    def check_centroids_projection(self, iobj, objname=None):
        TOLERANCE = 0.00001
        # TOLERANCE = 0.7 to pass the test for every object
        """Do the centroids projection """
        if iobj is not None:
            VERTEX_RELAXATION_ITERATIONS_COUNT = 0
            (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-3, +5, 0.2)
            if objname == "cube_with_cylinders" or objname == "twist_object" or objname == "french_fries" or objname == "rdice_vec" or objname == "rods" or objname == "bowl_15_holes":
                VERTEX_RELAXATION_ITERATIONS_COUNT = 1

            if objname == "cyl4":
                (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-32 / 2, +32 / 2, 1.92 / 4.0)

            elif objname == "french_fries" or objname == "rods":
                (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-3, +5, 0.11)  # 0.05

            elif objname == "bowl_15_holes":
                (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-3, +5, 0.15)

            elif objname == "cyl3":
                (RANGE_MIN, RANGE_MAX, STEPSIZE) = (-32 / 2, +32 / 2, 1.92 / 4.0)

            elif objname == "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)
            vertex, faces = vtk_mc(gridvals, (RANGE_MIN, RANGE_MAX, STEPSIZE))
            sys.stderr.write("MC calculated.")
            sys.stdout.flush()

            from ohtake_belyaev_demo_subdivision_projection_qem import process2_vertex_resampling_relaxation, compute_average_edge_length, set_centers_on_surface__ohtake_v3s

            from ohtake_belyaev_demo_subdivision_projection_qem import compute_centroid_gradients, vertices_apply_qem3

            for i in range(VERTEX_RELAXATION_ITERATIONS_COUNT):
                vertex, faces_not_used, centroids = process2_vertex_resampling_relaxation(vertex, faces, iobj)
            assert not np.any(np.isnan(vertex.ravel()))  # fails
            sys.stderr.write("Vertex relaxation applied.")
            sys.stdout.flush()

            # projection
            average_edge = compute_average_edge_length(vertex, faces)

            old_centroids = np.mean(vertex[faces[:], :], axis=1)
            check_vector3_vectorized(old_centroids)

            new_centroids = old_centroids.copy()

            set_centers_on_surface__ohtake_v3s(iobj, new_centroids, average_edge)

            vertex_neighbours_list = mesh_utils.make_neighbour_faces_of_vertex(faces)
            centroid_gradients = compute_centroid_gradients(new_centroids, iobj)

            new_vertex_qem = \
                vertices_apply_qem3(vertex, faces, new_centroids, vertex_neighbours_list, centroid_gradients)

            check_vector3_vectorized(new_vertex_qem)
            # checking if the projection is correct by calling the implicitFunction
            f = iobj.implicitFunction(new_vertex_qem)

            # Two ways of doing this test, in the first one we strictly consider that the test fail if one value is superior
            # to the tolerance and in the second we print the numbere of point who fail the test

            Number_of_point_who_fail = True

            if Number_of_point_who_fail is True:
                fail = 0
                for i in range(new_vertex_qem.shape[0]):
                    if math.fabs(f[i]) > TOLERANCE:
                        fail += 1
                print objname, "Number of points:", new_centroids.shape[0], "Number of points who fails the test:", fail

            else:
                for i in range(new_vertex_qem.shape[0]):
                    print "Fail the test", math.fabs(f[i])
                    self.assertTrue(math.fabs(f[i]) < TOLERANCE)