Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
util.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_UTIL_H_)
21 #define __MITSUBA_CORE_UTIL_H_
22 
23 #include <boost/static_assert.hpp>
24 
26 
27 /*! \addtogroup libcore
28  * @{
29  */
30 
31 // -----------------------------------------------------------------------
32 //! @{ \name String-related utility functions
33 // -----------------------------------------------------------------------
34 
35 /**
36  * \brief Given a list of delimiters, tokenize
37  * a std::string into a vector of strings
38  */
39 extern MTS_EXPORT_CORE std::vector<std::string> tokenize(
40  const std::string &string,
41  const std::string &delim
42 );
43 
44 /// Trim spaces (' ', '\\n', '\\r', '\\t') from the ends of a string
45 extern MTS_EXPORT_CORE std::string trim(const std::string& str);
46 
47 /// Indent a string (Used for recursive toString() structure dumping)
48 extern MTS_EXPORT_CORE std::string indent(const std::string &string, int amount=1);
49 
50 /// Wrapped snprintf
51 extern MTS_EXPORT_CORE std::string formatString(const char *pFmt, ...);
52 
53 /**
54  * \brief Convert a time difference (in seconds) to a string representation
55  * \param time Time difference in (fractional) sections
56  * \param precise When set to true, a higher-precision string representation
57  * is generated.
58  */
59 extern MTS_EXPORT_CORE std::string timeString(Float time, bool precise = false);
60 
61 /// Turn a memory size into a human-readable string
62 extern MTS_EXPORT_CORE std::string memString(size_t size, bool precise = false);
63 
64 /// Return a string representation of a list of objects
65 template<class Iterator> std::string containerToString(const Iterator &start, const Iterator &end) {
66  std::ostringstream oss;
67  oss << "{" << std::endl;
68  Iterator it = start;
69  while (it != end) {
70  oss << " " << indent((*it)->toString());
71  ++it;
72  if (it != end)
73  oss << "," << std::endl;
74  else
75  oss << std::endl;
76  }
77  oss << "}";
78  return oss.str();
79 }
80 
81 /// Simple functor for sorting string parameters by length and content
83  bool operator()(const std::string &a, const std::string &b) const {
84  if (a.length() == b.length())
85  return a < b;
86  return a.length() < b.length();
87  }
88 };
89 
90 //! @}
91 // -----------------------------------------------------------------------
92 
93 // -----------------------------------------------------------------------
94 //! @{ \name Miscellaneous
95 // -----------------------------------------------------------------------
96 
97 /// Allocate an aligned region of memory
98 extern MTS_EXPORT_CORE void * __restrict allocAligned(size_t size);
99 
100 /// Free an aligned region of memory
101 extern MTS_EXPORT_CORE void freeAligned(void *ptr);
102 
103 #if defined(WIN32)
104 /// Return a string version of GetLastError()
105 extern std::string MTS_EXPORT_CORE lastErrorText();
106 #endif
107 
108 /// Determine the number of available CPU cores
109 extern MTS_EXPORT_CORE int getCoreCount();
110 
111 /// Return the host name of this machine
112 extern MTS_EXPORT_CORE std::string getHostName();
113 
114 /// Return the process private memory usage in bytes
115 extern MTS_EXPORT_CORE size_t getPrivateMemoryUsage();
116 
117 /// Returns the total amount of memory available to the OS
118 extern MTS_EXPORT_CORE size_t getTotalSystemMemory();
119 
120 /// Return the fully qualified domain name of this machine
121 extern MTS_EXPORT_CORE std::string getFQDN();
122 
123 /**
124  * \brief Enable floating point exceptions (to catch NaNs, overflows,
125  * arithmetic with infinity).
126  *
127  * On Intel processors, this applies to both x87 and SSE2 math
128  *
129  * \return \c true if floating point exceptions were active
130  * before calling the function
131  */
132 extern MTS_EXPORT_CORE bool enableFPExceptions();
133 
134 /**
135  * \brief Disable floating point exceptions
136  *
137  * \return \c true if floating point exceptions were active
138  * before calling the function
139  */
141 
142 /// Restore floating point exceptions to the specified state
143 extern MTS_EXPORT_CORE void restoreFPExceptions(bool state);
144 
145 /// Cast between types that have an identical binary representation.
146 template<typename T, typename U> inline T union_cast(const U &val) {
147  BOOST_STATIC_ASSERT(sizeof(T) == sizeof(U));
148 
149  union {
150  U u;
151  T t;
152  } caster = {val};
153 
154  return caster.t;
155 }
156 
157 /// Swaps the byte order of the underlying representation
158 template<typename T> inline T endianness_swap(T value) {
159  union {
160  T value;
161  uint8_t byteValue[sizeof(T)];
162  } u;
163 
164  u.value = value;
165  std::reverse(&u.byteValue[0], &u.byteValue[sizeof(T)]);
166  return u.value;
167 }
168 
169 #ifdef __GNUC__
170 #if defined(__i386__)
171 static FINLINE uint64_t rdtsc(void) {
172  uint64_t x;
173  __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
174  return x;
175 }
176 #elif defined(__x86_64__)
177 static FINLINE uint64_t rdtsc(void) {
178  unsigned hi, lo;
179  __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
180  return ((uint64_t) lo)| (((uint64_t) hi) << 32);
181 }
182 #elif defined(__ARMEL__)
183 static FINLINE uint64_t rdtsc(void) {
184  // Code from gperftoos:
185  // https://code.google.com/p/gperftools/source/browse/trunk/src/base/cycleclock.h
186  uint32_t pmccntr;
187  uint32_t pmuseren;
188  uint32_t pmcntenset;
189  // Read the user mode perf monitor counter access permissions.
190  asm volatile ("mrc p15, 0, %0, c9, c14, 0" : "=r" (pmuseren));
191  if (EXPECT_TAKEN(pmuseren & 1)) { // Allows reading perfmon counters for user mode code.
192  asm volatile ("mrc p15, 0, %0, c9, c12, 1" : "=r" (pmcntenset));
193  if (EXPECT_TAKEN(pmcntenset & 0x80000000ul)) { // Is it counting?
194  asm volatile ("mrc p15, 0, %0, c9, c13, 0" : "=r" (pmccntr));
195  // The counter is set up to count every 64th cycle
196  return static_cast<uint64_t>(pmccntr) * 64; // Should optimize to << 6
197  }
198  }
199  // Soft-failover, assuming 1.5GHz CPUs
200 #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_CPUTIME)
201  timespec ts;
202  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
203  return static_cast<uint64_t>((ts.tv_sec + ts.tv_nsec * 1e-9) * 1.5e9);
204 #else
205  timeval tv;
206  gettimeofday(&tv, NULL);
207  return static_cast<uint64_t>((tv.tv_sec + tv.tv_usec * 1e-6) * 1.5e9);
208 #endif
209 }
210 #endif
211 #elif defined(__MSVC__)
212 static FINLINE __int64 rdtsc(void) {
213  return __rdtsc();
214 }
215 #else
216 # error "Cannot generate the rdtsc intrinsic."
217 #endif
218 
219 /**
220  * \brief Apply an arbitrary permutation to an array in linear time
221  *
222  * This algorithm is based on Donald Knuth's book
223  * "The Art of Computer Programming, Volume 3: Sorting and Searching"
224  * (1st edition, section 5.2, page 595)
225  *
226  * Given a permutation and an array of values, it applies the permutation
227  * in linear time without requiring additional memory. This is based on
228  * the fact that each permutation can be decomposed into a disjoint set
229  * of permutations, which can then be applied individually.
230  *
231  * \param data
232  * Pointer to the data that should be permuted
233  * \param perm
234  * Input permutation vector having the same size as \c data. After
235  * the function terminates, this vector will be set to the
236  * identity permutation.
237  */
238 template <typename DataType, typename IndexType> void permute_inplace(
239  DataType *data, std::vector<IndexType> &perm) {
240  for (size_t i=0; i<perm.size(); i++) {
241  if (perm[i] != i) {
242  /* The start of a new cycle has been found. Save
243  the value at this position, since it will be
244  overwritten */
245  IndexType j = (IndexType) i;
246  DataType curval = data[i];
247 
248  do {
249  /* Shuffle backwards */
250  IndexType k = perm[j];
251  data[j] = data[k];
252 
253  /* Also fix the permutations on the way */
254  perm[j] = j;
255  j = k;
256 
257  /* Until the end of the cycle has been found */
258  } while (perm[j] != i);
259 
260  /* Fix the final position with the saved value */
261  data[j] = curval;
262  perm[j] = j;
263  }
264  }
265 }
266 
267 //! @}
268 // -----------------------------------------------------------------------
269 
270 // -----------------------------------------------------------------------
271 //! @{ \name Numerical utility functions
272 // -----------------------------------------------------------------------
273 
274 /**
275  * \brief Solve a quadratic equation of the form a*x^2 + b*x + c = 0.
276  * \return \c true if a solution could be found
277  */
278 extern MTS_EXPORT_CORE bool solveQuadratic(Float a, Float b,
279  Float c, Float &x0, Float &x1);
280 
281 /**
282  * \brief Solve a double-precision quadratic equation of the
283  * form a*x^2 + b*x + c = 0.
284  * \return \c true if a solution could be found
285  */
286 extern MTS_EXPORT_CORE bool solveQuadraticDouble(double a, double b,
287  double c, double &x0, double &x1);
288 
289 //// Convert radians to degrees
290 inline Float radToDeg(Float value) { return value * (180.0f / M_PI); }
291 
292 /// Convert degrees to radians
293 inline Float degToRad(Float value) { return value * (M_PI / 180.0f); }
294 
295 /**
296  * \brief Numerically well-behaved routine for computing the angle
297  * between two unit direction vectors
298  *
299  * This should be used wherever one is tempted to compute the
300  * arc cosine of a dot product!
301  *
302  * Proposed by Don Hatch at
303  * http://www.plunk.org/~hatch/rightway.php
304  */
305 template <typename VectorType> inline Float unitAngle(const VectorType &u, const VectorType &v) {
306  if (dot(u, v) < 0)
307  return M_PI - 2 * std::asin(0.5f * (v+u).length());
308  else
309  return 2 * std::asin(0.5f * (v-u).length());
310 }
311 
312 //! @}
313 // -----------------------------------------------------------------------
314 
315 // -----------------------------------------------------------------------
316 //! @{ \name Warping and sampling-related utility functions
317 // -----------------------------------------------------------------------
318 
319 /**
320  * \brief Solve a 2x2 linear equation system using basic linear algebra
321  */
322 extern MTS_EXPORT_CORE bool solveLinearSystem2x2(const Float a[2][2], const Float b[2], Float x[2]);
323 
324 /**
325  * \brief Complete the set {a} to an orthonormal base
326  * \remark In Python, this function is used as
327  * follows: <tt>s, t = coordinateSystem(n)</tt>
328  * \ingroup libpython
329  */
330 extern MTS_EXPORT_CORE void coordinateSystem(const Vector &a, Vector &b, Vector &c);
331 
332 
333 /**
334  * \brief Given a smoothly varying shading normal and a tangent of a shape parameterization,
335  * compute a smoothly varying orthonormal frame
336  *
337  * \param n
338  * A shading normal at a surface position
339  * \param dpdu
340  * Position derivative of the underlying parameterization with respect to the 'u' coordinate
341  * \param frame
342  * Used to return the computed frame
343  *
344  * Mitsuba uses this function to compute the field \ref Intersection::shFrame
345  */
346 extern MTS_EXPORT_CORE void computeShadingFrame(const Vector &n, const Vector &dpdu, Frame &frame);
347 
348 /**
349  * \brief Compute the spatial derivative of \ref computeShadingFrame
350  *
351  * This is used by Manifold Exploration and Half Vector Light Transport
352  *
353  * \param n
354  * A shading normal at a surface position
355  * \param dpdu
356  * Position derivative of the underlying parameterization with respect to the 'u' coordinate
357  * \param dndu
358  * Derivative of the shading normal along the 'u' coordinate
359  * \param dndv
360  * Derivative of the shading normal along the 'v' coordinate
361  * \param du
362  * Used to return the 'u' derivative of the frame
363  * \param dv
364  * Used to return the 'v' derivative of the frame
365  */
366 extern MTS_EXPORT_CORE void computeShadingFrameDerivative(const Vector &n, const Vector &dpdu,
367  const Vector &dndu, const Vector &dndv, Frame &du, Frame &dv);
368 
369 /**
370  * \brief Generate (optionally jittered) stratified 1D samples
371  * \param random Source of random numbers
372  * \param dest A pointer to a floating point array with at least
373  * count entries
374  * \param count The interval [0, 1] is split into count strata
375  * \param jitter Randomly jitter the samples?
376  */
377 extern MTS_EXPORT_CORE void stratifiedSample1D(Random *random, Float *dest,
378  int count, bool jitter);
379 
380 /**
381  * \brief Generate (optionally jittered) stratified 2D samples
382  * \param random Source of random numbers
383  * \param dest A pointer to a floating point array
384  * \param countX The X axis interval [0, 1] is split into countX strata
385  * \param countY The Y axis interval [0, 1] is split into countY strata
386  * \param jitter Randomly jitter the samples?
387  */
388 extern MTS_EXPORT_CORE void stratifiedSample2D(Random *random, Point2 *dest,
389  int countX, int countY, bool jitter);
390 
391 /// Generate latin hypercube samples
392 extern MTS_EXPORT_CORE void latinHypercube(
393  Random *random, Float *dest, size_t nSamples, size_t nDim);
394 
395 /// Convert spherical coordinates to a direction
397 
398 /// Convert a direction to spherical coordinates
400 
401 //! @}
402 // -----------------------------------------------------------------------
403 
404 // -----------------------------------------------------------------------
405 //! @{ \name Fresnel reflectance computation and related things
406 // -----------------------------------------------------------------------
407 
408 /**
409  * \brief Calculates the unpolarized Fresnel reflection coefficient
410  * at a planar interface between two dielectrics
411  *
412  * This is a basic implementation that just returns the value of
413  * \f[
414  * R(\cos\theta_i,\cos\theta_t,\eta)=\frac{1}{2} \left[
415  * \left(\frac{\eta\cos\theta_i-\cos\theta_t}{\eta\cos\theta_i+\cos\theta_t}\right)^2+
416  * \left(\frac{\cos\theta_i-\eta\cos\theta_t}{\cos\theta_i+\eta\cos\theta_t}\right)^2
417  * \right]
418  * \f]
419  * The transmitted direction must be provided. There is no logic pertaining to
420  * total internal reflection or negative direction cosines.
421  *
422  * \param cosThetaI
423  * Absolute cosine of the angle between the normal and the incident ray
424  * \param cosThetaT
425  * Absolute cosine of the angle between the normal and the transmitted ray
426  * \param eta
427  * Relative refractive index to the transmitted direction
428  * \ingroup libpython
429  */
431  Float cosThetaT, Float eta);
432 
433 /**
434  * \brief Calculates the unpolarized Fresnel reflection coefficient
435  * at a planar interface between two dielectrics (extended version)
436  *
437  * In comparison to \ref fresnelDielectric(), this function internally
438  * computes the transmitted direction and returns it using the \c cosThetaT
439  * argument. When encountering total internal reflection, it sets
440  * <tt>cosThetaT=0</tt> and returns the value 1.
441  *
442  * When <tt>cosThetaI < 0</tt>, the function computes the Fresnel reflectance
443  * from the \a internal boundary, which is equivalent to calling the function
444  * with arguments <tt>fresnelDielectric(abs(cosThetaI), cosThetaT, 1/eta)</tt>.
445  *
446  * \remark When accessed from Python, this function has the signature
447  * "<tt>F, cosThetaT = fresnelDielectricExt(cosThetaI, eta)</tt>".
448  *
449  * \param cosThetaI
450  * Cosine of the angle between the normal and the incident ray
451  * (may be negative)
452  * \param cosThetaT
453  * Argument used to return the cosine of the angle between the normal
454  * and the transmitted ray, will have the opposite sign of \c cosThetaI
455  * \param eta
456  * Relative refractive index
457  * \ingroup libpython
458  */
460  Float &cosThetaT, Float eta);
461 
462 /**
463  * \brief Calculates the unpolarized Fresnel reflection coefficient
464  * at a planar interface between two dielectrics (extended version)
465  *
466  * This is just a convenience wrapper function around the other \c fresnelDielectricExt
467  * function, which does not return the transmitted direction cosine in case it is
468  * not needed by the application.
469  *
470  * \param cosThetaI
471  * Cosine of the angle between the normal and the incident ray
472  * \param eta
473  * Relative refractive index
474  */
475 inline Float fresnelDielectricExt(Float cosThetaI, Float eta) { Float cosThetaT;
476  return fresnelDielectricExt(cosThetaI, cosThetaT, eta); }
477 
478 /**
479  * \brief Calculates the unpolarized Fresnel reflection coefficient
480  * at a planar interface having a complex-valued relative index of
481  * refraction (approximate scalar version)
482  *
483  * The implementation of this function relies on a simplified expression
484  * that becomes increasingly accurate as k grows.
485  *
486  * The name of this function is a slight misnomer, since it supports
487  * the general case of a complex-valued relative index of refraction
488  * (rather than being restricted to conductors)
489  *
490  * \param cosThetaI
491  * Cosine of the angle between the normal and the incident ray
492  * \param eta
493  * Relative refractive index (real component)
494  * \param k
495  * Relative refractive index (imaginary component)
496  * \ingroup libpython
497  */
499  Float eta, Float k);
500 
501 /**
502  * \brief Calculates the unpolarized Fresnel reflection coefficient
503  * at a planar interface having a complex-valued relative index of
504  * refraction (approximate vectorized version)
505  *
506  * The implementation of this function relies on a simplified expression
507  * that becomes increasingly accurate as k grows.
508  *
509  * The name of this function is a slight misnomer, since it supports
510  * the general case of a complex-valued relative index of refraction
511  * (rather than being restricted to conductors)
512  *
513  * \param cosThetaI
514  * Cosine of the angle between the normal and the incident ray
515  * \param eta
516  * Relative refractive index (real component)
517  * \param k
518  * Relative refractive index (imaginary component)
519  * \ingroup libpython
520  */
521 extern MTS_EXPORT_CORE Spectrum fresnelConductorApprox(Float cosThetaI,
522  const Spectrum &eta, const Spectrum &k);
523 
524 /**
525  * \brief Calculates the unpolarized Fresnel reflection coefficient
526  * at a planar interface having a complex-valued relative index of
527  * refraction (accurate scalar version)
528  *
529  * The implementation of this function computes the exact unpolarized
530  * Fresnel reflectance for a complex index of refraction change.
531  *
532  * The name of this function is a slight misnomer, since it supports
533  * the general case of a complex-valued relative index of refraction
534  * (rather than being restricted to conductors)
535  *
536  * \param cosThetaI
537  * Cosine of the angle between the normal and the incident ray
538  * \param eta
539  * Relative refractive index (real component)
540  * \param k
541  * Relative refractive index (imaginary component)
542  * \ingroup libpython
543  */
545  Float eta, Float k);
546 
547 /**
548  * \brief Calculates the unpolarized Fresnel reflection coefficient
549  * at a planar interface having a complex-valued relative index of
550  * refraction (accurate vectorized version)
551  *
552  * The implementation of this function computes the exact unpolarized
553  * Fresnel reflectance for a complex index of refraction change.
554  *
555  * The name of this function is a slight misnomer, since it supports
556  * the general case of a complex-valued relative index of refraction
557  * (rather than being restricted to conductors)
558  *
559  * \param cosThetaI
560  * Cosine of the angle between the normal and the incident ray
561  * \param eta
562  * Relative refractive index (real component)
563  * \param k
564  * Relative refractive index (imaginary component)
565  * \ingroup libpython
566  */
567 extern MTS_EXPORT_CORE Spectrum fresnelConductorExact(Float cosThetaI,
568  const Spectrum &eta, const Spectrum &k);
569 
570 /**
571  * \brief Calculates the diffuse unpolarized Fresnel reflectance of
572  * a dielectric material (sometimes referred to as "Fdr").
573  *
574  * This value quantifies what fraction of diffuse incident illumination
575  * will, on average, be reflected at a dielectric material boundary
576  *
577  * \param eta
578  * Relative refraction coefficient
579  * \param fast
580  * Compute an approximate value? If set to \c true, the
581  * implementation will use a polynomial approximation with
582  * a max relative error of ~0.5% on the interval 0.5 < \c eta < 2.
583  * When \c fast=false, the code will use Gauss-Lobatto quadrature
584  * to compute the diffuse reflectance more accurately, and for
585  * a wider range of refraction coefficients, but at a cost
586  * in terms of performance.
587  * \ingroup libpython
588  */
590  Float eta, bool fast = false);
591 
592 /**
593  * \brief Specularly reflect direction \c wi with respect to the given surface normal
594  * \param wi
595  * Incident direction
596  * \param n
597  * Surface normal
598  * \return
599  * Specularly reflected direction
600  * \ingroup libpython
601  */
602 extern MTS_EXPORT_CORE Vector reflect(const Vector &wi, const Normal &n);
603 
604 /**
605  * \brief Specularly refract the direction \c wi into a planar dielectric with
606  * the given surface normal and index of refraction.
607  *
608  * This variant internally computes the transmitted direction cosine by
609  * calling \ref fresnelDielectricExt. As a side result, the cosine and
610  * Fresnel reflectance are computed and returned via the reference arguments
611  * \c cosThetaT and \c F.
612  *
613  * \remark When accessed from Python, this function has the signature
614  * "<tt>dir, cosThetaT, F = refract(wi, n, eta)</tt>".
615  *
616  * \param wi
617  * Incident direction
618  * \param n
619  * Surface normal
620  * \param eta
621  * Relative index of refraction at the interface
622  * \param cosThetaT
623  * Parameter used to return the signed cosine of the angle between the transmitted
624  * direction and the surface normal
625  * \param F
626  * Parameter used to return the Fresnel reflectance
627  * \return
628  * Specularly transmitted direction (or zero in
629  * the case of total internal reflection)
630  * \ingroup libpython
631  */
632 extern MTS_EXPORT_CORE Vector refract(const Vector &wi, const Normal &n,
633  Float eta, Float &cosThetaT, Float &F);
634 
635 /**
636  * \brief Specularly refract the direction \c wi into a planar dielectric with
637  * the given surface normal and index of refraction.
638  *
639  * This variant assumes that the transmitted direction cosine has
640  * has <em>already</em> been computed, allowing it to save some time.
641  *
642  * \param wi
643  * Incident direction
644  * \param n
645  * Surface normal
646  * \param eta
647  * Relative index of refraction at the interface
648  * \param cosThetaT
649  * Signed cosine of the angle between the transmitted direction and
650  * the surface normal obtained from a prior call to \ref fresnelDielectricExt()
651  * \return
652  * Specularly transmitted direction
653  * \ingroup libpython
654  */
655 extern MTS_EXPORT_CORE Vector refract(const Vector &wi, const Normal &n,
656  Float eta, Float cosThetaT);
657 
658 /**
659  * \brief Specularly refract the direction \c wi into a planar dielectric with
660  * the given surface normal and index of refraction.
661  *
662  * This function is a simple convenience function that only returns the refracted
663  * direction while not computing the Frensel reflectance.
664  *
665  * \param wi
666  * Incident direction
667  * \param n
668  * Surface normal
669  * \param eta
670  * Relative index of refraction at the interface
671  * \return
672  * Specularly transmitted direction (or zero in
673  * the case of total internal reflection)
674  */
675 extern MTS_EXPORT_CORE Vector refract(const Vector &wi, const Normal &n, Float eta);
676 
677 //! @}
678 // -----------------------------------------------------------------------
679 
680 /*! @} */
681 
683 
684 #endif /* __MITSUBA_CORE_UTIL_H_ */
MTS_EXPORT_CORE void latinHypercube(Random *random, Float *dest, size_t nSamples, size_t nDim)
Generate latin hypercube samples.
MTS_EXPORT_CORE bool solveQuadraticDouble(double a, double b, double c, double &x0, double &x1)
Solve a double-precision quadratic equation of the form a*x^2 + b*x + c = 0.
MTS_EXPORT_CORE std::string formatString(const char *pFmt,...)
Wrapped snprintf.
void permute_inplace(DataType *data, std::vector< IndexType > &perm)
Apply an arbitrary permutation to an array in linear time.
Definition: util.h:238
MTS_EXPORT_CORE size_t getTotalSystemMemory()
Returns the total amount of memory available to the OS.
MTS_EXPORT_CORE void restoreFPExceptions(bool state)
Restore floating point exceptions to the specified state.
MTS_EXPORT_CORE Float fresnelDielectricExt(Float cosThetaI, Float &cosThetaT, Float eta)
Calculates the unpolarized Fresnel reflection coefficient at a planar interface between two dielectri...
MTS_EXPORT_CORE bool solveLinearSystem2x2(const Float a[2][2], const Float b[2], Float x[2])
Solve a 2x2 linear equation system using basic linear algebra.
MTS_EXPORT_CORE std::string indent(const std::string &string, int amount=1)
Indent a string (Used for recursive toString() structure dumping)
MTS_EXPORT_CORE Float fresnelDiffuseReflectance(Float eta, bool fast=false)
Calculates the diffuse unpolarized Fresnel reflectance of a dielectric material (sometimes referred t...
MTS_EXPORT_CORE void stratifiedSample2D(Random *random, Point2 *dest, int countX, int countY, bool jitter)
Generate (optionally jittered) stratified 2D samples.
MTS_EXPORT_CORE void computeShadingFrame(const Vector &n, const Vector &dpdu, Frame &frame)
Given a smoothly varying shading normal and a tangent of a shape parameterization, compute a smoothly varying orthonormal frame.
MTS_EXPORT_CORE Point2 toSphericalCoordinates(const Vector &v)
Convert a direction to spherical coordinates.
Float degToRad(Float value)
Convert degrees to radians.
Definition: util.h:293
#define MTS_EXPORT_CORE
Definition: getopt.h:29
Float unitAngle(const VectorType &u, const VectorType &v)
Numerically well-behaved routine for computing the angle between two unit direction vectors...
Definition: util.h:305
#define M_PI
Definition: constants.h:90
MTS_EXPORT_CORE void coordinateSystem(const Vector &a, Vector &b, Vector &c)
Complete the set {a} to an orthonormal base.
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
MTS_EXPORT_CORE Vector refract(const Vector &wi, const Normal &n, Float eta, Float &cosThetaT, Float &F)
Specularly refract the direction wi into a planar dielectric with the given surface normal and index ...
MTS_EXPORT_CORE bool enableFPExceptions()
Enable floating point exceptions (to catch NaNs, overflows, arithmetic with infinity).
MTS_EXPORT_CORE std::string timeString(Float time, bool precise=false)
Convert a time difference (in seconds) to a string representation.
MTS_EXPORT_CORE std::string getFQDN()
Return the fully qualified domain name of this machine.
T endianness_swap(T value)
Swaps the byte order of the underlying representation.
Definition: util.h:158
MTS_EXPORT_CORE void computeShadingFrameDerivative(const Vector &n, const Vector &dpdu, const Vector &dndu, const Vector &dndv, Frame &du, Frame &dv)
Compute the spatial derivative of computeShadingFrame.
Float radToDeg(Float value)
Solve a quadratic equation of the form a*x^2 + b*x + c = 0.
Definition: util.h:290
MTS_EXPORT_CORE void *__restrict allocAligned(size_t size)
Allocate an aligned region of memory.
MTS_EXPORT_CORE int getCoreCount()
Determine the number of available CPU cores.
MTS_EXPORT_CORE Float fresnelConductorExact(Float cosThetaI, Float eta, Float k)
Calculates the unpolarized Fresnel reflection coefficient at a planar interface having a complex-valu...
MTS_EXPORT_CORE std::string trim(const std::string &str)
Trim spaces (&#39; &#39;, &#39;\n&#39;, &#39;\r&#39;, &#39;\t&#39;) from the ends of a string.
MTS_EXPORT_CORE bool solveQuadratic(Float a, Float b, Float c, Float &x0, Float &x1)
Solve a quadratic equation of the form a*x^2 + b*x + c = 0.
MTS_EXPORT_CORE Float fresnelDielectric(Float cosThetaI, Float cosThetaT, Float eta)
Calculates the unpolarized Fresnel reflection coefficient at a planar interface between two dielectri...
Definition: fwd.h:99
MTS_EXPORT_CORE Vector sphericalDirection(Float theta, Float phi)
Convert spherical coordinates to a direction.
MTS_EXPORT_CORE std::string memString(size_t size, bool precise=false)
Turn a memory size into a human-readable string.
Definition: fwd.h:96
MTS_EXPORT_CORE void stratifiedSample1D(Random *random, Float *dest, int count, bool jitter)
Generate (optionally jittered) stratified 1D samples.
Simple functor for sorting string parameters by length and content.
Definition: util.h:82
T union_cast(const U &val)
Cast between types that have an identical binary representation.
Definition: util.h:146
MTS_EXPORT_CORE void freeAligned(void *ptr)
Free an aligned region of memory.
std::string containerToString(const Iterator &start, const Iterator &end)
Return a string representation of a list of objects.
Definition: util.h:65
MTS_EXPORT_CORE Vector reflect(const Vector &wi, const Normal &n)
Specularly reflect direction wi with respect to the given surface normal.
MTS_EXPORT_CORE std::string getHostName()
Return the host name of this machine.
T dot(const TQuaternion< T > &q1, const TQuaternion< T > &q2)
Definition: quat.h:348
#define MTS_NAMESPACE_END
Definition: platform.h:138
MTS_EXPORT_CORE size_t getPrivateMemoryUsage()
Return the process private memory usage in bytes.
bool operator()(const std::string &a, const std::string &b) const
Definition: util.h:83
MTS_EXPORT_CORE std::vector< std::string > tokenize(const std::string &string, const std::string &delim)
Given a list of delimiters, tokenize a std::string into a vector of strings.
MTS_EXPORT_CORE Float fresnelConductorApprox(Float cosThetaI, Float eta, Float k)
Calculates the unpolarized Fresnel reflection coefficient at a planar interface having a complex-valu...
MTS_EXPORT_CORE bool disableFPExceptions()
Disable floating point exceptions.