0024023: Revamp the OCCT Handle -- ambiguity
[occt.git] / src / StdPrs / StdPrs_Curve.cxx
CommitLineData
b311480e 1// Created on: 1995-08-04
2// Created by: Modelistation
3// Copyright (c) 1995-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
17// Great zoom leads to non-coincidence of
18// a point and non-infinite lines passing throught this point:
7fd59977 19
20#include <StdPrs_Curve.ixx>
21
b8ddfc2f 22#include <Graphic3d_ArrayOfSegments.hxx>
23#include <Graphic3d_ArrayOfPolylines.hxx>
7fd59977 24#include <Graphic3d_Group.hxx>
25#include <Prs3d_LineAspect.hxx>
26#include <Prs3d_Arrow.hxx>
27#include <Prs3d_ArrowAspect.hxx>
28#include <gp_Pnt.hxx>
29#include <gp_Circ.hxx>
30#include <gp_Dir.hxx>
31#include <gp_Vec.hxx>
32#include <Prs3d.hxx>
33#include <Bnd_Box.hxx>
34#include <Precision.hxx>
35#include <TColgp_SequenceOfPnt.hxx>
36
37
7fd59977 38//==================================================================
39// function: FindLimits
40// purpose:
41//==================================================================
42static void FindLimits(const Adaptor3d_Curve& aCurve,
b8ddfc2f 43 const Standard_Real aLimit,
44 Standard_Real& First,
45 Standard_Real& Last)
7fd59977 46{
47 First = aCurve.FirstParameter();
48 Last = aCurve.LastParameter();
49 Standard_Boolean firstInf = Precision::IsNegativeInfinite(First);
50 Standard_Boolean lastInf = Precision::IsPositiveInfinite(Last);
51
52 if (firstInf || lastInf) {
53 gp_Pnt P1,P2;
54 Standard_Real delta = 1;
55 if (firstInf && lastInf) {
56 do {
b8ddfc2f 57 delta *= 2;
58 First = - delta;
59 Last = delta;
60 aCurve.D0(First,P1);
61 aCurve.D0(Last,P2);
7fd59977 62 } while (P1.Distance(P2) < aLimit);
63 }
64 else if (firstInf) {
65 aCurve.D0(Last,P2);
66 do {
b8ddfc2f 67 delta *= 2;
68 First = Last - delta;
69 aCurve.D0(First,P1);
7fd59977 70 } while (P1.Distance(P2) < aLimit);
71 }
72 else if (lastInf) {
73 aCurve.D0(First,P1);
74 do {
b8ddfc2f 75 delta *= 2;
76 Last = First + delta;
77 aCurve.D0(Last,P2);
7fd59977 78 } while (P1.Distance(P2) < aLimit);
79 }
80 }
81}
82
83
7fd59977 84//==================================================================
85// function: DrawCurve
86// purpose:
87//==================================================================
b8ddfc2f 88static void DrawCurve (const Adaptor3d_Curve& aCurve,
7fd59977 89 const Handle(Graphic3d_Group) aGroup,
b8ddfc2f 90 const Standard_Integer NbP,
7fd59977 91 const Standard_Real U1,
92 const Standard_Real U2,
b8ddfc2f 93 TColgp_SequenceOfPnt& Points,
94 const Standard_Boolean drawCurve)
7fd59977 95{
96 Standard_Integer nbintervals = 1;
b8ddfc2f 97
7fd59977 98 if (aCurve.GetType() == GeomAbs_BSplineCurve) {
99 nbintervals = aCurve.NbKnots() - 1;
100 nbintervals = Max(1, nbintervals/3);
101 }
102
b8ddfc2f 103 switch (aCurve.GetType())
104 {
105 case GeomAbs_Line:
7fd59977 106 {
b8ddfc2f 107 gp_Pnt p1 = aCurve.Value(U1);
108 gp_Pnt p2 = aCurve.Value(U2);
109 Points.Append(p1);
110 Points.Append(p2);
111 if(drawCurve)
112 {
113 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
114 aPrims->AddVertex(p1);
115 aPrims->AddVertex(p2);
116 aGroup->AddPrimitiveArray(aPrims);
117 }
118 }
7fd59977 119 break;
b8ddfc2f 120 default:
7fd59977 121 {
b8ddfc2f 122 const Standard_Integer N = Max(2, NbP*nbintervals);
123 const Standard_Real DU = (U2-U1) / (N-1);
7fd59977 124 gp_Pnt p;
125
b8ddfc2f 126 Handle(Graphic3d_ArrayOfPolylines) aPrims;
127 if(drawCurve)
128 aPrims = new Graphic3d_ArrayOfPolylines(N);
129
130 for (Standard_Integer i = 1; i <= N;i++) {
131 p = aCurve.Value(U1 + (i-1)*DU);
132 Points.Append(p);
133 if(drawCurve)
134 aPrims->AddVertex(p);
7fd59977 135 }
b8ddfc2f 136 if(drawCurve)
137 aGroup->AddPrimitiveArray(aPrims);
7fd59977 138 }
139 }
140}
141
142
7fd59977 143//==================================================================
144// function: MatchCurve
145// purpose:
146//==================================================================
147static Standard_Boolean MatchCurve (
148 const Quantity_Length X,
149 const Quantity_Length Y,
150 const Quantity_Length Z,
151 const Quantity_Length aDistance,
152 const Adaptor3d_Curve& aCurve,
b8ddfc2f 153 const Quantity_Length TheDeflection,
7fd59977 154 const Standard_Integer NbP,
b8ddfc2f 155 const Standard_Real U1,
156 const Standard_Real U2)
7fd59977 157{
158 Quantity_Length retdist;
b8ddfc2f 159 switch (aCurve.GetType())
160 {
161 case GeomAbs_Line:
7fd59977 162 {
b8ddfc2f 163 gp_Pnt p1 = aCurve.Value(U1);
164 if ( Abs(X-p1.X()) + Abs(Y-p1.Y()) + Abs(Z-p1.Z()) <= aDistance)
165 return Standard_True;
166 gp_Pnt p2 = aCurve.Value(U2);
167 if ( Abs(X-p2.X()) + Abs(Y-p2.Y()) + Abs(Z-p2.Z()) <= aDistance)
168 return Standard_True;
169 return Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist);
170 }
171 case GeomAbs_Circle:
172 {
173 const Standard_Real Radius = aCurve.Circle().Radius();
174 const Standard_Real DU = Sqrt(8.0 * TheDeflection / Radius);
175 const Standard_Real Er = Abs( U2 - U1) / DU;
176 const Standard_Integer N = Max(2, (Standard_Integer)IntegerPart(Er));
177 if ( N > 0) {
178 gp_Pnt p1,p2;
179 for (Standard_Integer Index = 1; Index <= N+1; Index++) {
180 p2 = aCurve.Value(U1 + (Index - 1) * DU);
181 if ( Abs(X-p2.X()) + Abs(Y-p2.Y()) + Abs(Z-p2.Z()) <= aDistance)
182 return Standard_True;
183
184 if (Index>1) {
185 if (Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist))
186 return Standard_True;
187 }
188 p1=p2;
189 }
190 }
191 break;
192 }
193 default:
7fd59977 194 {
b8ddfc2f 195 const Standard_Real DU = (U2-U1) / (NbP-1);
7fd59977 196 gp_Pnt p1,p2;
b8ddfc2f 197 for (Standard_Integer i=1;i<=NbP;i++) {
198 p2 = aCurve.Value(U1 + (i-1)*DU);
199 if ( Abs(X-p2.X()) + Abs(Y-p2.Y()) + Abs(Z-p2.Z()) <= aDistance)
200 return Standard_True;
201 if (i>1) {
202 if (Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist))
203 return Standard_True;
204 }
205 p1=p2;
7fd59977 206 }
7fd59977 207 }
7fd59977 208 }
209 return Standard_False;
210}
211
212
213//==================================================================
214// function: Add
215// purpose:
216//==================================================================
217void StdPrs_Curve::Add (const Handle (Prs3d_Presentation)& aPresentation,
b8ddfc2f 218 const Adaptor3d_Curve& aCurve,
219 const Handle (Prs3d_Drawer)& aDrawer,
220 const Standard_Boolean drawCurve)
221{
222 Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(aDrawer->LineAspect()->Aspect());
7fd59977 223
7fd59977 224 Standard_Real V1, V2;
225 FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2);
226
b8ddfc2f 227 const Standard_Integer NbPoints = aDrawer->Discretisation();
7fd59977 228 TColgp_SequenceOfPnt Pnts;
b8ddfc2f 229 DrawCurve(aCurve,Prs3d_Root::CurrentGroup(aPresentation),NbPoints,V1,V2,Pnts,drawCurve);
7fd59977 230
231 if (aDrawer->LineArrowDraw()) {
232 gp_Pnt Location;
233 gp_Vec Direction;
234 aCurve.D1(aCurve.LastParameter(),Location,Direction);
b8ddfc2f 235 Prs3d_Arrow::Draw (aPresentation,Location,gp_Dir(Direction),
236 aDrawer->ArrowAspect()->Angle(),
237 aDrawer->ArrowAspect()->Length());
7fd59977 238 }
239}
240
241
242//==================================================================
243// function: Add
244// purpose:
245//==================================================================
246void StdPrs_Curve::Add (const Handle (Prs3d_Presentation)& aPresentation,
b8ddfc2f 247 const Adaptor3d_Curve& aCurve,
302f96fb 248 const Quantity_Length /*aDeflection*/,
b8ddfc2f 249 const Handle(Prs3d_Drawer)& aDrawer,
250 TColgp_SequenceOfPnt& Points,
251 const Standard_Boolean drawCurve)
7fd59977 252{
7fd59977 253 Standard_Real V1, V2;
b8ddfc2f 254 FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2);
7fd59977 255
b8ddfc2f 256 const Standard_Integer NbPoints = aDrawer->Discretisation();
257 DrawCurve(aCurve,Prs3d_Root::CurrentGroup(aPresentation),NbPoints,V1,V2,Points,drawCurve);
7fd59977 258}
259
b8ddfc2f 260
7fd59977 261//==================================================================
262// function: Add
263// purpose:
264//==================================================================
265void StdPrs_Curve::Add (const Handle (Prs3d_Presentation)& aPresentation,
b8ddfc2f 266 const Adaptor3d_Curve& aCurve,
267 const Standard_Real U1,
268 const Standard_Real U2,
302f96fb 269 const Quantity_Length /*aDeflection*/,
b8ddfc2f 270 TColgp_SequenceOfPnt& Points,
271 const Standard_Integer NbPoints,
272 const Standard_Boolean drawCurve)
273{
274 DrawCurve(aCurve,Prs3d_Root::CurrentGroup(aPresentation),NbPoints,U1,U2,Points,drawCurve);
7fd59977 275}
276
277
7fd59977 278//==================================================================
279// function: Add
280// purpose:
281//==================================================================
282void StdPrs_Curve::Add (const Handle (Prs3d_Presentation)& aPresentation,
b8ddfc2f 283 const Adaptor3d_Curve& aCurve,
284 const Standard_Real U1,
285 const Standard_Real U2,
286 const Handle (Prs3d_Drawer)& aDrawer,
287 const Standard_Boolean drawCurve)
288{
7fd59977 289 Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(aDrawer->LineAspect()->Aspect());
290
7fd59977 291 Standard_Real V1 = U1;
292 Standard_Real V2 = U2;
293
294 if (Precision::IsNegativeInfinite(V1)) V1 = -aDrawer->MaximalParameterValue();
295 if (Precision::IsPositiveInfinite(V2)) V2 = aDrawer->MaximalParameterValue();
296
b8ddfc2f 297 const Standard_Integer NbPoints = aDrawer->Discretisation();
7fd59977 298 TColgp_SequenceOfPnt Pnts;
b8ddfc2f 299 DrawCurve(aCurve,Prs3d_Root::CurrentGroup(aPresentation),NbPoints,V1,V2,Pnts,drawCurve);
7fd59977 300
301 if (aDrawer->LineArrowDraw()) {
302 gp_Pnt Location;
303 gp_Vec Direction;
304 aCurve.D1(aCurve.LastParameter(),Location,Direction);
b8ddfc2f 305 Prs3d_Arrow::Draw (aPresentation,Location,gp_Dir(Direction),
306 aDrawer->ArrowAspect()->Angle(),
307 aDrawer->ArrowAspect()->Length());
7fd59977 308 }
309}
310
311
312//==================================================================
313// function: Match
314// purpose:
315//==================================================================
316Standard_Boolean StdPrs_Curve::Match
317 (const Quantity_Length X,
318 const Quantity_Length Y,
319 const Quantity_Length Z,
320 const Quantity_Length aDistance,
b8ddfc2f 321 const Adaptor3d_Curve& aCurve,
7fd59977 322 const Handle (Prs3d_Drawer)& aDrawer)
323{
7fd59977 324 Standard_Real V1, V2;
325 FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2);
326
b8ddfc2f 327 const Standard_Integer NbPoints = aDrawer->Discretisation();
7fd59977 328 return MatchCurve(X,Y,Z,aDistance,aCurve,
b8ddfc2f 329 aDrawer->MaximalChordialDeviation(),NbPoints,V1,V2);
7fd59977 330
331}
332
b8ddfc2f 333
7fd59977 334//==================================================================
335// function: Match
336// purpose:
337//==================================================================
338Standard_Boolean StdPrs_Curve::Match
339 (const Quantity_Length X,
340 const Quantity_Length Y,
341 const Quantity_Length Z,
342 const Quantity_Length aDistance,
343 const Adaptor3d_Curve& aCurve,
344 const Quantity_Length aDeflection,
345 const Standard_Real aLimit,
b8ddfc2f 346 const Standard_Integer NbPoints)
347{
7fd59977 348 Standard_Real V1, V2;
349 FindLimits(aCurve, aLimit, V1, V2);
350
351 return MatchCurve(X,Y,Z,aDistance,aCurve,
b8ddfc2f 352 aDeflection,NbPoints,V1,V2);
7fd59977 353}
354
355
7fd59977 356//==================================================================
357// function: Match
358// purpose:
359//==================================================================
360Standard_Boolean StdPrs_Curve::Match
361 (const Quantity_Length X,
362 const Quantity_Length Y,
363 const Quantity_Length Z,
364 const Quantity_Length aDistance,
365 const Adaptor3d_Curve& aCurve,
366 const Standard_Real U1,
367 const Standard_Real U2,
b8ddfc2f 368 const Handle (Prs3d_Drawer)& aDrawer)
369{
7fd59977 370 Standard_Real V1 = U1;
371 Standard_Real V2 = U2;
372
373 if (Precision::IsNegativeInfinite(V1)) V1 = -aDrawer->MaximalParameterValue();
374 if (Precision::IsPositiveInfinite(V2)) V2 = aDrawer->MaximalParameterValue();
375
376 return MatchCurve(X,Y,Z,aDistance,aCurve,
b8ddfc2f 377 aDrawer->MaximalChordialDeviation(),
378 aDrawer->Discretisation(),V1,V2);
7fd59977 379}
380
381
382//==================================================================
383// function: Match
384// purpose:
385//==================================================================
386Standard_Boolean StdPrs_Curve::Match
387 (const Quantity_Length X,
388 const Quantity_Length Y,
389 const Quantity_Length Z,
390 const Quantity_Length aDistance,
391 const Adaptor3d_Curve& aCurve,
392 const Standard_Real U1,
393 const Standard_Real U2,
394 const Quantity_Length aDeflection,
395 const Standard_Integer aNbPoints)
396{
b8ddfc2f 397 return MatchCurve(X,Y,Z,aDistance,aCurve,aDeflection,aNbPoints,U1,U2);
7fd59977 398}