示例#1
0
    def test_approx_real_surface(self):
        # Test approximation of real surface grid using a random subset
        #  hard test of regularization.
        logging.basicConfig(level=logging.DEBUG)

        xy_mat = np.array([[1.0, 0.0, 0],
                           [0.0, 1.0, 0]])  # rotate left pi/4 and blow up 1.44
        #z_mat = np.array( [1.0, 0] )
        xyz_func = eval_func_on_grid(function_sin_cos, xy_mat[:2, :2],
                                     xy_mat[:, 2])

        print("Compare: Func - Randomized.approx")
        points = bs.make_function_grid(function_sin_cos, 50, 50)
        points = points.reshape((-1, 3))
        n_sample_points = 400  # this is near the limit number of points to keep desired precision
        random_subset = np.random.randint(0, len(points), n_sample_points)
        points_random = points[random_subset, :]

        approx = bs_approx.SurfaceApprox(points_random)
        approx.set_quad(None)  # set unit square
        surface = approx.compute_approximation()
        xyz_grid = eval_z_surface_on_grid(surface, xy_mat[:2, :2], xy_mat[:,
                                                                          2])
        print("Approx error: ", approx.error)
        grid_cmp(xyz_func, xyz_grid, 0.02)
示例#2
0
    def test_aabb(self):
        # function surface
        def function(x):
            return x[0] * (x[1] + 1.0) + 3.0

        poles = bs.make_function_grid(function, 4, 5)
        u_basis = bs.SplineBasis.make_equidistant(2, 2)
        v_basis = bs.SplineBasis.make_equidistant(2, 3)
        surface_func = bs.Surface((u_basis, v_basis), np.array(poles))
        box = surface_func.aabb()
        assert np.allclose(box, np.array([[0, 0, 3], [1, 1, 5]]))
示例#3
0
    def plot_function(self):
        fig = plt.figure()
        ax = fig.gca(projection='3d')

        # function surface
        def function(x):
            return math.sin(x[0]) * math.cos(x[1])

        poles = bs.make_function_grid(function, 4, 5)
        u_basis = bs.SplineBasis.make_equidistant(2, 2)
        v_basis = bs.SplineBasis.make_equidistant(2, 3)
        surface_func = bs.Surface((u_basis, v_basis), poles)
        plotting.plot_surface_3d(surface_func, poles=True)
        plotting.show()
示例#4
0
    def plotting_3d(self, plotting):
        # plotting 3d surfaces
        def function(x):
            return math.sin(x[0]*4) * math.cos(x[1]*4)

        poles = bs.make_function_grid(function, 4, 5)
        u_basis = bs.SplineBasis.make_equidistant(2, 2)
        v_basis = bs.SplineBasis.make_equidistant(2, 3)
        surface_func = bs.Surface( (u_basis, v_basis), poles[:,:, [2] ])

        #quad = np.array( [ [0, 0], [0, 0.5], [1, 0.1],  [1.1, 1.1] ]  )
        quad = np.array([[0, 0], [1, 0], [1, 1], [0, 1]])
        z_surf = bs.Z_Surface(quad, surface_func)
        full_surf = z_surf.make_full_surface()
        z_surf.transform(np.array([[1., 0, 0], [0, 1, 0]]), np.array([2.0, 0]) )
        plotting.plot_surface_3d(z_surf)
        plotting.plot_surface_3d(full_surf)

        plotting.show()
示例#5
0
def make_a_test_grid(output_path, func, nuv):
    """
    Create a test grid file. Function evaluated on the unit square.
    Args:
        output_path: target file
        func: f(x,y) to use to create the grid.
        nuv: (nu, nv) - shape of the full grid.

    Returns:
        File with subset of the full grid and with added noise.
    """
    grid = bs.make_function_grid(func, *nuv)
    grid = grid.reshape((-1, 3))
    n_points = grid.shape[0]
    subindices = np.random.choice(n_points, size=int(0.7 * n_points))
    grid = grid[subindices, :]
    dx = 0.2 * 1/nuv[0]
    dy = 0.2 * 1/nuv[1]
    dz = 0.01 * np.ptp(grid[:, 2])       # value range
    grid += np.random.randn(*grid.shape) * np.array([dx,dy,dz])[None, :]
    np.savetxt(output_path, grid)
