1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : StdMeshers_FaceSide.hxx
25 // Created : Wed Jan 31 18:41:25 2007
26 // Author : Edward AGAPOV (eap)
29 #include "StdMeshers_FaceSide.hxx"
31 #include "SMDS_EdgePosition.hxx"
32 #include "SMDS_MeshNode.hxx"
33 #include "SMESHDS_Mesh.hxx"
34 #include "SMESHDS_SubMesh.hxx"
35 //#include "SMESH_Algo.hxx"
36 #include "SMESH_Mesh.hxx"
37 #include "SMESH_MeshEditor.hxx"
38 #include "SMESH_ComputeError.hxx"
39 #include "SMESH_Block.hxx"
41 #include <Adaptor2d_Curve2d.hxx>
42 #include <BRepAdaptor_CompCurve.hxx>
43 #include <BRepAdaptor_Curve.hxx>
44 #include <BRep_Builder.hxx>
45 #include <BRep_Tool.hxx>
47 #include <TopoDS_Face.hxx>
48 #include <TopoDS_Vertex.hxx>
49 #include <TopoDS_Wire.hxx>
53 #include "utilities.h"
55 //================================================================================
57 * \brief Constructor of a side of one edge
58 * \param theFace - the face
59 * \param theEdge - the edge
61 //================================================================================
63 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
64 const TopoDS_Edge& theEdge,
66 const bool theIsForward,
67 const bool theIgnoreMediumNodes)
69 list<TopoDS_Edge> edges(1,theEdge);
70 *this = StdMeshers_FaceSide( theFace, edges, theMesh, theIsForward, theIgnoreMediumNodes );
73 //================================================================================
75 * \brief Constructor of a side of several edges
76 * \param theFace - the face
77 * \param theEdge - the edge
79 //================================================================================
81 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
82 list<TopoDS_Edge>& theEdges,
84 const bool theIsForward,
85 const bool theIgnoreMediumNodes)
87 int nbEdges = theEdges.size();
88 myEdge.resize( nbEdges );
89 myC2d.resize( nbEdges );
90 myFirst.resize( nbEdges );
91 myLast.resize( nbEdges );
92 myNormPar.resize( nbEdges );
94 myNbPonits = myNbSegments = 0;
96 myMissingVertexNodes = false;
97 myIgnoreMediumNodes = theIgnoreMediumNodes;
98 if ( nbEdges == 0 ) return;
100 SMESHDS_Mesh* meshDS = theMesh->GetMeshDS();
101 vector<double> len( nbEdges );
104 list<TopoDS_Edge>::iterator edge = theEdges.begin();
105 for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
107 int i = theIsForward ? index : nbEdges - index - 1;
108 len[i] = SMESH_Algo::EdgeLength( *edge );
109 if ( len[i] < DBL_MIN ) nbDegen++;
112 if ( !theIsForward ) myEdge[i].Reverse();
114 if ( theFace.IsNull() )
115 BRep_Tool::Range( *edge, myFirst[i], myLast[i] );
117 myC2d[i] = BRep_Tool::CurveOnSurface( *edge, theFace, myFirst[i], myLast[i] );
118 if ( myEdge[i].Orientation() == TopAbs_REVERSED )
119 std::swap( myFirst[i], myLast[i] );
121 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements( *edge )) {
122 int nbN = sm->NbNodes();
123 if ( theIgnoreMediumNodes ) {
124 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
125 if ( elemIt->more() && elemIt->next()->IsQuadratic() )
126 nbN -= sm->NbElements();
129 myNbSegments += sm->NbElements();
131 if ( SMESH_Algo::VertexNode( TopExp::FirstVertex( *edge, 1), meshDS ))
132 myNbPonits += 1; // for the first end
134 myMissingVertexNodes = true;
136 if ( SMESH_Algo::VertexNode( TopExp::LastVertex( theEdges.back(), 1), meshDS ))
137 myNbPonits++; // for the last end
139 myMissingVertexNodes = true;
141 if ( nbEdges > 1 && myLength > DBL_MIN ) {
142 const double degenNormLen = 1.e-5;
143 double totLength = myLength;
145 totLength += myLength * degenNormLen * nbDegen;
146 double prevNormPar = 0;
147 for ( int i = 0; i < nbEdges; ++i ) {
148 if ( len[ i ] < DBL_MIN )
149 len[ i ] = myLength * degenNormLen;
150 myNormPar[ i ] = prevNormPar + len[i]/totLength;
151 prevNormPar = myNormPar[ i ];
154 myNormPar[nbEdges-1] = 1.;
158 //================================================================================
160 * \brief Return info on nodes on the side
161 * \retval UVPtStruct* - array of data structures
163 //================================================================================
165 const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool isXConst,
166 double constValue) const
168 if ( myPoints.empty() ) {
170 if ( NbEdges() == 0 ) return myPoints;
172 SMESHDS_Mesh* meshDS = myMesh->GetMeshDS();
174 // sort nodes of all edges putting them into a map
176 map< double, const SMDS_MeshNode*> u2node;
178 for ( int i = 0; i < myEdge.size(); ++i )
180 // put 1st vertex node
181 TopoDS_Vertex VFirst, VLast;
182 TopExp::Vertices( myEdge[i], VFirst, VLast, true);
183 const SMDS_MeshNode* node = SMESH_Algo::VertexNode( VFirst, meshDS );
184 double prevNormPar = ( i == 0 ? 0 : myNormPar[ i-1 ]); // normalized param
185 if ( node ) { // internal nodes may be missing
186 u2node.insert( make_pair( prevNormPar, node ));
187 } else if ( i == 0 ) {
188 MESSAGE(" NO NODE on VERTEX" );
192 // put 2nd vertex node for a last edge
193 if ( i+1 == myEdge.size() ) {
194 node = SMESH_Algo::VertexNode( VLast, meshDS );
196 MESSAGE(" NO NODE on VERTEX" );
199 u2node.insert( make_pair( 1., node ));
202 // put internal nodes
203 SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
205 SMDS_NodeIteratorPtr nItr = sm->GetNodes();
206 double paramSize = myLast[i] - myFirst[i], r = myNormPar[i] - prevNormPar;
207 while ( nItr->more() ) {
208 const SMDS_MeshNode* node = nItr->next();
209 if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( node, SMDSAbs_Edge ))
211 const SMDS_EdgePosition* epos =
212 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
213 double u = epos->GetUParameter();
214 // paramSize is signed so orientation is taken into account
215 double normPar = prevNormPar + r * ( u - myFirst[i] ) / paramSize;
217 if ( normPar > 1 || normPar < 0) {
219 cout << "WRONG normPar: "<<normPar<< " prevNormPar="<<prevNormPar
220 << " u="<<u << " myFirst[i]="<<myFirst[i]<< " myLast[i]="<<myLast[i]
221 << " paramSize="<<paramSize<<endl;
224 u2node.insert( make_pair( normPar, node ));
227 if ( u2node.size() != myNbPonits ) {
228 MESSAGE("Wrong node parameters on edges, u2node.size():"
229 <<u2node.size()<<" != myNbPonits:"<<myNbPonits);
233 // fill array of UVPtStruct
235 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myPoints );
236 points->resize( myNbPonits );
239 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
240 map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
241 for (int i = 0 ; u_node != u2node.end(); ++u_node, ++i ) {
242 UVPtStruct & uvPt = (*points)[i];
243 uvPt.node = u_node->second;
244 uvPt.x = uvPt.y = uvPt.normParam = u_node->first;
245 if ( isXConst ) uvPt.x = constValue;
246 else uvPt.y = constValue;
247 if ( myNormPar[ EdgeIndex ] < uvPt.normParam ) {
248 prevNormPar = myNormPar[ EdgeIndex ];
251 if ( EdgeIndex >= myEdge.size() ) {
253 cout << "WRONg EdgeIndex " << 1+EdgeIndex
254 << " myNormPar.size()="<<myNormPar.size()
255 << " myNormPar["<< EdgeIndex<<"]="<< myNormPar[ EdgeIndex ]
256 << " uvPt.normParam="<<uvPt.normParam <<endl;
259 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
261 const SMDS_EdgePosition* epos =
262 dynamic_cast<const SMDS_EdgePosition*>(uvPt.node->GetPosition().get());
264 uvPt.param = epos->GetUParameter();
267 double r = ( uvPt.normParam - prevNormPar )/ paramSize;
268 // uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
269 uvPt.param = ( r > 0.5 ? myLast[EdgeIndex] : myFirst[EdgeIndex] );
271 if ( !myC2d[ EdgeIndex ].IsNull() ) {
272 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
277 uvPt.u = uvPt.v = 1e+100;
284 //================================================================================
286 * \brief Falsificate info on nodes
287 * \param nbSeg - nb of segments on the side
288 * \retval UVPtStruct* - array of data structures
290 //================================================================================
292 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int nbSeg,
294 double constValue) const
296 if ( myFalsePoints.empty() ) {
298 if ( NbEdges() == 0 ) return myFalsePoints;
300 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
301 points->resize( nbSeg+1 );
304 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
305 for (int i = 0 ; i < myFalsePoints.size(); ++i ) {
306 double normPar = double(i) / double(nbSeg);
307 UVPtStruct & uvPt = (*points)[i];
309 uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
310 if ( isXConst ) uvPt.x = constValue;
311 else uvPt.y = constValue;
312 if ( myNormPar[ EdgeIndex ] < normPar ) {
313 prevNormPar = myNormPar[ EdgeIndex ];
315 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
317 double r = ( normPar - prevNormPar )/ paramSize;
318 uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
319 if ( !myC2d[ EdgeIndex ].IsNull() ) {
320 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
325 uvPt.u = uvPt.v = 1e+100;
329 return myFalsePoints;
331 // gp_Pnt StdMeshers_FaceSide::Value(double U) const
335 //================================================================================
337 * \brief reverse order of vector elements
338 * \param vec - vector to reverse
340 //================================================================================
342 template <typename T > void reverse(vector<T> & vec)
344 for ( int f=0, r=vec.size()-1; f < r; ++f, --r )
345 std::swap( vec[f], vec[r] );
348 //================================================================================
350 * \brief Change orientation of side geometry
352 //================================================================================
354 void StdMeshers_FaceSide::Reverse()
356 int nbEdges = myEdge.size();
357 for ( int i = nbEdges-1; i >= 0; --i ) {
358 std::swap( myFirst[i], myLast[i] );
360 if ( i > 0 ) // at the first loop 1. is overwritten
361 myNormPar[i] = 1 - myNormPar[i-1];
368 reverse( myNormPar );
370 myNormPar[nbEdges-1]=1.;
372 myFalsePoints.clear();
375 //================================================================================
377 * \brief Show side features
379 //================================================================================
381 void StdMeshers_FaceSide::dump(const char* msg) const
385 if (msg) cout << msg <<endl;
386 cout<<"NB EDGES: "<< myEdge.size() <<endl;
387 cout << "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() <<endl;
388 for ( int i=0; i<myEdge.size(); ++i)
390 cout << "\t"<<i+1<<endl;
392 if (myEdge[i].IsNull())
395 TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
396 cout << "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
397 << " V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() << endl;
400 if (myC2d[i].IsNull()) cout<<"NULL"<<endl;
401 else cout << myC2d[i].operator->()<<endl;
402 cout << "\tF: "<<myFirst[i]<< " L: "<< myLast[i]<<endl;
403 cout << "\tnormPar: "<<myNormPar[i]<<endl;
408 //================================================================================
410 * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
411 * \retval Adaptor2d_Curve2d* -
413 //================================================================================
415 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
417 const StdMeshers_FaceSide* mySide;
418 Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
419 gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
420 Standard_Real FirstParameter() const { return 0; }
421 Standard_Real LastParameter() const { return 1; }
424 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
426 return new Adaptor2dCurve2d( this );
429 //================================================================================
431 * \brief Creates a fully functional Adaptor_Curve
433 //================================================================================
435 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
437 if ( myEdge.empty() )
440 // if ( myEdge.size() == 1 )
441 // return new BRepAdaptor_Curve( myEdge[0] );
444 BRep_Builder aBuilder;
445 aBuilder.MakeWire(aWire);
446 for ( int i=0; i<myEdge.size(); ++i )
447 aBuilder.Add( aWire, myEdge[i] );
448 return new BRepAdaptor_CompCurve( aWire );
451 //================================================================================
453 * \brief Return 2D point by normalized parameter
454 * \param U - normalized parameter value
455 * \retval gp_Pnt2d - point
457 //================================================================================
459 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
461 if ( !myC2d[0].IsNull() ) {
462 int i = EdgeIndex( U );
463 double prevU = i ? myNormPar[ i-1 ] : 0;
464 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
465 return myC2d[ i ]->Value( myFirst[i] * ( 1 - r ) + myLast[i] * r );
467 return gp_Pnt2d( 1e+100, 1e+100 );
470 //================================================================================
472 * \brief Return wires of a face as StdMeshers_FaceSide's
474 //================================================================================
476 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
477 SMESH_Mesh & theMesh,
478 const bool theIgnoreMediumNodes,
482 list< TopoDS_Edge > edges;
483 list< int > nbEdgesInWires;
484 int nbWires = SMESH_Block::GetOrderedEdges (theFace, V1, edges, nbEdgesInWires);
486 // split list of all edges into separate wires
487 TSideVector wires( nbWires );
488 list< int >::iterator nbE = nbEdgesInWires.begin();
489 list< TopoDS_Edge >::iterator from, to;
490 from = to = edges.begin();
491 for ( int iW = 0; iW < nbWires; ++iW )
493 std::advance( to, *nbE++ );
494 list< TopoDS_Edge > wireEdges( from, to );
495 // assure that there is a node on the first vertex
496 // as StdMeshers_FaceSide::GetUVPtStruct() requires
497 while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
498 theMesh.GetMeshDS()))
500 wireEdges.splice(wireEdges.end(), wireEdges,
501 wireEdges.begin(), ++wireEdges.begin());
502 if ( from->IsSame( wireEdges.front() )) {
504 ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
505 return TSideVector(0);
508 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
509 true, theIgnoreMediumNodes);
510 wires[ iW ] = StdMeshers_FaceSidePtr( wire );