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 Sep 21, 2023
2 parents a96b203 + 3cc8157 commit 8a93e54
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 91 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
sudo apt update
sudo apt install \
libarmadillo-dev \
libflann-dev \
libeigen3-dev \
libcgal-dev \
libsuitesparse-dev \
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ find_package(Armadillo REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(CGAL REQUIRED)
find_package(SuiteSparse REQUIRED)
find_package(flann REQUIRED)


include_directories(SYSTEM ${embree_INCLUDE_DIRS})
Expand All @@ -68,6 +69,7 @@ include_directories(SYSTEM ${AMADILLO_INCLUDE_DIR})
include_directories(SYSTEM ${EIGEN3_INCLUDE_DIRS})
include_directories(SYSTEM ${CGAL_INCLUDE_DIRS})
include_directories(SYSTEM ${SuiteSparse_INCLUDE_DIRS})
include_directories(SYSTEM ${flann_INCLUDE_DIRS})


add_subdirectory(src) # gproshan library
Expand Down
31 changes: 0 additions & 31 deletions include/gproshan/pointcloud/kdtree.h

This file was deleted.

27 changes: 21 additions & 6 deletions include/gproshan/pointcloud/knn.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <vector>
#include <unordered_map>

#include <flann/flann.hpp>


inline gproshan::uvec3 hash(gproshan::vec3 p, const float & res)
{
Expand All @@ -26,24 +28,37 @@ struct std::hash<gproshan::uvec3>


// geometry processing and shape analysis framework
namespace gproshan {
namespace gproshan::knn {


using point = vec3;


class grid_knn // grid
class grid
{
private:
float res = 1000;
std::vector<point> points;
std::unordered_map<uvec3, std::vector<index_t> > grid;
std::unordered_map<uvec3, std::vector<index_t> > voxels;

public:
grid(const point * pc, const size_t & n_points, const mat4 & transform);
~grid() = default;

std::vector<index_t> operator () (const point & p, int k) const;
};


class k3tree
{
private:
flann::Matrix<int> indices;

public:
grid_knn(const point * pc, const size_t & n_points, const mat4 & transform);
virtual ~grid_knn() = default;
k3tree(const point * pc, const size_t & n_points, const size_t & k = 8, const std::vector<point> & query = {});
~k3tree();

std::vector<index_t> operator () (const point & p, int knn);
int * operator () (const index_t & i) const;
};


Expand Down
6 changes: 6 additions & 0 deletions include/gproshan/viewer/viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
// geometry processing and shape analysis framework
namespace gproshan {

const size_t max_nframes = 1000;

class viewer
{
Expand Down Expand Up @@ -80,6 +81,8 @@ class viewer
quaternion cam_light;

double render_time = 0;
double frametime[max_nframes] = {};
index_t nframes = 0;

std::vector<che_viewer *> meshes;
index_t idx_selected_mesh = 0;
Expand Down Expand Up @@ -121,6 +124,9 @@ class viewer
void render_gl();
void render_rt(che_viewer & mesh, frame & rt_frame);

void save_history(const std::string & file);
void save_frametime(const std::string & file);

static void framebuffer_size_callback(GLFWwindow * window, int width, int height);
static void window_size_callback(GLFWwindow * window, int width, int height);
static void keyboard_callback(GLFWwindow * window, int key, int scancode, int action, int mods);
Expand Down
1 change: 1 addition & 0 deletions src/gproshan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target_link_libraries(gproshan X11::X11)
target_link_libraries(gproshan ${ARMADILLO_LIBRARIES})
target_link_libraries(gproshan CGAL::CGAL)
target_link_libraries(gproshan ${SuiteSparse_LIBRARIES})
target_link_libraries(gproshan flann::flann_cpp)
target_link_libraries(gproshan ${OptiX_LIBRARY})
target_link_libraries(gproshan imgui)

Expand Down
44 changes: 38 additions & 6 deletions src/gproshan/app_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,48 @@ bool app_viewer::process_knn(viewer * p_view)
app_viewer * view = (app_viewer *) p_view;
che_viewer & mesh = view->selected_mesh();

grid_knn knn(&mesh->point(0), mesh->n_vertices, mesh.model_mat);
static int alg = 0;
static int k = 9;
ImGui::Combo("algorithm", &alg, "select\0grid\0k3tree\0\0");
ImGui::InputInt("k", &k);

if(mesh.selected.size())
if(ImGui::Button("Run"))
{
const index_t & p = mesh.selected.back();
for(const index_t & v: knn(mesh.model_mat * (mesh->point(p), 1), 9))
mesh.selected.push_back(v);
auto query = mesh.selected;
if(!query.size()) query.push_back(0);

mesh.selected.clear();

switch(alg)
{
case 1:
{
knn::grid grid(&mesh->point(0), mesh->n_vertices, mesh.model_mat);
for(const index_t & p: query)
{
for(const index_t & v: grid(mesh.model_mat * (mesh->point(p), 1), k))
mesh.selected.push_back(v);
}
}
break;

case 2:
{
knn::k3tree k3tree(&mesh->point(0), mesh->n_vertices, k);
for(const index_t & p: query)
{
const int * result = k3tree(p);
for(index_t i = 0; i < 8; ++i)
mesh.selected.push_back(result[i]);
}
}
break;

default: break;
}
}

return false;
return true;
}

bool app_viewer::process_compute_normals(viewer * p_view)
Expand Down
33 changes: 0 additions & 33 deletions src/gproshan/pointcloud/kdtree.cpp

This file was deleted.

70 changes: 57 additions & 13 deletions src/gproshan/pointcloud/knn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@


// geometry processing and shape analysis framework
namespace gproshan {
namespace gproshan::knn {


grid_knn::grid_knn(const point * pc, const size_t & n_points, const mat4 & transform): points(n_points)
grid::grid(const point * pc, const size_t & n_points, const mat4 & transform): points(n_points)
{
double build_time = 0;

Expand All @@ -21,19 +21,19 @@ grid_knn::grid_knn(const point * pc, const size_t & n_points, const mat4 & trans
point & p = points[i];
p = transform * (pc[i], 1);

grid[hash(p, res)].push_back(i);
voxels[hash(p, res)].push_back(i);
}

TOC(build_time);

gproshan_log_var(sizeof(size_t));
gproshan_log_var(build_time);
gproshan_log_var(res);
gproshan_log_var(grid.size());
gproshan_log_var(double(n_points) / grid.size());
gproshan_log_var(voxels.size());
gproshan_log_var(double(n_points) / voxels.size());
}

std::vector<index_t> grid_knn::operator () (const point & p, int knn)
std::vector<index_t> grid::operator () (const point & p, int k) const
{
const uvec3 key = hash(p, res);

Expand All @@ -43,32 +43,76 @@ std::vector<index_t> grid_knn::operator () (const point & p, int knn)
for(int j = -1; j < 2; ++j)
for(int k = -1; k < 2; ++k)
{
const uvec3 cell = {key.x() + i, key.y() + j, key.z() + k};
const uvec3 pos = {key.x() + i, key.y() + j, key.z() + k};

if(cell.x() == NIL || cell.y() == NIL || cell.z() == NIL)
if(pos.x() == NIL || pos.y() == NIL || pos.z() == NIL)
continue;

if(grid.find(cell) == grid.end())
const auto & iter = voxels.find(pos);
if(iter == voxels.end())
continue;

for(const index_t & v: grid[cell])
for(const index_t & v: iter->second)
q.push({-length(p - points[v]), v});
}

std::vector<index_t> nn;
nn.reserve(knn);
nn.reserve(k);

while(!q.empty() && knn)
while(!q.empty() && k)
{
nn.push_back(q.top().second);
q.pop();

--knn;
--k;
}

return nn;
}


///< Implementation using flann, by default compute all knn
k3tree::k3tree(const point * pc, const size_t & n_points, const size_t & k, const std::vector<point> & query)
{
double time_build, time_query;

TIC(time_build);
flann::Matrix<real_t> mpc((real_t *) pc, n_points, 3);
flann::Index<flann::L2<real_t> > index(mpc, flann::KDTreeSingleIndexParams());
index.buildIndex();
TOC(time_build);
gproshan_log_var(time_build);

TIC(time_query);
const point * q = query.size() ? query.data() : pc;
const size_t & n_results = query.size() ? query.size() : n_points;

flann::Matrix<real_t> mq((real_t *) q, n_results, 3);

indices = flann::Matrix(new int[n_results * k], n_results, k);
flann::Matrix dists(new real_t[n_results * k], n_results, k);

flann::SearchParams params;
params.cores = 16;
index.knnSearch(mq, indices, dists, k, params);
TOC(time_query);
gproshan_log_var(time_query);

gproshan_log_var(time_build + time_query);

delete [] dists.ptr();
}

k3tree::~k3tree()
{
delete [] indices.ptr();
}

int * k3tree::operator () (const index_t & i) const
{
return indices[i];
}


} // namespace gproshan

Loading

0 comments on commit 8a93e54

Please sign in to comment.