示例#6
0
 def make_point_grid(self):
     nu, nv = 5, 6
     grid = bs.make_function_grid(TestPointGrid.function, 5,
                                  6).reshape(nu * nv, 3)
     surf = bs.GridSurface(grid)
     return surf
示例#7
0
 def make_z_surf(self, func, quad):
     poles = bs.make_function_grid(func, 4, 5)
     u_basis = bs.SplineBasis.make_equidistant(2, 2)
     v_basis = bs.SplineBasis.make_equidistant(2, 3)
     surface_func = bs.Surface((u_basis, v_basis), poles[:, :, [2]])
     return bs.Z_Surface(quad, surface_func)
示例#8
0
    def test_approx_func(self):
        logging.basicConfig(level=logging.DEBUG)

        xy_mat = np.array([[1.0, -1.0, 10],
                           [1.0, 1.0,
                            20]])  # rotate left pi/4 and blow up 1.44
        z_mat = np.array([2.0, -10])

        # print("Compare: Func - GridSurface.transform")
        # points = bs.make_function_grid(function_sin_cos, 200, 200)
        # gs = bs.GridSurface(points.reshape(-1, 3))
        # gs.transform(xy_mat, z_mat)
        # xyz_grid = eval_z_surface_on_grid(gs, xy_mat[:2,:2], xy_mat[:, 2])
        # grid_cmp(xyz_func, xyz_grid, 0.02)
        #
        # print("Compare: Func - GridSurface.transform.z_surf")
        # xyz_grid = eval_z_surface_on_grid(gs.z_surface, xy_mat[:2, :2], xy_mat[:, 2])
        # xyz_grid[:,:,2] *= z_mat[0]
        # xyz_grid[:, :, 2] += z_mat[1]
        # grid_cmp(xyz_func, xyz_grid, 0.02)

        print("\nCompare: Func - GridSurface.approx")
        points = bs.make_function_grid(function_sin_cos, 50, 50)
        gs = bs.GridSurface(points.reshape(-1, 3))
        xy_center = gs.center()[0:2]
        z_center = gs.center()[2]
        gs.transform(xy_mat, z_mat)
        approx = bs_approx.SurfaceApprox.approx_from_grid_surface(gs)
        surface = approx.compute_approximation()

        xy_shift = xy_mat[:, 2] - np.dot(xy_mat[:2, :2], xy_center) + xy_center
        xyz_grid = eval_z_surface_on_grid(surface, xy_mat[:2, :2], xy_shift)

        xyz_func = eval_func_on_grid(function_sin_cos, xy_mat[:2, :2],
                                     xy_mat[:, 2])
        xyz_func[:, :, 2] -= z_center
        xyz_func[:, :, 2] *= z_mat[0]
        xyz_func[:, :, 2] += z_mat[1] + z_center
        print("Approx error: ", approx.error)
        grid_cmp(xyz_func, xyz_grid, 0.02)

        print("\nCompare: Func - points.approx")
        np.random.seed(seed=123)
        uv = np.random.rand(1000, 2)
        xy = xy_mat[:2, :2].dot(uv.T).T + xy_mat[:, 2]
        z = np.array([function_sin_cos([u, v]) for u, v in uv])
        xyz = np.concatenate((xy, z[:, None]), axis=1)
        approx = bs_approx.SurfaceApprox(xyz)
        quad = approx.compute_default_quad()

        nuv = approx.nuv
        ref_quad = np.array([[-1, 1], [0, 0], [1, 1], [0, 2]])
        ref_quad += np.array([10, 20])
        assert np.allclose(ref_quad, quad, atol=1e-2)

        nuv = approx.compute_default_nuv()
        assert np.allclose(np.array([8, 8]), nuv)

        surface = approx.compute_approximation()
        z_center = surface.center()[2]
        surface.transform(xy_mat=None, z_mat=z_mat)
        nu, nv = 50, 50
        uv_probe = gen_uv_grid(nu, nv)
        uv_probe = (0.9 * uv_probe + 0.05)
        xy_probe = xy_mat[:2, :2].dot(uv_probe.T).T + xy_mat[:, 2]
        z_func = np.array([function_sin_cos([u, v]) for u, v in uv_probe])
        z_func -= z_center
        z_func *= z_mat[0]
        z_func += z_mat[1] + z_center
        xyz_func = np.concatenate((xy_probe, z_func[:, None]),
                                  axis=1).reshape(nu, nv, 3)
        xyz_approx = surface.eval_xy_array(xy_probe).reshape(nu, nv, 3)
        print("Approx error: ", approx.error)
        grid_cmp(xyz_func, xyz_approx, 0.02)
