0023706: Cannot project point on curve
[occt.git] / src / DrawTrSurf / DrawTrSurf_Drawable.cxx
CommitLineData
b311480e 1// Created on: 1991-07-16
2// Created by: Christophe MARION
3// Copyright (c) 1991-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
7fd59977 21
22
23#include <DrawTrSurf_Drawable.ixx>
24#include <GCPnts_UniformDeflection.hxx>
25#include <gp_Pnt.hxx>
26#include <gp_Pnt2d.hxx>
27#include <TColStd_Array1OfReal.hxx>
28#include <GeomAdaptor_Curve.hxx>
29#include <Geom_BezierCurve.hxx>
30#include <Geom_BSplineCurve.hxx>
31#include <Precision.hxx>
32
33
34//=======================================================================
35//function : DrawTrSurf_Drawable
36//purpose : initialise the discretisation
37//=======================================================================
38
39DrawTrSurf_Drawable::DrawTrSurf_Drawable (
40
41const Standard_Integer discret,
42const Standard_Real deflection,
43const Standard_Integer DrawMode ) :
44 myDrawMode (DrawMode),
45 myDiscret(discret),
46 myDeflection(deflection)
47{
48}
49
50//=======================================================================
51//function : DrawCurve2dOn
52//purpose : draw a 2D curve
53//=======================================================================
54
55void DrawTrSurf_Drawable::DrawCurve2dOn (Adaptor2d_Curve2d& C,
56 Draw_Display& aDisplay) const
57{
58 gp_Pnt P;
59
60 gp_Pnt2d aPoint2d,
61 *aPoint2dPtr ;
62 if (myDrawMode == 1) {
63 Standard_Real Fleche = myDeflection/aDisplay.Zoom();
64 GCPnts_UniformDeflection LineVu(C,Fleche);
65 if (LineVu.IsDone()) {
66 P = LineVu.Value(1) ;
67 aPoint2dPtr = (gp_Pnt2d *) &P ;
68 aDisplay.MoveTo(*aPoint2dPtr);
69 for (Standard_Integer i = 2; i <= LineVu.NbPoints(); i++) {
70 P = LineVu.Value(i) ;
71 aPoint2dPtr = (gp_Pnt2d *) &P ;
72 aDisplay.DrawTo(*aPoint2dPtr);
73 }
74 }
75 }
76 else {
77 Standard_Integer intrv, nbintv = C.NbIntervals(GeomAbs_CN);
78 TColStd_Array1OfReal TI(1,nbintv+1);
79 C.Intervals(TI,GeomAbs_CN);
80 C.D0(C.FirstParameter(),aPoint2d);
81 aDisplay.MoveTo(aPoint2d);
82 for (intrv = 1; intrv <= nbintv; intrv++) {
83 if (C.GetType() != GeomAbs_Line) {
84 Standard_Real t = TI(intrv);
85 Standard_Real step = (TI(intrv+1) - t) / myDiscret;
86 for (Standard_Integer i = 1; i < myDiscret; i++) {
87 t += step;
88 C.D0(t,aPoint2d);
89 aDisplay.DrawTo(aPoint2d);
90 }
91 }
92 C.D0(TI(intrv+1),aPoint2d);
93 aDisplay.DrawTo(aPoint2d);
94 }
95 }
96}
97
98//=======================================================================
99//static function : PlotCurve
100//purpose : draw a 3D curve
101//=======================================================================
102static void PlotCurve (Draw_Display& aDisplay,
103 const Adaptor3d_Curve& C,
104 Standard_Real& theFirstParam,
105 Standard_Real theHalfStep,
106 const gp_Pnt& theFirstPnt,
107 const gp_Pnt& theLastPnt)
108{
109 Standard_Real IsoRatio = 1.001;
110 gp_Pnt Pm;
111
112 C.D0 (theFirstParam + theHalfStep, Pm);
113
114 Standard_Real dfLength = theFirstPnt.Distance(theLastPnt);
115 if (dfLength < Precision::Confusion() ||
116 Pm.Distance(theFirstPnt) + Pm.Distance(theLastPnt) <= IsoRatio*dfLength) {
117 aDisplay.DrawTo (theLastPnt);
118 } else {
119 PlotCurve (aDisplay, C, theFirstParam, theHalfStep/2., theFirstPnt, Pm);
120 Standard_Real aLocalF = theFirstParam + theHalfStep;
121 PlotCurve (aDisplay, C, aLocalF, theHalfStep/2., Pm, theLastPnt);
122 }
123}
124//=======================================================================
125//function : DrawCurveOn
126//purpose : draw a 3D curve
127//=======================================================================
128
129void DrawTrSurf_Drawable::DrawCurveOn (Adaptor3d_Curve& C,
32ca7a51 130 Draw_Display& aDisplay) const
7fd59977 131{
132 gp_Pnt P;
32ca7a51 133 if (myDrawMode == 1)
134 {
7fd59977 135 Standard_Real Fleche = myDeflection/aDisplay.Zoom();
136 GCPnts_UniformDeflection LineVu(C,Fleche);
32ca7a51 137 if (LineVu.IsDone())
138 {
7fd59977 139 aDisplay.MoveTo(LineVu.Value(1));
32ca7a51 140 for (Standard_Integer i = 2; i <= LineVu.NbPoints(); i++)
141 {
142 aDisplay.DrawTo(LineVu.Value(i));
7fd59977 143 }
32ca7a51 144 }
7fd59977 145 }
32ca7a51 146 else
147 {
148 Standard_Integer j;
7fd59977 149 Standard_Integer intrv, nbintv = C.NbIntervals(GeomAbs_CN);
150 TColStd_Array1OfReal TI(1,nbintv+1);
151 C.Intervals(TI,GeomAbs_CN);
152 C.D0(C.FirstParameter(),P);
153 aDisplay.MoveTo(P);
154 GeomAbs_CurveType CurvType = C.GetType();
155 gp_Pnt aPPnt=P, aNPnt;
156
32ca7a51 157 for (intrv = 1; intrv <= nbintv; intrv++)
158 {
7fd59977 159 Standard_Real t = TI(intrv);
160 Standard_Real step = (TI(intrv+1) - t) / myDiscret;
161
32ca7a51 162 switch (CurvType)
163 {
164 case GeomAbs_Line:
165 break;
166 case GeomAbs_Circle:
167 case GeomAbs_Ellipse:
168 for (j = 1; j < myDiscret; j++)
169 {
170 t += step;
171 C.D0(t,P);
172 aDisplay.DrawTo(P);
173 }
174 break;
175 case GeomAbs_Parabola:
176 case GeomAbs_Hyperbola:
177 case GeomAbs_BezierCurve:
178 case GeomAbs_BSplineCurve:
179 case GeomAbs_OtherCurve:
180 const Standard_Integer nIter = myDiscret/2;
181 for (j = 1; j < nIter; j++)
182 {
183 const Standard_Real t1 = t+step*2.;
184 C.D0 (t1, aNPnt);
7fd59977 185 PlotCurve (aDisplay, C, t, step, aPPnt, aNPnt);
32ca7a51 186 aPPnt = aNPnt;
187 t = t1;
188 }
189
190 break;
7fd59977 191 }
192
193 C.D0(TI(intrv+1),P);
32ca7a51 194 PlotCurve (aDisplay, C, t, step, aPPnt, P);
7fd59977 195 aDisplay.DrawTo(P);
196 }
197 }
198}
199
200
201//=======================================================================
202//function : DrawIsoCurveOn
203//purpose :
204//=======================================================================
205
206void DrawTrSurf_Drawable::DrawIsoCurveOn(Adaptor3d_IsoCurve& C,
207 const GeomAbs_IsoType T,
208 const Standard_Real P,
209 const Standard_Real F,
210 const Standard_Real L,
211 Draw_Display& dis) const
212{
213 C.Load(T,P,F,L);
214 if ((C.GetType() == GeomAbs_BezierCurve) ||
215 (C.GetType() == GeomAbs_BSplineCurve)) {
216 GeomAdaptor_Curve GC;
217 if (C.GetType() == GeomAbs_BezierCurve)
218 GC.Load(C.Bezier(),F,L);
219 else
220 GC.Load(C.BSpline(),F,L);
221
222 DrawCurveOn(GC,dis);
223 }
224 else
225 DrawCurveOn(C,dis);
226
227}