00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00031 #ifndef _wrapper_collada_CPP_
00032 #define _wrapper_collada_CPP_
00033
00042
00045
00046
00049 #include "wrapper_collada.h"
00050
00051
00052 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)
00053 {
00054
00055 daeElement* geomLib = root->add("library_geometries");
00056 daeElement* geom = geomLib->add("geometry");
00057 geom->setAttribute("id", (polygon_name + "-geom_id").c_str());
00058 daeElement* mesh = geom->add("mesh");
00059
00060
00061 daeElement* source = mesh->add("source");
00062 source->setAttribute("id", (geom->getAttribute("id") + "-positions").c_str());
00063 domFloat_array* fa = daeSafeCast<domFloat_array>(source->add("float_array"));
00064 fa->setId((source->getAttribute("id") + "-array").c_str());
00065 fa->setCount((int)cloud->points.size()*3);
00066 fa->getValue() = pcl_pointcloud_tp_daearray(cloud);
00067 domAccessor* acc = daeSafeCast<domAccessor>(source->add("technique_common accessor"));
00068 acc->setSource(makeUriRef(fa->getId()).c_str());
00069 std::list<std::string> params = cdom::tokenize("X Y Z", " ");
00070 acc->setStride(params.size());
00071 acc->setCount((int)cloud->points.size());
00072 for (cdom::tokenIter iter = params.begin(); iter != params.end(); iter++)
00073 {
00074 daeElement* p = acc->add("param");
00075 p->setAttribute("name", iter->c_str());
00076 p->setAttribute("type", "float");
00077 }
00078
00079
00080 daeElement* source1 = mesh->add("source");
00081 source1->setAttribute("id", (geom->getAttribute("id") + "-normals").c_str());
00082 domFloat_array* fa1 = daeSafeCast<domFloat_array>(source1->add("float_array"));
00083 fa1->setId((source1->getAttribute("id") + "-array").c_str());
00084 fa1->setCount((int)normal->points.size()*3);
00085 fa1->getValue() = pcl_pointcloud_tp_daearray(normal);
00086 domAccessor* acc1 = daeSafeCast<domAccessor>(source1->add("technique_common accessor"));
00087 acc1->setSource(makeUriRef(fa1->getId()).c_str());
00088 std::list<std::string> params1 = cdom::tokenize("X Y Z", " ");
00089 acc1->setStride(params1.size());
00090 acc1->setCount((int)normal->points.size());
00091 for (cdom::tokenIter iter = params1.begin(); iter != params1.end(); iter++)
00092 {
00093 daeElement* p = acc1->add("param");
00094 p->setAttribute("name", iter->c_str());
00095 p->setAttribute("type", "float");
00096 }
00097
00098
00099 daeElement* vertices = mesh->add("vertices");
00100 vertices->setAttribute("id", (geom->getAttribute("id") + "-vertices").c_str());
00101 daeElement* verticesInput = vertices->add("input");
00102 verticesInput->setAttribute("semantic", "POSITION");
00103 verticesInput->setAttribute("source", makeUriRef(geom->getAttribute("id") + "-positions").c_str());
00104
00105
00106 domPolygons* polygons = daeSafeCast<domPolygons>(mesh->add("polygons"));
00107 polygons->setCount(1);
00108 polygons->setMaterial("mtl");
00109 my_addInput(polygons, "VERTEX", makeUriRef(geom->getAttribute("id") + "-vertices"), 0);
00110 my_addInput(polygons, "NORMAL", makeUriRef(geom->getAttribute("id") + "-normals"), 1);
00111 domP* p = daeSafeCast<domP>(polygons->add("p"));
00112 daeTArray<long long unsigned int> Arr;
00113 for (long long unsigned int i = 0; i < (long long unsigned int)cloud->points.size(); i++)
00114 {
00115 Arr.append(i);
00116 Arr.append(0);
00117 }
00118 p->getValue() = Arr;
00119
00120 char str[1024];
00121 sprintf(str,"%f %f %f %f",r,g,b,a);
00122
00123 daeElement* effectLib = root->add("library_effects");
00124 daeElement* effect = effectLib->add("effect");
00125 effect->setAttribute("id", (polygon_name + "-effect").c_str());
00126 daeElement* profile = effect->add("profile_COMMON");
00127 daeElement* technique = profile->add("technique");
00128 technique->setAttribute("sid", "phong1");
00129 daeElement* phong = technique->add("phong");
00130 daeElement* emission = phong->add("emission color");
00131 emission->setCharData(str);
00132 daeElement* ambient = phong->add("ambient color");
00133 ambient->setCharData(str);
00134 daeElement* diffuse = phong->add("diffuse color");
00135 diffuse->setCharData(str);
00136 daeElement* specular = phong->add("specular color");
00137 specular->setCharData(str);
00138 daeElement* shininess = phong->add("shininess float");
00139 shininess->setCharData("2.0");
00140 daeElement* reflective = phong->add("reflective color");
00141 reflective->setCharData(str);
00142 daeElement* reflectivity = phong->add("reflectivity float");
00143 reflectivity->setCharData("0.5");
00144 daeElement* transparent = phong->add("transparent color");
00145 transparent->setCharData(str);
00146 daeElement* transparency = phong->add("transparency float");
00147 transparency->setCharData("1.0");
00148
00149
00150 daeElement* materialLib = root->add("library_materials");
00151 daeElement* material = materialLib->add("material");
00152 material->setAttribute("id", (polygon_name + "-material").c_str());
00153 daeElement* instance_effect = material->add("instance_effect");
00154 instance_effect->setAttribute("url", ("#" + polygon_name + "-effect").c_str());
00155
00156
00157
00158 my_addImage(root);
00159
00160
00161 daeElement* node = visualScene->add("node");
00162 node->setAttribute("id", ("#" + polygon_name + "-node").c_str());
00163 node->add("scale")->setCharData("1 1 1");
00164
00165
00166 daeElement* instanceGeom = node->add("instance_geometry");
00167 instanceGeom->setAttribute("url", makeUriRef((polygon_name + "-geom_id").c_str()).c_str());
00168
00169
00170 daeElement* instanceMaterial = instanceGeom->add("bind_material technique_common instance_material");
00171 instanceMaterial->setAttribute("symbol", "mtl");
00172 instanceMaterial->setAttribute("target", makeUriRef((polygon_name + "-material").c_str()).c_str());
00173
00174
00175
00176
00177
00178 }
00179
00180
00181
00182
00183
00184 template<typename T_wc>
00185 daeTArray<T_wc> wrapper_collada::rawArrayToDaeArray(T_wc rawArray[], size_t count) {
00186 daeTArray<T_wc> result;
00187 for (size_t i = 0; i < count; i++)
00188 result.append(rawArray[i]);
00189 return result;
00190 }
00191
00192 std::string wrapper_collada::makeUriRef(const std::string& id) {
00193 return std::string("#") + id;
00194 }
00195
00196 void wrapper_collada::my_addSource(daeElement* mesh,
00197 const std::string& srcID,
00198 const std::string& paramNames,
00199 domFloat values[],
00200 int valueCount) {
00201
00202 daeElement* src = mesh->add("source");
00203
00204
00205 src->setAttribute("id", srcID.c_str());
00206
00207 domFloat_array* fa = daeSafeCast<domFloat_array>(src->add("float_array"));
00208
00209 fa->setId((src->getAttribute("id") + "-array").c_str());
00210 fa->setCount(valueCount);
00211 fa->getValue() = rawArrayToDaeArray(values, valueCount);
00212
00213 domAccessor* acc = daeSafeCast<domAccessor>(src->add("technique_common accessor"));
00214
00215 acc->setSource(makeUriRef(fa->getId()).c_str());
00216
00217 std::list<std::string> params = cdom::tokenize(paramNames, " ");
00218 acc->setStride(params.size());
00219 acc->setCount(valueCount/params.size());
00220 for (cdom::tokenIter iter = params.begin(); iter != params.end(); iter++) {
00221
00222 daeElement* p = acc->add("param");
00223 p->setAttribute("name", iter->c_str());
00224 p->setAttribute("type", "float");
00225 }
00226
00227
00228 }
00229
00230 daeTArray<double> wrapper_collada::pcl_pointcloud_tp_daearray(pcl::PointCloud<pcl::PointXYZ>::Ptr pc)
00231 {
00232 daeTArray<double> Arr;
00233
00234 for (size_t i = 0; i < pc->points.size(); i++)
00235 {
00236 Arr.append(pc->points[i].x);
00237 Arr.append(pc->points[i].y);
00238 Arr.append(pc->points[i].z);
00239 }
00240
00241 return Arr;
00242 }
00243
00244 void wrapper_collada::my_addInput(daeElement* triangles,
00245 const std::string& semantic,
00246 const std::string& srcID,
00247 int offset) {
00248 domInput_local_offset* input = daeSafeCast<domInput_local_offset>(triangles->add("input"));
00249
00250 input->setSemantic(semantic.c_str());
00251 input->setOffset(offset);
00252 domUrifragment source(*triangles->getDAE(), srcID);
00253 input->setSource(source);
00254 if (semantic == "TEXCOORD")
00255 input->setSet(0);
00256
00257 }
00258
00259 void wrapper_collada::my_addGeometry(daeElement* root) {
00260
00261 daeElement* geomLib = root->add("library_geometries");
00262 daeElement* geom = geomLib->add("geometry");
00263 std::string geomID = "cubeGeom";
00264 geom->setAttribute("id", geomID.c_str());
00265 daeElement* mesh = geom->add("mesh");
00266
00267
00268 domFloat posArray[] = { -10, -10, -10,
00269 -10, -10, 10,
00270 -10, 10, -10,
00271 -10, 10, 10,
00272 10, -10, -10,
00273 10, -10, 10,
00274 10, 10, -10,
00275 10, 10, 10 };
00276 int count = sizeof(posArray)/sizeof(posArray[0]);
00277 my_addSource(mesh, geomID + "-positions", "X Y Z", posArray, count);
00278
00279
00280 domFloat normalArray[] = { 1, 0, 0,
00281 -1, 0, 0,
00282 0, 1, 0,
00283 0, -1, 0,
00284 0, 0, 1,
00285 0, 0, -1 };
00286 count = sizeof(normalArray)/sizeof(normalArray[0]);
00287 my_addSource(mesh, geomID + "-normals", "X Y Z", normalArray, count);
00288
00289
00290 domFloat uvArray[] = { 0, 0,
00291 0, 1,
00292 1, 0,
00293 1, 1 };
00294 count = sizeof(uvArray)/sizeof(uvArray[0]);
00295 my_addSource(mesh, geomID + "-uv", "S T", uvArray, count);
00296
00297
00298 daeElement* vertices = mesh->add("vertices");
00299 vertices->setAttribute("id", (geomID + "-vertices").c_str());
00300 daeElement* verticesInput = vertices->add("input");
00301 verticesInput->setAttribute("semantic", "POSITION");
00302 verticesInput->setAttribute("source", makeUriRef(geomID + "-positions").c_str());
00303
00304
00305
00306 domUint indices[] = { 0, 1, 0, 1, 1, 1, 2, 1, 2,
00307 1, 1, 1, 3, 1, 3, 2, 1, 2,
00308 0, 2, 0, 4, 2, 1, 1, 2, 2,
00309 4, 2, 1, 5, 2, 3, 1, 2, 2,
00310 1, 4, 0, 5, 4, 1, 3, 4, 2,
00311 5, 4, 1, 7, 4, 3, 3, 4, 2,
00312 5, 0, 0, 4, 0, 1, 7, 0, 2,
00313 4, 0, 1, 6, 0, 3, 7, 0, 2,
00314 4, 5, 0, 0, 5, 1, 6, 5, 2,
00315 0, 5, 1, 2, 5, 3, 6, 5, 2,
00316 3, 3, 0, 7, 3, 1, 2, 3, 2,
00317 7, 3, 1, 6, 3, 3, 2, 3, 2 };
00318 count = sizeof(indices)/sizeof(indices[0]);
00319
00320 domTriangles* triangles = daeSafeCast<domTriangles>(mesh->add("triangles"));
00321 triangles->setCount(count/(3*3));
00322 triangles->setMaterial("mtl");
00323
00324 my_addInput(triangles, "VERTEX", geomID + "-vertices", 0);
00325 my_addInput(triangles, "NORMAL", geomID + "-normals", 1);
00326 my_addInput(triangles, "TEXCOORD", geomID + "-uv", 2);
00327
00328 domP* p = daeSafeCast<domP>(triangles->add("p"));
00329 p->getValue() = rawArrayToDaeArray(indices, count);
00330
00331 }
00332
00333 void wrapper_collada::my_addImage(daeElement* root) {
00334 daeElement* imageLib = root->add("library_images");
00335 daeElement* image = imageLib->add("image");
00336 image->setAttribute("id", "img");
00337 image->setAttribute("name", "myimage");
00338 image->setAttribute("format","png");
00339 image->setAttribute("height","375");
00340 image->setAttribute("width","500");
00341 image->setAttribute("depth","3");
00342
00343 image->add("init_from")->setCharData("../texture.bmp");
00344
00345
00346
00347
00348
00349
00350
00351 std::string str("file://home/mike/workingcopy/arpua/utils/wrapper_collada/bin/texture1.png");
00352
00353
00354
00355
00356
00357
00358 daeBool res1;
00359
00360
00361
00362
00363
00364
00365 return;
00366
00367
00369
00371
00375
00376
00377
00378
00380
00381
00382
00385
00387 }
00388
00389 void wrapper_collada::my_addEffect(daeElement* root) {
00390 daeElement* effectLib = root->add("library_effects");
00391 daeElement* effect = effectLib->add("effect");
00392 effect->setAttribute("id", "cubeEffect");
00393 daeElement* profile = effect->add("profile_COMMON");
00394
00395
00396 daeElement* newparam = profile->add("newparam");
00397 newparam->setAttribute("sid", "sampler");
00398
00399
00400 daeElement* sampler = newparam->add("sampler2D");
00401 daeSafeCast<domInstance_image>(sampler->add("instance_image"))->setUrl("#img");
00402 sampler->add("minfilter")->setCharData("LINEAR");
00403 sampler->add("magfilter")->setCharData("LINEAR");
00404
00405 daeElement* technique = profile->add("technique");
00406 technique->setAttribute("sid", "common");
00407 daeElement* texture = technique->add("phong diffuse texture");
00408 texture->setAttribute("texture", "sampler");
00409 texture->setAttribute("texcoord", "uv0");
00410 }
00411
00412 void wrapper_collada::my_addMaterial(daeElement* root) {
00413
00414 daeElement* materialLib = root->add("library_materials");
00415
00416 daeElement* material = materialLib->add("material");
00417 material->setAttribute("id", "cubeMaterial");
00418 material->add("instance_effect")->setAttribute("url", makeUriRef("cubeEffect").c_str());
00419
00420 }
00421
00422 void wrapper_collada::my_addVisualScene(daeElement* root) {
00423
00424 daeElement* visualSceneLib = root->add("library_visual_scenes");
00425 daeElement* visualScene = visualSceneLib->add("visual_scene");
00426 visualScene->setAttribute("id", "cubeScene");
00427
00428
00429 daeElement* node = visualScene->add("node");
00430 node->setAttribute("id", "cubeNode");
00431 node->add("rotate")->setCharData("1 0 0 45");
00432 node->add("translate")->setCharData("0 10 0");
00433
00434
00435 daeElement* instanceGeom = node->add("instance_geometry");
00436 instanceGeom->setAttribute("url", makeUriRef("cubeGeom").c_str());
00437
00438
00439 daeElement* instanceMaterial = instanceGeom->add("bind_material technique_common instance_material");
00440 instanceMaterial->setAttribute("symbol", "mtl");
00441 instanceMaterial->setAttribute("target", makeUriRef("cubeMaterial").c_str());
00442
00443 daeElement* bindVertexInput = instanceMaterial->add("bind_vertex_input");
00444 bindVertexInput->setAttribute("semantic", "uv0");
00445 bindVertexInput->setAttribute("input_semantic", "TEXCOORD");
00446 bindVertexInput->setAttribute("input_set", "0");
00447
00448
00449 root->add("scene instance_visual_scene")->setAttribute("url", makeUriRef("cubeScene").c_str());
00450
00451 }
00452
00453
00454 #endif
00455