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 : SMESH_Regular_1D.cxx
25 // Author : Paul RASCLE, EDF
31 #include "SMESH_Regular_1D.hxx"
32 #include "SMESH_Gen.hxx"
33 #include "SMESH_Mesh.hxx"
35 #include "SMESH_LocalLength.hxx"
36 #include "SMESH_NumberOfSegments.hxx"
38 #include "SMDS_MeshElement.hxx"
39 #include "SMDS_MeshNode.hxx"
40 #include "SMDS_EdgePosition.hxx"
42 #include "utilities.h"
44 #include <TopoDS_Edge.hxx>
45 #include <TopoDS_Shape.hxx>
46 #include <GeomAdaptor_Curve.hxx>
47 #include <BRep_Tool.hxx>
48 #include <GCPnts_AbscissaPoint.hxx>
49 #include <GCPnts_UniformAbscissa.hxx>
54 //=============================================================================
58 //=============================================================================
60 SMESH_Regular_1D::SMESH_Regular_1D(int hypId, int studyId,
61 SMESH_Gen * gen):SMESH_1D_Algo(hypId, studyId, gen)
63 MESSAGE("SMESH_Regular_1D::SMESH_Regular_1D");
65 // _shapeType = TopAbs_EDGE;
66 _shapeType = (1 << TopAbs_EDGE);
67 _compatibleHypothesis.push_back("LocalLength");
68 _compatibleHypothesis.push_back("NumberOfSegments");
71 _numberOfSegments = 0;
72 _hypLocalLength = NULL;
73 _hypNumberOfSegments = NULL;
76 //=============================================================================
80 //=============================================================================
82 SMESH_Regular_1D::~SMESH_Regular_1D()
86 //=============================================================================
90 //=============================================================================
92 ostream & SMESH_Regular_1D::SaveTo(ostream & save)
97 //=============================================================================
101 //=============================================================================
103 istream & SMESH_Regular_1D::LoadFrom(istream & load)
105 return load >> (*this);
108 //=============================================================================
112 //=============================================================================
114 ostream & operator <<(ostream & save, SMESH_Regular_1D & hyp)
119 //=============================================================================
123 //=============================================================================
125 istream & operator >>(istream & load, SMESH_Regular_1D & hyp)
130 //=============================================================================
134 //=============================================================================
136 bool SMESH_Regular_1D::CheckHypothesis(SMESH_Mesh & aMesh,
137 const TopoDS_Shape & aShape)
139 //MESSAGE("SMESH_Regular_1D::CheckHypothesis");
141 list <const SMESHDS_Hypothesis * >::const_iterator itl;
142 const SMESHDS_Hypothesis *theHyp;
144 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
145 int nbHyp = hyps.size();
146 if (nbHyp != 1) return false; // only one compatible hypothesis allowed
151 string hypName = theHyp->GetName();
152 int hypId = theHyp->GetID();
157 if (hypName == "LocalLength")
159 _hypLocalLength = dynamic_cast <const SMESH_LocalLength * >(theHyp);
160 ASSERT(_hypLocalLength);
161 _localLength = _hypLocalLength->GetLength();
162 _numberOfSegments = 0;
166 if (hypName == "NumberOfSegments")
168 _hypNumberOfSegments =
169 dynamic_cast <const SMESH_NumberOfSegments * >(theHyp);
170 ASSERT(_hypNumberOfSegments);
171 _numberOfSegments = _hypNumberOfSegments->GetNumberOfSegments();
172 _scaleFactor = _hypNumberOfSegments->GetScaleFactor();
177 //SCRUTE(_localLength);
178 //SCRUTE(_numberOfSegments);
183 //=============================================================================
187 //=============================================================================
189 bool SMESH_Regular_1D::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
191 MESSAGE("SMESH_Regular_1D::Compute");
193 SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
194 SMESH_subMesh *theSubMesh = aMesh.GetSubMesh(aShape);
196 const TopoDS_Edge & EE = TopoDS::Edge(aShape);
197 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
200 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E, f, l);
202 TopoDS_Vertex VFirst, VLast;
203 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
205 double length = EdgeLength(E);
209 // if (_localLength > 0) eltSize = _localLength;
210 if (_localLength > 0)
212 double nbseg = ceil(length / _localLength); // integer sup
214 nbseg = 1; // degenerated edge
215 eltSize = length / nbseg;
219 ASSERT(_numberOfSegments > 0);
220 eltSize = length / _numberOfSegments;
223 ASSERT(!VFirst.IsNull());
224 SMDS_Iterator<const SMDS_MeshNode *> * lid=
225 aMesh.GetSubMesh(VFirst)->GetSubMeshDS()->GetNodes();
226 const SMDS_MeshNode * idFirst = lid->next();
229 ASSERT(!VLast.IsNull());
230 lid=aMesh.GetSubMesh(VLast)->GetSubMeshDS()->GetNodes();
231 const SMDS_MeshNode * idLast = lid->next();
236 GeomAdaptor_Curve C3d(Curve);
237 GCPnts_UniformAbscissa Discret(C3d, eltSize, f, l);
238 int NbPoints = Discret.NbPoints();
239 //MESSAGE("nb points on edge : "<<NbPoints);
241 // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
242 // only internal nodes receive an edge position with param on curve
244 const SMDS_MeshNode * idPrev = idFirst;
245 for (int i = 2; i < NbPoints; i++)
247 double param = Discret.Parameter(i);
249 if (_numberOfSegments > 1)
251 double epsilon = 0.001;
252 if (fabs(_scaleFactor - 1.0) > epsilon)
255 pow(_scaleFactor, 1.0 / (_numberOfSegments - 1));
257 length * (1 - pow(alpha, i - 1)) / (1 - pow(alpha,
263 gp_Pnt P = Curve->Value(param);
265 //Add the Node in the DataStructure
266 //MESSAGE("point "<<nodeId<<" "<<P.X()<<" "<<P.Y()<<" "<<P.Z()<<" - "<<i<<" "<<param);
267 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
268 meshDS->SetNodeOnEdge(node, E);
270 // **** edgePosition associe au point = param.
271 SMDS_EdgePosition* epos=dynamic_cast<SMDS_EdgePosition *>(node->GetPosition());
272 epos->SetUParameter(param);
274 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
275 meshDS->SetMeshElementOnShape(edge, E);
278 SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
279 meshDS->SetMeshElementOnShape(edge, E);
283 // MESSAGE ("Edge Degeneree non traitee --- arret");
285 if (BRep_Tool::Degenerated(E))
287 // Edge is a degenerated Edge : We put n = 5 points on the edge.
289 BRep_Tool::Range(E, f, l);
290 double du = (l - f) / (NbPoints - 1);
291 MESSAGE("************* Degenerated edge! *****************");
293 TopoDS_Vertex V1, V2;
294 TopExp::Vertices(E, V1, V2);
295 gp_Pnt P = BRep_Tool::Pnt(V1);
297 const SMDS_MeshNode * idPrev = idFirst;
298 for (int i = 2; i < NbPoints; i++)
300 double param = f + (i - 1) * du;
301 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
302 meshDS->SetNodeOnEdge(node, E);
304 // Handle (SMDS_EdgePosition) epos
305 // = new SMDS_EdgePosition(theSubMesh->GetId(),param);
306 // node->SetPosition(epos);
307 SMDS_EdgePosition* epos
308 = dynamic_cast<SMDS_EdgePosition*>(node->GetPosition());
309 epos->SetUParameter(param);
311 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node);
312 meshDS->SetMeshElementOnShape(edge, E);
315 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
316 meshDS->SetMeshElementOnShape(edge, E);