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"
47 #include "utilities.h"
49 #include <BRep_Tool.hxx>
50 #include <BRepTools_WireExplorer.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Shape.hxx>
54 #include <TopTools_Array1OfShape.hxx>
55 #include <TopTools_ListIteratorOfListOfShape.hxx>
57 #include <GeomAdaptor_Curve.hxx>
58 #include <GCPnts_AbscissaPoint.hxx>
59 #include <GCPnts_UniformAbscissa.hxx>
60 #include <GCPnts_UniformDeflection.hxx>
62 #include <Standard_ErrorHandler.hxx>
63 #include <Precision.hxx>
66 //#include <algorithm>
68 //=============================================================================
72 //=============================================================================
74 StdMeshers_Regular_1D::StdMeshers_Regular_1D(int hypId, int studyId,
75 SMESH_Gen * gen):SMESH_1D_Algo(hypId, studyId, gen)
77 MESSAGE("StdMeshers_Regular_1D::StdMeshers_Regular_1D");
79 _shapeType = (1 << TopAbs_EDGE);
81 _compatibleHypothesis.push_back("LocalLength");
82 _compatibleHypothesis.push_back("NumberOfSegments");
83 _compatibleHypothesis.push_back("StartEndLength");
84 _compatibleHypothesis.push_back("Deflection1D");
85 _compatibleHypothesis.push_back("Arithmetic1D");
88 //=============================================================================
92 //=============================================================================
94 StdMeshers_Regular_1D::~StdMeshers_Regular_1D()
98 //=============================================================================
102 //=============================================================================
104 bool StdMeshers_Regular_1D::CheckHypothesis
106 const TopoDS_Shape& aShape,
107 SMESH_Hypothesis::Hypothesis_Status& aStatus)
111 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
112 if (hyps.size() == 0)
114 aStatus = SMESH_Hypothesis::HYP_MISSING;
115 return false; // can't work without a hypothesis
118 // use only the first hypothesis
119 const SMESHDS_Hypothesis *theHyp = hyps.front();
121 string hypName = theHyp->GetName();
123 if (hypName == "LocalLength")
125 const StdMeshers_LocalLength * hyp =
126 dynamic_cast <const StdMeshers_LocalLength * >(theHyp);
128 _value[ BEG_LENGTH_IND ] = _value[ END_LENGTH_IND ] = hyp->GetLength();
129 ASSERT( _value[ BEG_LENGTH_IND ] > 0 );
130 _hypType = LOCAL_LENGTH;
131 aStatus = SMESH_Hypothesis::HYP_OK;
134 else if (hypName == "NumberOfSegments")
136 const StdMeshers_NumberOfSegments * hyp =
137 dynamic_cast <const StdMeshers_NumberOfSegments * >(theHyp);
139 _value[ NB_SEGMENTS_IND ] = hyp->GetNumberOfSegments();
140 _value[ SCALE_FACTOR_IND ] = hyp->GetScaleFactor();
141 ASSERT( _value[ NB_SEGMENTS_IND ] > 0 );
142 _hypType = NB_SEGMENTS;
143 aStatus = SMESH_Hypothesis::HYP_OK;
146 else if (hypName == "Arithmetic1D")
148 const StdMeshers_Arithmetic1D * hyp =
149 dynamic_cast <const StdMeshers_Arithmetic1D * >(theHyp);
151 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
152 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
153 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
154 _hypType = ARITHMETIC_1D;
155 aStatus = SMESH_Hypothesis::HYP_OK;
158 else if (hypName == "StartEndLength")
160 const StdMeshers_StartEndLength * hyp =
161 dynamic_cast <const StdMeshers_StartEndLength * >(theHyp);
163 _value[ BEG_LENGTH_IND ] = hyp->GetLength( true );
164 _value[ END_LENGTH_IND ] = hyp->GetLength( false );
165 ASSERT( _value[ BEG_LENGTH_IND ] > 0 && _value[ END_LENGTH_IND ] > 0 );
166 _hypType = BEG_END_LENGTH;
167 aStatus = SMESH_Hypothesis::HYP_OK;
170 else if (hypName == "Deflection1D")
172 const StdMeshers_Deflection1D * hyp =
173 dynamic_cast <const StdMeshers_Deflection1D * >(theHyp);
175 _value[ DEFLECTION_IND ] = hyp->GetDeflection();
176 ASSERT( _value[ DEFLECTION_IND ] > 0 );
177 _hypType = DEFLECTION;
178 aStatus = SMESH_Hypothesis::HYP_OK;
181 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
183 return ( _hypType != NONE );
186 //=============================================================================
190 //=============================================================================
191 bool StdMeshers_Regular_1D::computeInternalParameters(const TopoDS_Edge& theEdge,
192 list<double> & theParams) const
197 Handle(Geom_Curve) Curve = BRep_Tool::Curve(theEdge, f, l);
198 GeomAdaptor_Curve C3d(Curve);
200 double length = EdgeLength(theEdge);
209 if ( _hypType == LOCAL_LENGTH )
211 double nbseg = ceil(length / _value[ BEG_LENGTH_IND ]); // integer sup
213 nbseg = 1; // degenerated edge
214 eltSize = length / nbseg;
218 double epsilon = 0.001;
219 if (fabs(_value[ SCALE_FACTOR_IND ] - 1.0) > epsilon)
222 pow( _value[ SCALE_FACTOR_IND ], 1.0 / (_value[ NB_SEGMENTS_IND ] - 1));
224 length / (1 - pow( alpha,_value[ NB_SEGMENTS_IND ]));
226 int i, NbPoints = (int) _value[ NB_SEGMENTS_IND ];
227 for ( int i = 2; i < NbPoints; i++ )
229 double param = factor * (1 - pow(alpha, i - 1));
230 theParams.push_back( param );
236 eltSize = length / _value[ NB_SEGMENTS_IND ];
240 GCPnts_UniformAbscissa Discret(C3d, eltSize, f, l);
241 if ( !Discret.IsDone() )
244 int NbPoints = Discret.NbPoints();
245 for ( int i = 2; i < NbPoints; i++ )
247 double param = Discret.Parameter(i);
248 theParams.push_back( param );
253 case BEG_END_LENGTH: {
255 // geometric progression: SUM(n) = ( a1 - an * q ) / ( 1 - q ) = length
257 double a1 = _value[ BEG_LENGTH_IND ];
258 double an = _value[ END_LENGTH_IND ];
259 double q = ( length - a1 ) / ( length - an );
261 double U1 = Min ( f, l );
262 double Un = Max ( f, l );
266 // computes a point on a curve <C3d> at the distance <eltSize>
267 // from the point of parameter <param>.
268 GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
269 if ( !Discret.IsDone() ) break;
270 param = Discret.Parameter();
272 theParams.push_back( param );
277 if ( a1 + an < length ) {
279 double Ln = GCPnts_AbscissaPoint::Length( C3d, theParams.back(), Un );
280 double dLn = an - Ln;
281 if ( dLn < 0.5 * an )
284 theParams.pop_back();
285 Ln = GCPnts_AbscissaPoint::Length( C3d, theParams.back(), Un );
287 if ( dLn < 0.5 * an )
290 double dUn = dLn * ( Un - U1 ) / length;
294 list<double>::reverse_iterator itU = theParams.rbegin();
295 int i, n = theParams.size();
296 for ( i = 1 ; i < n; itU++, i++ ) {
307 GCPnts_UniformDeflection Discret(C3d, _value[ DEFLECTION_IND ], true);
308 if ( !Discret.IsDone() )
311 int NbPoints = Discret.NbPoints();
312 for ( int i = 2; i < NbPoints; i++ )
314 double param = Discret.Parameter(i);
315 theParams.push_back( param );
321 case ARITHMETIC_1D: {
322 // arithmetic progression: SUM(n) = ( an - a1 + q ) * ( a1 + an ) / ( 2 * q ) = length
324 double a1 = _value[ BEG_LENGTH_IND ];
325 double an = _value[ END_LENGTH_IND ];
327 double nd = (2 * length) / (an + a1) - 1;
332 double q = ((2 * length) / (n + 1) - 2 * a1) / n;
333 double U1 = Min ( f, l );
334 double Un = Max ( f, l );
341 // computes a point on a curve <C3d> at the distance <eltSize>
342 // from the point of parameter <param>.
343 GCPnts_AbscissaPoint Discret( C3d, eltSize, param );
344 if ( !Discret.IsDone() ) break;
345 param = Discret.Parameter();
346 if ( fabs(param - Un) > Precision::Confusion() && param < Un) {
347 theParams.push_back( param );
363 //=============================================================================
367 //=============================================================================
369 bool StdMeshers_Regular_1D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
371 MESSAGE("StdMeshers_Regular_1D::Compute");
373 if ( _hypType == NONE )
376 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
377 SMESH_subMesh *theSubMesh = aMesh.GetSubMesh(aShape);
379 const TopoDS_Edge & EE = TopoDS::Edge(aShape);
380 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
383 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
385 TopoDS_Vertex VFirst, VLast;
386 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
388 ASSERT(!VFirst.IsNull());
389 SMDS_NodeIteratorPtr lid= aMesh.GetSubMesh(VFirst)->GetSubMeshDS()->GetNodes();
392 MESSAGE (" NO NODE BUILT ON VERTEX ");
395 const SMDS_MeshNode * idFirst = lid->next();
397 ASSERT(!VLast.IsNull());
398 lid=aMesh.GetSubMesh(VLast)->GetSubMeshDS()->GetNodes();
401 MESSAGE (" NO NODE BUILT ON VERTEX ");
404 const SMDS_MeshNode * idLast = lid->next();
408 list< double > params;
410 if ( ! computeInternalParameters( E, params ))
413 catch ( Standard_Failure ) {
417 // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
418 // only internal nodes receive an edge position with param on curve
420 const SMDS_MeshNode * idPrev = idFirst;
422 for (list<double>::iterator itU = params.begin(); itU != params.end(); itU++)
425 gp_Pnt P = Curve->Value(param);
427 //Add the Node in the DataStructure
428 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
429 meshDS->SetNodeOnEdge(node, E);
431 // **** edgePosition associe au point = param.
432 SMDS_EdgePosition* epos =
433 dynamic_cast<SMDS_EdgePosition *>(node->GetPosition().get());
434 epos->SetUParameter(param);
436 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
437 meshDS->SetMeshElementOnShape(edge, E);
440 SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
441 meshDS->SetMeshElementOnShape(edge, E);
445 // Edge is a degenerated Edge : We put n = 5 points on the edge.
447 BRep_Tool::Range(E, f, l);
448 double du = (l - f) / (NbPoints - 1);
449 //MESSAGE("************* Degenerated edge! *****************");
451 TopoDS_Vertex V1, V2;
452 TopExp::Vertices(E, V1, V2);
453 gp_Pnt P = BRep_Tool::Pnt(V1);
455 const SMDS_MeshNode * idPrev = idFirst;
456 for (int i = 2; i < NbPoints; i++)
458 double param = f + (i - 1) * du;
459 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
460 meshDS->SetNodeOnEdge(node, E);
462 SMDS_EdgePosition* epos =
463 dynamic_cast<SMDS_EdgePosition*>(node->GetPosition().get());
464 epos->SetUParameter(param);
466 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
467 meshDS->SetMeshElementOnShape(edge, E);
470 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
471 meshDS->SetMeshElementOnShape(edge, E);
476 //=============================================================================
480 //=============================================================================
482 const list <const SMESHDS_Hypothesis *> & StdMeshers_Regular_1D::GetUsedHypothesis
483 (SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
485 _usedHypList.clear();
486 _usedHypList = GetAppliedHypothesis(aMesh, aShape); // copy
487 int nbHyp = _usedHypList.size();
489 // try to find being propagated hypothesis
490 string propName = StdMeshers_Propagation::GetName();
492 // Get all opposite edges
493 TopTools_ListOfShape anOppositeEdges;
494 TopoDS_Shape mainShape = aMesh.GetMeshDS()->ShapeToMesh();
495 GetOppositeEdges(mainShape, aShape, anOppositeEdges);
496 TopTools_ListIteratorOfListOfShape oppIt (anOppositeEdges);
497 for (; oppIt.More(); oppIt.Next()) {
498 const TopoDS_Shape& oppE = oppIt.Value();
500 // Find Propagation hypothesis on the opposite edge
501 if (IsPropagated(aMesh, oppE)) {
503 // Get hypothesis, used by the opposite edge
504 _usedHypList = SMESH_Algo::GetUsedHypothesis(aMesh, oppE);
505 nbHyp = _usedHypList.size();
512 // try to find relevant 1D hypothesis on ancestors
514 TopTools_ListIteratorOfListOfShape ancIt (aMesh.GetAncestors(aShape));
515 for (; ancIt.More(); ancIt.Next()) {
516 const TopoDS_Shape& ancestor = ancIt.Value();
517 _usedHypList = GetAppliedHypothesis(aMesh, ancestor); // copy
518 nbHyp = _usedHypList.size();
525 _usedHypList.clear(); //only one compatible hypothesis allowed
529 //=============================================================================
531 * Is Propagation hypothesis assigned to theShape or its ancestors
533 //=============================================================================
534 Standard_Boolean StdMeshers_Regular_1D::IsPropagated (SMESH_Mesh & theMesh,
535 const TopoDS_Shape & theShape)
537 const SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
539 // try to find Propagation hypothesis on theShape
540 const list<const SMESHDS_Hypothesis*> & listHyp = meshDS->GetHypothesis(theShape);
542 list<const SMESHDS_Hypothesis*>::const_iterator it = listHyp.begin();
543 for (; it != listHyp.end(); it++) {
544 const SMESHDS_Hypothesis *anHyp = *it;
545 if (anHyp->GetName() == StdMeshers_Propagation::GetName())
546 return Standard_True;
549 // try to find Propagation hypothesis on ancestors
550 TopTools_ListIteratorOfListOfShape ancIt (theMesh.GetAncestors(theShape));
551 for (; ancIt.More(); ancIt.Next()) {
552 const TopoDS_Shape& ancestor = ancIt.Value();
553 const list<const SMESHDS_Hypothesis*> & listAncHyp = meshDS->GetHypothesis(ancestor);
555 list<const SMESHDS_Hypothesis*>::const_iterator itAnc = listAncHyp.begin();
556 for (; itAnc != listAncHyp.end(); itAnc++) {
557 const SMESHDS_Hypothesis *anHyp = *itAnc;
558 if (anHyp->GetName() == StdMeshers_Propagation::GetName())
559 return Standard_True;
563 return Standard_False;
566 //=============================================================================
568 * GetOppositeEdges() - get all edges of theShape,
569 * laying on any quadrangle face in front of theEdge
571 //=============================================================================
572 void StdMeshers_Regular_1D::GetOppositeEdges (const TopoDS_Shape& theShape,
573 const TopoDS_Shape& theEdge,
574 TopTools_ListOfShape& theOppositeEdges) const
576 TopExp_Explorer aWires (theShape, TopAbs_WIRE);
577 for (; aWires.More(); aWires.Next()) {
578 const TopoDS_Shape& aWire = aWires.Current();
579 BRepTools_WireExplorer aWE (TopoDS::Wire(aWire));
580 Standard_Integer nb = 1, found = 0;
581 TopTools_Array1OfShape anEdges (1,4);
582 for (; aWE.More(); aWE.Next(), nb++) {
587 anEdges(nb) = aWE.Current();
588 if (anEdges(nb).IsSame(theEdge))
591 if (nb == 5 && found > 0) {
592 Standard_Integer opp = found + 2;
593 if (opp > 4) opp -= 4;
594 theOppositeEdges.Append(anEdges(opp));
599 //=============================================================================
603 //=============================================================================
605 ostream & StdMeshers_Regular_1D::SaveTo(ostream & save)
610 //=============================================================================
614 //=============================================================================
616 istream & StdMeshers_Regular_1D::LoadFrom(istream & load)
621 //=============================================================================
625 //=============================================================================
627 ostream & operator <<(ostream & save, StdMeshers_Regular_1D & hyp)
629 return hyp.SaveTo( save );
632 //=============================================================================
636 //=============================================================================
638 istream & operator >>(istream & load, StdMeshers_Regular_1D & hyp)
640 return hyp.LoadFrom( load );