def test_gauss_curvature_2(self): weights = [[1, 1, 1, 1], [1, 2, 3, 1], [1, 3, 4, 1], [1, 4, 5, 1], [1, 1, 1, 1]] #us, vs = self.us, self.vs us = np.linspace(0.0, 1.0, num=10) vs = np.linspace(0.0, 1.0, num=20) us, vs = np.meshgrid(us, vs) us = us.flatten() vs = vs.flatten() geomdl_surface = SvGeomdlSurface.build_geomdl( self.degree_u, self.degree_v, self.knotvector_u, self.knotvector_v, self.control_points, weights) native_surface = SvNativeNurbsSurface(self.degree_u, self.degree_v, self.knotvector_u, self.knotvector_v, self.control_points, weights) c1 = geomdl_surface.curvature_calculator(us, vs) c2 = native_surface.curvature_calculator(us, vs) self.assert_numpy_arrays_equal(c1.fu, c2.fu, precision=8) self.assert_numpy_arrays_equal(c1.fv, c2.fv, precision=8) self.assert_numpy_arrays_equal(c1.duu, c2.duu, precision=8) self.assert_numpy_arrays_equal(c1.dvv, c2.dvv, precision=8) self.assert_numpy_arrays_equal(c1.duv, c2.duv, precision=8) self.assert_numpy_arrays_equal(c1.nuu, c2.nuu, precision=8, fail_fast=False) self.assert_numpy_arrays_equal(c1.nvv, c2.nvv, precision=8) self.assert_numpy_arrays_equal(c1.nuv, c2.nuv, precision=8) vs1 = geomdl_surface.gauss_curvature_array(self.us, self.vs) vs2 = native_surface.gauss_curvature_array(self.us, self.vs) self.assert_numpy_arrays_equal(vs1, vs2, precision=8, fail_fast=False)
def get_geometry(self): if self.mode == 'CURVE': curves = self.inputs['Curves'].sv_get() if isinstance(curves[0], (list, tuple)): curves = sum(curves, []) container = multi.CurveContainer() for i, curve in enumerate(curves): if not isinstance(curve, SvNurbsCurve): if hasattr(curve, 'to_nurbs'): curve = curve.to_nurbs( implementation=SvNurbsCurve.GEOMDL) else: raise TypeError( "Provided object #%s is not a NURBS curve, but %s!" % (i, type(curve))) container.append(SvGeomdlCurve.from_any_nurbs(curve).curve) return container else: # SURFACE surfaces = self.inputs['Surfaces'].sv_get() if isinstance(surfaces[0], (list, tuple)): surfaces = sum(surfaces, []) container = multi.SurfaceContainer() for i, surface in enumerate(surfaces): if not isinstance(surface, SvNurbsSurface): if hasattr(surface, 'to_nurbs'): surface = surface.to_nurbs( implementation=SvNurbsCurve.GEOMDL) else: raise TypeError( "Provided object #%s is not a NURBS surface, but %s!" % (i, type(surface))) container.append( SvGeomdlSurface.from_any_nurbs(surface).surface) return container
def process(self): if not any(socket.is_linked for socket in self.outputs): return vertices_s = self.inputs['Vertices'].sv_get() u_size_s = self.inputs['USize'].sv_get() degree_u_s = self.inputs['DegreeU'].sv_get() degree_v_s = self.inputs['DegreeV'].sv_get() points_cnt_u_s = self.inputs['PointsCntU'].sv_get() points_cnt_v_s = self.inputs['PointsCntV'].sv_get() if self.input_mode == '1D': vertices_s = ensure_nesting_level(vertices_s, 3) else: vertices_s = ensure_nesting_level(vertices_s, 4) surfaces_out = [] points_out = [] knots_u_out = [] knots_v_out = [] for vertices, degree_u, degree_v, points_cnt_u, points_cnt_v, u_size in zip_long_repeat(vertices_s, degree_u_s, degree_v_s, points_cnt_u_s, points_cnt_v_s, u_size_s): 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, (tuple, list)): u_size = u_size[0] if isinstance(points_cnt_u, (tuple, list)): points_cnt_u = points_cnt_u[0] if isinstance(points_cnt_v, (tuple, list)): points_cnt_v = points_cnt_v[0] if self.input_mode == '1D': n_u = u_size n_v = len(vertices) // n_u else: n_u = len(vertices[0]) for i, verts_i in enumerate(vertices): if len(verts_i) != n_u: raise Exception("Number of vertices in row #{} is not the same as in the first ({} != {})!".format(i, n_u, len(verts_i))) vertices = sum(vertices, []) n_v = len(vertices) // n_u kwargs = dict(centripetal = self.centripetal) if self.has_points_cnt: kwargs['ctrlpts_size_u'] = points_cnt_u kwargs['ctrlpts_size_v'] = points_cnt_v surf = fitting.approximate_surface(vertices, n_u, n_v, degree_u, degree_v, **kwargs) points_out.append(surf.ctrlpts2d) knots_u_out.append(surf.knotvector_u) knots_v_out.append(surf.knotvector_v) surf = SvGeomdlSurface(surf) surfaces_out.append(surf) self.outputs['Surface'].sv_set(surfaces_out) self.outputs['ControlPoints'].sv_set(points_out) self.outputs['KnotsU'].sv_set(knots_u_out) self.outputs['KnotsV'].sv_set(knots_v_out)
def test_gauss_curvature(self): geomdl_surface = SvGeomdlSurface.build_geomdl( self.degree_u, self.degree_v, self.knotvector_u, self.knotvector_v, self.control_points, self.weights) native_surface = SvNativeNurbsSurface(self.degree_u, self.degree_v, self.knotvector_u, self.knotvector_v, self.control_points, self.weights) vs1 = geomdl_surface.gauss_curvature_array(self.us, self.vs) vs2 = native_surface.gauss_curvature_array(self.us, self.vs) self.assert_sverchok_data_equal(vs1, vs2, precision=8)
def test_normal(self): geomdl_surface = SvGeomdlSurface.build_geomdl( self.degree_u, self.degree_v, self.knotvector_u, self.knotvector_v, self.control_points, self.weights) native_surface = SvNativeNurbsSurface(self.degree_u, self.degree_v, self.knotvector_u, self.knotvector_v, self.control_points, self.weights) vs1 = geomdl_surface.normal_array(self.us, self.vs) vs2 = native_surface.normal_array(self.us, self.vs) self.assert_numpy_arrays_equal(vs1, vs2, precision=8)
def test_eval_2(self): weights = [[1, 1, 1, 1], [1, 2, 3, 1], [1, 3, 4, 1], [1, 4, 5, 1], [1, 1, 1, 1]] geomdl_surface = SvGeomdlSurface.build_geomdl( self.degree_u, self.degree_v, self.knotvector_u, self.knotvector_v, self.control_points, weights) native_surface = SvNativeNurbsSurface(self.degree_u, self.degree_v, self.knotvector_u, self.knotvector_v, self.control_points, weights) vs1 = geomdl_surface.evaluate_array(self.us, self.vs) vs2 = native_surface.evaluate_array(self.us, self.vs) self.assert_numpy_arrays_equal(vs1, vs2, precision=8, fail_fast=False)
def extrude_along_vector(self, vector): vector = np.array(vector) my_control_points = self.get_control_points() my_weights = self.get_weights() other_control_points = my_control_points + vector control_points = np.stack((my_control_points, other_control_points)) control_points = np.transpose(control_points, axes=(1,0,2)).tolist() weights = np.stack((my_weights, my_weights)).T.tolist() my_knotvector = self.get_knotvector() my_degree = self.get_degree() knotvector_v = sv_knotvector.generate(1, 2, clamped=True) surface = SvGeomdlSurface.build(degree_u = my_degree, degree_v = 1, knotvector_u = my_knotvector, knotvector_v = knotvector_v, control_points = control_points, weights = weights) return surface
def process(self): if not any(socket.is_linked for socket in self.outputs): return if not self.text_block: return curves_out = [] surfaces_out = [] items = self.load_json() for i, item in enumerate(items): if isinstance(item, BSpline.Curve): curve = SvGeomdlCurve(item) curves_out.append(curve) elif isinstance(item, BSpline.Surface): surface = SvGeomdlSurface(item) surfaces_out.append(surface) else: self.warning("JSON data item #%s contains unsupported data type: %s", i, type(item)) self.outputs['Curves'].sv_set(curves_out) self.outputs['Surfaces'].sv_set(surfaces_out)
def make_surface(self, face, degree_u, degree_v, vertices, planes, vert_weights, tangent_weights, face_weight, edge_weights_dict, edge_planes_dict): """ V0 ------ [E01] --- [E0C] --- [E02] --- V1 | | | | | | | | | | | | | | | [E11] --- [F1] ---- [E0F] --- [F2] --- [E21] | | | | | | | | | | | | | | | [E1C] --- [E1F] --- [CC] --- [E2F] --- [E2C] | | | | | | | | | | | | | | | [E12] --- [F3] ---- [E3F] --- [F4] --- [E22] | | | | | | | | | | | | | | | V3 ------ [E31] --- [E3C] --- [E32] --- V2 """ tangent_weights = [w / 3.0 for w in tangent_weights] vertices = [Vector(v) for v in vertices] def mk_edge_point(i, j): return (vertices[j] - vertices[i]) * tangent_weights[i] + vertices[i] def mk_face_corner_point(i, j, k): dv1 = (vertices[j] - vertices[i]) * tangent_weights[i] dv2 = (vertices[k] - vertices[i]) * tangent_weights[i] #m = face_weight return planes[i].projection_of_point(vertices[i] + dv1 + dv2) # edge planes e0p = edge_planes_dict[(face[0], face[1])] e1p = edge_planes_dict[(face[0], face[3])] e2p = edge_planes_dict[(face[1], face[2])] e3p = edge_planes_dict[(face[2], face[3])] def mk_edge_center_point(ep1, ep2): return (ep1 + ep2) / 2.0 def mk_face_edge_point(edge_plane, edge_point, edge_vec, vec1, vec2): length = (vec1.length + vec2.length) / 2.0 vec = edge_plane.normal.cross(edge_vec) #print("EV: %s, N: %s, L: %s, Res: %s" % (edge_vec, edge_plane.normal, length, vec)) vec = length * vec.normalized() return edge_point + vec e01 = planes[0].projection_of_point(mk_edge_point(0, 1)) e02 = planes[1].projection_of_point(mk_edge_point(1, 0)) e11 = planes[0].projection_of_point(mk_edge_point(0, 3)) e21 = planes[1].projection_of_point(mk_edge_point(1, 2)) f1 = mk_face_corner_point(0, 1, 3) f2 = mk_face_corner_point(1, 0, 2) e12 = planes[3].projection_of_point(mk_edge_point(3, 0)) e31 = planes[3].projection_of_point(mk_edge_point(3, 2)) e32 = planes[2].projection_of_point(mk_edge_point(2, 3)) e22 = planes[2].projection_of_point(mk_edge_point(2, 1)) f3 = mk_face_corner_point(3, 0, 2) f4 = mk_face_corner_point(2, 3, 1) e0c = mk_edge_center_point(e01, e02) e1c = mk_edge_center_point(e11, e12) e2c = mk_edge_center_point(e21, e22) e3c = mk_edge_center_point(e31, e32) e0f = mk_face_edge_point(e0p, e0c, (vertices[1] - vertices[0]), (f1 - e01), (f2 - e02)) e1f = mk_face_edge_point(e1p, e1c, (vertices[0] - vertices[3]), (f3 - e12), (f1 - e11)) e2f = mk_face_edge_point(e2p, e2c, (vertices[2] - vertices[1]), (f2 - e21), (f4 - e22)) e3f = mk_face_edge_point(e3p, e3c, (vertices[3] - vertices[2]), (f3 - e31), (f4 - e32)) cc = center([f1, e0f, f2, e2f, f4, e3f, f3, e1f]) control_points = [ vertices[0], e01, e0c, e02, vertices[1], e11, f1, e0f, f2, e21, e1c, e1f, cc, e2f, e2c, e12, f3, e3f, f4, e22, vertices[3], e31, e3c, e32, vertices[2] ] # edge point weights e0w = edge_weights_dict[(face[0], face[1])] e1w = edge_weights_dict[(face[0], face[3])] e2w = edge_weights_dict[(face[1], face[2])] e3w = edge_weights_dict[(face[2], face[3])] weights = [ vert_weights[0], e0w, e0w, e0w, vert_weights[1], e1w, face_weight, face_weight, face_weight, e2w, e1w, face_weight, face_weight, face_weight, e2w, e1w, face_weight, face_weight, face_weight, e2w, vert_weights[3], e3w, e3w, e3w, vert_weights[2] ] surface = NURBS.Surface() surface.degree_u = degree_u surface.degree_v = degree_v surface.ctrlpts_size_u = 5 surface.ctrlpts_size_v = 5 surface.ctrlpts = control_points surface.weights = weights surface.knotvector_u = knotvector.generate(surface.degree_u, 5) surface.knotvector_v = knotvector.generate(surface.degree_v, 5) new_surf = SvGeomdlSurface(surface) return new_surf, control_points, weights
def process(self): vertices_s = self.inputs['Vertices'].sv_get() u_size_s = self.inputs['USize'].sv_get() 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) surfaces_out = [] points_out = [] knots_u_out = [] knots_v_out = [] for vertices, degree_u, degree_v, u_size in zip_long_repeat( vertices_s, degree_u_s, degree_v_s, u_size_s): 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, (tuple, list)): u_size = u_size[0] if self.input_mode == '1D': n_u = u_size n_v = len(vertices) // n_u else: n_u = len(vertices[0]) for i, verts_i in enumerate(vertices): if len(verts_i) != n_u: raise Exception( "Number of vertices in row #{} is not the same as in the first ({} != {})!" .format(i, n_u, len(verts_i))) vertices = sum(vertices, []) n_v = len(vertices) // n_u if geomdl is not None and self.nurbs_implementation == SvNurbsMaths.GEOMDL: surf = fitting.interpolate_surface( vertices, n_u, n_v, degree_u, degree_v, centripetal=self.centripetal) surf = SvGeomdlSurface(surf) else: vertices_np = np.array(split_by_count(vertices, n_v)) vertices_np = np.transpose(vertices_np, axes=(1, 0, 2)) surf = interpolate_nurbs_surface(degree_u, degree_v, vertices_np, metric=self.metric) points_out.append(surf.get_control_points().tolist()) knots_u_out.append(surf.get_knotvector_u().tolist()) knots_v_out.append(surf.get_knotvector_v().tolist()) surfaces_out.append(surf) self.outputs['Surface'].sv_set(surfaces_out) self.outputs['ControlPoints'].sv_set(points_out) self.outputs['KnotsU'].sv_set(knots_u_out) self.outputs['KnotsV'].sv_set(knots_v_out)