started on the fracture code
[meshfrac] / src / frac.c
diff --git a/src/frac.c b/src/frac.c
new file mode 100644 (file)
index 0000000..8a6bcae
--- /dev/null
@@ -0,0 +1,127 @@
+#include <stdlib.h>
+#include <string.h>
+#include "frac.h"
+#include "dynarr.h"
+
+static void destroy_cell(struct frac_cell *cell);
+
+int frac_init(struct fracture *frac)
+{
+       frac->mesh = 0;
+
+       if(!(frac->cells = dynarr_alloc(0, sizeof *frac->cells))) {
+               return -1;
+       }
+       return 0;
+}
+
+void frac_destroy(struct fracture *frac)
+{
+       int i, num;
+
+       if(!frac) return;
+
+       if(frac->cells) {
+               num = dynarr_size(frac->cells);
+               for(i=0; i<num; i++) {
+                       destroy_cell(frac->cells + i);
+               }
+               dynarr_free(frac->cells);
+       }
+}
+
+static void destroy_cell(struct frac_cell *cell)
+{
+       if(!cell) return;
+
+       cmesh_free(cell->mesh);
+       dynarr_free(cell->planes);
+}
+
+void frac_mesh(struct fracture *frac, const struct cmesh *m)
+{
+       frac->mesh = (struct cmesh*)m;
+}
+
+int frac_point(struct fracture *frac, float x, float y, float z)
+{
+       struct frac_cell cell;
+       struct frac_cell *tmp;
+
+       cgm_vcons(&cell.pt, x, y, z);
+       if(!(cell.mesh = cmesh_alloc())) {
+               return -1;
+       }
+       if(!(cell.planes = dynarr_alloc(0, sizeof *cell.planes))) {
+               cmesh_free(cell.mesh);
+               return -1;
+       }
+       if(!(tmp = dynarr_push(frac->cells, &cell))) {
+               cmesh_free(cell.mesh);
+               dynarr_free(cell.planes);
+               return -1;
+       }
+       frac->cells = tmp;
+       return 0;
+}
+
+int frac_num_cells(struct fracture *frac)
+{
+       return dynarr_size(frac->cells);
+}
+
+int frac_gen_points(struct fracture *frac, int num)
+{
+       int i;
+       float x, y, z;
+       cgm_vec3 bbmin, bbmax, delta;
+
+       if(!frac || !frac->mesh) return -1;
+       if(!cmesh_poly_count(frac->mesh)) {
+               return -1;
+       }
+
+       cmesh_aabbox(frac->mesh, &bbmin, &bbmax);
+       delta = bbmax;
+       cgm_vsub(&delta, &bbmin);
+
+       for(i=0; i<num; i++) {
+               x = (float)rand() / RAND_MAX * delta.x + bbmin.x;
+               y = (float)rand() / RAND_MAX * delta.y + bbmin.y;
+               z = (float)rand() / RAND_MAX * delta.z + bbmin.z;
+
+               if(frac_point(frac, x, y, z) == -1) {
+                       return -1;
+               }
+       }
+       return 0;
+}
+
+int frac_build_cells(struct fracture *frac)
+{
+       return -1;
+}
+
+int frac_build_shell(struct fracture *frac)
+{
+       return -1;
+}
+
+int frac_build_walls(struct fracture *frac)
+{
+       return -1;
+}
+
+int frac_build(struct fracture *frac)
+{
+       if(frac_build_cells(frac) == -1) {
+               return -1;
+       }
+       if(frac_build_shell(frac) == -1) {
+               return -1;
+       }
+       if(frac_build_walls(frac) == -1) {
+               return -1;
+       }
+       return 0;
+}