Loading...
Searching...
No Matches
pbrtUtils.h
1// Note: these functions are adapted from the pbrt-v4 repository, with
2// modifications to use OpenUSD math types.
3// pbrt-v4 is located at https://github.com/mmp/pbrt-v4/
4
5// pbrt is Copyright(c) 1998-2020 Matt Pharr, Wenzel Jakob, and Greg Humphreys.
6// The pbrt source code is licensed under the Apache License, Version 2.0.
7// SPDX: Apache-2.0
8
9#ifndef PXR_IMAGING_PLUGIN_HD_EMBREE_PBRT_UTILS_H
10#define PXR_IMAGING_PLUGIN_HD_EMBREE_PBRT_UTILS_H
11
12#include "pxr/pxr.h"
13
14#include "pxr/base/arch/math.h"
15#include "pxr/base/gf/vec2f.h"
16#include "pxr/base/gf/vec3f.h"
17
18PXR_NAMESPACE_OPEN_SCOPE
19
20namespace pxr_pbrt {
21
22template <typename T>
23constexpr T pi = static_cast<T>(M_PI);
24
25// Ported from PBRT:
26// https://github.com/mmp/pbrt-v4/blob/master/src/pbrt/util/vecmath.h#L1666
27inline GfVec3f
28SphericalDirection(float sinTheta, float cosTheta, float phi)
29{
30 return GfVec3f(GfClamp(sinTheta, -1.0f, 1.0f) * GfCos(phi),
31 GfClamp(sinTheta, -1.0f, 1.0f) * GfSin(phi),
32 GfClamp(cosTheta, -1.0f, 1.0f));
33}
34
35// Ported from PBRT:
36// https://github.com/mmp/pbrt-v4/blob/master/src/pbrt/util/sampling.h#L427
37inline GfVec3f
38SampleUniformCone(GfVec2f const& u, float angle)
39{
40 float cosAngle = GfCos(angle);
41 float cosTheta = (1.0f - u[0]) + u[0] * cosAngle;
42 float sinTheta = GfSqrt(GfMax(0.0f, 1.0f - cosTheta*cosTheta));
43 float phi = u[1] * 2.0f * pi<float>;
44 return SphericalDirection(sinTheta, cosTheta, phi);
45}
46
47// Ported from PBRT:
48// https://github.com/mmp/pbrt-v4/blob/master/src/pbrt/util/sampling.h#L423
49// (but upside down)
50inline float
51InvUniformConePDF(float angle)
52{
53 return 2.0f * pi<float> * (1.0f - GfCos(angle));
54}
55
56} // namespace pxr_pbrt
57
58PXR_NAMESPACE_CLOSE_SCOPE
59
60#endif // PXR_IMAGING_PLUGIN_HD_EMBREE_PBRT_UTILS_H
Architecture-specific math function calls.
Basic type for a vector of 2 float components.
Definition: vec2f.h:46
Basic type for a vector of 3 float components.
Definition: vec3f.h:46
double GfCos(double v)
Return cos(v).
Definition: math.h:249
double GfClamp(double value, double min, double max)
Return the resulting of clamping value to lie between min and max.
Definition: math.h:263
double GfSqrt(double f)
Return sqrt(f).
Definition: math.h:187
T GfMax(T a1, T a2)
Returns the largest of the given values.
Definition: math.h:326
double GfSin(double v)
Return sin(v).
Definition: math.h:243