1 /*
2  * Copyright (c) 2019, The University of Oxford
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  * 3. Neither the name of the University of Oxford nor the names of its
13  *    contributors may be used to endorse or promote products derived from this
14  *    software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "math/oskar_spherical_harmonic.h"
30 #include "math/private_spherical_harmonic.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
oskar_spherical_harmonic_coeff_const(const oskar_SphericalHarmonic * h)36 const oskar_Mem* oskar_spherical_harmonic_coeff_const(
37         const oskar_SphericalHarmonic* h)
38 {
39     return h->coeff;
40 }
41 
oskar_spherical_harmonic_copy(oskar_SphericalHarmonic * dst,const oskar_SphericalHarmonic * src,int * status)42 void oskar_spherical_harmonic_copy(oskar_SphericalHarmonic* dst,
43         const oskar_SphericalHarmonic* src, int* status)
44 {
45     dst->l_max = src->l_max;
46     if (src->l_max >= 0)
47         oskar_mem_copy(dst->coeff, src->coeff, status);
48 }
49 
oskar_spherical_harmonic_create(int type,int location,int l_max,int * status)50 oskar_SphericalHarmonic* oskar_spherical_harmonic_create(int type,
51         int location, int l_max, int* status)
52 {
53     oskar_SphericalHarmonic* h = (oskar_SphericalHarmonic*)
54             calloc(1, sizeof(oskar_SphericalHarmonic));
55     h->l_max = l_max;
56     h->coeff = oskar_mem_create(type, location, 0, status);
57     if (l_max >= 0)
58     {
59         const int num_coeff = (l_max + 1) * (l_max + 1);
60         oskar_mem_realloc(h->coeff, num_coeff, status);
61     }
62     return h;
63 }
64 
oskar_spherical_harmonic_free(oskar_SphericalHarmonic * h)65 void oskar_spherical_harmonic_free(oskar_SphericalHarmonic* h)
66 {
67     int status = 0;
68     if (!h) return;
69     oskar_mem_free(h->coeff, &status);
70     free(h);
71 }
72 
oskar_spherical_harmonic_l_max(const oskar_SphericalHarmonic * h)73 int oskar_spherical_harmonic_l_max(const oskar_SphericalHarmonic* h)
74 {
75     return h->l_max;
76 }
77 
oskar_spherical_harmonic_set_coeff(oskar_SphericalHarmonic * h,int l_max,const oskar_Mem * coeff,int * status)78 void oskar_spherical_harmonic_set_coeff(oskar_SphericalHarmonic* h,
79         int l_max, const oskar_Mem* coeff, int* status)
80 {
81     const int num_coeff = (l_max + 1) * (l_max + 1);
82     if (oskar_mem_length(coeff) < (size_t) num_coeff)
83     {
84         *status = OSKAR_ERR_DIMENSION_MISMATCH;
85         return;
86     }
87     h->l_max = l_max;
88     oskar_mem_copy(h->coeff, coeff, status);
89 }
90 
91 #ifdef __cplusplus
92 }
93 #endif
94