Isis 3 Programmer Reference
SurfaceModel.cpp
1
6/* SPDX-License-Identifier: CC0-1.0 */
7#include "SurfaceModel.h"
8
9namespace Isis {
12 p_poly2d = new PolynomialBivariate(2);
13 p_lsq = new LeastSquares(*p_poly2d);
14 }
15
18 delete p_lsq;
19 delete p_poly2d;
20 }
21
24 void SurfaceModel::AddTriplet(const double x, const double y, const double z) {
25 std::vector<double> vec;
26 vec.push_back(x);
27 vec.push_back(y);
28 p_lsq->AddKnown(vec, z);
29 }
30
33 void SurfaceModel::AddTriplets(const double *x, const double *y,
34 const double *z, const int n) {
35 for(int i = 0; i < n; i++) {
36 AddTriplet(x[i], y[i], z[i]);
37 }
38 }
39
42 void SurfaceModel::AddTriplets(const std::vector<double> &x,
43 const std::vector<double> &y,
44 const std::vector<double> &z) {
45 for(int i = 0; i < (int)x.size(); i++) {
46 AddTriplet(x[i], y[i], z[i]);
47 }
48 }
49
52 p_lsq->Solve();
53 }
54
57 double SurfaceModel::Evaluate(const double x, const double y) {
58 std::vector<double> vec;
59 vec.push_back(x);
60 vec.push_back(y);
61 return p_lsq->Evaluate(vec);
62 }
63
70 int SurfaceModel::MinMax(double &x, double &y) {
71 /* For a PolynomialBivariate of 2nd degree, the partial derivatives are two lines:
72 *
73 * dz/dx = b + 2dx + ey
74 * dz/dy = c + ex + 2fy
75 *
76 * We will have a local min/max where dz/dx and dz/dy = 0.
77 * Solve using that condition using linear algebra yields:
78 *
79 * xlocal = (ce - 2bf) / (4df - ee)
80 * ylocal = (be - 2cd) / (4df - ee)
81 */
82
83 // Get coefficients
84 double b = p_poly2d->Coefficient(1);
85 double c = p_poly2d->Coefficient(2);
86 double d = p_poly2d->Coefficient(3);
87 double e = p_poly2d->Coefficient(4);
88 double f = p_poly2d->Coefficient(5);
89
90 // Compute the determinant
91 double det = 4.0 * d * f - e * e;
92 if(det == 0.0) return 1;
93
94 // Compute local min/max
95 x = (c * e - 2.0 * b * f) / det;
96 y = (b * e - 2.0 * c * d) / det;
97 return 0;
98 }
99}
double Coefficient(int i) const
Returns the ith coefficient.
Generic least square fitting class.
double Evaluate(const std::vector< double > &input)
Invokes the BasisFunction Evaluate method.
int Solve(Isis::LeastSquares::SolveMethod method=SVD)
After all the data has been registered through AddKnown, invoke this method to solve the system of eq...
void AddKnown(const std::vector< double > &input, double expected, double weight=1.0)
Invoke this method for each set of knowns.
Nth degree Polynomial with two variables.
void AddTriplet(const double x, const double y, const double z)
Add a single (x,y,z) triplet to the list of knowns. After all knowns are added invoke the Solve metho...
~SurfaceModel()
Destructor.
SurfaceModel()
Constructor.
int MinMax(double &x, double &y)
After invoking Solve, a coordinate (x,y) at a local minimum (or maximum) of the surface model can be ...
void Solve()
Fit a surface to the input triplets.
void AddTriplets(const double *x, const double *y, const double *z, const int n)
Add an array of (x,y,z) triplet to the list of knowns After all knowns are added invoke the Solve met...
double Evaluate(const double x, const double y)
Evaluate at x,y to compute z. This is available after the Solve method is invoked.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16