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 * quadric.cpp - This file contains the functions for dealing with quadrics.
48 */
49
50 #include "machine.hpp"
51 #include "types.hpp"
52 #include "macros.hpp"
53 #include "quadric.hpp"
54 #include "vector.hpp"
55 #include "intersect.hpp"
56 #include "util.hpp"
57
quadric_bbox(void * obj,vector * min,vector * max)58 int quadric_bbox(void *obj, vector *min, vector *max) {
59 return 0;
60 }
61
62 static object_methods quadric_methods = { (void (*)(void *, void *))(quadric_intersect),
63 (void (*)(void *, void *, void *, void *))(
64 quadric_normal),
65 quadric_bbox,
66 free };
67
newquadric()68 quadric *newquadric() {
69 quadric *q;
70
71 q = (quadric *)rt_getmem(sizeof(quadric));
72 memset(q, 0, sizeof(quadric));
73 q->ctr.x = 0.0;
74 q->ctr.y = 0.0;
75 q->ctr.z = 0.0;
76 q->methods = &quadric_methods;
77
78 return q;
79 }
80
quadric_intersect(quadric * q,ray * ry)81 void quadric_intersect(quadric *q, ray *ry) {
82 flt Aq, Bq, Cq;
83 flt t1, t2;
84 flt disc;
85 vector rd;
86 vector ro;
87
88 rd = ry->d;
89 VNorm(&rd);
90
91 ro.x = ry->o.x - q->ctr.x;
92 ro.y = ry->o.y - q->ctr.y;
93 ro.z = ry->o.z - q->ctr.z;
94
95 Aq = (q->mat.a * (rd.x * rd.x)) + (2.0 * q->mat.b * rd.x * rd.y) +
96 (2.0 * q->mat.c * rd.x * rd.z) + (q->mat.e * (rd.y * rd.y)) +
97 (2.0 * q->mat.f * rd.y * rd.z) + (q->mat.h * (rd.z * rd.z));
98
99 Bq = 2.0 * ((q->mat.a * ro.x * rd.x) + (q->mat.b * ((ro.x * rd.y) + (rd.x * ro.y))) +
100 (q->mat.c * ((ro.x * rd.z) + (rd.x * ro.z))) + (q->mat.d * rd.x) +
101 (q->mat.e * ro.y * rd.y) + (q->mat.f * ((ro.y * rd.z) + (rd.y * ro.z))) +
102 (q->mat.g * rd.y) + (q->mat.h * ro.z * rd.z) + (q->mat.i * rd.z));
103
104 Cq = (q->mat.a * (ro.x * ro.x)) + (2.0 * q->mat.b * ro.x * ro.y) +
105 (2.0 * q->mat.c * ro.x * ro.z) + (2.0 * q->mat.d * ro.x) + (q->mat.e * (ro.y * ro.y)) +
106 (2.0 * q->mat.f * ro.y * ro.z) + (2.0 * q->mat.g * ro.y) + (q->mat.h * (ro.z * ro.z)) +
107 (2.0 * q->mat.i * ro.z) + q->mat.j;
108
109 if (Aq == 0.0) {
110 t1 = -Cq / Bq;
111 add_intersection(t1, (object *)q, ry);
112 }
113 else {
114 disc = (Bq * Bq - 4.0 * Aq * Cq);
115 if (disc > 0.0) {
116 disc = sqrt(disc);
117 t1 = (-Bq + disc) / (2.0 * Aq);
118 t2 = (-Bq - disc) / (2.0 * Aq);
119 add_intersection(t1, (object *)q, ry);
120 add_intersection(t2, (object *)q, ry);
121 }
122 }
123 }
124
quadric_normal(quadric * q,vector * pnt,ray * incident,vector * N)125 void quadric_normal(quadric *q, vector *pnt, ray *incident, vector *N) {
126 N->x = (q->mat.a * (pnt->x - q->ctr.x) + q->mat.b * (pnt->y - q->ctr.y) +
127 q->mat.c * (pnt->z - q->ctr.z) + q->mat.d);
128
129 N->y = (q->mat.b * (pnt->x - q->ctr.x) + q->mat.e * (pnt->y - q->ctr.y) +
130 q->mat.f * (pnt->z - q->ctr.z) + q->mat.g);
131
132 N->z = (q->mat.c * (pnt->x - q->ctr.x) + q->mat.f * (pnt->y - q->ctr.y) +
133 q->mat.h * (pnt->z - q->ctr.z) + q->mat.i);
134
135 VNorm(N);
136
137 if (VDot(N, &(incident->d)) > 0.0) {
138 N->x = -N->x;
139 N->y = -N->y;
140 N->z = -N->z;
141 }
142 }
143