Debian Codegraph @

plugins/core/Standard/qRANSAC_SD/extern/RANSAC_SD/BitmapPrimitiveShape.h

raw
1 #ifndef BITMAPPRIMITIVESHAPE_HEADER
2 #define BITMAPPRIMITIVESHAPE_HEADER
3 #include "BasePrimitiveShape.h"
4 #include <GfxTL/AABox.h>
5 #include <MiscLib/Vector.h>
6 #include <algorithm>
7 #include <istream>
8 #include <MiscLib/Performance.h>
9 #include <GfxTL/MathHelper.h>
10 #include <GfxTL/IndexedIterator.h>
11 #include "IndexIterator.h"
12 #include <MiscLib/Pair.h>
13 #ifdef DOPARALLEL
14 #include <omp.h>
15 #endif
16
17 #ifndef DLL_LINKAGE
18 #define DLL_LINKAGE
19 #endif
20
21 struct BitmapInfo
22 {
23 MiscLib::Vector< std::pair< float, float > > params;
24 MiscLib::Vector< char > bitmap;
25 GfxTL::AABox< GfxTL::Vector2Df > bbox;
26 MiscLib::Vector< size_t > bmpIdx;
27 size_t uextent, vextent;
28 };
29
30 class DLL_LINKAGE BitmapPrimitiveShape
31 : public BasePrimitiveShape
32 {
33 public:
34 bool Init(bool binary, std::istream *i);
35 size_t ConnectedComponent(const PointCloud &pc, float epsilon,
36 MiscLib::Vector< size_t > *indices, bool doFiltering = true, float* borderRatio = 0 );
37 size_t AllConnectedComponents(const PointCloud &pc, float epsilon, BitmapInfo& bitmapInfo,
38 MiscLib::Vector< size_t > *indices, MiscLib::Vector< int >& componentsImg,
39 MiscLib::Vector< std::pair< int, size_t > >& labels, bool doFiltering = true );
40 void TrimmingPolygons(const PointCloud &pc, float epsilon,
41 size_t begin, size_t end,
42 std::deque< ComponentPolygons > *polys) const;
43 void GenerateBitmapPoints(const PointCloud &pc, float epsilon,
44 size_t begin, size_t end, PointCloud *bmpPc) const;
45
46 public:
47 virtual void Parameters(const Vec3f &p,
48 std::pair< float, float > *param) const = 0;
49 virtual bool InSpace(float u, float v, Vec3f *p, Vec3f *n) const = 0;
50 virtual void Parameters(GfxTL::IndexedIterator< MiscLib::Vector< size_t >::iterator,
51 PointCloud::const_iterator > begin,
52 GfxTL::IndexedIterator< MiscLib::Vector< size_t >::iterator,
53 PointCloud::const_iterator > end,
54 MiscLib::Vector< std::pair< float, float > > *bmpParams) const = 0;
55 virtual void Parameters(GfxTL::IndexedIterator< IndexIterator,
56 PointCloud::const_iterator > begin,
57 GfxTL::IndexedIterator< IndexIterator,
58 PointCloud::const_iterator > end,
59 MiscLib::Vector< std::pair< float, float > > *bmpParams) const = 0;
60 virtual void BitmapExtent(float epsilon,
61 GfxTL::AABox< GfxTL::Vector2Df > *bbox,
62 MiscLib::Vector< std::pair< float, float > > *params,
63 size_t *uextent, size_t *vextent) = 0;
64 virtual void InBitmap(const std::pair< float, float > &param,
65 float epsilon, const GfxTL::AABox< GfxTL::Vector2Df > &bbox,
66 size_t uextent, size_t vextent,
67 std::pair< int, int > *inBmp) const = 0;
68 virtual void PreWrapBitmap(const GfxTL::AABox< GfxTL::Vector2Df > &bbox,
69 float epsilon, size_t uextent, size_t vextent,
70 MiscLib::Vector< char > *bmp) const;
71 virtual void WrapBitmap(const GfxTL::AABox< GfxTL::Vector2Df > &bbox,
72 float epsilon, bool *uwrap, bool *vwrap) const = 0;
73 virtual void WrapComponents(const GfxTL::AABox< GfxTL::Vector2Df > &bbox,
74 float epsilon, size_t uextent, size_t vextent,
75 MiscLib::Vector< int > *componentImg,
76 MiscLib::Vector< std::pair< int, size_t > > *labels) const;
77 virtual bool InSpace(size_t u, size_t v, float epsilon,
78 const GfxTL::AABox< GfxTL::Vector2Df > &bbox, size_t uextent,
79 size_t vextent, Vec3f *p, Vec3f *n) const = 0;
80 template< class IteratorT >
81 void BuildBitmap(const PointCloud &pc, float *epsilon, IteratorT begin,
82 IteratorT end, MiscLib::Vector< std::pair< float, float > > *params,
83 GfxTL::AABox< GfxTL::Vector2Df > *bbox,
84 MiscLib::Vector< char > *bitmap, size_t *uextent, size_t *vextent,
85 MiscLib::Vector< size_t > *bmpIdx) const;
86 template< class IteratorT >
87 void BuildBitmap(const PointCloud &pc, float *epsilon, IteratorT begin,
88 IteratorT end, MiscLib::Vector< std::pair< float, float > > *params,
89 GfxTL::AABox< GfxTL::Vector2Df > *bbox,
90 MiscLib::Vector< char > *bitmap, size_t *uextent, size_t *vextent,
91 MiscLib::Vector< size_t > *bmpIdx, size_t border) const;
92 void BuildPolygons(const PointCloud &pc, float epsilon, size_t begin,
93 size_t end, GfxTL::AABox< GfxTL::Vector2Df > *bbox,
94 size_t *uextent, size_t *vextent,
95 std::deque< ComponentPolygons > *polys) const;
96
97 protected:
98 mutable GfxTL::AABox< GfxTL::Vector2Df > m_extBbox;
99 };
100
101 template< class IteratorT >
102 void BitmapPrimitiveShape::BuildBitmap(const PointCloud &pc, float *epsilon,
103 IteratorT begin, IteratorT end, MiscLib::Vector< std::pair< float, float > > *params,
104 GfxTL::AABox< GfxTL::Vector2Df > *bbox, MiscLib::Vector< char > *bitmap,
105 size_t *uextent, size_t *vextent, MiscLib::Vector< size_t > *bmpIdx) const
106 {
107 size_t size = end - begin;
108 params->resize(size);
109 // compute parameters and extent
110 Parameters(GfxTL::IndexIterate(begin, pc.begin()),
111 GfxTL::IndexIterate(end, pc.begin()), params);
112 bbox->Min() = GfxTL::Vector2Df(std::numeric_limits< float >::infinity(),
113 std::numeric_limits< float >::infinity());
114 bbox->Max() = -bbox->Min();
115 for(size_t i = 0; i < size; ++i)
116 {
117 if((*params)[i].first < bbox->Min()[0])
118 bbox->Min()[0] = (*params)[i].first;
119 if((*params)[i].first > bbox->Max()[0])
120 bbox->Max()[0] = (*params)[i].first;
121 if((*params)[i].second < bbox->Min()[1])
122 bbox->Min()[1] = (*params)[i].second;
123 if((*params)[i].second > bbox->Max()[1])
124 bbox->Max()[1] = (*params)[i].second;
125 }
126 // bbox gives the bounding box in parameter space
127 // we can now set up the bitmap
128 const_cast< BitmapPrimitiveShape * >(this)->BitmapExtent(*epsilon, bbox, params,
129 uextent, vextent);
130 if(*uextent < 2)
131 *uextent = 2;
132 if(*vextent < 2)
133 *vextent = 2;
134 bitmap->resize((*uextent) * (*vextent));
135 std::fill(bitmap->begin(), bitmap->end(), false);
136 // set all true bits in bitmap
137 bmpIdx->resize(params->size());
138 #ifdef DOPARALLEL
139 #pragma omp parallel for schedule(static)
140 #endif
141 for(int i = 0; i < size; ++i)
142 {
143 std::pair< int, int > bmpParam;
144 InBitmap((*params)[i], *epsilon, *bbox, *uextent, *vextent, &bmpParam);
145 // clamp bitmap coords
146 bmpParam.first = GfxTL::Math< int >::Clamp(bmpParam.first, 0, static_cast<int>(*uextent) - 1);
147 bmpParam.second = GfxTL::Math< int >::Clamp(bmpParam.second, 0, static_cast<int>(*vextent) - 1);
148 (*bitmap)[(*bmpIdx)[i] = bmpParam.first + bmpParam.second * (*uextent)] = true;
149 }
150 }
151
152 template< class IteratorT >
153 void BitmapPrimitiveShape::BuildBitmap(const PointCloud &pc, float *epsilon,
154 IteratorT begin, IteratorT end, MiscLib::Vector< std::pair< float, float > > *params,
155 GfxTL::AABox< GfxTL::Vector2Df > *bbox, MiscLib::Vector< char > *bitmap,
156 size_t *uextent, size_t *vextent, MiscLib::Vector< size_t > *bmpIdx,
157 size_t border) const
158 {
159 params->resize(end - begin);
160 // compute parameters and extent
161 Parameters(pc[*begin].pos, &(*params)[0]);
162 bbox->Min() = bbox->Max() = GfxTL::Vector2Df((*params)[0].first,
163 (*params)[0].second);
164 size_t j = 1;
165 IteratorT i = begin;
166 for(++i; i != end; ++i, ++j)
167 {
168 Parameters(pc[*i].pos, &(*params)[j]);
169 if(bbox->Min()[0] > (*params)[j].first)
170 bbox->Min()[0] = (*params)[j].first;
171 else if(bbox->Max()[0] < (*params)[j].first)
172 bbox->Max()[0] = (*params)[j].first;
173 if(bbox->Min()[1] > (*params)[j].second)
174 bbox->Min()[1] = (*params)[j].second;
175 else if(bbox->Max()[1] < (*params)[j].second)
176 bbox->Max()[1] = (*params)[j].second;
177 }
178 // bbox gives the bounding box in parameter space
179 // we can now set up the bitmap
180 const_cast< BitmapPrimitiveShape * >(this)->BitmapExtent(*epsilon, bbox, params,
181 uextent, vextent);
182 if(*uextent < 2)
183 *uextent = 2;
184 if(*vextent < 2)
185 *vextent = 2;
186 bitmap->resize(((*uextent) + 2 * border) * ((*vextent) + 2 * border));
187 std::fill(bitmap->begin(), bitmap->end(), false);
188 // set all true bits in bitmap
189 bmpIdx->resize(params->size());
190 size_t lineWidth = (*uextent) + 2 * border;
191 for(size_t i = 0; i < params->size(); ++i)
192 {
193 std::pair< int, int > bmpParam;
194 InBitmap((*params)[i], *epsilon, *bbox, *uextent, *vextent, &bmpParam);
195 // clamp bitmap coords
196 bmpParam.first = GfxTL::Math< int >::Clamp(bmpParam.first, 0, *uextent - 1);
197 bmpParam.second = GfxTL::Math< int >::Clamp(bmpParam.second, 0, *vextent - 1);
198 (*bitmap)[(*bmpIdx)[i] = bmpParam.first + border
199 + (bmpParam.second + border) * lineWidth] = true;
200 }
201 }
202
203 #endif