8 #pragma GCC diagnostic push
9 #pragma GCC diagnostic ignored "-Wpedantic"
10 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
11 #include <CGAL/convex_hull_2.h>
12 #include <CGAL/min_quadrilateral_2.h>
13 #pragma GCC diagnostic pop
15 #include <pcl/point_cloud.h>
16 #include <pcl/point_types.h>
18 #include <SimoxUtility/shapes/OrientedBox.h>
19 #include <SimoxUtility/shapes/XYConstrainedOrientedBox.h>
24 #include "../VisitPointLikeContainer.h"
36 mx.col(0) = normal.normalized();
37 const VectorT other = (mx.col(0) == VectorT::UnitZ()) ? VectorT::UnitX() : VectorT::UnitZ();
38 mx.col(1) = mx.col(0).cross(other).normalized();
39 mx.col(2) = mx.col(0).cross(mx.col(1)).normalized();
46 calculate2dOOBB(
const T& cloud,
const Eigen::Vector3d& dir)
49 const Eigen::Vector3d normal = dir.normalized();
50 using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
51 using Point_2 = Kernel::Point_2;
52 using Polygon_2 = CGAL::Polygon_2<Kernel>;
54 const Eigen::Matrix3d boxFrame = normalToRotation(normal);
57 std::vector<Point_2> allProjected;
58 double allMaxX = -std::numeric_limits<double>::infinity();
59 double allMinX = +std::numeric_limits<double>::infinity();
61 const auto& consume = [&](
auto x,
auto y,
auto z)
63 const Eigen::Vector3d t = boxFrame.transpose() * Eigen::Vector3d{x, y, z};
64 allProjected.emplace_back(t.y(), t.z());
68 const auto& resize = [&](
auto size) { allProjected.reserve(size); };
73 std::vector<Point_2> chullAll;
74 CGAL::convex_hull_2(allProjected.begin(), allProjected.end(), std::back_inserter(chullAll));
77 CGAL::min_rectangle_2(chullAll.begin(), chullAll.end(), std::back_inserter(polyAll));
78 const auto& polyps = polyAll.container();
81 <<
VAROUT(normal.transpose()) <<
'\n'
82 <<
VAROUT(boxFrame) <<
'\n'
83 <<
VAROUT(allProjected.size()) <<
'\n'
84 <<
VAROUT(chullAll.size()) <<
'\n'
85 <<
VAROUT(polyAll.container().size());
87 const auto v0 = polyps.at(0) - polyps.at(1);
88 const auto v1 = polyps.at(2) - polyps.at(1);
90 Eigen::Vector3d{allMinX, polyps.at(1).x(), polyps.at(1).y()},
91 Eigen::Vector3d{allMaxX - allMinX, 0, 0},
92 Eigen::Vector3d{0, v0.x(), v0.y()},
93 Eigen::Vector3d{0, v1.x(), v1.y()}
95 .transformed(boxFrame);
99 simox::XYConstrainedOrientedBox<double>
100 calculateXYOOBB(
const T& cloud)
102 const auto oobb = calculate2dOOBB(cloud, Eigen::Vector3d::UnitZ());
105 <<
VAROUT(oobb.transformation()) <<
'\n'
106 <<
VAROUT(oobb.dimensions()) <<
'\n'
107 <<
VAROUT(oobb.axis_x().transpose());
110 const Eigen::Vector3d e1 = oobb.extend(1);
111 const Eigen::Vector3d e2 = oobb.extend(2);
113 return {oobb.translation(), {e1(0), e1(1)}, {e2(0), e2(1)}, oobb.dimension(0)};
118 calculateDouble2dOOBB(
const T& cloud)
120 const auto box1 = calculate2dOOBB(cloud, Eigen::Vector3d::UnitZ());
125 <<
VAROUT(box1.transformation());
127 const Eigen::Vector3d
norm =
128 (box1.axis_y().norm() < box1.axis_z().norm()) ? box1.axis_y() : box1.axis_z();
130 const auto box2 = calculate2dOOBB(cloud,
norm);
133 <<
VAROUT(box2.transformation()) <<
'\n'
140 inline std::vector<Eigen::Vector3f>
143 std::vector<float> valuesX;
144 std::vector<float> valuesY;
145 std::vector<float> valuesZ;
148 const auto& consume = [&](
auto x,
auto y,
auto z)
150 const Eigen::Vector3f ted =
transform * Eigen::Vector3f{x, y, z};
151 valuesX.emplace_back(ted.x());
152 valuesY.emplace_back(ted.y());
153 valuesZ.emplace_back(ted.z());
155 const auto& resize = [&](
auto size)
158 valuesX.reserve(size);
159 valuesY.reserve(size);
160 valuesZ.reserve(size);
165 std::sort(valuesX.begin(), valuesX.end());
166 std::sort(valuesY.begin(), valuesY.end());
167 std::sort(valuesZ.begin(), valuesZ.end());
169 const std::size_t idxLo = sz * trimEachSideBy;
170 const std::size_t idxHi = sz -
std::max(idxLo, 1ul);
171 const float loX = valuesX.at(idxLo);
172 const float hiX = valuesX.at(idxHi);
173 const float loY = valuesY.at(idxLo);
174 const float hiY = valuesY.at(idxHi);
175 const float loZ = valuesZ.at(idxLo);
176 const float hiZ = valuesZ.at(idxHi);
179 std::vector<Eigen::Vector3f> result;
181 const auto& consume = [&](
auto x,
auto y,
auto z)
183 const Eigen::Vector3f ted =
transform * Eigen::Vector3f{x, y, z};
184 if (ted.x() <= hiX && ted.x() >= loX && ted.y() <= hiY && ted.y() >= loY &&
185 ted.z() <= hiZ && ted.z() >= loZ)
187 result.emplace_back(x, y, z);
190 const auto& resize = [&](
auto) {};