VoxelGrid.cpp
Go to the documentation of this file.
1#include "VoxelGrid.h"
2
3#include <algorithm>
4
6
7#include "../utils.h"
8
10{
11
13 {
14 setFree();
15 }
16
18 {
19 setOccupied(label);
20 }
21
22 bool
24 {
25 return !isOccupied();
26 }
27
28 bool
30 {
31 return _isOccupied;
32 }
33
34 Label
36 {
38 return _label;
39 }
40
41 void
43 {
44 _isOccupied = false;
45 _label = 0;
46 }
47
48 void
50 {
51 _isOccupied = true;
52 this->_label = label;
53 }
54
55 void
56 to_json(nlohmann::json& json, const Voxel& voxel)
57 {
58 if (voxel.isOccupied())
59 {
60 json = voxel.getLabel();
61 }
62 else
63 {
64 json = -1;
65 }
66 }
67
68 void
69 from_json(const nlohmann::json& json, Voxel& voxel)
70 {
71 if (json.get<int>() < 0)
72 {
73 voxel.setFree();
74 }
75 else
76 {
77 voxel.setOccupied(json.get<Label>());
78 }
79 }
80
81 const std::string VoxelGrid::JSON_VOXEL_ARRAY_NAME = "labels";
82
84 VoxelGrid::fromLabelDensity(const LabelDensity::VoxelGrid& density)
85 {
86 VoxelGrid grid(density.getStructure());
87 grid.setByLabelDensity(density);
88 return grid;
89 }
90
91 std::set<Label>
92 VoxelGrid::getUniqueLabels() const
93 {
94 std::set<Label> ids;
95 for (const Voxel& voxel : *this)
96 {
97 if (voxel.isOccupied())
98 {
99 ids.insert(voxel.getLabel());
100 }
101 }
102 return ids;
103 }
104
105 void
106 VoxelGrid::setByLabelDensity(const LabelDensity::VoxelGrid& density, bool adoptStructure)
107 {
108 if (density.getStructure() != getStructure())
109 {
110 if (adoptStructure)
111 {
112 resetStructure(density.getStructure());
113 }
114 else
115 {
117 density.getStructure(),
118 getStructure(),
119 "Provided density grid has different structure, but adoptStructure is false.");
120 }
121 }
122
123 for (std::size_t i = 0; i < numVoxels(); ++i)
124 {
125 const LabelDensity::Voxel& voxel = density.getVoxel(i);
126 if (voxel.isFree())
127 {
128 getVoxel(i).setFree();
129 }
130 else
131 {
132 getVoxel(i).setOccupied(voxel.getMaxLabel());
133 }
134 }
135 }
136
137 void
138 VoxelGrid::writeJson(std::ostream& os) const
139 {
141 }
142
143 void
144 VoxelGrid::writeJson(const std::filesystem::path& file) const
145 {
147 }
148
149 void
150 VoxelGrid::readJson(std::istream& is)
151 {
153 }
154
155 void
156 VoxelGrid::readJson(const std::filesystem::path& file)
157 {
159 }
160
161 void
162 to_json(nlohmann::json& json, const VoxelGrid& grid)
163 {
164 io::JsonIO::toJson(json, grid, VoxelGrid::JSON_VOXEL_ARRAY_NAME);
165 }
166
167 void
168 from_json(const nlohmann::json& json, VoxelGrid& grid)
169 {
170 io::JsonIO::fromJson(json, grid, VoxelGrid::JSON_VOXEL_ARRAY_NAME);
171 }
172
173} // namespace visionx::voxelgrid::LabelOccupancy
Voxel grid storing the number of points per label in each voxel.
Definition VoxelGrid.h:67
Map of labels (object IDs) to number of points.
Definition VoxelGrid.h:27
bool isFree() const
Indicate whether this voxel is free, i.e. it contains no points.
Definition VoxelGrid.cpp:15
Label getMaxLabel() const
Get the label with most points.
Definition VoxelGrid.cpp:46
Voxel grid storing the number of points per label in each voxel.
Definition VoxelGrid.h:63
void setByLabelDensity(const LabelDensity::VoxelGrid &density, bool adoptStructure=false)
Set the voxels by selecting the label with most points for each voxel.
static const std::string JSON_VOXEL_ARRAY_NAME
Name of the single voxel array in JSON.
Definition VoxelGrid.h:113
VoxelGrid()
Construct a voxel grid with 0 voxels of size 1.0.
A voxel storing whether it is occupied or free and the object label it is occupied by.
Definition VoxelGrid.h:23
void setFree()
Set the voxel to free, i.e. unoccupied.
Definition VoxelGrid.cpp:42
Voxel()
Construct an unoccupied voxel.
Definition VoxelGrid.cpp:12
bool isOccupied() const
Indicate whether this voxel is occupied.
Definition VoxelGrid.cpp:29
Label getLabel() const
Get the label. Voxel must be occupied.
Definition VoxelGrid.cpp:35
bool isFree() const
Indicate whether this voxel is free (i.e. it is not occupied).
Definition VoxelGrid.cpp:23
void setOccupied(Label label)
Set the voxel to occupied and set its label.
Definition VoxelGrid.cpp:49
A 3D grid of voxels of type _VoxelT.
Definition VoxelGrid.hpp:50
VoxelGridStructure getStructure() const
Get the voxel grid structure.
Definition VoxelGrid.hpp:87
void resetStructure(const VoxelGridStructure &structure)
Definition VoxelGrid.hpp:94
VoxelT & getVoxel(std::size_t index)
Get the voxel with the given index.
Indicates that a voxel grid structure should have matched another one, but did not.
Definition exceptions.h:40
static void toJson(nlohmann::json &j, const VoxelGrid< VoxelT > &grid, const VoxelAttributeGetterMap< VoxelT > &attributeMap)
Serialize a voxel grid to JSON.
Definition JsonIO.h:245
static void fromJson(const nlohmann::json &j, VoxelGrid< VoxelT > &grid, const VoxelAttributeSetterMap< VoxelT > &attributeMap)
Deserialize a voxel grid from JSON.
Definition JsonIO.h:294
static nlohmann::json readJson(const std::string &filename)
Read JSON from file.
Definition JsonIO.cpp:24
static void writeJson(const std::string &filename, const nlohmann::json &j, int indent=-1, char indentChar=' ')
Write JSON json to file.
Definition JsonIO.cpp:8
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
void to_json(nlohmann::json &json, const Voxel &voxel)
Set json to the voxel's label if occupied, and to -1 if free.
Definition VoxelGrid.cpp:56
void from_json(const nlohmann::json &json, Voxel &voxel)
Set this voxel free, if json is < 0.
Definition VoxelGrid.cpp:69
uint32_t Label
Type of an object label.
Definition types.h:6