BitmapPrimitiveShape.h
Go to the documentation of this file.
1#ifndef BITMAPPRIMITIVESHAPE_HEADER
2#define BITMAPPRIMITIVESHAPE_HEADER
3#include <algorithm>
4#include <istream>
5
7#include "IndexIterator.h"
8#include <GfxTL/AABox.h>
10#include <GfxTL/MathHelper.h>
11#include <MiscLib/Pair.h>
12#include <MiscLib/Performance.h>
13#include <MiscLib/Vector.h>
14#ifdef DOPARALLEL
15#include <omp.h>
16#endif
17
18#ifndef DLL_LINKAGE
19#define DLL_LINKAGE
20#endif
21
30
32{
33public:
34 bool Init(bool binary, std::istream* i);
35 size_t ConnectedComponent(const PointCloud& pc,
36 float epsilon,
38 bool doFiltering = true,
39 float* borderRatio = 0);
40 size_t AllConnectedComponents(const PointCloud& pc,
41 float epsilon,
42 BitmapInfo& bitmapInfo,
44 MiscLib::Vector<int>& componentsImg,
45 MiscLib::Vector<std::pair<int, size_t>>& labels,
46 bool doFiltering = true);
47 void TrimmingPolygons(const PointCloud& pc,
48 float epsilon,
49 size_t begin,
50 size_t end,
51 std::deque<ComponentPolygons>* polys) const;
52 void GenerateBitmapPoints(const PointCloud& pc,
53 float epsilon,
54 size_t begin,
55 size_t end,
56 PointCloud* bmpPc) const;
57
58public:
59 virtual void Parameters(const Vec3f& p, std::pair<float, float>* param) const = 0;
60 virtual bool InSpace(float u, float v, Vec3f* p, Vec3f* n) const = 0;
61 virtual void Parameters(
64 MiscLib::Vector<std::pair<float, float>>* bmpParams) const = 0;
67 MiscLib::Vector<std::pair<float, float>>* bmpParams) const = 0;
68 virtual void BitmapExtent(float epsilon,
70 MiscLib::Vector<std::pair<float, float>>* params,
71 size_t* uextent,
72 size_t* vextent) = 0;
73 virtual void InBitmap(const std::pair<float, float>& param,
74 float epsilon,
76 size_t uextent,
77 size_t vextent,
78 std::pair<int, int>* inBmp) const = 0;
79 virtual void PreWrapBitmap(const GfxTL::AABox<GfxTL::Vector2Df>& bbox,
80 float epsilon,
81 size_t uextent,
82 size_t vextent,
83 MiscLib::Vector<char>* bmp) const;
85 float epsilon,
86 bool* uwrap,
87 bool* vwrap) const = 0;
88 virtual void WrapComponents(const GfxTL::AABox<GfxTL::Vector2Df>& bbox,
89 float epsilon,
90 size_t uextent,
91 size_t vextent,
92 MiscLib::Vector<int>* componentImg,
93 MiscLib::Vector<std::pair<int, size_t>>* labels) const;
94 virtual bool InSpace(size_t u,
95 size_t v,
96 float epsilon,
98 size_t uextent,
99 size_t vextent,
100 Vec3f* p,
101 Vec3f* n) const = 0;
102 template <class IteratorT>
103 void BuildBitmap(const PointCloud& pc,
104 float* epsilon,
105 IteratorT begin,
106 IteratorT end,
107 MiscLib::Vector<std::pair<float, float>>* params,
109 MiscLib::Vector<char>* bitmap,
110 size_t* uextent,
111 size_t* vextent,
112 MiscLib::Vector<size_t>* bmpIdx) const;
113 template <class IteratorT>
114 void BuildBitmap(const PointCloud& pc,
115 float* epsilon,
116 IteratorT begin,
117 IteratorT end,
118 MiscLib::Vector<std::pair<float, float>>* params,
120 MiscLib::Vector<char>* bitmap,
121 size_t* uextent,
122 size_t* vextent,
124 size_t border) const;
125 void BuildPolygons(const PointCloud& pc,
126 float epsilon,
127 size_t begin,
128 size_t end,
130 size_t* uextent,
131 size_t* vextent,
132 std::deque<ComponentPolygons>* polys) const;
133
134protected:
136};
137
138template <class IteratorT>
139void
141 float* epsilon,
142 IteratorT begin,
143 IteratorT end,
144 MiscLib::Vector<std::pair<float, float>>* params,
146 MiscLib::Vector<char>* bitmap,
147 size_t* uextent,
148 size_t* vextent,
149 MiscLib::Vector<size_t>* bmpIdx) const
150{
151 int size = end - begin;
152 params->resize(size);
153 // compute parameters and extent
155 GfxTL::IndexIterate(begin, pc.begin()), GfxTL::IndexIterate(end, pc.begin()), params);
156 bbox->Min() = GfxTL::Vector2Df(std::numeric_limits<float>::infinity(),
157 std::numeric_limits<float>::infinity());
158 bbox->Max() = -bbox->Min();
159 for (size_t i = 0; i < (size_t)size; ++i)
160 {
161 if ((*params)[i].first < bbox->Min()[0])
162 {
163 bbox->Min()[0] = (*params)[i].first;
164 }
165 if ((*params)[i].first > bbox->Max()[0])
166 {
167 bbox->Max()[0] = (*params)[i].first;
168 }
169 if ((*params)[i].second < bbox->Min()[1])
170 {
171 bbox->Min()[1] = (*params)[i].second;
172 }
173 if ((*params)[i].second > bbox->Max()[1])
174 {
175 bbox->Max()[1] = (*params)[i].second;
176 }
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, uextent, vextent);
181 if (*uextent < 2)
182 {
183 *uextent = 2;
184 }
185 if (*vextent < 2)
186 {
187 *vextent = 2;
188 }
189 bitmap->resize((*uextent) * (*vextent));
190 std::fill(bitmap->begin(), bitmap->end(), false);
191 // set all true bits in bitmap
192 bmpIdx->resize(params->size());
193 //#pragma omp parallel for schedule(static)
194 for (int i = 0; i < size; ++i)
195 {
196 std::pair<int, int> bmpParam;
197 InBitmap((*params)[i], *epsilon, *bbox, *uextent, *vextent, &bmpParam);
198 // clamp bitmap coords
199 bmpParam.first = GfxTL::Math<int>::Clamp(bmpParam.first, 0, *uextent - 1);
200 bmpParam.second = GfxTL::Math<int>::Clamp(bmpParam.second, 0, *vextent - 1);
201 (*bitmap)[(*bmpIdx)[i] = bmpParam.first + bmpParam.second * (*uextent)] = true;
202 }
203}
204
205template <class IteratorT>
206void
208 float* epsilon,
209 IteratorT begin,
210 IteratorT end,
211 MiscLib::Vector<std::pair<float, float>>* params,
213 MiscLib::Vector<char>* bitmap,
214 size_t* uextent,
215 size_t* vextent,
217 size_t border) const
218{
219 params->resize(end - begin);
220 // compute parameters and extent
221 Parameters(pc[*begin].pos, &(*params)[0]);
222 bbox->Min() = bbox->Max() = GfxTL::Vector2Df((*params)[0].first, (*params)[0].second);
223 size_t j = 1;
224 IteratorT i = begin;
225 for (++i; i != end; ++i, ++j)
226 {
227 Parameters(pc[*i].pos, &(*params)[j]);
228 if (bbox->Min()[0] > (*params)[j].first)
229 {
230 bbox->Min()[0] = (*params)[j].first;
231 }
232 else if (bbox->Max()[0] < (*params)[j].first)
233 {
234 bbox->Max()[0] = (*params)[j].first;
235 }
236 if (bbox->Min()[1] > (*params)[j].second)
237 {
238 bbox->Min()[1] = (*params)[j].second;
239 }
240 else if (bbox->Max()[1] < (*params)[j].second)
241 {
242 bbox->Max()[1] = (*params)[j].second;
243 }
244 }
245 // bbox gives the bounding box in parameter space
246 // we can now set up the bitmap
247 const_cast<BitmapPrimitiveShape*>(this)->BitmapExtent(*epsilon, bbox, params, uextent, vextent);
248 if (*uextent < 2)
249 {
250 *uextent = 2;
251 }
252 if (*vextent < 2)
253 {
254 *vextent = 2;
255 }
256 bitmap->resize(((*uextent) + 2 * border) * ((*vextent) + 2 * border));
257 std::fill(bitmap->begin(), bitmap->end(), false);
258 // set all true bits in bitmap
259 bmpIdx->resize(params->size());
260 size_t lineWidth = (*uextent) + 2 * border;
261 for (size_t i = 0; i < params->size(); ++i)
262 {
263 std::pair<int, int> bmpParam;
264 InBitmap((*params)[i], *epsilon, *bbox, *uextent, *vextent, &bmpParam);
265 // clamp bitmap coords
266 bmpParam.first = GfxTL::Math<int>::Clamp(bmpParam.first, 0, *uextent - 1);
267 bmpParam.second = GfxTL::Math<int>::Clamp(bmpParam.second, 0, *vextent - 1);
268 (*bitmap)[(*bmpIdx)[i] = bmpParam.first + border + (bmpParam.second + border) * lineWidth] =
269 true;
270 }
271}
272
273#endif
#define DLL_LINKAGE
Definition basic.h:12
virtual void WrapComponents(const GfxTL::AABox< GfxTL::Vector2Df > &bbox, float epsilon, size_t uextent, size_t vextent, MiscLib::Vector< int > *componentImg, MiscLib::Vector< std::pair< int, size_t > > *labels) const
virtual void Parameters(const Vec3f &p, std::pair< float, float > *param) const =0
virtual void WrapBitmap(const GfxTL::AABox< GfxTL::Vector2Df > &bbox, float epsilon, bool *uwrap, bool *vwrap) const =0
virtual void PreWrapBitmap(const GfxTL::AABox< GfxTL::Vector2Df > &bbox, float epsilon, size_t uextent, size_t vextent, MiscLib::Vector< char > *bmp) const
virtual bool InSpace(float u, float v, Vec3f *p, Vec3f *n) const =0
bool Init(bool binary, std::istream *i)
void BuildPolygons(const PointCloud &pc, float epsilon, size_t begin, size_t end, GfxTL::AABox< GfxTL::Vector2Df > *bbox, size_t *uextent, size_t *vextent, std::deque< ComponentPolygons > *polys) const
virtual void Parameters(GfxTL::IndexedIterator< MiscLib::Vector< size_t >::iterator, PointCloud::const_iterator > begin, GfxTL::IndexedIterator< MiscLib::Vector< size_t >::iterator, PointCloud::const_iterator > end, MiscLib::Vector< std::pair< float, float > > *bmpParams) const =0
virtual void InBitmap(const std::pair< float, float > &param, float epsilon, const GfxTL::AABox< GfxTL::Vector2Df > &bbox, size_t uextent, size_t vextent, std::pair< int, int > *inBmp) const =0
virtual void BitmapExtent(float epsilon, GfxTL::AABox< GfxTL::Vector2Df > *bbox, MiscLib::Vector< std::pair< float, float > > *params, size_t *uextent, size_t *vextent)=0
size_t AllConnectedComponents(const PointCloud &pc, float epsilon, BitmapInfo &bitmapInfo, MiscLib::Vector< size_t > *indices, MiscLib::Vector< int > &componentsImg, MiscLib::Vector< std::pair< int, size_t > > &labels, bool doFiltering=true)
virtual bool InSpace(size_t u, size_t v, float epsilon, const GfxTL::AABox< GfxTL::Vector2Df > &bbox, size_t uextent, size_t vextent, Vec3f *p, Vec3f *n) const =0
void BuildBitmap(const PointCloud &pc, float *epsilon, IteratorT begin, IteratorT end, MiscLib::Vector< std::pair< float, float > > *params, GfxTL::AABox< GfxTL::Vector2Df > *bbox, MiscLib::Vector< char > *bitmap, size_t *uextent, size_t *vextent, MiscLib::Vector< size_t > *bmpIdx) const
virtual void Parameters(GfxTL::IndexedIterator< IndexIterator, PointCloud::const_iterator > begin, GfxTL::IndexedIterator< IndexIterator, PointCloud::const_iterator > end, MiscLib::Vector< std::pair< float, float > > *bmpParams) const =0
GfxTL::AABox< GfxTL::Vector2Df > m_extBbox
Point & Min()
Definition AABox.hpp:68
Point & Max()
Definition AABox.hpp:82
static ScalarT Clamp(ScalarT s, ScalarT bottom, ScalarT top)
Definition MathHelper.h:39
void resize(size_type s, const value_type &v)
Definition Vector.h:227
const Point * const_iterator
Definition Vector.h:25
virtual void GenerateBitmapPoints(const PointCloud &pc, float epsilon, size_t begin, size_t end, PointCloud *bmpPc) const =0
virtual void TrimmingPolygons(const PointCloud &pc, float epsilon, size_t begin, size_t end, std::deque< ComponentPolygons > *polys) const =0
virtual size_t ConnectedComponent(const PointCloud &pc, float epsilon, MiscLib::Vector< size_t > *indices, bool doFiltering=true, float *borderRatio=0)=0
Definition basic.h:18
IndexedIterator< IndexIteratorT, IteratorT > IndexIterate(IndexIteratorT idxIt, IteratorT it)
VectorXD< 2, float > Vector2Df
Definition VectorXD.h:716
MiscLib::Vector< char > bitmap
GfxTL::AABox< GfxTL::Vector2Df > bbox
MiscLib::Vector< std::pair< float, float > > params
MiscLib::Vector< size_t > bmpIdx