1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <ApproxInt_KnotTools.hxx>
15 #include <TColgp_Array1OfPnt2d.hxx>
16 #include <TColStd_Array1OfReal.hxx>
17 #include <TColStd_HArray1OfReal.hxx>
18 #include <TColStd_HArray1OfInteger.hxx>
19 #include <math_Vector.hxx>
20 #include <Geom_BSplineCurve.hxx>
21 #include <Geom2d_BSplineCurve.hxx>
22 #include <GeomInt_TheMultiLineOfWLApprox.hxx>
23 #include <NCollection_Sequence.hxx>
24 #include <NCollection_List.hxx>
26 #include <Precision.hxx>
27 #include <NCollection_Vector.hxx>
28 #include <TColgp_Array1OfPnt.hxx>
30 // (Sqrt(5.0) - 1.0) / 4.0
31 //static const Standard_Real aSinCoeff = 0.30901699437494742410229341718282;
32 static const Standard_Real aSinCoeff2 = 0.09549150281252627; // aSinCoeff^2 = (3. - Sqrt(5.)) / 8.
33 static const Standard_Integer aMaxPntCoeff = 15;
35 //=======================================================================
37 //purpose : Evaluate curvature in dim-dimension point.
38 //=======================================================================
39 static Standard_Real EvalCurv(const Standard_Real dim,
40 const Standard_Real* V1,
41 const Standard_Real* V2)
43 // Really V1 and V2 are arrays of size dim;
44 // V1 is first derivative, V2 is second derivative
45 // of n-dimension curve
46 // Curvature is curv = |V1^V2|/|V1|^3
47 // V1^V2 is outer product of two vectors:
48 // P(i,j) = V1(i)*V2(j) - V1(j)*V2(i);
49 Standard_Real mp = 0.;
50 Standard_Integer i, j;
52 for(i = 1; i < dim; ++i)
54 for(j = 0; j < i; ++j)
56 p = V1[i]*V2[j] - V1[j]*V2[i];
62 for(i = 0; i < dim; ++i)
67 if (q < 1 / Precision::Infinite())
69 // Indeed, if q is small then we can
70 // obtain equivocation of "0/0" type.
71 // In this case, local curvature can be
72 // not equal to 0 or Infinity.
73 // However, it is good solution to insert
74 // knot in the place with such singularity.
75 // Therefore, we need imitation of curvature
76 // jumping. Return of Precision::Infinite() is
79 return Precision::Infinite();
82 q = Min(q, Precision::Infinite());
86 Standard_Real curv = Sqrt(mp / q);
91 //=======================================================================
92 //function : ComputeKnotInds
94 //=======================================================================
95 void ApproxInt_KnotTools::ComputeKnotInds(const NCollection_LocalArray<Standard_Real>& theCoords,
96 const Standard_Integer theDim,
97 const math_Vector& thePars,
98 NCollection_Sequence<Standard_Integer>& theInds)
100 //I: Create discrete curvature.
101 NCollection_Sequence<Standard_Integer> aFeatureInds;
102 TColStd_Array1OfReal aCurv(thePars.Lower(), thePars.Upper());
103 // Arrays are allocated for max theDim = 7: 1 3d curve + 2 2d curves.
104 Standard_Real Val[21], Par[3], Res[21];
105 Standard_Integer i, j, m, ic;
106 Standard_Real aMaxCurv = 0.;
107 Standard_Integer dim = theDim;
110 for(j = 0; j < 3; ++j)
112 Standard_Integer k = i+j;
113 ic = (k - aCurv.Lower()) * dim;
114 Standard_Integer l = dim*j;
115 for(m = 0; m < dim; ++m)
117 Val[l + m] = theCoords[ic + m];
121 PLib::EvalLagrange(Par[0], 2, 2, dim, *Val, *Par, *Res);
123 aCurv(i) = EvalCurv(dim, &Res[dim], &Res[2*dim]);
125 if(aCurv(i) > aMaxCurv)
130 for(i = aCurv.Lower()+1; i < aCurv.Upper(); ++i)
132 for(j = 0; j < 3; ++j)
134 Standard_Integer k = i+j-1;
135 ic = (k - aCurv.Lower()) * dim;
136 Standard_Integer l = dim*j;
137 for(m = 0; m < dim; ++m)
139 Val[l + m] = theCoords[ic + m];
143 PLib::EvalLagrange(Par[1], 2, 2, dim, *Val, *Par, *Res);
145 aCurv(i) = EvalCurv(dim, &Res[dim], &Res[2*dim]);
146 if(aCurv(i) > aMaxCurv)
153 for(j = 0; j < 3; ++j)
155 Standard_Integer k = i+j-2;
156 ic = (k - aCurv.Lower()) * dim;
157 Standard_Integer l = dim*j;
158 for(m = 0; m < dim; ++m)
160 Val[l + m] = theCoords[ic + m];
164 PLib::EvalLagrange(Par[2], 2, 2, dim, *Val, *Par, *Res);
166 aCurv(i) = EvalCurv(dim, &Res[dim], &Res[2*dim]);
167 if(aCurv(i) > aMaxCurv)
172 #ifdef APPROXINT_KNOTTOOLS_DEBUG
173 cout << "Discrete curvature array is" << endl;
174 for(i = aCurv.Lower(); i <= aCurv.Upper(); ++i)
176 cout << i << " " << aCurv(i) << endl;
180 theInds.Append(aCurv.Lower());
181 if(aMaxCurv <= Precision::Confusion())
184 theInds.Append(aCurv.Upper());
188 // II: Find extremas of curvature.
189 // Not used Precision::PConfusion, by different from "param space" eps nature.
190 Standard_Real eps = 1.0e-9,
192 for(i = aCurv.Lower() + 1; i < aCurv.Upper(); ++i)
194 Standard_Real d1 = aCurv(i) - aCurv(i - 1),
195 d2 = aCurv(i) - aCurv(i + 1),
196 ad1 = Abs(d1), ad2 = Abs(d2);
198 if(d1*d2 > 0. && ad1 > eps && ad2 > eps)
200 if(i != theInds.Last())
203 aFeatureInds.Append(i);
206 else if((ad1 < eps && ad2 > eps1) || (ad1 > eps1 && ad2 < eps))
208 if(i != theInds.Last())
211 aFeatureInds.Append(i);
215 if(aCurv.Upper() != theInds.Last())
217 theInds.Append(aCurv.Upper());
220 #if defined(APPROXINT_KNOTTOOLS_DEBUG)
222 cout << "Feature indices new: " << endl;
224 for(i = theInds.Lower(); i <= theInds.Upper(); ++i)
226 cout << i << " : " << theInds(i) << endl;
231 //III: Put knots in monotone intervals of curvature.
238 Ok = InsKnotBefI(i, aCurv, theCoords, dim, theInds, Standard_True);
244 while(i < theInds.Length());
246 //IV: Cheking feature points.
248 for(i = 1; i <= aFeatureInds.Length(); ++i)
250 Standard_Integer anInd = aFeatureInds(i);
251 for(; j <= theInds.Length() - 1;)
253 if(theInds(j) == anInd)
255 Standard_Integer anIndPrev = theInds(j-1);
256 Standard_Integer anIndNext = theInds(j+1);
257 Standard_Integer ici = (anIndPrev - aCurv.Lower()) * theDim,
258 ici1 = (anIndNext - aCurv.Lower()) * theDim,
259 icm = (anInd - aCurv.Lower()) * theDim;
260 NCollection_LocalArray<Standard_Real> V1(theDim), V2(theDim);
261 Standard_Real mp = 0., m1 = 0., m2 = 0.;
263 for(Standard_Integer k = 0; k < theDim; ++k)
265 V1[k] = theCoords[icm + k] - theCoords[ici + k];
267 V2[k] = theCoords[ici1 + k] - theCoords[icm + k];
270 for(Standard_Integer k = 1; k < theDim; ++k)
272 for(Standard_Integer l = 0; l < k; ++l)
274 p = V1[k]*V2[l] - V1[l]*V2[k];
278 //mp *= 2.; //P(j,i) = -P(i,j);
281 if(mp > aSinCoeff2 * m1 * m2) // Sqrt (mp/(m1*m2)) > aSinCoeff
284 Standard_Real d1 = Abs(aCurv(anInd) - aCurv(anIndPrev));
285 Standard_Real d2 = Abs(aCurv(anInd) - aCurv(anIndNext));
288 Ok = InsKnotBefI(j, aCurv, theCoords, dim, theInds, Standard_False);
300 Ok = InsKnotBefI(j+1, aCurv, theCoords, dim, theInds, Standard_False);
323 //=======================================================================
324 //function : FilterKnots
326 //=======================================================================
327 void ApproxInt_KnotTools::FilterKnots(NCollection_Sequence<Standard_Integer>& theInds,
328 const Standard_Integer theMinNbPnts,
329 NCollection_Vector<Standard_Integer>& theLKnots)
331 // Maximum number of points per knot interval.
332 Standard_Integer aMaxNbPnts = aMaxPntCoeff*theMinNbPnts;
333 Standard_Integer i = 1;
334 Standard_Integer aMinNbStep = theMinNbPnts / 2;
336 // I: Filter too big number of points per knot interval.
337 while(i < theInds.Length())
339 Standard_Integer nbint = theInds(i + 1) - theInds(i) + 1;
340 if(nbint <= aMaxNbPnts)
347 Standard_Integer ind = theInds(i) + nbint / 2;
348 theInds.InsertAfter(i, ind);
352 // II: Filter poins with too small amount of points per knot interval.
354 theLKnots.Append(theInds(i));
355 Standard_Integer anIndsPrev = theInds(i);
356 for(i = 2; i <= theInds.Length(); ++i)
358 if(theInds(i) - anIndsPrev <= theMinNbPnts)
360 if (i != theInds.Length())
362 Standard_Integer anIdx = i + 1;
363 for( ; anIdx <= theInds.Length(); ++anIdx)
365 if (theInds(anIdx) - anIndsPrev >= theMinNbPnts)
370 Standard_Integer aMidIdx = (theInds(anIdx) + anIndsPrev) / 2;
371 if (aMidIdx - anIndsPrev < theMinNbPnts &&
372 aMidIdx - theInds(anIdx) < theMinNbPnts &&
373 theInds(anIdx) - anIndsPrev >= aMinNbStep)
375 if (theInds(anIdx) - anIndsPrev > 2 * theMinNbPnts)
377 // Bad distribution points merge into one knot interval.
378 theLKnots.Append(anIndsPrev + theMinNbPnts);
379 anIndsPrev = anIndsPrev + theMinNbPnts;
384 if (theInds(anIdx - 1) - anIndsPrev >= theMinNbPnts / 2)
386 // Bad distribution points merge into one knot interval.
387 theLKnots.Append(theInds(anIdx - 1));
388 anIndsPrev = theInds(anIdx - 1);
390 if (theInds(anIdx) - theInds(anIdx - 1) <= theMinNbPnts / 2)
392 theLKnots.SetValue(theLKnots.Upper(), theInds(anIdx));
393 anIndsPrev = theInds(anIdx );
399 // Bad distribution points merge into one knot interval.
400 theLKnots.Append(theInds(anIdx));
401 anIndsPrev = theInds(anIdx);
406 else if (anIdx == theInds.Upper() && // Last point obtained.
407 theLKnots.Length() >= 2) // It is possible to modify last item.
409 // Current bad interval from i to last.
410 // Trying to add knot to divide sequence on two parts:
411 // Last good index -> Last index - theMinNbPnts -> Last index
412 Standard_Integer aLastGoodIdx = theLKnots.Value(theLKnots.Upper() - 1);
413 if ( theInds.Last() - 2 * theMinNbPnts >= aLastGoodIdx)
415 theLKnots(theLKnots.Upper()) = theInds.Last() - theMinNbPnts;
416 theLKnots.Append(theInds.Last());
417 anIndsPrev = theInds(anIdx);
421 } // if (i != theInds.Length())
426 theLKnots.Append(theInds(i));
427 anIndsPrev = theInds(i);
431 // III: Fill Last Knot.
432 if(theLKnots.Length() < 2)
434 theLKnots.Append(theInds.Last());
438 if(theLKnots.Last() < theInds.Last())
440 theLKnots(theLKnots.Upper()) = theInds.Last();
444 //=======================================================================
445 //function : InsKnotBefI
447 //=======================================================================
448 Standard_Boolean ApproxInt_KnotTools::InsKnotBefI(const Standard_Integer theI,
449 const TColStd_Array1OfReal& theCurv,
450 const NCollection_LocalArray<Standard_Real>& theCoords,
451 const Standard_Integer theDim,
452 NCollection_Sequence<Standard_Integer>& theInds,
453 const Standard_Boolean ChkCurv)
455 Standard_Integer anInd1 = theInds(theI);
456 Standard_Integer anInd = theInds(theI - 1);
458 if((anInd1-anInd) == 1)
460 return Standard_False;
463 Standard_Real curv = 0.5*(theCurv(anInd) + theCurv(anInd1));
464 Standard_Integer mid = 0, j, jj;
465 const Standard_Real aLimitCurvatureChange = 3.0;
466 for(j = anInd+1; j < anInd1; ++j)
470 // I: Curvature change criteria:
471 // Non-null curvature.
472 if (theCurv(j) > Precision::Confusion() &&
473 theCurv(anInd) > Precision::Confusion() )
475 if (theCurv(j) / theCurv(anInd) > aLimitCurvatureChange ||
476 theCurv(j) / theCurv(anInd) < 1.0 / aLimitCurvatureChange)
478 // Curvature on current interval changed more than 3 times.
480 theInds.InsertBefore(theI, mid);
481 return Standard_True;
485 // II: Angular criteria:
486 Standard_Real ac = theCurv(j - 1), ac1 = theCurv(j);
487 if((curv >= ac && curv <= ac1) || (curv >= ac1 && curv <= ac))
489 if(Abs(curv - ac) < Abs(curv - ac1))
510 Standard_Integer ici = (anInd - theCurv.Lower()) * theDim,
511 ici1 = (anInd1 - theCurv.Lower()) * theDim,
512 icm = (mid - theCurv.Lower()) * theDim;
513 NCollection_LocalArray<Standard_Real> V1(theDim), V2(theDim);
515 Standard_Real mp = 0., m1 = 0., m2 = 0.;
517 for(i = 0; i < theDim; ++i)
519 V1[i] = theCoords[icm + i] - theCoords[ici + i];
521 V2[i] = theCoords[ici1 + i] - theCoords[icm + i];
524 for(i = 1; i < theDim; ++i)
526 for(jj = 0; jj < i; ++jj)
528 p = V1[i]*V2[jj] - V1[jj]*V2[i];
532 //mp *= 2.; //P(j,i) = -P(i,j);
535 if (mp > aSinCoeff2 * m1 * m2) // Sqrt (mp / m1m2) > aSinCoeff
537 theInds.InsertBefore(theI, mid);
538 return Standard_True;
543 theInds.InsertBefore(theI, mid);
544 return Standard_True;
549 return Standard_False;
552 //=======================================================================
553 //function : BuildKnots
555 //=======================================================================
556 void ApproxInt_KnotTools::BuildKnots(const TColgp_Array1OfPnt& thePntsXYZ,
557 const TColgp_Array1OfPnt2d& thePntsU1V1,
558 const TColgp_Array1OfPnt2d& thePntsU2V2,
559 const math_Vector& thePars,
560 const Standard_Boolean theApproxXYZ,
561 const Standard_Boolean theApproxU1V1,
562 const Standard_Boolean theApproxU2V2,
563 const Standard_Integer theMinNbPnts,
564 NCollection_Vector<Standard_Integer>& theKnots)
566 NCollection_Sequence<Standard_Integer> aKnots;
567 Standard_Integer aDim = 0;
569 // I: Convert input data to the corresponding format.
577 NCollection_LocalArray<Standard_Real> aCoords(thePars.Length()*aDim);
578 Standard_Integer i, j;
579 for(i = thePars.Lower(); i <= thePars.Upper(); ++i)
581 j = (i - thePars.Lower()) * aDim;
584 aCoords[j] = thePntsXYZ.Value(i).X();
586 aCoords[j] = thePntsXYZ.Value(i).Y();
588 aCoords[j] = thePntsXYZ.Value(i).Z();
593 aCoords[j] = thePntsU1V1.Value(i).X();
595 aCoords[j] = thePntsU1V1.Value(i).Y();
600 aCoords[j] = thePntsU2V2.Value(i).X();
602 aCoords[j] = thePntsU2V2.Value(i).Y();
607 // II: Build draft knot sequence.
608 ComputeKnotInds(aCoords, aDim, thePars, aKnots);
610 #if defined(APPROXINT_KNOTTOOLS_DEBUG)
611 cout << "Draft knot sequence: " << endl;
612 for(i = aKnots.Lower(); i <= aKnots.Upper(); ++i)
614 cout << i << " : " << aKnots(i) << endl;
618 // III: Build output knot sequence.
619 FilterKnots(aKnots, theMinNbPnts, theKnots);
621 #if defined(APPROXINT_KNOTTOOLS_DEBUG)
622 cout << "Result knot sequence: " << endl;
623 for(i = theKnots.Lower(); i <= theKnots.Upper(); ++i)
625 cout << i << " : " << theKnots(i) << endl;