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 #include <DrawTrSurf_Curve2d.hxx>
16
17 #include <Draw_Color.hxx>
18 #include <Draw_Display.hxx>
19 #include <Draw_Drawable3D.hxx>
20 #include <DrawTrSurf.hxx>
21 #include <DrawTrSurf_Params.hxx>
22 #include <Geom2d_Curve.hxx>
23 #include <Geom2d_Hyperbola.hxx>
24 #include <Geom2d_Line.hxx>
25 #include <Geom2d_OffsetCurve.hxx>
26 #include <Geom2d_Parabola.hxx>
27 #include <Geom2dAdaptor_Curve.hxx>
28 #include <Geom2dLProp_CLProps2d.hxx>
29 #include <Geom_Plane.hxx>
30 #include <GeomTools_Curve2dSet.hxx>
31 #include <gp.hxx>
32 #include <Precision.hxx>
33 #include <TColStd_Array1OfReal.hxx>
34
35 IMPLEMENT_STANDARD_RTTIEXT(DrawTrSurf_Curve2d, DrawTrSurf_Drawable)
36
37 static Standard_Real DrawTrSurf_CurveLimit = 400;
38 extern Standard_Boolean Draw_Bounds;
39
40 //=======================================================================
41 //function : DrawTrSurf_Curve2d
42 //purpose :
43 //=======================================================================
DrawTrSurf_Curve2d(const Handle (Geom2d_Curve)& C,const Standard_Boolean DispOrigin)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 //function : DrawTrSurf_Curve2d
58 //purpose :
59 //=======================================================================
DrawTrSurf_Curve2d(const Handle (Geom2d_Curve)& C,const Draw_Color & aColor,const Standard_Integer Discret,const Standard_Boolean DispOrigin,const Standard_Boolean DispCurvRadius,const Standard_Real RadiusMax,const Standard_Real RadiusRatio)60 DrawTrSurf_Curve2d::DrawTrSurf_Curve2d (const Handle(Geom2d_Curve)& C,
61 const Draw_Color& aColor,
62 const Standard_Integer Discret,
63 const Standard_Boolean DispOrigin,
64 const Standard_Boolean DispCurvRadius,
65 const Standard_Real RadiusMax,
66 const Standard_Real RadiusRatio) :
67 DrawTrSurf_Drawable (Discret)
68 {
69 curv = C;
70 look = aColor;
71 disporigin = DispOrigin ;
72 dispcurvradius = DispCurvRadius;
73 radiusmax = RadiusMax;
74 radiusratio = RadiusRatio;
75 }
76
77 //=======================================================================
78 //function : DrawOn
79 //purpose :
80 //=======================================================================
DrawOn(Draw_Display & dis) const81 void DrawTrSurf_Curve2d::DrawOn (Draw_Display& dis) const
82 {
83
84 Standard_Real First = curv->FirstParameter();
85 Standard_Real Last = curv->LastParameter();
86 Standard_Boolean firstInf = Precision::IsNegativeInfinite(First);
87 Standard_Boolean lastInf = Precision::IsPositiveInfinite(Last);
88
89 if (firstInf || lastInf) {
90 gp_Pnt2d P1,P2;
91 Standard_Real delta = 1;
92 if (firstInf && lastInf) {
93 do {
94 delta *= 2;
95 First = - delta;
96 Last = delta;
97 curv->D0(First,P1);
98 curv->D0(Last,P2);
99 } while (P1.Distance(P2) < DrawTrSurf_CurveLimit);
100 }
101 else if (firstInf) {
102 curv->D0(Last,P2);
103 do {
104 delta *= 2;
105 First = Last - delta;
106 curv->D0(First,P1);
107 } while (P1.Distance(P2) < DrawTrSurf_CurveLimit);
108 }
109 else if (lastInf) {
110 curv->D0(First,P1);
111 do {
112 delta *= 2;
113 Last = First + delta;
114 curv->D0(Last,P2);
115 } while (P1.Distance(P2) < DrawTrSurf_CurveLimit);
116 }
117 }
118
119 dis.SetColor (look);
120
121 Geom2dAdaptor_Curve C2d(curv,First,Last);
122 DrawCurve2dOn (C2d, dis);
123
124 // mark the origin
125 if (disporigin) {
126 Draw_Bounds = Standard_False;
127 gp_Pnt2d p1,p2;
128 gp_Vec2d v;
129 C2d.D1(Last,p1,v);
130 if (v.Magnitude() > gp::Resolution()) {
131 Standard_Real L = 20 / dis.Zoom();
132 Standard_Real H = 10 / dis.Zoom();
133 gp_Dir2d d(v);
134 p2.SetCoord(p1.X() - L*d.X() - H*d.Y(), p1.Y() - L*d.Y() + H*d.X());
135 dis.MoveTo(p2);
136 p2.SetCoord(p1.X() - L*d.X() + H*d.Y(), p1.Y() - L*d.Y() - H*d.X());
137 dis.DrawTo(p1);
138 dis.DrawTo(p2);
139 }
140 Draw_Bounds = Standard_True;
141 }
142
143 // Draw the curvature Radius
144 if (dispcurvradius && (C2d.GetType() != GeomAbs_Line)) {
145 Standard_Integer ii;
146 Standard_Integer intrv, nbintv = C2d.NbIntervals(GeomAbs_CN);
147 TColStd_Array1OfReal TI(1,nbintv+1);
148 C2d.Intervals(TI,GeomAbs_CN);
149 Standard_Real Resolution = 1.0e-9, Curvature;
150 Geom2dLProp_CLProps2d LProp(curv, 2, Resolution);
151 gp_Pnt2d P1, P2;
152
153 for (intrv = 1; intrv <= nbintv; intrv++) {
154 Standard_Real t = TI(intrv);
155 Standard_Real step = (TI(intrv+1) - t) / GetDiscretisation();
156 Standard_Real LRad, ratio;
157 for (ii = 1; ii <= GetDiscretisation(); ii++) {
158 LProp.SetParameter(t);
159 if (LProp.IsTangentDefined()) {
160 Curvature = Abs(LProp.Curvature());
161 if ( Curvature > Resolution) {
162 curv->D0(t, P1);
163 dis.MoveTo(P1);
164 LRad = 1./Curvature;
165 ratio = ( ( LRad > radiusmax) ? radiusmax/LRad : 1 );
166 ratio *= radiusratio;
167 LProp.CentreOfCurvature(P2);
168 gp_Vec2d V(P1, P2);
169 dis.DrawTo(P1.Translated(ratio*V));
170 }
171 }
172 t += step;
173 }
174 }
175 }
176 }
177
178 //=======================================================================
179 //function : Copy
180 //purpose :
181 //=======================================================================
Handle(Draw_Drawable3D)182 Handle(Draw_Drawable3D) DrawTrSurf_Curve2d::Copy() const
183 {
184 Handle(DrawTrSurf_Curve2d) DC = new DrawTrSurf_Curve2d
185 (Handle(Geom2d_Curve)::DownCast(curv->Copy()),
186 look,
187 GetDiscretisation());
188
189 return DC;
190 }
191
192 //=======================================================================
193 //function : Dump
194 //purpose :
195 //=======================================================================
Dump(Standard_OStream & S) const196 void DrawTrSurf_Curve2d::Dump (Standard_OStream& S) const
197 {
198 GeomTools_Curve2dSet::PrintCurve2d (curv, S);
199 }
200
201 //=======================================================================
202 //function : Save
203 //purpose :
204 //=======================================================================
Save(Standard_OStream & theStream) const205 void DrawTrSurf_Curve2d::Save (Standard_OStream& theStream) const
206 {
207 GeomTools_Curve2dSet::PrintCurve2d (GetCurve(), theStream, true);
208 }
209
210 //=======================================================================
211 //function : Restore
212 //purpose :
213 //=======================================================================
Handle(Draw_Drawable3D)214 Handle(Draw_Drawable3D) DrawTrSurf_Curve2d::Restore (Standard_IStream& theStream)
215 {
216 const DrawTrSurf_Params& aParams = DrawTrSurf::Parameters();
217 Handle(Geom2d_Curve) aGeomCurve = GeomTools_Curve2dSet::ReadCurve2d (theStream);
218 Handle(DrawTrSurf_Curve2d) aDrawCurve = new DrawTrSurf_Curve2d (aGeomCurve, aParams.CurvColor, aParams.Discret);
219 return aDrawCurve;
220 }
221
222 //=======================================================================
223 //function : Whatis
224 //purpose :
225 //=======================================================================
Whatis(Draw_Interpretor & S) const226 void DrawTrSurf_Curve2d::Whatis (Draw_Interpretor& S) const
227 {
228 S << "2d curve";
229 }
230
231 //=======================================================================
232 //function : Is3D
233 //purpose :
234 //=======================================================================
Is3D() const235 Standard_Boolean DrawTrSurf_Curve2d::Is3D() const
236 {
237 return Standard_False;
238 }
239