summaryrefslogtreecommitdiffstats
path: root/libdimension/model/objects/csg.c
blob: 15008c066a3e72a8130ad0852d0786dd6a0aa9d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*************************************************************************
 * Copyright (C) 2010-2014 Tavian Barnes <tavianator@tavianator.com>     *
 *                                                                       *
 * This file is part of The Dimension Library.                           *
 *                                                                       *
 * The Dimension Library is free software; you can redistribute it and/  *
 * or modify it under the terms of the GNU Lesser General Public License *
 * as published by the Free Software Foundation; either version 3 of the *
 * License, or (at your option) any later version.                       *
 *                                                                       *
 * The Dimension Library is distributed in the hope that it will be      *
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty   *
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
 * Lesser General Public License for more details.                       *
 *                                                                       *
 * You should have received a copy of the GNU Lesser General Public      *
 * License along with this program.  If not, see                         *
 * <http://www.gnu.org/licenses/>.                                       *
 *************************************************************************/

/**
 * @file
 * Constructive solid geometry.
 */

#include "internal.h"
#include "internal/bvh.h"
#include "dimension/model.h"
#include <stdlib.h>

////////////
// Unions //
////////////

typedef struct {
  dmnsn_object object;
  dmnsn_bvh *bvh;
} dmnsn_csg_union;

/// CSG union intersection callback.
static bool
dmnsn_csg_union_intersection_fn(const dmnsn_object *object,
                                dmnsn_ray ray,
                                dmnsn_intersection *intersection)
{
  const dmnsn_csg_union *csg = (const dmnsn_csg_union *)object;
  return dmnsn_bvh_intersection(csg->bvh, ray, intersection, true);
}

/// CSG union inside callback.
static bool
dmnsn_csg_union_inside_fn(const dmnsn_object *object, dmnsn_vector point)
{
  const dmnsn_csg_union *csg = (const dmnsn_csg_union *)object;
  return dmnsn_bvh_inside(csg->bvh, point);
}

/// CSG union precomputation callback.
static void
dmnsn_csg_union_precompute_fn(dmnsn_object *object)
{
  dmnsn_csg_union *csg = (dmnsn_csg_union *)object;
  csg->object.trans_inv = dmnsn_identity_matrix();

  dmnsn_bvh *bvh = dmnsn_new_bvh(csg->object.children, DMNSN_BVH_PRTREE);
  csg->bvh = bvh;
  csg->object.aabb = dmnsn_bvh_aabb(bvh);
}

/// CSG union vtable.
static const dmnsn_object_vtable dmnsn_csg_union_vtable = {
  .intersection_fn = dmnsn_csg_union_intersection_fn,
  .inside_fn = dmnsn_csg_union_inside_fn,
  .precompute_fn = dmnsn_csg_union_precompute_fn,
};

/// CSG union destruction callback.
static void
dmnsn_csg_union_cleanup(void *ptr)
{
  dmnsn_csg_union *csg = ptr;
  dmnsn_delete_bvh(csg->bvh);
}

// Bulk-load a union
dmnsn_object *
dmnsn_new_csg_union(dmnsn_pool *pool, dmnsn_array *objects)
{
  dmnsn_csg_union *csg = DMNSN_PALLOC_TIDY(pool, dmnsn_csg_union, dmnsn_csg_union_cleanup);
  csg->bvh = NULL;

  dmnsn_object *object = &csg->object;
  dmnsn_init_object(object);

  object->vtable = &dmnsn_csg_union_vtable;
  object->children = objects;
  object->split_children = true;

  return object;
}

/**
 * Generic CSG intersection callback.
 * @param[in]  csg           The CSG object.
 * @param[in]  ray           The intersection ray.
 * @param[out] intersection  The intersection data.
 * @param[in]  inside1       Whether the first object is allowed inside the
 *                           second object.
 * @param[in]  inside2       Whether the second object is allowed inside the
 *                           first object.
 * @return Whether \p ray intersected \p csg.
 */
