1// Copyright 2009-2021 Intel Corporation
2// SPDX-License-Identifier: Apache-2.0
3
4// ospray
5#include "common/Data.ih"
6#include "common/FilterIntersect.ih"
7#include "common/Intersect.ih"
8#include "common/Ray.ih"
9#include "common/World.ih"
10#include "geometry/Geometry.ih"
11#include "rkcommon/math/box.ih"
12#include "rkcommon/math/vec.ih"
13
14struct Boxes
15{
16  Geometry super;
17  Data1D boxes;
18};
19
20unmasked void Boxes_bounds(const RTCBoundsFunctionArguments *uniform args)
21{
22  Boxes *uniform self = (Boxes * uniform) args->geometryUserPtr;
23  uniform int primID = args->primID;
24
25  box3fa *uniform out = (box3fa * uniform) args->bounds_o;
26
27  *out = make_box3fa(get_box3f(self->boxes, primID));
28}
29
30void Boxes_intersect_kernel(const RTCIntersectFunctionNArguments *uniform args,
31    const uniform bool isOcclusionTest)
32{
33  // make sure to set the mask
34  if (!args->valid[programIndex])
35    return;
36
37  Boxes *uniform self = (Boxes * uniform) args->geometryUserPtr;
38  varying Ray *uniform ray = (varying Ray * uniform) args->rayhit;
39
40  uniform int primID = args->primID;
41
42  uniform box3f box = get_box3f(self->boxes, primID);
43  const Intersections isect = intersectBox(ray->org, ray->dir, box);
44
45  // call intersection filtering callback and setup hit if accepted
46  filterIntersectionBoth(args, isect, isOcclusionTest);
47}
48
49unmasked void Boxes_intersect(
50    const struct RTCIntersectFunctionNArguments *uniform args)
51{
52  Boxes_intersect_kernel(args, false);
53}
54
55unmasked void Boxes_occluded(
56    const struct RTCOccludedFunctionNArguments *uniform args)
57{
58  Boxes_intersect_kernel((RTCIntersectFunctionNArguments * uniform) args, true);
59}
60
61static void Boxes_postIntersect(const Geometry *uniform geometry,
62    varying DifferentialGeometry &dg,
63    const varying Ray &ray,
64    uniform int64 flags)
65{
66  dg.Ng = dg.Ns = ray.Ng;
67}
68
69export void *uniform Boxes_create()
70{
71  Boxes *uniform self = uniform new Boxes;
72
73  Geometry_Constructor(&self->super, Boxes_postIntersect);
74  Data1D_Constructor(&self->boxes);
75
76  return self;
77}
78
79export void *uniform Boxes_set(void *uniform _self,
80    void *uniform _embreeGeometry,
81    const Data1D *uniform boxes)
82{
83  Boxes *uniform self = (Boxes * uniform) _self;
84
85  self->boxes = *boxes;
86  self->super.numPrimitives = boxes->numItems;
87
88  Geometry_setEmbreeUserGeometry(&self->super,
89      (RTCGeometry)_embreeGeometry,
90      Boxes_bounds,
91      Boxes_intersect,
92      Boxes_occluded);
93}
94