wrapper_collada.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
31 #ifndef _wrapper_collada_CPP_
32 #define _wrapper_collada_CPP_
33 
42 //####################################################################
45 
46 //####################################################################
50 
51 
52 void wrapper_collada::add_polygon_fixed_color(std::string polygon_name, pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, pcl::PointCloud<pcl::PointXYZ>::Ptr normal, float r, float g, float b, float a)
53 {
54  //Add geometry
55  daeElement* geomLib = root->add("library_geometries");
56  daeElement* geom = geomLib->add("geometry");
57  geom->setAttribute("id", (polygon_name + "-geom_id").c_str());
58  daeElement* mesh = geom->add("mesh");
59 
60  // Add the position data (vertices)
61  daeElement* source = mesh->add("source");
62  source->setAttribute("id", (geom->getAttribute("id") + "-positions").c_str());
63  domFloat_array* fa = daeSafeCast<domFloat_array>(source->add("float_array"));
64  fa->setId((source->getAttribute("id") + "-array").c_str());
65  fa->setCount((int)cloud->points.size()*3);
66  fa->getValue() = pcl_pointcloud_tp_daearray(cloud);
67  domAccessor* acc = daeSafeCast<domAccessor>(source->add("technique_common accessor"));
68  acc->setSource(makeUriRef(fa->getId()).c_str());
69  std::list<std::string> params = cdom::tokenize("X Y Z", " ");
70  acc->setStride(params.size());
71  acc->setCount((int)cloud->points.size());
72  for (cdom::tokenIter iter = params.begin(); iter != params.end(); iter++)
73  {
74  daeElement* p = acc->add("param");
75  p->setAttribute("name", iter->c_str());
76  p->setAttribute("type", "float");
77  }
78 
79  // Add the normal to the polygon
80  daeElement* source1 = mesh->add("source");
81  source1->setAttribute("id", (geom->getAttribute("id") + "-normals").c_str());
82  domFloat_array* fa1 = daeSafeCast<domFloat_array>(source1->add("float_array"));
83  fa1->setId((source1->getAttribute("id") + "-array").c_str());
84  fa1->setCount((int)normal->points.size()*3); //just one normal
85  fa1->getValue() = pcl_pointcloud_tp_daearray(normal);
86  domAccessor* acc1 = daeSafeCast<domAccessor>(source1->add("technique_common accessor"));
87  acc1->setSource(makeUriRef(fa1->getId()).c_str());
88  std::list<std::string> params1 = cdom::tokenize("X Y Z", " ");
89  acc1->setStride(params1.size());
90  acc1->setCount((int)normal->points.size());
91  for (cdom::tokenIter iter = params1.begin(); iter != params1.end(); iter++)
92  {
93  daeElement* p = acc1->add("param");
94  p->setAttribute("name", iter->c_str());
95  p->setAttribute("type", "float");
96  }
97 
98  // Add the <vertices> index
99  daeElement* vertices = mesh->add("vertices");
100  vertices->setAttribute("id", (geom->getAttribute("id") + "-vertices").c_str());
101  daeElement* verticesInput = vertices->add("input");
102  verticesInput->setAttribute("semantic", "POSITION");
103  verticesInput->setAttribute("source", makeUriRef(geom->getAttribute("id") + "-positions").c_str());
104 
105  //Now finally add the polygon
106  domPolygons* polygons = daeSafeCast<domPolygons>(mesh->add("polygons"));
107  polygons->setCount(1); //just one polygon
108  polygons->setMaterial("mtl"); //just one polygon
109  my_addInput(polygons, "VERTEX", makeUriRef(geom->getAttribute("id") + "-vertices"), 0);
110  my_addInput(polygons, "NORMAL", makeUriRef(geom->getAttribute("id") + "-normals"), 1);
111  domP* p = daeSafeCast<domP>(polygons->add("p"));
112  daeTArray<long long unsigned int> Arr;
113  for (long long unsigned int i = 0; i < (long long unsigned int)cloud->points.size(); i++)
114  {
115  Arr.append(i); //append the vertex
116  Arr.append(0); //append the normal
117  }
118  p->getValue() = Arr;
119 
120  char str[1024];
121  sprintf(str,"%f %f %f %f",r,g,b,a);
122 
123  daeElement* effectLib = root->add("library_effects");
124  daeElement* effect = effectLib->add("effect");
125  effect->setAttribute("id", (polygon_name + "-effect").c_str());
126  daeElement* profile = effect->add("profile_COMMON");
127  daeElement* technique = profile->add("technique");
128  technique->setAttribute("sid", "phong1");
129  daeElement* phong = technique->add("phong");
130  daeElement* emission = phong->add("emission color");
131  emission->setCharData(str);
132  daeElement* ambient = phong->add("ambient color");
133  ambient->setCharData(str);
134  daeElement* diffuse = phong->add("diffuse color");
135  diffuse->setCharData(str);
136  daeElement* specular = phong->add("specular color");
137  specular->setCharData(str);
138  daeElement* shininess = phong->add("shininess float");
139  shininess->setCharData("2.0");
140  daeElement* reflective = phong->add("reflective color");
141  reflective->setCharData(str);
142  daeElement* reflectivity = phong->add("reflectivity float");
143  reflectivity->setCharData("0.5");
144  daeElement* transparent = phong->add("transparent color");
145  transparent->setCharData(str);
146  daeElement* transparency = phong->add("transparency float");
147  transparency->setCharData("1.0");
148 
149  //Material
150  daeElement* materialLib = root->add("library_materials");
151  daeElement* material = materialLib->add("material");
152  material->setAttribute("id", (polygon_name + "-material").c_str());
153  daeElement* instance_effect = material->add("instance_effect");
154  instance_effect->setAttribute("url", ("#" + polygon_name + "-effect").c_str());
155 
156 
157  //Image
158  my_addImage(root);
159 
160  // Add a <node> with a simple transformation
161  daeElement* node = visualScene->add("node");
162  node->setAttribute("id", ("#" + polygon_name + "-node").c_str());
163  node->add("scale")->setCharData("1 1 1");
164 
165  // Instantiate the <geometry>
166  daeElement* instanceGeom = node->add("instance_geometry");
167  instanceGeom->setAttribute("url", makeUriRef((polygon_name + "-geom_id").c_str()).c_str());
168 
169  // Bind material parameters
170  daeElement* instanceMaterial = instanceGeom->add("bind_material technique_common instance_material");
171  instanceMaterial->setAttribute("symbol", "mtl");
172  instanceMaterial->setAttribute("target", makeUriRef((polygon_name + "-material").c_str()).c_str());
173 
174 
175 
176 
177 
178 }
179 
180 // ---------------------------------------
181 // ------- Private methods
182 // ---------------------------------------
183 
184 template<typename T_wc>
185 daeTArray<T_wc> wrapper_collada::rawArrayToDaeArray(T_wc rawArray[], size_t count) {
186  daeTArray<T_wc> result;
187  for (size_t i = 0; i < count; i++)
188  result.append(rawArray[i]);
189  return result;
190 }
191 
192 std::string wrapper_collada::makeUriRef(const std::string& id) {
193  return std::string("#") + id;
194 }
195 
196 void wrapper_collada::my_addSource(daeElement* mesh,
197  const std::string& srcID,
198  const std::string& paramNames,
199  domFloat values[],
200  int valueCount) {
201 
202  daeElement* src = mesh->add("source");
203  //SafeAdd(mesh, "source", src);
204 
205  src->setAttribute("id", srcID.c_str());
206 
207  domFloat_array* fa = daeSafeCast<domFloat_array>(src->add("float_array"));
208  //CheckResult(fa);
209  fa->setId((src->getAttribute("id") + "-array").c_str());
210  fa->setCount(valueCount);
211  fa->getValue() = rawArrayToDaeArray(values, valueCount);
212 
213  domAccessor* acc = daeSafeCast<domAccessor>(src->add("technique_common accessor"));
214  //CheckResult(acc);
215  acc->setSource(makeUriRef(fa->getId()).c_str());
216 
217  std::list<std::string> params = cdom::tokenize(paramNames, " ");
218  acc->setStride(params.size());
219  acc->setCount(valueCount/params.size());
220  for (cdom::tokenIter iter = params.begin(); iter != params.end(); iter++) {
221  //SafeAdd(acc, "param", p);
222  daeElement* p = acc->add("param");
223  p->setAttribute("name", iter->c_str());
224  p->setAttribute("type", "float");
225  }
226 
227  //return testResult(true);
228 }
229 
230 daeTArray<double> wrapper_collada::pcl_pointcloud_tp_daearray(pcl::PointCloud<pcl::PointXYZ>::Ptr pc)
231 {
232  daeTArray<double> Arr;
233 
234  for (size_t i = 0; i < pc->points.size(); i++)
235  {
236  Arr.append(pc->points[i].x);
237  Arr.append(pc->points[i].y);
238  Arr.append(pc->points[i].z);
239  }
240 
241  return Arr;
242 }
243 
244 void wrapper_collada::my_addInput(daeElement* triangles,
245  const std::string& semantic,
246  const std::string& srcID,
247  int offset) {
248  domInput_local_offset* input = daeSafeCast<domInput_local_offset>(triangles->add("input"));
249  //CheckResult(input);
250  input->setSemantic(semantic.c_str());
251  input->setOffset(offset);
252  domUrifragment source(*triangles->getDAE(), srcID);
253  input->setSource(source);
254  if (semantic == "TEXCOORD")
255  input->setSet(0);
256  //return testResult(true);
257 }
258 
259 void wrapper_collada::my_addGeometry(daeElement* root) {
260 
261  daeElement* geomLib = root->add("library_geometries");
262  daeElement* geom = geomLib->add("geometry");
263  std::string geomID = "cubeGeom";
264  geom->setAttribute("id", geomID.c_str());
265  daeElement* mesh = geom->add("mesh");
266 
267  // Add the position data
268  domFloat posArray[] = { -10, -10, -10,
269  -10, -10, 10,
270  -10, 10, -10,
271  -10, 10, 10,
272  10, -10, -10,
273  10, -10, 10,
274  10, 10, -10,
275  10, 10, 10 };
276  int count = sizeof(posArray)/sizeof(posArray[0]);
277  my_addSource(mesh, geomID + "-positions", "X Y Z", posArray, count);
278 
279  // Add the normal data
280  domFloat normalArray[] = { 1, 0, 0,
281  -1, 0, 0,
282  0, 1, 0,
283  0, -1, 0,
284  0, 0, 1,
285  0, 0, -1 };
286  count = sizeof(normalArray)/sizeof(normalArray[0]);
287  my_addSource(mesh, geomID + "-normals", "X Y Z", normalArray, count);
288 
289  // Add the tex coord data
290  domFloat uvArray[] = { 0, 0,
291  0, 1,
292  1, 0,
293  1, 1 };
294  count = sizeof(uvArray)/sizeof(uvArray[0]);
295  my_addSource(mesh, geomID + "-uv", "S T", uvArray, count);
296 
297  // Add the <vertices> element
298  daeElement* vertices = mesh->add("vertices");
299  vertices->setAttribute("id", (geomID + "-vertices").c_str());
300  daeElement* verticesInput = vertices->add("input");
301  verticesInput->setAttribute("semantic", "POSITION");
302  verticesInput->setAttribute("source", makeUriRef(geomID + "-positions").c_str());
303 
304  // Add the <triangles> element.
305  // Each line is one triangle.
306  domUint indices[] = { 0, 1, 0, 1, 1, 1, 2, 1, 2,
307  1, 1, 1, 3, 1, 3, 2, 1, 2,
308  0, 2, 0, 4, 2, 1, 1, 2, 2,
309  4, 2, 1, 5, 2, 3, 1, 2, 2,
310  1, 4, 0, 5, 4, 1, 3, 4, 2,
311  5, 4, 1, 7, 4, 3, 3, 4, 2,
312  5, 0, 0, 4, 0, 1, 7, 0, 2,
313  4, 0, 1, 6, 0, 3, 7, 0, 2,
314  4, 5, 0, 0, 5, 1, 6, 5, 2,
315  0, 5, 1, 2, 5, 3, 6, 5, 2,
316  3, 3, 0, 7, 3, 1, 2, 3, 2,
317  7, 3, 1, 6, 3, 3, 2, 3, 2 };
318  count = sizeof(indices)/sizeof(indices[0]);
319 
320  domTriangles* triangles = daeSafeCast<domTriangles>(mesh->add("triangles"));
321  triangles->setCount(count/(3*3)); // 3 indices per vertex, 3 vertices per triangle
322  triangles->setMaterial("mtl");
323 
324  my_addInput(triangles, "VERTEX", geomID + "-vertices", 0);
325  my_addInput(triangles, "NORMAL", geomID + "-normals", 1);
326  my_addInput(triangles, "TEXCOORD", geomID + "-uv", 2);
327 
328  domP* p = daeSafeCast<domP>(triangles->add("p"));
329  p->getValue() = rawArrayToDaeArray(indices, count);
330 
331 }
332 
333 void wrapper_collada::my_addImage(daeElement* root) {
334  daeElement* imageLib = root->add("library_images");
335  daeElement* image = imageLib->add("image");
336  image->setAttribute("id", "img");
337  image->setAttribute("name", "myimage");
338  image->setAttribute("format","png");
339  image->setAttribute("height","375");
340  image->setAttribute("width","500");
341  image->setAttribute("depth","3");
342 
343  image->add("init_from")->setCharData("../texture.bmp");
344 
345 
346 
347 
348  //daeElement* tmp = image->add("init_from");
349  //daeElement* tmp = image->add("init_from");
350  //image->add("init_from")->setCharData("./texture.bmp");
351  std::string str("file://home/mike/workingcopy/arpua/utils/wrapper_collada/bin/texture1.png");
352  //std::string str("http://dubinko.info/writing/xforms/");
353  //const char* date = "file:///home/mike/workingcopy/arpua/utils/wrapper_collada/bin/texture.bmp";
354  //std::string str("MAX");
355  //daeBool res = tmp->setCharData(str);
356  //tmp->se
357  //daeDocument image_file()
358  daeBool res1;
359 
360  //dae->
361 
362  //image->setAttribute("name", "myimage");
363 
364 
365  return;
366 
367 
369  //daeElement* tmp = image->add("init_from");
371  //std::string str("file://home/mike/workingcopy/arpua/utils/wrapper_collada/bin/texture1.png");
375  //daeBool res = tmp->setCharData(str);
376  //printf("result is =%d",res);
377 
378 
380  //res1 = tmp->hasCharData();
381  //printf("init_from can have char data =%d\n",res1);
382  //ROS_INFO("Setting atribute");
385  //ROS_INFO("Setting atribute done");
387 }
388 
389 void wrapper_collada::my_addEffect(daeElement* root) {
390  daeElement* effectLib = root->add("library_effects");
391  daeElement* effect = effectLib->add("effect");
392  effect->setAttribute("id", "cubeEffect");
393  daeElement* profile = effect->add("profile_COMMON");
394 
395  // Add a <sampler2D>
396  daeElement* newparam = profile->add("newparam");
397  newparam->setAttribute("sid", "sampler");
398 
399 
400  daeElement* sampler = newparam->add("sampler2D");
401  daeSafeCast<domInstance_image>(sampler->add("instance_image"))->setUrl("#img");
402  sampler->add("minfilter")->setCharData("LINEAR");
403  sampler->add("magfilter")->setCharData("LINEAR");
404 
405  daeElement* technique = profile->add("technique");
406  technique->setAttribute("sid", "common");
407  daeElement* texture = technique->add("phong diffuse texture");
408  texture->setAttribute("texture", "sampler");
409  texture->setAttribute("texcoord", "uv0");
410 }
411 
412 void wrapper_collada::my_addMaterial(daeElement* root) {
413  //SafeAdd(root, "library_materials", materialLib);
414  daeElement* materialLib = root->add("library_materials");
415  //SafeAdd(materialLib, "material", material);
416  daeElement* material = materialLib->add("material");
417  material->setAttribute("id", "cubeMaterial");
418  material->add("instance_effect")->setAttribute("url", makeUriRef("cubeEffect").c_str());
419 
420 }
421 
422 void wrapper_collada::my_addVisualScene(daeElement* root) {
423 
424  daeElement* visualSceneLib = root->add("library_visual_scenes");
425  daeElement* visualScene = visualSceneLib->add("visual_scene");
426  visualScene->setAttribute("id", "cubeScene");
427 
428  // Add a <node> with a simple transformation
429  daeElement* node = visualScene->add("node");
430  node->setAttribute("id", "cubeNode");
431  node->add("rotate")->setCharData("1 0 0 45");
432  node->add("translate")->setCharData("0 10 0");
433 
434  // Instantiate the <geometry>
435  daeElement* instanceGeom = node->add("instance_geometry");
436  instanceGeom->setAttribute("url", makeUriRef("cubeGeom").c_str());
437 
438  // Bind material parameters
439  daeElement* instanceMaterial = instanceGeom->add("bind_material technique_common instance_material");
440  instanceMaterial->setAttribute("symbol", "mtl");
441  instanceMaterial->setAttribute("target", makeUriRef("cubeMaterial").c_str());
442 
443  daeElement* bindVertexInput = instanceMaterial->add("bind_vertex_input");
444  bindVertexInput->setAttribute("semantic", "uv0");
445  bindVertexInput->setAttribute("input_semantic", "TEXCOORD");
446  bindVertexInput->setAttribute("input_set", "0");
447 
448  // Add a <scene>
449  root->add("scene instance_visual_scene")->setAttribute("url", makeUriRef("cubeScene").c_str());
450 
451 }
452 
453 
454 #endif
455 
void my_addEffect(daeElement *root)
void my_addInput(daeElement *triangles, const std::string &semantic, const std::string &srcID, int offset)
daeElement * root
daeTArray< double > pcl_pointcloud_tp_daearray(pcl::PointCloud< pcl::PointXYZ >::Ptr pc)
void my_addGeometry(daeElement *root)
daeElement * visualScene
The actual wrapper collada class definition.
void my_addMaterial(daeElement *root)
void my_addImage(daeElement *root)
void add_polygon_fixed_color(std::string polygon_name, pcl::PointCloud< pcl::PointXYZ >::Ptr cloud, pcl::PointCloud< pcl::PointXYZ >::Ptr normal, float r, float g, float b, float a)
void my_addSource(daeElement *mesh, const std::string &srcID, const std::string &paramNames, domFloat values[], int valueCount)
daeTArray< T_wc > rawArrayToDaeArray(T_wc rawArray[], size_t count)
void my_addVisualScene(daeElement *root)
std::string makeUriRef(const std::string &id)


wrapper_collada
Author(s): Miguel Oliveira
autogenerated on Mon Mar 2 2015 01:33:01