26 :
AABB(sfc->getAABB()), _n_steps({{1, 1, 1}})
28 auto [min_point, max_point] = getMinMaxPoints();
31 for (std::size_t k(0); k < 3; ++k)
33 max_point[k] += std::abs(max_point[k]) * 1e-6;
34 if (std::abs(max_point[k]) < std::numeric_limits<double>::epsilon())
36 max_point[k] = (max_point[k] - min_point[k]) * (1.0 + 1e-6);
40 Eigen::Vector3d delta = max_point - min_point;
42 if (delta[0] < std::numeric_limits<double>::epsilon())
44 const double max_delta(std::max(delta[1], delta[2]));
45 min_point[0] -= max_delta * 0.5e-3;
46 max_point[0] += max_delta * 0.5e-3;
47 delta[0] = max_point[0] - min_point[0];
50 if (delta[1] < std::numeric_limits<double>::epsilon())
52 const double max_delta(std::max(delta[0], delta[2]));
53 min_point[1] -= max_delta * 0.5e-3;
54 max_point[1] += max_delta * 0.5e-3;
55 delta[1] = max_point[1] - min_point[1];
58 if (delta[2] < std::numeric_limits<double>::epsilon())
60 const double max_delta(std::max(delta[0], delta[1]));
61 min_point[2] -= max_delta * 0.5e-3;
62 max_point[2] += max_delta * 0.5e-3;
63 delta[2] = max_point[2] - min_point[2];
69 const std::size_t n_tris(sfc->getNumberOfTriangles());
70 const std::size_t n_tris_per_cell(5);
72 Eigen::Matrix<bool, 3, 1> dim =
73 delta.array() >= std::numeric_limits<double>::epsilon();
82 auto sc_ceil = [](
double v)
83 {
return static_cast<std::size_t
>(std::ceil(v)); };
88 sc_ceil(std::cbrt(n_tris * delta[0] * delta[0] /
89 (n_tris_per_cell * delta[1] * delta[2])));
91 sc_ceil(_n_steps[0] * std::min(delta[1] / delta[0], 100.0));
93 sc_ceil(_n_steps[0] * std::min(delta[2] / delta[0], 100.0));
98 _n_steps[0] = sc_ceil(std::sqrt(n_tris * delta[0] /
99 (n_tris_per_cell * delta[2])));
100 _n_steps[2] = sc_ceil(_n_steps[0] * delta[2] / delta[0]);
102 else if (dim[0] && dim[1])
104 _n_steps[0] = sc_ceil(std::sqrt(n_tris * delta[0] /
105 (n_tris_per_cell * delta[1])));
106 _n_steps[1] = sc_ceil(_n_steps[0] * delta[1] / delta[0]);
108 else if (dim[1] && dim[2])
110 _n_steps[1] = sc_ceil(std::sqrt(n_tris * delta[1] /
111 (n_tris_per_cell * delta[2])));
113 sc_ceil(n_tris * delta[2] / (n_tris_per_cell * delta[1]));
117 for (std::size_t k(0); k < 3; ++k)
122 sc_ceil(
static_cast<double>(n_tris) / n_tris_per_cell);
128 for (std::size_t k(0); k < 3; k++)
130 _step_sizes[k] = delta[k] / _n_steps[k];
131 if (delta[k] > std::numeric_limits<double>::epsilon())
133 _inverse_step_sizes[k] = 1.0 / _step_sizes[k];
137 _inverse_step_sizes[k] = 0;
141 _triangles_in_grid_box.resize(_n_steps[0] * _n_steps[1] * _n_steps[2]);
142 sortTrianglesInGridCells(sfc);
151 Point const& p0(*((*sfc)[l]->getPoint(0)));
152 Point const& p1(*((*sfc)[l]->getPoint(1)));
153 Point const& p2(*((*sfc)[l]->getPoint(2)));
156 "Sorting triangle {:d} [({:f},{:f},{:f}), ({:f},{:f},{:f}), "
157 "({:f},{:f},{:f}) into grid. Bounding box is [{:f}, {:f}] x "
158 "[{:f}, {:f}] x [{:f}, {:f}].",
159 l, p0[0], p0[1], p0[2], p1[0], p1[1], p1[2], p2[0], p2[1],
160 p2[2], min[0], max[0], min[1], max[1], min[2], max[2]);
168 std::optional<std::array<std::size_t, 3>
const> c_p0(
174 std::optional<std::array<std::size_t, 3>
const> c_p1(
180 std::optional<std::array<std::size_t, 3>
const> c_p2(
188 std::size_t
const i_min(
189 std::min(std::min((*c_p0)[0], (*c_p1)[0]), (*c_p2)[0]));
190 std::size_t
const i_max(
191 std::max(std::max((*c_p0)[0], (*c_p1)[0]), (*c_p2)[0]));
192 std::size_t
const j_min(
193 std::min(std::min((*c_p0)[1], (*c_p1)[1]), (*c_p2)[1]));
194 std::size_t
const j_max(
195 std::max(std::max((*c_p0)[1], (*c_p1)[1]), (*c_p2)[1]));
196 std::size_t
const k_min(
197 std::min(std::min((*c_p0)[2], (*c_p1)[2]), (*c_p2)[2]));
198 std::size_t
const k_max(
199 std::max(std::max((*c_p0)[2], (*c_p1)[2]), (*c_p2)[2]));
204 for (std::size_t i(i_min); i <= i_max; i++)
206 for (std::size_t j(j_min); j <= j_max; j++)
208 for (std::size_t k(k_min); k <= k_max; k++)
211 .push_back(triangle);