Exemplo n.º 1
0
        def process(self):
            if not any(socket.is_linked for socket in self.outputs):
                return

            vertices_s = self.inputs['Vertices'].sv_get()
            degree_s = self.inputs['Degree'].sv_get()

            curves_out = []
            points_out = []
            knots_out = []
            for vertices, degree in zip_long_repeat(vertices_s, degree_s):
                if isinstance(degree, (tuple, list)):
                    degree = degree[0]

                curve = fitting.interpolate_curve(vertices,
                                                  degree,
                                                  centripetal=self.centripetal)

                points_out.append(curve.ctrlpts)
                knots_out.append(curve.knotvector)

                curve = SvGeomdlCurve(curve)
                curves_out.append(curve)

            self.outputs['Curve'].sv_set(curves_out)
            self.outputs['ControlPoints'].sv_set(points_out)
            self.outputs['Knots'].sv_set(knots_out)
Exemplo n.º 2
0
    def test_remove_geomdl_1(self):
        points = np.array([[0, 0, 0], [1, 1, 0], [2, 0, 0]])
        degree = 2
        kv = sv_knotvector.generate(degree, 3)
        weights = [1, 1, 1]
        ts = np.linspace(0.0, 1.0, num=5)

        curve = SvGeomdlCurve.build_geomdl(degree, kv, points, weights)
        orig_pts = curve.evaluate_array(ts)
        inserted = curve.insert_knot(0.5, 1)

        self.assert_numpy_arrays_equal(inserted.evaluate_array(ts),
                                       orig_pts,
                                       precision=8)

        expected_inserted_kv = np.array([0, 0, 0, 0.5, 1, 1, 1])
        self.assert_numpy_arrays_equal(inserted.get_knotvector(),
                                       expected_inserted_kv,
                                       precision=8)

        removed = inserted.remove_knot(0.5, 1)
        self.assert_numpy_arrays_equal(removed.get_knotvector(),
                                       kv,
                                       precision=8)
        #self.assert_numpy_arrays_equal(removed.evaluate_array(ts), orig_pts)

        #print("CP", removed.get_control_points())
        #print("W", removed.get_weights())
        self.assert_numpy_arrays_equal(removed.get_control_points(),
                                       points,
                                       precision=8)
Exemplo n.º 3
0
 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
Exemplo n.º 4
0
        def process(self):
            if not any(socket.is_linked for socket in self.outputs):
                return

            vertices_s = self.inputs['Vertices'].sv_get()
            degree_s = self.inputs['Degree'].sv_get()
            points_cnt_s = self.inputs['PointsCnt'].sv_get()

            curves_out = []
            points_out = []
            knots_out = []
            for vertices, degree, points_cnt in zip_long_repeat(
                    vertices_s, degree_s, points_cnt_s):
                if isinstance(degree, (tuple, list)):
                    degree = degree[0]
                if isinstance(points_cnt, (tuple, list)):
                    points_cnt = points_cnt[0]

                kwargs = dict(centripetal=self.centripetal)
                if self.has_points_cnt:
                    kwargs['ctrlpts_size'] = points_cnt

                curve = fitting.approximate_curve(vertices, degree, **kwargs)

                points_out.append(curve.ctrlpts)
                knots_out.append(curve.knotvector)

                curve = SvGeomdlCurve(curve)
                curves_out.append(curve)

            self.outputs['Curve'].sv_set(curves_out)
            self.outputs['ControlPoints'].sv_set(points_out)
            self.outputs['Knots'].sv_set(knots_out)
Exemplo n.º 5
0
 def test_curve_tangent(self):
     geomdl_curve = SvGeomdlCurve.build(self.degree, self.knotvector,
                                        self.control_points, self.weights)
     t1s = geomdl_curve.tangent_array(self.ts)
     native_curve = SvNativeNurbsCurve(self.degree, self.knotvector,
                                       self.control_points, self.weights)
     t2s = native_curve.tangent_array(self.ts)
     self.assert_numpy_arrays_equal(t1s, t2s, precision=8)
Exemplo n.º 6
0
 def test_curve_eval_2(self):
     weights = [1.0, 2.0, 3.0, 1.0]
     geomdl_curve = SvGeomdlCurve.build_geomdl(self.degree, self.knotvector,
                                               self.control_points, weights)
     t1s = geomdl_curve.evaluate_array(self.ts)
     native_curve = SvNativeNurbsCurve(self.degree, self.knotvector,
                                       self.control_points, weights)
     t2s = native_curve.evaluate_array(self.ts)
     self.assert_numpy_arrays_equal(t1s, t2s, precision=8)
Exemplo n.º 7
0
 def test_curve_3436_2(self):
     points = [(0, 0, 0), (0.5, 0, 0.5), (1, 0, 0)]
     ts = np.array([0, 0.5, 1])
     degree = 2
     knotvector = [0, 0, 0, 1, 1, 1]
     weights = [1, 1, 1]
     geomdl_curve = SvGeomdlCurve.build(degree, knotvector, points, weights)
     native_curve = SvNativeNurbsCurve(degree, knotvector, points, weights)
     p1s = geomdl_curve.third_derivative_array(ts)
     p2s = native_curve.third_derivative_array(ts)
     self.assert_numpy_arrays_equal(p1s, p2s, precision=8)
