Skip to content

Commit

Permalink
Change interpolated curves' parameters to 0...1 instead of indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzBangar committed Nov 7, 2023
1 parent f6d6fbb commit 2057bd2
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 45 deletions.
7 changes: 4 additions & 3 deletions examples/complex/airfoil.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def get_curve(z: float) -> cb.SplineInterpolatedCurve:
# points 9...15
points[i + 9] = point

points[12] = foil_curve.get_point(foil_curve.get_closest_param(points[0]))

points[16] = np.average(np.take(points, (9, 4, 3, 2), axis=0), axis=0)
points[17] = np.average(np.take(points, (15, 5, 6, 7), axis=0), axis=0)

Expand Down Expand Up @@ -164,7 +166,7 @@ def make_link(leader):


# Points that slide along airfoil curve
for index in (10, 11, 12, 13, 14):
for index in (10, 11, 13, 14):
opt_vertex = find_vertex(index)
clamp = cb.CurveClamp(opt_vertex, foil_curve)
clamp.add_link(make_link(opt_vertex))
Expand Down Expand Up @@ -192,9 +194,8 @@ def optimize_along_line(point_index, line_index_1, line_index_2):
optimize_along_line(4, 3, 6)
optimize_along_line(5, 3, 6)


if OPTIMIZE:
optimizer.optimize(tolerance=0.001)
optimizer.optimize(tolerance=0.01)

