OpenVDB  2.1.0
RayTracer.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 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 //
43 
44 #ifndef OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
45 #define OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
46 
47 #include <openvdb/Types.h>
48 #include <openvdb/math/BBox.h>
49 #include <openvdb/math/Ray.h>
50 #include <openvdb/math/Math.h>
51 #include <openvdb/tools/RayIntersector.h>
52 #include <boost/scoped_ptr.hpp>
53 #include <vector>
54 
55 #ifdef OPENVDB_TOOLS_RAYTRACER_USE_EXR
56 #include <OpenEXR/ImfPixelType.h>
57 #include <OpenEXR/ImfChannelList.h>
58 #include <OpenEXR/ImfOutputFile.h>
59 #include <OpenEXR/ImfHeader.h>
60 #include <OpenEXR/ImfFrameBuffer.h>
61 #endif
62 
63 namespace openvdb {
65 namespace OPENVDB_VERSION_NAME {
66 namespace tools {
67 
68 // Forward declarations
69 class BaseCamera;
70 class BaseShader;
71 
73 template<typename GridT>
74 inline void rayTrace(const GridT&,
75  const BaseShader&,
76  BaseCamera&,
77  size_t pixelSamples = 1,
78  unsigned int seed = 0,
79  bool threaded = true);
80 
82 template<typename GridT, typename IntersectorT>
83 inline void rayTrace(const GridT&,
84  const IntersectorT&,
85  const BaseShader&,
86  BaseCamera&,
87  size_t pixelSamples = 1,
88  unsigned int seed = 0,
89  bool threaded = true);
90 
91 
93 
96 template<typename GridT, typename IntersectorT = tools::LevelSetRayIntersector<GridT> >
98 {
99 public:
100  typedef GridT GridType;
101  typedef typename IntersectorT::Vec3Type Vec3Type;
102  typedef typename IntersectorT::RayType RayType;
103 
105  LevelSetRayTracer(const GridT& grid,
106  const BaseShader& shader,
107  BaseCamera& camera,
108  size_t pixelSamples = 1,
109  unsigned int seed = 0);
110 
113  LevelSetRayTracer(const IntersectorT& inter,
114  const BaseShader& shader,
115  BaseCamera& camera,
116  size_t pixelSamples = 1,
117  unsigned int seed = 0);
118 
120  LevelSetRayTracer(const LevelSetRayTracer& other);
121 
124 
126  void setGrid(const GridT& grid);
127 
130  void setIntersector(const IntersectorT& inter);
131 
139  void setShader(const BaseShader& shader);
140 
142  void setCamera(BaseCamera& camera);
143 
148  void setPixelSamples(size_t pixelSamples, unsigned int seed = 0);
149 
151  void render(bool threaded = true);
152  OPENVDB_DEPRECATED void trace(bool threaded = true) { this->render(threaded); }
153 
156  void operator()(const tbb::blocked_range<size_t>& range) const;
157 
158 private:
159  const bool mIsMaster;
160  double* mRand;
161  IntersectorT mInter;
162  boost::scoped_ptr<const BaseShader> mShader;
163  BaseCamera* mCamera;
164  size_t mSubPixels;
165 };// LevelSetRayTracer
166 
167 
169 
172 class Film
173 {
174 public:
177  struct RGBA
178  {
179  typedef float ValueT;
180 
181  RGBA() : r(0), g(0), b(0), a(1) {}
182  explicit RGBA(ValueT intensity) : r(intensity), g(intensity), b(intensity), a(1) {}
183  RGBA(ValueT _r, ValueT _g, ValueT _b, ValueT _a=1.0f) : r(_r), g(_g), b(_b), a(_a) {}
184 
185  RGBA operator* (ValueT scale) const { return RGBA(r*scale, g*scale, b*scale);}
186  RGBA operator+ (const RGBA& rhs) const { return RGBA(r+rhs.r, g+rhs.g, b+rhs.b);}
187  RGBA operator* (const RGBA& rhs) const { return RGBA(r*rhs.r, g*rhs.g, b*rhs.b);}
188  RGBA& operator+=(const RGBA& rhs) { r+=rhs.r; g+=rhs.g; b+=rhs.b, a+=rhs.a; return *this;}
189 
190  void over(const RGBA& rhs)
191  {
192  const float s = rhs.a*(1.0f-a);
193  r = a*r+s*rhs.r;
194  g = a*g+s*rhs.g;
195  b = a*b+s*rhs.b;
196  a = a + s;
197  }
198 
199  ValueT r, g, b, a;
200  };
201 
202 
203  Film(size_t width, size_t height)
204  : mWidth(width), mHeight(height), mSize(width*height), mPixels(new RGBA[mSize])
205  {
206  }
207  Film(size_t width, size_t height, const RGBA& bg)
208  : mWidth(width), mHeight(height), mSize(width*height), mPixels(new RGBA[mSize])
209  {
210  this->fill(bg);
211  }
212  ~Film() { delete mPixels; }
213 
214  const RGBA& pixel(size_t w, size_t h) const
215  {
216  assert(w < mWidth);
217  assert(h < mHeight);
218  return mPixels[w + h*mWidth];
219  }
220 
221  RGBA& pixel(size_t w, size_t h)
222  {
223  assert(w < mWidth);
224  assert(h < mHeight);
225  return mPixels[w + h*mWidth];
226  }
227 
228  void fill(const RGBA& rgb=RGBA(0)) { for (size_t i=0; i<mSize; ++i) mPixels[i] = rgb; }
229  void checkerboard(const RGBA& c1=RGBA(0.3f), const RGBA& c2=RGBA(0.6f), size_t size=32)
230  {
231  RGBA *p = mPixels;
232  for (size_t j = 0; j < mHeight; ++j) {
233  for (size_t i = 0; i < mWidth; ++i, ++p) {
234  *p = ((i & size) ^ (j & size)) ? c1 : c2;
235  }
236  }
237  }
238 
239  void savePPM(const std::string& fileName)
240  {
241  std::string name(fileName + ".ppm");
242  unsigned char* tmp = new unsigned char[3*mSize], *q = tmp;
243  RGBA* p = mPixels;
244  size_t n = mSize;
245  while (n--) {
246  *q++ = static_cast<unsigned char>(255.0f*(*p ).r);
247  *q++ = static_cast<unsigned char>(255.0f*(*p ).g);
248  *q++ = static_cast<unsigned char>(255.0f*(*p++).b);
249  }
250 
251  std::ofstream os(name.c_str(), std::ios_base::binary);
252  if (!os.is_open()) {
253  std::cerr << "Error opening PPM file \"" << name << "\"" << std::endl;
254  return;
255  }
256 
257  os << "P6\n" << mWidth << " " << mHeight << "\n255\n";
258  os.write((const char *)&(*tmp), 3*mSize*sizeof(unsigned char));
259  delete [] tmp;
260  }
261 
262 #ifdef OPENVDB_TOOLS_RAYTRACER_USE_EXR
263  void saveEXR(const std::string& fileName, size_t compression = 2, size_t threads = 8)
264  {
265  std::string name(fileName + ".exr");
266 
267  if (threads>0) Imf::setGlobalThreadCount(threads);
268  Imf::Header header(mWidth, mHeight);
269  if (compression==0) header.compression() = Imf::NO_COMPRESSION;
270  if (compression==1) header.compression() = Imf::RLE_COMPRESSION;
271  if (compression>=2) header.compression() = Imf::ZIP_COMPRESSION;
272  header.channels().insert("R", Imf::Channel(Imf::FLOAT));
273  header.channels().insert("G", Imf::Channel(Imf::FLOAT));
274  header.channels().insert("B", Imf::Channel(Imf::FLOAT));
275  header.channels().insert("A", Imf::Channel(Imf::FLOAT));
276 
277  Imf::FrameBuffer framebuffer;
278  framebuffer.insert("R", Imf::Slice( Imf::FLOAT, (char *) &(mPixels[0].r),
279  sizeof (RGBA), sizeof (RGBA) * mWidth));
280  framebuffer.insert("G", Imf::Slice( Imf::FLOAT, (char *) &(mPixels[0].g),
281  sizeof (RGBA), sizeof (RGBA) * mWidth));
282  framebuffer.insert("B", Imf::Slice( Imf::FLOAT, (char *) &(mPixels[0].b),
283  sizeof (RGBA), sizeof (RGBA) * mWidth));
284  framebuffer.insert("A", Imf::Slice( Imf::FLOAT, (char *) &(mPixels[0].a),
285  sizeof (RGBA), sizeof (RGBA) * mWidth));
286 
287  Imf::OutputFile file(name.c_str(), header);
288  file.setFrameBuffer(framebuffer);
289  file.writePixels(mHeight);
290  }
291 #endif
292 
293  size_t width() const { return mWidth; }
294  size_t height() const { return mHeight; }
295  size_t numPixels() const { return mSize; }
296  const RGBA* pixels() const { return mPixels; }
297 
298 private:
299  size_t mWidth, mHeight, mSize;
300  RGBA* mPixels;
301 };// Film
302 
303 
305 
308 {
309 public:
310  BaseCamera(Film& film, const Vec3R& rotation, const Vec3R& translation,
311  double frameWidth, double nearPlane, double farPlane)
312  : mFilm(&film)
313  , mScaleWidth(frameWidth)
314  , mScaleHeight(frameWidth*film.height()/double(film.width()))
315  {
316  assert(nearPlane > 0 && farPlane > nearPlane);
317  mScreenToWorld.accumPostRotation(math::X_AXIS, rotation[0] * M_PI / 180.0);
318  mScreenToWorld.accumPostRotation(math::Y_AXIS, rotation[1] * M_PI / 180.0);
319  mScreenToWorld.accumPostRotation(math::Z_AXIS, rotation[2] * M_PI / 180.0);
320  mScreenToWorld.accumPostTranslation(translation);
321  this->initRay(nearPlane, farPlane);
322  }
323 
324  virtual ~BaseCamera() {}
325 
326  Film::RGBA& pixel(size_t i, size_t j) { return mFilm->pixel(i, j); }
327 
328  size_t width() const { return mFilm->width(); }
329  size_t height() const { return mFilm->height(); }
330 
335  void lookAt(const Vec3R& xyz, const Vec3R& up = Vec3R(0.0, 1.0, 0.0))
336  {
337  const Vec3R orig = mScreenToWorld.applyMap(Vec3R(0.0));
338  const Vec3R dir = orig - xyz;
339  try {
340  Mat4d xform = math::aim<Mat4d>(dir, up);
341  xform.postTranslate(orig);
342  mScreenToWorld = math::AffineMap(xform);
343  this->initRay(mRay.t0(), mRay.t1());
344  } catch (...) {}
345  }
346 
347  Vec3R rasterToScreen(double i, double j, double z) const
348  {
349  return Vec3R( (2 * i / mFilm->width() - 1) * mScaleWidth,
350  (1 - 2 * j / mFilm->height()) * mScaleHeight, z );
351  }
352 
356  virtual math::Ray<double> getRay(
357  size_t i, size_t j, double iOffset = 0.5, double jOffset = 0.5) const = 0;
358 
359 protected:
360  void initRay(double t0, double t1)
361  {
362  mRay.setTimes(t0, t1);
363  mRay.setEye(mScreenToWorld.applyMap(Vec3R(0.0)));
364  mRay.setDir(mScreenToWorld.applyJacobian(Vec3R(0.0, 0.0, -1.0)));
365  }
366 
368  double mScaleWidth, mScaleHeight;
371 };// BaseCamera
372 
373 
375 {
376  public:
393  const Vec3R& rotation = Vec3R(0.0),
394  const Vec3R& translation = Vec3R(0.0),
395  double focalLength = 50.0,
396  double aperture = 41.2136,
397  double nearPlane = 1e-3,
398  double farPlane = std::numeric_limits<double>::max())
399  : BaseCamera(film, rotation, translation, 0.5*aperture/focalLength, nearPlane, farPlane)
400  {
401  }
402 
403  virtual ~PerspectiveCamera() {}
404 
409  size_t i, size_t j, double iOffset = 0.5, double jOffset = 0.5) const
410  {
411  math::Ray<double> ray(mRay);
412  Vec3R dir = BaseCamera::rasterToScreen(i + iOffset, j + jOffset, -1.0);
413  dir = BaseCamera::mScreenToWorld.applyJacobian(dir);
414  dir.normalize();
415  ray.scaleTimes(1.0/dir.dot(ray.dir()));
416  ray.setDir(dir);
417  return ray;
418  }
419 
422  static double focalLengthToFieldOfView(double length, double aperture)
423  {
424  return 360.0 / M_PI * atan(aperture/(2.0*length));
425  }
428  static double fieldOfViewToFocalLength(double fov, double aperture)
429  {
430  return aperture/(2.0*(tan(fov * M_PI / 360.0)));
431  }
432 };// PerspectiveCamera
433 
434 
436 {
437 public:
451  const Vec3R& rotation = Vec3R(0.0),
452  const Vec3R& translation = Vec3R(0.0),
453  double frameWidth = 1.0,
454  double nearPlane = 1e-3,
455  double farPlane = std::numeric_limits<double>::max())
456  : BaseCamera(film, rotation, translation, 0.5*frameWidth, nearPlane, farPlane)
457  {
458  }
459  virtual ~OrthographicCamera() {}
460 
462  size_t i, size_t j, double iOffset = 0.5, double jOffset = 0.5) const
463  {
464  math::Ray<double> ray(mRay);
465  Vec3R eye = BaseCamera::rasterToScreen(i + iOffset, j + jOffset, 0.0);
466  ray.setEye(BaseCamera::mScreenToWorld.applyMap(eye));
467  return ray;
468  }
469 };// OrthographicCamera
470 
471 
473 
474 
477 {
478 public:
481  virtual ~BaseShader() {}
486  virtual Film::RGBA operator()(const Vec3R& xyz, const Vec3R& nml, const Vec3R& dir) const = 0;
487 
489  OPENVDB_DEPRECATED Film::RGBA operator()(const Vec3R& xyz, const Vec3R& nml, const RayT& ray) const
490  {
491  return (*this)(xyz, nml, ray.dir());
492  }
493  virtual BaseShader* copy() const = 0;
494 };
495 
496 
498 class MatteShader: public BaseShader
499 {
500 public:
501  MatteShader(const Film::RGBA& c = Film::RGBA(1.0f)): mRGBA(c) {}
502  virtual ~MatteShader() {}
503  virtual Film::RGBA operator()(const Vec3R&, const Vec3R&, const Vec3R&) const
504  {
505  return mRGBA;
506  }
507  virtual BaseShader* copy() const { return new MatteShader(*this); }
508 
509 private:
510  const Film::RGBA mRGBA;
511 };
512 
513 
516 {
517 public:
518  NormalShader(const Film::RGBA& c = Film::RGBA(1.0f)) : mRGBA(c*0.5f) {}
519  virtual ~NormalShader() {}
520  virtual Film::RGBA operator()(const Vec3R&, const Vec3R& normal, const Vec3R&) const
521  {
522  return mRGBA*Film::RGBA(normal[0]+1.0f, normal[1]+1.0f, normal[2]+1.0f);
523  }
524  virtual BaseShader* copy() const { return new NormalShader(*this); }
525 
526 private:
527  const Film::RGBA mRGBA;
528 };
529 
530 
534 {
535 public:
536  PositionShader(const math::BBox<Vec3R>& bbox, const Film::RGBA& c = Film::RGBA(1.0f))
537  : mMin(bbox.min()), mInvDim(1.0/bbox.extents()), mRGBA(c) {}
538  virtual ~PositionShader() {}
539  virtual Film::RGBA operator()(const Vec3R& xyz, const Vec3R&, const Vec3R&) const
540  {
541  const Vec3R rgb = (xyz - mMin)*mInvDim;
542  return mRGBA*Film::RGBA(rgb[0], rgb[1], rgb[2]);
543  }
544  virtual BaseShader* copy() const { return new PositionShader(*this); }
545 
546  private:
547  const Vec3R mMin, mInvDim;
548  const Film::RGBA mRGBA;
549 };
550 
557 {
558 public:
559  DiffuseShader(const Film::RGBA& d = Film::RGBA(1.0f)): mRGBA(d) {}
560  virtual Film::RGBA operator()(const Vec3R&, const Vec3R& normal, const Vec3R& rayDir) const
561  {
562  // We assume a single directional light source at the camera,
563  // so the cosine of the angle between the surface normal and the
564  // direction of the light source becomes the dot product of the
565  // surface normal and inverse direction of the ray. We also ignore
566  // negative dot products, corresponding to strict one-sided shading.
567  //return mRGBA * math::Max(0.0, normal.dot(-rayDir));
568 
569  // We take the abs of the dot product corresponding to having
570  // light sources at +/- rayDir, i.e., two-sided shading.
571  return mRGBA * math::Abs(normal.dot(rayDir));
572  }
573  virtual BaseShader* copy() const { return new DiffuseShader(*this); }
574 
575 private:
576  const Film::RGBA mRGBA;
577 };
578 
579 
581 
582 template<typename GridT>
583 inline void rayTrace(const GridT& grid,
584  const BaseShader& shader,
585  BaseCamera& camera,
586  size_t pixelSamples,
587  unsigned int seed,
588  bool threaded)
589 {
591  tracer(grid, shader, camera, pixelSamples, seed);
592  tracer.render(threaded);
593 }
594 
595 
596 template<typename GridT, typename IntersectorT>
597 inline void rayTrace(const GridT&,
598  const IntersectorT& inter,
599  const BaseShader& shader,
600  BaseCamera& camera,
601  size_t pixelSamples,
602  unsigned int seed,
603  bool threaded)
604 {
605  LevelSetRayTracer<GridT, IntersectorT> tracer(inter, shader, camera, pixelSamples, seed);
606  tracer.render(threaded);
607 }
608 
609 
611 
612 
613 template<typename GridT, typename IntersectorT>
614 inline LevelSetRayTracer<GridT, IntersectorT>::
615 LevelSetRayTracer(const GridT& grid,
616  const BaseShader& shader,
617  BaseCamera& camera,
618  size_t pixelSamples,
619  unsigned int seed)
620  : mIsMaster(true),
621  mRand(NULL),
622  mInter(grid),
623  mShader(shader.copy()),
624  mCamera(&camera)
625 {
626  this->setPixelSamples(pixelSamples, seed);
627 }
628 
629 template<typename GridT, typename IntersectorT>
631 LevelSetRayTracer(const IntersectorT& inter,
632  const BaseShader& shader,
633  BaseCamera& camera,
634  size_t pixelSamples,
635  unsigned int seed)
636  : mIsMaster(true),
637  mRand(NULL),
638  mInter(inter),
639  mShader(shader.copy()),
640  mCamera(&camera)
641 {
642  this->setPixelSamples(pixelSamples, seed);
643 }
644 
645 template<typename GridT, typename IntersectorT>
648  mIsMaster(false),
649  mRand(other.mRand),
650  mInter(other.mInter),
651  mShader(other.mShader->copy()),
652  mCamera(other.mCamera),
653  mSubPixels(other.mSubPixels)
654 {
655 }
656 
657 template<typename GridT, typename IntersectorT>
660 {
661  if (mIsMaster) delete [] mRand;
662 }
663 
664 template<typename GridT, typename IntersectorT>
666 setGrid(const GridT& grid)
667 {
668  assert(mIsMaster);
669  mInter = IntersectorT(grid);
670 }
671 
672 template<typename GridT, typename IntersectorT>
674 setIntersector(const IntersectorT& inter)
675 {
676  assert(mIsMaster);
677  mInter = inter;
678 }
679 
680 template<typename GridT, typename IntersectorT>
682 setShader(const BaseShader& shader)
683 {
684  assert(mIsMaster);
685  mShader.reset(shader.copy());
686 }
687 
688 template<typename GridT, typename IntersectorT>
691 {
692  assert(mIsMaster);
693  mCamera = &camera;
694 }
695 
696 template<typename GridT, typename IntersectorT>
698 setPixelSamples(size_t pixelSamples, unsigned int seed)
699 {
700  assert(mIsMaster);
701  if (pixelSamples == 0) {
702  OPENVDB_THROW(ValueError, "pixelSamples must be larger then zero!");
703  }
704  mSubPixels = pixelSamples - 1;
705  delete [] mRand;
706  if (mSubPixels > 0) {
707  mRand = new double[16];
708  math::Rand01<double> rand(seed);//offsets for anti-aliaing by jittered super-sampling
709  for (size_t i=0; i<16; ++i) mRand[i] = rand();
710  } else {
711  mRand = NULL;
712  }
713 }
714 
715 template<typename GridT, typename IntersectorT>
717 render(bool threaded)
718 {
719  tbb::blocked_range<size_t> range(0, mCamera->height());
720  threaded ? tbb::parallel_for(range, *this) : (*this)(range);
721 }
722 
723 template<typename GridT, typename IntersectorT>
725 operator()(const tbb::blocked_range<size_t>& range) const
726 {
727  const BaseShader& shader = *mShader;
728  Vec3Type xyz, nml;
729  const float frac = 1.0f / (1.0f + mSubPixels);
730  for (size_t j=range.begin(), n=0, je = range.end(); j<je; ++j) {
731  for (size_t i=0, ie = mCamera->width(); i<ie; ++i) {
732  Film::RGBA& bg = mCamera->pixel(i,j);
733  RayType ray = mCamera->getRay(i, j);//primary ray
734  Film::RGBA c = mInter.intersectsWS(ray, xyz, nml) ? shader(xyz, nml, ray.dir()) : bg;
735  for (size_t k=0; k<mSubPixels; ++k, n +=2 ) {
736  ray = mCamera->getRay(i, j, mRand[n & 15], mRand[(n+1) & 15]);
737  c += mInter.intersectsWS(ray, xyz, nml) ? shader(xyz, nml, ray.dir()) : bg;
738  }//loop over sub-pixels
739  bg = c*frac;
740  }//loop over image height
741  }//loop over image width
742 }
743 
744 } // namespace tools
745 } // namespace OPENVDB_VERSION_NAME
746 } // namespace openvdb
747 
748 #endif // OPENVDB_TOOLS_RAYTRACER_HAS_BEEN_INCLUDED
749 
750 // Copyright (c) 2012-2013 DreamWorks Animation LLC
751 // All rights reserved. This software is distributed under the
752 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
const RGBA * pixels() const
Definition: RayTracer.h:296
BaseShader()
Definition: RayTracer.h:480
~LevelSetRayTracer()
Destructor.
Definition: RayTracer.h:659
const Vec3T & dir() const
Definition: Ray.h:95
float ValueT
Definition: RayTracer.h:179
A simple class that allows for concurrent writes to pixels in an image, background initialization of ...
Definition: RayTracer.h:172
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:560
ValueT a
Definition: RayTracer.h:199
size_t height() const
Definition: RayTracer.h:294
#define OPENVDB_DEPRECATED
Definition: Platform.h:47
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
~Film()
Definition: RayTracer.h:212
void checkerboard(const RGBA &c1=RGBA(0.3f), const RGBA &c2=RGBA(0.6f), size_t size=32)
Definition: RayTracer.h:229
Simple diffuse Lambertian surface shader.
Definition: RayTracer.h:556
virtual ~BaseCamera()
Definition: RayTracer.h:324
Definition: Math.h:771
ValueT g
Definition: RayTracer.h:199
MatteShader(const Film::RGBA &c=Film::RGBA(1.0f))
Definition: RayTracer.h:501
Abstract base class for the shaders.
Definition: RayTracer.h:476
RGBA(ValueT intensity)
Definition: RayTracer.h:182
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:328
void over(const RGBA &rhs)
Definition: RayTracer.h:190
math::Vec3< Real > Vec3R
Definition: Types.h:74
OPENVDB_DEPRECATED Film::RGBA operator()(const Vec3R &xyz, const Vec3R &nml, const RayT &ray) const
Deprecated, use the method above instead.
Definition: RayTracer.h:489
Film * mFilm
Definition: RayTracer.h:367
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 then one implies a...
Definition: RayTracer.h:698
Abstract base class for the perspective and orthographic cameras.
Definition: RayTracer.h:307
const RGBA & pixel(size_t w, size_t h) const
Definition: RayTracer.h:214
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:539
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
virtual BaseShader * copy() const =0
RGBA()
Definition: RayTracer.h:181
void fill(const RGBA &rgb=RGBA(0))
Definition: RayTracer.h:228
Definition: Exceptions.h:88
A (very) simple multithreaded ray tracer specifically for narrow-band level sets. ...
Definition: RayTracer.h:97
void scaleTimes(RealT scale)
Definition: Ray.h:83
int32_t Abs(int32_t i)
Return the absolute value of the given quantity.
Definition: Math.h:253
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:461
Definition: RayTracer.h:533
MatType rotation(const Quat< typename MatType::value_type > &q, typename MatType::value_type eps=1.0e-8)
Definition: Mat.h:157
math::Ray< Real > RayT
Definition: RayTracer.h:479
virtual BaseShader * copy() const
Definition: RayTracer.h:524
IntersectorT::Vec3Type Vec3Type
Definition: RayTracer.h:101
MatType scale(const Vec3< typename MatType::value_type > &scaling)
Definition: Mat.h:595
A general linear transform using homogeneous coordinates to perform rotation, scaling, shear and translation.
Definition: Maps.h:323
double mScaleWidth
Definition: RayTracer.h:368
math::Ray< double > mRay
Definition: RayTracer.h:369
NormalShader(const Film::RGBA &c=Film::RGBA(1.0f))
Definition: RayTracer.h:518
RGBA & pixel(size_t w, size_t h)
Definition: RayTracer.h:221
void setEye(const Vec3Type &eye)
Definition: Ray.h:69
void rayTrace(const GridT &, const BaseShader &, BaseCamera &, size_t pixelSamples=1, unsigned int seed=0, bool threaded=true)
Ray-trace a volume.
Definition: RayTracer.h:583
ValueT r
Definition: RayTracer.h:199
virtual ~BaseShader()
Definition: RayTracer.h:481
#define OPENVDB_VERSION_NAME
Definition: version.h:45
GridT GridType
Definition: RayTracer.h:100
Simple generator of random numbers over the range [0, 1)
Definition: Math.h:143
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:450
Floating-point RGBA components in the range [0, 1].
Definition: RayTracer.h:177
virtual ~OrthographicCamera()
Definition: RayTracer.h:459
Film(size_t width, size_t height, const RGBA &bg)
Definition: RayTracer.h:207
virtual BaseShader * copy() const
Definition: RayTracer.h:544
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:674
void postTranslate(const Vec3< T0 > &tr)
Right multiplies by the specified translation matrix, i.e. (*this) * Trans.
Definition: Mat4.h:728
void operator()(const tbb::blocked_range< size_t > &range) const
Public method required by tbb::parallel_for.
Definition: RayTracer.h:725
size_t width() const
Definition: RayTracer.h:293
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:392
void setDir(const Vec3Type &dir)
Definition: Ray.h:71
void initRay(double t0, double t1)
Definition: RayTracer.h:360
void render(bool threaded=true)
Perform the actual (potentially multithreaded) ray-tracing.
Definition: RayTracer.h:717
virtual ~PositionShader()
Definition: RayTracer.h:538
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
Definition: Math.h:770
virtual BaseShader * copy() const
Definition: RayTracer.h:573
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Matrix multiplication.
Definition: Mat3.h:608
IntersectorT::RayType RayType
Definition: RayTracer.h:102
BaseCamera(Film &film, const Vec3R &rotation, const Vec3R &translation, double frameWidth, double nearPlane, double farPlane)
Definition: RayTracer.h:310
ValueT b
Definition: RayTracer.h:199
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:503
virtual ~MatteShader()
Definition: RayTracer.h:502
Vec3R rasterToScreen(double i, double j, double z) const
Definition: RayTracer.h:347
RGBA & operator+=(const RGBA &rhs)
Definition: RayTracer.h:188
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:615
Film::RGBA & pixel(size_t i, size_t j)
Definition: RayTracer.h:326
DiffuseShader(const Film::RGBA &d=Film::RGBA(1.0f))
Definition: RayTracer.h:559
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:408
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:428
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:422
virtual BaseShader * copy() const
Definition: RayTracer.h:507
void setCamera(BaseCamera &camera)
Set the camera derived from the abstract BaseCamera class.
Definition: RayTracer.h:690
Axis-aligned bounding box.
Definition: BBox.h:47
Shader that produces a simple matte.
Definition: RayTracer.h:498
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:382
OPENVDB_DEPRECATED void trace(bool threaded=true)
Definition: RayTracer.h:152
size_t height() const
Definition: RayTracer.h:329
Film(size_t width, size_t height)
Definition: RayTracer.h:203
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
void setShader(const BaseShader &shader)
Set the shader derived from the abstract BaseShader class.
Definition: RayTracer.h:682
Definition: Math.h:772
RGBA(ValueT _r, ValueT _g, ValueT _b, ValueT _a=1.0f)
Definition: RayTracer.h:183
math::AffineMap mScreenToWorld
Definition: RayTracer.h:370
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:520
void setGrid(const GridT &grid)
Set the level set grid to be ray-traced.
Definition: RayTracer.h:666
size_t width() const
Definition: RayTracer.h:328
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:199
void lookAt(const Vec3R &xyz, const Vec3R &up=Vec3R(0.0, 1.0, 0.0))
Definition: RayTracer.h:335
virtual ~NormalShader()
Definition: RayTracer.h:519
size_t numPixels() const
Definition: RayTracer.h:295
void savePPM(const std::string &fileName)
Definition: RayTracer.h:239
virtual ~PerspectiveCamera()
Definition: RayTracer.h:403
Color shader that treats the surface normal (x, y, z) as an RGB color.
Definition: RayTracer.h:515