1 // Great zoom leads to non-coincidence of
2 // a point and non-infinite lines passing throught this point:
5 #include <StdPrs_DeflectionCurve.ixx>
7 #include <Graphic3d_ArrayOfPrimitives.hxx>
8 #include <Graphic3d_Array1OfVertex.hxx>
9 #include <Graphic3d_Group.hxx>
10 #include <Prs3d_LineAspect.hxx>
11 #include <Prs3d_Arrow.hxx>
12 #include <Prs3d_ArrowAspect.hxx>
14 #include <gp_Circ.hxx>
18 #include <Bnd_Box.hxx>
19 #include <BndLib_Add3dCurve.hxx>
20 #include <Precision.hxx>
21 #include <GCPnts_QuasiUniformDeflection.hxx>
22 #include <GCPnts_TangentialDeflection.hxx>
23 #include <TColgp_SequenceOfPnt.hxx>
24 #include <TColStd_Array1OfReal.hxx>
27 //==================================================================
28 // function: GetDeflection
30 //==================================================================
31 static Standard_Real GetDeflection(const Adaptor3d_Curve& aCurve,
32 const Standard_Real U1,
33 const Standard_Real U2,
34 const Handle(Prs3d_Drawer)& aDrawer) {
35 Standard_Real TheDeflection;
36 Aspect_TypeOfDeflection TOD = aDrawer->TypeOfDeflection();
37 if (TOD == Aspect_TOD_RELATIVE) {
38 // On calcule la fleche en fonction des min max globaux de la piece:
40 BndLib_Add3dCurve::Add(aCurve, U1, U2, 0.,Total);
41 Standard_Real m = RealFirst();
42 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
43 Total.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
45 if ( ! (Total.IsOpenXmin() || Total.IsOpenXmax() ))
46 m = Abs (aXmax-aXmin);
47 if ( ! (Total.IsOpenYmin() || Total.IsOpenYmax() ))
48 m = Max ( m , Abs (aYmax-aYmin));
49 if ( ! (Total.IsOpenZmin() || Total.IsOpenZmax() ))
50 m = Max ( m , Abs (aZmax-aZmin));
52 m = Min ( m , aDrawer->MaximalParameterValue());
53 m = Max(m, Precision::Confusion());
55 TheDeflection = m * aDrawer->DeviationCoefficient();
58 TheDeflection = aDrawer->MaximalChordialDeviation();
63 //==================================================================
64 // function: FindLimits
66 //==================================================================
67 static Standard_Boolean FindLimits(const Adaptor3d_Curve& aCurve,
68 const Standard_Real aLimit,
72 First = aCurve.FirstParameter();
73 Last = aCurve.LastParameter();
74 Standard_Boolean firstInf = Precision::IsNegativeInfinite(First);
75 Standard_Boolean lastInf = Precision::IsPositiveInfinite(Last);
77 if (firstInf || lastInf) {
79 Standard_Real delta = 1;
80 Standard_Integer count = 0;
81 if (firstInf && lastInf) {
83 if (count++ == 100000) return Standard_False;
89 } while (P1.Distance(P2) < aLimit);
94 if (count++ == 100000) return Standard_False;
98 } while (P1.Distance(P2) < aLimit);
103 if (count++ == 100000) return Standard_False;
105 Last = First + delta;
107 } while (P1.Distance(P2) < aLimit);
110 return Standard_True;
116 //==================================================================
117 // function: DrawCurve
119 //==================================================================
120 static void DrawCurve (Adaptor3d_Curve& aCurve,
121 const Handle(Graphic3d_Group) aGroup,
122 const Quantity_Length TheDeflection,
123 const Standard_Real anAngle,
124 const Standard_Real U1,
125 const Standard_Real U2,
126 TColgp_SequenceOfPnt& Points,
127 const Standard_Boolean drawCurve)
129 Standard_Boolean isPrimArrayEnabled = Graphic3d_ArrayOfPrimitives::IsEnable() && !drawCurve;
131 switch (aCurve.GetType()) {
135 Graphic3d_Array1OfVertex VertexArray(1, 3);
136 gp_Pnt p = aCurve.Value(U1);
138 VertexArray(1).SetCoord(p.X(), p.Y(), p.Z());
139 p = aCurve.Value(0.5 * (U1 + U2));
141 VertexArray(2).SetCoord(p.X(), p.Y(), p.Z());
142 p = aCurve.Value(U2);
144 VertexArray(3).SetCoord(p.X(), p.Y(), p.Z());
145 if(!isPrimArrayEnabled)
146 aGroup->Polyline(VertexArray);
148 static Graphic3d_Array1OfVertex VertexArray(1,2);
149 gp_Pnt p = aCurve.Value(U1);
151 VertexArray(1).SetCoord(p.X(), p.Y(), p.Z());
152 p = aCurve.Value(U2);
154 VertexArray(2).SetCoord(p.X(), p.Y(), p.Z());
155 if(!isPrimArrayEnabled)
156 aGroup->Polyline(VertexArray);
162 Standard_Integer nbinter = aCurve.NbIntervals(GeomAbs_C1);
163 Standard_Integer i, j;
164 TColStd_Array1OfReal T(1, nbinter+1);
165 aCurve.Intervals(T, GeomAbs_C1);
166 Standard_Real theU1, theU2;
167 Standard_Integer NumberOfPoints;
168 TColgp_SequenceOfPnt SeqP;
170 for (j = 1; j <= nbinter; j++) {
171 theU1 = T(j); theU2 = T(j+1);
172 if (theU2 > U1 && theU1 < U2) {
173 theU1 = Max(theU1, U1);
174 theU2 = Min(theU2, U2);
176 GCPnts_TangentialDeflection Algo(aCurve, theU1, theU2, anAngle, TheDeflection);
177 NumberOfPoints = Algo.NbPoints();
179 if (NumberOfPoints > 0) {
180 for (i=1;i<NumberOfPoints;i++) {
181 SeqP.Append(Algo.Value(i));
184 SeqP.Append(Algo.Value(NumberOfPoints));
190 Graphic3d_Array1OfVertex VertexArray(1, SeqP.Length());
192 Standard_Integer totalpoints = 1;
193 for (i = 1; i <= SeqP.Length(); i++) {
194 const gp_Pnt& p = SeqP.Value(i);
196 VertexArray(totalpoints++).SetCoord(p.X(), p.Y(), p.Z());
198 if(!isPrimArrayEnabled)
199 aGroup->Polyline(VertexArray);
206 //==================================================================
207 // function: MatchCurve
209 //==================================================================
210 static Standard_Boolean MatchCurve (
211 const Quantity_Length X,
212 const Quantity_Length Y,
213 const Quantity_Length Z,
214 const Quantity_Length aDistance,
215 const Adaptor3d_Curve& aCurve,
216 const Quantity_Length TheDeflection,
217 const Standard_Real anAngle,
218 const Standard_Real U1,
219 const Standard_Real U2)
221 Quantity_Length retdist;
222 switch (aCurve.GetType()) {
225 static Graphic3d_Array1OfVertex VertexArray(1,2);
226 gp_Pnt p1 = aCurve.Value(U1);
227 if ( Abs(X-p1.X()) + Abs(Y-p1.Y()) + Abs(Z-p1.Z()) <= aDistance)
228 return Standard_True;
229 gp_Pnt p2 = aCurve.Value(U2);
230 if ( Abs(X-p2.X()) + Abs(Y-p2.Y()) + Abs(Z-p2.Z()) <= aDistance)
231 return Standard_True;
232 return Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist);
237 Standard_Real Radius = aCurve.Circle().Radius();
238 if (!Precision::IsInfinite(Radius)) {
239 Standard_Real DU = Sqrt(8.0 * TheDeflection / Radius);
240 Standard_Real Er = Abs( U2 - U1) / DU;
241 Standard_Integer N = Max(2, (Standard_Integer)IntegerPart(Er));
245 for (Standard_Integer Index = 1; Index <= N+1; Index++) {
246 U = U1 + (Index - 1) * DU;
247 p2 = aCurve.Value(U);
248 if ( Abs(X-p2.X()) + Abs(Y-p2.Y()) + Abs(Z-p2.Z()) <= aDistance)
249 return Standard_True;
252 if (Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist))
253 return Standard_True;
260 return Standard_False;
265 GCPnts_TangentialDeflection Algo(aCurve,U1, U2, anAngle, TheDeflection);
267 Standard_Integer NumberOfPoints = Algo.NbPoints();
268 if (NumberOfPoints > 0) {
269 for (Standard_Integer i=1;i<=NumberOfPoints;i++) {
271 if ( Abs(X-p2.X()) + Abs(Y-p2.Y()) + Abs(Z-p2.Z()) <= aDistance)
272 return Standard_True;
274 if (Prs3d::MatchSegment(X,Y,Z,aDistance,p1,p2,retdist))
275 return Standard_True;
280 return Standard_False;
283 return Standard_False;
287 //==================================================================
290 //==================================================================
291 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
292 Adaptor3d_Curve& aCurve,
293 const Handle (Prs3d_Drawer)& aDrawer,
294 const Standard_Boolean drawCurve) {
296 Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(aDrawer->LineAspect()->Aspect());
298 Standard_Real V1, V2, angle = aDrawer->DeviationAngle();
299 Standard_Boolean OK = FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2);
301 TColgp_SequenceOfPnt Points;
304 Prs3d_Root::CurrentGroup(aPresentation),
305 GetDeflection(aCurve, V1, V2, aDrawer),
307 V1 , V2, Points, drawCurve);
309 if (aDrawer->LineArrowDraw()) {
312 aCurve.D1(V2, Location,Direction);
313 Prs3d_Arrow::Draw (aPresentation,
316 aDrawer->ArrowAspect()->Angle(),
317 aDrawer->ArrowAspect()->Length());
323 //==================================================================
326 //==================================================================
327 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
328 Adaptor3d_Curve& aCurve,
329 const Standard_Real U1,
330 const Standard_Real U2,
331 const Handle (Prs3d_Drawer)& aDrawer,
332 const Standard_Boolean drawCurve) {
334 Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(aDrawer->LineAspect()->Aspect());
336 Standard_Real V1 = U1;
337 Standard_Real V2 = U2;
340 if (Precision::IsNegativeInfinite(V1)) V1 = -aDrawer->MaximalParameterValue();
341 if (Precision::IsPositiveInfinite(V2)) V2 = aDrawer->MaximalParameterValue();
343 Standard_Real angle = aDrawer->DeviationAngle();
344 TColgp_SequenceOfPnt Points;
346 Prs3d_Root::CurrentGroup(aPresentation),
347 GetDeflection(aCurve, V1, V2, aDrawer),
349 V1 , V2, Points, drawCurve);
351 if (aDrawer->LineArrowDraw()) {
354 aCurve.D1(V2, Location,Direction);
355 Prs3d_Arrow::Draw (aPresentation,
358 aDrawer->ArrowAspect()->Angle(),
359 aDrawer->ArrowAspect()->Length());
363 //==================================================================
366 //==================================================================
367 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
368 Adaptor3d_Curve& aCurve,
369 const Standard_Real U1,
370 const Standard_Real U2,
371 const Standard_Real aDeflection,
372 TColgp_SequenceOfPnt& Points,
373 const Standard_Real anAngle,
374 const Standard_Boolean drawCurve)
376 DrawCurve(aCurve, Prs3d_Root::CurrentGroup(aPresentation),
377 aDeflection, anAngle, U1, U2, Points, drawCurve);
381 //==================================================================
384 //==================================================================
385 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
386 Adaptor3d_Curve& aCurve,
387 const Standard_Real aDeflection,
388 const Standard_Real aLimit,
389 const Standard_Real anAngle,
390 const Standard_Boolean drawCurve)
392 Standard_Real V1, V2;
393 Standard_Boolean OK = FindLimits(aCurve, aLimit, V1, V2);
395 TColgp_SequenceOfPnt Points;
396 if (OK) DrawCurve(aCurve, Prs3d_Root::CurrentGroup(aPresentation),
397 aDeflection, anAngle, V1, V2, Points, drawCurve);
401 //================================================================================
404 //================================================================================
405 void StdPrs_DeflectionCurve::Add (const Handle (Prs3d_Presentation)& aPresentation,
406 Adaptor3d_Curve& aCurve,
407 const Standard_Real aDeflection,
408 const Handle(Prs3d_Drawer)& aDrawer,
409 TColgp_SequenceOfPnt& Points,
410 const Standard_Boolean drawCurve)
412 Standard_Real aLimit = aDrawer->MaximalParameterValue();
413 Standard_Real V1, V2;
414 Standard_Boolean OK = FindLimits(aCurve, aLimit, V1, V2);
416 if (OK) DrawCurve(aCurve, Prs3d_Root::CurrentGroup(aPresentation),
417 aDeflection, aDrawer->DeviationAngle(), V1, V2, Points, drawCurve);
421 //==================================================================
424 //==================================================================
425 Standard_Boolean StdPrs_DeflectionCurve::Match
426 (const Quantity_Length X,
427 const Quantity_Length Y,
428 const Quantity_Length Z,
429 const Quantity_Length aDistance,
430 const Adaptor3d_Curve& aCurve,
431 const Handle (Prs3d_Drawer)& aDrawer)
433 Standard_Real V1, V2;
434 Standard_Boolean OK = FindLimits(aCurve, aDrawer->MaximalParameterValue(), V1, V2);
436 return MatchCurve(X,Y,Z,aDistance,aCurve,
437 GetDeflection(aCurve, V1, V2, aDrawer),
438 aDrawer->DeviationAngle(),
442 return Standard_False;
448 //==================================================================
451 //==================================================================
452 Standard_Boolean StdPrs_DeflectionCurve::Match
453 (const Quantity_Length X,
454 const Quantity_Length Y,
455 const Quantity_Length Z,
456 const Quantity_Length aDistance,
457 const Adaptor3d_Curve& aCurve,
458 const Standard_Real U1,
459 const Standard_Real U2,
460 const Handle (Prs3d_Drawer)& aDrawer)
462 Standard_Real V1 = U1;
463 Standard_Real V2 = U2;
465 if (Precision::IsNegativeInfinite(V1)) V1 = -aDrawer->MaximalParameterValue();
466 if (Precision::IsPositiveInfinite(V2)) V2 = aDrawer->MaximalParameterValue();
468 return MatchCurve(X,Y,Z,aDistance,aCurve,
469 GetDeflection(aCurve, V1, V2, aDrawer),
470 aDrawer->DeviationAngle(), V1 , V2);
474 //==================================================================
477 //==================================================================
478 Standard_Boolean StdPrs_DeflectionCurve::Match
479 (const Quantity_Length X,
480 const Quantity_Length Y,
481 const Quantity_Length Z,
482 const Quantity_Length aDistance,
483 const Adaptor3d_Curve& aCurve,
484 const Standard_Real U1,
485 const Standard_Real U2,
486 const Standard_Real aDeflection,
487 const Standard_Real anAngle) {
490 return MatchCurve(X,Y,Z,aDistance,aCurve,aDeflection,anAngle,U1,U2);
494 //==================================================================
497 //==================================================================
498 Standard_Boolean StdPrs_DeflectionCurve::Match
499 (const Quantity_Length X,
500 const Quantity_Length Y,
501 const Quantity_Length Z,
502 const Quantity_Length aDistance,
503 const Adaptor3d_Curve& aCurve,
504 const Standard_Real aDeflection,
505 const Standard_Real aLimit,
506 const Standard_Real anAngle) {
508 Standard_Real V1, V2;
509 Standard_Boolean OK = FindLimits(aCurve, aLimit, V1, V2);
512 return MatchCurve(X,Y,Z,aDistance,aCurve,
518 return Standard_False;