OpenVDB 13.1.0
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#ifndef _USE_MATH_DEFINES
7#define _USE_MATH_DEFINES
8#endif
9#include <cmath>
10#include <chrono>
11#include <fstream>
12#include <nanovdb/NanoVDB.h>
13#include "ComputePrimitives.h"
14
15inline __hostdev__ uint32_t CompactBy1(uint32_t x)
16{
17 x &= 0x55555555;
18 x = (x ^ (x >> 1)) & 0x33333333;
19 x = (x ^ (x >> 2)) & 0x0f0f0f0f;
20 x = (x ^ (x >> 4)) & 0x00ff00ff;
21 x = (x ^ (x >> 8)) & 0x0000ffff;
22 return x;
23}
24
25inline __hostdev__ uint32_t SeparateBy1(uint32_t x)
26{
27 x &= 0x0000ffff;
28 x = (x ^ (x << 8)) & 0x00ff00ff;
29 x = (x ^ (x << 4)) & 0x0f0f0f0f;
30 x = (x ^ (x << 2)) & 0x33333333;
31 x = (x ^ (x << 1)) & 0x55555555;
32 return x;
33}
34
35inline __hostdev__ void mortonDecode(uint32_t code, uint32_t& x, uint32_t& y)
36{
37 x = CompactBy1(code);
38 y = CompactBy1(code >> 1);
39}
40
41inline __hostdev__ void mortonEncode(uint32_t& code, uint32_t x, uint32_t y)
42{
43 code = SeparateBy1(x) | (SeparateBy1(y) << 1);
44}
45
46template<typename RenderFn, typename GridT>
47inline float renderImage(bool useCuda, const RenderFn renderOp, int width, int height, float* image, const GridT* grid)
48{
49 using ClockT = std::chrono::high_resolution_clock;
50 auto t0 = ClockT::now();
51
53 useCuda, width * height, 512, __FILE__, __LINE__, [renderOp, image, grid] __hostdev__(int start, int end) {
54 renderOp(start, end, image, grid);
55 });
56 computeSync(useCuda, __FILE__, __LINE__);
57
58 auto t1 = ClockT::now();
59 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count() / 1000.f;
60 return duration;
61}
62
63inline void saveImage(const std::string& filename, int width, int height, const float* image)
64{
65 const auto isLittleEndian = []() -> bool {
66 static int x = 1;
67 static bool result = reinterpret_cast<uint8_t*>(&x)[0] == 1;
68 return result;
69 };
70
71 float scale = 1.0f;
72 if (isLittleEndian())
73 scale = -scale;
74
75 std::fstream fs(filename, std::ios::out | std::ios::binary);
76 if (!fs.is_open()) {
77 throw std::runtime_error("Unable to open file: " + filename);
78 }
79
80 fs << "Pf\n"
81 << width << "\n"
82 << height << "\n"
83 << scale << "\n";
84
85 for (int i = 0; i < width * height; ++i) {
86 float r = image[i];
87 fs.write((char*)&r, sizeof(float));
88 }
89}
90
91template<typename Vec3T>
93{
96
97 inline RayGenOp(float wBBoxDimZ, Vec3T wBBoxCenter)
98 : mWBBoxDimZ(wBBoxDimZ)
99 , mWBBoxCenter(wBBoxCenter)
100 {
101 }
102
103 inline __hostdev__ void operator()(int i, int w, int h, Vec3T& outOrigin, Vec3T& outDir) const
104 {
105 // perspective camera along Z-axis...
106 uint32_t x, y;
107#if 0
108 mortonDecode(i, x, y);
109#else
110 x = i % w;
111 y = i / w;
112#endif
113 const float fov = 45.f;
114 const float u = (float(x) + 0.5f) / w;
115 const float v = (float(y) + 0.5f) / h;
116 const float aspect = w / float(h);
117 const float Px = (2.f * u - 1.f) * tanf(fov / 2 * 3.14159265358979323846f / 180.f) * aspect;
118 const float Py = (2.f * v - 1.f) * tanf(fov / 2 * 3.14159265358979323846f / 180.f);
119 const Vec3T origin = mWBBoxCenter + Vec3T(0, 0, mWBBoxDimZ);
120 Vec3T dir(Px, Py, -1.f);
121 dir.normalize();
122 outOrigin = origin;
123 outDir = dir;
124 }
125};
126
128{
129 inline __hostdev__ void operator()(float* outImage, int i, int w, int h, float value, float alpha) const
130 {
131 uint32_t x, y;
132 int offset;
133#if 0
134 mortonDecode(i, x, y);
135 offset = x + y * w;
136#else
137 x = i % w;
138 y = i / w;
139 offset = i;
140#endif
141
142 // checkerboard background...
143 const int mask = 1 << 7;
144 const float bg = ((x & mask) ^ (y & mask)) ? 1.0f : 0.5f;
145 outImage[offset] = alpha * value + (1.0f - alpha) * bg;
146 }
147};
A collection of parallel compute primitives.
void computeForEach(bool useCuda, int numItems, int blockSize, const char *file, int line, const FunctorT &op, Args... args)
Definition ComputePrimitives.h:128
void computeSync(bool useCuda, const char *file, int line)
Definition ComputePrimitives.h:107
Implements a light-weight self-contained VDB data-structure in a single file! In other words,...
__hostdev__ void mortonDecode(uint32_t code, uint32_t &x, uint32_t &y)
Definition common.h:35
__hostdev__ void mortonEncode(uint32_t &code, uint32_t x, uint32_t y)
Definition common.h:41
__hostdev__ uint32_t SeparateBy1(uint32_t x)
Definition common.h:25
float renderImage(bool useCuda, const RenderFn renderOp, int width, int height, float *image, const GridT *grid)
Definition common.h:47
__hostdev__ uint32_t CompactBy1(uint32_t x)
Definition common.h:15
void saveImage(const std::string &filename, int width, int height, const float *image)
Definition common.h:63
__hostdev__ void mortonDecode(uint32_t code, uint32_t &x, uint32_t &y)
Definition common.h:35
#define __hostdev__
Definition SampleFromVoxels.h:29
Definition common.h:128
__hostdev__ void operator()(float *outImage, int i, int w, int h, float value, float alpha) const
Definition common.h:129
RayGenOp(float wBBoxDimZ, Vec3T wBBoxCenter)
Definition common.h:97
__hostdev__ void operator()(int i, int w, int h, Vec3T &outOrigin, Vec3T &outDir) const
Definition common.h:103
Vec3T mWBBoxCenter
Definition common.h:95
float mWBBoxDimZ
Definition common.h:94