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
|
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "bezier_curve.h"
namespace embree
{
namespace isa
{
template<typename V>
struct TensorLinearQuadraticBezierSurface
{
QuadraticBezierCurve<V> L;
QuadraticBezierCurve<V> R;
__forceinline TensorLinearQuadraticBezierSurface() {}
__forceinline TensorLinearQuadraticBezierSurface(const TensorLinearQuadraticBezierSurface<V>& curve)
: L(curve.L), R(curve.R) {}
__forceinline TensorLinearQuadraticBezierSurface& operator= (const TensorLinearQuadraticBezierSurface& other) {
L = other.L; R = other.R; return *this;
}
__forceinline TensorLinearQuadraticBezierSurface(const QuadraticBezierCurve<V>& L, const QuadraticBezierCurve<V>& R)
: L(L), R(R) {}
__forceinline BBox<V> bounds() const {
return merge(L.bounds(),R.bounds());
}
};
template<>
struct TensorLinearQuadraticBezierSurface<Vec2fa>
{
QuadraticBezierCurve<vfloat4> LR;
__forceinline TensorLinearQuadraticBezierSurface() {}
__forceinline TensorLinearQuadraticBezierSurface(const TensorLinearQuadraticBezierSurface<Vec2fa>& curve)
: LR(curve.LR) {}
__forceinline TensorLinearQuadraticBezierSurface& operator= (const TensorLinearQuadraticBezierSurface& other) {
LR = other.LR; return *this;
}
__forceinline TensorLinearQuadraticBezierSurface(const QuadraticBezierCurve<vfloat4>& LR)
: LR(LR) {}
__forceinline BBox<Vec2fa> bounds() const
{
const BBox<vfloat4> b = LR.bounds();
const BBox<Vec2fa> bl(Vec2fa(b.lower),Vec2fa(b.upper));
const BBox<Vec2fa> br(Vec2fa(shuffle<2,3,2,3>(b.lower)),Vec2fa(shuffle<2,3,2,3>(b.upper)));
return merge(bl,br);
}
};
template<typename V>
struct TensorLinearCubicBezierSurface
{
CubicBezierCurve<V> L;
CubicBezierCurve<V> R;
__forceinline TensorLinearCubicBezierSurface() {}
__forceinline TensorLinearCubicBezierSurface(const TensorLinearCubicBezierSurface& curve)
: L(curve.L), R(curve.R) {}
__forceinline TensorLinearCubicBezierSurface& operator= (const TensorLinearCubicBezierSurface& other) {
L = other.L; R = other.R; return *this;
}
__forceinline TensorLinearCubicBezierSurface(const CubicBezierCurve<V>& L, const CubicBezierCurve<V>& R)
: L(L), R(R) {}
template<template<typename T> class SourceCurve>
__forceinline static TensorLinearCubicBezierSurface fromCenterAndNormalCurve(const SourceCurve<Vec3ff>& center, const SourceCurve<Vec3fa>& normal)
{
SourceCurve<Vec3ff> vcurve = center;
SourceCurve<Vec3fa> ncurve = normal;
/* here we construct a patch which follows the curve l(t) =
* p(t) +/- r(t)*normalize(cross(n(t),dp(t))) */
const Vec3ff p0 = vcurve.eval(0.0f);
const Vec3ff dp0 = vcurve.eval_du(0.0f);
//const Vec3ff ddp0 = vcurve.eval_dudu(0.0f); // ddp0 is assumed to be 0
const Vec3fa n0 = ncurve.eval(0.0f);
const Vec3fa dn0 = ncurve.eval_du(0.0f);
const Vec3ff p1 = vcurve.eval(1.0f);
const Vec3ff dp1 = vcurve.eval_du(1.0f);
//const Vec3ff ddp1 = vcurve.eval_dudu(1.0f); // ddp1 is assumed to be 0
const Vec3fa n1 = ncurve.eval(1.0f);
const Vec3fa dn1 = ncurve.eval_du(1.0f);
const Vec3fa bt0 = cross(n0,dp0);
const Vec3fa dbt0 = cross(dn0,dp0);// + cross(n0,ddp0);
const Vec3fa bt1 = cross(n1,dp1);
const Vec3fa dbt1 = cross(dn1,dp1);// + cross(n1,ddp1);
const Vec3fa k0 = normalize(bt0);
const Vec3fa dk0 = dnormalize(bt0,dbt0);
const Vec3fa k1 = normalize(bt1);
const Vec3fa dk1 = dnormalize(bt1,dbt1);
const Vec3fa l0 = p0 - p0.w*k0;
const Vec3fa dl0 = dp0 - (dp0.w*k0 + p0.w*dk0);
const Vec3fa r0 = p0 + p0.w*k0;
const Vec3fa dr0 = dp0 + (dp0.w*k0 + p0.w*dk0);
const Vec3fa l1 = p1 - p1.w*k1;
const Vec3fa dl1 = dp1 - (dp1.w*k1 + p1.w*dk1);
const Vec3fa r1 = p1 + p1.w*k1;
const Vec3fa dr1 = dp1 + (dp1.w*k1 + p1.w*dk1);
const float scale = 1.0f/3.0f;
CubicBezierCurve<V> L(l0,l0+scale*dl0,l1-scale*dl1,l1);
CubicBezierCurve<V> R(r0,r0+scale*dr0,r1-scale*dr1,r1);
return TensorLinearCubicBezierSurface(L,R);
}
__forceinline BBox<V> bounds() const {
return merge(L.bounds(),R.bounds());
}
__forceinline BBox3fa accurateBounds() const {
return merge(L.accurateBounds(),R.accurateBounds());
}
__forceinline CubicBezierCurve<Interval1f> reduce_v() const {
return merge(CubicBezierCurve<Interval<V>>(L),CubicBezierCurve<Interval<V>>(R));
}
__forceinline LinearBezierCurve<Interval1f> reduce_u() const {
return LinearBezierCurve<Interval1f>(L.bounds(),R.bounds());
}
__forceinline TensorLinearCubicBezierSurface<float> xfm(const V& dx) const {
return TensorLinearCubicBezierSurface<float>(L.xfm(dx),R.xfm(dx));
}
__forceinline TensorLinearCubicBezierSurface<vfloatx> vxfm(const V& dx) const {
return TensorLinearCubicBezierSurface<vfloatx>(L.vxfm(dx),R.vxfm(dx));
}
__forceinline TensorLinearCubicBezierSurface<float> xfm(const V& dx, const V& p) const {
return TensorLinearCubicBezierSurface<float>(L.xfm(dx,p),R.xfm(dx,p));
}
__forceinline TensorLinearCubicBezierSurface<Vec3fa> xfm(const LinearSpace3fa& space) const {
return TensorLinearCubicBezierSurface(L.xfm(space),R.xfm(space));
}
__forceinline TensorLinearCubicBezierSurface<Vec3fa> xfm(const LinearSpace3fa& space, const Vec3fa& p) const {
return TensorLinearCubicBezierSurface(L.xfm(space,p),R.xfm(space,p));
}
__forceinline TensorLinearCubicBezierSurface<Vec3fa> xfm(const LinearSpace3fa& space, const Vec3fa& p, const float s) const {
return TensorLinearCubicBezierSurface(L.xfm(space,p,s),R.xfm(space,p,s));
}
__forceinline TensorLinearCubicBezierSurface clip_u(const Interval1f& u) const {
return TensorLinearCubicBezierSurface(L.clip(u),R.clip(u));
}
__forceinline TensorLinearCubicBezierSurface clip_v(const Interval1f& v) const {
return TensorLinearCubicBezierSurface(clerp(L,R,V(v.lower)),clerp(L,R,V(v.upper)));
}
__forceinline TensorLinearCubicBezierSurface clip(const Interval1f& u, const Interval1f& v) const {
return clip_v(v).clip_u(u);
}
__forceinline void split_u(TensorLinearCubicBezierSurface& left, TensorLinearCubicBezierSurface& right, const float u = 0.5f) const
{
CubicBezierCurve<V> L0,L1; L.split(L0,L1,u);
CubicBezierCurve<V> R0,R1; R.split(R0,R1,u);
new (&left ) TensorLinearCubicBezierSurface(L0,R0);
new (&right) TensorLinearCubicBezierSurface(L1,R1);
}
__forceinline TensorLinearCubicBezierSurface<Vec2vfx> vsplit_u(vboolx& valid, const BBox1f& u) const {
valid = true; clear(valid,VSIZEX-1);
return TensorLinearCubicBezierSurface<Vec2vfx>(L.split(u),R.split(u));
}
__forceinline V eval(const float u, const float v) const {
return clerp(L,R,V(v)).eval(u);
}
__forceinline V eval_du(const float u, const float v) const {
return clerp(L,R,V(v)).eval_dt(u);
}
__forceinline V eval_dv(const float u, const float v) const {
return (R-L).eval(u);
}
__forceinline void eval(const float u, const float v, V& p, V& dpdu, V& dpdv) const
{
V p0, dp0du; L.eval(u,p0,dp0du);
V p1, dp1du; R.eval(u,p1,dp1du);
p = lerp(p0,p1,v);
dpdu = lerp(dp0du,dp1du,v);
dpdv = p1-p0;
}
__forceinline TensorLinearQuadraticBezierSurface<V> derivative_u() const {
return TensorLinearQuadraticBezierSurface<V>(L.derivative(),R.derivative());
}
__forceinline CubicBezierCurve<V> derivative_v() const {
return R-L;
}
__forceinline V axis_u() const {
return (L.end()-L.begin())+(R.end()-R.begin());
}
__forceinline V axis_v() const {
return (R.begin()-L.begin())+(R.end()-L.end());
}
friend embree_ostream operator<<(embree_ostream cout, const TensorLinearCubicBezierSurface& a)
{
return cout << "TensorLinearCubicBezierSurface" << embree_endl
<< "{" << embree_endl
<< " L = " << a.L << ", " << embree_endl
<< " R = " << a.R << embree_endl
<< "}";
}
friend __forceinline TensorLinearCubicBezierSurface clerp(const TensorLinearCubicBezierSurface& a, const TensorLinearCubicBezierSurface& b, const float t) {
return TensorLinearCubicBezierSurface(clerp(a.L,b.L,V(t)), clerp(a.R,b.R,V(t)));
}
};
template<>
struct TensorLinearCubicBezierSurface<Vec2fa>
{
CubicBezierCurve<vfloat4> LR;
__forceinline TensorLinearCubicBezierSurface() {}
__forceinline TensorLinearCubicBezierSurface(const TensorLinearCubicBezierSurface& curve)
: LR(curve.LR) {}
__forceinline TensorLinearCubicBezierSurface& operator= (const TensorLinearCubicBezierSurface& other) {
LR = other.LR; return *this;
}
__forceinline TensorLinearCubicBezierSurface(const CubicBezierCurve<vfloat4>& LR)
: LR(LR) {}
__forceinline TensorLinearCubicBezierSurface(const CubicBezierCurve<Vec2fa>& L, const CubicBezierCurve<Vec2fa>& R)
: LR(shuffle<0,1,0,1>(vfloat4(L.v0),vfloat4(R.v0)),shuffle<0,1,0,1>(vfloat4(L.v1),vfloat4(R.v1)),shuffle<0,1,0,1>(vfloat4(L.v2),vfloat4(R.v2)),shuffle<0,1,0,1>(vfloat4(L.v3),vfloat4(R.v3))) {}
__forceinline CubicBezierCurve<Vec2fa> getL() const {
return CubicBezierCurve<Vec2fa>(Vec2fa(LR.v0),Vec2fa(LR.v1),Vec2fa(LR.v2),Vec2fa(LR.v3));
}
__forceinline CubicBezierCurve<Vec2fa> getR() const {
return CubicBezierCurve<Vec2fa>(Vec2fa(shuffle<2,3,2,3>(LR.v0)),Vec2fa(shuffle<2,3,2,3>(LR.v1)),Vec2fa(shuffle<2,3,2,3>(LR.v2)),Vec2fa(shuffle<2,3,2,3>(LR.v3)));
}
__forceinline BBox<Vec2fa> bounds() const
{
const BBox<vfloat4> b = LR.bounds();
const BBox<Vec2fa> bl(Vec2fa(b.lower),Vec2fa(b.upper));
const BBox<Vec2fa> br(Vec2fa(shuffle<2,3,2,3>(b.lower)),Vec2fa(shuffle<2,3,2,3>(b.upper)));
return merge(bl,br);
}
__forceinline BBox1f bounds(const Vec2fa& axis) const
{
const CubicBezierCurve<vfloat4> LRx = LR;
const CubicBezierCurve<vfloat4> LRy(shuffle<1,0,3,2>(LR.v0),shuffle<1,0,3,2>(LR.v1),shuffle<1,0,3,2>(LR.v2),shuffle<1,0,3,2>(LR.v3));
const CubicBezierCurve<vfloat4> LRa = cmadd(shuffle<0>(vfloat4(axis)),LRx,shuffle<1>(vfloat4(axis))*LRy);
const BBox<vfloat4> Lb = LRa.bounds();
const BBox<vfloat4> Rb(shuffle<3>(Lb.lower),shuffle<3>(Lb.upper));
const BBox<vfloat4> b = merge(Lb,Rb);
return BBox1f(b.lower[0],b.upper[0]);
}
__forceinline TensorLinearCubicBezierSurface<float> xfm(const Vec2fa& dx) const
{
const CubicBezierCurve<vfloat4> LRx = LR;
const CubicBezierCurve<vfloat4> LRy(shuffle<1,0,3,2>(LR.v0),shuffle<1,0,3,2>(LR.v1),shuffle<1,0,3,2>(LR.v2),shuffle<1,0,3,2>(LR.v3));
const CubicBezierCurve<vfloat4> LRa = cmadd(shuffle<0>(vfloat4(dx)),LRx,shuffle<1>(vfloat4(dx))*LRy);
return TensorLinearCubicBezierSurface<float>(CubicBezierCurve<float>(LRa.v0[0],LRa.v1[0],LRa.v2[0],LRa.v3[0]),
CubicBezierCurve<float>(LRa.v0[2],LRa.v1[2],LRa.v2[2],LRa.v3[2]));
}
__forceinline TensorLinearCubicBezierSurface<float> xfm(const Vec2fa& dx, const Vec2fa& p) const
{
const vfloat4 pxyxy = shuffle<0,1,0,1>(vfloat4(p));
const CubicBezierCurve<vfloat4> LRx = LR-pxyxy;
const CubicBezierCurve<vfloat4> LRy(shuffle<1,0,3,2>(LR.v0),shuffle<1,0,3,2>(LR.v1),shuffle<1,0,3,2>(LR.v2),shuffle<1,0,3,2>(LR.v3));
const CubicBezierCurve<vfloat4> LRa = cmadd(shuffle<0>(vfloat4(dx)),LRx,shuffle<1>(vfloat4(dx))*LRy);
return TensorLinearCubicBezierSurface<float>(CubicBezierCurve<float>(LRa.v0[0],LRa.v1[0],LRa.v2[0],LRa.v3[0]),
CubicBezierCurve<float>(LRa.v0[2],LRa.v1[2],LRa.v2[2],LRa.v3[2]));
}
__forceinline TensorLinearCubicBezierSurface clip_u(const Interval1f& u) const {
return TensorLinearCubicBezierSurface(LR.clip(u));
}
__forceinline TensorLinearCubicBezierSurface clip_v(const Interval1f& v) const
{
const CubicBezierCurve<vfloat4> LL(shuffle<0,1,0,1>(LR.v0),shuffle<0,1,0,1>(LR.v1),shuffle<0,1,0,1>(LR.v2),shuffle<0,1,0,1>(LR.v3));
const CubicBezierCurve<vfloat4> RR(shuffle<2,3,2,3>(LR.v0),shuffle<2,3,2,3>(LR.v1),shuffle<2,3,2,3>(LR.v2),shuffle<2,3,2,3>(LR.v3));
return TensorLinearCubicBezierSurface(clerp(LL,RR,vfloat4(v.lower,v.lower,v.upper,v.upper)));
}
__forceinline TensorLinearCubicBezierSurface clip(const Interval1f& u, const Interval1f& v) const {
return clip_v(v).clip_u(u);
}
__forceinline void split_u(TensorLinearCubicBezierSurface& left, TensorLinearCubicBezierSurface& right, const float u = 0.5f) const
{
CubicBezierCurve<vfloat4> LR0,LR1; LR.split(LR0,LR1,u);
new (&left ) TensorLinearCubicBezierSurface(LR0);
new (&right) TensorLinearCubicBezierSurface(LR1);
}
__forceinline TensorLinearCubicBezierSurface<Vec2vfx> vsplit_u(vboolx& valid, const BBox1f& u) const {
valid = true; clear(valid,VSIZEX-1);
return TensorLinearCubicBezierSurface<Vec2vfx>(getL().split(u),getR().split(u));
}
__forceinline Vec2fa eval(const float u, const float v) const
{
const vfloat4 p = LR.eval(u);
return Vec2fa(lerp(shuffle<0,1,0,1>(p),shuffle<2,3,2,3>(p),v));
}
__forceinline Vec2fa eval_du(const float u, const float v) const
{
const vfloat4 dpdu = LR.eval_dt(u);
return Vec2fa(lerp(shuffle<0,1,0,1>(dpdu),shuffle<2,3,2,3>(dpdu),v));
}
__forceinline Vec2fa eval_dv(const float u, const float v) const
{
const vfloat4 p = LR.eval(u);
return Vec2fa(shuffle<2,3,2,3>(p)-shuffle<0,1,0,1>(p));
}
__forceinline void eval(const float u, const float v, Vec2fa& p, Vec2fa& dpdu, Vec2fa& dpdv) const
{
vfloat4 p0, dp0du; LR.eval(u,p0,dp0du);
p = Vec2fa(lerp(shuffle<0,1,0,1>(p0),shuffle<2,3,2,3>(p0),v));
dpdu = Vec2fa(lerp(shuffle<0,1,0,1>(dp0du),shuffle<2,3,2,3>(dp0du),v));
dpdv = Vec2fa(shuffle<2,3,2,3>(p0)-shuffle<0,1,0,1>(p0));
}
__forceinline TensorLinearQuadraticBezierSurface<Vec2fa> derivative_u() const {
return TensorLinearQuadraticBezierSurface<Vec2fa>(LR.derivative());
}
__forceinline CubicBezierCurve<Vec2fa> derivative_v() const {
return getR()-getL();
}
__forceinline Vec2fa axis_u() const
{
const CubicBezierCurve<Vec2fa> L = getL();
const CubicBezierCurve<Vec2fa> R = getR();
return (L.end()-L.begin())+(R.end()-R.begin());
}
__forceinline Vec2fa axis_v() const
{
const CubicBezierCurve<Vec2fa> L = getL();
const CubicBezierCurve<Vec2fa> R = getR();
return (R.begin()-L.begin())+(R.end()-L.end());
}
friend embree_ostream operator<<(embree_ostream cout, const TensorLinearCubicBezierSurface& a)
{
return cout << "TensorLinearCubicBezierSurface" << embree_endl
<< "{" << embree_endl
<< " L = " << a.getL() << ", " << embree_endl
<< " R = " << a.getR() << embree_endl
<< "}";
}
};
typedef TensorLinearCubicBezierSurface<float> TensorLinearCubicBezierSurface1f;
typedef TensorLinearCubicBezierSurface<Vec2fa> TensorLinearCubicBezierSurface2fa;
typedef TensorLinearCubicBezierSurface<Vec3fa> TensorLinearCubicBezierSurface3fa;
}
}
|