rsys

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

test_real3.h (5554B)


      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 #include "rsys.h"
     17 #include <float.h>
     18 
     19 #define REALX_DIMENSION__ 3
     20 #include "realX_begin.h"
     21 #define REAL REAL_TYPE__
     22 #define REAL_COMPATIBLE REAL_TYPE_COMPATIBLE__
     23 
     24 #define CHECK_REAL3(a, b)                                                      \
     25   {                                                                            \
     26     REAL* a__ = (a);                                                           \
     27     REAL* b__ = (b);                                                           \
     28     CHK((a__)[0] == (b__)[0]);                                                \
     29     CHK((a__)[1] == (b__)[1]);                                                \
     30     CHK((a__)[2] == (b__)[2]);                                                \
     31   } (void)0
     32 
     33 int
     34 main(int argc, char** argv)
     35 {
     36   REAL a[3], b[3], dst[3], f, c[3];
     37   REAL_COMPATIBLE d[3];
     38   (void)argc, (void)argv;
     39 
     40   REALX_FUNC__(set)(a, REALX_FUNC__(splat)(c, -1.0));
     41   CHK(a[0] == -1.0);
     42   CHK(a[1] == -1.0);
     43   CHK(a[2] == -1.0);
     44   REALX_FUNC__(set)(a, REALX_CTOR__(c, 0.0, 1.0, 2.0));
     45   CHK(a[0] == 0.0);
     46   CHK(a[1] == 1.0);
     47   CHK(a[2] == 2.0);
     48   REALX_FUNC__(splat)(a, -2.0);
     49   CHK(a[0] == -2.0);
     50   CHK(a[1] == -2.0);
     51   CHK(a[2] == -2.0);
     52 
     53   REALX_FUNC__(set)(a, REALX_CTOR__(c, -1.0, 2.0, -3.0));
     54   CHECK_REAL3(REALX_FUNC__(minus)(b, a), REALX_CTOR__(c, 1.0, -2.0, 3.0));
     55   CHECK_REAL3(b, REALX_CTOR__(c, 1.0, -2.0, 3.0));
     56 
     57   d[0] = (REAL_COMPATIBLE)0.1;
     58   d[1] = (REAL_COMPATIBLE)(1.0/3.0);
     59   d[2] = (REAL_COMPATIBLE)0.3;
     60   REALX_CTOR__(c,
     61     (REAL)(REAL_COMPATIBLE)0.1,
     62     (REAL)(REAL_COMPATIBLE)(1.0/3.0),
     63     (REAL)(REAL_COMPATIBLE)0.3);
     64   CHECK_REAL3(REALX_CAST__(dst, d), c);
     65 
     66   CHECK_REAL3(REALX_FUNC__(add)(dst, a, b), REALX_FUNC__(splat)(c, 0.0));
     67   CHECK_REAL3(dst, REALX_FUNC__(splat)(c, 0.0));
     68   CHECK_REAL3(REALX_REAL_FUNC__(add)(dst, a, 1), REALX_CTOR__(c, 0.0, 3.0, -2.0));
     69   CHECK_REAL3(dst, REALX_CTOR__(c, 0.0, 3.0, -2.0));
     70   CHECK_REAL3(REALX_FUNC__(sub)(dst, a, b), REALX_CTOR__(c, -2.0, 4.0, -6.0));
     71   CHECK_REAL3(dst, REALX_CTOR__(c, -2.0, 4.0, -6.0));
     72   CHECK_REAL3(REALX_REAL_FUNC__(sub)(dst, a, 1), REALX_CTOR__(c, -2.0, 1.0, -4.0));
     73   CHECK_REAL3(dst, REALX_CTOR__(c, -2.0, 1.0, -4.0));
     74   CHECK_REAL3(REALX_REAL_FUNC__(mul)(dst, a, 2), REALX_CTOR__(c, -2.0, 4.0, -6.0));
     75   CHECK_REAL3(dst, REALX_CTOR__(c, -2.0, 4.0, -6.0));
     76   CHECK_REAL3(REALX_FUNC__(mul)(dst, a, b), REALX_CTOR__(c, -1.0, -4.0, -9.0));
     77   CHECK_REAL3(dst, REALX_CTOR__(c, -1.0, -4.0, -9.0));
     78   CHECK_REAL3(REALX_FUNC__(div)(dst, dst, a), REALX_CTOR__(c, 1.0, -2.0, 3.0));
     79   CHECK_REAL3(dst, REALX_CTOR__(c, 1.0, -2.0, 3.0));
     80   CHECK_REAL3(REALX_REAL_FUNC__(div)(dst, a, 2.0), REALX_CTOR__(c, -0.5, 1.0, -1.5));
     81   CHECK_REAL3(dst, REALX_CTOR__(c, -0.5, 1.0, -1.5));
     82 
     83   REALX_FUNC__(set)(a, REALX_CTOR__(c, 0.0, 1.0, 2.0));
     84   REALX_FUNC__(set)(b, REALX_CTOR__(c, 1.0, 2.0, -1.0));
     85   CHECK_REAL3(REALX_FUNC__(lerp)(dst, a, b, 0.5), REALX_CTOR__(c, 0.5, 1.5, 0.5));
     86   CHECK_REAL3(dst, REALX_CTOR__(c, 0.5, 1.5, 0.5));
     87   CHK(REALX_FUNC__(sum)(b) == 2.0);
     88   CHK(REALX_FUNC__(dot)(a, b) == 0.0);
     89   CHK(eq_eps(REALX_FUNC__(len)(a), sqrt(5.0), REAL_EPSILON__) == 1);
     90 
     91   CHK(REALX_FUNC__(is_normalized)(a) == 0);
     92   f = REALX_FUNC__(normalize)(dst, a);
     93   CHK(REALX_FUNC__(is_normalized)(a) == 0);
     94   CHK(REALX_FUNC__(is_normalized)(dst) !=  0);
     95   CHK(eq_eps(f, sqrt(5.0), REAL_EPSILON__) == 1);
     96   CHK(eq_eps(dst[0], a[0] / f, REAL_EPSILON__) == 1);
     97   CHK(eq_eps(dst[1], a[1] / f, REAL_EPSILON__) == 1);
     98   CHK(eq_eps(dst[2], a[2] / f, REAL_EPSILON__) == 1);
     99 
    100   CHK(REALX_FUNC__(eq)(a, a) == 1);
    101   CHK(REALX_FUNC__(eq)(a, b) == 0);
    102   CHK(REALX_FUNC__(eq)(a, REALX_CTOR__(c, b[0], a[1], a[2])) == 0);
    103   CHK(REALX_FUNC__(eq)(a, REALX_CTOR__(c, a[0], b[1], a[2])) == 0);
    104   CHK(REALX_FUNC__(eq)(a, REALX_CTOR__(c, a[0], a[1], b[2])) == 0);
    105 
    106   REALX_FUNC__(set)(b, a);
    107   REALX_FUNC__(add)(b, b, REALX_FUNC__(splat)(c, (REAL)(REAL_EPSILON__ * 0.5)));
    108   CHK(REALX_FUNC__(eq)(a, b) == 0);
    109   CHK(REALX_FUNC__(eq_eps)(a, b, REAL_EPSILON__) == 1);
    110   CHK(REALX_FUNC__(eq_eps)(a, b, (REAL)(REAL_EPSILON__ * 0.25)) == 0);
    111   REALX_FUNC__(set)(a, REALX_CTOR__(c, 1.0, -2.0, 2.0));
    112   REALX_FUNC__(set)(b, REALX_CTOR__(c, 3.0, 1.0, -1.5));
    113   CHECK_REAL3(REALX_FUNC__(cross)(dst, a, b), REALX_CTOR__(c, 1.0, 7.5, 7.0));
    114   CHECK_REAL3(dst, REALX_CTOR__(c, 1.0, 7.5, 7.0));
    115 
    116   REALX_CTOR__(a, 0.0, 1.0, 0.0);
    117   REALX_CTOR__(b, 0.0, 0.0, 1.0);
    118   CHECK_REAL3(REALX_FUNC__(cross)(a, a, b), REALX_CTOR__(c, 1.0, 0.0, 0.0));
    119 
    120   REALX_FUNC__(set)(a, REALX_CTOR__(c, 1.0, -2.0, 1.5));
    121   REALX_FUNC__(set)(b, REALX_CTOR__(c, -3.0, 1.0, -1.5));
    122   CHECK_REAL3(REALX_FUNC__(max)(dst, a, b), REALX_CTOR__(c, 1.0, 1.0, 1.5));
    123   CHECK_REAL3(dst, REALX_CTOR__(c, 1.0, 1.0, 1.5));
    124   CHECK_REAL3(REALX_FUNC__(min)(dst, a, b), REALX_CTOR__(c, -3.0, -2.0, -1.5));
    125   CHECK_REAL3(dst, REALX_CTOR__(c, -3.0, -2.0, -1.5));
    126   return 0;
    127 }