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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
// Copyright (c) 2022-2024 INRIA Sophia-Antipolis (France), GeometryFactory (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/Isosurfacing_3/include/CGAL/Isosurfacing_3/Cartesian_grid_3.h $
// $Id: include/CGAL/Isosurfacing_3/Cartesian_grid_3.h 08b27d3db14 $
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s) : Julian Stahl
// Mael Rouxel-Labbé
#ifndef CGAL_ISOSURFACING_3_CARTESIAN_GRID_3_H
#define CGAL_ISOSURFACING_3_CARTESIAN_GRID_3_H
#include <CGAL/license/Isosurfacing_3.h>
#include <CGAL/Isosurfacing_3/internal/partition_traits_Cartesian_grid.h>
#include <CGAL/Isosurfacing_3/IO/OBJ.h>
#include <CGAL/assertions.h>
#include <CGAL/boost/graph/named_params_helper.h>
#include <CGAL/Named_function_parameters.h>
#include <array>
#include <type_traits>
namespace CGAL {
namespace Isosurfacing {
/**
* \ingroup IS_Partitions_helpers_grp
*
* A policy to choose whether grid vertex locations should be cached, or recomputed at each access.
*
* \tparam Tag a tag that is either `Tag_true` (locations are cached) or `Tag_false` (locations are not cached).
*/
template <typename Tag>
struct Grid_vertex_memory_policy : public Tag { };
/**
* \ingroup IS_Partitions_helpers_grp
*
* A convenience alias for the policy that caches grid vertex locations.
*/
using Cache_vertex_locations = Grid_vertex_memory_policy<Tag_true>;
/**
* \ingroup IS_Partitions_helpers_grp
*
* A convenience alias for the policy that does not cache grid vertex locations.
*/
using Do_not_cache_vertex_locations = Grid_vertex_memory_policy<Tag_false>;
namespace internal {
template <typename GeomTraits,
typename MemoryPolicy = Do_not_cache_vertex_locations>
struct Cartesian_grid_location
{
using Point_3 = typename GeomTraits::Point_3;
using Vector_3 = typename GeomTraits::Vector_3;
using Iso_cuboid_3 = typename GeomTraits::Iso_cuboid_3;
Cartesian_grid_location() { } // just for compilation
Cartesian_grid_location(const Iso_cuboid_3& /*span*/,
const std::array<std::size_t, 3>& /*dims*/,
const Vector_3& /*spacing*/)
{ }
template <typename Grid>
Point_3 operator()(const std::size_t i,
const std::size_t j,
const std::size_t k,
const Grid& g) const
{
typename GeomTraits::Compute_x_3 x_coord = g.geom_traits().compute_x_3_object();
typename GeomTraits::Compute_y_3 y_coord = g.geom_traits().compute_y_3_object();
typename GeomTraits::Compute_z_3 z_coord = g.geom_traits().compute_z_3_object();
typename GeomTraits::Construct_point_3 point = g.geom_traits().construct_point_3_object();
typename GeomTraits::Construct_vertex_3 vertex = g.geom_traits().construct_vertex_3_object();
const Point_3& min_p = vertex(g.span(), 0);
return point(x_coord(min_p) + typename GeomTraits::FT(i) * g.spacing()[0],
y_coord(min_p) + typename GeomTraits::FT(j) * g.spacing()[1],
z_coord(min_p) + typename GeomTraits::FT(k) * g.spacing()[2]);
}
};
template <typename GeomTraits>
struct Cartesian_grid_location<GeomTraits, Cache_vertex_locations>
{
using Point_3 = typename GeomTraits::Point_3;
using Vector_3 = typename GeomTraits::Vector_3;
using Iso_cuboid_3 = typename GeomTraits::Iso_cuboid_3;
std::vector<Point_3> m_points;
Cartesian_grid_location() { } // just for compilation
Cartesian_grid_location(const Iso_cuboid_3& span,
const std::array<std::size_t, 3>& dims,
const Vector_3& spacing)
{
m_points.reserve(dims[0] * dims[1] * dims[2]);
for(std::size_t k=0; k<dims[2]; ++k)
for(std::size_t j=0; j<dims[1]; ++j)
for(std::size_t i=0; i<dims[0]; ++i)
m_points.emplace_back((span.min)() + Vector_3(i * spacing[0], j * spacing[1], k * spacing[2]));
}
template <typename Grid>
const Point_3& operator()(const std::size_t i,
const std::size_t j,
const std::size_t k,
const Grid& g) const
{
const std::size_t linear_index = g.linear_index(i, j, k);
CGAL_precondition(linear_index < m_points.size());
return m_points[linear_index];
}
};
} // namespace internal
/**
* \ingroup IS_Partitions_grp
*
* \cgalModels{IsosurfacingPartition_3}
*
* \brief The class `Cartesian_grid_3` represents a 3D %Cartesian grid, that is the partition of
* an iso-cuboid into identical iso-cuboidal cells.
*
* The class `Cartesian_grid_3` is one of the possible space partitioning data structures
* that can be used along with value and gradient fields to make up a domain.
*
* \tparam GeomTraits must be a model of `IsosurfacingTraits_3`.
* \tparam MemoryPolicy whether the geometric locations of the grid vertices are stored or not.
* Possible values are `CGAL::Isosurfacing::Cache_vertex_locations` and `CGAL::Isosurfacing::Do_not_cache_vertex_locations`.
*
* \sa `CGAL::Isosurfacing::Marching_cubes_domain_3()`
* \sa `CGAL::Isosurfacing::Dual_contouring_domain_3()`
*/
template <typename GeomTraits,
typename MemoryPolicy = Do_not_cache_vertex_locations>
class Cartesian_grid_3
{
public:
using Geom_traits = GeomTraits;
using FT = typename Geom_traits::FT;
using Point_3 = typename Geom_traits::Point_3;
using Vector_3 = typename Geom_traits::Vector_3;
using Iso_cuboid_3 = typename Geom_traits::Iso_cuboid_3;
using Positioner = internal::Cartesian_grid_location<GeomTraits, MemoryPolicy>;
private:
Iso_cuboid_3 m_span;
std::array<std::size_t, 3> m_dims;
Vector_3 m_spacing;
Positioner m_positioner;
Geom_traits m_gt;
private:
void initialize_spacing()
{
typename Geom_traits::Compute_x_3 x_coord = m_gt.compute_x_3_object();
typename Geom_traits::Compute_y_3 y_coord = m_gt.compute_y_3_object();
typename Geom_traits::Compute_z_3 z_coord = m_gt.compute_z_3_object();
typename Geom_traits::Construct_vector_3 vector = m_gt.construct_vector_3_object();
typename Geom_traits::Construct_vertex_3 vertex = m_gt.construct_vertex_3_object();
// calculate grid spacing
const Point_3& min_p = vertex(m_span, 0);
const Point_3& max_p = vertex(m_span, 7);
const FT x_span = x_coord(max_p) - x_coord(min_p);
const FT y_span = y_coord(max_p) - y_coord(min_p);
const FT z_span = z_coord(max_p) - z_coord(min_p);
const FT d_x = x_span / FT(m_dims[0] - 1);
const FT d_y = y_span / FT(m_dims[1] - 1);
const FT d_z = z_span / FT(m_dims[2] - 1);
m_spacing = vector(d_x, d_y, d_z);
m_positioner = Positioner { m_span, m_dims, m_spacing };
}
void initialize_dimensions()
{
typename Geom_traits::Compute_x_3 x_coord = m_gt.compute_x_3_object();
typename Geom_traits::Compute_y_3 y_coord = m_gt.compute_y_3_object();
typename Geom_traits::Compute_z_3 z_coord = m_gt.compute_z_3_object();
typename Geom_traits::Construct_vertex_3 vertex = m_gt.construct_vertex_3_object();
const Point_3& min_p = vertex(m_span, 0);
const Point_3& max_p = vertex(m_span, 7);
const FT x_span = x_coord(max_p) - x_coord(min_p);
const FT y_span = y_coord(max_p) - y_coord(min_p);
const FT z_span = z_coord(max_p) - z_coord(min_p);
m_dims[0] = static_cast<std::size_t>(std::ceil(CGAL::to_double(x_span / m_spacing[0]))) + 1;
m_dims[1] = static_cast<std::size_t>(std::ceil(CGAL::to_double(y_span / m_spacing[1]))) + 1;
m_dims[2] = static_cast<std::size_t>(std::ceil(CGAL::to_double(z_span / m_spacing[2]))) + 1;
m_positioner = Positioner { m_span, m_dims, m_spacing };
}
public:
/*!
* \brief Default constructor
*/
Cartesian_grid_3()
: m_span{Point_3{0, 0, 0}, Point_3{0, 0, 0}},
m_dims{2, 2, 2},
m_spacing{0, 0, 0},
m_gt{Geom_traits()}
{ }
/**
* \brief creates a %Cartesian grid with `dimensions[0]*dimensions[1]*dimensions[2]` grid vertices.
*
* The grid covers the space described by the iso-cuboid `span`.
*
* \param span the geometric span of the grid
* \param dimensions the number of grid vertices in the `x`, `y`, and `z` directions
* \param gt the geometric traits
*
* \pre all dimensions are strictly positive.
*/
Cartesian_grid_3(const Iso_cuboid_3& span,
const std::array<std::size_t, 3>& dimensions,
const Geom_traits& gt = Geom_traits())
: m_span{span},
m_dims{dimensions},
m_gt{gt}
{
initialize_spacing();
}
/**
* \brief creates a %Cartesian grid with `dimensions[0]*dimensions[1]*dimensions[2]` grid vertices.
*
* The grid covers the space described by the iso-cuboid span,
* itself described through two diagonal corners `p` and `q`.
*
* \param p the lexicographically smallest corner of the iso-cuboid
* \param q the lexicographically largest corner of the iso-cuboid
* \param dimensions the number of grid vertices in the `x`, `y`, and `z` directions
* \param gt the geometric traits
*
* \pre `p` is lexicographically strictly smaller than `q`
* \pre all dimensions are strictly positive.
*/
Cartesian_grid_3(const Point_3& p, const Point_3& q,
const std::array<std::size_t, 3>& dimensions,
const Geom_traits& gt = Geom_traits())
: Cartesian_grid_3{Iso_cuboid_3{p, q}, dimensions, gt}
{ }
/**
* \brief creates a %Cartesian grid using a prescribed grid step `spacing`.
*
* The grid covers the space described by the iso-cuboid `span`.
*
* \param span the geometric span of the grid
* \param spacing the dimension of the paving cell, in the `x`, `y`, and `z` directions
* \param gt the geometric traits
*
* \pre the diagonal of `span` has length a multiple of `spacing`
*/
Cartesian_grid_3(const Iso_cuboid_3& span,
const Vector_3& spacing,
const Geom_traits& gt = Geom_traits())
: m_span{span},
m_spacing{spacing},
m_gt{gt}
{
initialize_dimensions();
}
/**
* \brief creates a %Cartesian grid using a prescribed grid step.
*
* The grid covers the space described by an iso-cuboid, itself described through two diagonal corners.
*
* \param p the lexicographically smallest corner of the iso-cuboid
* \param q the lexicographically largest corner of the iso-cuboid
* \param spacing the dimension of the paving cell, in the `x`, `y`, and `z` directions, respectively.
* \param gt the geometric traits
*
* \pre `p` is lexicographically strictly smaller than `q`
* \pre the diagonal of the iso-cuboid has length a multiple of `spacing`
*/
Cartesian_grid_3(const Point_3& p, const Point_3& q,
const Vector_3& spacing,
const Geom_traits& gt = Geom_traits())
: Cartesian_grid_3{Iso_cuboid_3{p, q}, spacing, gt}
{ }
public:
/**
* returns the geometric traits class.
*/
const Geom_traits& geom_traits() const
{
return m_gt;
}
/**
* returns an iso-cuboid representing the geometric span of the %Cartesian grid.
*/
const Iso_cuboid_3& span() const { return m_span; }
/**
* returns the number of grid vertices in the `x` direction.
*/
std::size_t xdim() const { return m_dims[0]; }
/**
* returns the number of grid vertices in the `y` direction.
*/
std::size_t ydim() const { return m_dims[1]; }
/**
* returns the number of grid vertices in the `z` direction.
*/
std::size_t zdim() const { return m_dims[2]; }
/**
* returns the spacing of the %Cartesian grid, that is a vector whose coordinates are
* the grid steps in the `x`, `y`, and `z` directions, respectively.
*/
const Vector_3& spacing() const { return m_spacing; }
public:
/**
* returns the index of a grid cell given its indices (i.e., `(k * y_dim + j) * x_dim + i`).
*/
std::size_t linear_index(const std::size_t i,
const std::size_t j,
const std::size_t k) const
{
CGAL_precondition(i < m_dims[0] && j < m_dims[1] && k < m_dims[2]);
return (k * m_dims[1] + j) * m_dims[0] + i;
}
public:
/**
* \brief returns the coordinates of the grid cell that contains a given point.
*
* For points on the boundary between two cells, the smaller index is returned.
*
* \param p the point to be located
*
* \pre `p` is inside the grid.
*/
std::array<std::size_t, 3> index(const Point_3& p) const
{
typename Geom_traits::Compute_x_3 x_coord = m_gt.compute_x_3_object();
typename Geom_traits::Compute_y_3 y_coord = m_gt.compute_y_3_object();
typename Geom_traits::Compute_z_3 z_coord = m_gt.compute_z_3_object();
typename Geom_traits::Construct_vertex_3 vertex = m_gt.construct_vertex_3_object();
const Point_3& min_p = vertex(m_span, 0);
std::size_t i = std::size_t((x_coord(p) - x_coord(min_p)) / x_coord(m_spacing));
std::size_t j = std::size_t((y_coord(p) - y_coord(min_p)) / y_coord(m_spacing));
std::size_t k = std::size_t((z_coord(p) - z_coord(min_p)) / z_coord(m_spacing));
if(i == xdim() - 1)
--i;
if(j == ydim() - 1)
--j;
if(k == zdim() - 1)
--k;
return {i, j, k};
}
// Geometry
public:
/**
* \brief returns the geometric location of the grid vertex described by its three indices.
*
* Depending on the value of the template parameter `MemoryPolicy`, locations might not be stored
* but calculated on-the-fly.
*
* \param i the index in the `x` direction
* \param j the index in the `y` direction
* \param k the index in the `z` direction
*
* \pre `i < xdim()` and `j < ydim()` and `k < zdim()`
*
* \returns a const reference or a newly constructed point, depending on the caching policy.
*/
decltype(auto) /*Point_3*/ point(const std::size_t i,
const std::size_t j,
const std::size_t k) const
{
return m_positioner(i, j, k, *this);
}
};
} // namespace Isosurfacing
} // namespace CGAL
#endif // CGAL_ISOSURFACING_3_CARTESIAN_GRID_3_H
|