18 #ifndef OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
19 #define OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
33 #include <type_traits>
46 template<
typename Gr
idT>
50 size_t pixelSamples = 1,
51 unsigned int seed = 0,
52 bool threaded =
true);
55 template<
typename Gr
idT,
typename IntersectorT>
60 size_t pixelSamples = 1,
61 unsigned int seed = 0,
62 bool threaded =
true);
69 template<
typename Gr
idT,
typename IntersectorT = tools::LevelSetRayIntersector<Gr
idT> >
74 using Vec3Type =
typename IntersectorT::Vec3Type;
75 using RayType =
typename IntersectorT::RayType;
81 size_t pixelSamples = 1,
82 unsigned int seed = 0);
89 size_t pixelSamples = 1,
90 unsigned int seed = 0);
99 void setGrid(
const GridT& grid);
103 void setIntersector(
const IntersectorT& inter);
121 void setPixelSamples(
size_t pixelSamples,
unsigned int seed = 0);
124 void render(
bool threaded =
true)
const;
128 void operator()(
const tbb::blocked_range<size_t>& range)
const;
131 const bool mIsMaster;
134 std::unique_ptr<const BaseShader> mShader;
146 template <
typename IntersectorT,
typename SamplerT = tools::BoxSampler>
152 using RayType =
typename IntersectorT::RayType;
156 static_assert(std::is_floating_point<ValueType>::value,
157 "VolumeRender requires a floating-point-valued grid");
166 void render(
bool threaded=
true)
const;
173 void setIntersector(
const IntersectorT& inter);
205 void print(std::ostream& os = std::cout,
int verboseLevel = 1);
209 void operator()(
const tbb::blocked_range<size_t>& range)
const;
213 AccessorType mAccessor;
215 std::unique_ptr<IntersectorT> mPrimary, mShadow;
216 Real mPrimaryStep, mShadowStep, mCutOff, mLightGain;
217 Vec3R mLightDir, mLightColor, mAbsorption, mScattering;
233 RGBA() : r(0), g(0), b(0), a(1) {}
234 explicit RGBA(
ValueT intensity) : r(intensity), g(intensity), b(intensity), a(1) {}
236 r(_r), g(_g), b(_b), a(_a)
238 RGBA(
double _r,
double _g,
double _b,
double _a = 1.0)
239 : r(static_cast<
ValueT>(_r))
240 , g(static_cast<
ValueT>(_g))
241 , b(static_cast<
ValueT>(_b))
242 , a(static_cast<
ValueT>(_a))
252 const float s = rhs.
a*(1.0f-a);
263 Film(
size_t width,
size_t height)
264 : mWidth(width), mHeight(height), mSize(width*height), mPixels(new
RGBA[mSize])
268 : mWidth(width), mHeight(height), mSize(width*height), mPixels(new
RGBA[mSize])
277 return mPixels[w + h*mWidth];
284 return mPixels[w + h*mWidth];
287 void fill(
const RGBA& rgb=
RGBA(0)) {
for (
size_t i=0; i<mSize; ++i) mPixels[i] = rgb; }
290 RGBA *p = mPixels.get();
291 for (
size_t j = 0; j < mHeight; ++j) {
292 for (
size_t i = 0; i < mWidth; ++i, ++p) {
293 *p = ((i & size) ^ (j & size)) ? c1 : c2;
300 std::string name(fileName);
301 if (name.find_last_of(
".") == std::string::npos) name.append(
".ppm");
303 std::unique_ptr<unsigned char[]> buffer(
new unsigned char[3*mSize]);
304 unsigned char *tmp = buffer.get(), *q = tmp;
305 RGBA* p = mPixels.get();
308 *q++ =
static_cast<unsigned char>(255.0f*(*p ).r);
309 *q++ =
static_cast<unsigned char>(255.0f*(*p ).g);
310 *q++ =
static_cast<unsigned char>(255.0f*(*p++).b);
313 std::ofstream os(name.c_str(), std::ios_base::binary);
315 std::cerr <<
"Error opening PPM file \"" << name <<
"\"" << std::endl;
319 os <<
"P6\n" << mWidth <<
" " << mHeight <<
"\n255\n";
320 os.write(
reinterpret_cast<const char*
>(&(*tmp)), 3 * mSize *
sizeof(
unsigned char));
323 size_t width()
const {
return mWidth; }
324 size_t height()
const {
return mHeight; }
329 size_t mWidth, mHeight, mSize;
330 std::unique_ptr<RGBA[]> mPixels;
341 double frameWidth,
double nearPlane,
double farPlane)
343 , mScaleWidth(frameWidth)
344 , mScaleHeight(frameWidth * double(film.height()) / double(film.width()))
346 assert(nearPlane > 0 && farPlane > nearPlane);
350 mScreenToWorld.accumPostTranslation(translation);
351 this->initRay(nearPlane, farPlane);
358 size_t width()
const {
return mFilm->width(); }
359 size_t height()
const {
return mFilm->height(); }
367 const Vec3R orig = mScreenToWorld.applyMap(
Vec3R(0.0));
368 const Vec3R dir = orig - xyz;
370 Mat4d xform = math::aim<Mat4d>(dir, up);
371 xform.postTranslate(orig);
373 this->initRay(mRay.t0(), mRay.t1());
379 return Vec3R( (2 * i /
double(mFilm->width()) - 1) * mScaleWidth,
380 (1 - 2 * j /
double(mFilm->height())) * mScaleHeight, z );
387 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const = 0;
392 mRay.setTimes(t0, t1);
393 mRay.setEye(mScreenToWorld.applyMap(
Vec3R(0.0)));
394 mRay.setDir(mScreenToWorld.applyJacobian(
Vec3R(0.0, 0.0, -1.0)));
425 double focalLength = 50.0,
426 double aperture = 41.2136,
427 double nearPlane = 1e-3,
429 :
BaseCamera(film,
rotation, translation, 0.5*aperture/focalLength, nearPlane, farPlane)
439 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const override
442 Vec3R dir = BaseCamera::rasterToScreen(
Real(i) + iOffset,
Real(j) + jOffset, -1.0);
443 dir = BaseCamera::mScreenToWorld.applyJacobian(dir);
454 return 360.0 / M_PI * atan(aperture/(2.0*length));
460 return aperture/(2.0*(tan(fov * M_PI / 360.0)));
483 double frameWidth = 1.0,
484 double nearPlane = 1e-3,
492 size_t i,
size_t j,
double iOffset = 0.5,
double jOffset = 0.5)
const override
495 Vec3R eye = BaseCamera::rasterToScreen(
Real(i) + iOffset,
Real(j) + jOffset, 0.0);
496 ray.
setEye(BaseCamera::mScreenToWorld.applyMap(eye));
533 MatteShader(
const GridT& grid) : mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
538 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
539 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
545 typename GridT::ConstAccessor mAcc;
550 template<
typename SamplerType>
575 template<
typename GridT = Film::RGBA,
580 NormalShader(
const GridT& grid) : mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
585 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
586 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
587 return Film::RGBA(v[0]*(normal[0]+1.0), v[1]*(normal[1]+1.0), v[2]*(normal[2]+1.0));
592 typename GridT::ConstAccessor mAcc;
597 template<
typename SamplerType>
606 return mRGBA *
Film::RGBA(normal[0] + 1.0, normal[1] + 1.0, normal[2] + 1.0);
622 template<
typename GridT = Film::RGBA,
629 , mInvDim(1.0/bbox.extents())
630 , mAcc(grid.getAccessor())
631 , mXform(&grid.transform())
638 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
639 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
640 const Vec3R rgb = (xyz - mMin) * mInvDim;
646 const Vec3R mMin, mInvDim;
647 typename GridT::ConstAccessor mAcc;
652 template<
typename SamplerType>
657 : mMin(bbox.
min()), mInvDim(1.0/bbox.extents()), mRGBA(c) {}
662 const Vec3R rgb = (xyz - mMin)*mInvDim;
663 return mRGBA*
Film::RGBA(rgb[0], rgb[1], rgb[2]);
668 const Vec3R mMin, mInvDim;
682 template<
typename GridT = Film::RGBA,
687 DiffuseShader(
const GridT& grid): mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
692 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
693 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
702 typename GridT::ConstAccessor mAcc;
707 template <
typename SamplerType>
736 template<
typename Gr
idT>
745 tracer(grid, shader, camera, pixelSamples, seed);
750 template<
typename Gr
idT,
typename IntersectorT>
752 const IntersectorT& inter,
767 template<
typename Gr
idT,
typename IntersectorT>
777 mShader(shader.copy()),
783 template<
typename Gr
idT,
typename IntersectorT>
793 mShader(shader.copy()),
799 template<
typename Gr
idT,
typename IntersectorT>
804 mInter(other.mInter),
805 mShader(other.mShader->copy()),
806 mCamera(other.mCamera),
807 mSubPixels(other.mSubPixels)
811 template<
typename Gr
idT,
typename IntersectorT>
815 if (mIsMaster)
delete [] mRand;
818 template<
typename Gr
idT,
typename IntersectorT>
823 mInter = IntersectorT(grid);
826 template<
typename Gr
idT,
typename IntersectorT>
834 template<
typename Gr
idT,
typename IntersectorT>
839 mShader.reset(shader.
copy());
842 template<
typename Gr
idT,
typename IntersectorT>
850 template<
typename Gr
idT,
typename IntersectorT>
855 if (pixelSamples == 0) {
858 mSubPixels = pixelSamples - 1;
860 if (mSubPixels > 0) {
861 mRand =
new double[16];
863 for (
size_t i=0; i<16; ++i) mRand[i] = rand();
869 template<
typename Gr
idT,
typename IntersectorT>
871 render(
bool threaded)
const
873 tbb::blocked_range<size_t> range(0, mCamera->height());
874 threaded ? tbb::parallel_for(range, *
this) : (*this)(range);
877 template<
typename Gr
idT,
typename IntersectorT>
879 operator()(
const tbb::blocked_range<size_t>& range)
const
883 const float frac = 1.0f / (1.0f + float(mSubPixels));
884 for (
size_t j=range.begin(), n=0, je = range.end(); j<je; ++j) {
885 for (
size_t i=0, ie = mCamera->width(); i<ie; ++i) {
887 RayType ray = mCamera->getRay(i, j);
888 Film::RGBA c = mInter.intersectsWS(ray, xyz, nml) ? shader(xyz, nml, ray.dir()) : bg;
889 for (
size_t k=0; k<mSubPixels; ++k, n +=2 ) {
890 ray = mCamera->getRay(i, j, mRand[n & 15], mRand[(n+1) & 15]);
891 c += mInter.intersectsWS(ray, xyz, nml) ? shader(xyz, nml, ray.dir()) : bg;
900 template<
typename IntersectorT,
typename SampleT>
903 : mAccessor(inter.grid().getConstAccessor())
905 , mPrimary(new IntersectorT(inter))
906 , mShadow(new IntersectorT(inter))
912 , mLightColor(0.7, 0.7, 0.7)
918 template<
typename IntersectorT,
typename SampleT>
921 : mAccessor(other.mAccessor)
922 , mCamera(other.mCamera)
923 , mPrimary(new IntersectorT(*(other.mPrimary)))
924 , mShadow(new IntersectorT(*(other.mShadow)))
925 , mPrimaryStep(other.mPrimaryStep)
926 , mShadowStep(other.mShadowStep)
927 , mCutOff(other.mCutOff)
928 , mLightGain(other.mLightGain)
929 , mLightDir(other.mLightDir)
930 , mLightColor(other.mLightColor)
931 , mAbsorption(other.mAbsorption)
932 , mScattering(other.mScattering)
936 template<
typename IntersectorT,
typename SampleT>
938 print(std::ostream& os,
int verboseLevel)
940 if (verboseLevel>0) {
941 os <<
"\nPrimary step: " << mPrimaryStep
942 <<
"\nShadow step: " << mShadowStep
943 <<
"\nCutoff: " << mCutOff
944 <<
"\nLightGain: " << mLightGain
945 <<
"\nLightDir: " << mLightDir
946 <<
"\nLightColor: " << mLightColor
947 <<
"\nAbsorption: " << mAbsorption
948 <<
"\nScattering: " << mScattering << std::endl;
950 mPrimary->print(os, verboseLevel);
953 template<
typename IntersectorT,
typename SampleT>
957 mPrimary.reset(
new IntersectorT(inter));
958 mShadow.reset(
new IntersectorT(inter));
961 template<
typename IntersectorT,
typename SampleT>
963 render(
bool threaded)
const
965 tbb::blocked_range<size_t> range(0, mCamera->height());
966 threaded ? tbb::parallel_for(range, *
this) : (*this)(range);
969 template<
typename IntersectorT,
typename SampleT>
971 operator()(
const tbb::blocked_range<size_t>& range)
const
973 SamplerType sampler(mAccessor, mShadow->grid().transform());
976 const Vec3R extinction = -mScattering-mAbsorption, One(1.0);
977 const Vec3R albedo = mLightColor*mScattering/(mScattering+mAbsorption);
978 const Real sGain = mLightGain;
979 const Real pStep = mPrimaryStep;
980 const Real sStep = mShadowStep;
981 const Real cutoff = mCutOff;
990 std::vector<typename RayType::TimeSpan> pTS, sTS;
995 for (
size_t j=range.begin(), je = range.end(); j<je; ++j) {
996 for (
size_t i=0, ie = mCamera->width(); i<ie; ++i) {
998 bg.
a = bg.
r = bg.
g = bg.
b = 0;
999 RayType pRay = mCamera->getRay(i, j);
1000 if( !mPrimary->setWorldRay(pRay))
continue;
1001 Vec3R pTrans(1.0), pLumi(0.0);
1004 while (mPrimary->march(pT0, pT1)) {
1005 for (
Real pT = pStep*ceil(pT0/pStep); pT <= pT1; pT += pStep) {
1007 mPrimary->hits(pTS);
1008 for (
size_t k=0; k<pTS.size(); ++k) {
1009 Real pT = pStep*ceil(pTS[k].t0/pStep), pT1=pTS[k].t1;
1010 for (; pT <= pT1; pT += pStep) {
1012 Vec3R pPos = mPrimary->getWorldPos(pT);
1014 if (density < cutoff)
continue;
1018 if( !mShadow->setWorldRay(sRay))
continue;
1021 while (mShadow->march(sT0, sT1)) {
1022 for (
Real sT = sStep*ceil(sT0/sStep); sT <= sT1; sT+= sStep) {
1025 for (
size_t l=0; l<sTS.size(); ++l) {
1026 Real sT = sStep*ceil(sTS[l].t0/sStep), sT1=sTS[l].t1;
1027 for (; sT <= sT1; sT+= sStep) {
1029 const Real d = sampler.
wsSample(mShadow->getWorldPos(sT));
1030 if (d < cutoff)
continue;
1031 sTrans *=
math::Exp(extinction * d * sStep/(1.0+sT*sGain));
1032 if (sTrans.
lengthSqr()<cutoff)
goto Luminance;
1036 pLumi += albedo * sTrans * pTrans * (One-dT);
1038 if (pTrans.lengthSqr()<cutoff)
goto Pixel;
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Accelerated intersection of a ray with a narrow-band level set or a generic (e.g. density) volume....
Definition: Exceptions.h:65
A general linear transform using homogeneous coordinates to perform rotation, scaling,...
Definition: Maps.h:310
Axis-aligned bounding box.
Definition: BBox.h:24
Simple generator of random numbers over the range [0, 1)
Definition: Math.h:166
void setEye(const Vec3Type &eye)
Definition: Ray.h:64
void setDir(const Vec3Type &dir)
Definition: Ray.h:66
void scaleTimes(RealT scale)
Definition: Ray.h:84
const Vec3T & dir() const
Definition: Ray.h:99
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:195
T lengthSqr() const
Definition: Vec3.h:215
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec3.h:378
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:366
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.
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:637
Coord Abs(const Coord &xyz)
Definition: Coord.h:514
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:524
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:194
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:670
Type Exp(const Type &x)
Return ex.
Definition: Math.h:713
Mat4< double > Mat4d
Definition: Mat4.h:1368
@ Z_AXIS
Definition: Math.h:907
@ X_AXIS
Definition: Math.h:905
@ Y_AXIS
Definition: Math.h:906
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:611
double Real
Definition: Types.h:60
math::Vec3< Real > Vec3R
Definition: Types.h:72
Definition: Exceptions.h:13
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:180