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"
39 #include <Adaptor2d_Curve2d.hxx>
40 #include <BRepAdaptor_CompCurve.hxx>
41 #include <BRepAdaptor_Curve.hxx>
42 #include <BRep_Builder.hxx>
43 #include <BRep_Tool.hxx>
45 #include <TopoDS_Face.hxx>
46 #include <TopoDS_Vertex.hxx>
47 #include <TopoDS_Wire.hxx>
51 #include "utilities.h"
53 //================================================================================
55 * \brief Constructor of a side of one edge
56 * \param theFace - the face
57 * \param theEdge - the edge
59 //================================================================================
61 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
62 const TopoDS_Edge& theEdge,
64 const bool theIsForward)
66 list<TopoDS_Edge> edges(1,theEdge);
67 *this = StdMeshers_FaceSide( theFace, edges, theMesh, theIsForward );
70 //================================================================================
72 * \brief Constructor of a side of several edges
73 * \param theFace - the face
74 * \param theEdge - the edge
76 //================================================================================
78 StdMeshers_FaceSide::StdMeshers_FaceSide(const TopoDS_Face& theFace,
79 list<TopoDS_Edge>& theEdges,
81 const bool theIsForward)
83 int nbEdges = theEdges.size();
84 myEdge.resize( nbEdges );
85 myC2d.resize( nbEdges );
86 myFirst.resize( nbEdges );
87 myLast.resize( nbEdges );
88 myNormPar.resize( nbEdges );
90 myNbPonits = myNbSegments = 0;
92 myMissingVertexNodes = false;
93 if ( nbEdges == 0 ) return;
95 SMESHDS_Mesh* meshDS = theMesh->GetMeshDS();
96 vector<double> len( nbEdges );
99 list<TopoDS_Edge>::iterator edge = theEdges.begin();
100 for ( int index = 0; edge != theEdges.end(); ++index, ++edge )
102 int i = theIsForward ? index : nbEdges - index - 1;
103 len[i] = SMESH_Algo::EdgeLength( *edge );
104 if ( len[i] < DBL_MIN ) nbDegen++;
107 if ( !theIsForward ) myEdge[i].Reverse();
109 if ( theFace.IsNull() )
110 BRep_Tool::Range( *edge, myFirst[i], myLast[i] );
112 myC2d[i] = BRep_Tool::CurveOnSurface( *edge, theFace, myFirst[i], myLast[i] );
113 if ( myEdge[i].Orientation() == TopAbs_REVERSED )
114 std::swap( myFirst[i], myLast[i] );
116 if ( SMESHDS_SubMesh* sm = meshDS->MeshElements( *edge )) {
117 int nbN = sm->NbNodes();
118 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
119 if ( elemIt->more() && elemIt->next()->IsQuadratic() )
120 nbN -= sm->NbElements();
122 myNbSegments += sm->NbElements();
124 if ( SMESH_Algo::VertexNode( TopExp::FirstVertex( *edge, 1), meshDS ))
125 myNbPonits += 1; // for the first end
127 myMissingVertexNodes = true;
129 if ( SMESH_Algo::VertexNode( TopExp::LastVertex( theEdges.back(), 1), meshDS ))
130 myNbPonits++; // for the last end
132 myMissingVertexNodes = true;
134 if ( nbEdges > 1 && myLength > DBL_MIN ) {
135 const double degenNormLen = 1.e-5;
136 double totLength = myLength;
138 totLength += myLength * degenNormLen * nbDegen;
139 double prevNormPar = 0;
140 for ( int i = 0; i < nbEdges; ++i ) {
141 if ( len[ i ] < DBL_MIN )
142 len[ i ] = myLength * degenNormLen;
143 myNormPar[ i ] = prevNormPar + len[i]/totLength;
144 prevNormPar = myNormPar[ i ];
147 myNormPar[nbEdges-1] = 1.;
151 //================================================================================
153 * \brief Return info on nodes on the side
154 * \retval UVPtStruct* - array of data structures
156 //================================================================================
158 const vector<UVPtStruct>& StdMeshers_FaceSide::GetUVPtStruct(bool isXConst,
159 double constValue) const
161 if ( myPoints.empty() ) {
163 if ( NbEdges() == 0 ) return myPoints;
165 SMESHDS_Mesh* meshDS = myMesh->GetMeshDS();
167 // sort nodes of all edges putting them into a map
169 map< double, const SMDS_MeshNode*> u2node;
171 for ( int i = 0; i < myEdge.size(); ++i )
173 // put 1st vertex node
174 TopoDS_Vertex VFirst, VLast;
175 TopExp::Vertices( myEdge[i], VFirst, VLast, true);
176 const SMDS_MeshNode* node = SMESH_Algo::VertexNode( VFirst, meshDS );
177 double prevNormPar = ( i == 0 ? 0 : myNormPar[ i-1 ]); // normalized param
178 if ( node ) { // internal nodes may be missing
179 u2node.insert( make_pair( prevNormPar, node ));
180 } else if ( i == 0 ) {
181 MESSAGE(" NO NODE on VERTEX" );
185 // put 2nd vertex node for a last edge
186 if ( i+1 == myEdge.size() ) {
187 node = SMESH_Algo::VertexNode( VLast, meshDS );
189 MESSAGE(" NO NODE on VERTEX" );
192 u2node.insert( make_pair( 1., node ));
195 // put internal nodes
196 SMESHDS_SubMesh* sm = meshDS->MeshElements( myEdge[i] );
198 SMDS_NodeIteratorPtr nItr = sm->GetNodes();
199 double paramSize = myLast[i] - myFirst[i], r = myNormPar[i] - prevNormPar;
200 while ( nItr->more() ) {
201 const SMDS_MeshNode* node = nItr->next();
202 if ( SMESH_MeshEditor::IsMedium( node, SMDSAbs_Edge ))
204 const SMDS_EdgePosition* epos =
205 static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
206 double u = epos->GetUParameter();
207 // paramSize is signed so orientation is taken into account
208 double normPar = prevNormPar + r * ( u - myFirst[i] ) / paramSize;
210 if ( normPar > 1 || normPar < 0) {
212 cout << "WRONG normPar: "<<normPar<< " prevNormPar="<<prevNormPar
213 << " u="<<u << " myFirst[i]="<<myFirst[i]<< " myLast[i]="<<myLast[i]
214 << " paramSize="<<paramSize<<endl;
217 u2node.insert( make_pair( normPar, node ));
220 if ( u2node.size() != myNbPonits ) {
221 MESSAGE("Wrong node parameters on edges, u2node.size():"
222 <<u2node.size()<<" != myNbPonits:"<<myNbPonits);
226 // fill array of UVPtStruct
228 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myPoints );
229 points->resize( myNbPonits );
232 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
233 map< double, const SMDS_MeshNode*>::iterator u_node = u2node.begin();
234 for (int i = 0 ; u_node != u2node.end(); ++u_node, ++i ) {
235 UVPtStruct & uvPt = (*points)[i];
236 uvPt.node = u_node->second;
237 uvPt.x = uvPt.y = uvPt.normParam = u_node->first;
238 if ( isXConst ) uvPt.x = constValue;
239 else uvPt.y = constValue;
240 if ( myNormPar[ EdgeIndex ] < uvPt.normParam ) {
241 prevNormPar = myNormPar[ EdgeIndex ];
244 if ( EdgeIndex >= myEdge.size() ) {
246 cout << "WRONg EdgeIndex " << 1+EdgeIndex
247 << " myNormPar.size()="<<myNormPar.size()
248 << " myNormPar["<< EdgeIndex<<"]="<< myNormPar[ EdgeIndex ]
249 << " uvPt.normParam="<<uvPt.normParam <<endl;
252 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
254 const SMDS_EdgePosition* epos =
255 dynamic_cast<const SMDS_EdgePosition*>(uvPt.node->GetPosition().get());
257 uvPt.param = epos->GetUParameter();
260 double r = ( uvPt.normParam - prevNormPar )/ paramSize;
261 uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
263 if ( !myC2d[ EdgeIndex ].IsNull() ) {
264 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
269 uvPt.u = uvPt.v = 1e+100;
276 //================================================================================
278 * \brief Falsificate info on nodes
279 * \param nbSeg - nb of segments on the side
280 * \retval UVPtStruct* - array of data structures
282 //================================================================================
284 const vector<UVPtStruct>& StdMeshers_FaceSide::SimulateUVPtStruct(int nbSeg,
286 double constValue) const
288 if ( myFalsePoints.empty() ) {
290 if ( NbEdges() == 0 ) return myFalsePoints;
292 vector<uvPtStruct>* points = const_cast<vector<uvPtStruct>*>( &myFalsePoints );
293 points->resize( nbSeg+1 );
296 double prevNormPar = 0, paramSize = myNormPar[ EdgeIndex ];
297 for (int i = 0 ; i < myFalsePoints.size(); ++i ) {
298 double normPar = double(i) / double(nbSeg);
299 UVPtStruct & uvPt = (*points)[i];
301 uvPt.x = uvPt.y = uvPt.param = uvPt.normParam = normPar;
302 if ( isXConst ) uvPt.x = constValue;
303 else uvPt.y = constValue;
304 if ( myNormPar[ EdgeIndex ] < normPar ) {
305 prevNormPar = myNormPar[ EdgeIndex ];
307 paramSize = myNormPar[ EdgeIndex ] - prevNormPar;
309 double r = ( normPar - prevNormPar )/ paramSize;
310 uvPt.param = myFirst[EdgeIndex] * ( 1 - r ) + myLast[EdgeIndex] * r;
311 if ( !myC2d[ EdgeIndex ].IsNull() ) {
312 gp_Pnt2d p = myC2d[ EdgeIndex ]->Value( uvPt.param );
317 uvPt.u = uvPt.v = 1e+100;
321 return myFalsePoints;
323 // gp_Pnt StdMeshers_FaceSide::Value(double U) const
327 //================================================================================
329 * \brief reverse order of vector elements
330 * \param vec - vector to reverse
332 //================================================================================
334 template <typename T > void reverse(vector<T> & vec)
336 for ( int f=0, r=vec.size()-1; f < r; ++f, --r )
337 std::swap( vec[f], vec[r] );
340 //================================================================================
342 * \brief Change orientation of side geometry
344 //================================================================================
346 void StdMeshers_FaceSide::Reverse()
348 int nbEdges = myEdge.size();
349 for ( int i = nbEdges-1; i >= 0; --i ) {
350 std::swap( myFirst[i], myLast[i] );
351 // at the first loop 1. is overwritten
352 myNormPar[i] = 1 - myNormPar[i-1];
360 reverse( myNormPar );
362 myNormPar[nbEdges-1]=1.;
364 myFalsePoints.clear();
367 //================================================================================
369 * \brief Show side features
371 //================================================================================
373 void StdMeshers_FaceSide::dump(const char* msg) const
377 if (msg) cout << msg <<endl;
378 cout<<"NB EDGES: "<< myEdge.size() <<endl;
379 cout << "nbPoints: "<<myNbPonits<<" vecSize: " << myPoints.size()<<" "<<myFalsePoints.size() <<endl;
380 for ( int i=0; i<myEdge.size(); ++i)
382 cout << "\t"<<i+1<<endl;
384 if (myEdge[i].IsNull())
387 TopAbs::Print(myEdge[i].Orientation(),cout)<<" "<<myEdge[i].TShape().operator->()<<endl;
388 cout << "\tV1: " << TopExp::FirstVertex( myEdge[i], 1).TShape().operator->()
389 << " V2: " << TopExp::LastVertex( myEdge[i], 1).TShape().operator->() << endl;
392 if (myC2d[i].IsNull()) cout<<"NULL"<<endl;
393 else cout << myC2d[i].operator->()<<endl;
394 cout << "\tF: "<<myFirst[i]<< " L: "<< myLast[i]<<endl;
395 cout << "\tnormPar: "<<myNormPar[i]<<endl;
400 //================================================================================
402 * \brief Creates a Adaptor2d_Curve2d to be used in SMESH_Block
403 * \retval Adaptor2d_Curve2d* -
405 //================================================================================
407 struct Adaptor2dCurve2d : public Adaptor2d_Curve2d
409 const StdMeshers_FaceSide* mySide;
410 Adaptor2dCurve2d(const StdMeshers_FaceSide* faceSide):mySide(faceSide) {}
411 gp_Pnt2d Value(const Standard_Real U) const { return mySide->Value2d( U ); }
412 Standard_Real FirstParameter() const { return 0; }
413 Standard_Real LastParameter() const { return 1; }
416 Adaptor2d_Curve2d* StdMeshers_FaceSide::GetCurve2d() const
418 return new Adaptor2dCurve2d( this );
421 //================================================================================
423 * \brief Creates a fully functional Adaptor_Curve
425 //================================================================================
427 BRepAdaptor_CompCurve* StdMeshers_FaceSide::GetCurve3d() const
429 if ( myEdge.empty() )
432 // if ( myEdge.size() == 1 )
433 // return new BRepAdaptor_Curve( myEdge[0] );
436 BRep_Builder aBuilder;
437 aBuilder.MakeWire(aWire);
438 for ( int i=0; i<myEdge.size(); ++i )
439 aBuilder.Add( aWire, myEdge[i] );
440 return new BRepAdaptor_CompCurve( aWire );
443 //================================================================================
445 * \brief Return 2D point by normalized parameter
446 * \param U - normalized parameter value
447 * \retval gp_Pnt2d - point
449 //================================================================================
451 gp_Pnt2d StdMeshers_FaceSide::Value2d(double U) const
453 if ( !myC2d[0].IsNull() ) {
454 int i = EdgeIndex( U );
455 double prevU = i ? myNormPar[ i-1 ] : 0;
456 double r = ( U - prevU )/ ( myNormPar[ i ] - prevU );
457 return myC2d[ i ]->Value( myFirst[i] * ( 1 - r ) + myLast[i] * r );
459 return gp_Pnt2d( 1e+100, 1e+100 );