sdis_primkey.c (3355B)
1 /* Copyright (C) 2016-2025 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "sdis.h" 17 18 #include <rsys/double2.h> 19 #include <rsys/double3.h> 20 21 /******************************************************************************* 22 * Helper functions 23 ******************************************************************************/ 24 static INLINE int 25 cmp_dbl2(const double v0[2], const double v1[2]) 26 { 27 if(v0[0] < v1[0]) return -1; 28 if(v0[0] > v1[0]) return +1; 29 if(v0[1] < v1[1]) return -1; 30 if(v0[1] > v1[1]) return +1; 31 return 0; 32 } 33 34 static INLINE int 35 cmp_dbl3(const double v0[3], const double v1[3]) 36 { 37 if(v0[0] < v1[0]) return -1; 38 if(v0[0] > v1[0]) return +1; 39 if(v0[1] < v1[1]) return -1; 40 if(v0[1] > v1[1]) return +1; 41 if(v0[2] < v1[2]) return -1; 42 if(v0[2] > v1[2]) return +1; 43 return 0; 44 } 45 46 /******************************************************************************* 47 * Exported functions 48 ******************************************************************************/ 49 void 50 sdis_primkey_2d_setup 51 (struct sdis_primkey* key, 52 const double node0[2], 53 const double node1[2]) 54 { 55 double *v0, *v1; 56 ASSERT(key && node0 && node1); 57 v0 = d2_set(key->nodes+0, node0); 58 v1 = d2_set(key->nodes+2, node1); 59 if(cmp_dbl2(v0, v1) > 0) { 60 SWAP(double, v0[0], v1[0]); 61 SWAP(double, v0[1], v1[1]); 62 } 63 key->ncoords = 4; 64 } 65 66 void 67 sdis_primkey_setup 68 (struct sdis_primkey* key, 69 const double node0[3], 70 const double node1[3], 71 const double node2[3]) 72 { 73 double *v0, *v1, *v2; 74 ASSERT(key && node0 && node1 && node2); 75 v0 = d3_set(key->nodes+0, node0); 76 v1 = d3_set(key->nodes+3, node1); 77 v2 = d3_set(key->nodes+6, node2); 78 79 /* Bubble sort */ 80 #define SWAP_DBL3(V0, V1) { \ 81 SWAP(double, (V0)[0], (V1)[0]); \ 82 SWAP(double, (V0)[1], (V1)[1]); \ 83 SWAP(double, (V0)[2], (V1)[2]); \ 84 } (void)0 85 if(cmp_dbl3(v0, v1) > 0) SWAP_DBL3(v0, v1); 86 if(cmp_dbl3(v1, v2) > 0) SWAP_DBL3(v1, v2); 87 if(cmp_dbl3(v0, v1) > 0) SWAP_DBL3(v0, v1); 88 #undef SWAP_DBL3 89 key->ncoords = 9; 90 } 91 92 size_t 93 sdis_primkey_hash(const struct sdis_primkey* key) 94 { 95 return hash_fnv64(key->nodes, sizeof(double)*key->ncoords); 96 } 97 98 char 99 sdis_primkey_eq 100 (const struct sdis_primkey* key0, 101 const struct sdis_primkey* key1) 102 { 103 unsigned i = 0; 104 ASSERT(key0 && key1); 105 106 if(key0->ncoords != key1->ncoords) return 0; 107 if(key0->ncoords != 4 && key0->ncoords != 9) return 0; 108 if(key1->ncoords != 4 && key1->ncoords != 9) return 0; 109 FOR_EACH(i, 0, key0->ncoords) { 110 if(key0->nodes[i] != key1->nodes[i]) return 0; 111 } 112 return 1; 113 }