/
mesh.cpp
2669 lines (2268 loc) · 93.2 KB
/
mesh.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
/******************************************************************************
* Copyright (C) 2006-2022 by the GIMLi development team *
* Carsten Rücker carsten@resistivity.net *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
******************************************************************************/
#include "mesh.h"
#include "kdtreeWrapper.h"
#include "line.h"
#include "memwatch.h"
#include "meshentities.h"
#include "node.h"
#include "plane.h"
#include "shape.h"
#include "sparsematrix.h"
#include "stopwatch.h"
#include <boost/bind.hpp>
#include <map>
namespace GIMLI{
std::ostream & operator << (std::ostream & str, const Mesh & mesh){
str << "\tNodes: " << mesh.nodeCount() << "\tCells: " << mesh.cellCount() << "\tBoundaries: " << mesh.boundaryCount();
return str;
}
Mesh::Mesh(Index dim, bool isGeometry)
: dimension_(dim),
rangesKnown_(false),
neighborsKnown_(false),
tree_(NULL),
staticGeometry_(true),
isGeometry_(isGeometry){
oldTet10NumberingStyle_ = true;
cellToBoundaryInterpolationCache_ = 0;
}
Mesh::Mesh(const std::string & filename, bool createNeighborInfos)
: rangesKnown_(false),
neighborsKnown_(false),
tree_(NULL),
staticGeometry_(true),
isGeometry_(false){
dimension_ = 3;
oldTet10NumberingStyle_ = true;
cellToBoundaryInterpolationCache_ = 0;
load(filename, createNeighborInfos);
}
Mesh::Mesh(const Mesh & mesh)
: rangesKnown_(false),
neighborsKnown_(false),
tree_(NULL),
staticGeometry_(true),
isGeometry_(false){
oldTet10NumberingStyle_ = true;
cellToBoundaryInterpolationCache_ = 0;
copy_(mesh);
}
Mesh & Mesh::operator = (const Mesh & mesh){
if (this != & mesh){
copy_(mesh);
} return *this;
}
void Mesh::copy_(const Mesh & mesh){
clear();
rangesKnown_ = false;
setStaticGeometry(mesh.staticGeometry());
dimension_ = mesh.dim();
nodeVector_.reserve(mesh.nodeCount());
secNodeVector_.reserve(mesh.secondaryNodeCount());
for (Index i = 0; i < mesh.nodeCount(); i ++){
this->createNode(mesh.node(i));
}
for (Index i = 0; i < mesh.secondaryNodeCount(); i ++){
this->createSecondaryNode(mesh.secondaryNode(i).pos());
}
boundaryVector_.reserve(mesh.boundaryCount());
for (Index i = 0; i < mesh.boundaryCount(); i ++){
this->createBoundary(mesh.boundary(i));
}
cellVector_.reserve(mesh.cellCount());
for (Index i = 0; i < mesh.cellCount(); i ++){
this->createCell(mesh.cell(i));
}
for (Index i = 0; i < mesh.regionMarkers().size(); i ++){
this->addRegionMarker(mesh.regionMarkers()[i]);
}
for (Index i = 0; i < mesh.holeMarker().size(); i ++){
this->addHoleMarker(mesh.holeMarker()[i]);
}
// we don't need expensive tests for copying
setGeometry(mesh.isGeometry());
setDataMap(mesh.dataMap());
setCellAttributes(mesh.cellAttributes());
if (mesh.neighborsKnown()){
this->createNeighborInfos(true);
}
// std::cout << "COPY mesh " << mesh.cell(0) << " " << cell(0) << std::endl;
}
Mesh::~Mesh(){
clear();
}
void Mesh::setStaticGeometry(bool stat){
staticGeometry_ = stat;
}
void Mesh::setGeometry(bool b) {
isGeometry_ = b;
}
void Mesh::clear(){
if (tree_) {
deletePtr()(tree_);
tree_ = nullptr;
}
for_each(cellVector_.begin(), cellVector_.end(), deletePtr());
cellVector_.clear();
for_each(boundaryVector_.begin(), boundaryVector_.end(), deletePtr());
boundaryVector_.clear();
for_each(nodeVector_.begin(), nodeVector_.end(), deletePtr());
nodeVector_.clear();
for_each(secNodeVector_.begin(), secNodeVector_.end(), deletePtr());
secNodeVector_.clear();
if (cellToBoundaryInterpolationCache_){
delete cellToBoundaryInterpolationCache_;
}
rangesKnown_ = false;
neighborsKnown_ = false;
}
Node * Mesh::createNode_(const RVector3 & pos, int marker){
rangesKnown_ = false;
Index id = nodeCount();
nodeVector_.push_back(new Node(pos));
nodeVector_.back()->setMarker(marker);
nodeVector_.back()->setId(id);
return nodeVector_.back();
}
Node * Mesh::createNodeGC_(const RVector3 & pos, int marker){
if (this->isGeometry_){
// __M
Index oldCount = this->nodeCount();
Node *n = this->createNodeWithCheck(pos);
n->setMarker(marker);
if ((this->nodeCount() == oldCount)){
if (n->state() == No) n->setState(Original);
}
if ((this->dim() == 3) and (this->nodeCount() > oldCount)){
for (auto *b: this->boundaryVector_){
// for (Index i = 0; i < this->boundaryVector_.size(); i ++ ){
// Boundary *b = this->boundaryVector_[i];
// __MS(b->rtti())
if (b->rtti() == MESH_POLYGON_FACE_RTTI){
// __MS(pos)
// __MS(b->center())
if (b->shape().touch(n->pos())){
// __MS(pos)
// __MS(b->node(0).pos() << " " << b->node(1).pos()
// << " "<< b->node(2).pos())
// __MS(*b)
dynamic_cast< PolygonFace* >(b)->insertNode(n);
}
} else {
// __MS(*b)
// log(Error, "Adding a node in a non Polygon Face is not supported.");
}
}
}
return n;
} else {
return this->createNode_(pos, marker);
}
}
Node * Mesh::createNode(const Node & node){
return createNodeGC_(node.pos(), node.marker());
}
Node * Mesh::createNode(double x, double y, double z, int marker){
return createNodeGC_(RVector3(x, y, z), marker);
}
Node * Mesh::createNode(const RVector3 & pos, int marker){
return createNodeGC_(pos, marker);
}
Node & Mesh::secondaryNode(Index i) {
ASSERT_RANGE(i, 0, this->secondaryNodeCount())
return *secNodeVector_[i];
}
Node & Mesh::secondaryNode(Index i) const {
ASSERT_RANGE(i, 0, this->secondaryNodeCount())
return *secNodeVector_[i];
}
Node * Mesh::createSecondaryNode_(const RVector3 & pos){
Index id = this->secondaryNodeCount();
secNodeVector_.push_back(new Node(pos));
secNodeVector_.back()->setId(this->nodeCount() + id);
return secNodeVector_.back();
}
Node * Mesh::createSecondaryNode(const RVector3 & pos, double tol){
bool useTree = false;
if (tol > 0.0){
fillKDTree_();
useTree = true;
Node * refNode = tree_->nearest(pos);
if (refNode){
if (pos.distance(refNode->pos()) < tol) {
return refNode;
}
}
}
Node *newNode = createSecondaryNode_(pos);
if (useTree) tree_->insert(newNode);
return newNode;
}
Node * Mesh::createNodeWithCheck(const RVector3 & pos, double tol, bool warn, bool edgeCheck){
bool useTree = false;
if (tol > -1.0){
fillKDTree_();
useTree = true;
Node * refNode = tree_->nearest(pos);
if (refNode){
if (pos.distance(refNode->pos()) < tol) {
if (warn) log(LogType::Warning,
"Duplicated node found for: " + str(pos));
return refNode;
}
}
// for (Index i = 0, imax = nodeVector_.size(); i < imax; i++){
// if (pos.distance(nodeVector_[i]->pos()) < 1e-6) return nodeVector_[i];
// }
}
Node * newNode = this->createNode_(pos, 0);
if (useTree) tree_->insert(newNode);
if (edgeCheck){
if (this->dim() != 2){
if (warn || debug()) log(LogType::Warning,
"edgeCheck is currently only supported for 2d meshes");
} else {
///// TODO refaktor in extra function
for (Index i = 0; i < this->boundaryVector_.size(); i ++ ){
Boundary *b = this->boundaryVector_[i];
if (b->rtti() == MESH_EDGE_RTTI){
int pIn;
Line(b->node(0).pos(), b->node(1).pos()).touch1(newNode->pos(), pIn);
if (pIn == 3){
Node *n1 = &b->node(0);
Node *n2 = &b->node(1);
// __MS(*n1)
// __MS(*n2)
// __MS(*newNode)
dynamic_cast< Edge * >(b)->setNodes(*n1, *newNode);
this->createEdge(*newNode, *n2, b->marker());
break;
}
} else {
log(LogType::Error,
"edge split is currently only supported for 2d edges");
}
}
}
}
return newNode;
}
Boundary * Mesh::createBoundary(const IndexArray & idx, int marker, bool check){
std::vector < Node * > nodes(idx.size());
for (Index i = 0; i < idx.size(); i ++ ) nodes[i] = &this->node(idx[i]);
if (isGeometry_){
return createPolygonFace(nodes, marker, check);
}
return createBoundary(nodes, marker, check);
}
Boundary * Mesh::createBoundary(std::vector < Node * > & nodes, int marker, bool check){
switch (nodes.size()){
case 1: return createBoundaryChecked_< NodeBoundary >(nodes, marker, check); break;
case 2: return createBoundaryChecked_< Edge >(nodes, marker, check); break;
case 3: {
if (dimension_ == 2)
return createBoundaryChecked_< Edge3 >(nodes, marker, check);
return createBoundaryChecked_< TriangleFace >(nodes, marker, check); } break;
case 4: return createBoundaryChecked_< QuadrangleFace >(nodes, marker, check); break;
case 6: return createBoundaryChecked_< Triangle6Face >(nodes, marker, check); break;
case 8: return createBoundaryChecked_< Quadrangle8Face >(nodes, marker, check); break;
default:
return createBoundaryChecked_< PolygonFace >(nodes, marker, check); break;
}
std::cout << WHERE_AM_I << "WHERE_AM_I << cannot determine boundary for nodes: " << nodes.size() << std::endl;
return NULL;
}
Boundary * Mesh::createBoundary(const Boundary & bound, bool check){
// only work for copy meshes where all nodes already copied
std::vector < Node * > nodes(bound.nodeCount());
for (Index i = 0; i < bound.nodeCount(); i ++) nodes[i] = &node(bound.node(i).id());
Boundary *b = 0;
if (bound.rtti() == MESH_POLYGON_FACE_RTTI){
const PolygonFace & f = dynamic_cast< const PolygonFace & >(bound);
b = createBoundaryChecked_< PolygonFace >(nodes, bound.marker(), check);
for (Index i = 0; i < f.subfaceCount(); i ++ ){
dynamic_cast< PolygonFace* >(b)->addSubface(
this->nodes(ids(f.subface(i))));
}
for (Index i = 0; i < f.holeMarkers().size(); i ++ ){
dynamic_cast< PolygonFace* >(b)->addHoleMarker(f.holeMarkers()[i]);
}
} else {
b = createBoundary(nodes, bound.marker(), check);
}
for (Index j = 0; j < bound.secondaryNodes().size(); j ++){
b->addSecondaryNode(& this->node(bound.secondaryNodes()[j]->id()));
}
return b;
}
Boundary * Mesh::createBoundary(const Cell & cell, bool check){
std::vector < Node * > nodes(cell.nodeCount());
for (Index i = 0; i < cell.nodeCount(); i ++) nodes[i] = &node(cell.node(i).id());
return createBoundary(nodes, cell.marker(), check);
}
Boundary * Mesh::createNodeBoundary(Node & n1, int marker, bool check){
std::vector < Node * > nodes(1); nodes[0] = & n1;
return createBoundaryChecked_< NodeBoundary >(nodes, marker, check);
}
Boundary * Mesh::createEdge(Node & n1, Node & n2, int marker, bool check){
std::vector < Node * > nodes(2); nodes[0] = & n1; nodes[1] = & n2;
return createBoundaryChecked_< Edge >(nodes, marker, check);
}
Boundary * Mesh::createEdge3(Node & n1, Node & n2, Node & n3, int marker, bool check){
std::vector < Node * > nodes(3); nodes[0] = & n1; nodes[1] = & n2; nodes[2] = & n3;
return createBoundaryChecked_< Edge3 >(nodes, marker, check);
}
Boundary * Mesh::createTriangleFace(Node & n1, Node & n2, Node & n3, int marker, bool check){
std::vector < Node * > nodes(3); nodes[0] = & n1; nodes[1] = & n2; nodes[2] = & n3;
return createBoundaryChecked_< TriangleFace >(nodes, marker, check);
}
Boundary * Mesh::createQuadrangleFace(Node & n1, Node & n2, Node & n3, Node & n4, int marker, bool check){
std::vector < Node * > nodes(4); nodes[0] = & n1; nodes[1] = & n2; nodes[2] = & n3, nodes[3] = & n4;
return createBoundaryChecked_< QuadrangleFace >(nodes, marker, check);
}
Boundary * Mesh::createPolygonFace(std::vector < Node * > & nodes, int marker, bool check){
return createBoundaryChecked_< PolygonFace >(nodes, marker, check);
}
Cell * Mesh::createCell(int marker){
std::vector < Node * > nodes(0);
return createCell_< Cell >(nodes, marker, cellCount());
}
Cell * Mesh::createCell(const IndexArray & idx, int marker){
std::vector < Node * > nodes(idx.size());
for (Index i = 0; i < idx.size(); i ++ ) nodes[i] = &this->node(idx[i]);
return createCell(nodes, marker);
}
Cell * Mesh::createCell(std::vector < Node * > & nodes, int marker){
switch (nodes.size()){
case 0: return createCell_< Cell >(nodes, marker, cellCount()); break;
case 2: return createCell_< EdgeCell >(nodes, marker, cellCount()); break;
case 3:
switch (dimension_){
case 1: return createCell_< Edge3Cell >(nodes, marker, cellCount()); break;
case 2: return createCell_< Triangle >(nodes, marker, cellCount()); break;
}
break;
case 4:
switch (dimension_){
case 2: return createCell_< Quadrangle >(nodes, marker, cellCount()); break;
case 3: return createCell_< Tetrahedron >(nodes, marker, cellCount()); break;
}
break;
case 5: return createCell_< Pyramid >(nodes, marker, cellCount()); break;
case 6:
switch (dimension_){
case 2: return createCell_< Triangle6 >(nodes, marker, cellCount()); break;
case 3: return createCell_< TriPrism >(nodes, marker, cellCount()); break;
}
break;
case 8:
switch (dimension_){
case 2: return createCell_< Quadrangle8 >(nodes, marker, cellCount()); break;
case 3: return createCell_< Hexahedron >(nodes, marker, cellCount()); break;
}
break;
case 10: return createCell_< Tetrahedron10 >(nodes, marker, cellCount()); break;
case 13: return createCell_< Pyramid13 >(nodes, marker, cellCount()); break;
case 15: return createCell_< TriPrism15 >(nodes, marker, cellCount()); break;
case 20: return createCell_< Hexahedron20 >(nodes, marker, cellCount()); break;
}
std::cout << WHERE_AM_I << "WHERE_AM_I << cannot determine cell for nodes: " << nodes.size() << " for dim: " << dimension_ << std::endl;
return NULL;
}
Cell * Mesh::createCell(const Cell & cell){
std::vector < Node * > nodes(cell.nodeCount());
for (Index i = 0; i < cell.nodeCount(); i ++) nodes[i] = &node(cell.node(i).id());
Cell *c = createCell(nodes, cell.marker());
for (Index j = 0; j < cell.secondaryNodes().size(); j ++){
c->addSecondaryNode(& this->node(cell.secondaryNodes()[j]->id()));
}
return c;
}
Cell * Mesh::createTriangle(Node & n1, Node & n2, Node & n3, int marker){
std::vector < Node * > nodes(3); nodes[0] = & n1; nodes[1] = & n2; nodes[2] = & n3;
return createCell_< Triangle >(nodes, marker, cellCount());
}
Cell * Mesh::createQuadrangle(Node & n1, Node & n2, Node & n3, Node & n4, int marker){
std::vector < Node * > nodes(4);
nodes[0] = & n1; nodes[1] = & n2; nodes[2] = & n3; nodes[3] = & n4;
return createCell_< Quadrangle >(nodes, marker, cellCount());
}
Cell * Mesh::createTetrahedron(Node & n1, Node & n2, Node & n3, Node & n4, int marker){
std::vector < Node * > nodes(4);
nodes[0] = & n1; nodes[1] = & n2; nodes[2] = & n3; nodes[3] = & n4;
return createCell_< Tetrahedron >(nodes, marker, cellCount());
}
Cell * Mesh::copyCell(const Cell & cell, double tol){
std::vector < Node * > nodes(cell.nodeCount());
for (Index i = 0; i < nodes.size(); i ++) {
nodes[i] = createNodeWithCheck(cell.node(i).pos(), tol);
nodes[i]->setMarker(cell.node(i).marker());
}
Cell * c = createCell(nodes);
c->setMarker(cell.marker());
c->setAttribute(cell.attribute());
return c;
}
Boundary * findSecParent(const std::vector < Node * > & v){
std::set < MeshEntity * > common;
for (auto *n: v){
common.insert(n->secondaryParent());
}
if (common.size() == 1) {
return dynamic_cast < Boundary * >(*common.begin());
}
return 0;
}
Boundary * Mesh::copyBoundary(const Boundary & bound, double tol, bool check){
bool debug = false;
#define _D(...) if (debug) __MSP(__VA_ARGS__)
_D("copyBoundary", bound.ids())
std::vector < Node * > nodes(bound.nodeCount());
bool isFreeFace = false; //** the new face is no subface
std::vector < Node * > oldNodes;
std::vector < Node * > conNodes;
std::vector < Node * > secNodes;
// __M
for (Index i = 0; i < nodes.size(); i ++) {
// this switch should not be necessary and need to be checked TODO
if (bound.rtti() == MESH_POLYGON_FACE_RTTI){
// this works with 3D poly tests but copy bounds into 2d mesh will double bounds
nodes[i] = createNode(bound.node(i).pos(), tol);
} else {
// 3D poly tests fail!! .. need to be checked and fixed TODO
if (check== true){
nodes[i] = createNodeWithCheck(bound.node(i).pos(), tol);
} else {
nodes[i] = createNode(bound.node(i).pos());
}
}
nodes[i]->setMarker(bound.node(i).marker());
// __MS(nodes[i]->state())
switch (nodes[i]->state()){
case NodeState::No:
// at least one node is not inside boundary
isFreeFace = true; break;
case NodeState::Secondary:
secNodes.push_back(nodes[i]); break;
// __MS(*nodes[i])
case NodeState::Connected:
conNodes.push_back(nodes[i]); break;
case NodeState::Original:
oldNodes.push_back(nodes[i]);
break;
}
}
Boundary * parent = 0;
Boundary * ret = 0;
std::vector < Node * > subNodes; // nodes for the new subface
if (bound.rtti() == MESH_POLYGON_FACE_RTTI && check == true){
_D("connectedNodes:", conNodes,
"secondaryNodes:", secNodes,
"origNodes:", oldNodes)
_D("new face nodes:", nodes, "is subface", not isFreeFace)
Boundary * secParent = findSecParent(secNodes);
//** identify if face is freeface
if (!isFreeFace){
std::set < Boundary * > conParentCand = findBoundaries(conNodes);
Boundary * conParent = 0;
if (conParentCand.size() > 0 ){
for (auto *b: conParentCand){
if (b->shape().plane().compare(bound.shape().plane(),
TOLERANCE, true)){
conParent = b;
break;
}
}
}
if (conNodes.size() && secNodes.size()){
if (!conParent || conParent != secParent){
isFreeFace = true;
}
if (conParent){
_D("conParent", conParent->ids())
} else {
_D("no parent for connected nodes")
}
if (secParent){
_D("secParent", secParent->ids())
} else {
_D("no parent for secondary nodes")
}
// subNodes = conNodes;
// subNodes.insert(subNodes.end(), secNodes.begin(), secNodes.end());
subNodes = nodes;
_D("subNodes", subNodes)
parent = secParent;
} else if (conNodes.size()){
if (!conParent){
isFreeFace = true;
_D("no conParent")
} else {
_D("conParent", conParent->ids())
if (oldNodes.size()){
//original nodes may not be part of connected Parent
for (auto *n: oldNodes){
if (!conParent->shape().touch(n->pos())) {
_D("node not on connected parent", n->id())
isFreeFace = true;
}
}
}
}
subNodes = nodes;
parent = conParent;
} else if (secNodes.size()){
if(!secParent){
isFreeFace = true;
_D("no secondary parent")
} else if (!secParent->shape().plane().compare(
bound.shape().plane(), TOLERANCE, true)){
_D("secParent", secParent->ids())
_D("secParent.Plane:", secParent->shape().plane());
_D("bound.Plane:", bound.shape().plane());
isFreeFace = true;
}
subNodes = secNodes;
parent = secParent;
} else if (oldNodes.size()){
isFreeFace = true;
}
}
//** create new face
_D("is subface", not isFreeFace, subNodes.size(), nodes.size())
if (isFreeFace){
ret = createBoundaryChecked_< PolygonFace >(nodes,
bound.marker(), check);
_D("added freeFace: ", ret->ids())
parent = ret;
} else {
if (subNodes.size() > 2){
if (parent){
for (auto *n: secNodes){
parent->delSecondaryNode(n);
n->setState(No);
}
auto *p = dynamic_cast< PolygonFace * >(parent);
_D("addSubface: ", p->subfaceCount())
p->addSubface(subNodes);
_D("subfacecount: ", p->subfaceCount())
if (debug) for (auto i: range(p->subfaceCount())){
_D("added subface:", p->subface(i))
}
} else {
log(Error, "no parent boundary");
}
ret = parent;
} else {
log(Error, "new subface but only two nodes");
}
}
const PolygonFace & f = dynamic_cast< const PolygonFace & >(bound);
if (f.subfaceCount() > 0){
log(Error, "Can't yet copy a boundary with subfaces");
}
for (Index i = 0; i < f.holeMarkers().size(); i ++ ){
dynamic_cast< PolygonFace* >(parent)->addHoleMarker(
f.holeMarkers()[i]);
}
} else { // if no Polygonface
ret = createBoundary(nodes, bound.marker(), check);
}
return ret;
}
Node & Mesh::node(Index i) {
if (i > nodeCount() - 1){
if (i < nodeCount() + secondaryNodeCount())
return this->secondaryNode(i - this->nodeCount());
std::cerr << WHERE_AM_I << " requested node: " << i << " does not exist." << std::endl;
} return *nodeVector_[i];
}
Node & Mesh::node(Index i) const {
if (i > nodeCount() - 1){
if (i < nodeCount() + secondaryNodeCount())
return this->secondaryNode(i - this->nodeCount());
std::cerr << WHERE_AM_I << " requested node: " << i << " does not exist." << std::endl;
} return *nodeVector_[i];
}
Cell & Mesh::cell(Index i) const {
if (i > cellCount() - 1){
std::cerr << WHERE_AM_I << " requested cell: " << i << " does not exist." << std::endl;
} return *cellVector_[i];
}
Cell & Mesh::cell(Index i) {
if (i > cellCount() - 1){
std::cerr << WHERE_AM_I << " requested cell: " << i << " does not exist." << std::endl;
} return *cellVector_[i];
}
Boundary & Mesh::boundary(Index i) const {
if (i > boundaryCount() - 1){
std::cerr << WHERE_AM_I << " requested boundary: " << i << " does not exist." << std::endl;
} return *boundaryVector_[i];
}
Boundary & Mesh::boundary(Index i) {
if (i > boundaryCount() - 1){
std::cerr << WHERE_AM_I << " requested boundary: " << i << " does not exist." << std::endl;
} return *boundaryVector_[i];
}
void Mesh::findRange_() const{
if (!rangesKnown_ || !staticGeometry_){
minRange_ = RVector3(MAX_DOUBLE, MAX_DOUBLE, MAX_DOUBLE);
maxRange_ = -minRange_;
for (Index i=0; i < nodeVector_.size(); i ++){
for (Index j=0; j < 3; j ++){
minRange_[j] = min(nodeVector_[i]->pos()[j], minRange_[j]);
maxRange_[j] = max(nodeVector_[i]->pos()[j], maxRange_[j]);
}
}
rangesKnown_ = true;
}
}
Mesh Mesh::createHull() const{
Mesh out(3);
out.createHull_(*this);
return out;
}
void Mesh::createHull_(const Mesh & mesh){
if (this->dim() == 3 && mesh.dim() == 2){
clear();
rangesKnown_ = false;
nodeVector_.reserve(mesh.nodeCount());
for (Index i = 0; i < mesh.nodeCount(); i ++) createNode(mesh.node(i));
boundaryVector_.reserve(mesh.cellCount());
for (Index i = 0; i < mesh.cellCount(); i ++) createBoundary(mesh.cell(i));
} else {
std::cerr << WHERE_AM_I << " increasing dimension fails, you should set the dimension for this mesh to 3" << std::endl;
}
}
Index Mesh::findNearestNode(const RVector3 & pos){
fillKDTree_();
return tree_->nearest(pos)->id();
}
IndexArray cellIDX__;
Cell * Mesh::findCellBySlopeSearch_(const RVector3 & pos, Cell * start,
size_t & count, bool useTagging) const {
Cell * cell = start;
Index cellCounter = 0; //** for avoiding infinite loop
do {
if (useTagging && cell->tagged()) {
cell = NULL;
} else {
cell->tag();
cellIDX__.push_back(cell->id());
RVector sf;
// std::cout << cellIDX__.size() << " testpos: " << pos << std::endl;
// std::cout << "cell: " << *cell << " touch: " << cell->shape().isInside(pos, true) << std::endl;
// for (Index i = 0; i < cell->nodeCount() ; i ++){
// std::cout << cell->node(i)<< std::endl;
// }
if (cell->shape().isInside(pos, sf, false)) {
return cell;
} else {
if (!neighborsKnown_){
const_cast<Mesh*>(this)->createNeighborInfosCell_(cell);
// for (Index j = 0; j < cell->neighborCellCount(); j++){
// cell->findNeighborCell(j);
// }
}
// for (Index i = 0; i < cell->neighborCellCount(); i ++ ){
// if (cell->neighborCell(i)){
// std::cout << "\t " << i << " " << *cell->neighborCell(i) << std::endl;
// } else {
// std::cout << "\t " << i << " " << 0 << std::endl;
// }
// }
cell = cell->neighborCell(sf);
// std::cout << "sf: " << sf << std::endl;
// std::cout << "neighCell " << cell << std::endl;
}
count++;
if (count == 50){
// std::cout << "testpos: " << pos << std::endl;
// std::cout << "cell: " << this->cell(cellIDX__.back()) << std::endl;
// for (Index i = 0;
// i < this->cell(cellIDX__.back()).nodeCount(); i ++){
// std::cout << this->cell(cellIDX__.back()).node(i)<< std::endl;
// }
if (debug()){
std::cout << WHERE_AM_I << " exit with submesh " << cellIDX__.size() << std::endl;
std::cout << "probably cant find a cell for " << pos << std::endl;
Mesh subMesh; subMesh.createMeshByCellIdx(*this, cellIDX__);
subMesh.exportVTK("submesh");
this->exportVTK("submeshParent");
}
return NULL;
}
}
} while (++cellCounter < cellCount() && cell);
return NULL;
}
Cell * Mesh::findCell(const RVector3 & pos, size_t & count,
bool extensive) const {
bool bruteForce = false;
Cell * cell = NULL;
if (bruteForce){
for (Index i = 0; i < this->cellCount(); i ++) {
count++;
if (cellVector_[i]->shape().isInside(pos)){
cell = cellVector_[i];
// std::cout << "testpos: " << pos << std::endl;
// std::cout << "cell: " << *cell<< std::endl;
break;
}
}
} else {
Stopwatch swatch(true);
cellIDX__.clear();
count = 0;
fillKDTree_();
Node * refNode = tree_->nearest(pos);
if (!refNode){
std::cout << "pos: " << pos << std::endl;
throwError(WHERE_AM_I +
" no nearest node to pos. This is a empty mesh");
}
if (refNode->cellSet().empty()){
std::cout << "Node: " << *refNode << std::endl;
throwError(WHERE_AM_I +
" no cells for this node. This is a corrupt mesh");
}
// std::cout << "Node: " << *refNode << std::endl;
// small fast precheck to avoid strange behaviour for symmetric SF.
for (std::set< Cell * >::iterator it = refNode->cellSet().begin();
it != refNode->cellSet().end(); it ++){
// std::cout << (*it)->id() << std::endl;
if ((*it)->shape().isInside(pos, false)) return *it;
}
cell = findCellBySlopeSearch_(pos, *refNode->cellSet().begin(),
count, false);
if (cell) return cell;
// exportVTK("slopesearch");
// exit(0);
if (extensive || 0){
// __M
// std::cout << "More expensive test here" << std::endl;
cellIDX__.clear();
std::for_each(cellVector_.begin(), cellVector_.end(), std::mem_fn(&Cell::untag));
//!** *sigh, no luck with simple kd-tree search, try more expensive full slope search
count = 0;
for (Index i = 0; i < this->cellCount(); i ++) {
cell = cellVector_[i];
cell = findCellBySlopeSearch_(pos, cell, count, true);
if (cell) {
break;
}
}
} else {
return NULL;
}
// std::cout << " n: " << count;
}
return cell;
}
std::vector < Cell * > Mesh::findCellsAlongRay(const RVector3 & start,
const RVector3 & dir,
PosVector & pos) const {
pos.clear();
Pos d(dir);
d.normalize();
std::vector < Cell * > cells;
RVector3 inPos(start);
if (!this->findCell(inPos, false)){
inPos = tree_->nearest(inPos)->pos();
}
pos.push_back(inPos);
double stepTol = 1e-5;
while (1){
Cell *c = this->findCell(inPos + dir*stepTol, false);
if (!c) break;
RVector3 outPos(false);
for (Index i = 0; i < c->boundaryCount(); i++){
Shape * s = c->boundary(i)->pShape();
if (s->intersectRay(inPos, dir, outPos)){
if (outPos != inPos){
outPos.setValid(true);
break;
} else {
outPos.setValid(false);
}
}
}
if (outPos.valid()){
pos.push_back(outPos);
cells.push_back(c);
inPos = outPos;
}
}
return cells;
}
std::vector < Boundary * > Mesh::findBoundaryByMarker(int marker) const {
return findBoundaryByMarker(marker, marker + 1);
}
std::vector < Boundary * > Mesh::findBoundaryByMarker(int from, int to) const {
// __MS(from)
std::vector < Boundary * > vBounds;
vBounds.reserve(boundaryCount());
for (std::vector< Boundary * >::const_iterator it = boundaryVector_.begin();
it != boundaryVector_.end(); it++){
if ((*it)->marker() >= from && (*it)->marker() < to) vBounds.push_back((*it));
}
return vBounds;
}
void Mesh::setBoundaryMarkers(const IVector & marker){
ASSERT_EQUAL(boundaryCount(), marker.size())
for (Index i = 0; i < boundaryVector_.size(); i ++){
boundaryVector_[i]->setMarker(marker[i]);
}
}
void Mesh::setBoundaryMarkers(const IndexArray & ids, int marker){
for (IndexArray::iterator it = ids.begin(); it != ids.end(); it++){
if (*it < boundaryCount()){
boundaryVector_[*it]->setMarker(marker);
}
}
}
std::vector < Cell * > Mesh::findCellByMarker(int from, int to) const {
if (to == -1) to = MAX_INT;
else if (to == 0) to = from + 1;
std::vector < Cell * > vCell;
vCell.reserve(cellCount());
for(std::vector< Cell * >::const_iterator it = cellVector_.begin();
it != cellVector_.end(); it++){
if ((*it)->marker() >= from && (*it)->marker() < to) vCell.push_back((*it));
}
return vCell;
}
std::vector < Cell * > Mesh::findCellByAttribute(double from, double to) const {
std::vector < Cell * > vCell;
vCell.reserve(cellCount());