0023625: New functionality building reflect lines on a shape
[occt.git] / src / HLRBRep / HLRBRep.cxx
1 // Created on: 1992-08-27
2 // Created by: Christophe MARION
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 #include <HLRBRep.ixx>
23 #include <BRepLib_MakeEdge2d.hxx>
24 #include <Geom2d_BezierCurve.hxx>
25 #include <Geom2d_BSplineCurve.hxx>
26 #include <Geom_BSplineCurve.hxx>
27 #include <TColStd_Array1OfInteger.hxx>
28 #include <TColStd_Array1OfReal.hxx>
29 #include <TColgp_Array1OfPnt2d.hxx>
30 #include <BRep_Tool.hxx>
31 #include <TopoDS.hxx>
32 #include <TopExp.hxx>
33 #include <BRepLib_MakeVertex.hxx>
34 #include <BRep_Builder.hxx>
35
36
37 //=======================================================================
38 //function : MakeEdge
39 //purpose  : 
40 //=======================================================================
41
42 TopoDS_Edge HLRBRep::MakeEdge (const HLRBRep_Curve& ec,
43                                const Standard_Real U1,
44                                const Standard_Real U2)
45 {
46   TopoDS_Edge Edg;
47   const Standard_Real sta = ec.Parameter2d(U1);
48   const Standard_Real end = ec.Parameter2d(U2);
49
50   switch (ec.GetType())
51   {
52   case GeomAbs_Line:
53     Edg = BRepLib_MakeEdge2d(ec.Line(),sta,end);
54     break;
55     
56   case GeomAbs_Circle:
57     Edg = BRepLib_MakeEdge2d(ec.Circle(),sta,end);
58     break;
59     
60   case GeomAbs_Ellipse:
61     Edg = BRepLib_MakeEdge2d(ec.Ellipse(),sta,end);
62     break;
63     
64   case GeomAbs_Hyperbola:
65     Edg = BRepLib_MakeEdge2d(ec.Hyperbola(),sta,end);
66     break;
67     
68   case GeomAbs_Parabola:
69     Edg = BRepLib_MakeEdge2d(ec.Parabola(),sta,end);
70     break;
71     
72   case GeomAbs_BezierCurve: {
73     TColgp_Array1OfPnt2d Poles(1,ec.NbPoles());
74     Handle(Geom2d_BezierCurve) ec2d;
75     if (ec.IsRational()) {
76       TColStd_Array1OfReal Weights(1,ec.NbPoles());
77       ec.PolesAndWeights(Poles,Weights);
78       ec2d = new Geom2d_BezierCurve(Poles,Weights);
79     }
80     else {
81       ec.Poles(Poles);
82       ec2d = new Geom2d_BezierCurve(Poles);
83     }
84     BRepLib_MakeEdge2d mke2d(ec2d,sta,end);
85     if (mke2d.IsDone())
86       Edg = mke2d.Edge();
87     break;
88   }
89     
90   case GeomAbs_BSplineCurve: {
91     Handle(Geom2d_BSplineCurve) ec2d;
92     GeomAdaptor_Curve GAcurve = ec.GetCurve().Curve();
93     TopoDS_Edge anEdge = ec.GetCurve().Edge();
94     Standard_Real fpar, lpar;
95     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, fpar, lpar);
96     const Handle(Geom_BSplineCurve)& BSplCurve = Handle(Geom_BSplineCurve)::DownCast(aCurve);
97     Handle(Geom_BSplineCurve) theCurve = Handle(Geom_BSplineCurve)::DownCast(BSplCurve->Copy());
98     if (theCurve->IsPeriodic() && !GAcurve.IsClosed())
99     {
100       theCurve->Segment(sta, end);
101       TColgp_Array1OfPnt2d    Poles(1, theCurve->NbPoles());
102       TColStd_Array1OfReal    knots(1, theCurve->NbKnots());
103       TColStd_Array1OfInteger mults(1, theCurve->NbKnots());
104       //-- ec.KnotsAndMultiplicities(knots,mults);
105       theCurve->Knots(knots);
106       theCurve->Multiplicities(mults);
107       if (theCurve->IsRational()) {
108         TColStd_Array1OfReal Weights(1, theCurve->NbPoles());
109         ec.PolesAndWeights(theCurve, Poles, Weights);
110         ec2d = new Geom2d_BSplineCurve(Poles, Weights, knots, mults,
111                                        theCurve->Degree(), theCurve->IsPeriodic());
112       }
113       else {
114         ec.Poles(theCurve, Poles);
115         ec2d = new Geom2d_BSplineCurve(Poles, knots, mults,
116                                        theCurve->Degree(), theCurve->IsPeriodic());
117       }
118     }
119     else
120     {
121       TColgp_Array1OfPnt2d    Poles(1,ec.NbPoles());
122       TColStd_Array1OfReal    knots(1,ec.NbKnots());
123       TColStd_Array1OfInteger mults(1,ec.NbKnots());
124       //-- ec.KnotsAndMultiplicities(knots,mults);
125       ec.Knots(knots);
126       ec.Multiplicities(mults);
127       if (ec.IsRational()) {
128         TColStd_Array1OfReal Weights(1,ec.NbPoles());
129         ec.PolesAndWeights(Poles,Weights);
130         ec2d = new Geom2d_BSplineCurve(Poles,Weights,knots,mults,ec.Degree(),ec.IsPeriodic());
131       }
132       else {
133         ec.Poles(Poles);
134         ec2d = new Geom2d_BSplineCurve(Poles,knots,mults,ec.Degree(),ec.IsPeriodic());
135       }
136     }
137     BRepLib_MakeEdge2d mke2d(ec2d, sta, end);
138     if (mke2d.IsDone())
139       Edg = mke2d.Edge();
140     break;
141   }
142   default: {
143     const Standard_Integer nbPnt = 15;
144     TColgp_Array1OfPnt2d    Poles(1,nbPnt);
145     TColStd_Array1OfReal    knots(1,nbPnt);
146     TColStd_Array1OfInteger mults(1,nbPnt);
147     mults.Init(1);
148     mults(1    ) = 2;
149     mults(nbPnt) = 2;
150     const Standard_Real step = (U2-U1)/(nbPnt-1);
151     Standard_Real par3d = U1;
152     for (Standard_Integer i = 1; i < nbPnt; i++) {
153       Poles(i) = ec.Value(par3d);
154       knots(i) = par3d;
155       par3d += step;
156     }
157     Poles(nbPnt) = ec.Value(U2);
158     knots(nbPnt) = U2;
159     
160     Handle(Geom2d_BSplineCurve) ec2d = new Geom2d_BSplineCurve(Poles,knots,mults,1);
161     BRepLib_MakeEdge2d mke2d(ec2d,sta,end);
162     if (mke2d.IsDone())
163       Edg = mke2d.Edge();
164   }
165   }
166   return Edg;
167 }
168
169 //=======================================================================
170 //function : MakeEdge3d
171 //purpose  : 
172 //=======================================================================
173
174 TopoDS_Edge HLRBRep::MakeEdge3d(const HLRBRep_Curve& ec,
175                                 const Standard_Real U1,
176                                 const Standard_Real U2)
177 {
178   TopoDS_Edge Edg;
179   //const Standard_Real sta = ec.Parameter2d(U1);
180   //const Standard_Real end = ec.Parameter2d(U2);
181
182   TopoDS_Edge anEdge = ec.GetCurve().Edge();
183   Standard_Real fpar, lpar;
184   //BRep_Tool::Range(anEdge, fpar, lpar);
185   //Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, fpar, lpar);
186   BRepAdaptor_Curve BAcurve(anEdge);
187   fpar = BAcurve.FirstParameter();
188   lpar = BAcurve.LastParameter();
189   
190   Edg = TopoDS::Edge(anEdge.EmptyCopied());
191   Edg.Orientation(TopAbs_FORWARD);
192   BRep_Builder BB;
193   BB.Range(Edg, U1, U2);
194
195   //Share vertices if possible
196   TopoDS_Vertex V1, V2, V1new, V2new;
197   TopExp::Vertices(anEdge, V1, V2);
198
199   Standard_Real Tol = Precision::PConfusion();
200   if (Abs(fpar - U1) <= Tol)
201     V1new = V1;
202   else
203   {
204     gp_Pnt aPnt = BAcurve.Value(U1);
205     V1new = BRepLib_MakeVertex(aPnt);
206   }
207   if (Abs(lpar - U2) <= Tol)
208     V2new = V2;
209   else
210   {
211     gp_Pnt aPnt = BAcurve.Value(U2);
212     V2new = BRepLib_MakeVertex(aPnt);
213   }
214
215   V1new.Orientation(TopAbs_FORWARD);
216   V2new.Orientation(TopAbs_REVERSED);
217   BB.Add(Edg, V1new);
218   BB.Add(Edg, V2new);
219   return Edg;
220 }
221
222 //=======================================================================
223 //function : PolyHLRAngleAndDeflection
224 //purpose  : 
225 //=======================================================================
226
227 void 
228 HLRBRep::PolyHLRAngleAndDeflection (const Standard_Real InAngl,
229                                     Standard_Real& OutAngl,
230                                     Standard_Real& OutDefl)
231 {
232   static Standard_Real HAngMin =  1*M_PI/180;
233   static Standard_Real HAngLim =  5*M_PI/180;
234   static Standard_Real HAngMax = 35*M_PI/180;
235
236   OutAngl = InAngl;
237   if (OutAngl < HAngMin) OutAngl = HAngMin;
238   if (OutAngl > HAngMax) OutAngl = HAngMax;
239   OutAngl = HAngLim + sqrt((OutAngl - HAngMin) * (HAngMax - HAngLim) *
240                            (HAngMax - HAngLim) / (HAngMax - HAngMin));
241   OutDefl = OutAngl * OutAngl * 0.5;
242 }