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
11CVB_BEGIN_INLINE_NS
12
18template <class T>
19class Mesh final
20{
21 struct PrivateTag{};
22
23 public:
24 Mesh(const SparsePointCloudPtr& pointCloud, PrivateTag)
25 : pointCloud_(pointCloud)
26 , components_(pointCloud->PointComponents<T>())
27 {
28 CVB_CALL_CAPI_CHECKED(CVC3DPointCloudGetPolygonTable(pointCloud_->Handle(), polygonTable_, numPolygons_));
29 }
30
31 Mesh(const Mesh&) = delete;
32 Mesh& operator=(const Mesh&) noexcept = delete;
33 Mesh(Mesh&&) noexcept = default;
34 Mesh& operator=(Mesh&&) = default;
35 ~Mesh() = default;
36
38
45 static MeshPtr<T> FromFile(const String& fileName)
46 {
47 // TODO get flags
48 auto pointCloud = Internal::DoResCallShareOut<SparsePointCloud>([&](void*& handle)
49 {
50 return CVB_CALL_CAPI(CVC3DLoadFileTyped(fileName.c_str(), static_cast<CExports::cvbval_t>(PointTypeAsFlags()), handle));
51 });
52 return std::make_shared<Mesh<T>>(pointCloud, PrivateTag{});
53 }
54
56
68 {
69 auto pMesh = std::make_shared<Mesh<T>>(pointCloud, PrivateTag{});
70 if(!pMesh)
71 throw std::runtime_error("could not create mesh from point cloud");
72
73 return pMesh;
74 }
75
77
81 std::size_t NumPolygons() const noexcept
82 {
83 return static_cast<std::size_t>(numPolygons_);
84 }
85
87
93 {
94 auto ptr = IndexTablePtrAt(index);
95 std::vector<T> points;
96 std::generate_n(std::back_inserter(points), *ptr, [this, ptr, i = 0]() mutable
97 {
98 ++i;
99 return components_.PointAt(*(ptr + i));
100 });
101 return Polygon3D<T>{ std::move(points) };
102 }
103
105
111 {
112 auto ptr = IndexTablePtrAt(index);
114 std::generate_n(std::back_inserter(indices), *ptr, [ptr, i = 0]() mutable
115 {
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, DensePointCloud& denseCloud)
161 {
162 CVB_CALL_CAPI_CHECKED(CVC3DRasterMeshToDensePointCloud(pointCloud_->Handle(),
163 *reinterpret_cast<const CExports::CVC3DMatrix*>(&cameraRotation),
164 *reinterpret_cast<CExports::CVC3DVector*>(&cameraTranslation),
165 nullptr,
166 denseCloud.Handle()));
167 }
168
170
178 DensePointCloudPtr RasterDensePointCloud(const Matrix3D& cameraRotation, Vector3D<double> cameraTranslation, double xResIncrement, double yResIncrement)
179 {
180 return Internal::DoResCallShareOut<DensePointCloud>([&](void*& handle)
181 {
182 return CVB_CALL_CAPI(CVC3DCreateRasterMeshToDensePointCloud(pointCloud_->Handle(),
183 *reinterpret_cast<const CExports::CVC3DMatrix*>(&cameraRotation),
184 *reinterpret_cast<CExports::CVC3DVector*>(&cameraTranslation),
185 xResIncrement,
186 yResIncrement,
187 nullptr,
188 handle));
189 });
190 }
191
192 private:
193
194 CExports::cvbuint32_t* IndexTablePtrAt(std::size_t index) const noexcept
195 {
196 auto ptr = polygonTable_;
197 for (std::size_t i = 0; i < index; ++i)
198 ptr = IncrementTablePtr(ptr);
199 return ptr;
200 }
201
202 static CExports::cvbuint32_t* IncrementTablePtr(CExports::cvbuint32_t* ptr) noexcept
203 {
204 return ptr + *ptr + 1;
205 }
206
207 static PointCloudFlags PointTypeAsFlags() noexcept;
208
209 SparsePointCloudPtr pointCloud_;
210 SparseComponentsPointers3D<T> components_;
211 CExports::cvbuint32_t* polygonTable_ = nullptr;
212 CExports::cvbint64_t numPolygons_ = 0;
213
214};
215
216template<>
217inline Cvb::PointCloudFlags Mesh<Cvb::Point3D<float>>::PointTypeAsFlags() noexcept
218{
220}
221
222template<>
223inline Cvb::PointCloudFlags Mesh<Cvb::Point3DH<float>>::PointTypeAsFlags() noexcept
224{
226}
227
228template<>
229inline Cvb::PointCloudFlags Mesh<Cvb::Point3D<double>>::PointTypeAsFlags() noexcept
230{
232}
233
234template<>
235inline Cvb::PointCloudFlags Mesh<Cvb::Point3DH<double>>::PointTypeAsFlags() noexcept
236{
238}
239
240
241CVB_END_INLINE_NS
242
243}
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:31
Double precision 3x3 matrix class.
Definition: matrix_3d.hpp:60
3D mesh object consisting of polygons.
Definition: mesh.hpp:20
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:81
Polygon3D< T > PolygonAt(std::size_t index) const
Gets polygons for this mesh.
Definition: mesh.hpp:92
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:178
static MeshPtr< T > FromFile(const String &fileName)
Loads a mesh from file.
Definition: mesh.hpp:45
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:67
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:110
std::size_t NumPoints() const noexcept
Get the number of points in the mesh.
Definition: mesh.hpp:127
Multi-purpose 3D vector class.
Definition: point_3d.hpp:22
void * Handle() const noexcept
Returns C-API style handle to Node Object.
Definition: decl_point_cloud.hpp:768
A polygon in 3D space.
Definition: polygon_3d.hpp:29
Root namespace for the Image Manager interface.
Definition: c_barcode.h:15
PointCloudFlags
Flags for creating point clouds.
Definition: core_3d.hpp:90
std::shared_ptr< SparsePointCloud > SparsePointCloudPtr
Convenience shared pointer for SparsePointCloud.
Definition: core_3d.hpp:48