OGS
AnalyticalGeometry-impl.h
Go to the documentation of this file.
1
11namespace GeoLib
12{
13template <typename InputIterator>
14std::pair<Eigen::Vector3d, double> getNewellPlane(InputIterator pnts_begin,
15 InputIterator pnts_end)
16{
17 Eigen::Vector3d plane_normal({0, 0, 0});
18 Eigen::Vector3d centroid({0, 0, 0});
19 for (auto i = std::prev(pnts_end), j = pnts_begin; j != pnts_end;
20 i = j, ++j)
21 {
22 auto const& pt_i = *(*i);
23 auto const& pt_j = *(*j);
24 plane_normal[0] +=
25 (pt_i[1] - pt_j[1]) * (pt_i[2] + pt_j[2]); // projection on yz
26 plane_normal[1] +=
27 (pt_i[2] - pt_j[2]) * (pt_i[0] + pt_j[0]); // projection on xz
28 plane_normal[2] +=
29 (pt_i[0] - pt_j[0]) * (pt_i[1] + pt_j[1]); // projection on xy
30
31 centroid += pt_j.asEigenVector3d();
32 }
33
34 plane_normal.normalize();
35 auto n_pnts(std::distance(pnts_begin, pnts_end));
36 assert(n_pnts > 2);
37 double d = 0.0;
38 if (n_pnts > 0)
39 {
40 d = centroid.dot(plane_normal) / n_pnts;
41 }
42 return std::make_pair(plane_normal, d);
43}
44
45template <class T_POINT>
46std::pair<Eigen::Vector3d, double> getNewellPlane(
47 const std::vector<T_POINT*>& pnts)
48{
49 return getNewellPlane(pnts.begin(), pnts.end());
50}
51
52template <class T_POINT>
53std::pair<Eigen::Vector3d, double> getNewellPlane(
54 const std::vector<T_POINT>& pnts)
55{
56 Eigen::Vector3d plane_normal({0, 0, 0});
57 Eigen::Vector3d centroid({0, 0, 0});
58 std::size_t n_pnts(pnts.size());
59 for (std::size_t i = n_pnts - 1, j = 0; j < n_pnts; i = j, j++)
60 {
61 plane_normal[0] += (pnts[i][1] - pnts[j][1]) *
62 (pnts[i][2] + pnts[j][2]); // projection on yz
63 plane_normal[1] += (pnts[i][2] - pnts[j][2]) *
64 (pnts[i][0] + pnts[j][0]); // projection on xz
65 plane_normal[2] += (pnts[i][0] - pnts[j][0]) *
66 (pnts[i][1] + pnts[j][1]); // projection on xy
67
68 centroid += pnts[j].asEigenVector3d();
69 }
70
71 plane_normal.normalize();
72 double const d = centroid.dot(plane_normal) / n_pnts;
73 return std::make_pair(plane_normal, d);
74}
75
76template <typename InputIterator>
77void rotatePoints(Eigen::Matrix3d const& rot_mat, InputIterator pnts_begin,
78 InputIterator pnts_end)
79{
80 for (auto it = pnts_begin; it != pnts_end; ++it)
81 {
82 (*it)->asEigenVector3d() = rot_mat * (*it)->asEigenVector3d();
83 }
84}
85
86template <typename InputIterator1, typename InputIterator2>
87Eigen::Matrix3d rotatePointsToXY(InputIterator1 p_pnts_begin,
88 InputIterator1 p_pnts_end,
89 InputIterator2 r_pnts_begin,
90 InputIterator2 r_pnts_end)
91{
92 assert(std::distance(p_pnts_begin, p_pnts_end) > 2);
93
94 // compute the plane normal
95 auto const [plane_normal, d] =
96 GeoLib::getNewellPlane(p_pnts_begin, p_pnts_end);
97
98 // rotate points into x-y-plane
99 Eigen::Matrix3d const rot_mat = computeRotationMatrixToXY(plane_normal);
100 rotatePoints(rot_mat, r_pnts_begin, r_pnts_end);
101
102 for (auto it = r_pnts_begin; it != r_pnts_end; ++it)
103 {
104 (*(*it))[2] = 0.0; // should be -= d but there are numerical errors
105 }
106
107 return rot_mat;
108}
109
110template <typename P>
111void rotatePoints(Eigen::Matrix3d const& rot_mat, std::vector<P*> const& pnts)
112{
113 rotatePoints(rot_mat, pnts.begin(), pnts.end());
114}
115
116} // end namespace GeoLib
void rotatePoints(Eigen::Matrix3d const &rot_mat, InputIterator pnts_begin, InputIterator pnts_end)
Eigen::Matrix3d computeRotationMatrixToXY(Eigen::Vector3d const &n)
Eigen::Matrix3d rotatePointsToXY(InputIterator1 p_pnts_begin, InputIterator1 p_pnts_end, InputIterator2 r_pnts_begin, InputIterator2 r_pnts_end)
std::pair< Eigen::Vector3d, double > getNewellPlane(InputIterator pnts_begin, InputIterator pnts_end)