Easy3D 2.5.3
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:
27 explicit TriangleMeshKdTree(const SurfaceMesh *mesh, unsigned int max_faces = 10, unsigned int max_depth = 30);
28
29 ~TriangleMeshKdTree() { delete root_; }
30
33 float dist;
35 vec3 nearest;
36 int tests;
37 };
38
40 NearestNeighbor nearest(const vec3 &p) const;
41
42 private:
43 // triangle stores corners and face handle
44 struct Triangle {
45 Triangle() = default;
46
47 Triangle(const vec3 &x0, const vec3 &x1, const vec3 &x2, SurfaceMesh::Face ff) {
48 x[0] = x0;
49 x[1] = x1;
50 x[2] = x2;
51 f = ff;
52 }
53
54 vec3 x[3];
56 };
57
58 // vector of Triangle
59 typedef std::vector<Triangle> Triangles;
60
61 // Node of the tree: contains parent, children and splitting plane
62 struct Node {
63 Node() : faces(nullptr), left_child(nullptr), right_child(nullptr) {};
64
65 ~Node() {
66 delete faces;
67 delete left_child;
68 delete right_child;
69 }
70
71 unsigned char axis;
72 float split;
73 Triangles *faces;
74 Node *left_child;
75 Node *right_child;
76 };
77
78 // Recursive part of build()
79 unsigned int build_recurse(Node *node, unsigned int max_handles, unsigned int depth);
80
81 // Recursive part of nearest()
82 void nearest_recurse(Node *node, const vec3 &point, NearestNeighbor &data) const;
83
84 private:
85 Node *root_;
86 };
87
88} // namespace easy3d
89
90
91#endif // EASY3D_ALGO_TRIANGLE_MESH_KDTREE_H
A halfedge data structure for polygonal meshes of 2-manifold.
Definition: surface_mesh.h:52
A k-d tree for triangular surface meshes.
Definition: triangle_mesh_kdtree.h:24
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
void split(const std::string &in, char separator, std::vector< std::string > &out, bool skip_empty_fields)
Splits a string into parts.
Definition: string.cpp:24
Definition: collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition: types.h:45
Definition: surface_mesh.h:134
nearest neighbor information
Definition: triangle_mesh_kdtree.h:32