1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 // Great zoom leads to non-coincidence of
16 // a point and non-infinite lines passing throught this point:
18 #include <Adaptor3d_Curve.hxx>
19 #include <Bnd_Box.hxx>
20 #include <BndLib_Add3dCurve.hxx>
21 #include <GCPnts_QuasiUniformDeflection.hxx>
22 #include <GCPnts_TangentialDeflection.hxx>
23 #include <gp_Circ.hxx>
27 #include <Graphic3d_ArrayOfPolylines.hxx>
28 #include <Graphic3d_ArrayOfSegments.hxx>
29 #include <Graphic3d_Group.hxx>
30 #include <Precision.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>
37 #include <TColgp_SequenceOfPnt.hxx>
38 #include <TColStd_Array1OfReal.hxx>
40 //==================================================================
41 // function: GetDeflection
43 //==================================================================
44 static Standard_Real GetDeflection(const Adaptor3d_Curve& aCurve,
45 const Standard_Real U1,
46 const Standard_Real U2,
47 const Handle(Prs3d_Drawer)& aDrawer)
49 Standard_Real TheDeflection;
51 if (aDrawer->TypeOfDeflection() == Aspect_TOD_RELATIVE)
53 // On calcule la fleche en fonction des min max globaux de la piece:
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));
66 m = Min ( m , aDrawer->MaximalParameterValue());
67 m = Max(m, Precision::Confusion());
69 TheDeflection = m * aDrawer->DeviationCoefficient();
72 TheDeflection = aDrawer->MaximalChordialDeviation();
77 //==================================================================
78 // function: FindLimits
80 //==================================================================
81 static Standard_Boolean FindLimits(const Adaptor3d_Curve& aCurve,
82 const Standard_Real aLimit,
86 First = aCurve.FirstParameter();
87 Last = aCurve.LastParameter();
88 Standard_Boolean firstInf = Precision::IsNegativeInfinite(First);
89 Standard_Boolean lastInf = Precision::IsPositiveInfinite(Last);
91 if (firstInf || lastInf) {
93 Standard_Real delta = 1;
94 Standard_Integer count = 0;
95 if (firstInf && lastInf) {
97 if (count++ == 100000) return Standard_False;
103 } while (P1.Distance(P2) < aLimit);
108 if (count++ == 100000) return Standard_False;
110 First = Last - delta;
112 } while (P1.Distance(P2) < aLimit);
117 if (count++ == 100000) return Standard_False;
119 Last = First + delta;
121 } while (P1.Distance(P2) < aLimit);
124 return Standard_True;
128 //==================================================================
129 // function: drawCurve
131 //==================================================================
132 static void drawCurve (Adaptor3d_Curve& aCurve,
133 const Handle(Graphic3d_Group)& aGroup,
134 const Standard_Real TheDeflection,
135 const Standard_Real anAngle,
136 const Standard_Real U1,
137 const Standard_Real U2,
138 TColgp_SequenceOfPnt& Points)
140 switch (aCurve.GetType())
144 gp_Pnt p1 = aCurve.Value(U1);
145 gp_Pnt p2 = aCurve.Value(U2);
148 if (!aGroup.IsNull())
150 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
151 aPrims->AddVertex(p1);
152 aPrims->AddVertex(p2);
153 aGroup->AddPrimitiveArray(aPrims);
159 const Standard_Integer nbinter = aCurve.NbIntervals(GeomAbs_C1);
160 TColStd_Array1OfReal T(1, nbinter+1);
161 aCurve.Intervals(T, GeomAbs_C1);
163 Standard_Real theU1, theU2;
164 Standard_Integer NumberOfPoints, i, j;
165 TColgp_SequenceOfPnt SeqP;
167 for (j = 1; j <= nbinter; j++) {
168 theU1 = T(j); theU2 = T(j+1);
169 if (theU2 > U1 && theU1 < U2) {
170 theU1 = Max(theU1, U1);
171 theU2 = Min(theU2, U2);
173 GCPnts_TangentialDeflection Algo(aCurve, theU1, theU2, anAngle, TheDeflection);
174 NumberOfPoints = Algo.NbPoints();
176 if (NumberOfPoints > 0) {
177 for (i = 1; i <= NumberOfPoints; i++)
178 SeqP.Append(Algo.Value(i));
183 Handle(Graphic3d_ArrayOfPolylines) aPrims;
184 if (!aGroup.IsNull())
185 aPrims = new Graphic3d_ArrayOfPolylines(SeqP.Length());
187 for (i = 1; i <= SeqP.Length(); i++) {
188 const gp_Pnt& p = SeqP.Value(i);
190 if (!aGroup.IsNull())
192 aPrims->AddVertex(p);
195 if (!aGroup.IsNull())
197 aGroup->AddPrimitiveArray (aPrims);
204 //==================================================================
205 // function: MatchCurve
207 //==================================================================
208 static Standard_Boolean MatchCurve (
209 const Standard_Real X,
210 const Standard_Real Y,
211 const Standard_Real Z,
212 const Standard_Real aDistance,
213 const Adaptor3d_Curve& aCurve,
214 const Standard_Real TheDeflection,
215 const Standard_Real anAngle,
216 const Standard_Real U1,
217 const Standard_Real U2)
219 Standard_Real retdist;
220 switch (aCurve.GetType())
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);
234 const Standard_Real Radius = aCurve.Circle().Radius();
235 if (!Precision::IsInfinite(Radius)) {
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));
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;
247 if (Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist))
248 return Standard_True;
258 GCPnts_TangentialDeflection Algo(aCurve,U1, U2, anAngle, TheDeflection);
259 const Standard_Integer NumberOfPoints = Algo.NbPoints();
260 if (NumberOfPoints > 0) {
262 for (Standard_Integer i=1;i<=NumberOfPoints;i++) {
264 if ( Abs(X-p2.X()) + Abs(Y-p2.Y()) + Abs(Z-p2.Z()) <= aDistance)
265 return Standard_True;
267 if (Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist))
268 return Standard_True;
275 return Standard_False;
279 //==================================================================
282 //==================================================================
283 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
284 Adaptor3d_Curve& aCurve,
285 const Handle (Prs3d_Drawer)& aDrawer,
286 const Standard_Boolean theToDrawCurve)
288 Handle(Graphic3d_Group) aGroup;
291 aGroup = aPresentation->CurrentGroup();
292 aGroup->SetPrimitivesAspect (aDrawer->LineAspect()->Aspect());
295 Standard_Real V1, V2;
296 if (FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2))
298 TColgp_SequenceOfPnt Points;
301 GetDeflection(aCurve, V1, V2, aDrawer),
302 aDrawer->DeviationAngle(),
305 if (aDrawer->LineArrowDraw()
310 aCurve.D1(V2, Location,Direction);
311 Prs3d_Arrow::Draw (aGroup,
314 aDrawer->ArrowAspect()->Angle(),
315 aDrawer->ArrowAspect()->Length());
321 //==================================================================
324 //==================================================================
325 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
326 Adaptor3d_Curve& aCurve,
327 const Standard_Real U1,
328 const Standard_Real U2,
329 const Handle (Prs3d_Drawer)& aDrawer,
330 const Standard_Boolean theToDrawCurve)
332 Handle(Graphic3d_Group) aGroup;
335 aGroup = aPresentation->CurrentGroup();
336 aGroup->SetPrimitivesAspect(aDrawer->LineAspect()->Aspect());
339 Standard_Real V1 = U1;
340 Standard_Real V2 = U2;
342 if (Precision::IsNegativeInfinite(V1)) V1 = -aDrawer->MaximalParameterValue();
343 if (Precision::IsPositiveInfinite(V2)) V2 = aDrawer->MaximalParameterValue();
345 TColgp_SequenceOfPnt Points;
348 GetDeflection(aCurve, V1, V2, aDrawer),
349 aDrawer->DeviationAngle(),
352 if (aDrawer->LineArrowDraw()
357 aCurve.D1(V2, Location,Direction);
358 Prs3d_Arrow::Draw (aGroup,
361 aDrawer->ArrowAspect()->Angle(),
362 aDrawer->ArrowAspect()->Length());
366 //==================================================================
369 //==================================================================
370 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
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,
377 const Standard_Boolean theToDrawCurve)
379 Handle(Graphic3d_Group) aGroup;
382 aGroup = aPresentation->CurrentGroup();
385 drawCurve (aCurve, aGroup, aDeflection, anAngle, U1, U2, Points);
388 //==================================================================
391 //==================================================================
392 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
393 Adaptor3d_Curve& aCurve,
394 const Standard_Real aDeflection,
395 const Standard_Real aLimit,
396 const Standard_Real anAngle,
397 const Standard_Boolean theToDrawCurve)
399 Standard_Real V1, V2;
400 if (!FindLimits(aCurve, aLimit, V1, V2))
405 Handle(Graphic3d_Group) aGroup;
408 aGroup = aPresentation->CurrentGroup();
411 TColgp_SequenceOfPnt Points;
412 drawCurve (aCurve, aGroup, aDeflection, anAngle, V1, V2, Points);
416 //================================================================================
419 //================================================================================
420 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
421 Adaptor3d_Curve& aCurve,
422 const Standard_Real aDeflection,
423 const Handle(Prs3d_Drawer)& aDrawer,
424 TColgp_SequenceOfPnt& Points,
425 const Standard_Boolean theToDrawCurve)
427 Standard_Real V1, V2;
428 if (!FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2))
433 Handle(Graphic3d_Group) aGroup;
436 aGroup = aPresentation->CurrentGroup();
438 drawCurve (aCurve, aGroup, aDeflection, aDrawer->DeviationAngle(), V1, V2, Points);
442 //==================================================================
445 //==================================================================
446 Standard_Boolean StdPrs_DeflectionCurve::Match
447 (const Standard_Real X,
448 const Standard_Real Y,
449 const Standard_Real Z,
450 const Standard_Real aDistance,
451 const Adaptor3d_Curve& aCurve,
452 const Handle (Prs3d_Drawer)& aDrawer)
454 Standard_Real V1, V2;
455 if (FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2))
457 return MatchCurve(X,Y,Z,aDistance,aCurve,
458 GetDeflection(aCurve, V1, V2, aDrawer),
459 aDrawer->DeviationAngle(),
462 return Standard_False;
465 //==================================================================
468 //==================================================================
469 Standard_Boolean StdPrs_DeflectionCurve::Match
470 (const Standard_Real X,
471 const Standard_Real Y,
472 const Standard_Real Z,
473 const Standard_Real aDistance,
474 const Adaptor3d_Curve& aCurve,
475 const Standard_Real U1,
476 const Standard_Real U2,
477 const Handle (Prs3d_Drawer)& aDrawer)
479 Standard_Real V1 = U1;
480 Standard_Real V2 = U2;
482 if (Precision::IsNegativeInfinite(V1)) V1 = -aDrawer->MaximalParameterValue();
483 if (Precision::IsPositiveInfinite(V2)) V2 = aDrawer->MaximalParameterValue();
485 return MatchCurve(X,Y,Z,aDistance,aCurve,
486 GetDeflection(aCurve, V1, V2, aDrawer),
487 aDrawer->DeviationAngle(), V1, V2);
490 //==================================================================
493 //==================================================================
494 Standard_Boolean StdPrs_DeflectionCurve::Match
495 (const Standard_Real X,
496 const Standard_Real Y,
497 const Standard_Real Z,
498 const Standard_Real 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)
505 return MatchCurve(X,Y,Z,aDistance,aCurve,aDeflection,anAngle,U1,U2);
508 //==================================================================
511 //==================================================================
512 Standard_Boolean StdPrs_DeflectionCurve::Match
513 (const Standard_Real X,
514 const Standard_Real Y,
515 const Standard_Real Z,
516 const Standard_Real aDistance,
517 const Adaptor3d_Curve& aCurve,
518 const Standard_Real aDeflection,
519 const Standard_Real aLimit,
520 const Standard_Real anAngle)
522 Standard_Real V1, V2;
523 if (FindLimits(aCurve, aLimit, V1, V2))
525 return MatchCurve(X,Y,Z,aDistance,aCurve,aDeflection,anAngle,V1,V2);
527 return Standard_False;