-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry_utils.cpp
38 lines (32 loc) · 955 Bytes
/
geometry_utils.cpp
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
#include "geometry_utils.h"
#include "igl/point_mesh_squared_distance.h"
Eigen::Vector3d Convert(const std::vector<double>& v)
{
if (v.size() != 3)
{
throw std::invalid_argument("the size of the vector must be 3");
}
return Eigen::Vector3d(v[0], v[1], v[2]);
}
int FindNearestVertex(const VectorArray& V, const IndicesArray& F, const Eigen::Vector3d& coordinate)
{
Eigen::VectorXd sqrD;
Eigen::VectorXi I;
Eigen::MatrixXd C;
igl::point_mesh_squared_distance(coordinate.transpose(), V, F, sqrD, I, C);
auto nearest_facet = I(0);
auto nearest_vertex_index = -1;
auto nearest_distance2 = std::numeric_limits<double>::max();
for (auto i = 0; i < 3; ++i)
{
auto v = F(nearest_facet, i);
Eigen::RowVector3d vertex = V.row(v);
auto distance2 = (vertex - coordinate.transpose()).squaredNorm();
if (distance2 < nearest_distance2)
{
nearest_vertex_index = v;
nearest_distance2 = distance2;
}
}
return nearest_vertex_index;
}