1 /**
2  * \file NETGeographicLib/SphericalHarmonic.cpp
3  * \brief Implementation for NETGeographicLib::SphericalHarmonic class
4  *
5  * NETGeographicLib is copyright (c) Scott Heiman (2013)
6  * GeographicLib is Copyright (c) Charles Karney (2010-2012)
7  * <charles@karney.com> and licensed under the MIT/X11 License.
8  * For more information, see
9  * https://geographiclib.sourceforge.io/
10  **********************************************************************/
11 #include "stdafx.h"
12 #include "GeographicLib/SphericalHarmonic.hpp"
13 #include "SphericalHarmonic.h"
14 #include "CircularEngine.h"
15 #include "SphericalCoefficients.h"
16 #include "NETGeographicLib.h"
17 
18 using namespace NETGeographicLib;
19 
20 const char BADALLOC[] = "Failed to allocate memory for a GeographicLib::SphericalHarmonic";
21 
22 //*****************************************************************************
23 SphericalHarmonic::!SphericalHarmonic(void)
24 {
25     if ( m_pSphericalHarmonic != NULL )
26     {
27         delete m_pSphericalHarmonic;
28         m_pSphericalHarmonic = NULL;
29     }
30     if ( m_C != NULL )
31     {
32         delete m_C;
33         m_C = NULL;
34     }
35     if ( m_S != NULL )
36     {
37         delete m_S;
38         m_S = NULL;
39     }
40 }
41 
42 //*****************************************************************************
43 SphericalHarmonic::SphericalHarmonic(array<double>^ C,
44                     array<double>^ S,
45                     int N, double a, Normalization norm )
46 {
47     try
48     {
49         m_C = new std::vector<double>();
50         m_S = new std::vector<double>();
51         for each ( double x in C ) m_C->push_back(x);
52         for each ( double x in S ) m_S->push_back(x);
53         m_pSphericalHarmonic = new GeographicLib::SphericalHarmonic(
54             *m_C, *m_S, N, a, static_cast<unsigned>(norm) );
55     }
56     catch (std::bad_alloc)
57     {
58         throw gcnew GeographicErr( BADALLOC );
59     }
60     catch ( const std::exception& xcpt )
61     {
62         throw gcnew GeographicErr( xcpt.what() );
63     }
64     catch ( System::Exception^ sxpt )
65     {
66         throw gcnew GeographicErr( sxpt->Message );
67     }
68 }
69 
70 //*****************************************************************************
71 SphericalHarmonic::SphericalHarmonic(array<double>^ C,
72                     array<double>^ S,
73                     int N, int nmx, int mmx,
74                     double a, Normalization norm)
75 {
76     try
77     {
78         m_C = new std::vector<double>();
79         m_S = new std::vector<double>();
80         for each ( double x in C ) m_C->push_back(x);
81         for each ( double x in S ) m_S->push_back(x);
82         m_pSphericalHarmonic = new GeographicLib::SphericalHarmonic(
83             *m_C, *m_S, N, nmx, mmx, a, static_cast<unsigned>(norm) );
84     }
85     catch (std::bad_alloc)
86     {
87         throw gcnew GeographicErr( BADALLOC );
88     }
89     catch ( const std::exception& xcpt )
90     {
91         throw gcnew GeographicErr( xcpt.what() );
92     }
93     catch ( System::Exception^ sxpt )
94     {
95         throw gcnew GeographicErr( sxpt->Message );
96     }
97 }
98 
99 //*****************************************************************************
HarmonicSum(double x,double y,double z)100 double SphericalHarmonic::HarmonicSum(double x, double y, double z)
101 {
102     return m_pSphericalHarmonic->operator()( x, y, z );
103 }
104 
105 //*****************************************************************************
106 double SphericalHarmonic::HarmonicSum(double x, double y, double z,
107                         double% gradx, double% grady, double% gradz)
108 {
109     double lx, ly, lz;
110     double out = m_pSphericalHarmonic->operator()( x, y, z, lx, ly, lz );
111     gradx = lx;
112     grady = ly;
113     gradz = lz;
114     return out;
115 }
116 
117 //*****************************************************************************
118 CircularEngine^ SphericalHarmonic::Circle(double p, double z, bool gradp)
119 {
120     return gcnew CircularEngine( m_pSphericalHarmonic->Circle( p, z, gradp ) );
121 }
122 
123 //*****************************************************************************
124 SphericalCoefficients^ SphericalHarmonic::Coefficients()
125 {
126     return gcnew SphericalCoefficients( m_pSphericalHarmonic->Coefficients() );
127 }
128