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