summaryrefslogtreecommitdiff
path: root/inc/Blend_Walking_3.gxx
blob: e95be6344884289fb5d32336649e5f1c694749c2 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//26-04-1997 modified by pmn : Initialisation plus fine de la resolution

Standard_Integer Blend_Walking::ArcToRecadre(const Standard_Boolean OnFirst,
					     const math_Vector& sol,
					     const Standard_Integer PrevIndex,
					     gp_Pnt2d& lastpt2d,
					     gp_Pnt2d& pt2d,
					     Standard_Real& ponarc)
{
  Standard_Integer IndexSol = 0,  nbarc = 0;
  Standard_Boolean ok = Standard_False;
  Standard_Boolean byinter = (line->NbPoints() != 0), okinter = 0;
  Standard_Real distmin = RealLast();
  Standard_Real uprev = 0,vprev = 0, prm = 0.0, dist = 0.0;
  Handle(TheTopolTool) Iter;

  if (OnFirst) {
    if(byinter) previousP.ParametersOnS1(uprev,vprev);
    pt2d.SetCoord(sol(1),sol(2));
    Iter = recdomain1;
  }
  else {
    if(byinter) previousP.ParametersOnS2(uprev,vprev);
    pt2d.SetCoord(sol(3),sol(4));
    Iter = recdomain2;
  }
  lastpt2d.SetCoord(uprev,vprev);
  Iter->Init();
  while (Iter->More()) {
    nbarc++; ok = 0;
    if (OnFirst) {
      if(byinter) { 
	ok = okinter = TheBlendTool::Inters(pt2d,lastpt2d,
					    surf1,Iter->Value(),prm,dist); 
      }
      if(!ok) ok = TheBlendTool::Project(pt2d,surf1,Iter->Value(),prm,dist);
    }
    else {
      if(byinter) { 
	ok = okinter = TheBlendTool::Inters(pt2d,lastpt2d,
					    surf2,Iter->Value(),prm,dist); 
      }
      if(!ok) ok = TheBlendTool::Project(pt2d,surf2,Iter->Value(),prm,dist);
    }
    if (ok && (nbarc != PrevIndex) ) {
      if (dist<distmin || okinter) {
	distmin = dist;
	ponarc = prm;
	IndexSol = nbarc;
	if(okinter && (PrevIndex==0)) break;
      }
    }
    Iter->Next();
  }
  return IndexSol;
}

Standard_Boolean Blend_Walking::Recadre(Blend_FuncInv& FuncInv,
					const Standard_Boolean OnFirst,
					const math_Vector& sol,
					math_Vector& solrst,
					Standard_Integer& Indexsol,
					Standard_Boolean& IsVtx,
					TheVertex& Vtx,
					const Standard_Real Extrap)

