star-phor

Radiative transfer solver for photoreactors.
git clone https://www.edstar.cnrs.fr/git/star-phor.git
Log | Files | Refs | README | LICENSE

sphor_ran_geometry.c (3008B)


      1 /* Copyright (C) 2024-2026 Centre National de la Recherche Scientifique
      2  * Copyright (C) 2024-2026 Clermont Auvergne INP
      3  * Copyright (C) 2024-2026 INSA Lyon
      4  * Copyright (C) 2024-2026 Institut Mines Télécom Albi-Carmaux
      5  * Copyright (C) 2024-2026 Institut National Polytechnique de Toulouse
      6  * Copyright (C) 2024-2026 |Méso|Star> (contact@meso-star.com)
      7  * Copyright (C) 2024-2026 PhotonLyX (info@photonlyx.com)
      8  * Copyright (C) 2024-2026 Université de Lorraine
      9  * Copyright (C) 2024-2026 Université Paul Sabatier
     10  * Copyright (C) 2024-2026 Université Toulouse - Jean Jaurès
     11  *
     12  * This program is free software: you can redistribute it and/or modify
     13  * it under the terms of the GNU General Public License as published by
     14  * the Free Software Foundation, either version 3 of the License, or
     15  * (at your option) any later version.
     16  *
     17  * This program is distributed in the hope that it will be useful,
     18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     20  * GNU General Public License for more details.
     21  *
     22  * You should have received a copy of the GNU General Public License
     23  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     24 
     25 #include "sphor_ran_geometry.h"
     26 
     27 #include <rsys/double3.h>
     28 #include <rsys/rsys.h>
     29 #include <star/s3d.h>
     30 #include <star/ssp.h>
     31 
     32 #include <math.h>
     33 
     34 /*******************************************************************************
     35  * Local functions
     36  ******************************************************************************/
     37 /* Compute the rotation of the vector 'vec' by an angle of 'theta' radians in
     38  * the axis 'axis' using the Rodrigues' rotation formula:
     39  * https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula */
     40 res_T
     41 rotate_vector
     42   (double* vrot,
     43    double* vec,
     44    double* axis,
     45    double theta)
     46 {
     47   res_T res = RES_OK;
     48 
     49   double cos_theta = cos(theta);
     50   double sin_theta = sin(theta);
     51   double dot;
     52   double cross[3];
     53   double tmp1[3];
     54   double tmp2[3];
     55   double tmp3[3];
     56 
     57   dot = d3_dot(axis, vec);
     58   d3_cross(cross, axis, vec);
     59 
     60   /* Compute the three terms of the Rodrigues' rotation formula */
     61   d3_muld(tmp1, axis, (1-cos_theta) * dot);
     62   d3_muld(tmp2, cross, sin_theta);
     63   d3_muld(tmp3, vec, cos_theta);
     64 
     65   /* Sum the three terms of the formula */
     66   d3_add(vrot, tmp1, tmp2);
     67   d3_add(vrot, vrot, tmp3);
     68 
     69   return res;
     70 }
     71 
     72 res_T
     73 sample_surface_position
     74   (struct sphor* sphor,
     75    struct ssp_rng* rng,
     76    struct s3d_scene_view* view,
     77    struct s3d_primitive* primitive,
     78    float st[2])
     79 {
     80   float u = 0;
     81   float v = 0;
     82   float w = 0;
     83   float uv[2];
     84   res_T res = RES_OK;
     85 
     86   ASSERT(NULL != rng);
     87   ASSERT(NULL != sphor);
     88   ASSERT(NULL != view);
     89 
     90   (void)sphor;
     91 
     92   u = (float)ssp_rng_canonical(rng);
     93   v = (float)ssp_rng_canonical(rng);
     94   w = (float)ssp_rng_canonical(rng);
     95 
     96   res = s3d_scene_view_sample(view, u, v, w, primitive, uv);
     97   if (RES_OK != res) { goto error; }
     98 
     99   st[0] = uv[0];
    100   st[1] = uv[1];
    101 
    102 exit:
    103   return res;
    104 error:
    105   goto exit;
    106 }