1 // Created on: 1997-09-23
2 // Created by: Roman BORISOV
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
20 #include <Adaptor2d_HCurve2d.hxx>
21 #include <Adaptor3d_HCurve.hxx>
22 #include <Adaptor3d_HSurface.hxx>
23 #include <Extrema_ExtCS.hxx>
24 #include <Extrema_ExtPS.hxx>
25 #include <Extrema_GenLocateExtPS.hxx>
26 #include <Extrema_POnCurv.hxx>
27 #include <Extrema_POnSurf.hxx>
28 #include <GeomAbs_CurveType.hxx>
29 #include <GeomLib.hxx>
30 #include <gp_Mat2d.hxx>
31 #include <gp_Pnt2d.hxx>
32 #include <gp_Vec2d.hxx>
34 #include <Precision.hxx>
35 #include <ProjLib_CompProjectedCurve.hxx>
36 #include <ProjLib_HCompProjectedCurve.hxx>
37 #include <ProjLib_PrjResolve.hxx>
38 #include <Standard_DomainError.hxx>
39 #include <Standard_NoSuchObject.hxx>
40 #include <Standard_NotImplemented.hxx>
41 #include <Standard_OutOfRange.hxx>
42 #include <TColgp_HSequenceOfPnt.hxx>
43 #include <Adaptor3d_CurveOnSurface.hxx>
44 #include <Geom2d_Line.hxx>
45 #include <Geom2dAdaptor_HCurve.hxx>
46 #include <Extrema_ExtCC.hxx>
47 #include <NCollection_Vector.hxx>
49 #define FuncTol 1.e-10
51 #ifdef OCCT_DEBUG_CHRONO
52 #include <OSD_Timer.hxx>
54 static OSD_Chronometer chr_init_point, chr_dicho_bound;
56 Standard_EXPORT Standard_Real t_init_point, t_dicho_bound;
57 Standard_EXPORT Standard_Integer init_point_count, dicho_bound_count;
59 static void InitChron(OSD_Chronometer& ch)
65 static void ResultChron( OSD_Chronometer & ch, Standard_Real & time)
74 // Structure to perform splits computation.
75 // This structure is not thread-safe since operations under mySplits should be performed in a critical section.
76 // myPeriodicDir - 0 for U periodicity and 1 for V periodicity.
79 SplitDS(const Handle(Adaptor3d_HCurve) &theCurve,
80 const Handle(Adaptor3d_HSurface) &theSurface,
81 NCollection_Vector<Standard_Real> &theSplits)
83 mySurface(theSurface),
92 // Assignment operator is forbidden.
93 void operator=(const SplitDS &theSplitDS);
95 const Handle(Adaptor3d_HCurve) myCurve;
96 const Handle(Adaptor3d_HSurface) mySurface;
97 NCollection_Vector<Standard_Real> &mySplits;
99 Standard_Real myPerMinParam;
100 Standard_Real myPerMaxParam;
101 Standard_Integer myPeriodicDir;
103 Extrema_ExtCC *myExtCC;
104 Extrema_ExtPS *myExtPS;
107 //! Compute split points in the parameter space of the curve.
108 static void BuildCurveSplits(const Handle(Adaptor3d_HCurve) &theCurve,
109 const Handle(Adaptor3d_HSurface) &theSurface,
110 const Standard_Real theTolU,
111 const Standard_Real theTolV,
112 NCollection_Vector<Standard_Real> &theSplits);
114 //! Perform splitting on a specified direction. Sub-method in BuildCurveSplits.
115 static void SplitOnDirection(SplitDS & theSplitDS);
117 //! Perform recursive search of the split points.
118 static void FindSplitPoint(SplitDS & theSplitDS,
119 const Standard_Real theMinParam,
120 const Standard_Real theMaxParam);
123 //=======================================================================
124 //function : Comparator
125 //purpose : used in sort algorithm
126 //=======================================================================
127 inline Standard_Boolean Comparator(const Standard_Real theA,
128 const Standard_Real theB)
133 //=======================================================================
135 //purpose : computes first derivative of the projected curve
136 //=======================================================================
138 static void d1(const Standard_Real t,
139 const Standard_Real u,
140 const Standard_Real v,
142 const Handle(Adaptor3d_HCurve)& Curve,
143 const Handle(Adaptor3d_HSurface)& Surface)
146 gp_Vec DS1_u, DS1_v, DS2_u, DS2_uv, DS2_v, DC1_t;
147 Surface->D2(u, v, S, DS1_u, DS1_v, DS2_u, DS2_v, DS2_uv);
148 Curve->D1(t, C, DC1_t);
149 gp_Vec Ort(C, S);// Ort = S - C
151 gp_Vec2d dE_dt(-DC1_t*DS1_u, -DC1_t*DS1_v);
152 gp_XY dE_du(DS1_u*DS1_u + Ort*DS2_u,
153 DS1_u*DS1_v + Ort*DS2_uv);
154 gp_XY dE_dv(DS1_v*DS1_u + Ort*DS2_uv,
155 DS1_v*DS1_v + Ort*DS2_v);
157 Standard_Real det = dE_du.X()*dE_dv.Y() - dE_du.Y()*dE_dv.X();
158 if (fabs(det) < gp::Resolution()) throw Standard_ConstructionError();
160 gp_Mat2d M(gp_XY(dE_dv.Y()/det, -dE_du.Y()/det),
161 gp_XY(-dE_dv.X()/det, dE_du.X()/det));
163 V = - gp_Vec2d(gp_Vec2d(M.Row(1))*dE_dt, gp_Vec2d(M.Row(2))*dE_dt);
166 //=======================================================================
168 //purpose : computes second derivative of the projected curve
169 //=======================================================================
171 static void d2(const Standard_Real t,
172 const Standard_Real u,
173 const Standard_Real v,
174 gp_Vec2d& V1, gp_Vec2d& V2,
175 const Handle(Adaptor3d_HCurve)& Curve,
176 const Handle(Adaptor3d_HSurface)& Surface)
179 gp_Vec DS1_u, DS1_v, DS2_u, DS2_uv, DS2_v,
180 DS3_u, DS3_v, DS3_uuv, DS3_uvv,
182 Surface->D3(u, v, S, DS1_u, DS1_v, DS2_u, DS2_v, DS2_uv,
183 DS3_u, DS3_v, DS3_uuv, DS3_uvv);
184 Curve->D2(t, C, DC1_t, DC2_t);
187 gp_Vec2d dE_dt(-DC1_t*DS1_u, -DC1_t*DS1_v);
188 gp_XY dE_du(DS1_u*DS1_u + Ort*DS2_u,
189 DS1_u*DS1_v + Ort*DS2_uv);
190 gp_XY dE_dv(DS1_v*DS1_u + Ort*DS2_uv,
191 DS1_v*DS1_v + Ort*DS2_v);
193 Standard_Real det = dE_du.X()*dE_dv.Y() - dE_du.Y()*dE_dv.X();
194 if (fabs(det) < gp::Resolution()) throw Standard_ConstructionError();
196 gp_Mat2d M(gp_XY(dE_dv.Y()/det, -dE_du.Y()/det),
197 gp_XY(-dE_dv.X()/det, dE_du.X()/det));
200 V1 = - gp_Vec2d(gp_Vec2d(M.Row(1))*dE_dt, gp_Vec2d(M.Row(2))*dE_dt);
202 /* Second derivative */
204 // Computation of d2E_dt2 = S1
205 gp_Vec2d d2E_dt(-DC2_t*DS1_u, -DC2_t*DS1_v);
207 // Computation of 2*(d2E/dtdX)(dX/dt) = S2
208 gp_Vec2d d2E1_dtdX(-DC1_t*DS2_u,
210 gp_Vec2d d2E2_dtdX(-DC1_t*DS2_uv,
212 gp_Vec2d S2 = 2*gp_Vec2d(d2E1_dtdX*V1, d2E2_dtdX*V1);
214 // Computation of (d2E/dX2)*(dX/dt)2 = S3
216 // Row11 = (d2E1/du2, d2E1/dudv)
218 gp_Vec2d Row11(3*DS1_u*DS2_u + Ort*DS3_u,
219 tmp = 2*DS1_u*DS2_uv +
220 DS1_v*DS2_u + Ort*DS3_uuv);
222 // Row12 = (d2E1/dudv, d2E1/dv2)
223 gp_Vec2d Row12(tmp, DS2_v*DS1_u + 2*DS1_v*DS2_uv +
226 // Row21 = (d2E2/du2, d2E2/dudv)
227 gp_Vec2d Row21(DS2_u*DS1_v + 2*DS1_u*DS2_uv + Ort*DS3_uuv,
228 tmp = 2*DS2_uv*DS1_v + DS1_u*DS2_v + Ort*DS3_uvv);
230 // Row22 = (d2E2/duv, d2E2/dvdv)
231 gp_Vec2d Row22(tmp, 3*DS1_v*DS2_v + Ort*DS3_v);
233 gp_Vec2d S3(V1*gp_Vec2d(Row11*V1, Row12*V1),
234 V1*gp_Vec2d(Row21*V1, Row22*V1));
236 gp_Vec2d Sum = d2E_dt + S2 + S3;
238 V2 = - gp_Vec2d(gp_Vec2d(M.Row(1))*Sum, gp_Vec2d(M.Row(2))*Sum);
240 //=======================================================================
241 //function : d1CurveOnSurf
242 //purpose : computes first derivative of the 3d projected curve
243 //=======================================================================
246 static void d1CurvOnSurf(const Standard_Real t,
247 const Standard_Real u,
248 const Standard_Real v,
250 const Handle(Adaptor3d_HCurve)& Curve,
251 const Handle(Adaptor3d_HSurface)& Surface)
255 gp_Vec DS1_u, DS1_v, DS2_u, DS2_uv, DS2_v, DC1_t;
256 Surface->D2(u, v, S, DS1_u, DS1_v, DS2_u, DS2_v, DS2_uv);
257 Curve->D1(t, C, DC1_t);
258 gp_Vec Ort(C, S);// Ort = S - C
260 gp_Vec2d dE_dt(-DC1_t*DS1_u, -DC1_t*DS1_v);
261 gp_XY dE_du(DS1_u*DS1_u + Ort*DS2_u,
262 DS1_u*DS1_v + Ort*DS2_uv);
263 gp_XY dE_dv(DS1_v*DS1_u + Ort*DS2_uv,
264 DS1_v*DS1_v + Ort*DS2_v);
266 Standard_Real det = dE_du.X()*dE_dv.Y() - dE_du.Y()*dE_dv.X();
267 if (fabs(det) < gp::Resolution()) throw Standard_ConstructionError();
269 gp_Mat2d M(gp_XY(dE_dv.Y()/det, -dE_du.Y()/det),
270 gp_XY(-dE_dv.X()/det, dE_du.X()/det));
272 V2d = - gp_Vec2d(gp_Vec2d(M.Row(1))*dE_dt, gp_Vec2d(M.Row(2))*dE_dt);
274 V = DS1_u * V2d.X() + DS1_v * V2d.Y();
279 //=======================================================================
280 //function : d2CurveOnSurf
281 //purpose : computes second derivative of the 3D projected curve
282 //=======================================================================
284 static void d2CurvOnSurf(const Standard_Real t,
285 const Standard_Real u,
286 const Standard_Real v,
287 gp_Vec& V1 , gp_Vec& V2 ,
288 const Handle(Adaptor3d_HCurve)& Curve,
289 const Handle(Adaptor3d_HSurface)& Surface)
293 gp_Vec DS1_u, DS1_v, DS2_u, DS2_uv, DS2_v,
294 DS3_u, DS3_v, DS3_uuv, DS3_uvv,
296 Surface->D3(u, v, S, DS1_u, DS1_v, DS2_u, DS2_v, DS2_uv,
297 DS3_u, DS3_v, DS3_uuv, DS3_uvv);
298 Curve->D2(t, C, DC1_t, DC2_t);
301 gp_Vec2d dE_dt(-DC1_t*DS1_u, -DC1_t*DS1_v);
302 gp_XY dE_du(DS1_u*DS1_u + Ort*DS2_u,
303 DS1_u*DS1_v + Ort*DS2_uv);
304 gp_XY dE_dv(DS1_v*DS1_u + Ort*DS2_uv,
305 DS1_v*DS1_v + Ort*DS2_v);
307 Standard_Real det = dE_du.X()*dE_dv.Y() - dE_du.Y()*dE_dv.X();
308 if (fabs(det) < gp::Resolution()) throw Standard_ConstructionError();
310 gp_Mat2d M(gp_XY(dE_dv.Y()/det, -dE_du.Y()/det),
311 gp_XY(-dE_dv.X()/det, dE_du.X()/det));
314 V12d = - gp_Vec2d(gp_Vec2d(M.Row(1))*dE_dt, gp_Vec2d(M.Row(2))*dE_dt);
316 /* Second derivative */
318 // Computation of d2E_dt2 = S1
319 gp_Vec2d d2E_dt(-DC2_t*DS1_u, -DC2_t*DS1_v);
321 // Computation of 2*(d2E/dtdX)(dX/dt) = S2
322 gp_Vec2d d2E1_dtdX(-DC1_t*DS2_u,
324 gp_Vec2d d2E2_dtdX(-DC1_t*DS2_uv,
326 gp_Vec2d S2 = 2*gp_Vec2d(d2E1_dtdX*V12d, d2E2_dtdX*V12d);
328 // Computation of (d2E/dX2)*(dX/dt)2 = S3
330 // Row11 = (d2E1/du2, d2E1/dudv)
332 gp_Vec2d Row11(3*DS1_u*DS2_u + Ort*DS3_u,
333 tmp = 2*DS1_u*DS2_uv +
334 DS1_v*DS2_u + Ort*DS3_uuv);
336 // Row12 = (d2E1/dudv, d2E1/dv2)
337 gp_Vec2d Row12(tmp, DS2_v*DS1_u + 2*DS1_v*DS2_uv +
340 // Row21 = (d2E2/du2, d2E2/dudv)
341 gp_Vec2d Row21(DS2_u*DS1_v + 2*DS1_u*DS2_uv + Ort*DS3_uuv,
342 tmp = 2*DS2_uv*DS1_v + DS1_u*DS2_v + Ort*DS3_uvv);
344 // Row22 = (d2E2/duv, d2E2/dvdv)
345 gp_Vec2d Row22(tmp, 3*DS1_v*DS2_v + Ort*DS3_v);
347 gp_Vec2d S3(V12d*gp_Vec2d(Row11*V12d, Row12*V12d),
348 V12d*gp_Vec2d(Row21*V12d, Row22*V12d));
350 gp_Vec2d Sum = d2E_dt + S2 + S3;
352 V22d = - gp_Vec2d(gp_Vec2d(M.Row(1))*Sum, gp_Vec2d(M.Row(2))*Sum);
354 V1 = DS1_u * V12d.X() + DS1_v * V12d.Y();
355 V2 = DS2_u * V12d.X() *V12d.X()
357 + 2 * DS2_uv * V12d.X() *V12d.Y()
358 + DS2_v * V12d.Y() * V12d.Y()
362 //=======================================================================
363 //function : ExactBound
364 //purpose : computes exact boundary point
365 //=======================================================================
367 static Standard_Boolean ExactBound(gp_Pnt& Sol,
368 const Standard_Real NotSol,
369 const Standard_Real Tol,
370 const Standard_Real TolU,
371 const Standard_Real TolV,
372 const Handle(Adaptor3d_HCurve)& Curve,
373 const Handle(Adaptor3d_HSurface)& Surface)
375 Standard_Real U0, V0, t, t1, t2, FirstU, LastU, FirstV, LastV;
379 FirstU = Surface->FirstUParameter();
380 LastU = Surface->LastUParameter();
381 FirstV = Surface->FirstVParameter();
382 LastV = Surface->LastVParameter();
383 // Here we have to compute the boundary that projection is going to intersect
385 //these variables are to estimate which boundary has more apportunity
387 Standard_Real RU1, RU2, RV1, RV2;
388 d1(Sol.X(), U0, V0, D2d, Curve, Surface);
389 // Here we assume that D2d != (0, 0)
390 if(Abs(D2d.X()) < gp::Resolution())
392 RU1 = Precision::Infinite();
393 RU2 = Precision::Infinite();
397 else if(Abs(D2d.Y()) < gp::Resolution())
401 RV1 = Precision::Infinite();
402 RV2 = Precision::Infinite();
406 RU1 = gp_Pnt2d(U0, V0).
407 Distance(gp_Pnt2d(FirstU, V0 + (FirstU - U0)*D2d.Y()/D2d.X()));
408 RU2 = gp_Pnt2d(U0, V0).
409 Distance(gp_Pnt2d(LastU, V0 + (LastU - U0)*D2d.Y()/D2d.X()));
410 RV1 = gp_Pnt2d(U0, V0).
411 Distance(gp_Pnt2d(U0 + (FirstV - V0)*D2d.X()/D2d.Y(), FirstV));
412 RV2 = gp_Pnt2d(U0, V0).
413 Distance(gp_Pnt2d(U0 + (LastV - V0)*D2d.X()/D2d.Y(), LastV));
415 TColgp_SequenceOfPnt Seq;
416 Seq.Append(gp_Pnt(FirstU, RU1, 2));
417 Seq.Append(gp_Pnt(LastU, RU2, 2));
418 Seq.Append(gp_Pnt(FirstV, RV1, 3));
419 Seq.Append(gp_Pnt(LastV, RV2, 3));
420 Standard_Integer i, j;
421 for(i = 1; i <= 3; i++)
423 for(j = 1; j <= 4-i; j++)
425 if(Seq(j).Y() < Seq(j+1).Y())
428 swp = Seq.Value(j+1);
429 Seq.ChangeValue(j+1) = Seq.Value(j);
430 Seq.ChangeValue(j) = swp;
436 t1 = Min (Sol.X (), NotSol);
437 t2 = Max (Sol.X (), NotSol);
439 Standard_Boolean isDone = Standard_False;
440 while (!Seq.IsEmpty ())
444 Seq.Remove (Seq.Length ());
445 ProjLib_PrjResolve aPrjPS (Curve->Curve (),
447 Standard_Integer (P.Z ()));
448 if (Standard_Integer (P.Z ()) == 2)
450 aPrjPS.Perform (t, P.X (), V0, gp_Pnt2d (Tol, TolV),
451 gp_Pnt2d (t1, Surface->FirstVParameter ()),
452 gp_Pnt2d (t2, Surface->LastVParameter ()), FuncTol);
453 if (!aPrjPS.IsDone ()) continue;
454 POnS = aPrjPS.Solution ();
455 Sol = gp_Pnt (POnS.X (), P.X (), POnS.Y ());
456 isDone = Standard_True;
461 aPrjPS.Perform (t, U0, P.X (), gp_Pnt2d (Tol, TolU),
462 gp_Pnt2d (t1, Surface->FirstUParameter ()),
463 gp_Pnt2d (t2, Surface->LastUParameter ()), FuncTol);
464 if (!aPrjPS.IsDone ()) continue;
465 POnS = aPrjPS.Solution ();
466 Sol = gp_Pnt (POnS.X (), POnS.Y (), P.X ());
467 isDone = Standard_True;
475 //=======================================================================
476 //function : DichExactBound
477 //purpose : computes exact boundary point
478 //=======================================================================
480 static void DichExactBound(gp_Pnt& Sol,
481 const Standard_Real NotSol,
482 const Standard_Real Tol,
483 const Standard_Real TolU,
484 const Standard_Real TolV,
485 const Handle(Adaptor3d_HCurve)& Curve,
486 const Handle(Adaptor3d_HSurface)& Surface)
488 #ifdef OCCT_DEBUG_CHRONO
489 InitChron(chr_dicho_bound);
492 Standard_Real U0, V0, t;
496 ProjLib_PrjResolve aPrjPS(Curve->Curve(), Surface->Surface(), 1);
498 Standard_Real aNotSol = NotSol;
499 while (fabs(Sol.X() - aNotSol) > Tol)
501 t = (Sol.X() + aNotSol)/2;
502 aPrjPS.Perform(t, U0, V0, gp_Pnt2d(TolU, TolV),
503 gp_Pnt2d(Surface->FirstUParameter(),Surface->FirstVParameter()),
504 gp_Pnt2d(Surface->LastUParameter(),Surface->LastVParameter()),
505 FuncTol, Standard_True);
509 POnS = aPrjPS.Solution();
510 Sol = gp_Pnt(t, POnS.X(), POnS.Y());
516 #ifdef OCCT_DEBUG_CHRONO
517 ResultChron(chr_dicho_bound,t_dicho_bound);
522 //=======================================================================
523 //function : InitialPoint
525 //=======================================================================
527 static Standard_Boolean InitialPoint(const gp_Pnt& Point,
528 const Standard_Real t,
529 const Handle(Adaptor3d_HCurve)& C,
530 const Handle(Adaptor3d_HSurface)& S,
531 const Standard_Real TolU,
532 const Standard_Real TolV,
537 ProjLib_PrjResolve aPrjPS(C->Curve(), S->Surface(), 1);
538 Standard_Real ParU,ParV;
539 Extrema_ExtPS aExtPS;
540 aExtPS.Initialize(S->Surface(), S->FirstUParameter(),
541 S->LastUParameter(), S->FirstVParameter(),
542 S->LastVParameter(), TolU, TolV);
544 aExtPS.Perform(Point);
545 Standard_Integer argmin = 0;
546 if (aExtPS.IsDone() && aExtPS.NbExt())
548 Standard_Integer i, Nend;
549 // Search for the nearest solution which is also a normal projection
550 Nend = aExtPS.NbExt();
551 for(i = 1; i <= Nend; i++)
553 Extrema_POnSurf POnS = aExtPS.Point(i);
554 POnS.Parameter(ParU, ParV);
555 aPrjPS.Perform(t, ParU, ParV, gp_Pnt2d(TolU, TolV),
556 gp_Pnt2d(S->FirstUParameter(), S->FirstVParameter()),
557 gp_Pnt2d(S->LastUParameter(), S->LastVParameter()),
558 FuncTol, Standard_True);
560 if (argmin == 0 || aExtPS.SquareDistance(i) < aExtPS.SquareDistance(argmin)) argmin = i;
563 if( argmin == 0 ) return Standard_False;
566 Extrema_POnSurf POnS = aExtPS.Point(argmin);
567 POnS.Parameter(U, V);
568 return Standard_True;
572 //=======================================================================
573 //function : ProjLib_CompProjectedCurve
575 //=======================================================================
577 ProjLib_CompProjectedCurve::ProjLib_CompProjectedCurve()
585 //=======================================================================
586 //function : ProjLib_CompProjectedCurve
588 //=======================================================================
590 ProjLib_CompProjectedCurve::ProjLib_CompProjectedCurve
591 (const Handle(Adaptor3d_HSurface)& theSurface,
592 const Handle(Adaptor3d_HCurve)& theCurve,
593 const Standard_Real theTolU,
594 const Standard_Real theTolV)
595 : mySurface (theSurface),
598 mySequence(new ProjLib_HSequenceOfHSequenceOfPnt()),
606 //=======================================================================
607 //function : ProjLib_CompProjectedCurve
609 //=======================================================================
611 ProjLib_CompProjectedCurve::ProjLib_CompProjectedCurve
612 (const Handle(Adaptor3d_HSurface)& theSurface,
613 const Handle(Adaptor3d_HCurve)& theCurve,
614 const Standard_Real theTolU,
615 const Standard_Real theTolV,
616 const Standard_Real theMaxDist)
617 : mySurface (theSurface),
620 mySequence(new ProjLib_HSequenceOfHSequenceOfPnt()),
623 myMaxDist (theMaxDist)
628 //=======================================================================
631 //=======================================================================
633 void ProjLib_CompProjectedCurve::Init()
636 NCollection_Vector<Standard_Real> aSplits;
639 Standard_Real Tol;// Tolerance for ExactBound
640 Standard_Integer i, Nend = 0, aSplitIdx = 0;
641 Standard_Boolean FromLastU = Standard_False,
642 isSplitsComputed = Standard_False;
644 const Standard_Real aTolExt = Precision::PConfusion();
645 Extrema_ExtCS CExt(myCurve->Curve(), mySurface->Surface(), aTolExt, aTolExt);
646 if (CExt.IsDone() && CExt.NbExt())
648 // Search for the minimum solution.
649 // Avoid usage of extrema result that can be wrong for extrusion.
652 mySurface->GetType() != GeomAbs_SurfaceOfExtrusion)
654 Standard_Real min_val2;
655 min_val2 = CExt.SquareDistance(1);
658 for(i = 2; i <= Nend; i++)
660 if (CExt.SquareDistance(i) < min_val2)
661 min_val2 = CExt.SquareDistance(i);
663 if (min_val2 > myMaxDist * myMaxDist)
664 return; // No near solution -> exit.
668 Standard_Real FirstU, LastU, Step, SearchStep, WalkStep, t;
670 FirstU = myCurve->FirstParameter();
671 LastU = myCurve->LastParameter();
672 const Standard_Real GlobalMinStep = 1.e-4;
673 //<GlobalMinStep> is sufficiently small to provide solving from initial point
674 //and, on the other hand, it is sufficiently large to avoid too close solutions.
675 const Standard_Real MinStep = 0.01*(LastU - FirstU),
676 MaxStep = 0.1*(LastU - FirstU);
677 SearchStep = 10*MinStep;
680 gp_Pnt2d aLowBorder(mySurface->FirstUParameter(),mySurface->FirstVParameter());
681 gp_Pnt2d aUppBorder(mySurface->LastUParameter(), mySurface->LastVParameter());
682 gp_Pnt2d aTol(myTolU, myTolV);
683 ProjLib_PrjResolve aPrjPS(myCurve->Curve(), mySurface->Surface(), 1);
686 Standard_Boolean new_part;
687 Standard_Real prevDeb=0.;
688 Standard_Boolean SameDeb=Standard_False;
691 gp_Pnt Triple, prevTriple;
696 // Search for the beginning of a new continuous part
697 // to avoid infinite computation in some difficult cases.
698 new_part = Standard_False;
699 if(t > FirstU && Abs(t-prevDeb) <= Precision::PConfusion()) SameDeb=Standard_True;
700 while(t <= LastU && !new_part && !FromLastU && !SameDeb)
703 if (t == LastU) FromLastU=Standard_True;
704 Standard_Boolean initpoint=Standard_False;
705 Standard_Real U = 0., V = 0.;
707 Standard_Real ParT,ParU,ParV;
709 // Search an initial point in the list of Extrema Curve-Surface
710 if(Nend != 0 && !CExt.IsParallel())
712 for (i=1;i<=Nend;i++)
716 CExt.Points(i,P1,P2);
718 P2.Parameter(ParU, ParV);
720 aPrjPS.Perform(ParT, ParU, ParV, aTol, aLowBorder, aUppBorder, FuncTol, Standard_True);
722 if ( aPrjPS.IsDone() && P1.Parameter() > Max(FirstU,t-Step+Precision::PConfusion())
723 && P1.Parameter() <= t)
729 initpoint = Standard_True;
736 myCurve->D0(t,CPoint);
737 #ifdef OCCT_DEBUG_CHRONO
738 InitChron(chr_init_point);
740 // PConfusion - use geometric tolerances in extrema / optimization.
741 initpoint=InitialPoint(CPoint, t,myCurve,mySurface, Precision::PConfusion(), Precision::PConfusion(), U, V);
742 #ifdef OCCT_DEBUG_CHRONO
743 ResultChron(chr_init_point,t_init_point);
749 // When U or V lie on surface joint in some cases we cannot use them
750 // as initial point for aPrjPS, so we switch them
753 if ((mySurface->IsUPeriodic() &&
754 Abs(aUppBorder.X() - aLowBorder.X() - mySurface->UPeriod()) < Precision::Confusion()) ||
755 (mySurface->IsVPeriodic() &&
756 Abs(aUppBorder.Y() - aLowBorder.Y() - mySurface->VPeriod()) < Precision::Confusion()))
758 if((Abs(U - aLowBorder.X()) < mySurface->UResolution(Precision::PConfusion())) &&
759 mySurface->IsUPeriodic())
761 d1(t, U, V, D, myCurve, mySurface);
762 if (D.X() < 0 ) U = aUppBorder.X();
764 else if((Abs(U - aUppBorder.X()) < mySurface->UResolution(Precision::PConfusion())) &&
765 mySurface->IsUPeriodic())
767 d1(t, U, V, D, myCurve, mySurface);
768 if (D.X() > 0) U = aLowBorder.X();
771 if((Abs(V - aLowBorder.Y()) < mySurface->VResolution(Precision::PConfusion())) &&
772 mySurface->IsVPeriodic())
774 d1(t, U, V, D, myCurve, mySurface);
775 if (D.Y() < 0) V = aUppBorder.Y();
777 else if((Abs(V - aUppBorder.Y()) <= mySurface->VResolution(Precision::PConfusion())) &&
778 mySurface->IsVPeriodic())
780 d1(t, U, V, D, myCurve, mySurface);
781 if (D.Y() > 0) V = aLowBorder.Y();
787 // Here we are going to stop if the distance between projection and
788 // corresponding curve point is greater than myMaxDist
791 mySurface->D0(U, V, POnS);
792 d = CPoint.Distance(POnS);
800 Triple = gp_Pnt(t, U, V);
803 //Search for exact boundary point
804 Tol = Min(myTolU, myTolV);
806 d1(Triple.X(), Triple.Y(), Triple.Z(), aD, myCurve, mySurface);
807 Tol /= Max(Abs(aD.X()), Abs(aD.Y()));
809 if(!ExactBound(Triple, t - Step, Tol,
810 myTolU, myTolV, myCurve, mySurface))
813 std::cout<<"There is a problem with ExactBound computation"<<std::endl;
815 DichExactBound(Triple, t - Step, Tol, myTolU, myTolV,
819 new_part = Standard_True;
823 if(t == LastU) break;
832 if (!new_part) break;
834 //We have found a new continuous part
835 Handle(TColgp_HSequenceOfPnt) hSeq = new TColgp_HSequenceOfPnt();
836 mySequence->Append(hSeq);
838 mySequence->Value(myNbCurves)->Append(Triple);
841 if (Triple.X() == LastU) break;//return;
843 //Computation of WalkStep
845 Standard_Real MagnD1, MagnD2;
846 d2CurvOnSurf(Triple.X(), Triple.Y(), Triple.Z(), D1, D2, myCurve, mySurface);
847 MagnD1 = D1.Magnitude();
848 MagnD2 = D2.Magnitude();
849 if(MagnD2 < Precision::Confusion()) WalkStep = MaxStep;
850 else WalkStep = Min(MaxStep, Max(MinStep, 0.1*MagnD1/MagnD2));
854 t = Triple.X() + Step;
855 if (t > LastU) t = LastU;
856 Standard_Real prevStep = Step;
857 Standard_Real U0, V0;
859 //Here we are trying to prolong continuous part
860 while (t <= LastU && new_part)
863 U0 = Triple.Y() + (Step / prevStep) * (Triple.Y() - prevTriple.Y());
864 V0 = Triple.Z() + (Step / prevStep) * (Triple.Z() - prevTriple.Z());
865 // adjust U0 to be in [mySurface->FirstUParameter(),mySurface->LastUParameter()]
866 U0 = Min(Max(U0, aLowBorder.X()), aUppBorder.X());
867 // adjust V0 to be in [mySurface->FirstVParameter(),mySurface->LastVParameter()]
868 V0 = Min(Max(V0, aLowBorder.Y()), aUppBorder.Y());
871 aPrjPS.Perform(t, U0, V0, aTol,
872 aLowBorder, aUppBorder, FuncTol, Standard_True);
875 if (Step <= GlobalMinStep)
877 //Search for exact boundary point
878 Tol = Min(myTolU, myTolV);
880 d1(Triple.X(), Triple.Y(), Triple.Z(), D, myCurve, mySurface);
881 Tol /= Max(Abs(D.X()), Abs(D.Y()));
883 if(!ExactBound(Triple, t, Tol, myTolU, myTolV,
887 std::cout<<"There is a problem with ExactBound computation"<<std::endl;
889 DichExactBound(Triple, t, Tol, myTolU, myTolV,
893 if((Triple.X() - mySequence->Value(myNbCurves)->Value(mySequence->Value(myNbCurves)->Length()).X()) > 1.e-10)
894 mySequence->Value(myNbCurves)->Append(Triple);
895 if((LastU - Triple.X()) < Tol) {t = LastU + 1; break;}//return;
898 t = Triple.X() + Step;
899 if (t > (LastU-MinStep/2) )
904 new_part = Standard_False;
909 Standard_Real SaveStep = Step;
911 t = Triple .X() + Step;
912 if (t > (LastU-MinStep/4) )
915 if (Abs(Step - SaveStep) <= Precision::PConfusion())
916 Step = GlobalMinStep; //to avoid looping
926 Triple = gp_Pnt(t, aPrjPS.Solution().X(), aPrjPS.Solution().Y());
928 // Check for possible local traps.
929 UpdateTripleByTrapCriteria(Triple);
931 // Protection from case when the whole curve lies on a seam.
932 if (!isSplitsComputed)
934 Standard_Boolean isUPossible = Standard_False;
935 if (mySurface->IsUPeriodic() &&
936 (Abs(Triple.Y() - mySurface->FirstUParameter() ) > Precision::PConfusion() &&
937 Abs(Triple.Y() - mySurface->LastUParameter() ) > Precision::PConfusion()))
939 isUPossible = Standard_True;
942 Standard_Boolean isVPossible = Standard_False;
943 if (mySurface->IsVPeriodic() &&
944 (Abs(Triple.Z() - mySurface->FirstVParameter() ) > Precision::PConfusion() &&
945 Abs(Triple.Z() - mySurface->LastVParameter() ) > Precision::PConfusion()))
947 isVPossible = Standard_True;
950 if (isUPossible || isVPossible)
952 // When point is good conditioned.
953 BuildCurveSplits(myCurve, mySurface, myTolU, myTolV, aSplits);
954 isSplitsComputed = Standard_True;
958 if((Triple.X() - mySequence->Value(myNbCurves)->Value(mySequence->Value(myNbCurves)->Length()).X()) > 1.e-10)
959 mySequence->Value(myNbCurves)->Append(Triple);
960 if (t == LastU) {t = LastU + 1; break;}//return;
961 //Computation of WalkStep
962 d2CurvOnSurf(Triple.X(), Triple.Y(), Triple.Z(), D1, D2, myCurve, mySurface);
963 MagnD1 = D1.Magnitude();
964 MagnD2 = D2.Magnitude();
965 if(MagnD2 < Precision::Confusion() ) WalkStep = MaxStep;
966 else WalkStep = Min(MaxStep, Max(MinStep, 0.1*MagnD1/MagnD2));
970 if (t > (LastU-MinStep/2))
972 Step = Step + LastU - t;
976 // We assume at least one point of cache inside of a split.
977 const Standard_Integer aSize = aSplits.Size();
978 for(Standard_Integer anIdx = aSplitIdx; anIdx < aSize; ++anIdx)
980 const Standard_Real aParam = aSplits(anIdx);
981 if (Abs(aParam - Triple.X() ) < Precision::PConfusion())
983 // The current point is equal to a split point.
984 new_part = Standard_False;
986 // Move split index to avoid check of the whole list.
990 else if (aParam < t + Precision::PConfusion() )
992 // The next point crosses the split point.
994 Step = t - prevTriple.X();
996 } // for(Standard_Integer anIdx = aSplitIdx; anIdx < aSize; ++anIdx)
1001 // Sequence post-proceeding.
1004 // 1. Removing poor parts
1005 Standard_Integer NbPart=myNbCurves;
1006 Standard_Integer ipart=1;
1007 for(i = 1; i <= NbPart; i++) {
1008 // Standard_Integer NbPoints = mySequence->Value(i)->Length();
1009 if(mySequence->Value(ipart)->Length() < 2) {
1010 mySequence->Remove(ipart);
1016 if(myNbCurves == 0) return;
1018 // 2. Removing common parts of bounds
1019 for(i = 1; i < myNbCurves; i++)
1021 if(mySequence->Value(i)->Value(mySequence->Value(i)->Length()).X() >=
1022 mySequence->Value(i+1)->Value(1).X())
1024 mySequence->ChangeValue(i+1)->ChangeValue(1).SetX(mySequence->Value(i)->Value(mySequence->Value(i)->Length()).X() + 1.e-12);
1028 // 3. Computation of the maximum distance from each part of curve to surface
1030 myMaxDistance = new TColStd_HArray1OfReal(1, myNbCurves);
1031 myMaxDistance->Init(0);
1032 for(i = 1; i <= myNbCurves; i++)
1034 for(j = 1; j <= mySequence->Value(i)->Length(); j++)
1036 gp_Pnt POnC, POnS, aTriple;
1037 Standard_Real Distance;
1038 aTriple = mySequence->Value(i)->Value(j);
1039 myCurve->D0(aTriple.X(), POnC);
1040 mySurface->D0(aTriple.Y(), aTriple.Z(), POnS);
1041 Distance = POnC.Distance(POnS);
1042 if (myMaxDistance->Value(i) < Distance)
1044 myMaxDistance->ChangeValue(i) = Distance;
1049 // 4. Check the projection to be a single point
1051 gp_Pnt2d Pmoy, Pcurr, P;
1052 Standard_Real AveU, AveV;
1053 mySnglPnts = new TColStd_HArray1OfBoolean(1, myNbCurves);
1054 mySnglPnts->Init (Standard_True);
1056 for(i = 1; i <= myNbCurves; i++)
1058 //compute an average U and V
1060 for(j = 1, AveU = 0., AveV = 0.; j <= mySequence->Value(i)->Length(); j++)
1062 AveU += mySequence->Value(i)->Value(j).Y();
1063 AveV += mySequence->Value(i)->Value(j).Z();
1065 AveU /= mySequence->Value(i)->Length();
1066 AveV /= mySequence->Value(i)->Length();
1068 Pmoy.SetCoord(AveU,AveV);
1069 for(j = 1; j <= mySequence->Value(i)->Length(); j++)
1072 gp_Pnt2d(mySequence->Value(i)->Value(j).Y(), mySequence->Value(i)->Value(j).Z());
1073 if (Pcurr.Distance(Pmoy) > ((myTolU < myTolV) ? myTolV : myTolU))
1075 mySnglPnts->SetValue(i, Standard_False);
1081 // 5. Check the projection to be an isoparametric curve of the surface
1083 myUIso = new TColStd_HArray1OfBoolean(1, myNbCurves);
1084 myUIso->Init (Standard_True);
1086 myVIso = new TColStd_HArray1OfBoolean(1, myNbCurves);
1087 myVIso->Init (Standard_True);
1089 for(i = 1; i <= myNbCurves; i++) {
1090 if (IsSinglePnt(i, P)|| mySequence->Value(i)->Length() <=2) {
1091 myUIso->SetValue(i, Standard_False);
1092 myVIso->SetValue(i, Standard_False);
1096 // new test for isoparametrics
1098 if ( mySequence->Value(i)->Length() > 2) {
1099 //compute an average U and V
1101 for(j = 1, AveU = 0., AveV = 0.; j <= mySequence->Value(i)->Length(); j++) {
1102 AveU += mySequence->Value(i)->Value(j).Y();
1103 AveV += mySequence->Value(i)->Value(j).Z();
1105 AveU /= mySequence->Value(i)->Length();
1106 AveV /= mySequence->Value(i)->Length();
1108 // is i-part U-isoparametric ?
1109 for(j = 1; j <= mySequence->Value(i)->Length(); j++)
1111 if(Abs(mySequence->Value(i)->Value(j).Y() - AveU) > myTolU)
1113 myUIso->SetValue(i, Standard_False);
1118 // is i-part V-isoparametric ?
1119 for(j = 1; j <= mySequence->Value(i)->Length(); j++)
1121 if(Abs(mySequence->Value(i)->Value(j).Z() - AveV) > myTolV)
1123 myVIso->SetValue(i, Standard_False);
1131 //=======================================================================
1134 //=======================================================================
1136 void ProjLib_CompProjectedCurve::Load(const Handle(Adaptor3d_HSurface)& S)
1141 //=======================================================================
1144 //=======================================================================
1146 void ProjLib_CompProjectedCurve::Load(const Handle(Adaptor3d_HCurve)& C)
1151 //=======================================================================
1152 //function : GetSurface
1154 //=======================================================================
1156 const Handle(Adaptor3d_HSurface)& ProjLib_CompProjectedCurve::GetSurface() const
1162 //=======================================================================
1163 //function : GetCurve
1165 //=======================================================================
1167 const Handle(Adaptor3d_HCurve)& ProjLib_CompProjectedCurve::GetCurve() const
1172 //=======================================================================
1173 //function : GetTolerance
1175 //=======================================================================
1177 void ProjLib_CompProjectedCurve::GetTolerance(Standard_Real& TolU,
1178 Standard_Real& TolV) const
1184 //=======================================================================
1185 //function : NbCurves
1187 //=======================================================================
1189 Standard_Integer ProjLib_CompProjectedCurve::NbCurves() const
1193 //=======================================================================
1196 //=======================================================================
1198 void ProjLib_CompProjectedCurve::Bounds(const Standard_Integer Index,
1199 Standard_Real& Udeb,
1200 Standard_Real& Ufin) const
1202 if(Index < 1 || Index > myNbCurves) throw Standard_NoSuchObject();
1203 Udeb = mySequence->Value(Index)->Value(1).X();
1204 Ufin = mySequence->Value(Index)->Value(mySequence->Value(Index)->Length()).X();
1206 //=======================================================================
1207 //function : IsSinglePnt
1209 //=======================================================================
1211 Standard_Boolean ProjLib_CompProjectedCurve::IsSinglePnt(const Standard_Integer Index, gp_Pnt2d& P) const
1213 if(Index < 1 || Index > myNbCurves) throw Standard_NoSuchObject();
1214 P = gp_Pnt2d(mySequence->Value(Index)->Value(1).Y(), mySequence->Value(Index)->Value(1).Z());
1215 return mySnglPnts->Value(Index);
1218 //=======================================================================
1221 //=======================================================================
1223 Standard_Boolean ProjLib_CompProjectedCurve::IsUIso(const Standard_Integer Index, Standard_Real& U) const
1225 if(Index < 1 || Index > myNbCurves) throw Standard_NoSuchObject();
1226 U = mySequence->Value(Index)->Value(1).Y();
1227 return myUIso->Value(Index);
1229 //=======================================================================
1232 //=======================================================================
1234 Standard_Boolean ProjLib_CompProjectedCurve::IsVIso(const Standard_Integer Index, Standard_Real& V) const
1236 if(Index < 1 || Index > myNbCurves) throw Standard_NoSuchObject();
1237 V = mySequence->Value(Index)->Value(1).Z();
1238 return myVIso->Value(Index);
1240 //=======================================================================
1243 //=======================================================================
1245 gp_Pnt2d ProjLib_CompProjectedCurve::Value(const Standard_Real t) const
1251 //=======================================================================
1254 //=======================================================================
1256 void ProjLib_CompProjectedCurve::D0(const Standard_Real U,gp_Pnt2d& P) const
1258 Standard_Integer i, j;
1259 Standard_Real Udeb, Ufin;
1260 Standard_Boolean found = Standard_False;
1262 for(i = 1; i <= myNbCurves; i++)
1264 Bounds(i, Udeb, Ufin);
1265 if (U >= Udeb && U <= Ufin)
1267 found = Standard_True;
1271 if (!found) throw Standard_DomainError("ProjLib_CompProjectedCurve::D0");
1273 Standard_Real U0, V0;
1275 Standard_Integer End = mySequence->Value(i)->Length();
1276 for(j = 1; j < End; j++)
1277 if ((U >= mySequence->Value(i)->Value(j).X()) && (U <= mySequence->Value(i)->Value(j + 1).X())) break;
1279 // U0 = mySequence->Value(i)->Value(j).Y();
1280 // V0 = mySequence->Value(i)->Value(j).Z();
1282 // Cubic Interpolation
1283 if(mySequence->Value(i)->Length() < 4 ||
1284 (Abs(U-mySequence->Value(i)->Value(j).X()) <= Precision::PConfusion()) )
1286 U0 = mySequence->Value(i)->Value(j).Y();
1287 V0 = mySequence->Value(i)->Value(j).Z();
1289 else if (Abs(U-mySequence->Value(i)->Value(j+1).X())
1290 <= Precision::PConfusion())
1292 U0 = mySequence->Value(i)->Value(j+1).Y();
1293 V0 = mySequence->Value(i)->Value(j+1).Z();
1298 if (j > mySequence->Value(i)->Length() - 2)
1299 j = mySequence->Value(i)->Length() - 2;
1301 gp_Vec2d I1, I2, I3, I21, I22, I31, Y1, Y2, Y3, Y4, Res;
1302 Standard_Real X1, X2, X3, X4;
1304 X1 = mySequence->Value(i)->Value(j - 1).X();
1305 X2 = mySequence->Value(i)->Value(j).X();
1306 X3 = mySequence->Value(i)->Value(j + 1).X();
1307 X4 = mySequence->Value(i)->Value(j + 2).X();
1309 Y1 = gp_Vec2d(mySequence->Value(i)->Value(j - 1).Y(),
1310 mySequence->Value(i)->Value(j - 1).Z());
1311 Y2 = gp_Vec2d(mySequence->Value(i)->Value(j).Y(),
1312 mySequence->Value(i)->Value(j).Z());
1313 Y3 = gp_Vec2d(mySequence->Value(i)->Value(j + 1).Y(),
1314 mySequence->Value(i)->Value(j + 1).Z());
1315 Y4 = gp_Vec2d(mySequence->Value(i)->Value(j + 2).Y(),
1316 mySequence->Value(i)->Value(j + 2).Z());
1318 I1 = (Y1 - Y2)/(X1 - X2);
1319 I2 = (Y2 - Y3)/(X2 - X3);
1320 I3 = (Y3 - Y4)/(X3 - X4);
1322 I21 = (I1 - I2)/(X1 - X3);
1323 I22 = (I2 - I3)/(X2 - X4);
1325 I31 = (I21 - I22)/(X1 - X4);
1327 Res = Y1 + (U - X1)*(I1 + (U - X2)*(I21 + (U - X3)*I31));
1332 if(U0 < mySurface->FirstUParameter()) U0 = mySurface->FirstUParameter();
1333 else if(U0 > mySurface->LastUParameter()) U0 = mySurface->LastUParameter();
1335 if(V0 < mySurface->FirstVParameter()) V0 = mySurface->FirstVParameter();
1336 else if(V0 > mySurface->LastVParameter()) V0 = mySurface->LastVParameter();
1338 //End of cubic interpolation
1340 ProjLib_PrjResolve aPrjPS(myCurve->Curve(), mySurface->Surface(), 1);
1341 aPrjPS.Perform(U, U0, V0, gp_Pnt2d(myTolU, myTolV),
1342 gp_Pnt2d(mySurface->FirstUParameter(), mySurface->FirstVParameter()),
1343 gp_Pnt2d(mySurface->LastUParameter(), mySurface->LastVParameter()));
1344 if (aPrjPS.IsDone())
1345 P = aPrjPS.Solution();
1348 gp_Pnt thePoint = myCurve->Value(U);
1349 Extrema_ExtPS aExtPS(thePoint, mySurface->Surface(), myTolU, myTolV);
1350 if (aExtPS.IsDone() && aExtPS.NbExt())
1352 Standard_Integer k, Nend, imin = 1;
1353 // Search for the nearest solution which is also a normal projection
1354 Nend = aExtPS.NbExt();
1355 for(k = 2; k <= Nend; k++)
1356 if (aExtPS.SquareDistance(k) < aExtPS.SquareDistance(imin))
1358 const Extrema_POnSurf& POnS = aExtPS.Point(imin);
1359 Standard_Real ParU,ParV;
1360 POnS.Parameter(ParU, ParV);
1361 P.SetCoord(ParU, ParV);
1367 //=======================================================================
1370 //=======================================================================
1372 void ProjLib_CompProjectedCurve::D1(const Standard_Real t,
1380 d1(t, u, v, V, myCurve, mySurface);
1382 //=======================================================================
1385 //=======================================================================
1387 void ProjLib_CompProjectedCurve::D2(const Standard_Real t,
1396 d2(t, u, v, V1, V2, myCurve, mySurface);
1398 //=======================================================================
1401 //=======================================================================
1403 gp_Vec2d ProjLib_CompProjectedCurve::DN(const Standard_Real t,
1404 const Standard_Integer N) const
1406 if (N < 1 ) throw Standard_OutOfRange("ProjLib_CompProjectedCurve : N must be greater than 0");
1422 throw Standard_NotImplemented("ProjLib_CompProjectedCurve::DN");
1426 //=======================================================================
1427 //function : GetSequence
1429 //=======================================================================
1431 const Handle(ProjLib_HSequenceOfHSequenceOfPnt)& ProjLib_CompProjectedCurve::GetSequence() const
1435 //=======================================================================
1436 //function : FirstParameter
1438 //=======================================================================
1440 Standard_Real ProjLib_CompProjectedCurve::FirstParameter() const
1442 return myCurve->FirstParameter();
1445 //=======================================================================
1446 //function : LastParameter
1448 //=======================================================================
1450 Standard_Real ProjLib_CompProjectedCurve::LastParameter() const
1452 return myCurve->LastParameter();
1455 //=======================================================================
1456 //function : MaxDistance
1458 //=======================================================================
1460 Standard_Real ProjLib_CompProjectedCurve::MaxDistance(const Standard_Integer Index) const
1462 if(Index < 1 || Index > myNbCurves) throw Standard_NoSuchObject();
1463 return myMaxDistance->Value(Index);
1466 //=======================================================================
1467 //function : NbIntervals
1469 //=======================================================================
1471 Standard_Integer ProjLib_CompProjectedCurve::NbIntervals(const GeomAbs_Shape S) const
1473 const_cast<ProjLib_CompProjectedCurve*>(this)->myTabInt.Nullify();
1475 return myTabInt->Length() - 1;
1478 //=======================================================================
1479 //function : Intervals
1481 //=======================================================================
1483 void ProjLib_CompProjectedCurve::Intervals(TColStd_Array1OfReal& T,const GeomAbs_Shape S) const
1485 if (myTabInt.IsNull()) BuildIntervals (S);
1486 T = myTabInt->Array1();
1489 //=======================================================================
1490 //function : BuildIntervals
1492 //=======================================================================
1494 void ProjLib_CompProjectedCurve::BuildIntervals(const GeomAbs_Shape S) const
1496 GeomAbs_Shape SforS = GeomAbs_CN;
1514 throw Standard_OutOfRange();
1516 Standard_Integer i, j, k;
1517 Standard_Integer NbIntCur = myCurve->NbIntervals(S);
1518 Standard_Integer NbIntSurU = mySurface->NbUIntervals(SforS);
1519 Standard_Integer NbIntSurV = mySurface->NbVIntervals(SforS);
1521 TColStd_Array1OfReal CutPntsT(1, NbIntCur+1);
1522 TColStd_Array1OfReal CutPntsU(1, NbIntSurU+1);
1523 TColStd_Array1OfReal CutPntsV(1, NbIntSurV+1);
1525 myCurve->Intervals(CutPntsT, S);
1526 mySurface->UIntervals(CutPntsU, SforS);
1527 mySurface->VIntervals(CutPntsV, SforS);
1529 Standard_Real Tl, Tr, Ul, Ur, Vl, Vr, Tol;
1531 Handle(TColStd_HArray1OfReal) BArr = NULL,
1536 // proccessing projection bounds
1537 BArr = new TColStd_HArray1OfReal(1, 2*myNbCurves);
1538 for(i = 1; i <= myNbCurves; i++)
1540 Bounds(i, BArr->ChangeValue(2*i - 1), BArr->ChangeValue(2*i));
1543 // proccessing curve discontinuities
1545 CArr = new TColStd_HArray1OfReal(1, NbIntCur - 1);
1546 for(i = 1; i <= CArr->Length(); i++)
1548 CArr->ChangeValue(i) = CutPntsT(i + 1);
1552 // proccessing U-surface discontinuities
1553 TColStd_SequenceOfReal TUdisc;
1555 for(k = 2; k <= NbIntSurU; k++) {
1556 // std::cout<<"CutPntsU("<<k<<") = "<<CutPntsU(k)<<std::endl;
1557 for(i = 1; i <= myNbCurves; i++)
1559 for(j = 1; j < mySequence->Value(i)->Length(); j++)
1561 Ul = mySequence->Value(i)->Value(j).Y();
1562 Ur = mySequence->Value(i)->Value(j + 1).Y();
1564 if(Abs(Ul - CutPntsU(k)) <= myTolU)
1565 TUdisc.Append(mySequence->Value(i)->Value(j).X());
1566 else if(Abs(Ur - CutPntsU(k)) <= myTolU)
1567 TUdisc.Append(mySequence->Value(i)->Value(j + 1).X());
1568 else if((Ul < CutPntsU(k) && CutPntsU(k) < Ur) ||
1569 (Ur < CutPntsU(k) && CutPntsU(k) < Ul))
1572 V = (mySequence->Value(i)->Value(j).Z()
1573 + mySequence->Value(i)->Value(j +1).Z())/2;
1574 ProjLib_PrjResolve Solver(myCurve->Curve(), mySurface->Surface(), 2);
1578 Triple = mySequence->Value(i)->Value(j);
1579 d1(Triple.X(), Triple.Y(), Triple.Z(), D, myCurve, mySurface);
1580 if (Abs(D.X()) < Precision::Confusion())
1583 Tol = Min(myTolU, myTolU / Abs(D.X()));
1585 Tl = mySequence->Value(i)->Value(j).X();
1586 Tr = mySequence->Value(i)->Value(j + 1).X();
1588 Solver.Perform((Tl + Tr)/2, CutPntsU(k), V,
1589 gp_Pnt2d(Tol, myTolV),
1590 gp_Pnt2d(Tl, mySurface->FirstVParameter()),
1591 gp_Pnt2d(Tr, mySurface->LastVParameter()));
1595 TUdisc.Append(Solver.Solution().X());
1601 for(i = 2; i <= TUdisc.Length(); i++)
1603 if(TUdisc(i) - TUdisc(i-1) < Precision::PConfusion())
1611 UArr = new TColStd_HArray1OfReal(1, TUdisc.Length());
1612 for(i = 1; i <= UArr->Length(); i++)
1614 UArr->ChangeValue(i) = TUdisc(i);
1617 // proccessing V-surface discontinuities
1618 TColStd_SequenceOfReal TVdisc;
1620 for(k = 2; k <= NbIntSurV; k++)
1622 for(i = 1; i <= myNbCurves; i++)
1624 // std::cout<<"CutPntsV("<<k<<") = "<<CutPntsV(k)<<std::endl;
1625 for(j = 1; j < mySequence->Value(i)->Length(); j++) {
1627 Vl = mySequence->Value(i)->Value(j).Z();
1628 Vr = mySequence->Value(i)->Value(j + 1).Z();
1630 if(Abs(Vl - CutPntsV(k)) <= myTolV)
1631 TVdisc.Append(mySequence->Value(i)->Value(j).X());
1632 else if (Abs(Vr - CutPntsV(k)) <= myTolV)
1633 TVdisc.Append(mySequence->Value(i)->Value(j + 1).X());
1634 else if((Vl < CutPntsV(k) && CutPntsV(k) < Vr) ||
1635 (Vr < CutPntsV(k) && CutPntsV(k) < Vl))
1638 U = (mySequence->Value(i)->Value(j).Y()
1639 + mySequence->Value(i)->Value(j +1).Y())/2;
1640 ProjLib_PrjResolve Solver(myCurve->Curve(), mySurface->Surface(), 3);
1644 Triple = mySequence->Value(i)->Value(j);
1645 d1(Triple.X(), Triple.Y(), Triple.Z(), D, myCurve, mySurface);
1646 if (Abs(D.Y()) < Precision::Confusion())
1649 Tol = Min(myTolV, myTolV / Abs(D.Y()));
1651 Tl = mySequence->Value(i)->Value(j).X();
1652 Tr = mySequence->Value(i)->Value(j + 1).X();
1654 Solver.Perform((Tl + Tr)/2, U, CutPntsV(k),
1655 gp_Pnt2d(Tol, myTolV),
1656 gp_Pnt2d(Tl, mySurface->FirstUParameter()),
1657 gp_Pnt2d(Tr, mySurface->LastUParameter()));
1661 TVdisc.Append(Solver.Solution().X());
1668 for(i = 2; i <= TVdisc.Length(); i++)
1670 if(TVdisc(i) - TVdisc(i-1) < Precision::PConfusion())
1678 VArr = new TColStd_HArray1OfReal(1, TVdisc.Length());
1679 for(i = 1; i <= VArr->Length(); i++)
1681 VArr->ChangeValue(i) = TVdisc(i);
1686 TColStd_SequenceOfReal Fusion;
1689 GeomLib::FuseIntervals(BArr->ChangeArray1(),
1690 CArr->ChangeArray1(),
1691 Fusion, Precision::PConfusion());
1692 BArr = new TColStd_HArray1OfReal(1, Fusion.Length());
1693 for(i = 1; i <= BArr->Length(); i++)
1695 BArr->ChangeValue(i) = Fusion(i);
1702 GeomLib::FuseIntervals(BArr->ChangeArray1(),
1703 UArr->ChangeArray1(),
1704 Fusion, Precision::PConfusion());
1705 BArr = new TColStd_HArray1OfReal(1, Fusion.Length());
1706 for(i = 1; i <= BArr->Length(); i++)
1708 BArr->ChangeValue(i) = Fusion(i);
1715 GeomLib::FuseIntervals(BArr->ChangeArray1(),
1716 VArr->ChangeArray1(),
1717 Fusion, Precision::PConfusion());
1718 BArr = new TColStd_HArray1OfReal(1, Fusion.Length());
1719 for(i = 1; i <= BArr->Length(); i++)
1721 BArr->ChangeValue(i) = Fusion(i);
1725 const_cast<ProjLib_CompProjectedCurve*>(this)->myTabInt = new TColStd_HArray1OfReal(1, BArr->Length());
1726 for(i = 1; i <= BArr->Length(); i++)
1728 myTabInt->ChangeValue(i) = BArr->Value(i);
1732 //=======================================================================
1735 //=======================================================================
1737 Handle(Adaptor2d_HCurve2d) ProjLib_CompProjectedCurve::Trim
1738 (const Standard_Real First,
1739 const Standard_Real Last,
1740 const Standard_Real Tol) const
1742 Handle(ProjLib_HCompProjectedCurve) HCS =
1743 new ProjLib_HCompProjectedCurve(*this);
1744 HCS->ChangeCurve2d().Load(mySurface);
1745 HCS->ChangeCurve2d().Load(myCurve->Trim(First,Last,Tol));
1749 //=======================================================================
1750 //function : GetType
1752 //=======================================================================
1754 GeomAbs_CurveType ProjLib_CompProjectedCurve::GetType() const
1756 return GeomAbs_OtherCurve;
1759 //=======================================================================
1760 //function : UpdateTripleByTrapCriteria
1762 //=======================================================================
1763 void ProjLib_CompProjectedCurve::UpdateTripleByTrapCriteria(gp_Pnt &thePoint) const
1765 Standard_Boolean isProblemsPossible = Standard_False;
1766 // Check possible traps cases:
1769 if (mySurface->GetType() == GeomAbs_SurfaceOfRevolution)
1771 // Compute maximal deviation from 3D and choose the biggest one.
1772 Standard_Real aVRes = mySurface->VResolution(Precision::Confusion());
1773 Standard_Real aMaxTol = Max(Precision::PConfusion(), aVRes);
1775 if (Abs (thePoint.Z() - mySurface->FirstVParameter()) < aMaxTol ||
1776 Abs (thePoint.Z() - mySurface->LastVParameter() ) < aMaxTol )
1778 isProblemsPossible = Standard_True;
1782 // 27135 bug. Trap on degenerated edge.
1783 if (mySurface->GetType() == GeomAbs_Sphere &&
1784 (Abs (thePoint.Z() - mySurface->FirstVParameter()) < Precision::PConfusion() ||
1785 Abs (thePoint.Z() - mySurface->LastVParameter() ) < Precision::PConfusion() ||
1786 Abs (thePoint.Y() - mySurface->FirstUParameter()) < Precision::PConfusion() ||
1787 Abs (thePoint.Y() - mySurface->LastUParameter() ) < Precision::PConfusion() ))
1789 isProblemsPossible = Standard_True;
1792 if (!isProblemsPossible)
1796 Standard_Boolean isDone =
1797 InitialPoint(myCurve->Value(thePoint.X()), thePoint.X(), myCurve, mySurface,
1798 Precision::PConfusion(), Precision::PConfusion(), U, V);
1803 // Restore original position in case of period jump.
1804 if (mySurface->IsUPeriodic() &&
1805 Abs (Abs(U - thePoint.Y()) - mySurface->UPeriod()) < Precision::PConfusion())
1809 if (mySurface->IsVPeriodic() &&
1810 Abs (Abs(V - thePoint.Z()) - mySurface->VPeriod()) < Precision::PConfusion())
1818 //=======================================================================
1819 //function : BuildCurveSplits
1821 //=======================================================================
1822 void BuildCurveSplits(const Handle(Adaptor3d_HCurve) &theCurve,
1823 const Handle(Adaptor3d_HSurface) &theSurface,
1824 const Standard_Real theTolU,
1825 const Standard_Real theTolV,
1826 NCollection_Vector<Standard_Real> &theSplits)
1828 SplitDS aDS(theCurve, theSurface, theSplits);
1830 Extrema_ExtPS anExtPS;
1831 anExtPS.Initialize(theSurface->Surface(),
1832 theSurface->FirstUParameter(), theSurface->LastUParameter(),
1833 theSurface->FirstVParameter(), theSurface->LastVParameter(),
1835 aDS.myExtPS = &anExtPS;
1837 if (theSurface->IsUPeriodic())
1839 aDS.myPeriodicDir = 0;
1840 SplitOnDirection(aDS);
1842 if (theSurface->IsVPeriodic())
1844 aDS.myPeriodicDir = 1;
1845 SplitOnDirection(aDS);
1848 std::sort(aDS.mySplits.begin(), aDS.mySplits.end(), Comparator);
1851 //=======================================================================
1852 //function : SplitOnDirection
1853 //purpose : This method compute points in the parameter space of the curve
1854 // on which curve should be split since period jump is happen.
1855 //=======================================================================
1856 void SplitOnDirection(SplitDS & theSplitDS)
1859 // Create 3D curve which is correspond to the periodic bound in 2d space.
1860 // Run curve / curve extrema and run extrema point / surface to check that
1861 // the point will be projected to the periodic bound.
1862 // In this method assumed that the points cannot be closer to each other that 1% of the parameter space.
1864 gp_Pnt2d aStartPnt(theSplitDS.mySurface->FirstUParameter(), theSplitDS.mySurface->FirstVParameter());
1865 gp_Dir2d aDir(theSplitDS.myPeriodicDir, (Standard_Integer)!theSplitDS.myPeriodicDir);
1867 theSplitDS.myPerMinParam = !theSplitDS.myPeriodicDir ? theSplitDS.mySurface->FirstUParameter():
1868 theSplitDS.mySurface->FirstVParameter();
1869 theSplitDS.myPerMaxParam = !theSplitDS.myPeriodicDir ? theSplitDS.mySurface->LastUParameter():
1870 theSplitDS.mySurface->LastVParameter();
1871 Standard_Real aLast2DParam = theSplitDS.myPeriodicDir ?
1872 theSplitDS.mySurface->LastUParameter() - theSplitDS.mySurface->FirstUParameter():
1873 theSplitDS.mySurface->LastVParameter() - theSplitDS.mySurface->FirstVParameter();
1875 // Create line which is represent periodic border.
1876 Handle(Geom2d_Curve) aC2GC = new Geom2d_Line(aStartPnt, aDir);
1877 Handle(Geom2dAdaptor_HCurve) aC = new Geom2dAdaptor_HCurve(aC2GC, 0, aLast2DParam);
1878 Adaptor3d_CurveOnSurface aCOnS(aC, theSplitDS.mySurface);
1880 Extrema_ExtCC anExtCC;
1881 anExtCC.SetCurve(1, aCOnS);
1882 anExtCC.SetCurve(2, theSplitDS.myCurve->Curve());
1883 anExtCC.SetSingleSolutionFlag(Standard_True); // Search only one solution since multiple invocations are needed.
1884 anExtCC.SetRange(1, 0, aLast2DParam);
1885 theSplitDS.myExtCC = &anExtCC;
1887 FindSplitPoint(theSplitDS,
1888 theSplitDS.myCurve->FirstParameter(), // Initial curve range.
1889 theSplitDS.myCurve->LastParameter());
1893 //=======================================================================
1894 //function : FindSplitPoint
1896 //=======================================================================
1897 void FindSplitPoint(SplitDS &theSplitDS,
1898 const Standard_Real theMinParam,
1899 const Standard_Real theMaxParam)
1901 // Make extrema copy to avoid dependencies between different levels of the recursion.
1902 Extrema_ExtCC anExtCC(*theSplitDS.myExtCC);
1903 anExtCC.SetRange(2, theMinParam, theMaxParam);
1906 if (anExtCC.IsDone() && !anExtCC.IsParallel())
1908 const Standard_Integer aNbExt = anExtCC.NbExt();
1909 for (Standard_Integer anIdx = 1; anIdx <= aNbExt; ++anIdx)
1911 Extrema_POnCurv aPOnC1, aPOnC2;
1912 anExtCC.Points(anIdx, aPOnC1, aPOnC2);
1914 theSplitDS.myExtPS->Perform(aPOnC2.Value());
1915 if (!theSplitDS.myExtPS->IsDone())
1918 // Find point with the minimal Euclidean distance to avoid
1919 // false positive points detection.
1920 Standard_Integer aMinIdx = -1;
1921 Standard_Real aMinSqDist = RealLast();
1922 const Standard_Integer aNbPext = theSplitDS.myExtPS->NbExt();
1923 for(Standard_Integer aPIdx = 1; aPIdx <= aNbPext; ++aPIdx)
1925 const Standard_Real aCurrSqDist = theSplitDS.myExtPS->SquareDistance(aPIdx);
1927 if (aCurrSqDist < aMinSqDist)
1929 aMinSqDist = aCurrSqDist;
1934 // Check that is point will be projected to the periodic border.
1935 const Extrema_POnSurf &aPOnS = theSplitDS.myExtPS->Point(aMinIdx);
1936 Standard_Real U, V, aProjParam;
1937 aPOnS.Parameter(U, V);
1938 aProjParam = theSplitDS.myPeriodicDir ? V : U;
1941 if (Abs(aProjParam - theSplitDS.myPerMinParam) < Precision::PConfusion() ||
1942 Abs(aProjParam - theSplitDS.myPerMaxParam) < Precision::PConfusion() )
1944 const Standard_Real aParam = aPOnC2.Parameter();
1945 const Standard_Real aCFParam = theSplitDS.myCurve->FirstParameter();
1946 const Standard_Real aCLParam = theSplitDS.myCurve->LastParameter();
1948 if (aParam > aCFParam + Precision::PConfusion() &&
1949 aParam < aCLParam - Precision::PConfusion() )
1951 // Add only inner points.
1952 theSplitDS.mySplits.Append(aParam);
1955 const Standard_Real aDeltaCoeff = 0.01;
1956 const Standard_Real aDelta = (theMaxParam - theMinParam +
1957 aCLParam - aCFParam) * aDeltaCoeff;
1959 if (aParam - aDelta > theMinParam + Precision::PConfusion())
1961 FindSplitPoint(theSplitDS,
1962 theMinParam, aParam - aDelta); // Curve parameters.
1965 if (aParam + aDelta < theMaxParam - Precision::PConfusion())
1967 FindSplitPoint(theSplitDS,
1968 aParam + aDelta, theMaxParam); // Curve parameters.
1971 } // for (Standard_Integer anIdx = 1; anIdx <= aNbExt; ++anIdx)