0030686: Visualization, SelectMgr_ViewerSelector - sorting issues of transformation...
[occt.git] / src / ChFiKPart / ChFiKPart_ComputeData_ChPlnPln.cxx
1 // Created on: 1995-04-28
2 // Created by: Flore Lantheaume
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <Adaptor3d_HSurface.hxx>
19 #include <ChFiDS_Spine.hxx>
20 #include <ChFiDS_SurfData.hxx>
21 #include <ChFiKPart_ComputeData.hxx>
22 #include <ChFiKPart_ComputeData_Fcts.hxx>
23 #include <ElCLib.hxx>
24 #include <ElSLib.hxx>
25 #include <Geom2d_Line.hxx>
26 #include <Geom_Line.hxx>
27 #include <Geom_Plane.hxx>
28 #include <gp.hxx>
29 #include <gp_Ax3.hxx>
30 #include <gp_Dir.hxx>
31 #include <gp_Dir2d.hxx>
32 #include <gp_Lin.hxx>
33 #include <gp_Lin2d.hxx>
34 #include <gp_Pln.hxx>
35 #include <gp_Pnt.hxx>
36 #include <gp_Pnt2d.hxx>
37 #include <IntAna_QuadQuadGeo.hxx>
38 #include <Precision.hxx>
39 #include <TopOpeBRepDS_DataStructure.hxx>
40
41 //=======================================================================
42 //function : MakeChamfer
43 //Purpose  : Compute the chamfer in the particular case plane/plane.
44 //           Compute the SurfData <Data> of the chamfer on the <Spine> 
45 //           between the plane <Pl1> and the plane <Pl2>, with distances
46 //           <Dis1> on <Pl1> and <Dis2> on <Pl2>.
47 //           <First> is the parameter of the start point on the <Spine>
48 //           <Or1> and <Or2> are the orientations of the plane <Pl1> and
49 //           <Pl2>, and <Of1> the orientation of the face build on the
50 //           plane <Pl1>.
51 //Out      : True if the chamfer has been computed
52 //           False else
53 //=======================================================================
54 Standard_Boolean ChFiKPart_MakeChamfer(TopOpeBRepDS_DataStructure& DStr,
55                                        const Handle(ChFiDS_SurfData)& Data, 
56                                        const ChFiDS_ChamfMode theMode,
57                                        const gp_Pln& Pl1, 
58                                        const gp_Pln& Pl2, 
59                                        const TopAbs_Orientation Or1,
60                                        const TopAbs_Orientation Or2,
61                                        const Standard_Real theDis1, 
62                                        const Standard_Real theDis2, 
63                                        const gp_Lin& Spine, 
64                                        const Standard_Real First, 
65                                        const TopAbs_Orientation Of1)
66 {
67
68   // Creation of the plane which carry the chamfer
69
70     // compute the normals to the planes Pl1 and Pl2
71   gp_Ax3 Pos1 = Pl1.Position();
72   gp_Dir D1 = Pos1.XDirection().Crossed(Pos1.YDirection());
73   if (Or1 == TopAbs_REVERSED) { D1.Reverse(); }
74   gp_Ax3 Pos2 = Pl2.Position();
75   gp_Dir D2 = Pos2.XDirection().Crossed(Pos2.YDirection());
76   if (Or2 == TopAbs_REVERSED) { D2.Reverse(); }
77
78     // compute the intersection line of Pl1 and Pl2
79   IntAna_QuadQuadGeo LInt (Pl1,Pl2,Precision::Angular(),
80                            Precision::Confusion());
81   
82   gp_Pnt P;
83   Standard_Real Fint;
84   if (LInt.IsDone()) {
85     Fint = ElCLib::Parameter(LInt.Line(1),ElCLib::Value(First,Spine));
86     P = ElCLib::Value(Fint,LInt.Line(1));
87   }
88   else { return Standard_False; }
89
90   gp_Dir LinAx1 = Spine.Direction();
91   gp_Dir VecTransl1 = LinAx1.Crossed(D1);
92   if ( VecTransl1.Dot(D2) <=0. )
93     VecTransl1.Reverse();
94
95   gp_Dir VecTransl2 = LinAx1.Crossed(D2);
96   if ( VecTransl2.Dot(D1) <=0. )
97     VecTransl2.Reverse();
98
99   Standard_Real Dis1 = theDis1, Dis2 = theDis2;
100   Standard_Real Alpha = VecTransl1.Angle(VecTransl2);
101   Standard_Real CosHalfAlpha = Cos(Alpha/2);
102   if (theMode == ChFiDS_ConstThroatChamfer)
103     Dis1 = Dis2 = theDis1 / CosHalfAlpha;
104   else if (theMode == ChFiDS_ConstThroatWithPenetrationChamfer)
105   {
106     Standard_Real aDis1 = Min(theDis1, theDis2);
107     Standard_Real aDis2 = Max(theDis1, theDis2);
108     Standard_Real dis1dis1 = aDis1*aDis1, dis2dis2 = aDis2*aDis2;
109     Standard_Real SinAlpha = Sin(Alpha);
110     Standard_Real CosAlpha = Cos(Alpha);
111     Standard_Real CotanAlpha = CosAlpha/SinAlpha;
112     Dis1 = sqrt(dis2dis2 - dis1dis1) - aDis1*CotanAlpha;
113     Standard_Real CosBeta = sqrt(1-dis1dis1/dis2dis2)*CosAlpha + aDis1/aDis2*SinAlpha;
114     Standard_Real FullDist1 = aDis2/CosBeta;
115     Dis2 = FullDist1 - aDis1/SinAlpha;
116   }
117
118     // Compute a point on the plane Pl1 and on the chamfer
119   gp_Pnt P1( P.X()+Dis1*VecTransl1.X(),
120              P.Y()+Dis1*VecTransl1.Y(),
121              P.Z()+Dis1*VecTransl1.Z());
122
123     // Point on the plane Pl2 and on the chamfer
124   gp_Pnt P2( P.X()+Dis2*VecTransl2.X(),
125              P.Y()+Dis2*VecTransl2.Y(),
126              P.Z()+Dis2*VecTransl2.Z());
127
128     //the middle point of P1 P2 is the origin of the chamfer
129   gp_Pnt Po ( (P1.X()+P2.X())/2. ,(P1.Y()+P2.Y())/2. , (P1.Z()+P2.Z())/2. ); 
130   
131     // compute a second point on the plane Pl2 
132   gp_Pnt Pp = ElCLib::Value(Fint+10.,LInt.Line(1));
133   gp_Pnt P22(Pp.X()+Dis2*VecTransl2.X(),
134              Pp.Y()+Dis2*VecTransl2.Y(),
135              Pp.Z()+Dis2*VecTransl2.Z()); 
136   
137     // Compute the normal vector <AxisPlan> to the chamfer's plane
138   gp_Dir V1 ( P2.X()-P1.X(), P2.Y()-P1.Y(), P2.Z()-P1.Z());
139   gp_Dir V2 ( P22.X()-P1.X(), P22.Y()-P1.Y(), P22.Z()-P1.Z());
140   gp_Dir AxisPlan = V1.Crossed(V2);
141
142   gp_Dir xdir = LinAx1; // u axis
143   gp_Ax3 PlanAx3 ( Po, AxisPlan, xdir);
144   if (PlanAx3.YDirection().Dot(D2)>=0.)  PlanAx3.YReverse();
145
146   Handle(Geom_Plane) gpl= new Geom_Plane(PlanAx3);
147   Data->ChangeSurf(ChFiKPart_IndexSurfaceInDS(gpl,DStr));
148
149
150   // About the orientation of the chamfer plane
151     // Compute the normal to the face 1
152   gp_Dir norpl = Pos1.XDirection().Crossed(Pos1.YDirection());
153   gp_Dir norface1 = norpl;
154   if (Of1 == TopAbs_REVERSED ) { norface1.Reverse(); }
155
156     // Compute the orientation of the chamfer plane
157   gp_Dir norplch = gpl->Pln().Position().XDirection().Crossed (
158                                 gpl->Pln().Position().YDirection());
159   
160   gp_Dir DirCh12(gp_Vec(P1, P2));
161   Standard_Boolean toreverse = ( norplch.Dot(norface1) <= 0. );
162   if (VecTransl1.Dot(DirCh12) > 0)  toreverse = !toreverse;
163
164   if (toreverse)
165     Data->ChangeOrientation() = TopAbs_REVERSED; 
166   else
167     Data->ChangeOrientation() = TopAbs_FORWARD;
168
169
170
171   // Loading of the FaceInterferences with pcurves & 3d curves.
172   
173     // case face 1 
174   gp_Lin linPln(P1, xdir);
175   Handle(Geom_Line) GLinPln1 = new Geom_Line(linPln);
176
177   Standard_Real u,v;
178   ElSLib::PlaneParameters(Pos1,P1,u,v);
179   gp_Pnt2d p2dPln(u,v);
180   gp_Dir2d dir2dPln( xdir.Dot(Pos1.XDirection()),
181                      xdir.Dot(Pos1.YDirection()));
182   gp_Lin2d lin2dPln(p2dPln,dir2dPln);
183   Handle(Geom2d_Line) GLin2dPln1 = new Geom2d_Line(lin2dPln);
184
185   ElSLib::PlaneParameters(PlanAx3,P1,u,v);
186   p2dPln.SetCoord(u,v);
187   lin2dPln.SetLocation(p2dPln);
188   lin2dPln.SetDirection(gp::DX2d());
189   Handle(Geom2d_Line) GLin2dPlnCh1 = new Geom2d_Line(lin2dPln);
190
191   TopAbs_Orientation trans; 
192   toreverse = ( norplch.Dot(norpl) <= 0. );
193   if (VecTransl1.Dot(DirCh12) > 0)  toreverse = !toreverse;
194   if (toreverse)
195     trans = TopAbs_FORWARD;
196   else 
197     trans = TopAbs_REVERSED; 
198
199   Data->ChangeInterferenceOnS1().
200     SetInterference(ChFiKPart_IndexCurveInDS(GLinPln1,DStr),
201                     trans,GLin2dPln1,GLin2dPlnCh1);
202
203
204     // case face 2
205
206   linPln.SetLocation(P2);
207   Handle(Geom_Line) GLinPln2 = new Geom_Line(linPln);
208
209   ElSLib::PlaneParameters(Pos2,P2,u,v);
210   p2dPln.SetCoord(u,v);
211   dir2dPln.SetCoord( xdir.Dot(Pos2.XDirection()),
212                      xdir.Dot(Pos2.YDirection()));
213   lin2dPln.SetLocation(p2dPln);
214   lin2dPln.SetDirection(dir2dPln);
215   Handle(Geom2d_Line) GLin2dPln2 = new Geom2d_Line(lin2dPln);
216
217   ElSLib::PlaneParameters(PlanAx3,P2,u,v);
218   p2dPln.SetCoord(u,v);
219   lin2dPln.SetLocation(p2dPln);
220   lin2dPln.SetDirection(gp::DX2d());
221   Handle(Geom2d_Line) GLin2dPlnCh2 = new Geom2d_Line(lin2dPln);
222
223
224   norpl = Pos2.XDirection().Crossed(Pos2.YDirection());
225   toreverse = ( norplch.Dot(norpl) <= 0. );
226   if (VecTransl2.Dot(DirCh12) < 0)  toreverse = !toreverse;
227   if (toreverse)
228     trans = TopAbs_REVERSED; 
229   else
230     trans = TopAbs_FORWARD; 
231
232   Data->ChangeInterferenceOnS2().
233     SetInterference(ChFiKPart_IndexCurveInDS(GLinPln2,DStr),
234                     trans,GLin2dPln2,GLin2dPlnCh2);
235
236   return Standard_True;
237
238  }
239
240
241
242
243
244