Skip to content

Commit 4542952

Browse files
committed
Add CurveReversed class and enhance CurveExtended and CurveRepametrized bindings in Python
1 parent 51e8ea8 commit 4542952

File tree

5 files changed

+143
-3
lines changed

5 files changed

+143
-3
lines changed

gbs/curveReversed.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#pragma once
2+
#include <memory>
3+
#include <gbs/bscurve.h>
4+
namespace gbs {
5+
/**
6+
* @brief A curve that reverses the parameterization of an underlying curve.
7+
*
8+
* This class is useful for creating curves that need to be evaluated in reverse,
9+
* such as when dealing with control points that are defined in reverse order.
10+
*/
11+
template <typename T, size_t dim> class CurveReversed : public Curve<T, dim>
12+
{
13+
private:
14+
/**
15+
* @brief The underlying curve to be extended.
16+
*/
17+
std::shared_ptr<Curve<T, dim>> m_curve;
18+
19+
public:
20+
/**
21+
* @brief Construct a new CurveReversed object.
22+
*
23+
* @param crv Shared pointer to the underlying curve.
24+
*/
25+
CurveReversed(std::shared_ptr<Curve<T, dim>> crv)
26+
: m_curve(std::move(crv))
27+
{
28+
}
29+
30+
/**
31+
* @brief Evaluate the curve (and optionally its derivatives) at the parameter \p u.
32+
*
33+
* This method reverses the parameterization of the underlying curve.
34+
*
35+
* @param u The parameter value at which to evaluate the curve.
36+
* @param d The order of derivative to compute (0 for value, 1 for first derivative).
37+
* @return std::array<T, dim> The evaluated value or derivative at \p u.
38+
*/
39+
virtual auto value(T u, size_t d = 0) const -> std::array<T, dim> override
40+
{
41+
auto u1 = m_curve->bounds()[0];
42+
auto u2 = m_curve->bounds()[1];
43+
44+
u = u2 - (u - u1); // Reverse the parameterization
45+
T sign = (d % 2 == 0) ? 1 : -1;
46+
47+
return sign * m_curve->value(u, d);
48+
}
49+
50+
virtual auto bounds() const -> std::array<T, 2> override
51+
{
52+
return m_curve->bounds();
53+
}
54+
55+
const auto & basisCurve() const
56+
{
57+
return m_curve;
58+
}
59+
};
60+
} // namespace gbs

gbs/curveextended.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ class CurveExtended : public Curve<T, dim>
156156
std::numeric_limits<T>::max()
157157
};
158158
}
159+
160+
const auto & basisCurve() const
161+
{
162+
return m_curve;
163+
}
164+
165+
bool isClamped() const
166+
{
167+
return m_clamped;
168+
}
159169
};
160170

161171
} // namespace gbs

gbs/curvereparametrized.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace gbs{
88
class CurveReparametrized : public Curve<T,dim>
99
{
1010
const std::shared_ptr<Curve<T,dim>> m_p_crv;
11-
const BSCfunction<T> m_f_u;
11+
BSCfunction<T> m_f_u;
1212
public:
1313
CurveReparametrized(const std::shared_ptr<Curve<T,dim>> &crv, const BSCfunction<T> &f_u) : m_p_crv{crv}, m_f_u{f_u} {}
1414
/**

gbs/curves

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
#include <gbs/curveonsurface.h>
88
#include <gbs/curvetrimmed.h>
99
#include <gbs/curvecomposite.h>
10-
#include <gbs/curveextended.h>
10+
#include <gbs/curveextended.h>
11+
#include <gbs/curvereparametrized.h>
12+
#include <gbs/curveReversed.h>

python/gbsBindCurves.h

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,24 @@ inline void gbs_bind_curves(py::module &m)
259259
, py::arg("crv")
260260
, py::arg("clamped") = false
261261
)
262-
;
262+
.def("basisCurve", &gbs::CurveExtended<T, dim>::basisCurve, "Get the basis curve")
263+
.def("isClamped", &gbs::CurveExtended<T, dim>::isClamped, "Check if the curve is clamped")
264+
.def(py::pickle(
265+
[](CurveExtended<T, dim> &crv) {
266+
return py::make_tuple(// __getstate__
267+
crv.basisCurve(),
268+
crv.isClamped()
269+
);
270+
},
271+
[](py::tuple t) { // __setstate__
272+
if (t.size() != 2)
273+
throw std::runtime_error("Invalid state!");
274+
return CurveExtended<T, dim>{
275+
t[0].cast<std::shared_ptr<gbs::Curve<T, dim>>>(),
276+
t[1].cast<bool>()
277+
};
278+
}
279+
));
263280

264281

265282
py::class_<
@@ -268,4 +285,55 @@ inline void gbs_bind_curves(py::module &m)
268285
gbs::Curve<T, dim>
269286
>(m, add_ext<dim>("CurveOffset_func_base").c_str() );
270287

288+
289+
py::class_<
290+
gbs::CurveReversed<T, dim>,
291+
std::shared_ptr<gbs::CurveReversed<T, dim>>,
292+
gbs::Curve<T, dim>
293+
>(m, add_ext<dim>("CurveReversed").c_str())
294+
.def(py::init<std::shared_ptr<gbs::Curve<T, dim>>>(), py::arg("crv"))
295+
.def("basisCurve",&gbs::CurveReversed<T, dim>::basisCurve)
296+
.def(py::pickle(
297+
[](CurveReversed<T, dim> &crv) {
298+
return py::make_tuple(// __getstate__
299+
crv.basisCurve()
300+
);
301+
},
302+
[](py::tuple t) { // __setstate__
303+
if (t.size() != 1)
304+
throw std::runtime_error("Invalid state!");
305+
return CurveReversed<T, dim>{
306+
t[0].cast<std::shared_ptr<gbs::Curve<T, dim>>>()
307+
};
308+
}
309+
))
310+
;
311+
312+
py::class_<
313+
gbs::CurveReparametrized<T, dim>,
314+
std::shared_ptr<gbs::CurveReparametrized<T, dim>>,
315+
gbs::Curve<T, dim>
316+
>(m, add_ext<dim>("CurveReparametrized").c_str())
317+
.def(py::init<const std::shared_ptr<gbs::Curve<T, dim>> &, const BSCfunction<T> &>(),
318+
py::arg("crv"), py::arg("f_u"))
319+
.def("basisCurve",&gbs::CurveReparametrized<T, dim>::basisCurve)
320+
.def("paramFunction",&gbs::CurveReparametrized<T, dim>::paramFunction)
321+
.def("setParamFunction",&gbs::CurveReparametrized<T, dim>::setParamFunction, py::arg("f_u"))
322+
.def(py::pickle(
323+
[](CurveReparametrized<T, dim> &crv) {
324+
return py::make_tuple(// __getstate__
325+
crv.basisCurve(),
326+
crv.paramFunction()
327+
);
328+
},
329+
[](py::tuple t) { // __setstate__
330+
if (t.size() != 2)
331+
throw std::runtime_error("Invalid state!");
332+
return CurveReparametrized<T, dim>{
333+
t[0].cast<std::shared_ptr<gbs::Curve<T, dim>>>(),
334+
t[1].cast<BSCfunction<T>>()
335+
};
336+
}
337+
))
338+
;
271339
}

0 commit comments

Comments
 (0)