0023948: Wrong intersection between a surface of revolution and a plane.
[occt.git] / src / GeomFill / GeomFill_GuideTrihedronAC.cxx
1 // Created by: Stephanie HUMEAU
2 // Copyright (c) 1998-1999 Matra Datavision
3 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 // Creted:      Tue Jun 23 15:39:24 1998
17
18
19 #include <GeomFill_GuideTrihedronAC.ixx>
20
21 #include <gp_Pnt.hxx>
22 #include <gp_Dir.hxx>
23 #include <gp_Vec.hxx>
24 #include <Precision.hxx>
25 #include <TColStd_SequenceOfReal.hxx>
26
27 #include <Approx_CurvlinFunc.hxx>
28 #include <Adaptor3d_Curve.hxx>
29 #include <GeomAdaptor.hxx>
30 #include <GeomAdaptor_HCurve.hxx>
31
32 #include <GeomFill_Frenet.hxx>
33 #include <GeomLib.hxx>
34
35
36 //=======================================================================
37 //function : GuideTrihedron
38 //purpose  : Constructor
39 //=======================================================================
40 GeomFill_GuideTrihedronAC::GeomFill_GuideTrihedronAC(const Handle(Adaptor3d_HCurve) & guide)
41 {
42   myCurve.Nullify();
43   myGuide =  guide;
44   myTrimG =  guide;
45   myGuideAC = new (Approx_CurvlinFunc) (myGuide,1.e-7);
46   Lguide = myGuideAC->GetLength(); 
47   UTol = STol = Precision::PConfusion();
48   Orig1 = 0; // origines pour le cas path multi-edges
49   Orig2 = 1;
50 }
51
52 //=======================================================================
53 //function : Guide
54 //purpose  : calculation of trihedron
55 //=======================================================================
56
57  Handle(Adaptor3d_HCurve) GeomFill_GuideTrihedronAC::Guide()const
58 {
59   return myGuide;
60 }
61
62 //=======================================================================
63 //function : D0
64 //purpose  : calculation of trihedron
65 //=======================================================================
66  Standard_Boolean GeomFill_GuideTrihedronAC::D0(const Standard_Real Param,
67                                                 gp_Vec& Tangent,
68                                                 gp_Vec& Normal,
69                                                 gp_Vec& BiNormal) 
70
71   Standard_Real s = myCurveAC->GetSParameter(Param); // abscisse curviligne <=> Param
72   Standard_Real OrigG = Orig1 + s*(Orig2-Orig1); // abscisse curv sur le guide (cas multi-edges)
73   Standard_Real tG = myGuideAC->GetUParameter(myGuide->GetCurve(), OrigG, 1); // param <=> s sur theGuide
74
75   gp_Pnt P, PG;
76   gp_Vec To, B;
77   myTrimmed->D1(Param, P, To);//point et derivee au parametre Param sur myCurve
78   myTrimG->D0(tG, PG);// point au parametre tG sur myGuide
79   myCurPointOnGuide = PG;
80  
81   gp_Vec n (P, PG); // vecteur definissant la normale
82   
83   Normal = n.Normalized();
84   B = To.Crossed(Normal);
85   BiNormal = B/B.Magnitude();
86   Tangent = Normal.Crossed(BiNormal);
87   Tangent.Normalize();
88
89   return Standard_True;
90 }
91
92 //=======================================================================
93 //function : D1
94 //purpose  : calculation of trihedron and first derivative
95 //=======================================================================
96  Standard_Boolean GeomFill_GuideTrihedronAC::D1(const Standard_Real Param,
97                                                 gp_Vec& Tangent,
98                                                 gp_Vec& DTangent,
99                                                 gp_Vec& Normal,
100                                                 gp_Vec& DNormal,
101                                                 gp_Vec& BiNormal,          
102                                                 gp_Vec& DBiNormal) 
103
104 //triedre
105   Standard_Real s, OrigG, tG, dtg; 
106  // abscisse curviligne <=> Param
107   s = myCurveAC->GetSParameter(Param);
108   // parametre <=> s sur theGuide
109   OrigG = Orig1 + s*(Orig2-Orig1); 
110   // parametre <=> s sur  theGuide
111   tG = myGuideAC->GetUParameter(myGuide->GetCurve(), OrigG, 1); 
112
113   gp_Pnt P, PG;
114   gp_Vec To, DTo, TG, B, BPrim;
115   
116   myTrimmed->D2(Param, P, To, DTo);
117   myTrimG->D1(tG, PG, TG);
118   myCurPointOnGuide = PG;
119   
120   gp_Vec n (P, PG), dn; 
121   Standard_Real Norm = n.Magnitude();
122   if (Norm < 1.e-12) {
123     Norm = 1;
124 #if DEB
125     cout << "GuideTrihedronAC : Normal indefinie" << endl;
126 #endif
127   }
128   
129   n /= Norm;
130   //derivee de n par rapport a Param
131   dtg = (Orig2-Orig1)*(To.Magnitude()/TG.Magnitude())*(Lguide/L);
132   dn.SetLinearForm(dtg, TG, -1, To);
133   dn /= Norm;
134
135 // triedre
136   Normal = n;
137   B = To.Crossed(Normal);
138   Standard_Real NormB = B.Magnitude();
139   B/= NormB;
140
141   BiNormal = B; 
142
143   Tangent = Normal.Crossed(BiNormal);
144   Tangent.Normalize();
145
146 // derivee premiere
147   DNormal.SetLinearForm(-(n.Dot(dn)), n, dn);  
148  
149   BPrim.SetLinearForm(DTo.Crossed(Normal), To.Crossed(DNormal));
150
151   DBiNormal.SetLinearForm(-(B.Dot(BPrim)), B, BPrim);
152   DBiNormal /= NormB;
153
154   DTangent.SetLinearForm(Normal.Crossed(DBiNormal), DNormal.Crossed(BiNormal));
155
156   return Standard_True;
157 }
158
159
160 //=======================================================================
161 //function : D2
162 //purpose  : calculation of trihedron and derivatives
163 //=======================================================================
164  Standard_Boolean GeomFill_GuideTrihedronAC::D2(const Standard_Real Param,
165                                                 gp_Vec& Tangent,
166                                                 gp_Vec& DTangent,
167                                                 gp_Vec& D2Tangent,
168                                                 gp_Vec& Normal,
169                                                 gp_Vec& DNormal,
170                                                 gp_Vec& D2Normal,
171                                                 gp_Vec& BiNormal,                         
172                                                 gp_Vec& DBiNormal,                
173                                                 gp_Vec& D2BiNormal) 
174
175   // abscisse curviligne <=> Param
176   Standard_Real s = myCurveAC->GetSParameter(Param); 
177   // parametre <=> s sur theGuide
178   Standard_Real OrigG = Orig1 + s*(Orig2-Orig1); 
179   Standard_Real tG = myGuideAC->GetUParameter(myGuide->GetCurve(), 
180                                               OrigG, 1); 
181
182   gp_Pnt P,PG;
183   gp_Vec TG,DTG;
184 //  gp_Vec To,DTo,D2To,B;
185   gp_Vec To,DTo,D2To;
186   
187   myTrimmed->D3(Param, P, To, DTo, D2To);
188   myTrimG->D2(tG, PG, TG, DTG);
189   myCurPointOnGuide = PG;
190
191   Standard_Real NTo = To.Magnitude();
192   Standard_Real N2To = To.SquareMagnitude();
193   Standard_Real NTG = TG.Magnitude();
194   Standard_Real N2Tp = TG.SquareMagnitude();
195   Standard_Real d2tp_dt2, dtg_dt; 
196   dtg_dt = (Orig2-Orig1)*(NTo/NTG)*(Lguide/L);
197
198   gp_Vec n(P, PG); // vecteur definissant la normale
199   Standard_Real Norm = n.Magnitude(), ndn;
200   //derivee de n par rapport a Param
201   gp_Vec dn, d2n;
202   dn.SetLinearForm(dtg_dt, TG, -1, To);
203
204   //derivee seconde de tG par rapport a Param
205   d2tp_dt2 = (Orig2-Orig1)*(Lguide/L) * 
206     ( DTo.Dot(To) / (NTo*NTG) - N2To*TG*DTG*(Lguide/L) / (N2Tp*N2Tp));
207   //derivee seconde de n par rapport a Param
208   d2n.SetLinearForm(dtg_dt*dtg_dt,DTG, d2tp_dt2, TG, -1, DTo);
209
210   if (Norm > 1.e-9) {
211     n /= Norm;
212     dn /= Norm;
213     d2n /= Norm;
214   }
215 //triedre
216   Normal = n;
217
218   gp_Vec TN, DTN, D2TN;
219   TN  = To.Crossed(Normal);
220
221
222   Standard_Real Norma = TN.Magnitude();
223   if (Norma > 1.e-9) TN /= Norma;
224
225   BiNormal = TN; 
226
227   Tangent = Normal.Crossed(BiNormal);
228 //  Tangent.Normalize();
229
230 // derivee premiere du triedre
231 //  gp_Vec DTN = DTo.Crossed(Normal);
232 //  gp_Vec TDN = To.Crossed(DNormal);
233 //  gp_Vec DT = DTN + TDN;
234
235   ndn = n.Dot(dn);
236   DNormal.SetLinearForm(-ndn, n, dn); 
237
238   DTN.SetLinearForm(DTo.Crossed(Normal),  To.Crossed(DNormal));
239   DTN /= Norma;
240   Standard_Real TNDTN = TN.Dot(DTN);
241
242   DBiNormal.SetLinearForm(-TNDTN, TN, DTN);
243
244   DTangent.SetLinearForm(Normal.Crossed(DBiNormal),
245                          DNormal.Crossed(BiNormal));
246
247
248 //derivee seconde du triedre
249 #ifdef DEB
250   gp_Vec DTDN = DTo.Crossed(DNormal);
251 #else
252   DTo.Crossed(DNormal);
253 #endif
254   Standard_Real TN2 = TN.SquareMagnitude();
255
256   D2Normal.SetLinearForm(-2*ndn, dn, 
257                          3*ndn*ndn - (dn.SquareMagnitude() + n.Dot(d2n)),n,
258                          d2n);
259                          
260
261   D2TN.SetLinearForm(1, D2To.Crossed(Normal), 
262                      2, DTo.Crossed(DNormal),
263                      To.Crossed(D2Normal));
264   D2TN /= Norma;
265
266   D2BiNormal.SetLinearForm(-2*TNDTN, DTN, 
267                            3*TNDTN*TNDTN - (TN2 + TN.Dot(D2TN)), TN,
268                            D2TN);
269     
270   D2Tangent.SetLinearForm(1, D2Normal.Crossed(BiNormal),
271                           2, DNormal.Crossed(DBiNormal), 
272                           Normal.Crossed(D2BiNormal) );
273
274 //  return Standard_True;
275   return Standard_False;
276
277 }
278
279
280 //=======================================================================
281 //function : Copy
282 //purpose  : 
283 //=======================================================================
284  Handle(GeomFill_TrihedronLaw) GeomFill_GuideTrihedronAC::Copy() const
285 {
286  Handle(GeomFill_GuideTrihedronAC) copy = 
287    new (GeomFill_GuideTrihedronAC) (myGuide);
288  copy->SetCurve(myCurve);
289  copy->Origine(Orig1,Orig2);
290  return copy;
291
292
293 //=======================================================================
294 //function : SetCurve
295 //purpose  : 
296 //=======================================================================
297  void GeomFill_GuideTrihedronAC::SetCurve(const Handle(Adaptor3d_HCurve)& C) 
298 {
299   myCurve = C;
300   myTrimmed = C;
301   if (!myCurve.IsNull()) {
302     myCurveAC = new (Approx_CurvlinFunc) (C,1.e-7);
303     L = myCurveAC->GetLength();
304 //    CorrectOrient(myGuide);
305   }
306 }
307
308
309 //=======================================================================
310 //function : NbIntervals
311 //purpose  : 
312 //=======================================================================
313  Standard_Integer GeomFill_GuideTrihedronAC::NbIntervals(const GeomAbs_Shape S) const
314 {
315   Standard_Integer Nb;
316   Nb = myCurveAC->NbIntervals(S);
317   TColStd_Array1OfReal DiscC(1, Nb+1);
318   myCurveAC->Intervals(DiscC, S);
319   Nb =  myGuideAC->NbIntervals(S);
320   TColStd_Array1OfReal DiscG(1, Nb+1);
321   myGuideAC->Intervals(DiscG, S);
322
323   TColStd_SequenceOfReal Seq;
324   GeomLib::FuseIntervals(DiscC, DiscG, Seq);
325   
326   return Seq.Length()-1;
327
328 }
329
330 //======================================================================
331 //function :Intervals
332 //purpose  : 
333 //=======================================================================
334  void GeomFill_GuideTrihedronAC::Intervals(TColStd_Array1OfReal& TT,
335                                            const GeomAbs_Shape S) const
336 {
337   Standard_Integer Nb, ii;
338   Nb = myCurveAC->NbIntervals(S);
339   TColStd_Array1OfReal DiscC(1, Nb+1);
340   myCurveAC->Intervals(DiscC, S);
341   Nb =  myGuideAC->NbIntervals(S);
342   TColStd_Array1OfReal DiscG(1, Nb+1);
343   myGuideAC->Intervals(DiscG, S);
344
345   TColStd_SequenceOfReal Seq;
346   GeomLib::FuseIntervals(DiscC, DiscG, Seq); 
347   Nb = Seq.Length();
348
349   for (ii=1; ii<=Nb; ii++) {
350     TT(ii) =  myCurveAC->GetUParameter(myCurve->GetCurve(), Seq(ii), 1);
351   }
352
353 }
354
355 //======================================================================
356 //function :SetInterval
357 //purpose  : 
358 //=======================================================================
359 void GeomFill_GuideTrihedronAC::SetInterval(const Standard_Real First,
360                                             const Standard_Real Last) 
361 {
362   myTrimmed = myCurve->Trim(First, Last, UTol); 
363   Standard_Real Sf, Sl, U;
364
365   Sf = myCurveAC->GetSParameter(First);
366   Sl = myCurveAC->GetSParameter(Last);
367 //  if (Sl>1) Sl=1;
368 //  myCurveAC->Trim(Sf, Sl, UTol);
369
370   U = Orig1 + Sf*(Orig2-Orig1);
371   Sf = myGuideAC->GetUParameter(myGuide->GetCurve(), U, 1);
372   U = Orig1 + Sl*(Orig2-Orig1);
373   Sl = myGuideAC->GetUParameter(myGuide->GetCurve(), U, 1);
374   myTrimG = myGuide->Trim(Sf, Sl, UTol); 
375 }
376
377
378
379 //=======================================================================
380 //function : GetAverageLaw
381 //purpose  : 
382 //=======================================================================
383  void GeomFill_GuideTrihedronAC::GetAverageLaw(gp_Vec& ATangent,
384                                                gp_Vec& ANormal,
385                                                gp_Vec& ABiNormal) 
386 {
387   Standard_Integer ii;
388   Standard_Real t, Delta = (myCurve->LastParameter() - 
389                             myCurve->FirstParameter())/20.001;
390
391   ATangent.SetCoord(0.,0.,0.);
392   ANormal.SetCoord(0.,0.,0.);
393   ABiNormal.SetCoord(0.,0.,0.);
394   gp_Vec T, N, B;
395   
396   for (ii=1; ii<=20; ii++) {
397     t = myCurve->FirstParameter() +(ii-1)*Delta;
398     D0(t, T, N, B);
399     ATangent +=T;
400     ANormal  +=N;
401     ABiNormal+=B;
402   }
403   ATangent  /= 20;
404   ANormal   /= 20;
405   ABiNormal /= 20; 
406 }
407
408 //=======================================================================
409 //function : IsConstant
410 //purpose  : 
411 //=======================================================================
412  Standard_Boolean GeomFill_GuideTrihedronAC::IsConstant() const
413 {
414   return  Standard_False;
415 }
416
417 //=======================================================================
418 //function : IsOnlyBy3dCurve
419 //purpose  : 
420 //=======================================================================
421  Standard_Boolean GeomFill_GuideTrihedronAC::IsOnlyBy3dCurve() const
422 {
423   return Standard_False;
424 }
425
426 //=======================================================================
427 //function : Origine
428 //purpose  : 
429 //=======================================================================
430  void GeomFill_GuideTrihedronAC::Origine(const Standard_Real OrACR1,
431                                          const Standard_Real OrACR2)
432 {
433   Orig1 = OrACR1;
434   Orig2 = OrACR2;
435 }