forked from imageworks/Field3D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldSampler.h
470 lines (414 loc) · 15.2 KB
/
FieldSampler.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
//----------------------------------------------------------------------------//
#ifndef __F3DUTIL_FIELDSAMPLER_H__
#define __F3DUTIL_FIELDSAMPLER_H__
//------------------------------------------------------------------------------
// Project includes
#include "Types.h"
//----------------------------------------------------------------------------//
#include "ns.h"
FIELD3D_NAMESPACE_OPEN
//------------------------------------------------------------------------------
// detail namespace
//------------------------------------------------------------------------------
namespace detail {
//! Min operation on mixed types
template <typename T, typename T2>
T min(const T a, const T2 b)
{
return std::min(a, static_cast<T>(b));
}
//! Max operation on mixed types
template <typename T, typename T2>
T max(const T a, const T2 b)
{
return std::max(a, static_cast<T>(b));
}
//! Min operation on mixed vector types
template <typename T, typename T2>
FIELD3D_VEC3_T<T> min(const FIELD3D_VEC3_T<T> &a,
const FIELD3D_VEC3_T<T2> &b)
{
return FIELD3D_VEC3_T<T>(std::min(a.x, static_cast<T>(b.x)),
std::min(a.y, static_cast<T>(b.y)),
std::min(a.z, static_cast<T>(b.z)));
}
//! Max operation on mixed vector types
template <typename T, typename T2>
FIELD3D_VEC3_T<T> max(const FIELD3D_VEC3_T<T> &a,
const FIELD3D_VEC3_T<T2> &b)
{
return FIELD3D_VEC3_T<T>(std::max(a.x, static_cast<T>(b.x)),
std::max(a.y, static_cast<T>(b.y)),
std::max(a.z, static_cast<T>(b.z)));
}
//! Typedefs float or V3f, depending on Dims_T
template <int Dims_T>
struct ScalarOrVector;
template <>
struct ScalarOrVector<1>
{
typedef float type;
};
template <>
struct ScalarOrVector<3>
{
typedef V3f type;
};
}
//------------------------------------------------------------------------------
// FieldSampler
//------------------------------------------------------------------------------
//! Interface for sampling a vector of fields of the same type
template <typename WrapperVec_T, int Dims_T>
struct FieldSampler
{
enum Mode {
Min,
Max
};
typedef typename WrapperVec_T::value_type::field_type Field_T;
typedef typename Field_T::value_type Data_T;
typedef typename detail::ScalarOrVector<Dims_T>::type Input_T;
// Ordinary fields
static void sample(const WrapperVec_T &f, const V3d &wsP, float *value,
size_t &numHits)
{
// Reinterpret the pointer according to Dims_T
Input_T *data = reinterpret_cast<Input_T*>(value);
// Loop over fields in vector
for (size_t i = 0, end = f.size(); i < end; ++i) {
V3d vsP;
// Apply world to object transform
if (f[i].doOsToWs) {
V3d osP;
f[i].wsToOs.multVecMatrix(wsP, osP);
f[i].mapping->worldToVoxel(osP, vsP);
} else {
f[i].mapping->worldToVoxel(wsP, vsP);
}
// Sample
if (f[i].vsBounds.intersects(vsP)) {
// Count as within field
numHits++;
// Sample and remap
if (f[i].valueRemapOp) {
const Data_T unremapped = f[i].interp.sample(*f[i].field, vsP);
*data += f[i].valueRemapOp->remap(unremapped);
} else {
*data += f[i].interp.sample(*f[i].field, vsP);
}
}
}
}
// Ordinary fields
static void sampleMultiple(const WrapperVec_T &f, const size_t neval,
const float *wsPs, float *value, size_t *numHits)
{
// Loop over fields in vector
for (size_t i = 0; i < f.size(); ++i) {
const typename WrapperVec_T::value_type &field = f[i];
// Reinterpret the pointer according to Dims_T
Input_T *data = reinterpret_cast<Input_T*>(value);
if (field.doOsToWs || field.valueRemapOp) {
// Loop over samples
for (size_t ieval = 0; ieval < neval; ++ieval) {
const V3d wsP(*reinterpret_cast<const V3f*>(wsPs + 3 * ieval));
V3d vsP;
// Apply world to object transform
if (field.doOsToWs) {
V3d osP;
field.wsToOs.multVecMatrix(wsP, osP);
field.mapping->worldToVoxel(osP, vsP);
} else {
field.mapping->worldToVoxel(wsP, vsP);
}
// Sample
if (field.vsBounds.intersects(vsP)) {
// Count as within field
numHits[ieval]++;
// Sample and remap
if (field.valueRemapOp) {
const Data_T unremapped = field.interp.sample(*field.field, vsP);
data[ieval] += field.valueRemapOp->remap(unremapped);
} else {
data[ieval] += field.interp.sample(*field.field, vsP);
}
}
}
} else {
const Imath::Box3d &vsBounds_d = field.vsBounds;
// Loop over samples
for (size_t ieval = 0; ieval < neval; ++ieval) {
const V3d wsP(*reinterpret_cast<const V3f*>(wsPs + 3 * ieval));
V3d vsP;
// Apply world to object transform
field.mapping->worldToVoxel(wsP, vsP);
// Sample
if (vsBounds_d.intersects(vsP)) {
// Count as within field
numHits[ieval]++;
// Sample
data[ieval] += field.interp.sample(*field.field, vsP);
}
}
}
}
}
// MIP fields
static void sampleMIP(const WrapperVec_T &f, const V3d &wsP,
const float wsSpotSize, float *value, size_t &numHits)
{
// Reinterpret the pointer according to Dims_T
Input_T *data = reinterpret_cast<Input_T*>(value);
// Loop over fields in vector
for (size_t i = 0, end = f.size(); i < end; ++i) {
V3d vsP;
float spotSize = wsSpotSize / f[i].worldScale;
// Apply world to object transform
if (f[i].doOsToWs) {
V3d osP;
f[i].wsToOs.multVecMatrix(wsP, osP);
f[i].mapping->worldToVoxel(osP, vsP);
spotSize = wsSpotSize / f[i].worldScale;
} else {
f[i].mapping->worldToVoxel(wsP, vsP);
}
// Sample
if (f[i].vsBounds.intersects(vsP)) {
// Count as within field
numHits++;
// Sample and remap
if (f[i].valueRemapOp) {
const Data_T unremapped = f[i].interp->sample(vsP, spotSize);
*data += f[i].valueRemapOp->remap(unremapped);
} else {
*data += f[i].interp->sample(vsP, spotSize);
}
}
}
}
// MIP fields
static void sampleMIPMultiple(const WrapperVec_T &f, const size_t neval,
const float *wsPs, const float *wsSpotSizes,
float *value, size_t *numHits)
{
// Loop over fields in vector
for (size_t i = 0; i < f.size(); ++i) {
const typename WrapperVec_T::value_type &field = f[i];
// Reinterpret the pointer according to Dims_T
Input_T *data = reinterpret_cast<Input_T*>(value);
if (field.doOsToWs || field.valueRemapOp) {
if (field.valueRemapOp && field.doWsBoundsOptimization) {
// Loop over samples
for (size_t ieval = 0; ieval < neval; ++ieval) {
const V3f &wsP = *reinterpret_cast<const V3f*>(wsPs + 3 * ieval);
if (field.wsBounds.intersects(wsP)) {
// Apply world to object transform
V3d vsP;
field.wsToVs.multVecMatrix(V3d(wsP), vsP);
// Sample
if (field.vsBounds.intersects(vsP)) {
// Count as within field
numHits[ieval]++;
const float spotSize = wsSpotSizes[ieval] / field.worldScale;
const Data_T unremapped = field.interp->sample(vsP, spotSize);
data[ieval] += field.valueRemapOp->remap(unremapped);
}
}
}
} else {
// Loop over samples
for (size_t ieval = 0; ieval < neval; ++ieval) {
const V3d wsP(*reinterpret_cast<const V3f*>(wsPs + 3 * ieval));
const float wsSpotSize = wsSpotSizes[ieval];
Input_T *idata = data + ieval;
V3d vsP;
float spotSize = wsSpotSize / field.worldScale;
// Apply world to object transform
if (field.doOsToWs) {
V3d osP;
field.wsToOs.multVecMatrix(wsP, osP);
field.mapping->worldToVoxel(osP, vsP);
spotSize = wsSpotSize / field.worldScale;
} else {
field.mapping->worldToVoxel(wsP, vsP);
}
// Sample
if (field.vsBounds.intersects(vsP)) {
// Count as within field
numHits[ieval]++;
if (field.valueRemapOp) {
const Data_T unremapped = field.interp->sample(vsP, spotSize);
*idata += field.valueRemapOp->remap(unremapped);
} else {
*idata += field.interp->sample(vsP, spotSize);
}
}
}
}
} else {
const Imath::Box3d &vsBounds_d = field.vsBounds;
const double worldScale = field.worldScale;
// Loop over samples
for (size_t ieval = 0; ieval < neval; ++ieval) {
const V3d wsP(*reinterpret_cast<const V3f*>(wsPs + 3 * ieval));
V3d vsP;
// Apply world to object transform
field.mapping->worldToVoxel(wsP, vsP);
// Sample
if (vsBounds_d.intersects(vsP)) {
// Count as within field
numHits[ieval]++;
const double spotSize = wsSpotSizes[ieval] / worldScale;
data[ieval] += field.interp->sample(vsP, spotSize);
}
}
}
}
}
// Get min/max
static void getMinMax(const WrapperVec_T &f,
const Box3d &wsBounds, float *min, float *max)
{
// Reinterpret the pointer according to Dims_T
Input_T *minData = reinterpret_cast<Input_T*>(min);
Input_T *maxData = reinterpret_cast<Input_T*>(max);
// Loop over fields in vector
for (size_t field = 0, end = f.size(); field < end; ++field) {
// Data window
const Box3i dw = f[field].field->dataWindow();
// Transform corners to voxel space and compute bounds
Box3i dvsBounds;
if (wsBounds.isInfinite()) {
dvsBounds = dw;
} else {
Box3d vsBounds;
if (f[field].doOsToWs) {
Box3d osBounds;
transformBounds(f[field].wsToOs, wsBounds, osBounds);
worldToVoxel(f[field].mapping, osBounds, vsBounds);
} else {
worldToVoxel(f[field].mapping, wsBounds, vsBounds);
}
dvsBounds = clipBounds(discreteBounds(vsBounds), dw);
// Early termination if no intersection
if (!dw.intersects(dvsBounds)) {
return;
}
}
for (int k = dvsBounds.min.z; k <= dvsBounds.max.z; ++k) {
for (int j = dvsBounds.min.y; j <= dvsBounds.max.y; ++j) {
for (int i = dvsBounds.min.x; i <= dvsBounds.max.x; ++i) {
const Data_T val = f[field].field->fastValue(i, j, k);
*minData = detail::min(val, *minData);
*maxData = detail::max(val, *maxData);
}
}
}
}
}
// Get min/max from MIP (uses finest level)
static void getMinMaxMIP(const WrapperVec_T &f,
const Box3d &wsBounds, float *min, float *max)
{
// Reinterpret the pointer according to Dims_T
Input_T *minData = reinterpret_cast<Input_T*>(min);
Input_T *maxData = reinterpret_cast<Input_T*>(max);
// Loop over fields in vector
for (size_t field = 0, end = f.size(); field < end; ++field) {
// Data window
const Box3i dw = f[field].field->dataWindow();
// Transform corners to voxel space and compute bounds
Box3i dvsBounds;
if (wsBounds.isInfinite()) {
dvsBounds = dw;
} else {
Box3d vsBounds;
if (f[field].doOsToWs) {
Box3d osBounds;
transformBounds(f[field].wsToOs, wsBounds, osBounds);
worldToVoxel(f[field].mapping, osBounds, vsBounds);
} else {
worldToVoxel(f[field].mapping, wsBounds, vsBounds);
}
dvsBounds = clipBounds(discreteBounds(vsBounds), dw);
// Early termination if no intersection
if (!dw.intersects(dvsBounds)) {
return;
}
}
for (int k = dvsBounds.min.z; k <= dvsBounds.max.z; ++k) {
for (int j = dvsBounds.min.y; j <= dvsBounds.max.y; ++j) {
for (int i = dvsBounds.min.x; i <= dvsBounds.max.x; ++i) {
const Data_T val = f[field].field->fastMipValue(0, i, j, k);
*minData = detail::min(val, *minData);
*maxData = detail::max(val, *maxData);
}
}
}
}
}
// Get min/max for pre-filtered data
static void getMinMaxPrefilt(const WrapperVec_T &f,
const Box3d &wsBounds,
float *result,
const Mode mode)
{
// Reinterpret the pointer according to Dims_T
Input_T *data = reinterpret_cast<Input_T*>(result);
// Loop over fields in vector
for (size_t field = 0, end = f.size(); field < end; ++field) {
// Choose the MIP level to check
const size_t numLevels = f[field].field->numLevels();
size_t level = 0;
Box3i dvsBounds;
// Infinite bounds?
if (wsBounds.isInfinite()) {
// Use the coarsest level
level = numLevels - 1;
dvsBounds = f[field].field->mipLevel(level)->dataWindow();
} else {
for (size_t i = 0; i < numLevels; ++i) {
// Update current level
level = i;
// Data window of current level
const Box3i dw = f[field].field->mipLevel(level)->dataWindow();
Box3d vsBounds;
if (f[field].doOsToWs) {
Box3d osBounds;
transformBounds(f[field].wsToOs, wsBounds, osBounds);
worldToVoxel(f[field].field->mipLevel(level)->mapping().get(),
osBounds, vsBounds);
} else {
worldToVoxel(f[field].field->mipLevel(level)->mapping().get(),
wsBounds, vsBounds);
}
dvsBounds = clipBounds(discreteBounds(vsBounds), dw);
// If size of dvsBounds is <= 2, stop
Imath::V3i size = dvsBounds.size();
if (std::max(size.x, std::max(size.y, size.z)) <= 2) {
break;
}
}
}
// Level chosen. Run loop
for (int k = dvsBounds.min.z; k <= dvsBounds.max.z; ++k) {
for (int j = dvsBounds.min.y; j <= dvsBounds.max.y; ++j) {
for (int i = dvsBounds.min.x; i <= dvsBounds.max.x; ++i) {
const Data_T val = f[field].field->fastMipValue(level, i, j, k);
if (mode == Min) {
*data = detail::min(val, *data);
} else {
*data = detail::max(val, *data);
}
}
}
}
}
}
};
//----------------------------------------------------------------------------//
FIELD3D_NAMESPACE_HEADER_CLOSE
//------------------------------------------------------------------------------
#endif // include guard
//------------------------------------------------------------------------------