Spaces:
Sleeping
Sleeping
File size: 5,810 Bytes
66c9c8a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | /** Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#include "mesh.h"
#include "bvh.h"
#include "warp.h"
#include "cuda_util.h"
using namespace wp;
#include <map>
namespace
{
// host-side copy of mesh descriptors, maps GPU mesh address (id) to a CPU desc
std::map<uint64_t, Mesh> g_mesh_descriptors;
} // anonymous namespace
namespace wp
{
bool mesh_get_descriptor(uint64_t id, Mesh& mesh)
{
const auto& iter = g_mesh_descriptors.find(id);
if (iter == g_mesh_descriptors.end())
return false;
else
mesh = iter->second;
return true;
}
void mesh_add_descriptor(uint64_t id, const Mesh& mesh)
{
g_mesh_descriptors[id] = mesh;
}
void mesh_rem_descriptor(uint64_t id)
{
g_mesh_descriptors.erase(id);
}
} // namespace wp
void bvh_refit_with_solid_angle_recursive_host(BVH& bvh, int index, Mesh& mesh)
{
BVHPackedNodeHalf& lower = bvh.node_lowers[index];
BVHPackedNodeHalf& upper = bvh.node_uppers[index];
if (lower.b)
{
// Leaf, compute properties
const int leaf_index = lower.i;
precompute_triangle_solid_angle_props(mesh.points[mesh.indices[leaf_index*3+0]], mesh.points[mesh.indices[leaf_index*3+1]], mesh.points[mesh.indices[leaf_index*3+2]], mesh.solid_angle_props[index]);
(vec3&)lower = mesh.solid_angle_props[index].box.lower;
(vec3&)upper = mesh.solid_angle_props[index].box.upper;
}
else
{
int left_index = lower.i;
int right_index = upper.i;
bvh_refit_with_solid_angle_recursive_host(bvh, left_index, mesh);
bvh_refit_with_solid_angle_recursive_host(bvh, right_index, mesh);
// combine
SolidAngleProps* left_child_data = &mesh.solid_angle_props[left_index];
SolidAngleProps* right_child_data = (left_index != right_index) ? &mesh.solid_angle_props[right_index] : NULL;
combine_precomputed_solid_angle_props(mesh.solid_angle_props[index], left_child_data, right_child_data);
// compute union of children
const vec3& left_lower = (vec3&)bvh.node_lowers[left_index];
const vec3& left_upper = (vec3&)bvh.node_uppers[left_index];
const vec3& right_lower = (vec3&)bvh.node_lowers[right_index];
const vec3& right_upper = (vec3&)bvh.node_uppers[right_index];
// union of child bounds
vec3 new_lower = min(left_lower, right_lower);
vec3 new_upper = max(left_upper, right_upper);
// write new BVH nodes
(vec3&)lower = new_lower;
(vec3&)upper = new_upper;
}
}
void bvh_refit_with_solid_angle_host(BVH& bvh, Mesh& mesh)
{
bvh_refit_with_solid_angle_recursive_host(bvh, 0, mesh);
}
uint64_t mesh_create_host(array_t<wp::vec3> points, array_t<wp::vec3> velocities, array_t<int> indices, int num_points, int num_tris, int support_winding_number)
{
Mesh* m = new Mesh(points, velocities, indices, num_points, num_tris);
m->lowers = new vec3[num_tris];
m->uppers = new vec3[num_tris];
float sum = 0.0;
for (int i=0; i < num_tris; ++i)
{
wp::vec3& p0 = points[indices[i*3+0]];
wp::vec3& p1 = points[indices[i*3+1]];
wp::vec3& p2 = points[indices[i*3+2]];
// compute triangle bounds
bounds3 b;
b.add_point(p0);
b.add_point(p1);
b.add_point(p2);
m->lowers[i] = b.lower;
m->uppers[i] = b.upper;
// compute edge lengths
sum += length(p0-p1) + length(p0-p2) + length(p2-p1);
}
m->average_edge_length = sum / (num_tris*3);
m->bvh = *(wp::BVH*)bvh_create_host(m->lowers, m->uppers, num_tris);
if (support_winding_number)
{
// Let's first compute the sold
int num_bvh_nodes = 2*num_tris-1;
m->solid_angle_props = new SolidAngleProps[num_bvh_nodes];
bvh_refit_with_solid_angle_host(m->bvh, *m);
}
return (uint64_t)m;
}
void mesh_destroy_host(uint64_t id)
{
Mesh* m = (Mesh*)(id);
delete[] m->lowers;
delete[] m->uppers;
if (m->solid_angle_props) {
delete [] m->solid_angle_props;
}
bvh_destroy_host(m->bvh);
delete m;
}
void mesh_refit_host(uint64_t id)
{
Mesh* m = (Mesh*)(id);
float sum = 0.0;
for (int i=0; i < m->num_tris; ++i)
{
wp::vec3 p0 = m->points.data[m->indices.data[i*3+0]];
wp::vec3 p1 = m->points.data[m->indices.data[i*3+1]];
wp::vec3 p2 = m->points.data[m->indices.data[i*3+2]];
// compute triangle bounds
bounds3 b;
b.add_point(p0);
b.add_point(p1);
b.add_point(p2);
m->lowers[i] = b.lower;
m->uppers[i] = b.upper;
sum += length(p0-p1) + length(p0-p2) + length(p2-p1);
}
m->average_edge_length = sum / (m->num_tris*3);
if (m->solid_angle_props)
{
// If solid angle were used, use refit solid angle
bvh_refit_with_solid_angle_host(m->bvh, *m);
}
else
{
bvh_refit_host(m->bvh);
}
}
// stubs for non-CUDA platforms
#if !WP_ENABLE_CUDA
WP_API uint64_t mesh_create_device(void* context, wp::array_t<wp::vec3> points, wp::array_t<wp::vec3> velocities, wp::array_t<int> tris, int num_points, int num_tris, int support_winding_number) { return 0; }
WP_API void mesh_destroy_device(uint64_t id) {}
WP_API void mesh_refit_device(uint64_t id) {}
#endif // !WP_ENABLE_CUDA |