solstice-solver

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

ssol_atmosphere.c (3451B)


      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 #include "ssol.h"
     18 #include "ssol_atmosphere_c.h"
     19 #include "ssol_device_c.h"
     20 #include "ssol_spectrum_c.h"
     21 
     22 #include <rsys/rsys.h>
     23 #include <rsys/mem_allocator.h>
     24 #include <rsys/ref_count.h>
     25 
     26 /*******************************************************************************
     27  * Helper functions
     28  ******************************************************************************/
     29 static void
     30 atmosphere_release(ref_T* ref)
     31 {
     32   struct ssol_device* dev;
     33   struct ssol_atmosphere* atmosphere = CONTAINER_OF(ref, struct ssol_atmosphere, ref);
     34   ASSERT(ref);
     35   dev = atmosphere->dev;
     36   ASSERT(dev && dev->allocator);
     37   ssol_data_clear(&atmosphere->extinction);
     38   MEM_RM(dev->allocator, atmosphere);
     39   SSOL(device_ref_put(dev));
     40 }
     41 
     42 static INLINE int
     43 check_extinction(const struct ssol_data* extinction)
     44 {
     45   if(!extinction) return 0;
     46 
     47   /* Check extinction in [0, INF) */
     48   switch(extinction->type) {
     49   case SSOL_DATA_REAL:
     50     if(extinction->value.real < 0 || extinction->value.real > 1)
     51       return 0;
     52     break;
     53   case SSOL_DATA_SPECTRUM:
     54     if(!extinction->value.spectrum
     55       || !spectrum_check_data(extinction->value.spectrum, 0, 1))
     56       return 0;
     57     break;
     58   default: FATAL("Unreachable code\n"); break;
     59   }
     60 
     61   return 1;
     62 }
     63 
     64 /*******************************************************************************
     65  * Exported ssol_atmosphere functions
     66  ******************************************************************************/
     67 res_T
     68 ssol_atmosphere_create
     69   (struct ssol_device* dev,
     70    struct ssol_atmosphere** out_atmosphere)
     71 {
     72   struct ssol_atmosphere* atmosphere = NULL;
     73   res_T res = RES_OK;
     74   if(!dev || !out_atmosphere) {
     75     return RES_BAD_ARG;
     76   }
     77 
     78   atmosphere = (struct ssol_atmosphere*)MEM_CALLOC
     79     (dev->allocator, 1, sizeof(struct ssol_atmosphere));
     80   if(!atmosphere) {
     81     res = RES_MEM_ERR;
     82     goto error;
     83   }
     84 
     85   SSOL(device_ref_get(dev));
     86   atmosphere->dev = dev;
     87   ref_init(&atmosphere->ref);
     88 
     89 exit:
     90   if(out_atmosphere) *out_atmosphere = atmosphere;
     91   return res;
     92 error:
     93   if(atmosphere) {
     94     SSOL(atmosphere_ref_put(atmosphere));
     95     atmosphere = NULL;
     96   }
     97   goto exit;
     98 }
     99 
    100 res_T
    101 ssol_atmosphere_ref_get
    102   (struct ssol_atmosphere* atmosphere)
    103 {
    104   if(!atmosphere) return RES_BAD_ARG;
    105   ref_get(&atmosphere->ref);
    106   return RES_OK;
    107 }
    108 
    109 res_T
    110 ssol_atmosphere_ref_put
    111   (struct ssol_atmosphere* atmosphere)
    112 {
    113   if(!atmosphere) return RES_BAD_ARG;
    114   ref_put(&atmosphere->ref, atmosphere_release);
    115   return RES_OK;
    116 }
    117 
    118 res_T
    119 ssol_atmosphere_set_extinction
    120   (struct ssol_atmosphere* atmosphere,
    121    struct ssol_data* extinction)
    122 {
    123   if(!atmosphere || !extinction || !check_extinction(extinction))
    124     return RES_BAD_ARG;
    125   ssol_data_copy(&atmosphere->extinction, extinction);
    126   return RES_OK;
    127 }
    128