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),
87 // Assignment operator is forbidden.
88 void operator=(const SplitDS &theSplitDS);
90 const Handle(Adaptor3d_HCurve) myCurve;
91 const Handle(Adaptor3d_HSurface) mySurface;
92 NCollection_Vector<Standard_Real> &mySplits;
94 Standard_Real myPerMinParam;
95 Standard_Real myPerMaxParam;
96 Standard_Integer myPeriodicDir;
98 Extrema_ExtCC *myExtCC;
99 Extrema_ExtPS *myExtPS;
102 //! Compute split points in the parameter space of the curve.
103 static void BuildCurveSplits(const Handle(Adaptor3d_HCurve) &theCurve,
104 const Handle(Adaptor3d_HSurface) &theSurface,
105 const Standard_Real theTolU,
106 const Standard_Real theTolV,
107 NCollection_Vector<Standard_Real> &theSplits);
109 //! Perform splitting on a specified direction. Sub-method in BuildCurveSplits.
110 static void SplitOnDirection(SplitDS & theSplitDS);
112 //! Perform recursive search of the split points.
113 static void FindSplitPoint(SplitDS & theSplitDS,
114 const Standard_Real theMinParam,
115 const Standard_Real theMaxParam);
118 //=======================================================================
119 //function : Comparator
120 //purpose : used in sort algorithm
121 //=======================================================================
122 inline Standard_Boolean Comparator(const Standard_Real theA,
123 const Standard_Real theB)
128 //=======================================================================
130 //purpose : computes first derivative of the projected curve
131 //=======================================================================
133 static void d1(const Standard_Real t,
134 const Standard_Real u,
135 const Standard_Real v,
137 const Handle(Adaptor3d_HCurve)& Curve,
138 const Handle(Adaptor3d_HSurface)& Surface)
141 gp_Vec DS1_u, DS1_v, DS2_u, DS2_uv, DS2_v, DC1_t;
142 Surface->D2(u, v, S, DS1_u, DS1_v, DS2_u, DS2_v, DS2_uv);
143 Curve->D1(t, C, DC1_t);
144 gp_Vec Ort(C, S);// Ort = S - C
146 gp_Vec2d dE_dt(-DC1_t*DS1_u, -DC1_t*DS1_v);
147 gp_XY dE_du(DS1_u*DS1_u + Ort*DS2_u,
148 DS1_u*DS1_v + Ort*DS2_uv);
149 gp_XY dE_dv(DS1_v*DS1_u + Ort*DS2_uv,
150 DS1_v*DS1_v + Ort*DS2_v);
152 Standard_Real det = dE_du.X()*dE_dv.Y() - dE_du.Y()*dE_dv.X();
153 if (fabs(det) < gp::Resolution()) throw Standard_ConstructionError();
155 gp_Mat2d M(gp_XY(dE_dv.Y()/det, -dE_du.Y()/det),
156 gp_XY(-dE_dv.X()/det, dE_du.X()/det));
158 V = - gp_Vec2d(gp_Vec2d(M.Row(1))*dE_dt, gp_Vec2d(M.Row(2))*dE_dt);
161 //=======================================================================
163 //purpose : computes second derivative of the projected curve
164 //=======================================================================
166 static void d2(const Standard_Real t,
167 const Standard_Real u,
168 const Standard_Real v,
169 gp_Vec2d& V1, gp_Vec2d& V2,
170 const Handle(Adaptor3d_HCurve)& Curve,
171 const Handle(Adaptor3d_HSurface)& Surface)
174 gp_Vec DS1_u, DS1_v, DS2_u, DS2_uv, DS2_v,
175 DS3_u, DS3_v, DS3_uuv, DS3_uvv,
177 Surface->D3(u, v, S, DS1_u, DS1_v, DS2_u, DS2_v, DS2_uv,
178 DS3_u, DS3_v, DS3_uuv, DS3_uvv);
179 Curve->D2(t, C, DC1_t, DC2_t);
182 gp_Vec2d dE_dt(-DC1_t*DS1_u, -DC1_t*DS1_v);
183 gp_XY dE_du(DS1_u*DS1_u + Ort*DS2_u,
184 DS1_u*DS1_v + Ort*DS2_uv);
185 gp_XY dE_dv(DS1_v*DS1_u + Ort*DS2_uv,
186 DS1_v*DS1_v + Ort*DS2_v);
188 Standard_Real det = dE_du.X()*dE_dv.Y() - dE_du.Y()*dE_dv.X();
189 if (fabs(det) < gp::Resolution()) throw Standard_ConstructionError();
191 gp_Mat2d M(gp_XY(dE_dv.Y()/det, -dE_du.Y()/det),
192 gp_XY(-dE_dv.X()/det, dE_du.X()/det));
195 V1 = - gp_Vec2d(gp_Vec2d(M.Row(1))*dE_dt, gp_Vec2d(M.Row(2))*dE_dt);
197 /* Second derivative */
199 // Computation of d2E_dt2 = S1
200 gp_Vec2d d2E_dt(-DC2_t*DS1_u, -DC2_t*DS1_v);
202 // Computation of 2*(d2E/dtdX)(dX/dt) = S2
203 gp_Vec2d d2E1_dtdX(-DC1_t*DS2_u,
205 gp_Vec2d d2E2_dtdX(-DC1_t*DS2_uv,
207 gp_Vec2d S2 = 2*gp_Vec2d(d2E1_dtdX*V1, d2E2_dtdX*V1);
209 // Computation of (d2E/dX2)*(dX/dt)2 = S3
211 // Row11 = (d2E1/du2, d2E1/dudv)
213 gp_Vec2d Row11(3*DS1_u*DS2_u + Ort*DS3_u,
214 tmp = 2*DS1_u*DS2_uv +
215 DS1_v*DS2_u + Ort*DS3_uuv);
217 // Row12 = (d2E1/dudv, d2E1/dv2)
218 gp_Vec2d Row12(tmp, DS2_v*DS1_u + 2*DS1_v*DS2_uv +
221 // Row21 = (d2E2/du2, d2E2/dudv)
222 gp_Vec2d Row21(DS2_u*DS1_v + 2*DS1_u*DS2_uv + Ort*DS3_uuv,
223 tmp = 2*DS2_uv*DS1_v + DS1_u*DS2_v + Ort*DS3_uvv);
225 // Row22 = (d2E2/duv, d2E2/dvdv)
226 gp_Vec2d Row22(tmp, 3*DS1_v*DS2_v + Ort*DS3_v);
228 gp_Vec2d S3(V1*gp_Vec2d(Row11*V1, Row12*V1),
229 V1*gp_Vec2d(Row21*V1, Row22*V1));
231 gp_Vec2d Sum = d2E_dt + S2 + S3;
233 V2 = - gp_Vec2d(gp_Vec2d(M.Row(1))*Sum, gp_Vec2d(M.Row(2))*Sum);
235 //=======================================================================
236 //function : d1CurveOnSurf
237 //purpose : computes first derivative of the 3d projected curve
238 //=======================================================================
241 static void d1CurvOnSurf(const Standard_Real t,
242 const Standard_Real u,
243 const Standard_Real v,
245 const Handle(Adaptor3d_HCurve)& Curve,
246 const Handle(Adaptor3d_HSurface)& Surface)
250 gp_Vec DS1_u, DS1_v, DS2_u, DS2_uv, DS2_v, DC1_t;
251 Surface->D2(u, v, S, DS1_u, DS1_v, DS2_u, DS2_v, DS2_uv);
252 Curve->D1(t, C, DC1_t);
253 gp_Vec Ort(C, S);// Ort = S - C
255 gp_Vec2d dE_dt(-DC1_t*DS1_u, -DC1_t*DS1_v);
256 gp_XY dE_du(DS1_u*DS1_u + Ort*DS2_u,
257 DS1_u*DS1_v + Ort*DS2_uv);
258 gp_XY dE_dv(DS1_v*DS1_u + Ort*DS2_uv,
259 DS1_v*DS1_v + Ort*DS2_v);
261 Standard_Real det = dE_du.X()*dE_dv.Y() - dE_du.Y()*dE_dv.X();
262 if (fabs(det) < gp::Resolution()) throw Standard_ConstructionError();
264 gp_Mat2d M(gp_XY(dE_dv.Y()/det, -dE_du.Y()/det),
265 gp_XY(-dE_dv.X()/det, dE_du.X()/det));
267 V2d = - gp_Vec2d(gp_Vec2d(M.Row(1))*dE_dt, gp_Vec2d(M.Row(2))*dE_dt);
269 V = DS1_u * V2d.X() + DS1_v * V2d.Y();
274 //=======================================================================
275 //function : d2CurveOnSurf
276 //purpose : computes second derivative of the 3D projected curve
277 //=======================================================================
279 static void d2CurvOnSurf(const Standard_Real t,
280 const Standard_Real u,
281 const Standard_Real v,
282 gp_Vec& V1 , gp_Vec& V2 ,
283 const Handle(Adaptor3d_HCurve)& Curve,
284 const Handle(Adaptor3d_HSurface)& Surface)
288 gp_Vec DS1_u, DS1_v, DS2_u, DS2_uv, DS2_v,
289 DS3_u, DS3_v, DS3_uuv, DS3_uvv,
291 Surface->D3(u, v, S, DS1_u, DS1_v, DS2_u, DS2_v, DS2_uv,
292 DS3_u, DS3_v, DS3_uuv, DS3_uvv);
293 Curve->D2(t, C, DC1_t, DC2_t);
296 gp_Vec2d dE_dt(-DC1_t*DS1_u, -DC1_t*DS1_v);
297 gp_XY dE_du(DS1_u*DS1_u + Ort*DS2_u,
298 DS1_u*DS1_v + Ort*DS2_uv);
299 gp_XY dE_dv(DS1_v*DS1_u + Ort*DS2_uv,
300 DS1_v*DS1_v + Ort*DS2_v);
302 Standard_Real det = dE_du.X()*dE_dv.Y() - dE_du.Y()*dE_dv.X();
303 if (fabs(det) < gp::Resolution()) throw Standard_ConstructionError();
305 gp_Mat2d M(gp_XY(dE_dv.Y()/det, -dE_du.Y()/det),
306 gp_XY(-dE_dv.X()/det, dE_du.X()/det));
309 V12d = - gp_Vec2d(gp_Vec2d(M.Row(1))*dE_dt, gp_Vec2d(M.Row(2))*dE_dt);
311 /* Second derivative */
313 // Computation of d2E_dt2 = S1
314 gp_Vec2d d2E_dt(-DC2_t*DS1_u, -DC2_t*DS1_v);
316 // Computation of 2*(d2E/dtdX)(dX/dt) = S2
317 gp_Vec2d d2E1_dtdX(-DC1_t*DS2_u,
319 gp_Vec2d d2E2_dtdX(-DC1_t*DS2_uv,
321 gp_Vec2d S2 = 2*gp_Vec2d(d2E1_dtdX*V12d, d2E2_dtdX*V12d);
323 // Computation of (d2E/dX2)*(dX/dt)2 = S3
325 // Row11 = (d2E1/du2, d2E1/dudv)
327 gp_Vec2d Row11(3*DS1_u*DS2_u + Ort*DS3_u,
328 tmp = 2*DS1_u*DS2_uv +
329 DS1_v*DS2_u + Ort*DS3_uuv);
331 // Row12 = (d2E1/dudv, d2E1/dv2)
332 gp_Vec2d Row12(tmp, DS2_v*DS1_u + 2*DS1_v*DS2_uv +
335 // Row21 = (d2E2/du2, d2E2/dudv)
336 gp_Vec2d Row21(DS2_u*DS1_v + 2*DS1_u*DS2_uv + Ort*DS3_uuv,
337 tmp = 2*DS2_uv*DS1_v + DS1_u*DS2_v + Ort*DS3_uvv);
339 // Row22 = (d2E2/duv, d2E2/dvdv)
340 gp_Vec2d Row22(tmp, 3*DS1_v*DS2_v + Ort*DS3_v);
342 gp_Vec2d S3(V12d*gp_Vec2d(Row11*V12d, Row12*V12d),
343 V12d*gp_Vec2d(Row21*V12d, Row22*V12d));
345 gp_Vec2d Sum = d2E_dt + S2 + S3;
347 V22d = - gp_Vec2d(gp_Vec2d(M.Row(1))*Sum, gp_Vec2d(M.Row(2))*Sum);
349 V1 = DS1_u * V12d.X() + DS1_v * V12d.Y();
350 V2 = DS2_u * V12d.X() *V12d.X()
352 + 2 * DS2_uv * V12d.X() *V12d.Y()
353 + DS2_v * V12d.Y() * V12d.Y()
357 //=======================================================================
358 //function : ExactBound
359 //purpose : computes exact boundary point
360 //=======================================================================
362 static Standard_Boolean ExactBound(gp_Pnt& Sol,
363 const Standard_Real NotSol,
364 const Standard_Real Tol,
365 const Standard_Real TolU,
366 const Standard_Real TolV,
367 const Handle(Adaptor3d_HCurve)& Curve,
368 const Handle(Adaptor3d_HSurface)& Surface)
370 Standard_Real U0, V0, t, t1, t2, FirstU, LastU, FirstV, LastV;
374 FirstU = Surface->FirstUParameter();
375 LastU = Surface->LastUParameter();
376 FirstV = Surface->FirstVParameter();
377 LastV = Surface->LastVParameter();
378 // Here we have to compute the boundary that projection is going to intersect
380 //these variables are to estimate which boundary has more apportunity
382 Standard_Real RU1, RU2, RV1, RV2;
383 d1(Sol.X(), U0, V0, D2d, Curve, Surface);
384 // Here we assume that D2d != (0, 0)
385 if(Abs(D2d.X()) < gp::Resolution())
387 RU1 = Precision::Infinite();
388 RU2 = Precision::Infinite();
392 else if(Abs(D2d.Y()) < gp::Resolution())
396 RV1 = Precision::Infinite();
397 RV2 = Precision::Infinite();
401 RU1 = gp_Pnt2d(U0, V0).
402 Distance(gp_Pnt2d(FirstU, V0 + (FirstU - U0)*D2d.Y()/D2d.X()));
403 RU2 = gp_Pnt2d(U0, V0).
404 Distance(gp_Pnt2d(LastU, V0 + (LastU - U0)*D2d.Y()/D2d.X()));
405 RV1 = gp_Pnt2d(U0, V0).
406 Distance(gp_Pnt2d(U0 + (FirstV - V0)*D2d.X()/D2d.Y(), FirstV));
407 RV2 = gp_Pnt2d(U0, V0).
408 Distance(gp_Pnt2d(U0 + (LastV - V0)*D2d.X()/D2d.Y(), LastV));
410 TColgp_SequenceOfPnt Seq;
411 Seq.Append(gp_Pnt(FirstU, RU1, 2));
412 Seq.Append(gp_Pnt(LastU, RU2, 2));
413 Seq.Append(gp_Pnt(FirstV, RV1, 3));
414 Seq.Append(gp_Pnt(LastV, RV2, 3));
415 Standard_Integer i, j;
416 for(i = 1; i <= 3; i++)
418 for(j = 1; j <= 4-i; j++)
420 if(Seq(j).Y() < Seq(j+1).Y())
423 swp = Seq.Value(j+1);
424 Seq.ChangeValue(j+1) = Seq.Value(j);
425 Seq.ChangeValue(j) = swp;
431 t1 = Min (Sol.X (), NotSol);
432 t2 = Max (Sol.X (), NotSol);
434 Standard_Boolean isDone = Standard_False;
435 while (!Seq.IsEmpty ())
439 Seq.Remove (Seq.Length ());
440 ProjLib_PrjResolve aPrjPS (Curve->Curve (),
442 Standard_Integer (P.Z ()));
443 if (Standard_Integer (P.Z ()) == 2)
445 aPrjPS.Perform (t, P.X (), V0, gp_Pnt2d (Tol, TolV),
446 gp_Pnt2d (t1, Surface->FirstVParameter ()),
447 gp_Pnt2d (t2, Surface->LastVParameter ()), FuncTol);
448 if (!aPrjPS.IsDone ()) continue;
449 POnS = aPrjPS.Solution ();
450 Sol = gp_Pnt (POnS.X (), P.X (), POnS.Y ());
451 isDone = Standard_True;
456 aPrjPS.Perform (t, U0, P.X (), gp_Pnt2d (Tol, TolU),
457 gp_Pnt2d (t1, Surface->FirstUParameter ()),
458 gp_Pnt2d (t2, Surface->LastUParameter ()), FuncTol);
459 if (!aPrjPS.IsDone ()) continue;
460 POnS = aPrjPS.Solution ();
461 Sol = gp_Pnt (POnS.X (), POnS.Y (), P.X ());
462 isDone = Standard_True;
470 //=======================================================================
471 //function : DichExactBound
472 //purpose : computes exact boundary point
473 //=======================================================================
475 static void DichExactBound(gp_Pnt& Sol,
476 const Standard_Real NotSol,
477 const Standard_Real Tol,
478 const Standard_Real TolU,
479 const Standard_Real TolV,
480 const Handle(Adaptor3d_HCurve)& Curve,
481 const Handle(Adaptor3d_HSurface)& Surface)
483 #ifdef OCCT_DEBUG_CHRONO
484 InitChron(chr_dicho_bound);
487 Standard_Real U0, V0, t;
491 ProjLib_PrjResolve aPrjPS(Curve->Curve(), Surface->Surface(), 1);
493 Standard_Real aNotSol = NotSol;
494 while (fabs(Sol.X() - aNotSol) > Tol)
496 t = (Sol.X() + aNotSol)/2;
497 aPrjPS.Perform(t, U0, V0, gp_Pnt2d(TolU, TolV),
498 gp_Pnt2d(Surface->FirstUParameter(),Surface->FirstVParameter()),
499 gp_Pnt2d(Surface->LastUParameter(),Surface->LastVParameter()),
500 FuncTol, Standard_True);
504 POnS = aPrjPS.Solution();
505 Sol = gp_Pnt(t, POnS.X(), POnS.Y());
511 #ifdef OCCT_DEBUG_CHRONO
512 ResultChron(chr_dicho_bound,t_dicho_bound);
517 //=======================================================================
518 //function : InitialPoint
520 //=======================================================================
522 static Standard_Boolean InitialPoint(const gp_Pnt& Point,
523 const Standard_Real t,
524 const Handle(Adaptor3d_HCurve)& C,
525 const Handle(Adaptor3d_HSurface)& S,
526 const Standard_Real TolU,
527 const Standard_Real TolV,
532 ProjLib_PrjResolve aPrjPS(C->Curve(), S->Surface(), 1);
533 Standard_Real ParU,ParV;
534 Extrema_ExtPS aExtPS;
535 aExtPS.Initialize(S->Surface(), S->FirstUParameter(),
536 S->LastUParameter(), S->FirstVParameter(),
537 S->LastVParameter(), TolU, TolV);
539 aExtPS.Perform(Point);
540 Standard_Integer argmin = 0;
541 if (aExtPS.IsDone() && aExtPS.NbExt())
543 Standard_Integer i, Nend;
544 // Search for the nearest solution which is also a normal projection
545 Nend = aExtPS.NbExt();
546 for(i = 1; i <= Nend; i++)
548 Extrema_POnSurf POnS = aExtPS.Point(i);
549 POnS.Parameter(ParU, ParV);
550 aPrjPS.Perform(t, ParU, ParV, gp_Pnt2d(TolU, TolV),
551 gp_Pnt2d(S->FirstUParameter(), S->FirstVParameter()),
552 gp_Pnt2d(S->LastUParameter(), S->LastVParameter()),
553 FuncTol, Standard_True);
555 if (argmin == 0 || aExtPS.SquareDistance(i) < aExtPS.SquareDistance(argmin)) argmin = i;
558 if( argmin == 0 ) return Standard_False;
561 Extrema_POnSurf POnS = aExtPS.Point(argmin);
562 POnS.Parameter(U, V);
563 return Standard_True;
567 //=======================================================================
568 //function : ProjLib_CompProjectedCurve
570 //=======================================================================
572 ProjLib_CompProjectedCurve::ProjLib_CompProjectedCurve()
580 //=======================================================================
581 //function : ProjLib_CompProjectedCurve
583 //=======================================================================
585 ProjLib_CompProjectedCurve::ProjLib_CompProjectedCurve
586 (const Handle(Adaptor3d_HSurface)& theSurface,
587 const Handle(Adaptor3d_HCurve)& theCurve,
588 const Standard_Real theTolU,
589 const Standard_Real theTolV)
590 : mySurface (theSurface),
593 mySequence(new ProjLib_HSequenceOfHSequenceOfPnt()),
601 //=======================================================================
602 //function : ProjLib_CompProjectedCurve
604 //=======================================================================
606 ProjLib_CompProjectedCurve::ProjLib_CompProjectedCurve
607 (const Handle(Adaptor3d_HSurface)& theSurface,
608 const Handle(Adaptor3d_HCurve)& theCurve,
609 const Standard_Real theTolU,
610 const Standard_Real theTolV,
611 const Standard_Real theMaxDist)
612 : mySurface (theSurface),
615 mySequence(new ProjLib_HSequenceOfHSequenceOfPnt()),
618 myMaxDist (theMaxDist)
623 //=======================================================================
626 //=======================================================================
628 void ProjLib_CompProjectedCurve::Init()
631 NCollection_Vector<Standard_Real> aSplits;
634 Standard_Real Tol;// Tolerance for ExactBound
635 Standard_Integer i, Nend = 0, aSplitIdx = 0;
636 Standard_Boolean FromLastU = Standard_False,
637 isSplitsComputed = Standard_False;
639 const Standard_Real aTol3D = Precision::Confusion();
640 Extrema_ExtCS CExt(myCurve->Curve(), mySurface->Surface(), aTol3D, aTol3D);
641 if (CExt.IsDone() && CExt.NbExt())
643 // Search for the minimum solution.
644 // Avoid usage of extrema result that can be wrong for extrusion.
647 mySurface->GetType() != GeomAbs_SurfaceOfExtrusion)
649 Standard_Real min_val2;
650 min_val2 = CExt.SquareDistance(1);
653 for(i = 2; i <= Nend; i++)
655 if (CExt.SquareDistance(i) < min_val2)
656 min_val2 = CExt.SquareDistance(i);
658 if (min_val2 > myMaxDist * myMaxDist)
659 return; // No near solution -> exit.
663 Standard_Real FirstU, LastU, Step, SearchStep, WalkStep, t;
665 FirstU = myCurve->FirstParameter();
666 LastU = myCurve->LastParameter();
667 const Standard_Real GlobalMinStep = 1.e-4;
668 //<GlobalMinStep> is sufficiently small to provide solving from initial point
669 //and, on the other hand, it is sufficiently large to avoid too close solutions.
670 const Standard_Real MinStep = 0.01*(LastU - FirstU),
671 MaxStep = 0.1*(LastU - FirstU);
672 SearchStep = 10*MinStep;
675 gp_Pnt2d aLowBorder(mySurface->FirstUParameter(),mySurface->FirstVParameter());
676 gp_Pnt2d aUppBorder(mySurface->LastUParameter(), mySurface->LastVParameter());
677 gp_Pnt2d aTol(myTolU, myTolV);
678 ProjLib_PrjResolve aPrjPS(myCurve->Curve(), mySurface->Surface(), 1);
681 Standard_Boolean new_part;
682 Standard_Real prevDeb=0.;
683 Standard_Boolean SameDeb=Standard_False;
686 gp_Pnt Triple, prevTriple;
691 // Search for the beginning of a new continuous part
692 // to avoid infinite computation in some difficult cases.
693 new_part = Standard_False;
694 if(t > FirstU && Abs(t-prevDeb) <= Precision::PConfusion()) SameDeb=Standard_True;
695 while(t <= LastU && !new_part && !FromLastU && !SameDeb)
698 if (t == LastU) FromLastU=Standard_True;
699 Standard_Boolean initpoint=Standard_False;
700 Standard_Real U = 0., V = 0.;
702 Standard_Real ParT,ParU,ParV;
704 // Search an initial point in the list of Extrema Curve-Surface
705 if(Nend != 0 && !CExt.IsParallel())
707 for (i=1;i<=Nend;i++)
711 CExt.Points(i,P1,P2);
713 P2.Parameter(ParU, ParV);
715 aPrjPS.Perform(ParT, ParU, ParV, aTol, aLowBorder, aUppBorder, FuncTol, Standard_True);
717 if ( aPrjPS.IsDone() && P1.Parameter() > Max(FirstU,t-Step+Precision::PConfusion())
718 && P1.Parameter() <= t)
724 initpoint = Standard_True;
731 myCurve->D0(t,CPoint);
732 #ifdef OCCT_DEBUG_CHRONO
733 InitChron(chr_init_point);
735 // PConfusion - use geometric tolerances in extrema / optimization.
736 initpoint=InitialPoint(CPoint, t,myCurve,mySurface, Precision::PConfusion(), Precision::PConfusion(), U, V);
737 #ifdef OCCT_DEBUG_CHRONO
738 ResultChron(chr_init_point,t_init_point);
744 // When U or V lie on surface joint in some cases we cannot use them
745 // as initial point for aPrjPS, so we switch them
748 if ((mySurface->IsUPeriodic() &&
749 Abs(aUppBorder.X() - aLowBorder.X() - mySurface->UPeriod()) < Precision::Confusion()) ||
750 (mySurface->IsVPeriodic() &&
751 Abs(aUppBorder.Y() - aLowBorder.Y() - mySurface->VPeriod()) < Precision::Confusion()))
753 if((Abs(U - aLowBorder.X()) < mySurface->UResolution(Precision::PConfusion())) &&
754 mySurface->IsUPeriodic())
756 d1(t, U, V, D, myCurve, mySurface);
757 if (D.X() < 0 ) U = aUppBorder.X();
759 else if((Abs(U - aUppBorder.X()) < mySurface->UResolution(Precision::PConfusion())) &&
760 mySurface->IsUPeriodic())
762 d1(t, U, V, D, myCurve, mySurface);
763 if (D.X() > 0) U = aLowBorder.X();
766 if((Abs(V - aLowBorder.Y()) < mySurface->VResolution(Precision::PConfusion())) &&
767 mySurface->IsVPeriodic())
769 d1(t, U, V, D, myCurve, mySurface);
770 if (D.Y() < 0) V = aUppBorder.Y();
772 else if((Abs(V - aUppBorder.Y()) <= mySurface->VResolution(Precision::PConfusion())) &&
773 mySurface->IsVPeriodic())
775 d1(t, U, V, D, myCurve, mySurface);
776 if (D.Y() > 0) V = aLowBorder.Y();
782 // Here we are going to stop if the distance between projection and
783 // corresponding curve point is greater than myMaxDist
786 mySurface->D0(U, V, POnS);
787 d = CPoint.Distance(POnS);
795 Triple = gp_Pnt(t, U, V);
798 //Search for exact boundary point
799 Tol = Min(myTolU, myTolV);
801 d1(Triple.X(), Triple.Y(), Triple.Z(), aD, myCurve, mySurface);
802 Tol /= Max(Abs(aD.X()), Abs(aD.Y()));
804 if(!ExactBound(Triple, t - Step, Tol,
805 myTolU, myTolV, myCurve, mySurface))
808 cout<<"There is a problem with ExactBound computation"<<endl;
810 DichExactBound(Triple, t - Step, Tol, myTolU, myTolV,
814 new_part = Standard_True;
818 if(t == LastU) break;
827 if (!new_part) break;
829 //We have found a new continuous part
830 Handle(TColgp_HSequenceOfPnt) hSeq = new TColgp_HSequenceOfPnt();
831 mySequence->Append(hSeq);
833 mySequence->Value(myNbCurves)->Append(Triple);
836 if (Triple.X() == LastU) break;//return;
838 //Computation of WalkStep
840 Standard_Real MagnD1, MagnD2;
841 d2CurvOnSurf(Triple.X(), Triple.Y(), Triple.Z(), D1, D2, myCurve, mySurface);
842 MagnD1 = D1.Magnitude();
843 MagnD2 = D2.Magnitude();
844 if(MagnD2 < Precision::Confusion()) WalkStep = MaxStep;
845 else WalkStep = Min(MaxStep, Max(MinStep, 0.1*MagnD1/MagnD2));
849 t = Triple.X() + Step;
850 if (t > LastU) t = LastU;
851 Standard_Real prevStep = Step;
852 Standard_Real U0, V0;
854 //Here we are trying to prolong continuous part
855 while (t <= LastU && new_part)
858 U0 = Triple.Y() + (Step / prevStep) * (Triple.Y() - prevTriple.Y());
859 V0 = Triple.Z() + (Step / prevStep) * (Triple.Z() - prevTriple.Z());
860 // adjust U0 to be in [mySurface->FirstUParameter(),mySurface->LastUParameter()]
861 U0 = Min(Max(U0, aLowBorder.X()), aUppBorder.X());
862 // adjust V0 to be in [mySurface->FirstVParameter(),mySurface->LastVParameter()]
863 V0 = Min(Max(V0, aLowBorder.Y()), aUppBorder.Y());
866 aPrjPS.Perform(t, U0, V0, aTol,
867 aLowBorder, aUppBorder, FuncTol, Standard_True);
870 if (Step <= GlobalMinStep)
872 //Search for exact boundary point
873 Tol = Min(myTolU, myTolV);
875 d1(Triple.X(), Triple.Y(), Triple.Z(), D, myCurve, mySurface);
876 Tol /= Max(Abs(D.X()), Abs(D.Y()));
878 if(!ExactBound(Triple, t, Tol, myTolU, myTolV,
882 cout<<"There is a problem with ExactBound computation"<<endl;
884 DichExactBound(Triple, t, Tol, myTolU, myTolV,
888 if((Triple.X() - mySequence->Value(myNbCurves)->Value(mySequence->Value(myNbCurves)->Length()).X()) > 1.e-10)
889 mySequence->Value(myNbCurves)->Append(Triple);
890 if((LastU - Triple.X()) < Tol) {t = LastU + 1; break;}//return;
893 t = Triple.X() + Step;
894 if (t > (LastU-MinStep/2) )
899 new_part = Standard_False;
904 Standard_Real SaveStep = Step;
906 t = Triple .X() + Step;
907 if (t > (LastU-MinStep/4) )
910 if (Abs(Step - SaveStep) <= Precision::PConfusion())
911 Step = GlobalMinStep; //to avoid looping
921 Triple = gp_Pnt(t, aPrjPS.Solution().X(), aPrjPS.Solution().Y());
923 // Check for possible local traps.
924 UpdateTripleByTrapCriteria(Triple);
926 // Protection from case when the whole curve lies on a seam.
927 if (!isSplitsComputed)
929 Standard_Boolean isUPossible = Standard_False;
930 if (mySurface->IsUPeriodic() &&
931 (Abs(Triple.Y() - mySurface->FirstUParameter() ) > Precision::PConfusion() &&
932 Abs(Triple.Y() - mySurface->LastUParameter() ) > Precision::PConfusion()))
934 isUPossible = Standard_True;
937 Standard_Boolean isVPossible = Standard_False;
938 if (mySurface->IsVPeriodic() &&
939 (Abs(Triple.Z() - mySurface->FirstVParameter() ) > Precision::PConfusion() &&
940 Abs(Triple.Z() - mySurface->LastVParameter() ) > Precision::PConfusion()))
942 isVPossible = Standard_True;
945 if (isUPossible || isVPossible)
947 // When point is good conditioned.
948 BuildCurveSplits(myCurve, mySurface, myTolU, myTolV, aSplits);
949 isSplitsComputed = Standard_True;
953 if((Triple.X() - mySequence->Value(myNbCurves)->Value(mySequence->Value(myNbCurves)->Length()).X()) > 1.e-10)
954 mySequence->Value(myNbCurves)->Append(Triple);
955 if (t == LastU) {t = LastU + 1; break;}//return;
956 //Computation of WalkStep
957 d2CurvOnSurf(Triple.X(), Triple.Y(), Triple.Z(), D1, D2, myCurve, mySurface);
958 MagnD1 = D1.Magnitude();
959 MagnD2 = D2.Magnitude();
960 if(MagnD2 < Precision::Confusion() ) WalkStep = MaxStep;
961 else WalkStep = Min(MaxStep, Max(MinStep, 0.1*MagnD1/MagnD2));
965 if (t > (LastU-MinStep/2))
967 Step = Step + LastU - t;
971 // We assume at least one point of cache inside of a split.
972 const Standard_Integer aSize = aSplits.Size();
973 for(Standard_Integer anIdx = aSplitIdx; anIdx < aSize; ++anIdx)
975 const Standard_Real aParam = aSplits(anIdx);
976 if (Abs(aParam - Triple.X() ) < Precision::PConfusion())
978 // The current point is equal to a split point.
979 new_part = Standard_False;
981 // Move split index to avoid check of the whole list.
985 else if (aParam < t + Precision::PConfusion() )
987 // The next point crosses the split point.
989 Step = t - prevTriple.X();
991 } // for(Standard_Integer anIdx = aSplitIdx; anIdx < aSize; ++anIdx)
996 // Sequence post-proceeding.
999 // 1. Removing poor parts
1000 Standard_Integer NbPart=myNbCurves;
1001 Standard_Integer ipart=1;
1002 for(i = 1; i <= NbPart; i++) {
1003 // Standard_Integer NbPoints = mySequence->Value(i)->Length();
1004 if(mySequence->Value(ipart)->Length() < 2) {
1005 mySequence->Remove(ipart);
1011 if(myNbCurves == 0) return;
1013 // 2. Removing common parts of bounds
1014 for(i = 1; i < myNbCurves; i++)
1016 if(mySequence->Value(i)->Value(mySequence->Value(i)->Length()).X() >=
1017 mySequence->Value(i+1)->Value(1).X())
1019 mySequence->ChangeValue(i+1)->ChangeValue(1).SetX(mySequence->Value(i)->Value(mySequence->Value(i)->Length()).X() + 1.e-12);
1023 // 3. Computation of the maximum distance from each part of curve to surface
1025 myMaxDistance = new TColStd_HArray1OfReal(1, myNbCurves);
1026 myMaxDistance->Init(0);
1027 for(i = 1; i <= myNbCurves; i++)
1029 for(j = 1; j <= mySequence->Value(i)->Length(); j++)
1031 gp_Pnt POnC, POnS, aTriple;
1032 Standard_Real Distance;
1033 aTriple = mySequence->Value(i)->Value(j);
1034 myCurve->D0(aTriple.X(), POnC);
1035 mySurface->D0(aTriple.Y(), aTriple.Z(), POnS);
1036 Distance = POnC.Distance(POnS);
1037 if (myMaxDistance->Value(i) < Distance)
1039 myMaxDistance->ChangeValue(i) = Distance;
1044 // 4. Check the projection to be a single point
1046 gp_Pnt2d Pmoy, Pcurr, P;
1047 Standard_Real AveU, AveV;
1048 mySnglPnts = new TColStd_HArray1OfBoolean(1, myNbCurves);
1049 mySnglPnts->Init (Standard_True);
1051 for(i = 1; i <= myNbCurves; i++)
1053 //compute an average U and V
1055 for(j = 1, AveU = 0., AveV = 0.; j <= mySequence->Value(i)->Length(); j++)
1057 AveU += mySequence->Value(i)->Value(j).Y();
1058 AveV += mySequence->Value(i)->Value(j).Z();
1060 AveU /= mySequence->Value(i)->Length();
1061 AveV /= mySequence->Value(i)->Length();
1063 Pmoy.SetCoord(AveU,AveV);
1064 for(j = 1; j <= mySequence->Value(i)->Length(); j++)
1067 gp_Pnt2d(mySequence->Value(i)->Value(j).Y(), mySequence->Value(i)->Value(j).Z());
1068 if (Pcurr.Distance(Pmoy) > ((myTolU < myTolV) ? myTolV : myTolU))
1070 mySnglPnts->SetValue(i, Standard_False);
1076 // 5. Check the projection to be an isoparametric curve of the surface
1078 myUIso = new TColStd_HArray1OfBoolean(1, myNbCurves);
1079 myUIso->Init (Standard_True);
1081 myVIso = new TColStd_HArray1OfBoolean(1, myNbCurves);
1082 myVIso->Init (Standard_True);
1084 for(i = 1; i <= myNbCurves; i++) {
1085 if (IsSinglePnt(i, P)|| mySequence->Value(i)->Length() <=2) {
1086 myUIso->SetValue(i, Standard_False);
1087 myVIso->SetValue(i, Standard_False);
1091 // new test for isoparametrics
1093 if ( mySequence->Value(i)->Length() > 2) {
1094 //compute an average U and V
1096 for(j = 1, AveU = 0., AveV = 0.; j <= mySequence->Value(i)->Length(); j++) {
1097 AveU += mySequence->Value(i)->Value(j).Y();
1098 AveV += mySequence->Value(i)->Value(j).Z();
1100 AveU /= mySequence->Value(i)->Length();
1101 AveV /= mySequence->Value(i)->Length();
1103 // is i-part U-isoparametric ?
1104 for(j = 1; j <= mySequence->Value(i)->Length(); j++)
1106 if(Abs(mySequence->Value(i)->Value(j).Y() - AveU) > myTolU)
1108 myUIso->SetValue(i, Standard_False);
1113 // is i-part V-isoparametric ?
1114 for(j = 1; j <= mySequence->Value(i)->Length(); j++)
1116 if(Abs(mySequence->Value(i)->Value(j).Z() - AveV) > myTolV)
1118 myVIso->SetValue(i, Standard_False);
1126 //=======================================================================
1129 //=======================================================================
1131 void ProjLib_CompProjectedCurve::Load(const Handle(Adaptor3d_HSurface)& S)
1136 //=======================================================================
1139 //=======================================================================
1141 void ProjLib_CompProjectedCurve::Load(const Handle(Adaptor3d_HCurve)& C)
1146 //=======================================================================
1147 //function : GetSurface
1149 //=======================================================================
1151 const Handle(Adaptor3d_HSurface)& ProjLib_CompProjectedCurve::GetSurface() const
1157 //=======================================================================
1158 //function : GetCurve
1160 //=======================================================================
1162 const Handle(Adaptor3d_HCurve)& ProjLib_CompProjectedCurve::GetCurve() const
1167 //=======================================================================
1168 //function : GetTolerance
1170 //=======================================================================
1172 void ProjLib_CompProjectedCurve::GetTolerance(Standard_Real& TolU,
1173 Standard_Real& TolV) const
1179 //=======================================================================
1180 //function : NbCurves
1182 //=======================================================================
1184 Standard_Integer ProjLib_CompProjectedCurve::NbCurves() const
1188 //=======================================================================
1191 //=======================================================================
1193 void ProjLib_CompProjectedCurve::Bounds(const Standard_Integer Index,
1194 Standard_Real& Udeb,
1195 Standard_Real& Ufin) const
1197 if(Index < 1 || Index > myNbCurves) throw Standard_NoSuchObject();
1198 Udeb = mySequence->Value(Index)->Value(1).X();
1199 Ufin = mySequence->Value(Index)->Value(mySequence->Value(Index)->Length()).X();
1201 //=======================================================================
1202 //function : IsSinglePnt
1204 //=======================================================================
1206 Standard_Boolean ProjLib_CompProjectedCurve::IsSinglePnt(const Standard_Integer Index, gp_Pnt2d& P) const
1208 if(Index < 1 || Index > myNbCurves) throw Standard_NoSuchObject();
1209 P = gp_Pnt2d(mySequence->Value(Index)->Value(1).Y(), mySequence->Value(Index)->Value(1).Z());
1210 return mySnglPnts->Value(Index);
1213 //=======================================================================
1216 //=======================================================================
1218 Standard_Boolean ProjLib_CompProjectedCurve::IsUIso(const Standard_Integer Index, Standard_Real& U) const
1220 if(Index < 1 || Index > myNbCurves) throw Standard_NoSuchObject();
1221 U = mySequence->Value(Index)->Value(1).Y();
1222 return myUIso->Value(Index);
1224 //=======================================================================
1227 //=======================================================================
1229 Standard_Boolean ProjLib_CompProjectedCurve::IsVIso(const Standard_Integer Index, Standard_Real& V) const
1231 if(Index < 1 || Index > myNbCurves) throw Standard_NoSuchObject();
1232 V = mySequence->Value(Index)->Value(1).Z();
1233 return myVIso->Value(Index);
1235 //=======================================================================
1238 //=======================================================================
1240 gp_Pnt2d ProjLib_CompProjectedCurve::Value(const Standard_Real t) const
1246 //=======================================================================
1249 //=======================================================================
1251 void ProjLib_CompProjectedCurve::D0(const Standard_Real U,gp_Pnt2d& P) const
1253 Standard_Integer i, j;
1254 Standard_Real Udeb, Ufin;
1255 Standard_Boolean found = Standard_False;
1257 for(i = 1; i <= myNbCurves; i++)
1259 Bounds(i, Udeb, Ufin);
1260 if (U >= Udeb && U <= Ufin)
1262 found = Standard_True;
1266 if (!found) throw Standard_DomainError("ProjLib_CompProjectedCurve::D0");
1268 Standard_Real U0, V0;
1270 Standard_Integer End = mySequence->Value(i)->Length();
1271 for(j = 1; j < End; j++)
1272 if ((U >= mySequence->Value(i)->Value(j).X()) && (U <= mySequence->Value(i)->Value(j + 1).X())) break;
1274 // U0 = mySequence->Value(i)->Value(j).Y();
1275 // V0 = mySequence->Value(i)->Value(j).Z();
1277 // Cubic Interpolation
1278 if(mySequence->Value(i)->Length() < 4 ||
1279 (Abs(U-mySequence->Value(i)->Value(j).X()) <= Precision::PConfusion()) )
1281 U0 = mySequence->Value(i)->Value(j).Y();
1282 V0 = mySequence->Value(i)->Value(j).Z();
1284 else if (Abs(U-mySequence->Value(i)->Value(j+1).X())
1285 <= Precision::PConfusion())
1287 U0 = mySequence->Value(i)->Value(j+1).Y();
1288 V0 = mySequence->Value(i)->Value(j+1).Z();
1293 if (j > mySequence->Value(i)->Length() - 2)
1294 j = mySequence->Value(i)->Length() - 2;
1296 gp_Vec2d I1, I2, I3, I21, I22, I31, Y1, Y2, Y3, Y4, Res;
1297 Standard_Real X1, X2, X3, X4;
1299 X1 = mySequence->Value(i)->Value(j - 1).X();
1300 X2 = mySequence->Value(i)->Value(j).X();
1301 X3 = mySequence->Value(i)->Value(j + 1).X();
1302 X4 = mySequence->Value(i)->Value(j + 2).X();
1304 Y1 = gp_Vec2d(mySequence->Value(i)->Value(j - 1).Y(),
1305 mySequence->Value(i)->Value(j - 1).Z());
1306 Y2 = gp_Vec2d(mySequence->Value(i)->Value(j).Y(),
1307 mySequence->Value(i)->Value(j).Z());
1308 Y3 = gp_Vec2d(mySequence->Value(i)->Value(j + 1).Y(),
1309 mySequence->Value(i)->Value(j + 1).Z());
1310 Y4 = gp_Vec2d(mySequence->Value(i)->Value(j + 2).Y(),
1311 mySequence->Value(i)->Value(j + 2).Z());
1313 I1 = (Y1 - Y2)/(X1 - X2);
1314 I2 = (Y2 - Y3)/(X2 - X3);
1315 I3 = (Y3 - Y4)/(X3 - X4);
1317 I21 = (I1 - I2)/(X1 - X3);
1318 I22 = (I2 - I3)/(X2 - X4);
1320 I31 = (I21 - I22)/(X1 - X4);
1322 Res = Y1 + (U - X1)*(I1 + (U - X2)*(I21 + (U - X3)*I31));
1327 if(U0 < mySurface->FirstUParameter()) U0 = mySurface->FirstUParameter();
1328 else if(U0 > mySurface->LastUParameter()) U0 = mySurface->LastUParameter();
1330 if(V0 < mySurface->FirstVParameter()) V0 = mySurface->FirstVParameter();
1331 else if(V0 > mySurface->LastVParameter()) V0 = mySurface->LastVParameter();
1333 //End of cubic interpolation
1335 ProjLib_PrjResolve aPrjPS(myCurve->Curve(), mySurface->Surface(), 1);
1336 aPrjPS.Perform(U, U0, V0, gp_Pnt2d(myTolU, myTolV),
1337 gp_Pnt2d(mySurface->FirstUParameter(), mySurface->FirstVParameter()),
1338 gp_Pnt2d(mySurface->LastUParameter(), mySurface->LastVParameter()));
1339 if (aPrjPS.IsDone())
1340 P = aPrjPS.Solution();
1343 gp_Pnt thePoint = myCurve->Value(U);
1344 Extrema_ExtPS aExtPS(thePoint, mySurface->Surface(), myTolU, myTolV);
1345 if (aExtPS.IsDone() && aExtPS.NbExt())
1347 Standard_Integer k, Nend, imin = 1;
1348 // Search for the nearest solution which is also a normal projection
1349 Nend = aExtPS.NbExt();
1350 for(k = 2; k <= Nend; k++)
1351 if (aExtPS.SquareDistance(k) < aExtPS.SquareDistance(imin))
1353 const Extrema_POnSurf& POnS = aExtPS.Point(imin);
1354 Standard_Real ParU,ParV;
1355 POnS.Parameter(ParU, ParV);
1356 P.SetCoord(ParU, ParV);
1362 //=======================================================================
1365 //=======================================================================
1367 void ProjLib_CompProjectedCurve::D1(const Standard_Real t,
1375 d1(t, u, v, V, myCurve, mySurface);
1377 //=======================================================================
1380 //=======================================================================
1382 void ProjLib_CompProjectedCurve::D2(const Standard_Real t,
1391 d2(t, u, v, V1, V2, myCurve, mySurface);
1393 //=======================================================================
1396 //=======================================================================
1398 gp_Vec2d ProjLib_CompProjectedCurve::DN(const Standard_Real t,
1399 const Standard_Integer N) const
1401 if (N < 1 ) throw Standard_OutOfRange("ProjLib_CompProjectedCurve : N must be greater than 0");
1417 throw Standard_NotImplemented("ProjLib_CompProjectedCurve::DN");
1421 //=======================================================================
1422 //function : GetSequence
1424 //=======================================================================
1426 const Handle(ProjLib_HSequenceOfHSequenceOfPnt)& ProjLib_CompProjectedCurve::GetSequence() const
1430 //=======================================================================
1431 //function : FirstParameter
1433 //=======================================================================
1435 Standard_Real ProjLib_CompProjectedCurve::FirstParameter() const
1437 return myCurve->FirstParameter();
1440 //=======================================================================
1441 //function : LastParameter
1443 //=======================================================================
1445 Standard_Real ProjLib_CompProjectedCurve::LastParameter() const
1447 return myCurve->LastParameter();
1450 //=======================================================================
1451 //function : MaxDistance
1453 //=======================================================================
1455 Standard_Real ProjLib_CompProjectedCurve::MaxDistance(const Standard_Integer Index) const
1457 if(Index < 1 || Index > myNbCurves) throw Standard_NoSuchObject();
1458 return myMaxDistance->Value(Index);
1461 //=======================================================================
1462 //function : NbIntervals
1464 //=======================================================================
1466 Standard_Integer ProjLib_CompProjectedCurve::NbIntervals(const GeomAbs_Shape S) const
1468 const_cast<ProjLib_CompProjectedCurve*>(this)->myTabInt.Nullify();
1470 return myTabInt->Length() - 1;
1473 //=======================================================================
1474 //function : Intervals
1476 //=======================================================================
1478 void ProjLib_CompProjectedCurve::Intervals(TColStd_Array1OfReal& T,const GeomAbs_Shape S) const
1480 if (myTabInt.IsNull()) BuildIntervals (S);
1481 T = myTabInt->Array1();
1484 //=======================================================================
1485 //function : BuildIntervals
1487 //=======================================================================
1489 void ProjLib_CompProjectedCurve::BuildIntervals(const GeomAbs_Shape S) const
1491 GeomAbs_Shape SforS = GeomAbs_CN;
1509 throw Standard_OutOfRange();
1511 Standard_Integer i, j, k;
1512 Standard_Integer NbIntCur = myCurve->NbIntervals(S);
1513 Standard_Integer NbIntSurU = mySurface->NbUIntervals(SforS);
1514 Standard_Integer NbIntSurV = mySurface->NbVIntervals(SforS);
1516 TColStd_Array1OfReal CutPntsT(1, NbIntCur+1);
1517 TColStd_Array1OfReal CutPntsU(1, NbIntSurU+1);
1518 TColStd_Array1OfReal CutPntsV(1, NbIntSurV+1);
1520 myCurve->Intervals(CutPntsT, S);
1521 mySurface->UIntervals(CutPntsU, SforS);
1522 mySurface->VIntervals(CutPntsV, SforS);
1524 Standard_Real Tl, Tr, Ul, Ur, Vl, Vr, Tol;
1526 Handle(TColStd_HArray1OfReal) BArr = NULL,
1531 // proccessing projection bounds
1532 BArr = new TColStd_HArray1OfReal(1, 2*myNbCurves);
1533 for(i = 1; i <= myNbCurves; i++)
1535 Bounds(i, BArr->ChangeValue(2*i - 1), BArr->ChangeValue(2*i));
1538 // proccessing curve discontinuities
1540 CArr = new TColStd_HArray1OfReal(1, NbIntCur - 1);
1541 for(i = 1; i <= CArr->Length(); i++)
1543 CArr->ChangeValue(i) = CutPntsT(i + 1);
1547 // proccessing U-surface discontinuities
1548 TColStd_SequenceOfReal TUdisc;
1550 for(k = 2; k <= NbIntSurU; k++) {
1551 // cout<<"CutPntsU("<<k<<") = "<<CutPntsU(k)<<endl;
1552 for(i = 1; i <= myNbCurves; i++)
1554 for(j = 1; j < mySequence->Value(i)->Length(); j++)
1556 Ul = mySequence->Value(i)->Value(j).Y();
1557 Ur = mySequence->Value(i)->Value(j + 1).Y();
1559 if(Abs(Ul - CutPntsU(k)) <= myTolU)
1560 TUdisc.Append(mySequence->Value(i)->Value(j).X());
1561 else if(Abs(Ur - CutPntsU(k)) <= myTolU)
1562 TUdisc.Append(mySequence->Value(i)->Value(j + 1).X());
1563 else if((Ul < CutPntsU(k) && CutPntsU(k) < Ur) ||
1564 (Ur < CutPntsU(k) && CutPntsU(k) < Ul))
1567 V = (mySequence->Value(i)->Value(j).Z()
1568 + mySequence->Value(i)->Value(j +1).Z())/2;
1569 ProjLib_PrjResolve Solver(myCurve->Curve(), mySurface->Surface(), 2);
1573 Triple = mySequence->Value(i)->Value(j);
1574 d1(Triple.X(), Triple.Y(), Triple.Z(), D, myCurve, mySurface);
1575 if (Abs(D.X()) < Precision::Confusion())
1578 Tol = Min(myTolU, myTolU / Abs(D.X()));
1580 Tl = mySequence->Value(i)->Value(j).X();
1581 Tr = mySequence->Value(i)->Value(j + 1).X();
1583 Solver.Perform((Tl + Tr)/2, CutPntsU(k), V,
1584 gp_Pnt2d(Tol, myTolV),
1585 gp_Pnt2d(Tl, mySurface->FirstVParameter()),
1586 gp_Pnt2d(Tr, mySurface->LastVParameter()));
1590 TUdisc.Append(Solver.Solution().X());
1596 for(i = 2; i <= TUdisc.Length(); i++)
1598 if(TUdisc(i) - TUdisc(i-1) < Precision::PConfusion())
1606 UArr = new TColStd_HArray1OfReal(1, TUdisc.Length());
1607 for(i = 1; i <= UArr->Length(); i++)
1609 UArr->ChangeValue(i) = TUdisc(i);
1612 // proccessing V-surface discontinuities
1613 TColStd_SequenceOfReal TVdisc;
1615 for(k = 2; k <= NbIntSurV; k++)
1617 for(i = 1; i <= myNbCurves; i++)
1619 // cout<<"CutPntsV("<<k<<") = "<<CutPntsV(k)<<endl;
1620 for(j = 1; j < mySequence->Value(i)->Length(); j++) {
1622 Vl = mySequence->Value(i)->Value(j).Z();
1623 Vr = mySequence->Value(i)->Value(j + 1).Z();
1625 if(Abs(Vl - CutPntsV(k)) <= myTolV)
1626 TVdisc.Append(mySequence->Value(i)->Value(j).X());
1627 else if (Abs(Vr - CutPntsV(k)) <= myTolV)
1628 TVdisc.Append(mySequence->Value(i)->Value(j + 1).X());
1629 else if((Vl < CutPntsV(k) && CutPntsV(k) < Vr) ||
1630 (Vr < CutPntsV(k) && CutPntsV(k) < Vl))
1633 U = (mySequence->Value(i)->Value(j).Y()
1634 + mySequence->Value(i)->Value(j +1).Y())/2;
1635 ProjLib_PrjResolve Solver(myCurve->Curve(), mySurface->Surface(), 3);
1639 Triple = mySequence->Value(i)->Value(j);
1640 d1(Triple.X(), Triple.Y(), Triple.Z(), D, myCurve, mySurface);
1641 if (Abs(D.Y()) < Precision::Confusion())
1644 Tol = Min(myTolV, myTolV / Abs(D.Y()));
1646 Tl = mySequence->Value(i)->Value(j).X();
1647 Tr = mySequence->Value(i)->Value(j + 1).X();
1649 Solver.Perform((Tl + Tr)/2, U, CutPntsV(k),
1650 gp_Pnt2d(Tol, myTolV),
1651 gp_Pnt2d(Tl, mySurface->FirstUParameter()),
1652 gp_Pnt2d(Tr, mySurface->LastUParameter()));
1656 TVdisc.Append(Solver.Solution().X());
1663 for(i = 2; i <= TVdisc.Length(); i++)
1665 if(TVdisc(i) - TVdisc(i-1) < Precision::PConfusion())
1673 VArr = new TColStd_HArray1OfReal(1, TVdisc.Length());
1674 for(i = 1; i <= VArr->Length(); i++)
1676 VArr->ChangeValue(i) = TVdisc(i);
1681 TColStd_SequenceOfReal Fusion;
1684 GeomLib::FuseIntervals(BArr->ChangeArray1(),
1685 CArr->ChangeArray1(),
1686 Fusion, Precision::PConfusion());
1687 BArr = new TColStd_HArray1OfReal(1, Fusion.Length());
1688 for(i = 1; i <= BArr->Length(); i++)
1690 BArr->ChangeValue(i) = Fusion(i);
1697 GeomLib::FuseIntervals(BArr->ChangeArray1(),
1698 UArr->ChangeArray1(),
1699 Fusion, Precision::PConfusion());
1700 BArr = new TColStd_HArray1OfReal(1, Fusion.Length());
1701 for(i = 1; i <= BArr->Length(); i++)
1703 BArr->ChangeValue(i) = Fusion(i);
1710 GeomLib::FuseIntervals(BArr->ChangeArray1(),
1711 VArr->ChangeArray1(),
1712 Fusion, Precision::PConfusion());
1713 BArr = new TColStd_HArray1OfReal(1, Fusion.Length());
1714 for(i = 1; i <= BArr->Length(); i++)
1716 BArr->ChangeValue(i) = Fusion(i);
1720 const_cast<ProjLib_CompProjectedCurve*>(this)->myTabInt = new TColStd_HArray1OfReal(1, BArr->Length());
1721 for(i = 1; i <= BArr->Length(); i++)
1723 myTabInt->ChangeValue(i) = BArr->Value(i);
1727 //=======================================================================
1730 //=======================================================================
1732 Handle(Adaptor2d_HCurve2d) ProjLib_CompProjectedCurve::Trim
1733 (const Standard_Real First,
1734 const Standard_Real Last,
1735 const Standard_Real Tol) const
1737 Handle(ProjLib_HCompProjectedCurve) HCS =
1738 new ProjLib_HCompProjectedCurve(*this);
1739 HCS->ChangeCurve2d().Load(mySurface);
1740 HCS->ChangeCurve2d().Load(myCurve->Trim(First,Last,Tol));
1744 //=======================================================================
1745 //function : GetType
1747 //=======================================================================
1749 GeomAbs_CurveType ProjLib_CompProjectedCurve::GetType() const
1751 return GeomAbs_OtherCurve;
1754 //=======================================================================
1755 //function : UpdateTripleByTrapCriteria
1757 //=======================================================================
1758 void ProjLib_CompProjectedCurve::UpdateTripleByTrapCriteria(gp_Pnt &thePoint) const
1760 Standard_Boolean isProblemsPossible = Standard_False;
1761 // Check possible traps cases:
1764 if (mySurface->GetType() == GeomAbs_SurfaceOfRevolution)
1766 // Compute maximal deviation from 3D and choose the biggest one.
1767 Standard_Real aVRes = mySurface->VResolution(Precision::Confusion());
1768 Standard_Real aMaxTol = Max(Precision::PConfusion(), aVRes);
1770 if (Abs (thePoint.Z() - mySurface->FirstVParameter()) < aMaxTol ||
1771 Abs (thePoint.Z() - mySurface->LastVParameter() ) < aMaxTol )
1773 isProblemsPossible = Standard_True;
1777 // 27135 bug. Trap on degenerated edge.
1778 if (mySurface->GetType() == GeomAbs_Sphere &&
1779 (Abs (thePoint.Z() - mySurface->FirstVParameter()) < Precision::PConfusion() ||
1780 Abs (thePoint.Z() - mySurface->LastVParameter() ) < Precision::PConfusion() ||
1781 Abs (thePoint.Y() - mySurface->FirstUParameter()) < Precision::PConfusion() ||
1782 Abs (thePoint.Y() - mySurface->LastUParameter() ) < Precision::PConfusion() ))
1784 isProblemsPossible = Standard_True;
1787 if (!isProblemsPossible)
1791 Standard_Boolean isDone =
1792 InitialPoint(myCurve->Value(thePoint.X()), thePoint.X(), myCurve, mySurface,
1793 Precision::PConfusion(), Precision::PConfusion(), U, V);
1798 // Restore original position in case of period jump.
1799 if (mySurface->IsUPeriodic() &&
1800 Abs (Abs(U - thePoint.Y()) - mySurface->UPeriod()) < Precision::PConfusion())
1804 if (mySurface->IsVPeriodic() &&
1805 Abs (Abs(V - thePoint.Z()) - mySurface->VPeriod()) < Precision::PConfusion())
1813 //=======================================================================
1814 //function : BuildCurveSplits
1816 //=======================================================================
1817 void BuildCurveSplits(const Handle(Adaptor3d_HCurve) &theCurve,
1818 const Handle(Adaptor3d_HSurface) &theSurface,
1819 const Standard_Real theTolU,
1820 const Standard_Real theTolV,
1821 NCollection_Vector<Standard_Real> &theSplits)
1823 SplitDS aDS(theCurve, theSurface, theSplits);
1825 Extrema_ExtPS anExtPS;
1826 anExtPS.Initialize(theSurface->Surface(),
1827 theSurface->FirstUParameter(), theSurface->LastUParameter(),
1828 theSurface->FirstVParameter(), theSurface->LastVParameter(),
1830 aDS.myExtPS = &anExtPS;
1832 if (theSurface->IsUPeriodic())
1834 aDS.myPeriodicDir = 0;
1835 SplitOnDirection(aDS);
1837 if (theSurface->IsVPeriodic())
1839 aDS.myPeriodicDir = 1;
1840 SplitOnDirection(aDS);
1843 std::sort(aDS.mySplits.begin(), aDS.mySplits.end(), Comparator);
1846 //=======================================================================
1847 //function : SplitOnDirection
1848 //purpose : This method compute points in the parameter space of the curve
1849 // on which curve should be split since period jump is happen.
1850 //=======================================================================
1851 void SplitOnDirection(SplitDS & theSplitDS)
1854 // Create 3D curve which is correspond to the periodic bound in 2d space.
1855 // Run curve / curve extrema and run extrema point / surface to check that
1856 // the point will be projected to the periodic bound.
1857 // In this method assumed that the points cannot be closer to each other that 1% of the parameter space.
1859 gp_Pnt2d aStartPnt(theSplitDS.mySurface->FirstUParameter(), theSplitDS.mySurface->FirstVParameter());
1860 gp_Dir2d aDir(theSplitDS.myPeriodicDir, (Standard_Integer)!theSplitDS.myPeriodicDir);
1862 theSplitDS.myPerMinParam = !theSplitDS.myPeriodicDir ? theSplitDS.mySurface->FirstUParameter():
1863 theSplitDS.mySurface->FirstVParameter();
1864 theSplitDS.myPerMaxParam = !theSplitDS.myPeriodicDir ? theSplitDS.mySurface->LastUParameter():
1865 theSplitDS.mySurface->LastVParameter();
1866 Standard_Real aLast2DParam = theSplitDS.myPeriodicDir ?
1867 theSplitDS.mySurface->LastUParameter() - theSplitDS.mySurface->FirstUParameter():
1868 theSplitDS.mySurface->LastVParameter() - theSplitDS.mySurface->FirstVParameter();
1870 // Create line which is represent periodic border.
1871 Handle(Geom2d_Curve) aC2GC = new Geom2d_Line(aStartPnt, aDir);
1872 Handle(Geom2dAdaptor_HCurve) aC = new Geom2dAdaptor_HCurve(aC2GC, 0, aLast2DParam);
1873 Adaptor3d_CurveOnSurface aCOnS(aC, theSplitDS.mySurface);
1875 Extrema_ExtCC anExtCC;
1876 anExtCC.SetCurve(1, aCOnS);
1877 anExtCC.SetCurve(2, theSplitDS.myCurve->Curve());
1878 anExtCC.SetSingleSolutionFlag(Standard_True); // Search only one solution since multiple invocations are needed.
1879 anExtCC.SetRange(1, 0, aLast2DParam);
1880 theSplitDS.myExtCC = &anExtCC;
1882 FindSplitPoint(theSplitDS,
1883 theSplitDS.myCurve->FirstParameter(), // Initial curve range.
1884 theSplitDS.myCurve->LastParameter());
1888 //=======================================================================
1889 //function : FindSplitPoint
1891 //=======================================================================
1892 void FindSplitPoint(SplitDS &theSplitDS,
1893 const Standard_Real theMinParam,
1894 const Standard_Real theMaxParam)
1896 // Make extrema copy to avoid dependencies between different levels of the recursion.
1897 Extrema_ExtCC anExtCC(*theSplitDS.myExtCC);
1898 anExtCC.SetRange(2, theMinParam, theMaxParam);
1901 if (anExtCC.IsDone())
1903 const Standard_Integer aNbExt = anExtCC.NbExt();
1904 for (Standard_Integer anIdx = 1; anIdx <= aNbExt; ++anIdx)
1906 Extrema_POnCurv aPOnC1, aPOnC2;
1907 anExtCC.Points(anIdx, aPOnC1, aPOnC2);
1909 theSplitDS.myExtPS->Perform(aPOnC2.Value());
1910 if (!theSplitDS.myExtPS->IsDone())
1913 // Find point with the minimal Euclidean distance to avoid
1914 // false positive points detection.
1915 Standard_Integer aMinIdx = -1;
1916 Standard_Real aMinSqDist = RealLast();
1917 const Standard_Integer aNbPext = theSplitDS.myExtPS->NbExt();
1918 for(Standard_Integer aPIdx = 1; aPIdx <= aNbPext; ++aPIdx)
1920 const Standard_Real aCurrSqDist = theSplitDS.myExtPS->SquareDistance(aPIdx);
1922 if (aCurrSqDist < aMinSqDist)
1924 aMinSqDist = aCurrSqDist;
1929 // Check that is point will be projected to the periodic border.
1930 const Extrema_POnSurf &aPOnS = theSplitDS.myExtPS->Point(aMinIdx);
1931 Standard_Real U, V, aProjParam;
1932 aPOnS.Parameter(U, V);
1933 aProjParam = theSplitDS.myPeriodicDir ? V : U;
1936 if (Abs(aProjParam - theSplitDS.myPerMinParam) < Precision::PConfusion() ||
1937 Abs(aProjParam - theSplitDS.myPerMaxParam) < Precision::PConfusion() )
1939 const Standard_Real aParam = aPOnC2.Parameter();
1940 const Standard_Real aCFParam = theSplitDS.myCurve->FirstParameter();
1941 const Standard_Real aCLParam = theSplitDS.myCurve->LastParameter();
1943 if (aParam > aCFParam + Precision::PConfusion() &&
1944 aParam < aCLParam - Precision::PConfusion() )
1946 // Add only inner points.
1947 theSplitDS.mySplits.Append(aParam);
1950 const Standard_Real aDeltaCoeff = 0.01;
1951 const Standard_Real aDelta = (theMaxParam - theMinParam +
1952 aCLParam - aCFParam) * aDeltaCoeff;
1954 if (aParam - aDelta > theMinParam + Precision::PConfusion())
1956 FindSplitPoint(theSplitDS,
1957 theMinParam, aParam - aDelta); // Curve parameters.
1960 if (aParam + aDelta < theMaxParam - Precision::PConfusion())
1962 FindSplitPoint(theSplitDS,
1963 aParam + aDelta, theMaxParam); // Curve parameters.
1966 } // for (Standard_Integer anIdx = 1; anIdx <= aNbExt; ++anIdx)