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 
8 namespace Cvb
9 {
10 
11 CVB_BEGIN_INLINE_NS
12 
18 template <class T>
19 class Mesh final
20 {
21  struct PrivateTag{};
22 
23  public:
24  Mesh(const SparsePointCloudPtr& pointCloud, PrivateTag)
25  : pointCloud_(pointCloud)
26  {
27  CVB_CALL_CAPI_CHECKED(CVC3DPointCloudGetPolygonTable(pointCloud_->Handle(), polygonTable_, numPolygons_));
28  components_ = pointCloud_->PointComponents<T>();
29  }
30 
31  Mesh(const Mesh&) = delete;
32  Mesh& operator=(const Mesh&) = delete;
33 
35 
42  static MeshPtr<T> FromFile(const String& fileName)
43  {
44  // TODO get flags
45  auto pointCloud = Internal::DoResCallShareOut<SparsePointCloud>([&](void*& handle)
46  {
47  return CVB_CALL_CAPI(CVC3DLoadFileTyped(fileName.c_str(), static_cast<CExports::cvbval_t>(PointTypeAsFlags()), handle));
48  });
49  return std::make_shared<Mesh<T>>(pointCloud, PrivateTag{});
50  }
51 
53 
64  static MeshPtr<T> FromPointCloud(const SparsePointCloudPtr& pointCloud)
65  {
66  auto pMesh = std::make_shared<Mesh<T>>(pointCloud, PrivateTag{});
67  if(!pMesh)
68  throw std::runtime_error("could not create mesh from point cloud");
69 
70  return pMesh;
71  }
72 
74 
78  std::size_t NumPolygons() const noexcept
79  {
80  return static_cast<std::size_t>(numPolygons_);
81  }
82 
84 
90  {
91  auto ptr = IndexTablePtrAt(index);
92  std::vector<T> points;
93  std::generate_n(std::back_inserter(points), *ptr, [this, ptr, i = 0]() mutable
94  {
95  ++i;
96  return components_.PointAt(*(ptr + i));
97  });
98  return { std::move(points) };
99  }
100 
102 
108  {
109  auto ptr = IndexTablePtrAt(index);
111  std::generate_n(std::back_inserter(indices), *ptr, [ptr, i = 0]() mutable
112  {
113  ++i;
114  return *(ptr + i);
115  });
116  return indices;
117  }
118 
120 
124  std::size_t NumPoints() const noexcept
125  {
126  return components_.NumPoints();
127  }
128 
130 
135  T PointAt(std::size_t index) const noexcept
136  {
137  return components_.PointAt(index);
138  }
139 
141 
146  {
147  return pointCloud_->CalculateBoundingBox();
148  }
149 
151 
157  void RasterDensePointCloudToDst(const Matrix3D& cameraRotation, Vector3D<double> cameraTranslation, DensePointCloud& denseCloud)
158  {
159  CVB_CALL_CAPI_CHECKED(CVC3DRasterMeshToDensePointCloud(pointCloud_->Handle(),
160  *reinterpret_cast<const CExports::CVC3DMatrix*>(&cameraRotation),
161  *reinterpret_cast<CExports::CVC3DVector*>(&cameraTranslation),
162  nullptr,
163  denseCloud.Handle()));
164  }
165 
167 
175  DensePointCloudPtr RasterDensePointCloud(const Matrix3D& cameraRotation, Vector3D<double> cameraTranslation, double xResIncrement, double yResIncrement)
176  {
177  return Internal::DoResCallShareOut<DensePointCloud>([&](void*& handle)
178  {
179  return CVB_CALL_CAPI(CVC3DCreateRasterMeshToDensePointCloud(pointCloud_->Handle(),
180  *reinterpret_cast<const CExports::CVC3DMatrix*>(&cameraRotation),
181  *reinterpret_cast<CExports::CVC3DVector*>(&cameraTranslation),
182  xResIncrement,
183  yResIncrement,
184  nullptr,
185  handle));
186  });
187  }
188 
189  private:
190 
191  CExports::cvbuint32_t* IndexTablePtrAt(std::size_t index) const noexcept
192  {
193  auto ptr = polygonTable_;
194  for (std::size_t i = 0; i < index; ++i)
195  ptr = IncrementTablePtr(ptr);
196  return ptr;
197  }
198 
199  static CExports::cvbuint32_t* IncrementTablePtr(CExports::cvbuint32_t* ptr) noexcept
200  {
201  return ptr + *ptr + 1;
202  }
203 
204  static PointCloudFlags PointTypeAsFlags() noexcept;
205 
206  SparsePointCloudPtr pointCloud_;
207  SparseComponentsPointers3D<T> components_;
208  CExports::cvbuint32_t* polygonTable_ = nullptr;
209  CExports::cvbint64_t numPolygons_ = 0;
210 
211 };
212 
213 template<>
214 inline Cvb::PointCloudFlags Mesh<Cvb::Point3D<float>>::PointTypeAsFlags() noexcept
215 {
217 }
218 
219 template<>
220 inline Cvb::PointCloudFlags Mesh<Cvb::Point3DH<float>>::PointTypeAsFlags() noexcept
221 {
223 }
224 
225 template<>
226 inline Cvb::PointCloudFlags Mesh<Cvb::Point3D<double>>::PointTypeAsFlags() noexcept
227 {
229 }
230 
231 template<>
232 inline Cvb::PointCloudFlags Mesh<Cvb::Point3DH<double>>::PointTypeAsFlags() noexcept
233 {
235 }
236 
237 
238 CVB_END_INLINE_NS
239 
240 }
STL class.
T PointAt(std::size_t index) const noexcept
Gets the point at the specified index.
Definition: mesh.hpp:135
static MeshPtr< T > FromFile(const String &fileName)
Loads a mesh from file.
Definition: mesh.hpp:42
std::vector< std::uint32_t > PolygonIndicesAt(std::size_t index) const
Gets point indices for a polygon.
Definition: mesh.hpp:107
3D rectangle in the X, Y and Z domain.
Definition: cuboid.hpp:18
std::size_t NumPolygons() const noexcept
Get the number of polygons in this mesh.
Definition: mesh.hpp:78
std::shared_ptr< SparsePointCloud > SparsePointCloudPtr
Convenience shared pointer for SparsePointCloud.
Definition: core_3d.hpp:48
Double precision 3x3 matrix class.
Definition: matrix_3d.hpp:58
STL class.
Root namespace for the Image Manager interface.
Definition: version.hpp:11
A polygon in 3D space.
Definition: polygon_3d.hpp:28
void * Handle() const noexcept
Returns C-API style handle to Node Object.
Definition: decl_point_cloud.hpp:767
Polygon3D< T > PolygonAt(std::size_t index) const
Gets polygons for this mesh.
Definition: mesh.hpp:89
static MeshPtr< T > FromPointCloud(const SparsePointCloudPtr &pointCloud)
Creates a mesh from a point cloud containing mesh information.
Definition: mesh.hpp:64
Multi-purpose 3D vector class.
Definition: point_3d.hpp:21
STL class.
Cuboid CalculateBoundingBox() const
Calculates the minimum and maximum extent of the mesh.
Definition: mesh.hpp:145
std::size_t NumPoints() const noexcept
Get the number of points in the mesh.
Definition: mesh.hpp:124
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:175
PointCloudFlags
Flags for creating point clouds.
Definition: core_3d.hpp:89
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:157
A dense Cartesian 3D point cloud object.
Definition: decl_dense_point_cloud.hpp:29