8a6bcae807ba0407234fc5f2c1b8e1b5f87b9b1a
[meshfrac] / src / frac.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include "frac.h"
4 #include "dynarr.h"
5
6 static void destroy_cell(struct frac_cell *cell);
7
8 int frac_init(struct fracture *frac)
9 {
10         frac->mesh = 0;
11
12         if(!(frac->cells = dynarr_alloc(0, sizeof *frac->cells))) {
13                 return -1;
14         }
15         return 0;
16 }
17
18 void frac_destroy(struct fracture *frac)
19 {
20         int i, num;
21
22         if(!frac) return;
23
24         if(frac->cells) {
25                 num = dynarr_size(frac->cells);
26                 for(i=0; i<num; i++) {
27                         destroy_cell(frac->cells + i);
28                 }
29                 dynarr_free(frac->cells);
30         }
31 }
32
33 static void destroy_cell(struct frac_cell *cell)
34 {
35         if(!cell) return;
36
37         cmesh_free(cell->mesh);
38         dynarr_free(cell->planes);
39 }
40
41 void frac_mesh(struct fracture *frac, const struct cmesh *m)
42 {
43         frac->mesh = (struct cmesh*)m;
44 }
45
46 int frac_point(struct fracture *frac, float x, float y, float z)
47 {
48         struct frac_cell cell;
49         struct frac_cell *tmp;
50
51         cgm_vcons(&cell.pt, x, y, z);
52         if(!(cell.mesh = cmesh_alloc())) {
53                 return -1;
54         }
55         if(!(cell.planes = dynarr_alloc(0, sizeof *cell.planes))) {
56                 cmesh_free(cell.mesh);
57                 return -1;
58         }
59         if(!(tmp = dynarr_push(frac->cells, &cell))) {
60                 cmesh_free(cell.mesh);
61                 dynarr_free(cell.planes);
62                 return -1;
63         }
64         frac->cells = tmp;
65         return 0;
66 }
67
68 int frac_num_cells(struct fracture *frac)
69 {
70         return dynarr_size(frac->cells);
71 }
72
73 int frac_gen_points(struct fracture *frac, int num)
74 {
75         int i;
76         float x, y, z;
77         cgm_vec3 bbmin, bbmax, delta;
78
79         if(!frac || !frac->mesh) return -1;
80         if(!cmesh_poly_count(frac->mesh)) {
81                 return -1;
82         }
83
84         cmesh_aabbox(frac->mesh, &bbmin, &bbmax);
85         delta = bbmax;
86         cgm_vsub(&delta, &bbmin);
87
88         for(i=0; i<num; i++) {
89                 x = (float)rand() / RAND_MAX * delta.x + bbmin.x;
90                 y = (float)rand() / RAND_MAX * delta.y + bbmin.y;
91                 z = (float)rand() / RAND_MAX * delta.z + bbmin.z;
92
93                 if(frac_point(frac, x, y, z) == -1) {
94                         return -1;
95                 }
96         }
97         return 0;
98 }
99
100 int frac_build_cells(struct fracture *frac)
101 {
102         return -1;
103 }
104
105 int frac_build_shell(struct fracture *frac)
106 {
107         return -1;
108 }
109
110 int frac_build_walls(struct fracture *frac)
111 {
112         return -1;
113 }
114
115 int frac_build(struct fracture *frac)
116 {
117         if(frac_build_cells(frac) == -1) {
118                 return -1;
119         }
120         if(frac_build_shell(frac) == -1) {
121                 return -1;
122         }
123         if(frac_build_walls(frac) == -1) {
124                 return -1;
125         }
126         return 0;
127 }