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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : StdMeshers_Regular_1D.cxx
25 // Moved here from SMESH_Regular_1D.cxx
26 // Author : Paul RASCLE, EDF
32 #include "StdMeshers_Regular_1D.hxx"
33 #include "SMESH_Gen.hxx"
34 #include "SMESH_Mesh.hxx"
36 #include "StdMeshers_LocalLength.hxx"
37 #include "StdMeshers_NumberOfSegments.hxx"
38 #include "StdMeshers_Arithmetic1D.hxx"
39 #include "StdMeshers_StartEndLength.hxx"
40 #include "StdMeshers_Deflection1D.hxx"
41 #include "StdMeshers_Propagation.hxx"
43 #include "SMDS_MeshElement.hxx"
44 #include "SMDS_MeshNode.hxx"
45 #include "SMDS_EdgePosition.hxx"
46 #include "SMESH_subMesh.hxx"
48 #include "utilities.h"
50 #include <BRep_Tool.hxx>
51 #include <BRepTools_WireExplorer.hxx>
53 #include <TopoDS_Edge.hxx>
54 #include <TopoDS_Shape.hxx>
55 #include <TopTools_Array1OfShape.hxx>
56 #include <TopTools_ListIteratorOfListOfShape.hxx>
58 #include <GeomAdaptor_Curve.hxx>
59 #include <GCPnts_AbscissaPoint.hxx>
60 #include <GCPnts_UniformAbscissa.hxx>
61 #include <GCPnts_UniformDeflection.hxx>
63 #include <Standard_ErrorHandler.hxx>
64 #include <Precision.hxx>
67 //#include <algorithm>
69 //=============================================================================
73 //=============================================================================
75 StdMeshers_Regular_1D::StdMeshers_Regular_1D(int hypId, int studyId,
76 SMESH_Gen * gen):SMESH_1D_Algo(hypId, studyId, gen)
78 MESSAGE("StdMeshers_Regular_1D::StdMeshers_Regular_1D");
80 _shapeType = (1 << TopAbs_EDGE);
82 _compatibleHypothesis.push_back("LocalLength");
83 _compatibleHypothesis.push_back("NumberOfSegments");
84 _compatibleHypothesis.push_back("StartEndLength");
85 _compatibleHypothesis.push_back("Deflection1D");
86 _compatibleHypothesis.push_back("Arithmetic1D");
89 //=============================================================================
93 //=============================================================================
95 StdMeshers_Regular_1D::~StdMeshers_Regular_1D()
99 //=============================================================================
103 //=============================================================================
105 bool StdMeshers_Regular_1D::CheckHypothesis
107 const TopoDS_Shape& aShape,
108 SMESH_Hypothesis::Hypothesis_Status& aStatus)
112 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
113 if (hyps.size() == 0)
115 aStatus = SMESH_Hypothesis::HYP_MISSING;
116 return false; // can't work without a hypothesis
119 // use only the first hypothesis
120 const SMESHDS_Hypothesis *theHyp = hyps.front();
122 string hypName = theHyp->GetName();
124 if (hypName == "LocalLength")
126 const StdMeshers_LocalLength * hyp =
127 dynamic_cast <const StdMeshers_LocalLength * >(theHyp);
129 _value[ BEG_LENGTH_IND ] = _value[ END_LENGTH_IND ] = hyp->GetLength();
130 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
131 _hypType = LOCAL_LENGTH;
132 aStatus = SMESH_Hypothesis::HYP_OK;
135 else if (hypName == "NumberOfSegments")
137 const StdMeshers_NumberOfSegments * hyp =
138 dynamic_cast <const StdMeshers_NumberOfSegments * >(theHyp);
140 _value[ NB_SEGMENTS_IND ] = hyp->GetNumberOfSegments();
141 _value[ SCALE_FACTOR_IND ] = hyp->GetScaleFactor();
142 ASSERT( _value[ NB_SEGMENTS_IND ] > 0 );
143 _hypType = NB_SEGMENTS;
144 aStatus = SMESH_Hypothesis::HYP_OK;
147 else if (hypName == "Arithmetic1D")
149 const StdMeshers_Arithmetic1D * hyp =
150 dynamic_cast <const StdMeshers_Arithmetic1D * >(theHyp);
152 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
153 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
154 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
155 _hypType = ARITHMETIC_1D;
156 aStatus = SMESH_Hypothesis::HYP_OK;
159 else if (hypName == "StartEndLength")
161 const StdMeshers_StartEndLength * hyp =
162 dynamic_cast <const StdMeshers_StartEndLength * >(theHyp);
164 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
165 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
166 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
167 _hypType = BEG_END_LENGTH;
168 aStatus = SMESH_Hypothesis::HYP_OK;
171 else if (hypName == "Deflection1D")
173 const StdMeshers_Deflection1D * hyp =
174 dynamic_cast <const StdMeshers_Deflection1D * >(theHyp);
176 _value[ DEFLECTION_IND ] = hyp->GetDeflection();
177 ASSERT( _value[ DEFLECTION_IND ] > 0 );
178 _hypType = DEFLECTION;
179 aStatus = SMESH_Hypothesis::HYP_OK;
182 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
184 return ( _hypType != NONE );
187 //=============================================================================
191 //=============================================================================
192 bool StdMeshers_Regular_1D::computeInternalParameters(const TopoDS_Edge& theEdge,
193 list<double> & theParams) const
198 Handle(Geom_Curve) Curve = BRep_Tool::Curve(theEdge, f, l);
199 GeomAdaptor_Curve C3d(Curve);
201 double length = EdgeLength(theEdge);
210 if ( _hypType == LOCAL_LENGTH )
212 double nbseg = ceil(length / _value[ BEG_LENGTH_IND ]); // integer sup
214 nbseg = 1; // degenerated edge
215 eltSize = length / nbseg;
219 double epsilon = 0.001;
220 if (fabs(_value[ SCALE_FACTOR_IND ] - 1.0) > epsilon)
223 pow( _value[ SCALE_FACTOR_IND ], 1.0 / (_value[ NB_SEGMENTS_IND ] - 1));
225 length / (1 - pow( alpha,_value[ NB_SEGMENTS_IND ]));
227 int i, NbPoints = (int) _value[ NB_SEGMENTS_IND ];
228 for ( i = 2; i < NbPoints; i++ )
230 double param = factor * (1 - pow(alpha, i - 1));
231 theParams.push_back( param );
237 eltSize = length / _value[ NB_SEGMENTS_IND ];
241 GCPnts_UniformAbscissa Discret(C3d, eltSize, f, l);
242 if ( !Discret.IsDone() )
245 int NbPoints = Discret.NbPoints();
246 for ( int i = 2; i < NbPoints; i++ )
248 double param = Discret.Parameter(i);
249 theParams.push_back( param );
254 case BEG_END_LENGTH: {
256 // geometric progression: SUM(n) = ( a1 - an * q ) / ( 1 - q ) = length
258 double a1 = _value[ BEG_LENGTH_IND ];
259 double an = _value[ END_LENGTH_IND ];
260 double q = ( length - a1 ) / ( length - an );
262 double U1 = Min ( f, l );
263 double Un = Max ( f, l );
267 // computes a point on a curve <C3d> at the distance <eltSize>
268 // from the point of parameter <param>.
269 GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
270 if ( !Discret.IsDone() ) break;
271 param = Discret.Parameter();
273 theParams.push_back( param );
278 if ( a1 + an < length ) {
280 double Ln = GCPnts_AbscissaPoint::Length( C3d, theParams.back(), Un );
281 double dLn = an - Ln;
282 if ( dLn < 0.5 * an )
285 theParams.pop_back();
286 Ln = GCPnts_AbscissaPoint::Length( C3d, theParams.back(), Un );
288 if ( dLn < 0.5 * an )
291 double dUn = dLn * ( Un - U1 ) / length;
295 list<double>::reverse_iterator itU = theParams.rbegin();
296 int i, n = theParams.size();
297 for ( i = 1 ; i < n; itU++, i++ ) {
308 GCPnts_UniformDeflection Discret(C3d, _value[ DEFLECTION_IND ], true);
309 if ( !Discret.IsDone() )
312 int NbPoints = Discret.NbPoints();
313 for ( int i = 2; i < NbPoints; i++ )
315 double param = Discret.Parameter(i);
316 theParams.push_back( param );
322 case ARITHMETIC_1D: {
323 // arithmetic progression: SUM(n) = ( an - a1 + q ) * ( a1 + an ) / ( 2 * q ) = length
325 double a1 = _value[ BEG_LENGTH_IND ];
326 double an = _value[ END_LENGTH_IND ];
328 double nd = (2 * length) / (an + a1) - 1;
333 double q = ((2 * length) / (n + 1) - 2 * a1) / n;
334 double U1 = Min ( f, l );
335 double Un = Max ( f, l );
342 // computes a point on a curve <C3d> at the distance <eltSize>
343 // from the point of parameter <param>.
344 GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
345 if ( !Discret.IsDone() ) break;
346 param = Discret.Parameter();
347 if ( fabs(param - Un) > Precision::Confusion() && param < Un) {
348 theParams.push_back( param );
364 //=============================================================================
368 //=============================================================================
370 bool StdMeshers_Regular_1D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
372 MESSAGE("StdMeshers_Regular_1D::Compute");
374 if ( _hypType == NONE )
377 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
378 aMesh.GetSubMesh(aShape);
380 const TopoDS_Edge & EE = TopoDS::Edge(aShape);
381 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
384 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
386 TopoDS_Vertex VFirst, VLast;
387 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
389 ASSERT(!VFirst.IsNull());
390 SMDS_NodeIteratorPtr lid= aMesh.GetSubMesh(VFirst)->GetSubMeshDS()->GetNodes();
393 MESSAGE (" NO NODE BUILT ON VERTEX ");
396 const SMDS_MeshNode * idFirst = lid->next();
398 ASSERT(!VLast.IsNull());
399 lid=aMesh.GetSubMesh(VLast)->GetSubMeshDS()->GetNodes();
402 MESSAGE (" NO NODE BUILT ON VERTEX ");
405 const SMDS_MeshNode * idLast = lid->next();
409 list< double > params;
411 if ( ! computeInternalParameters( E, params ))
414 catch ( Standard_Failure ) {
418 // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
419 // only internal nodes receive an edge position with param on curve
421 const SMDS_MeshNode * idPrev = idFirst;
423 for (list<double>::iterator itU = params.begin(); itU != params.end(); itU++)
426 gp_Pnt P = Curve->Value(param);
428 //Add the Node in the DataStructure
429 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
430 meshDS->SetNodeOnEdge(node, E);
432 // **** edgePosition associe au point = param.
433 SMDS_EdgePosition* epos =
434 dynamic_cast<SMDS_EdgePosition *>(node->GetPosition().get());
435 epos->SetUParameter(param);
437 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
438 meshDS->SetMeshElementOnShape(edge, E);
441 SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
442 meshDS->SetMeshElementOnShape(edge, E);
446 // Edge is a degenerated Edge : We put n = 5 points on the edge.
448 BRep_Tool::Range(E, f, l);
449 double du = (l - f) / (NbPoints - 1);
450 //MESSAGE("************* Degenerated edge! *****************");
452 TopoDS_Vertex V1, V2;
453 TopExp::Vertices(E, V1, V2);
454 gp_Pnt P = BRep_Tool::Pnt(V1);
456 const SMDS_MeshNode * idPrev = idFirst;
457 for (int i = 2; i < NbPoints; i++)
459 double param = f + (i - 1) * du;
460 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
461 meshDS->SetNodeOnEdge(node, E);
463 SMDS_EdgePosition* epos =
464 dynamic_cast<SMDS_EdgePosition*>(node->GetPosition().get());
465 epos->SetUParameter(param);
467 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
468 meshDS->SetMeshElementOnShape(edge, E);
471 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
472 meshDS->SetMeshElementOnShape(edge, E);
477 //=============================================================================
481 //=============================================================================
483 const list <const SMESHDS_Hypothesis *> & StdMeshers_Regular_1D::GetUsedHypothesis
484 (SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
486 _usedHypList.clear();
487 _usedHypList = GetAppliedHypothesis(aMesh, aShape); // copy
488 int nbHyp = _usedHypList.size();
490 // try to find being propagated hypothesis
491 string propName = StdMeshers_Propagation::GetName();
493 // Get all opposite edges
494 TopTools_ListOfShape anOppositeEdges;
495 TopoDS_Shape mainShape = aMesh.GetMeshDS()->ShapeToMesh();
496 GetOppositeEdges(mainShape, aShape, anOppositeEdges);
497 TopTools_ListIteratorOfListOfShape oppIt (anOppositeEdges);
498 for (; oppIt.More(); oppIt.Next()) {
499 const TopoDS_Shape& oppE = oppIt.Value();
501 // Find Propagation hypothesis on the opposite edge
502 if (IsPropagated(aMesh, oppE)) {
504 // Get hypothesis, used by the opposite edge
505 _usedHypList = SMESH_Algo::GetUsedHypothesis(aMesh, oppE);
506 nbHyp = _usedHypList.size();
513 // try to find relevant 1D hypothesis on ancestors
515 TopTools_ListIteratorOfListOfShape ancIt (aMesh.GetAncestors(aShape));
516 for (; ancIt.More(); ancIt.Next()) {
517 const TopoDS_Shape& ancestor = ancIt.Value();
518 _usedHypList = GetAppliedHypothesis(aMesh, ancestor); // copy
519 nbHyp = _usedHypList.size();
526 _usedHypList.clear(); //only one compatible hypothesis allowed
530 //=============================================================================
532 * Is Propagation hypothesis assigned to theShape or its ancestors
534 //=============================================================================
535 Standard_Boolean StdMeshers_Regular_1D::IsPropagated (SMESH_Mesh & theMesh,
536 const TopoDS_Shape & theShape)
538 const SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
540 // try to find Propagation hypothesis on theShape
541 const list<const SMESHDS_Hypothesis*> & listHyp = meshDS->GetHypothesis(theShape);
543 list<const SMESHDS_Hypothesis*>::const_iterator it = listHyp.begin();
544 for (; it != listHyp.end(); it++) {
545 const SMESHDS_Hypothesis *anHyp = *it;
546 if (anHyp->GetName() == StdMeshers_Propagation::GetName())
547 return Standard_True;
550 // try to find Propagation hypothesis on ancestors
551 TopTools_ListIteratorOfListOfShape ancIt (theMesh.GetAncestors(theShape));
552 for (; ancIt.More(); ancIt.Next()) {
553 const TopoDS_Shape& ancestor = ancIt.Value();
554 const list<const SMESHDS_Hypothesis*> & listAncHyp = meshDS->GetHypothesis(ancestor);
556 list<const SMESHDS_Hypothesis*>::const_iterator itAnc = listAncHyp.begin();
557 for (; itAnc != listAncHyp.end(); itAnc++) {
558 const SMESHDS_Hypothesis *anHyp = *itAnc;
559 if (anHyp->GetName() == StdMeshers_Propagation::GetName())
560 return Standard_True;
564 return Standard_False;
567 //=============================================================================
569 * GetOppositeEdges() - get all edges of theShape,
570 * laying on any quadrangle face in front of theEdge
572 //=============================================================================
573 void StdMeshers_Regular_1D::GetOppositeEdges (const TopoDS_Shape& theShape,
574 const TopoDS_Shape& theEdge,
575 TopTools_ListOfShape& theOppositeEdges) const
577 TopExp_Explorer aWires (theShape, TopAbs_WIRE);
578 for (; aWires.More(); aWires.Next()) {
579 const TopoDS_Shape& aWire = aWires.Current();
580 BRepTools_WireExplorer aWE (TopoDS::Wire(aWire));
581 Standard_Integer nb = 1, found = 0;
582 TopTools_Array1OfShape anEdges (1,4);
583 for (; aWE.More(); aWE.Next(), nb++) {
588 anEdges(nb) = aWE.Current();
589 if (anEdges(nb).IsSame(theEdge))
592 if (nb == 5 && found > 0) {
593 Standard_Integer opp = found + 2;
594 if (opp > 4) opp -= 4;
595 theOppositeEdges.Append(anEdges(opp));
600 //=============================================================================
604 //=============================================================================
606 ostream & StdMeshers_Regular_1D::SaveTo(ostream & save)
611 //=============================================================================
615 //=============================================================================
617 istream & StdMeshers_Regular_1D::LoadFrom(istream & load)
622 //=============================================================================
626 //=============================================================================
628 ostream & operator <<(ostream & save, StdMeshers_Regular_1D & hyp)
630 return hyp.SaveTo( save );
633 //=============================================================================
637 //=============================================================================
639 istream & operator >>(istream & load, StdMeshers_Regular_1D & hyp)
641 return hyp.LoadFrom( load );