### Write the mesh
mesh.modify_patch("topAndBottom", "empty")
Expand Down
2 changes: 1 addition & 1 deletion src/classy_blocks/construct/curves/curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _get_params(self, param_from: Optional[float] = None, param_to: Optional[flo
self._check_param(param_to)

if param_from == param_to:
raise ValueError("Provide two different parameters for discretization " "(or use get_point())")
raise ValueError("Provide two different parameters for discretization (or use get_point())")

return param_from, param_to

Expand Down
26 changes: 20 additions & 6 deletions src/classy_blocks/construct/curves/interpolated.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ class InterpolatedCurveBase(FunctionCurveBase, abc.ABC):

_interpolator: Type[InterpolatorBase]

def __init__(self, points: PointListType, extrapolate: bool = True):
def __init__(self, points: PointListType):
self.points = self._check_points(points)
self.function = self._interpolator(self.points, extrapolate)
self.bounds = (0, len(self.points) - 1)
self.function = self._interpolator(self.points, False)
self.bounds = (0, 1)

@property
def segments(self) -> int:
"""Returns number of points this curve was created from"""
return len(self.points) - 1

@property
def parts(self):
Expand All @@ -46,10 +51,19 @@ def get_length(self, param_from: Optional[float] = None, param_to: Optional[floa
points. The 'count' parameter is ignored as the original points are taken."""
# TODO: use the same function as items.edges.spline
param_from, param_to = self._get_params(param_from, param_to)
index_from = int(param_from) + 1
index_to = int(param_to) - 1

params = [param_from, *list(range(index_from, index_to + 1)), param_to]
index_from = int(param_from * self.segments) + 1
index_to = int(param_to * self.segments)

if index_from < index_to:
indexes = list(range(index_from, index_to + 1))
else:
indexes = []

params = [param_from, *[i / self.segments for i in indexes], param_to]

print(params)

discretized = np.array([self.function(t) for t in params])
return np.sum(np.sqrt(np.sum((discretized[:-1] - discretized[1:]) ** 2, axis=1)))

Expand Down
2 changes: 1 addition & 1 deletion src/classy_blocks/construct/curves/interpolators.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def invalidate(self) -> None:

@property
def params(self) -> NDArray:
return np.array(list(range(len(self.points))))
return np.linspace(0, 1, num=len(self.points))

@property
def positions(self) -> NPPointListType:
Expand Down
56 changes: 28 additions & 28 deletions tests/test_construct/test_curves/test_interpolated.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,40 @@ def setUp(self):

@property
def curve(self) -> LinearInterpolatedCurve:
return LinearInterpolatedCurve(self.points, True)
return LinearInterpolatedCurve(self.points)

def test_discretize(self):
np.testing.assert_array_equal(self.curve.discretize(count=len(self.points)), self.points)

@parameterized.expand(
(
(0, 0.25),
(0, 0.5),
(0, 0.75),
(0.5, 1),
(0.75, 1),
(0, 1),
(0, 2),
(0, 3),
(2, 4),
(3, 4),
(0, 4),
)
)
def test_get_length(self, param_from, param_to):
length = param_to - param_from
length = (param_to - param_from) * 4
self.assertAlmostEqual(self.curve.get_length(param_from, param_to), length)

def test_length(self):
self.assertEqual(self.curve.length, 4)

def test_get_point(self):
np.testing.assert_array_equal(self.curve.get_point(0.5), [0, 0.5, 0])
np.testing.assert_array_equal(self.curve.get_point(0.125), [0, 0.5, 0])

@parameterized.expand(
(
# the easy ones
([-1, 3, 0], 1),
([0.2, 2, 0], 1.2),
([0.8, 2, 0], 1.8),
([1.1, -0.5, 0], 3.1),
([-1, 3, 0], 1 / 4),
([0.2, 2, 0], 1.2 / 4),
([0.8, 2, 0], 1.8 / 4),
([1.1, -0.5, 0], 3.1 / 4),
# more difficult samples
([1.1, 0.1, 0], 3.1),
([1.1, 0.1, 0], 3.1 / 4),
)
)
def test_get_closest_param(self, point, param):
Expand Down Expand Up @@ -81,7 +81,7 @@ def setUp(self):

@property
def curve(self) -> SplineInterpolatedCurve:
return SplineInterpolatedCurve(self.points, True)
return SplineInterpolatedCurve(self.points)

def test_length(self):
# spline must be longer than linear segments combined
Expand All @@ -91,37 +91,37 @@ def test_length(self):
@parameterized.expand(
(
(0,),
(0.25,),
(0.5,),
(0.75,),
(1,),
(2,),
(3,),
(4,),
)
)
def test_through_points(self, param):
"""Make sure the curve goes through original points"""
# param equals to index
np.testing.assert_almost_equal(self.curve.get_point(param), self.points[param])
index = int(4 * param)
np.testing.assert_almost_equal(self.curve.get_point(param), self.points[index])

def test_transform(self):
curve = self.curve

curve.translate([0, 0, 1])

np.testing.assert_equal(curve.get_point(1), [1, 1, 1])
np.testing.assert_equal(curve.get_point(0), [0, 0, 1])

def test_center_warning(self):
with self.assertWarns(Warning):
_ = self.curve.center

@parameterized.expand(
(
(0,),
(0.5,),
(1,),
(1.5,),
(2,),
(2.5,),
(3,),
(0 / 3,),
(0.5 / 3,),
(1 / 3,),
(1.5 / 3,),
(2 / 3,),
(2.5 / 3,),
(3 / 3,),
)
)
def test_interpolation_values_line(self, param):
Expand All @@ -133,4 +133,4 @@ def test_interpolation_values_line(self, param):
]
curve = self.curve

np.testing.assert_almost_equal(curve.get_point(param), [param, param, 0])
np.testing.assert_almost_equal(curve.get_point(param), [3 * param, 3 * param, 0])
10 changes: 5 additions & 5 deletions tests/test_construct/test_curves/test_interpolators.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def setUp(self):
@parameterized.expand(
(
(0, [0, 0, 0]),
(1, [0, 1, 0]),
(2, [1, 1, 0]),
(0.5, [0, 0.5, 0]),
(1 / 4, [0, 1, 0]),
(2 / 4, [1, 1, 0]),
(0.5 / 4, [0, 0.5, 0]),
)
)
def test_points(self, param, result):
Expand All @@ -44,7 +44,7 @@ def test_cache(self):
point.rotate(np.pi / 2, [0, 0, 1])

# the interpolation function must not change unless invalidated
np.testing.assert_equal(intfun(1), [0, 1, 0])
np.testing.assert_equal(intfun(0.25), [0, 1, 0])

def test_invalidate(self):
intfun = LinearInterpolator(self.points, True)
Expand All @@ -54,4 +54,4 @@ def test_invalidate(self):

intfun.invalidate()

np.testing.assert_equal(intfun(1), [0, 1, 1])
np.testing.assert_equal(intfun(0.25), [0, 1, 1])
2 changes: 1 addition & 1 deletion tests/test_items/test_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def test_param_start(self):
self.assertAlmostEqual(self.edge.param_start, 0)

def test_param_end(self):
self.assertAlmostEqual(self.edge.param_end, 4) # index of the last point
self.assertAlmostEqual(self.edge.param_end, 1) # the last point

def test_point_array(self):
self.assertEqual(len(self.edge.point_array), self.edge.data.n_points)
Expand Down

0 comments on commit 2057bd2

Please sign in to comment.