rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

real22.h (2282B)


      1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr)
      2  *
      3  * The RSys library is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published
      5  * by the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * The RSys library is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #ifndef REAL_TYPE__
     17   #error Missing arguments
     18 #endif
     19 
     20 /* Generate common realXY funcs */
     21 #define REALX_DIMENSION__ 2
     22 #define REALY_DIMENSION__ 2
     23 #include "realXY_begin.h"
     24 #include "realXY.h"
     25 
     26 /* Specific real22 funcs */
     27 static FINLINE REAL_TYPE__*
     28 REALXY_CTOR__
     29   (REAL_TYPE__* dst,
     30    const REAL_TYPE__ a,
     31    const REAL_TYPE__ b,
     32    const REAL_TYPE__ c,
     33    const REAL_TYPE__ d)
     34 {
     35   ASSERT(dst);
     36   dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
     37   return dst;
     38 }
     39 
     40 static FINLINE REAL_TYPE__
     41 REALXY_FUNC__(det)(const REAL_TYPE__* mat)
     42 {
     43   return mat[0] * mat[3] - mat[2] * mat[1];
     44 }
     45 
     46 static FINLINE REAL_TYPE__
     47 REALXY_FUNC__(inverse)(REAL_TYPE__* dst, const REAL_TYPE__* mat)
     48 {
     49   REAL_TYPE__ det, rcp_det, mat0;
     50   ASSERT(dst && mat);
     51   det = REALXY_FUNC__(det)(mat);
     52   rcp_det = 1.f / det;
     53   mat0 = mat[0];
     54   dst[0] =  mat[3] * rcp_det;
     55   dst[1] = -mat[1] * rcp_det;
     56   dst[2] = -mat[2] * rcp_det;
     57   dst[3] =  mat0   * rcp_det;
     58   return det;
     59 }
     60 
     61 static FINLINE REAL_TYPE__
     62 REALXY_FUNC__(invtrans)(REAL_TYPE__* dst, const REAL_TYPE__* mat)
     63 {
     64   REAL_TYPE__ det, dst1, dst2;
     65   ASSERT(dst && mat);
     66   det = REALXY_FUNC__(inverse)(dst, mat);
     67   dst1 = dst[1];
     68   dst2 = dst[2];
     69   dst[1] = dst2;
     70   dst[2] = dst1;
     71   return det;
     72 }
     73 
     74 static FINLINE REAL_TYPE__*
     75 REALXY_FUNC__(rotation)
     76   (REAL_TYPE__ dst[4], const REAL_TYPE__ angle/*in radian*/)
     77 {
     78   const REAL_TYPE__ c = (REAL_TYPE__)cos((double)angle);
     79   const REAL_TYPE__ s = (REAL_TYPE__)sin((double)angle);
     80   ASSERT(dst);
     81   dst[0] = c; dst[1] = s;
     82   dst[2] =-s; dst[3] = c;
     83   return dst;
     84 }
     85 
     86 #include "realXY_end.h"