Skip to content

Commit

Permalink
Merge branch 'dev' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
larc committed Jan 23, 2024
2 parents 7eb4512 + 8e09bf2 commit 2d0c822
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 27 deletions.
5 changes: 5 additions & 0 deletions include/gproshan/mesh/che.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CHE_H

#include <gproshan/include.h>
#include <gproshan/util.h>
#include <gproshan/geometry/vec.h>
#include <gproshan/geometry/mat.h>

Expand Down Expand Up @@ -63,6 +64,7 @@ class che
vertex * VN = nullptr; ///< vertex normals : v -> normal(v)
rgb_t * VC = nullptr; ///< vertex color : v -> color(v)
real_t * VHC = nullptr; ///< vertex color heatmap : v -> heatmap(v)
real_t scale_hm = 1; ///< vertex color heatmap scale factor

bool manifold = true;

Expand All @@ -85,6 +87,8 @@ class che

// vertex color methods
const real_t * heatmap_ptr() const;
void heatmap_scale(const real_t shm);
real_t heatmap_scale(const index_t v) const;
real_t heatmap(const index_t v) const;
real_t & heatmap(const index_t v);
const rgb_t * rgb_ptr() const;
Expand Down Expand Up @@ -212,6 +216,7 @@ struct CHE
vertex * GT = nullptr;
vertex * VN = nullptr;
che::rgb_t * VC = nullptr;
real_t * VHC = nullptr;
index_t * VT = nullptr;
index_t * OT = nullptr;
index_t * EVT = nullptr;
Expand Down
30 changes: 29 additions & 1 deletion include/gproshan/raytracing/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ struct t_eval_hit
{
index_t primID = NIL;
int illum = 1;
T dist = 0;
T u = 0, v = 0;
T dist = 0;
T heatmap = 0;
vec<T, 3> position;
vec<T, 3> normal;
vec<T, 3> Ka = 1;
Expand All @@ -101,6 +102,7 @@ struct t_eval_hit
Kd = {T(mesh.VC[aprimID].r), T(mesh.VC[aprimID].g), T(mesh.VC[aprimID].b)};
Kd /= 255;
normal = mesh.VN[aprimID];
heatmap = mesh.VHC[aprimID];
return;
}

Expand All @@ -116,6 +118,7 @@ struct t_eval_hit

Kd = ((1.f - u - v) * ca + u * cb + v * cc) / 255;
normal = (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c];
heatmap = (1.f - u - v) * mesh.VHC[a] + u * mesh.VHC[b] + v * mesh.VHC[c];

if(!sc.trig_mat) return;
if(sc.trig_mat[primID] == NIL) return;
Expand Down Expand Up @@ -263,6 +266,31 @@ vec<T, 3> ray_view_dir( const uvec2 & pos,
}


template <class H>
__host_device__
index_t closest_hit_vertex(const CHE & mesh, const H & hit)
{
if(!mesh.n_trigs) return hit.primID;

index_t he = 0;
real_t w = 1 - hit.u - hit.v;

if(w < hit.u)
{
he = 1;
w = hit.u;
}

if(w < hit.v)
{
he = 2;
w = hit.v;
}

return mesh.VT[hit.primID * 3 + he];
}


} // namespace gproshan

#endif // RT_UTILS_H
Expand Down
2 changes: 1 addition & 1 deletion include/gproshan/scenes/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace gproshan {


che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos);
che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos, const bool dist_error = false);

che * scanner_ptx_jpg(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos, const std::string & file_jpg = "");

Expand Down
19 changes: 19 additions & 0 deletions include/gproshan/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ void copy_real_t_array(double * destination, const float * source, const size_t
void copy_real_t_array(double * destination, const double * source, const size_t n_elem);


template <class T>
T normalize(T * data, const size_t n_elem)
{
T max = 0;

#pragma omp parallel for reduction(std::max: max)
for(index_t i = 0; i < n_elem; ++i)
max = std::max(max, data[i]);

if(max <= 1) return 1;

#pragma omp parallel for
for(index_t i = 0; i < n_elem; ++i)
data[i] /= max;

return max;
}


} // namespace gproshan

#endif // UTIL_H
Expand Down
9 changes: 8 additions & 1 deletion src/gproshan/app_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ bool app_viewer::process_simulate_scanner(viewer * p_view)
static const size_t n_min = 100;
static const size_t n_max = 10000;
static vertex cam_pos = {0, 0, 0};
static bool dist_error = true;
static bool save_jpg = false;

if(size(view->sphere_points) != 1)
{
Expand All @@ -209,9 +211,14 @@ bool app_viewer::process_simulate_scanner(viewer * p_view)
view->sphere_points[0] = cam_pos;
}

ImGui::Checkbox("dist_error", &dist_error);
ImGui::Checkbox("save_jpg", &save_jpg);

if(ImGui::Button("Scan"))
{
che * ptx_mesh = scanner_ptx_jpg(mesh.rt_embree, n_rows, n_cols, cam_pos, mesh->filename);
che * ptx_mesh = save_jpg ? scanner_ptx_jpg(mesh.rt_embree, n_rows, n_cols, cam_pos, mesh->filename)
: scanner_ptx(mesh.rt_embree, n_rows, n_cols, cam_pos, dist_error);

che_ptx::write_file(ptx_mesh, mesh->filename, n_rows, n_cols);
ptx_mesh->filename = mesh->filename + ".ptx";
view->add_mesh(ptx_mesh);
Expand Down
14 changes: 14 additions & 0 deletions src/gproshan/mesh/che.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ const real_t * che::heatmap_ptr() const
return VHC;
}

void che::heatmap_scale(const real_t shm)
{
scale_hm = shm;
}