{
  Standard_Boolean jalons_Trouve = Standard_False;
  Standard_Boolean recadre = Standard_True, ok;
  Standard_Boolean byinter = (line->NbPoints() != 0);
#ifndef DEB
  Standard_Integer LeJalon = 0;
#else
  Standard_Integer LeJalon;
#endif
  Standard_Integer nbarc;
  Standard_Real dist,prm,pmin, vtol;
  gp_Pnt2d pt2d, lastpt2d;

  math_Vector toler(1,4),infb(1,4),supb(1,4),valsol(1,4);

  Handle(Adaptor2d_HCurve2d) thecur;
  Handle(TheTopolTool) Iter;

  if (OnFirst) Iter = recdomain1;
  else         Iter = recdomain2;

  Indexsol = ArcToRecadre(OnFirst, sol, 0, 
			  lastpt2d, pt2d, pmin);
  IsVtx = Standard_False;
  if (Indexsol == 0) {
    return Standard_False;
  }

  Iter->Init();
  nbarc = 1;
  while (nbarc < Indexsol) {
    nbarc++;
    Iter->Next();
  }

  TheArc thearc = Iter->Value();

  if (OnFirst) {
    thecur = TheBlendTool::CurveOnSurf(thearc,surf1);
  }
  else {
    thecur = TheBlendTool::CurveOnSurf(thearc,surf2);
  }

// Le probleme a resoudre
  FuncInv.Set(OnFirst,thecur);
  FuncInv.GetBounds(infb,supb);
  infb(2) -= Extrap;
  supb(2) += Extrap;
  
  FuncInv.GetTolerance(toler,tolesp/10);//Il vaut mieux garder un peu de marge
  math_FunctionSetRoot rsnld(FuncInv,toler,35);
  toler *= 10; // Mais on fait les tests correctements

// Calcul d'un point d'init
  Standard_Real ufirst,ulast;
  TheBlendTool::Bounds(thecur, ufirst,ulast);
  // Pour aider a trouver les coins singuliers on recadre eventuelement le paramtere
  if (Abs(pmin-ufirst) < Abs(ulast-ufirst)/1000) {
    pmin = ufirst;
  }
  if (Abs(pmin-ulast) < Abs(ulast-ufirst)/1000) {
    pmin = ulast;
  }

  if (byinter) { 
    Standard_Real lastParam = previousP.Parameter();
    // Verifie que le recadrage n'est pas un jalons
    if (jalons.Length()!=0) {
      Standard_Real t1, t2, t;
      Standard_Boolean Cherche=Standard_True;
      Standard_Integer ii;
      if (lastParam < param) {
	t1 = lastParam; t2 = param;
      }
      else {
	t1 = param; t2 = lastParam;
      }
      for (ii=1; ii<=jalons.Length() && Cherche; ii++) {
	t = jalons.Value(ii).Parameter();
        if  (((t1 < t) && (t2 > t)) || (t==param)) {
	 jalons_Trouve = Standard_True;
	 LeJalon = ii;
	 Cherche = Standard_False; //Ne marche que si l'on sort simultanement
       }
	else Cherche = t < t2; // On s'arrete si t>=t2;
      }
    }
    if (!jalons_Trouve) {
      //Initialisation par Interpolation
      Standard_Real lambda, u, v;
      gp_Pnt2d Pnt, Pnt1, Pnt2;//,  POnC;
      thecur->D0(pmin, Pnt);    
      if (OnFirst) {
	previousP.ParametersOnS2(u,v);
	Pnt1.SetCoord(u, v);
	Pnt2.SetCoord(sol(3), sol(4));
      }
      else {
	previousP.ParametersOnS1(u,v);
	Pnt1.SetCoord(u, v);
	Pnt2.SetCoord(sol(1), sol(2));
      }

      lambda = Pnt.Distance(lastpt2d);
      if (lambda > 1.e-12) lambda /=  Pnt.Distance(lastpt2d) + Pnt.Distance(pt2d);
      else lambda = 0;
      solrst(1) = pmin;
      solrst(2) = (1-lambda)*lastParam + lambda*param;
      solrst(3) = (1-lambda)*Pnt1.X()  + lambda*Pnt2.X();
      solrst(4) = (1-lambda)*Pnt1.Y()  + lambda*Pnt2.Y();
    }
  }
  else { // sinon on initialise par le dernier point calcule
    solrst(1) = pmin;
    solrst(2) = param; 
    if (OnFirst) {
      solrst(3) = sol(3);
      solrst(4) = sol(4);
    }
    else {
      solrst(3) = sol(1);
      solrst(4) = sol(2);
    }
  }

  if (jalons_Trouve) { // On recupere le jalon
    Blend_Point MonJalon;
    Standard_Boolean periodic;
    Standard_Real uperiod = 0, vperiod = 0;
    gp_Pnt2d Pnt;
    Standard_Real distaux;
    MonJalon = jalons.Value(LeJalon);
    solrst(2) =  MonJalon.Parameter();
    if (OnFirst) {
      MonJalon.ParametersOnS2(solrst(3), solrst(4));
      periodic = (surf2->IsUPeriodic() || surf2->IsVPeriodic());
    }
    else  {
       MonJalon.ParametersOnS1(solrst(3), solrst(4));
       periodic = (surf1->IsUPeriodic() || surf1->IsVPeriodic());
     }

    // Recadrage eventuelle pour le cas periodique
    if (periodic) {
      Handle(Adaptor3d_HSurface) surf;
      if (OnFirst) surf = surf2;
      else surf = surf1;

      lastpt2d = thecur->Value(pmin);      

      if (surf->IsUPeriodic()) {
	uperiod =  surf->UPeriod();
	if (solrst(3)-lastpt2d.X() >  uperiod*0.6) solrst(3) -= uperiod;
	if (solrst(3)-lastpt2d.X() < -uperiod*0.6) solrst(3) += uperiod;
      }
      if (surf->IsVPeriodic()) {
	vperiod =  surf->VPeriod();
	if (solrst(4)-lastpt2d.Y() >  vperiod*0.6) solrst(4) -= vperiod;
	if (solrst(4)-lastpt2d.Y() < -vperiod*0.6) solrst(4) += vperiod;
      }	
    }

    // Pour le parametre sur arc il faut projeter...
    pt2d.SetCoord(solrst(3), solrst(4));
    Pnt = thecur->Value(ufirst);
    dist = pt2d.Distance(Pnt);
    solrst(1) = ufirst;
    Pnt = thecur->Value(ulast);
    distaux = pt2d.Distance(Pnt);
    if ( distaux < dist) {
      solrst(1) = ulast;
      dist = distaux;
    }

    if (dist>Precision::PConfusion()) {
      prm = pmin;
      if (OnFirst) { 
	ok = TheBlendTool::Project(pt2d,surf1,thearc,prm,distaux);
      }
      else {
	ok = TheBlendTool::Project(pt2d,surf2,thearc,prm,distaux);
      }
      if (ok && (pt2d.Distance(thecur->Value(prm)) < dist)) solrst(1) = prm;
      else solrst(1) = pmin;
    }
    // On verifie le jalon
    jalons_Trouve = (FuncInv.IsSolution(solrst,tolesp));
  }

  if (!jalons_Trouve) {
    // Resolution...
    rsnld.Perform(FuncInv,solrst,infb,supb);  
    if (!rsnld.IsDone()) {
      cout << "Walking::Recadre : RSNLD not done " << endl;
      recadre = Standard_False;
    }
    else {
      rsnld.Root(solrst);
      recadre = FuncInv.IsSolution(solrst,tolesp);
    }
  }

  // En cas d'echecs, on regarde si un autre arc 
  // peut faire l'affaire (cas des sorties a proximite d'un vertex)
  dist = (ulast - ufirst)/100;
  if ((!recadre) && 
      ((Abs(pmin-ulast) < dist) || (Abs(pmin-ufirst) < dist)) ) {

    Indexsol =  ArcToRecadre(OnFirst, sol, Indexsol, 
			     lastpt2d, pt2d, pmin);
    if (Indexsol == 0) {
      return Standard_False;
    }

    Iter->Init();
    nbarc = 1;
    while (nbarc < Indexsol) {
      nbarc++;
      Iter->Next();
    }
    thearc = Iter->Value();
 
    if (OnFirst) {
      thecur = TheBlendTool::CurveOnSurf(thearc,surf1);
    }
    else {
      thecur = TheBlendTool::CurveOnSurf(thearc,surf2);
    }
    solrst(1) = pmin;
    // Le probleme a resoudre
    FuncInv.Set(OnFirst,thecur);
    FuncInv.GetBounds(infb,supb);
    FuncInv.GetTolerance(toler,tolesp/10);//Il vaut mieux garder un peu de marge
    math_FunctionSetRoot rsnld(FuncInv,toler,35);
    toler *= 10; // Mais on fait les tests correctements
    // Resolution...
    rsnld.Perform(FuncInv,solrst,infb,supb);
  
    if (!rsnld.IsDone()) {
      cout << "Walking::Recadre : RSNLD not done " << endl;
      recadre = Standard_False;
    }
    else {
      rsnld.Root(solrst);
      recadre = FuncInv.IsSolution(solrst,tolesp);
    }
  }

  if (recadre) {
    // Classification topologique  
    if (OnFirst) {
      thecur = TheBlendTool::CurveOnSurf(thearc,surf1);
    }
    else {
      thecur = TheBlendTool::CurveOnSurf(thearc,surf2);
    }
    TheBlendTool::Bounds(thecur, ufirst,ulast);    

    Iter->Initialize(thearc);
    Iter->InitVertexIterator();
    IsVtx = !Iter->MoreVertex();
    while (!IsVtx) {
      Vtx = Iter->Vertex();
      vtol = 0.4*Abs(ulast-ufirst); // Un majorant de la tolerance
      if (vtol > Max(TheBlendTool::Tolerance(Vtx,thearc), toler(1)))
	vtol = Max(TheBlendTool::Tolerance(Vtx,thearc), toler(1));
      if (Abs(TheBlendTool::Parameter(Vtx,thearc)-solrst(1)) <= vtol) {
	IsVtx = Standard_True; // On est dans la boule du vertex ou 
	                       // le vertex est dans la "boule" du recadrage
      }
      else {
	Iter->NextVertex();
	IsVtx = !Iter->MoreVertex();
      }
    }
    if (!Iter->MoreVertex()) {
      IsVtx = Standard_False;
    }
    return Standard_True;
  }
  return Standard_False;
}


