-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgeogrid.cpp
1367 lines (1197 loc) · 51 KB
/
geogrid.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
#include "domain/geogrid.h"
#include "viewer3d/view3dviewdata.h"
#include "viewer3d/view3dbuilders.h"
#include "domain/attribute.h"
#include "domain/cartesiangrid.h"
#include "spatialindex/spatialindex.h"
#include "domain/application.h"
#include "auxiliary/meshloader.h"
#include "domain/pointset.h"
#include "domain/segmentset.h"
#include "util.h"
#include "domain/project.h"
#include "geometry/vector3d.h"
#include "geometry/face3d.h"
#include "geometry/boundingbox.h"
#include "exceptions/invalidmethodexception.h"
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <QProgressDialog>
#include <QTextStream>
#include <QThread>
#include <QApplication>
#include <cassert>
GeoGrid::GeoGrid( QString path ) :
GridFile( path ),
m_spatialIndex( new SpatialIndex() ),
m_lastModifiedDateTimeLastMeshLoad()
{
this->_no_data_value = "";
this->m_nreal = 1;
this->m_nI = 0;
this->m_nJ = 0;
this->m_nK = 0;
}
GeoGrid::GeoGrid(QString path, Attribute * atTop, Attribute * atBase, uint nHorizonSlices) :
GridFile( path ),
m_spatialIndex( new SpatialIndex() ),
m_lastModifiedDateTimeLastMeshLoad()
{
CartesianGrid *cgTop = dynamic_cast<CartesianGrid*>( atTop->getContainingFile() );
CartesianGrid *cgBase = dynamic_cast<CartesianGrid*>( atBase->getContainingFile() );
assert( cgTop && "GeoGrid(): top attribute is not from a CartesianGrid object." );
assert( cgBase && "GeoGrid(): base attribute is not from a CartesianGrid object." );
assert( cgBase == cgTop && "GeoGrid(): top and base attributes must be from the same CartesianGrid object." );
assert( ! cgBase->isTridimensional() && "GeoGrid(): The CartesianGrid of top and base must be 2D (map)." );
uint nIVertexes = cgBase->getNI();
uint nJVertexes = cgBase->getNJ();
uint nKVertexes = nHorizonSlices + 1;
uint nVertexes = nIVertexes * nJVertexes * nKVertexes;
//allocate the vertex list
m_vertexesPart.reserve( nVertexes );
//get the indexes of the properties holding the top and base values.
uint columnIndexBase = atBase->getAttributeGEOEASgivenIndex()-1;
uint columnIndexTop = atTop->getAttributeGEOEASgivenIndex()-1;
//create the vertexes
for( uint k = 0; k < nKVertexes; ++k ){
for( uint j = 0; j < nJVertexes; ++j ){
for( uint i = 0; i < nIVertexes; ++i ){
//get base and top values
double vBase = cgBase->dataIJK( columnIndexBase, i, j, 0 );
double vTop = cgTop->dataIJK( columnIndexTop, i, j, 0 );
//compute the depth (z) of the current vertex.
double depth = vBase + ( k / (double)nHorizonSlices ) * ( vTop - vBase );
//create and set the position of the vertex
VertexRecordPtr vertex( new VertexRecord() );
double x, y, z;
cgBase->IJKtoXYZ( i, j, 0, x, y, z );
vertex->X = x;
vertex->Y = y;
vertex->Z = depth;
m_vertexesPart.push_back( vertex );
}
}
}
//define the number of cells (cell centered values, one less than the number of vertexes in each direction )
uint nICells = cgBase->getNI()-1;
uint nJCells = cgBase->getNJ()-1;
uint nKCells = nHorizonSlices;
uint nCells = nICells * nJCells * nKCells;
//allocate the cell definition list
m_cellDefsPart.reserve( nCells );
//assign vertexes id's to the cells
for( uint k = 0; k < nKCells; ++k ){
for( uint j = 0; j < nJCells; ++j ){
for( uint i = 0; i < nICells; ++i ){
CellDefRecordPtr cellDef( new CellDefRecord() );
//see Doxygen of CellDefRecordPtr in geogrid.h for a diagram of vertex arrangement in space
// and how they form the edges and faces of the visual representation of the cell.
cellDef->vId[0] = ( k + 0 ) * nJVertexes * nIVertexes + ( j + 0 ) * nIVertexes + ( i + 0 );
cellDef->vId[1] = ( k + 0 ) * nJVertexes * nIVertexes + ( j + 0 ) * nIVertexes + ( i + 1 );
cellDef->vId[2] = ( k + 0 ) * nJVertexes * nIVertexes + ( j + 1 ) * nIVertexes + ( i + 1 );
cellDef->vId[3] = ( k + 0 ) * nJVertexes * nIVertexes + ( j + 1 ) * nIVertexes + ( i + 0 );
cellDef->vId[4] = ( k + 1 ) * nJVertexes * nIVertexes + ( j + 0 ) * nIVertexes + ( i + 0 );
cellDef->vId[5] = ( k + 1 ) * nJVertexes * nIVertexes + ( j + 0 ) * nIVertexes + ( i + 1 );
cellDef->vId[6] = ( k + 1 ) * nJVertexes * nIVertexes + ( j + 1 ) * nIVertexes + ( i + 1 );
cellDef->vId[7] = ( k + 1 ) * nJVertexes * nIVertexes + ( j + 1 ) * nIVertexes + ( i + 0 );
m_cellDefsPart.push_back( cellDef );
}
}
}
//initialize member variables
m_nI = nICells;
m_nJ = nJCells;
m_nK = nKCells;
m_nreal = 1;
_no_data_value = "";
}
GeoGrid::GeoGrid(QString path, std::vector<GeoGridZone> zones) :
GridFile( path ),
m_spatialIndex( new SpatialIndex() ),
m_lastModifiedDateTimeLastMeshLoad()
{
//get origin Cartesian grid and do some sanity checks
CartesianGrid *cg = nullptr;
for( const GeoGridZone zone : zones ){
CartesianGrid *cgTop = dynamic_cast<CartesianGrid*>( zone.top->getContainingFile() );
CartesianGrid *cgBase = dynamic_cast<CartesianGrid*>( zone.base->getContainingFile() );
assert( cgTop && "GeoGrid(): top attribute is not from a CartesianGrid object." );
assert( cgBase && "GeoGrid(): base attribute is not from a CartesianGrid object." );
assert( cgBase == cgTop && "GeoGrid(): top and base attributes must be from the same CartesianGrid object." );
assert( ! cgBase->isTridimensional() && "GeoGrid(): The CartesianGrid of top and base must be 2D (map)." );
cg = cgTop;
}
assert( cg && "GeoGrid(): no zones passed." );
//get grid geometry
uint nIVertexes = cg->getNI();
uint nJVertexes = cg->getNJ();
uint nKVertexes = 0;
for( const GeoGridZone zone : zones )
nKVertexes += zone.nHorizonSlices + 1;
uint nVertexes = nIVertexes * nJVertexes * nKVertexes;
//allocate the vertex list
m_vertexesPart.reserve( nVertexes );
//traverse the collection of zones from bottommost to topmost (reverse of user-entered order)
for ( std::vector<GeoGridZone>::const_reverse_iterator iZone = zones.crbegin(); iZone != zones.crend(); ++iZone ) {
const GeoGridZone& zone = *(iZone);
//get the indexes of the properties holding the top and base values.
uint columnIndexBase = zone.base->getAttributeGEOEASgivenIndex()-1;
uint columnIndexTop = zone.top->getAttributeGEOEASgivenIndex()-1;
uint nKVertexesZone = zone.nHorizonSlices + 1;
//create the vertexes
for( uint k = 0; k < nKVertexesZone; ++k ){
for( uint j = 0; j < nJVertexes; ++j ){
for( uint i = 0; i < nIVertexes; ++i ){
//get base and top values
double vBase = cg->dataIJK( columnIndexBase, i, j, 0 );
double vTop = cg->dataIJK( columnIndexTop, i, j, 0 );
//compute the depth (z) of the current vertex.
double depth = vBase + ( k / (double)(nKVertexesZone-1) ) * ( vTop - vBase );
//create and set the position of the vertex
VertexRecordPtr vertex( new VertexRecord() );
double x, y, z;
cg->IJKtoXYZ( i, j, 0, x, y, z );
vertex->X = x;
vertex->Y = y;
vertex->Z = depth;
m_vertexesPart.push_back( vertex );
}
}
}
}
//define the number of cells (cell centered values, one less than the number of vertexes in each direction )
uint nICells = cg->getNI()-1;
uint nJCells = cg->getNJ()-1;
uint nKCells = 0;
for( const GeoGridZone zone : zones )
nKCells += zone.nHorizonSlices;
uint nCells = nICells * nJCells * nKCells;
//allocate the cell definition list
m_cellDefsPart.reserve( nCells );
//assign the vertexes of each cell, thus defining them
uint vertexKoffset = 0;
for ( std::vector<GeoGridZone>::const_reverse_iterator iZone = zones.crbegin();
iZone != zones.crend();
++iZone ) { //traversing from base->top (inverse of user-entered order)
const GeoGridZone& zone = *(iZone);
uint nKCellsZone = zone.nHorizonSlices;
//assign vertexes id's to the cells
for( uint k = 0; k < nKCellsZone; ++k ){
for( uint j = 0; j < nJCells; ++j ){
for( uint i = 0; i < nICells; ++i ){
CellDefRecordPtr cellDef( new CellDefRecord() );
//see Doxygen of CellDefRecordPtr in geogrid.h for a diagram of vertex arrangement in space
// and how they form the edges and faces of the visual representation of the cell.
cellDef->vId[0] = ( vertexKoffset + k + 0 ) * nJVertexes * nIVertexes + ( j + 0 ) * nIVertexes + ( i + 0 );
cellDef->vId[1] = ( vertexKoffset + k + 0 ) * nJVertexes * nIVertexes + ( j + 0 ) * nIVertexes + ( i + 1 );
cellDef->vId[2] = ( vertexKoffset + k + 0 ) * nJVertexes * nIVertexes + ( j + 1 ) * nIVertexes + ( i + 1 );
cellDef->vId[3] = ( vertexKoffset + k + 0 ) * nJVertexes * nIVertexes + ( j + 1 ) * nIVertexes + ( i + 0 );
cellDef->vId[4] = ( vertexKoffset + k + 1 ) * nJVertexes * nIVertexes + ( j + 0 ) * nIVertexes + ( i + 0 );
cellDef->vId[5] = ( vertexKoffset + k + 1 ) * nJVertexes * nIVertexes + ( j + 0 ) * nIVertexes + ( i + 1 );
cellDef->vId[6] = ( vertexKoffset + k + 1 ) * nJVertexes * nIVertexes + ( j + 1 ) * nIVertexes + ( i + 1 );
cellDef->vId[7] = ( vertexKoffset + k + 1 ) * nJVertexes * nIVertexes + ( j + 1 ) * nIVertexes + ( i + 0 );
m_cellDefsPart.push_back( cellDef );
}
}
}
vertexKoffset += nKCellsZone+1;
}
//initialize member variables
m_nI = nICells;
m_nJ = nJCells;
m_nK = nKCells;
m_nreal = 1;
_no_data_value = "";
}
void GeoGrid::getCellBoundingBox(uint cellIndex,
double & minX, double & minY, double & minZ,
double & maxX, double & maxY, double & maxZ ) const
{
//initialize the results to ensure the returned extrema are those of the cell.
minX = minY = minZ = std::numeric_limits<double>::max();
maxX = maxY = maxZ = -std::numeric_limits<double>::max();
//Get the cell
CellDefRecordPtr cellDef = m_cellDefsPart.at( cellIndex );
//for each of the eight vertexes of the cell
for( uint i = 0; i < 8; ++i ){
//Get the vertex
VertexRecordPtr vertex = m_vertexesPart.at( cellDef->vId[i] );
//set the max's and min's
minX = std::min( minX, vertex->X );
minY = std::min( minY, vertex->Y );
minZ = std::min( minZ, vertex->Z );
maxX = std::max( maxX, vertex->X );
maxY = std::max( maxY, vertex->Y );
maxZ = std::max( maxZ, vertex->Z );
}
}
QString GeoGrid::getMeshFilePath()
{
QString mesh_file_path( this->_path );
return mesh_file_path.append(".mesh");
}
void GeoGrid::saveMesh()
{
if( m_vertexesPart.empty() || m_vertexesPart.empty() ){
Application::instance()->logInfo("GeoGrid::saveMesh(): No mesh or mesh not loaded. Nothing done.");
return;
}
//close mesh file and write header
QFile file( this->getMeshFilePath() );
file.open( QFile::WriteOnly | QFile::Text );
QTextStream out(&file);
out << APP_NAME << " GeoGrid mesh file. This file is generated automatically. Do not edit this file.\n";
out << "version=" << APP_VERSION << '\n';
out << "VERTEX LOCATIONS:\n";
{
std::vector< VertexRecordPtr >::iterator it = m_vertexesPart.begin();
for( ; it != m_vertexesPart.end(); ++it ){
//making sure the values are written in GSLib-like precision
std::stringstream ss;
ss << std::setprecision( 12 ) << (*it)->X << ';';
ss << std::setprecision( 12 ) << (*it)->Y << ';';
ss << std::setprecision( 12 ) << (*it)->Z ;
out << ss.str().c_str() << '\n';
}
}
out << "CELL VERTEX INDEXES:\n";
{
std::vector< CellDefRecordPtr >::iterator it = m_cellDefsPart.begin();
for( ; it != m_cellDefsPart.end(); ++it ){
std::stringstream ss;
for( int i = 0; i < 7; ++i)
ss << (*it)->vId[i] << ';';
ss << (*it)->vId[7];
out << ss.str().c_str() << "\n";
}
}
//close mesh file
file.close();
Application::instance()->logInfo("GeoGrid::saveMesh(): Mesh saved.");
}
void GeoGrid::loadMesh()
{
QFile file( this->getMeshFilePath() );
file.open(QFile::ReadOnly | QFile::Text);
uint data_line_count = 0;
QFileInfo info( this->getMeshFilePath() );
// if loaded mesh is not empty and was loaded before
if ( (!m_cellDefsPart.empty() || !m_vertexesPart.empty() ) && !m_lastModifiedDateTimeLastMeshLoad.isNull()) {
QDateTime currentLastModified = info.lastModified();
// if modified datetime didn't change since last call to loadMesh
if (currentLastModified <= m_lastModifiedDateTimeLastMeshLoad) {
Application::instance()->logInfo(
QString("Mesh file ")
.append( this->getMeshFilePath() )
.append(" already loaded and up to date. Did nothing."));
return; // does nothing
}
}
// record the current datetime of file change
m_lastModifiedDateTimeLastMeshLoad = info.lastModified();
Application::instance()->logInfo(
QString("Loading mesh from ").append( this->getMeshFilePath() ).append("..."));
// make sure mesh data is empty
m_cellDefsPart.clear();
m_vertexesPart.clear();
std::vector< CellDefRecordPtr >().swap( m_cellDefsPart ); //clear() may not actually free memory
std::vector< VertexRecordPtr >().swap( m_vertexesPart ); //clear() may not actually free memory
// mesh load takes place in another thread, so we can show and update a progress bar
//////////////////////////////////
QProgressDialog progressDialog;
progressDialog.show();
progressDialog.setLabelText("Loading and parsing mesh file " + this->getMeshFilePath() + "...");
progressDialog.setMinimum(0);
progressDialog.setValue(0);
progressDialog.setMaximum(getFileSize() / 100); // see MeshLoader::doLoad(). Dividing
// by 100 allows a max value of ~400GB
// when converting from long to int
QThread *thread = new QThread(); // does it need to set parent (a QObject)?
MeshLoader *ml = new MeshLoader(file, m_vertexesPart, m_cellDefsPart, data_line_count ); // Do not set a parent. The object
// cannot be moved if it has a
// parent.
ml->moveToThread(thread);
ml->connect(thread, SIGNAL(finished()), ml, SLOT(deleteLater()));
ml->connect(thread, SIGNAL(started()), ml, SLOT(doLoad()));
ml->connect(ml, SIGNAL(progress(int)), &progressDialog, SLOT(setValue(int)));
thread->start();
/////////////////////////////////
// wait for the mesh load to finish
// not very beautiful, but simple and effective
while (!ml->isFinished()) {
thread->wait(200); // reduces cpu usage, refreshes at each 500 milliseconds
QCoreApplication::processEvents(); // let Qt repaint widgets
}
file.close();
Application::instance()->logInfo("Finished loading mesh.");
}
void GeoGrid::setInfoFromMetadataFile()
{
QString md_file_path( this->_path );
QFile md_file( md_file_path.append(".md") );
uint nI = 0, nJ = 0, nK = 0;
uint nreal = 0;
QString ndv;
QMap<uint, QPair<uint,QString> > nsvar_var_trn;
QList< QPair<uint,QString> > categorical_attributes;
if( md_file.exists() ){
md_file.open( QFile::ReadOnly | QFile::Text );
QTextStream in(&md_file);
for (int i = 0; !in.atEnd(); ++i)
{
QString line = in.readLine();
if( line.startsWith( "NI:" ) ){
QString value = line.split(":")[1];
nI = value.toInt();
}else if( line.startsWith( "NJ:" ) ){
QString value = line.split(":")[1];
nJ = value.toInt();
}else if( line.startsWith( "NK:" ) ){
QString value = line.split(":")[1];
nK = value.toInt();
}else if( line.startsWith( "NREAL:" ) ){
QString value = line.split(":")[1];
nreal = value.toInt();
}else if( line.startsWith( "NDV:" ) ){
QString value = line.split(":")[1];
ndv = value;
}else if( line.startsWith( "NSCORE:" ) ){
QString triad = line.split(":")[1];
QString var = triad.split(">")[0];
QString pair = triad.split(">")[1];
QString ns_var = pair.split("=")[0];
QString trn_filename = pair.split("=")[1];
//normal variable index is key
//variable index and transform table filename are the value
nsvar_var_trn.insert( ns_var.toUInt(), QPair<uint,QString>(var.toUInt(), trn_filename ));
}else if( line.startsWith( "CATEGORICAL:" ) ){
QString var_and_catDefName = line.split(":")[1];
QString var = var_and_catDefName.split(",")[0];
QString catDefName = var_and_catDefName.split(",")[1];
categorical_attributes.append( QPair<uint,QString>(var.toUInt(), catDefName) );
}
}
md_file.close();
this->setInfo( nI, nJ, nK, nreal, ndv, nsvar_var_trn, categorical_attributes);
}
}
void GeoGrid::setInfo(int nI, int nJ, int nK, int nreal, const QString no_data_value,
QMap<uint, QPair<uint, QString> > nvar_var_trn_triads,
const QList<QPair<uint, QString> > & categorical_attributes)
{
//updating metadata
this->_no_data_value = no_data_value;
this->m_nreal = nreal;
this->m_nI = nI;
this->m_nJ = nJ;
this->m_nK = nK;
_nsvar_var_trn.clear();
_nsvar_var_trn.unite( nvar_var_trn_triads );
_categorical_attributes.clear();
_categorical_attributes << categorical_attributes;
//update the attribute fields
this->updateChildObjectsCollection();
}
uint GeoGrid::getMeshNumberOfVertexes()
{
this->loadMesh();
return m_vertexesPart.size();
}
void GeoGrid::getMeshVertexLocation(uint index, double & x, double & y, double & z) const
{
x = m_vertexesPart[index]->X;
y = m_vertexesPart[index]->Y;
z = m_vertexesPart[index]->Z;
}
uint GeoGrid::getMeshNumberOfCells()
{
this->loadMesh();
return m_cellDefsPart.size();
}
void GeoGrid::getMeshCellDefinition(uint index, uint (&vIds)[8]) const
{
vIds[0] = m_cellDefsPart[index]->vId[0];
vIds[1] = m_cellDefsPart[index]->vId[1];
vIds[2] = m_cellDefsPart[index]->vId[2];
vIds[3] = m_cellDefsPart[index]->vId[3];
vIds[4] = m_cellDefsPart[index]->vId[4];
vIds[5] = m_cellDefsPart[index]->vId[5];
vIds[6] = m_cellDefsPart[index]->vId[6];
vIds[7] = m_cellDefsPart[index]->vId[7];
}
PointSet *GeoGrid::unfold( PointSet *inputPS, QString nameForNewPointSet )
{
if( ! inputPS->is3D() ){
Application::instance()->logError("GeoGrid::unfold(): input point set is not 3D.");
return nullptr;
}
//create the new point set object
PointSet* result = new PointSet( Application::instance()->getProject()->getPath() +
'/' + nameForNewPointSet );
//make a duplicate of the input point set
{
//copy the physical data file
Util::copyFile( inputPS->getPath(), result->getPath() );
//copy metadata from the input point set
result->setInfoFromOtherPointSet( inputPS );
}
//load the data
result->loadData();
//get the number of samples in the input point set
uint nSamples = result->getDataLineCount();
//append three new columns to the samples (output): the UVW coordinates
result->addEmptyDataColumn( "U", nSamples );
result->addEmptyDataColumn( "V", nSamples );
result->addEmptyDataColumn( "W", nSamples );
result->writeToFS();
uint nColumns = result->getDataColumnCount();
//iterate over the samples in the point set
uint xIndex = result->getXindex() - 1; //first GEO-EAS index = 1
uint yIndex = result->getYindex() - 1;
uint zIndex = result->getZindex() - 1;
std::vector<uint> samplesToRemove;
bool empty = true;
for( uint iSample = 0; iSample < nSamples; ++iSample ){
//get the XYZ location of the sample
double x = result->data( iSample, xIndex );
double y = result->data( iSample, yIndex );
double z = result->data( iSample, zIndex );
//get the UVW coordinates
double u = -1.0;
double v = -1.0;
double w = -1.0;
if( XYZtoUVW( x, y, z, u, v, w ) ){
empty = false;
//assign them to the point set
result->setData( iSample, nColumns - 3, u );
result->setData( iSample, nColumns - 2, v );
result->setData( iSample, nColumns - 1, w );
} else {
//a cell was not found (likely the sample is outside the grid)
//so mark the sample for removal
samplesToRemove.push_back( iSample );
}
}
//changed the assigned X, Y, Z fields of the unfolded point set to the new U, V, W columns
result->setInfo( nColumns - 2, nColumns - 1, nColumns, result->getNoDataValue(),
result->getWeightsVariablesPairs(), result->getNSVarVarTrnTriads(), result->getCategoricalAttributes() );
//remove the samples with invalid UVW coordinates
std::vector<uint>::iterator it = samplesToRemove.begin();
uint offset = 0; //adjust for previously deleted lines.
for( ; it != samplesToRemove.end(); ++it, ++offset )
result->removeDataLine( *it - offset );
//if no data remained
if( empty ){
//remove the file with partial data
QFile resultFile( result->getPath() );
resultFile.remove();
//de-allocate the object
delete result;
Application::instance()->logError("GeoGrid::unfold(): Unfolding resulted in an empty point set. Canceled.");
//return null pointer
return nullptr;
}
//commit changes to filesystem
result->writeToFS();
return result;
}
SegmentSet *GeoGrid::unfold(SegmentSet *inputSS, QString nameForNewSegmentSet)
{
if( ! inputSS->is3D() ){
Application::instance()->logError("GeoGrid::unfold(): input segment set is not 3D.");
return nullptr;
}
//create the new segment set object
SegmentSet* result = new SegmentSet( Application::instance()->getProject()->getPath() +
'/' + nameForNewSegmentSet );
//make a duplicate of the input segment set
{
//copy the physical data file
Util::copyFile( inputSS->getPath(), result->getPath() );
//copy metadata from the input point set
result->setInfoFromAnotherSegmentSet( inputSS );
}
//load the data
result->loadData();
//get the number of samples in the input point set
uint nSamples = result->getDataLineCount();
//append six new columns to the samples (output):
//the initial and final UVW segment coordinates
result->addEmptyDataColumn( "U_i", nSamples );
result->addEmptyDataColumn( "V_i", nSamples );
result->addEmptyDataColumn( "W_i", nSamples );
result->addEmptyDataColumn( "U_f", nSamples );
result->addEmptyDataColumn( "V_f", nSamples );
result->addEmptyDataColumn( "W_f", nSamples );
result->writeToFS();
uint nColumns = result->getDataColumnCount();
//iterate over the samples in the point set
uint xIIndex = result->getXindex() - 1; //first GEO-EAS index = 1
uint yIIndex = result->getYindex() - 1;
uint zIIndex = result->getZindex() - 1;
uint xFIndex = result->getXFinalIndex() - 1;
uint yFIndex = result->getYFinalIndex() - 1;
uint zFIndex = result->getZFinalIndex() - 1;
std::vector<uint> samplesToRemove;
bool empty = true;
for( uint iSample = 0; iSample < nSamples; ++iSample ){
//get the XYZ locations of the sample
double xi = result->data( iSample, xIIndex );
double yi = result->data( iSample, yIIndex );
double zi = result->data( iSample, zIIndex );
double xf = result->data( iSample, xFIndex );
double yf = result->data( iSample, yFIndex );
double zf = result->data( iSample, zFIndex );
//get the UVW coordinates
double ui = -1.0;
double vi = -1.0;
double wi = -1.0;
double uf = -1.0;
double vf = -1.0;
double wf = -1.0;
if( XYZtoUVW( xi, yi, zi, ui, vi, wi ) &&
XYZtoUVW( xf, yf, zf, uf, vf, wf ) ){
empty = false;
//assign them to the point set
result->setData( iSample, nColumns - 6, ui );
result->setData( iSample, nColumns - 5, vi );
result->setData( iSample, nColumns - 4, wi );
result->setData( iSample, nColumns - 3, uf );
result->setData( iSample, nColumns - 2, vf );
result->setData( iSample, nColumns - 1, wf );
} else {
//a cell was not found (likely one or both ends of a sample is outside the grid)
//so mark the sample for removal
samplesToRemove.push_back( iSample );
}
}
//changed the assigned X, Y, Z fields of the unfolded point set to the new U, V, W columns
result->setInfo( nColumns - 5, nColumns - 4, nColumns - 3,
nColumns - 2, nColumns - 1, nColumns , result->getNoDataValue(),
result->getWeightsVariablesPairs(), result->getNSVarVarTrnTriads(), result->getCategoricalAttributes() );
//remove the samples with invalid UVW coordinates
std::vector<uint>::iterator it = samplesToRemove.begin();
uint offset = 0; //adjust for previously deleted lines.
for( ; it != samplesToRemove.end(); ++it, ++offset )
result->removeDataLine( *it - offset );
//if no data remained
if( empty ){
//remove the file with partial data
QFile resultFile( result->getPath() );
resultFile.remove();
//de-allocate the object
delete result;
Application::instance()->logError("GeoGrid::unfold(): Unfolding resulted in an empty segment set. Canceled.");
//return null pointer
return nullptr;
}
//commit changes to filesystem
result->writeToFS();
return result;
}
bool GeoGrid::XYZtoUVW(double x, double y, double z, double &u, double &v, double &w)
{
// https://math.stackexchange.com/questions/13404/mapping-irregular-quadrilateral-to-a-rectangle
//Obtain the topological coordinates (IJK) of the cell that contains the location.
uint i, j, k;
if( ! XYZtoIJK( x, y, z, i, j, k ) )
return false;
//Obtain the index of the cell.
uint cellIndex = IJKtoIndex( i, j, k );
//Make a point object corresponding to the query location.
Vertex3D location{ x, y, z };
//Get the faces' geometries of the cell.
std::vector<Face3D> cellFaces = getFaces( cellIndex );
//faces 2 and 3 are along I direction, thus are respect to U depositional coordinate
//faces 4 and 5 are along J direction, thus are respect to V depositional coordinate
//faces 0 and 1 are along K direction, thus are respect to W depositional coordinate
//compute the six distances between the location and the faces
double dU0 = cellFaces[2].distance( location );
double dU1 = cellFaces[3].distance( location );
double dV0 = cellFaces[4].distance( location );
double dV1 = cellFaces[5].distance( location );
double dW0 = cellFaces[0].distance( location );
double dW1 = cellFaces[1].distance( location );
//compute the UVW within the cell (min = 0.0, max = 1.0)
double local_u = dU0 / (dU0 + dU1);
double local_v = dV0 / (dV0 + dV1);
double local_w = dW0 / (dW0 + dW1);
//compute the global UVW steps.
double du = 1.0 / m_nI;
double dv = 1.0 / m_nJ;
double dw = 1.0 / m_nK;
//compute the UVW coordinates.
u = du * i + du * local_u;
v = dv * j + dv * local_v;
w = dw * k + dw * local_w;
//enforcing sanity
if( std::isfinite( u ) && std::isfinite( v ) && std::isfinite( w ) )
return true;
else
return false;
}
std::vector<Face3D> GeoGrid::getFaces( uint cellIndex )
{
//get the cell geometry definition (vertexes' indexes).
CellDefRecordPtr cellDef = m_cellDefsPart.at( cellIndex );
//get the vertex data of the cell
VertexRecordPtr vd[8];
vd[0] = m_vertexesPart.at( cellDef->vId[0] );
vd[1] = m_vertexesPart.at( cellDef->vId[1] );
vd[2] = m_vertexesPart.at( cellDef->vId[2] );
vd[3] = m_vertexesPart.at( cellDef->vId[3] );
vd[4] = m_vertexesPart.at( cellDef->vId[4] );
vd[5] = m_vertexesPart.at( cellDef->vId[5] );
vd[6] = m_vertexesPart.at( cellDef->vId[6] );
vd[7] = m_vertexesPart.at( cellDef->vId[7] );
//------------make the six face geometries------------
std::vector<Face3D> fs( 6 );
fs[0].v[0] = Vertex3D{ vd[0]->X, vd[0]->Y, vd[0]->Z };
fs[0].v[1] = Vertex3D{ vd[1]->X, vd[1]->Y, vd[1]->Z };
fs[0].v[2] = Vertex3D{ vd[2]->X, vd[2]->Y, vd[2]->Z };
fs[0].v[3] = Vertex3D{ vd[3]->X, vd[3]->Y, vd[3]->Z };
fs[1].v[0] = Vertex3D{ vd[4]->X, vd[4]->Y, vd[4]->Z };
fs[1].v[1] = Vertex3D{ vd[7]->X, vd[7]->Y, vd[7]->Z };
fs[1].v[2] = Vertex3D{ vd[6]->X, vd[6]->Y, vd[6]->Z };
fs[1].v[3] = Vertex3D{ vd[5]->X, vd[5]->Y, vd[5]->Z };
fs[2].v[0] = Vertex3D{ vd[0]->X, vd[0]->Y, vd[0]->Z };
fs[2].v[1] = Vertex3D{ vd[3]->X, vd[3]->Y, vd[3]->Z };
fs[2].v[2] = Vertex3D{ vd[7]->X, vd[7]->Y, vd[7]->Z };
fs[2].v[3] = Vertex3D{ vd[4]->X, vd[4]->Y, vd[4]->Z };
fs[3].v[0] = Vertex3D{ vd[1]->X, vd[1]->Y, vd[1]->Z };
fs[3].v[1] = Vertex3D{ vd[5]->X, vd[5]->Y, vd[5]->Z };
fs[3].v[2] = Vertex3D{ vd[6]->X, vd[6]->Y, vd[6]->Z };
fs[3].v[3] = Vertex3D{ vd[2]->X, vd[2]->Y, vd[2]->Z };
fs[4].v[0] = Vertex3D{ vd[0]->X, vd[0]->Y, vd[0]->Z };
fs[4].v[1] = Vertex3D{ vd[4]->X, vd[4]->Y, vd[4]->Z };
fs[4].v[2] = Vertex3D{ vd[5]->X, vd[5]->Y, vd[5]->Z };
fs[4].v[3] = Vertex3D{ vd[1]->X, vd[1]->Y, vd[1]->Z };
fs[5].v[0] = Vertex3D{ vd[3]->X, vd[3]->Y, vd[3]->Z };
fs[5].v[1] = Vertex3D{ vd[2]->X, vd[2]->Y, vd[2]->Z };
fs[5].v[2] = Vertex3D{ vd[6]->X, vd[6]->Y, vd[6]->Z };
fs[5].v[3] = Vertex3D{ vd[7]->X, vd[7]->Y, vd[7]->Z };
//----------------------------------------------------
return fs;
}
std::vector<Face3D> GeoGrid::getFacesInvertedWinding(uint cellIndex)
{
//get the cell geometry definition (vertexes' indexes).
CellDefRecordPtr cellDef = m_cellDefsPart.at( cellIndex );
//get the vertex data of the cell
VertexRecordPtr vd[8];
vd[0] = m_vertexesPart.at( cellDef->vId[0] );
vd[1] = m_vertexesPart.at( cellDef->vId[1] );
vd[2] = m_vertexesPart.at( cellDef->vId[2] );
vd[3] = m_vertexesPart.at( cellDef->vId[3] );
vd[4] = m_vertexesPart.at( cellDef->vId[4] );
vd[5] = m_vertexesPart.at( cellDef->vId[5] );
vd[6] = m_vertexesPart.at( cellDef->vId[6] );
vd[7] = m_vertexesPart.at( cellDef->vId[7] );
//------------make the six face geometries------------
std::vector<Face3D> fs( 6 );
fs[0].v[0] = Vertex3D{ vd[3]->X, vd[3]->Y, vd[3]->Z };
fs[0].v[1] = Vertex3D{ vd[2]->X, vd[2]->Y, vd[2]->Z };
fs[0].v[2] = Vertex3D{ vd[1]->X, vd[1]->Y, vd[1]->Z };
fs[0].v[3] = Vertex3D{ vd[0]->X, vd[0]->Y, vd[0]->Z };
fs[1].v[0] = Vertex3D{ vd[5]->X, vd[5]->Y, vd[5]->Z };
fs[1].v[1] = Vertex3D{ vd[6]->X, vd[6]->Y, vd[6]->Z };
fs[1].v[2] = Vertex3D{ vd[7]->X, vd[7]->Y, vd[7]->Z };
fs[1].v[3] = Vertex3D{ vd[4]->X, vd[4]->Y, vd[4]->Z };
fs[2].v[0] = Vertex3D{ vd[4]->X, vd[4]->Y, vd[4]->Z };
fs[2].v[1] = Vertex3D{ vd[7]->X, vd[7]->Y, vd[7]->Z };
fs[2].v[2] = Vertex3D{ vd[3]->X, vd[3]->Y, vd[3]->Z };
fs[2].v[3] = Vertex3D{ vd[0]->X, vd[0]->Y, vd[0]->Z };
fs[3].v[0] = Vertex3D{ vd[2]->X, vd[2]->Y, vd[2]->Z };
fs[3].v[1] = Vertex3D{ vd[6]->X, vd[6]->Y, vd[6]->Z };
fs[3].v[2] = Vertex3D{ vd[5]->X, vd[5]->Y, vd[5]->Z };
fs[3].v[3] = Vertex3D{ vd[1]->X, vd[1]->Y, vd[1]->Z };
fs[4].v[0] = Vertex3D{ vd[1]->X, vd[1]->Y, vd[1]->Z };
fs[4].v[1] = Vertex3D{ vd[5]->X, vd[5]->Y, vd[5]->Z };
fs[4].v[2] = Vertex3D{ vd[4]->X, vd[4]->Y, vd[4]->Z };
fs[4].v[3] = Vertex3D{ vd[0]->X, vd[0]->Y, vd[0]->Z };
fs[5].v[0] = Vertex3D{ vd[7]->X, vd[7]->Y, vd[7]->Z };
fs[5].v[1] = Vertex3D{ vd[6]->X, vd[6]->Y, vd[6]->Z };
fs[5].v[2] = Vertex3D{ vd[2]->X, vd[2]->Y, vd[2]->Z };
fs[5].v[3] = Vertex3D{ vd[3]->X, vd[3]->Y, vd[3]->Z };
//----------------------------------------------------
return fs;
}
void GeoGrid::computeCellVolumes(QString variable_name)
{
//Get pointer to the Cartesian grid object used as data store
CartesianGrid* myCartesianGrid = getUnderlyingCartesianGrid();
//load the data
myCartesianGrid->loadData();
//appends a new variable
myCartesianGrid->addEmptyDataColumn( variable_name, myCartesianGrid->getDataLineCount() );
//get the current data column count
uint columnCount = myCartesianGrid->getDataColumnCount();
//load grid mesh
loadMesh();
//compute the volumes
for( uint k = 0; k < m_nK; ++k )
for( uint j = 0; j < m_nJ; ++j )
for( uint i = 0; i < m_nI; ++i ){
uint cellIndex = IJKtoIndex( i, j, k );
Hexahedron hexa = makeHexahedron( cellIndex );
double cellVolume = hexa.getVolume();
myCartesianGrid->setData( cellIndex, columnCount-1, cellVolume );
}
//commit results to file system
myCartesianGrid->writeToFS();
}
CartesianGrid *GeoGrid::getUnderlyingCartesianGrid()
{
for( int i = 0; i < getChildCount(); ++i ){
ProjectComponent* pc = getChildByIndex( i );
if( pc->getTypeName() == "CARTESIANGRID" )
return dynamic_cast<CartesianGrid*>( pc );
}
return nullptr;
}
Hexahedron GeoGrid::makeHexahedron( uint cellIndex ) const
{
Hexahedron hexa;
uint vertexIndexes[8];
getMeshCellDefinition( cellIndex, vertexIndexes );
double x, y, z;
for( uint i = 0; i < 8; ++i ){
getMeshVertexLocation( vertexIndexes[i], x, y, z );
hexa.v[i].x = x;
hexa.v[i].y = y;
hexa.v[i].z = z;
}
return hexa;
}
void GeoGrid::exportToEclipseGridGRDECL(const QString filePath, bool invertSignZ)
{
//TODO: implement a test for odd non-pillar-grid like geometry.
Application::instance()->logWarn("GeoGrid::exportToEclipseGridGRDECL(): assuming the GeoGrid "
"has a pillar-grid like geometry!");
//load grid data and geometry
loadData();
loadMesh();
//define Z sign inversion.
int signZ = 1;
if( invertSignZ )
signZ = -1;
//get some griding info.
const uint nI = getNI();
const uint nJ = getNJ();
const uint nK = getNK();
//show a progress dialog
QProgressDialog progressDialog;
progressDialog.show();
progressDialog.setLabelText("Exporting GeoGrid to Eclipse Grid ASCII format...");
progressDialog.setMinimum(0);
progressDialog.setValue(0);
progressDialog.setMaximum( 0 );
//open the file for output
QFile outputFile( filePath );
outputFile.open( QFile::WriteOnly | QFile::Text );
QTextStream out(&outputFile);
//set max number of significant digits to 12 is good enough for the Eclipse Grid ASCII format.
out.setRealNumberPrecision(12);
//the SPECGRID section declares grid cell counts along the three axes.
out << "SPECGRID" << '\n';
out << nI << ' ' << nJ << ' ' << nK << " 1 F\n";
out << "/\n\n";
//the COORD section declares the starting and ending (x,y,z)'s of the fibers delimiting
//where the cell stacks will be placed.
out << "COORD" << '\n';
uint cellVertexesIDs[8];
uint runLengthIndex;
double x, y, z;
for( uint j = 0; j < nJ; ++j ) {
for( uint i = 0; i < nI; ++i ) {
//output the southernmost, westernmost, bottommost vertex of the
//bottommost cell in the current volume trace.
runLengthIndex = IJKtoIndex( i, j, 0 );
getMeshCellDefinition( runLengthIndex, cellVertexesIDs );
getMeshVertexLocation( cellVertexesIDs[0], x, y, z );
out << x << ' ' << y << ' ' << signZ*z << " ";
//output the southernmost, westernmost, topmost vertex of the
//topmost cell in the current volume trace.
runLengthIndex = IJKtoIndex( i, j, nK-1 );
getMeshCellDefinition( runLengthIndex, cellVertexesIDs );
getMeshVertexLocation( cellVertexesIDs[4], x, y, z );
out << x << ' ' << y << ' ' << signZ*z << '\n';
//for the easternmost traces, we also...
if( i == nI-1 ){
//output the southernmost, easternmost, bottommost vertex of the
//bottommost cell in the current volume trace.
runLengthIndex = IJKtoIndex( i, j, 0 );
getMeshCellDefinition( runLengthIndex, cellVertexesIDs );
getMeshVertexLocation( cellVertexesIDs[1], x, y, z );
out << x << ' ' << y << ' ' << signZ*z << " ";
//output the southernmost, easternmost, topmost vertex of the
//topmost cell in the current volume trace.
runLengthIndex = IJKtoIndex( i, j, nK-1 );
getMeshCellDefinition( runLengthIndex, cellVertexesIDs );
getMeshVertexLocation( cellVertexesIDs[5], x, y, z );
out << x << ' ' << y << ' ' << signZ*z << '\n';
}
}
QApplication::processEvents(); //let Qt do repainting
}
//for the northernmost traces, we also...
for( uint i = 0; i < nI; ++i ){
//output the nothernmost, westernmost, bottommost vertex of the
//bottommost cell in the current volume trace.
runLengthIndex = IJKtoIndex( i, nJ-1, 0 );
getMeshCellDefinition( runLengthIndex, cellVertexesIDs );
getMeshVertexLocation( cellVertexesIDs[3], x, y, z );
out << x << ' ' << y << ' ' << signZ*z << " ";
//output the the northernmost, westernmost, topmost vertex of the
//topmost cell in the current volume trace.
runLengthIndex = IJKtoIndex( i, nJ-1, nK-1 );
getMeshCellDefinition( runLengthIndex, cellVertexesIDs );
getMeshVertexLocation( cellVertexesIDs[7], x, y, z );
out << x << ' ' << y << ' ' << signZ*z << '\n';
QApplication::processEvents(); //let Qt do repainting
}
//for the northernmost, easternmost trace of the entire grid, we also...
{
//output the nothernmost, easternmost, bottommost vertex of the
//bottommost cell in the current volume trace.
runLengthIndex = IJKtoIndex( nI-1, nJ-1, 0 );
getMeshCellDefinition( runLengthIndex, cellVertexesIDs );
getMeshVertexLocation( cellVertexesIDs[2], x, y, z );
out << x << ' ' << y << ' ' << signZ*z << " ";
//output the northernmost, easternmost, topmost vertex of the
//topmost cell in the current volume trace.
runLengthIndex = IJKtoIndex( nI-1, nJ-1, nK-1 );
getMeshCellDefinition( runLengthIndex, cellVertexesIDs );
getMeshVertexLocation( cellVertexesIDs[6], x, y, z );
out << x << ' ' << y << ' ' << signZ*z << '\n';
}
out << "/\n\n";
//the ZCORN section declares the eight Z depths (four for the bottom and four for the top) that define
// the geometry of one cell supported by four fibers. The fibers are defined in the COORD section.
// The Z axis is pointed downwards (left-hand rule) in the Eclipse standard, thus we need to scan the grid from
// top->bottom as the Z axis in GammaRay is pointed upwards (right-hand rule). Also, it is necessary to invert