class CubicSplineTests(SverchokTestCase): def setUp(self): super().setUp() vertices = [(-1, -1, 0), (0, 0, 0), (1, 2, 0), (2, 3, 0)] self.spline = CubicSpline(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) #info(result) expected_result = np.array([[-1.0, -1.0, 0.0], [-0.60984526, -0.66497986, 0.0], [0.29660356, 0.5303721, 0.0], [0.5, 1.0, 0.0], [0.94256655, 1.91347161, 0.0], [2.0, 3.0, 0.0]]) 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([[0.00789736, 0.00663246, 0.0], [0.00761454, 0.0068363, 0.0], [0.00430643, 0.00922065, 0.0], [0.0039487, 0.0094785, 0.0], [0.00537964, 0.00844713, 0.0], [0.00789736, 0.00663246, 0.0]]) self.assert_numpy_arrays_equal(result, expected_result, precision=8)
def evaluate_cubic(self, ts): xs = self.ts ys = self.summands zs = np.zeros_like(xs, dtype=np.float64) verts = np.stack((xs, ys, zs)).T spline = CubicSpline(verts, tknots=xs, is_cyclic=False) return spline.eval(ts)[:, 1]
class CubicSplineTests(SverchokTestCase): def setUp(self): super().setUp() vertices = [(-1, -1, 0), (0, 0, 0), (1, 2, 0), (2, 3, 0)] self.spline = CubicSpline(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) #info(result) expected_result = np.array( [[-1.0, -1.0, 0.0 ], [-0.60984526, -0.66497986, 0.0 ], [ 0.29660356, 0.5303721, 0.0 ], [ 0.5, 1.0, 0.0 ], [ 0.94256655, 1.91347161, 0.0 ], [ 2.0, 3.0, 0.0 ]]) 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( [[ 0.00789736, 0.00663246, 0.0 ], [ 0.00761454, 0.0068363, 0.0 ], [ 0.00430643, 0.00922065, 0.0 ], [ 0.0039487, 0.0094785, 0.0 ], [ 0.00537964, 0.00844713, 0.0 ], [ 0.00789736, 0.00663246, 0.0 ]]) self.assert_numpy_arrays_equal(result, expected_result, precision=8)
class CubicSplineTests(SverchokTestCase): def setUp(self): super().setUp() vertices = [(-1, -1, 0), (0, 0, 0), (1, 2, 0), (2, 3, 0)] self.spline = CubicSpline(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) #info(result) expected_result = np.array([[-1.0, -1.0, 0.0], [-0.60984526, -0.66497986, 0.0], [0.29660356, 0.5303721, 0.0], [0.5, 1.0, 0.0], [0.94256655, 1.91347161, 0.0], [2.0, 3.0, 0.0]]) 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) expected_result = np.array([[7.89735717, 6.63246233, 0.], [7.61454151, 6.83630432, 0.], [4.30643188, 9.22065484, 0.], [3.94869522, 9.47849683, 0.], [5.37964186, 8.44712885, 0.], [7.89735717, 6.63246233, 0.]]) self.assert_numpy_arrays_equal(result, expected_result, precision=8)
class CubicSplineTests(SverchokTestCase): 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 = CubicSpline(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) #info(result) expected_result = np.array([[-1.0, -1.0, 0.0], [-0.60984526, -0.66497986, 0.0], [0.29660356, 0.5303721, 0.0], [0.5, 1.0, 0.0], [0.94256655, 1.91347161, 0.0], [2.0, 3.0, 0.0]]) 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) expected_result = np.array([[7.89735717, 6.63246233, 0.], [7.61454151, 6.83630432, 0.], [4.30643188, 9.22065484, 0.], [3.94869522, 9.47849683, 0.], [5.37964186, 8.44712885, 0.], [7.89735717, 6.63246233, 0.]]) self.assert_numpy_arrays_equal(result, expected_result, precision=8) def test_control_points_1(self): #index = np.array([0,1,2]) points = self.spline.get_control_points() self.assertEquals(points.shape, (3, 4, 3)) #print(points) for i in range(3): with self.subTest(segmentNum=i): p0 = points[i][0] p3 = points[i][3] expected_p0 = self.control_points[i] expected_p3 = self.control_points[i + 1] self.assert_numpy_arrays_equal(p0, expected_p0, precision=8) self.assert_numpy_arrays_equal(p3, expected_p3, precision=8) bezier = SvBezierCurve([points[i][k] for k in range(4)]) t_min_spline = self.spline.tknots[i] t_max_spline = self.spline.tknots[i + 1] #print(f"Spline #{i}: {t_min_spline}, {t_max_spline}") t_spline = np.linspace(t_min_spline, t_max_spline, num=10) t_bezier = np.linspace(0, 1, num=10) pts_spline = self.spline.eval(t_spline) pts_bezier = bezier.evaluate_array(t_bezier) self.assert_numpy_arrays_equal(pts_bezier, pts_spline, precision=6)
def resample_1D_array(profile, samples, cyclic): ''' Resample 1D array ''' N = len(profile) v = [[n / (N - 1), p, 0] for n, p in enumerate(profile)] samples_array = np.array(samples).clip(0, 1) spline = CubicSpline(v, metric="POINTS", is_cyclic=cyclic) out = spline.eval(samples_array) verts = out.tolist() resampled_profile = [v[1] for v in verts] return resampled_profile
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 from_points(cls, points, metric=None, is_cyclic=False): if not points or len(points) < 2: raise Exception("At least two points are required") if len(points) < 3: return SvLine.from_two_points(points[0], points[1]) spline = CubicSpline(points, metric=metric, is_cyclic=is_cyclic) return SvSplineCurve(spline)
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 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 _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 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 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 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 setUp(self): super().setUp() vertices = [(-1, -1, 0), (0, 0, 0), (1, 2, 0), (2, 3, 0)] self.spline = CubicSpline(vertices, metric="DISTANCE")
def build_spline(self, path): spline = CubicSpline(path, metric=self.metric, is_cyclic=self.is_cyclic) return spline
def make(vertices): spline = CubicSpline(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 = CubicSpline(vertices, metric="DISTANCE")