Kinect Scol plugin
DeviceManager.cpp
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OpenSpace3D
4 For the latest info, see http://www.openspace3d.com
5 
6 Copyright (c) 2012 I-maginer
7 
8 This program is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Lesser General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20 http://www.gnu.org/copyleft/lesser.txt
21 
22 -----------------------------------------------------------------------------
23 */
24 
33 #include "DeviceManager.h"
34 #include "objects/KinectDevice.h"
35 #include "ScolPlugin.h"
36 #include "lib/common.h"
37 
38 template<> DeviceManager* Singleton<DeviceManager>::instance = 0;
39 
40 // Constructor and Destructor
41 DeviceManager::DeviceManager()
42 {
43 }
44 
45 DeviceManager::~DeviceManager()
46 {
47  // Close device manager
48  close();
49 }
50 
51 // Get singleton instance
52 DeviceManager& DeviceManager::GetSingleton()
53 {
54  assert(instance);
55  return *instance;
56 }
57 
58 DeviceManager* DeviceManager::GetSingletonPtr()
59 {
60  return instance;
61 }
62 
63 void DeviceManager::close()
64 {
65  bool closeNI = (listOfKinectDevice.empty()) ? false : true;
66  for (KinectDeviceMap::iterator it = listOfKinectDevice.begin(); it != listOfKinectDevice.end(); it++)
67  {
68  SAFE_DELETE(it->second);
69  }
70 
71  listOfKinectDevice.clear();
72 
73  if (closeNI)
74  {
75  nite::NiTE::shutdown();
76  openni::OpenNI::shutdown();
77  }
78 }
79 
80 unsigned int DeviceManager::createId()
81 {
82  int idFound = 0;
83  while (listOfKinectDevice.find(idFound) != listOfKinectDevice.end())
84  {
85  idFound++;
86  }
87 
88  return idFound;
89 }
90 
91 KinectDevice* DeviceManager::CreateKinectDevice()
92 {
93  if (listOfKinectDevice.empty())
94  {
95  // init openNI
96  if (openni::OpenNI::initialize() != openni::STATUS_OK)
97  {
98  MMechostr(MSKDEBUG," > OpenNI initialization failed ! : %s\n", openni::OpenNI::getExtendedError());
99  }
100 
101  // init nite
102  if (nite::NiTE::initialize() != nite::STATUS_OK)
103  {
104  MMechostr(MSKDEBUG," > NIte initialization failed ! : \n");
105  }
106  }
107 
108  KinectDevice* newKinectDevice = new KinectDevice(createId());
109 
110  addKinectDevice(newKinectDevice);
111  return newKinectDevice;
112 }
113 
114 void DeviceManager::DestroyKinectDevice(KinectDevice* existingKinectDevice)
115 {
116  KinectDeviceMap::iterator iKinectDeviceSearched = listOfKinectDevice.find(existingKinectDevice->GetId());
117  if (iKinectDeviceSearched != listOfKinectDevice.end())
118  {
119  listOfKinectDevice.erase(iKinectDeviceSearched);
120  SAFE_DELETE(existingKinectDevice);
121  }
122  else
123  MMechostr(MSKRUNTIME, "DeviceManagerManager::DestroyKinectDevice : Can't remove Kinect Device");
124 
125  if (listOfKinectDevice.empty())
126  {
127  nite::NiTE::shutdown();
128  openni::OpenNI::shutdown();
129  }
130 }
131 
132 void DeviceManager::addKinectDevice(KinectDevice* existingKinectDevice)
133 {
134  KinectDeviceMap::iterator iKinectDeviceSearched = listOfKinectDevice.find(existingKinectDevice->GetId());
135  if (iKinectDeviceSearched == listOfKinectDevice.end())
136  {
137  listOfKinectDevice.insert(std::make_pair(existingKinectDevice->GetId(), existingKinectDevice));
138  }
139  else
140  MMechostr(MSKRUNTIME, "DeviceManager::addKinectDevice : Kinect Device already Exists");
141 }
142 
143 KinectDevice* DeviceManager::GetKinectDevice(unsigned int id)
144 {
145  KinectDeviceMap::iterator iKinectDeviceSearched = listOfKinectDevice.find(id);
146  if(iKinectDeviceSearched != listOfKinectDevice.end())
147  {
148  return iKinectDeviceSearched->second;
149  }
150  return 0;
151 }
Kinect device handling. .
Definition: KinectDevice.h:39
Handle DeviceManager type. .
Definition: DeviceManager.h:42