14 #include <unordered_map>
30 voxel_size_(src_voxel_grid.voxel_size_),
31 origin_(src_voxel_grid.origin_),
32 rotation_(src_voxel_grid.rotation_),
33 voxels_(src_voxel_grid.voxels_) {}
41 origin_ = Eigen::Vector3d::Zero();
53 Eigen::Vector3d min_bound = corners.front();
54 for (
const auto &p : corners) {
55 min_bound = min_bound.cwiseMin(p);
68 Eigen::Vector3d max_bound = corners.front();
69 for (
const auto &p : corners) {
70 max_bound = max_bound.cwiseMax(p);
77 std::vector<Eigen::Vector3d> voxel_corners;
78 voxel_corners.reserve(
voxels_.size() * 8);
81 static const Eigen::Vector3d offsets[8] = {{0, 0, 0}, {1, 0, 0}, {0, 1, 0},
82 {1, 1, 0}, {0, 0, 1}, {1, 0, 1},
83 {0, 1, 1}, {1, 1, 1}};
85 for (
const auto &it :
voxels_) {
87 Eigen::Vector3d base =
91 for (
const auto &off : offsets) {
100 Eigen::Vector3d center(0, 0, 0);
106 for (
const auto &it :
voxels_) {
111 center /= double(
voxels_.size());
130 transformation.block<3, 1>(0, 3);
148 const Eigen::Vector3d ¢er) {
158 "[VoxelGrid] Could not combine VoxelGrid because voxel_size "
159 "differs (this=%f, other=%f)",
164 "[VoxelGrid] Could not combine VoxelGrid because origin "
165 "differs (this=%f,%f,%f, other=%f,%f,%f)",
171 "[VoxelGrid] Could not combine VoxelGrid one has colors and "
176 voxelindex_to_accpoint;
177 Eigen::Vector3d ref_coord;
180 for (
const auto &it : voxelgrid.
voxels_) {
189 for (
const auto &it :
voxels_) {
198 this->voxels_.clear();
199 for (
const auto &accpoint : voxelindex_to_accpoint) {
201 accpoint.second.GetAverageColor()));
221 std::vector<Eigen::Vector3d>
points(8);
240 const std::vector<Eigen::Vector3d> &queries) {
241 std::vector<bool> output;
242 output.resize(queries.size());
244 for (
auto &query_double : queries) {
245 auto query =
GetVoxel(query_double);
246 output[i] =
voxels_.count(query) > 0;
255 std::unordered_map<std::shared_ptr<OctreeColorLeafNode>,
256 std::shared_ptr<OctreeNodeInfo>>
257 map_node_to_node_info;
258 auto f_collect_nodes =
259 [&map_node_to_node_info](
260 const std::shared_ptr<OctreeNode> &node,
261 const std::shared_ptr<OctreeNodeInfo> &node_info) ->
bool {
262 if (
auto color_leaf_node =
263 std::dynamic_pointer_cast<OctreeColorLeafNode>(node)) {
264 map_node_to_node_info[color_leaf_node] = node_info;
268 octree.Traverse(f_collect_nodes);
273 for (
const auto &it : map_node_to_node_info) {
278 for (
const auto &it : map_node_to_node_info) {
279 const std::shared_ptr<OctreeColorLeafNode> &node = it.first;
280 const std::shared_ptr<OctreeNodeInfo> &node_info = it.second;
281 Eigen::Array3d node_center =
282 Eigen::Array3d(node_info->origin_) + node_info->size_ / 2.0;
292 const size_t &max_depth)
const {
293 auto octree = std::make_shared<geometry::Octree>(max_depth);
294 octree->CreateFromVoxelGrid(*
this);
299 const Image &depth_map,
301 bool keep_voxels_outside_image) {
305 "[VoxelGrid] provided depth_map dimensions are not compatible "
306 "with the provided camera_parameters");
309 auto rot = camera_parameter.
extrinsic_.block<3, 3>(0, 0);
310 auto trans = camera_parameter.
extrinsic_.block<3, 1>(0, 3);
319 for (
auto &
x : pts) {
320 auto x_trans = rot *
x + trans;
321 auto uvz = intrinsic * x_trans;
323 double u = uvz(0) /
z;
324 double v = uvz(1) /
z;
326 bool within_boundary;
327 std::tie(within_boundary, d) = depth_map.
FloatValueAt(u, v);
328 if ((!within_boundary && keep_voxels_outside_image) ||
329 (within_boundary && d > 0 &&
z >= d)) {
343 const Image &silhouette_mask,
345 bool keep_voxels_outside_image) {
349 "[VoxelGrid] provided silhouette_mask dimensions are not "
350 "compatible with the provided camera_parameters");
353 auto rot = camera_parameter.
extrinsic_.block<3, 3>(0, 0);
354 auto trans = camera_parameter.
extrinsic_.block<3, 1>(0, 3);
363 for (
auto &
x : pts) {
364 auto x_trans = rot *
x + trans;
365 auto uvz = intrinsic * x_trans;
367 double u = uvz(0) /
z;
368 double v = uvz(1) /
z;
370 bool within_boundary;
371 std::tie(within_boundary, d) = silhouette_mask.
FloatValueAt(u, v);
372 if ((!within_boundary && keep_voxels_outside_image) ||
373 (within_boundary && d > 0)) {
387 std::vector<Voxel>
result;
389 for (
const auto &keyval :
voxels_) {
390 result.push_back(keyval.second);
virtual bool IsEmpty() const override
Hierarchical CLOUDVIEWER Object.
const Vector3Tpl< T > & maxCorner() const
Returns max corner (const)
void setValidity(bool state)
Sets bonding box validity.
const Vector3Tpl< T > & minCorner() const
Returns min corner (const)
int height_
Height of the image.
int width_
Width of the image.
Eigen::Matrix3d intrinsic_matrix_
Contains both intrinsic and extrinsic pinhole camera parameters.
PinholeCameraIntrinsic intrinsic_
PinholeCameraIntrinsic object.
Eigen::Matrix4d_u extrinsic_
Camera extrinsic parameters.
Class to aggregate color values from different votes in one voxel Computes the average color value in...
The Image class stores image with customizable width, height, num of channels and bytes per channel.
std::pair< bool, double > FloatValueAt(double u, double v) const
int height_
Height of the image.
int width_
Width of the image.
VoxelGrid is a collection of voxels which are aligned in grid.
virtual Eigen::Vector3d GetMinBound() const override
Returns min bounds for geometry coordinates.
Eigen::Matrix3d rotation_
std::vector< Voxel > GetVoxels() const
void CreateFromOctree(const Octree &octree)
VoxelGrid & CarveSilhouette(const Image &silhouette_mask, const camera::PinholeCameraParameters &camera_parameter, bool keep_voxels_outside_image)
virtual VoxelGrid & Rotate(const Eigen::Matrix3d &R, const Eigen::Vector3d ¢er) override
Apply rotation to the geometry coordinates and normals. Given a rotation matrix , and center ,...
std::vector< bool > CheckIfIncluded(const std::vector< Eigen::Vector3d > &queries)
virtual ccBBox getOwnBB(bool withGLFeatures=false) override
Returns the entity's own bounding-box.
std::shared_ptr< geometry::Octree > ToOctree(const size_t &max_depth) const
std::vector< Eigen::Vector3d > GetAllVoxelCorners() const
Returns the 3D coordinates of all corners of every voxel in the grid.
virtual ccBBox GetAxisAlignedBoundingBox() const override
Returns an axis-aligned bounding box of the geometry.
std::unordered_map< Eigen::Vector3i, Voxel, cloudViewer::utility::hash_eigen< Eigen::Vector3i > > voxels_
Voxels contained in voxel grid.
double voxel_size_
Size of the voxel.
virtual VoxelGrid & Transform(const Eigen::Matrix4d &transformation) override
Apply transformation (4x4 matrix) to the geometry coordinates.
Eigen::Vector3d GetVoxelCenterCoordinate(const Eigen::Vector3i &idx) const
Function that returns the 3d coordinates of the queried voxel center.
virtual ecvOrientedBBox GetOrientedBoundingBox() const override
bool HasColors() const
Returns true if the voxel grid contains voxel colors.
VoxelGrid & CarveDepthMap(const Image &depth_map, const camera::PinholeCameraParameters &camera_parameter, bool keep_voxels_outside_image)
bool HasVoxels() const
Returns true if the voxel grid contains voxels.
virtual Eigen::Vector3d GetCenter() const override
Returns the center of the geometry coordinates.
VoxelGrid operator+(const VoxelGrid &voxelgrid) const
Eigen::Vector3d origin_
Coorindate of the origin point.
VoxelGrid(const char *name="VoxelGrid")
Default Constructor.
VoxelGrid & operator+=(const VoxelGrid &voxelgrid)
virtual VoxelGrid & Translate(const Eigen::Vector3d &translation, bool relative=true) override
Apply translation to the geometry coordinates.
Eigen::Vector3i GetVoxel(const Eigen::Vector3d &point) const
Returns voxel index given query point.
void RemoveVoxel(const Eigen::Vector3i &idx)
Remove a voxel with specified grid index.
virtual Eigen::Vector3d GetMaxBound() const override
Returns max bounds for geometry coordinates.
void AddVoxel(const Voxel &voxel)
Add a voxel with specified grid index and color.
std::vector< Eigen::Vector3d > GetVoxelBoundingPoints(const Eigen::Vector3i &index) const
Return a vector of 3D coordinates that define the indexed voxel cube.
virtual VoxelGrid & Scale(const double s, const Eigen::Vector3d ¢er) override
Apply scaling to the geometry coordinates. Given a scaling factor , and center , a given point is tr...
Base Voxel class, containing grid id and color.
Eigen::Vector3i grid_index_
Grid coordinate index of the voxel.
Eigen::Vector3d color_
Color of the voxel.
static ecvOrientedBBox CreateFromPoints(const std::vector< Eigen::Vector3d > &points)
MiniVec< float, N > floor(const MiniVec< float, N > &a)
Generic file read and write utility for python interface.
Eigen::Matrix< Index, 3, 1 > Vector3i