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)
def surf_app(self): fc = np.zeros([self.x_n_samples * self.y_n_samples, 3]) for i in range(self.x_n_samples): for j in range(self.y_n_samples): fc[i + j * self.y_n_samples, 0:2] = [i / self.x_n_samples * self.x_length, j / self.y_n_samples * self.y_length] fc[i + j * self.y_n_samples, 2] = self.surfzf(fc[i + j * self.y_n_samples, 0:2]) approx = bsa.SurfaceApprox(fc) surfz = approx.compute_adaptive_approximation(nuv=np.array([self.x_n_control_points, self.y_n_control_points])) err = approx.error surfzf = surfz.make_full_surface() return err, surfzf
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)
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