Project

General

Profile

BitmapToolkit Scol plugin
ArManager.cpp
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25#include "ArManager.h"
26#include "ArCameraParam.h"
27
28#include <vector>
29
30// Initialize singleton
31ArManager* ArManager::_singleton = 0;
32
33ArManager::ArManager()
34{
35 detector = 0;
36 fdetector = 0;
37 facedetector = 0;
38 tkdetector = 0;
39
40#ifdef HAVE_BTSLAM
41 slamdetector = 0;
42#endif
43}
44
46{
47 SAFE_DELETE(detector);
48 SAFE_DELETE(fdetector);
49 SAFE_DELETE(facedetector);
50 SAFE_DELETE(tkdetector);
51
52#ifdef HAVE_BTSLAM
53 SAFE_DELETE(slamdetector);
54#endif
55 //SAFE_DELETE(ndetector);
56
57 for (unsigned int i = 0; i < bdetectorList.size(); i ++)
58 SAFE_DELETE(bdetectorList[i]);
59
60 bdetectorList.clear();
61
62 for (MarkerList::iterator imarker = markerList.begin(); imarker != markerList.end(); imarker++)
63 {
64 ArMarker* marker = *imarker;
65 SAFE_DELETE(marker);
66 }
67 markerList.clear();
68}
69
71{
72 if (0 == _singleton)
73 {
74 _singleton = new ArManager();
75 }
76
77 return _singleton;
78}
79
81{
82 if (0 != _singleton)
83 {
84 delete _singleton;
85 _singleton = 0;
86 }
87}
88
89ArBlobDetector* ArManager::AddBlobDetector(cv::SimpleBlobDetector::Params params)
90{
91 ArBlobDetector* detector = new ArBlobDetector(params);
92 bdetectorList.push_back(detector);
93
94 return detector;
95}
96
98{
99 for (unsigned int i = 0; i < bdetectorList.size(); i++)
100 {
101 if (bdetectorList[i] == detector)
102 {
103 SAFE_DELETE(detector);
104 bdetectorList.erase(bdetectorList.begin() + i);
105 return;
106 }
107 }
108}
109
110#ifdef HAVE_BTSLAM
111void ArManager::EnableSlamDetector(bool state)
112{
113 if (state && slamdetector)
114 return;
115 else if (state)
116 slamdetector = new Sslam();
117 else if (!state)
118 SAFE_DELETE(slamdetector);
119}
120
121void ArManager::SetSlamDetectorParams(int nbp, int nbimp, int minf, int maxf)
122{
123 dso::setting_desiredPointDensity = nbp;
124 dso::setting_desiredImmatureDensity = nbimp;
125 dso::setting_minFrames = minf;
126 dso::setting_maxFrames = maxf;
127 if (slamdetector)
128 slamdetector->Reset();
129}
130
131Vector3 ArManager::GetSlamCameraPostition()
132{
133 if (!slamdetector)
134 return Vector3();
135
136 return slamdetector->GetCameraPosition();
137}
138
139BtQuaternion ArManager::GetSlamCameraOrientation()
140{
141 if (!slamdetector)
142 return BtQuaternion();
143
144 return slamdetector->GetCameraOrientation();
145}
146
147bool ArManager::IsSlamFound()
148{
149 if (!slamdetector)
150 return false;
151
152 return slamdetector->IsFound();
153}
154
155void ArManager::ResetSlamDetector()
156{
157 if (!slamdetector)
158 return;
159
160 return slamdetector->Reset();
161}
162#endif
163
164void ArManager::UpdateMarkers(cv::Mat image, ArCameraParam* arCameraParam, bool reversedBitmap, bool debugDraw)
165{
166 if (image.cols != arCameraParam->GetBitmapSize().width || image.rows != arCameraParam->GetBitmapSize().height)
167 return;
168
169#ifdef HAVE_BTSLAM
170 if (!image.empty() && (detector || fdetector || tkdetector || slamdetector || facedetector || !bdetectorList.empty()))
171#else
172 if (!image.empty() && (detector || fdetector || tkdetector || facedetector || !bdetectorList.empty()))
173#endif
174 {
175 //just don't call update when the camera return nil to avoid double detection
176 /*
177 try
178 {
179 if (!lastData.frame.empty())
180 {
181 cv::Mat diff;
182 cv::cvtColor(image, diff, cv::COLOR_BGR2GRAY);
183 cv::compare(lastData.image, diff, diff, cv::CMP_NE);
184
185 if (cv::countNonZero(diff) == 0)
186 return;
187 }
188 }
189 catch(std::exception &)
190 {
191 // camera param
192 aruco::CameraParameters camParam = arCameraParam.GetCameraParameter();
193 lastData = LASTDATA(image, camParam, reversedBitmap);
194 return;
195 }*/
196
197 // camera param
198 {
199 //exclusive access (write)
200 boost::unique_lock< boost::shared_mutex > lock(updateMutex);
201 double timeStamp = std::chrono::duration<double, std::nano>(std::chrono::system_clock::now().time_since_epoch()).count() / 1e9;
202 double delta = (lastData.timeStamp == 0.0) ? 0.0 : timeStamp - lastData.timeStamp;
203 lastData = LASTDATA(image, arCameraParam, reversedBitmap, timeStamp, delta);
204 arCameraParam->ResetCameraImu();
205 }
206
207 if (debugDraw)
208 {
209 //read only
210 boost::shared_lock< boost::shared_mutex > lockList(listMutex);
211 MarkerList::iterator imarker;
212 for (imarker=markerList.begin(); imarker!=markerList.end(); ++imarker)
213 {
214 if((*imarker)->IsVisible())
215 (*imarker)->Draw(image);
216 }
217 }
218
219#ifdef HAVE_BTSLAM
220 if (slamdetector)
221 {
222 slamdetector->needUpdate = true;
223 if (debugDraw)
224 slamdetector->DrawLandmarks(image);
225 }
226#endif
227
228 if (tkdetector)
229 tkdetector->needUpdate = true;
230
231 if (fdetector)
232 fdetector->needUpdate = true;
233
234 if(facedetector)
235 facedetector->needUpdate = true;
236
237 for (unsigned int i = 0; i < bdetectorList.size(); i ++)
238 bdetectorList[i]->needUpdate = true;
239
240 if (detector)
241 detector->needUpdate = true;
242 }
243}
244
245ArMarker* ArManager::AddMarker(int index, float size)
246{
247 //conditional lock
248 boost::upgrade_lock< boost::shared_mutex > lock(listMutex);
249 //take the lock exclusive
250 boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
251
252 if(!detector)
253 {
254 detector = new ArDetector();
255 }
256
257 ArMarker* marker = 0;
258 if ((index >= 0) || (index < 1024))
259 {
260 marker = new ArMarker(index, size, ArMarker::AR_ARUCO_MARKER);
261 markerList.insert(marker);
262 }
263 return marker;
264}
265
266ArMarker* ArManager::AddMarker(std::string path, float size, unsigned int maxFeatures)
267{
268 //conditional lock
269 boost::upgrade_lock< boost::shared_mutex > lock(listMutex);
270 //take the lock exclusive
271 boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
272
273 if (!tkdetector)
274 {
275 tkdetector = new ArTkDetector();
276 }
277
278 /*if(!fdetector)
279 {
280 fdetector = new ArFeaturedDetector();
281 }*/
282
283 //ArMarker* marker = new ArFeaturedMarker(path, CalcNewID(), size, maxFeatures);
284 ArMarker* marker = new ArTkMarker(path, CalcNewID(), size, maxFeatures);
285 tkdetector->AddMarker(marker);
286 markerList.insert(marker);
287
288 return marker;
289}
290
291ArMarker* ArManager::AddMarker(cv::Mat tpl, float size, unsigned int maxFeatures)
292{
293 //conditional lock
294 boost::upgrade_lock< boost::shared_mutex > lock(listMutex);
295 //take the lock exclusive
296 boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
297/*
298 if (!tkdetector)
299 {
300 tkdetector = new ArTkDetector();
301 }*/
302
303 if(!fdetector)
304 {
305 fdetector = new ArFeaturedDetector();
306 }
307
308 ArMarker* marker = new ArFeaturedMarker(tpl, CalcNewID(), size, maxFeatures);
309 markerList.insert(marker);
310
311 return marker;
312}
313
315{
316 //conditional lock
317 boost::upgrade_lock< boost::shared_mutex > lock(listMutex);
318 //take the lock exclusive
319 boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
320
321 if(!facedetector)
322 {
323 facedetector = new ArFaceDetector();
324 }
325
326 ArMarker* marker = new ArFaceMarker(CalcNewID(), size);
327 markerList.insert(marker);
328
329 return marker;
330}
331
333{
334 //conditional lock
335 boost::upgrade_lock< boost::shared_mutex > lock(listMutex);
336
337 MarkerList::iterator imarker = markerList.find(marker);
338 if (imarker != markerList.end())
339 {
340 //take the lock exclusive
341 boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
342
343 if (tkdetector && (*imarker)->GetType() == ArMarker::AR_ARTK_FFT_MARKER)
344 tkdetector->RemoveMarker(*imarker);
345
346 markerList.erase(imarker);
347
348 SAFE_DELETE(marker);
349 }
350
351 if(markerList.size() == 0)
352 {
353 SAFE_DELETE(detector);
354 SAFE_DELETE(fdetector);
355 SAFE_DELETE(facedetector);
356 SAFE_DELETE(tkdetector);
357 }
358}
359
360void ArManager::updateFiducialMarkers(std::vector<aruco::Marker> detectedMarkers, LASTDATA lastd)
361{
362 //read only
363 boost::shared_lock< boost::shared_mutex > lockList(listMutex);
364
365 std::vector<aruco::Marker*> matchList;
366 matchList.resize(1024, 0);
367
368 for(unsigned int i = 0; i < detectedMarkers.size(); i++)
369 {
370 if (detectedMarkers[i].isValid())
371 matchList[detectedMarkers[i].id] = &(detectedMarkers[i]);
372 }
373
374 for (MarkerList::iterator imarker = markerList.begin(); imarker != markerList.end(); ++imarker)
375 {
376 if ((*imarker)->GetType() == ArMarker::AR_ARUCO_MARKER)
377 {
378 aruco::Marker* amarker = matchList[(*imarker)->GetID()];
379 if(amarker != 0)
380 {
381 (*imarker)->Update(lastd.arCamParam, *amarker, lastd.reversedBitmap);
382 }
383 else
384 {
385 (*imarker)->SetVisible(false);
386 }
387 }
388 }
389}
390
391unsigned int ArManager::CalcNewID()
392{
393 unsigned int id = 1024;
394 MarkerList::iterator imarker;
395 for (imarker = markerList.begin(); imarker != markerList.end(); ++imarker)
396 {
397 id = std::max(id, (*imarker)->GetID());
398 }
399 return id + 1;
400}
401
403{
404 boost::shared_lock< boost::shared_mutex > lockUpdate(updateMutex);
405 data = LASTDATA(lastData);
406}
407
409{
410 if (tkdetector)
411 {
412 tkdetector->SetDirty();
413 }
414}
cv::Size GetBitmapSize()
bool needUpdate
Definition ArDetector.h:50
static void Kill()
Definition ArManager.cpp:80
void GetLastData(LASTDATA &data)
void RemoveBlobDetector(ArBlobDetector *detector)
Definition ArManager.cpp:97
boost::shared_mutex updateMutex
Definition ArManager.h:49
void SetNFTdetectorDirty()
ArMarker * AddFaceMarker(float size)
ArMarker * AddMarker(int index, float size)
ArBlobDetector * AddBlobDetector(cv::SimpleBlobDetector::Params params)
Definition ArManager.cpp:89
MarkerList markerList
Definition ArManager.h:51
static ArManager * GetInstance()
Definition ArManager.cpp:70
boost::shared_mutex listMutex
Definition ArManager.h:50
void updateFiducialMarkers(std::vector< aruco::Marker > detectedMarkers, LASTDATA lastd)
void RemoveMarker(ArMarker *marker)
void UpdateMarkers(cv::Mat image, ArCameraParam *arCameraParam, bool reversedBitmap=false, bool debugDraw=false)
@ AR_ARUCO_MARKER
Definition ArMarker.h:42
@ AR_ARTK_FFT_MARKER
Definition ArMarker.h:45
void RemoveMarker(ArMarker *marker)
void AddMarker(ArMarker *marker)
This class represents a marker. It is a vector of the fours corners ot the marker.
Definition ArTkMarker.h:51
ArCameraParam arCamParam
bool reversedBitmap
double timeStamp
Definition sSlam.h:74