1 // Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // NETGENPlugin : C++ implementation
21 // File : NETGENPlugin_NETGEN_2D_SA.cxx
22 // Author : Cesar Conopoima (OCC)
25 //=============================================================================
27 #include "NETGENPlugin_NETGEN_2D_SA.hxx"
28 #include "NETGENPlugin_Hypothesis_2D.hxx"
29 #include "NETGENPlugin_SimpleHypothesis_2D.hxx"
30 #include "NETGENPlugin_Mesher.hxx"
32 #include <SMESHDS_Mesh.hxx>
33 #include <SMESH_ControlsDef.hxx>
34 #include <SMESH_Gen.hxx>
35 #include <SMESH_Mesh.hxx>
36 #include <SMESH_MesherHelper.hxx>
37 #include <SMESH_DriverShape.hxx>
38 #include <SMESH_DriverMesh.hxx>
39 #include <StdMeshers_LengthFromEdges.hxx>
40 #include <StdMeshers_MaxElementArea.hxx>
41 #include <utilities.h>
45 namespace fs = std::filesystem;
47 #include <boost/filesystem.hpp>
48 namespace fs = boost::filesystem;
54 #include <meshing.hpp>
56 using namespace nglib;
58 //=============================================================================
62 //=============================================================================
64 NETGENPlugin_NETGEN_2D_SA::NETGENPlugin_NETGEN_2D_SA()
65 : NETGENPlugin_NETGEN_2D_ONLY(0, new SMESH_Gen())
67 _name = "NETGEN_2D_SA";
70 //=============================================================================
74 //=============================================================================
76 NETGENPlugin_NETGEN_2D_SA::~NETGENPlugin_NETGEN_2D_SA()
81 * @brief Check presence and content of orientation file. Implemented for completness and future reference.
83 * @param element_orientation_file Binary file containing the orientation of surface elemnts
86 bool NETGENPlugin_NETGEN_2D_SA::checkOrientationFile( const std::string element_orientation_file )
88 if(element_orientation_file.empty()){
89 MESSAGE("No element orientation file");
92 MESSAGE("Reading from elements from file: " << element_orientation_file);
93 // By construction the orientation file written by Remote version has a zero written to mark no need of orientation in 2D meshing
95 std::ifstream df(element_orientation_file, ios::binary|ios::in);
96 df.read((char*)&nbElement, sizeof(int));
98 return (nbElement == 0);
103 * @brief fill plugin hypothesis from the netgen_params structure
105 * @param aParams the structure
107 void NETGENPlugin_NETGEN_2D_SA::fillHyp(const std::string param_file, netgen_params aParams)
110 if( aParams.has_netgen_param && aParams.myType == hypoType::Hypo )
112 NETGENPlugin_Hypothesis_2D * hyp = new NETGENPlugin_Hypothesis_2D(0, GetGen());
113 hyp->SetMaxSize(aParams.maxh);
114 hyp->SetMinSize(aParams.minh);
115 hyp->SetGrowthRate(aParams.grading);
116 hyp->SetQuadAllowed(aParams.quad);
117 hyp->SetFineness((NETGENPlugin_Hypothesis::Fineness)aParams.fineness);
118 hyp->SetChordalErrorEnabled(aParams.chordalError);
119 if(aParams.optimize){
120 hyp->SetNbSurfOptSteps(aParams.optsteps2d);
122 _hypParameters = dynamic_cast< const NETGENPlugin_Hypothesis_2D *> (hyp);
126 NETGENPlugin_Hypothesis * hyp = new NETGENPlugin_Hypothesis(0, GetGen());
127 if (param_file.find("lenghtfromedge") != std::string::npos )
129 _hypLengthFromEdges = dynamic_cast<const StdMeshers_LengthFromEdges*> (hyp);
131 else if ( param_file.find("maxarea") != std::string::npos )
133 StdMeshers_MaxElementArea * hyp = new StdMeshers_MaxElementArea(0, GetGen());
135 std::ifstream myfile(param_file);
138 std::getline(myfile, line);
139 bool hashypothesis = std::stoi(line);
142 std::getline(myfile, line);
143 maxArea = std::stod(line);
144 hyp->SetMaxArea( maxArea );
145 _hypMaxElementArea = static_cast<const StdMeshers_MaxElementArea*> (hyp);
155 * @brief Write a binary file containing information on the elements/nodes
156 * created by the mesher
158 * @param premeshedNodes map of the premeshed nodes of 1D elements
159 * @param newNetgenCoordinates map of the coordinate of new netgen points created in the mesh
160 * @param newNetgenElements map of the element triangulation
161 * @return true if there are some error
163 bool NETGENPlugin_NETGEN_2D_SA::fillNewElementFile( std::string new_element_file,
164 const int numberOfGlobalPremeshedNodes,
165 std::map<int,const SMDS_MeshNode*>& premeshedNodes,
166 std::map<int,std::vector<double>>& newNetgenCoordinates,
167 std::map<int,std::vector<smIdType>>& newNetgenElements )
169 MESSAGE("Writting new elements")
171 int NetgenNbOfNodes = premeshedNodes.size();
172 int NetgenNbOfNodesNew = numberOfGlobalPremeshedNodes + newNetgenCoordinates.size();
173 int NetgenNbOfTriangles = newNetgenElements.size();
174 bool isOK = ( NetgenNbOfTriangles > 0 );
175 if ( isOK && !new_element_file.empty() )
177 int NetgenElement[3];
178 std::ofstream df(new_element_file, ios::out|ios::binary);
179 // Writing nodevec (correspondence netgen numbering mesh numbering)
181 df.write((char*) &numberOfGlobalPremeshedNodes, sizeof(int));
182 df.write((char*) &NetgenNbOfNodes, sizeof(int));
183 df.write((char*) &NetgenNbOfNodesNew, sizeof(int));
184 for (auto k : premeshedNodes )
185 df.write((char*) &k.first, sizeof(int));
187 // Writing info on new points
188 for (auto k : newNetgenCoordinates )
189 df.write((char*) k.second.data(), sizeof(double)*3 );
192 df.write((char*) &NetgenNbOfTriangles, sizeof(int));
193 for ( int elemIndex = 1; elemIndex <= NetgenNbOfTriangles; ++elemIndex )
195 int nodes = newNetgenElements[ elemIndex ].size();
196 for (int i = 0; i < nodes; i++)
197 NetgenElement[ i ] = (int) newNetgenElements[ elemIndex ][ i ];
199 df.write((char*) &NetgenElement, sizeof(int)* nodes );
208 * @brief Compute the mesh based on the
210 * @param input_mesh_file Mesh file (containing 2D elements)
211 * @param shape_file Shape file (BREP or STEP format)
212 * @param hypo_file Ascii file containing the netgen parameters
213 * @param element_orientation_file Binary file containing the orientation of surface elemnts
214 * @param new_element_file output file containing info the elements created by the mesher
215 * @param output_mesh_file output mesh file (if empty it will not be created)
216 * @return true, false
218 bool NETGENPlugin_NETGEN_2D_SA::Compute( SMESH_Mesh& aMesh, TopoDS_Shape &aShape, std::string new_element_file )
220 // Nodes on edge are double because each face is treat one by one, so seam edges are iterated twice
221 vector< const SMDS_MeshNode* > nodeVec;
222 NETGENPlugin_Mesher aMesher( &aMesh, aShape, false /*isVolume=*/ );
223 NETGENPlugin_NetgenLibWrapper ngLib;
224 ngLib._isComputeOk = false;
226 fs::path netgen_log_file = fs::path(new_element_file).remove_filename() / fs::path("NETGEN.out");
227 MESSAGE("netgen ouput"<<netgen_log_file.string());
228 ngLib.setOutputFile(netgen_log_file.string());
230 netgen::OCCGeometry occgeoComm;
231 NETGENPlugin_NETGEN_2D_ONLY::SetParameteres( aMesh, aShape, aMesher, ngLib._ngMesh, occgeoComm, false /*submesh is not supported*/ );
233 std::map<int,const SMDS_MeshNode*> premeshedNodes;
234 std::map<int,std::vector<double>> newNetgenCoordinates;
235 std::map<int,std::vector<smIdType>> newNetgenElements;
236 const int numberOfTotalPremeshedNodes = aMesh.NbNodes();
237 bool compute = NETGENPlugin_NETGEN_2D_ONLY::MapSegmentsToEdges( aMesh, aShape, ngLib, nodeVec,
238 premeshedNodes, newNetgenCoordinates,
241 compute = fillNewElementFile(new_element_file,
242 numberOfTotalPremeshedNodes,
244 newNetgenCoordinates,
250 * @brief Running the mesher on the given files
252 * @param input_mesh_file Mesh file (containing 2D elements)
253 * @param shape_file Shape file (BREP or STEP format)
254 * @param hypo_file Ascii file containing the netgen parameters
255 * @param element_orientation_file Binary file containing the orientation of surface elemnts
256 * @param new_element_file output file containing info the elements created by the mesher
257 * @param output_mesh_file output mesh file (if empty it will not be created)
260 int NETGENPlugin_NETGEN_2D_SA::run(const std::string input_mesh_file,
261 const std::string shape_file,
262 const std::string hypo_file,
263 const std::string element_orientation_file,
264 const std::string new_element_file,
265 const std::string output_mesh_file)
268 std::unique_ptr<SMESH_Mesh> myMesh(_gen->CreateMesh(false));
270 SMESH_DriverMesh::importMesh(input_mesh_file, *myMesh);
272 TopoDS_Shape myShape;
273 SMESH_DriverShape::importShape(shape_file, myShape);
274 // Importing hypothesis
275 netgen_params myParams;
276 importNetgenParams(hypo_file, myParams);
277 fillHyp(hypo_file,myParams);
278 MESSAGE("Meshing with netgen2d");
280 if ( checkOrientationFile(element_orientation_file) )
282 ret = (int) Compute( *myMesh, myShape, new_element_file );
285 std::cerr << "Meshing failed" << std::endl;
289 if(!output_mesh_file.empty()){
290 std::string meshName = "MESH";
291 SMESH_DriverMesh::exportMesh(output_mesh_file, *myMesh, meshName);
295 std::cerr << "For NETGENPlugin_NETGEN_2D_SA orientation file should be market with 0 or be empty!" << std::endl;