-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcollada2eus_urdfmodel.cpp
More file actions
2185 lines (2035 loc) · 81 KB
/
Copy pathcollada2eus_urdfmodel.cpp
File metadata and controls
2185 lines (2035 loc) · 81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Author: Yohei Kakiuchi */
#include <ros/ros.h>
#include "collada_parser/collada_parser.h"
#include "urdf/model.h"
#include "urdf_parser/urdf_parser.h"
#include <sys/utsname.h>
#include <math.h>
// assimp_devel
#include <Importer.hpp>
#include <scene.h>
#include <postprocess.h>
#include <IOStream.hpp>
#include <IOSystem.hpp>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/program_options.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/foreach.hpp>
#include <tf/LinearMath/Transform.h>
#include <tf/LinearMath/Quaternion.h>
#include <resource_retriever/retriever.h>
#include "yaml-cpp/yaml.h"
extern "C" {
#ifdef __APPLE__
#include <libqhull/qhull_a.h>
#else
#include <qhull/qhull_a.h>
#endif
}
#include "rospack/rospack.h"
// using collada just for parsing sensors
#include "dae.h"
#include "dom/domCOLLADA.h"
#ifdef __dom150COLLADA_h__
using namespace ColladaDOM150;
#endif
// copy from rviz/src/rviz/mesh_loader.cpp
class ResourceIOStream : public Assimp::IOStream
{
public:
ResourceIOStream(const resource_retriever::MemoryResource& res)
: res_(res)
, pos_(res.data.get())
{}
~ResourceIOStream()
{}
size_t Read(void* buffer, size_t size, size_t count)
{
size_t to_read = size * count;
if (pos_ + to_read > res_.data.get() + res_.size)
{
to_read = res_.size - (pos_ - res_.data.get());
}
memcpy(buffer, pos_, to_read);
pos_ += to_read;
return to_read;
}
size_t Write( const void* buffer, size_t size, size_t count) { ROS_BREAK(); return 0; }
aiReturn Seek( size_t offset, aiOrigin origin)
{
uint8_t* new_pos = 0;
switch (origin)
{
case aiOrigin_SET:
new_pos = res_.data.get() + offset;
break;
case aiOrigin_CUR:
new_pos = pos_ + offset; // TODO is this right? can offset really not be negative
break;
case aiOrigin_END:
new_pos = res_.data.get() + res_.size - offset; // TODO is this right?
break;
default:
ROS_BREAK();
}
if (new_pos < res_.data.get() || new_pos > res_.data.get() + res_.size)
{
return aiReturn_FAILURE;
}
pos_ = new_pos;
return aiReturn_SUCCESS;
}
size_t Tell() const
{
return pos_ - res_.data.get();
}
size_t FileSize() const
{
return res_.size;
}
void Flush() {}
private:
resource_retriever::MemoryResource res_;
uint8_t* pos_;
};
class ResourceIOSystem : public Assimp::IOSystem
{
public:
ResourceIOSystem()
{
}
~ResourceIOSystem()
{
}
// Check whether a specific file exists
bool Exists(const char* file) const
{
// Ugly -- two retrievals where there should be one (Exists + Open)
// resource_retriever needs a way of checking for existence
// TODO: cache this
resource_retriever::MemoryResource res;
try
{
res = retriever_.get(file);
}
catch (resource_retriever::Exception& e)
{
return false;
}
return true;
}
// Get the path delimiter character we'd like to see
char getOsSeparator() const
{
return '/';
}
// ... and finally a method to open a custom stream
Assimp::IOStream* Open(const char* file, const char* mode = "rb")
{
ROS_ASSERT(mode == std::string("r") || mode == std::string("rb"));
// Ugly -- two retrievals where there should be one (Exists + Open)
// resource_retriever needs a way of checking for existence
resource_retriever::MemoryResource res;
try
{
res = retriever_.get(file);
}
catch (resource_retriever::Exception& e)
{
return 0;
}
return new ResourceIOStream(res);
}
void Close(Assimp::IOStream* stream);
private:
mutable resource_retriever::Retriever retriever_;
};
void ResourceIOSystem::Close(Assimp::IOStream* stream)
{
delete stream;
}
////
using namespace urdf;
using namespace std;
#define FLOAT_PRECISION_FINE "%.16e"
#define FLOAT_PRECISION_COARSE "%.3f"
//
class ModelEuslisp {
public:
ModelEuslisp () { } ;
#if URDFDOM_1_0_0_API
ModelEuslisp (ModelInterfaceSharedPtr r);
#else
ModelEuslisp (boost::shared_ptr<ModelInterface> r);
#endif
~ModelEuslisp ();
// accessor
void setRobotName (string &name) { arobot_name = name; };
void setUseCollision(bool &b) { use_collision = b; };
void setUseSimpleGeometry(bool &b) { use_simple_geometry = b; };
void setUseLoadbleMesh(bool &b) { use_loadable_mesh = b; };
void setRemoveNormal(bool &b) { remove_normal = b; };
void setAddJointSuffix(bool &b) { add_joint_suffix = b; };
void setAddLinkSuffix(bool &b) { add_link_suffix = b; };
void setAddSensorSuffix(bool &b) { add_sensor_suffix = b; };
// methods for parsing robot model for euslisp
void addLinkCoords();
void printMesh(const aiScene* scene, const aiNode* node, const Vector3 &scale,
const string &material_name, vector<coordT> &store_pt, bool printq);
void readYaml(std::vector<string> &config_file);
#if URDFDOM_1_0_0_API
Pose getLinkPose(LinkConstSharedPtr link) {
#else
Pose getLinkPose(boost::shared_ptr<const Link> link) {
#endif
if (!link->parent_joint) {
Pose ret;
return ret;
}
Pose p_pose = getLinkPose(link->getParent());
Pose l_pose = link->parent_joint->parent_to_joint_origin_transform;
Pose ret;
ret.rotation = p_pose.rotation * l_pose.rotation;
ret.position = (p_pose.rotation * l_pose.position) + p_pose.position;
return ret;
}
#if URDFDOM_1_0_0_API
void printLink (LinkConstSharedPtr Link, Pose &pose);
void printJoint (JointConstSharedPtr joint);
void printGeometry (GeometrySharedPtr g, const Pose &pose,
const string &name, const string &material_name);
#else
void printLink (boost::shared_ptr<const Link> Link, Pose &pose);
void printJoint (boost::shared_ptr<const Joint> joint);
void printGeometry (boost::shared_ptr<Geometry> g, const Pose &pose,
const string &name, const string &material_name);
#endif
void printLinks ();
void printJoints ();
void printMimicJoints ();
void printEndCoords();
void printSensors();
void printSensorLists();
void printGeometries();
void printUniqueLimbs();
// print methods
void copyRobotClassDefinition ();
void printRobotDefinition();
void printRobotMethods();
void writeToFile (string &filename);
// temporary using collada file directly for sensors
string collada_file;
daeDocument *g_document;
//boost::shared_ptr< DAE> dae;
DAE dae;
domLink* findLinkfromKinematics (domLink* thisLink, const string& link_name);
void parseSensors();
class daeSensor {
public:
string sensor_type;
string parent_link;
string sensor_id;
string name;
vector<daeElementRef> ptrans;
static bool compare(const daeSensor& a, const daeSensor& b) {
return (a.sensor_id < b.sensor_id);
}
};
vector<daeSensor> m_sensors;
void getSensorInfo (const domExtraRef pextra, daeSensor& dsensor);
private:
#if URDFDOM_1_0_0_API
typedef map <string, VisualConstSharedPtr> MapVisual;
typedef map <string, CollisionConstSharedPtr> MapCollision;
#else
typedef map <string, boost::shared_ptr<const Visual> > MapVisual;
typedef map <string, boost::shared_ptr<const Collision> > MapCollision;
#endif
typedef pair<vector<string>, vector<string> > link_joint;
typedef pair<string, link_joint > link_joint_pair;
#if URDFDOM_1_0_0_API
ModelInterfaceSharedPtr robot;
#else
boost::shared_ptr<ModelInterface> robot;
#endif
string arobot_name;
#if URDFDOM_1_0_0_API
struct LinkPtrComparator {
bool operator()(const LinkConstSharedPtr & left, const LinkConstSharedPtr & right) const {
return (left->name < right->name);
}
};
map <LinkConstSharedPtr, Pose, LinkPtrComparator > m_link_coords;
map <LinkConstSharedPtr, MapVisual, LinkPtrComparator > m_link_visual;
map <LinkConstSharedPtr, MapCollision, LinkPtrComparator > m_link_collision;
map <string, MaterialConstSharedPtr> m_materials;
#else
struct LinkPtrComparator {
bool operator()(const boost::shared_ptr<const Link> & left, const boost::shared_ptr<const Link> & right) const {
return (left->name < right->name);
}
};
map <boost::shared_ptr<const Link>, Pose, LinkPtrComparator > m_link_coords;
map <boost::shared_ptr<const Link>, MapVisual, LinkPtrComparator > m_link_visual;
map <boost::shared_ptr<const Link>, MapCollision, LinkPtrComparator > m_link_collision;
map <string, boost::shared_ptr<const Material> > m_materials;
#endif
vector<pair<string, string> > g_all_link_names;
vector<link_joint_pair> limbs;
FILE *fp;
YAML::Node doc;
//
bool add_joint_suffix;
bool add_link_suffix;
bool add_sensor_suffix;
bool use_simple_geometry;
bool use_collision;
bool use_loadable_mesh;
bool remove_normal;
};
#if URDFDOM_1_0_0_API
ModelEuslisp::ModelEuslisp (ModelInterfaceSharedPtr r) {
#else
ModelEuslisp::ModelEuslisp (boost::shared_ptr<ModelInterface> r) {
#endif
robot = r;
add_joint_suffix = true;
add_link_suffix = true;
add_sensor_suffix = false;
use_simple_geometry = false;
use_collision = false;
use_loadable_mesh = false;
remove_normal = false;
}
ModelEuslisp::~ModelEuslisp () {
}
void ModelEuslisp::addLinkCoords() {
#if URDFDOM_1_0_0_API
for (map<string, LinkSharedPtr>::iterator link = robot->links_.begin();
#else
for (map<string, boost::shared_ptr<Link> >::iterator link = robot->links_.begin();
#endif
link != robot->links_.end(); link++) {
Pose p = getLinkPose(link->second);
m_link_coords.insert
#if URDFDOM_1_0_0_API
(map<LinkConstSharedPtr, Pose >::value_type (link->second, p));
#else
(map<boost::shared_ptr<const Link>, Pose >::value_type (link->second, p));
#endif
#if DEBUG
cerr << "name: " << link->first;
cerr << ", #f(" << p.position.x << " ";
cerr << p.position.y << " ";
cerr << p.position.z << ") #f(";
cerr << p.rotation.w << " ";
cerr << p.rotation.x << " ";
cerr << p.rotation.y << " ";
cerr << p.rotation.z << ")" << endl;
#endif
if (use_collision) {
int counter = 0;
if(link->second->collision_array.size() > 0) {
MapCollision mc;
#if URDFDOM_1_0_0_API
for (vector<CollisionSharedPtr >::iterator it = link->second->collision_array.begin();
#else
for (vector<boost::shared_ptr <Collision> >::iterator it = link->second->collision_array.begin();
#endif
it != link->second->collision_array.end(); it++) {
stringstream ss;
ss << link->second->name << "_geom" << counter;
mc.insert(MapCollision::value_type (ss.str(), (*it)));
counter++;
}
m_link_collision.insert
#if URDFDOM_1_0_0_API
(map <LinkConstSharedPtr, MapCollision >::value_type
#else
(map <boost::shared_ptr<const Link>, MapCollision >::value_type
#endif
(link->second, mc));
} else if(!!link->second->collision) {
MapCollision mc;
string gname(link->second->name);
gname += "_geom0";
mc.insert(MapCollision::value_type (gname, link->second->collision));
m_link_collision.insert
#if URDFDOM_1_0_0_API
(map <LinkConstSharedPtr, MapCollision >::value_type
#else
(map <boost::shared_ptr<const Link>, MapCollision >::value_type
#endif
(link->second, mc));
}
} else {
int counter = 0;
if(link->second->visual_array.size() > 0) {
MapVisual mv;
#if URDFDOM_1_0_0_API
for (vector<VisualSharedPtr>::iterator it = link->second->visual_array.begin();
#else
for (vector<boost::shared_ptr <Visual> >::iterator it = link->second->visual_array.begin();
#endif
it != link->second->visual_array.end(); it++) {
m_materials.insert
#if URDFDOM_1_0_0_API
(map <string, MaterialConstSharedPtr>::value_type ((*it)->material_name, (*it)->material));
#else
(map <string, boost::shared_ptr<const Material> >::value_type ((*it)->material_name, (*it)->material));
#endif
stringstream ss;
ss << link->second->name << "_geom" << counter;
mv.insert(MapVisual::value_type (ss.str(), (*it)));
counter++;
}
m_link_visual.insert
#if URDFDOM_1_0_0_API
(map <LinkConstSharedPtr, MapVisual >::value_type
#else
(map <boost::shared_ptr<const Link>, MapVisual >::value_type
#endif
(link->second, mv));
} else if(!!link->second->visual) {
m_materials.insert
#if URDFDOM_1_0_0_API
(map <string, MaterialConstSharedPtr>::value_type
#else
(map <string, boost::shared_ptr<const Material> >::value_type
#endif
(link->second->visual->material_name, link->second->visual->material));
MapVisual mv;
string gname(link->second->name);
gname += "_geom0";
mv.insert(MapVisual::value_type (gname, link->second->visual));
m_link_visual.insert
#if URDFDOM_1_0_0_API
(map <LinkConstSharedPtr, MapVisual >::value_type
#else
(map <boost::shared_ptr<const Link>, MapVisual >::value_type
#endif
(link->second, mv));
}
}
}
}
void ModelEuslisp::printMesh(const aiScene* scene, const aiNode* node, const Vector3 &scale,
const string &material_name, vector<coordT> &store_pt, bool printq) {
aiMatrix4x4 transform = node->mTransformation;
aiNode *pnode = node->mParent;
while (pnode) {
if (pnode->mParent != NULL) {
transform = pnode->mTransformation * transform;
}
pnode = pnode->mParent;
}
aiMatrix3x3 rotation(transform);
aiMatrix3x3 inverse_transpose_rotation(rotation);
inverse_transpose_rotation.Inverse();
inverse_transpose_rotation.Transpose();
for (uint32_t i = 0; i < node->mNumMeshes; i++) {
aiMesh* input_mesh = scene->mMeshes[node->mMeshes[i]];
if (input_mesh->mPrimitiveTypes != aiPrimitiveType_TRIANGLE) {
continue; // print only triangle mesh
}
if (printq) fprintf(fp, " (list ;; mesh description\n");
if (printq) fprintf(fp, " (list :type :triangles)\n");
if (printq) fprintf(fp, " (list :material (list");
if (material_name.size() > 0) {
if (printq) fprintf(fp, ";; material: %s\n", material_name.c_str());
#if URDFDOM_1_0_0_API
map <string, MaterialConstSharedPtr>::iterator it = m_materials.find(material_name);
#else
map <string, boost::shared_ptr<const Material> >::iterator it = m_materials.find(material_name);
#endif
if (it != m_materials.end()) {
#if URDFDOM_1_0_0_API
MaterialConstSharedPtr m = it->second;
#else
boost::shared_ptr<const Material> m = it->second;
#endif
float col_r = m->color.r;
float col_g = m->color.g;
float col_b = m->color.b;
float col_a = m->color.a;
if (printq) fprintf(fp, "\n (list :ambient (float-vector %f %f %f %f))", col_r, col_g, col_b, col_a);
if (printq) fprintf(fp, "\n (list :diffuse (float-vector %f %f %f %f))", col_r, col_g, col_b, col_a);
} else {
std::cerr << "can not find material " << material_name << std::endl;
}
} else {
if (!!scene->mMaterials) {
aiMaterial *am = scene->mMaterials[input_mesh->mMaterialIndex];
aiReturn ar;
aiColor4D clr4d( 0.0, 0.0, 0.0, 0.0);
ar = am->Get (AI_MATKEY_COLOR_AMBIENT, clr4d);
if (ar == aiReturn_SUCCESS) {
if (printq) fprintf(fp, "\n (list :ambient (float-vector %f %f %f %f))",
clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
}
ar = am->Get (AI_MATKEY_COLOR_DIFFUSE, clr4d);
if (ar == aiReturn_SUCCESS) {
if (printq) fprintf(fp, "\n (list :diffuse (float-vector %f %f %f %f))",
clr4d[0], clr4d[1], clr4d[2], clr4d[3]);
}
// ar = am->Get (AI_MATKEY_COLOR_SPECULAR, clr4d);
// ar = am->Get (AI_MATKEY_COLOR_EMISSIVE, clr4d);
// float val;
// ar = am->Get (AI_MATKEY_SHININESS, val);
}
}
if (printq) fprintf(fp, "))\n");
if (printq) fprintf(fp, " (list :indices #i(");
for (uint32_t j = 0; j < input_mesh->mNumFaces; j++) {
aiFace& face = input_mesh->mFaces[j];
for (uint32_t k = 0; k < face.mNumIndices; ++k) {
if (printq) fprintf(fp, " %d", face.mIndices[k]);
aiVector3D p = input_mesh->mVertices[face.mIndices[k]];
p *= transform;
store_pt.push_back(p.x * scale.x);
store_pt.push_back(p.y * scale.y);
store_pt.push_back(p.z * scale.z);
}
}
if (printq) fprintf(fp, "))\n");
if (printq) fprintf(fp, " (list :vertices (let ((mat (make-matrix %d 3))) (fvector-replace (array-entity mat) #f(", input_mesh->mNumVertices);
// Add the vertices
for (uint32_t j = 0; j < input_mesh->mNumVertices; j++) {
aiVector3D p = input_mesh->mVertices[j];
p *= transform;
//p *= scale;
if (printq) fprintf(fp, FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE" ",
1000 * p.x * scale.x, 1000 * p.y * scale.y, 1000 * p.z * scale.z);
}
if (printq) fprintf(fp, ")) mat))");
if (input_mesh->HasNormals()) {
if (printq) fprintf(fp, "\n");
if (printq) fprintf(fp, " (list :normals (let ((mat (make-matrix %d 3))) (fvector-replace (array-entity mat) #f(", input_mesh->mNumVertices);
for (uint32_t j = 0; j < input_mesh->mNumVertices; j++) {
if (isnan(input_mesh->mNormals[j].x) ||
isnan(input_mesh->mNormals[j].y) ||
isnan(input_mesh->mNormals[j].z)) {
if (printq) fprintf(fp, " 0 0 0"); // should be normalized vector #f(1 0 0) ???
} else {
aiVector3D n = inverse_transpose_rotation * input_mesh->mNormals[j];
n.Normalize();
if (printq) fprintf(fp, FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE" ", n.x, n.y, n.z);
}
}
if (printq) fprintf(fp, ")) mat))");
}
#if 0
if (input_mesh->HasTextureCoords(0)) {
for (uint32_t j = 0; j < input_mesh->mNumVertices; j++) {
aiVector3D n = inverse_transpose_rotation * input_mesh->mNormals[j];
//*vertices++ = input_mesh->mTextureCoords[0][j].x;
//*vertices++ = input_mesh->mTextureCoords[0][j].y;
}
}
#endif
if (printq) fprintf(fp, ")\n");
}
for (uint32_t i = 0; i < node->mNumChildren; ++i) {
printMesh(scene, node->mChildren[i], scale, material_name, store_pt, printq);
}
}
bool limb_order_asc(const pair<string, size_t>& left, const pair<string, size_t>& right) { return left.second < right.second; }
void ModelEuslisp::readYaml (std::vector<string> &config_files) {
// read yaml
std::vector<string> limb_candidates; // limb names is given from yaml files
vector<pair<string, size_t> > limb_order;
doc = YAML::Node(YAML::NodeType::Map);
for(int i = config_files.size() - 1; i >= 0; i--) { // override values from later config_file with values from earlier config_file
YAML::Node tmp_doc;
#ifndef USE_CURRENT_YAML
ifstream fin(config_files[i].c_str());
if (fin.fail()) {
fprintf(stderr, "%c[31m;; Could not open %s%c[m\n", 0x1b, config_files[i].c_str(), 0x1b);
} else {
YAML::Parser parser(fin);
parser.GetNextDocument(tmp_doc);
for(YAML::const_iterator it=tmp_doc.begin();it != tmp_doc.end();++it) {
doc[it->first] = it->second;
}
}
#else
// yaml-cpp is greater than 0.5.0
tmp_doc = YAML::LoadFile(config_files[i].c_str());
for(YAML::const_iterator it=tmp_doc.begin();it != tmp_doc.end();++it) {
doc[it->first] = it->second;
}
#endif
}
{
// set limb_candidates from yaml files
for(YAML::const_iterator it=doc.begin();it != doc.end();++it) {
// -end-coords, *-vector, sensors
if ( boost::algorithm::ends_with(it->first.as<std::string>(), "-coords") ||
boost::algorithm::ends_with(it->first.as<std::string>(), "-vector") ||
it->first.as<std::string>() == "sensors") {
} else {
limb_candidates.push_back(it->first.as<std::string>());
}
}
/* re-order limb name by lines of yaml */
BOOST_FOREACH(string& limb, limb_candidates) {
#ifdef USE_CURRENT_YAML
if (doc[limb]) {
int line=0;
bool found = false;
for(int i = 0; i < config_files.size() && !found; i++) { // super ugry hack until yaml-cpp 0.5.2
ifstream fin2(config_files[i].c_str());
string buffer;
for (;fin2;) {
getline(fin2, buffer); line++;
if(buffer == limb+":") {
found = true;
break;
}
}
fin2.close();
}
std::cerr << limb << "@" << line << std::endl;
limb_order.push_back(pair<string, size_t>(limb, line));
}
#else
if ( doc.FindValue(limb) ) {
cerr << limb << "@" << doc[limb].GetMark().line << endl;
limb_order.push_back(pair<string, size_t> (limb, doc[limb].GetMark().line));
}
#endif
}
sort(limb_order.begin(), limb_order.end(), limb_order_asc);
}
// generate limbs including limb_name, link_names, and joint_names
for (size_t l = 0; l < limb_order.size(); l++) {
string limb_name = limb_order[l].first;
vector<string> tmp_link_names, tmp_joint_names, tmp_method_names;
try {
const YAML::Node& limb_doc = doc[limb_name];
for(unsigned int i = 0; i < limb_doc.size(); i++) {
const YAML::Node& n = limb_doc[i];
if(n.Type() != YAML::NodeType::Map) {
ROS_WARN_STREAM("invalid yaml representation. ignore limb " << limb_name);
goto nextlimb;
}
#ifdef USE_CURRENT_YAML
for(YAML::const_iterator it=n.begin();it!=n.end();it++) {
if(it->second.Type() != YAML::NodeType::Scalar) {
ROS_WARN_STREAM("invalid yaml representation. ignore limb " << limb_name);
goto nextlimb;
}
string key, value;
key = it->first.as<std::string>();
value = it->second.as<std::string>();
#else
for(YAML::Iterator it=n.begin();it!=n.end();it++) {
if(it->second.Type() != YAML::NodeType::Scalar) {
ROS_WARN_STREAM("invalid yaml representation. ignore limb " << limb_name);
goto nextlimb;
}
string key, value; it.first() >> key; it.second() >> value;
#endif
#if URDFDOM_1_0_0_API
JointConstSharedPtr jnt = robot->getJoint(key);
#else
boost::shared_ptr<const Joint> jnt = robot->getJoint(key);
#endif
if (!!jnt) {
tmp_joint_names.push_back(key);
tmp_method_names.push_back(value);
tmp_link_names.push_back(jnt->child_link_name);
} else {
ROS_WARN_STREAM("joint " << key << " is not found. ignore limb " << limb_name);
goto nextlimb;
}
}
}
for(unsigned int j = 0; j < limb_doc.size(); j++) {
g_all_link_names.push_back(pair<string, string>(tmp_joint_names[j], tmp_method_names[j]));
}
limbs.push_back(link_joint_pair(limb_name, link_joint(tmp_link_names, tmp_joint_names)));
nextlimb:
;
} catch(YAML::RepresentationException& e) {
}
}
}
void ModelEuslisp::copyRobotClassDefinition () {
// load class definition
fprintf(fp, ";;\n");
fprintf(fp, ";; copy euscollada-robot class definition from euscollada/src/euscollada-robot.l\n");
fprintf(fp, ";;\n");
try {
string euscollada_path;
rospack::Rospack rp;
vector<string> search_path;
rp.getSearchPathFromEnv(search_path);
rp.crawl(search_path, 1);
string path;
rp.find("euscollada",euscollada_path);
euscollada_path += "/src/euscollada-robot.l";
ifstream fin(euscollada_path.c_str());
string buf;
while(fin && getline(fin, buf)) {
fprintf(fp, "%s\n", buf.c_str());
}
fprintf(fp, ";;\n");
fprintf(fp, ";; end of copy from %s\n", euscollada_path.c_str());
fprintf(fp, ";;\n");
} catch (runtime_error &e) {
cerr << "cannot resolve euscollada package path" << endl;
}
}
void ModelEuslisp::printRobotDefinition() {
fprintf(fp, "(defun %s () (setq *%s* (instance %s-robot :init)))\n",
arobot_name.c_str(), arobot_name.c_str(), arobot_name.c_str());
fprintf(fp, "\n");
fprintf(fp, "(defclass %s-robot\n", arobot_name.c_str());
fprintf(fp, " :super euscollada-robot\n");
fprintf(fp, " :slots ( ;; link names\n");
fprintf(fp, " ");
#if URDFDOM_1_0_0_API
for (map<string, LinkSharedPtr>::iterator link = robot->links_.begin();
#else
for (map<string, boost::shared_ptr<Link> >::iterator link = robot->links_.begin();
#endif
link != robot->links_.end(); link++) {
if (add_link_suffix) {
fprintf(fp," %s_lk", link->second->name.c_str());
} else {
fprintf(fp," %s", link->second->name.c_str());
}
}
fprintf(fp, "\n ;; joint names\n");
fprintf(fp, " ");
#if URDFDOM_1_0_0_API
for (map<string, JointSharedPtr>::iterator joint = robot->joints_.begin();
#else
for (map<string, boost::shared_ptr<Joint> >::iterator joint = robot->joints_.begin();
#endif
joint != robot->joints_.end(); joint++) {
if(add_joint_suffix) {
if (joint->second->type == Joint::FIXED) {
fprintf(fp, " %s_fixed_jt", joint->second->name.c_str());
} else {
fprintf(fp, " %s_jt", joint->second->name.c_str());
}
} else {
fprintf(fp, " %s", joint->second->name.c_str());
}
}
fprintf(fp, "\n ;; sensor names\n");
fprintf(fp, " ");
for (vector<daeSensor>::iterator it = m_sensors.begin(); it != m_sensors.end(); it++) {
fprintf(fp, " %s-sensor-coords", it->name.c_str());
}
fprintf(fp, "\n ;; non-default limb names\n");
fprintf(fp, " ");
BOOST_FOREACH(link_joint_pair& limb, limbs) {
if( limb.first == "torso" || limb.first == "larm" || limb.first == "rarm" || limb.first == "lleg" || limb.first == "rleg" || limb.first == "head" ) {
continue;
}
fprintf(fp, " %s %s-end-coords %s-root-link", limb.first.c_str(), limb.first.c_str(), limb.first.c_str());
}
fprintf(fp, "\n )\n )\n");
// TODO: add openrave manipulator tip frame
}
void ModelEuslisp::printRobotMethods() {
if (use_loadable_mesh) {
fprintf(fp, ";;\n(load \"package://eus_assimp/euslisp/eus-assimp.l\") ;; for loadable mesh\n;;\n\n");
}
fprintf(fp, "(defmethod %s-robot\n", arobot_name.c_str());
fprintf(fp, " (:init\n");
fprintf(fp, " (&rest args)\n");
fprintf(fp, " (let ()\n");
// send super :init
fprintf(fp, " (send-super* :init :name \"%s\" args)\n", arobot_name.c_str());
fprintf(fp, "\n");
printLinks();
printJoints();
printMimicJoints();
printEndCoords();
printSensors();
printUniqueLimbs();
printGeometries();
fprintf(fp, " )\n");
}
void ModelEuslisp::printLinks () {
#if URDFDOM_1_0_0_API
for (map<LinkConstSharedPtr, Pose >::iterator it = m_link_coords.begin();
#else
for (map<boost::shared_ptr<const Link>, Pose >::iterator it = m_link_coords.begin();
#endif
it != m_link_coords.end(); it++) {
printLink(it->first, it->second);
}
#if URDFDOM_1_0_0_API
for (map<string, JointSharedPtr>::iterator it = robot->joints_.begin();
#else
for (map<string, boost::shared_ptr<Joint> >::iterator it = robot->joints_.begin();
#endif
it != robot->joints_.end(); it++) {
if (add_link_suffix) {
fprintf(fp, " (send %s_lk :assoc %s_lk)\n",
it->second->parent_link_name.c_str(), it->second->child_link_name.c_str());
} else {
fprintf(fp, " (send %s :assoc %s)\n",
it->second->parent_link_name.c_str(), it->second->child_link_name.c_str());
}
}
if (add_link_suffix) {
fprintf(fp, " (send self :assoc %s_lk)\n", robot->root_link_->name.c_str());
} else {
fprintf(fp, " (send self :assoc %s)\n", robot->root_link_->name.c_str());
}
}
#if URDFDOM_1_0_0_API
void ModelEuslisp::printLink (LinkConstSharedPtr link, Pose &pose) {
#else
void ModelEuslisp::printLink (boost::shared_ptr<const Link> link, Pose &pose) {
#endif
string thisNodeName;
if (add_link_suffix) {
thisNodeName.assign(link->name);
thisNodeName += "_lk";
} else {
thisNodeName.assign(link->name);
}
fprintf(fp, " ;; link: %s\n", thisNodeName.c_str());
fprintf(fp, " (let ((geom-lst (list\n");
{
int geom_counter = 0;
#if URDFDOM_1_0_0_API
map <LinkConstSharedPtr, MapVisual >::iterator it = m_link_visual.find (link);
#else
map <boost::shared_ptr<const Link>, MapVisual >::iterator it = m_link_visual.find (link);
#endif
if (it != m_link_visual.end()) {
for( MapVisual::iterator vmap = it->second.begin();
vmap != it->second.end(); vmap++) {
if(geom_counter > 0) fprintf(fp, "\n");
fprintf(fp, " (send self :_make_instance_%s)", vmap->first.c_str());
geom_counter++;
}
}
#if 0
if(geom_counter == 0) {
fprintf(fp, "(make-cube 10 10 10)))) ;; no geometry in this link\n");
} else {
fprintf(fp, ")))\n");
}
#endif
fprintf(fp, ")))\n"); //
}
fprintf(fp, " (dolist (g (cdr geom-lst)) (send (car geom-lst) :assoc g))\n");
fprintf(fp, " (setq %s\n", thisNodeName.c_str());
fprintf(fp, " (instance bodyset-link\n");
fprintf(fp, " :init (make-cascoords)\n");
fprintf(fp, " :bodies geom-lst\n");
fprintf(fp, " :name \"%s\"))\n", link->name.c_str());
if (!!link->inertial) {
fprintf(fp, " ;; inertial parameter for %s\n", thisNodeName.c_str());
fprintf(fp, " (let ((tmp-cds (make-coords :pos ");
fprintf(fp, "(float-vector "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE")",
link->inertial->origin.position.x * 1000,
link->inertial->origin.position.y * 1000,
link->inertial->origin.position.z * 1000);
{
double qx, qy, qz, qw;
link->inertial->origin.rotation.getQuaternion(qx, qy, qz, qw);
if (qx != 0.0 || qy != 0.0 || qz != 0.0 || qw != 1.0) {
fprintf(fp, "\n :rot (quaternion2matrix ");
fprintf(fp, "(float-vector "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE"))",
qw, qx, qy, qz);
}
fprintf(fp, ")\n");
}
fprintf(fp, " ))\n");
//
fprintf(fp, " (send %s :weight %.3f)\n", thisNodeName.c_str(), link->inertial->mass * 1000);
fprintf(fp, " (setq (%s . inertia-tensor)\n", thisNodeName.c_str());
fprintf(fp, " (m* (m* (send tmp-cds :worldrot) (matrix\n");
fprintf(fp, " (float-vector "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE")\n",
link->inertial->ixx * 1000 * 1000 * 1000,
link->inertial->ixy * 1000 * 1000 * 1000,
link->inertial->ixz * 1000 * 1000 * 1000);
fprintf(fp, " (float-vector "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE")\n",
link->inertial->ixy * 1000 * 1000 * 1000,
link->inertial->iyy * 1000 * 1000 * 1000,
link->inertial->iyz * 1000 * 1000 * 1000);
fprintf(fp, " (float-vector "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE"))\n",
link->inertial->ixz * 1000 * 1000 * 1000,
link->inertial->iyz * 1000 * 1000 * 1000,
link->inertial->izz * 1000 * 1000 * 1000);
fprintf(fp, " ) (transpose (send tmp-cds :worldrot))))\n");
fprintf(fp, " (setq (%s . acentroid) (copy-seq (send tmp-cds :worldpos)))\n", thisNodeName.c_str());
fprintf(fp, " )\n");
} else {
fprintf(fp, " (progn (send %s :weight 0.0) (setq (%s . acentroid) (float-vector 0 0 0)) (send %s :inertia-tensor #2f((0 0 0)(0 0 0)(0 0 0))))\n",
thisNodeName.c_str(), thisNodeName.c_str(), thisNodeName.c_str());
}
//
fprintf(fp, " ;; global coordinates for %s\n", thisNodeName.c_str());
fprintf(fp, " (let ((world-cds (make-coords :pos ");
fprintf(fp, "(float-vector "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE")",
pose.position.x * 1000, pose.position.y * 1000, pose.position.z * 1000);
{
double qx, qy, qz, qw;
pose.rotation.getQuaternion(qx, qy, qz, qw);
if (qx != 0.0 || qy != 0.0 || qz != 0.0 || qw != 1.0) {
fprintf(fp, "\n :rot (quaternion2matrix ");
fprintf(fp, "(float-vector "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE" "FLOAT_PRECISION_FINE"))",
qw, qx, qy, qz);
}
fprintf(fp, ")\n");
}
fprintf(fp, " ))\n");
fprintf(fp, " (send %s :transform world-cds))\n", thisNodeName.c_str());
fprintf(fp, " )\n\n");
}
void ModelEuslisp::printJoints () {
fprintf(fp, "\n ;; joint models\n");
#if URDFDOM_1_0_0_API
for (map<string, JointSharedPtr>::iterator joint = robot->joints_.begin();
#else
for (map<string, boost::shared_ptr<Joint> >::iterator joint = robot->joints_.begin();
#endif
joint != robot->joints_.end(); joint++) {
printJoint(joint->second);
}
}
#if URDFDOM_1_0_0_API
void ModelEuslisp::printJoint (JointConstSharedPtr joint) {
#else
void ModelEuslisp::printJoint (boost::shared_ptr<const Joint> joint) {
#endif
bool linear = (joint->type==Joint::PRISMATIC);
if (joint->type != Joint::REVOLUTE && joint->type !=Joint::CONTINUOUS