solstice-solver

Solver library of the solstice app
git clone git://git.meso-star.com/solstice-solver.git
Log | Files | Refs | README | LICENSE

ssol_data.c (2767B)


      1 /* Copyright (C) 2018-2026 |Meso|Star> (contact@meso-star.com)
      2  * Copyright (C) 2016, 2018 CNRS
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation, either version 3 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     16 
     17 #ifndef SSOL_DATA_C_H
     18 #define SSOL_DATA_C_H
     19 
     20 #include "ssol.h"
     21 #include "ssol_spectrum_c.h"
     22 
     23 /*******************************************************************************
     24  * Exported functions
     25  ******************************************************************************/
     26 struct ssol_data*
     27 ssol_data_set_real(struct ssol_data* data, const double real)
     28 {
     29   const double r = real;
     30   ASSERT(data);
     31   ssol_data_clear(data);
     32   data->type = SSOL_DATA_REAL;
     33   data->value.real = r;
     34   return data;
     35 }
     36 
     37 struct ssol_data*
     38 ssol_data_set_spectrum(struct ssol_data* data, struct ssol_spectrum* spectrum)
     39 {
     40   ASSERT(data && spectrum);
     41   if(data->type == SSOL_DATA_SPECTRUM && data->value.spectrum == spectrum)
     42     return data;
     43   ssol_data_clear(data);
     44   data->type = SSOL_DATA_SPECTRUM;
     45   data->value.spectrum = spectrum;
     46   SSOL(spectrum_ref_get(spectrum));
     47   return data;
     48 }
     49 
     50 struct ssol_data*
     51 ssol_data_clear(struct ssol_data* data)
     52 {
     53   ASSERT(data);
     54   if(data->type != SSOL_DATA_SPECTRUM) return data;
     55   ASSERT(data->value.spectrum);
     56   SSOL(spectrum_ref_put(data->value.spectrum));
     57   *data = SSOL_DATA_NULL;
     58   return data;
     59 }
     60 
     61 struct ssol_data*
     62 ssol_data_copy(struct ssol_data* dst, const struct ssol_data* src)
     63 {
     64   ASSERT(dst && src);
     65   if(dst == src) return dst;
     66   ssol_data_clear(dst);
     67   switch(src->type) {
     68     case SSOL_DATA_REAL:
     69       dst->value.real = src->value.real;
     70       break;
     71     case SSOL_DATA_SPECTRUM:
     72       ssol_data_set_spectrum(dst, src->value.spectrum);
     73       break;
     74     default: FATAL("Unreachable code.\n"); break;
     75   }
     76   dst->type = src->type;
     77   return dst;
     78 }
     79 
     80 double
     81 ssol_data_get_value(const struct ssol_data* data, const double wavelength)
     82 {
     83   double val;
     84   ASSERT(data);
     85 
     86   switch(data->type) {
     87     case SSOL_DATA_REAL:
     88       val = data->value.real;
     89       break;
     90     case SSOL_DATA_SPECTRUM:
     91       ASSERT(wavelength >= 0);
     92       val = spectrum_interpolate(data->value.spectrum, wavelength);
     93       break;
     94     default: FATAL("Unreachable code\n"); break;
     95   }
     96   return val;
     97 }
     98 
     99 #endif /* SSOL_DATA_C_H */
    100