Add single precision option

fixes-turtlebasket
Mattia Montanari 2023-04-02 12:06:21 +02:00
parent db2c65cac9
commit 14fc2d80d6
4 changed files with 25 additions and 9 deletions

View File

@ -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

View File

@ -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. */

View File

@ -32,10 +32,17 @@
#ifndef OPENGJK_H__
#define OPENGJK_H__
#include <float.h>
/*! @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.
*

View File

@ -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])