ModelLoader_impl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * National Institute of Advanced Industrial Science and Technology (AIST)
8  */
9 
15 #include "ModelLoader_impl.h"
16 
17 #include <iostream>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 
21 #include "VrmlUtil.h"
22 
23 using namespace std;
24 
25 #ifdef OPENHRP_COLLADA_FOUND
26 #include <boost/foreach.hpp>
27 #include "ColladaWriter.h"
28 #include "BodyInfoCollada_impl.h"
29 #include "SceneInfoCollada_impl.h"
30 
31 static bool IsColladaFile(const std::string& filename)
32 {
33  size_t len = filename.size();
34  if( len < 4 ) {
35  return false;
36  }
37  if( filename[len-4] == '.' && ::tolower(filename[len-3]) == 'd' && ::tolower(filename[len-2]) == 'a' && ::tolower(filename[len-1]) == 'e' ) {
38  return true;
39  }
40  if( filename[len-4] == '.' && ::tolower(filename[len-3]) == 'z' && ::tolower(filename[len-2]) == 'a' && ::tolower(filename[len-1]) == 'e' ) {
41  return true;
42  }
43  return false;
44 }
45 
46 #endif
47 
48 std::string replaceProjectDir(std::string url) {
49  std::string path = url;
50  if ( path.find("$(PROJECT_DIR)") != std::string::npos ) {
51  std::string shdir = OPENHRP_SHARE_DIR;
52  std::string pjdir = shdir + "/sample/project";
53  path.replace(path.find("$(PROJECT_DIR)"),14, pjdir);
54  }
55  return path;
56 }
57 
58 ModelLoader_impl::ModelLoader_impl(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa)
59  :
60  orb(CORBA::ORB::_duplicate(orb)),
61  poa(PortableServer::POA::_duplicate(poa))
62 {
63 
64 }
65 
66 
68 {
69  clearData();
70 }
71 
72 
73 PortableServer::POA_ptr ModelLoader_impl::_default_POA()
74 {
75  return PortableServer::POA::_duplicate(poa);
76 }
77 
78 // the dynamic casts are necessary since the changetoBoundingBox functions are not part of BodyInfo class.
79 static void setLastUpdateTime(POA_OpenHRP::BodyInfo* bodyInfo, time_t time)
80 {
81  BodyInfo_impl* pBodyInfo_impl = dynamic_cast<BodyInfo_impl*>(bodyInfo);
82  if( !!pBodyInfo_impl ) {
83  pBodyInfo_impl->setLastUpdateTime(time);
84  return;
85  }
86 #ifdef OPENHRP_COLLADA_FOUND
87  BodyInfoCollada_impl* pBodyInfoCollada_impl = dynamic_cast<BodyInfoCollada_impl*>(bodyInfo);
88  if( !!pBodyInfoCollada_impl ) {
89  pBodyInfoCollada_impl->setLastUpdateTime(time);
90  return;
91  }
92 #endif
93  throw ModelLoader::ModelLoaderException("setLastUpdateTime invalid pointer");
94 };
95 
96 static time_t getLastUpdateTime(POA_OpenHRP::BodyInfo* bodyInfo) {
97  BodyInfo_impl* pBodyInfo_impl = dynamic_cast<BodyInfo_impl*>(bodyInfo);
98  if( !!pBodyInfo_impl ) {
99  return pBodyInfo_impl->getLastUpdateTime();
100  }
101 #ifdef OPENHRP_COLLADA_FOUND
102  BodyInfoCollada_impl* pBodyInfoCollada_impl = dynamic_cast<BodyInfoCollada_impl*>(bodyInfo);
103  if( !!pBodyInfoCollada_impl ) {
104  return pBodyInfoCollada_impl->getLastUpdateTime();
105  }
106 #endif
107  throw ModelLoader::ModelLoaderException("getLastUpdateTime invalid pointer");
108 }
109 
110 static bool checkInlineFileUpdateTime(POA_OpenHRP::BodyInfo* bodyInfo)
111 {
112  BodyInfo_impl* pBodyInfo_impl = dynamic_cast<BodyInfo_impl*>(bodyInfo);
113  if( !!pBodyInfo_impl ) {
114  return pBodyInfo_impl->checkInlineFileUpdateTime();
115  }
116 #ifdef OPENHRP_COLLADA_FOUND
117  BodyInfoCollada_impl* pBodyInfoCollada_impl = dynamic_cast<BodyInfoCollada_impl*>(bodyInfo);
118  if( !!pBodyInfoCollada_impl ) {
119  return pBodyInfoCollada_impl->checkInlineFileUpdateTime();
120  }
121 #endif
122  throw ModelLoader::ModelLoaderException("checkInlineFileUpdateTime invalid pointer");
123 }
124 
125 static void setParam(POA_OpenHRP::BodyInfo* bodyInfo, std::string param, int value)
126 {
127  BodyInfo_impl* pBodyInfo_impl = dynamic_cast<BodyInfo_impl*>(bodyInfo);
128  if( !!pBodyInfo_impl ) {
129  pBodyInfo_impl->setParam(param,value);
130  return;
131  }
132 #ifdef OPENHRP_COLLADA_FOUND
133  BodyInfoCollada_impl* pBodyInfoCollada_impl = dynamic_cast<BodyInfoCollada_impl*>(bodyInfo);
134  if( !!pBodyInfoCollada_impl ) {
135  pBodyInfoCollada_impl->setParam(param,value);
136  return;
137  }
138 #endif
139  throw ModelLoader::ModelLoaderException("setParam(param,value) invalid pointer");
140 }
141 
142 static void changetoBoundingBox(POA_OpenHRP::BodyInfo* bodyInfo, unsigned int* depth)
143 {
144  BodyInfo_impl* pBodyInfo_impl = dynamic_cast<BodyInfo_impl*>(bodyInfo);
145  if( !!pBodyInfo_impl ) {
146  pBodyInfo_impl->changetoBoundingBox(depth);
147  return;
148  }
149 #ifdef OPENHRP_COLLADA_FOUND
150  BodyInfoCollada_impl* pBodyInfoCollada_impl = dynamic_cast<BodyInfoCollada_impl*>(bodyInfo);
151  if( !!pBodyInfoCollada_impl ) {
152  pBodyInfoCollada_impl->changetoBoundingBox(depth);
153  return;
154  }
155 #endif
156  throw ModelLoader::ModelLoaderException("changetoBoundingBox(depth) invalid pointer");
157 }
158 
159 BodyInfo_ptr ModelLoader_impl::loadBodyInfo(const char* url)
160  throw (CORBA::SystemException, OpenHRP::ModelLoader::ModelLoaderException)
161 {
162  OpenHRP::ModelLoader::ModelLoadOption option;
163  option.readImage = false;
164  option.AABBdata.length(0);
165  option.AABBtype = OpenHRP::ModelLoader::AABB_NUM;
166  POA_OpenHRP::BodyInfo* bodyInfo = loadBodyInfoFromModelFile(replaceProjectDir(std::string(url)), option);
167  return bodyInfo->_this();
168 }
169 
170 BodyInfo_ptr ModelLoader_impl::loadBodyInfoEx(const char* url, const OpenHRP::ModelLoader::ModelLoadOption& option)
171  throw (CORBA::SystemException, OpenHRP::ModelLoader::ModelLoaderException)
172 {
173  POA_OpenHRP::BodyInfo* bodyInfo = loadBodyInfoFromModelFile(replaceProjectDir(std::string(url)), option);
174  if(option.AABBdata.length()){
175  setParam(bodyInfo,"AABBType", (int)option.AABBtype);
176  int length=option.AABBdata.length();
177  unsigned int* _AABBdata = new unsigned int[length];
178  for(int i=0; i<length; i++)
179  _AABBdata[i] = option.AABBdata[i];
180  changetoBoundingBox(bodyInfo,_AABBdata);
181  delete[] _AABBdata;
182  }
183  return bodyInfo->_this();
184 }
185 
186 BodyInfo_ptr ModelLoader_impl::getBodyInfoEx(const char* url0, const OpenHRP::ModelLoader::ModelLoadOption& option)
187  throw (CORBA::SystemException, OpenHRP::ModelLoader::ModelLoaderException)
188 {
189  string url(url0);
190 
191  BodyInfo_ptr bodyInfo = 0;
193  struct stat statbuff;
194  time_t mtime = 0;
195 
196  // get a file modification time
197  if( stat( filename.c_str(), &statbuff ) == 0 ){
198  mtime = statbuff.st_mtime;
199  }
200 
201  UrlToBodyInfoMap::iterator p = urlToBodyInfoMap.find(url);
202  if(p != urlToBodyInfoMap.end() && mtime == getLastUpdateTime(p->second) && checkInlineFileUpdateTime(p->second)){
203  bodyInfo = p->second->_this();
204  cout << string("cache found for ") + url << endl;
205  if(option.AABBdata.length()){
206  setParam(p->second,"AABBType", (int)option.AABBtype);
207  int length=option.AABBdata.length();
208  unsigned int* _AABBdata = new unsigned int[length];
209  for(int i=0; i<length; i++)
210  _AABBdata[i] = option.AABBdata[i];
211  changetoBoundingBox(p->second,_AABBdata);
212  delete[] _AABBdata;
213  }
214  return bodyInfo;
215  }
216  return loadBodyInfoEx(url0, option);
217 }
218 
219 BodyInfo_ptr ModelLoader_impl::getBodyInfo(const char* url)
220  throw (CORBA::SystemException, OpenHRP::ModelLoader::ModelLoaderException)
221 {
222  OpenHRP::ModelLoader::ModelLoadOption option;
223  option.readImage = false;
224  option.AABBdata.length(0);
225  option.AABBtype = OpenHRP::ModelLoader::AABB_NUM;
226  return getBodyInfoEx(url, option);
227 }
228 
229 POA_OpenHRP::BodyInfo* ModelLoader_impl::loadBodyInfoFromModelFile(const string url, const OpenHRP::ModelLoader::ModelLoadOption option)
230 {
231  cout << "loading " << url << endl;
232  POA_OpenHRP::BodyInfo* bodyInfo;
233  try {
234 #ifdef OPENHRP_COLLADA_FOUND
235  if( IsColladaFile(url) ) {
237  p->setParam("readImage", option.readImage);
238  p->loadModelFile(url);
239  bodyInfo = p;
240  }
241  else
242 #endif
243  {
244  BodyInfo_impl* p = new BodyInfo_impl(poa);
245  p->setParam("readImage", option.readImage);
246  p->loadModelFile(url);
247  bodyInfo = p;
248  }
249  }
250  catch(OpenHRP::ModelLoader::ModelLoaderException& ex){
251  cout << "loading failed.\n";
252  cout << ex.description << endl;
253  //bodyInfo->_remove_ref();
254  throw;
255  }
256 
257  cout << "The model was successfully loaded ! " << endl;
258 
259 #ifdef OPENHRP_COLLADA_FOUND
260 // cout << "Saving COLLADA file to hiro.dae" << endl;
261 // ColladaWriter colladawriter;
262 // colladawriter.Write(bodyInfo);
263 // colladawriter.Save("/home/jsk/rdiankov/hiro.dae");
264 #endif
265 
266  //poa->activate_object(bodyInfo);
267  urlToBodyInfoMap[url] = bodyInfo;
268 
269  string filename(deleteURLScheme(url));
270  struct stat statbuff;
271  time_t mtime = 0;
272 
273  // get a file modification time
274  if( stat( filename.c_str(), &statbuff ) == 0 ){
275  mtime = statbuff.st_mtime;
276  }
277  setLastUpdateTime(bodyInfo, mtime );
278 
279  return bodyInfo;
280 }
281 
282 
283 
284 SceneInfo_ptr ModelLoader_impl::loadSceneInfo(const char* url)
285  throw (CORBA::SystemException, OpenHRP::ModelLoader::ModelLoaderException)
286 {
287  cout << "loading " << url << endl;
288 
289  SceneInfo_impl* sceneInfo;
290  try {
291 #ifdef OPENHRP_COLLADA_FOUND
292  if( IsColladaFile(url) ) {
294  p->load(url);
295  sceneInfo = p;
296  }
297  else
298 #endif
299  {
301  p->load(url);
302  sceneInfo = p;
303  }
304  }
305  catch(OpenHRP::ModelLoader::ModelLoaderException& ex){
306  cout << "loading failed.\n";
307  cout << ex.description << endl;
308  //sceneInfo->_remove_ref();
309  throw;
310  }
311  cout << url << " was successfully loaded ! " << endl;
312 
313  //poa->activate_object(sceneInfo);
314 
315  return sceneInfo->_this();
316 }
317 
318 
320 {
321  //UrlToBodyInfoMap::iterator p;
322  //for(p = urlToBodyInfoMap.begin(); p != urlToBodyInfoMap.end(); ++p){
323  // BodyInfo_impl* bodyInfo = p->second;
324  // PortableServer::ObjectId_var objectId = poa->servant_to_id(bodyInfo);
325  // poa->deactivate_object(objectId);
326  // bodyInfo->_remove_ref();
327  //}
328  urlToBodyInfoMap.clear();
329 }
330 
331 
333 {
334  clearData();
335  orb->shutdown(false);
336 }
void setParam(std::string param, bool value)
static void setParam(POA_OpenHRP::BodyInfo *bodyInfo, std::string param, int value)
char * filename
Definition: cdjpeg.h:133
virtual void clearData()
UrlToBodyInfoMap urlToBodyInfoMap
static void setLastUpdateTime(POA_OpenHRP::BodyInfo *bodyInfo, time_t time)
std::string replaceProjectDir(std::string url)
png_voidp int value
Definition: png.h:2113
PortableServer::POA_var poa
void setLastUpdateTime(time_t time)
Definition: BodyInfo_impl.h:47
static bool checkInlineFileUpdateTime(POA_OpenHRP::BodyInfo *bodyInfo)
png_bytep png_bytep png_size_t length
Definition: png.h:1541
virtual SceneInfo_ptr loadSceneInfo(const char *url)
png_uint_32 i
Definition: png.h:2735
JCOPY_OPTION option
Definition: transupp.h:131
reads in collada files and initializes a BodyInfo struct
void changetoBoundingBox(unsigned int *depth)
virtual BodyInfo_ptr getBodyInfoEx(const char *url, const OpenHRP::ModelLoader::ModelLoadOption &option)
static void changetoBoundingBox(POA_OpenHRP::BodyInfo *bodyInfo, unsigned int *depth)
virtual BodyInfo_ptr loadBodyInfo(const char *url)
virtual ~ModelLoader_impl()
void loadModelFile(const std::string &filename)
void loadModelFile(const std::string &filename)
This function loads a model file and creates a BodyInfo object.
POA_OpenHRP::BodyInfo * loadBodyInfoFromModelFile(const std::string url, const OpenHRP::ModelLoader::ModelLoadOption option)
void setParam(std::string param, bool value)
virtual BodyInfo_ptr loadBodyInfoEx(const char *url, const OpenHRP::ModelLoader::ModelLoadOption &option)
bool checkInlineFileUpdateTime()
Definition: BodyInfo_impl.h:49
path
time_t getLastUpdateTime()
Definition: BodyInfo_impl.h:48
HRP_UTIL_EXPORT string deleteURLScheme(string url)
Definition: UrlUtil.cpp:25
CORBA::ORB_var orb
void load(const std::string &filename)
virtual BodyInfo_ptr getBodyInfo(const char *url)
void changetoBoundingBox(unsigned int *depth)
void load(const std::string &filename)
static time_t getLastUpdateTime(POA_OpenHRP::BodyInfo *bodyInfo)
void setLastUpdateTime(time_t time)
ModelLoader_impl(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa)
virtual PortableServer::POA_ptr _default_POA()


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat May 8 2021 02:42:39