CVB++ 15.0
decl_plane_enumerator.hpp
1#pragma once
2
3#include <algorithm>
4
5#include "../_decl/decl_point_cloud.hpp"
6
7#include "../plane.hpp"
8#include "../global.hpp"
9
10namespace Cvb
11{
12 CVB_BEGIN_INLINE_NS
13
14 template <>
15 inline HandleGuard<PlaneEnumerator>::HandleGuard(void *handle) noexcept
16 : HandleGuard<PlaneEnumerator>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
17 {
18 }
19
21
26 class PlaneEnumerator final
27 {
28 struct PrivateTag
29 {
30 };
31
32 public:
33 using GuardType = HandleGuard<PlaneEnumerator>;
34
36
39 PlaneEnumerator() noexcept
40 : handle_(nullptr)
41 {
42 planes_.resize(PlaneCount());
43 }
44
46
49 PlaneEnumerator(HandleGuard<PlaneEnumerator> &&guard, PrivateTag) noexcept
50 : handle_(std::move(guard))
51 {
52 planes_.resize(PlaneCount());
53 }
54
56
64 template <class T>
65 static PlaneEnumerator FromObject(const T &object);
66
68
71 static PlaneEnumeratorPtr FromHandle(HandleGuard<PlaneEnumerator> &&guard)
72 {
73 if (!guard.Handle() || CVB_CALL_CAPI(CVCIsComposite(guard.Handle())))
74 throw std::runtime_error("handle must not be null and object must not be a composite");
75
76 return std::make_shared<PlaneEnumerator>(std::move(guard), PrivateTag{});
77 }
78
79 template <class T>
80 static PlaneEnumeratorPtr FromHandle(HandleGuard<PlaneEnumerator> &&guard)
81 {
82 static_assert(std::is_base_of<PlaneEnumerator, T>::value, "CVB: Type must be derived from PlaneEnumerator");
83 return FromHandle(std::move(guard));
84 }
85
87
93 void *Handle() const noexcept
94 {
95 return handle_.Handle();
96 }
97
99
103 int PlaneCount() const noexcept
104 {
105 CExports::cvbdim_t numPlanes = 0;
106 CVB_CALL_CAPI(CVCPlaneEnumGetCount(Handle(), numPlanes));
107 return static_cast<int>(numPlanes);
108 }
109
111
116 {
118 std::generate_n(std::back_inserter(planes), PlaneCount(), [this, i = 0]() mutable { return Plane(i++); });
119 return planes;
120 }
121
123
130 PlanePtr operator[](int index) const
131 {
132 return Plane(index);
133 }
134
136
143 PlanePtr Plane(int index) const
144 {
145 if (index < 0 || index >= PlaneCount())
146 throw std::length_error("index out of range");
147
148 if (auto plane = planes_[index].lock())
149 return plane;
150
151 CExports::CVPLANE handle = nullptr;
152 CVB_CALL_CAPI_CHECKED(CVCPlaneEnumGetAt(Handle(), static_cast<CExports::cvbdim_t>(index), handle));
153 auto plane = Plane::FromHandle(HandleGuard<class Plane>(handle));
154 planes_[index] = plane;
155 return plane;
156 }
157
158 private:
160
161 HandleGuard<PlaneEnumerator> handle_;
162 };
163
164 CVB_END_INLINE_NS
165} // namespace Cvb
T back_inserter(T... args)
PlanePtr Plane(int index) const
Index based plane access.
Definition decl_plane_enumerator.hpp:143
int PlaneCount() const noexcept
Gets the number of planes enumerated by this object.
Definition decl_plane_enumerator.hpp:103
static PlaneEnumerator FromObject(const T &object)
Create a plane enumerator for a given object.
static PlaneEnumeratorPtr FromHandle(HandleGuard< PlaneEnumerator > &&guard)
Creates a plane enumerator from a classic API handle.
Definition decl_plane_enumerator.hpp:71
PlaneEnumerator(HandleGuard< PlaneEnumerator > &&guard, PrivateTag) noexcept
Plane enumerator constructor.
Definition decl_plane_enumerator.hpp:49
PlanePtr operator[](int index) const
Index based plane access.
Definition decl_plane_enumerator.hpp:130
PlaneEnumerator() noexcept
Plane enumerator constructor.
Definition decl_plane_enumerator.hpp:39
std::vector< PlanePtr > Planes() const
Gets all available planes enumerated by this object.
Definition decl_plane_enumerator.hpp:115
void * Handle() const noexcept
Classic API device handle.
Definition decl_plane_enumerator.hpp:93
static PlanePtr FromHandle(HandleGuard< Plane > &&guard)
Creates a plane from a classic API handle.
Definition decl_plane.hpp:76
T generate_n(T... args)
cvbbool_t ReleaseObject(OBJ &Object)
T make_shared(T... args)
T move(T... args)
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::shared_ptr< PlaneEnumerator > PlaneEnumeratorPtr
Convenience shared pointer for PlaneEnumerator.
Definition global.hpp:82
std::shared_ptr< Plane > PlanePtr
Convenience shared pointer for Plane.
Definition global.hpp:78