static bool
dmnsn_csg_intersection_fn(const dmnsn_object *csg, dmnsn_ray ray,
                          dmnsn_intersection *intersection,
                          bool inside1, bool inside2)
{
  const dmnsn_object *A = *(dmnsn_object **)dmnsn_array_first(csg->children);
  const dmnsn_object *B = *(dmnsn_object **)dmnsn_array_last(csg->children);

  dmnsn_intersection i1, i2;
  bool is_i1 = dmnsn_object_intersection(A, ray, &i1);
  bool is_i2 = dmnsn_object_intersection(B, ray, &i2);

  double oldt = 0.0;
  while (is_i1) {
    i1.ray = ray;
    i1.t += oldt;
    oldt = i1.t + dmnsn_epsilon;

    dmnsn_vector point = dmnsn_ray_point(i1.ray, i1.t);
    if (inside2 ^ dmnsn_object_inside(B, point)) {
      dmnsn_ray newray = ray;
      newray.x0 = dmnsn_ray_point(ray, i1.t);
      newray    = dmnsn_ray_add_epsilon(newray);
      is_i1 = dmnsn_object_intersection(A, newray, &i1);
    } else {
      break;
    }
  }

  oldt = 0.0;
  while (is_i2) {
    i2.ray = ray;
    i2.t += oldt;
    oldt = i2.t + dmnsn_epsilon;

    dmnsn_vector point = dmnsn_ray_point(i2.ray, i2.t);
    if (inside1 ^ dmnsn_object_inside(A, point)) {
      dmnsn_ray newray = ray;
      newray.x0 = dmnsn_ray_point(ray, i2.t);
      newray    = dmnsn_ray_add_epsilon(newray);
      is_i2 = dmnsn_object_intersection(B, newray, &i2);
    } else {
      break;
    }
  }

  if (is_i1 && is_i2) {
    if (i1.t < i2.t) {
      *intersection = i1;
    } else {
      *intersection = i2;
    }
  } else if (is_i1) {
    *intersection = i1;
  } else if (is_i2) {
    *intersection = i2;
  } else {
    return false;
  }

  return true;
}

///////////////////
// Intersections //
///////////////////

/// CSG intersection intersection callback.
static bool
dmnsn_csg_intersection_intersection_fn(const dmnsn_object *csg,
                                       dmnsn_ray ray,
                                       dmnsn_intersection *intersection)
{
  return dmnsn_csg_intersection_fn(csg, ray, intersection, true, true);
}

/// CSG intersection inside callback.
static bool
dmnsn_csg_intersection_inside_fn(const dmnsn_object *csg, dmnsn_vector point)
{
  const dmnsn_object *A = *(dmnsn_object **)dmnsn_array_first(csg->children);
  const dmnsn_object *B = *(dmnsn_object **)dmnsn_array_last(csg->children);
  return dmnsn_object_inside(A, point) && dmnsn_object_inside(B, point);
}

/// CSG intersection precomputation callback.
static void
dmnsn_csg_intersection_precompute_fn(dmnsn_object *csg)
{
  dmnsn_object *A = *(dmnsn_object **)dmnsn_array_first(csg->children);
  dmnsn_object *B = *(dmnsn_object **)dmnsn_array_last(csg->children);

  csg->trans_inv = dmnsn_identity_matrix();
  csg->aabb.min = dmnsn_vector_max(A->aabb.min, B->aabb.min);
  csg->aabb.max = dmnsn_vector_min(A->aabb.max, B->aabb.max);
}

/// CSG intersection vtable.
static const dmnsn_object_vtable dmnsn_csg_intersection_vtable = {
  .intersection_fn = dmnsn_csg_intersection_intersection_fn,
  .inside_fn = dmnsn_csg_intersection_inside_fn,
  .precompute_fn = dmnsn_csg_intersection_precompute_fn,
};

dmnsn_object *
dmnsn_new_csg_intersection(dmnsn_pool *pool, dmnsn_object *A, dmnsn_object *B)
{
  dmnsn_object *csg = dmnsn_new_object(pool);
  csg->vtable = &dmnsn_csg_intersection_vtable;

  csg->children = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *);
  dmnsn_array_push(csg->children, &A);
  dmnsn_array_push(csg->children, &B);

  return csg;
}

