0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / DrawTrSurf / DrawTrSurf_Curve2d.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15
16 #include <Draw_Color.hxx>
17 #include <Draw_Display.hxx>
18 #include <Draw_Drawable3D.hxx>
19 #include <DrawTrSurf_Curve2d.hxx>
20 #include <Geom2d_Curve.hxx>
21 #include <Geom2d_Hyperbola.hxx>
22 #include <Geom2d_Line.hxx>
23 #include <Geom2d_OffsetCurve.hxx>
24 #include <Geom2d_Parabola.hxx>
25 #include <Geom2dAdaptor_Curve.hxx>
26 #include <Geom2dLProp_CLProps2d.hxx>
27 #include <Geom_Plane.hxx>
28 #include <GeomTools_Curve2dSet.hxx>
29 #include <gp.hxx>
30 #include <Precision.hxx>
31 #include <Standard_Type.hxx>
32 #include <TColStd_Array1OfReal.hxx>
33
34 IMPLEMENT_STANDARD_RTTIEXT(DrawTrSurf_Curve2d,DrawTrSurf_Drawable)
35
36 static Standard_Real DrawTrSurf_CurveLimit = 400;
37 extern Standard_Boolean Draw_Bounds;
38
39 //=======================================================================
40 //function : DrawTrSurf_Curve2d
41 //purpose  : 
42 //=======================================================================
43
44 DrawTrSurf_Curve2d::DrawTrSurf_Curve2d (const Handle(Geom2d_Curve)& C,
45                                         const Standard_Boolean DispOrigin) :
46        DrawTrSurf_Drawable (50)
47 {
48   curv = C;
49   look = Draw_vert;
50   disporigin = DispOrigin ;
51   dispcurvradius = Standard_False;
52   radiusmax =  1.0e3;
53   radiusratio = 0.1;
54 }
55
56
57 //=======================================================================
58 //function : DrawTrSurf_Curve2d
59 //purpose  : 
60 //=======================================================================
61
62 DrawTrSurf_Curve2d::DrawTrSurf_Curve2d (const Handle(Geom2d_Curve)& C,
63                                         const Draw_Color& aColor,
64                                         const Standard_Integer Discret,
65                                         const Standard_Boolean DispOrigin,
66                                         const Standard_Boolean DispCurvRadius,
67                                         const Standard_Real  RadiusMax,
68                                         const Standard_Real  RadiusRatio) :
69        DrawTrSurf_Drawable (Discret)
70 {
71   curv = C;
72   look = aColor;
73   disporigin = DispOrigin ;
74   dispcurvradius = DispCurvRadius;
75   radiusmax =  RadiusMax;
76   radiusratio = RadiusRatio;
77 }
78
79
80 //=======================================================================
81 //function : DrawOn
82 //purpose  : 
83 //=======================================================================
84
85 void DrawTrSurf_Curve2d::DrawOn (Draw_Display& dis) const 
86 {
87   
88   Standard_Real First = curv->FirstParameter();
89   Standard_Real Last  = curv->LastParameter();
90   Standard_Boolean firstInf = Precision::IsNegativeInfinite(First);
91   Standard_Boolean lastInf  = Precision::IsPositiveInfinite(Last);
92
93   if (firstInf || lastInf) {
94     gp_Pnt2d P1,P2;
95     Standard_Real delta = 1;
96     if (firstInf && lastInf) {
97       do {
98         delta *= 2;
99         First = - delta;
100         Last  =   delta;
101         curv->D0(First,P1);
102         curv->D0(Last,P2);
103       } while (P1.Distance(P2) < DrawTrSurf_CurveLimit);
104     }
105     else if (firstInf) {
106       curv->D0(Last,P2);
107       do {
108         delta *= 2;
109         First = Last - delta;
110         curv->D0(First,P1);
111       } while (P1.Distance(P2) < DrawTrSurf_CurveLimit);
112     }
113     else if (lastInf) {
114       curv->D0(First,P1);
115       do {
116         delta *= 2;
117         Last = First + delta;
118         curv->D0(Last,P2);
119       } while (P1.Distance(P2) < DrawTrSurf_CurveLimit);
120     }
121   }    
122   
123   dis.SetColor (look);
124   
125   Geom2dAdaptor_Curve C2d(curv,First,Last);
126   DrawCurve2dOn (C2d, dis);
127
128 // mark the origin
129   if (disporigin) {
130     Draw_Bounds = Standard_False;
131     gp_Pnt2d p1,p2;
132     gp_Vec2d v;
133     C2d.D1(Last,p1,v);
134     if (v.Magnitude() > gp::Resolution()) {
135       Standard_Real L = 20 / dis.Zoom();
136       Standard_Real H = 10 / dis.Zoom();
137       gp_Dir2d d(v);
138       p2.SetCoord(p1.X() - L*d.X() - H*d.Y(), p1.Y() - L*d.Y() + H*d.X());
139       dis.MoveTo(p2);
140       p2.SetCoord(p1.X() - L*d.X() + H*d.Y(), p1.Y() - L*d.Y() - H*d.X());
141       dis.DrawTo(p1);
142       dis.DrawTo(p2);
143     }
144     Draw_Bounds = Standard_True;
145   }
146
147 // Draw the curvature Radius      
148   if (dispcurvradius && (C2d.GetType() != GeomAbs_Line)) {
149     Standard_Integer ii;
150     Standard_Integer intrv, nbintv = C2d.NbIntervals(GeomAbs_CN);
151     TColStd_Array1OfReal TI(1,nbintv+1);
152     C2d.Intervals(TI,GeomAbs_CN);
153     Standard_Real Resolution = 1.0e-9, Curvature;
154     Geom2dLProp_CLProps2d LProp(curv, 2, Resolution);
155     gp_Pnt2d P1, P2;    
156
157     for (intrv = 1; intrv <= nbintv; intrv++) {
158         Standard_Real t = TI(intrv);
159         Standard_Real step = (TI(intrv+1) - t) / GetDiscretisation();
160         Standard_Real LRad, ratio;
161         for (ii = 1; ii <= GetDiscretisation(); ii++) {  
162           LProp.SetParameter(t);
163           if (LProp.IsTangentDefined()) {
164              Curvature = Abs(LProp.Curvature());
165              if ( Curvature >  Resolution) {
166                curv->D0(t, P1);
167                dis.MoveTo(P1);
168                LRad = 1./Curvature;
169                ratio = ( (  LRad > radiusmax) ? radiusmax/LRad : 1 );
170                ratio *= radiusratio;
171                LProp.CentreOfCurvature(P2);
172                gp_Vec2d V(P1, P2);
173                dis.DrawTo(P1.Translated(ratio*V));
174             }
175            }
176            t += step;
177         }
178       }
179   }
180 }
181
182
183
184 //=======================================================================
185 //function : Copy
186 //purpose  : 
187 //=======================================================================
188 Handle(Draw_Drawable3D)  DrawTrSurf_Curve2d::Copy()const 
189 {
190   Handle(DrawTrSurf_Curve2d) DC = new DrawTrSurf_Curve2d
191     (Handle(Geom2d_Curve)::DownCast(curv->Copy()),
192      look,
193      GetDiscretisation());
194      
195   return DC;
196 }
197
198
199 //=======================================================================
200 //function : Dump
201 //purpose  : 
202 //=======================================================================
203
204 void  DrawTrSurf_Curve2d::Dump(Standard_OStream& S) const 
205 {
206   GeomTools_Curve2dSet::PrintCurve2d(curv,S);
207 }
208
209
210 //=======================================================================
211 //function : Whatis
212 //purpose  : 
213 //=======================================================================
214
215 void  DrawTrSurf_Curve2d::Whatis(Draw_Interpretor& S)const 
216 {
217   S << "2d curve";
218 }
219
220
221 //=======================================================================
222 //function : Is3D
223 //purpose  : 
224 //=======================================================================
225
226 Standard_Boolean DrawTrSurf_Curve2d::Is3D() const
227 {
228   return Standard_False;
229 }