1 //=============================================================================
2 // File : NETGENPlugin_NETGEN_3D.cxx
3 // Moved here from SMESH_NETGEN_3D.cxx
4 // Created : lundi 27 Janvier 2003
5 // Author : Nadir BOUHAMOU (CEA)
7 // Copyright : CEA 2003
9 //=============================================================================
12 #include "NETGENPlugin_NETGEN_3D.hxx"
14 #include "SMESH_Gen.hxx"
15 #include "SMESH_Mesh.hxx"
16 #include "SMESH_ControlsDef.hxx"
17 #include "SMESHDS_Mesh.hxx"
18 #include "SMDS_MeshElement.hxx"
19 #include "SMDS_MeshNode.hxx"
23 #include "utilities.h"
35 //=============================================================================
39 //=============================================================================
41 NETGENPlugin_NETGEN_3D::NETGENPlugin_NETGEN_3D(int hypId, int studyId,
43 : SMESH_3D_Algo(hypId, studyId, gen)
45 MESSAGE("NETGENPlugin_NETGEN_3D::NETGENPlugin_NETGEN_3D");
47 // _shapeType = TopAbs_SOLID;
48 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
49 // MESSAGE("_shapeType octal " << oct << _shapeType);
50 _compatibleHypothesis.push_back("MaxElementVolume");
52 _maxElementVolume = 0.;
54 _hypMaxElementVolume = NULL;
57 //=============================================================================
61 //=============================================================================
63 NETGENPlugin_NETGEN_3D::~NETGENPlugin_NETGEN_3D()
65 MESSAGE("NETGENPlugin_NETGEN_3D::~NETGENPlugin_NETGEN_3D");
68 //=============================================================================
72 //=============================================================================
74 bool NETGENPlugin_NETGEN_3D::CheckHypothesis
76 const TopoDS_Shape& aShape,
77 SMESH_Hypothesis::Hypothesis_Status& aStatus)
79 MESSAGE("NETGENPlugin_NETGEN_3D::CheckHypothesis");
81 _hypMaxElementVolume = NULL;
83 list<const SMESHDS_Hypothesis*>::const_iterator itl;
84 const SMESHDS_Hypothesis* theHyp;
86 const list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape);
87 int nbHyp = hyps.size();
90 aStatus = SMESH_Hypothesis::HYP_MISSING;
91 return false; // can't work with no hypothesis
95 theHyp = (*itl); // use only the first hypothesis
97 string hypName = theHyp->GetName();
101 if (hypName == "MaxElementVolume")
103 _hypMaxElementVolume = static_cast<const StdMeshers_MaxElementVolume*> (theHyp);
104 ASSERT(_hypMaxElementVolume);
105 _maxElementVolume = _hypMaxElementVolume->GetMaxVolume();
107 aStatus = SMESH_Hypothesis::HYP_OK;
110 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
115 //=============================================================================
117 *Here we are going to use the NETGEN mesher
119 //=============================================================================
121 bool NETGENPlugin_NETGEN_3D::Compute(SMESH_Mesh& aMesh,
122 const TopoDS_Shape& aShape)
124 MESSAGE("NETGENPlugin_NETGEN_3D::Compute with maxElmentsize = " << _maxElementVolume);
126 SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
128 // -------------------------------------------------------------------
129 // get triangles on aShell and make a map of nodes to Netgen node IDs
130 // -------------------------------------------------------------------
132 typedef map< const SMDS_MeshNode*, int> TNodeToIDMap;
133 TNodeToIDMap nodeToNetgenID;
134 list< const SMDS_MeshElement* > triangles;
135 list< bool > isReversed; // orientation of triangles
137 SMESH::Controls::Area areaControl;
138 SMESH::Controls::TSequenceOfXYZ nodesCoords;
140 for (TopExp_Explorer exp(aShape,TopAbs_FACE);exp.More();exp.Next())
142 TopoDS_Shape aShapeFace = exp.Current();
143 int faceID = meshDS->ShapeToIndex( aShapeFace );
144 TopoDS_Shape aMeshedFace = meshDS->IndexToShape( faceID );
145 const SMESHDS_SubMesh * aSubMeshDSFace = meshDS->MeshElements( faceID );
146 if ( aSubMeshDSFace )
148 bool isRev = ( aShapeFace.Orientation() != aMeshedFace.Orientation() );
149 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
150 while ( iteratorElem->more() ) // loop on elements on a face
153 const SMDS_MeshElement* elem = iteratorElem->next();
154 if ( !elem || elem->NbNodes() != 3 ) {
155 INFOS( "NETGENPlugin_NETGEN_3D::Compute(), bad mesh");
159 triangles.push_back( elem );
160 isReversed.push_back( isRev );
161 // put elem nodes to nodeToNetgenID map
162 SMDS_ElemIteratorPtr triangleNodesIt = elem->nodesIterator();
163 while ( triangleNodesIt->more() ) {
164 const SMDS_MeshNode * node =
165 static_cast<const SMDS_MeshNode *>(triangleNodesIt->next());
166 nodeToNetgenID.insert( make_pair( node, 0 ));
169 // check if a trainge is degenerated
170 areaControl.GetPoints( elem, nodesCoords );
171 double area = areaControl.GetValue( nodesCoords );
172 if ( area <= DBL_MIN ) {
173 MESSAGE( "Warning: Degenerated " << elem );
179 // ---------------------------------
180 // Feed the Netgen with surface mesh
181 // ---------------------------------
183 int Netgen_NbOfNodes = nodeToNetgenID.size();
184 int Netgen_param2ndOrder = 0;
185 double Netgen_paramFine = 1.;
186 double Netgen_paramSize = _maxElementVolume;
188 double Netgen_point[3];
189 int Netgen_triangle[3];
190 int Netgen_tetrahedron[4];
194 Ng_Mesh * Netgen_mesh = Ng_NewMesh();
196 // set nodes and remember thier netgen IDs
197 TNodeToIDMap::iterator n_id = nodeToNetgenID.begin();
198 for ( int id = 0; n_id != nodeToNetgenID.end(); ++n_id )
200 const SMDS_MeshNode* node = n_id->first;
201 Netgen_point [ 0 ] = node->X();
202 Netgen_point [ 1 ] = node->Y();
203 Netgen_point [ 2 ] = node->Z();
205 Ng_AddPoint(Netgen_mesh, Netgen_point);
208 list< const SMDS_MeshElement* >::iterator tria = triangles.begin();
209 list< bool >::iterator reverse = isReversed.begin();
210 for ( ; tria != triangles.end(); ++tria, ++reverse )
213 SMDS_ElemIteratorPtr triangleNodesIt = (*tria)->nodesIterator();
214 while ( triangleNodesIt->more() ) {
215 const SMDS_MeshNode * node =
216 static_cast<const SMDS_MeshNode *>(triangleNodesIt->next());
217 Netgen_triangle[ *reverse ? 2 - i : i ] = nodeToNetgenID[ node ];
220 Ng_AddSurfaceElement(Netgen_mesh, NG_TRIG, Netgen_triangle);
223 // -------------------------
224 // Generate the volume mesh
225 // -------------------------
227 Ng_Meshing_Parameters Netgen_param;
229 Netgen_param.secondorder = Netgen_param2ndOrder;
230 Netgen_param.fineness = Netgen_paramFine;
231 Netgen_param.maxh = Netgen_paramSize;
236 status = Ng_GenerateVolumeMesh(Netgen_mesh, &Netgen_param);
238 MESSAGE("An exception has been caught during the Volume Mesh Generation ...");
239 status = NG_VOLUME_FAILURE;
242 int Netgen_NbOfNodesNew = Ng_GetNP(Netgen_mesh);
244 int Netgen_NbOfTetra = Ng_GetNE(Netgen_mesh);
246 MESSAGE("End of Volume Mesh Generation. status=" << status <<
247 ", nb new nodes: " << Netgen_NbOfNodesNew - Netgen_NbOfNodes <<
248 ", nb tetra: " << Netgen_NbOfTetra);
250 // -------------------------------------------------------------------
251 // Feed back the SMESHDS with the generated Nodes and Volume Elements
252 // -------------------------------------------------------------------
254 bool isOK = ( status == NG_OK && Netgen_NbOfTetra > 0 );
257 // vector of nodes in which node index == netgen ID
258 vector< const SMDS_MeshNode* > nodeVec ( Netgen_NbOfNodesNew + 1 );
259 // insert old nodes into nodeVec
260 for ( n_id = nodeToNetgenID.begin(); n_id != nodeToNetgenID.end(); ++n_id )
261 nodeVec[ n_id->second ] = n_id->first;
262 // create and insert new nodes into nodeVec
263 int nodeIndex = Netgen_NbOfNodes;
264 int shapeID = meshDS->ShapeToIndex( aShape );
265 for ( ; nodeIndex <= Netgen_NbOfNodesNew; ++nodeIndex )
267 Ng_GetPoint( Netgen_mesh, nodeIndex, Netgen_point );
268 SMDS_MeshNode * node = meshDS->AddNode(Netgen_point[0],
271 meshDS->SetNodeInVolume(node, shapeID);
272 nodeVec[nodeIndex] = node;
275 // create tetrahedrons
276 for ( int elemIndex = 1; elemIndex <= Netgen_NbOfTetra; ++elemIndex )
278 Ng_GetVolumeElement(Netgen_mesh, elemIndex, Netgen_tetrahedron);
279 SMDS_MeshVolume * elt = meshDS->AddVolume (nodeVec[ Netgen_tetrahedron[0] ],
280 nodeVec[ Netgen_tetrahedron[1] ],
281 nodeVec[ Netgen_tetrahedron[2] ],
282 nodeVec[ Netgen_tetrahedron[3] ]);
283 meshDS->SetMeshElementOnShape(elt, shapeID );
287 Ng_DeleteMesh(Netgen_mesh);
293 //=============================================================================
297 //=============================================================================
299 ostream & NETGENPlugin_NETGEN_3D::SaveTo(ostream & save)
304 //=============================================================================
308 //=============================================================================
310 istream & NETGENPlugin_NETGEN_3D::LoadFrom(istream & load)
315 //=============================================================================
319 //=============================================================================
321 ostream & operator << (ostream & save, NETGENPlugin_NETGEN_3D & hyp)
323 return hyp.SaveTo( save );
326 //=============================================================================
330 //=============================================================================
332 istream & operator >> (istream & load, NETGENPlugin_NETGEN_3D & hyp)
334 return hyp.LoadFrom( load );