fitpack
Modern Fortran library for curve and surface fitting with splines
Loading...
Searching...
No Matches
fpClosedCurve.hpp
Go to the documentation of this file.
1/***************************************************************************************************
2! ____________________ ___ ________ __
3! / ____/ _/_ __/ __ \/ | / ____/ //_/
4! / /_ / / / / / /_/ / /| |/ / / ,<
5! / __/ _/ / / / / ____/ ___ / /___/ /| |
6! /_/ /___/ /_/ /_/ /_/ |_\____/_/ |_|
7!
8! A Curve Fitting Package
9!
10! fpClosedCurve.hpp (class fpClosedCurve)
11!> @brief Standalone C++ wrapper for fitpack_closed_curve (no fortran-arrays dependency)
12!
13! @author Federico Perini
14! @date 2026-08-27
15!
16! References :
17! - C. De Boor, "On calculating with b-splines", J Approx Theory 6 (1972) 50-62
18! - M. G. Cox, "The numerical evaluation of b-splines", J Inst Maths Applics 10 (1972) 134-149
19! - P. Dierckx, "Curve and surface fitting with splines", Monographs on numerical analysis,
20! Oxford university press, 1993.
21!
22! **************************************************************************************************/
23
24#ifndef FPCLOSEDCURVE_HPP_INCLUDED
25#define FPCLOSEDCURVE_HPP_INCLUDED
26
27#include "fitpack_config.h"
28
29#if HAVE_FXARRAY
30#include "fxArrays.hpp"
31#endif
32
33#include "fitpack_closed_curve_c.h"
34#include "fpParametricCurve.hpp"
35#include <string>
36#include <vector>
37#include <cstdint>
38#include <memory>
39#include <stdexcept>
40#include <variant>
41#include <optional>
42
43static_assert(sizeof(fitpack_closed_curve_c) == sizeof(fitpack_parametric_curve_c),
44 "C descriptor layout mismatch: fitpack_closed_curve_c vs fitpack_parametric_curve_c");
45
55public:
56 // ===========================================================================================
57 // Constructors and Destructor
58 // ===========================================================================================
59
64 fitpack_closed_curve_c_allocate(as<fitpack_closed_curve_c>(), nullptr);
65 }
66
70 ~fpClosedCurve() override {
71 fitpack_closed_curve_c_destroy(as<fitpack_closed_curve_c>(), nullptr);
72 }
73
78 *as<fitpack_closed_curve_c>() = fitpack_closed_curve_c_null;
79 fitpack_closed_curve_c_copy(as<fitpack_closed_curve_c>(), other.as<fitpack_closed_curve_c>(), false, nullptr);
80 }
81
86 if (this != &other) {
87 fitpack_closed_curve_c_destroy(as<fitpack_closed_curve_c>(), nullptr);
88 fitpack_closed_curve_c_copy(as<fitpack_closed_curve_c>(), other.as<fitpack_closed_curve_c>(), false, nullptr);
89 }
90 return *this;
91 }
92
97 *as<fitpack_closed_curve_c>() = fitpack_closed_curve_c_null;
98 fitpack_closed_curve_c_move_alloc(as<fitpack_closed_curve_c>(), other.as<fitpack_closed_curve_c>(), nullptr);
99 }
100
105 if (this != &other) {
106 fitpack_closed_curve_c_destroy(as<fitpack_closed_curve_c>(), nullptr);
107 fitpack_closed_curve_c_move_alloc(as<fitpack_closed_curve_c>(), other.as<fitpack_closed_curve_c>(), nullptr);
108 }
109 return *this;
110 }
111
115 explicit fpClosedCurve(fitpack_closed_curve_c& c_wrapper, bool move = false) : fpParametricCurve(NoAlloc{}) {
116 if (move) {
117 fitpack_closed_curve_c_move_alloc(as<fitpack_closed_curve_c>(), &c_wrapper, nullptr);
118 } else {
119 *as<fitpack_closed_curve_c>() = fitpack_closed_curve_c_null;
120 fitpack_closed_curve_c_copy(as<fitpack_closed_curve_c>(), &c_wrapper, false, nullptr);
121 }
122 }
123
129 explicit fpClosedCurve(fitpack_closed_curve_c& c_wrapper, ViewTag) : fpParametricCurve(NoAlloc{}) {
130 *as<fitpack_closed_curve_c>() = c_wrapper;
131 as<fitpack_closed_curve_c>()->is_pointer = true;
132 }
133
134 // ===========================================================================================
135 // Utility Methods
136 // ===========================================================================================
137
141 bool is_allocated() const override {
142 return as<fitpack_closed_curve_c>()->cptr != nullptr;
143 }
144
148 bool is_pointer() const override {
149 return as<fitpack_closed_curve_c>()->is_pointer;
150 }
151
155 const char* c_type_name() const override {
156 return fitpack_closed_curve_c_c_type_name(*as<fitpack_closed_curve_c>());
157 }
158
163 const char* cpp_type_name() const override {
164 return "fpClosedCurve";
165 }
166
170 fitpack_closed_curve_c& c_handle() {
172 }
173
177 const fitpack_closed_curve_c& c_handle() const {
179 }
180
190 return fpClosedCurve(NoAlloc{});
191 }
192
197 return static_cast<fpParametricCurve&>(*this);
198 }
200 return static_cast<const fpParametricCurve&>(*this);
201 }
202
203 // ===========================================================================================
204 // Method Wrappers (standalone — no fxArray dependency)
205 // ===========================================================================================
206
210 void new_points(int32_t x_n1, int32_t x_n2, std::vector<double>& x, double* u = nullptr, double* w = nullptr) override {
211 fitpack_closed_curve_c_new_points(as<fitpack_closed_curve_c>(), x_n1, x_n2, x.data(), u, w);
212 }
213
214
215 void set_default_parameters() override {
216 fitpack_closed_curve_c_set_default_parameters(as<fitpack_closed_curve_c>());
217 }
218
222 int32_t new_fit(int32_t x_n1, int32_t x_n2, std::vector<double>& x, double* u = nullptr, double* w = nullptr, double* smoothing = nullptr, int32_t* order = nullptr) override {
223 return fitpack_closed_curve_c_new_fit(as<fitpack_closed_curve_c>(), x_n1, x_n2, x.data(), u, w, smoothing, order);
224 }
225
226
227 int32_t fit(double* smoothing = nullptr, int32_t* order = nullptr, bool* keep_knots = nullptr) override {
228 return fitpack_closed_curve_c_fit(as<fitpack_closed_curve_c>(), smoothing, order, keep_knots);
229 }
230
231 int32_t interpolate(int32_t* order = nullptr, bool* reset_knots = nullptr) override {
232 return fitpack_closed_curve_c_interpolate(as<fitpack_closed_curve_c>(), order, reset_knots);
233 }
234
235 int32_t least_squares(double* smoothing = nullptr, bool* reset_knots = nullptr) override {
236 return fitpack_closed_curve_c_least_squares(as<fitpack_closed_curve_c>(), smoothing, reset_knots);
237 }
238
242 std::vector<double> eval_one(double u, int32_t n_result, int32_t* ierr = nullptr) override {
243 std::vector<double> result(n_result);
244 fitpack_closed_curve_c_eval_one(as<fitpack_closed_curve_c>(), u, ierr, result.data(), n_result);
245 return result;
246 }
247
248
252 std::vector<double> eval_many(std::vector<double>& u, int32_t n_result, int32_t* ierr = nullptr) override {
253 int32_t n = static_cast<int32_t>(u.size());
254 std::vector<double> result(n_result);
255 fitpack_closed_curve_c_eval_many(as<fitpack_closed_curve_c>(), n, u.data(), ierr, result.data(), n_result);
256 return result;
257 }
258
259
263 std::vector<double> dfdx(double u, int32_t order, int32_t n_result, int32_t* ierr = nullptr) override {
264 std::vector<double> result(n_result);
265 fitpack_closed_curve_c_curve_derivative(as<fitpack_closed_curve_c>(), u, order, ierr, result.data(), n_result);
266 return result;
267 }
268
269
273 std::vector<double> dfdx(std::vector<double>& u, int32_t order, int32_t n_result, int32_t* ierr = nullptr) override {
274 int32_t n = static_cast<int32_t>(u.size());
275 std::vector<double> result(n_result);
276 fitpack_closed_curve_c_curve_derivatives(as<fitpack_closed_curve_c>(), n, u.data(), order, ierr, result.data(), n_result);
277 return result;
278 }
279
280
284 std::vector<double> dfdx_all(double u, int32_t n_result, int32_t* ierr = nullptr) override {
285 std::vector<double> result(n_result);
286 fitpack_closed_curve_c_curve_all_derivatives(as<fitpack_closed_curve_c>(), u, ierr, result.data(), n_result);
287 return result;
288 }
289
290
291 int32_t comm_size() const override {
292 return fitpack_closed_curve_c_comm_size(as<fitpack_closed_curve_c>());
293 }
294
298 void comm_pack(std::vector<double>& buffer) const override {
299 int32_t n = static_cast<int32_t>(buffer.size());
300 fitpack_closed_curve_c_comm_pack(as<fitpack_closed_curve_c>(), n, buffer.data());
301 }
302
303
307 void comm_expand(std::vector<double>& buffer) override {
308 int32_t n = static_cast<int32_t>(buffer.size());
309 fitpack_closed_curve_c_comm_expand(as<fitpack_closed_curve_c>(), n, buffer.data());
310 }
311
312
313 double mse() const override {
314 return fitpack_closed_curve_c_mse(as<fitpack_closed_curve_c>());
315 }
316
317 int32_t core_comm_size() const override {
318 return fitpack_closed_curve_c_core_comm_size(as<fitpack_closed_curve_c>());
319 }
320
324 void core_comm_pack(std::vector<double>& buffer) const override {
325 int32_t n = static_cast<int32_t>(buffer.size());
326 fitpack_closed_curve_c_core_comm_pack(as<fitpack_closed_curve_c>(), n, buffer.data());
327 }
328
329
333 void core_comm_expand(std::vector<double>& buffer) override {
334 int32_t n = static_cast<int32_t>(buffer.size());
335 fitpack_closed_curve_c_core_comm_expand(as<fitpack_closed_curve_c>(), n, buffer.data());
336 }
337
338
339 void destroy_base() override {
340 fitpack_closed_curve_c_destroy_base(as<fitpack_closed_curve_c>());
341 }
342
343 // ===========================================================================================
344 // extra_methods/fpParametricInherit.hpp (hand-maintained)
345 //
346 // This class overrides the raw new_fit / fit, which hides the fpPoint<dim> overloads
347 // spliced into fpParametricCurve. Re-expose them; the overrides above still win for their
348 // own signatures, and every fpPoint<dim> overload dispatches back through them.
349 // ===========================================================================================
350
353
354protected:
356
357};
358
359#endif /* FPCLOSEDCURVE_HPP_INCLUDED */
std::vector< double > eval_many(std::vector< double > &u, int32_t n_result, int32_t *ierr=nullptr) override
eval_many
Definition fpClosedCurve.hpp:252
bool is_allocated() const override
Check if object is allocated.
Definition fpClosedCurve.hpp:141
fpClosedCurve(fitpack_closed_curve_c &c_wrapper, bool move=false)
Construct from existing C wrapper (takes ownership if move=true)
Definition fpClosedCurve.hpp:115
fpClosedCurve & operator=(const fpClosedCurve &other)
Copy assignment - deep copy.
Definition fpClosedCurve.hpp:85
double mse() const override
Definition fpClosedCurve.hpp:313
int32_t fit(double *smoothing=nullptr, int32_t *order=nullptr, bool *keep_knots=nullptr) override
Definition fpClosedCurve.hpp:227
bool is_pointer() const override
Check if this is a non-owning pointer.
Definition fpClosedCurve.hpp:148
fpClosedCurve & operator=(fpClosedCurve &&other) noexcept
Move assignment - transfer ownership.
Definition fpClosedCurve.hpp:104
void set_default_parameters() override
Definition fpClosedCurve.hpp:215
fpParametricCurve & as_parent()
Upcast to parent type (reference, no copy)
Definition fpClosedCurve.hpp:196
int32_t new_fit(int32_t x_n1, int32_t x_n2, std::vector< double > &x, double *u=nullptr, double *w=nullptr, double *smoothing=nullptr, int32_t *order=nullptr) override
new_fit
Definition fpClosedCurve.hpp:222
int32_t least_squares(double *smoothing=nullptr, bool *reset_knots=nullptr) override
Definition fpClosedCurve.hpp:235
fpClosedCurve(const fpClosedCurve &other)
Copy constructor - deep copy.
Definition fpClosedCurve.hpp:77
int32_t comm_size() const override
Definition fpClosedCurve.hpp:291
std::vector< double > dfdx_all(double u, int32_t n_result, int32_t *ierr=nullptr) override
dfdx_all
Definition fpClosedCurve.hpp:284
void comm_expand(std::vector< double > &buffer) override
comm_expand
Definition fpClosedCurve.hpp:307
const fpParametricCurve & as_parent() const
Definition fpClosedCurve.hpp:199
void comm_pack(std::vector< double > &buffer) const override
comm_pack
Definition fpClosedCurve.hpp:298
const fitpack_closed_curve_c & c_handle() const
Get underlying C wrapper (const, for interop)
Definition fpClosedCurve.hpp:177
void destroy_base() override
Definition fpClosedCurve.hpp:339
fpClosedCurve(fpClosedCurve &&other) noexcept
Move constructor - transfer ownership.
Definition fpClosedCurve.hpp:96
void core_comm_pack(std::vector< double > &buffer) const override
core_comm_pack
Definition fpClosedCurve.hpp:324
~fpClosedCurve() override
Destructor - deallocates if owned.
Definition fpClosedCurve.hpp:70
const char * c_type_name() const override
Return the C wrapper struct name for this class (e.g. "fitpack_closed_curve_c").
Definition fpClosedCurve.hpp:155
void new_points(int32_t x_n1, int32_t x_n2, std::vector< double > &x, double *u=nullptr, double *w=nullptr) override
new_points
Definition fpClosedCurve.hpp:210
fpClosedCurve(fitpack_closed_curve_c &c_wrapper, ViewTag)
Non-owning view ctor: bit-copy the C handle, mark is_pointer=true. Used by parent classes' polymorphi...
Definition fpClosedCurve.hpp:129
fpClosedCurve(NoAlloc tag)
Definition fpClosedCurve.hpp:355
int32_t interpolate(int32_t *order=nullptr, bool *reset_knots=nullptr) override
Definition fpClosedCurve.hpp:231
fpClosedCurve()
Default constructor - allocates new fitpack_closed_curve.
Definition fpClosedCurve.hpp:63
static fpClosedCurve make_view()
Construct an empty (non-owning) wrapper, for use as a view target.
Definition fpClosedCurve.hpp:189
std::vector< double > dfdx(std::vector< double > &u, int32_t order, int32_t n_result, int32_t *ierr=nullptr) override
dfdx
Definition fpClosedCurve.hpp:273
const char * cpp_type_name() const override
Return the fully-qualified C++ class name for this class. Owned by the C++ layer — does not round-tri...
Definition fpClosedCurve.hpp:163
int32_t core_comm_size() const override
Definition fpClosedCurve.hpp:317
std::vector< double > dfdx(double u, int32_t order, int32_t n_result, int32_t *ierr=nullptr) override
dfdx
Definition fpClosedCurve.hpp:263
void core_comm_expand(std::vector< double > &buffer) override
core_comm_expand
Definition fpClosedCurve.hpp:333
fitpack_closed_curve_c & c_handle()
Get underlying C wrapper (for interop)
Definition fpClosedCurve.hpp:170
std::vector< double > eval_one(double u, int32_t n_result, int32_t *ierr=nullptr) override
eval_one
Definition fpClosedCurve.hpp:242
double & smoothing()
Definition fpFitter.hpp:317
T * as()
Definition fpFitter.hpp:360
fpParametricCurve()
Default constructor - allocates new fitpack_parametric_curve.
Definition fpParametricCurve.hpp:65
virtual int32_t fit(double *smoothing=nullptr, int32_t *order=nullptr, bool *keep_knots=nullptr)
Definition fpParametricCurve.hpp:229
fxArray< double > u() const
Zero-copy fxArray view of component 'u'.
Definition fpParametricCurve.hpp:436
fxArray< double > x() const
Zero-copy fxArray view of component 'x'.
Definition fpParametricCurve.hpp:391
virtual int32_t new_fit(int32_t x_n1, int32_t x_n2, std::vector< double > &x, double *u=nullptr, double *w=nullptr, double *smoothing=nullptr, int32_t *order=nullptr)
new_fit
Definition fpParametricCurve.hpp:224
fxArray< double > w() const
Zero-copy fxArray view of component 'w'.
Definition fpParametricCurve.hpp:526
int32_t & order()
Definition fpParametricCurve.hpp:674
Tag type for derived class constructors (no allocation)
Definition fpFitter.hpp:349
Tag type for non-owning view constructors of polymorphic concrete subtypes. Declared on roots and abs...
Definition fpFitter.hpp:103