1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
/*----------------------------------------------------------------------------*/
/* */
/* LIBMESHB-HELPERS V0.91 */
/* */
/*----------------------------------------------------------------------------*/
/* */
/* Description: Set of helpers functions useful for the libMeshb */
/* Author: Loic MARECHAL */
/* Creation date: mar 24 2021 */
/* Last modification: nov 05 2021 */
/* */
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/* Includes */
/*----------------------------------------------------------------------------*/
#include "libmeshb7_helpers.h"
#include "libmeshb7.h"
#include <stdio.h>
#include <stdlib.h>
/*----------------------------------------------------------------------------*/
/* Defines */
/*----------------------------------------------------------------------------*/
#ifdef INT64
#define itg int64_t
#else
#define itg int32_t
#endif
#ifdef REAL32
#define fpn float
#else
#define fpn double
#endif
/*----------------------------------------------------------------------------*/
/* Defintion of macro commands */
/*----------------------------------------------------------------------------*/
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define POW(a) ((a) * (a))
/*----------------------------------------------------------------------------*/
/* Allocate a structure to hold all polygons and polyhedra related data */
/*----------------------------------------------------------------------------*/
PolMshSct *GmfAllocatePolyhedralStructure(int64_t MshIdx)
{
int SrfFlg = 0, VolFlg = 0, ErrFlg = 0;
itg NmbBndHdr, NmbBndVer, NmbInrHdr, NmbInrVer, NmbVolHdr, NmbVolFac;
PolMshSct *pol;
if (!MshIdx)
return (NULL);
// Get all the polyhedra related field's sizes
NmbBndHdr = (itg)GmfStatKwd(MshIdx, GmfBoundaryPolygonHeaders);
NmbBndVer = (itg)GmfStatKwd(MshIdx, GmfBoundaryPolygonVertices);
NmbInrHdr = (itg)GmfStatKwd(MshIdx, GmfInnerPolygonHeaders);
NmbInrVer = (itg)GmfStatKwd(MshIdx, GmfInnerPolygonVertices);
NmbVolHdr = (itg)GmfStatKwd(MshIdx, GmfPolyhedraHeaders);
NmbVolFac = (itg)GmfStatKwd(MshIdx, GmfPolyhedraFaces);
// There are surface polygons
if (NmbBndHdr && NmbBndVer)
SrfFlg = 1;
// There are volume polyhedra
if (NmbInrHdr && NmbInrVer && NmbVolHdr && NmbVolFac)
VolFlg = 1;
if (!SrfFlg && !VolFlg)
return (NULL);
pol = malloc(sizeof(PolMshSct));
if (!pol)
return (NULL);
pol->MshIdx = MshIdx;
// Store surface information and allocate Headers and data tables
if (SrfFlg)
{
pol->NmbBndHdr = NmbBndHdr;
pol->NmbBndVer = NmbBndVer;
pol->BndHdrTab = malloc((NmbBndHdr + 1) * 2 * sizeof(itg));
pol->BndVerTab = malloc((NmbBndVer + 1) * sizeof(itg));
if (!pol->BndHdrTab || !pol->BndVerTab)
ErrFlg = 1;
}
else
{
pol->NmbBndHdr = 0;
pol->NmbBndVer = 0;
}
// Store volume information and allocate Headers and data tables
// for both inner polygons and polyhedra
if (VolFlg)
{
pol->NmbInrHdr = NmbInrHdr;
pol->NmbInrVer = NmbInrVer;
pol->NmbVolHdr = NmbVolHdr;
pol->NmbVolFac = NmbVolFac;
pol->InrHdrTab = malloc((NmbInrHdr + 1) * 2 * sizeof(itg));
pol->InrVerTab = malloc((NmbInrVer + 1) * sizeof(itg));
pol->VolHdrTab = malloc((NmbVolHdr + 1) * 2 * sizeof(itg));
pol->VolFacTab = malloc((NmbVolFac + 1) * sizeof(itg));
if (!pol->InrHdrTab || !pol->InrVerTab || !pol->VolHdrTab || !pol->VolFacTab)
ErrFlg = 1;
}
else
{
pol->NmbInrHdr = 0;
pol->NmbInrVer = 0;
pol->NmbVolHdr = 0;
pol->NmbVolFac = 0;
}
// If something went wrong, free all memories
if (ErrFlg)
{
GmfFreePolyghedralStructure(pol);
return (NULL);
}
return (pol);
}
/*----------------------------------------------------------------------------*/
/* Free all polyhedral tables and the head structure itself */
/*----------------------------------------------------------------------------*/
void GmfFreePolyghedralStructure(PolMshSct *pol)
{
if (!pol)
return;
if (pol->BndHdrTab)
free(pol->BndHdrTab);
if (pol->BndVerTab)
free(pol->BndVerTab);
if (pol->InrHdrTab)
free(pol->InrHdrTab);
if (pol->InrVerTab)
free(pol->InrVerTab);
if (pol->VolHdrTab)
free(pol->VolHdrTab);
if (pol->VolFacTab)
free(pol->VolFacTab);
free(pol);
}
/*----------------------------------------------------------------------------*/
/* Read the surface polygons from an already opened mesh file */
/* and store the data in the provided polyhedral structure */
/*----------------------------------------------------------------------------*/
int GmfReadBoundaryPolygons(PolMshSct *pol)
{
int ret;
if (!pol || !pol->BndHdrTab || !pol->BndVerTab)
return (0);
ret = GmfGetBlock(pol->MshIdx, GmfBoundaryPolygonHeaders,
1, pol->NmbBndHdr, 0, NULL, NULL, GmfIntVec, 2,
pol->BndHdrTab[1], pol->BndHdrTab[pol->NmbBndHdr]);
if (!ret)
return (0);
ret = GmfGetBlock(pol->MshIdx, GmfBoundaryPolygonVertices,
1, pol->NmbBndVer, 0, NULL, NULL, GmfInt,
&pol->BndVerTab[1], &pol->BndVerTab[pol->NmbBndVer]);
if (!ret)
return (0);
return (1);
}
/*----------------------------------------------------------------------------*/
/* Read the polyhedra from an already opened mesh file */
/* and store the data in the provided polyhedral structure */
/*----------------------------------------------------------------------------*/
int GmfReadPolyhedra(PolMshSct *pol)
{
int ret;
if (!pol || !pol->InrHdrTab || !pol->InrVerTab || !pol->VolHdrTab || !pol->VolFacTab)
{
return (0);
}
ret = GmfGetBlock(pol->MshIdx, GmfInnerPolygonHeaders,
1, pol->NmbInrHdr, 0, NULL, NULL, GmfIntVec, 2,
pol->InrHdrTab[1], pol->InrHdrTab[pol->NmbInrHdr]);
if (!ret)
return (0);
ret = GmfGetBlock(pol->MshIdx, GmfInnerPolygonVertices,
1, pol->NmbInrVer, 0, NULL, NULL, GmfInt,
&pol->InrVerTab[1], &pol->InrVerTab[pol->NmbInrVer]);
if (!ret)
return (0);
ret = GmfGetBlock(pol->MshIdx, GmfPolyhedraHeaders,
1, pol->NmbVolHdr, 0, NULL, NULL, GmfIntVec, 2,
pol->VolHdrTab[1], pol->VolHdrTab[pol->NmbVolHdr]);
if (!ret)
return (0);
ret = GmfGetBlock(pol->MshIdx, GmfPolyhedraFaces,
1, pol->NmbVolFac, 0, NULL, NULL, GmfInt,
&pol->VolFacTab[1], &pol->VolFacTab[pol->NmbVolFac]);
if (!ret)
return (0);
return (1);
}
/*----------------------------------------------------------------------------*/
/* Copy one surface polygon list of vertices in the table and return its size */
/*----------------------------------------------------------------------------*/
itg GmfGetBoundaryPolygon(PolMshSct *pol, itg EleIdx, itg *UsrTab)
{
itg i, BegIdx, EndIdx;
if (!pol || (EleIdx <= 0) || (EleIdx > pol->NmbBndHdr))
return (0);
BegIdx = pol->BndHdrTab[EleIdx][0];
if (EleIdx == pol->NmbBndHdr)
EndIdx = pol->NmbBndVer;
else
EndIdx = pol->BndHdrTab[EleIdx + 1][0] - 1;
EndIdx = MIN(EndIdx, BegIdx + 256);
for (i = BegIdx; i <= EndIdx; i++)
UsrTab[i - BegIdx] = pol->BndVerTab[i];
return (EndIdx - BegIdx + 1);
}
/*----------------------------------------------------------------------------*/
/* Copy one inner polygon list of vertices in the table and return its size */
/*----------------------------------------------------------------------------*/
itg GmfGetInnerPolygon(PolMshSct *pol, itg EleIdx, itg *UsrTab)
{
itg i, BegIdx, EndIdx, ord = 1;
if(EleIdx < 0)
{
EleIdx = -EleIdx;
ord = -1;
}
if (!pol || (EleIdx <= 0) || (EleIdx > pol->NmbInrHdr))
return (0);
BegIdx = pol->InrHdrTab[EleIdx][0];
if (EleIdx == pol->NmbInrHdr)
EndIdx = pol->NmbInrVer;
else
EndIdx = pol->InrHdrTab[EleIdx + 1][0] - 1;
EndIdx = MIN(EndIdx, BegIdx + 256);
if(ord == 1)
for (i = BegIdx; i <= EndIdx; i++)
UsrTab[i - BegIdx] = pol->InrVerTab[i];
else
for (i = EndIdx; i >= BegIdx; i--)
UsrTab[EndIdx - i] = pol->InrVerTab[i];
return (EndIdx - BegIdx + 1);
}
/*----------------------------------------------------------------------------*/
/* Copy one polyhedra list of polygonal faces in the table and return its size*/
/*----------------------------------------------------------------------------*/
itg GmfGetPolyhedron(PolMshSct *pol, itg EleIdx, itg *UsrTab)
{
itg i, BegIdx, EndIdx;
if (!pol || (EleIdx <= 0) || (EleIdx > pol->NmbVolHdr))
return (0);
BegIdx = pol->VolHdrTab[EleIdx][0];
if (EleIdx == pol->NmbVolHdr)
EndIdx = pol->NmbVolFac;
else
EndIdx = pol->VolHdrTab[EleIdx + 1][0] - 1;
EndIdx = MIN(EndIdx, BegIdx + 256);
for (i = BegIdx; i <= EndIdx; i++)
UsrTab[i - BegIdx] = pol->VolFacTab[i];
return (EndIdx - BegIdx + 1);
}
/*----------------------------------------------------------------------------*/
/* Create the tessellation in triangles of a polygon */
/* Output: number of triangles, list of trangles and visibility of egdes */
/*----------------------------------------------------------------------------*/
itg GmfTesselatePolygon(PolMshSct *pol, itg EleIdx, itg (*Tri)[3], itg (*VisEdg)[3])
{
itg NmbTri, deg1, i, buf1[256];
deg1 = GmfGetBoundaryPolygon(pol, EleIdx, buf1);
NmbTri = deg1 - 2;
//-- First triangle
Tri[0][0] = buf1[0];
Tri[0][1] = buf1[1];
Tri[0][2] = buf1[2];
VisEdg[0][0] = 1;
VisEdg[0][1] = 0;
VisEdg[0][2] = 1;
//-- Other triangles
for (i = 1; i < NmbTri - 1; i++)
{
Tri[i][0] = buf1[0];
Tri[i][1] = buf1[i + 1];
Tri[i][2] = buf1[i + 2];
VisEdg[i][0] = 1;
VisEdg[i][1] = 0;
VisEdg[i][2] = 0;
}
//-- Last triangle
Tri[NmbTri - 1][0] = buf1[0];
Tri[NmbTri - 1][1] = buf1[deg1 - 2];
Tri[NmbTri - 1][2] = buf1[deg1 - 1];
VisEdg[NmbTri - 1][0] = 1;
VisEdg[NmbTri - 1][1] = 1;
VisEdg[NmbTri - 1][2] = 0;
// for (i = 0; i < NmbTri; i++) {
// printf("GmfTesselatePolygon: Tri[%d] %d %d %d \n", i, Tri[i][0], Tri[i][1], Tri[i][2]);
// printf("GmfTesselatePolygon: VisEdg[%d] %d %d %d \n", i, VisEdg[i][0], VisEdg[i][1], VisEdg[i][2]);
// }
return NmbTri;
}
/*----------------------------------------------------------------------------*/
/* Return the percentage of cache hit while doing element to node accesses */
/*----------------------------------------------------------------------------*/
float GmfEvaluateNumbering(int NmbEle, int NmbNod, int *PtrEle, int *PtrEnd)
{
int i, j, EleStp, HshTab[256] = {0}, NmbHit = 0;
if(!NmbEle || !NmbNod || !PtrEle || !PtrEnd || (PtrEnd < PtrEle))
return(0.);
// Compute the user data structure stride between two lines of elements
EleStp = (NmbEle > 1) ? (PtrEnd - PtrEle) / (NmbEle - 1) : 0;
// Simulate the search and insert in a 256 entries hash table
for(i=0;i<NmbEle;i++)
{
// Get the next element's pointer on its nodes
PtrEle += EleStp;
// Search for each node in the hash table:
// if its found, increase the hit counter and insert it otherwise
for(j=0;j<NmbNod;j++)
if(HshTab[ PtrEle[j] & 0xff ] == PtrEle[j])
NmbHit++;
else
HshTab[ PtrEle[j] & 0xff ] = PtrEle[j];
}
// Return the percentage of cache hits
return( (100. * NmbHit) / (NmbEle * NmbNod) );
}
|