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"
39 #include "SMDS_MeshElement.hxx"
40 #include "SMDS_MeshNode.hxx"
41 #include "SMDS_EdgePosition.hxx"
43 #include "utilities.h"
45 #include <TopoDS_Edge.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <GeomAdaptor_Curve.hxx>
48 #include <BRep_Tool.hxx>
49 #include <GCPnts_AbscissaPoint.hxx>
50 #include <GCPnts_UniformAbscissa.hxx>
55 //=============================================================================
59 //=============================================================================
61 StdMeshers_Regular_1D::StdMeshers_Regular_1D(int hypId, int studyId,
62 SMESH_Gen * gen):SMESH_1D_Algo(hypId, studyId, gen)
64 MESSAGE("StdMeshers_Regular_1D::StdMeshers_Regular_1D");
66 // _shapeType = TopAbs_EDGE;
67 _shapeType = (1 << TopAbs_EDGE);
68 _compatibleHypothesis.push_back("LocalLength");
69 _compatibleHypothesis.push_back("NumberOfSegments");
72 _numberOfSegments = 0;
73 _hypLocalLength = NULL;
74 _hypNumberOfSegments = NULL;
77 //=============================================================================
81 //=============================================================================
83 StdMeshers_Regular_1D::~StdMeshers_Regular_1D()
87 //=============================================================================
91 //=============================================================================
93 bool StdMeshers_Regular_1D::CheckHypothesis
95 const TopoDS_Shape& aShape,
96 SMESH_Hypothesis::Hypothesis_Status& aStatus)
98 //MESSAGE("StdMeshers_Regular_1D::CheckHypothesis");
100 list <const SMESHDS_Hypothesis * >::const_iterator itl;
101 const SMESHDS_Hypothesis *theHyp;
103 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
104 int nbHyp = hyps.size();
107 aStatus = SMESH_Hypothesis::HYP_MISSING;
108 return false; // can't work with no hypothesis
112 theHyp = (*itl); // use only the first hypothesis
114 string hypName = theHyp->GetName();
115 int hypId = theHyp->GetID();
120 if (hypName == "LocalLength")
122 _hypLocalLength = dynamic_cast <const StdMeshers_LocalLength * >(theHyp);
123 ASSERT(_hypLocalLength);
124 _localLength = _hypLocalLength->GetLength();
125 _numberOfSegments = 0;
127 aStatus = SMESH_Hypothesis::HYP_OK;
130 else if (hypName == "NumberOfSegments")
132 _hypNumberOfSegments =
133 dynamic_cast <const StdMeshers_NumberOfSegments * >(theHyp);
134 ASSERT(_hypNumberOfSegments);
135 _numberOfSegments = _hypNumberOfSegments->GetNumberOfSegments();
136 _scaleFactor = _hypNumberOfSegments->GetScaleFactor();
139 aStatus = SMESH_Hypothesis::HYP_OK;
142 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
144 //SCRUTE(_localLength);
145 //SCRUTE(_numberOfSegments);
150 //=============================================================================
154 //=============================================================================
156 bool StdMeshers_Regular_1D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
158 MESSAGE("StdMeshers_Regular_1D::Compute");
160 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
161 SMESH_subMesh *theSubMesh = aMesh.GetSubMesh(aShape);
163 const TopoDS_Edge & EE = TopoDS::Edge(aShape);
164 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
167 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
169 TopoDS_Vertex VFirst, VLast;
170 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
172 double length = EdgeLength(E);
176 // if (_localLength > 0) eltSize = _localLength;
177 if (_localLength > 0)
179 double nbseg = ceil(length / _localLength); // integer sup
181 nbseg = 1; // degenerated edge
182 eltSize = length / nbseg;
186 ASSERT(_numberOfSegments > 0);
187 eltSize = length / _numberOfSegments;
190 ASSERT(!VFirst.IsNull());
191 SMDS_NodeIteratorPtr lid= aMesh.GetSubMesh(VFirst)->GetSubMeshDS()->GetNodes();
192 const SMDS_MeshNode * idFirst = lid->next();
194 ASSERT(!VLast.IsNull());
195 lid=aMesh.GetSubMesh(VLast)->GetSubMeshDS()->GetNodes();
196 const SMDS_MeshNode * idLast = lid->next();
200 GeomAdaptor_Curve C3d(Curve);
201 GCPnts_UniformAbscissa Discret(C3d, eltSize, f, l);
202 int NbPoints = Discret.NbPoints();
203 //MESSAGE("nb points on edge : "<<NbPoints);
205 // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
206 // only internal nodes receive an edge position with param on curve
208 const SMDS_MeshNode * idPrev = idFirst;
209 for (int i = 2; i < NbPoints; i++)
211 double param = Discret.Parameter(i);
213 if (_numberOfSegments > 1)
215 double epsilon = 0.001;
216 if (fabs(_scaleFactor - 1.0) > epsilon)
219 pow(_scaleFactor, 1.0 / (_numberOfSegments - 1));
221 length * (1 - pow(alpha, i - 1)) / (1 - pow(alpha,
227 gp_Pnt P = Curve->Value(param);
229 //Add the Node in the DataStructure
230 //MESSAGE("point "<<nodeId<<" "<<P.X()<<" "<<P.Y()<<" "<<P.Z()<<" - "<<i<<" "<<param);
231 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
232 meshDS->SetNodeOnEdge(node, E);
234 // **** edgePosition associe au point = param.
235 SMDS_EdgePosition* epos =
236 dynamic_cast<SMDS_EdgePosition *>(node->GetPosition().get());
237 epos->SetUParameter(param);
239 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
240 meshDS->SetMeshElementOnShape(edge, E);
243 SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
244 meshDS->SetMeshElementOnShape(edge, E);
248 // MESSAGE ("Edge Degeneree non traitee --- arret");
250 if (BRep_Tool::Degenerated(E))
252 // Edge is a degenerated Edge : We put n = 5 points on the edge.
254 BRep_Tool::Range(E, f, l);
255 double du = (l - f) / (NbPoints - 1);
256 MESSAGE("************* Degenerated edge! *****************");
258 TopoDS_Vertex V1, V2;
259 TopExp::Vertices(E, V1, V2);
260 gp_Pnt P = BRep_Tool::Pnt(V1);
262 const SMDS_MeshNode * idPrev = idFirst;
263 for (int i = 2; i < NbPoints; i++)
265 double param = f + (i - 1) * du;
266 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
267 meshDS->SetNodeOnEdge(node, E);
269 // Handle (SMDS_EdgePosition) epos
270 // = new SMDS_EdgePosition(theSubMesh->GetId(),param);
271 // node->SetPosition(epos);
272 SMDS_EdgePosition* epos =
273 dynamic_cast<SMDS_EdgePosition*>(node->GetPosition().get());
274 epos->SetUParameter(param);
276 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
277 meshDS->SetMeshElementOnShape(edge, E);
280 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
281 meshDS->SetMeshElementOnShape(edge, E);
289 //=============================================================================
293 //=============================================================================
295 ostream & StdMeshers_Regular_1D::SaveTo(ostream & save)
300 //=============================================================================
304 //=============================================================================
306 istream & StdMeshers_Regular_1D::LoadFrom(istream & load)
311 //=============================================================================
315 //=============================================================================
317 ostream & operator <<(ostream & save, StdMeshers_Regular_1D & hyp)
319 return hyp.SaveTo( save );
322 //=============================================================================
326 //=============================================================================
328 istream & operator >>(istream & load, StdMeshers_Regular_1D & hyp)
330 return hyp.LoadFrom( load );