OpenVDB 3.2.0

RayTracer.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2016 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
44
45 #ifndef OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
46 #define OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
47
48 #include <openvdb/Types.h>
49 #include <openvdb/math/BBox.h>
50 #include <openvdb/math/Ray.h>
51 #include <openvdb/math/Math.h>
52 #include <openvdb/tools/RayIntersector.h>
53 #include <openvdb/tools/Interpolation.h>
54 #include <boost/scoped_ptr.hpp>
55 #include <boost/scoped_array.hpp>
56 #include <fstream>
57 #include <vector>
58 #include <deque>
59
60 #ifdef OPENVDB_TOOLS_RAYTRACER_USE_EXR
61 #include <OpenEXR/ImfPixelType.h>
62 #include <OpenEXR/ImfChannelList.h>
63 #include <OpenEXR/ImfOutputFile.h>
64 #include <OpenEXR/ImfHeader.h>
65 #include <OpenEXR/ImfFrameBuffer.h>
66 #endif
67
68 namespace openvdb {
69 OPENVDB_USE_VERSION_NAMESPACE
70 namespace OPENVDB_VERSION_NAME {
71 namespace tools {
72
73 // Forward declarations
74 class BaseCamera;
75 class BaseShader;
76
78 template<typename GridT>
79 inline void rayTrace(const GridT&,
80 const BaseShader&,
81 BaseCamera&,
82 size_t pixelSamples = 1,
83 unsigned int seed = 0,
84 bool threaded = true);
85
87 template<typename GridT, typename IntersectorT>
88 inline void rayTrace(const GridT&,
89 const IntersectorT&,
90 const BaseShader&,
91 BaseCamera&,
92 size_t pixelSamples = 1,
93 unsigned int seed = 0,
94 bool threaded = true);
95
96
98
101 template<typename GridT, typename IntersectorT = tools::LevelSetRayIntersector<GridT> >
102 class LevelSetRayTracer
103 {
104 public:
105 typedef GridT GridType;
106 typedef typename IntersectorT::Vec3Type Vec3Type;
107 typedef typename IntersectorT::RayType RayType;
108
110 LevelSetRayTracer(const GridT& grid,
111 const BaseShader& shader,
112 BaseCamera& camera,
113 size_t pixelSamples = 1,
114 unsigned int seed = 0);
115
118 LevelSetRayTracer(const IntersectorT& inter,
119 const BaseShader& shader,
120 BaseCamera& camera,
121 size_t pixelSamples = 1,
122 unsigned int seed = 0);
123
125 LevelSetRayTracer(const LevelSetRayTracer& other);
126
128 ~LevelSetRayTracer();
129
131 void setGrid(const GridT& grid);
132
135 void setIntersector(const IntersectorT& inter);
136
144 void setShader(const BaseShader& shader);
145
147 void setCamera(BaseCamera& camera);
148
153 void setPixelSamples(size_t pixelSamples, unsigned int seed = 0);
154
156 void render(bool threaded = true) const;
157
160 void operator()(const tbb::blocked_range<size_t>& range) const;
161
162 private:
163 const bool mIsMaster;
164 double* mRand;
165 IntersectorT mInter;
166 boost::scoped_ptr<const BaseShader> mShader;
167 BaseCamera* mCamera;
168 size_t mSubPixels;
169 };// LevelSetRayTracer
170
171
173
178 template <typename IntersectorT, typename SamplerT = tools::BoxSampler>
179 class VolumeRender
180 {
181 public:
182
183 typedef typename IntersectorT::GridType GridType;
184 typedef typename IntersectorT::RayType RayType;
185 typedef typename GridType::ValueType ValueType;
186 typedef typename GridType::ConstAccessor AccessorType;
187 typedef tools::GridSampler<AccessorType, SamplerT> SamplerType;
188 BOOST_STATIC_ASSERT(boost::is_floating_point<ValueType>::value);
189
191 VolumeRender(const IntersectorT& inter, BaseCamera& camera);
192
194 VolumeRender(const VolumeRender& other);
195
197 void render(bool threaded=true) const;
198
200 void setCamera(BaseCamera& camera) { mCamera = &camera; }
201
204 void setIntersector(const IntersectorT& inter);
205
208 void setLightDir(Real x, Real y, Real z) { mLightDir = Vec3R(x,y,z).unit(); }
209
211 void setLightColor(Real r, Real g, Real b) { mLightColor = Vec3R(r,g,b); }
212
214 void setPrimaryStep(Real primaryStep) { mPrimaryStep = primaryStep; }
215
217 void setShadowStep(Real shadowStep) { mShadowStep = shadowStep; }
218
220 void setScattering(Real x, Real y, Real z) { mScattering = Vec3R(x,y,z); }
221
223 void setAbsorption(Real x, Real y, Real z) { mAbsorption = Vec3R(x,y,z); }
224
227 void setLightGain(Real gain) { mLightGain = gain; }
228
230 void setCutOff(Real cutOff) { mCutOff = cutOff; }
231
236 void print(std::ostream& os = std::cout, int verboseLevel = 1);
237
240 void operator()(const tbb::blocked_range<size_t>& range) const;
241
242 private:
243
244 AccessorType mAccessor;
245 BaseCamera* mCamera;
246 boost::scoped_ptr<IntersectorT> mPrimary, mShadow;
247 Real mPrimaryStep, mShadowStep, mCutOff, mLightGain;
248 Vec3R mLightDir, mLightColor, mAbsorption, mScattering;
249 };//VolumeRender
250
252
255 class Film
256 {
257 public:
260 struct RGBA
261 {
262 typedef float ValueT;
263
264 RGBA() : r(0), g(0), b(0), a(1) {}
265 explicit RGBA(ValueT intensity) : r(intensity), g(intensity), b(intensity), a(1) {}
266 RGBA(ValueT _r, ValueT _g, ValueT _b, ValueT _a = static_cast<ValueT>(1.0)):
267 r(_r), g(_g), b(_b), a(_a)
268 {}
269
270 RGBA operator* (ValueT scale) const { return RGBA(r*scale, g*scale, b*scale);}
271 RGBA operator+ (const RGBA& rhs) const { return RGBA(r+rhs.r, g+rhs.g, b+rhs.b);}
272 RGBA operator* (const RGBA& rhs) const { return RGBA(r*rhs.r, g*rhs.g, b*rhs.b);}
273 RGBA& operator+=(const RGBA& rhs) { r+=rhs.r; g+=rhs.g; b+=rhs.b, a+=rhs.a; return *this;}
274
275 void over(const RGBA& rhs)
276 {
277 const float s = rhs.a*(1.0f-a);
278 r = a*r+s*rhs.r;
279 g = a*g+s*rhs.g;
280 b = a*b+s*rhs.b;
281 a = a + s;
282 }
283
284 ValueT r, g, b, a;
285 };
286
287
288 Film(size_t width, size_t height)
289 : mWidth(width), mHeight(height), mSize(width*height), mPixels(new RGBA[mSize])
290 {
291 }
292 Film(size_t width, size_t height, const RGBA& bg)
293 : mWidth(width), mHeight(height), mSize(width*height), mPixels(new RGBA[mSize])
294 {
295 this->fill(bg);
296 }
297
298 const RGBA& pixel(size_t w, size_t h) const
299 {
300 assert(w < mWidth);
301 assert(h < mHeight);
302 return mPixels[w + h*mWidth];
303 }
304
305 RGBA& pixel(size_t w, size_t h)
306 {
307 assert(w < mWidth);
308 assert(h < mHeight);
309 return mPixels[w + h*mWidth];
310 }
311
312 void fill(const RGBA& rgb=RGBA(0)) { for (size_t i=0; i<mSize; ++i) mPixels[i] = rgb; }
313 void checkerboard(const RGBA& c1=RGBA(0.3f), const RGBA& c2=RGBA(0.6f), size_t size=32)
314 {
315 RGBA *p = mPixels.get();
316 for (size_t j = 0; j < mHeight; ++j) {
317 for (size_t i = 0; i < mWidth; ++i, ++p) {
318 *p = ((i & size) ^ (j & size)) ? c1 : c2;
319 }
320 }
321 }
322
323 void savePPM(const std::string& fileName)
324 {
325 std::string name(fileName);
326 if (name.find_last_of(".") == std::string::npos) name.append(".ppm");
327
328 boost::scoped_array<unsigned char> buffer(new unsigned char[3*mSize]);
329 unsigned char *tmp = buffer.get(), *q = tmp;
330 RGBA* p = mPixels.get();
331 size_t n = mSize;
332 while (n--) {
333 *q++ = static_cast<unsigned char>(255.0f*(*p ).r);
334 *q++ = static_cast<unsigned char>(255.0f*(*p ).g);
335 *q++ = static_cast<unsigned char>(255.0f*(*p++).b);
336 }
337
338 std::ofstream os(name.c_str(), std::ios_base::binary);
339 if (!os.is_open()) {
340 std::cerr << "Error opening PPM file \"" << name << "\"" << std::endl;
341 return;
342 }
343
344 os << "P6\n" << mWidth << " " << mHeight << "\n255\n";
345 os.write((const char *)&(*tmp), 3*mSize*sizeof(unsigned char));
346 }
347
348 #ifdef OPENVDB_TOOLS_RAYTRACER_USE_EXR
349 void saveEXR(const std::string& fileName, size_t compression = 2, size_t threads = 8)
350 {
351 std::string name(fileName);
352 if (name.find_last_of(".") == std::string::npos) name.append(".exr");
353
354 if (threads>0) Imf::setGlobalThreadCount(threads);
355 Imf::Header header(mWidth, mHeight);
356 if (compression==0) header.compression() = Imf::NO_COMPRESSION;
357 if (compression==1) header.compression() = Imf::RLE_COMPRESSION;
358 if (compression>=2) header.compression() = Imf::ZIP_COMPRESSION;
359 header.channels().insert("R", Imf::Channel(Imf::FLOAT));
360 header.channels().insert("G", Imf::Channel(Imf::FLOAT));
361 header.channels().insert("B", Imf::Channel(Imf::FLOAT));
362 header.channels().insert("A", Imf::Channel(Imf::FLOAT));
363
364 Imf::FrameBuffer framebuffer;
365 framebuffer.insert("R", Imf::Slice( Imf::FLOAT, (char *) &(mPixels[0].r),
366 sizeof (RGBA), sizeof (RGBA) * mWidth));
367 framebuffer.insert("G", Imf::Slice( Imf::FLOAT, (char *) &(mPixels[0].g),
368 sizeof (RGBA), sizeof (RGBA) * mWidth));
369 framebuffer.insert("B", Imf::Slice( Imf::FLOAT, (char *) &(mPixels[0].b),
370 sizeof (RGBA), sizeof (RGBA) * mWidth));
371 framebuffer.insert("A", Imf::Slice( Imf::FLOAT, (char *) &(mPixels[0].a),
372 sizeof (RGBA), sizeof (RGBA) * mWidth));
373
374 Imf::OutputFile file(name.c_str(), header);
375 file.setFrameBuffer(framebuffer);
376 file.writePixels(mHeight);
377 }
378 #endif
379
380 size_t width() const { return mWidth; }
381 size_t height() const { return mHeight; }
382 size_t numPixels() const { return mSize; }
383 const RGBA* pixels() const { return mPixels.get(); }
384
385 private:
386 size_t mWidth, mHeight, mSize;
387 boost::scoped_array<RGBA> mPixels;
388 };// Film
389
390
392
394 class BaseCamera
395 {
396 public:
397 BaseCamera(Film& film, const Vec3R& rotation, const Vec3R& translation,
398 double frameWidth, double nearPlane, double farPlane)
399 : mFilm(&film)
400 , mScaleWidth(frameWidth)
401 , mScaleHeight(frameWidth * double(film.height()) / double(film.width()))
402 {
403 assert(nearPlane > 0 && farPlane > nearPlane);
404 mScreenToWorld.accumPostRotation(math::X_AXIS, rotation[0] * M_PI / 180.0);
405 mScreenToWorld.accumPostRotation(math::Y_AXIS, rotation[1] * M_PI / 180.0);
406 mScreenToWorld.accumPostRotation(math::Z_AXIS, rotation[2] * M_PI / 180.0);
407 mScreenToWorld.accumPostTranslation(translation);
408 this->initRay(nearPlane, farPlane);
409 }
410
411 virtual ~BaseCamera() {}
412
413 Film::RGBA& pixel(size_t i, size_t j) { return mFilm->pixel(i, j); }
414
415 size_t width() const { return mFilm->width(); }
416 size_t height() const { return mFilm->height(); }
417
422 void lookAt(const Vec3R& xyz, const Vec3R& up = Vec3R(0.0, 1.0, 0.0))
423 {
424 const Vec3R orig = mScreenToWorld.applyMap(Vec3R(0.0));
425 const Vec3R dir = orig - xyz;
426 try {
427 Mat4d xform = math::aim<Mat4d>(dir, up);
428 xform.postTranslate(orig);
429 mScreenToWorld = math::AffineMap(xform);
430 this->initRay(mRay.t0(), mRay.t1());
431 } catch (...) {}
432 }
433
434 Vec3R rasterToScreen(double i, double j, double z) const
435 {
436 return Vec3R( (2 * i / double(mFilm->width()) - 1) * mScaleWidth,
437 (1 - 2 * j / double(mFilm->height())) * mScaleHeight, z );
438 }
439
443 virtual math::Ray<double> getRay(
444 size_t i, size_t j, double iOffset = 0.5, double jOffset = 0.5) const = 0;
445
446 protected:
447 void initRay(double t0, double t1)
448 {
449 mRay.setTimes(t0, t1);
450 mRay.setEye(mScreenToWorld.applyMap(Vec3R(0.0)));
451 mRay.setDir(mScreenToWorld.applyJacobian(Vec3R(0.0, 0.0, -1.0)));
452 }
453
454 Film* mFilm;
455 double mScaleWidth, mScaleHeight;
456 math::Ray<double> mRay;
457 math::AffineMap mScreenToWorld;
458 };// BaseCamera
459
460
461 class PerspectiveCamera: public BaseCamera
462 {
463 public:
479 PerspectiveCamera(Film& film,
480 const Vec3R& rotation = Vec3R(0.0),
481 const Vec3R& translation = Vec3R(0.0),
482 double focalLength = 50.0,
483 double aperture = 41.2136,
484 double nearPlane = 1e-3,
485 double farPlane = std::numeric_limits<double>::max())
486 : BaseCamera(film, rotation, translation, 0.5*aperture/focalLength, nearPlane, farPlane)
487 {
488 }
489
490 virtual ~PerspectiveCamera() {}
491
495 virtual math::Ray<double> getRay(
496 size_t i, size_t j, double iOffset = 0.5, double jOffset = 0.5) const
497 {
498 math::Ray<double> ray(mRay);
499 Vec3R dir = BaseCamera::rasterToScreen(Real(i) + iOffset, Real(j) + jOffset, -1.0);
500 dir = BaseCamera::mScreenToWorld.applyJacobian(dir);
501 dir.normalize();
502 ray.scaleTimes(1.0/dir.dot(ray.dir()));
503 ray.setDir(dir);
504 return ray;
505 }
506
509 static double focalLengthToFieldOfView(double length, double aperture)
510 {
511 return 360.0 / M_PI * atan(aperture/(2.0*length));
512 }
515 static double fieldOfViewToFocalLength(double fov, double aperture)
516 {
517 return aperture/(2.0*(tan(fov * M_PI / 360.0)));
518 }
519 };// PerspectiveCamera
520
521
522 class OrthographicCamera: public BaseCamera
523 {
524 public:
537 OrthographicCamera(Film& film,
538 const Vec3R& rotation = Vec3R(0.0),
539 const Vec3R& translation = Vec3R(0.0),
540 double frameWidth = 1.0,
541 double nearPlane = 1e-3,
542 double farPlane = std::numeric_limits<double>::max())
543 : BaseCamera(film, rotation, translation, 0.5*frameWidth, nearPlane, farPlane)
544 {
545 }
546 virtual ~OrthographicCamera() {}
547
548 virtual math::Ray<double> getRay(
549 size_t i, size_t j, double iOffset = 0.5, double jOffset = 0.5) const
550 {
551 math::Ray<double> ray(mRay);
552 Vec3R eye = BaseCamera::rasterToScreen(Real(i) + iOffset, Real(j) + jOffset, 0.0);
553 ray.setEye(BaseCamera::mScreenToWorld.applyMap(eye));
554 return ray;
555 }
556 };// OrthographicCamera
557
558
560
561
563 class BaseShader
564 {
565 public:
566 typedef math::Ray<Real> RayT;
567 BaseShader() {}
568 virtual ~BaseShader() {}
573 virtual Film::RGBA operator()(const Vec3R& xyz, const Vec3R& nml, const Vec3R& dir) const = 0;
574 virtual BaseShader* copy() const = 0;
575 };
576
577
584 template <typename GridT = Film::RGBA,
585 typename SamplerType = tools::PointSampler>
586 class MatteShader: public BaseShader
587 {
588 public:
589 MatteShader(const GridT& grid) : mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
590 virtual ~MatteShader() {}
591 virtual Film::RGBA operator()(const Vec3R& xyz, const Vec3R&, const Vec3R&) const
592 {
593 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
594 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
595 return Film::RGBA(
596 static_cast<Film::RGBA::ValueT>(v[0]),
597 static_cast<Film::RGBA::ValueT>(v[1]),
598 static_cast<Film::RGBA::ValueT>(v[2]));
599 }
600 virtual BaseShader* copy() const { return new MatteShader<GridT, SamplerType>(*this); }
601
602 private:
603 typename GridT::ConstAccessor mAcc;
604 const math::Transform* mXform;
605 };
606 // Template specialization using a constant color of the material.
607 template <typename SamplerType>
608 class MatteShader<Film::RGBA, SamplerType>: public BaseShader
609 {
610 public:
611 MatteShader(const Film::RGBA& c = Film::RGBA(1.0f)): mRGBA(c) {}
612 virtual ~MatteShader() {}
613 virtual Film::RGBA operator()(const Vec3R&, const Vec3R&, const Vec3R&) const
614 {
615 return mRGBA;
616 }
617 virtual BaseShader* copy() const { return new MatteShader<Film::RGBA, SamplerType>(*this); }
618
619 private:
620 const Film::RGBA mRGBA;
621 };
622
623
631 template <typename GridT = Film::RGBA,
632 typename SamplerType = tools::PointSampler>
633 class NormalShader: public BaseShader
634 {
635 public:
636 NormalShader(const GridT& grid) : mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
637 virtual ~NormalShader() {}
638 virtual Film::RGBA operator()(const Vec3R& xyz, const Vec3R& normal, const Vec3R&) const
639 {
640 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
641 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
642 return Film::RGBA(v[0]*(normal[0]+1.0f), v[1]*(normal[1]+1.0f), v[2]*(normal[2]+1.0f));
643 }
644 virtual BaseShader* copy() const { return new NormalShader<GridT, SamplerType>(*this); }
645
646 private:
647 typename GridT::ConstAccessor mAcc;
648 const math::Transform* mXform;
649 };
650 // Template specialization using a constant color of the material.
651 template <typename SamplerType>
652 class NormalShader<Film::RGBA, SamplerType>: public BaseShader
653 {
654 public:
655 NormalShader(const Film::RGBA& c = Film::RGBA(1.0f)) : mRGBA(c*0.5f) {}
656 virtual ~NormalShader() {}
657 virtual Film::RGBA operator()(const Vec3R&, const Vec3R& normal, const Vec3R&) const
658 {
659 return mRGBA*Film::RGBA(normal[0]+1.0f, normal[1]+1.0f, normal[2]+1.0f);
660 }
661 virtual BaseShader* copy() const { return new NormalShader<Film::RGBA, SamplerType>(*this); }
662
663 private:
664 const Film::RGBA mRGBA;
665 };
666
667
675 template <typename GridT = Film::RGBA,
676 typename SamplerType = tools::PointSampler>
677 class PositionShader: public BaseShader
678 {
679 public:
680 PositionShader(const math::BBox<Vec3R>& bbox, const GridT& grid)
681 : mMin(bbox.min())
682 , mInvDim(1.0/bbox.extents())
683 , mAcc(grid.getAccessor())
684 , mXform(&grid.transform())
685 {
686 }
687 virtual ~PositionShader() {}
688 virtual Film::RGBA operator()(const Vec3R& xyz, const Vec3R&, const Vec3R&) const
689 {
690 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
691 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
692 const Vec3R rgb = (xyz - mMin)*mInvDim;
693 return Film::RGBA(v[0],v[1],v[2]) * Film::RGBA(rgb[0], rgb[1], rgb[2]);
694 }
695 virtual BaseShader* copy() const { return new PositionShader<GridT, SamplerType>(*this); }
696
697 private:
698 const Vec3R mMin, mInvDim;
699 typename GridT::ConstAccessor mAcc;
700 const math::Transform* mXform;
701 };
702 // Template specialization using a constant color of the material.
703 template <typename SamplerType>
704 class PositionShader<Film::RGBA, SamplerType>: public BaseShader
705 {
706 public:
707 PositionShader(const math::BBox<Vec3R>& bbox, const Film::RGBA& c = Film::RGBA(1.0f))
708 : mMin(bbox.min()), mInvDim(1.0/bbox.extents()), mRGBA(c) {}
709 virtual ~PositionShader() {}
710 virtual Film::RGBA operator()(const Vec3R& xyz, const Vec3R&, const Vec3R&) const
711 {
712 const Vec3R rgb = (xyz - mMin)*mInvDim;
713 return mRGBA*Film::RGBA(rgb[0], rgb[1], rgb[2]);
714 }
715 virtual BaseShader* copy() const { return new PositionShader<Film::RGBA, SamplerType>(*this); }
716
717 private:
718 const Vec3R mMin, mInvDim;
719 const Film::RGBA mRGBA;
720 };
721
731 template <typename GridT = Film::RGBA,
732 typename SamplerType = tools::PointSampler>
733 class DiffuseShader: public BaseShader
734 {
735 public:
736 DiffuseShader(const GridT& grid): mAcc(grid.getAccessor()), mXform(&grid.transform()) {}
737 virtual ~DiffuseShader() {}
738 virtual Film::RGBA operator()(const Vec3R& xyz, const Vec3R& normal, const Vec3R& rayDir) const
739 {
740 typename GridT::ValueType v = zeroVal<typename GridT::ValueType>();
741 SamplerType::sample(mAcc, mXform->worldToIndex(xyz), v);
742 // We take the abs of the dot product corresponding to having
743 // light sources at +/- rayDir, i.e., two-sided shading.
744 return Film::RGBA(v[0],v[1],v[2]) * math::Abs(normal.dot(rayDir));
745 }
746 virtual BaseShader* copy() const { return new DiffuseShader<GridT, SamplerType>(*this); }
747
748 private:
749 typename GridT::ConstAccessor mAcc;
750 const math::Transform* mXform;
751 };
752 // Template specialization using a constant color of the material.
753 template <typename SamplerType>
754 class DiffuseShader<Film::RGBA, SamplerType>: public BaseShader
755 {
756 public:
757 DiffuseShader(const Film::RGBA& d = Film::RGBA(1.0f)): mRGBA(d) {}
758 virtual ~DiffuseShader() {}
759 virtual Film::RGBA operator()(const Vec3R&, const Vec3R& normal, const Vec3R& rayDir) const
760 {
761 // We assume a single directional light source at the camera,
762 // so the cosine of the angle between the surface normal and the
763 // direction of the light source becomes the dot product of the
764 // surface normal and inverse direction of the ray. We also ignore
765 // negative dot products, corresponding to strict one-sided shading.
766 //return mRGBA * math::Max(0.0, normal.dot(-rayDir));
767
768 // We take the abs of the dot product corresponding to having
769 // light sources at +/- rayDir, i.e., two-sided shading.
770 return mRGBA * math::Abs(normal.dot(rayDir));
771 }
772 virtual BaseShader* copy() const { return new DiffuseShader<Film::RGBA, SamplerType>(*this); }
773
774 private:
775 const Film::RGBA mRGBA;
776 };
777
779
780 template<typename GridT>
781 inline void rayTrace(const GridT& grid,
782 const BaseShader& shader,
783 BaseCamera& camera,
784 size_t pixelSamples,
785 unsigned int seed,
786 bool threaded)
787 {
788 LevelSetRayTracer<GridT, tools::LevelSetRayIntersector<GridT> >
789 tracer(grid, shader, camera, pixelSamples, seed);
790 tracer.render(threaded);
791 }
792
793
794 template<typename GridT, typename IntersectorT>
795 inline void rayTrace(const GridT&,
796 const IntersectorT& inter,
797 const BaseShader& shader,
798 BaseCamera& camera,
799 size_t pixelSamples,
800 unsigned int seed,
801 bool threaded)
802 {
803 LevelSetRayTracer<GridT, IntersectorT> tracer(inter, shader, camera, pixelSamples, seed);
804 tracer.render(threaded);
805 }
806
807
809
810
811 template<typename GridT, typename IntersectorT>
812 inline LevelSetRayTracer<GridT, IntersectorT>::
813 LevelSetRayTracer(const GridT& grid,
814 const BaseShader& shader,
815 BaseCamera& camera,
816 size_t pixelSamples,
817 unsigned int seed)
818 : mIsMaster(true),
819 mRand(NULL),
820 mInter(grid),
821 mShader(shader.copy()),
822 mCamera(&camera)
823 {
824 this->setPixelSamples(pixelSamples, seed);
825 }
826
827 template<typename GridT, typename IntersectorT>
828 inline LevelSetRayTracer<GridT, IntersectorT>::
829 LevelSetRayTracer(const IntersectorT& inter,
830 const BaseShader& shader,
831 BaseCamera& camera,
832 size_t pixelSamples,
833 unsigned int seed)
834 : mIsMaster(true),
835 mRand(NULL),
836 mInter(inter),
837 mShader(shader.copy()),
838 mCamera(&camera)
839 {
840 this->setPixelSamples(pixelSamples, seed);
841 }
842
843 template<typename GridT, typename IntersectorT>
844 inline LevelSetRayTracer<GridT, IntersectorT>::
845 LevelSetRayTracer(const LevelSetRayTracer& other) :
846 mIsMaster(false),
847 mRand(other.mRand),
848 mInter(other.mInter),
849 mShader(other.mShader->copy()),
850 mCamera(other.mCamera),
851 mSubPixels(other.mSubPixels)
852 {
853 }
854
855 template<typename GridT, typename IntersectorT>
856 inline LevelSetRayTracer<GridT, IntersectorT>::
857 ~LevelSetRayTracer()
858 {
859 if (mIsMaster) delete [] mRand;
860 }
861
862 template<typename GridT, typename IntersectorT>
863 inline void LevelSetRayTracer<GridT, IntersectorT>::
864 setGrid(const GridT& grid)
865 {
866 assert(mIsMaster);
867 mInter = IntersectorT(grid);
868 }
869
870 template<typename GridT, typename IntersectorT>
871 inline void LevelSetRayTracer<GridT, IntersectorT>::
872 setIntersector(const IntersectorT& inter)
873 {
874 assert(mIsMaster);
875 mInter = inter;
876 }
877
878 template<typename GridT, typename IntersectorT>
879 inline void LevelSetRayTracer<GridT, IntersectorT>::
880 setShader(const BaseShader& shader)
881 {
882 assert(mIsMaster);
883 mShader.reset(shader.copy());
884 }
885
886 template<typename GridT, typename IntersectorT>
887 inline void LevelSetRayTracer<GridT, IntersectorT>::
888 setCamera(BaseCamera& camera)
889 {
890 assert(mIsMaster);
891 mCamera = &camera;
892 }
893
894 template<typename GridT, typename IntersectorT>
895 inline void LevelSetRayTracer<GridT, IntersectorT>::
896 setPixelSamples(size_t pixelSamples, unsigned int seed)
897 {
898 assert(mIsMaster);
899 if (pixelSamples == 0) {
900 OPENVDB_THROW(ValueError, "pixelSamples must be larger than zero!");
901 }
902 mSubPixels = pixelSamples - 1;
903 delete [] mRand;
904 if (mSubPixels > 0) {
905 mRand = new double[16];
906 math::Rand01<double> rand(seed);//offsets for anti-aliaing by jittered super-sampling
907 for (size_t i=0; i<16; ++i) mRand[i] = rand();
908 } else {
909 mRand = NULL;
910 }
911 }
912
913 template<typename GridT, typename IntersectorT>
914 inline void LevelSetRayTracer<GridT, IntersectorT>::
915 render(bool threaded) const
916 {
917 tbb::blocked_range<size_t> range(0, mCamera->height());
918 threaded ? tbb::parallel_for(range, *this) : (*this)(range);
919 }
920
921 template<typename GridT, typename IntersectorT>
922 inline void LevelSetRayTracer<GridT, IntersectorT>::
923 operator()(const tbb::blocked_range<size_t>& range) const
924 {
925 const BaseShader& shader = *mShader;
926 Vec3Type xyz, nml;
927 const float frac = 1.0f / (1.0f + mSubPixels);
928 for (size_t j=range.begin(), n=0, je = range.end(); j<je; ++j) {
929 for (size_t i=0, ie = mCamera->width(); i<ie; ++i) {
930 Film::RGBA& bg = mCamera->pixel(i,j);
931 RayType ray = mCamera->getRay(i, j);//primary ray
932 Film::RGBA c = mInter.intersectsWS(ray, xyz, nml) ? shader(xyz, nml, ray.dir()) : bg;
933 for (size_t k=0; k<mSubPixels; ++k, n +=2 ) {
934 ray = mCamera->getRay(i, j, mRand[n & 15], mRand[(n+1) & 15]);
935 c += mInter.intersectsWS(ray, xyz, nml) ? shader(xyz, nml, ray.dir()) : bg;
936 }//loop over sub-pixels
937 bg = c*frac;
938 }//loop over image height
939 }//loop over image width
940 }
941
943
944 template<typename IntersectorT, typename SampleT>
945 inline VolumeRender<IntersectorT, SampleT>::
946 VolumeRender(const IntersectorT& inter, BaseCamera& camera)
947 : mAccessor(inter.grid().getConstAccessor())
948 , mCamera(&camera)
949 , mPrimary(new IntersectorT(inter))
950 , mShadow(new IntersectorT(inter))
951 , mPrimaryStep(1.0)
952 , mShadowStep(3.0)
953 , mCutOff(0.005)
954 , mLightGain(0.2)
955 , mLightDir(Vec3R(0.3, 0.3, 0).unit())
956 , mLightColor(0.7, 0.7, 0.7)
957 , mAbsorption(0.1)
958 , mScattering(1.5)
959 {
960 }
961
962 template<typename IntersectorT, typename SampleT>
963 inline VolumeRender<IntersectorT, SampleT>::
964 VolumeRender(const VolumeRender& other)
965 : mAccessor(other.mAccessor)
966 , mCamera(other.mCamera)
967 , mPrimary(new IntersectorT(*(other.mPrimary)))
968 , mShadow(new IntersectorT(*(other.mShadow)))
969 , mPrimaryStep(other.mPrimaryStep)
970 , mShadowStep(other.mShadowStep)
971 , mCutOff(other.mCutOff)
972 , mLightGain(other.mLightGain)
973 , mLightDir(other.mLightDir)
974 , mLightColor(other.mLightColor)
975 , mAbsorption(other.mAbsorption)
976 , mScattering(other.mScattering)
977 {
978 }
979
980 template<typename IntersectorT, typename SampleT>
981 inline void VolumeRender<IntersectorT, SampleT>::
982 print(std::ostream& os, int verboseLevel)
983 {
984 if (verboseLevel>0) {
985 os << "\nPrimary step: " << mPrimaryStep
986 << "\nShadow step: " << mShadowStep
987 << "\nCutoff: " << mCutOff
988 << "\nLightGain: " << mLightGain
989 << "\nLightDir: " << mLightDir
990 << "\nLightColor: " << mLightColor
991 << "\nAbsorption: " << mAbsorption
992 << "\nScattering: " << mScattering << std::endl;
993 }
994 mPrimary->print(os, verboseLevel);
995 }
996
997 template<typename IntersectorT, typename SampleT>
998 inline void VolumeRender<IntersectorT, SampleT>::
999 setIntersector(const IntersectorT& inter)
1000 {
1001 mPrimary.reset(new IntersectorT(inter));
1002 mShadow.reset( new IntersectorT(inter));
1003 }
1004
1005 template<typename IntersectorT, typename SampleT>
1006 inline void VolumeRender<IntersectorT, SampleT>::
1007 render(bool threaded) const
1008 {
1009 tbb::blocked_range<size_t> range(0, mCamera->height());
1010 threaded ? tbb::parallel_for(range, *this) : (*this)(range);
1011 }
1012
1013 template<typename IntersectorT, typename SampleT>
1014 inline void VolumeRender<IntersectorT, SampleT>::
1015 operator()(const tbb::blocked_range<size_t>& range) const
1016 {
1017 SamplerType sampler(mAccessor, mShadow->grid().transform());//light-weight wrapper
1018
1019 // Any variable prefixed with p (or s) means it's associated with a primary (or shadow) ray
1020 const Vec3R extinction = -mScattering-mAbsorption, One(1.0);
1021 const Vec3R albedo = mLightColor*mScattering/(mScattering+mAbsorption);//single scattering
1022 const Real sGain = mLightGain;//in-scattering along shadow ray
1023 const Real pStep = mPrimaryStep;//Integration step along primary ray in voxel units
1024 const Real sStep = mShadowStep;//Integration step along shadow ray in voxel units
1025 const Real cutoff = mCutOff;//Cutoff for density and transmittance
1026
1027 // For the sake of completeness we show how to use two different
1028 // methods (hits/march) in VolumeRayIntersector that produce
1029 // segments along the ray that intersects active values. Comment out
1030 // the line below to use VolumeRayIntersector::march instead of
1031 // VolumeRayIntersector::hits.
1032 #define USE_HITS
1033 #ifdef USE_HITS
1034 std::vector<typename RayType::TimeSpan> pTS, sTS;
1035 //std::deque<typename RayType::TimeSpan> pTS, sTS;
1036 #endif
1037
1038 RayType sRay(Vec3R(0), mLightDir);//Shadow ray
1039 for (size_t j=range.begin(), je = range.end(); j<je; ++j) {
1040 for (size_t i=0, ie = mCamera->width(); i<ie; ++i) {
1041 Film::RGBA& bg = mCamera->pixel(i, j);
1042 bg.a = bg.r = bg.g = bg.b = 0;
1043 RayType pRay = mCamera->getRay(i, j);// Primary ray
1044 if( !mPrimary->setWorldRay(pRay)) continue;
1045 Vec3R pTrans(1.0), pLumi(0.0);
1046 #ifndef USE_HITS
1047 Real pT0, pT1;
1048 while (mPrimary->march(pT0, pT1)) {
1049 for (Real pT = pStep*ceil(pT0/pStep); pT <= pT1; pT += pStep) {
1050 #else
1051 mPrimary->hits(pTS);
1052 for (size_t k=0; k<pTS.size(); ++k) {
1053 Real pT = pStep*ceil(pTS[k].t0/pStep), pT1=pTS[k].t1;
1054 for (; pT <= pT1; pT += pStep) {
1055 #endif
1056 Vec3R pPos = mPrimary->getWorldPos(pT);
1057 const Real density = sampler.wsSample(pPos);
1058 if (density < cutoff) continue;
1059 const Vec3R dT = math::Exp(extinction * density * pStep);
1060 Vec3R sTrans(1.0);
1061 sRay.setEye(pPos);
1062 if( !mShadow->setWorldRay(sRay)) continue;
1063 #ifndef USE_HITS
1064 Real sT0, sT1;
1065 while (mShadow->march(sT0, sT1)) {
1066 for (Real sT = sStep*ceil(sT0/sStep); sT <= sT1; sT+= sStep) {
1067 #else
1068 mShadow->hits(sTS);
1069 for (size_t l=0; l<sTS.size(); ++l) {
1070 Real sT = sStep*ceil(sTS[l].t0/sStep), sT1=sTS[l].t1;
1071 for (; sT <= sT1; sT+= sStep) {
1072 #endif
1073 const Real d = sampler.wsSample(mShadow->getWorldPos(sT));
1074 if (d < cutoff) continue;
1075 sTrans *= math::Exp(extinction * d * sStep/(1.0+sT*sGain));
1076 if (sTrans.lengthSqr()<cutoff) goto Luminance;//Terminate sRay
1077 }//Integration over shadow segment
1078 }// Shadow ray march
1079 Luminance:
1080 pLumi += albedo * sTrans * pTrans * (One-dT);
1081 pTrans *= dT;
1082 if (pTrans.lengthSqr()<cutoff) goto Pixel; // Terminate Ray
1083 }//Integration over primary segment
1084 }// Primary ray march
1085 Pixel:
1086 bg.r = static_cast<Film::RGBA::ValueT>(pLumi[0]);
1087 bg.g = static_cast<Film::RGBA::ValueT>(pLumi[1]);
1088 bg.b = static_cast<Film::RGBA::ValueT>(pLumi[2]);
1089 bg.a = static_cast<Film::RGBA::ValueT>(1.0f - pTrans.sum()/3.0f);
1090 }//Horizontal pixel scan
1091 }//Vertical pixel scan
1092 }
1093
1094 } // namespace tools
1095 } // namespace OPENVDB_VERSION_NAME
1096 } // namespace openvdb
1097
1098 #endif // OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
1099
1100 // Copyright (c) 2012-2016 DreamWorks Animation LLC
1101 // All rights reserved. This software is distributed under the
1102 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
openvdb::v3_2_0::tools::BaseCamera::width
size_t width() const
Definition: RayTracer.h:415
openvdb::v3_2_0::tools::LevelSetRayTracer::Vec3Type
IntersectorT::Vec3Type Vec3Type
Definition: RayTracer.h:106
openvdb::v3_2_0::tools::Film::savePPM
void savePPM(const std::string &fileName)
Definition: RayTracer.h:323
openvdb::v3_2_0::tools::PerspectiveCamera::getRay
virtual math::Ray< double > getRay(size_t i, size_t j, double iOffset=0.5, double jOffset=0.5) const
Return a Ray in world space given the pixel indices and optional offsets in the range [0...
Definition: RayTracer.h:495
openvdb::v3_2_0::tools::Film::RGBA::RGBA
RGBA(ValueT _r, ValueT _g, ValueT _b, ValueT _a=static_cast< ValueT >(1.0))
Definition: RayTracer.h:266
openvdb::v3_2_0::tools::Film::pixels
const RGBA * pixels() const
Definition: RayTracer.h:383
openvdb::v3_2_0::tools::VolumeRender::RayType
IntersectorT::RayType RayType
Definition: RayTracer.h:184
openvdb::v3_2_0::tools::NormalShader
Color shader that treats the surface normal (x, y, z) as an RGB color.
Definition: RayTracer.h:633
openvdb::v3_2_0::tools::BaseCamera::getRay
virtual math::Ray< double > getRay(size_t i, size_t j, double iOffset=0.5, double jOffset=0.5) const =0
Return a Ray in world space given the pixel indices and optional offsets in the range [0...
openvdb::v3_2_0::tools::VolumeRender::render
void render(bool threaded=true) const
Perform the actual (potentially multithreaded) volume rendering.
Definition: RayTracer.h:1007
openvdb::v3_2_0::tools::BaseShader::RayT
math::Ray< Real > RayT
Definition: RayTracer.h:566
openvdb::v3_2_0::math::Abs
Coord Abs(const Coord &xyz)
Definition: Coord.h:247
openvdb::v3_2_0::math::Vec3::dot
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:203
openvdb::v3_2_0::tools::PositionShader::PositionShader
PositionShader(const math::BBox< Vec3R > &bbox, const GridT &grid)
Definition: RayTracer.h:680
openvdb::v3_2_0::tools::Film
A simple class that allows for concurrent writes to pixels in an image, background initialization of ...
Definition: RayTracer.h:255
Types.h
openvdb::v3_2_0::tools::NormalShader< Film::RGBA, SamplerType >
Definition: RayTracer.h:652
openvdb::v3_2_0::tools::LevelSetRayTracer::setShader
void setShader(const BaseShader &shader)
Set the shader derived from the abstract BaseShader class.
Definition: RayTracer.h:880
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
openvdb::v3_2_0::tools::VolumeRender::ValueType
GridType::ValueType ValueType
Definition: RayTracer.h:185
openvdb::v3_2_0::math::X_AXIS
Definition: Math.h:857
openvdb::v3_2_0::tools::VolumeRender::AccessorType
GridType::ConstAccessor AccessorType
Definition: RayTracer.h:186
openvdb::v3_2_0::tools::PositionShader< Film::RGBA, SamplerType >
Definition: RayTracer.h:704
openvdb::v3_2_0::tools::PerspectiveCamera::~PerspectiveCamera
virtual ~PerspectiveCamera()
Definition: RayTracer.h:490
openvdb::v3_2_0::tools::OrthographicCamera
Definition: RayTracer.h:522
openvdb::v3_2_0::tools::DiffuseShader< Film::RGBA, SamplerType >::operator()
virtual Film::RGBA operator()(const Vec3R &, const Vec3R &normal, const Vec3R &rayDir) const
Defines the interface of the virtual function that returns a RGB color.
Definition: RayTracer.h:759
openvdb::v3_2_0::tools::VolumeRender::setIntersector
void setIntersector(const IntersectorT &inter)
Set the intersector that performs the actual intersection of the rays against the volume...
Definition: RayTracer.h:999
openvdb::v3_2_0::tools::VolumeRender::setCamera
void setCamera(BaseCamera &camera)
Set the camera derived from the abstract BaseCamera class.
Definition: RayTracer.h:200
openvdb::v3_2_0::tools::MatteShader< Film::RGBA, SamplerType >::~MatteShader
virtual ~MatteShader()
Definition: RayTracer.h:612
openvdb::v3_2_0::tools::VolumeRender::SamplerType
tools::GridSampler< AccessorType, SamplerT > SamplerType
Definition: RayTracer.h:187
openvdb::v3_2_0::math::Vec3::unit
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec3.h:360
openvdb::v3_2_0::tools::NormalShader::copy
virtual BaseShader * copy() const
Definition: RayTracer.h:644
openvdb::v3_2_0::math::Vec3< Real >
openvdb::v3_2_0::tools::MatteShader< Film::RGBA, SamplerType >
Definition: RayTracer.h:608
openvdb::v3_2_0::tools::DiffuseShader::DiffuseShader
DiffuseShader(const GridT &grid)
Definition: RayTracer.h:736
openvdb::v3_2_0::tools::BaseCamera::initRay
void initRay(double t0, double t1)
Definition: RayTracer.h:447
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
openvdb::v3_2_0::tools::VolumeRender::VolumeRender
VolumeRender(const IntersectorT &inter, BaseCamera &camera)
Constructor taking an intersector and a base camera.
Definition: RayTracer.h:946
openvdb::v3_2_0::math::rotation
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:153
openvdb::v3_2_0::tools::BaseCamera::mScreenToWorld
math::AffineMap mScreenToWorld
Definition: RayTracer.h:457
openvdb::v3_2_0::math::Rand01
Simple generator of random numbers over the range [0, 1)
Definition: Math.h:143
openvdb::v3_2_0::tools::VolumeRender::setScattering
void setScattering(Real x, Real y, Real z)
Set Scattering coefficients.
Definition: RayTracer.h:220
openvdb::v3_2_0::tools::Film::Film
Film(size_t width, size_t height, const RGBA &bg)
Definition: RayTracer.h:292
openvdb::v3_2_0::tools::Film::numPixels
size_t numPixels() const
Definition: RayTracer.h:382
openvdb::v3_2_0::tools::BaseCamera::pixel
Film::RGBA & pixel(size_t i, size_t j)
Definition: RayTracer.h:413
openvdb::v3_2_0::tools::Film::RGBA::RGBA
RGBA(ValueT intensity)
Definition: RayTracer.h:265
openvdb::v3_2_0::math::operator*
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Matrix multiplication.
Definition: Mat3.h:658
openvdb::v3_2_0::tools::NormalShader< Film::RGBA, SamplerType >::operator()
virtual Film::RGBA operator()(const Vec3R &, const Vec3R &normal, const Vec3R &) const
Defines the interface of the virtual function that returns a RGB color.
Definition: RayTracer.h:657
openvdb::v3_2_0::math::Y_AXIS
Definition: Math.h:858
openvdb::v3_2_0::tools::MatteShader< Film::RGBA, SamplerType >::operator()
virtual Film::RGBA operator()(const Vec3R &, const Vec3R &, const Vec3R &) const
Defines the interface of the virtual function that returns a RGB color.
Definition: RayTracer.h:613
openvdb::v3_2_0::tools::Film::RGBA::over
void over(const RGBA &rhs)
Definition: RayTracer.h:275
openvdb::v3_2_0::tools::composite::max
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:132
BBox.h
openvdb::v3_2_0::tools::OrthographicCamera::~OrthographicCamera
virtual ~OrthographicCamera()
Definition: RayTracer.h:546
openvdb::v3_2_0::math::BBox
Axis-aligned bounding box.
Definition: BBox.h:47
openvdb::v3_2_0::tools::DiffuseShader::operator()
virtual Film::RGBA operator()(const Vec3R &xyz, const Vec3R &normal, const Vec3R &rayDir) const
Defines the interface of the virtual function that returns a RGB color.
Definition: RayTracer.h:738
openvdb::v3_2_0::math::Ray< double >
openvdb::v3_2_0::tools::BaseCamera::~BaseCamera
virtual ~BaseCamera()
Definition: RayTracer.h:411
openvdb::v3_2_0::tools::VolumeRender::setLightDir
void setLightDir(Real x, Real y, Real z)
Set the vector components of a directional light source.
Definition: RayTracer.h:208
openvdb::v3_2_0::tools::BaseShader::BaseShader
BaseShader()
Definition: RayTracer.h:567
openvdb::v3_2_0::math::operator+
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:494
openvdb::v3_2_0::tools::Film::RGBA::RGBA
RGBA()
Definition: RayTracer.h:264
openvdb::v3_2_0::math::Mat4< double >
openvdb::v3_2_0::Real
double Real
Definition: Types.h:64
openvdb::v3_2_0::math::Exp
Type Exp(const Type &x)
Return .
Definition: Math.h:676
openvdb::v3_2_0::tools::DiffuseShader< Film::RGBA, SamplerType >::DiffuseShader
DiffuseShader(const Film::RGBA &d=Film::RGBA(1.0f))
Definition: RayTracer.h:757
openvdb::v3_2_0::tools::LevelSetRayTracer::LevelSetRayTracer
LevelSetRayTracer(const GridT &grid, const BaseShader &shader, BaseCamera &camera, size_t pixelSamples=1, unsigned int seed=0)
Constructor based on an instance of the grid to be rendered.
Definition: RayTracer.h:813
openvdb::v3_2_0::tools::BaseShader::copy
virtual BaseShader * copy() const =0
openvdb::v3_2_0::tools::LevelSetRayTracer::setPixelSamples
void setPixelSamples(size_t pixelSamples, unsigned int seed=0)
Set the number of pixel samples and the seed for jittered sub-rays. A value larger than one implies a...
Definition: RayTracer.h:896
RayIntersector.h
Accelerated intersection of a ray with a narrow-band level set or a generic (e.g. density) volume...
openvdb::v3_2_0::tools::BaseCamera::rasterToScreen
Vec3R rasterToScreen(double i, double j, double z) const
Definition: RayTracer.h:434
openvdb::v3_2_0::tools::LevelSetRayTracer::render
void render(bool threaded=true) const
Perform the actual (potentially multithreaded) ray-tracing.
Definition: RayTracer.h:915
openvdb::v3_2_0::math::Ray::setEye
void setEye(const Vec3Type &eye)
Definition: Ray.h:88
openvdb::v3_2_0::math::Ray::dir
const Vec3T & dir() const
Definition: Ray.h:121
openvdb::v3_2_0::tools::PerspectiveCamera
Definition: RayTracer.h:461
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
Definition: version.h:43
openvdb::v3_2_0::tools::DiffuseShader::~DiffuseShader
virtual ~DiffuseShader()
Definition: RayTracer.h:737
openvdb::v3_2_0::tools::MatteShader::~MatteShader
virtual ~MatteShader()
Definition: RayTracer.h:590
openvdb::v3_2_0::tools::LevelSetRayTracer::setCamera
void setCamera(BaseCamera &camera)
Set the camera derived from the abstract BaseCamera class.
Definition: RayTracer.h:888
openvdb::v3_2_0::tools::PositionShader::copy
virtual BaseShader * copy() const
Definition: RayTracer.h:695
openvdb::v3_2_0::tools::LevelSetRayTracer
A (very) simple multithreaded ray tracer specifically for narrow-band level sets. ...
Definition: RayTracer.h:102
openvdb::v3_2_0::tools::Film::checkerboard
void checkerboard(const RGBA &c1=RGBA(0.3f), const RGBA &c2=RGBA(0.6f), size_t size=32)
Definition: RayTracer.h:313
openvdb::v3_2_0::tools::DiffuseShader::copy
virtual BaseShader * copy() const
Definition: RayTracer.h:746
openvdb::v3_2_0::tools::BaseCamera::mScaleWidth
double mScaleWidth
Definition: RayTracer.h:455
openvdb::v3_2_0::tools::DiffuseShader< Film::RGBA, SamplerType >::copy
virtual BaseShader * copy() const
Definition: RayTracer.h:772
openvdb::v3_2_0::tools::VolumeRender
A (very) simple multithreaded volume render specifically for scalar density.
Definition: RayTracer.h:179
openvdb::v3_2_0::tools::PositionShader
Color shader that treats position (x, y, z) as an RGB color in a cube defined from an axis-aligned bo...
Definition: RayTracer.h:677
openvdb::v3_2_0::tools::LevelSetRayTracer::setIntersector
void setIntersector(const IntersectorT &inter)
Set the intersector that performs the actual intersection of the rays against the narrow-band level s...
Definition: RayTracer.h:872
openvdb::v3_2_0::tools::NormalShader::NormalShader
NormalShader(const GridT &grid)
Definition: RayTracer.h:636
openvdb::v3_2_0::math::Transform
Calculate an axis-aligned bounding box in index space from a bounding sphere in world space...
Definition: Transform.h:66
openvdb::v3_2_0::tools::BaseShader
Abstract base class for the shaders.
Definition: RayTracer.h:563
openvdb::v3_2_0::tools::BaseCamera::mRay
math::Ray< double > mRay
Definition: RayTracer.h:456
openvdb::v3_2_0::tools::Film::fill
void fill(const RGBA &rgb=RGBA(0))
Definition: RayTracer.h:312
openvdb::v3_2_0::tools::VolumeRender::setCutOff
void setCutOff(Real cutOff)
Set the cut-off value for density and transmittance.
Definition: RayTracer.h:230
openvdb::v3_2_0::tools::BaseShader::~BaseShader
virtual ~BaseShader()
Definition: RayTracer.h:568
openvdb::v3_2_0::tools::PositionShader< Film::RGBA, SamplerType >::~PositionShader
virtual ~PositionShader()
Definition: RayTracer.h:709
openvdb::v3_2_0::tools::PerspectiveCamera::fieldOfViewToFocalLength
static double fieldOfViewToFocalLength(double fov, double aperture)
Return the focal length in mm given a horizontal field of view in degrees and the specified aperture ...
Definition: RayTracer.h:515
openvdb::v3_2_0::math::AffineMap
A general linear transform using homogeneous coordinates to perform rotation, scaling, shear and translation.
Definition: Maps.h:324
openvdb::v3_2_0::tools::VolumeRender::GridType
IntersectorT::GridType GridType
Definition: RayTracer.h:183
openvdb
Definition: Exceptions.h:39
openvdb::v3_2_0::tools::BaseCamera::mFilm
Film * mFilm
Definition: RayTracer.h:454
openvdb::v3_2_0::tools::MatteShader::MatteShader
MatteShader(const GridT &grid)
Definition: RayTracer.h:589
openvdb::v3_2_0::math::scale
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:594
openvdb::v3_2_0::tools::Film::RGBA::r
ValueT r
Definition: RayTracer.h:284
openvdb::v3_2_0::tools::OrthographicCamera::OrthographicCamera
OrthographicCamera(Film &film, const Vec3R &rotation=Vec3R(0.0), const Vec3R &translation=Vec3R(0.0), double frameWidth=1.0, double nearPlane=1e-3, double farPlane=std::numeric_limits< double >::max())
Constructor.
Definition: RayTracer.h:537
openvdb::v3_2_0::tools::Film::pixel
RGBA & pixel(size_t w, size_t h)
Definition: RayTracer.h:305
openvdb::v3_2_0::tools::LevelSetRayTracer::setGrid
void setGrid(const GridT &grid)
Set the level set grid to be ray-traced.
Definition: RayTracer.h:864
openvdb::v3_2_0::tools::LevelSetRayTracer::~LevelSetRayTracer
~LevelSetRayTracer()
Destructor.
Definition: RayTracer.h:857
openvdb::v3_2_0::tools::PerspectiveCamera::PerspectiveCamera
PerspectiveCamera(Film &film, const Vec3R &rotation=Vec3R(0.0), const Vec3R &translation=Vec3R(0.0), double focalLength=50.0, double aperture=41.2136, double nearPlane=1e-3, double farPlane=std::numeric_limits< double >::max())
Constructor.
Definition: RayTracer.h:479
openvdb::v3_2_0::tools::Film::height
size_t height() const
Definition: RayTracer.h:381
openvdb::v3_2_0::tools::VolumeRender::setShadowStep
void setShadowStep(Real shadowStep)
Set the integration step-size in voxel units for the primay ray.
Definition: RayTracer.h:217
openvdb::v3_2_0::tools::Film::RGBA::b
ValueT b
Definition: RayTracer.h:284
openvdb::v3_2_0::math::Vec3::normalize
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:348
openvdb::v3_2_0::tools::PositionShader::operator()
virtual Film::RGBA operator()(const Vec3R &xyz, const Vec3R &, const Vec3R &) const
Defines the interface of the virtual function that returns a RGB color.
Definition: RayTracer.h:688
openvdb::v3_2_0::tools::Film::RGBA::ValueT
float ValueT
Definition: RayTracer.h:262
openvdb::v3_2_0::math::Z_AXIS
Definition: Math.h:859
openvdb::v3_2_0::tools::OrthographicCamera::getRay
virtual math::Ray< double > getRay(size_t i, size_t j, double iOffset=0.5, double jOffset=0.5) const
Return a Ray in world space given the pixel indices and optional offsets in the range [0...
Definition: RayTracer.h:548
openvdb::v3_2_0::tools::MatteShader::operator()
virtual Film::RGBA operator()(const Vec3R &xyz, const Vec3R &, const Vec3R &) const
Defines the interface of the virtual function that returns a RGB color.
Definition: RayTracer.h:591
openvdb::v3_2_0::tools::LevelSetRayTracer::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Public method required by tbb::parallel_for.
Definition: RayTracer.h:923
openvdb::v3_2_0::tools::BaseCamera
Abstract base class for the perspective and orthographic cameras.
Definition: RayTracer.h:394
openvdb::v3_2_0::tools::BaseCamera::BaseCamera
BaseCamera(Film &film, const Vec3R &rotation, const Vec3R &translation, double frameWidth, double nearPlane, double farPlane)
Definition: RayTracer.h:397
openvdb::v3_2_0::tools::VolumeRender::setPrimaryStep
void setPrimaryStep(Real primaryStep)
Set the integration step-size in voxel units for the primay ray.
Definition: RayTracer.h:214
openvdb::v3_2_0::tools::VolumeRender::operator()
void operator()(const tbb::blocked_range< size_t > &range) const
Public method required by tbb::parallel_for.
Definition: RayTracer.h:1015
openvdb::v3_2_0::tools::MatteShader
Shader that produces a simple matte.
Definition: RayTracer.h:586
openvdb::v3_2_0::tools::LevelSetRayTracer::GridType
GridT GridType
Definition: RayTracer.h:105
openvdb::v3_2_0::tools::DiffuseShader< Film::RGBA, SamplerType >::~DiffuseShader
virtual ~DiffuseShader()
Definition: RayTracer.h:758
openvdb::v3_2_0::tools::Film::pixel
const RGBA & pixel(size_t w, size_t h) const
Definition: RayTracer.h:298
openvdb::v3_2_0::tools::NormalShader::operator()
virtual Film::RGBA operator()(const Vec3R &xyz, const Vec3R &normal, const Vec3R &) const
Defines the interface of the virtual function that returns a RGB color.
Definition: RayTracer.h:638
openvdb::v3_2_0::tools::VolumeRender::setLightGain
void setLightGain(Real gain)
Set parameter that imitates multi-scattering. A value of zero implies no multi-scattering.
Definition: RayTracer.h:227
Interpolation.h
openvdb::v3_2_0::tools::PositionShader::~PositionShader
virtual ~PositionShader()
Definition: RayTracer.h:687
openvdb::v3_2_0::tools::MatteShader< Film::RGBA, SamplerType >::MatteShader
MatteShader(const Film::RGBA &c=Film::RGBA(1.0f))
Definition: RayTracer.h:611
openvdb::v3_2_0::tools::LevelSetRayTracer::RayType
IntersectorT::RayType RayType
Definition: RayTracer.h:107
openvdb::v3_2_0::tools::Film::RGBA::g
ValueT g
Definition: RayTracer.h:284
openvdb::v3_2_0::tools::PositionShader< Film::RGBA, SamplerType >::PositionShader
PositionShader(const math::BBox< Vec3R > &bbox, const Film::RGBA &c=Film::RGBA(1.0f))
Definition: RayTracer.h:707
openvdb::v3_2_0::tools::Film::RGBA
Floating-point RGBA components in the range [0, 1].
Definition: RayTracer.h:260
openvdb::v3_2_0::tools::BaseCamera::lookAt
void lookAt(const Vec3R &xyz, const Vec3R &up=Vec3R(0.0, 1.0, 0.0))
Definition: RayTracer.h:422
openvdb::v3_2_0::ValueError
Definition: Exceptions.h:88
openvdb::v3_2_0::tools::Film::RGBA::operator+=
RGBA & operator+=(const RGBA &rhs)
Definition: RayTracer.h:273
openvdb::v3_2_0::tools::rayTrace
void rayTrace(const GridT &, const IntersectorT &, const BaseShader &, BaseCamera &, size_t pixelSamples=1, unsigned int seed=0, bool threaded=true)
Ray-trace a volume using a given ray intersector.
Definition: RayTracer.h:795
openvdb::v3_2_0::math::Vec3::lengthSqr
T lengthSqr() const
Definition: Vec3.h:223
openvdb::v3_2_0::tools::DiffuseShader
Simple diffuse Lambertian surface shader.
Definition: RayTracer.h:733
openvdb::v3_2_0::math::Ray::scaleTimes
void scaleTimes(RealT scale)
Definition: Ray.h:107
openvdb::v3_2_0::Vec3R
math::Vec3< Real > Vec3R
Definition: Types.h:76
openvdb::v3_2_0::math::Ray::setDir
void setDir(const Vec3Type &dir)
Definition: Ray.h:90
openvdb::v3_2_0::tools::DiffuseShader< Film::RGBA, SamplerType >
Definition: RayTracer.h:754
openvdb::v3_2_0::tools::MatteShader< Film::RGBA, SamplerType >::copy
virtual BaseShader * copy() const
Definition: RayTracer.h:617
openvdb::v3_2_0::tools::NormalShader< Film::RGBA, SamplerType >::~NormalShader
virtual ~NormalShader()
Definition: RayTracer.h:656
openvdb::v3_2_0::tools::VolumeRender::print
void print(std::ostream &os=std::cout, int verboseLevel=1)
Print parameters, statistics, memory usage and other information.
Definition: RayTracer.h:982
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
openvdb::v3_2_0::tools::Film::width
size_t width() const
Definition: RayTracer.h:380
openvdb::v3_2_0::tools::VolumeRender::setAbsorption
void setAbsorption(Real x, Real y, Real z)
Set absorption coefficients.
Definition: RayTracer.h:223
openvdb::v3_2_0::tools::Film::RGBA::a
ValueT a
Definition: RayTracer.h:284
openvdb::v3_2_0::tools::PositionShader< Film::RGBA, SamplerType >::copy
virtual BaseShader * copy() const
Definition: RayTracer.h:715
Ray.h
A Ray class.
openvdb::v3_2_0::tools::PerspectiveCamera::focalLengthToFieldOfView
static double focalLengthToFieldOfView(double length, double aperture)
Return the horizontal field of view in degrees given a focal lenth in mm and the specified aperture i...
Definition: RayTracer.h:509
openvdb::v3_2_0::tools::GridSampler
Class that provides the interface for continuous sampling of values in a tree.
Definition: Interpolation.h:310
openvdb::v3_2_0::tools::PointSampler
Definition: Interpolation.h:123
openvdb::v3_2_0::tools::NormalShader< Film::RGBA, SamplerType >::NormalShader
NormalShader(const Film::RGBA &c=Film::RGBA(1.0f))
Definition: RayTracer.h:655
openvdb::v3_2_0::math::Mat4::postTranslate
void postTranslate(const Vec3< T0 > &tr)
Right multiplies by the specified translation matrix, i.e. (*this) * Trans.
Definition: Mat4.h:756
openvdb::v3_2_0::tools::VolumeRender::setLightColor
void setLightColor(Real r, Real g, Real b)
Set the color of the directional light source.
Definition: RayTracer.h:211
openvdb::v3_2_0::tools::BaseCamera::height
size_t height() const
Definition: RayTracer.h:416
openvdb::v3_2_0::tools::PositionShader< Film::RGBA, SamplerType >::operator()
virtual Film::RGBA operator()(const Vec3R &xyz, const Vec3R &, const Vec3R &) const
Defines the interface of the virtual function that returns a RGB color.
Definition: RayTracer.h:710
openvdb::v3_2_0::tools::NormalShader::~NormalShader
virtual ~NormalShader()
Definition: RayTracer.h:637
openvdb::v3_2_0::tools::Film::Film
Film(size_t width, size_t height)
Definition: RayTracer.h:288
openvdb::v3_2_0::tools::NormalShader< Film::RGBA, SamplerType >::copy
virtual BaseShader * copy() const
Definition: RayTracer.h:661
openvdb::v3_2_0::math::unit
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:627
openvdb::v3_2_0::tools::MatteShader::copy
virtual BaseShader * copy() const
Definition: RayTracer.h:600
openvdb::v3_2_0::tools::composite::min
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:128