|
1 | | -#include <mntRegridAxis.h> |
2 | | - |
3 | | -LIBRARY_API |
4 | | -int mnt_regridaxis_new(RegridAxis_t** self) { |
5 | | - |
6 | | - *self = new RegridAxis_t(); |
7 | | - |
8 | | - (*self)->spline = vtkCardinalSpline::New(); |
9 | | - |
10 | | - // let the the spline estimate the second derivative at the boundaries |
11 | | - (*self)->spline->SetLeftConstraint(3); |
12 | | - (*self)->spline->SetRightConstraint(3); |
13 | | - |
14 | | - return 0; |
15 | | -} |
16 | | - |
17 | | -LIBRARY_API |
18 | | -int mnt_regridaxis_del(RegridAxis_t** self) { |
19 | | - |
20 | | - // destroy the spline object |
21 | | - (*self)->spline->Delete(); |
22 | | - |
23 | | - delete *self; |
24 | | - |
25 | | - return 0; |
26 | | -} |
27 | | - |
28 | | - |
29 | | -LIBRARY_API |
30 | | -int mnt_regridaxis_build(RegridAxis_t** self, int numValues, const double srcValues[]) { |
31 | | - |
32 | | - (*self)->spline->RemoveAllPoints(); |
33 | | - |
34 | | - if (numValues < 2) { |
35 | | - // need at least two values (one interval) |
36 | | - return 1; |
37 | | - } |
38 | | - |
39 | | - (*self)->numValues = numValues; |
40 | | - (*self)->tLo = 0; |
41 | | - (*self)->tHi = numValues - 1; |
42 | | - |
43 | | - // axis is monotonically increasing |
44 | | - (*self)->increasing = true; |
45 | | - if (srcValues[numValues - 1] < srcValues[0]) { |
46 | | - // axis is monotonically decreasing |
47 | | - (*self)->increasing = false; |
48 | | - } |
49 | | - |
50 | | - // construct the spline object. Note that the spline object maps the |
51 | | - // axis values to indices. This will allow us to quickly find the |
52 | | - // float index of a target axis value. |
53 | | - int ier = 0; |
54 | | - for (int i = 0; i < numValues - 1; ++i) { |
55 | | - |
56 | | - (*self)->spline->AddPoint(srcValues[i], double(i)); |
57 | | - |
58 | | - if ((*self)->increasing && srcValues[i + 1] < srcValues[i]) { |
59 | | - ier++; |
60 | | - } |
61 | | - else if (!(*self)->increasing && srcValues[i + 1] > srcValues[i]) { |
62 | | - ier++; |
63 | | - } |
64 | | - } |
65 | | - // add last value |
66 | | - (*self)->spline->AddPoint(srcValues[numValues - 1], double(numValues - 1)); |
67 | | - |
68 | | - // compute the spline coefficients |
69 | | - (*self)->spline->Compute(); |
70 | | - |
71 | | - return ier; |
72 | | -} |
73 | | - |
74 | | -LIBRARY_API |
75 | | -int mnt_regridaxis_getPointWeights(RegridAxis_t** self, double target, int indices[2], double weights[2]) { |
76 | | - |
77 | | - double t = (*self)->spline->Evaluate(target); |
78 | | - |
79 | | - // low side of the interval |
80 | | - indices[0] = std::max(0, std::min((*self)->numValues - 2, int(floor(t)))); |
81 | | - |
82 | | - // high side of the interval |
83 | | - indices[1] = indices[0] + 1; |
84 | | - |
85 | | - // linear interpolation weights |
86 | | - weights[0] = indices[1] - t; |
87 | | - weights[1] = t - indices[0]; |
88 | | - |
89 | | - return 0; |
90 | | -} |
91 | | - |
92 | | - |
93 | | -LIBRARY_API |
94 | | -int mnt_regridaxis_getCellIndexBounds(RegridAxis_t** self, const double targets[2], double indexBounds[2]) { |
95 | | - |
96 | | - indexBounds[0] = (*self)->spline->Evaluate(targets[0]); |
97 | | - indexBounds[1] = (*self)->spline->Evaluate(targets[1]); |
98 | | - |
99 | | - return 0; |
100 | | -} |
101 | | - |
| 1 | +#include <mntRegridAxis.h> |
| 2 | +#include <cmath> |
| 3 | + |
| 4 | +LIBRARY_API |
| 5 | +int mnt_regridaxis_new(RegridAxis_t** self) { |
| 6 | + |
| 7 | + *self = new RegridAxis_t(); |
| 8 | + |
| 9 | + (*self)->spline = vtkCardinalSpline::New(); |
| 10 | + |
| 11 | + // let the the spline estimate the second derivative at the boundaries |
| 12 | + (*self)->spline->SetLeftConstraint(3); |
| 13 | + (*self)->spline->SetRightConstraint(3); |
| 14 | + |
| 15 | + return 0; |
| 16 | +} |
| 17 | + |
| 18 | +LIBRARY_API |
| 19 | +int mnt_regridaxis_del(RegridAxis_t** self) { |
| 20 | + |
| 21 | + // destroy the spline object |
| 22 | + (*self)->spline->Delete(); |
| 23 | + |
| 24 | + delete *self; |
| 25 | + |
| 26 | + return 0; |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +LIBRARY_API |
| 31 | +int mnt_regridaxis_build(RegridAxis_t** self, int numValues, const double srcValues[]) { |
| 32 | + |
| 33 | + (*self)->spline->RemoveAllPoints(); |
| 34 | + |
| 35 | + if (numValues < 2) { |
| 36 | + // need at least two values (one interval) |
| 37 | + return 1; |
| 38 | + } |
| 39 | + |
| 40 | + (*self)->numValues = numValues; |
| 41 | + (*self)->tLo = 0; |
| 42 | + (*self)->tHi = numValues - 1; |
| 43 | + |
| 44 | + // axis is monotonically increasing |
| 45 | + (*self)->increasing = true; |
| 46 | + if (srcValues[numValues - 1] < srcValues[0]) { |
| 47 | + // axis is monotonically decreasing |
| 48 | + (*self)->increasing = false; |
| 49 | + } |
| 50 | + |
| 51 | + // construct the spline object. Note that the spline object maps the |
| 52 | + // axis values to indices. This will allow us to quickly find the |
| 53 | + // float index of a target axis value. |
| 54 | + int ier = 0; |
| 55 | + for (int i = 0; i < numValues - 1; ++i) { |
| 56 | + |
| 57 | + (*self)->spline->AddPoint(srcValues[i], double(i)); |
| 58 | + |
| 59 | + if ((*self)->increasing && srcValues[i + 1] < srcValues[i]) { |
| 60 | + ier++; |
| 61 | + } |
| 62 | + else if (!(*self)->increasing && srcValues[i + 1] > srcValues[i]) { |
| 63 | + ier++; |
| 64 | + } |
| 65 | + } |
| 66 | + // add last value |
| 67 | + (*self)->spline->AddPoint(srcValues[numValues - 1], double(numValues - 1)); |
| 68 | + |
| 69 | + // compute the spline coefficients |
| 70 | + (*self)->spline->Compute(); |
| 71 | + |
| 72 | + return ier; |
| 73 | +} |
| 74 | + |
| 75 | +LIBRARY_API |
| 76 | +int mnt_regridaxis_getPointWeights(RegridAxis_t** self, double target, int indices[2], double weights[2]) { |
| 77 | + |
| 78 | + double t = (*self)->spline->Evaluate(target); |
| 79 | + |
| 80 | + // low side of the interval |
| 81 | + indices[0] = std::max(0, std::min((*self)->numValues - 2, int(floor(t)))); |
| 82 | + |
| 83 | + // high side of the interval |
| 84 | + indices[1] = indices[0] + 1; |
| 85 | + |
| 86 | + // linear interpolation weights |
| 87 | + weights[0] = indices[1] - t; |
| 88 | + weights[1] = t - indices[0]; |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +LIBRARY_API |
| 95 | +int mnt_regridaxis_getCellIndexBounds(RegridAxis_t** self, const double targets[2], double indexBounds[2]) { |
| 96 | + |
| 97 | + indexBounds[0] = (*self)->spline->Evaluate(targets[0]); |
| 98 | + indexBounds[1] = (*self)->spline->Evaluate(targets[1]); |
| 99 | + |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
0 commit comments