Fermat
vtl.h
1 /*
2  * Fermat
3  *
4  * Copyright (c) 2016-2019, NVIDIA CORPORATION. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the NVIDIA CORPORATION nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <types.h>
32 #include <ray.h>
33 #include <vertex.h>
34 #include <mesh_utils.h>
35 
38 
41 
44 struct VTL
45 {
46  FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
47  VTL() {}
48 
49  FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
50  VTL(const uint32 _prim_id, const float2 _uv0, const float2 _uv1, const float2 _uv2, const float _area)
51  {
52  prim_id = _prim_id;
53  area = _area;
54  uv0 = _uv2;
55  uv1 = _uv1;
56  uv2 = _uv0;
57  }
58 
59  FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
60  void interpolate_positions(MeshView mesh, cugar::Vector3f& p0, cugar::Vector3f& p1, cugar::Vector3f& p2) const
61  {
62  // fetch the original triangle
63  const MeshStorage::vertex_triangle tri = reinterpret_cast<const MeshStorage::vertex_triangle*>(mesh.vertex_indices)[prim_id];
64  const cugar::Vector3f vtp0 = load_vertex(mesh, tri.x);
65  const cugar::Vector3f vtp1 = load_vertex(mesh, tri.y);
66  const cugar::Vector3f vtp2 = load_vertex(mesh, tri.z);
67 
68  // interpolate the VTL
69  p0 = vtp2 * (1.0f - uv0.x - uv0.y) + vtp0 * uv0.x + vtp1 * uv0.y;
70  p1 = vtp2 * (1.0f - uv1.x - uv1.y) + vtp0 * uv1.x + vtp1 * uv1.y;
71  p2 = vtp2 * (1.0f - uv2.x - uv2.y) + vtp0 * uv2.x + vtp1 * uv2.y;
72  }
73 
74  FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
75  void interpolate_tex_coords(MeshView mesh, cugar::Vector2f& t0, cugar::Vector2f& t1, cugar::Vector2f& t2) const
76  {
77  // estimate the triangle area in texture space
78  const MeshStorage::texture_triangle tri = reinterpret_cast<const MeshStorage::texture_triangle*>(mesh.texture_indices)[prim_id];
79  const cugar::Vector2f vtt0 = tri.x >= 0 ? reinterpret_cast<const float2*>(mesh.texture_data)[tri.x] : cugar::Vector2f(1.0f, 0.0f);
80  const cugar::Vector2f vtt1 = tri.y >= 0 ? reinterpret_cast<const float2*>(mesh.texture_data)[tri.y] : cugar::Vector2f(0.0f, 1.0f);
81  const cugar::Vector2f vtt2 = tri.z >= 0 ? reinterpret_cast<const float2*>(mesh.texture_data)[tri.z] : cugar::Vector2f(0.0f, 0.0f);
82 
83  // interpolate the VTL
84  t0 = vtt2 * (1.0f - uv0.x - uv0.y) + vtt0 * uv0.x + vtt1 * uv0.y;
85  t1 = vtt2 * (1.0f - uv1.x - uv1.y) + vtt0 * uv1.x + vtt1 * uv1.y;
86  t2 = vtt2 * (1.0f - uv2.x - uv2.y) + vtt0 * uv2.x + vtt1 * uv2.y;
87  }
88 
89  FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
90  cugar::Vector2f interpolate_uv(const cugar::Vector2f& uv) const
91  {
92  return uv2 * (1.0f - uv.x - uv.y) + uv0 * uv.x + uv1 * uv.y;
93  }
94 
95  FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
96  cugar::Vector2f uv_centroid() const
97  {
98  return (uv0 + uv1 + uv2) / 3.0f;
99  }
100 
101  FERMAT_HOST_DEVICE FERMAT_FORCEINLINE
102  cugar::Vector3f centroid(MeshView mesh) const
103  {
104  const cugar::Vector2f uv = (uv0 + uv1 + uv2) / 3.0f;
105  return interpolate_position( mesh, VertexGeometryId(prim_id, uv) );
106  }
107 
108  uint32 prim_id;
109  float area;
110  cugar::Vector2f uv0;
111  cugar::Vector2f uv1;
112  cugar::Vector2f uv2;
113 };
114 
Definition: vertex.h:105
Definition: vtl.h:44
Definition: MeshView.h:96
FERMAT_HOST_DEVICE cugar::Vector3f interpolate_position(const MeshView &mesh, const uint32 tri_id, const float u, const float v, float *pdf=0)
Definition: mesh_utils.h:323