def test_bspline_surface_loadsave(): surf_save = BSpline.Surface() surf_save.degree_u = S_DEGREE_U surf_save.degree_v = S_DEGREE_V surf_save.ctrlpts_size_u = 3 surf_save.ctrlpts_size_v = 3 surf_save.ctrlpts = S_CTRLPTS surf_save.knotvector_u = S_KV_U surf_save.knotvector_v = S_KV_V surf_save.save(FILE_NAME) surf_load = BSpline.Surface() surf_load.load(FILE_NAME) # Remove save file os.remove(FILE_NAME) assert surf_save.degree_u == surf_load.degree_u assert surf_save.degree_v == surf_load.degree_v assert surf_save.knotvector_u == surf_load.knotvector_u assert surf_save.knotvector_v == surf_load.knotvector_v assert surf_save.ctrlpts == surf_load.ctrlpts assert surf_save.ctrlpts_size_u == surf_load.ctrlpts_size_u assert surf_save.ctrlpts_size_v == surf_load.ctrlpts_size_v assert surf_save.dimension == surf_load.dimension
def spline_surf(): """ Creates a B-spline surface instance """ # Create a surface instance surf = BSpline.Surface() # Set degrees surf.degree_u = 3 surf.degree_v = 3 ctrlpts = [[-25.0, -25.0, -10.0], [-25.0, -15.0, -5.0], [-25.0, -5.0, 0.0], [-25.0, 5.0, 0.0], [-25.0, 15.0, -5.0], [-25.0, 25.0, -10.0], [-15.0, -25.0, -8.0], [-15.0, -15.0, -4.0], [-15.0, -5.0, -4.0], [-15.0, 5.0, -4.0], [-15.0, 15.0, -4.0], [-15.0, 25.0, -8.0], [-5.0, -25.0, -5.0], [-5.0, -15.0, -3.0], [-5.0, -5.0, -8.0], [-5.0, 5.0, -8.0], [-5.0, 15.0, -3.0], [-5.0, 25.0, -5.0], [5.0, -25.0, -3.0], [5.0, -15.0, -2.0], [5.0, -5.0, -8.0], [5.0, 5.0, -8.0], [5.0, 15.0, -2.0], [5.0, 25.0, -3.0], [15.0, -25.0, -8.0], [15.0, -15.0, -4.0], [15.0, -5.0, -4.0], [15.0, 5.0, -4.0], [15.0, 15.0, -4.0], [15.0, 25.0, -8.0], [25.0, -25.0, -10.0], [25.0, -15.0, -5.0], [25.0, -5.0, 2.0], [25.0, 5.0, 2.0], [25.0, 15.0, -5.0], [25.0, 25.0, -10.0]] # Set control points surf.set_ctrlpts(ctrlpts, 6, 6) # Set knot vectors surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 0.33, 0.66, 1.0, 1.0, 1.0, 1.0] surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 0.33, 0.66, 1.0, 1.0, 1.0, 1.0] return surf
def surface_fit(self, terrain_grid: np.ndarray, grid_resolution: int, u_degree: int, v_degree: int, delta: float): # Create a BSpline surface instance surf = BSpline.Surface() # Set evaluation delta surf.delta = delta # Set up the surface surf.degree_u = u_degree surf.degree_v = v_degree control_points = terrain_grid.tolist() surf.set_ctrlpts(control_points, grid_resolution, grid_resolution) surf.knotvector_u = utilities.generate_knot_vector( surf.degree_u, grid_resolution) surf.knotvector_v = utilities.generate_knot_vector( surf.degree_v, grid_resolution) # Evaluate surface points surf.evaluate() return surf
def build_geomdl(cls, degree_u, degree_v, knotvector_u, knotvector_v, control_points, weights, normalize_knots=False): def convert_row(verts_row, weights_row): return [(x * w, y * w, z * w, w) for (x, y, z), w in zip(verts_row, weights_row)] if weights is None: surf = BSpline.Surface(normalize_kv=normalize_knots) else: surf = NURBS.Surface(normalize_kv=normalize_knots) surf.degree_u = degree_u surf.degree_v = degree_v if weights is None: ctrlpts = control_points else: ctrlpts = list(map(convert_row, control_points, weights)) surf.ctrlpts2d = ctrlpts surf.knotvector_u = knotvector_u surf.knotvector_v = knotvector_v result = SvGeomdlSurface(surf) result.u_bounds = surf.knotvector_u[0], surf.knotvector_u[-1] result.v_bounds = surf.knotvector_v[0], surf.knotvector_v[-1] return result
def test_convert_surface(): surf_bs = BSpline.Surface() surf_bs.degree_u = S_DEGREE_U surf_bs.degree_v = S_DEGREE_V surf_bs.ctrlpts_size_u = 3 surf_bs.ctrlpts_size_v = 3 surf_bs.ctrlpts = S_CTRLPTS surf_bs.knotvector_u = S_KV_U surf_bs.knotvector_v = S_KV_V surf_nurbs = convert.bspline_to_nurbs(surf_bs) surf_nurbs.sample_size = SAMPLE_SIZE # Expected weights vector res_weights = [1.0 for _ in range(3*3)] # Expected output res = [[0.0, 0.0, 0.0], [0.0, 0.5, -0.1875], [0.0, 1.0, -0.75], [0.0, 1.5, -1.6875], [0.0, 2.0, -3.0], [0.5, 0.0, 2.25], [0.5, 0.5, 1.171875], [0.5, 1.0, 0.1875], [0.5, 1.5, -0.703125], [0.5, 2.0, -1.5], [1.0, 0.0, 3.0], [1.0, 0.5, 1.6875], [1.0, 1.0, 0.75], [1.0, 1.5, 0.1875], [1.0, 2.0, 0.0], [1.5, 0.0, 2.25], [1.5, 0.5, 1.359375], [1.5, 1.0, 0.9375], [1.5, 1.5, 0.984375], [1.5, 2.0, 1.5], [2.0, 0.0, 0.0], [2.0, 0.5, 0.1875], [2.0, 1.0, 0.75], [2.0, 1.5, 1.6875], [2.0, 2.0, 3.0]] assert not surf_bs.rational assert surf_nurbs.rational assert surf_nurbs.evalpts == res assert surf_nurbs.weights == res_weights
def main(): # Fix file path os.chdir(os.path.dirname(os.path.realpath(__file__))) # Create a BSpline surface instance surf = BSpline.Surface() # Set degrees surf.degree_u = 3 surf.degree_v = 3 # Set control points surf.set_ctrlpts( *exchange.import_txt("ex_surface01.cpt", two_dimensional=True)) # Set knot vectors surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] # Set evaluation delta surf.delta = 0.025 # Evaluate surface points surf.evaluate() # Import and use Matplotlib's colormaps from matplotlib import cm # Plot the control point grid and the evaluated surface vis_comp = vis.VisSurface() surf.vis = vis_comp surf.render(colormap=cm.cool) # Good to have something here to put a breakpoint pass
def bspline_surface(): """ Creates a B-Spline surface instance """ # Create a surface instance surf = BSpline.Surface() # Set degrees surf.degree_u = 3 surf.degree_v = 3 # Set control points surf.ctrlpts_size_u = 6 surf.ctrlpts_size_v = 6 surf.ctrlpts = [[-25.0, -25.0, -10.0], [-25.0, -15.0, -5.0], [-25.0, -5.0, 0.0], [-25.0, 5.0, 0.0], [-25.0, 15.0, -5.0], [-25.0, 25.0, -10.0], [-15.0, -25.0, -8.0], [-15.0, -15.0, -4.0], [-15.0, -5.0, -4.0], [-15.0, 5.0, -4.0], [-15.0, 15.0, -4.0], [-15.0, 25.0, -8.0], [-5.0, -25.0, -5.0], [-5.0, -15.0, -3.0], [-5.0, -5.0, -8.0], [-5.0, 5.0, -8.0], [-5.0, 15.0, -3.0], [-5.0, 25.0, -5.0], [5.0, -25.0, -3.0], [5.0, -15.0, -2.0], [5.0, -5.0, -8.0], [5.0, 5.0, -8.0], [5.0, 15.0, -2.0], [5.0, 25.0, -3.0], [15.0, -25.0, -8.0], [15.0, -15.0, -4.0], [15.0, -5.0, -4.0], [15.0, 5.0, -4.0], [15.0, 15.0, -4.0], [15.0, 25.0, -8.0], [25.0, -25.0, -10.0], [25.0, -15.0, -5.0], [25.0, -5.0, 2.0], [25.0, 5.0, 2.0], [25.0, 15.0, -5.0], [25.0, 25.0, -10.0]] # Set knot vectors surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 0.33, 0.66, 1.0, 1.0, 1.0, 1.0] surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 0.33, 0.66, 1.0, 1.0, 1.0, 1.0] # Set sample size surf.sample_size = SAMPLE_SIZE # Return the instance return surf
def bs_surface2(): surf = BSpline.Surface() ctrlpts = [[1.0, 1.0, 10.0], [1.0, 2.0, 11.0], [1.0, 3.0, 12.0], [2.0, 1.0, 13.0], [2.0, 2.0, 14.0], [2.0, 3.0, 15.0], [3.0, 1.0, 16.0], [3.0, 2.0, 17.0], [3.0, 3.0, 18.0], [4.0, 1.0, 19.0], [4.0, 2.0, 20.0], [4.0, 3.0, 21.0]] surf.ctrlpts_size_v = 3 surf.ctrlpts_size_u = 4 surf.degree_u = 2 surf.degree_v = 2 surf.ctrlpts = ctrlpts return surf
def bspline_surface(): """ Creates a B-Spline surface instance """ surf = BSpline.Surface() surf.degree_u = 2 surf.degree_v = 2 surf.ctrlpts_size_u = 3 surf.ctrlpts_size_v = 3 surf.ctrlpts = [[0, 0, 0], [0, 1, 0], [0, 2, -3], [1, 0, 6], [1, 1, 0], [1, 2, 0], [2, 0, 0], [2, 1, 0], [2, 2, 3]] surf.knotvector_u = [0, 0, 0, 1, 1, 1] surf.knotvector_v = [0, 0, 0, 1, 1, 1] return surf
def generate_surface(): surf = BSpline.Surface() control_points1 = [[0, 0, randint(0, 100)], [0, randint(0, 20), randint(0, 100)], [0, randint(50, 80), randint(0, 100)], [0, 100, randint(0, 100)], [randint(20, 80), 0, randint(0, 100)], [randint(20, 80), randint(0, 20), randint(0, 100)], [randint(20, 80), randint(50, 80), randint(0, 100)], [randint(20, 80), 100, randint(0, 100)], [100, 0, randint(0, 100)], [100, randint(0, 20), randint(0, 100)], [100, randint(20, 80), randint(0, 100)], [100, 100, randint(0, 100)]] # control_points2 = [[0, 0, 0], [0, 20, 0], [0, 70, 0], [0, 100, 0], # [50, 0, 30], [50, 30, 30], [50, 70, 30], [50, 100, 70], # [100, 0, 0], [100, 20, 0], [100, 70, 0], [100, 100, 0]] # control_points3 = [[0, 0, 0], [0, 20, 0], [0, 70, 0], [0, 100, 0], # [50, 0, 100], [50, 30, 30], [50, 70, 30], [50, 100, 80], # [100, 0, 0], [100, 20, 0], [100, 70, 0], [100, 100, 0]] # control_points4 = [[0, 0, 0], [0, 20, 0], [0, 70, 0], [0, 100, 0], # [50, 0, 0], [50, 30, 0], [50, 70, 0], [50, 100, 0], # [100, 0, 0], [100, 20, 0], [100, 70, 0], [100, 100, 0]] control_points5 = [[0, 0, 0], [0, 50, 0], [0, 50, 0], [0, 100, 0], [50, 0, 0], [50, 50, 0], [50, 50, 0], [50, 100, 0], [100, 0, 0], [100, 50, 0], [100, 50, 0], [100, 100, 0]] diam = np.random.randint(60, 100) r = diam/2 x = 0.05*diam z = 4.*r/3. control_points6 = [[0, 0, 0], [0, 20, 0], [0, 70, 0], [0, 100, 0], [x, 0, z], [x, 20, z], [x, 70, z], [x, 100, z], [diam-x, 0, z], [diam-x, 20, z], [diam-x, 70, z], [diam-x, 100, z], [diam, 0, 0], [diam, 20, 0], [diam, 70, 0], [diam, 100, 0]] # control_points_set = [control_points1, control_points2, control_points3, control_points4, # control_points5, control_points6] control_points_set = [control_points1]#, control_points5, control_points6] control_points = choice(control_points_set) if len(control_points) == 12: surf.degree_u = 2 surf.degree_v = 3 surf.set_ctrlpts(control_points, 3, 4) elif len(control_points) == 16: surf.degree_u = 3 surf.degree_v = 3 surf.set_ctrlpts(control_points, 4, 4) surf.knotvector_u = utilities.generate_knot_vector(surf.degree_u, surf.ctrlpts_size_u) surf.knotvector_v = utilities.generate_knot_vector(surf.degree_v, surf.ctrlpts_size_v) return surf
def render_spline(ctrl_pts, rows, cols): ctrl_pts = ctrl_pts.reshape(-1, 3).detach().cpu().numpy().tolist() pts_h = rows pts_v = cols surf = BSpline.Surface() surf.degree_u = 3 surf.degree_v = 3 surf.set_ctrlpts(ctrl_pts, pts_h, pts_v) surf.knotvector_u = knotvector.generate(3, pts_h, clamped=True) surf.knotvector_v = knotvector.generate(3, pts_v, clamped=True) surf.delta = 0.025 surf.evaluate() surf.vis = VisPlotly.VisSurface() surf.render(colormap=cm.cool)
def test_bspline_surface_evaluate(): surf = BSpline.Surface() surf.degree_u = S_DEGREE_U surf.degree_v = S_DEGREE_V surf.set_ctrlpts(S_CTRLPTS, 3, 3) surf.knotvector_u = S_KV_U surf.knotvector_v = S_KV_V surf.sample_size = SAMPLE_SIZE # Expected output res = [[0.0, 0.0, 0.0], [0.0, 0.5, -0.1875], [0.0, 1.0, -0.75], [0.0, 1.5, -1.6875], [0.0, 2.0, -3.0], [0.5, 0.0, 2.25], [0.5, 0.5, 1.171875], [0.5, 1.0, 0.1875], [0.5, 1.5, -0.703125], [0.5, 2.0, -1.5], [1.0, 0.0, 3.0], [1.0, 0.5, 1.6875], [1.0, 1.0, 0.75], [1.0, 1.5, 0.1875], [1.0, 2.0, 0.0], [1.5, 0.0, 2.25], [1.5, 0.5, 1.359375], [1.5, 1.0, 0.9375], [1.5, 1.5, 0.984375], [1.5, 2.0, 1.5], [2.0, 0.0, 0.0], [2.0, 0.5, 0.1875], [2.0, 1.0, 0.75], [2.0, 1.5, 1.6875], [2.0, 2.0, 3.0]] assert surf.evalpts == res
def test_bspline_surface_loadsave(bspline_surface): fname = FILE_NAME + ".pickle" bspline_surface.save(fname) surf_load = BSpline.Surface() surf_load.load(fname) # Remove save file os.remove(fname) assert bspline_surface.degree_u == surf_load.degree_u assert bspline_surface.degree_v == surf_load.degree_v assert bspline_surface.knotvector_u == surf_load.knotvector_u assert bspline_surface.knotvector_v == surf_load.knotvector_v assert bspline_surface.ctrlpts == surf_load.ctrlpts assert bspline_surface.ctrlpts_size_u == surf_load.ctrlpts_size_u assert bspline_surface.ctrlpts_size_v == surf_load.ctrlpts_size_v assert bspline_surface.dimension == surf_load.dimension
def createBsplineSurface(self, pts, corsequences, sagsequences, degree=3): # Create a BSpline surface instance surface = BSpline.Surface() # Set up the surface surface.degree_u = degree surface.degree_v = degree npoints_u = len(sagsequences) npoints_v = len(corsequences) # Set control points of the diaphragmatic surface surface.set_ctrlpts(pts, npoints_u, npoints_v) surface.knotvector_u = utils.generate_knot_vector( surface.degree_u, npoints_u) surface.knotvector_v = utils.generate_knot_vector( surface.degree_v, npoints_v) return surface
def load_spline_surf(self, spline): # Create a BSpline surface if spline["v_rational"] or spline["u_rational"]: surf = NURBS.Surface() control_points = np.array(spline["poles"]) size_u, size_v = control_points.shape[0], control_points.shape[1] # Set degrees surf.degree_u = spline["u_degree"] surf.degree_v = spline["v_degree"] # Set control points surf.ctrlpts2d = np.concatenate( [control_points, np.ones((size_u, size_v, 1))], 2).tolist() surf.knotvector_v = spline["v_knots"] surf.knotvector_u = spline["u_knots"] weights = spline["weights"] l = [] for i in weights: l += i surf.weights = l return surf else: surf = BSpline.Surface() # Set degrees surf.degree_u = spline["u_degree"] surf.degree_v = spline["v_degree"] # Set control points surf.ctrlpts2d = spline["poles"] # Set knot vectors surf.knotvector_u = spline["u_knots"] surf.knotvector_v = spline["v_knots"] return surf
fname = 'sinusoidalxmindef.txt' f = open(fname, 'w') f.write('Mesh Coordinates\n') f.write('X Y dX dY\n') for i in range(0, len(coords)): f.write('{0} {1} {2} {3} \n'.format(coords[i, 0], coords[i, 1], coords_disp[i, 0], coords_disp[i, 1])) f.close() # Visualize results (displacement only) # Visualize minimization results # Set up new surface disp_surf = bs.Surface() disp_surf.degree_u = 3 disp_surf.degree_v = 3 disp_surf.set_ctrlpts(coords_disp.tolist(), num_ctrlpts, num_ctrlpts) disp_surf.knotvector_u = gutil.generate_knot_vector(disp_surf.degree_u, num_ctrlpts) disp_surf.knotvector_v = gutil.generate_knot_vector(disp_surf.degree_v, num_ctrlpts) disp_surf.delta = 0.01 fname = 'sinusoidalxmindispl' visualize.viz_displacement(def_image, disp_surf, indices[0], indices[1], indices[2], indices[3], fname)
# Control Points rowmin = subregion_indices[-2:].min() rowmax = subregion_indices[-2:].max() colmin = subregion_indices[:2].min() colmax = subregion_indices[:2].max() x = np.linspace(colmin, colmax, 4) y = np.linspace(rowmin, rowmax, 4) coords = np.zeros((len(x) * len(y), 2)) k = 0 for i in range(0, len(x)): for j in range(0, len(y)): coords[k, :] = np.array([x[i], y[j]]) k += 1 # Surface ref_surf = bs.Surface() ref_surf.degree_u = 3 ref_surf.degree_v = 3 num_ctrlpts = np.sqrt(len(coords)).astype('int') ref_surf.set_ctrlpts(coords.tolist(), num_ctrlpts, num_ctrlpts) ref_surf.knotvector_u = gutil.generate_knot_vector(ref_surf.degree_u, num_ctrlpts) ref_surf.knotvector_v = gutil.generate_knot_vector(ref_surf.degree_v, num_ctrlpts) ref_surf.delta = 0.01
# Control Points rowmin_index = subregion_indices[-2:].min() rowmax_index = subregion_indices[-2:].max() colmin_index = subregion_indices[:2].min() colmax_index = subregion_indices[:2].max() x = np.linspace(colmin_index, colmax_index, 4) y = np.linspace(rowmin_index, rowmax_index, 4) coords = np.zeros((len(x) * len(y), 2)) k = 0 for i in range(0, len(x)): for j in range(0, len(y)): coords[k, :] = np.array([x[i], y[j]]) k += 1 # Surface ref_surf = bs.Surface() ref_surf.degree_u = 3 ref_surf.degree_v = 3 num_ctrlpts = np.sqrt(len(coords)).astype('int') ref_surf.set_ctrlpts(coords.tolist(), num_ctrlpts, num_ctrlpts) ref_surf.knotvector_u = gutil.generate_knot_vector(ref_surf.degree_u, num_ctrlpts) ref_surf.knotvector_v = gutil.generate_knot_vector(ref_surf.degree_v, num_ctrlpts) ref_surf.delta = 0.01 #TODO: MAKE THIS A FUNCTION # Compute ROI and ROI uv values
from geomdl import BSpline from geomdl import multi from geomdl import CPGen from geomdl import utilities from geomdl import construct from geomdl.visualization import VisMPL as vis # Required for multiprocessing module if __name__ == "__main__": # Generate control points grid for Surface #1 sg01 = CPGen.Grid(15, 10, z_value=0.0) sg01.generate(8, 8) # Create a BSpline surface instance surf01 = BSpline.Surface() # Set degrees surf01.degree_u = 1 surf01.degree_v = 1 # Get the control points from the generated grid surf01.ctrlpts2d = sg01.grid # Set knot vectors surf01.knotvector_u = utilities.generate_knot_vector( surf01.degree_u, surf01.ctrlpts_size_u) surf01.knotvector_v = utilities.generate_knot_vector( surf01.degree_v, surf01.ctrlpts_size_v) # Generate control points grid for Surface #2
def generate_landscape(): # Create a B-Spline surface instance landscape = BSpline.Surface() # Set up the Bezier surface landscape.degree_u = 3 landscape.degree_v = 3 landscape.ctrlpts_size_u = 17 # x axis (visually vertical) landscape.ctrlpts_size_v = 14 # y axis (visually horizontal) # Use numpy arrays to define each section of the landscape to allow for easier piecewise and global processing # Define the 7x7 mountain section mountain = np.array([[0, 0, 10], [0, 0, 8], [0, 0, 8], [0, 0, 10], [0, 0, 10], [0, 0, 25], [0, 0, 10], [0, 0, 12], [0, 0, 24], [0, 0, 48], [0, 0, 24], [0, 0, 44], [0, 0, 65], [0, 0, 20], [0, 0, 10], [0, 0, 29], [0, 0, 75], [0, 0, 40], [0, 0, 28], [0, 0, 33], [0, 0, 24], [0, 0, 10], [0, 0, 15], [0, 0, 55], [0, 0, 31], [0, 0, 15], [0, 0, 13], [0, 0, 8], [0, 0, -3], [0, 0, 0], [0, 0, 25], [0, 0, 48], [0, 0, 38], [0, 0, 40], [0, 0, 14], [0, 0, -3], [0, 0, -4], [0, 0, -3], [0, 0, 23], [0, 0, 70], [0, 0, 85], [0, 0, 30], [0, 0, 5], [0, 0, -8], [0, 0, -4], [0, 0, 15], [0, 0, 40], [0, 0, 35], [0, 0, 20]]).reshape(7, 7, 3) # Set its global position - top left mountain = set_x_y(mountain, 0, 0) # Define the 7x7 bumpy plains section bumpy_plains = np.array([[0, 0, 10], [0, 0, 10], [0, 0, 10], [0, 0, 10], [0, 0, 10], [0, 0, 10], [0, 0, 10], [0, 0, 10], [0, 0, 10], [0, 0, 20], [0, 0, 20], [0, 0, 40], [0, 0, 30], [0, 0, 10], [0, 0, 20], [0, 0, 30], [0, 0, 20], [0, 0, 60], [0, 0, 80], [0, 0, 10], [0, 0, 10], [0, 0, 15], [0, 0, 20], [0, 0, 40], [0, 0, 20], [0, 0, 70], [0, 0, 20], [0, 0, 10], [0, 0, 40], [0, 0, 10], [0, 0, 30], [0, 0, 60], [0, 0, 70], [0, 0, 20], [0, 0, 10], [0, 0, 40], [0, 0, 40], [0, 0, 40], [0, 0, 20], [0, 0, 30], [0, 0, 40], [0, 0, 10], [0, 0, 20], [0, 0, 40], [0, 0, 20], [0, 0, 10], [0, 0, 40], [0, 0, 40], [0, 0, 10]]).reshape(7, 7, 3) # Set its global position - top right bumpy_plains = set_x_y(bumpy_plains, 0, 35) # Define the 10x7 hill section hill = np.array([[0, 0, 10], [0, 0, -8], [0, 0, 0], [0, 0, 8], [0, 0, 10], [0, 0, 10], [0, 0, 10], [0, 0, 10], [0, 0, -10], [0, 0, -10], [0, 0, 5], [0, 0, -10], [0, 0, -10], [0, 0, -10], [0, 0, 5], [0, 0, 5], [0, 0, 0], [0, 0, -10], [0, 0, -10], [0, 0, -5], [0, 0, 5], [0, 0, 5], [0, 0, 10], [0, 0, 10], [0, 0, 10], [0, 0, 10], [0, 0, 10], [0, 0, 10], [0, 0, 5], [0, 0, 13], [0, 0, 18], [0, 0, 20], [0, 0, 14], [0, 0, 11], [0, 0, 5], [0, 0, 5], [0, 0, 18], [0, 0, 22], [0, 0, 25], [0, 0, 17], [0, 0, 10], [0, 0, 5], [0, 0, 5], [0, 0, 24], [0, 0, 25], [0, 0, 28], [0, 0, 25], [0, 0, 13], [0, 0, 5], [0, 0, 5], [0, 0, 14], [0, 0, 22], [0, 0, 25], [0, 0, 28], [0, 0, 9], [0, 0, 5], [0, 0, 5], [0, 0, 9], [0, 0, 18], [0, 0, 20], [0, 0, 11], [0, 0, 6], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5]]).reshape(10, 7, 3) # Set its global position - bottom left hill = set_x_y(hill, 35, 0) # Define the 10x7 lake section lake = np.array([[0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, -15], [0, 0, -13], [0, 0, -13], [0, 0, 0], [0, 0, 3], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, -13], [0, 0, -15], [0, 0, -13], [0, 0, 0], [0, 0, 3], [0, 0, 5], [0, 0, 5], [0, 0, -13], [0, 0, -18], [0, 0, -15], [0, 0, -13], [0, 0, 0], [0, 0, 5], [0, 0, 5], [0, 0, -13], [0, 0, -15], [0, 0, -18], [0, 0, -15], [0, 0, -13], [0, 0, 5], [0, 0, 5], [0, 0, -10], [0, 0, -15], [0, 0, -18], [0, 0, -20], [0, 0, -15], [0, 0, 5], [0, 0, 5], [0, 0, 3], [0, 0, 0], [0, 0, -13], [0, 0, -20], [0, 0, -13], [0, 0, 5], [0, 0, 5], [0, 0, 3], [0, 0, 0], [0, 0, -13], [0, 0, -5], [0, 0, -13], [0, 0, 5], [0, 0, 5], [0, 0, 3], [0, 0, 2], [0, 0, 3], [0, 0, 5], [0, 0, 6], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5], [0, 0, 5]]).reshape(10, 7, 3) # Set its global position - bottom right lake = set_x_y(lake, 35, 35) # Resulting layout: # -> v axis # mountains | bumpy_plains # ------------------ # hill | lake # Join the mountains and bumpy_plains up along the v axis (horizontally) global_pts = np.hstack((mountain, bumpy_plains)) # Do the same for hill and lake temp_pts = np.hstack((hill, lake)) # Join these two up together vertically to create the final layout, outlined above global_pts = np.vstack((global_pts, temp_pts)) # reshape global_pts to be a flat list global_pts = global_pts.reshape(-1, 3) # convert global_pts from numpy array to python list global_pts = global_pts.tolist() landscape.ctrlpts = global_pts # Return the landscape return landscape
Released under MIT License Developed by Onur Rauf Bingol (c) 2019 """ import os from geomdl import BSpline from geomdl import multi from geomdl import operations from geomdl import exchange from geomdl.visualization import VisMPL as vis # Fix file path os.chdir(os.path.dirname(os.path.realpath(__file__))) # Create a BSpline surface instance surf1 = BSpline.Surface() # Set degrees surf1.degree_u = 3 surf1.degree_v = 3 # Set control points surf1.set_ctrlpts( *exchange.import_txt("ex_surface01.cpt", two_dimensional=True)) # Set knot vectors surf1.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] surf1.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] # Set evaluation delta surf1.delta = 0.025
def process(self): vertices_s = self.inputs['ControlPoints'].sv_get() has_weights = self.inputs['Weights'].is_linked weights_s = self.inputs['Weights'].sv_get(default=[[1.0]]) samples_s = self.inputs['Samples'].sv_get() u_size_s = self.inputs['USize'].sv_get() knots_u_s = self.inputs['KnotsU'].sv_get(default=[[]]) knots_v_s = self.inputs['KnotsV'].sv_get(default=[[]]) degree_u_s = self.inputs['DegreeU'].sv_get() degree_v_s = self.inputs['DegreeV'].sv_get() if self.input_mode == '1D': vertices_s = ensure_nesting_level(vertices_s, 3) else: vertices_s = ensure_nesting_level(vertices_s, 4) def convert_row(verts_row, weights_row): return [(x, y, z, w) for (x, y, z), w in zip(verts_row, weights_row)] verts_out = [] edges_out = [] faces_out = [] surfaces_out = [] inputs = zip_long_repeat(vertices_s, weights_s, knots_u_s, knots_v_s, degree_u_s, degree_v_s, samples_s, u_size_s) for vertices, weights, knots_u, knots_v, degree_u, degree_v, samples, u_size in inputs: if isinstance(samples, (list, tuple)): samples = samples[0] if isinstance(degree_u, (tuple, list)): degree_u = degree_u[0] if isinstance(degree_v, (tuple, list)): degree_v = degree_v[0] if isinstance(u_size, (list, tuple)): u_size = u_size[0] if self.input_mode == '1D': fullList(weights, len(vertices)) else: if isinstance(weights[0], (int, float)): weights = [weights] fullList(weights, len(vertices)) for verts_u, weights_u in zip(vertices, weights): fullList(weights_u, len(verts_u)) # Generate surface if self.surface_mode == 'NURBS': surf = NURBS.Surface(normalize_kv=self.normalize_knots) else: # BSPLINE surf = BSpline.Surface(normalize_kv=self.normalize_knots) surf.degree_u = degree_u surf.degree_v = degree_v if self.input_mode == '1D': n_v = u_size n_u = len(vertices) // n_v vertices = grouper(vertices, n_u) weights = grouper(weights, n_u) if self.knot_mode == 'AUTO': if self.is_cyclic_v: for row_idx in range(len(vertices)): vertices[row_idx].extend( vertices[row_idx][:degree_v + 1]) weights[row_idx].extend( weights[row_idx][:degree_v + 1]) if self.is_cyclic_u: vertices.extend(vertices[:degree_u + 1]) weights.extend(weights[:degree_u + 1]) self.debug("UxV: %s x %s", len(vertices), len(vertices[0])) # Control points if self.surface_mode == 'NURBS': ctrlpts = list(map(convert_row, vertices, weights)) surf.ctrlpts2d = ctrlpts else: surf.ctrlpts2d = vertices n_u_total = len(vertices) n_v_total = len(vertices[0]) if self.knot_mode == 'AUTO': if self.is_cyclic_u: knots_u = list(range(n_u_total + degree_u + 1)) else: knots_u = knotvector.generate(surf.degree_u, n_u_total) self.debug("Auto knots U: %s", knots_u) surf.knotvector_u = knots_u if self.is_cyclic_v: knots_v = list(range(n_v_total + degree_v + 1)) else: knots_v = knotvector.generate(surf.degree_v, n_v_total) self.debug("Auto knots V: %s", knots_v) surf.knotvector_v = knots_v else: surf.knotvector_u = knots_u surf.knotvector_v = knots_v new_surf = SvExGeomdlSurface(surf) if self.is_cyclic_u: u_min = surf.knotvector_u[degree_u] u_max = surf.knotvector_u[-degree_u - 2] new_surf.u_bounds = u_min, u_max print("U:", new_surf.u_bounds) else: u_min = min(surf.knotvector_u) u_max = max(surf.knotvector_u) new_surf.u_bounds = u_min, u_max if self.is_cyclic_v: v_min = surf.knotvector_v[degree_v] v_max = surf.knotvector_v[-degree_v - 2] new_surf.v_bounds = v_min, v_max print("V:", new_surf.v_bounds) else: v_min = min(surf.knotvector_v) v_max = max(surf.knotvector_v) new_surf.v_bounds = v_min, v_max surfaces_out.append(new_surf) if self.make_grid: surf.sample_size = samples surf.tessellate() new_verts = [vert.data for vert in surf.vertices] new_faces = [f.data for f in surf.faces] else: new_verts = [] new_faces = [] verts_out.append(new_verts) faces_out.append(new_faces) if self.make_grid: self.outputs['Vertices'].sv_set(verts_out) self.outputs['Faces'].sv_set(faces_out) self.outputs['Surface'].sv_set(surfaces_out)
def tracks2surf(tracks, param, delta=0.01, xparam="bp_rp", yparam="mg", xstep=0.1, ystep=0.1): """ tracks2surf(tracks, param, delta=0.01, xparam="bp_rp", yparam="mg", xstep=0.1, ystep=0.1) Compute a NURBS surface from a set of stellar evolution tracks, using the `geomdl` package. Parameters ---------- tracks : Table Stellar-track grid, as retrieved by `stam.gentracks.get_isomasses` or `stam.gentracks.get_combined_isomasses`. param : str The parameter to evaluate (options: "mass", "age", "mh"). delta : float, optional `geomdl` surface evaluation step size. xparam : str, optional x-axis parameter (default: "bp_rp"). yparam : str, optional y-axis parameter (default: "mg"). xstep : float, optional x-axis grid step (default: 0.1). ystep : float, optional y-axis grid step (default: 0.1). Returns ------- surf : `geomdl.NURBS.Surface` object 2D NURBS surface based on `tracks`. """ xmin = np.min(np.around(tracks[xparam], -int(np.round(np.log10(xstep))))) xmax = np.max(np.around(tracks[xparam], -int(np.round(np.log10(xstep))))) ymin = np.min(np.around(tracks[yparam], -int(np.round(np.log10(ystep))))) ymax = np.max(np.around(tracks[yparam], -int(np.round(np.log10(ystep))))) x, y = np.meshgrid(np.arange(xmin, xmax, xstep), np.arange(ymin, ymax, ystep)) z = np.zeros_like(x) * np.nan for i in range(x.shape[0]): for j in range(x.shape[1]): dist = np.sqrt((x[i, j] - tracks[xparam])**2 + (y[i, j] - tracks[yparam])**2) min_idx = np.argmin(dist) z[i, j] = tracks[param][min_idx] ctrlpts = np.array([x.flatten(), y.flatten(), z.flatten()]).T ctrlpts = ctrlpts.reshape((x.shape[0], x.shape[1], 3)) ctrlpts = ctrlpts.tolist() surf = BSpline.Surface() surf.degree_u = 3 surf.degree_v = 3 surf.ctrlpts2d = ctrlpts surf.knotvector_u = knotvector.generate(surf.degree_u, surf.ctrlpts_size_u) surf.knotvector_v = knotvector.generate(surf.degree_v, surf.ctrlpts_size_v) surf.delta = delta return surf
Released under MIT License Developed by Onur Rauf Bingol (c) 2019 """ import os from geomdl import BSpline from geomdl import exchange from geomdl import tessellate from geomdl.visualization import VisVTK as vis from geomdl.shapes import analytic # Fix file path os.chdir(os.path.dirname(os.path.realpath(__file__))) # Create a BSpline surface instance surf = BSpline.Surface() # Set degrees surf.degree_u = 3 surf.degree_v = 3 # Set control points surf.set_ctrlpts( *exchange.import_txt("../ex_surface01.cpt", two_dimensional=True)) # Set knot vectors surf.knotvector_u = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] surf.knotvector_v = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 3.0, 3.0, 3.0] # Set sample size surf.sample_size = 35
def mean_f(self, x, Bspl): # Produce one row in the F matrix if self.reg is None or self.reg.lower() == 'constant': f = np.array([1]) return f elif self.reg.lower() == 'first': # 1, x1, x2 # F = np.array([[1] * n, [x[0]] * n, [x[1]] * n]).T f = np.array([1, x[0], x[1], x[0] * x[1]]) # f = np.array([1, x[0], x[1], x[0] * x[1], x[0]**2, x[1]**2]) # f = np.array([1, x[0], x[1], x[0] * x[1]**4, x[0]**4, x[1]**4]) return f elif self.reg.lower() == 'second': f = np.array([1, x[0], x[1], x[0] * x[1], x[0]**2, x[1]**2]) return f elif self.reg.lower() == 'third': f = np.array([ 1, x[0], x[1], x[0] * x[1], x[0]**2, x[1]**2, x[1] * x[0]**2, x[0] * x[1]**2 ]) return f elif self.reg.lower() == 'cubic': ''' Natural cubic spline, implementation from ch 5.3 in The elements of statisitical modeling ''' # minv = min(self.X) # maxv = max(self.X) # Check if significant fewer than points # if self.n > 3 ** self.PLS_order: # self.PLS_order = PLS_order # # self.PLS_order = int(np.floor(np.log(self.n) / np.log(3))) # knots = np.linspace(-1, 1, num=3) f = nc.basis_2d(x, nd=self.PLS_order) return f elif self.reg.lower() == 'cubic2': ''' Natural cubic spline, implementation from ch 5.3 in The elements of statisitical modeling ''' knots = np.linspace(-1, 1, num=4) f = nc.basis_2d(x, knots) return f elif self.reg.lower() == 'bspline': Bspl = BSpline.Surface() Bspl.delta = 0.025 degree_u = 2 degree_v = 2 Bspl.degree_u = degree_u Bspl.degree_v = degree_v # Set ctrlpts ctrlpts_size_u = 4 ctrlpts_size_v = 4 i_vec = np.linspace(0, 1, num=ctrlpts_size_u) j_vec = np.linspace(0, 1, num=ctrlpts_size_v) initial_CP = [] # np.zeros((6, 6, 3)) mean_inp = np.sum(self.y) / len(self.y) for i in range(0, len(i_vec)): for j in range(0, len(j_vec)): initial_CP.append([i_vec[i], j_vec[j], mean_inp]) Bspl.set_ctrlpts(initial_CP, ctrlpts_size_u, ctrlpts_size_v) # open Bspl.knotvector_u = tuple( np.linspace(0, 1, num=Bspl.degree_u + ctrlpts_size_u + 1).tolist()) Bspl.knotvector_v = tuple( np.linspace(0, 1, num=Bspl.degree_v + ctrlpts_size_v + 1).tolist()) ################ numb = Bspl.degree_u + ctrlpts_size_u + 1 vec = np.linspace(0.1, 1, num=np.floor(numb / 2)).tolist() k = 2 vec = [elem**k for elem in vec] # Normalize vec = vec / (2 * np.max(vec)) if numb % 2 == 0: # Do not append mid number if uneven number of points! lst = np.sort(np.append(vec, -vec)) + 0.5 else: lst = np.sort(np.append(np.append(vec, 0), -vec)) + 0.5 Bspl.knotvector_u = tuple(lst) Bspl.knotvector_v = tuple(lst) ################################################################ self.Bspl = Bspl start_u = Bspl._knot_vector_u[Bspl._degree_u] stop_u = Bspl._knot_vector_u[-(Bspl._degree_u + 1)] start_v = Bspl._knot_vector_u[Bspl._degree_v] stop_v = Bspl._knot_vector_u[-(Bspl._degree_v + 1)] # Map variables to valid knot space knots_u = start_u + (stop_u - start_u) * x[:, 0] knots_v = start_v + (stop_v - start_v) * x[:, 1] spans_u = helpers.find_spans(degree_u, Bspl.knotvector_u, ctrlpts_size_u, knots_u, Bspl._span_func) spans_v = helpers.find_spans(degree_v, Bspl.knotvector_v, ctrlpts_size_v, knots_v, Bspl._span_func) basis_u = helpers.basis_functions(degree_u, Bspl.knotvector_u, spans_u, knots_u) basis_v = helpers.basis_functions(degree_v, Bspl.knotvector_v, spans_v, knots_v) # Adds the zeros for the missing bases! basis_u_full = self.basis_full(basis_u, degree_u, spans_u) basis_v_full = self.basis_full(basis_v, degree_v, spans_v) plot_base = 0 if plot_base: ''' Ad hoc programmed helper function that plot the shape functions of the bspline in one direction, for a few different made up knot vectors.''' help_plots.plot_base(ctrlpts_size_u, Bspl) B_row = None for i in range(len(basis_u_full)): # Following Nils Carlssons master thesis A_conc = np.concatenate( np.outer(basis_u_full[i], basis_v_full[i])) if B_row is None: B_row = A_conc else: B_row = np.append( B_row, A_conc) # one long row of data, Rewrite for speed? F = np.reshape( B_row, (-1, len(A_conc))) # Sort up the control points in one vector return F else: print('Unknown trend function type') raise ValueError