/
NETGENPlugin_Mesher.cpp
4073 lines (3687 loc) · 144 KB
/
NETGENPlugin_Mesher.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) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
//
// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
//
// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// NETGENPlugin : C++ implementation
// File : NETGENPlugin_Mesher.cxx
// Author : Michael Sazonov (OCN)
// Date : 31/03/2006
// Project : SALOME
//=============================================================================
#include "NETGENPlugin_Mesher.hxx"
#include "NETGENPlugin_Hypothesis_2D.hxx"
#include "NETGENPlugin_SimpleHypothesis_3D.hxx"
#include <SMDS_FaceOfNodes.hxx>
#include <SMDS_MeshElement.hxx>
#include <SMDS_MeshNode.hxx>
#include <SMESHDS_Mesh.hxx>
#include <SMESH_Block.hxx>
#include <SMESH_Comment.hxx>
#include <SMESH_ComputeError.hxx>
#include <SMESH_File.hxx>
#include <SMESH_Mesh.hxx>
#include <SMESH_Gen.hxx>
#include <SMESH_MesherHelper.hxx>
#include <SMESH_subMesh.hxx>
#include <StdMeshers_QuadToTriaAdaptor.hxx>
#include <StdMeshers_ViscousLayers2D.hxx>
// #include <SALOMEDS_Tool.hxx>
#include <utilities.h>
#include <BRepBuilderAPI_Copy.hxx>
#include <BRep_Tool.hxx>
#include <Bnd_B3d.hxx>
#include <NCollection_Map.hxx>
#include <Standard_ErrorHandler.hxx>
#include <Standard_ProgramError.hxx>
#include <TColStd_MapOfInteger.hxx>
#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#include <TopTools_DataMapIteratorOfDataMapOfShapeInteger.hxx>
#include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
#include <TopTools_DataMapOfShapeInteger.hxx>
#include <TopTools_DataMapOfShapeShape.hxx>
#include <TopTools_MapOfShape.hxx>
#include <TopoDS.hxx>
#ifdef _MSC_VER
#pragma warning(disable : 4067)
#endif
namespace nglib {
#include <nglib.h>
}
#ifndef OCCGEOMETRY
#define OCCGEOMETRY
#endif
#include <occgeom.hpp>
#include <meshing.hpp>
//#include <ngexception.hpp>
namespace netgen {
#if NETGEN_VERSION > 5
DLL_HEADER extern int OCCGenerateMesh (OCCGeometry&, shared_ptr<Mesh>&, MeshingParameters&, int, int);
#elif NETGEN_VERSION == 5
DLL_HEADER extern int OCCGenerateMesh (OCCGeometry&, Mesh*&, MeshingParameters&, int, int);
#else
DLL_HEADER extern int OCCGenerateMesh (OCCGeometry&, Mesh*&, int, int, char*);
#endif
//extern void OCCSetLocalMeshSize(OCCGeometry & geom, Mesh & mesh);
DLL_HEADER extern MeshingParameters mparam;
DLL_HEADER extern volatile multithreadt multithread;
DLL_HEADER extern bool merge_solids;
}
#include <vector>
#include <limits>
#ifdef WIN32
#include <process.h>
#endif
using namespace nglib;
using namespace std;
#ifdef _DEBUG_
#define nodeVec_ACCESS(index) ((SMDS_MeshNode*) nodeVec.at((index)))
#else
#define nodeVec_ACCESS(index) ((SMDS_MeshNode*) nodeVec[index])
#endif
#define NGPOINT_COORDS(p) p(0),p(1),p(2)
#ifdef _DEBUG_
// dump elements added to ng mesh
//#define DUMP_SEGMENTS
//#define DUMP_TRIANGLES
//#define DUMP_TRIANGLES_SCRIPT "/tmp/trias.py" //!< debug AddIntVerticesInSolids()
#endif
TopTools_IndexedMapOfShape ShapesWithLocalSize;
std::map<int,double> VertexId2LocalSize;
std::map<int,double> EdgeId2LocalSize;
std::map<int,double> FaceId2LocalSize;
//=============================================================================
/*!
*
*/
//=============================================================================
NETGENPlugin_Mesher::NETGENPlugin_Mesher (SMESH_Mesh* mesh,
const TopoDS_Shape& aShape,
const bool isVolume)
: _mesh (mesh),
_shape (aShape),
_isVolume(isVolume),
_optimize(true),
_fineness(NETGENPlugin_Hypothesis::GetDefaultFineness()),
_isViscousLayers2D(false),
_ngMesh(NULL),
_occgeom(NULL),
_curShapeIndex(-1),
_progressTic(1),
_totalTime(1.0),
_simpleHyp(NULL),
_ptrToMe(NULL)
{
SetDefaultParameters();
ShapesWithLocalSize.Clear();
VertexId2LocalSize.clear();
EdgeId2LocalSize.clear();
FaceId2LocalSize.clear();
}
//================================================================================
/*!
* Destuctor
*/
//================================================================================
NETGENPlugin_Mesher::~NETGENPlugin_Mesher()
{
if ( _ptrToMe )
*_ptrToMe = NULL;
_ptrToMe = 0;
_ngMesh = NULL;
}
//================================================================================
/*!
* Set pointer to NETGENPlugin_Mesher* field of the holder, that will be
* nullified at destruction of this
*/
//================================================================================
void NETGENPlugin_Mesher::SetSelfPointer( NETGENPlugin_Mesher ** ptr )
{
if ( _ptrToMe )
*_ptrToMe = NULL;
_ptrToMe = ptr;
if ( _ptrToMe )
*_ptrToMe = this;
}
//================================================================================
/*!
* \brief Initialize global NETGEN parameters with default values
*/
//================================================================================
void NETGENPlugin_Mesher::SetDefaultParameters()
{
netgen::MeshingParameters& mparams = netgen::mparam;
// maximal mesh edge size
mparams.maxh = 0;//NETGENPlugin_Hypothesis::GetDefaultMaxSize();
mparams.minh = 0;
// minimal number of segments per edge
mparams.segmentsperedge = NETGENPlugin_Hypothesis::GetDefaultNbSegPerEdge();
// rate of growth of size between elements
mparams.grading = NETGENPlugin_Hypothesis::GetDefaultGrowthRate();
// safety factor for curvatures (elements per radius)
mparams.curvaturesafety = NETGENPlugin_Hypothesis::GetDefaultNbSegPerRadius();
// create elements of second order
mparams.secondorder = NETGENPlugin_Hypothesis::GetDefaultSecondOrder();
// quad-dominated surface meshing
if (_isVolume)
mparams.quad = 0;
else
mparams.quad = NETGENPlugin_Hypothesis_2D::GetDefaultQuadAllowed();
_fineness = NETGENPlugin_Hypothesis::GetDefaultFineness();
mparams.uselocalh = NETGENPlugin_Hypothesis::GetDefaultSurfaceCurvature();
netgen::merge_solids = NETGENPlugin_Hypothesis::GetDefaultFuseEdges();
}
//=============================================================================
/*!
*
*/
//=============================================================================
void SetLocalSize(TopoDS_Shape GeomShape, double LocalSize)
{
if ( GeomShape.IsNull() ) return;
TopAbs_ShapeEnum GeomType = GeomShape.ShapeType();
if (GeomType == TopAbs_COMPOUND) {
for (TopoDS_Iterator it (GeomShape); it.More(); it.Next()) {
SetLocalSize(it.Value(), LocalSize);
}
return;
}
int key;
if (! ShapesWithLocalSize.Contains(GeomShape))
key = ShapesWithLocalSize.Add(GeomShape);
else
key = ShapesWithLocalSize.FindIndex(GeomShape);
if (GeomType == TopAbs_VERTEX) {
VertexId2LocalSize[key] = LocalSize;
} else if (GeomType == TopAbs_EDGE) {
EdgeId2LocalSize[key] = LocalSize;
} else if (GeomType == TopAbs_FACE) {
FaceId2LocalSize[key] = LocalSize;
}
}
//=============================================================================
/*!
* Pass parameters to NETGEN
*/
//=============================================================================
void NETGENPlugin_Mesher::SetParameters(const NETGENPlugin_Hypothesis* hyp)
{
if (hyp)
{
netgen::MeshingParameters& mparams = netgen::mparam;
// Initialize global NETGEN parameters:
// maximal mesh segment size
mparams.maxh = hyp->GetMaxSize();
// maximal mesh element linear size
mparams.minh = hyp->GetMinSize();
// minimal number of segments per edge
mparams.segmentsperedge = hyp->GetNbSegPerEdge();
// rate of growth of size between elements
mparams.grading = hyp->GetGrowthRate();
// safety factor for curvatures (elements per radius)
mparams.curvaturesafety = hyp->GetNbSegPerRadius();
// create elements of second order
mparams.secondorder = hyp->GetSecondOrder() ? 1 : 0;
// quad-dominated surface meshing
// only triangles are allowed for volumic mesh (before realizing IMP 0021676)
//if (!_isVolume)
mparams.quad = hyp->GetQuadAllowed() ? 1 : 0;
_optimize = hyp->GetOptimize();
_fineness = hyp->GetFineness();
mparams.uselocalh = hyp->GetSurfaceCurvature();
netgen::merge_solids = hyp->GetFuseEdges();
_simpleHyp = NULL;
/* vejmarie
SMESH_Gen_i* smeshGen_i = SMESH_Gen_i::GetSMESHGen();
CORBA::Object_var anObject = smeshGen_i->GetNS()->Resolve("/myStudyManager");
SALOMEDS::StudyManager_var aStudyMgr = SALOMEDS::StudyManager::_narrow(anObject);
SALOMEDS::Study_var myStudy = aStudyMgr->GetStudyByID(hyp->GetStudyId());
const NETGENPlugin_Hypothesis::TLocalSize localSizes = hyp->GetLocalSizesAndEntries();
NETGENPlugin_Hypothesis::TLocalSize::const_iterator it = localSizes.begin();
for (it ; it != localSizes.end() ; it++)
{
std::string entry = (*it).first;
double val = (*it).second;
// --
GEOM::GEOM_Object_var aGeomObj;
TopoDS_Shape S = TopoDS_Shape();
SALOMEDS::SObject_var aSObj = myStudy->FindObjectID( entry.c_str() );
if (!aSObj->_is_nil()) {
CORBA::Object_var obj = aSObj->GetObject();
aGeomObj = GEOM::GEOM_Object::_narrow(obj);
aSObj->UnRegister();
}
if ( !aGeomObj->_is_nil() )
S = smeshGen_i->GeomObjectToShape( aGeomObj.in() );
// --
SetLocalSize(S, val);
}
*/
}
}
//=============================================================================
/*!
* Pass simple parameters to NETGEN
*/
//=============================================================================
void NETGENPlugin_Mesher::SetParameters(const NETGENPlugin_SimpleHypothesis_2D* hyp)
{
_simpleHyp = hyp;
if ( _simpleHyp )
SetDefaultParameters();
}
//=============================================================================
/*!
* Link - a pair of integer numbers
*/
//=============================================================================
struct Link
{
int n1, n2;
Link(int _n1, int _n2) : n1(_n1), n2(_n2) {}
Link() : n1(0), n2(0) {}
bool Contains( int n ) const { return n == n1 || n == n2; }
bool IsConnected( const Link& other ) const
{
return (( Contains( other.n1 ) || Contains( other.n2 )) && ( this != &other ));
}
};
int HashCode(const Link& aLink, int aLimit)
{
return HashCode(aLink.n1 + aLink.n2, aLimit);
}
Standard_Boolean IsEqual(const Link& aLink1, const Link& aLink2)
{
return ((aLink1.n1 == aLink2.n1 && aLink1.n2 == aLink2.n2) ||
(aLink1.n1 == aLink2.n2 && aLink1.n2 == aLink2.n1));
}
namespace
{
//================================================================================
/*!
* \brief return id of netgen point corresponding to SMDS node
*/
//================================================================================
typedef map< const SMDS_MeshNode*, int > TNode2IdMap;
int ngNodeId( const SMDS_MeshNode* node,
netgen::Mesh& ngMesh,
TNode2IdMap& nodeNgIdMap)
{
int newNgId = ngMesh.GetNP() + 1;
TNode2IdMap::iterator node_id = nodeNgIdMap.insert( make_pair( node, newNgId )).first;
if ( node_id->second == newNgId)
{
#if defined(DUMP_SEGMENTS) || defined(DUMP_TRIANGLES)
cout << "Ng " << newNgId << " - " << node;
#endif
netgen::MeshPoint p( netgen::Point<3> (node->X(), node->Y(), node->Z()) );
ngMesh.AddPoint( p );
}
return node_id->second;
}
//================================================================================
/*!
* \brief Return computed EDGEs connected to the given one
*/
//================================================================================
list< TopoDS_Edge > getConnectedEdges( const TopoDS_Edge& edge,
const TopoDS_Face& face,
const set< SMESH_subMesh* > & computedSM,
const SMESH_MesherHelper& helper,
map< SMESH_subMesh*, set< int > >& addedEdgeSM2Faces)
{
// get ordered EDGEs
list< TopoDS_Edge > edges;
list< int > nbEdgesInWire;
int nbWires = SMESH_Block::GetOrderedEdges( face, edges, nbEdgesInWire);
// find <edge> within <edges>
list< TopoDS_Edge >::iterator eItFwd = edges.begin();
for ( ; eItFwd != edges.end(); ++eItFwd )
if ( edge.IsSame( *eItFwd ))
break;
if ( eItFwd == edges.end()) return list< TopoDS_Edge>();
if ( eItFwd->Orientation() >= TopAbs_INTERNAL )
{
// connected INTERNAL edges returned from GetOrderedEdges() are wrongly oriented
// so treat each INTERNAL edge separately
TopoDS_Edge e = *eItFwd;
edges.clear();
edges.push_back( e );
return edges;
}
// get all computed EDGEs connected to <edge>
list< TopoDS_Edge >::iterator eItBack = eItFwd, ePrev;
TopoDS_Vertex vCommon;
TopTools_MapOfShape eAdded; // map used not to add a seam edge twice to <edges>
eAdded.Add( edge );
// put edges before <edge> to <edges> back
while ( edges.begin() != eItFwd )
edges.splice( edges.end(), edges, edges.begin() );
// search forward
ePrev = eItFwd;
while ( ++eItFwd != edges.end() )
{
SMESH_subMesh* sm = helper.GetMesh()->GetSubMesh( *eItFwd );
bool connected = TopExp::CommonVertex( *ePrev, *eItFwd, vCommon );
bool computed = sm->IsMeshComputed();
bool added = addedEdgeSM2Faces[ sm ].count( helper.GetSubShapeID() );
bool doubled = !eAdded.Add( *eItFwd );
bool orientOK = (( ePrev ->Orientation() < TopAbs_INTERNAL ) ==
( eItFwd->Orientation() < TopAbs_INTERNAL ) );
if ( !connected || !computed || !orientOK || added || doubled )
{
// stop advancement; move edges from tail to head
while ( edges.back() != *ePrev )
edges.splice( edges.begin(), edges, --edges.end() );
break;
}
ePrev = eItFwd;
}
// search backward
while ( eItBack != edges.begin() )
{
ePrev = eItBack;
--eItBack;
SMESH_subMesh* sm = helper.GetMesh()->GetSubMesh( *eItBack );
bool connected = TopExp::CommonVertex( *ePrev, *eItBack, vCommon );
bool computed = sm->IsMeshComputed();
bool added = addedEdgeSM2Faces[ sm ].count( helper.GetSubShapeID() );
bool doubled = !eAdded.Add( *eItBack );
bool orientOK = (( ePrev ->Orientation() < TopAbs_INTERNAL ) ==
( eItBack->Orientation() < TopAbs_INTERNAL ) );
if ( !connected || !computed || !orientOK || added || doubled)
{
// stop advancement
edges.erase( edges.begin(), ePrev );
break;
}
}
if ( edges.front() != edges.back() )
{
// assure that the 1st vertex is meshed
TopoDS_Edge eLast = edges.back();
while ( !SMESH_Algo::VertexNode( SMESH_MesherHelper::IthVertex( 0, edges.front()), helper.GetMeshDS())
&&
edges.front() != eLast )
edges.splice( edges.end(), edges, edges.begin() );
}
return edges;
}
//================================================================================
/*!
* \brief Make triangulation of a shape precise enough
*/
//================================================================================
void updateTriangulation( const TopoDS_Shape& shape )
{
// static set< Poly_Triangulation* > updated;
// TopLoc_Location loc;
// TopExp_Explorer fExp( shape, TopAbs_FACE );
// for ( ; fExp.More(); fExp.Next() )
// {
// Handle(Poly_Triangulation) triangulation =
// BRep_Tool::Triangulation ( TopoDS::Face( fExp.Current() ), loc);
// if ( triangulation.IsNull() ||
// updated.insert( triangulation.operator->() ).second )
// {
// BRepTools::Clean (shape);
try {
OCC_CATCH_SIGNALS;
BRepMesh_IncrementalMesh e(shape, 0.01, true);
}
catch (Standard_Failure)
{
}
// updated.erase( triangulation.operator->() );
// triangulation = BRep_Tool::Triangulation ( TopoDS::Face( fExp.Current() ), loc);
// updated.insert( triangulation.operator->() );
// }
// }
}
//================================================================================
/*!
* \brief Returns a medium node either existing in SMESH of created by NETGEN
* \param [in] corner1 - corner node 1
* \param [in] corner2 - corner node 2
* \param [in] defaultMedium - the node created by NETGEN
* \param [in] helper - holder of medium nodes existing in SMESH
* \return const SMDS_MeshNode* - the result node
*/
//================================================================================
const SMDS_MeshNode* mediumNode( const SMDS_MeshNode* corner1,
const SMDS_MeshNode* corner2,
const SMDS_MeshNode* defaultMedium,
const SMESH_MesherHelper* helper)
{
if ( helper )
{
TLinkNodeMap::const_iterator l2n =
helper->GetTLinkNodeMap().find( SMESH_TLink( corner1, corner2 ));
if ( l2n != helper->GetTLinkNodeMap().end() )
defaultMedium = l2n->second;
}
return defaultMedium;
}
//================================================================================
/*!
* \brief Assure that mesh on given shapes is quadratic
*/
//================================================================================
inline void makeQuadratic( const TopTools_IndexedMapOfShape& shapes,
SMESH_Mesh* mesh )
{
for ( int i = 1; i <= shapes.Extent(); ++i )
{
SMESHDS_SubMesh* smDS = mesh->GetMeshDS()->MeshElements( shapes(i) );
if ( !smDS ) continue;
SMDS_ElemIteratorPtr elemIt = smDS->GetElements();
if ( !elemIt->more() ) continue;
const SMDS_MeshElement* e = elemIt->next();
if ( !e || e->IsQuadratic() )
continue;
TIDSortedElemSet elems;
elems.insert( e );
while ( elemIt->more() )
elems.insert( elems.end(), elemIt->next() );
SMESH_MeshEditor( mesh ).ConvertToQuadratic( /*3d=*/false, elems, /*biQuad=*/false );
}
}
}
//================================================================================
/*!
* \brief Initialize netgen::OCCGeometry with OCCT shape
*/
//================================================================================
void NETGENPlugin_Mesher::PrepareOCCgeometry(netgen::OCCGeometry& occgeo,
const TopoDS_Shape& shape,
SMESH_Mesh& mesh,
list< SMESH_subMesh* > * meshedSM,
NETGENPlugin_Internals* intern)
{
updateTriangulation( shape );
Bnd_Box bb;
BRepBndLib::Add (shape, bb);
double x1,y1,z1,x2,y2,z2;
bb.Get (x1,y1,z1,x2,y2,z2);
MESSAGE("shape bounding box:\n" <<
"(" << x1 << " " << y1 << " " << z1 << ") " <<
"(" << x2 << " " << y2 << " " << z2 << ")");
netgen::Point<3> p1 = netgen::Point<3> (x1,y1,z1);
netgen::Point<3> p2 = netgen::Point<3> (x2,y2,z2);
occgeo.boundingbox = netgen::Box<3> (p1,p2);
occgeo.shape = shape;
occgeo.changed = 1;
// fill maps of shapes of occgeo with not yet meshed subshapes
// get root submeshes
list< SMESH_subMesh* > rootSM;
const int shapeID = mesh.GetMeshDS()->ShapeToIndex( shape );
if ( shapeID > 0 ) { // SMESH_subMesh with ID 0 may exist, don't use it!
rootSM.push_back( mesh.GetSubMesh( shape ));
}
else {
for ( TopoDS_Iterator it( shape ); it.More(); it.Next() )
rootSM.push_back( mesh.GetSubMesh( it.Value() ));
}
// add subshapes of empty submeshes
list< SMESH_subMesh* >::iterator rootIt = rootSM.begin(), rootEnd = rootSM.end();
for ( ; rootIt != rootEnd; ++rootIt ) {
SMESH_subMesh * root = *rootIt;
SMESH_subMeshIteratorPtr smIt = root->getDependsOnIterator(/*includeSelf=*/true,
/*complexShapeFirst=*/true);
// to find a right orientation of subshapes (PAL20462)
TopTools_IndexedMapOfShape subShapes;
TopExp::MapShapes(root->GetSubShape(), subShapes);
while ( smIt->more() )
{
SMESH_subMesh* sm = smIt->next();
TopoDS_Shape shape = sm->GetSubShape();
if ( intern && intern->isShapeToPrecompute( shape ))
continue;
if ( !meshedSM || sm->IsEmpty() )
{
if ( shape.ShapeType() != TopAbs_VERTEX )
shape = subShapes( subShapes.FindIndex( shape ));// shape -> index -> oriented shape
if ( shape.Orientation() >= TopAbs_INTERNAL )
shape.Orientation( TopAbs_FORWARD ); // isuue 0020676
switch ( shape.ShapeType() ) {
case TopAbs_FACE : occgeo.fmap.Add( shape ); break;
case TopAbs_EDGE : occgeo.emap.Add( shape ); break;
case TopAbs_VERTEX: occgeo.vmap.Add( shape ); break;
case TopAbs_SOLID :occgeo.somap.Add( shape ); break;
default:;
}
}
// collect submeshes of meshed shapes
else if (meshedSM)
{
const int dim = SMESH_Gen::GetShapeDim( shape );
meshedSM[ dim ].push_back( sm );
}
}
}
occgeo.facemeshstatus.SetSize (occgeo.fmap.Extent());
occgeo.facemeshstatus = 0;
occgeo.face_maxh_modified.SetSize(occgeo.fmap.Extent());
occgeo.face_maxh_modified = 0;
occgeo.face_maxh.SetSize(occgeo.fmap.Extent());
occgeo.face_maxh = netgen::mparam.maxh;
}
//================================================================================
/*!
* \brief Return a default min size value suitable for the given geometry.
*/
//================================================================================
double NETGENPlugin_Mesher::GetDefaultMinSize(const TopoDS_Shape& geom,
const double maxSize)
{
updateTriangulation( geom );
TopLoc_Location loc;
int i1, i2, i3;
const int* pi[4] = { &i1, &i2, &i3, &i1 };
double minh = 1e100;
Bnd_B3d bb;
TopExp_Explorer fExp( geom, TopAbs_FACE );
for ( ; fExp.More(); fExp.Next() )
{
Handle(Poly_Triangulation) triangulation =
BRep_Tool::Triangulation ( TopoDS::Face( fExp.Current() ), loc);
if ( triangulation.IsNull() ) continue;
const double fTol = BRep_Tool::Tolerance( TopoDS::Face( fExp.Current() ));
const TColgp_Array1OfPnt& points = triangulation->Nodes();
const Poly_Array1OfTriangle& trias = triangulation->Triangles();
for ( int iT = trias.Lower(); iT <= trias.Upper(); ++iT )
{
trias(iT).Get( i1, i2, i3 );
for ( int j = 0; j < 3; ++j )
{
double dist2 = points(*pi[j]).SquareDistance( points( *pi[j+1] ));
if ( dist2 < minh && fTol*fTol < dist2 )
minh = dist2;
bb.Add( points(*pi[j]));
}
}
}
if ( minh > 0.25 * bb.SquareExtent() ) // simple geometry, rough triangulation
{
minh = 1e-3 * sqrt( bb.SquareExtent());
//cout << "BND BOX minh = " <<minh << endl;
}
else
{
minh = 3 * sqrt( minh ); // triangulation for visualization is rather fine
//cout << "TRIANGULATION minh = " <<minh << endl;
}
if ( minh > 0.5 * maxSize )
minh = maxSize / 3.;
return minh;
}
//================================================================================
/*!
* \brief Restrict size of elements at a given point
*/
//================================================================================
void NETGENPlugin_Mesher::RestrictLocalSize(netgen::Mesh& ngMesh,
const gp_XYZ& p,
double size,
const bool overrideMinH)
{
if ( size <= std::numeric_limits<double>::min() )
return;
if ( netgen::mparam.minh > size )
{
if ( overrideMinH )
{
ngMesh.SetMinimalH( size );
netgen::mparam.minh = size;
}
else
{
size = netgen::mparam.minh;
}
}
netgen::Point3d pi(p.X(), p.Y(), p.Z());
ngMesh.RestrictLocalH( pi, size );
}
//================================================================================
/*!
* \brief fill ngMesh with nodes and elements of computed submeshes
*/
//================================================================================
bool NETGENPlugin_Mesher::FillNgMesh(netgen::OCCGeometry& occgeom,
netgen::Mesh& ngMesh,
vector<const SMDS_MeshNode*>& nodeVec,
const list< SMESH_subMesh* > & meshedSM,
SMESH_MesherHelper* quadHelper,
SMESH_ProxyMesh::Ptr proxyMesh)
{
TNode2IdMap nodeNgIdMap;
for ( int i = 1; i < nodeVec.size(); ++i )
nodeNgIdMap.insert( make_pair( nodeVec[i], i ));
TopTools_MapOfShape visitedShapes;
map< SMESH_subMesh*, set< int > > visitedEdgeSM2Faces;
set< SMESH_subMesh* > computedSM( meshedSM.begin(), meshedSM.end() );
SMESH_MesherHelper helper (*_mesh);
int faceNgID = ngMesh.GetNFD();
list< SMESH_subMesh* >::const_iterator smIt, smEnd = meshedSM.end();
for ( smIt = meshedSM.begin(); smIt != smEnd; ++smIt )
{
SMESH_subMesh* sm = *smIt;
if ( !visitedShapes.Add( sm->GetSubShape() ))
continue;
const SMESHDS_SubMesh * smDS = sm->GetSubMeshDS();
if ( !smDS ) continue;
switch ( sm->GetSubShape().ShapeType() )
{
case TopAbs_EDGE: { // EDGE
// ----------------------
TopoDS_Edge geomEdge = TopoDS::Edge( sm->GetSubShape() );
if ( geomEdge.Orientation() >= TopAbs_INTERNAL )
geomEdge.Orientation( TopAbs_FORWARD ); // issue 0020676
// Add ng segments for each not meshed FACE the EDGE bounds
PShapeIteratorPtr fIt = helper.GetAncestors( geomEdge, *sm->GetFather(), TopAbs_FACE );
while ( const TopoDS_Shape * anc = fIt->next() )
{
faceNgID = occgeom.fmap.FindIndex( *anc );
if ( faceNgID < 1 )
continue; // meshed face
int faceSMDSId = helper.GetMeshDS()->ShapeToIndex( *anc );
if ( visitedEdgeSM2Faces[ sm ].count( faceSMDSId ))
continue; // already treated EDGE
TopoDS_Face face = TopoDS::Face( occgeom.fmap( faceNgID ));
if ( face.Orientation() >= TopAbs_INTERNAL )
face.Orientation( TopAbs_FORWARD ); // issue 0020676
// get all meshed EDGEs of the FACE connected to geomEdge (issue 0021140)
helper.SetSubShape( face );
list< TopoDS_Edge > edges = getConnectedEdges( geomEdge, face, computedSM, helper,
visitedEdgeSM2Faces );
if ( edges.empty() )
continue; // wrong ancestor?
// find out orientation of <edges> within <face>
TopoDS_Edge eNotSeam = edges.front();
if ( helper.HasSeam() )
{
list< TopoDS_Edge >::iterator eIt = edges.begin();
while ( helper.IsRealSeam( *eIt )) ++eIt;
if ( eIt != edges.end() )
eNotSeam = *eIt;
}
TopAbs_Orientation fOri = helper.GetSubShapeOri( face, eNotSeam );
bool isForwad = ( fOri == eNotSeam.Orientation() || fOri >= TopAbs_INTERNAL );
// get all nodes from connected <edges>
const bool isQuad = smDS->IsQuadratic();
StdMeshers_FaceSide fSide( face, edges, _mesh, isForwad, isQuad );
const vector<UVPtStruct>& points = fSide.GetUVPtStruct();
if ( points.empty() )
return false; // invalid node params?
int i, nbSeg = fSide.NbSegments();
// remember EDGEs of fSide to treat only once
for ( int iE = 0; iE < fSide.NbEdges(); ++iE )
visitedEdgeSM2Faces[ helper.GetMesh()->GetSubMesh( fSide.Edge(iE )) ].insert(faceSMDSId);
double otherSeamParam = 0;
bool isSeam = false;
// add segments
int prevNgId = ngNodeId( points[0].node, ngMesh, nodeNgIdMap );
for ( i = 0; i < nbSeg; ++i )
{
const UVPtStruct& p1 = points[ i ];
const UVPtStruct& p2 = points[ i+1 ];
if ( p1.node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX ) //an EDGE begins
{
isSeam = false;
if ( helper.IsRealSeam( p1.node->getshapeId() ))
{
TopoDS_Edge e = fSide.Edge( fSide.EdgeIndex( 0.5 * ( p1.normParam + p2.normParam )));
isSeam = helper.IsRealSeam( e );
if ( isSeam )
{
otherSeamParam = helper.GetOtherParam( helper.GetPeriodicIndex() & 1 ? p2.u : p2.v );
}
}
}
netgen::Segment seg;
// ng node ids
seg[0] = prevNgId;
seg[1] = prevNgId = ngNodeId( p2.node, ngMesh, nodeNgIdMap );
// node param on curve
seg.epgeominfo[ 0 ].dist = p1.param;
seg.epgeominfo[ 1 ].dist = p2.param;
// uv on face
seg.epgeominfo[ 0 ].u = p1.u;
seg.epgeominfo[ 0 ].v = p1.v;
seg.epgeominfo[ 1 ].u = p2.u;
seg.epgeominfo[ 1 ].v = p2.v;
//geomEdge = fSide.Edge( fSide.EdgeIndex( 0.5 * ( p1.normParam + p2.normParam )));
//seg.epgeominfo[ 0 ].edgenr = seg.epgeominfo[ 1 ].edgenr = occgeom.emap.FindIndex( geomEdge );
//seg.epgeominfo[ iEnd ].edgenr = edgeID; // = geom.emap.FindIndex(edge);
seg.si = faceNgID; // = geom.fmap.FindIndex (face);
seg.edgenr = ngMesh.GetNSeg() + 1; // segment id
ngMesh.AddSegment (seg);
SMESH_TNodeXYZ np1( p1.node ), np2( p2.node );
RestrictLocalSize( ngMesh, 0.5*(np1+np2), (np1-np2).Modulus() );
#ifdef DUMP_SEGMENTS
cout << "Segment: " << seg.edgenr << " on SMESH face " << helper.GetMeshDS()->ShapeToIndex( face ) << endl
<< "\tface index: " << seg.si << endl
<< "\tp1: " << seg[0] << endl
<< "\tp2: " << seg[1] << endl
<< "\tp0 param: " << seg.epgeominfo[ 0 ].dist << endl
<< "\tp0 uv: " << seg.epgeominfo[ 0 ].u <<", "<< seg.epgeominfo[ 0 ].v << endl
//<< "\tp0 edge: " << seg.epgeominfo[ 0 ].edgenr << endl
<< "\tp1 param: " << seg.epgeominfo[ 1 ].dist << endl
<< "\tp1 uv: " << seg.epgeominfo[ 1 ].u <<", "<< seg.epgeominfo[ 1 ].v << endl;
//<< "\tp1 edge: " << seg.epgeominfo[ 1 ].edgenr << endl;
#endif
if ( isSeam )
{
if ( helper.GetPeriodicIndex() && 1 ) {
seg.epgeominfo[ 0 ].u = otherSeamParam;
seg.epgeominfo[ 1 ].u = otherSeamParam;
swap (seg.epgeominfo[0].v, seg.epgeominfo[1].v);
} else {
seg.epgeominfo[ 0 ].v = otherSeamParam;
seg.epgeominfo[ 1 ].v = otherSeamParam;
swap (seg.epgeominfo[0].u, seg.epgeominfo[1].u);
}
swap (seg[0], seg[1]);
swap (seg.epgeominfo[0].dist, seg.epgeominfo[1].dist);
seg.edgenr = ngMesh.GetNSeg() + 1; // segment id
ngMesh.AddSegment (seg);
#ifdef DUMP_SEGMENTS
cout << "Segment: " << seg.edgenr << endl
<< "\t is SEAM (reverse) of the previous. "
<< " Other " << (helper.GetPeriodicIndex() && 1 ? "U" : "V")
<< " = " << otherSeamParam << endl;
#endif
}
else if ( fOri == TopAbs_INTERNAL )
{
swap (seg[0], seg[1]);
swap( seg.epgeominfo[0], seg.epgeominfo[1] );
seg.edgenr = ngMesh.GetNSeg() + 1; // segment id
ngMesh.AddSegment (seg);
#ifdef DUMP_SEGMENTS
cout << "Segment: " << seg.edgenr << endl << "\t is REVERSE of the previous" << endl;
#endif
}
}
} // loop on geomEdge ancestors
if ( quadHelper ) // remember medium nodes of sub-meshes
{
SMDS_ElemIteratorPtr edges = smDS->GetElements();
while ( edges->more() )
{
const SMDS_MeshElement* e = edges->next();
if ( !quadHelper->AddTLinks( static_cast< const SMDS_MeshEdge*>( e )))
break;
}
}
break;
} // case TopAbs_EDGE
case TopAbs_FACE: { // FACE
// ----------------------
const TopoDS_Face& geomFace = TopoDS::Face( sm->GetSubShape() );
helper.SetSubShape( geomFace );
bool isInternalFace = ( geomFace.Orientation() == TopAbs_INTERNAL );
// Find solids the geomFace bounds
int solidID1 = 0, solidID2 = 0;
StdMeshers_QuadToTriaAdaptor* quadAdaptor =
dynamic_cast<StdMeshers_QuadToTriaAdaptor*>( proxyMesh.get() );
if ( quadAdaptor )
{
solidID1 = occgeom.somap.FindIndex( quadAdaptor->GetShape() );
}
else
{
PShapeIteratorPtr solidIt = helper.GetAncestors( geomFace, *sm->GetFather(), TopAbs_SOLID);
while ( const TopoDS_Shape * solid = solidIt->next() )
{
int id = occgeom.somap.FindIndex ( *solid );
if ( solidID1 && id != solidID1 ) solidID2 = id;
else solidID1 = id;
}
}
// Add ng face descriptors of meshed faces
faceNgID++;
ngMesh.AddFaceDescriptor (netgen::FaceDescriptor(faceNgID, solidID1, solidID2, 0));
// if second oreder is required, even already meshed faces must be passed to NETGEN
int fID = occgeom.fmap.Add( geomFace );
while ( fID < faceNgID ) // geomFace is already in occgeom.fmap, add a copy
fID = occgeom.fmap.Add( BRepBuilderAPI_Copy( geomFace, /*copyGeom=*/false ));
// Problem with the second order in a quadrangular mesh remains.
// 1) All quadrangles generated by NETGEN are moved to an inexistent face
// by FillSMesh() (find "AddFaceDescriptor")
// 2) Temporary triangles generated by StdMeshers_QuadToTriaAdaptor
// are on faces where quadrangles were.
// Due to these 2 points, wrong geom faces are used while conversion to qudratic
// of the mentioned above quadrangles and triangles
// Orient the face correctly in solidID1 (issue 0020206)
bool reverse = false;
if ( solidID1 ) {
TopoDS_Shape solid = occgeom.somap( solidID1 );
TopAbs_Orientation faceOriInSolid = helper.GetSubShapeOri( solid, geomFace );
if ( faceOriInSolid >= 0 )
reverse =
helper.IsReversedSubMesh( TopoDS::Face( geomFace.Oriented( faceOriInSolid )));
}
// Add surface elements
netgen::Element2d tri(3);
tri.SetIndex ( faceNgID );
SMESH_TNodeXYZ xyz[3];
#ifdef DUMP_TRIANGLES
cout << "SMESH face " << helper.GetMeshDS()->ShapeToIndex( geomFace )
<< " internal="<<isInternalFace << endl;
#endif
if ( proxyMesh )
smDS = proxyMesh->GetSubMesh( geomFace );
SMDS_ElemIteratorPtr faces = smDS->GetElements();
while ( faces->more() )