fitpack
Modern Fortran library for curve and surface fitting with splines
Loading...
Searching...
No Matches
fpSphere.hpp
Go to the documentation of this file.
1/***************************************************************************************************
2! ____________________ ___ ________ __
3! / ____/ _/_ __/ __ \/ | / ____/ //_/
4! / /_ / / / / / /_/ / /| |/ / / ,<
5! / __/ _/ / / / / ____/ ___ / /___/ /| |
6! /_/ /___/ /_/ /_/ /_/ |_\____/_/ |_|
7!
8! A Curve Fitting Package
9!
10! fpSphere.hpp (class fpSphere)
11!> @brief Standalone C++ wrapper for fitpack_sphere (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 FPSPHERE_HPP_INCLUDED
25#define FPSPHERE_HPP_INCLUDED
26
27#include "fitpack_config.h"
28
29#if HAVE_FXARRAY
30#include "fxArrays.hpp"
31#endif
32
33#include "fitpack_sphere_c.h"
34#include "fpFitter.hpp"
35#include <string>
36#include <vector>
37#include <cstdint>
38#include <memory>
39#include <stdexcept>
40#include <variant>
41#include <optional>
42#include <array>
43
44static_assert(sizeof(fitpack_sphere_c) == sizeof(fitpack_fitter_c),
45 "C descriptor layout mismatch: fitpack_sphere_c vs fitpack_fitter_c");
46
55class fpSphere : public fpFitter {
56public:
57 // ===========================================================================================
58 // Constructors and Destructor
59 // ===========================================================================================
60
65 fitpack_sphere_c_allocate(as<fitpack_sphere_c>(), nullptr);
66 }
67
71 ~fpSphere() override {
72 fitpack_sphere_c_destroy(as<fitpack_sphere_c>(), nullptr);
73 }
74
78 fpSphere(const fpSphere& other) : fpFitter(NoAlloc{}) {
79 *as<fitpack_sphere_c>() = fitpack_sphere_c_null;
80 fitpack_sphere_c_copy(as<fitpack_sphere_c>(), other.as<fitpack_sphere_c>(), false, nullptr);
81 }
82
86 fpSphere& operator=(const fpSphere& other) {
87 if (this != &other) {
88 fitpack_sphere_c_destroy(as<fitpack_sphere_c>(), nullptr);
89 fitpack_sphere_c_copy(as<fitpack_sphere_c>(), other.as<fitpack_sphere_c>(), false, nullptr);
90 }
91 return *this;
92 }
93
97 fpSphere(fpSphere&& other) noexcept : fpFitter(NoAlloc{}) {
98 *as<fitpack_sphere_c>() = fitpack_sphere_c_null;
99 fitpack_sphere_c_move_alloc(as<fitpack_sphere_c>(), other.as<fitpack_sphere_c>(), nullptr);
100 }
101
105 fpSphere& operator=(fpSphere&& other) noexcept {
106 if (this != &other) {
107 fitpack_sphere_c_destroy(as<fitpack_sphere_c>(), nullptr);
108 fitpack_sphere_c_move_alloc(as<fitpack_sphere_c>(), other.as<fitpack_sphere_c>(), nullptr);
109 }
110 return *this;
111 }
112
116 explicit fpSphere(fitpack_sphere_c& c_wrapper, bool move = false) : fpFitter(NoAlloc{}) {
117 if (move) {
118 fitpack_sphere_c_move_alloc(as<fitpack_sphere_c>(), &c_wrapper, nullptr);
119 } else {
120 *as<fitpack_sphere_c>() = fitpack_sphere_c_null;
121 fitpack_sphere_c_copy(as<fitpack_sphere_c>(), &c_wrapper, false, nullptr);
122 }
123 }
124
130 explicit fpSphere(fitpack_sphere_c& c_wrapper, ViewTag) : fpFitter(NoAlloc{}) {
131 *as<fitpack_sphere_c>() = c_wrapper;
132 as<fitpack_sphere_c>()->is_pointer = true;
133 }
134
135 // ===========================================================================================
136 // Utility Methods
137 // ===========================================================================================
138
142 bool is_allocated() const override {
143 return as<fitpack_sphere_c>()->cptr != nullptr;
144 }
145
149 bool is_pointer() const override {
150 return as<fitpack_sphere_c>()->is_pointer;
151 }
152
156 const char* c_type_name() const override {
157 return fitpack_sphere_c_c_type_name(*as<fitpack_sphere_c>());
158 }
159
164 const char* cpp_type_name() const override {
165 return "fpSphere";
166 }
167
171 fitpack_sphere_c& c_handle() {
172 return *as<fitpack_sphere_c>();
173 }
174
178 const fitpack_sphere_c& c_handle() const {
179 return *as<fitpack_sphere_c>();
180 }
181
191 return fpSphere(NoAlloc{});
192 }
193
198 return static_cast<fpFitter&>(*this);
199 }
200 const fpFitter& as_parent() const {
201 return static_cast<const fpFitter&>(*this);
202 }
203
204 // ===========================================================================================
205 // Method Wrappers (standalone — no fxArray dependency)
206 // ===========================================================================================
207
211 virtual void new_points(std::vector<double>& theta, std::vector<double>& phi, std::vector<double>& r, double* w = nullptr) {
212 int32_t n = static_cast<int32_t>(theta.size());
213 fitpack_sphere_c_new_points(as<fitpack_sphere_c>(), n, theta.data(), phi.data(), r.data(), w);
214 }
215
216
220 virtual int32_t new_fit(std::vector<double>& theta, std::vector<double>& phi, std::vector<double>& r, double* w = nullptr, double* smoothing = nullptr) {
221 int32_t n = static_cast<int32_t>(theta.size());
222 return fitpack_sphere_c_new_fit(as<fitpack_sphere_c>(), n, theta.data(), phi.data(), r.data(), w, smoothing);
223 }
224
225
226 virtual int32_t fit(double* smoothing = nullptr, bool* keep_knots = nullptr) {
227 return fitpack_sphere_c_fit(as<fitpack_sphere_c>(), smoothing, keep_knots);
228 }
229
230 virtual int32_t least_squares(double* smoothing = nullptr, bool* reset_knots = nullptr) {
231 return fitpack_sphere_c_least_squares(as<fitpack_sphere_c>(), smoothing, reset_knots);
232 }
233
234 virtual int32_t interpolate(bool* reset_knots = nullptr) {
235 return fitpack_sphere_c_interpolate(as<fitpack_sphere_c>(), reset_knots);
236 }
237
238 virtual double eval(double theta, double phi, int32_t* ierr = nullptr) {
239 return fitpack_sphere_c_sphere_eval_one(as<fitpack_sphere_c>(), theta, phi, ierr);
240 }
241
245 virtual std::vector<double> eval(std::vector<double>& theta, std::vector<double>& phi, int32_t* ierr = nullptr) {
246 int32_t n = static_cast<int32_t>(theta.size());
247 std::vector<double> result(n * n);
248 int32_t n_result = 0;
249 fitpack_sphere_c_sphere_eval_many(as<fitpack_sphere_c>(), n, theta.data(), phi.data(), ierr, result.data(), &n_result, n * n);
250 result.resize(n_result);
251 return result;
252 }
253
254
255 int32_t comm_size() const override {
256 return fitpack_sphere_c_comm_size(as<fitpack_sphere_c>());
257 }
258
262 void comm_pack(std::vector<double>& buffer) const override {
263 int32_t n = static_cast<int32_t>(buffer.size());
264 fitpack_sphere_c_comm_pack(as<fitpack_sphere_c>(), n, buffer.data());
265 }
266
267
271 void comm_expand(std::vector<double>& buffer) override {
272 int32_t n = static_cast<int32_t>(buffer.size());
273 fitpack_sphere_c_comm_expand(as<fitpack_sphere_c>(), n, buffer.data());
274 }
275
276
277 double mse() const override {
278 return fitpack_sphere_c_mse(as<fitpack_sphere_c>());
279 }
280
281 int32_t core_comm_size() const override {
282 return fitpack_sphere_c_core_comm_size(as<fitpack_sphere_c>());
283 }
284
288 void core_comm_pack(std::vector<double>& buffer) const override {
289 int32_t n = static_cast<int32_t>(buffer.size());
290 fitpack_sphere_c_core_comm_pack(as<fitpack_sphere_c>(), n, buffer.data());
291 }
292
293
297 void core_comm_expand(std::vector<double>& buffer) override {
298 int32_t n = static_cast<int32_t>(buffer.size());
299 fitpack_sphere_c_core_comm_expand(as<fitpack_sphere_c>(), n, buffer.data());
300 }
301
302
303 void destroy_base() override {
304 fitpack_sphere_c_destroy_base(as<fitpack_sphere_c>());
305 }
306
307 // ===========================================================================================
308 // Component Array Accessors
309 // ===========================================================================================
310
315 std::vector<double> theta_vector() const {
316 double* raw = nullptr;
317 int64_t extents[1] = {0};
318 fitpack_sphere_c_getcomp_theta(as<fitpack_sphere_c>(), &raw, extents);
319 const int64_t total = extents[0];
320 if (raw == nullptr || total <= 0) return {};
321 const double* first = reinterpret_cast<const double*>(raw);
322 return std::vector<double>(first, first + total);
323 }
324
325#if HAVE_FXARRAY
338 fxArray<double> theta() const {
339 double* raw = nullptr;
340 int64_t extents[1] = {0};
341 fitpack_sphere_c_getcomp_theta(as<fitpack_sphere_c>(), &raw, extents);
342 FX_SIZE bounds[2]; // (lower, upper) per dimension, Fortran lbound 1
343 for (int k = 0; k < 1; ++k) {
344 bounds[2 * k] = 1;
345 bounds[2 * k + 1] = static_cast<FX_SIZE>(extents[k]);
346 }
347 array_c descr = array_c_null;
348 array_c_from_ptr(&descr, "theta", static_cast<void*>(raw),
349 getCFITypeFlag<double>(),
350 static_cast<FX_SIZE>(sizeof(double)),
351 static_cast<FX_RANK>(1), bounds);
352 return fxArray<double>(descr);
353 }
354#endif // HAVE_FXARRAY
355
360 std::vector<double> phi_vector() const {
361 double* raw = nullptr;
362 int64_t extents[1] = {0};
363 fitpack_sphere_c_getcomp_phi(as<fitpack_sphere_c>(), &raw, extents);
364 const int64_t total = extents[0];
365 if (raw == nullptr || total <= 0) return {};
366 const double* first = reinterpret_cast<const double*>(raw);
367 return std::vector<double>(first, first + total);
368 }
369
370#if HAVE_FXARRAY
383 fxArray<double> phi() const {
384 double* raw = nullptr;
385 int64_t extents[1] = {0};
386 fitpack_sphere_c_getcomp_phi(as<fitpack_sphere_c>(), &raw, extents);
387 FX_SIZE bounds[2]; // (lower, upper) per dimension, Fortran lbound 1
388 for (int k = 0; k < 1; ++k) {
389 bounds[2 * k] = 1;
390 bounds[2 * k + 1] = static_cast<FX_SIZE>(extents[k]);
391 }
392 array_c descr = array_c_null;
393 array_c_from_ptr(&descr, "phi", static_cast<void*>(raw),
394 getCFITypeFlag<double>(),
395 static_cast<FX_SIZE>(sizeof(double)),
396 static_cast<FX_RANK>(1), bounds);
397 return fxArray<double>(descr);
398 }
399#endif // HAVE_FXARRAY
400
405 std::vector<double> r_vector() const {
406 double* raw = nullptr;
407 int64_t extents[1] = {0};
408 fitpack_sphere_c_getcomp_r(as<fitpack_sphere_c>(), &raw, extents);
409 const int64_t total = extents[0];
410 if (raw == nullptr || total <= 0) return {};
411 const double* first = reinterpret_cast<const double*>(raw);
412 return std::vector<double>(first, first + total);
413 }
414
415#if HAVE_FXARRAY
428 fxArray<double> r() const {
429 double* raw = nullptr;
430 int64_t extents[1] = {0};
431 fitpack_sphere_c_getcomp_r(as<fitpack_sphere_c>(), &raw, extents);
432 FX_SIZE bounds[2]; // (lower, upper) per dimension, Fortran lbound 1
433 for (int k = 0; k < 1; ++k) {
434 bounds[2 * k] = 1;
435 bounds[2 * k + 1] = static_cast<FX_SIZE>(extents[k]);
436 }
437 array_c descr = array_c_null;
438 array_c_from_ptr(&descr, "r", static_cast<void*>(raw),
439 getCFITypeFlag<double>(),
440 static_cast<FX_SIZE>(sizeof(double)),
441 static_cast<FX_RANK>(1), bounds);
442 return fxArray<double>(descr);
443 }
444#endif // HAVE_FXARRAY
445
450 std::vector<double> w_vector() const {
451 double* raw = nullptr;
452 int64_t extents[1] = {0};
453 fitpack_sphere_c_getcomp_w(as<fitpack_sphere_c>(), &raw, extents);
454 const int64_t total = extents[0];
455 if (raw == nullptr || total <= 0) return {};
456 const double* first = reinterpret_cast<const double*>(raw);
457 return std::vector<double>(first, first + total);
458 }
459
460#if HAVE_FXARRAY
473 fxArray<double> w() const {
474 double* raw = nullptr;
475 int64_t extents[1] = {0};
476 fitpack_sphere_c_getcomp_w(as<fitpack_sphere_c>(), &raw, extents);
477 FX_SIZE bounds[2]; // (lower, upper) per dimension, Fortran lbound 1
478 for (int k = 0; k < 1; ++k) {
479 bounds[2 * k] = 1;
480 bounds[2 * k + 1] = static_cast<FX_SIZE>(extents[k]);
481 }
482 array_c descr = array_c_null;
483 array_c_from_ptr(&descr, "w", static_cast<void*>(raw),
484 getCFITypeFlag<double>(),
485 static_cast<FX_SIZE>(sizeof(double)),
486 static_cast<FX_RANK>(1), bounds);
487 return fxArray<double>(descr);
488 }
489#endif // HAVE_FXARRAY
490
495 std::vector<double> wrk2_vector() const {
496 double* raw = nullptr;
497 int64_t extents[1] = {0};
498 fitpack_sphere_c_getcomp_wrk2(as<fitpack_sphere_c>(), &raw, extents);
499 const int64_t total = extents[0];
500 if (raw == nullptr || total <= 0) return {};
501 const double* first = reinterpret_cast<const double*>(raw);
502 return std::vector<double>(first, first + total);
503 }
504
505#if HAVE_FXARRAY
518 fxArray<double> wrk2() const {
519 double* raw = nullptr;
520 int64_t extents[1] = {0};
521 fitpack_sphere_c_getcomp_wrk2(as<fitpack_sphere_c>(), &raw, extents);
522 FX_SIZE bounds[2]; // (lower, upper) per dimension, Fortran lbound 1
523 for (int k = 0; k < 1; ++k) {
524 bounds[2 * k] = 1;
525 bounds[2 * k + 1] = static_cast<FX_SIZE>(extents[k]);
526 }
527 array_c descr = array_c_null;
528 array_c_from_ptr(&descr, "wrk2", static_cast<void*>(raw),
529 getCFITypeFlag<double>(),
530 static_cast<FX_SIZE>(sizeof(double)),
531 static_cast<FX_RANK>(1), bounds);
532 return fxArray<double>(descr);
533 }
534#endif // HAVE_FXARRAY
535
544 std::vector<double> t_vector() const {
545 double* raw = nullptr;
546 int64_t extents[2] = {0, 0};
547 fitpack_sphere_c_getcomp_t(as<fitpack_sphere_c>(), &raw, extents);
548 const int64_t total = extents[0] * extents[1];
549 if (raw == nullptr || total <= 0) return {};
550 const double* first = reinterpret_cast<const double*>(raw);
551 return std::vector<double>(first, first + total);
552 }
553
558 std::array<int64_t, 2> t_shape() const {
559 double* raw = nullptr;
560 int64_t extents[2] = {0, 0};
561 fitpack_sphere_c_getcomp_t(as<fitpack_sphere_c>(), &raw, extents);
562 return { extents[0], extents[1] };
563 }
564
565#if HAVE_FXARRAY
578 fxArray<double> t() const {
579 double* raw = nullptr;
580 int64_t extents[2] = {0, 0};
581 fitpack_sphere_c_getcomp_t(as<fitpack_sphere_c>(), &raw, extents);
582 FX_SIZE bounds[4]; // (lower, upper) per dimension, Fortran lbound 1
583 for (int k = 0; k < 2; ++k) {
584 bounds[2 * k] = 1;
585 bounds[2 * k + 1] = static_cast<FX_SIZE>(extents[k]);
586 }
587 array_c descr = array_c_null;
588 array_c_from_ptr(&descr, "t", static_cast<void*>(raw),
589 getCFITypeFlag<double>(),
590 static_cast<FX_SIZE>(sizeof(double)),
591 static_cast<FX_RANK>(2), bounds);
592 return fxArray<double>(descr);
593 }
594#endif // HAVE_FXARRAY
595
596 // ===========================================================================================
597 // Scalar Property Accessors
598 // ===========================================================================================
599
600 int32_t& m() {
601 return *fitpack_sphere_c_ref_m(as<fitpack_sphere_c>());
602 }
603 const int32_t& m() const {
604 return *fitpack_sphere_c_ref_m(as<fitpack_sphere_c>());
605 }
606
607 int32_t& lwrk2() {
608 return *fitpack_sphere_c_ref_lwrk2(as<fitpack_sphere_c>());
609 }
610 const int32_t& lwrk2() const {
611 return *fitpack_sphere_c_ref_lwrk2(as<fitpack_sphere_c>());
612 }
613
614 int32_t& nmax() {
615 return *fitpack_sphere_c_ref_nmax(as<fitpack_sphere_c>());
616 }
617 const int32_t& nmax() const {
618 return *fitpack_sphere_c_ref_nmax(as<fitpack_sphere_c>());
619 }
620
621protected:
622 explicit fpSphere(NoAlloc tag) : fpFitter(tag) {}
623
624};
625
626#endif /* FPSPHERE_HPP_INCLUDED */
double & smoothing()
Definition fpFitter.hpp:317
fpFitter(NoAlloc)
Protected constructor for derived classes (does not allocate)
Definition fpFitter.hpp:354
T * as()
Definition fpFitter.hpp:360
virtual int32_t least_squares(double *smoothing=nullptr, bool *reset_knots=nullptr)
Definition fpSphere.hpp:230
fxArray< double > phi() const
Zero-copy fxArray view of component 'phi'.
Definition fpSphere.hpp:383
std::vector< double > wrk2_vector() const
Deep copy of component 'wrk2' as a std::vector.
Definition fpSphere.hpp:495
const int32_t & lwrk2() const
Definition fpSphere.hpp:610
fxArray< double > theta() const
Zero-copy fxArray view of component 'theta'.
Definition fpSphere.hpp:338
std::vector< double > phi_vector() const
Deep copy of component 'phi' as a std::vector.
Definition fpSphere.hpp:360
std::vector< double > theta_vector() const
Deep copy of component 'theta' as a std::vector.
Definition fpSphere.hpp:315
virtual int32_t fit(double *smoothing=nullptr, bool *keep_knots=nullptr)
Definition fpSphere.hpp:226
const int32_t & m() const
Definition fpSphere.hpp:603
const int32_t & nmax() const
Definition fpSphere.hpp:617
void comm_pack(std::vector< double > &buffer) const override
comm_pack
Definition fpSphere.hpp:262
fxArray< double > wrk2() const
Zero-copy fxArray view of component 'wrk2'.
Definition fpSphere.hpp:518
fitpack_sphere_c & c_handle()
Get underlying C wrapper (for interop)
Definition fpSphere.hpp:171
fpSphere & operator=(const fpSphere &other)
Copy assignment - deep copy.
Definition fpSphere.hpp:86
int32_t comm_size() const override
Definition fpSphere.hpp:255
std::vector< double > t_vector() const
Deep copy of component 't' as a std::vector.
Definition fpSphere.hpp:544
void comm_expand(std::vector< double > &buffer) override
comm_expand
Definition fpSphere.hpp:271
fpSphere()
Default constructor - allocates new fitpack_sphere.
Definition fpSphere.hpp:64
virtual int32_t interpolate(bool *reset_knots=nullptr)
Definition fpSphere.hpp:234
fxArray< double > r() const
Zero-copy fxArray view of component 'r'.
Definition fpSphere.hpp:428
fpSphere(fitpack_sphere_c &c_wrapper, ViewTag)
Non-owning view ctor: bit-copy the C handle, mark is_pointer=true. Used by parent classes' polymorphi...
Definition fpSphere.hpp:130
~fpSphere() override
Destructor - deallocates if owned.
Definition fpSphere.hpp:71
std::array< int64_t, 2 > t_shape() const
Extents of component 't', leading dimension first.
Definition fpSphere.hpp:558
int32_t & m()
Definition fpSphere.hpp:600
fpSphere(const fpSphere &other)
Copy constructor - deep copy.
Definition fpSphere.hpp:78
void destroy_base() override
Definition fpSphere.hpp:303
void core_comm_pack(std::vector< double > &buffer) const override
core_comm_pack
Definition fpSphere.hpp:288
const fpFitter & as_parent() const
Definition fpSphere.hpp:200
fpSphere & operator=(fpSphere &&other) noexcept
Move assignment - transfer ownership.
Definition fpSphere.hpp:105
virtual double eval(double theta, double phi, int32_t *ierr=nullptr)
Definition fpSphere.hpp:238
const char * c_type_name() const override
Return the C wrapper struct name for this class (e.g. "fitpack_sphere_c").
Definition fpSphere.hpp:156
std::vector< double > w_vector() const
Deep copy of component 'w' as a std::vector.
Definition fpSphere.hpp:450
fxArray< double > t() const
Zero-copy fxArray view of component 't'.
Definition fpSphere.hpp:578
std::vector< double > r_vector() const
Deep copy of component 'r' as a std::vector.
Definition fpSphere.hpp:405
int32_t & lwrk2()
Definition fpSphere.hpp:607
fpSphere(NoAlloc tag)
Definition fpSphere.hpp:622
bool is_pointer() const override
Check if this is a non-owning pointer.
Definition fpSphere.hpp:149
void core_comm_expand(std::vector< double > &buffer) override
core_comm_expand
Definition fpSphere.hpp:297
virtual void new_points(std::vector< double > &theta, std::vector< double > &phi, std::vector< double > &r, double *w=nullptr)
new_points
Definition fpSphere.hpp:211
virtual int32_t new_fit(std::vector< double > &theta, std::vector< double > &phi, std::vector< double > &r, double *w=nullptr, double *smoothing=nullptr)
new_fit
Definition fpSphere.hpp:220
int32_t core_comm_size() const override
Definition fpSphere.hpp:281
fpSphere(fitpack_sphere_c &c_wrapper, bool move=false)
Construct from existing C wrapper (takes ownership if move=true)
Definition fpSphere.hpp:116
fxArray< double > w() const
Zero-copy fxArray view of component 'w'.
Definition fpSphere.hpp:473
static fpSphere make_view()
Construct an empty (non-owning) wrapper, for use as a view target.
Definition fpSphere.hpp:190
virtual std::vector< double > eval(std::vector< double > &theta, std::vector< double > &phi, int32_t *ierr=nullptr)
eval
Definition fpSphere.hpp:245
bool is_allocated() const override
Check if object is allocated.
Definition fpSphere.hpp:142
fpSphere(fpSphere &&other) noexcept
Move constructor - transfer ownership.
Definition fpSphere.hpp:97
fpFitter & as_parent()
Upcast to parent type (reference, no copy)
Definition fpSphere.hpp:197
double mse() const override
Definition fpSphere.hpp:277
int32_t & nmax()
Definition fpSphere.hpp:614
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 fpSphere.hpp:164
const fitpack_sphere_c & c_handle() const
Get underlying C wrapper (const, for interop)
Definition fpSphere.hpp:178
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