0024624: Lost word in license statement in source files
[occt.git] / src / GeomLib / GeomLib_CheckBSplineCurve.cxx
1 // Created on: 1997-05-28
2 // Created by: Xavier BENVENISTE
3 // Copyright (c) 1997-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 #include <GeomLib_CheckBSplineCurve.ixx>
18 #include <Geom_BSplineCurve.hxx>
19 #include <gp_Pnt.hxx>
20 #include <gp_Vec.hxx>
21 //=======================================================================
22 //function : GeomLib_CheckBSplineCurve
23 //purpose  : 
24 //=======================================================================
25
26 GeomLib_CheckBSplineCurve::GeomLib_CheckBSplineCurve(const Handle(Geom_BSplineCurve)& Curve,
27                                                      const Standard_Real Tolerance,
28                                                      const Standard_Real AngularTolerance)
29 :myCurve(Curve),
30 myDone(Standard_False),
31 myFixFirstTangent(Standard_False),
32 myFixLastTangent(Standard_False),
33 myAngularTolerance(Abs(AngularTolerance)),
34 myTolerance(Abs(Tolerance)),
35 myFirstPole(1.0,0.0e0,0.e0),
36 myLastPole(1.0e0,0.0e0,0.e0)
37 {
38   
39
40   Standard_Integer ii,
41     num_poles ;
42   Standard_Real tangent_magnitude,
43     value,
44     angular_value,
45     factor,
46     vector_magnitude ;
47   num_poles = Curve->NbPoles() ;
48   if (( ! myCurve->IsPeriodic() )&& num_poles >= 4) {
49     
50     gp_Vec tangent,
51       diff,
52       a_vector;
53     for (ii = 1 ; ii <= 3 ; ii++) {
54       tangent.SetCoord(ii,myCurve->Pole(2).Coord(ii) - myCurve->Pole(1).Coord(ii))  ;
55       a_vector.SetCoord(ii, myCurve->Pole(3).Coord(ii) - myCurve->Pole(1).Coord(ii)) ;
56     }
57     tangent_magnitude = tangent.Magnitude() ;
58     vector_magnitude = a_vector.Magnitude() ;
59     if (tangent_magnitude > myTolerance &&
60         vector_magnitude > myTolerance)
61       {
62         value = tangent.Dot(a_vector) ;
63         if ( value < 0.0e0) {
64           for (ii = 1 ; ii <= 3 ; ii++) {
65             diff.SetCoord(ii, (tangent.Coord(ii) / tangent_magnitude) + (a_vector.Coord(ii) / vector_magnitude)) ; 
66           } 
67           angular_value = 
68             diff.Magnitude() ;
69           if (angular_value < myAngularTolerance) {
70             myFixFirstTangent = Standard_True ;
71             factor = 1.0e0 ;
72             if (tangent_magnitude > 0.5e0 * vector_magnitude) {
73               factor = 0.5e0 *  vector_magnitude / tangent_magnitude ;
74             }
75             for (ii = 1 ; ii <= 3 ; ii++) {
76               myFirstPole.SetCoord(ii, myCurve->Pole(1).Coord(ii)  - factor * tangent.Coord(ii))  ;
77             }
78           }
79           
80         }
81       }
82     for (ii = 1 ; ii <= 3 ; ii++) {
83       tangent.SetCoord(ii,myCurve->Pole(num_poles-1).Coord(ii) - myCurve->Pole(num_poles).Coord(ii))  ;
84       a_vector.SetCoord(ii, myCurve->Pole(num_poles-2).Coord(ii) - myCurve->Pole(num_poles).Coord(ii)) ;
85     }
86     tangent_magnitude = tangent.Magnitude() ;
87     vector_magnitude = a_vector.Magnitude() ;
88     
89     if (tangent_magnitude > myTolerance &&
90         vector_magnitude > myTolerance)
91       {
92         value = tangent.Dot(a_vector) ;
93         if (value < 0.0e0) {
94           for (ii = 1 ; ii <= 3 ; ii++) {
95             diff.SetCoord(ii, (tangent.Coord(ii) / tangent_magnitude) + (a_vector.Coord(ii) / vector_magnitude)) ; 
96           } 
97           angular_value = 
98             diff.Magnitude() ;
99           if ( angular_value < myAngularTolerance) {
100             myFixLastTangent = Standard_True ;
101             factor = 1.0e0 ;
102             if (tangent_magnitude > 0.5e0 * vector_magnitude) {
103               factor = 0.5e0 *  vector_magnitude / tangent_magnitude ;
104             }
105             for (ii = 1 ; ii <= 3 ; ii++) {
106               myLastPole.SetCoord(ii, myCurve->Pole(num_poles).Coord(ii)  - factor * tangent.Coord(ii))  ;
107             }
108           }
109           
110         }
111       }
112     
113   }
114   else {
115     myDone = Standard_True ;
116   }
117 }
118   
119 //=======================================================================
120 //function : NeedTangentFix
121 //purpose  : 
122 //=======================================================================
123
124 void GeomLib_CheckBSplineCurve::NeedTangentFix(Standard_Boolean & FirstFlag,
125                                                Standard_Boolean & LastFlag) const 
126 {
127   FirstFlag = myFixFirstTangent ;
128   LastFlag = myFixLastTangent ;
129 }
130 //=======================================================================
131 //function : FixTangent
132 //purpose  : 
133 //=======================================================================
134
135 Handle(Geom_BSplineCurve)  GeomLib_CheckBSplineCurve::FixedTangent(const Standard_Boolean FirstFlag,
136                                                                  const Standard_Boolean LastFlag)
137
138   Handle(Geom_BSplineCurve) new_curve ;
139   if ((myFixFirstTangent && FirstFlag) ||(myFixLastTangent && LastFlag)) {
140     new_curve =
141       Handle(Geom_BSplineCurve)::DownCast(myCurve->Copy()) ;
142     
143   }
144   if (myFixFirstTangent && FirstFlag) {
145     new_curve->SetPole(2,
146                      myFirstPole) ;
147   }
148   if (myFixLastTangent && LastFlag) {
149     Standard_Integer num_poles = myCurve->NbPoles() ;
150     new_curve->SetPole(num_poles-1,
151                      myLastPole) ;
152   }
153   
154   myDone = Standard_True ;
155   return new_curve ;
156 }                                  
157 //=======================================================================
158 //function : FixTangent
159 //purpose  : 
160 //=======================================================================
161
162 void GeomLib_CheckBSplineCurve::FixTangent(const Standard_Boolean FirstFlag,
163                                            const Standard_Boolean LastFlag)
164
165   
166   if (myFixFirstTangent && FirstFlag) {
167     myCurve->SetPole(2,
168                      myFirstPole) ;
169   }
170   if (myFixLastTangent && LastFlag) {
171     Standard_Integer num_poles = myCurve->NbPoles() ;
172     myCurve->SetPole(num_poles-1,
173                      myLastPole) ;
174   }
175   
176   myDone = Standard_True ;
177 }