rsys

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

test_real33.h (10167B)


      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 #include <math.h>
     19 
     20 #define REALX_DIMENSION__ 3
     21 #define REALY_DIMENSION__ 3
     22 #include "realXY_begin.h"
     23 
     24 #define REAL REAL_TYPE__
     25 #define REAL_COMPATIBLE REAL_TYPE_COMPATIBLE__
     26 
     27 #define CHECK_REAL3(A, B)                                                      \
     28   {                                                                            \
     29     const REAL* a__ = (A);                                                     \
     30     const REAL* b__ = (B);                                                     \
     31     int i__;                                                                   \
     32     FOR_EACH(i__, 0, 3)                                                        \
     33       CHK(a__[i__] == b__[i__]);                                               \
     34   } (void)0
     35 #define CHECK_REAL33(A, B)                                                     \
     36   {                                                                            \
     37     const REAL* a__ = (A);                                                     \
     38     const REAL* b__ = (B);                                                     \
     39     int i__;                                                                   \
     40     FOR_EACH(i__, 0, 9)                                                        \
     41       CHK(a__[i__] == b__[i__]);                                               \
     42   } (void)0
     43 
     44 int
     45 main(int argc, char** argv)
     46 {
     47   REAL a[9], b[9], dst[9], c[9];
     48   REAL_COMPATIBLE d[9];
     49   int i;
     50   (void)argc, (void)argv;
     51 
     52   REALXY_FUNC__(set)(a, REALXY_FUNC__(splat)(c, -1.0));
     53   FOR_EACH(i, 0, 9) {
     54     CHK(a[i] == -1.0);
     55     CHK(c[i] == -1.0);
     56   }
     57   REALXY_CTOR__(a, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
     58   FOR_EACH(i, 0, 9) {
     59     CHK(a[i] == (REAL)i);
     60   }
     61   CHECK_REAL33
     62     (REALXY_FUNC__(set_identity)(a),
     63      REALXY_CTOR__(c, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0));
     64   CHECK_REAL33(a, REALXY_CTOR__(c, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0));
     65   CHK(REALXY_FUNC__(is_identity)(a) == 1);
     66 
     67   REALXY_FUNC__(set)(c, a);
     68   FOR_EACH(i, 0, REALX_DIMENSION__*REALY_DIMENSION__) {
     69     REAL_TYPE__ r = c[i];
     70     c[i] = c[i] + REAL_EPSILON__;
     71     CHK(REALXY_FUNC__(is_identity)(c) == 0);
     72     c[i] = r;
     73   }
     74 
     75   d[0] = (REAL_COMPATIBLE)0.1;
     76   d[1] = (REAL_COMPATIBLE)(1.0/3.0);
     77   d[2] = (REAL_COMPATIBLE)0.3;
     78   d[3] = (REAL_COMPATIBLE)-0.7;
     79   d[4] = (REAL_COMPATIBLE)0.9;
     80   d[5] = (REAL_COMPATIBLE)-0.41;
     81   d[6] = (REAL_COMPATIBLE)0.22;
     82   d[7] = (REAL_COMPATIBLE)-0.01;
     83   d[8] = (REAL_COMPATIBLE)0.02;
     84 
     85   REALXY_CTOR__(c,
     86     (REAL)(REAL_COMPATIBLE)0.1,
     87     (REAL)(REAL_COMPATIBLE)(1.0/3.0),
     88     (REAL)(REAL_COMPATIBLE)0.3,
     89     (REAL)(REAL_COMPATIBLE)-0.7,
     90     (REAL)(REAL_COMPATIBLE)0.9,
     91     (REAL)(REAL_COMPATIBLE)-0.41,
     92     (REAL)(REAL_COMPATIBLE)0.22,
     93     (REAL)(REAL_COMPATIBLE)-0.01,
     94     (REAL)(REAL_COMPATIBLE)0.02);
     95   CHECK_REAL33(REALXY_CAST__(dst, d), c);
     96 
     97   REALXY_FUNC__(splat)(a, -1.0);
     98   CHECK_REAL33
     99     (REALXY_FUNC__(set_row)(a, REALX_CTOR__(c, 0.0, 1.0, 2.0), 0),
    100      REALXY_CTOR__(c, 0.0, -1.0, -1.0, 1.0, -1.0, -1.0, 2.0, -1.0, -1.0));
    101   CHECK_REAL33(a, REALXY_CTOR__(c, 0.0, -1.0, -1.0, 1.0, -1.0, -1.0, 2.0, -1.0, -1.0));
    102   CHECK_REAL33
    103     (REALXY_FUNC__(set_row)(a, REALX_CTOR__(c, 3.0, 4.0, 5.0), 1),
    104      REALXY_CTOR__(c, 0.0, 3.0, -1.0, 1.0, 4.0, -1.0, 2.0, 5.0, -1.0));
    105   CHECK_REAL33(a, REALXY_CTOR__(c, 0.0, 3.0, -1.0, 1.0, 4.0, -1.0, 2.0, 5.0, -1.0));
    106   CHECK_REAL33
    107     (REALXY_FUNC__(set_row)(a, REALX_CTOR__(c, 6.0, 7.0, 8.0), 2),
    108      REALXY_CTOR__(c, 0.0, 3.0, 6.0, 1.0, 4.0, 7.0, 2.0, 5.0, 8.0));
    109   CHECK_REAL33(a, REALXY_CTOR__(c, 0.0, 3.0, 6.0, 1.0, 4.0, 7.0, 2.0, 5.0, 8.0));
    110 
    111   CHECK_REAL33
    112     (REALXY_FUNC__(transpose)(a, a),
    113      REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0));
    114   CHECK_REAL33(a, REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0));
    115   CHECK_REAL33
    116     (REALXY_FUNC__(transpose)(b, a),
    117      REALXY_CTOR__(c, 0.0, 3.0, 6.0, 1.0, 4.0, 7.0, 2.0, 5.0, 8.0));
    118   CHECK_REAL33(b, REALXY_CTOR__(c, 0.0, 3.0, 6.0, 1.0, 4.0, 7.0, 2.0, 5.0, 8.0));
    119 
    120   REALXY_FUNC__(splat)(a, -1.0);
    121   CHECK_REAL33
    122     (REALXY_FUNC__(set_col)(a, REALX_CTOR__(c, 0.0, 1.0, 2.0), 0),
    123      REALXY_CTOR__(c, 0.0, 1.0, 2.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0));
    124   CHECK_REAL33(a, REALXY_CTOR__(c, 0.0, 1.0, 2.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0));
    125   CHECK_REAL33
    126     (REALXY_FUNC__(set_col)(a, REALX_CTOR__(c, 3.0, 4.0, 5.0), 1),
    127      REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, -1.0, -1.0, -1.0));
    128   CHECK_REAL33(a, REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, -1.0, -1.0, -1.0));
    129   CHECK_REAL33
    130     (REALXY_FUNC__(set_col)(a, REALX_CTOR__(c, 6.0, 7.0, 8.0), 2),
    131      REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0));
    132   CHECK_REAL33(a, REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0));
    133 
    134   CHECK_REAL3(REALXY_FUNC__(row)(b, a, 0), REALX_CTOR__(c, 0.0, 3.0, 6.0));
    135   CHECK_REAL3(b, REALX_CTOR__(c, 0.0, 3.0, 6.0));
    136   CHECK_REAL3(REALXY_FUNC__(row)(b, a, 1), REALX_CTOR__(c, 1.0, 4.0, 7.0));
    137   CHECK_REAL3(b, REALX_CTOR__(c, 1.0, 4.0, 7.0));
    138   CHECK_REAL3(REALXY_FUNC__(row)(b, a, 2), REALX_CTOR__(c, 2.0, 5.0, 8.0));
    139   CHECK_REAL3(b, REALX_CTOR__(c, 2.0, 5.0, 8.0));
    140 
    141   CHECK_REAL3(REALXY_FUNC__(col)(b + 2, a, 0), REALX_CTOR__(c, 0.0, 1.0, 2.0));
    142   CHECK_REAL3(b + 2, REALX_CTOR__(c, 0.0, 1.0, 2.0));
    143   CHECK_REAL3(REALXY_FUNC__(col)(b + 1, a, 1), REALX_CTOR__(c, 3.0, 4.0, 5.0));
    144   CHECK_REAL3(b + 1, REALX_CTOR__(c, 3.0, 4.0, 5.0));
    145   CHECK_REAL3(REALXY_FUNC__(col)(b + 5, a, 2), REALX_CTOR__(c, 6.0, 7.0, 8.0));
    146   CHECK_REAL3(b + 5, REALX_CTOR__(c, 6.0, 7.0, 8.0));
    147 
    148   CHECK_REAL3(REALXY_FUNC__(col_ptr)(a, 0), REALX_CTOR__(c, 0.0, 1.0, 2.0));
    149   CHECK_REAL3(REALXY_FUNC__(col_ptr)(a, 1), REALX_CTOR__(c, 3.0, 4.0, 5.0));
    150   CHECK_REAL3(REALXY_FUNC__(col_ptr)(a, 2), REALX_CTOR__(c, 6.0, 7.0, 8.0));
    151   CHECK_REAL3(REALXY_FUNC__(col_cptr)(a, 0), REALX_CTOR__(c, 0.0, 1.0, 2.0));
    152   CHECK_REAL3(REALXY_FUNC__(col_cptr)(a, 1), REALX_CTOR__(c, 3.0, 4.0, 5.0));
    153   CHECK_REAL3(REALXY_FUNC__(col_cptr)(a, 2), REALX_CTOR__(c, 6.0, 7.0, 8.0));
    154 
    155   REALXY_CTOR__(a, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
    156   CHECK_REAL3(REALXY_REALX_FUNC__(mul)(dst, a, REALX_CTOR__(c, 1.0, 2.0, 3.0)),
    157     REALX_CTOR__(c, 30.0, 36.0, 42.0));
    158   CHECK_REAL3(dst, REALX_CTOR__(c, 30.0, 36.0, 42.0));
    159   CHECK_REAL3(REALX_REALXY_FUNC__(mul)(dst, REALX_CTOR__(c, 1.0, 2.0, 3.0), a),
    160     REALX_CTOR__(c, 14.0, 32.0, 50.0));
    161   CHECK_REAL3(dst, REALX_CTOR__(c, 14.0, 32.0, 50.0));
    162   CHECK_REAL3
    163     (REALXY_FUNC__(mul)(dst, a, -1.0),
    164      REALXY_CTOR__(c, -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, -8.0, -9.0));
    165   CHECK_REAL33(dst,
    166     REALXY_CTOR__(c, -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, -8.0, -9.0));
    167 
    168   REALXY_CTOR__(b, 2.0, 9.0, 8.0, 1.0, -2.0, 2.0, 1.0, -8.0, -4.0);
    169   CHECK_REAL33
    170     (REALXY_REALXY_FUNC__(mul)(dst, a, b),
    171      REALXY_CTOR__(c, 94.0, 113.0, 132.0, 7.0, 8.0, 9.0, -59.0, -70.0,-81.0));
    172 	CHECK_REAL33(dst,
    173     REALXY_CTOR__(c, 94.0, 113.0, 132.0, 7.0, 8.0, 9.0, -59.0, -70.0,-81.0));
    174   CHECK_REAL33
    175     (REALXY_REALXY_FUNC__(mul)(a, a, b),
    176      REALXY_CTOR__(c, 94.0, 113.0, 132.0, 7.0, 8.0, 9.0, -59.0, -70.0, -81.0));
    177 	CHECK_REAL33(a,
    178     REALXY_CTOR__(c, 94.0, 113.0, 132.0, 7.0, 8.0, 9.0, -59.0, -70.0, -81.0));
    179   REALXY_CTOR__(a, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
    180   CHECK_REAL33
    181     (REALXY_REALXY_FUNC__(mul)(b, a, b),
    182      REALXY_CTOR__(c, 94.0, 113.0, 132.0, 7.0, 8.0, 9.0, -59.0, -70.0, -81.0));
    183 	CHECK_REAL33(b,
    184     REALXY_CTOR__(c, 94.0, 113.0, 132.0, 7.0, 8.0, 9.0, -59.0, -70.0, -81.0));
    185   REALXY_CTOR__(b, 2.0, 9.0, 8.0, 1.0, -2.0, 2.0, 1.0, -8.0, -4.0);
    186 
    187   REALXY_CTOR__(a, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);
    188   REALXY_CTOR__(b, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
    189   CHECK_REAL33
    190     (REALXY_FUNC__(add)(dst, a, b),
    191      REALXY_CTOR__(c, 1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0));
    192   CHECK_REAL33(dst,
    193     REALXY_CTOR__(c, 1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0));
    194   CHECK_REAL33
    195     (REALXY_FUNC__(sub)(dst, dst, b),
    196      REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0));
    197 	CHECK_REAL33(dst,
    198     REALXY_CTOR__(c, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0));
    199   CHECK_REAL33
    200     (REALXY_FUNC__(minus)(a, b),
    201      REALXY_CTOR__(c,  -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, -8.0, -9.0));
    202   CHECK_REAL33(a,
    203     REALXY_CTOR__(c,  -1.0, -2.0, -3.0, -4.0, -5.0, -6.0, -7.0, -8.0, -9.0));
    204 
    205   REALXY_FUNC__(set)(a, b);
    206   CHK(REALXY_FUNC__(eq)(a, b) == 1);
    207   REALXY_FUNC__(add)(a, a, REALXY_FUNC__(splat)(c, FLT_EPSILON));
    208   CHK(REALXY_FUNC__(eq)(a, b) == 0);
    209   CHK(REALXY_FUNC__(eq_eps)(a, b, FLT_EPSILON) == 1);
    210   CHK(REALXY_FUNC__(eq_eps)(a, b, FLT_EPSILON * (REAL)0.9) == 0);
    211 
    212   REALXY_CTOR__(a, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 3.0, -4.0, 9.0);
    213   CHK(REALXY_FUNC__(det)(a) == -60.0);
    214   CHK(REALXY_FUNC__(inverse)(b, a) == -60.0);
    215   REALXY_REALXY_FUNC__(mul)(dst, a, b);
    216   CHK(REALXY_FUNC__(eq_eps)(dst,
    217     REALXY_CTOR__(c, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0), 1.e-6f));
    218   CHK(REALXY_FUNC__(invtrans)(a, a) == -60.0);
    219   CHECK_REAL33
    220     (a, REALXY_CTOR__(c, b[0], b[3], b[6], b[1], b[4], b[7], b[2], b[5], b[8]));
    221 
    222   REALX_CTOR__(c, (REAL)0.66666667, (REAL)0.33333333, (REAL)0.66666667);
    223   REALXY_FUNC__(rotation_axis_angle)(a, c, (REAL)0.349066);
    224   CHK(REALXY_FUNC__(eq_eps)(a, REALXY_CTOR__(c,
    225     (REAL)0.966496, (REAL)0.241415, (REAL)-0.0872034,
    226     (REAL)-0.214612, (REAL)0.946393, (REAL)0.241415,
    227     (REAL)0.14081, (REAL)-0.214612, (REAL)0.966496), (REAL)1.e-6));
    228 
    229   return 0;
    230 }