1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 /*
18     The original source for this example is
19     Copyright (c) 1994-2008 John E. Stone
20     All rights reserved.
21 
22     Redistribution and use in source and binary forms, with or without
23     modification, are permitted provided that the following conditions
24     are met:
25     1. Redistributions of source code must retain the above copyright
26        notice, this list of conditions and the following disclaimer.
27     2. Redistributions in binary form must reproduce the above copyright
28        notice, this list of conditions and the following disclaimer in the
29        documentation and/or other materials provided with the distribution.
30     3. The name of the author may not be used to endorse or promote products
31        derived from this software without specific prior written permission.
32 
33     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
34     OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
37     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43     SUCH DAMAGE.
44 */
45 
46 /*
47  * cylinder.cpp - This file contains the functions for dealing with cylinders.
48  */
49 
50 #include "machine.hpp"
51 #include "types.hpp"
52 #include "macros.hpp"
53 #include "vector.hpp"
54 #include "intersect.hpp"
55 #include "util.hpp"
56 
57 #define CYLINDER_PRIVATE
58 #include "cylinder.hpp"
59 
60 static object_methods cylinder_methods = { (void (*)(void *, void *))(cylinder_intersect),
61                                            (void (*)(void *, void *, void *, void *))(
62                                                cylinder_normal),
63                                            cylinder_bbox,
64                                            free };
65 
66 static object_methods fcylinder_methods = { (void (*)(void *, void *))(fcylinder_intersect),
67                                             (void (*)(void *, void *, void *, void *))(
68                                                 cylinder_normal),
69                                             fcylinder_bbox,
70                                             free };
71 
newcylinder(void * tex,vector ctr,vector axis,flt rad)72 object *newcylinder(void *tex, vector ctr, vector axis, flt rad) {
73     cylinder *c;
74 
75     c = (cylinder *)rt_getmem(sizeof(cylinder));
76     memset(c, 0, sizeof(cylinder));
77     c->methods = &cylinder_methods;
78 
79     c->tex = (texture *)tex;
80     c->ctr = ctr;
81     c->axis = axis;
82     c->rad = rad;
83     return (object *)c;
84 }
85 
cylinder_bbox(void * obj,vector * min,vector * max)86 static int cylinder_bbox(void *obj, vector *min, vector *max) {
87     return 0; /* infinite / unbounded object */
88 }
89 
cylinder_intersect(cylinder * cyl,ray * ry)90 static void cylinder_intersect(cylinder *cyl, ray *ry) {
91     vector rc, n, D, O;
92     flt t, s, tin, tout, ln, d;
93 
94     rc.x = ry->o.x - cyl->ctr.x;
95     rc.y = ry->o.y - cyl->ctr.y;
96     rc.z = ry->o.z - cyl->ctr.z;
97 
98     VCross(&ry->d, &cyl->axis, &n);
99 
100     VDOT(ln, n, n);
101     ln = sqrt(ln); /* finish length calculation */
102 
103     if (ln == 0.0) { /* ray is parallel to the cylinder.. */
104         VDOT(d, rc, cyl->axis);
105         D.x = rc.x - d * cyl->axis.x;
106         D.y = rc.y - d * cyl->axis.y;
107         D.z = rc.z - d * cyl->axis.z;
108         VDOT(d, D, D);
109         d = sqrt(d);
110         tin = -FHUGE;
111         tout = FHUGE;
112         /* if (d <= cyl->rad) then ray is inside cylinder.. else outside */
113     }
114 
115     VNorm(&n);
116     VDOT(d, rc, n);
117     d = fabs(d);
118 
119     if (d <= cyl->rad) { /* ray intersects cylinder.. */
120         VCross(&rc, &cyl->axis, &O);
121         VDOT(t, O, n);
122         t = -t / ln;
123         VCross(&n, &cyl->axis, &O);
124         VNorm(&O);
125         VDOT(s, ry->d, O);
126         s = fabs(sqrt(cyl->rad * cyl->rad - d * d) / s);
127         tin = t - s;
128         add_intersection(tin, (object *)cyl, ry);
129         tout = t + s;
130         add_intersection(tout, (object *)cyl, ry);
131     }
132 }
133 
cylinder_normal(cylinder * cyl,vector * pnt,ray * incident,vector * N)134 static void cylinder_normal(cylinder *cyl, vector *pnt, ray *incident, vector *N) {
135     vector a, b, c;
136     flt t;
137 
138     VSub((vector *)pnt, &(cyl->ctr), &a);
139 
140     c = cyl->axis;
141 
142     VNorm(&c);
143 
144     VDOT(t, a, c);
145 
146     b.x = c.x * t + cyl->ctr.x;
147     b.y = c.y * t + cyl->ctr.y;
148     b.z = c.z * t + cyl->ctr.z;
149 
150     VSub(pnt, &b, N);
151     VNorm(N);
152 
153     if (VDot(N, &(incident->d)) > 0.0) { /* make cylinder double sided */
154         N->x = -N->x;
155         N->y = -N->y;
156         N->z = -N->z;
157     }
158 }
159 
newfcylinder(void * tex,vector ctr,vector axis,flt rad)160 object *newfcylinder(void *tex, vector ctr, vector axis, flt rad) {
161     cylinder *c;
162 
163     c = (cylinder *)rt_getmem(sizeof(cylinder));
164     memset(c, 0, sizeof(cylinder));
165     c->methods = &fcylinder_methods;
166 
167     c->tex = (texture *)tex;
168     c->ctr = ctr;
169     c->axis = axis;
170     c->rad = rad;
171 
172     return (object *)c;
173 }
174 
fcylinder_bbox(void * obj,vector * min,vector * max)175 static int fcylinder_bbox(void *obj, vector *min, vector *max) {
176     cylinder *c = (cylinder *)obj;
177     vector mintmp, maxtmp;
178 
179     mintmp.x = c->ctr.x;
180     mintmp.y = c->ctr.y;
181     mintmp.z = c->ctr.z;
182     maxtmp.x = c->ctr.x + c->axis.x;
183     maxtmp.y = c->ctr.y + c->axis.y;
184     maxtmp.z = c->ctr.z + c->axis.z;
185 
186     min->x = MYMIN(mintmp.x, maxtmp.x);
187     min->y = MYMIN(mintmp.y, maxtmp.y);
188     min->z = MYMIN(mintmp.z, maxtmp.z);
189     min->x -= c->rad;
190     min->y -= c->rad;
191     min->z -= c->rad;
192 
193     max->x = MYMAX(mintmp.x, maxtmp.x);
194     max->y = MYMAX(mintmp.y, maxtmp.y);
195     max->z = MYMAX(mintmp.z, maxtmp.z);
196     max->x += c->rad;
197     max->y += c->rad;
198     max->z += c->rad;
199 
200     return 1;
201 }
202 
fcylinder_intersect(cylinder * cyl,ray * ry)203 static void fcylinder_intersect(cylinder *cyl, ray *ry) {
204     vector rc, n, O, hit, tmp2, ctmp4;
205     flt t, s, tin, tout, ln, d, tmp, tmp3;
206 
207     rc.x = ry->o.x - cyl->ctr.x;
208     rc.y = ry->o.y - cyl->ctr.y;
209     rc.z = ry->o.z - cyl->ctr.z;
210 
211     VCross(&ry->d, &cyl->axis, &n);
212 
213     VDOT(ln, n, n);
214     ln = sqrt(ln); /* finish length calculation */
215 
216     if (ln == 0.0) { /* ray is parallel to the cylinder.. */
217         return; /* in this case, we want to miss or go through the "hole" */
218     }
219 
220     VNorm(&n);
221     VDOT(d, rc, n);
222     d = fabs(d);
223 
224     if (d <= cyl->rad) { /* ray intersects cylinder.. */
225         VCross(&rc, &cyl->axis, &O);
226         VDOT(t, O, n);
227         t = -t / ln;
228         VCross(&n, &cyl->axis, &O);
229         VNorm(&O);
230         VDOT(s, ry->d, O);
231         s = fabs(sqrt(cyl->rad * cyl->rad - d * d) / s);
232         tin = t - s;
233 
234         RAYPNT(hit, (*ry), tin);
235 
236         ctmp4 = cyl->axis;
237         VNorm(&ctmp4);
238 
239         tmp2.x = hit.x - cyl->ctr.x;
240         tmp2.y = hit.y - cyl->ctr.y;
241         tmp2.z = hit.z - cyl->ctr.z;
242 
243         VDOT(tmp, tmp2, ctmp4);
244         VDOT(tmp3, cyl->axis, cyl->axis);
245 
246         if ((tmp > 0.0) && (tmp < sqrt(tmp3)))
247             add_intersection(tin, (object *)cyl, ry);
248         tout = t + s;
249 
250         RAYPNT(hit, (*ry), tout);
251 
252         tmp2.x = hit.x - cyl->ctr.x;
253         tmp2.y = hit.y - cyl->ctr.y;
254         tmp2.z = hit.z - cyl->ctr.z;
255 
256         VDOT(tmp, tmp2, ctmp4);
257         VDOT(tmp3, cyl->axis, cyl->axis);
258 
259         if ((tmp > 0.0) && (tmp < sqrt(tmp3)))
260             add_intersection(tout, (object *)cyl, ry);
261     }
262 }
263