Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/smesh.git] / src / StdMeshers / StdMeshers_Regular_1D.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : StdMeshers_Regular_1D.cxx
25 //           Moved here from SMESH_Regular_1D.cxx
26 //  Author : Paul RASCLE, EDF
27 //  Module : SMESH
28 //  $Header$
29
30 using namespace std;
31
32 #include "StdMeshers_Regular_1D.hxx"
33 #include "SMESH_Gen.hxx"
34 #include "SMESH_Mesh.hxx"
35
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"
42
43 #include "SMDS_MeshElement.hxx"
44 #include "SMDS_MeshNode.hxx"
45 #include "SMDS_EdgePosition.hxx"
46 #include "SMESH_subMesh.hxx"
47
48 #include "utilities.h"
49
50 #include <BRep_Tool.hxx>
51 #include <BRepTools_WireExplorer.hxx>
52
53 #include <TopoDS_Edge.hxx>
54 #include <TopoDS_Shape.hxx>
55 #include <TopTools_Array1OfShape.hxx>
56 #include <TopTools_ListIteratorOfListOfShape.hxx>
57
58 #include <GeomAdaptor_Curve.hxx>
59 #include <GCPnts_AbscissaPoint.hxx>
60 #include <GCPnts_UniformAbscissa.hxx>
61 #include <GCPnts_UniformDeflection.hxx>
62
63 #include <Standard_ErrorHandler.hxx>
64 #include <Precision.hxx>
65
66 #include <string>
67 //#include <algorithm>
68
69 //=============================================================================
70 /*!
71  *
72  */
73 //=============================================================================
74
75 StdMeshers_Regular_1D::StdMeshers_Regular_1D(int hypId, int studyId,
76         SMESH_Gen * gen):SMESH_1D_Algo(hypId, studyId, gen)
77 {
78         MESSAGE("StdMeshers_Regular_1D::StdMeshers_Regular_1D");
79         _name = "Regular_1D";
80         _shapeType = (1 << TopAbs_EDGE);
81
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");
87 }
88
89 //=============================================================================
90 /*!
91  *
92  */
93 //=============================================================================
94
95 StdMeshers_Regular_1D::~StdMeshers_Regular_1D()
96 {
97 }
98
99 //=============================================================================
100 /*!
101  *
102  */
103 //=============================================================================
104
105 bool StdMeshers_Regular_1D::CheckHypothesis
106                          (SMESH_Mesh& aMesh,
107                           const TopoDS_Shape& aShape,
108                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
109 {
110   _hypType = NONE;
111
112   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
113   if (hyps.size() == 0)
114   {
115     aStatus = SMESH_Hypothesis::HYP_MISSING;
116     return false;  // can't work without a hypothesis
117   }
118
119   // use only the first hypothesis
120   const SMESHDS_Hypothesis *theHyp = hyps.front();
121
122   string hypName = theHyp->GetName();
123
124   if (hypName == "LocalLength")
125   {
126     const StdMeshers_LocalLength * hyp =
127       dynamic_cast <const StdMeshers_LocalLength * >(theHyp);
128     ASSERT(hyp);
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;
133   }
134
135   else if (hypName == "NumberOfSegments")
136   {
137     const StdMeshers_NumberOfSegments * hyp =
138       dynamic_cast <const StdMeshers_NumberOfSegments * >(theHyp);
139     ASSERT(hyp);
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;
145   }
146
147   else if (hypName == "Arithmetic1D")
148   {
149     const StdMeshers_Arithmetic1D * hyp =
150       dynamic_cast <const StdMeshers_Arithmetic1D * >(theHyp);
151     ASSERT(hyp);
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;
157   }
158
159   else if (hypName == "StartEndLength")
160   {
161     const StdMeshers_StartEndLength * hyp =
162       dynamic_cast <const StdMeshers_StartEndLength * >(theHyp);
163     ASSERT(hyp);
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;
169   }
170
171   else if (hypName == "Deflection1D")
172   {
173     const StdMeshers_Deflection1D * hyp =
174       dynamic_cast <const StdMeshers_Deflection1D * >(theHyp);
175     ASSERT(hyp);
176     _value[ DEFLECTION_IND ] = hyp->GetDeflection();
177     ASSERT( _value[ DEFLECTION_IND ] > 0 );
178     _hypType = DEFLECTION;
179     aStatus = SMESH_Hypothesis::HYP_OK;
180   }
181   else
182     aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
183
184   return ( _hypType != NONE );
185 }
186
187 //=============================================================================
188 /*!
189  *
190  */
191 //=============================================================================
192 bool StdMeshers_Regular_1D::computeInternalParameters(const TopoDS_Edge& theEdge,
193                                                       list<double> &     theParams) const
194 {
195   theParams.clear();
196
197   double f, l;
198   Handle(Geom_Curve) Curve = BRep_Tool::Curve(theEdge, f, l);
199   GeomAdaptor_Curve C3d(Curve);
200
201   double length = EdgeLength(theEdge);
202   //SCRUTE(length);
203
204   switch( _hypType )
205   {
206   case LOCAL_LENGTH:
207   case NB_SEGMENTS: {
208
209     double eltSize = 1;
210     if ( _hypType == LOCAL_LENGTH )
211     {
212       double nbseg = ceil(length / _value[ BEG_LENGTH_IND ]); // integer sup
213       if (nbseg <= 0)
214         nbseg = 1;                        // degenerated edge
215       eltSize = length / nbseg;
216     }
217     else
218     {
219       double epsilon = 0.001;
220       if (fabs(_value[ SCALE_FACTOR_IND ] - 1.0) > epsilon)
221       {
222         double alpha =
223           pow( _value[ SCALE_FACTOR_IND ], 1.0 / (_value[ NB_SEGMENTS_IND ] - 1));
224         double factor =
225           length / (1 - pow( alpha,_value[ NB_SEGMENTS_IND ]));
226
227         int i, NbPoints = (int) _value[ NB_SEGMENTS_IND ];
228         for ( i = 2; i < NbPoints; i++ )
229         {
230           double param = factor * (1 - pow(alpha, i - 1));
231           theParams.push_back( param );
232         }
233         return true;
234       }
235       else
236       {
237         eltSize = length / _value[ NB_SEGMENTS_IND ];
238       }
239     }
240
241     GCPnts_UniformAbscissa Discret(C3d, eltSize, f, l);
242     if ( !Discret.IsDone() )
243       return false;
244
245     int NbPoints = Discret.NbPoints();
246     for ( int i = 2; i < NbPoints; i++ )
247     {
248       double param = Discret.Parameter(i);
249       theParams.push_back( param );
250     }
251     return true;
252   }
253
254   case BEG_END_LENGTH: {
255
256     // geometric progression: SUM(n) = ( a1 - an * q ) / ( 1 - q ) = length
257
258     double a1 = _value[ BEG_LENGTH_IND ];
259     double an = _value[ END_LENGTH_IND ];
260     double q  = ( length - a1 ) / ( length - an );
261
262     double U1 = Min ( f, l );
263     double Un = Max ( f, l );
264     double param = U1;
265     double eltSize = a1;
266     while ( 1 ) {
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();
272       if ( param < Un )
273         theParams.push_back( param );
274       else
275         break;
276       eltSize *= q;
277     }
278     if ( a1 + an < length ) {
279       // compensate error
280       double Ln = GCPnts_AbscissaPoint::Length( C3d, theParams.back(), Un );
281       double dLn = an - Ln;
282       if ( dLn < 0.5 * an )
283         dLn = -dLn;
284       else {
285         theParams.pop_back();
286         Ln = GCPnts_AbscissaPoint::Length( C3d, theParams.back(), Un );
287         dLn = an - Ln;
288         if ( dLn < 0.5 * an )
289           dLn = -dLn;
290       }
291       double dUn = dLn * ( Un - U1 ) / length;
292 //       SCRUTE( Ln );
293 //       SCRUTE( dLn );
294 //       SCRUTE( dUn );
295       list<double>::reverse_iterator itU = theParams.rbegin();
296       int i, n = theParams.size();
297       for ( i = 1 ; i < n; itU++, i++ ) {
298         (*itU) += dUn;
299         dUn /= q;
300       }
301     }
302
303     return true;
304   }
305
306   case DEFLECTION: {
307
308     GCPnts_UniformDeflection Discret(C3d, _value[ DEFLECTION_IND ], true);
309     if ( !Discret.IsDone() )
310       return false;
311
312     int NbPoints = Discret.NbPoints();
313     for ( int i = 2; i < NbPoints; i++ )
314     {
315       double param = Discret.Parameter(i);
316       theParams.push_back( param );
317     }
318     return true;
319
320   }
321
322   case ARITHMETIC_1D: {
323         // arithmetic progression: SUM(n) = ( an - a1 + q ) * ( a1 + an ) / ( 2 * q ) = length
324
325     double a1 = _value[ BEG_LENGTH_IND ];
326     double an = _value[ END_LENGTH_IND ];
327
328     double nd = (2 * length) / (an + a1) - 1;
329     int n = int(nd);
330     if(n != nd)
331       n++;
332
333     double q = ((2 * length) / (n + 1) - 2 * a1) / n;
334     double U1 = Min ( f, l );
335     double Un = Max ( f, l );
336     double param = U1;
337     double eltSize = a1;
338
339     double L=0;
340     while ( 1 ) {
341       L+=eltSize;
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 );
349       }
350       else
351         break;
352       eltSize += q;
353     }
354
355     return true;
356   }
357
358   default:;
359   }
360
361   return false;
362 }
363
364 //=============================================================================
365 /*!
366  *
367  */
368 //=============================================================================
369
370 bool StdMeshers_Regular_1D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
371 {
372   MESSAGE("StdMeshers_Regular_1D::Compute");
373
374   if ( _hypType == NONE )
375     return false;
376
377   SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
378   aMesh.GetSubMesh(aShape);
379
380   const TopoDS_Edge & EE = TopoDS::Edge(aShape);
381   TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
382
383   double f, l;
384   Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
385
386   TopoDS_Vertex VFirst, VLast;
387   TopExp::Vertices(E, VFirst, VLast);   // Vfirst corresponds to f and Vlast to l
388
389   ASSERT(!VFirst.IsNull());
390   SMDS_NodeIteratorPtr lid= aMesh.GetSubMesh(VFirst)->GetSubMeshDS()->GetNodes();
391   if (!lid->more())
392   {
393     MESSAGE (" NO NODE BUILT ON VERTEX ");
394     return false;
395   }
396   const SMDS_MeshNode * idFirst = lid->next();
397
398   ASSERT(!VLast.IsNull());
399   lid=aMesh.GetSubMesh(VLast)->GetSubMeshDS()->GetNodes();
400   if (!lid->more())
401   {
402     MESSAGE (" NO NODE BUILT ON VERTEX ");
403     return false;
404   }
405   const SMDS_MeshNode * idLast = lid->next();
406
407   if (!Curve.IsNull())
408   {
409     list< double > params;
410     try {
411       if ( ! computeInternalParameters( E, params ))
412         return false;
413     }
414     catch ( Standard_Failure ) {
415       return false;
416     }
417
418     // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
419     // only internal nodes receive an edge position with param on curve
420
421     const SMDS_MeshNode * idPrev = idFirst;
422
423     for (list<double>::iterator itU = params.begin(); itU != params.end(); itU++)
424     {
425       double param = *itU;
426       gp_Pnt P = Curve->Value(param);
427
428       //Add the Node in the DataStructure
429       SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
430       meshDS->SetNodeOnEdge(node, E);
431
432       // **** edgePosition associe au point = param.
433       SMDS_EdgePosition* epos =
434         dynamic_cast<SMDS_EdgePosition *>(node->GetPosition().get());
435       epos->SetUParameter(param);
436
437       SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
438       meshDS->SetMeshElementOnShape(edge, E);
439       idPrev = node;
440     }
441     SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
442     meshDS->SetMeshElementOnShape(edge, E);
443   }
444   else
445   {
446     // Edge is a degenerated Edge : We put n = 5 points on the edge.
447     int NbPoints = 5;
448     BRep_Tool::Range(E, f, l);
449     double du = (l - f) / (NbPoints - 1);
450     //MESSAGE("************* Degenerated edge! *****************");
451
452     TopoDS_Vertex V1, V2;
453     TopExp::Vertices(E, V1, V2);
454     gp_Pnt P = BRep_Tool::Pnt(V1);
455
456     const SMDS_MeshNode * idPrev = idFirst;
457     for (int i = 2; i < NbPoints; i++)
458     {
459       double param = f + (i - 1) * du;
460       SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
461       meshDS->SetNodeOnEdge(node, E);
462
463       SMDS_EdgePosition* epos =
464         dynamic_cast<SMDS_EdgePosition*>(node->GetPosition().get());
465       epos->SetUParameter(param);
466
467       SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
468       meshDS->SetMeshElementOnShape(edge, E);
469       idPrev = node;
470     }
471     SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
472     meshDS->SetMeshElementOnShape(edge, E);
473   }
474   return true;
475 }
476
477 //=============================================================================
478 /*!
479  *  GetUsedHypothesis
480  */
481 //=============================================================================
482
483 const list <const SMESHDS_Hypothesis *> & StdMeshers_Regular_1D::GetUsedHypothesis
484   (SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
485 {
486   _usedHypList.clear();
487   _usedHypList = GetAppliedHypothesis(aMesh, aShape); // copy
488   int nbHyp = _usedHypList.size();
489
490   // try to find being propagated hypothesis
491   string propName = StdMeshers_Propagation::GetName();
492   if (nbHyp == 0) {
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();
500
501       // Find Propagation hypothesis on the opposite edge
502       if (IsPropagated(aMesh, oppE)) {
503
504         // Get hypothesis, used by the opposite edge
505         _usedHypList = SMESH_Algo::GetUsedHypothesis(aMesh, oppE);
506         nbHyp = _usedHypList.size();
507         if (nbHyp == 1)
508           break;
509       }
510     }
511   }
512
513   // try to find relevant 1D hypothesis on ancestors
514   if (nbHyp == 0) {
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();
520       if (nbHyp == 1)
521         break;
522     }
523   }
524
525   if (nbHyp > 1)
526     _usedHypList.clear(); //only one compatible hypothesis allowed
527   return _usedHypList;
528 }
529
530 //=============================================================================
531 /*!
532  *  Is Propagation hypothesis assigned to theShape or its ancestors
533  */
534 //=============================================================================
535 Standard_Boolean StdMeshers_Regular_1D::IsPropagated (SMESH_Mesh         & theMesh,
536                                                       const TopoDS_Shape & theShape)
537 {
538   const SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
539
540   // try to find Propagation hypothesis on theShape
541   const list<const SMESHDS_Hypothesis*> & listHyp = meshDS->GetHypothesis(theShape);
542
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;
548   }
549
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);
555
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;
561     }
562   }
563
564   return Standard_False;
565 }
566
567 //=============================================================================
568 /*!
569  * GetOppositeEdges() - get all edges of theShape,
570  * laying on any quadrangle face in front of theEdge
571  */
572 //=============================================================================
573 void StdMeshers_Regular_1D::GetOppositeEdges (const TopoDS_Shape&   theShape,
574                                               const TopoDS_Shape&   theEdge,
575                                               TopTools_ListOfShape& theOppositeEdges) const
576 {
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++) {
584       if (nb > 4) {
585         found = 0;
586         break;
587       }
588       anEdges(nb) = aWE.Current();
589       if (anEdges(nb).IsSame(theEdge))
590         found = nb;
591     }
592     if (nb == 5 && found > 0) {
593       Standard_Integer opp = found + 2;
594       if (opp > 4) opp -= 4;
595       theOppositeEdges.Append(anEdges(opp));
596     }
597   }
598 }
599
600 //=============================================================================
601 /*!
602  *
603  */
604 //=============================================================================
605
606 ostream & StdMeshers_Regular_1D::SaveTo(ostream & save)
607 {
608   return save;
609 }
610
611 //=============================================================================
612 /*!
613  *
614  */
615 //=============================================================================
616
617 istream & StdMeshers_Regular_1D::LoadFrom(istream & load)
618 {
619   return load;
620 }
621
622 //=============================================================================
623 /*!
624  *
625  */
626 //=============================================================================
627
628 ostream & operator <<(ostream & save, StdMeshers_Regular_1D & hyp)
629 {
630   return hyp.SaveTo( save );
631 }
632
633 //=============================================================================
634 /*!
635  *
636  */
637 //=============================================================================
638
639 istream & operator >>(istream & load, StdMeshers_Regular_1D & hyp)
640 {
641   return hyp.LoadFrom( load );
642 }