20 #if !defined(__MITSUBA_CORE_UTIL_H_)
21 #define __MITSUBA_CORE_UTIL_H_
23 #include <boost/static_assert.hpp>
40 const std::string &
string,
41 const std::string &delim
65 template<
class Iterator> std::string
containerToString(
const Iterator &start,
const Iterator &end) {
66 std::ostringstream oss;
67 oss <<
"{" << std::endl;
70 oss <<
" " <<
indent((*it)->toString());
73 oss <<
"," << std::endl;
83 bool operator()(
const std::string &a,
const std::string &b)
const {
84 if (a.length() == b.length())
86 return a.length() < b.length();
146 template<
typename T,
typename U>
inline T
union_cast(
const U &val) {
147 BOOST_STATIC_ASSERT(
sizeof(T) ==
sizeof(U));
165 std::reverse(&u.byteValue[0], &u.byteValue[
sizeof(T)]);
170 #if defined(__i386__)
171 static FINLINE uint64_t rdtsc(
void) {
173 __asm__
volatile (
".byte 0x0f, 0x31" :
"=A" (x));
176 #elif defined(__x86_64__)
177 static FINLINE uint64_t rdtsc(
void) {
179 __asm__ __volatile__ (
"rdtsc" :
"=a"(lo),
"=d"(hi));
180 return ((uint64_t) lo)| (((uint64_t) hi) << 32);
182 #elif defined(__ARMEL__)
183 static FINLINE uint64_t rdtsc(
void) {
190 asm volatile (
"mrc p15, 0, %0, c9, c14, 0" :
"=r" (pmuseren));
191 if (EXPECT_TAKEN(pmuseren & 1)) {
192 asm volatile (
"mrc p15, 0, %0, c9, c12, 1" :
"=r" (pmcntenset));
193 if (EXPECT_TAKEN(pmcntenset & 0x80000000ul)) {
194 asm volatile (
"mrc p15, 0, %0, c9, c13, 0" :
"=r" (pmccntr));
196 return static_cast<uint64_t
>(pmccntr) * 64;
200 #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_CPUTIME)
202 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
203 return static_cast<uint64_t
>((ts.tv_sec + ts.tv_nsec * 1e-9) * 1.5e9);
206 gettimeofday(&tv, NULL);
207 return static_cast<uint64_t
>((tv.tv_sec + tv.tv_usec * 1e-6) * 1.5e9);
211 #elif defined(__MSVC__)
212 static FINLINE __int64 rdtsc(
void) {
216 # error "Cannot generate the rdtsc intrinsic."
239 DataType *data, std::vector<IndexType> &perm) {
240 for (
size_t i=0; i<perm.size(); i++) {
245 IndexType j = (IndexType) i;
246 DataType curval = data[i];
250 IndexType k = perm[j];
258 }
while (perm[j] != i);
287 double c,
double &x0,
double &x1);
305 template <
typename VectorType>
inline Float unitAngle(
const VectorType &u,
const VectorType &v) {
307 return M_PI - 2 * std::asin(0.5f * (v+u).length());
309 return 2 * std::asin(0.5f * (v-u).length());
367 const Vector &dndu,
const Vector &dndv, Frame &du, Frame &dv);
378 int count,
bool jitter);
389 int countX,
int countY,
bool jitter);
393 Random *random,
Float *dest,
size_t nSamples,
size_t nDim);
522 const Spectrum &eta,
const Spectrum &k);
568 const Spectrum &eta,
const Spectrum &k);
590 Float eta,
bool fast =
false);
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.
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 (' ', '\n', '\r', '\t') 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...
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.
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
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.