Created
March 30, 2017 10:41
-
-
Save Grieverheart/bf5415c130e63c7d90d94b9538e8322b to your computer and use it in GitHub Desktop.
Collision Detection lib C API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#if 1 | |
//////////// | |
/* TYPE A */ | |
//////////// | |
typedef void (*support_t)(const void* data, const double* dir, double* point); | |
typedef struct{ | |
support_t support; | |
}convex_t; | |
typedef struct{ | |
convex_t convex; | |
double min[3], max[3]; | |
}box_t; | |
typedef struct{ | |
convex_t convex; | |
}sphere_t; | |
void box_support(const void* data, const double* dir, double* point){ | |
printf("Hello from box\n"); | |
} | |
void sphere_support(const void* data, const double* dir, double* point){ | |
printf("Hello from sphere\n"); | |
} | |
void init_box(box_t* box, const double* min, const double* max){ | |
box->convex.support = box_support; | |
for(int i = 0; i < 3; ++i){ | |
box->min[i] = min[i]; | |
box->max[i] = max[i]; | |
} | |
} | |
void init_sphere(sphere_t* sphere){ | |
sphere->convex.support = sphere_support; | |
} | |
typedef struct{ | |
double pos[3]; | |
double rot[4]; | |
double size; | |
}transform_t; | |
int gjk_boolean( | |
const convex_t* sa, const transform_t* ta, | |
const convex_t* sb, const transform_t* tb | |
){ | |
double dir[3], point[3]; | |
sa->support(sa, dir, point); | |
sb->support(sa, dir, point); | |
return 0; | |
} | |
#define TO_CONVEX(x) (convex_t*)(x) | |
int main(int argc, char* argv[]){ | |
double min[] = {-1.0, -1.0, -1.0}; | |
double max[] = {1.0, 1.0, 1.0}; | |
box_t box; | |
sphere_t sphere; | |
init_sphere(&sphere); | |
init_box(&box, min, max); | |
transform_t ta = { | |
{0.0, 0.0, 0.0}, | |
{1.0, 0.0, 0.0, 1.0}, | |
1.0 | |
}; | |
gjk_boolean(TO_CONVEX(&box), &ta, TO_CONVEX(&sphere), &ta); | |
return 0; | |
} | |
#else | |
//////////// | |
/* TYPE B */ | |
//////////// | |
typedef void (*support_t)(const void* data, const double* dir, double* point); | |
typedef struct{ | |
support_t support; | |
}convex_t; | |
typedef struct{ | |
convex_t convex; | |
double min[3], max[3]; | |
}box_t; | |
typedef struct{ | |
convex_t convex; | |
}sphere_t; | |
typedef struct{ | |
int shape_type; | |
union{ | |
box_t box; | |
sphere_t sphere; | |
}shape_data; | |
}shape_t; | |
void box_support(const void* data, const double* dir, double* point){ | |
printf("Hello from box\n"); | |
} | |
void sphere_support(const void* data, const double* dir, double* point){ | |
printf("Hello from sphere\n"); | |
} | |
enum e_shape_t{ | |
SPHERE_TYPE = 0, | |
BOX_TYPE | |
}; | |
void init_box(shape_t* shape, const double* min, const double* max){ | |
shape->shape_type = BOX_TYPE; | |
shape->shape_data.box.convex.support = box_support; | |
for(int i = 0; i < 3; ++i){ | |
shape->shape_data.box.min[i] = min[i]; | |
shape->shape_data.box.max[i] = max[i]; | |
} | |
} | |
void init_sphere(shape_t* sphere){ | |
sphere->shape_type = SPHERE_TYPE; | |
sphere->shape_data.sphere.convex.support = sphere_support; | |
} | |
typedef struct{ | |
double pos[3]; | |
double rot[4]; | |
double size; | |
}transform_t; | |
int shape_boolean( | |
const shape_t* sa, const transform_t* ta, | |
const shape_t* sb, const transform_t* tb | |
){ | |
double dir[3], point[3]; | |
if(sa->shape_type >= 0){ | |
const convex_t* csa = (const convex_t*)(&sa->shape_data); | |
csa->support(csa, dir, point); | |
} | |
if(sb->shape_type >= 0){ | |
const convex_t* csb = (const convex_t*)(&sb->shape_data); | |
csb->support(csb, dir, point); | |
} | |
return 0; | |
} | |
int main(int argc, char* argv[]){ | |
double min[] = {-1.0, -1.0, -1.0}; | |
double max[] = {1.0, 1.0, 1.0}; | |
shape_t box; | |
shape_t sphere; | |
init_sphere(&sphere); | |
init_box(&box, min, max); | |
transform_t ta = { | |
{0.0, 0.0, 0.0}, | |
{1.0, 0.0, 0.0, 1.0}, | |
1.0 | |
}; | |
shape_boolean(&box, &ta, &sphere, &ta); | |
return 0; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment