def generate_boundary(self): super().generate_boundary() self.boundary = Bspline(self.control_points, self.spline_degree, n=self.n, periodic=self.spline_periodic, **self.kwargs) self.surface_points = self.boundary.rvals self.surface_points[0] = self.control_points[0] self.surface_points[-1] = self.control_points[-1] self.surface_lines = self._create_line_list(self.surface_points)
def test_bspline_first_order(self): # When spline = Bspline(cv=self.cv, degree=1) # Then self.assertEqual(len(spline.rvals), 5) self.assertListEqual(spline.rvals, self.cv)
class TestBspline(unittest.TestCase): def setUp(self): self.cv = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [2.0, 1.0], [2.0, 2.0]] self.bspline = Bspline(self.cv, n=120) def test_edge_points(self): rvals = np.array(self.bspline.rvals) control_points = self.bspline.cv self.assertTrue( np.linalg.norm(rvals[0] - control_points[0]) < _TOLERANCE) self.assertTrue( np.linalg.norm(rvals[-1] - control_points[-1]) < _TOLERANCE) normal_vector_left = self.bspline.normal(control_points[0]) normal_vector_left_expected = np.array([-1.0, 0.0]) normal_vector_right = self.bspline.normal(control_points[0]) normal_vector_right_expected = np.array([-1.0, 0.0]) self.assertTrue( np.linalg.norm(normal_vector_left - normal_vector_left_expected) < _TOLERANCE) self.assertTrue( (np.linalg.norm(normal_vector_right - normal_vector_right_expected) < _TOLERANCE)) def test_get_t_from_point(self): self.assertTrue( abs( self.bspline.get_t_from_point(self.bspline.cv[0]) - self.bspline.kv[0]) < _TOLERANCE) self.assertTrue( abs( self.bspline.get_t_from_point(self.bspline.cv[2]) - self.bspline.kv[-1] / 2.0) < _TOLERANCE) self.assertTrue( abs( self.bspline.get_t_from_point(self.bspline.cv[-1]) - self.bspline.kv[-1]) < _TOLERANCE) def test_bspline_first_order(self): # When spline = Bspline(cv=self.cv, degree=1) # Then self.assertEqual(len(spline.rvals), 5) self.assertListEqual(spline.rvals, self.cv)
class BSplineElement(BoundaryElement): def __init__(self, control_points, bcond=None, el_size=0.05, **kwargs): super().__init__(control_points, bcond=bcond, el_size=el_size, **kwargs) self.spline_degree = kwargs.pop("degree", 3) self.spline_periodic = kwargs.pop("periodic", False) self.kwargs = kwargs # Do not specify extra points for lines: control points themselves # are enough if self.spline_degree == 1: self.n = len(self.control_points) - 1 else: self.n = self.estimate_points_number(self.control_points, self.el_size) self.control_points = control_points def generate_boundary(self): super().generate_boundary() self.boundary = Bspline(self.control_points, self.spline_degree, n=self.n, periodic=self.spline_periodic, **self.kwargs) self.surface_points = self.boundary.rvals self.surface_points[0] = self.control_points[0] self.surface_points[-1] = self.control_points[-1] self.surface_lines = self._create_line_list(self.surface_points) def get_normal(self, point): return self.boundary.normal(point) def get_curvature(self, point): return self.boundary.curvature(point) def get_displacement(self, point, cp_index): return self.boundary.displacement(point, cp_index)
def default_bspline(): cv = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [2.0, 1.0], [2.0, 2.0]] bspline = Bspline(cv, n=TEST_N) return bspline
def setUp(self): self.cv = [[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [2.0, 1.0], [2.0, 2.0]] self.bspline = Bspline(self.cv, n=120)
def plot_periodic_bspline(): control_points = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.5, 1.5], [0.0, 1.0]] periodic_bspline = Bspline(control_points, periodic=True) periodic_bspline.plot()
def bspline(): bspline = Bspline(control_points, degree=degree) bspline.tolerance = 1.0e-5 return bspline