Fermat
lambert_edf.h
1 /*
2 * Copyright (c) 2010-2016, NVIDIA Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of NVIDIA Corporation nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
34 #pragma once
35 
36 #include <cugar/linalg/vector.h>
37 #include <cugar/basic/numbers.h>
39 #include <cugar/bsdf/differential_geometry.h>
40 
41 
42 namespace cugar {
43 
48 struct LambertEdf
52 {
53  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
54  LambertEdf(const Vector3f _color) :
55  color(_color) {}
56 
59  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
60  Vector3f f(const DifferentialGeometry& geometry, const Vector3f in, const Vector3f out) const
61  {
62  const float NoL = dot(geometry.normal_s, out);
63 
64  return NoL > 0.0f ? color : Vector3f(0.0f);
65  }
66 
69  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
70  Vector3f f_over_p(const DifferentialGeometry& geometry, const Vector3f in, const Vector3f out) const
71  {
72  const float NoL = dot(geometry.normal_s, out);
73 
74  return NoL > 0.0f ? color * float(M_PI) : Vector3f(0.0f);
75  }
76 
79  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
80  float p(const DifferentialGeometry& geometry, const Vector3f in, const Vector3f out, const SphericalMeasure measure = kProjectedSolidAngle) const
81  {
82  return (measure == kProjectedSolidAngle) ?
83  1.0f / float(M_PI) :
84  fabsf(dot(geometry.normal_s, out)) / float(M_PI);
85  }
86 
89  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
90  void sample(
91  const Vector2f u,
92  const DifferentialGeometry& geometry,
93  const Vector3f in,
94  Vector3f& out,
95  Vector3f& g,
96  float& p,
97  float& p_proj) const
98  {
100 
101  out = local_dir[0] * geometry.tangent +
102  local_dir[1] * geometry.binormal +
103  local_dir[2] * geometry.normal_s;
104  g = color * float(M_PI);
105  p = local_dir[2] / float(M_PI);
106  p_proj = 1.0f / float(M_PI);
107  }
108 
111  template <typename RandomGeneratorT>
112  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
113  bool invert(
114  const DifferentialGeometry& geometry,
115  const Vector3f V,
116  const Vector3f L,
117  RandomGeneratorT& random,
118  Vector3f& z,
119  float& p,
120  float& p_proj) const
121  {
122  const Vector3f local_L(
123  dot(L, geometry.tangent),
124  dot(L, geometry.binormal),
125  dot(L, geometry.normal_s));
126 
127  const float LN = local_L[2];
128 
129  if (LN < 0.0f)
130  return false;
131 
132  p = float(M_PI) / cugar::max( LN, 1.0e-8f );
133  p_proj = float(M_PI);
134 
135  const Vector2f u = cosine_hemisphere_to_square(local_L);
136  z = Vector3f(u.x, u.y, random.next());
137  return true;
138  }
139 
142  CUGAR_FORCEINLINE CUGAR_HOST_DEVICE
144  const DifferentialGeometry& geometry,
145  const Vector3f V,
146  const Vector3f L,
147  const Vector3f u,
148  float& p,
149  float& p_proj) const
150  {
151  const Vector3f N = geometry.normal_s;
152 
153  const float LN = dot(L, N);
154 
155  if (LN < 0.0f)
156  p = p_proj = 0.0f;
157  else
158  {
159  p = float(M_PI) / cugar::max( LN, 1.0e-8f );
160  p_proj = float(M_PI);
161  }
162  }
163 
164 public:
165  Vector3f color;
166 };
167 
171 } // namespace cugar
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE Vector3f f_over_p(const DifferentialGeometry &geometry, const Vector3f in, const Vector3f out) const
Definition: lambert_edf.h:70
SphericalMeasure
Definition: differential_geometry.h:50
Defines various spherical mappings.
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE bool invert(const DifferentialGeometry &geometry, const Vector3f V, const Vector3f L, RandomGeneratorT &random, Vector3f &z, float &p, float &p_proj) const
Definition: lambert_edf.h:113
Definition: differential_geometry.h:59
float random()
Definition: tiled_sampling.h:44
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE Vector3f f(const DifferentialGeometry &geometry, const Vector3f in, const Vector3f out) const
Definition: lambert_edf.h:60
Define a vector_view POD type and plain_view() for std::vector.
Definition: diff.h:38
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE float p(const DifferentialGeometry &geometry, const Vector3f in, const Vector3f out, const SphericalMeasure measure=kProjectedSolidAngle) const
Definition: lambert_edf.h:80
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE void sample(const Vector2f u, const DifferentialGeometry &geometry, const Vector3f in, Vector3f &out, Vector3f &g, float &p, float &p_proj) const
Definition: lambert_edf.h:90
CUGAR_HOST CUGAR_DEVICE Vector3f square_to_cosine_hemisphere(const Vector2f &uv)
Definition: mappings_inline.h:119
CUGAR_HOST CUGAR_DEVICE Vector2f cosine_hemisphere_to_square(const Vector3f &dir)
Definition: mappings_inline.h:137
CUGAR_FORCEINLINE CUGAR_HOST_DEVICE void inverse_pdf(const DifferentialGeometry &geometry, const Vector3f V, const Vector3f L, const Vector3f u, float &p, float &p_proj) const
Definition: lambert_edf.h:143