示例#9
0
文件: test_isec.py 项目: mib81/bgem
    def plot_extrude(self):
        #fig1 = plt.figure()

        #ax1 = fig1.gca(projection='3d')



        def function(x):
            return math.sin(x[0]*4) * math.cos(x[1] * 4)

        def function2(x):
            return math.cos(x[0]*4) * math.sin(x[1] * 4)

        def function3(x):
            return (-x[0] + x[1] + 4 + 3 + math.cos(3 * x[0]))

        def function4(x):
            return (2 * x[0] - x[1] + 3 + math.cos(3 * x[0]))

        u1_int = 4
        v1_int = 4
        u2_int = 4
        v2_int = 4

        u_basis = bs.SplineBasis.make_equidistant(2, u1_int) #10
        v_basis = bs.SplineBasis.make_equidistant(2, v1_int) #15
        u2_basis = bs.SplineBasis.make_equidistant(2, u2_int) #10
        v2_basis = bs.SplineBasis.make_equidistant(2, v2_int) #15
        poles = bs.make_function_grid(function, u1_int + 2, v1_int + 2) #12, 17
        surface_extrude = bs.Surface((u_basis, v_basis), poles)

        myplot = bp.Plotting((bp.PlottingPlotly()))
        #myplot.plot_surface_3d(surface_extrude, poles = False)
        poles2 = bs.make_function_grid(function2,  u2_int + 2, v2_int + 2) #12, 17
        surface_extrude2 = bs.Surface((u2_basis, v2_basis), poles2)
        #myplot.plot_surface_3d(surface_extrude2, poles=False)

        m = 100
        fc = np.zeros([m * m, 3])
        fc2 = np.empty([m * m, 3])
        a = 5
        b = 7
        #print(fc)

        for i in range(m):
            for j in range(m):
                #print([i,j])
                x = i / m * a
                y = j / m * b
                z = function3([x, y])
                z2 = function4([x, y])
                fc[i + j * m, :] = [x, y, z]
                fc2[i + j * m, :] = [x, y, z2]

        #print(fc)

        #gs = bs.GridSurface(fc.reshape(-1, 3))
        #gs.transform(xy_mat, z_mat)
        #approx = bsa.SurfaceApprox.approx_from_grid_surface(gs)




        approx = bsa.SurfaceApprox(fc)
        approx2 = bsa.SurfaceApprox(fc2)
        surfz = approx.compute_approximation(nuv=np.array([11, 26]))
        surfz2 = approx2.compute_approximation(nuv=np.array([20, 16]))
        #surfz = approx.compute_approximation(nuv=np.array([3, 5]))
        #surfz2 = approx2.compute_approximation(nuv=np.array([2, 4]))
        surfzf = surfz.make_full_surface()
        surfzf2 = surfz2.make_full_surface()


        myplot.plot_surface_3d(surfzf, poles=False)
        myplot.plot_surface_3d(surfzf2, poles=False)

        #return surface_extrude, surface_extrude2, myplot
        return surfzf, surfzf2, myplot