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 SMESH_subMesh *firstSubMesh = aMesh.GetSubMesh(VFirst);
225 const vector<int> & lidf
226 = firstSubMesh->GetSubMeshDS()->GetIDNodes();
227 int idFirst = lidf[0];
230 ASSERT(!VLast.IsNull());
231 SMESH_subMesh *lastSubMesh = aMesh.GetSubMesh(VLast);
232 const vector<int> & lidl
233 = lastSubMesh->GetSubMeshDS()->GetIDNodes();
234 int idLast = lidl[0];
239 GeomAdaptor_Curve C3d(Curve);
240 GCPnts_UniformAbscissa Discret(C3d, eltSize, f, l);
241 int NbPoints = Discret.NbPoints();
242 //MESSAGE("nb points on edge : "<<NbPoints);
244 // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
245 // only internal nodes receive an edge position with param on curve
247 int idPrev = idFirst;
248 for (int i = 2; i < NbPoints; i++)
250 double param = Discret.Parameter(i);
252 if (_numberOfSegments > 1)
254 double epsilon = 0.001;
255 if (fabs(_scaleFactor - 1.0) > epsilon)
258 pow(_scaleFactor, 1.0 / (_numberOfSegments - 1));
260 length * (1 - pow(alpha, i - 1)) / (1 - pow(alpha,
266 gp_Pnt P = Curve->Value(param);
268 //Add the Node in the DataStructure
269 //MESSAGE("point "<<nodeId<<" "<<P.X()<<" "<<P.Y()<<" "<<P.Z()<<" - "<<i<<" "<<param);
270 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
271 meshDS->SetNodeOnEdge(node, E);
273 // **** edgePosition associe au point = param.
274 SMDS_EdgePosition* epos=dynamic_cast<SMDS_EdgePosition *>(node->GetPosition());
275 epos->SetUParameter(param);
277 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node->GetID());
278 meshDS->SetMeshElementOnShape(edge, E);
279 idPrev = node->GetID();
281 SMDS_MeshEdge* edge = meshDS->AddEdge(idPrev, idLast);
282 meshDS->SetMeshElementOnShape(edge, E);
286 // MESSAGE ("Edge Degeneree non traitee --- arret");
288 if (BRep_Tool::Degenerated(E))
290 // Edge is a degenerated Edge : We put n = 5 points on the edge.
292 BRep_Tool::Range(E, f, l);
293 double du = (l - f) / (NbPoints - 1);
294 MESSAGE("************* Degenerated edge! *****************");
296 TopoDS_Vertex V1, V2;
297 TopExp::Vertices(E, V1, V2);
298 gp_Pnt P = BRep_Tool::Pnt(V1);
300 int idPrev = idFirst;
301 for (int i = 2; i < NbPoints; i++)
303 double param = f + (i - 1) * du;
304 SMDS_MeshNode * node = meshDS->AddNode(P.X(), P.Y(), P.Z());
305 meshDS->SetNodeOnEdge(node, E);
307 // Handle (SMDS_EdgePosition) epos
308 // = new SMDS_EdgePosition(theSubMesh->GetId(),param);
309 // node->SetPosition(epos);
310 SMDS_EdgePosition* epos
311 = dynamic_cast<SMDS_EdgePosition*>(node->GetPosition());
312 epos->SetUParameter(param);
314 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, node->GetID());
315 meshDS->SetMeshElementOnShape(edge, E);
316 idPrev = node->GetID();
318 SMDS_MeshEdge * edge = meshDS->AddEdge(idPrev, idLast);
319 meshDS->SetMeshElementOnShape(edge, E);