summaryrefslogtreecommitdiff
path: root/inc/Extrema_CurveCache.gxx
blob: 94434cc4de64903cff1b45f82f3c95ae0b76a9a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// File     : Extrema_CurveCache.gxx
// Created  : Sun Dec 28 2008
// Author   : Roman Lygin
//            roman.lygin@gmail.com
// Copyright: Roman Lygin, Open CASCADE 2008

#include <Precision.hxx>

//=======================================================================
//function : Extrema_CurveCache
//purpose  : 
//=======================================================================

Extrema_CurveCache::Extrema_CurveCache(const Curve& theC,
                                   const Standard_Real theUFirst,
                                   const Standard_Real theULast,
                                   const Standard_Integer theNbSamples,
                                   const Standard_Boolean theToCalculate) :
    myC (0), myNbSamples (-1), myIsArrayValid (Standard_False)
{
  SetCurve (theC, theUFirst, theULast, theNbSamples, theToCalculate);
}

//=======================================================================
//function : Extrema_CurveCache
//purpose  : 
//=======================================================================

Extrema_CurveCache::Extrema_CurveCache() : myC (0), myNbSamples (-1),
    myIsArrayValid (Standard_False)
{
}

//=======================================================================
//function : SetCurve
//purpose  : 
//=======================================================================

void Extrema_CurveCache::SetCurve (const Curve& theC,
                                   const Standard_Integer theNbSamples,
                                   const Standard_Boolean theToCalculate)
{
  myC = (Standard_Address)&theC;
  myNbSamples = theNbSamples;
  myIsArrayValid = Standard_False;
  myPntArray.Nullify();
  if (theToCalculate) {
    CalculatePoints();
  }
}

//=======================================================================
//function : SetCurve
//purpose  : 
//=======================================================================

void Extrema_CurveCache::SetCurve (const Curve& theC,
                                   const Standard_Real theUFirst,
                                   const Standard_Real theULast,
                                   const Standard_Integer theNbSamples,
                                   const Standard_Boolean theToCalculate)
{
  SetCurve (theC, theNbSamples, Standard_False); //no calculation
  SetRange (theUFirst, theULast, theToCalculate);
}

//=======================================================================
//function : SetRange
//purpose  : 
//=======================================================================

void Extrema_CurveCache::SetRange (const Standard_Real theUFirst,
                                   const Standard_Real theULast,
                                   const Standard_Boolean theToCalculate)
{
  //myTrimFirst and myTrimLast are used to compute values on unlimited curves
  myTrimFirst = myFirst = theUFirst;
  if (Precision::IsInfinite(myTrimFirst)){
    myTrimFirst = -1.0e+10;
  }
  myTrimLast = myLast = theULast;
  if (Precision::IsInfinite(myTrimLast)){
    myTrimLast = 1.0e+10;
  }

  myIsArrayValid = Standard_False;
  myPntArray.Nullify();
  if (theToCalculate) {
    CalculatePoints();
  }
}

//=======================================================================
//function : CalculatePoints
//purpose  : 
//=======================================================================

void Extrema_CurveCache::CalculatePoints()
{
  if (myIsArrayValid) return; //no need to recalculate if nothing has changed
  const Curve& aCurve = *((Curve*)myC);

  // compute myNbSamples point along the [myTrimFirst, myTrimLast] range

  Standard_Real aDelta = myTrimLast - myTrimFirst;
  Standard_Real aPar0 = aDelta / myNbSamples / 100.;
  aDelta = (aDelta - aPar0) / (myNbSamples - 1);
  aPar0 = myTrimFirst + (aPar0/2.);

  //Cache points

  myPntArray = new ArrayOfPnt (1, myNbSamples);

  Standard_Integer i;
  Standard_Real aPar;
  for (i = 1, aPar = aPar0; i <= myNbSamples; i++, aPar += aDelta) {
    myPntArray->SetValue (i, aCurve.Value (aPar));
  }

  myIsArrayValid = Standard_True; //cache is available now
}