Exemplo n.º 8
0
 def make(vertices):
     metric = 'CENTRIPETAL' if self.centripetal else 'DISTANCE'
     vertices = np.array(vertices)
     if geomdl is not None and self.nurbs_implementation == SvNurbsCurve.GEOMDL:
         curve = SvGeomdlCurve.interpolate(degree,
                                           vertices,
                                           metric=metric)
     else:
         curve = SvNativeNurbsCurve.interpolate(degree,
                                                vertices,
                                                metric=metric)
     return curve
Exemplo n.º 9
0
        def process(self):
            if not any(socket.is_linked for socket in self.outputs):
                return

            vertices_s = self.inputs['Vertices'].sv_get()
            degree_s = self.inputs['Degree'].sv_get()
            points_cnt_s = self.inputs['PointsCnt'].sv_get()

            input_level = get_data_nesting_level(vertices_s)
            vertices_s = ensure_nesting_level(vertices_s, 4)
            degree_s = ensure_nesting_level(degree_s, 2)
            points_cnt_s = ensure_nesting_level(points_cnt_s, 2)

            nested_output = input_level > 3

            curves_out = []
            points_out = []
            knots_out = []
            for params in zip_long_repeat(vertices_s, degree_s, points_cnt_s):
                new_curves = []
                new_points = []
                new_knots = []
                for vertices, degree, points_cnt in zip_long_repeat(*params):

                    kwargs = dict(centripetal=self.centripetal)
                    if self.has_points_cnt:
                        kwargs['ctrlpts_size'] = points_cnt

                    curve = fitting.approximate_curve(vertices, degree,
                                                      **kwargs)

                    new_points.append(curve.ctrlpts)
                    new_knots.append(curve.knotvector)

                    curve = SvGeomdlCurve(curve)
                    new_curves.append(curve)

                if nested_output:
                    curves_out.append(new_curves)
                    points_out.append(new_points)
                    knots_out.append(new_knots)
                else:
                    curves_out.extend(new_curves)
                    points_out.extend(new_points)
                    knots_out.extend(new_knots)

            self.outputs['Curve'].sv_set(curves_out)
            self.outputs['ControlPoints'].sv_set(points_out)
            self.outputs['Knots'].sv_set(knots_out)
Exemplo n.º 10
0
        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)
Exemplo n.º 11
0
    def process(self):
        if not any(socket.is_linked for socket in self.outputs):
            return

        vertices_s = self.inputs['Vertices'].sv_get()
        weights_s = self.inputs['Weights'].sv_get(default=[[[None]]])
        degree_s = self.inputs['Degree'].sv_get()
        points_cnt_s = self.inputs['PointsCnt'].sv_get()
        smoothing_s = self.inputs['Smoothing'].sv_get()

        input_level = get_data_nesting_level(vertices_s)
        vertices_s = ensure_nesting_level(vertices_s, 4)
        degree_s = ensure_nesting_level(degree_s, 2)
        points_cnt_s = ensure_nesting_level(points_cnt_s, 2)
        smoothing_s = ensure_nesting_level(smoothing_s, 2)
        has_weights = self.inputs['Weights'].is_linked
        if has_weights:
            weights_s = ensure_nesting_level(weights_s, 3)

        nested_output = input_level > 3

        curves_out = []
        points_out = []
        knots_out = []
        for params in zip_long_repeat(vertices_s, weights_s, degree_s,
                                      points_cnt_s, smoothing_s):
            new_curves = []
            new_points = []
            new_knots = []
            for vertices, weights, degree, points_cnt, smoothing in zip_long_repeat(
                    *params):

                if self.implementation == 'GEOMDL':
                    kwargs = dict(centripetal=self.centripetal)
                    if self.has_points_cnt:
                        kwargs['ctrlpts_size'] = points_cnt

                    curve = fitting.approximate_curve(vertices, degree,
                                                      **kwargs)
                    control_points = curve.ctrlpts
                    knotvector = curve.knotvector
                    curve = SvGeomdlCurve(curve)
                else:  # SCIPY:
                    points = np.array(vertices)
                    if has_weights:
                        weights = repeat_last_for_length(
                            weights, len(vertices))
                    else:
                        weights = None
                    if not self.has_smoothing:
                        smoothing = None

                    if self.is_cyclic:
                        if self.auto_cyclic:
                            dv = np.linalg.norm(points[0] - points[-1])
                            is_cyclic = dv <= self.cyclic_threshold
                            self.info("Dv %s, threshold %s => is_cyclic %s",
                                      dv, self.cyclic_threshold, is_cyclic)
                        else:
                            is_cyclic = True
                    else:
                        is_cyclic = False

                    curve = scipy_nurbs_approximate(
                        points,
                        weights=weights,
                        metric=self.metric,
                        degree=degree,
                        filter_doubles=None
                        if not self.remove_doubles else self.threshold,
                        smoothing=smoothing,
                        is_cyclic=is_cyclic)

                    control_points = curve.get_control_points().tolist()
                    knotvector = curve.get_knotvector().tolist()

                new_curves.append(curve)
                new_points.append(control_points)
                new_knots.append(knotvector)

            if nested_output:
                curves_out.append(new_curves)
                points_out.append(new_points)
                knots_out.append(new_knots)
            else:
                curves_out.extend(new_curves)
                points_out.extend(new_points)
                knots_out.extend(new_knots)

        self.outputs['Curve'].sv_set(curves_out)
        self.outputs['ControlPoints'].sv_set(points_out)
        self.outputs['Knots'].sv_set(knots_out)
Exemplo n.º 12
0
 def make(vertices):
     curve = fitting.interpolate_curve(vertices,
                                       degree,
                                       centripetal=self.centripetal)
     return SvGeomdlCurve(curve)