summaryrefslogtreecommitdiff
path: root/inc/LProp_CLProps.gxx
blob: 5bea448f99a5077884185fb6417cc6f2d4c1888f (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <LProp_Status.hxx>
#include <LProp_NotDefined.hxx>
#include <Standard_OutOfRange.hxx>


 
LProp_CLProps::LProp_CLProps (const Curve& C, 
                              const Standard_Real U,
			      const Standard_Integer N, 
                              const Standard_Real Resolution) 
     : myCurve(C), 
       level(N),
       cn(4), // cn(Tool::Continuity(C)),   RLE
       linTol(Resolution), 
       tangentStatus (LProp_Undecided)
{

  Standard_OutOfRange_Raise_if (N < 0 || N > 3,
                                "LProp_CLProps::LProp_CLProps()");
  
  SetParameter(U);
}

LProp_CLProps::LProp_CLProps (const Curve& C, 
			      const Standard_Integer N, 
                              const Standard_Real Resolution) 
     : myCurve(C), 
       u(RealLast()),
       level(N),
       cn(4), // (Tool::Continuity(C)), RLE
       linTol(Resolution), 
       tangentStatus (LProp_Undecided)

{

  Standard_OutOfRange_Raise_if (N < 0 || N > 3, "");  
}

LProp_CLProps::LProp_CLProps (const Standard_Integer N, 
                              const Standard_Real Resolution) 
     : u(RealLast()),
       level(N),
       cn(0),
       linTol(Resolution),
       tangentStatus (LProp_Undecided)
{

  Standard_OutOfRange_Raise_if (N < 0 || N > 3, "");  
}

void LProp_CLProps::SetParameter(const Standard_Real U)
{ 
  u = U;
  switch (level) {
  case 0:
    Tool::Value(myCurve, u, pnt);
    break;
  case 1:
    Tool::D1(myCurve, u, pnt, d[0]);
    break;
  case 2:
    Tool::D2(myCurve, u, pnt, d[0], d[1]);
    break;
  case 3:
    Tool::D3(myCurve, u, pnt, d[0], d[1], d[2]);
    break;
  }
  tangentStatus = LProp_Undecided;
}

void LProp_CLProps::SetCurve(const Curve& C) {
       myCurve = C ; 
       cn = 4; // Tool::Continuity(C); RLE
}

const Pnt& LProp_CLProps::Value () const
{ 
  return pnt;
}

const Vec& LProp_CLProps::D1 ()
{
  if (level < 1) {
    level = 1;
    Tool::D1(myCurve, u, pnt, d[0]);
  }
  return d[0];
}

const Vec& LProp_CLProps::D2 ()
{
  if (level < 2) {
    level = 2;
    Tool::D2(myCurve, u, pnt, d[0], d[1]);
  }
  return d[1];
}

const Vec& LProp_CLProps::D3 ()
{
  if (level < 3) {
    level = 3;
    Tool::D3(myCurve, u, pnt, d[0], d[1], d[2]);
  }
  return d[2];
}

Standard_Boolean LProp_CLProps::IsTangentDefined ()
{

  if (tangentStatus == LProp_Undefined) {
    return Standard_False;
  }
  else if (tangentStatus >= LProp_Defined) {
    return Standard_True;
  }

  // tangentStatus == Lprop_Undecided 
  // we have to calculate the first non null derivative
  Standard_Real Tol = linTol * linTol;
  Vec V;
  Standard_Integer Order = 0;
  while (Order < 4) {
    Order++;
    if(cn >= Order) {
      switch(Order) {
      case 1 :
	V = D1();
	break;
      case 2 :
	V = D2();
	break;
      case 3 :
	V = D3();
	break;
      };
      if(V.SquareMagnitude() > Tol) {
	significantFirstDerivativeOrder = Order;
	tangentStatus = LProp_Defined;
	return Standard_True;
      }
    }
    else {
      tangentStatus = LProp_Undefined;
      return Standard_False;
    }
  }
  return Standard_False;
}


void  LProp_CLProps::Tangent (Dir& D)
{

  if(!IsTangentDefined()) { LProp_NotDefined::Raise(); }
  D = Dir(d[significantFirstDerivativeOrder - 1]);
}

Standard_Real LProp_CLProps::Curvature ()
{
  Standard_Boolean isDefined = IsTangentDefined();
  LProp_NotDefined_Raise_if(!isDefined,
			    "LProp_CLProps::CurvatureNotDefined()");

  // if the first derivative is null the curvature is infinite.
  if(significantFirstDerivativeOrder > 1) return RealLast();

  Standard_Real Tol = linTol * linTol;
  Standard_Real DD1 = d[0].SquareMagnitude();
  Standard_Real DD2 = d[1].SquareMagnitude();
  // if the second derivative is null the curvature is null.
  if (DD2 <= Tol) { 
    curvature = 0.0;
  }
  else {
    Standard_Real N = d[0].CrossSquareMagnitude(d[1]);
    // if d[0] and d[1] are colinear the curvature is null.
    Standard_Real t = N/(DD1*DD2);
    if (t<=Tol) {
      curvature = 0.0;
    }
    else {
      curvature = sqrt(N) / (DD1*sqrt(DD1));
    }
  }
  return curvature;
} 


void  LProp_CLProps::Normal (Dir& D)
{
  Standard_Real c = Curvature();
  if(c==RealLast() || Abs(c) <= linTol) { LProp_NotDefined::Raise(); }

  // we used here the following vector relation 
  // a ^ (b ^ c) = b(ac) - c(ab)
  // Norm = d[0] ^ (d[1] ^ d[0])

  Vec Norm = d[1] * (d[0] * d[0]) - d[0] * (d[0] * d[1]);
  D = Dir(Norm);
}


void  LProp_CLProps::CentreOfCurvature (Pnt& P)
{
  if(Abs(Curvature()) <= linTol) { LProp_NotDefined::Raise(); }

  // we used here the following vector relation 
  // a ^ (b ^ c) = b(ac) - c(ab)
  // Norm = d[0] ^ (d[1] ^ d[0])

  Vec Norm = d[1] * (d[0] * d[0]) - d[0] * (d[0] * d[1]);
  Norm.Normalize();
  Norm.Divide(curvature);
  P= pnt.Translated(Norm);
}