summaryrefslogtreecommitdiff
path: root/inc/IntCurveSurface_HCurveTool.gxx
blob: 5315d508b3fbccee48c8e42adccb3f19c9d9def2 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// File:	IntCurveSurface_CurveTool.gxx
// Created:	Mon Jul 17 17:39:39 1995
// Author:	Modelistation
//		<model@mastox>

#include CurveGen_hxx
#include <GeomAbs_CurveType.hxx>
#include <GeomAbs_Shape.hxx>
#include <Handle_Geom_BezierCurve.hxx>
#include <Handle_Geom_BSplineCurve.hxx>

#include <TColStd_Array1OfReal.hxx>
#include <TColStd_Array1OfBoolean.hxx>
#include <gce_MakeLin.hxx>
#include <gp_Pnt.hxx>
#include <gp_Lin.hxx>

#define myMinPnts 5
//============================================================
Standard_Integer IntCurveSurface_HCurveTool::NbSamples (const CurveGen& C,
						       const Standard_Real U0,
						       const Standard_Real U1) {
  GeomAbs_CurveType typC = C->GetType();
  static Standard_Real nbsOther = 10.0;
  Standard_Real nbs = nbsOther;
  
  if(typC == GeomAbs_Line) 
    nbs = 2;
  else if(typC == GeomAbs_BezierCurve) 
    nbs = 3 + C->NbPoles();
  else if(typC == GeomAbs_BSplineCurve) { 
    nbs = C->NbKnots();
    nbs*= C->Degree();
    nbs*= C->LastParameter()- C->FirstParameter();
    nbs/= U1-U0;
    if(nbs < 2.0) nbs=2;
  }
  if(nbs>50)
    nbs = 50;
  return((Standard_Integer)nbs);
}
//============================================================
void IntCurveSurface_HCurveTool::SamplePars (const CurveGen& C,
					     const Standard_Real U0,
					     const Standard_Real U1,
					     const Standard_Real Defl,
					     const Standard_Integer NbMin,
					     Handle(TColStd_HArray1OfReal)& Pars) {
  GeomAbs_CurveType typC = C->GetType();
  static Standard_Real nbsOther = 10.0;
  Standard_Real nbs = nbsOther;
  
  if(typC == GeomAbs_Line) 
    nbs = 2;
  else if(typC == GeomAbs_BezierCurve) {
    nbs = 3 + C->NbPoles();
  }
  
  if(typC != GeomAbs_BSplineCurve) {
    if(nbs>50)
      nbs = 50;
    Standard_Integer nnbs = (Standard_Integer)nbs;

    Pars = new TColStd_HArray1OfReal(1, nnbs);
    Standard_Real du = (U1-U0)/(nnbs - 1);

    Pars->SetValue(1, U0);
    Pars->SetValue(nnbs, U1);
    Standard_Integer i;
    Standard_Real u;
    for(i = 2, u = U0+du; i < nnbs; ++i, u += du) {
      Pars->SetValue(i, u);
    }
    return;
  }

  const Handle(Geom_BSplineCurve)& aBC = C->BSpline();

  Standard_Integer i, j, k, nbi;
  Standard_Real t1, t2, dt;
  Standard_Integer ui1 = aBC->FirstUKnotIndex();
  Standard_Integer ui2 = aBC->LastUKnotIndex();
 
  for(i = ui1; i < ui2; ++i) {
    if(U0 >= aBC->Knot(i) && U0 < aBC->Knot(i+1)) {
      ui1 = i;
      break;
    }
  }

  for(i = ui2; i > ui1; --i) {
    if(U1 <= aBC->Knot(i) && U1 > aBC->Knot(i-1)) {
      ui2 = i;
      break;
    }
  }

  Standard_Integer nbsu = ui2-ui1+1; nbsu += (nbsu - 1) * (aBC->Degree()-1);
  Standard_Boolean bUniform = Standard_False;
  if(nbsu < NbMin) {
    nbsu = NbMin;
    bUniform = Standard_True;
  }

  TColStd_Array1OfReal aPars(1, nbsu);
  TColStd_Array1OfBoolean aFlg(1, nbsu);
  //Filling of sample parameters
  if(bUniform) {
    t1 = U0;
    t2 = U1;
    dt = (t2 - t1)/(nbsu - 1);
    aPars(1) = t1;
    aFlg(1) = Standard_False;
    aPars(nbsu) = t2;
    aFlg(nbsu) = Standard_False;
    for(i = 2, t1 += dt; i < nbsu; ++i, t1 += dt) {
      aPars(i) = t1;
      aFlg(i) = Standard_False;
    }
  }
  else {  
    nbi = aBC->Degree();
    k = 0;
    t1 = U0;
    for(i = ui1+1; i <= ui2; ++i) {
      if(i == ui2) t2 = U1;
      else t2 = aBC->Knot(i);
      dt = (t2 - t1)/nbi;
      j = 1;
      do { 
	++k;
	aPars(k) = t1;
	aFlg(k) = Standard_False;
	t1 += dt;	
      }
      while (++j <= nbi);
      t1 = t2;
    }
    ++k;
    aPars(k) = t1;
  }
 //Analysis of deflection


  Standard_Real aDefl2 = Max(Defl*Defl, 1.e-9);
  Standard_Real tol = Max(0.01*aDefl2, 1.e-9);
  Standard_Integer l;

  Standard_Integer NbSamples = 2;
  aFlg(1) = Standard_True;
  aFlg(nbsu) = Standard_True;
  j = 1;
  Standard_Boolean bCont = Standard_True;
  while (j < nbsu-1 && bCont) {
    
    if(aFlg(j+1)) {
      ++j;
      continue;
    }
    
    t2 = aPars(j);
    gp_Pnt p1 = aBC->Value(t2);
    for(k = j+2; k <= nbsu; ++k) {
      t2 = aPars(k);
      gp_Pnt p2 = aBC->Value(t2);

      if(p1.SquareDistance(p2) <= tol) continue;

      gce_MakeLin MkLin(p1, p2);
      const gp_Lin& lin = MkLin.Value();
      Standard_Boolean ok = Standard_True;
      for(l = j+1; l < k; ++l) {
	
	if(aFlg(l)) {
	  ok = Standard_False;
	  break;
	}
	
	gp_Pnt pp =  aBC->Value(aPars(l));
	Standard_Real d = lin.SquareDistance(pp);
	
	if(d <= aDefl2) continue;
	
	ok = Standard_False;
	break;
      }
      
      if(!ok) {
	j = k - 1;
	aFlg(j) = Standard_True;
	++NbSamples;
	break;
      }
      
      if(aFlg(k)) {
	j = k;
	break;
      }
      
      
    }
    
    if(k >= nbsu) bCont = Standard_False;
    
  }

  if(NbSamples < myMinPnts) {
    //uniform distribution 
    NbSamples = myMinPnts;
    Pars = new TColStd_HArray1OfReal(1, NbSamples);
    t1 = U0;
    t2 = U1;
    dt = (t2 - t1)/(NbSamples - 1);
    Pars->SetValue(1, t1);
    Pars->SetValue(NbSamples, t2);
    for(i = 2, t1 += dt; i < NbSamples; ++i, t1 += dt) {
      Pars->SetValue(i, t1);
    }
    return;
  }

  Pars = new TColStd_HArray1OfReal(1, NbSamples);
  j = 0;
  for(i = 1; i <= nbsu; ++i) {
    if(aFlg(i)) {
      ++j;
      Pars->SetValue(j,aPars(i));
    }
  }

  

}