void Blend_Walking::Transition(const Standard_Boolean OnFirst,
			       const TheArc& A,
			       const Standard_Real Param,
			       IntSurf_Transition& TLine,
			       IntSurf_Transition& TArc)
{
  Standard_Boolean computetranstionaveclacorde = 0;
  gp_Vec tgline;
  Blend_Point prevprev;

  if(previousP.IsTangencyPoint()){
    if(line->NbPoints() < 2) return;
    computetranstionaveclacorde = 1;
    if(sens < 0){
      prevprev = line->Point(2);
    }
    else {
      prevprev = line->Point(line->NbPoints() - 1);
    }
  }
  gp_Pnt2d p2d;
  gp_Vec2d dp2d;

  gp_Pnt pbid;
  gp_Vec d1u,d1v,normale,tgrst;
  gp_Dir thenormal;
  CSLib_NormalStatus stat;

  TheArcTool::D1(A,Param,p2d,dp2d);
  if (OnFirst) {
    TheSurfaceTool::D1(surf1,p2d.X(),p2d.Y(),pbid,d1u,d1v);
    if(!computetranstionaveclacorde) tgline = previousP.TangentOnS1();
    else tgline = gp_Vec(prevprev.PointOnS1(),previousP.PointOnS1());
  }
  else {
    TheSurfaceTool::D1(surf2,p2d.X(),p2d.Y(),pbid,d1u,d1v);
    if(!computetranstionaveclacorde) tgline = previousP.TangentOnS2();
    else tgline = gp_Vec(prevprev.PointOnS2(),previousP.PointOnS2());
  }

  tgrst.SetLinearForm(dp2d.X(),d1u,dp2d.Y(),d1v);

  CSLib::Normal(d1u, d1v, 1.e-9, stat, thenormal);
  if (stat ==  CSLib_Defined) normale.SetXYZ(thenormal.XYZ());
  else {
    Handle(Adaptor3d_HSurface) surf;
    if (OnFirst) surf = surf1;
    else         surf = surf2;
    Standard_Integer iu, iv;
    TColgp_Array2OfVec Der(0, 2 , 0, 2);
    TheSurfaceTool::D2(surf,p2d.X(),p2d.Y(),pbid, Der(1,0), Der(0,1), 
		       Der(2,0), Der(0,2), Der(1,1));
    Der(2,1) = TheSurfaceTool::DN(surf, p2d.X(), p2d.Y(), 2,1);
    Der(1,2) = TheSurfaceTool::DN(surf,p2d.X(),p2d.Y(), 1,2);
    Der(2,2) = TheSurfaceTool::DN(surf,p2d.X(),p2d.Y(), 2,2);
    CSLib::Normal(2, Der, 1.e-9,  
		  p2d.X(), p2d.Y(), 
		  TheSurfaceTool::FirstUParameter(surf),
		  TheSurfaceTool::LastUParameter(surf),
		  TheSurfaceTool::FirstVParameter(surf),
		  TheSurfaceTool::LastVParameter(surf),
		  stat, thenormal, iu, iv);
    normale.SetXYZ(thenormal.XYZ());
#if DEB
    if (stat == CSLib_InfinityOfSolutions)
      cout << "Blend_Walking::Transition : Infinite de Normal" << endl;
#endif 
  }

  IntSurf::MakeTransition(tgline,tgrst,normale,TLine,TArc);

}


