-
Notifications
You must be signed in to change notification settings - Fork 2
/
DeviceCopyableObjects.h
2089 lines (1786 loc) · 51.7 KB
/
DeviceCopyableObjects.h
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
// visionaray
#include "visionaray/bvh.h"
#include "visionaray/directional_light.h"
#include "visionaray/matrix_camera.h"
#include "visionaray/area_light.h"
#include "visionaray/point_light.h"
#include "visionaray/spot_light.h"
#include "visionaray/thin_lens_camera.h"
#if defined(WITH_CUDA)
#include "visionaray/texture/cuda_texture.h"
#elif defined(WITH_HIP)
#include "visionaray/texture/hip_texture.h"
#else
#include "visionaray/texture/texture.h"
#endif
// ours
#include "frame/common.h"
#include "renderer/DDA.h"
#include "scene/volume/spatial_field/Plane.h"
#include "scene/volume/spatial_field/UElems.h"
#include "scene/volume/spatial_field/UElemGrid.h"
#include "common.h"
#include "sampleCDF.h"
#if defined(WITH_CUDA) && !defined(__CUDACC__)
#include <visionaray/cuda/device_vector.h>
namespace visionaray {
// visionaray only defines these when compiling with nvcc:
template <typename P>
using cuda_bvh = bvh_t<cuda::device_vector<P>, cuda::device_vector<bvh_node>>;
template <typename P>
using cuda_index_bvh = index_bvh_t<cuda::device_vector<P>, cuda::device_vector<bvh_node>, cuda::device_vector<unsigned>>;
} // namespace visionaray
#endif
#if defined(WITH_HIP) && !defined(__HIPCC__)
#include <visionaray/hip/device_vector.h>
namespace visionaray {
// visionaray only defines these when compiling with hipcc:
template <typename P>
using hip_bvh = bvh_t<hip::device_vector<P>, hip::device_vector<bvh_node>>;
template <typename P>
using hip_index_bvh = index_bvh_t<hip::device_vector<P>, hip::device_vector<bvh_node>, hip::device_vector<unsigned>>;
} // namespace visionaray
#endif
namespace visionaray {
namespace dco {
typedef uint32_t Handle;
VSNRAY_FUNC
inline bool validHandle(Handle hnd)
{ return hnd < UINT_MAX; }
} // namespace dco
typedef dco::Handle DeviceObjectHandle;
// Ray //
struct Ray : basic_ray<float>
{
enum IntersectionMask {
All = 0xffffffff,
Triangle = 0x1,
Quad = 0x2,
Sphere = 0x4,
Cone = 0x8,
Cylinder = 0x10,
Curve = 0x20,
BezierCurve = 0x40,
ISOSurface = 0x80,
Volume = 0x100,
VolumeBounds = 0x200,
};
unsigned intersectionMask = All;
float time{0.f};
void *prd{nullptr};
#if 1
bool dbg{false};
VSNRAY_FUNC inline bool debug() const {
return dbg;
}
#endif
};
} // namespace visionaray
namespace visionaray::dco {
// Unstructured element primitive //
struct UElem
{
uint64_t begin;
uint64_t end;
uint64_t elemID;
const uint64_t *indexBuffer;
float4 *vertexBuffer;
// "stitcher" extension
int3 *gridDimsBuffer;
aabb *gridDomainsBuffer;
uint64_t *gridScalarsOffsetBuffer;
float *gridScalarsBuffer;
};
VSNRAY_FUNC
inline aabb get_bounds(const UElem &elem)
{
aabb result;
if (elem.end-elem.begin > 0) {
result.invalidate();
for (uint64_t i=elem.begin;i<elem.end;++i) {
result.insert(elem.vertexBuffer[elem.indexBuffer[i]].xyz());
}
} else { // no vertices -> voxel grid
result = elem.gridDomainsBuffer[elem.elemID];
}
return result;
}
inline void split_primitive(aabb &L, aabb &R, float plane, int axis, const UElem &elem)
{
assert(0);
}
VSNRAY_FUNC
inline hit_record<Ray, primitive<unsigned>> intersect(
const Ray &ray, const UElem &elem)
{
hit_record<Ray, primitive<unsigned>> result;
float3 pos = ray.ori;
float value = 0.f;
uint64_t numVerts = elem.end-elem.begin;
if (numVerts > 0) { // regular uelem
float4 v[8];
for (int i=0; i<numVerts; ++i) {
uint64_t idx = elem.indexBuffer[elem.begin+i];
v[i] = elem.vertexBuffer[idx];
}
bool hit=numVerts==4 && intersectTet(value,pos,v[0],v[1],v[2],v[3])
|| numVerts==5 && intersectPyrEXT(value,pos,v[0],v[1],v[2],v[3],v[4])
|| numVerts==6 && intersectWedgeEXT(value,pos,v[0],v[1],v[2],v[3],v[4],v[5])
|| numVerts==8 && intersectHexEXT(value,pos,v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7]);
result.hit = hit;
} else {
// element is a voxel grid (for "stitcher" AMR data)
int3 dims = elem.gridDimsBuffer[elem.elemID];
aabb domain = elem.gridDomainsBuffer[elem.elemID];
uint64_t scalarsOffset = elem.gridScalarsOffsetBuffer[elem.elemID];
bool hit = intersectGrid(dims, domain, scalarsOffset, elem.gridScalarsBuffer,
pos, value);
result.hit = hit;
}
if (result.hit) {
result.t = 0.f;
result.prim_id = elem.elemID;
result.u = value; // misuse "u" to store value
}
return result;
}
// Block primitive //
struct Block
{
uint32_t ID{UINT_MAX};
aabbi bounds;
int level;
uint32_t scalarOffset;
box1 valueRange;
float *scalarsBuffer{nullptr};
VSNRAY_FUNC
float getScalar(int ix, int iy, int iz) const
{
const int3 blockSize = numCells();
const uint32_t idx
= scalarOffset
+ ix
+ iy * blockSize.x
+ iz * blockSize.x*blockSize.y;
return scalarsBuffer[idx];
}
VSNRAY_FUNC
int cellSize() const
{ return 1<<level; }
VSNRAY_FUNC
int3 numCells() const
{ return bounds.max-bounds.min+int3(1); }
VSNRAY_FUNC
aabb worldBounds() const
{
return aabb(
float3(bounds.min)*float(cellSize()),
float3(bounds.max+int3(1))*float(cellSize())
);
}
VSNRAY_FUNC
aabb filterDomain() const
{
const float3 cellSize2(cellSize()*0.5f);
const aabb wb = worldBounds();
return aabb(wb.min-cellSize2, wb.max+cellSize2);
}
VSNRAY_FUNC
aabb cellBounds(const vec3i cellID) const
{
aabb cb;
cb.min = float3(bounds.min+cellID)*float(cellSize());
cb.max = float3(bounds.max+cellID+int3(1))*float(cellSize());
return cb;
}
};
VSNRAY_FUNC
inline aabb get_bounds(const Block &block)
{
return block.filterDomain();
}
inline void split_primitive(aabb &L, aabb &R, float plane, int axis, const Block &block)
{
assert(0);
}
VSNRAY_FUNC
inline hit_record<Ray, primitive<unsigned>> intersect(
const Ray &ray, const Block &block)
{
hit_record<Ray, primitive<unsigned>> result;
float3 pos = ray.ori;
if (!block.filterDomain().contains(pos)) {
result.hit = false;
return result;
}
result.t = 0.f;
result.hit = true;
float *prd = (float *)ray.prd;
float &sumWeightedValues = prd[0];
float &sumWeights = prd[1];
const float3 P = ray.ori;
const aabb brickBounds = block.worldBounds();
const int3 blockSize = block.numCells();
const float3 localPos = (P-brickBounds.min) / float3(block.cellSize()) - 0.5f;
int3 idx_lo = int3(floorf(localPos.x),floorf(localPos.y),floorf(localPos.z));
idx_lo = max(int3(-1), idx_lo);
const int3 idx_hi = idx_lo + int3(1);
const float3 frac = localPos - float3(idx_lo);
const float3 neg_frac = float3(1.f) - frac;
// #define INV_CELL_WIDTH invCellWidth
#define INV_CELL_WIDTH 1.f
if (idx_lo.z >= 0 && idx_lo.z < blockSize.z) {
if (idx_lo.y >= 0 && idx_lo.y < blockSize.y) {
if (idx_lo.x >= 0 && idx_lo.x < blockSize.x) {
const float scalar = block.getScalar(idx_lo.x,idx_lo.y,idx_lo.z);
const float weight = (neg_frac.z)*(neg_frac.y)*(neg_frac.x);
sumWeights += weight;
sumWeightedValues += weight*scalar;
}
if (idx_hi.x < blockSize.x) {
const float scalar = block.getScalar(idx_hi.x,idx_lo.y,idx_lo.z);
const float weight = (neg_frac.z)*(neg_frac.y)*(frac.x);
sumWeights += weight;
sumWeightedValues += weight*scalar;
}
}
if (idx_hi.y < blockSize.y) {
if (idx_lo.x >= 0 && idx_lo.x < blockSize.x) {
const float scalar = block.getScalar(idx_lo.x,idx_hi.y,idx_lo.z);
const float weight = (neg_frac.z)*(frac.y)*(neg_frac.x);
sumWeights += weight;
sumWeightedValues += weight*scalar;
}
if (idx_hi.x < blockSize.x) {
const float scalar = block.getScalar(idx_hi.x,idx_hi.y,idx_lo.z);
const float weight = (neg_frac.z)*(frac.y)*(frac.x);
sumWeights += weight;
sumWeightedValues += weight*scalar;
}
}
}
if (idx_hi.z < blockSize.z) {
if (idx_lo.y >= 0 && idx_lo.y < blockSize.y) {
if (idx_lo.x >= 0 && idx_lo.x < blockSize.x) {
const float scalar = block.getScalar(idx_lo.x,idx_lo.y,idx_hi.z);
const float weight = (frac.z)*(neg_frac.y)*(neg_frac.x);
sumWeights += weight;
sumWeightedValues += weight*scalar;
}
if (idx_hi.x < blockSize.x) {
const float scalar = block.getScalar(idx_hi.x,idx_lo.y,idx_hi.z);
const float weight = (frac.z)*(neg_frac.y)*(frac.x);
sumWeights += weight;
sumWeightedValues += weight*scalar;
}
}
if (idx_hi.y < blockSize.y) {
if (idx_lo.x >= 0 && idx_lo.x < blockSize.x) {
const float scalar = block.getScalar(idx_lo.x,idx_hi.y,idx_hi.z);
const float weight = (frac.z)*(frac.y)*(neg_frac.x);
sumWeights += weight;
sumWeightedValues += weight*scalar;
}
if (idx_hi.x < blockSize.x) {
const float scalar = block.getScalar(idx_hi.x,idx_hi.y,idx_hi.z);
const float weight = (frac.z)*(frac.y)*(frac.x);
sumWeights += weight;
sumWeightedValues += weight*scalar;
}
}
}
return result;
}
// Grid accelerator to traverse spatial fields //
struct GridAccel
{
unsigned fieldID{UINT_MAX}; // the field this grid belongs to
int3 dims;
box3 worldBounds;
box1 *valueRanges; // min/max ranges
float *maxOpacities; // used as majorants
VSNRAY_FUNC
inline bool isValid() const
{
return dims != int3(0) && valueRanges && maxOpacities;
}
};
// Spatial Field //
struct SpatialField
{
enum Type { StructuredRegular, Unstructured, BlockStructured, Unknown, };
Type type{Unknown};
unsigned fieldID{UINT_MAX};
float baseDT{0.5f};
GridAccel gridAccel;
mat4x3 voxelSpaceTransform;
// Transform point in object space to voxel space
VSNRAY_FUNC
inline float3 pointToVoxelSpace(const float3 &object) const
{
mat3 rot = top_left(voxelSpaceTransform);
vec3 trans = voxelSpaceTransform(3);
return rot * (object + trans);
}
// Transform vector in object space to voxel space
VSNRAY_FUNC
inline float3 vectorToVoxelSpace(const float3 &object) const
{
mat3 rot = top_left(voxelSpaceTransform);
return rot * object;
}
union {
struct {
#ifdef WITH_CUDA
cuda_texture_ref<float, 3> sampler;
#elif defined(WITH_HIP)
hip_texture_ref<float, 3> sampler;
#else
texture_ref<float, 3> sampler;
#endif
} asStructuredRegular;
struct {
// Sampling BVH. This BVH is in _voxel_ space, so rays that take samples
// must first be transformed there from world space in case these spaces
// aren't the same!
#ifdef WITH_CUDA
cuda_index_bvh<UElem>::bvh_ref samplingBVH;
#elif defined(WITH_HIP)
hip_index_bvh<UElem>::bvh_ref samplingBVH;
#else
index_bvh<UElem>::bvh_ref samplingBVH;
#endif
} asUnstructured;
struct {
#ifdef WITH_CUDA
cuda_index_bvh<Block>::bvh_ref samplingBVH;
#elif defined(WITH_HIP)
hip_index_bvh<Block>::bvh_ref samplingBVH;
#else
index_bvh<Block>::bvh_ref samplingBVH;
#endif
} asBlockStructured;
};
};
VSNRAY_FUNC
inline bool sampleField(SpatialField sf, vec3 P, float &value) {
// This assumes that P is in voxel space!
if (sf.type == SpatialField::StructuredRegular) {
value = tex3D(sf.asStructuredRegular.sampler,P);
return true;
} else if (sf.type == SpatialField::Unstructured) {
Ray ray;
ray.ori = P;
ray.dir = float3(1.f);
ray.tmin = ray.tmax = 0.f;
auto hr = intersect(ray, sf.asUnstructured.samplingBVH);
if (!hr.hit)
return false;
value = hr.u; // value is stored in "u"!
return true;
} else if (sf.type == SpatialField::BlockStructured) {
Ray ray;
ray.ori = P;
ray.dir = float3(1.f);
ray.tmin = ray.tmax = 0.f;
// sumValues+sumWeightedValues
float basisPRD[2] = {0.f,0.f};
ray.prd = &basisPRD;
auto hr = intersect(ray, sf.asBlockStructured.samplingBVH);
if (!hr.hit || basisPRD[1] == 0.f)
return false;
value = basisPRD[0]/basisPRD[1];
return true;
}
return false;
}
VSNRAY_FUNC
inline bool sampleGradient(SpatialField sf, vec3 P, float3 &value) {
float x0=0, x1=0, y0=0, y1=0, z0=0, z1=0;
bool b0 = sampleField(sf, P+float3{sf.baseDT, 0.f, 0.f}, x1);
bool b1 = sampleField(sf, P-float3{sf.baseDT, 0.f, 0.f}, x0);
bool b2 = sampleField(sf, P+float3{0.f, sf.baseDT, 0.f}, y1);
bool b3 = sampleField(sf, P-float3{0.f, sf.baseDT, 0.f}, y0);
bool b4 = sampleField(sf, P+float3{0.f, 0.f, sf.baseDT}, z1);
bool b5 = sampleField(sf, P-float3{0.f, 0.f, sf.baseDT}, z0);
if (b0 && b1 && b2 && b3 && b4 && b5) {
value = float3{x1,y1,z1}-float3{x0,y0,z0};
return true; // TODO
} else {
value = float3{0.f};
return false;
}
}
// Transfer functions //
struct TransferFunction1D
{
unsigned numValues;
box1 valueRange;
#ifdef WITH_CUDA
cuda_texture_ref<float4, 1> sampler;
#elif defined(WITH_HIP)
hip_texture_ref<float4, 1> sampler;
#else
texture_ref<float4, 1> sampler;
#endif
};
VSNRAY_FUNC
inline float4 postClassify(TransferFunction1D tf, float v) {
box1 valueRange = tf.valueRange;
v = (v - valueRange.min) / (valueRange.max - valueRange.min);
return tex1D(tf.sampler, v);
}
// Volume //
struct Volume
{
enum Type { TransferFunction1D, Unknown, };
Type type{Unknown};
unsigned volID{UINT_MAX};
float unitDistance;
SpatialField field;
union {
struct TransferFunction1D asTransferFunction1D;
};
aabb bounds;
};
VSNRAY_FUNC
inline aabb get_bounds(const Volume &vol)
{
return vol.bounds;
}
inline void split_primitive(aabb &L, aabb &R, float plane, int axis, const Volume &vol)
{
assert(0);
}
struct HitRecordVolume
{
bool hit{false};
float t{FLT_MAX};
float3 albedo{0.f,0.f,0.f};
float extinction{0.f};
float Tr{1.f};
int volID{-1};
int instID{-1};
};
struct VolumePRD
{
HitRecordVolume *hr;
Random *rnd;
};
VSNRAY_FUNC
inline hit_record<Ray, primitive<unsigned>> intersect(Ray ray, const Volume &vol)
{
VolumePRD &prd = *(VolumePRD *)ray.prd;
HitRecordVolume &hrv = *prd.hr;
auto boxHit = intersect(ray,vol.bounds);
hit_record<Ray, primitive<unsigned>> hr;
hr.t = FLT_MAX;
hr.hit = false;
if (!boxHit.hit)
return hr;
if (ray.intersectionMask & Ray::VolumeBounds) {
// we just report that we did hit the box; the user
// is later expected to intersect the volume bounds
// themselves to compute [t0,t1]
hr.hit = boxHit.hit && (boxHit.tfar >= ray.tmin);
hr.t = max(ray.tmin,boxHit.tnear);
hr.geom_id = vol.volID;
if (hr.t < hrv.t) {
hrv.hit = true;
hrv.t = hr.t;
hrv.volID = hr.geom_id;
}
return hr;
}
Random &rnd = *prd.rnd;
hr.geom_id = vol.volID;
const auto &sf = vol.field;
dco::GridAccel grid = sf.gridAccel;
float3 albedo;
float Tr{1.f};
float extinction{0.f};
float unitDistance = vol.unitDistance;
auto woodcockFunc = [&](const int leafID, float t0, float t1) {
const float majorant = grid.isValid() ? grid.maxOpacities[leafID] : 1.f;
float t = t0;
while (1) {
if (majorant <= 0.f)
break;
t -= (logf(1.f - rnd()) / majorant) * unitDistance;
if (t >= t1)
break;
float3 P = ray.ori+ray.dir*t;
float v = 0.f;
if (sampleField(sf,P,v)) {
float4 sample
= postClassify(vol.asTransferFunction1D,v);
albedo = sample.xyz();
extinction = sample.w;
float u = rnd();
if (extinction >= u * majorant) {
hr.hit = true;
Tr = 0.f;
hr.t = t;
return false; // stop traversal
}
}
}
return true; // cont. traversal to the next spat. partition
};
ray.tmin = max(ray.tmin, boxHit.tnear);
ray.tmax = min(ray.tmax, boxHit.tfar);
// transform ray to voxel space
ray.ori = sf.pointToVoxelSpace(ray.ori);
ray.dir = sf.vectorToVoxelSpace(ray.dir);
const float dt_scale = length(ray.dir);
ray.dir = normalize(ray.dir);
ray.tmin = ray.tmin * dt_scale;
ray.tmax = ray.tmax * dt_scale;
unitDistance = unitDistance * dt_scale;
hr.t = ray.tmax;
if (sf.gridAccel.isValid())
dda3(ray, grid.dims, grid.worldBounds, woodcockFunc);
else
woodcockFunc(-1, ray.tmin, ray.tmax);
if (hr.hit) {
hr.t /= dt_scale;
if (hr.t < hrv.t) {
hrv.hit = true;
hrv.t = hr.t;
hrv.volID = hr.geom_id;
hrv.albedo = albedo;
hrv.Tr = Tr;
hrv.extinction = extinction;
}
}
return hr;
}
// ISO surface //
struct ISOSurface
{
unsigned isoID{UINT_MAX};
unsigned geomID{UINT_MAX};
SpatialField field;
unsigned numValues{0};
const float *values{nullptr};
aabb bounds;
};
VSNRAY_FUNC
inline aabb get_bounds(const ISOSurface &iso)
{
return iso.bounds;
}
inline void split_primitive(
aabb &L, aabb &R, float plane, int axis, const ISOSurface &vol)
{
assert(0);
}
VSNRAY_FUNC
inline hit_record<Ray, primitive<unsigned>> intersect(
Ray ray, const ISOSurface &iso)
{
hit_record<Ray, primitive<unsigned>> result;
auto boxHit = intersect(ray, iso.bounds);
if (!boxHit.hit)
return result;
float dt = iso.field.baseDT;
auto isectFunc = [&](const int leafID, float t0, float t1) {
bool empty = (leafID != -1);
if (leafID >= 0 && iso.field.gridAccel.valueRanges) {
box1 valueRange = iso.field.gridAccel.valueRanges[leafID];
for (unsigned i=0;i<iso.numValues;i++) {
float isoValue = iso.values[i];
if (valueRange.min <= isoValue && isoValue < valueRange.max) {
empty = false;
break;
}
}
}
if (empty)
return true;
float t0_old = t0;
float t1_old = t1;
t0 = t1 = boxHit.tnear-dt/2.f;
while (t0 < t0_old) t0 += dt;
while (t1 < t1_old) t1 += dt;
for (float t=t0;t<t1;t+=dt) {
float3 P1 = ray.ori+ray.dir*t;
float3 P2 = ray.ori+ray.dir*(t+dt);
float v1 = 0.f, v2 = 0.f;
if (sampleField(iso.field,P1,v1)
&& sampleField(iso.field,P2,v2)) {
unsigned numISOs = iso.numValues;
bool hit=false;
for (unsigned i=0;i<numISOs;i++) {
float isoValue = iso.values[i];
if ((v1 <= isoValue && v2 > isoValue) || (v2 <= isoValue && v1 > isoValue)) {
float tHit = t+dt/2.f;
if (tHit < result.t) {
result.hit = true;
result.prim_id = i;
result.geom_id = iso.geomID;
result.t = tHit;
}
hit = true;
}
}
if (hit) return false; // stop traversal
}
}
return true; // cont. traversal to the next spat. partition
};
ray.tmin = boxHit.tnear;
ray.tmax = boxHit.tfar;
if (iso.field.type == dco::SpatialField::Unstructured ||
iso.field.type == dco::SpatialField::StructuredRegular)
dda3(ray, iso.field.gridAccel.dims, iso.field.gridAccel.worldBounds, isectFunc);
else
isectFunc(-1, boxHit.tnear, boxHit.tfar);
return result;
}
// Cone primitive //
struct Cone : public primitive<unsigned>
{
float3 v1, v2;
float r1, r2;
};
VSNRAY_FUNC
inline hit_record<Ray, primitive<unsigned>> intersect(
const Ray &r, const Cone &cone)
{
// From https://iquilezles.org/articles/intersectors/
hit_record<Ray, primitive<unsigned>> result;
result.hit = false;
const vec3f &ro = r.ori;
const vec3f &rd = r.dir;
const vec3f &pa = cone.v1;
const vec3f &pb = cone.v2;
const float ra = cone.r1;
const float rb = cone.r2;
const vec3f ba = pb - pa;
const vec3f oa = ro - pa;
const vec3f ob = ro - pb;
const float m0 = dot(ba,ba);
const float m1 = dot(oa,ba);
const float m2 = dot(rd,ba);
const float m3 = dot(rd,oa);
const float m5 = dot(oa,oa);
const float m9 = dot(ob,ba);
auto dot2 = [](const vec3f v) { return dot(v,v); };
// Caps:
if (m1 < 0.f) {
if (dot2(oa*m2-rd*m1) < ra*ra*m2*m2) {
result.t = -m1 / m2;
result.u = 0.f;
result.hit = true;
result.isect_pos = r.ori + result.t * r.dir;
result.prim_id = cone.prim_id;
result.geom_id = cone.geom_id;
return result;
}
} else if (m9 > 0.f) {
const float t = -m9/m2;
if (dot2(ob+rd*t) < rb*rb) {
result.t = t;
result.u = 1.f;
result.hit = true;
result.isect_pos = r.ori + result.t * r.dir;
result.prim_id = cone.prim_id;
result.geom_id = cone.geom_id;
return result;
}
}
// Body
const float rr = ra - rb;
const float hy = m0 + rr*rr;
const float k2 = m0*m0 - m2*m2*hy;
const float k1 = m0*m0*m3 - m1*m2*hy + m0*ra*(rr*m2*1.f);
const float k0 = m0*m0*m5 - m1*m1*hy + m0*ra*(rr*m1*2.f - m0*ra);
const float h = k1*k1 - k2*k0;
if (h < 0.f) return result;
const float t = (-k1-sqrtf(h))/k2;
const float y = m1 + t*m2;
if (y > 0.f && y<m0) {
result.t = t;
result.u = y/m0;
result.v = y;
result.hit = true;
result.isect_pos = r.ori + result.t * r.dir;
result.prim_id = cone.prim_id;
result.geom_id = cone.geom_id;
}
return result;
}
VSNRAY_FUNC inline aabb get_bounds(const Cone &cone)
{
aabb result;
result.invalidate();
result.insert(cone.v1 - cone.r1);
result.insert(cone.v1 + cone.r1);
result.insert(cone.v2 - cone.r2);
result.insert(cone.v2 + cone.r2);
return result;
}
VSNRAY_FUNC inline void split_primitive(
aabb& L, aabb& R, float plane, int axis, const Cone &cone)
{
VSNRAY_UNUSED(L);
VSNRAY_UNUSED(R);
VSNRAY_UNUSED(plane);
VSNRAY_UNUSED(axis);
VSNRAY_UNUSED(cone);
// TODO: implement this to support SBVHs
}
// Bezier curve primitive //
struct BezierCurve : public primitive<unsigned>
{
float3 w0, w1, w2, w3;
float r;
VSNRAY_FUNC vec3 f(float t) const
{
float tinv = 1.0f - t;
return tinv * tinv * tinv * w0
+ 3.0f * tinv * tinv * t * w1
+ 3.0f * tinv * t * t * w2
+ t * t * t * w3;
}
VSNRAY_FUNC vec3 dfdt(float t) const
{
float tinv = 1.0f - t;
return -3.0f * tinv * tinv * w0
+ 3.0f * (3.0f * t * t - 4.0f * t + 1.0f) * w1
+ 3.0f * (2.0f - 3.0f * t) * t * w2
+ 3.0f * t * t * w3;
}
};
VSNRAY_FUNC
inline BezierCurve make_bezierCurve(
const vec3 &w0, const vec3 &w1, const vec3 &w2, const vec3 &w3, float r)
{
BezierCurve curve;
curve.w0 = w0;
curve.w1 = w1;
curve.w2 = w2;
curve.w3 = w3;
curve.r = r;
return curve;
}
//=========================================================
// Phantom Ray-Hair Intersector (Reshetov and Luebke, 2018)
//=========================================================
namespace phantom {
// Ray/cone intersection from appendix A
struct RayConeIntersection
{
VSNRAY_FUNC inline bool intersect(float r, float dr)
{
float r2 = r * r;
float drr = r * dr;
float ddd = cd.x * cd.x + cd.y * cd.y;
dp = c0.x * c0.x + c0.y * c0.y;
float cdd = c0.x * cd.x + c0.y * cd.y;
float cxd = c0.x * cd.y - c0.y * cd.x;
float c = ddd;
float b = cd.z * (drr - cdd);
float cdz2 = cd.z * cd.z;
ddd += cdz2;
float a = 2.0f * drr * cdd + cxd * cxd - ddd * r2 + dp * cdz2;
float discr = b * b - a * c;
s = (b - (discr > 0.0f ? sqrtf(discr) : 0.0f)) / c;
dt = (s * cd.z - cdd) / ddd;
dc = s * s + dp;
sp = cdd / cd.z;
dp += sp * sp;
return discr > 0.0f;
}
vec3 c0;
vec3 cd;
float s;
float dt;
float dp;
float dc;
float sp;
};
// TODO: use visionaray's ray/cyl test?!
VSNRAY_FUNC inline
bool intersectCylinder(const Ray &ray, vec3 p0, vec3 p1, float ra)
{
vec3 ba = p1 - p0;
vec3 oc = ray.ori - p0;
float baba = dot(ba, ba);
float bard = dot(ba, ray.dir);
float baoc = dot(ba, oc);
float k2 = baba - bard * bard;
float k1 = baba * dot(oc, ray.dir) - baoc * bard;
float k0 = baba * dot(oc, oc) - baoc * baoc - ra * ra * baba;
float h = k1 * k1 - k2 * k0;
if (h < 0.0f)
return false;
h = sqrtf(h);
float t = (-k1 - h) / k2;
// body
float y = baoc + t * bard;
if (y > 0.0f && y < baba)
return true;
// caps
t = ((y < 0.0f ? 0.0f : baba) - baoc) / bard;
if (fabsf(k1 + k2 * t) < h)
return true;
return false;
}
struct TransformToRCC
{
VSNRAY_FUNC inline TransformToRCC(const Ray &r)
{
vec3 e1;
vec3 e2;
vec3 e3 = normalize(r.dir);
make_orthonormal_basis(e1, e2, e3);
xformInv = mat4(
vec4(e1, 0.0f),
vec4(e2, 0.0f),
vec4(e3, 0.0f),
vec4(r.ori, 1.0f)
);
xform = inverse(xformInv);
}
VSNRAY_FUNC inline vec3 xfmPoint(vec3 point)