2 // CC++ : Implementation Cascade des lignes
4 // Copyright (C) 2009-2013 CEA/DEN, EDF R&D
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HexKasLine.hxx"
24 #include "HexKasPoint.hxx"
25 #include "HexKas_functions.hxx"
29 #include <BRepTools.hxx>
30 #include <BRep_Builder.hxx>
31 #include <BRepAdaptor_Curve.hxx>
32 #include <BRepBuilderAPI_Transform.hxx>
33 #include <BRepBuilderAPI_MakeVertex.hxx>
35 #include <GCPnts_AbscissaPoint.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Compound.hxx>
41 #include <TopoDS_Vertex.hxx>
49 #include "HexVertex.hxx"
50 #include "HexEdge.hxx"
51 #include "HexOldShape.hxx"
52 #include "HexDiagnostics.hxx"
54 #include <GeomAPI_ProjectPointOnCurve.hxx>
58 static bool db = on_debug ();
59 static const double TolAsso = 1e-2; // Tolerance pour les associations
61 void arrondir (double &val);
63 // ======================================================= Constructeur
74 // geom_line = TopoDS::Edge(shape);
77 geom_total_length = 0;
78 for (int nc=0; nc <DIM3 ; nc++) start_coord [nc] = end_coord [nc] = 0;
80 // ========================================================= Constructeur bis
81 KasLine::KasLine (Shape* asso, double deb, double fin)
85 defineLine (asso, deb, fin);
87 // ========================================================= Destructeur
92 // ========================================================= defineLine
93 // === Creation de la quincaillerie associee a une shape
94 void KasLine::defineLine (Shape* asso, double deb, double fin)
96 lig_brep = asso->getBrep ();
97 lig_ident = asso->getIdent ();
98 lig_ior = asso->getIor ();
102 lig_debut = asso->getStart();;
103 lig_fin = asso->getEnd();;
105 else if (deb >= UnEpsil)
116 geom_inverse = false;
120 istringstream streamBrep (lig_brep);
121 BRep_Builder aBuilder;
124 BRepTools::Read(topo, streamBrep, aBuilder);
125 geom_line = TopoDS::Edge(topo);
127 geom_curve = new BRepAdaptor_Curve (geom_line);
128 // Longueur de la ligne
129 double umin = 0, umax = 0;
131 Handle(Geom_Curve) handle = BRep_Tool::Curve (geom_line, loc, umin, umax);
132 GeomAdaptor_Curve AdaptCurve (handle);
133 geom_total_length = GCPnts_AbscissaPoint::Length(AdaptCurve, umin, umax);
134 geom_length = geom_total_length * fabs (lig_fin-lig_debut);
137 GCPnts_AbscissaPoint s1 (*geom_curve, geom_total_length*lig_debut,
138 geom_curve->FirstParameter());
139 GCPnts_AbscissaPoint s2 (*geom_curve, geom_total_length*lig_fin,
140 geom_curve->FirstParameter());
142 par_mini = s1.Parameter ();
143 par_maxi = s2.Parameter ();
144 start_gpoint = geom_curve->Value (par_mini);
145 end_gpoint = geom_curve->Value (par_maxi);
150 Echo (" ____________________________________ KasLine::defineLine");
153 HexDisplay (lig_debut);
154 HexDisplay (lig_fin);
155 HexDisplay (geom_total_length);
156 HexDisplay (geom_curve->FirstParameter());
157 HexDisplay (s1.Parameter());
158 HexDisplay (s2.Parameter());
160 PutCoord (start_coord);
161 PutCoord (end_coord);
164 // ========================================================= assoPoint
165 void KasLine::assoPoint (double abscis, Vertex* node)
167 GCPnts_AbscissaPoint s1 (*geom_curve, abscis,
168 geom_curve->FirstParameter());
169 double gparam = s1.Parameter ();
170 gp_Pnt pnt_asso = geom_curve->Value (gparam);
173 gpoint.definePoint (pnt_asso);
174 gpoint.associate (node);
178 double* coord = gpoint.getCoord();
180 if (node->definedBy (coord[dir_x], coord[dir_y], coord[dir_z]))
182 printf (" ... assoPoint%c: v=%s (%g,%g,%g), p=%g", car,
183 node->getName(), node->getX(), node->getY(), node->getZ(), abscis);
185 printf (" -> (%g,%g,%g)\n", coord[dir_x], coord[dir_y], coord[dir_z]);
188 // ========================================================= associate
189 void KasLine::associate (Edge* edge, double sm1, double sm2, int vorig)
191 if (sm2 < start_absc) return;
192 if (sm1 > end_absc) return;
194 Vertex* segment[V_TWO] = { edge->getVertex (vorig),
195 edge->getVertex (1-vorig) };
197 double vpara1 = lig_debut + (sm1-start_absc)/geom_total_length;
198 double vpara2 = lig_debut + (sm2-start_absc)/geom_total_length;
201 vpara2 = lig_fin - (sm1-start_absc)/geom_total_length;
202 vpara1 = lig_fin - (sm2-start_absc)/geom_total_length;
205 double lpara1 = std::max (lig_debut, std::min (lig_fin, vpara1));
206 double lpara2 = std::max (lig_debut, std::min (lig_fin, vpara2));
208 arrondir (vpara1); arrondir (vpara2);
209 arrondir (lpara1); arrondir (lpara2);
213 cout << " ++ KasLine::associate : rg=" << geom_rang << "s=" << vorig
215 cout << " ligpara = [ " << lig_debut << ", " << lig_fin << " ]" << endl;
216 cout << " absc = [ " << start_absc << ", " << end_absc << " ]\n" ;
218 cout << " Edge = " << edge->getName() <<
219 " = [ " << segment[0]->getName() << ", "
220 << "," << segment[1]->getName() << endl;
221 cout << " smx = [ " << sm1 << ", " << sm2 << " ]" << endl;
222 cout << " vparam = [ " << vpara1 << ", " << vpara2 << " ]" << endl;
223 cout << " lparam = [ " << lpara1 << ", " << lpara2 << " ]" << endl;
226 if (lpara2 >= lpara1 + TolAsso)
228 assoEdge (edge, lpara1, lpara2);
232 printf (" Asso Line refusee %s -> (%g,%g)\n", edge->getName(),
236 // ---------------Association du vertex
237 double hparam = geom_inverse ? vpara2 : vpara1;
239 double absc1 = start_absc - TolAsso*geom_total_length;
240 double absc2 = end_absc + TolAsso*geom_total_length;
242 for (int nx=V_AMONT ; nx<=V_AVAL ; nx++)
244 if (smx >= absc1 && smx <= absc2)
246 Vertex* node = segment [nx];
247 if (node->getAssociation()==NULL)
249 // ..... Coordonnees du point
251 double abscis = geom_total_length*hparam;
252 GCPnts_AbscissaPoint s1 (*geom_curve, abscis,
253 geom_curve->FirstParameter());
254 double gparam = s1.Parameter ();
255 gp_Pnt pnt_asso = geom_curve->Value (gparam);
257 // ..... Creation d'un vertex Geom
259 gpoint.definePoint (pnt_asso);
260 gpoint.associate (node);
263 double* ass = gpoint.getCoord();
264 printf (" Asso Point %s (%g,%g,%g) -> (%g,%g,%g) p=%g s=%g\n",
265 node->getName(), node->getX(), node->getY(), node->getZ(),
266 ass[dir_x], ass[dir_y], ass[dir_z], hparam, smx);
271 double abscis = geom_total_length*hparam;
272 GCPnts_AbscissaPoint s1 (*geom_curve, abscis,
273 geom_curve->FirstParameter());
274 double gparam = s1.Parameter ();
275 gp_Pnt pnt_asso = geom_curve->Value (gparam);
277 // ..... Creation d'un vertex Geom
279 gpoint.definePoint (pnt_asso);
280 double* ass = gpoint.getCoord();
281 // gpoint.associate (node);
282 printf (" Asso Point %s (%g,%g,%g) -> (%g,%g,%g) p=%g s=%g\n",
284 node->getX(), node->getY(), node->getZ(),
285 ass[dir_x], ass[dir_y], ass[dir_z], hparam, smx);
287 gpoint.definePoint (node);
288 ass = gpoint.getCoord();
289 printf (" ignore car deja associe a (%g,%g, %g)\n",
290 ass[dir_x], ass[dir_y], ass[dir_z]);
293 hparam = geom_inverse ? vpara1 : vpara2;
297 // ========================================================= assoEdge
298 void KasLine::assoEdge (Edge* edge, double para1, double para2, int vass)
300 Shape* shape = new Shape (lig_brep);
301 shape->setIdent (lig_ident);
302 shape->setIor (lig_ior);
303 shape->setBounds (para1, para2);
305 edge ->addAssociation (shape);
307 if (db) printf (" ... Asso Edge %s -> (%g,%g)\n",
308 edge->getName(), para1, para2);
310 double lg = geom_total_length;
314 assoPoint (para1*lg, edge->getVertex (V_AMONT));
317 assoPoint (para2*lg, edge->getVertex (V_AVAL));
320 assoPoint (para1*lg, edge->getVertex (V_AMONT));
321 assoPoint (para2*lg, edge->getVertex (V_AVAL ));
326 // ========================================================= majCoord
327 void KasLine::majCoord ()
329 start_coord [dir_x] = start_gpoint.X();
330 start_coord [dir_y] = start_gpoint.Y();
331 start_coord [dir_z] = start_gpoint.Z();
333 end_coord [dir_x] = end_gpoint.X();
334 end_coord [dir_y] = end_gpoint.Y();
335 end_coord [dir_z] = end_gpoint.Z();
337 // ========================================================= setBounds
338 void KasLine::setBounds (double deb, double fin)
343 // ========================================================= inverser
344 void KasLine::inverser ()
346 gp_Pnt foo = start_gpoint;
347 start_gpoint = end_gpoint;
350 // Prendre le complementaire de l'intercvale initial
351 if (lig_debut > Epsil && lig_debut < UnEpsil)
356 cout << " ... inverser : fin = debut = " << lig_fin << endl;
358 else if (lig_fin > Epsil && lig_fin < UnEpsil)
363 cout << " ... inverser : debut = fin = " << lig_debut << endl;
366 geom_inverse = NOT geom_inverse;
369 // ========================================================= setRank
370 void KasLine::setRank (int nro, int sens, double& abscisse)
376 start_absc = abscisse;
377 abscisse = end_absc = start_absc + (lig_fin-lig_debut) * geom_total_length;
381 cout << "KasLine::setRank : nro = " << nro << " sens=" << sens
382 << " = (" << start_absc << ", " << end_absc << ")" << endl;
385 // ========================================================= findBound
386 int KasLine::findBound (double* coord)
388 if (same_coords (coord, start_coord) )
391 if (same_coords (coord, end_coord) )
396 // ========================================================= findParam
397 double KasLine::findParam (double* coord)
399 double umin = 0, umax = 0;
400 gp_Pnt gpoint (coord[dir_x], coord[dir_y], coord[dir_z]);
401 Handle(Geom_Curve) handle = BRep_Tool::Curve (geom_line, umin, umax);
403 GeomAPI_ProjectPointOnCurve projector (gpoint, handle);
404 if ( projector.NbPoints() == 0 )
407 double gparam = projector.LowerDistanceParameter();
408 if (gparam <par_mini || gparam>par_maxi)
410 // cout << " Rejet : " << gparam << " not in (" << par_mini
411 // << ", " << par_maxi << ")" << endl;
415 gp_Pnt rpoint = geom_curve->Value (gparam);
416 if (NOT same_coords (gpoint, rpoint))
418 // cout << " Rejet : points differents " << endl;
423 GeomAdaptor_Curve adapt_curve (handle);
424 double abscis = GCPnts_AbscissaPoint::Length (adapt_curve, umin, gparam);
425 double hparam = abscis/geom_total_length;
427 // gparam = (gparam-par_mini) / (par_maxi-par_mini);
430 // ========================================================= arrondir
431 void arrondir (double &val)
433 if (val >= -TolAsso && val <= TolAsso)
437 else if (val >= 1.0-TolAsso && val <= 1.0+TolAsso)