18 #ifndef OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED 19 #define OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED 35 #include <type_traits> 48 template<
typename Gr
idT>
52 size_t pixelSamples = 1,
53 unsigned int seed = 0,
57 template<
typename Gr
idT,
typename IntersectorT>
62 size_t pixelSamples = 1,
63 unsigned int seed = 0,
71 template<
typename Gr
idT,
typename IntersectorT = tools::LevelSetRayIntersector<Gr
idT> >
76 using Vec3Type =
typename IntersectorT::Vec3Type;
77 using RayType =
typename IntersectorT::RayType;
83 size_t pixelSamples = 1,
84 unsigned int seed = 0);
91 size_t pixelSamples = 1,
92 unsigned int seed = 0);
101 void setGrid(
const GridT& grid);
105 void setIntersector(
const IntersectorT& inter);
123 void setPixelSamples(
size_t pixelSamples,
unsigned int seed = 0);
126 void render(
bool threaded =
true)
const;
130 void operator()(
const tbb::blocked_range<size_t>& range)
const;
133 const bool mIsMaster;
136 std::unique_ptr<const BaseShader> mShader;
148 template <
typename IntersectorT,
typename SamplerT = tools::BoxSampler>
154 using RayType =
typename IntersectorT::RayType;
158 static_assert(std::is_floating_point<ValueType>::value,
159 "VolumeRender requires a floating-point-valued grid");
168 void render(
bool threaded=
true)
const;
175 void setIntersector(
const IntersectorT& inter);
207 void print(std::ostream& os = std::cout,
int verboseLevel = 1);
211 void operator()(
const tbb::blocked_range<size_t>& range)
const;
217 std::unique_ptr<IntersectorT> mPrimary, mShadow;
218 Real mPrimaryStep, mShadowStep, mCutOff, mLightGain;
219 Vec3R mLightDir, mLightColor, mAbsorption, mScattering;
235 RGBA() : r(0), g(0), b(0), a(1) {}
236 explicit RGBA(
ValueT intensity) : r(intensity), g(intensity), b(intensity), a(1) {}
238 r(_r), g(_g), b(_b), a(_a)
240 RGBA(
double _r,
double _g,
double _b,
double _a = 1.0)
241 : r(static_cast<
ValueT>(_r))
242 , g(static_cast<
ValueT>(_g))
243 , b(static_cast<
ValueT>(_b))
244 , a(static_cast<
ValueT>(_a))
254 const float s = rhs.
a*(1.0f-a);
265 Film(
size_t width,
size_t height)
266 : mWidth(width), mHeight(height), mSize(width*height), mPixels(new
RGBA[mSize])
270 : mWidth(width), mHeight(height), mSize(width*height), mPixels(new
RGBA[mSize])
279 return mPixels[w + h*mWidth];
286 return mPixels[w + h*mWidth];
289 void fill(
const RGBA& rgb=
RGBA(0)) {
for (
size_t i=0; i<mSize; ++i) mPixels[i] = rgb; }
292 RGBA *p = mPixels.get();
293 for (
size_t j = 0; j < mHeight; ++j) {
294 for (
size_t i = 0; i < mWidth; ++i, ++p) {
295 *p = ((i & size) ^ (j & size)) ? c1 : c2;
300 template <
typename Type =
unsigned char>
303 const size_t totalSize = mSize * (alpha ? 4 : 3);
304 std::unique_ptr<Type[]> buffer(
new Type[totalSize]);
305 Type *q = buffer.get();
306 const RGBA* p = this->pixels();
309 *q++ =
static_cast<Type
>(255.0f*(*p).r);
310 *q++ =
static_cast<Type
>(255.0f*(*p).g);
311 *q++ =
static_cast<Type
>(255.0f*(*p).b);
313 *q++ =
static_cast<Type
>(255.0f*(*p).a);
321 std::string name(fileName);
322 if (name.find_last_of(
".") == std::string::npos) name.append(
".ppm");
324 std::ofstream os(name.c_str(), std::ios_base::binary);
326 std::cerr <<
"Error opening PPM file \"" << name <<
"\"" << std::endl;
330 auto buf = this->convertToBitBuffer<unsigned char>(
false);
331 unsigned char* tmp = buf.get();
333 os <<
"P6\n" << mWidth <<
" " << mHeight <<
"\n255\n";
334 os.write(reinterpret_cast<const char*>(&(*tmp)), 3 * mSize *
sizeof(
unsigned char));
337 size_t width()
const {
return mWidth; }
338 size_t height()
const {
return mHeight; }
343 size_t mWidth, mHeight, mSize;
344 std::unique_ptr<RGBA[]> mPixels;
355 double frameWidth,
double nearPlane,
double farPlane)
357 , mScaleWidth(frameWidth)
358 , mScaleHeight(frameWidth * double(film.height()) / double(film.width()))
361 mScreenToWorld.accumPostRotation(
math::X_AXIS, rotation[0] * math::pi<double>() / 180.0);
362 mScreenToWorld.accumPostRotation(
math::Y_AXIS, rotation[1] * math::pi<double>() / 180.0);
363 mScreenToWorld.accumPostRotation(
math::Z_AXIS, rotation[2] * math::pi<double>() / 180.0);
364 mScreenToWorld.accumPostTranslation(translation);
365 this->initRay(nearPlane, farPlane);
372 size_t width()
const {
return mFilm->width(); }
373 size_t height()
const {
return mFilm->height(); }
381 const Vec3R orig = mScreenToWorld.applyMap(
Vec3R(0.0));
382 const Vec3R dir = orig - xyz;
384 Mat4d xform = math::aim<Mat4d>(dir, up);
387 this->initRay(mRay.t0(), mRay.t1());
393 return Vec3R( (2 * i /
double(mFilm->width()) - 1) * mScaleWidth,
394 (1 - 2 * j / double(mFilm->height())) * mScaleHeight, z );
401 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const = 0;
406 mRay.setTimes(t0, t1);
407 mRay.setEye(mScreenToWorld.applyMap(
Vec3R(0.0)));
408 mRay.setDir(mScreenToWorld.applyJacobian(
Vec3R(0.0, 0.0, -1.0)));
439 double focalLength = 50.0,
440 double aperture = 41.2136,
441 double nearPlane = 1e-3,
443 :
BaseCamera(film,
rotation, translation, 0.5*aperture/focalLength, nearPlane, farPlane)
453 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const override 456 Vec3R dir = BaseCamera::rasterToScreen(
Real(i) + iOffset,
Real(j) + jOffset, -1.0);
457 dir = BaseCamera::mScreenToWorld.applyJacobian(dir);
468 return 360.0 / math::pi<double>() * atan(aperture/(2.0*length));
474 return aperture/(2.0*(tan(fov * math::pi<double>() / 360.0)));
497 double frameWidth = 1.0,
498 double nearPlane = 1e-3,
506 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const override 509 Vec3R eye = BaseCamera::rasterToScreen(
Real(i) + iOffset,
Real(j) + jOffset, 0.0);
510 ray.
setEye(BaseCamera::mScreenToWorld.applyMap(eye));
547 MatteShader(
const GridT& grid) : mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
552 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
553 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
559 typename GridT::ConstAccessor mAcc;
564 template<
typename SamplerType>
594 NormalShader(
const GridT& grid) : mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
599 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
600 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
601 return Film::RGBA(v[0]*(normal[0]+1.0), v[1]*(normal[1]+1.0), v[2]*(normal[2]+1.0));
606 typename GridT::ConstAccessor mAcc;
611 template<
typename SamplerType>
620 return mRGBA *
Film::RGBA(normal[0] + 1.0, normal[1] + 1.0, normal[2] + 1.0);
643 , mInvDim(1.0/bbox.extents())
644 , mAcc(grid.getAccessor())
645 , mXform(&grid.transform())
652 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
653 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
654 const Vec3R rgb = (xyz - mMin) * mInvDim;
660 const Vec3R mMin, mInvDim;
661 typename GridT::ConstAccessor mAcc;
666 template<
typename SamplerType>
671 : mMin(bbox.
min()), mInvDim(1.0/bbox.extents()), mRGBA(c) {}
676 const Vec3R rgb = (xyz - mMin)*mInvDim;
677 return mRGBA*
Film::RGBA(rgb[0], rgb[1], rgb[2]);
682 const Vec3R mMin, mInvDim;
701 DiffuseShader(
const GridT& grid): mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
706 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
707 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
716 typename GridT::ConstAccessor mAcc;
721 template <
typename SamplerType>
750 template<
typename Gr
idT>
759 tracer(grid, shader, camera, pixelSamples, seed);
764 template<
typename Gr
idT,
typename IntersectorT>
766 const IntersectorT& inter,
781 template<
typename Gr
idT,
typename IntersectorT>
791 mShader(shader.copy()),
797 template<
typename Gr
idT,
typename IntersectorT>
807 mShader(shader.copy()),
813 template<
typename Gr
idT,
typename IntersectorT>
818 mInter(other.mInter),
819 mShader(other.mShader->copy()),
820 mCamera(other.mCamera),
821 mSubPixels(other.mSubPixels)
825 template<
typename Gr
idT,
typename IntersectorT>
829 if (mIsMaster)
delete [] mRand;
832 template<
typename Gr
idT,
typename IntersectorT>
837 mInter = IntersectorT(grid);
840 template<
typename Gr
idT,
typename IntersectorT>
848 template<
typename Gr
idT,
typename IntersectorT>
853 mShader.reset(shader.
copy());
856 template<
typename Gr
idT,
typename IntersectorT>
864 template<
typename Gr
idT,
typename IntersectorT>
869 if (pixelSamples == 0) {
872 mSubPixels = pixelSamples - 1;
874 if (mSubPixels > 0) {
875 mRand =
new double[16];
877 for (
size_t i=0; i<16; ++i) mRand[i] = rand();
883 template<
typename Gr
idT,
typename IntersectorT>
887 tbb::blocked_range<size_t> range(0, mCamera->
height());
888 threaded ? tbb::parallel_for(range, *
this) : (*this)(range);
891 template<
typename Gr
idT,
typename IntersectorT>
897 const float frac = 1.0f / (1.0f + float(mSubPixels));
898 for (
size_t j=range.begin(), n=0, je = range.end(); j<je; ++j) {
899 for (
size_t i=0, ie = mCamera->
width(); i<ie; ++i) {
902 Film::RGBA c = mInter.intersectsWS(ray, xyz, nml) ? shader(xyz, nml, ray.dir()) : bg;
903 for (
size_t k=0; k<mSubPixels; ++k, n +=2 ) {
904 ray = mCamera->
getRay(i, j, mRand[n & 15], mRand[(n+1) & 15]);
905 c += mInter.intersectsWS(ray, xyz, nml) ? shader(xyz, nml, ray.dir()) : bg;
914 template<
typename IntersectorT,
typename SampleT>
917 : mAccessor(inter.grid().getConstAccessor())
919 , mPrimary(new IntersectorT(inter))
920 , mShadow(new IntersectorT(inter))
926 , mLightColor(0.7, 0.7, 0.7)
932 template<
typename IntersectorT,
typename SampleT>
935 : mAccessor(other.mAccessor)
936 , mCamera(other.mCamera)
937 , mPrimary(new IntersectorT(*(other.mPrimary)))
938 , mShadow(new IntersectorT(*(other.mShadow)))
939 , mPrimaryStep(other.mPrimaryStep)
940 , mShadowStep(other.mShadowStep)
941 , mCutOff(other.mCutOff)
942 , mLightGain(other.mLightGain)
943 , mLightDir(other.mLightDir)
944 , mLightColor(other.mLightColor)
945 , mAbsorption(other.mAbsorption)
946 , mScattering(other.mScattering)
950 template<
typename IntersectorT,
typename SampleT>
952 print(std::ostream& os,
int verboseLevel)
954 if (verboseLevel>0) {
955 os <<
"\nPrimary step: " << mPrimaryStep
956 <<
"\nShadow step: " << mShadowStep
957 <<
"\nCutoff: " << mCutOff
958 <<
"\nLightGain: " << mLightGain
959 <<
"\nLightDir: " << mLightDir
960 <<
"\nLightColor: " << mLightColor
961 <<
"\nAbsorption: " << mAbsorption
962 <<
"\nScattering: " << mScattering << std::endl;
964 mPrimary->print(os, verboseLevel);
967 template<
typename IntersectorT,
typename SampleT>
971 mPrimary.reset(
new IntersectorT(inter));
972 mShadow.reset(
new IntersectorT(inter));
975 template<
typename IntersectorT,
typename SampleT>
979 tbb::blocked_range<size_t> range(0, mCamera->
height());
980 threaded ? tbb::parallel_for(range, *
this) : (*this)(range);
983 template<
typename IntersectorT,
typename SampleT>
987 SamplerType sampler(mAccessor, mShadow->grid().transform());
990 const Vec3R extinction = -mScattering-mAbsorption, One(1.0);
991 const Vec3R albedo = mLightColor*mScattering/(mScattering+mAbsorption);
992 const Real sGain = mLightGain;
993 const Real pStep = mPrimaryStep;
994 const Real sStep = mShadowStep;
995 const Real cutoff = mCutOff;
1004 std::vector<typename RayType::TimeSpan> pTS, sTS;
1009 for (
size_t j=range.begin(), je = range.end(); j<je; ++j) {
1010 for (
size_t i=0, ie = mCamera->
width(); i<ie; ++i) {
1012 bg.
a = bg.
r = bg.
g = bg.
b = 0;
1014 if( !mPrimary->setWorldRay(pRay))
continue;
1015 Vec3R pTrans(1.0), pLumi(0.0);
1018 while (mPrimary->march(pT0, pT1)) {
1019 for (
Real pT = pStep*ceil(pT0/pStep); pT <= pT1; pT += pStep) {
1021 mPrimary->hits(pTS);
1022 for (
size_t k=0; k<pTS.size(); ++k) {
1023 Real pT = pStep*ceil(pTS[k].t0/pStep), pT1=pTS[k].t1;
1024 for (; pT <= pT1; pT += pStep) {
1026 Vec3R pPos = mPrimary->getWorldPos(pT);
1027 const Real density = sampler.wsSample(pPos);
1028 if (density < cutoff)
continue;
1032 if( !mShadow->setWorldRay(sRay))
continue;
1035 while (mShadow->march(sT0, sT1)) {
1036 for (
Real sT = sStep*ceil(sT0/sStep); sT <= sT1; sT+= sStep) {
1039 for (
size_t l=0; l<sTS.size(); ++l) {
1040 Real sT = sStep*ceil(sTS[l].t0/sStep), sT1=sTS[l].t1;
1041 for (; sT <= sT1; sT+= sStep) {
1043 const Real d = sampler.wsSample(mShadow->getWorldPos(sT));
1044 if (d < cutoff)
continue;
1045 sTrans *=
math::Exp(extinction * d * sStep/(1.0+sT*sGain));
1046 if (sTrans.
lengthSqr()<cutoff)
goto Luminance;
1050 pLumi += albedo * sTrans * pTrans * (One-dT);
1052 if (pTrans.lengthSqr()<cutoff)
goto Pixel;
1070 #ifdef OPENVDB_USE_EXPLICIT_INSTANTIATION 1072 #ifdef OPENVDB_INSTANTIATE_RAYTRACER 1076 #define _FUNCTION(TreeT) \ 1077 void rayTrace(const Grid<TreeT>&, const BaseShader&, BaseCamera&, size_t, unsigned int, bool) 1081 #define _FUNCTION(TreeT) \ 1082 void rayTrace(const Grid<TreeT>&, const tools::LevelSetRayIntersector<Grid<TreeT>>&, const BaseShader&, BaseCamera&, size_t, unsigned int, bool) 1089 #endif // OPENVDB_USE_EXPLICIT_INSTANTIATION 1096 #endif // OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec3.h:375
const Vec3T & dir() const
Definition: Ray.h:100
void setDir(const Vec3Type &dir)
Definition: Ray.h:67
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
Coord Abs(const Coord &xyz)
Definition: Coord.h:518
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Mat4< double > Mat4d
Definition: Mat4.h:1355
Type Exp(const Type &x)
Return ex.
Definition: Math.h:710
OPENVDB_AX_API void print(const ast::Node &node, const bool numberStatements=true, std::ostream &os=std::cout, const char *indent=" ")
Writes a descriptive printout of a Node hierarchy into a target stream.
Axis-aligned bounding box.
Definition: BBox.h:23
void setEye(const Vec3Type &eye)
Definition: Ray.h:65
void scaleTimes(RealT scale)
Definition: Ray.h:85
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Multiply m0 by m1 and return the resulting matrix.
Definition: Mat3.h:597
Definition: Exceptions.h:65
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:363
Accelerated intersection of a ray with a narrow-band level set or a generic (e.g. density) volume...
Vec3< typename promote< T, typename Coord::ValueType >::type > operator+(const Vec3< T > &v0, const Coord &v1)
Allow a Coord to be added to or subtracted from a Vec3.
Definition: Coord.h:528
#define OPENVDB_ASSERT(X)
Definition: Assert.h:41
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:615
MatType unit(const MatType &mat, typename MatType::value_type eps=1.0e-8)
Return a copy of the given matrix with its upper 3×3 rows normalized.
Definition: Mat.h:648
Definition: Exceptions.h:13
Simple generator of random numbers over the range [0, 1)
Definition: Math.h:166
double Real
Definition: Types.h:60
#define OPENVDB_INSTANTIATE_CLASS
Definition: version.h.in:158
GridType
List of types that are currently supported by NanoVDB.
Definition: NanoVDB.h:219
MatType rotation(const Quat< typename MatType::value_type > &q, typename MatType::value_type eps=static_cast< typename MatType::value_type >(1.0e-8))
Return the rotation matrix specified by the given quaternion.
Definition: Mat.h:172
#define OPENVDB_REAL_TREE_INSTANTIATE(Function)
Definition: version.h.in:162
void postTranslate(const Vec3< T0 > &tr)
Right multiplies by the specified translation matrix, i.e. (*this) * Trans.
Definition: Mat4.h:714
A general linear transform using homogeneous coordinates to perform rotation, scaling, shear and translation.
Definition: Maps.h:295
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
T lengthSqr() const
Definition: Vec3.h:212
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:192
math::Vec3< Real > Vec3R
Definition: Types.h:72
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:218