45 #ifndef OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED 46 #define OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED 54 #include <boost/scoped_array.hpp> 58 #include <type_traits> 61 #ifdef OPENVDB_TOOLS_RAYTRACER_USE_EXR 62 #include <OpenEXR/ImfPixelType.h> 63 #include <OpenEXR/ImfChannelList.h> 64 #include <OpenEXR/ImfOutputFile.h> 65 #include <OpenEXR/ImfHeader.h> 66 #include <OpenEXR/ImfFrameBuffer.h> 79 template<
typename Gr
idT>
83 size_t pixelSamples = 1,
84 unsigned int seed = 0,
85 bool threaded =
true);
88 template<
typename Gr
idT,
typename IntersectorT>
93 size_t pixelSamples = 1,
94 unsigned int seed = 0,
95 bool threaded =
true);
102 template<
typename Gr
idT,
typename IntersectorT = tools::LevelSetRayIntersector<Gr
idT> >
108 using RayType =
typename IntersectorT::RayType;
114 size_t pixelSamples = 1,
115 unsigned int seed = 0);
122 size_t pixelSamples = 1,
123 unsigned int seed = 0);
132 void setGrid(
const GridT& grid);
136 void setIntersector(
const IntersectorT& inter);
154 void setPixelSamples(
size_t pixelSamples,
unsigned int seed = 0);
157 void render(
bool threaded =
true)
const;
161 void operator()(
const tbb::blocked_range<size_t>& range)
const;
164 const bool mIsMaster;
167 std::unique_ptr<const BaseShader> mShader;
179 template <
typename IntersectorT,
typename SamplerT = tools::BoxSampler>
185 using RayType =
typename IntersectorT::RayType;
189 static_assert(std::is_floating_point<ValueType>::value,
190 "VolumeRender requires a floating-point-valued grid");
199 void render(
bool threaded=
true)
const;
206 void setIntersector(
const IntersectorT& inter);
238 void print(std::ostream& os = std::cout,
int verboseLevel = 1);
242 void operator()(
const tbb::blocked_range<size_t>& range)
const;
246 AccessorType mAccessor;
248 std::unique_ptr<IntersectorT> mPrimary, mShadow;
249 Real mPrimaryStep, mShadowStep, mCutOff, mLightGain;
250 Vec3R mLightDir, mLightColor, mAbsorption, mScattering;
266 RGBA() : r(0), g(0), b(0), a(1) {}
267 explicit RGBA(
ValueT intensity) : r(intensity), g(intensity), b(intensity), a(1) {}
269 r(_r), g(_g), b(_b), a(_a)
271 RGBA(
double _r,
double _g,
double _b,
double _a = 1.0)
272 : r(static_cast<
ValueT>(_r))
273 , g(static_cast<
ValueT>(_g))
274 , b(static_cast<
ValueT>(_b))
275 , a(static_cast<
ValueT>(_a))
285 const float s = rhs.
a*(1.0f-a);
296 Film(
size_t width,
size_t height)
297 : mWidth(width), mHeight(height), mSize(width*height), mPixels(new
RGBA[mSize])
301 : mWidth(width), mHeight(height), mSize(width*height), mPixels(new
RGBA[mSize])
310 return mPixels[w + h*mWidth];
317 return mPixels[w + h*mWidth];
323 RGBA *p = mPixels.get();
324 for (
size_t j = 0; j < mHeight; ++j) {
325 for (
size_t i = 0;
i < mWidth; ++
i, ++p) {
326 *p = ((
i &
size) ^ (j &
size)) ? c1 : c2;
333 std::string name(fileName);
334 if (name.find_last_of(
".") == std::string::npos) name.append(
".ppm");
336 boost::scoped_array<unsigned char> buffer(
new unsigned char[3*mSize]);
337 unsigned char *tmp = buffer.get(), *q = tmp;
338 RGBA* p = mPixels.get();
341 *q++ =
static_cast<unsigned char>(255.0f*(*p ).r);
342 *q++ =
static_cast<unsigned char>(255.0f*(*p ).g);
343 *q++ =
static_cast<unsigned char>(255.0f*(*p++).b);
346 std::ofstream os(name.c_str(), std::ios_base::binary);
348 std::cerr <<
"Error opening PPM file \"" << name <<
"\"" << std::endl;
352 os <<
"P6\n" << mWidth <<
" " << mHeight <<
"\n255\n";
353 os.write(reinterpret_cast<const char*>(&(*tmp)), 3 * mSize *
sizeof(
unsigned char));
356 #ifdef OPENVDB_TOOLS_RAYTRACER_USE_EXR 357 void saveEXR(
const std::string& fileName,
size_t compression = 2,
size_t threads = 8)
359 std::string name(fileName);
360 if (name.find_last_of(
".") == std::string::npos) name.append(
".exr");
362 if (threads>0) Imf::setGlobalThreadCount(threads);
363 Imf::Header header(mWidth, mHeight);
364 if (compression==0) header.compression() = Imf::NO_COMPRESSION;
365 if (compression==1) header.compression() = Imf::RLE_COMPRESSION;
366 if (compression>=2) header.compression() = Imf::ZIP_COMPRESSION;
367 header.channels().insert(
"R", Imf::Channel(Imf::FLOAT));
368 header.channels().insert(
"G", Imf::Channel(Imf::FLOAT));
369 header.channels().insert(
"B", Imf::Channel(Imf::FLOAT));
370 header.channels().insert(
"A", Imf::Channel(Imf::FLOAT));
372 Imf::FrameBuffer framebuffer;
373 framebuffer.insert(
"R", Imf::Slice( Imf::FLOAT, (
char *) &(mPixels[0].r),
374 sizeof (RGBA),
sizeof (RGBA) * mWidth));
375 framebuffer.insert(
"G", Imf::Slice( Imf::FLOAT, (
char *) &(mPixels[0].g),
376 sizeof (RGBA),
sizeof (RGBA) * mWidth));
377 framebuffer.insert(
"B", Imf::Slice( Imf::FLOAT, (
char *) &(mPixels[0].b),
378 sizeof (RGBA),
sizeof (RGBA) * mWidth));
379 framebuffer.insert(
"A", Imf::Slice( Imf::FLOAT, (
char *) &(mPixels[0].a),
380 sizeof (RGBA),
sizeof (RGBA) * mWidth));
382 Imf::OutputFile file(name.c_str(), header);
383 file.setFrameBuffer(framebuffer);
384 file.writePixels(mHeight);
388 size_t width()
const {
return mWidth; }
389 size_t height()
const {
return mHeight; }
394 size_t mWidth, mHeight, mSize;
395 boost::scoped_array<RGBA> mPixels;
406 double frameWidth,
double nearPlane,
double farPlane)
408 , mScaleWidth(frameWidth)
409 , mScaleHeight(frameWidth * double(film.height()) / double(film.width()))
411 assert(nearPlane > 0 && farPlane > nearPlane);
415 mScreenToWorld.accumPostTranslation(translation);
416 this->initRay(nearPlane, farPlane);
423 size_t width()
const {
return mFilm->width(); }
424 size_t height()
const {
return mFilm->height(); }
432 const Vec3R orig = mScreenToWorld.applyMap(
Vec3R(0.0));
433 const Vec3R dir = orig - xyz;
435 Mat4d xform = math::aim<Mat4d>(dir, up);
438 this->initRay(mRay.t0(), mRay.t1());
444 return Vec3R( (2 *
i /
double(mFilm->width()) - 1) * mScaleWidth,
445 (1 - 2 * j / double(mFilm->height())) * mScaleHeight, z );
452 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const = 0;
457 mRay.setTimes(t0, t1);
458 mRay.setEye(mScreenToWorld.applyMap(
Vec3R(0.0)));
459 mRay.setDir(mScreenToWorld.applyJacobian(
Vec3R(0.0, 0.0, -1.0)));
490 double focalLength = 50.0,
491 double aperture = 41.2136,
492 double nearPlane = 1e-3,
494 :
BaseCamera(film,
rotation, translation, 0.5*aperture/focalLength, nearPlane, farPlane)
504 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const override 507 Vec3R dir = BaseCamera::rasterToScreen(
Real(
i) + iOffset,
Real(j) + jOffset, -1.0);
508 dir = BaseCamera::mScreenToWorld.applyJacobian(dir);
519 return 360.0 / M_PI * atan(aperture/(2.0*length));
525 return aperture/(2.0*(tan(fov * M_PI / 360.0)));
548 double frameWidth = 1.0,
549 double nearPlane = 1e-3,
557 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const override 560 Vec3R eye = BaseCamera::rasterToScreen(
Real(
i) + iOffset,
Real(j) + jOffset, 0.0);
561 ray.
setEye(BaseCamera::mScreenToWorld.applyMap(eye));
593 template<
typename GridT = Film::RGBA,
598 MatteShader(
const GridT& grid) : mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
603 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
604 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
610 typename GridT::ConstAccessor mAcc;
615 template<
typename SamplerType>
640 template<
typename GridT = Film::RGBA,
645 NormalShader(
const GridT& grid) : mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
650 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
651 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
652 return Film::RGBA(v[0]*(normal[0]+1.0), v[1]*(normal[1]+1.0), v[2]*(normal[2]+1.0));
657 typename GridT::ConstAccessor mAcc;
662 template<
typename SamplerType>
671 return mRGBA *
Film::RGBA(normal[0] + 1.0, normal[1] + 1.0, normal[2] + 1.0);
687 template<
typename GridT = Film::RGBA,
694 , mInvDim(1.0/bbox.extents())
695 , mAcc(grid.getAccessor())
696 , mXform(&grid.transform())
703 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
704 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
705 const Vec3R rgb = (xyz - mMin) * mInvDim;
711 const Vec3R mMin, mInvDim;
712 typename GridT::ConstAccessor mAcc;
717 template<
typename SamplerType>
722 : mMin(bbox.
min()), mInvDim(1.0/bbox.extents()), mRGBA(c) {}
727 const Vec3R rgb = (xyz - mMin)*mInvDim;
728 return mRGBA*
Film::RGBA(rgb[0], rgb[1], rgb[2]);
733 const Vec3R mMin, mInvDim;
747 template<
typename GridT = Film::RGBA,
752 DiffuseShader(
const GridT& grid): mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
757 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
758 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
767 typename GridT::ConstAccessor mAcc;
772 template <
typename SamplerType>
801 template<
typename Gr
idT>
810 tracer(grid, shader, camera, pixelSamples, seed);
815 template<
typename Gr
idT,
typename IntersectorT>
817 const IntersectorT& inter,
832 template<
typename Gr
idT,
typename IntersectorT>
833 inline LevelSetRayTracer<GridT, IntersectorT>::
834 LevelSetRayTracer(
const GridT& grid,
842 mShader(shader.copy()),
848 template<
typename Gr
idT,
typename IntersectorT>
858 mShader(shader.copy()),
864 template<
typename Gr
idT,
typename IntersectorT>
869 mInter(other.mInter),
870 mShader(other.mShader->copy()),
871 mCamera(other.mCamera),
872 mSubPixels(other.mSubPixels)
876 template<
typename Gr
idT,
typename IntersectorT>
880 if (mIsMaster)
delete [] mRand;
883 template<
typename Gr
idT,
typename IntersectorT>
888 mInter = IntersectorT(grid);
891 template<
typename Gr
idT,
typename IntersectorT>
899 template<
typename Gr
idT,
typename IntersectorT>
904 mShader.reset(shader.
copy());
907 template<
typename Gr
idT,
typename IntersectorT>
915 template<
typename Gr
idT,
typename IntersectorT>
920 if (pixelSamples == 0) {
923 mSubPixels = pixelSamples - 1;
925 if (mSubPixels > 0) {
926 mRand =
new double[16];
928 for (
size_t i=0;
i<16; ++
i) mRand[
i] = rand();
934 template<
typename Gr
idT,
typename IntersectorT>
938 tbb::blocked_range<size_t> range(0, mCamera->height());
939 threaded ? tbb::parallel_for(range, *
this) : (*this)(range);
942 template<
typename Gr
idT,
typename IntersectorT>
948 const float frac = 1.0f / (1.0f + mSubPixels);
949 for (
size_t j=range.begin(), n=0, je = range.end(); j<je; ++j) {
950 for (
size_t i=0, ie = mCamera->width();
i<ie; ++
i) {
952 RayType ray = mCamera->getRay(
i, j);
953 Film::RGBA c = mInter.intersectsWS(ray, xyz, nml) ? shader(xyz, nml, ray.dir()) : bg;
954 for (
size_t k=0; k<mSubPixels; ++k, n +=2 ) {
955 ray = mCamera->getRay(
i, j, mRand[n & 15], mRand[(n+1) & 15]);
956 c += mInter.intersectsWS(ray, xyz, nml) ? shader(xyz, nml, ray.dir()) : bg;
965 template<
typename IntersectorT,
typename SampleT>
968 : mAccessor(inter.grid().getConstAccessor())
970 , mPrimary(new IntersectorT(inter))
971 , mShadow(new IntersectorT(inter))
977 , mLightColor(0.7, 0.7, 0.7)
983 template<
typename IntersectorT,
typename SampleT>
986 : mAccessor(other.mAccessor)
987 , mCamera(other.mCamera)
988 , mPrimary(new IntersectorT(*(other.mPrimary)))
989 , mShadow(new IntersectorT(*(other.mShadow)))
990 , mPrimaryStep(other.mPrimaryStep)
991 , mShadowStep(other.mShadowStep)
992 , mCutOff(other.mCutOff)
993 , mLightGain(other.mLightGain)
994 , mLightDir(other.mLightDir)
995 , mLightColor(other.mLightColor)
996 , mAbsorption(other.mAbsorption)
997 , mScattering(other.mScattering)
1001 template<
typename IntersectorT,
typename SampleT>
1003 print(std::ostream& os,
int verboseLevel)
1005 if (verboseLevel>0) {
1006 os <<
"\nPrimary step: " << mPrimaryStep
1007 <<
"\nShadow step: " << mShadowStep
1008 <<
"\nCutoff: " << mCutOff
1009 <<
"\nLightGain: " << mLightGain
1010 <<
"\nLightDir: " << mLightDir
1011 <<
"\nLightColor: " << mLightColor
1012 <<
"\nAbsorption: " << mAbsorption
1013 <<
"\nScattering: " << mScattering << std::endl;
1015 mPrimary->print(os, verboseLevel);
1018 template<
typename IntersectorT,
typename SampleT>
1022 mPrimary.reset(
new IntersectorT(inter));
1023 mShadow.reset(
new IntersectorT(inter));
1026 template<
typename IntersectorT,
typename SampleT>
1030 tbb::blocked_range<size_t> range(0, mCamera->height());
1031 threaded ? tbb::parallel_for(range, *
this) : (*this)(range);
1034 template<
typename IntersectorT,
typename SampleT>
1038 SamplerType sampler(mAccessor, mShadow->grid().transform());
1041 const Vec3R extinction = -mScattering-mAbsorption, One(1.0);
1042 const Vec3R albedo = mLightColor*mScattering/(mScattering+mAbsorption);
1043 const Real sGain = mLightGain;
1044 const Real pStep = mPrimaryStep;
1045 const Real sStep = mShadowStep;
1046 const Real cutoff = mCutOff;
1055 std::vector<typename RayType::TimeSpan> pTS, sTS;
1060 for (
size_t j=range.begin(), je = range.end(); j<je; ++j) {
1061 for (
size_t i=0, ie = mCamera->width();
i<ie; ++
i) {
1063 bg.
a = bg.
r = bg.
g = bg.
b = 0;
1064 RayType pRay = mCamera->getRay(
i, j);
1065 if( !mPrimary->setWorldRay(pRay))
continue;
1066 Vec3R pTrans(1.0), pLumi(0.0);
1069 while (mPrimary->march(pT0, pT1)) {
1070 for (
Real pT = pStep*ceil(pT0/pStep); pT <= pT1; pT += pStep) {
1072 mPrimary->hits(pTS);
1073 for (
size_t k=0; k<pTS.size(); ++k) {
1074 Real pT = pStep*ceil(pTS[k].t0/pStep), pT1=pTS[k].t1;
1075 for (; pT <= pT1; pT += pStep) {
1077 Vec3R pPos = mPrimary->getWorldPos(pT);
1078 const Real density = sampler.wsSample(pPos);
1079 if (density < cutoff)
continue;
1083 if( !mShadow->setWorldRay(sRay))
continue;
1086 while (mShadow->march(sT0, sT1)) {
1087 for (
Real sT = sStep*ceil(sT0/sStep); sT <= sT1; sT+= sStep) {
1090 for (
size_t l=0; l<sTS.size(); ++l) {
1091 Real sT = sStep*ceil(sTS[l].t0/sStep), sT1=sTS[l].t1;
1092 for (; sT <= sT1; sT+= sStep) {
1094 const Real d = sampler.wsSample(mShadow->getWorldPos(sT));
1095 if (d < cutoff)
continue;
1096 sTrans *=
math::Exp(extinction * d * sStep/(1.0+sT*sGain));
1097 if (sTrans.
lengthSqr()<cutoff)
goto Luminance;
1101 pLumi += albedo * sTrans * pTrans * (One-dT);
1103 if (pTrans.lengthSqr()<cutoff)
goto Pixel;
1119 #endif // OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
void postTranslate(const Vec3< T0 > &tr)
Right multiplies by the specified translation matrix, i.e. (*this) * Trans.
Definition: Mat4.h:768
const Vec3T & dir() const
Definition: Ray.h:121
double Real
Definition: Types.h:63
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:215
A general linear transform using homogeneous coordinates to perform rotation, scaling, shear and translation.
Definition: Maps.h:324
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
void setDir(const Vec3Type &dir)
Definition: Ray.h:90
tbb::atomic< Index32 > i
Definition: LeafBuffer.h:71
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec3.h:388
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:643
Accelerated intersection of a ray with a narrow-band level set or a generic (e.g. density) volume...
#define OPENVDB_VERSION_NAME
Definition: version.h:43
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:202
Coord Abs(const Coord &xyz)
Definition: Coord.h:509
Type Exp(const Type &x)
Return .
Definition: Math.h:660
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:376
Definition: Exceptions.h:39
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:519
Definition: Exceptions.h:92
MatType unit(const MatType &mat, typename MatType::value_type eps=1.0e-8)
Return a copy of the given matrix with its upper 3x3 rows normalized.
Definition: Mat.h:676
void setEye(const Vec3Type &eye)
Definition: Ray.h:88
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Matrix multiplication.
Definition: Mat3.h:654
void scaleTimes(RealT scale)
Definition: Ray.h:107
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
T lengthSqr() const
Definition: Vec3.h:235
static constexpr size_t size
The size of a LeafBuffer when LeafBuffer::mOutOfCore is atomic.
Definition: LeafBuffer.h:85
math::Vec3< Real > Vec3R
Definition: Types.h:75
Simple generator of random numbers over the range [0, 1)
Definition: Math.h:135
Axis-aligned bounding box.
Definition: BBox.h:47