void Blend_Walking::MakeExtremity(TheExtremity& Extrem,
				  const Standard_Boolean OnFirst,
				  const Standard_Integer Index,
				  const Standard_Real Param,
				  const Standard_Boolean IsVtx,
				  const TheVertex& Vtx)
{

  IntSurf_Transition Tline,Tarc;
  Standard_Integer nbarc;
  Handle(TheTopolTool) Iter;

  if (OnFirst) {
    Extrem.SetValue(previousP.PointOnS1(),
		    sol(1),sol(2),
		    previousP.Parameter(), tolesp);
    if (!previousP.IsTangencyPoint())  
      Extrem.SetTangent(previousP.TangentOnS1());
    Iter = recdomain1;
  }
  else {
    Extrem.SetValue(previousP.PointOnS2(),
		    sol(3),sol(4),
		    previousP.Parameter(), tolesp);
    if (!previousP.IsTangencyPoint())  
      Extrem.SetTangent(previousP.TangentOnS2());
    Iter = recdomain2;
  }

  Iter->Init();
  nbarc = 1;

  while (nbarc < Index) {
    nbarc++;
    Iter->Next();
  }

  Transition(OnFirst,Iter->Value(),Param,Tline,Tarc);
  Extrem.AddArc(Iter->Value(),Param,Tline,Tarc);
  if (IsVtx) Extrem.SetVertex(Vtx);
}

void  Blend_Walking::MakeSingularExtremity( TheExtremity& Extrem,
					   const Standard_Boolean OnFirst,
					   const TheVertex& Vtx)
{
  IntSurf_Transition Tline,Tarc;
  Handle(TheTopolTool) Iter;
  Standard_Real prm;

  if (OnFirst) {
    Iter = recdomain1;
    if (!previousP.IsTangencyPoint())
      Extrem.SetTangent(previousP.TangentOnS1());
  }
  else {
    if (!previousP.IsTangencyPoint()) 
      Extrem.SetTangent(previousP.TangentOnS2());
    Iter = recdomain2;
  }
  
  Iter->Init(); 
  Extrem.SetVertex(Vtx);
  while (Iter->More()) {
    TheArc arc = Iter->Value();
    Iter->Initialize(arc);
    Iter->InitVertexIterator();
    while (Iter->MoreVertex()) {
      if (Iter->Identical(Vtx,Iter->Vertex())) {
	prm = TheBlendTool::Parameter(Vtx,arc);
	Transition(OnFirst,arc,prm,Tline,Tarc);
	Extrem.AddArc(arc,prm,Tline,Tarc);
      }
      Iter->NextVertex();
    }
    Iter->Next();
  }   
}