-
Notifications
You must be signed in to change notification settings - Fork 3
/
DataGeneration.cpp
executable file
·1579 lines (1290 loc) · 44.5 KB
/
DataGeneration.cpp
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
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 RC 1 *
* (c) 2006-2011 MGH, INRIA, USTL, UJF, CNRS *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Modules *
* *
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#define SOFA_RGBDTRACKING_DATAGENERATION_CPP
#include <sofa/core/ObjectFactory.h>
#include <sofa/core/visual/VisualParams.h>
#include <sofa/core/behavior/ForceField.inl>
#include <sofa/core/objectmodel/BaseContext.h>
#include <sofa/core/Mapping.inl>
#include <sofa/simulation/Simulation.h>
#include <sofa/core/topology/BaseMeshTopology.h>
#include <sofa/gui/BaseGUI.h>
#include <sofa/gui/BaseViewer.h>
#include <sofa/gui/GUIManager.h>
#include <iostream>
#include <map>
#ifdef USING_OMP_PRAGMAS
#include <omp.h>
#endif
#include <SofaLoader/MeshObjLoader.h>
#include <limits>
#include <set>
#include <iterator>
#include <sofa/helper/gl/Color.h>
#ifdef Success
#undef Success
#endif
#include <pcl/common/common_headers.h>
#include <pcl/point_cloud.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/point_types.h>
#include <pcl/impl/point_types.hpp>
#include <pcl/features/normal_3d.h>
#include <pcl/common/projection_matrix.h>
#include <pcl/io/pcd_io.h>
#include <pcl/common/io.h>
#include <pcl/registration/transforms.h>
#include <pcl/search/kdtree.h>
#include <pcl/registration/transformation_estimation_svd.h>
#include <pcl/registration/icp.h>
#include <pcl/registration/correspondence_rejection_sample_consensus.h>
#include <pcl/common/transforms.h>
#include <algorithm>
#include "DataGeneration.h"
using std::cerr;
using std::endl;
namespace sofa
{
namespace component
{
namespace forcefield
{
using namespace sofa::defaulttype;
SOFA_DECL_CLASS(DataGeneration)
// Register in the Factory
int DataGenerationClass = core::RegisterObject("Compute forces based on closest points from/to a target surface/point set")
#ifndef SOFA_FLOAT
.add< DataGeneration<Vec3dTypes,ImageUC> >()
.add< DataGeneration<Vec3dTypes,ImageUS> >()
.add< DataGeneration<Vec3dTypes,ImageF> >()
#endif
#ifndef SOFA_DOUBLE
.add< DataGeneration<Vec3fTypes,ImageUC> >()
.add< DataGeneration<Vec3fTypes,ImageUS> >()
.add< DataGeneration<Vec3fTypes,ImageF> >()
#endif
;
#ifndef SOFA_FLOAT
template class SOFA_RGBDTRACKING_API DataGeneration<Vec3dTypes,ImageUC>;
template class SOFA_RGBDTRACKING_API DataGeneration<Vec3dTypes,ImageUS>;
template class SOFA_RGBDTRACKING_API DataGeneration<Vec3dTypes,ImageF>;
#endif
#ifndef SOFA_DOUBLE
template class SOFA_RGBDTRACKING_API DataGeneration<Vec3fTypes,ImageUC>;
template class SOFA_RGBDTRACKING_API DataGeneration<Vec3fTypes,ImageUS>;
template class SOFA_RGBDTRACKING_API DataGeneration<Vec3fTypes,ImageF>;
#endif
using namespace helper;
template <class DataTypes, class DepthTypes>
DataGeneration<DataTypes, DepthTypes>::DataGeneration(core::behavior::MechanicalState<DataTypes> *mm )
: Inherit(mm)
, depthImage(initData(&depthImage,DepthTypes(),"depthImage","depth map"))
//, depthTransform(initData(&depthTransform, TransformType(), "depthTransform" , ""))
, image(initData(&image,ImageTypes(),"image","image"))
//, transform(initData(&transform, TransformType(), "transform" , ""))
, cameraIntrinsicParameters(initData(&cameraIntrinsicParameters,Vector4(),"cameraIntrinsicParameters","camera parameters"))
, ks(initData(&ks,(Real)0.0,"stiffness","uniform stiffness for the all springs."))
, kd(initData(&kd,(Real)0.0,"damping","uniform damping for the all springs."))
, cacheSize(initData(&cacheSize,(unsigned int)10,"cacheSize","number of closest points used in the cache to speed up closest point computation."))
, normalThreshold(initData(&normalThreshold,(Real)0,"normalThreshold","suppress outliers when normal.closestPointNormal < threshold."))
, projectToPlane(initData(&projectToPlane,false,"projectToPlane","project closest points in the plane defined by the normal."))
, rejectBorders(initData(&rejectBorders,false,"rejectBorders","ignore border vertices."))
, springs(initData(&springs,"spring","index, stiffness, damping"))
, sourceSurfacePositions(initData(&sourceSurfacePositions,"sourceSurface","Points of the surface of the source mesh."))
, sourcePositions(initData(&sourcePositions,"sourcePositions","Points of the surface of the source mesh."))
, sourceTriangles(initData(&sourceTriangles,"sourceTriangles","Triangles of the source mesh."))
, sourceNormals(initData(&sourceNormals,"sourceNormals","Normals of the source mesh."))
, sourceSurfaceNormals(initData(&sourceSurfaceNormals,"sourceSurfaceNormals","Normals of the surface of the source mesh."))
, showStrainsPerElement(initData(&showStrainsPerElement, false, "showStrainsPerElement", " "))
, plasticStrainsN(initData(&plasticStrainsN, "plasticStrains", "plastic strain per element"))
, elasticStrainsN(initData(&elasticStrainsN, "elasticStrains", "elastic strain per element"))
, totalStrainsN(initData(&totalStrainsN, "totalStrains", "total strain per element"))
, plasticStrainsPerNode(initData(&plasticStrainsPerNode, "plasticStrainsPerNode", "plastic strain per node"))
, elasticStrainsPerNode(initData(&elasticStrainsPerNode, "elasticStrainsPerNode", "elastic strain per node"))
, totalStrainsPerNode(initData(&totalStrainsPerNode, "totalStrainsPerNode", "total strain per node"))
, vonMisesStress(initData(&vonMisesStress, "vonMisesStress", "vonmisesstress per element"))
, barycenter(initData(&barycenter,"barycenter","Barycenter of the mesh."))
, showArrowSize(initData(&showArrowSize,0.01f,"showArrowSize","size of the axis."))
, drawMode(initData(&drawMode,0,"drawMode","The way springs will be drawn:\n- 0: Line\n- 1:Cylinder\n- 2: Arrow."))
, drawColorMap(initData(&drawColorMap,true,"drawColorMap","Hue mapping of distances to closest point"))
, theCloserTheStiffer(initData(&theCloserTheStiffer,false,"theCloserTheStiffer","Modify stiffness according to distance"))
, useContour(initData(&useContour,false,"useContour","Emphasize forces close to the target contours"))
, useVisible(initData(&useVisible,true,"useVisible","Use the vertices of the viisible surface of the source mesh"))
, visibilityThreshold(initData(&visibilityThreshold,(Real)0.001,"visibilityThreshold","Threshold to determine visible vertices"))
, alphaIntensity(initData(&alphaIntensity,(Real)0.00004,"alphaIntensity","Weight of intensity features"))
, useRealData(initData(&useRealData,true,"useRealData","Use real data"))
, useGroundTruth(initData(&useGroundTruth,false,"useGroundTruth","Use the vertices of the visible surface of the source mesh"))
//, useKalman(initData(&useKalman,false,"useKalman","Use the Kalman filter"))
, sensorType(initData(&sensorType, 0,"sensorType","Type of the sensor"))
, generateSynthData(initData(&generateSynthData, false,"generateSynthData","Generate synthetic data"))
, niterations(initData(&niterations,3,"niterations","Number of iterations in the tracking process"))
, nimages(initData(&nimages,1500,"nimages","Number of images to read"))
, borderThdPCD(initData(&borderThdPCD,4,"borderThdPCD","border threshold on the target silhouette"))
, borderThdSource(initData(&borderThdSource,7,"borderThdSource","border threshold on the source silhouette"))
, inputPath(initData(&inputPath,"inputPath","Path for data readings",false))
, outputPath(initData(&outputPath,"outputPath","Path for data writings",false))
{
//softk.init();
this->addAlias(&depthImage, "depthImage");
depthImage.setGroup("depthImage");
depthImage.setReadOnly(true);
this->addAlias(&image, "image");
image.setGroup("image");
image.setReadOnly(true);
//niterations.setValue(2);
nimages = 1500;
pcl = false;
disp = false;
iter_im = 0;
timeTotal = 0;
color = cv::Mat::zeros(240,320, CV_8UC3);
color_1 = color.clone();
color_2 = color_1.clone();
color_3 = color_2.clone();
color_4 = color_3.clone();
color_5 = color_4.clone();
depth = cv::Mat::zeros(240,320,CV_32FC1);
depth_1 = depth;
listimg.resize(0);
listimgseg.resize(0);
listimgklt.resize(0);
listdepth.resize(0);
listrtt.resize(0);
listrttstress.resize(0);
listrttstressplast.resize(0);
listpcd.resize(0);
listvisible.resize(0);
timef = 0;//(double)getTickCount();
timei = 0;
timeOverall = 0;
timeResolution = 0;
timer = 0;
timeSecondPass = 0;
timeFirstPass = 0;
timeOverall1 = 0;
// Tracker parameters
}
template <class DataTypes, class DepthTypes>
DataGeneration<DataTypes, DepthTypes>::~DataGeneration()
{
}
template <class DataTypes, class DepthTypes>
void DataGeneration<DataTypes, DepthTypes>::reinit()
{
const VecCoord& x = this->mstate->read(core::ConstVecCoordId::position())->getValue();
this->clearSprings(x.size());
for(unsigned int i=0;i<x.size();i++) this->addSpring(i, (Real) ks.getValue(),(Real) kd.getValue());
}
template <class DataTypes, class DepthTypes>
void DataGeneration<DataTypes, DepthTypes>::init()
{
//pcddataprocessing.init();
//**********************************************************************************
this->Inherit::init();
core::objectmodel::BaseContext* context = this->getContext();
if(!(this->mstate)) this->mstate = dynamic_cast<sofa::core::behavior::MechanicalState<DataTypes> *>(context->getMechanicalState());
// Get source normals
if(!sourceNormals.getValue().size()) serr<<"normals of the source model not found"<<sendl;
// add a spring for every input point
const VecCoord& x = this->mstate->read(core::ConstVecCoordId::position())->getValue(); //RDataRefVecCoord x(*this->getMState()->read(core::ConstVecCoordId::position()));
this->clearSprings(x.size());
npoints = x.size();
for(unsigned int i=0;i<x.size();i++) this->addSpring(i, (Real) ks.getValue(),(Real) kd.getValue());
//initCamera();
bool opt_device = false;
bool opt_display = true;
bool use_cuda = true;
bool opt_click_allowed = true;
int start_image = 0;
/*listimg.resize(0);
listimgseg.resize(0);
listdepth.resize(0);*/
cv::Rect ROI(160, 120, 320, 240);
sofa::simulation::Node::SPtr root = dynamic_cast<simulation::Node*>(this->getContext());
root->get(meshprocessing);
root->get(rendertexturear);
meshprocessing->visibilityThreshold.setValue(visibilityThreshold.getValue());
meshprocessing->borderThdSource.setValue(borderThdSource.getValue());
if (showStrainsPerElement.getValue())
npasses = 2;
else npasses = 1;
}
template <class DataTypes, class DepthTypes>
void DataGeneration<DataTypes, DepthTypes>::resetSprings()
{
this->clearSprings(sourceVisiblePositions.getValue().size());
for(unsigned int i=0;i<sourceVisiblePositions.getValue().size();i++) this->addSpring(i, (Real) ks.getValue(),(Real) kd.getValue());
}
template<class DataTypes, class DepthTypes>
void DataGeneration<DataTypes, DepthTypes>::initSource()
{
// build k-d tree
const VecCoord& p = this->mstate->read(core::ConstVecCoordId::position())->getValue();
}
template<class DataTypes, class DepthTypes>
void DataGeneration<DataTypes, DepthTypes>::initSourceSurface()
{
// build k-d tree
const VecCoord& p = this->mstate->read(core::ConstVecCoordId::position())->getValue();
ReadAccessor< Data< VecCoord > > pSurface(sourceSurfacePositions);
ReadAccessor< Data< VecCoord > > nSurface(sourceSurfaceNormals);
VecCoord nSurfaceM;
sourceSurface.resize(p.size());
Vector3 pos;
Vector3 col;
nSurfaceM.resize(p.size());
for (unsigned int i=0; i<p.size(); i++)
{
sourceSurface[i] = false;
for (unsigned int j=0; j<pSurface.size(); j++)
{
Real dist=(p[i]-pSurface[j]).norm();
if ((float)dist < 0.001)
{
sourceSurface[i] = true;
nSurfaceM[i] = nSurface[j];
}
}
}
sourceSurfaceNormalsM.setValue(nSurfaceM);
}
template<class DataTypes, class DepthTypes>
void DataGeneration<DataTypes, DepthTypes>::initSourceVisible()
{
// build k-d tree
const VecCoord& p = sourceVisiblePositions.getValue();
}
void writePCDToFile(string path, std::vector<Vec3d>& pcd, std::vector<bool>& visible)
{
//create the file stream
ofstream file(path.c_str(), ios::out | ios::binary );
double dvalue0,dvalue1,dvalue2;
for (int k = 0; k < pcd.size(); k++)
{
dvalue0 = pcd[k][0];
dvalue1 = pcd[k][1];
dvalue2 = pcd[k][2];
file << dvalue0;
file << "\t";
file << dvalue1;
file << "\t";
file << dvalue2;
file << "\t";
if (visible[k])
file << 1;
else file << 0;
file << "\n";
}
file.close();
}
void writeStressStrainToFile(string path1, string path2, std::vector<double>& vMStress, std::vector<double>& plasticStrains, std::vector<double>& elasticStrainsPerNode, std::vector<double>& plasticStrainsPerNode)
{
//create the file stream
ofstream file1(path1.c_str(), ios::out | ios::binary );
ofstream file2(path2.c_str(), ios::out | ios::binary );
double dvalue0,dvalue1,dvalue2,dvalue3;
for (int k = 0; k < vMStress.size(); k++)
{
dvalue0 = vMStress[k];
dvalue1 = plasticStrains[k];
file1 << dvalue0;
file1 << "\t";
file1 << dvalue1;
file1 << "\n";
}
file1.close();
for (int k = 0; k < elasticStrainsPerNode.size(); k++)
{
//std::cout << " elastic strains " << elasticStrainsPerNode[k] << std::endl;
dvalue2 = elasticStrainsPerNode[k];
dvalue3 = plasticStrainsPerNode[k];
file2 << dvalue2;
file2 << "\t";
file2 << dvalue3;
file2 << "\n";
}
file2.close();
}
int writeMatToFile(const cv::Mat &I, string path) {
//load the matrix size
int matWidth = I.size().width, matHeight = I.size().height;
//read type from Mat
int type = I.type();
//declare values to be written
float fvalue;
double dvalue;
Vec3f vfvalue;
Vec3d vdvalue;
//create the file stream
ofstream file(path.c_str(), ios::out | ios::binary );
if (!file)
return -1;
//write type and size of the matrix first
file.write((const char*) &type, sizeof(type));
file.write((const char*) &matWidth, sizeof(matWidth));
file.write((const char*) &matHeight, sizeof(matHeight));
//write data depending on the image's type
switch (type)
{
default:
cout << "Error: wrong Mat type: must be CV_32F, CV_64F, CV_32FC3 or CV_64FC3" << endl;
break;
// FLOAT ONE CHANNEL
case CV_32F:
//cout << "Writing CV_32F image" << endl;
for (int i=0; i < matWidth*matHeight; ++i) {
fvalue = I.at<float>(i);
file.write((const char*) &fvalue, sizeof(fvalue));
}
break;
// DOUBLE ONE CHANNEL
case CV_64F:
cout << "Writing CV_64F image" << endl;
for (int i=0; i < matWidth*matHeight; ++i) {
dvalue = I.at<double>(i);
file.write((const char*) &dvalue, sizeof(dvalue));
}
break;
// FLOAT THREE CHANNELS
case CV_32FC3:
cout << "Writing CV_32FC3 image" << endl;
for (int i=0; i < matWidth*matHeight; ++i) {
vfvalue = I.at<Vec3f>(i);
file.write((const char*) &vfvalue, sizeof(vfvalue));
}
break;
// DOUBLE THREE CHANNELS
case CV_64FC3:
cout << "Writing CV_64FC3 image" << endl;
for (int i=0; i < matWidth*matHeight; ++i) {
vdvalue = I.at<Vec3d>(i);
file.write((const char*) &vdvalue, sizeof(vdvalue));
}
break;
}
//close file
file.close();
return 0;
}
int writeMatToFile0(const cv::Mat &I, string path) {
//load the matrix size
int matWidth = I.size().width, matHeight = I.size().height;
//read type from Mat
int type = I.type();
//declare values to be written
float fvalue;
double dvalue;
Vec3f vfvalue;
Vec3d vdvalue;
//create the file stream
ofstream file(path.c_str(), ios::out | ios::binary );
if (!file)
return -1;
//write type and size of the matrix first
file.write((const char*) &type, sizeof(type));
file.write((const char*) &matWidth, sizeof(matWidth));
file.write((const char*) &matHeight, sizeof(matHeight));
//write data depending on the image's type
switch (type)
{
default:
cout << "Error: wrong Mat type: must be CV_32F, CV_64F, CV_32FC3 or CV_64FC3" << endl;
break;
// FLOAT ONE CHANNEL
case CV_32F:
cout << "Writing CV_32F image" << endl;
for (int i=0; i < matWidth*matHeight; ++i) {
fvalue = I.at<float>(i);
file.write((const char*) &fvalue, sizeof(fvalue));
}
break;
// DOUBLE ONE CHANNEL
case CV_64F:
cout << "Writing CV_64F image" << endl;
for (int i=0; i < matWidth*matHeight; ++i) {
dvalue = I.at<double>(i);
file.write((const char*) &dvalue, sizeof(dvalue));
}
break;
// FLOAT THREE CHANNELS
case CV_32FC3:
cout << "Writing CV_32FC3 image" << endl;
for (int i=0; i < matWidth*matHeight; ++i) {
vfvalue = I.at<Vec3f>(i);
file.write((const char*) &vfvalue, sizeof(vfvalue));
}
break;
// DOUBLE THREE CHANNELS
case CV_64FC3:
cout << "Writing CV_64FC3 image" << endl;
for (int i=0; i < matWidth*matHeight; ++i) {
vdvalue = I.at<Vec3d>(i);
file.write((const char*) &vdvalue, sizeof(vdvalue));
}
break;
}
//close file
file.close();
return 0;
}
template<class DataTypes, class DepthTypes>
void DataGeneration<DataTypes, DepthTypes>::writeData()
{
//std::string opath = "in/images4/img1%06d.png";
std::string opath = outputPath.getValue() + "/img1%06d.png";
std::string opathdepth = outputPath.getValue() + "/depth%06d.png";
std::string opath1 = outputPath.getValue() + "/depthfile%06d.txt";
std::string opath2 = outputPath.getValue() + "/depthim%06d.txt";
std::string opath3 = outputPath.getValue() + "/stressstrain%06d.txt";
std::string opath4 = outputPath.getValue() + "/stressstrainNode%06d.txt";
std::string extensionfile = outputPath.getValue() + "/in/images4/extension.txt";
std::vector<Vec3d> pcd1;
std::vector<double> vmstress;
std::vector<double> plsstrain;
std::vector<double> plsstrainnode;
std::vector<double> elsstrainnode;
std::vector<bool> visible1;
ofstream extfile(extensionfile.c_str(), ios::out | ios::binary );
double extension;
double pos = 0;
double pos0;
cv::Mat rtt,rtt1,rtt2,depthi, depthin;
for (int frame_count = 0 ;frame_count < listpcd.size(); frame_count++)
{
std::cout << " ok write " << frame_count << std::endl;
pcd1 = *listpcd[frame_count];
vmstress = *listvm[frame_count];
elsstrainnode = *listesnode[frame_count];
plsstrain = *listps[frame_count];
plsstrainnode = *listpsnode[frame_count];
pos = pcd1[132][1];
if (frame_count == 0) pos0 = pos;
extension = pos - pos0;
extfile << pos;
extfile << "\t";
extfile << extension;
extfile << "\n";
visible1 = *listvisible[frame_count];
char buf1[FILENAME_MAX];
sprintf(buf1, opath1.c_str(), frame_count);
std::string filename1(buf1);
char buf3[FILENAME_MAX];
sprintf(buf3, opath3.c_str(), frame_count);
std::string filename3(buf3);
char buf4[FILENAME_MAX];
sprintf(buf4, opath4.c_str(), frame_count);
std::string filename4(buf4);
writePCDToFile(filename1,pcd1,visible1);
writeStressStrainToFile(filename3, filename4, vmstress, plsstrain, elsstrainnode, plsstrainnode);
/*delete listimg[frame_count];
delete listimgseg[frame_count];
delete listdepth[frame_count];*/
}
extfile.close();
//for (int frame_count = 1 ;frame_count < nimages.getValue()*niterations.getValue(); frame_count++)
for (int frame_count = 1 ;frame_count < listrtt.size(); frame_count++)
{
std::cout << " ok write rtt0 " << frame_count << std::endl;
rtt = *listrtt[frame_count];
cvtColor(rtt,rtt1 ,CV_RGB2BGR);
cv::flip(rtt1,rtt2,0);
//cv::flip(rtt1,rtt2,0);
char buf4[FILENAME_MAX];
sprintf(buf4, opath.c_str(), frame_count-1);
std::string filename4(buf4);
cv::imwrite(filename4,rtt2);
depthi = *listdepth[frame_count];
char buf5[FILENAME_MAX];
sprintf(buf5, opathdepth.c_str(), frame_count-1);
std::string filename5(buf5);
char buf6[FILENAME_MAX];
sprintf(buf6, opath2.c_str(), frame_count-1);
std::string filename6(buf6);
ofstream depthim(buf6, ios::out | ios::binary );
for (int j = 0; j < depthi.cols; j++)
for (int i = 0; i< depthi.rows; i++)
{
depthim << depthi.at<float>(i,j);
depthim << "\n";
}
cv::flip(depthi,depthin,0);
depthin.convertTo(depthi, CV_8UC1, 255);
cv::Mat depth0 = depthi.clone();
cv::imwrite(filename5,depth0);
char buf1[FILENAME_MAX];
sprintf(buf1, opath1.c_str(), frame_count-1);
std::string filename1(buf1);
writeMatToFile0(depthin,filename1);
}
}
template<class DataTypes, class DepthTypes>
void DataGeneration<DataTypes, DepthTypes>::setViewPointData()
{
Eigen::Affine3f scene_sensor_pose (Eigen::Affine3f::Identity ());
source.reset(new pcl::PointCloud<pcl::PointXYZRGB>);
const VecCoord& x = this->mstate->read(core::ConstVecCoordId::position())->getValue();
pcl::PointXYZRGB newPoint;
for (unsigned int i=0; i<x.size(); i++)
{
newPoint.z = x[i][2];
newPoint.x = x[i][0];
newPoint.y = x[i][1];
//cout << "OK " << newPoint.z << " " << newPoint.x << " " << newPoint.y << endl;
newPoint.r = 0;
newPoint.g = 0;
newPoint.b = 0;
source->points.push_back(newPoint);
}
pcl::PointCloud<pcl::PointXYZRGB>& point_cloud = *source;
scene_sensor_pose = Eigen::Affine3f (Eigen::Translation3f (point_cloud.sensor_origin_[0],
point_cloud.sensor_origin_[1],
point_cloud.sensor_origin_[2])) *
Eigen::Affine3f (point_cloud.sensor_orientation_);
Eigen::Affine3f viewer_pose = scene_sensor_pose;
Eigen::Vector3f pos_vector = viewer_pose * Eigen::Vector3f(0, 0, 0);
Eigen::Vector3f look_at_vector = viewer_pose.rotation () * Eigen::Vector3f(0, 0, 1) + pos_vector;
Eigen::Vector3f up_vector = viewer_pose.rotation () * Eigen::Vector3f(0, -1, 0);
/*viewer.setCameraPosition (pos_vector[0], pos_vector[1], pos_vector[2],
look_at_vector[0], look_at_vector[1], look_at_vector[2],
up_vector[0], up_vector[1], up_vector[2]);*/
int hght = 480;
int wdth = 640;
sofa::gui::GUIManager::SetDimension(wdth,hght);
sofa::gui::BaseGUI *gui = sofa::gui::GUIManager::getGUI();
if (!gui)
{
std::cout << " no gui " << std::endl;
}
sofa::gui::BaseViewer * viewer = gui->getViewer();
if (!viewer)
{
std::cout << " no viewer " << std::endl;
}
//viewer->getView(pos,orient);
{
Vec3d position;
Quat orientation;
orientation[0] = point_cloud.sensor_orientation_.w ();
orientation[1] = point_cloud.sensor_orientation_.x ();
orientation[2] = point_cloud.sensor_orientation_.y ();
orientation[3] = point_cloud.sensor_orientation_.z ();
position[0] = point_cloud.sensor_origin_[0];
position[1] = point_cloud.sensor_origin_[1];
position[2] = point_cloud.sensor_origin_[2];
//if (currentCamera)
//currentCamera->setView(position, orientation);
viewer->setView(position, orientation);
//glutPostRedisplay();
//glPushAttrib( GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_LINE_BIT | GL_CURRENT_BIT);
//glPopAttrib();
}
}
template <class DataTypes, class DepthTypes>
void DataGeneration<DataTypes, DepthTypes>::generateData(const core::MechanicalParams* mparams,DataVecDeriv& _f , const DataVecCoord& _x , const DataVecDeriv& _v )
{
int t = (int)this->getContext()->getTime();
//getImages();
//readData()
//timeOverall
double timeT;
if (t == 0)
{
timeOverall = 0;
timeTotal = 0;
timeT = (double)getTickCount();
//extractTargetContour();
}
else if (t > 0 && t%niterations.getValue() == 0)
{
double time0 = (double)getTickCount();
double time1 = (double)getTickCount();
}
if(ks.getValue()==0) return;
VecDeriv& f = *_f.beginEdit(); //WDataRefVecDeriv f(_f);
const VecCoord& x = _x.getValue(); //RDataRefVecCoord x(_x);
const VecDeriv& v = _v.getValue(); //RDataRefVecDeriv v(_v);
ReadAccessor< Data< VecCoord > > ssn(sourceSurfaceNormalsM);
ReadAccessor< Data<helper::vector<Real> > > vM(vonMisesStress);
ReadAccessor< Data<helper::vector<Real> > > eS(elasticStrainsN);
ReadAccessor< Data<helper::vector<Real> > > pS(plasticStrainsN);
ReadAccessor< Data<helper::vector<Real> > > tS(totalStrainsN);
ReadAccessor< Data<helper::vector<Real> > > eSNode(elasticStrainsPerNode);
ReadAccessor< Data<helper::vector<Real> > > pSNode(plasticStrainsPerNode);
ReadAccessor< Data<helper::vector<Real> > > tSNode(totalStrainsPerNode);
const vector<Spring>& s = this->springs.getValue();
this->dfdx.resize(s.size());
this->closestPos.resize(s.size());
initSourceSurface();
if (t < 2)
setViewPointData();
sofa::simulation::Node::SPtr root = dynamic_cast<simulation::Node*>(this->getContext());
sofa::component::visualmodel::BaseCamera::SPtr currentCamera;// = root->getNodeObject<sofa::component::visualmodel::InteractiveCamera>();
root->get(currentCamera);
if (t > 1)
{
double znear = currentCamera->getZNear();
double zfar = currentCamera->getZFar();
//meshprocessing->getSourceVisible(znear, zfar);
sourceVisible = meshprocessing->sourceVisible;
sourceVisiblePositions.setValue(meshprocessing->getSourceVisiblePositions());
indicesVisible = meshprocessing->indicesVisible;
depthMap = meshprocessing->depthMap.clone();
std::cout << " npoints " << std::endl;
double maxz = 0;
int kmaxz;
double minx = 100;
int kminx;
double minx1 = 100;
int kminx1;
double maxx = 0;
int kmaxx;
double maxx1 = 0;
int kmaxx1;
double maxz2 = 0;
int kmaxz2;
double maxz3 = 0;
int kmaxz3;
double maxz4 = 0;
int kmaxz4;
double maxz5 = 0;
int kmaxz5;
double maxz6 = 0;
int kmaxz6;
double maxz7 = -1000;
int kmaxz7;
double maxz8 = -1000;
int kmaxz8;
double maxz9 = -1000;
int kmaxz9;
double maxz10 = -1000;
int kmaxz10;
double minz = 100;
int kminz;
double minz2 = 100;
int kminz2;
double minz3 = 100;
int kminz3;
if (t > 0 && t%niterations.getValue() == 0)
{
double timertt = (double)getTickCount();
std::cout << " npoints 12 " << std::endl;
rtt = new cv::Mat;
depthl = new cv::Mat;
cv::Mat rtt_,rttdepth_;
rendertexturear->renderToTextureDepth(rtt_,rttdepth_);
*rtt = rtt_.clone();
*depthl = rttdepth_.clone();
listrtt.push_back(rtt);
listdepth.push_back(depthl);
timertt = ((double)getTickCount() - timertt)/getTickFrequency();
cout << "Time RTT " << timertt << endl;
pcd = new std::vector<Vec3d>;
pcd->resize(0);
Vec3d pcd0;
visible = new std::vector<bool>;
visible->resize(0);
bool vis0;
vm = new std::vector<double>;
vm->resize(0);
es = new std::vector<double>;
es->resize(0);
ps = new std::vector<double>;
ps->resize(0);
ts = new std::vector<double>;
ts->resize(0);
esnode = new std::vector<double>;
esnode->resize(0);
psnode = new std::vector<double>;
psnode->resize(0);
tsnode = new std::vector<double>;
tsnode->resize(0);
std::cout << " srouce visible" << sourceVisible.size() << std::endl;
for (unsigned int i=0; i<x.size(); i++)
{
if(sourceVisible[i])
{
vis0 = true;
}
else vis0 = false;
if (x[i][0] < minx)
{
minx = x[i][0];
kminx = i;
}
else
{
if (x[i][0] < minx1)
{
minx1 = x[i][0];
kminx1 = i;
}
}
if (x[i][0] > maxx)
{
maxx = x[i][0];
kmaxx = i;
}
else
{
if (x[i][0] > maxx1)
{
maxx1 = x[i][0];
kmaxx1 = i;
}
}
if (x[i][1] > maxz)
{
maxz = x[i][1];
kmaxz = i;
}
else
{
if (x[i][1] > maxz2)
{
maxz2 = x[i][1];
kmaxz2 = i;
}
else
{
if (x[i][1] > maxz3)
{
maxz3 = x[i][1];
kmaxz3 = i;
}
else
{
if (x[i][1] > maxz4)
{
maxz4 = x[i][1];
kmaxz4 = i;
}
else
{
if (x[i][1] > maxz5)
{
maxz5 = x[i][1];
kmaxz5 = i;
}
else
{
if (x[i][1] > maxz6)
{
maxz6 = x[i][1];
kmaxz6 = i;
}
else
{
//std::cout << " ok " << x[i][1] << std::endl;
if (x[i][1] > maxz7)
{
maxz7 = x[i][1];
kmaxz7 = i;
}
else{
if (x[i][1] > maxz8)
{
maxz8 = x[i][1];
kmaxz8 = i;
}
else
{
if (x[i][1] > maxz9)
{
maxz9 = x[i][1];
kmaxz9 = i;
}
}
}
}