CVB++ 15.0
mesh.hpp
1#pragma once
2
3#include "sparse_point_cloud.hpp"
4#include "dense_point_cloud.hpp"
5#include "sparse_components_pointers_3d.hpp"
6#include "polygon_3d.hpp"
7
8namespace Cvb
9{
10
11 CVB_BEGIN_INLINE_NS
12
18 template <class T>
19 class Mesh final
20 {
21 struct PrivateTag
22 {
23 };
24
25 public:
26 Mesh(const SparsePointCloudPtr &pointCloud, PrivateTag)
27 : pointCloud_(pointCloud)
28 , components_(pointCloud->PointComponents<T>())
29 {
30 CVB_CALL_CAPI_CHECKED(CVC3DPointCloudGetPolygonTable(pointCloud_->Handle(), polygonTable_, numPolygons_));
31 }
32
33 Mesh(const Mesh &) = delete;
34 Mesh &operator=(const Mesh &) noexcept = delete;
35 Mesh(Mesh &&) noexcept = default;
36 Mesh &operator=(Mesh &&) = default;
37 ~Mesh() = default;
38
40
47 static MeshPtr<T> FromFile(const String &fileName)
48 {
49 // TODO get flags
50 auto pointCloud = Internal::DoResCallShareOut<SparsePointCloud>([&](void *&handle) {
51 return CVB_CALL_CAPI(
52 CVC3DLoadFileTyped(fileName.c_str(), static_cast<CExports::cvbval_t>(PointTypeAsFlags()), handle));
53 });
54 return std::make_shared<Mesh<T>>(pointCloud, PrivateTag{});
55 }
56
58
70 {
71 auto pMesh = std::make_shared<Mesh<T>>(pointCloud, PrivateTag{});
72 if (!pMesh)
73 throw std::runtime_error("could not create mesh from point cloud");
74
75 return pMesh;
76 }
77
79
83 std::size_t NumPolygons() const noexcept
84 {
85 return static_cast<std::size_t>(numPolygons_);
86 }
87
89
95 {
96 auto ptr = IndexTablePtrAt(index);
97 std::vector<T> points;
98 std::generate_n(std::back_inserter(points), *ptr, [this, ptr, i = 0]() mutable {
99 ++i;
100 return components_.PointAt(*(ptr + i));
101 });
102 return Polygon3D<T>{std::move(points)};
103 }
104
106
112 {
113 auto ptr = IndexTablePtrAt(index);
115 std::generate_n(std::back_inserter(indices), *ptr, [ptr, i = 0]() mutable {
116 ++i;
117 return *(ptr + i);
118 });
119 return indices;
120 }
121
123
127 std::size_t NumPoints() const noexcept
128 {
129 return components_.NumPoints();
130 }
131
133
138 T PointAt(std::size_t index) const noexcept
139 {
140 return components_.PointAt(index);
141 }
142
144
149 {
150 return pointCloud_->CalculateBoundingBox();
151 }
152
154
160 void RasterDensePointCloudToDst(const Matrix3D &cameraRotation, Vector3D<double> cameraTranslation,
161 DensePointCloud &denseCloud)
162 {
163 CVB_CALL_CAPI_CHECKED(CVC3DRasterMeshToDensePointCloud(
164 pointCloud_->Handle(), *reinterpret_cast<const CExports::CVC3DMatrix *>(&cameraRotation),
165 *reinterpret_cast<CExports::CVC3DVector *>(&cameraTranslation), nullptr, denseCloud.Handle()));
166 }
167
169
177 DensePointCloudPtr RasterDensePointCloud(const Matrix3D &cameraRotation, Vector3D<double> cameraTranslation,
178 double xResIncrement, double yResIncrement)
179 {
180 return Internal::DoResCallShareOut<DensePointCloud>([&](void *&handle) {
181 return CVB_CALL_CAPI(CVC3DCreateRasterMeshToDensePointCloud(
182 pointCloud_->Handle(), *reinterpret_cast<const CExports::CVC3DMatrix *>(&cameraRotation),
183 *reinterpret_cast<CExports::CVC3DVector *>(&cameraTranslation), xResIncrement, yResIncrement, nullptr,
184 handle));
185 });
186 }
187
188 private:
189 CExports::cvbuint32_t *IndexTablePtrAt(std::size_t index) const noexcept
190 {
191 auto ptr = polygonTable_;
192 for (std::size_t i = 0; i < index; ++i)
193 ptr = IncrementTablePtr(ptr);
194 return ptr;
195 }
196
197 static CExports::cvbuint32_t *IncrementTablePtr(CExports::cvbuint32_t *ptr) noexcept
198 {
199 return ptr + *ptr + 1;
200 }
201
202 static PointCloudFlags PointTypeAsFlags() noexcept;
203
204 SparsePointCloudPtr pointCloud_;
205 SparseComponentsPointers3D<T> components_;
206 CExports::cvbuint32_t *polygonTable_ = nullptr;
207 CExports::cvbint64_t numPolygons_ = 0;
208 };
209
210 template <>
211 inline Cvb::PointCloudFlags Mesh<Cvb::Point3D<float>>::PointTypeAsFlags() noexcept
212 {
214 }
215
216 template <>
217 inline Cvb::PointCloudFlags Mesh<Cvb::Point3DH<float>>::PointTypeAsFlags() noexcept
218 {
220 }
221
222 template <>
223 inline Cvb::PointCloudFlags Mesh<Cvb::Point3D<double>>::PointTypeAsFlags() noexcept
224 {
226 }
227
228 template <>
229 inline Cvb::PointCloudFlags Mesh<Cvb::Point3DH<double>>::PointTypeAsFlags() noexcept
230 {
232 }
233
234 CVB_END_INLINE_NS
235
236} // namespace Cvb
T back_inserter(T... args)
3D rectangle in the X, Y and Z domain.
Definition cuboid.hpp:19
A dense Cartesian 3D point cloud object.
Definition decl_dense_point_cloud.hpp:30
Double precision 3x3 matrix class.
Definition matrix_3d.hpp:54
T PointAt(std::size_t index) const noexcept
Gets the point at the specified index.
Definition mesh.hpp:138
std::size_t NumPolygons() const noexcept
Get the number of polygons in this mesh.
Definition mesh.hpp:83
Polygon3D< T > PolygonAt(std::size_t index) const
Gets polygons for this mesh.
Definition mesh.hpp:94
DensePointCloudPtr RasterDensePointCloud(const Matrix3D &cameraRotation, Vector3D< double > cameraTranslation, double xResIncrement, double yResIncrement)
Rasters this mesh into a dense point cloud and stores the result.
Definition mesh.hpp:177
static MeshPtr< T > FromFile(const String &fileName)
Loads a mesh from file.
Definition mesh.hpp:47
void RasterDensePointCloudToDst(const Matrix3D &cameraRotation, Vector3D< double > cameraTranslation, DensePointCloud &denseCloud)
Rasters this mesh into a dense point cloud and stores the result.
Definition mesh.hpp:160
static MeshPtr< T > FromPointCloud(const SparsePointCloudPtr &pointCloud)
Creates a mesh from a point cloud containing mesh information.
Definition mesh.hpp:69
Cuboid CalculateBoundingBox() const
Calculates the minimum and maximum extent of the mesh.
Definition mesh.hpp:148
std::vector< std::uint32_t > PolygonIndicesAt(std::size_t index) const
Gets point indices for a polygon.
Definition mesh.hpp:111
std::size_t NumPoints() const noexcept
Get the number of points in the mesh.
Definition mesh.hpp:127
void * Handle() const noexcept
Returns C-API style handle to Node Object.
Definition decl_point_cloud.hpp:773
A polygon in 3D space.
Definition polygon_3d.hpp:28
T generate_n(T... args)
T make_shared(T... args)
T move(T... args)
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
PointCloudFlags
Flags for creating point clouds.
Definition core_3d.hpp:90
@ Float
Definition core_3d.hpp:94
@ XYZW
Definition core_3d.hpp:109
@ Double
Definition core_3d.hpp:99
@ XYZ
Definition core_3d.hpp:104
Point3D< T > Vector3D
Alias for Point3D.
Definition point_3d.hpp:363
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
std::shared_ptr< SparsePointCloud > SparsePointCloudPtr
Convenience shared pointer for SparsePointCloud.
Definition core_3d.hpp:48
std::shared_ptr< Mesh< T > > MeshPtr
Convenience shared pointer for Mesh.
Definition core_3d.hpp:86
std::shared_ptr< DensePointCloud > DensePointCloudPtr
Convenience shared pointer for DensePointCloud.
Definition core_3d.hpp:44