real_t che::heatmap_scale(const index_t v) const
{
assert(v < n_vertices);
return scale_hm * VHC[v];
}

real_t che::heatmap(const index_t v) const
{
assert(v < n_vertices);
Expand Down Expand Up @@ -338,6 +349,7 @@ void che::update_heatmap(const real_t * hm)
}

memcpy(VHC, hm, n_vertices * sizeof(real_t));
scale_hm = normalize(VHC, n_vertices);
}

void che::update_normals()
Expand Down Expand Up @@ -1162,6 +1174,7 @@ che * che::load_mesh(const std::string & file_path)
if(extension == "ply") return new che_ply(file_path);
if(extension == "ptx") return new che_ptx(file_path);
if(extension == "xyz") return new che_xyz(file_path);
if(extension == "txt") return new che_xyz(file_path);
if(extension == "pts") return new che_pts(file_path);
if(extension == "pcd") return new che_pcd(file_path);

Expand Down Expand Up @@ -1211,6 +1224,7 @@ CHE::CHE(const che * mesh)
GT = mesh->GT;
VN = mesh->VN;
VC = mesh->VC;
VHC = mesh->VHC;
VT = mesh->VT;
OT = mesh->OT;
EVT = mesh->EVT;
Expand Down
4 changes: 4 additions & 0 deletions src/gproshan/mesh/che.cu
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & norm
{
cudaMalloc(&dd_che->VC, sizeof(che::rgb_t) * h_che->n_vertices);
cudaMemcpy(dd_che->VC, h_che->VC, sizeof(che::rgb_t) * h_che->n_vertices, cudaMemcpyHostToDevice);

cudaMalloc(&dd_che->VHC, sizeof(real_t) * h_che->n_vertices);
cudaMemcpy(dd_che->VHC, h_che->VHC, sizeof(real_t) * h_che->n_vertices, cudaMemcpyHostToDevice);
}

cudaMalloc(&dd_che->VT, sizeof(index_t) * h_che->n_half_edges);
Expand All @@ -45,6 +48,7 @@ void cuda_free_CHE(CHE *& dd_che, CHE *& d_che)
cudaFree(dd_che->GT);
cudaFree(dd_che->VN);
cudaFree(dd_che->VC);
cudaFree(dd_che->VHC);
cudaFree(dd_che->VT);
cudaFree(dd_che->OT);
cudaFree(dd_che->EVT);
Expand Down
10 changes: 8 additions & 2 deletions src/gproshan/mesh/che_xyz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,30 @@ void che_xyz::read_file(const std::string & file)
float x, y, z;
unsigned char r, g, b;
size_t n;
real_t h;

std::vector<vertex> vertices;
std::vector<rgb_t> vertices_color;
std::vector<real_t> vertices_hm;

while(fgets(line, sizeof(line), fp))
{
sscanf(line, "%c", &c);
if(c == '#') continue;

n = sscanf(line, "%f %f %f %hhu %hhu %hhu", &x, &y, &z, &r, &g, &b);
n = sscanf(line, "%f %f %f %hhu %hhu %hhu %f", &x, &y, &z, &r, &g, &b, &h);
vertices.push_back({x, y, z});
vertices_color.push_back(n == 6 ? rgb_t{r, g, b} : rgb_t());
vertices_color.push_back(n > 5 ? rgb_t{r, g, b} : rgb_t());
vertices_hm.push_back(n > 6 ? h : 0.45);
}

fclose(fp);

alloc(size(vertices), 0);
memcpy(GT, vertices.data(), n_vertices * sizeof(vertex));
memcpy(VC, vertices_color.data(), n_vertices * sizeof(rgb_t));

update_heatmap(vertices_hm.data());
}

void che_xyz::write_file(const che * mesh, const std::string & file, const bool & color)
Expand All @@ -60,6 +65,7 @@ void che_xyz::write_file(const che * mesh, const std::string & file, const bool
{
const rgb_t c = mesh->rgb(i);
fprintf(fp, " %hhu %hhu %hhu", c.r, c.g, c.b);
fprintf(fp, " %f", mesh->heatmap_scale(i));
}
fprintf(fp, "\n");
}
Expand Down
21 changes: 1 addition & 20 deletions src/gproshan/raytracing/embree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,7 @@ index_t embree::closest_vertex(const vertex & org, const vertex & dir) const
ray_hit r(org, dir);
if(!intersect(r)) return NIL;

const CHE * mesh = g_meshes[r.hit.geomID];

if(!mesh->n_trigs) return r.hit.primID;

index_t he = che::mtrig * r.hit.primID;
float w = 1 - r.hit.u - r.hit.v;

if(w < r.hit.u)
{
he = che::mtrig * r.hit.primID + 1;
w = r.hit.u;
}

if(w < r.hit.v)
{
he = che::mtrig * r.hit.primID + 2;
w = r.hit.v;
}

return mesh->VT[he];
return closest_hit_vertex(*g_meshes[r.hit.geomID], r.hit);
}

eval_hit embree::intersect(const vertex & org, const vertex & dir, const bool & flat) const
Expand Down
4 changes: 2 additions & 2 deletions src/gproshan/scenes/scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using namespace cimg_library;
namespace gproshan {


che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos)
che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos, const bool dist_error)
{
che * mesh_ptx = new che(n_cols * n_rows);

Expand All @@ -40,7 +40,7 @@ che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n

mesh_ptx->point(v) = h.position;
mesh_ptx->normal(v) = h.normal;
mesh_ptx->heatmap(v) = h.dist;
mesh_ptx->heatmap(v) = dist_error ? h.dist : h.heatmap;
mesh_ptx->rgb(v) = h.Kd;
}

Expand Down

0 comments on commit 2d0c822

Please sign in to comment.