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 <TopExp_Explorer.hxx>
48 #include <TopoDS_Face.hxx>
49 #include <TopoDS_Vertex.hxx>
50 #include <TopoDS_Wire.hxx>
54 #include "utilities.h"
58 //================================================================================
60 * \brief Constructor of a side of one edge
61 * \param theFace - the face
62 * \param theEdge - the edge
64 //================================================================================
66 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
67 const TopoDS_Edge& theEdge,
69 const bool theIsForward,
70 const bool theIgnoreMediumNodes)
72 list<TopoDS_Edge> edges(1,theEdge);
73 *this = StdMeshers_FaceSide( theFace, edges, theMesh, theIsForward, theIgnoreMediumNodes );
76 //================================================================================
78 * \brief Constructor of a side of several edges
79 * \param theFace - the face
80 * \param theEdge - the edge
82 //================================================================================
84 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
85 list<TopoDS_Edge>& theEdges,
87 const bool theIsForward,
88 const bool theIgnoreMediumNodes)
90 int nbEdges = theEdges.size();
91 myEdge.resize( nbEdges );
92 myC2d.resize( nbEdges );
93 myFirst.resize( nbEdges );
94 myLast.resize( nbEdges );
95 myNormPar.resize( nbEdges );
97 myNbPonits = myNbSegments = 0;
99 myMissingVertexNodes = false;
100 myIgnoreMediumNodes = theIgnoreMediumNodes;
101 if ( nbEdges == 0 ) return;
103 SMESHDS_Mesh* meshDS = theMesh->GetMeshDS();
104 vector<double> len( nbEdges );
107 list<TopoDS_Edge>::iterator edge = theEdges.begin();
108 for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
110 int i = theIsForward ? index : nbEdges - index - 1;
111 len[i] = SMESH_Algo::EdgeLength( *edge );
112 if ( len[i] < DBL_MIN ) nbDegen++;
115 if ( !theIsForward ) myEdge[i].Reverse();
117 if ( theFace.IsNull() )
118 BRep_Tool::Range( *edge, myFirst[i], myLast[i] );
120 myC2d[i] = BRep_Tool::CurveOnSurface( *edge, theFace, myFirst[i], myLast[i] );
121 if ( myEdge[i].Orientation() == TopAbs_REVERSED )
122 std::swap( myFirst[i], myLast[i] );
124 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements( *edge )) {
125 int nbN = sm->NbNodes();
126 if ( theIgnoreMediumNodes ) {
127 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
128 if ( elemIt->more() && elemIt->next()->IsQuadratic() )
129 nbN -= sm->NbElements();
132 myNbSegments += sm->NbElements();
134 if ( SMESH_Algo::VertexNode( TopExp::FirstVertex( *edge, 1), meshDS ))
135 myNbPonits += 1; // for the first end
137 myMissingVertexNodes = true;
139 if ( SMESH_Algo::VertexNode( TopExp::LastVertex( theEdges.back(), 1), meshDS ))
140 myNbPonits++; // for the last end
142 myMissingVertexNodes = true;
144 if ( nbEdges > 1 && myLength > DBL_MIN ) {
145 const double degenNormLen = 1.e-5;
146 double totLength = myLength;
148 totLength += myLength * degenNormLen * nbDegen;
149 double prevNormPar = 0;
150 for ( int i = 0; i < nbEdges; ++i ) {
151 if ( len[ i ] < DBL_MIN )
152 len[ i ] = myLength * degenNormLen;
153 myNormPar[ i ] = prevNormPar + len[i]/totLength;
154 prevNormPar = myNormPar[ i ];
157 myNormPar[nbEdges-1] = 1.;
161 //================================================================================
163 * \brief Return info on nodes on the side
164 * \retval UVPtStruct* - array of data structures
166 //================================================================================
168 const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool isXConst,
169 double constValue) const
171 if ( myPoints.empty() ) {
173 if ( NbEdges() == 0 ) return myPoints;
175 SMESHDS_Mesh* meshDS = myMesh->GetMeshDS();
177 // sort nodes of all edges putting them into a map
179 map< double, const SMDS_MeshNode*> u2node;
181 for ( int i = 0; i < myEdge.size(); ++i )
183 // put 1st vertex node
184 TopoDS_Vertex VFirst, VLast;
185 TopExp::Vertices( myEdge[i], VFirst, VLast, true);
186 const SMDS_MeshNode* node = SMESH_Algo::VertexNode( VFirst, meshDS );
187 double prevNormPar = ( i == 0 ? 0 : myNormPar[ i-1 ]); // normalized param
188 if ( node ) { // internal nodes may be missing
189 u2node.insert( make_pair( prevNormPar, node ));
190 } else if ( i == 0 ) {
191 MESSAGE(" NO NODE on VERTEX" );
195 // put 2nd vertex node for a last edge
196 if ( i+1 == myEdge.size() ) {
197 node = SMESH_Algo::VertexNode( VLast, meshDS );
199 MESSAGE(" NO NODE on VERTEX" );
202 u2node.insert( make_pair( 1., node ));
205 // put internal nodes
206 SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
208 SMDS_NodeIteratorPtr nItr = sm->GetNodes();
209 double paramSize = myLast[i] - myFirst[i], r = myNormPar[i] - prevNormPar;
210 while ( nItr->more() ) {
211 const SMDS_MeshNode* node = nItr->next();
212 if ( myIgnoreMediumNodes && SMESH_MeshEditor::IsMedium( node, SMDSAbs_Edge ))
214 const SMDS_EdgePosition* epos =
215 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
216 double u = epos->GetUParameter();
217 // paramSize is signed so orientation is taken into account
218 double normPar = prevNormPar + r * ( u - myFirst[i] ) / paramSize;
220 if ( normPar > 1 || normPar < 0) {
222 MESSAGE ( "WRONG normPar: "<<normPar<< " prevNormPar="<<prevNormPar
223 << " u="<<u << " myFirst[i]="<<myFirst[i]<< " myLast[i]="<<myLast[i]
224 << " paramSize="<<paramSize );
227 u2node.insert( make_pair( normPar, node ));
230 if ( u2node.size() != myNbPonits ) {
231 MESSAGE("Wrong node parameters on edges, u2node.size():"
232 <<u2node.size()<<" != myNbPonits:"<<myNbPonits);
236 // fill array of UVPtStruct
238 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myPoints );
239 points->resize( myNbPonits );
242 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
243 map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
244 for (int i = 0 ; u_node != u2node.end(); ++u_node, ++i ) {
245 UVPtStruct & uvPt = (*points)[i];
246 uvPt.node = u_node->second;
247 uvPt.x = uvPt.y = uvPt.normParam = u_node->first;
248 if ( isXConst ) uvPt.x = constValue;
249 else uvPt.y = constValue;
250 if ( myNormPar[ EdgeIndex ] < uvPt.normParam ) {
251 prevNormPar = myNormPar[ EdgeIndex ];
254 if ( EdgeIndex >= myEdge.size() ) {
256 MESSAGE ( "WRONg EdgeIndex " << 1+EdgeIndex
257 << " myNormPar.size()="<<myNormPar.size()
258 << " myNormPar["<< EdgeIndex<<"]="<< myNormPar[ EdgeIndex ]
259 << " uvPt.normParam="<<uvPt.normParam );
262 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
264 const SMDS_EdgePosition* epos =
265 dynamic_cast<const SMDS_EdgePosition*>(uvPt.node->GetPosition().get());
267 uvPt.param = epos->GetUParameter();
270 double r = ( uvPt.normParam - prevNormPar )/ paramSize;
271 // uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
272 uvPt.param = ( r > 0.5 ? myLast[EdgeIndex] : myFirst[EdgeIndex] );
274 if ( !myC2d[ EdgeIndex ].IsNull() ) {
275 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
280 uvPt.u = uvPt.v = 1e+100;
287 //================================================================================
289 * \brief Falsificate info on nodes
290 * \param nbSeg - nb of segments on the side
291 * \retval UVPtStruct* - array of data structures
293 //================================================================================
295 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int nbSeg,
297 double constValue) const
299 if ( myFalsePoints.empty() ) {
301 if ( NbEdges() == 0 ) return myFalsePoints;
303 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
304 points->resize( nbSeg+1 );
307 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
308 for (int i = 0 ; i < myFalsePoints.size(); ++i ) {
309 double normPar = double(i) / double(nbSeg);
310 UVPtStruct & uvPt = (*points)[i];
312 uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
313 if ( isXConst ) uvPt.x = constValue;
314 else uvPt.y = constValue;
315 if ( myNormPar[ EdgeIndex ] < normPar ) {
316 prevNormPar = myNormPar[ EdgeIndex ];
318 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
320 double r = ( normPar - prevNormPar )/ paramSize;
321 uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
322 if ( !myC2d[ EdgeIndex ].IsNull() ) {
323 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
328 uvPt.u = uvPt.v = 1e+100;
332 return myFalsePoints;
334 // gp_Pnt StdMeshers_FaceSide::Value(double U) const
338 //================================================================================
340 * \brief reverse order of vector elements
341 * \param vec - vector to reverse
343 //================================================================================
345 template <typename T > void reverse(vector<T> & vec)
347 for ( int f=0, r=vec.size()-1; f < r; ++f, --r )
348 std::swap( vec[f], vec[r] );
351 //================================================================================
353 * \brief Change orientation of side geometry
355 //================================================================================
357 void StdMeshers_FaceSide::Reverse()
359 int nbEdges = myEdge.size();
360 for ( int i = nbEdges-1; i >= 0; --i ) {
361 std::swap( myFirst[i], myLast[i] );
363 if ( i > 0 ) // at the first loop 1. is overwritten
364 myNormPar[i] = 1 - myNormPar[i-1];
371 reverse( myNormPar );
373 myNormPar[nbEdges-1]=1.;
375 myFalsePoints.clear();
378 //================================================================================
380 * \brief Show side features
382 //================================================================================
384 void StdMeshers_FaceSide::dump(const char* msg) const
387 if (msg) MESSAGE ( std::endl << msg );
388 MESSAGE_BEGIN ("NB EDGES: "<< myEdge.size() );
389 MESSAGE_ADD ( "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() );
390 for ( int i=0; i<myEdge.size(); ++i)
392 MESSAGE_ADD ( "\t"<<i+1 );
393 MESSAGE_ADD ( "\tEDGE: " );
394 if (myEdge[i].IsNull()) {
395 MESSAGE_ADD ( "NULL" );
398 TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
399 MESSAGE_ADD ( "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
400 << " V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() );
402 MESSAGE_ADD ( "\tC2d: ");
404 if (myC2d[i].IsNull()) {
405 MESSAGE_ADD ( "NULL" );
408 MESSAGE_ADD ( myC2d[i].operator->() );
411 MESSAGE_ADD ( "\tF: "<<myFirst[i]<< " L: "<< myLast[i] );
412 MESSAGE_END ( "\tnormPar: "<<myNormPar[i]<<endl );
417 //================================================================================
419 * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
420 * \retval Adaptor2d_Curve2d* -
422 //================================================================================
424 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
426 const StdMeshers_FaceSide* mySide;
427 Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
428 gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
429 Standard_Real FirstParameter() const { return 0; }
430 Standard_Real LastParameter() const { return 1; }
433 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
435 return new Adaptor2dCurve2d( this );
438 //================================================================================
440 * \brief Creates a fully functional Adaptor_Curve
442 //================================================================================
444 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
446 if ( myEdge.empty() )
449 // if ( myEdge.size() == 1 )
450 // return new BRepAdaptor_Curve( myEdge[0] );
453 BRep_Builder aBuilder;
454 aBuilder.MakeWire(aWire);
455 for ( int i=0; i<myEdge.size(); ++i )
456 aBuilder.Add( aWire, myEdge[i] );
457 return new BRepAdaptor_CompCurve( aWire );
460 //================================================================================
462 * \brief Return 2D point by normalized parameter
463 * \param U - normalized parameter value
464 * \retval gp_Pnt2d - point
466 //================================================================================
468 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
470 if ( !myC2d[0].IsNull() ) {
471 int i = EdgeIndex( U );
472 double prevU = i ? myNormPar[ i-1 ] : 0;
473 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
474 return myC2d[ i ]->Value( myFirst[i] * ( 1 - r ) + myLast[i] * r );
476 return gp_Pnt2d( 1e+100, 1e+100 );
479 //================================================================================
481 * \brief Return wires of a face as StdMeshers_FaceSide's
483 //================================================================================
485 TSideVector StdMeshers_FaceSide::GetFaceWires(const TopoDS_Face& theFace,
486 SMESH_Mesh & theMesh,
487 const bool theIgnoreMediumNodes,
491 list< TopoDS_Edge > edges;
492 list< int > nbEdgesInWires;
493 int nbWires = SMESH_Block::GetOrderedEdges (theFace, V1, edges, nbEdgesInWires);
495 // split list of all edges into separate wires
496 TSideVector wires( nbWires );
497 list< int >::iterator nbE = nbEdgesInWires.begin();
498 list< TopoDS_Edge >::iterator from, to;
499 from = to = edges.begin();
500 for ( int iW = 0; iW < nbWires; ++iW )
502 std::advance( to, *nbE++ );
503 list< TopoDS_Edge > wireEdges( from, to );
504 // assure that there is a node on the first vertex
505 // as StdMeshers_FaceSide::GetUVPtStruct() requires
506 while ( !SMESH_Algo::VertexNode( TopExp::FirstVertex( wireEdges.front(), true),
507 theMesh.GetMeshDS()))
509 wireEdges.splice(wireEdges.end(), wireEdges,
510 wireEdges.begin(), ++wireEdges.begin());
511 if ( from->IsSame( wireEdges.front() )) {
513 ( new SMESH_ComputeError(COMPERR_BAD_INPUT_MESH,"No nodes on vertices"));
514 return TSideVector(0);
517 // find out side orientation, which is important if there are several wires (PAL19080)
518 bool isForward = true;
520 TopExp_Explorer e( theFace, TopAbs_EDGE );
521 while ( ! e.Current().IsSame( wireEdges.back() ))
523 isForward = ( e.Current().Orientation() == wireEdges.back().Orientation() );
526 StdMeshers_FaceSide* wire = new StdMeshers_FaceSide( theFace, wireEdges, &theMesh,
527 isForward, theIgnoreMediumNodes);
528 wires[ iW ] = StdMeshers_FaceSidePtr( wire );