Easy3D 2.6.1
Loading...
Searching...
No Matches
triangle_mesh_kdtree.h
1/********************************************************************
2 * Copyright (C) 2020-2021 by Liangliang Nan <liangliang.nan@gmail.com>
3 * Copyright (C) 2011-2020 the Polygon Mesh Processing Library developers.
4 *
5 * The code in this file is adapted from the PMP (Polygon Mesh Processing
6 * Library) with modifications.
7 * https://github.com/pmp-library/pmp-library
8 * The original code was distributed under a MIT-style license, see
9 * https://github.com/pmp-library/pmp-library/blob/master/LICENSE.txt
10 ********************************************************************/
11
12#ifndef EASY3D_ALGO_TRIANGLE_MESH_KDTREE_H
13#define EASY3D_ALGO_TRIANGLE_MESH_KDTREE_H
14
15
16#include <easy3d/core/surface_mesh.h>
17#include <vector>
18
19
20namespace easy3d {
21
25 public:
32 explicit TriangleMeshKdTree(const SurfaceMesh *mesh, unsigned int max_faces = 10, unsigned int max_depth = 30);
36 ~TriangleMeshKdTree() { delete root_; }
37
40 float dist;
43 int tests;
44 };
45
51 NearestNeighbor nearest(const vec3 &p) const;
52
53 private:
54 // triangle stores corners and face handle
55 struct Triangle {
56 Triangle() = default;
57
58 Triangle(const vec3 &x0, const vec3 &x1, const vec3 &x2, SurfaceMesh::Face ff) {
59 x[0] = x0;
60 x[1] = x1;
61 x[2] = x2;
62 f = ff;
63 }
64
65 vec3 x[3];
67 };
68
69 // vector of Triangle
70 typedef std::vector<Triangle> Triangles;
71
72 // Node of the tree: contains parent, children and splitting plane
73 struct Node {
74 Node() : faces(nullptr), left_child(nullptr), right_child(nullptr) {};
75
76 ~Node() {
77 delete faces;
78 delete left_child;
79 delete right_child;
80 }
81
82 unsigned char axis;
83 float split;
84 Triangles *faces;
85 Node *left_child;
86 Node *right_child;
87 };
88
89 // Recursive part of build()
90 unsigned int build_recurse(Node *node, unsigned int max_handles, unsigned int depth);
91
92 // Recursive part of nearest()
93 void nearest_recurse(Node *node, const vec3 &point, NearestNeighbor &data) const;
94
95 private:
96 Node *root_;
97 };
98
99} // namespace easy3d
100
101
102#endif // EASY3D_ALGO_TRIANGLE_MESH_KDTREE_H
A halfedge data structure for polygonal meshes of 2-manifold.
Definition surface_mesh.h:51
~TriangleMeshKdTree()
Destructor.
Definition triangle_mesh_kdtree.h:36
TriangleMeshKdTree(const SurfaceMesh *mesh, unsigned int max_faces=10, unsigned int max_depth=30)
Construct with mesh.
Definition triangle_mesh_kdtree.cpp:21
NearestNeighbor nearest(const vec3 &p) const
Return handle of the nearest neighbor.
Definition triangle_mesh_kdtree.cpp:160
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
Definition surface_mesh.h:191
Nearest neighbor information.
Definition triangle_mesh_kdtree.h:39
float dist
distance to the nearest neighbor
Definition triangle_mesh_kdtree.h:40
vec3 nearest
nearest point on the face
Definition triangle_mesh_kdtree.h:42
SurfaceMesh::Face face
face handle of the nearest neighbor
Definition triangle_mesh_kdtree.h:41