Skip to content

Commit

Permalink
Adding a work-around for a Segmentation Fault that I was encountering.
Browse files Browse the repository at this point in the history
It seems that it was possible for the divconqrecurse to be given a vertex array with 1 or 0 verticies. This would cause an infinite loop causing the crash.
I believe that there's already a ticket for this issue:
srv#63
Additionally, adding a minor fix to the CMakeLists.txt to allow 'cmake .' to run.
  • Loading branch information
P3TE committed Dec 8, 2020
1 parent d36a404 commit 195e2a6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
56 changes: 28 additions & 28 deletions libviso2/libviso2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# project
cmake_minimum_required (VERSION 2.6)
project (libviso2)

# directories
set (LIBVISO2_SRC_DIR src)

# include directory
include_directories("${LIBVISO2_SRC_DIR}")

# use sse3 instruction set
if(ARM_CROSS_COMPILATION)
SET(CMAKE_SYSTEM_PROCESSOR arm)
SET(CMAKE_CXX_FLAGS -mfpu=neon)
else(ARM_CROSS_COMPILATION)
add_definitions(-msse3)
endif

# sources
FILE(GLOB LIBVISO2_SRC_FILES "src/*.cpp")

# make release version
set(CMAKE_BUILD_TYPE Release)

# demo program
add_executable(viso2 ${LIBVISO2_SRC_FILES})
target_link_libraries (viso2 png)

# project
cmake_minimum_required (VERSION 2.6)
project (libviso2)

# directories
set (LIBVISO2_SRC_DIR src)

# include directory
include_directories("${LIBVISO2_SRC_DIR}")

# use sse3 instruction set
if(ARM_CROSS_COMPILATION)
SET(CMAKE_SYSTEM_PROCESSOR arm)
SET(CMAKE_CXX_FLAGS -mfpu=neon)
else(ARM_CROSS_COMPILATION)
add_definitions(-msse3)
endif(ARM_CROSS_COMPILATION)

# sources
FILE(GLOB LIBVISO2_SRC_FILES "src/*.cpp")

# make release version
set(CMAKE_BUILD_TYPE Release)

# demo program
add_executable(viso2 ${LIBVISO2_SRC_FILES})
target_link_libraries (viso2 png)

27 changes: 19 additions & 8 deletions libviso2/libviso2/src/triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5950,19 +5950,27 @@ void mergehulls(struct mesh *m, struct behavior *b, struct otri *farleft,
/* */
/*****************************************************************************/

void divconqrecurse(struct mesh *m, struct behavior *b, vertex *sortarray,
int divconqrecurse(struct mesh *m, struct behavior *b, vertex *sortarray,
int vertices, int axis,
struct otri *farleft, struct otri *farright)
{
struct otri midtri, tri1, tri2, tri3;
struct otri innerleft, innerright;
float area;
int divider;
int divider, result;
result = 0;

if (b->verbose > 2) {
printf(" Triangulating %d vertices.\n", vertices);
}
if (vertices == 2) {
if(vertices <= 1){
//This shouldn't happen, but it can and if not handled, we will have infinite recursion and crash.*/
//What we'll do here to try and avoid crashing is just to make no triangles...
if (b->verbose > 2) {
printf(" Invalid divconqrecurse requested, can't have less than 2 vertices (Requested %d).\n", vertices);
}
result = 1; //Invalid.
} else if (vertices == 2) {
/* The triangulation of two vertices is an edge. An edge is */
/* represented by two bounding triangles. */
maketriangle(m, b, farleft);
Expand All @@ -5988,7 +5996,6 @@ void divconqrecurse(struct mesh *m, struct behavior *b, vertex *sortarray,
}
/* Ensure that the origin of `farleft' is sortarray[0]. */
lprev(*farright, *farleft);
return;
} else if (vertices == 3) {
/* The triangulation of three vertices is either a triangle (with */
/* three bounding triangles) or two edges (with four bounding */
Expand Down Expand Up @@ -6085,13 +6092,12 @@ void divconqrecurse(struct mesh *m, struct behavior *b, vertex *sortarray,
printf(" Creating ");
printtriangle(m, b, &tri3);
}
return;
} else {
/* Split the vertices in half. */
divider = vertices >> 1;
/* Recursively triangulate each half. */
divconqrecurse(m, b, sortarray, divider, 1 - axis, farleft, &innerleft);
divconqrecurse(m, b, &sortarray[divider], vertices - divider, 1 - axis,
result += divconqrecurse(m, b, sortarray, divider, 1 - axis, farleft, &innerleft);
result += divconqrecurse(m, b, &sortarray[divider], vertices - divider, 1 - axis,
&innerright, farright);
if (b->verbose > 1) {
printf(" Joining triangulations with %d and %d vertices.\n", divider,
Expand All @@ -6100,6 +6106,7 @@ void divconqrecurse(struct mesh *m, struct behavior *b, vertex *sortarray,
/* Merge the two triangulations into one. */
mergehulls(m, b, farleft, &innerleft, &innerright, farright, axis);
}
return result;
}

long removeghosts(struct mesh *m, struct behavior *b, struct otri *startghost)
Expand Down Expand Up @@ -6210,7 +6217,11 @@ long divconqdelaunay(struct mesh *m, struct behavior *b)
}

/* Form the Delaunay triangulation. */
divconqrecurse(m, b, sortarray, i, 0, &hullleft, &hullright);
if(divconqrecurse(m, b, sortarray, i, 0, &hullleft, &hullright)){
/* Will only return a non-zero value if something went wrong. */
trifree((int *) sortarray);
return 0l;
}
trifree((int *) sortarray);

return removeghosts(m, b, &hullleft);
Expand Down

0 comments on commit 195e2a6

Please sign in to comment.