summaryrefslogtreecommitdiff
path: root/test/raytracer/intersect.c
blob: 3b2d7b0dd7f8caddb52b6aba100086aaeacf9421 (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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "config.h"
#include "arrays.h"
#include "point.h"
#include "vector.h"
#include "eval.h"
#include "object.h"
#include "matrix.h"
#include "intersect.h"

/* Operations on interval lists */

#define POS_INFTY HUGE_VAL

struct intervlist {
  flt beg;
  struct object * obeg;
  flt end;
  struct object * oend;
  struct intervlist * next;
};

typedef struct intervlist * intervlist;

static intervlist cons(flt b, struct object * ob,
                       flt e, struct object * oe,
                       intervlist next)
{
  intervlist l = arena_alloc(sizeof(struct intervlist));
  l->beg = b;
  l->obeg = ob;
  l->end = e;
  l->oend = oe;
  l->next = next;
  return l;
}

static intervlist conshead(intervlist i, intervlist next)
{
  intervlist l = arena_alloc(sizeof(struct intervlist));
  l->beg = i->beg;
  l->obeg = i->obeg;
  l->end = i->end;
  l->oend = i->oend;
  l->next = next;
  return l;
}

static intervlist union_intervals(intervlist l1, intervlist l2)
{
  flt beg;
  struct object * obeg;

  if (l1 == NULL) return l2;
  if (l2 == NULL) return l1;
  if (l1->end < l2->beg)
    /* int1 strictly before int2 */
    return conshead(l1, union_intervals(l1->next, l2));
  if (l2->end < l1->beg)
    /* int2 strictly before int1 */
    return conshead(l2, union_intervals(l1, l2->next));
  /* int1 and int2 overlap, merge */
  if (l1->beg < l2->beg) {
    beg = l1->beg;
    obeg = l1->obeg;
  } else {
    beg = l2->beg;
    obeg = l2->obeg;
  }
  if (l1->end < l2->end)
    return union_intervals(l1->next,
                           cons(beg, obeg, l2->end, l2->oend, l2->next));
  else
    return union_intervals(cons(beg, obeg, l1->end, l1->oend, l1->next),
                           l2->next);
}

static intervlist intersect_intervals(intervlist l1, intervlist l2)
{
  flt beg;
  struct object * obeg;

  if (l1 == NULL) return NULL;
  if (l2 == NULL) return NULL;
  if (l1->end < l2->beg)
    /* int1 strictly before int2 */
    return intersect_intervals(l1->next, l2);
  if (l2->end < l1->beg)
    /* int2 strictly before int1 */
    return intersect_intervals(l1, l2->next);
  /* int1 and int2 overlap, add intersection */
  if (l1->beg > l2->beg) {
    beg = l1->beg;
    obeg = l1->obeg;
  } else {
    beg = l2->beg;
    obeg = l2->obeg;
  }
  if (l1->end < l2->end)
    return cons(beg, obeg, l1->end, l1->oend, intersect_intervals(l1->next, l2));
  else
    return cons(beg, obeg, l2->end, l2->oend, intersect_intervals(l1, l2->next));
}

static intervlist difference_intervals(intervlist l1, intervlist l2)
{
  intervlist l;

  if (l1 == NULL) return NULL;
  if (l2 == NULL) return l1;
  if (l1->end < l2->beg)
    /* int1 strictly before int2, keep int1 */
    return conshead(l1, difference_intervals(l1->next, l2));
  if (l2->end < l1->beg)
    /* int2 strictly before int1, throw int2 away */
    return difference_intervals(l1, l2->next);
  /* int and int2 overlap */
  if (l1->end > l2->end)
    /* int1 extends beyond int2, add segment [l2->end,l1->end] */
    l = difference_intervals(cons(l2->end, l2->oend, l1->end, l1->oend, l1->next), l2->next);
  else
    /* int2 doesn't extend beyond int2, nothing to add */
    l = difference_intervals(l1->next, l2);
  if (l1->beg < l2->beg)
    /* int1 starts before l2, add segment [l1->beg,l2->beg] */
    l = cons(l1->beg, l1->obeg, l2->beg, l2->obeg, l);
  return l;
}

/* Intersections between rays (p + tv) and basic objects (bobj) */

static intervlist intersect_ray_plane(struct object * bobj,
                                      struct point * p,
                                      struct vector * v)
{
  if (p->y >= 0)
    if (v->dy >= 0)
      return NULL;
    else
      return cons(- p->y / v->dy, bobj, POS_INFTY, bobj, NULL);
  else
    if (v->dy >= 0)
      return cons(0, bobj, - p->y / v->dy, bobj, NULL);
    else
      return cons(0, bobj, POS_INFTY, bobj, NULL);
}

static intervlist intersect_ray_sphere(struct object * bobj,
                                       struct point * p,
                                       struct vector * v)
{
  flt a, b, c, d, sq, t0, t1, tswap;

  a = v->dx * v->dx + v->dy * v->dy + v->dz * v->dz;
  b = p->x * v->dx + p->y * v->dy + p->z * v->dz;
  c = p->x * p->x + p->y * p->y + p->z * p->z - 1.0;
  d = b * b - a * c;
  if (d <= 0) return NULL;
  sq = sqrt(d);
  /* Numerically stable solution of quadratic */
  if (b >= 0) {
    t0 = c / (- b - sq);
    t1 = (- b - sq) / a;
  } else {
    t0 = c / (- b + sq);
    t1 = (- b + sq) / a;
  }
  if (t0 >= t1) {
    tswap = t0; t0 = t1; t1 = tswap;
  }
  if (t1 <= 0) {
    assert (t0 <= 0);
    return NULL;
  }
  if (t0 >= 0) {
    assert (t1 >= 0);
    return cons(t0, bobj, t1, bobj, NULL);
  }
  return cons(0, bobj, t1, bobj, NULL);
}

static intervlist intersect_ray_slice(struct object * bobj,
                                      flt pc, flt vc)
{
  if (pc > 1.0) {
    if (vc >= 0.0)
      return NULL;
    else
      return cons((1.0 - pc) / vc, bobj, - pc / vc, bobj, NULL);
  }
  if (pc < 0.0) {
    if (vc <= 0.0)
      return NULL;
    else
      return cons(- pc / vc, bobj, (1.0 - pc) / vc, bobj, NULL);
  }
  if (vc == 0.0)
    return cons(0.0, bobj, POS_INFTY, bobj, NULL);
  if (vc > 0.0)
    return cons(0.0, bobj, (1.0 - pc) / vc, bobj, NULL);
  else
    return cons(0.0, bobj, - pc / vc, bobj, NULL);
}

static intervlist intersect_ray_cube(struct object * bobj,
                                     struct point * p,
                                     struct vector * v)
{
  return intersect_intervals(intersect_ray_slice(bobj, p->x, v->dx),
           intersect_intervals(intersect_ray_slice(bobj, p->y, v->dy),
                               intersect_ray_slice(bobj, p->z, v->dz)));
}

static intervlist intersect_ray_infinite_cylinder(struct object * bobj,
                                                  struct point * p,
                                                  struct vector * v)
{
  flt a, b, c, d, sq, t0, t1, tswap;

  a = v->dx * v->dx + v->dz * v->dz;
  b = p->x * v->dx + p->z * v->dz;
  c = p->x * p->x + p->z * p->z - 1.0;
  d = b * b - a * c;
  if (d <= 0.0) return NULL;
  sq = sqrt(d);
  if (b >= 0.0) {
    t0 = c / (- b - sq);
    t1 = (- b - sq) / a;
  } else {
    t0 = c / (- b + sq);
    t1 = (- b + sq) / a;
  }
  if (t0 >= t1) { tswap = t0; t0 = t1; t1 = tswap; }
  if (t1 <= 0.0) {
    assert (t0 <= 0.0);
    return NULL;
  }
  if (t0 >= 0.0) {
    assert (t1 >= 0.0);
    return cons(t0, bobj, t1, bobj, NULL);
  }
  return cons(0.0, bobj, t1, bobj, NULL);
}

static intervlist intersect_ray_cylinder(struct object * bobj,
                                         struct point * p,
                                         struct vector * v)
{
  return intersect_intervals(intersect_ray_infinite_cylinder(bobj, p, v),
                             intersect_ray_slice(bobj, p->y, v->dy));
}

static intervlist intersect_ray_infinite_cone(struct object * bobj,
                                              struct point * p,
                                              struct vector * v)
{
  flt a, b, c, d, sq, t, t0, t1, tswap;

  a = v->dx * v->dx - v->dy * v->dy + v->dz * v->dz;
  b = p->x * v->dx - p->y * v->dy + p->z * v->dz;
  c = p->x * p->x - p->y * p->y + p->z * p->z;
  if (a == 0.0) {
    if (b == 0.0) return NULL;
    t = - 0.5 * c / b;
    if (c < 0.0) {
      if (t <= 0.0)
        return cons(0.0, bobj, POS_INFTY, bobj, NULL);
      else
        return cons(0.0, bobj, t, bobj, NULL);
    }
    if (t <= 0.0)
      return NULL;
    else
      return cons(t, bobj, POS_INFTY, bobj, NULL);
  }
  d = b * b - a * c;
  if (d <= 0.0) return NULL;
  sq = sqrt(d);
  if (b >= 0.0) {
    t0 = c / (- b - sq);
    t1 = (- b - sq) / a;
  } else {
    t0 = c / (- b + sq);
    t1 = (- b + sq) / a;
  }
  if (t0 >= t1) { tswap = t0; t0 = t1; t1 = tswap; }
  if (t1 <= 0.0) {
      assert (t0 <= 0.0);
      if (c < 0.0)
        return cons(0.0, bobj, POS_INFTY, bobj, NULL);
      else
        return NULL;
  }
  if (t0 >= 0.0) {
    assert (t1 >= 0.0);
    if (c < 0.0)
      return cons(0.0, bobj, t0, bobj,
                  cons (t1, bobj, POS_INFTY, bobj, NULL));
    else
      return cons(t0, bobj, t1, bobj, NULL);
  }
  if (c < 0.0)
    return cons(0.0, bobj, t1, bobj, NULL);
  else
    return cons(t1, bobj, POS_INFTY, bobj, NULL);
}

static intervlist intersect_ray_cone(struct object * bobj,
                                     struct point * p,
                                     struct vector * v)
{
  return intersect_intervals(intersect_ray_infinite_cone(bobj, p, v),
                             intersect_ray_slice(bobj, p->y, v->dy));
}

/* Approximate test based on bounding sphere */

static inline int inside_bs(struct point * p,
                            struct vector * v,
                            struct object * obj)
{
  flt x, y, z, a, b, c;

  assert(obj->radius >= 0.0);

  if (obj->radius >= POS_INFTY) return 1;
  /* Shift origin to obj.center */
  x = p->x - obj->center.x;
  y = p->y - obj->center.y;
  z = p->z - obj->center.z;
  /* Check whether quadratic has positive discriminant */
  a = v->dx * v->dx + v->dy * v->dy + v->dz * v->dz;
  b = x * v->dx + y * v->dy + z * v->dz;
  c = x * x + y * y + z * z - obj->radius * obj->radius;
  return (b * b > a * c);
}

/* Interval list representing the intersection of the given ray
   and the given composite object */

static intervlist intersect_ray_object(struct point * p,
                                       struct vector * v,
                                       struct object * obj)
{
#define CONVERT_V_P \
    apply_to_point(obj->world2obj, p, &p2); \
    apply_to_vect(obj->world2obj, v, &v2)
  struct point p2;
  struct vector v2;
  intervlist res;

  /* Fast, approximate test based on bounding sphere */
  if (! inside_bs(p, v, obj)) return NULL;
  /* Slow, exact test */
  switch (obj->kind) {
  case Cone:
    CONVERT_V_P;
    res = intersect_ray_cone(obj, &p2, &v2);
    break;
  case Cube:
    CONVERT_V_P;
    res = intersect_ray_cube(obj, &p2, &v2);
    break;
  case Cylinder:
    CONVERT_V_P;
    res = intersect_ray_cylinder(obj, &p2, &v2);
    break;
  case Plane:
    CONVERT_V_P;
    res = intersect_ray_plane(obj, &p2, &v2);
    break;
  case Sphere:
    CONVERT_V_P;
    res = intersect_ray_sphere(obj, &p2, &v2);
    break;
  case Union:
    res = union_intervals(intersect_ray_object(p, v, obj->o1),
                           intersect_ray_object(p, v, obj->o2));
    break;
  case Intersection:
    res = intersect_intervals(intersect_ray_object(p, v, obj->o1),
                               intersect_ray_object(p, v, obj->o2));
    break;
  case Difference:
    res = difference_intervals(intersect_ray_object(p, v, obj->o1),
                                intersect_ray_object(p, v, obj->o2));
    break;
  default:
    assert(0);
  }
#undef CONVERT_V_P
  return res;
}

/* Return the closest base object intersecting the given ray, and
   the curvilinear abscissa t of the intersection point.
   Return NULL if no intersection.
   Return t = 0.0 if the viewpoint (origin of the ray) is inside an
   object. */

struct object * intersect_ray(struct point * p,
                              struct vector * v,
                              struct object * obj,
                              int initial,
                              /*out*/ flt * t)
{
  intervlist l = intersect_ray_object(p, v, obj);
  if (l == NULL) return NULL;
  assert(l->beg >= 0.0);
  if (l->beg <= 0.0 && !initial) {
    /* skip first intersection */
    l = l->next;
    if (l == NULL) return NULL;
  }
  *t = l->beg;
  return l->obeg;
}