2 //=============================================================================
3 // File : SMESH_Regular_1D.cxx
4 // Created : sam mai 18 08:11:58 CEST 2002
5 // Author : Paul RASCLE, EDF
7 // Copyright : EDF 2002
9 //=============================================================================
12 #include "SMESH_Regular_1D.hxx"
13 #include "SMESH_Gen.hxx"
14 #include "SMESH_Mesh.hxx"
16 #include "SMESH_LocalLength.hxx"
17 #include "SMESH_NumberOfSegments.hxx"
19 #include "SMESHDS_ListOfPtrHypothesis.hxx"
20 #include "SMESHDS_ListIteratorOfListOfPtrHypothesis.hxx"
21 #include "SMDS_MeshElement.hxx"
22 #include "SMDS_MeshNode.hxx"
23 #include "SMDS_EdgePosition.hxx"
25 #include "utilities.h"
27 #include <TopoDS_Edge.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <GeomAdaptor_Curve.hxx>
30 #include <BRep_Tool.hxx>
31 #include <GCPnts_AbscissaPoint.hxx>
32 #include <GCPnts_UniformAbscissa.hxx>
37 //=============================================================================
41 //=============================================================================
43 SMESH_Regular_1D::SMESH_Regular_1D(int hypId, int studyId, SMESH_Gen* gen)
44 : SMESH_1D_Algo(hypId, studyId, gen)
46 MESSAGE("SMESH_Regular_1D::SMESH_Regular_1D");
48 // _shapeType = TopAbs_EDGE;
49 _shapeType = (1<<TopAbs_EDGE);
50 _compatibleHypothesis.push_back("LocalLength");
51 _compatibleHypothesis.push_back("NumberOfSegments");
54 _numberOfSegments = 0;
55 _hypLocalLength = NULL;
56 _hypNumberOfSegments = NULL;
59 //=============================================================================
63 //=============================================================================
65 SMESH_Regular_1D::~SMESH_Regular_1D()
69 //=============================================================================
73 //=============================================================================
75 ostream & SMESH_Regular_1D::SaveTo(ostream & save)
80 //=============================================================================
84 //=============================================================================
86 istream & SMESH_Regular_1D::LoadFrom(istream & load)
88 return load >> (*this);
91 //=============================================================================
95 //=============================================================================
97 ostream& operator << (ostream & save, SMESH_Regular_1D & hyp)
102 //=============================================================================
106 //=============================================================================
108 istream& operator >> (istream & load, SMESH_Regular_1D & hyp)
113 //=============================================================================
117 //=============================================================================
119 bool SMESH_Regular_1D::CheckHypothesis(SMESH_Mesh& aMesh,
120 const TopoDS_Shape& aShape)
122 //MESSAGE("SMESH_Regular_1D::CheckHypothesis");
124 list<SMESHDS_Hypothesis*>::const_iterator itl;
125 SMESHDS_Hypothesis* theHyp;
127 const list<SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape);
128 int nbHyp = hyps.size();
129 if (nbHyp != 1) return false; // only one compatible hypothesis allowed
134 string hypName = theHyp->GetName();
135 int hypId = theHyp->GetID();
140 if (hypName == "LocalLength")
142 _hypLocalLength = dynamic_cast<SMESH_LocalLength*> (theHyp);
143 ASSERT(_hypLocalLength);
144 _localLength = _hypLocalLength->GetLength();
145 _numberOfSegments = 0;
149 if (hypName == "NumberOfSegments")
151 _hypNumberOfSegments = dynamic_cast<SMESH_NumberOfSegments*> (theHyp);
152 ASSERT(_hypNumberOfSegments);
153 _numberOfSegments = _hypNumberOfSegments->GetNumberOfSegments();
158 //SCRUTE(_localLength);
159 //SCRUTE(_numberOfSegments);
164 //=============================================================================
168 //=============================================================================
170 bool SMESH_Regular_1D::Compute(SMESH_Mesh& aMesh,
171 const TopoDS_Shape& aShape)
173 //MESSAGE("SMESH_Regular_1D::Compute");
175 const Handle(SMESHDS_Mesh)& meshDS = aMesh.GetMeshDS();
176 SMESH_subMesh* theSubMesh = aMesh.GetSubMesh(aShape);
178 const TopoDS_Edge& EE = TopoDS::Edge(aShape);
179 TopoDS_Edge E = TopoDS::Edge(EE.Oriented(TopAbs_FORWARD));
182 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E,f,l);
184 TopoDS_Vertex VFirst, VLast;
185 TopExp::Vertices(E, VFirst, VLast); // Vfirst corresponds to f and Vlast to l
187 double length = EdgeLength(E);
191 // if (_localLength > 0) eltSize = _localLength;
192 if (_localLength > 0)
194 double nbseg = ceil(length/_localLength); // integer sup
195 if (nbseg <=0) nbseg = 1; // degenerated edge
196 eltSize = length/nbseg;
200 ASSERT(_numberOfSegments> 0);
201 eltSize = length/_numberOfSegments;
204 ASSERT(!VFirst.IsNull());
205 SMESH_subMesh* firstSubMesh = aMesh.GetSubMesh(VFirst);
206 const TColStd_ListOfInteger& lidf
207 = firstSubMesh->GetSubMeshDS()->GetIDNodes();
208 int idFirst= lidf.First();
211 ASSERT(!VLast.IsNull());
212 SMESH_subMesh* lastSubMesh = aMesh.GetSubMesh(VLast);
213 const TColStd_ListOfInteger& lidl
214 = lastSubMesh->GetSubMeshDS()->GetIDNodes();
215 int idLast= lidl.First();
220 GeomAdaptor_Curve C3d(Curve);
221 GCPnts_UniformAbscissa Discret(C3d,eltSize,f,l);
222 int NbPoints = Discret.NbPoints();
223 //MESSAGE("nb points on edge : "<<NbPoints);
225 // edge extrema (indexes : 1 & NbPoints) already in SMDS (TopoDS_Vertex)
226 // only internal nodes receive an edge position with param on curve
228 int idPrev = idFirst;
229 for (int i=2; i<NbPoints; i++)
231 double param = Discret.Parameter(i);
232 gp_Pnt P = Curve->Value(param);
234 //Add the Node in the DataStructure
235 int nodeId = meshDS->AddNode(P.X(), P.Y(), P.Z());
236 //MESSAGE("point "<<nodeId<<" "<<P.X()<<" "<<P.Y()<<" "<<P.Z()<<" - "<<i<<" "<<param);
237 Handle (SMDS_MeshElement) elt = meshDS->FindNode(nodeId);
238 Handle (SMDS_MeshNode) node = meshDS->GetNode(1, elt);
239 meshDS->SetNodeOnEdge(node, E);
241 // **** edgePosition associe au point = param.
242 // Handle (SMDS_EdgePosition) epos
243 // = new SMDS_EdgePosition(theSubMesh->GetId(),param); // non, deja cree
244 // node->SetPosition(epos);
245 Handle (SMDS_EdgePosition) epos
246 = Handle (SMDS_EdgePosition)::DownCast(node->GetPosition());
247 epos->SetUParameter(param);
249 int edgeId = meshDS->AddEdge(idPrev, nodeId);
250 elt = meshDS->FindElement(edgeId);
251 meshDS->SetMeshElementOnShape(elt, E);
254 int edgeId = meshDS->AddEdge(idPrev, idLast);
255 Handle (SMDS_MeshElement) elt = meshDS->FindElement(edgeId);
256 meshDS->SetMeshElementOnShape(elt, E);
260 // MESSAGE ("Edge Degeneree non traitee --- arret");
262 if (BRep_Tool::Degenerated(E))
264 // Edge is a degenerated Edge : We put n = 5 points on the edge.
266 BRep_Tool::Range(E,f,l);
267 double du = (l-f)/(NbPoints-1);
268 MESSAGE("************* Degenerated edge! *****************");
271 TopExp::Vertices (E,V1,V2);
272 gp_Pnt P = BRep_Tool::Pnt(V1);
274 int idPrev = idFirst;
275 for (int i=2; i<NbPoints; i++)
277 double param = f + (i-1)*du;
278 //Add the Node in the DataStructure
279 int nodeId = meshDS->AddNode(P.X(), P.Y(), P.Z());
280 //MESSAGE("point "<<nodeId<<" "<<P.X()<<" "<<P.Y()<<" "<<P.Z()<<" - "<<i<<" "<<param);
282 Handle (SMDS_MeshElement) elt = meshDS->FindNode(nodeId);
283 Handle (SMDS_MeshNode) node = meshDS->GetNode(1, elt);
284 meshDS->SetNodeOnEdge(node, E);
286 // Handle (SMDS_EdgePosition) epos
287 // = new SMDS_EdgePosition(theSubMesh->GetId(),param);
288 // node->SetPosition(epos);
289 Handle (SMDS_EdgePosition) epos
290 = Handle (SMDS_EdgePosition)::DownCast(node->GetPosition());
291 epos->SetUParameter(param);
293 int edgeId = meshDS->AddEdge(idPrev, nodeId);
294 elt = meshDS->FindElement(edgeId);
295 meshDS->SetMeshElementOnShape(elt, E);
298 int edgeId = meshDS->AddEdge(idPrev, idLast);
299 Handle (SMDS_MeshElement) elt = meshDS->FindElement(edgeId);
300 meshDS->SetMeshElementOnShape(elt, E);