]> SALOME platform Git repositories - modules/hexablock.git/blob - src/HEXABLOCK/HexEdgeShape.cxx
Salome HOME
3a5b86d13f136565e392c1c6f083895abd69dbe9
[modules/hexablock.git] / src / HEXABLOCK / HexEdgeShape.cxx
1
2 // C++ : Gestion des soous-shapes
3
4 // Copyright (C) 2009-2013  CEA/DEN, EDF R&D
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.
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 #ifndef NO_CASCADE
28
29 #include <TopoDS.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <gp_Pnt.hxx>
32
33 #include <BRep_Builder.hxx>
34 #include <BRepAdaptor_Curve.hxx>
35
36 #include <GCPnts_AbscissaPoint.hxx>
37
38 #include <GeomAPI_ProjectPointOnCurve.hxx>
39
40 BEGIN_NAMESPACE_HEXA
41
42 static bool db = on_debug ();  // == getenv ("HEXA_DB") > 0
43
44 // ====================================================== Constructeur
45 EdgeShape::EdgeShape (NewShape* dad, int id)
46          : SubShape  (dad, id, 1)
47 {
48    maj_curve  = true;
49    lin_curve  = NULL;
50    lin_length = 0;
51    lin_start [dir_x] = lin_start [dir_y] = lin_start [dir_z] = 0;
52    lin_end   [dir_x] = lin_end   [dir_y] = lin_end   [dir_z] = 0;
53    par_mini   = 0;
54    par_maxi   = 0;
55 }
56 // ====================================================== getCurve
57 BRepAdaptor_Curve* EdgeShape::getCurve ()
58 {
59    if (maj_curve)
60       updateCurve ();
61
62    return lin_curve;
63 }
64 // ====================================================== makeCurve
65 BRepAdaptor_Curve* EdgeShape::makeCurve ()
66 {
67    if (maj_shape)
68       updateShape ();
69
70    TopoDS_Edge     geo_line = TopoDS::Edge (geo_shape);
71    BRepAdaptor_Curve* curve = new BRepAdaptor_Curve (geo_line);
72    return curve;
73 }
74 // ====================================================== getCoords
75 void EdgeShape::getCoords (double* pstart, double* pend)
76 {
77    if (maj_curve)
78       updateCurve ();
79
80    for (int nc=0 ; nc<DIM3 ; nc++)
81        {
82        pstart [nc] = lin_start [nc];
83        pend   [nc] = lin_end   [nc];
84        }
85 }
86 // ====================================================== getLength
87 double EdgeShape::getLength ()
88 {
89    if (maj_curve)
90       updateCurve ();
91
92    return lin_length;
93 }
94 // ====================================================== getPoint
95 int EdgeShape::getPoint (double param, double* point)
96 {
97    if (param < -0.01 || param > 1.01)
98       {
99       point [dir_x] = point [dir_y] = point [dir_z] = 0;
100       return HERR;
101       }
102    else if (param < 0.01)
103       param = 0;
104    else if (param > 0.99)
105       param = 1;
106
107    if (maj_curve)
108        updateCurve ();
109
110    GCPnts_AbscissaPoint s1 (*lin_curve, param*lin_length,
111                              lin_curve->FirstParameter());
112
113    double para1  = s1.Parameter ();
114    gp_Pnt gpnt   = lin_curve->Value (para1);
115
116    point [dir_x] = gpnt.X();
117    point [dir_y] = gpnt.Y();
118    point [dir_z] = gpnt.Z();
119    return HOK;
120 }
121 // ========================================================= samePoints
122 bool EdgeShape::samePoints (double* point1, double* point2)
123 {
124    const double Epsilon2 = 1e-4;
125    bool   rep = same_coords (point1, point2, Epsilon2);
126    return rep;
127 }
128 // ========================================================= onExtremity
129 int EdgeShape::onExtremity (double* point)
130 {
131    if (maj_curve)
132       updateCurve ();
133
134    if (samePoints  (point, lin_start))
135       return V_AMONT;
136    else if (samePoints  (point, lin_end))
137       return V_AVAL;
138    else
139       return NOTHING;
140 }
141 // ========================================================= getParam
142 double EdgeShape::getParam (double* coord)
143 {
144    if (maj_curve)
145       updateCurve ();
146
147    if (samePoints  (coord, lin_start))
148       return 0.0;
149    else if (samePoints  (coord, lin_end))
150       return 1.0;
151
152    double umin = 0, umax = 0;
153    gp_Pnt gpoint (coord[dir_x], coord[dir_y], coord[dir_z]);
154
155    TopoDS_Edge        geo_line = TopoDS::Edge (geo_shape);
156    Handle(Geom_Curve) handle   = BRep_Tool::Curve (geo_line, umin, umax);
157
158    GeomAPI_ProjectPointOnCurve projector (gpoint, handle);
159    if ( projector.NbPoints() == 0 )
160       return -1.0;
161
162    double gparam = projector.LowerDistanceParameter();
163    if (gparam <par_mini || gparam>par_maxi)
164       return -1.0;
165
166    gp_Pnt rpoint = lin_curve->Value (gparam);
167    Real3  rcoord = { rpoint.X(), rpoint.Y(), rpoint.Z() };
168
169    if (NOT samePoints (coord, rcoord))
170       return -1.0;
171
172    GeomAdaptor_Curve  adapt_curve (handle);
173    double abscis = GCPnts_AbscissaPoint::Length (adapt_curve, umin, gparam);
174    double hparam = abscis/lin_length;
175
176    // gparam = (gparam-par_mini) / (par_maxi-par_mini);
177    return hparam;
178 }
179 // ====================================================== getAssociation
180 Edge* EdgeShape::getAssociation (int nro)
181 {
182    if (nro>0 && nro<tab_assoc.size())
183       return tab_assoc[nro];
184    else
185       return NULL;
186 }
187 // ========================================================== addAssociation
188 void EdgeShape::addAssociation (Edge* edge)
189 {
190    tab_assoc.push_back (edge);
191    is_associated = true;
192 }
193 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
194 // ====================================================== updateCurve
195 void EdgeShape::updateCurve ()
196 {
197    delete lin_curve;
198    maj_curve = false;
199
200    if (maj_shape)
201       updateShape ();
202
203    TopoDS_Edge geo_line = TopoDS::Edge (geo_shape);
204    lin_curve            = new BRepAdaptor_Curve (geo_line);
205                                // Longueur de la ligne
206    double umin = 0, umax = 0;
207    TopLoc_Location    loc;
208    Handle(Geom_Curve) handle = BRep_Tool::Curve (geo_line, loc, umin, umax);
209    GeomAdaptor_Curve  adapt_curve (handle);
210    lin_length = GCPnts_AbscissaPoint::Length(adapt_curve, umin, umax);
211
212                                // Extremites (1)
213    GCPnts_AbscissaPoint s1(*lin_curve, 0,          lin_curve->FirstParameter());
214    GCPnts_AbscissaPoint s2(*lin_curve, lin_length, lin_curve->FirstParameter());
215
216    par_mini = s1.Parameter ();
217    par_maxi = s2.Parameter ();
218                                // Extremites
219    getPoint (0, lin_start);
220    getPoint (1, lin_end);
221 }
222 END_NAMESPACE_HEXA
223 #endif