VoxelLine.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Eigen/Core>
4 
5 namespace visionx
6 {
7 
8  /**
9  * @brief A line of voxels, from start voxel to an end voxel.
10  *
11  * Voxels are represented by their 3D integer index.
12  *
13  * Usage:
14  *
15  * (1) (a) Construct a VoxelLine line.
16  * (b) while !line.finished(), call line.next().
17  *
18  * (2) Call VoxelLine::getLineVoxels() to get all voxels at once.
19  *
20  *
21  * Uses Bresenham's algorithm for line drawing in 3D (i.e. drawing voxels).
22  *
23  * Implementation based on:
24  * https://www.geeksforgeeks.org/bresenhams-algorithm-for-3-d-line-drawing/
25  */
26  class VoxelLine
27  {
28 
29  public:
30  /// Construct an empty voxel line.
31  VoxelLine();
32 
33  /**
34  * @brief Construct a voxel line from start to end.
35  *
36  * @param start the start voxel
37  * @param end the end voxel
38  * @param includeStart if true, the start voxel is included in the result list
39  * @param includeEnd if true, the end voxel is included in the result list
40  *
41  * If `start` and `end` are equal and any of `includeStart` or
42  * `includeEnd` is true, the point is included.
43  */
44  VoxelLine(const Eigen::Vector3i& start,
45  const Eigen::Vector3i& end,
46  bool includeStart = true,
47  bool includeEnd = true);
48 
49 
50  /// Indicate whether there are more voxels on the line.
51  bool finished() const;
52 
53  /// Get the next voxel.
54  /// @throw `std::logic_error` if there are no more voxels (`this->finished()`)
55  Eigen::Vector3i next();
56 
57 
58  /// Get the voxels indices of the line from start to end.
59  /// @see `VoxelLine::VoxelLine()` (constructor)
60  static std::vector<Eigen::Vector3i> getLineVoxels(const Eigen::Vector3i& start,
61  const Eigen::Vector3i& end,
62  bool includeStart = true,
63  bool includeEnd = true);
64 
65 
66  private:
67  using Index = Eigen::Vector3i::Index;
68  static const Eigen::IOFormat iof;
69 
70 
71  void init();
72  void advance();
73 
74 
75  // INPUT
76  /// Start voxel.
77  Eigen::Vector3i start;
78  /// End voxel.
79  Eigen::Vector3i end;
80 
81  /// Whether to include the start voxel.
82  bool includeStart;
83  /// Whether to include the end voxel.
84  bool includeEnd;
85 
86 
87  // COMPUTATION
88 
89  Eigen::Vector3i delta; ///< The cwise absolute (end - start).
90  Eigen::Vector3i step; ///< Step directions (+1/-1).
91 
92  Index x; ///< The driving axis.
93  Index y, z; ///< The two non-driving axes.
94  int py, pz; ///< Slope error terms.
95 
96  ///< The current voxel (most recently returned).
97  Eigen::Vector3i _next;
98  bool _finished = false;
99  };
100 
101 } // namespace visionx
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::VoxelLine
A line of voxels, from start voxel to an end voxel.
Definition: VoxelLine.h:26
visionx::VoxelLine::getLineVoxels
static std::vector< Eigen::Vector3i > getLineVoxels(const Eigen::Vector3i &start, const Eigen::Vector3i &end, bool includeStart=true, bool includeEnd=true)
Get the voxels indices of the line from start to end.
Definition: VoxelLine.cpp:110
visionx::VoxelLine::next
Eigen::Vector3i next()
Get the next voxel.
Definition: VoxelLine.cpp:96
visionx::VoxelLine::VoxelLine
VoxelLine()
Construct an empty voxel line.
Definition: VoxelLine.cpp:10
visionx::VoxelLine::finished
bool finished() const
Indicate whether there are more voxels on the line.
Definition: VoxelLine.cpp:90