class LinearSplineTests(SverchokTestCase): def setUp(self): super().setUp() vertices = [(-1, -1, 0), (0, 0, 0), (1, 2, 0), (2, 3, 0)] self.spline = LinearSpline(vertices, metric="DISTANCE") def test_eval(self): t_in = np.array([0.0, 0.1, 0.4, 0.5, 0.7, 1.0]) result = self.spline.eval(t_in) expected_result = np.array( [[-1.0, -1.0, 0.0 ], [-0.64188612, -0.64188612, 0.0 ], [ 0.27350889, 0.54701779, 0.0 ], [ 0.5, 1.0, 0.0 ], [ 0.95298221, 1.90596443, 0.0 ], [ 2.0, 3.0, 0.0 ]]) #info(result) self.assert_numpy_arrays_equal(result, expected_result, precision=8) def test_tangent(self): t_in = np.array([0.0, 0.1, 0.4, 0.5, 0.7, 1.0]) result = self.spline.tangent(t_in) #info(result) expected_result = np.array( [[-1, -1, 0], [-1, -1, 0], [-1, -2, 0], [-1, -2, 0], [-1, -2, 0], [-1, -1, 0]]) self.assert_numpy_arrays_equal(result, expected_result)
class LinearSplineTests(SverchokTestCase): def setUp(self): super().setUp() vertices = [(-1, -1, 0), (0, 0, 0), (1, 2, 0), (2, 3, 0)] self.spline = LinearSpline(vertices, metric="DISTANCE") def test_eval(self): t_in = np.array([0.0, 0.1, 0.4, 0.5, 0.7, 1.0]) result = self.spline.eval(t_in) expected_result = np.array([[-1.0, -1.0, 0.0], [-0.64188612, -0.64188612, 0.0], [0.27350889, 0.54701779, 0.0], [0.5, 1.0, 0.0], [0.95298221, 1.90596443, 0.0], [2.0, 3.0, 0.0]]) #info(result) self.assert_numpy_arrays_equal(result, expected_result, precision=8) def test_tangent(self): t_in = np.array([0.0, 0.1, 0.4, 0.5, 0.7, 1.0]) result = self.spline.tangent(t_in) #info(result) expected_result = np.array([[-1, -1, 0], [-1, -1, 0], [-1, -2, 0], [-1, -2, 0], [-1, -2, 0], [-1, -1, 0]]) self.assert_numpy_arrays_equal(result, expected_result)
def build_spline(self, path, mode, is_cyclic, metric=None): if metric is None: metric = self.metric if mode == 'LIN': spline = LinearSpline(path, metric = metric, is_cyclic = is_cyclic) else: # SPL spline = CubicSpline(path, metric = metric, is_cyclic = is_cyclic) return spline
def _make_spline(self, mode, tknots): zeros = np.zeros(len(tknots)) control_points = np.vstack((self._length_params, tknots, zeros)).T if mode == 'LIN': spline = LinearSpline(control_points, tknots = self._length_params, is_cyclic = False) elif mode == 'SPL': spline = CubicSpline(control_points, tknots = self._length_params, is_cyclic = False) else: raise Exception("Unsupported mode; supported are LIN and SPL.") return spline
def build_spline(self, path): if self.mode == 'LIN': spline = LinearSpline(path, metric=self.metric, is_cyclic=self.is_cyclic) else: # SPL spline = CubicSpline(path, metric=self.metric, is_cyclic=self.is_cyclic) return spline
def process(self): if not any((s.is_linked for s in self.outputs)): return calc_tanget = self.outputs['Tanget'].is_linked or self.outputs['Unit Tanget'].is_linked norm_tanget = self.outputs['Unit Tanget'].is_linked h = self.h if self.inputs['Vertices'].is_linked: verts = self.inputs['Vertices'].sv_get() verts = dataCorrect(verts) t_ins = self.inputs['Interval'].sv_get() if self.infer_from_integer_input: t_ins = [make_range(int(value)) for value in t_ins[0]] if len(t_ins) > len(verts): new_verts = verts[:] for i in range(len(t_ins) - len(verts)): new_verts.append(verts[-1]) verts = new_verts verts_out = [] tanget_out = [] norm_tanget_out = [] for v, t_in in zip(verts, repeat_last(t_ins)): t_corr = np.array(t_in).clip(0, 1) if self.mode == 'LIN': spline = LinearSpline(v, metric = self.knot_mode, is_cyclic = self.is_cyclic) out = spline.eval(t_corr) verts_out.append(out.tolist()) if calc_tanget: tanget_out.append(spline.tangent(t_corr).tolist()) else: # SPL spline = CubicSpline(v, metric = self.knot_mode, is_cyclic = self.is_cyclic) out = spline.eval(t_corr) verts_out.append(out.tolist()) if calc_tanget: tangent = spline.tangent(t_corr, h) if norm_tanget: norm = np.linalg.norm(tangent, axis=1) norm_tanget_out.append((tangent / norm[:, np.newaxis]).tolist()) tanget_out.append(tangent.tolist()) outputs = self.outputs if outputs['Vertices'].is_linked: outputs['Vertices'].sv_set(verts_out) if outputs['Tanget'].is_linked: outputs['Tanget'].sv_set(tanget_out) if outputs['Unit Tanget'].is_linked: outputs['Unit Tanget'].sv_set(norm_tanget_out)
def interpolate(self, verts, n, is_cyclic): if len(verts) < 3: edges = [[i,i+1] for i in range(len(verts)-1)] return verts, edges if self.spline_mode == 'SPL': spline = CubicSpline(verts, metric='DISTANCE', is_cyclic = is_cyclic) else: spline = LinearSpline(verts, metric='DISTANCE', is_cyclic = is_cyclic) ts = numpy.linspace(0, 1, n) verts = [tuple(v) for v in spline.eval(ts).tolist()] edges = [[i,i+1] for i in range(len(verts)-1)] return verts, edges
def make_taper_spline(self, vertices): if len(vertices) == 0: # if no taper object provided: use constant scale of 1.0 def make_unit(z): u = Vector((0,0,0)) u[self.orient_axis_idx] = z u[(self.orient_axis_idx+1) % 3] = 1 return u vertices = [make_unit(0), make_unit(1)] return LinearSpline(vertices, metric = self.metric, is_cyclic = False) return self.build_spline(vertices, self.taper_mode, False)
def make_spline(self, control_points): xs = [p[0] for p in control_points] min_x = xs[0] max_x = xs[-1] control_points = np.array(control_points) xs = np.array(xs) if self.mode == 'SPL': spline = CubicSpline(control_points, tknots=xs, is_cyclic=False) else: spline = LinearSpline(control_points, tknots=xs, is_cyclic=False) return spline
def make_twist_spline(self, data): if data is None or len(data) == 0: # if no twist object provided, use constant twist of 0.0 vertices = [Vector((0,0,0)), Vector((0,0,1))] return LinearSpline(vertices, metric = self.metric, is_cyclic = self.is_cyclic) elif type(data[0]) in (list, tuple) and len(data[0]) == 2: vertices = [Vector((twist, 0, t)) for t, twist in data] return self.build_spline(vertices, self.twist_mode, is_cyclic=self.is_cyclic, metric = self.metric) else: n = len(data) ts = [i / (n-1) for i in range(n)] vertices = [Vector((twist, 0, t)) for t, twist in zip(ts, data)] return self.build_spline(vertices, self.twist_mode, is_cyclic=self.is_cyclic, metric = self.metric)
def process_data(self, params): verts, t_ins = params calc_tanget = self.outputs['Tanget'].is_linked or self.outputs[ 'Unit Tanget'].is_linked norm_tanget = self.outputs['Unit Tanget'].is_linked h = self.h verts_out, tanget_out, norm_tanget_out = [], [], [] for v, t_in in zip(verts, t_ins): if self.infer_from_integer_input: t_corr = make_range(int(t_in), self.end_point) else: t_corr = np.array(t_in).clip(0, 1) if self.mode == 'LIN': spline = LinearSpline(v, metric=self.knot_mode, is_cyclic=self.is_cyclic) out = spline.eval(t_corr) verts_out.append(out if self.output_numpy else out.tolist()) if calc_tanget: tanget_out.append( spline.tangent(t_corr) if self. output_numpy else spline.tangent(t_corr).tolist()) else: # SPL spline = CubicSpline(v, metric=self.knot_mode, is_cyclic=self.is_cyclic) out = spline.eval(t_corr) verts_out.append(out if self.output_numpy else out.tolist()) if calc_tanget: tangent = spline.tangent(t_corr, h) if norm_tanget: norm = np.linalg.norm(tangent, axis=1) tangent_norm = tangent / norm[:, np.newaxis] norm_tanget_out.append( tangent_norm if self. output_numpy else tangent_norm.tolist()) tanget_out.append( tangent if self.output_numpy else tangent.tolist()) return verts_out, tanget_out, norm_tanget_out
def process(self): if not any(o.is_linked for o in self.outputs): return vertices_s = self.inputs['Vertices'].sv_get(default=[[]]) out_curves = [] for vertices in vertices_s: if self.concat: spline = LinearSpline(vertices, metric=self.metric, is_cyclic=self.is_cyclic) curve = SvSplineCurve(spline) out_curves.append(curve) else: curves = self.make_edges(vertices, is_cyclic=self.is_cyclic) out_curves.append(curves) self.outputs['Curve'].sv_set(out_curves)
def process(self): if 'Unit Tanget' not in self.outputs: return if not any((s.is_linked for s in self.outputs)): return calc_tanget = self.outputs['Tanget'].is_linked or self.outputs['Unit Tanget'].is_linked norm_tanget = self.outputs['Unit Tanget'].is_linked h = self.h if self.inputs['Vertices'].is_linked: verts = self.inputs['Vertices'].sv_get() verts = dataCorrect(verts) t_ins = self.inputs['Interval'].sv_get() verts_out = [] tanget_out = [] norm_tanget_out = [] for v, t_in in zip(verts, repeat_last(t_ins)): t_corr = np.array(t_in).clip(0, 1) if self.mode == 'LIN': spline = LinearSpline(v, metric = self.knot_mode, is_cyclic = self.is_cyclic) out = spline.eval(t_corr) verts_out.append(out.tolist()) if calc_tanget: tanget_out.append(spline.tangent(t_corr).tolist()) else: # SPL spline = CubicSpline(v, metric = self.knot_mode, is_cyclic = self.is_cyclic) out = spline.eval(t_corr) verts_out.append(out.tolist()) if calc_tanget: tangent = spline.tangent(t_corr, h) if norm_tanget: norm = np.linalg.norm(tangent, axis=1) norm_tanget_out.append((tangent / norm[:, np.newaxis]).tolist()) tanget_out.append(tangent.tolist()) outputs = self.outputs if outputs['Vertices'].is_linked: outputs['Vertices'].sv_set(verts_out) if outputs['Tanget'].is_linked: outputs['Tanget'].sv_set(tanget_out) if outputs['Unit Tanget'].is_linked: outputs['Unit Tanget'].sv_set(norm_tanget_out)
def make_curve(self, control_points): curves = [] for series in control_points: xs = [p.x for p in series] min_x = xs[0] max_x = xs[-1] series = np.array([p.to_tuple() for p in series]) xs = np.array(xs) if self.mode == 'SPL': spline = CubicSpline(series, tknots=xs, is_cyclic=False) else: spline = LinearSpline(series, tknots=xs, is_cyclic=False) curve = SvSplineCurve(spline) curve.u_bounds = (series[0][0], series[-1][0]) curves.append(curve) if len(curves) == 1: return curves[0] else: return SvConcatCurve(curves)
def build_spline(self, path): spline = LinearSpline(path, metric=self.metric, is_cyclic=self.is_cyclic) return spline
def setUp(self): super().setUp() vertices = [(-1, -1, 0), (0, 0, 0), (1, 2, 0), (2, 3, 0)] self.spline = LinearSpline(vertices, metric="DISTANCE")
def make(vertices): spline = LinearSpline(vertices, metric='DISTANCE', is_cyclic=self.is_cyclic) return SvSplineCurve(spline)
def setUp(self): super().setUp() vertices = [(-1, -1, 0), (0, 0, 0), (1, 2, 0), (2, 3, 0)] self.control_points = np.array(vertices) self.spline = LinearSpline(vertices, metric="DISTANCE")