diff --git a/CMakeLists.txt b/CMakeLists.txt index 3aa13d3..6259f8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,10 @@ if(BUILD_MONO) add_compile_definitions(CS_MONO_BUILD) endif(BUILD_MONO) +if(SINGLE_PRECISION) + add_compile_definitions(USE_32BITS) +endif(SINGLE_PRECISION) + add_library( obj_openGJK OBJECT ${CMAKE_CURRENT_SOURCE_DIR}/openGJK.c diff --git a/examples/c/main.c b/examples/c/main.c index aea3c2a..60b1126 100644 --- a/examples/c/main.c +++ b/examples/c/main.c @@ -32,7 +32,7 @@ /// @brief Function for reading input file with body's coordinates. int -readinput(const char* inputfile, double*** pts, int* out) { +readinput(const char* inputfile, gkFloat*** pts, int* out) { int npoints = 0; int idx = 0; FILE* fp; @@ -56,16 +56,21 @@ readinput(const char* inputfile, double*** pts, int* out) { } /* Allocate memory. */ - double** arr = (double**)malloc(npoints * sizeof(double*)); + gkFloat** arr = (gkFloat**)malloc(npoints * sizeof(gkFloat*)); for (int i = 0; i < npoints; i++) { - arr[i] = (double*)malloc(3 * sizeof(double)); + arr[i] = (gkFloat*)malloc(3 * sizeof(gkFloat)); } /* Read and store vertices' coordinates. */ for (idx = 0; idx < npoints; idx++) { - if (fscanf_s(fp, "%lf %lf %lf\n", &arr[idx][0], &arr[idx][1], &arr[idx][2]) != 3) { +#ifdef USE_32BITS + if (fscanf_s(fp, "%f %f %f\n", &arr[idx][0], &arr[idx][1], &arr[idx][2]) != 3) { return 1; } +#else + return 1; + if (fscanf_s(fp, "%lf %lf %lf\n", &arr[idx][0], &arr[idx][1], &arr[idx][2]) != 3) {} +#endif } fclose(fp); @@ -83,7 +88,7 @@ readinput(const char* inputfile, double*** pts, int* out) { int main() { /* Squared distance computed by openGJK. */ - double dd; + gkFloat dd; /* Structure of simplex used by openGJK. */ gkSimplex s; /* Number of vertices defining body 1 and body 2, respectively. */ @@ -94,7 +99,7 @@ main() { /* Specify name of input files for body 1 and body 2, respectively. */ char inputfileA[40] = "userP.dat", inputfileB[40] = "userQ.dat"; /* Pointers to vertices' coordinates of body 1 and body 2, respectively. */ - double(**vrtx1) = NULL, (**vrtx2) = NULL; + gkFloat(**vrtx1) = NULL, (**vrtx2) = NULL; /* For importing openGJK this is Step 2: adapt the data structure for the * two bodies that will be passed to the GJK procedure. */ diff --git a/include/openGJK/openGJK.h b/include/openGJK/openGJK.h index a1923e6..ecb4f44 100644 --- a/include/openGJK/openGJK.h +++ b/include/openGJK/openGJK.h @@ -32,10 +32,17 @@ #ifndef OPENGJK_H__ #define OPENGJK_H__ +#include /*! @brief Precision of floating-point numbers. * * Default is set to 64-bit (Double). Change this to quickly play around with 16- and 32-bit. */ -#define gkFloat double +#ifdef USE_32BITS +#define gkFloat float +#define gkEpsilon FLT_EPSILON +#else +#define gkFloat double +#define gkEpsilon DBL_EPSILON +#endif /*! @brief Data structure for convex polytopes. * diff --git a/openGJK.c b/openGJK.c index 73cb2a8..4702fa8 100644 --- a/openGJK.c +++ b/openGJK.c @@ -43,8 +43,8 @@ #define mexPrintf printf #endif -#define eps_rel22 1e-10 -#define eps_tot22 1e-12 +#define eps_rel22 (gkFloat) gkEpsilon * 1e4f +#define eps_tot22 (gkFloat) gkEpsilon * 1e2f #define norm2(a) (a[0] * a[0] + a[1] * a[1] + a[2] * a[2]) #define dotProduct(a, b) (a[0] * b[0] + a[1] * b[1] + a[2] * b[2])