/////////////////
// Differences //
/////////////////

/// CSG difference intersection callback.
static bool
dmnsn_csg_difference_intersection_fn(const dmnsn_object *csg,
                                     dmnsn_ray ray,
                                     dmnsn_intersection *intersection)
{
  return dmnsn_csg_intersection_fn(csg, ray, intersection, true, false);
}

/// CSG difference inside callback.
static bool
dmnsn_csg_difference_inside_fn(const dmnsn_object *csg, dmnsn_vector point)
{
  const dmnsn_object *A = *(dmnsn_object **)dmnsn_array_first(csg->children);
  const dmnsn_object *B = *(dmnsn_object **)dmnsn_array_last(csg->children);
  return dmnsn_object_inside(A, point)  && !dmnsn_object_inside(B, point);
}

/// CSG difference precomputation callback.
static void
dmnsn_csg_difference_precompute_fn(dmnsn_object *csg)
{
  dmnsn_object *A = *(dmnsn_object **)dmnsn_array_first(csg->children);

  csg->trans_inv = dmnsn_identity_matrix();
  csg->aabb = A->aabb;
}

/// CSG difference vtable.
static const dmnsn_object_vtable dmnsn_csg_difference_vtable = {
  .intersection_fn = dmnsn_csg_difference_intersection_fn,
  .inside_fn = dmnsn_csg_difference_inside_fn,
  .precompute_fn = dmnsn_csg_difference_precompute_fn,
};

dmnsn_object *
dmnsn_new_csg_difference(dmnsn_pool *pool, dmnsn_object *A, dmnsn_object *B)
{
  dmnsn_object *csg = dmnsn_new_object(pool);
  csg->vtable = &dmnsn_csg_difference_vtable;

  csg->children = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *);
  dmnsn_array_push(csg->children, &A);
  dmnsn_array_push(csg->children, &B);

  return csg;
}

////////////
// Merges //
////////////

/// CSG merge intersection callback.
static bool
dmnsn_csg_merge_intersection_fn(const dmnsn_object *csg,
                                dmnsn_ray ray,
                                dmnsn_intersection *intersection)
{
  return dmnsn_csg_intersection_fn(csg, ray, intersection, false, false);
}

/// CSG merge inside callback.
static bool
dmnsn_csg_merge_inside_fn(const dmnsn_object *csg, dmnsn_vector point)
{
  const dmnsn_object *A = *(dmnsn_object **)dmnsn_array_first(csg->children);
  const dmnsn_object *B = *(dmnsn_object **)dmnsn_array_last(csg->children);
  return dmnsn_object_inside(A, point) || dmnsn_object_inside(B, point);
}

/// CSG merge precomputation callback.
static void
dmnsn_csg_merge_precompute_fn(dmnsn_object *csg)
{
  dmnsn_object *A = *(dmnsn_object **)dmnsn_array_first(csg->children);
  dmnsn_object *B = *(dmnsn_object **)dmnsn_array_last(csg->children);

  csg->trans_inv = dmnsn_identity_matrix();
  csg->aabb.min = dmnsn_vector_min(A->aabb.min, B->aabb.min);
  csg->aabb.max = dmnsn_vector_max(A->aabb.max, B->aabb.max);
}

/// CSG merge vtable.
static const dmnsn_object_vtable dmnsn_csg_merge_vtable = {
  .intersection_fn = dmnsn_csg_merge_intersection_fn,
  .inside_fn = dmnsn_csg_merge_inside_fn,
  .precompute_fn = dmnsn_csg_merge_precompute_fn,
};

dmnsn_object *
dmnsn_new_csg_merge(dmnsn_pool *pool, dmnsn_object *A, dmnsn_object *B)
{
  dmnsn_object *csg = dmnsn_new_object(pool);
  csg->vtable = &dmnsn_csg_merge_vtable;

  csg->children = DMNSN_PALLOC_ARRAY(pool, dmnsn_object *);
  dmnsn_array_push(csg->children, &A);
  dmnsn_array_push(csg->children, &B);

  return csg;
}