0027756: Visualization - add Draw() method taking Graphic3d_Group to tools Prs3d_Arro...
[occt.git] / src / StdPrs / StdPrs_DeflectionCurve.cxx
CommitLineData
b311480e 1// Copyright (c) 1995-1999 Matra Datavision
973c2be1 2// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 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
973c2be1 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.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
b311480e 14
7fd59977 15// Great zoom leads to non-coincidence of
16// a point and non-infinite lines passing throught this point:
7fd59977 17
42cf5bc1 18#include <Adaptor3d_Curve.hxx>
7fd59977 19#include <Bnd_Box.hxx>
20#include <BndLib_Add3dCurve.hxx>
7fd59977 21#include <GCPnts_QuasiUniformDeflection.hxx>
22#include <GCPnts_TangentialDeflection.hxx>
42cf5bc1 23#include <gp_Circ.hxx>
24#include <gp_Dir.hxx>
25#include <gp_Pnt.hxx>
26#include <gp_Vec.hxx>
27#include <Graphic3d_ArrayOfPolylines.hxx>
28#include <Graphic3d_ArrayOfSegments.hxx>
29#include <Graphic3d_Group.hxx>
30#include <Precision.hxx>
31#include <Prs3d.hxx>
32#include <Prs3d_Arrow.hxx>
33#include <Prs3d_ArrowAspect.hxx>
34#include <Prs3d_LineAspect.hxx>
35#include <Prs3d_Presentation.hxx>
36#include <StdPrs_DeflectionCurve.hxx>
7fd59977 37#include <TColgp_SequenceOfPnt.hxx>
38#include <TColStd_Array1OfReal.hxx>
39
7fd59977 40//==================================================================
41// function: GetDeflection
42// purpose:
43//==================================================================
44static Standard_Real GetDeflection(const Adaptor3d_Curve& aCurve,
b8ddfc2f 45 const Standard_Real U1,
46 const Standard_Real U2,
47 const Handle(Prs3d_Drawer)& aDrawer)
48{
49 Standard_Real TheDeflection;
50
51 if (aDrawer->TypeOfDeflection() == Aspect_TOD_RELATIVE)
52 {
53 // On calcule la fleche en fonction des min max globaux de la piece:
54 Bnd_Box Total;
55 BndLib_Add3dCurve::Add(aCurve, U1, U2, 0.,Total);
56 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
57 Total.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
58 Standard_Real m = RealLast();
59 if ( ! (Total.IsOpenXmin() || Total.IsOpenXmax() ))
60 m = Abs (aXmax-aXmin);
61 if ( ! (Total.IsOpenYmin() || Total.IsOpenYmax() ))
62 m = Max ( m , Abs (aYmax-aYmin));
63 if ( ! (Total.IsOpenZmin() || Total.IsOpenZmax() ))
64 m = Max ( m , Abs (aZmax-aZmin));
7fd59977 65
b8ddfc2f 66 m = Min ( m , aDrawer->MaximalParameterValue());
67 m = Max(m, Precision::Confusion());
7fd59977 68
b8ddfc2f 69 TheDeflection = m * aDrawer->DeviationCoefficient();
7fd59977 70 }
b8ddfc2f 71 else
72 TheDeflection = aDrawer->MaximalChordialDeviation();
73
74 return TheDeflection;
75}
7fd59977 76
77//==================================================================
78// function: FindLimits
79// purpose:
80//==================================================================
b8ddfc2f 81static Standard_Boolean FindLimits(const Adaptor3d_Curve& aCurve,
82 const Standard_Real aLimit,
83 Standard_Real& First,
84 Standard_Real& Last)
7fd59977 85{
86 First = aCurve.FirstParameter();
87 Last = aCurve.LastParameter();
88 Standard_Boolean firstInf = Precision::IsNegativeInfinite(First);
89 Standard_Boolean lastInf = Precision::IsPositiveInfinite(Last);
90
91 if (firstInf || lastInf) {
92 gp_Pnt P1,P2;
93 Standard_Real delta = 1;
94 Standard_Integer count = 0;
95 if (firstInf && lastInf) {
96 do {
b8ddfc2f 97 if (count++ == 100000) return Standard_False;
98 delta *= 2;
99 First = - delta;
100 Last = delta;
101 aCurve.D0(First,P1);
102 aCurve.D0(Last,P2);
7fd59977 103 } while (P1.Distance(P2) < aLimit);
104 }
105 else if (firstInf) {
106 aCurve.D0(Last,P2);
107 do {
b8ddfc2f 108 if (count++ == 100000) return Standard_False;
109 delta *= 2;
110 First = Last - delta;
111 aCurve.D0(First,P1);
7fd59977 112 } while (P1.Distance(P2) < aLimit);
113 }
114 else if (lastInf) {
115 aCurve.D0(First,P1);
116 do {
b8ddfc2f 117 if (count++ == 100000) return Standard_False;
118 delta *= 2;
119 Last = First + delta;
120 aCurve.D0(Last,P2);
7fd59977 121 } while (P1.Distance(P2) < aLimit);
122 }
123 }
124 return Standard_True;
125}
126
127
7fd59977 128//==================================================================
8d3aa19e 129// function: drawCurve
7fd59977 130// purpose:
131//==================================================================
8d3aa19e 132static void drawCurve (Adaptor3d_Curve& aCurve,
133 const Handle(Graphic3d_Group)& aGroup,
7fd59977 134 const Quantity_Length TheDeflection,
b8ddfc2f 135 const Standard_Real anAngle,
7fd59977 136 const Standard_Real U1,
137 const Standard_Real U2,
8d3aa19e 138 TColgp_SequenceOfPnt& Points)
7fd59977 139{
b8ddfc2f 140 switch (aCurve.GetType())
141 {
142 case GeomAbs_Line:
7fd59977 143 {
b8ddfc2f 144 gp_Pnt p1 = aCurve.Value(U1);
145 gp_Pnt p2 = aCurve.Value(U2);
146 Points.Append(p1);
147 Points.Append(p2);
8d3aa19e 148 if (!aGroup.IsNull())
b8ddfc2f 149 {
150 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
151 aPrims->AddVertex(p1);
152 aPrims->AddVertex(p2);
153 aGroup->AddPrimitiveArray(aPrims);
154 }
155 break;
156 }
157 default:
7fd59977 158 {
b8ddfc2f 159 const Standard_Integer nbinter = aCurve.NbIntervals(GeomAbs_C1);
7fd59977 160 TColStd_Array1OfReal T(1, nbinter+1);
161 aCurve.Intervals(T, GeomAbs_C1);
b8ddfc2f 162
7fd59977 163 Standard_Real theU1, theU2;
b8ddfc2f 164 Standard_Integer NumberOfPoints, i, j;
7fd59977 165 TColgp_SequenceOfPnt SeqP;
166
167 for (j = 1; j <= nbinter; j++) {
b8ddfc2f 168 theU1 = T(j); theU2 = T(j+1);
169 if (theU2 > U1 && theU1 < U2) {
170 theU1 = Max(theU1, U1);
171 theU2 = Min(theU2, U2);
7fd59977 172
b8ddfc2f 173 GCPnts_TangentialDeflection Algo(aCurve, theU1, theU2, anAngle, TheDeflection);
174 NumberOfPoints = Algo.NbPoints();
175
176 if (NumberOfPoints > 0) {
c59fcd11 177 for (i = 1; i <= NumberOfPoints; i++)
b8ddfc2f 178 SeqP.Append(Algo.Value(i));
b8ddfc2f 179 }
180 }
7fd59977 181 }
182
b8ddfc2f 183 Handle(Graphic3d_ArrayOfPolylines) aPrims;
8d3aa19e 184 if (!aGroup.IsNull())
b8ddfc2f 185 aPrims = new Graphic3d_ArrayOfPolylines(SeqP.Length());
7fd59977 186
7fd59977 187 for (i = 1; i <= SeqP.Length(); i++) {
b8ddfc2f 188 const gp_Pnt& p = SeqP.Value(i);
189 Points.Append(p);
8d3aa19e 190 if (!aGroup.IsNull())
191 {
b8ddfc2f 192 aPrims->AddVertex(p);
8d3aa19e 193 }
194 }
195 if (!aGroup.IsNull())
196 {
197 aGroup->AddPrimitiveArray (aPrims);
7fd59977 198 }
7fd59977 199 }
200 }
7fd59977 201}
202
203
204//==================================================================
205// function: MatchCurve
206// purpose:
207//==================================================================
208static Standard_Boolean MatchCurve (
209 const Quantity_Length X,
210 const Quantity_Length Y,
211 const Quantity_Length Z,
212 const Quantity_Length aDistance,
213 const Adaptor3d_Curve& aCurve,
b8ddfc2f 214 const Quantity_Length TheDeflection,
7fd59977 215 const Standard_Real anAngle,
b8ddfc2f 216 const Standard_Real U1,
217 const Standard_Real U2)
7fd59977 218{
219 Quantity_Length retdist;
b8ddfc2f 220 switch (aCurve.GetType())
221 {
222 case GeomAbs_Line:
7fd59977 223 {
b8ddfc2f 224 gp_Pnt p1 = aCurve.Value(U1);
225 if ( Abs(X-p1.X()) + Abs(Y-p1.Y()) + Abs(Z-p1.Z()) <= aDistance)
226 return Standard_True;
227 gp_Pnt p2 = aCurve.Value(U2);
228 if ( Abs(X-p2.X()) + Abs(Y-p2.Y()) + Abs(Z-p2.Z()) <= aDistance)
229 return Standard_True;
230 return Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist);
231 }
232 case GeomAbs_Circle:
7fd59977 233 {
b8ddfc2f 234 const Standard_Real Radius = aCurve.Circle().Radius();
7fd59977 235 if (!Precision::IsInfinite(Radius)) {
b8ddfc2f 236 const Standard_Real DU = Sqrt(8.0 * TheDeflection / Radius);
237 const Standard_Real Er = Abs( U2 - U1) / DU;
238 const Standard_Integer N = Max(2, (Standard_Integer)IntegerPart(Er));
239 if ( N > 0) {
240 gp_Pnt p1,p2;
241 for (Standard_Integer Index = 1; Index <= N+1; Index++) {
242 p2 = aCurve.Value(U1 + (Index - 1) * DU);
243 if ( Abs(X-p2.X()) + Abs(Y-p2.Y()) + Abs(Z-p2.Z()) <= aDistance)
244 return Standard_True;
245
246 if (Index>1) {
247 if (Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist))
248 return Standard_True;
249 }
250 p1=p2;
251 }
252 }
7fd59977 253 }
b8ddfc2f 254 break;
255 }
256 default:
7fd59977 257 {
258 GCPnts_TangentialDeflection Algo(aCurve,U1, U2, anAngle, TheDeflection);
b8ddfc2f 259 const Standard_Integer NumberOfPoints = Algo.NbPoints();
7fd59977 260 if (NumberOfPoints > 0) {
b8ddfc2f 261 gp_Pnt p1,p2;
262 for (Standard_Integer i=1;i<=NumberOfPoints;i++) {
263 p2 = Algo.Value(i);
264 if ( Abs(X-p2.X()) + Abs(Y-p2.Y()) + Abs(Z-p2.Z()) <= aDistance)
265 return Standard_True;
266 if (i>1) {
267 if (Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist))
268 return Standard_True;
269 }
270 p1=p2;
271 }
7fd59977 272 }
7fd59977 273 }
274 }
275 return Standard_False;
276}
277
278
279//==================================================================
280// function: Add
281// purpose:
282//==================================================================
283void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
b8ddfc2f 284 Adaptor3d_Curve& aCurve,
285 const Handle (Prs3d_Drawer)& aDrawer,
8d3aa19e 286 const Standard_Boolean theToDrawCurve)
b8ddfc2f 287{
1b9f5d95 288 Handle(Graphic3d_Group) aGroup;
289 if (theToDrawCurve)
290 {
291 aGroup = Prs3d_Root::CurrentGroup(aPresentation);
292 aGroup->SetPrimitivesAspect (aDrawer->LineAspect()->Aspect());
293 }
7fd59977 294
b8ddfc2f 295 Standard_Real V1, V2;
296 if (FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2))
297 {
298 TColgp_SequenceOfPnt Points;
8d3aa19e 299 drawCurve(aCurve,
300 aGroup,
b8ddfc2f 301 GetDeflection(aCurve, V1, V2, aDrawer),
302 aDrawer->DeviationAngle(),
8d3aa19e 303 V1, V2, Points);
b8ddfc2f 304
1b9f5d95 305 if (aDrawer->LineArrowDraw()
306 && !aGroup.IsNull())
307 {
7fd59977 308 gp_Pnt Location;
309 gp_Vec Direction;
310 aCurve.D1(V2, Location,Direction);
4ad142d9 311 Prs3d_Arrow::Draw (aGroup,
b8ddfc2f 312 Location,
313 gp_Dir(Direction),
314 aDrawer->ArrowAspect()->Angle(),
315 aDrawer->ArrowAspect()->Length());
7fd59977 316 }
317 }
318}
319
320
321//==================================================================
322// function: Add
323// purpose:
324//==================================================================
325void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
b8ddfc2f 326 Adaptor3d_Curve& aCurve,
327 const Standard_Real U1,
328 const Standard_Real U2,
329 const Handle (Prs3d_Drawer)& aDrawer,
8d3aa19e 330 const Standard_Boolean theToDrawCurve)
b8ddfc2f 331{
8d3aa19e 332 Handle(Graphic3d_Group) aGroup;
333 if (theToDrawCurve)
334 {
335 aGroup = Prs3d_Root::CurrentGroup (aPresentation);
336 aGroup->SetPrimitivesAspect(aDrawer->LineAspect()->Aspect());
337 }
7fd59977 338
339 Standard_Real V1 = U1;
340 Standard_Real V2 = U2;
341
7fd59977 342 if (Precision::IsNegativeInfinite(V1)) V1 = -aDrawer->MaximalParameterValue();
343 if (Precision::IsPositiveInfinite(V2)) V2 = aDrawer->MaximalParameterValue();
344
7fd59977 345 TColgp_SequenceOfPnt Points;
8d3aa19e 346 drawCurve(aCurve,
347 aGroup,
b8ddfc2f 348 GetDeflection(aCurve, V1, V2, aDrawer),
349 aDrawer->DeviationAngle(),
8d3aa19e 350 V1 , V2, Points);
7fd59977 351
1b9f5d95 352 if (aDrawer->LineArrowDraw()
353 && !aGroup.IsNull())
354 {
7fd59977 355 gp_Pnt Location;
356 gp_Vec Direction;
357 aCurve.D1(V2, Location,Direction);
4ad142d9 358 Prs3d_Arrow::Draw (aGroup,
b8ddfc2f 359 Location,
360 gp_Dir(Direction),
361 aDrawer->ArrowAspect()->Angle(),
362 aDrawer->ArrowAspect()->Length());
7fd59977 363 }
364}
365
366//==================================================================
367// function: Add
368// purpose:
369//==================================================================
370void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
b8ddfc2f 371 Adaptor3d_Curve& aCurve,
372 const Standard_Real U1,
373 const Standard_Real U2,
374 const Standard_Real aDeflection,
375 TColgp_SequenceOfPnt& Points,
376 const Standard_Real anAngle,
8d3aa19e 377 const Standard_Boolean theToDrawCurve)
7fd59977 378{
8d3aa19e 379 Handle(Graphic3d_Group) aGroup;
380 if (theToDrawCurve)
381 {
382 aGroup = Prs3d_Root::CurrentGroup (aPresentation);
383 }
384
385 drawCurve (aCurve, aGroup, aDeflection, anAngle, U1, U2, Points);
7fd59977 386}
387
7fd59977 388//==================================================================
389// function: Add
390// purpose:
391//==================================================================
392void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
b8ddfc2f 393 Adaptor3d_Curve& aCurve,
394 const Standard_Real aDeflection,
395 const Standard_Real aLimit,
396 const Standard_Real anAngle,
8d3aa19e 397 const Standard_Boolean theToDrawCurve)
7fd59977 398{
399 Standard_Real V1, V2;
8d3aa19e 400 if (!FindLimits(aCurve, aLimit, V1, V2))
b8ddfc2f 401 {
8d3aa19e 402 return;
403 }
404
405 Handle(Graphic3d_Group) aGroup;
406 if (theToDrawCurve)
407 {
408 aGroup = Prs3d_Root::CurrentGroup (aPresentation);
b8ddfc2f 409 }
8d3aa19e 410
411 TColgp_SequenceOfPnt Points;
412 drawCurve (aCurve, aGroup, aDeflection, anAngle, V1, V2, Points);
7fd59977 413}
414
415
416//================================================================================
417// function: Add
418// purpose:
419//================================================================================
420void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
b8ddfc2f 421 Adaptor3d_Curve& aCurve,
422 const Standard_Real aDeflection,
423 const Handle(Prs3d_Drawer)& aDrawer,
424 TColgp_SequenceOfPnt& Points,
8d3aa19e 425 const Standard_Boolean theToDrawCurve)
7fd59977 426{
7fd59977 427 Standard_Real V1, V2;
8d3aa19e 428 if (!FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2))
429 {
430 return;
431 }
432
433 Handle(Graphic3d_Group) aGroup;
434 if (theToDrawCurve)
435 {
436 aGroup = Prs3d_Root::CurrentGroup (aPresentation);
437 }
438 drawCurve (aCurve, aGroup, aDeflection, aDrawer->DeviationAngle(), V1, V2, Points);
7fd59977 439}
440
441
442//==================================================================
443// function: Match
444// purpose:
445//==================================================================
446Standard_Boolean StdPrs_DeflectionCurve::Match
447 (const Quantity_Length X,
448 const Quantity_Length Y,
449 const Quantity_Length Z,
450 const Quantity_Length aDistance,
451 const Adaptor3d_Curve& aCurve,
452 const Handle (Prs3d_Drawer)& aDrawer)
453{
454 Standard_Real V1, V2;
b8ddfc2f 455 if (FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2))
456 {
7fd59977 457 return MatchCurve(X,Y,Z,aDistance,aCurve,
b8ddfc2f 458 GetDeflection(aCurve, V1, V2, aDrawer),
459 aDrawer->DeviationAngle(),
460 V1, V2);
7fd59977 461 }
b8ddfc2f 462 return Standard_False;
7fd59977 463}
464
7fd59977 465//==================================================================
466// function: Match
467// purpose:
468//==================================================================
469Standard_Boolean StdPrs_DeflectionCurve::Match
470 (const Quantity_Length X,
471 const Quantity_Length Y,
472 const Quantity_Length Z,
473 const Quantity_Length aDistance,
474 const Adaptor3d_Curve& aCurve,
475 const Standard_Real U1,
476 const Standard_Real U2,
477 const Handle (Prs3d_Drawer)& aDrawer)
478{
479 Standard_Real V1 = U1;
b8ddfc2f 480 Standard_Real V2 = U2;
7fd59977 481
482 if (Precision::IsNegativeInfinite(V1)) V1 = -aDrawer->MaximalParameterValue();
483 if (Precision::IsPositiveInfinite(V2)) V2 = aDrawer->MaximalParameterValue();
484
485 return MatchCurve(X,Y,Z,aDistance,aCurve,
b8ddfc2f 486 GetDeflection(aCurve, V1, V2, aDrawer),
487 aDrawer->DeviationAngle(), V1, V2);
7fd59977 488}
489
7fd59977 490//==================================================================
491// function: Match
492// purpose:
493//==================================================================
494Standard_Boolean StdPrs_DeflectionCurve::Match
495 (const Quantity_Length X,
b8ddfc2f 496 const Quantity_Length Y,
497 const Quantity_Length Z,
498 const Quantity_Length aDistance,
499 const Adaptor3d_Curve& aCurve,
500 const Standard_Real U1,
501 const Standard_Real U2,
502 const Standard_Real aDeflection,
503 const Standard_Real anAngle)
504{
7fd59977 505 return MatchCurve(X,Y,Z,aDistance,aCurve,aDeflection,anAngle,U1,U2);
7fd59977 506}
507
508//==================================================================
509// function: Match
510// purpose:
511//==================================================================
512Standard_Boolean StdPrs_DeflectionCurve::Match
513 (const Quantity_Length X,
514 const Quantity_Length Y,
515 const Quantity_Length Z,
516 const Quantity_Length aDistance,
517 const Adaptor3d_Curve& aCurve,
518 const Standard_Real aDeflection,
519 const Standard_Real aLimit,
b8ddfc2f 520 const Standard_Real anAngle)
521{
7fd59977 522 Standard_Real V1, V2;
b8ddfc2f 523 if (FindLimits(aCurve, aLimit, V1, V2))
524 {
525 return MatchCurve(X,Y,Z,aDistance,aCurve,aDeflection,anAngle,V1,V2);
7fd59977 526 }
b8ddfc2f 527 return Standard_False;
7fd59977 528}