Skip to content

Commit

Permalink
Merge pull request #10 from cpmech/catch-tetgen-assert
Browse files Browse the repository at this point in the history
Catch tetgen assert
  • Loading branch information
cpmech authored Dec 30, 2024
2 parents f74a947 + 4b9b8b5 commit 5209811
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 205 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test_and_coverage.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Test & Coverage
on: [pull_request, push]
on: [pull_request]
jobs:
test_and_coverage:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions c_code/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ const int32_t TRITET_ERROR_INVALID_FACET_POINT_ID = 7000;
const int32_t TRITET_ERROR_INVALID_REGION_INDEX = 8000;
const int32_t TRITET_ERROR_INVALID_HOLE_INDEX = 9000;

const int32_t TRITET_ERROR_TETGEN_FAIL = 10000;

#endif // CONSTANTS_H
10 changes: 8 additions & 2 deletions c_code/interface_tetgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ int32_t tet_run_delaunay(struct ExtTetgen *tetgen, int32_t verbose) {
strcat(command, "Q");
}
try {
tetrahedralize(command, &tetgen->input, &tetgen->output, NULL, NULL);
int status = tetrahedralize(command, &tetgen->input, &tetgen->output, NULL, NULL);
if (status != 0) {
return TRITET_ERROR_TETGEN_FAIL;
}
} catch (int32_t status) {
printf("status = %d\n", status); // TODO
} catch (...) {
Expand Down Expand Up @@ -318,7 +321,10 @@ int32_t tet_run_tetrahedralize(struct ExtTetgen *tetgen, int32_t verbose, int32_
strcat(command, "q");
}
try {
tetrahedralize(command, &tetgen->input, &tetgen->output, NULL, NULL);
int status = tetrahedralize(command, &tetgen->input, &tetgen->output, NULL, NULL);
if (status != 0) {
return TRITET_ERROR_TETGEN_FAIL;
}
} catch (int32_t status) {
printf("status = %d\n", status); // TODO
} catch (...) {
Expand Down
23 changes: 17 additions & 6 deletions c_code/predicates.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@

// dorival / gemlab #include "tetgen.h" // Defines the symbol REAL (float or double).
#define REAL double // dorival / gemlab
#include <assert.h> // dorival / gemlab

#ifdef USE_CGAL_PREDICATES
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
Expand Down Expand Up @@ -540,9 +539,13 @@ int test_double(int verbose)
/* */
/* Don't change this routine unless you fully understand it. */
/* */
/* Dorival: */
/* Returns 1 if errors are found */
/* Returns 0 if OK */
/* */
/*****************************************************************************/

void exactinit(int verbose, int noexact, int nofilter, REAL maxx, REAL maxy,
int exactinit(int verbose, int noexact, int nofilter, REAL maxx, REAL maxy,
REAL maxz)
{
REAL half;
Expand Down Expand Up @@ -630,11 +633,18 @@ void exactinit(int verbose, int noexact, int nofilter, REAL maxx, REAL maxy,
// Calculate the two static filters for orient3d() and insphere() tests.
// Added by H. Si, 2012-08-23.

// Sort maxx < maxy < maxz. Re-use 'half' for swapping.
assert(maxx > 0);
assert(maxy > 0);
assert(maxz > 0);
// dorival: make sure the points are not coplanar
if (!(maxx > 0)) {
return 1; // error
}
if (!(maxy > 0)) {
return 1; // error
}
if (!(maxz > 0)) {
return 1; // error
}

// Sort maxx < maxy < maxz. Re-use 'half' for swapping.
if (maxx > maxz) {
half = maxx; maxx = maxz; maxz = half;
}
Expand All @@ -648,6 +658,7 @@ void exactinit(int verbose, int noexact, int nofilter, REAL maxx, REAL maxy,
o3dstaticfilter = 5.1107127829973299e-15 * maxx * maxy * maxz;
ispstaticfilter = 1.2466136531027298e-13 * maxx * maxy * maxz * (maxz * maxz);

return 0; // dorival: OK: no errors found
}

/*****************************************************************************/
Expand Down
25 changes: 18 additions & 7 deletions c_code/tetgen.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30875,9 +30875,12 @@ void tetgenmesh::outmesh2vtk(char* ofilename)
// - Write the output files and print the statistics. //
// - Check the consistency of the mesh (-C). //
// //
// Dorival: //
// Returns 1 if errors are found //
// Returns 0 if OK //
///////////////////////////////////////////////////////////////////////////////

void tetrahedralize(tetgenbehavior *b, tetgenio *in, tetgenio *out,
int tetrahedralize(tetgenbehavior *b, tetgenio *in, tetgenio *out,
tetgenio *addin, tetgenio *bgmin)
{
#ifdef DORIDEBUG
Expand Down Expand Up @@ -30953,9 +30956,13 @@ void tetrahedralize(tetgenbehavior *b, tetgenio *in, tetgenio *out,
m.initializepools();
m.transfernodes();

exactinit(b->verbose, b->noexact, b->nostaticfilter,
int status = exactinit(b->verbose, b->noexact, b->nostaticfilter,
m.xmax - m.xmin, m.ymax - m.ymin, m.zmax - m.zmin);

if (status != 0) {
return 1; // dorival: error found
}

tv[1] = clock();

if (b->refine) { // -r
Expand Down Expand Up @@ -31001,7 +31008,7 @@ void tetrahedralize(tetgenbehavior *b, tetgenio *in, tetgenio *out,
m.outsubfaces(out);
}

return;
return 0; // dorival: OK: no errors found
}
}

Expand Down Expand Up @@ -31265,6 +31272,8 @@ void tetrahedralize(tetgenbehavior *b, tetgenio *in, tetgenio *out,
if (!b->quiet) {
m.statistics();
}

return 0; // dorival: OK: no errors found
}

#ifndef TETLIBRARY
Expand All @@ -31285,7 +31294,7 @@ int main(int argc, char *argv[])
// //
///////////////////////////////////////////////////////////////////////////////

void tetrahedralize(char const *switches, tetgenio *in, tetgenio *out,
int tetrahedralize(char const *switches, tetgenio *in, tetgenio *out,
tetgenio *addin, tetgenio *bgmin,
char const *outfilename) // dorival

Expand Down Expand Up @@ -31321,9 +31330,9 @@ void tetrahedralize(char const *switches, tetgenio *in, tetgenio *out,
bgmin.load_tetmesh(b.bgmeshfilename, (int) b.object);
}

tetrahedralize(&b, &in, NULL, &addin, &bgmin);
int status = tetrahedralize(&b, &in, NULL, &addin, &bgmin);

return 0;
return status; // dorival

#else // with TETLIBRARY

Expand All @@ -31336,7 +31345,9 @@ void tetrahedralize(char const *switches, tetgenio *in, tetgenio *out,
strcpy(b.outfilename, outfilename); // dorival
} // dorival

tetrahedralize(&b, in, out, addin, bgmin);
int status = tetrahedralize(&b, in, out, addin, bgmin);

return status; // dorival

#endif // not TETLIBRARY
}
Expand Down
6 changes: 3 additions & 3 deletions c_code/tetgen.h
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ class tetgenbehavior {
// //
///////////////////////////////////////////////////////////////////////////////

void exactinit(int, int, int, REAL, REAL, REAL);
int exactinit(int, int, int, REAL, REAL, REAL);
REAL orient3d(REAL *pa, REAL *pb, REAL *pc, REAL *pd);
REAL insphere(REAL *pa, REAL *pb, REAL *pc, REAL *pd, REAL *pe);
REAL orient4d(REAL *pa, REAL *pb, REAL *pc, REAL *pd, REAL *pe,
Expand Down Expand Up @@ -2226,11 +2226,11 @@ class tetgenmesh {
// //
///////////////////////////////////////////////////////////////////////////////

void tetrahedralize(tetgenbehavior *b, tetgenio *in, tetgenio *out,
int tetrahedralize(tetgenbehavior *b, tetgenio *in, tetgenio *out,
tetgenio *addin = NULL, tetgenio *bgmin = NULL);

#ifdef TETLIBRARY
void tetrahedralize(char const *switches, tetgenio *in, tetgenio *out,
int tetrahedralize(char const *switches, tetgenio *in, tetgenio *out,
tetgenio *addin = NULL, tetgenio *bgmin = NULL,
char const *outfilename=NULL); // dorival
#endif // #ifdef TETLIBRARY
Expand Down
70 changes: 70 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::StrError;

pub(crate) const TRITET_SUCCESS: i32 = 0;

pub(crate) const TRITET_ERROR_NULL_DATA: i32 = 10;
Expand All @@ -20,6 +22,74 @@ pub(crate) const TRITET_ERROR_INVALID_FACET_POINT_ID: i32 = 7000;
pub(crate) const TRITET_ERROR_INVALID_REGION_INDEX: i32 = 8000;
pub(crate) const TRITET_ERROR_INVALID_HOLE_INDEX: i32 = 9000;

pub(crate) const TRITET_ERROR_TETGEN_FAIL: i32 = 10000;

pub(crate) fn handle_status(status: i32) -> Result<(), StrError> {
if status == TRITET_SUCCESS {
return Ok(());
}

if status == TRITET_ERROR_NULL_DATA {
return Err("INTERNAL ERROR: found NULL data");
}
if status == TRITET_ERROR_STRING_CONCAT {
return Err("INTERNAL ERROR: cannot write string with commands for TetGen");
}

if status == TRITET_ERROR_NULL_POINT_LIST {
return Err("INTERNAL ERROR: found NULL point list");
}
if status == TRITET_ERROR_NULL_SEGMENT_LIST {
return Err("INTERNAL ERROR: found NULL segment list");
}
if status == TRITET_ERROR_NULL_FACET_LIST {
return Err("INTERNAL ERROR: found NULL facet list");
}
if status == TRITET_ERROR_NULL_FACET_POLYGON_LIST {
return Err("INTERNAL ERROR: found NULL facet polygon list");
}
if status == TRITET_ERROR_NULL_REGION_LIST {
return Err("INTERNAL ERROR: found NULL region list");
}
if status == TRITET_ERROR_NULL_HOLE_LIST {
return Err("INTERNAL ERROR: found NULL hole list");
}

if status == TRITET_ERROR_INVALID_POINT_INDEX {
return Err("index of point is out of bounds");
}
if status == TRITET_ERROR_INVALID_SEGMENT_INDEX {
return Err("index of segment is out of bounds");
}
if status == TRITET_ERROR_INVALID_SEGMENT_POINT_ID {
return Err("id of segment point is out of bounds");
}
if status == TRITET_ERROR_INVALID_FACET_INDEX {
return Err("index of facet is out of bounds");
}
if status == TRITET_ERROR_INVALID_FACET_NUM_POLYGON {
return Err("INTERNAL ERROR: found invalid facet number of polygon");
}
if status == TRITET_ERROR_INVALID_FACET_POINT_INDEX {
return Err("index of facet point is out of bounds");
}
if status == TRITET_ERROR_INVALID_FACET_POINT_ID {
return Err("id of facet point is out of bounds");
}
if status == TRITET_ERROR_INVALID_REGION_INDEX {
return Err("index of region is out of bounds");
}
if status == TRITET_ERROR_INVALID_HOLE_INDEX {
return Err("index of hole is out of bounds");
}

if status == TRITET_ERROR_TETGEN_FAIL {
return Err("TetGen failed: points are probably coplanar");
}

return Err("INTERNAL ERROR: some error occurred");
}

/// Maps indices used in this library (tritet) to indices used in Triangle
///
/// ```text
Expand Down
Loading

0 comments on commit 5209811

Please sign in to comment.