Salome HOME
Updated copyright comment
[modules/hexablock.git] / src / HEXABLOCK / HexEdgeShape.cxx
1
2 // C++ : Gestion des soous-shapes
3
4 // Copyright (C) 2009-2024  CEA, EDF
5 //
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, or (at your option) any later version.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
24
25 #include "HexEdgeShape.hxx"
26
27 #include <TopoDS.hxx>
28 #include <TopoDS_Edge.hxx>
29 #include <gp_Pnt.hxx>
30
31 #include <BRep_Builder.hxx>
32 #include <BRepAdaptor_Curve.hxx>
33
34 #include <GCPnts_AbscissaPoint.hxx>
35
36 #include <GeomAPI_ProjectPointOnCurve.hxx>
37 #include <Geom_Circle.hxx>
38
39 /****************
40 #include <Standard_Stream.hxx>
41 #include <GEOMImpl_IMeasureOperations.hxx>
42 #include <GEOMImpl_Types.hxx>
43 #include <GEOMImpl_MeasureDriver.hxx>
44 #include <GEOMImpl_IMeasure.hxx>
45 #include <GEOMImpl_IShapesOperations.hxx>
46 #include <GEOMUtils.hxx>
47 #include <GEOMAlgo_ShapeInfo.hxx>
48 #include <GEOMAlgo_ShapeInfoFiller.hxx>
49 *********************************/
50
51 BEGIN_NAMESPACE_HEXA
52
53 static bool db = on_debug ();  // == getenv ("HEXA_DB") > 0
54
55 // ====================================================== Constructeur
56 EdgeShape::EdgeShape (NewShape* dad, int id)
57          : SubShape  (dad, id, 1)
58 {
59    maj_curve  = true;
60    lin_curve  = NULL;
61    lin_length = 0;
62    lin_start [dir_x] = lin_start [dir_y] = lin_start [dir_z] = 0;
63    lin_end   [dir_x] = lin_end   [dir_y] = lin_end   [dir_z] = 0;
64    par_mini   = 0;
65    par_maxi   = 0;
66    lin_radius = 0;
67    lin_angle  = 0;
68 }
69 // ====================================================== getCurve
70 BRepAdaptor_Curve* EdgeShape::getCurve ()
71 {
72    if (maj_curve)
73       updateCurve ();
74
75    return lin_curve;
76 }
77 // ====================================================== makeCurve
78 BRepAdaptor_Curve* EdgeShape::makeCurve ()
79 {
80    if (maj_shape)
81       updateShape ();
82
83    TopoDS_Edge     geo_line = TopoDS::Edge (geo_shape);
84    BRepAdaptor_Curve* curve = new BRepAdaptor_Curve (geo_line);
85    return curve;
86 }
87 // ====================================================== getCoords
88 void EdgeShape::getCoords (double* pstart, double* pend)
89 {
90    if (maj_curve)
91       updateCurve ();
92
93    for (int nc=0 ; nc<DIM3 ; nc++)
94        {
95        pstart [nc] = lin_start [nc];
96        pend   [nc] = lin_end   [nc];
97        }
98 }
99 // ====================================================== getLength
100 double EdgeShape::getLength ()
101 {
102    if (maj_curve)
103       updateCurve ();
104
105    return lin_length;
106 }
107 // ====================================================== getPoint
108 int EdgeShape::getPoint (double param, double* point)
109 {
110    if (param < -0.01 || param > 1.01)
111       {
112       point [dir_x] = point [dir_y] = point [dir_z] = 0;
113       return HERR;
114       }
115    else if (param < 0.01)
116       param = 0;
117    else if (param > 0.99)
118       param = 1;
119
120    if (maj_curve)
121        updateCurve ();
122
123    GCPnts_AbscissaPoint s1 (*lin_curve, param*lin_length,
124                              lin_curve->FirstParameter());
125
126    double para1  = s1.Parameter ();
127    gp_Pnt gpnt   = lin_curve->Value (para1);
128
129    point [dir_x] = gpnt.X();
130    point [dir_y] = gpnt.Y();
131    point [dir_z] = gpnt.Z();
132    return HOK;
133 }
134 // ========================================================= samePoints
135 bool EdgeShape::samePoints (double* point1, double* point2)
136 {
137    const double Epsilon2 = 1e-4;
138    bool   rep = same_coords (point1, point2, Epsilon2);
139    return rep;
140 }
141 // ========================================================= onExtremity
142 int EdgeShape::onExtremity (double* point)
143 {
144    if (maj_curve)
145       updateCurve ();
146
147    if (samePoints  (point, lin_start))
148       return V_AMONT;
149    else if (samePoints  (point, lin_end))
150       return V_AVAL;
151    else
152       return NOTHING;
153 }
154 // ========================================================= definedBy
155 bool EdgeShape::definedBy (double p1[], double p2[])
156 {
157    if (maj_curve)
158       updateCurve ();
159
160    bool rep = false;
161    if (samePoints  (p1, lin_start))
162       rep = samePoints  (p2, lin_end);
163    else if (samePoints  (p1, lin_end))
164       rep = samePoints  (p2, lin_start);
165    return rep;
166 }
167 // ========================================================= getParam
168 double EdgeShape::getParam (double* coord)
169 {
170    if (maj_curve)
171       updateCurve ();
172
173    if (samePoints  (coord, lin_start))
174       return 0.0;
175    else if (samePoints  (coord, lin_end))
176       return 1.0;
177
178    double umin = 0, umax = 0;
179    gp_Pnt gpoint (coord[dir_x], coord[dir_y], coord[dir_z]);
180
181    TopoDS_Edge        geo_line = TopoDS::Edge (geo_shape);
182    Handle(Geom_Curve) handle   = BRep_Tool::Curve (geo_line, umin, umax);
183
184    GeomAPI_ProjectPointOnCurve projector (gpoint, handle);
185    if ( projector.NbPoints() == 0 )
186       return -1.0;
187
188    double gparam = projector.LowerDistanceParameter();
189    if (gparam <par_mini || gparam>par_maxi)
190       return -1.0;
191
192    gp_Pnt rpoint = lin_curve->Value (gparam);
193    Real3  rcoord = { rpoint.X(), rpoint.Y(), rpoint.Z() };
194
195    if (NOT samePoints (coord, rcoord))
196       return -1.0;
197
198    GeomAdaptor_Curve  adapt_curve (handle);
199
200 /******************
201    enum GeomAbs_CurveType { GeomAbs_Line, GeomAbs_Circle, GeomAbs_Ellipse,
202                             GeomAbs_Hyperbola, GeomAbs_Parabola, GeomAbs_BezierCurve,
203                             GeomAbs_BSplineCurve, GeomAbs_OtherCurve };
204  **********************/
205
206    double abscis = GCPnts_AbscissaPoint::Length (adapt_curve, umin, gparam);
207    double hparam = abscis/lin_length;
208
209    // gparam = (gparam-par_mini) / (par_maxi-par_mini);
210    return hparam;
211 }
212 // ====================================================== getAssociation
213 Edge* EdgeShape::getAssociation (int nro)
214 {
215    if (nro>0 && nro<(int)tab_assoc.size())
216       return tab_assoc[nro];
217    else
218       return NULL;
219 }
220 // ==================================================== addAssociation
221 void EdgeShape::addAssociation (Edge* edge)
222 {
223    tab_assoc.push_back (edge);
224    is_associated = true;
225 }
226 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
227
228  // ------- Cf GEOMImpl_IMeasureOperations.cxx
229
230 // ====================================================== updateCurve
231 void EdgeShape::updateCurve ()
232 {
233    delete lin_curve;
234    maj_curve = false;
235
236    if (maj_shape)
237       updateShape ();
238
239    TopoDS_Edge geo_line = TopoDS::Edge (geo_shape);
240    lin_curve            = new BRepAdaptor_Curve (geo_line);
241                                // Longueur de la ligne
242    double umin = 0, umax = 0;
243    TopLoc_Location    loc;
244    Handle(Geom_Curve) handle = BRep_Tool::Curve (geo_line, loc, umin, umax);
245    GeomAdaptor_Curve  adapt_curve (handle);
246    lin_length = GCPnts_AbscissaPoint::Length(adapt_curve, umin, umax);
247
248                                // Extremites (1)
249    GCPnts_AbscissaPoint s1(*lin_curve, 0,          lin_curve->FirstParameter());
250    GCPnts_AbscissaPoint s2(*lin_curve, lin_length, lin_curve->FirstParameter());
251
252    par_mini = s1.Parameter ();
253    par_maxi = s2.Parameter ();
254                                // Extremites
255    getPoint (0, lin_start);
256    getPoint (1, lin_end);
257
258    lin_radius = lin_angle = 0;
259    kind_of    = (EnumKindOfShape) adapt_curve.GetType();
260 #ifndef NO_CASCADE
261    if (kind_of==KS_Circle)
262       {
263       Handle(Geom_Circle) hgc = Handle(Geom_Circle)::DownCast (handle);
264       lin_radius = hgc->Radius ();
265       lin_angle  = (par_maxi-par_mini)*180/M_PI;
266       }
267 #endif
268 }
269 // ====================================================== getAngle
270 double EdgeShape::getAngle ()
271 {
272    if (maj_curve)
273       updateCurve ();
274
275    return lin_angle;
276 }
277 // ====================================================== getRadius
278 double EdgeShape::getRadius ()
279 {
280    if (maj_curve)
281       updateCurve ();
282
283    return lin_radius;
284 }
285 // ====================================================== getRadius
286 EnumKindOfShape EdgeShape::kindOf ()
287 {
288    if (maj_curve)
289       updateCurve ();
290
291    return kind_of;
292 }
293 END_NAMESPACE_HEXA