summaryrefslogtreecommitdiff
path: root/src/MAT2d/MAT2d_MiniPath.cxx
blob: ca886e2f036088ad2c19d2987437ccae81ba510f (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
// File:	MAT2d_MiniPath.cxx
// Created:	Thu Oct  7 19:04:22 1993
// Author:	Yves FRICAUD
//		<yfr@phylox>

#include <MAT2d_MiniPath.ixx>
#include <MAT2d_Connexion.hxx>
#include <MAT2d_Array2OfConnexion.hxx>
#include <Extrema_POnCurv2d.hxx>
#include <Extrema_ExtCC2d.hxx>
#include <Extrema_ExtPC2d.hxx>
#include <Geom2dAdaptor_Curve.hxx> 
#include <Geom2d_Point.hxx> 
#include <Geom2d_CartesianPoint.hxx> 
#include <TColStd_SequenceOfInteger.hxx>
#include <TColGeom2d_SequenceOfGeometry.hxx>
#include <Standard_NotImplemented.hxx>

//============================================================================
//function : MAT2d_MiniPath()
//purpose  :
//============================================================================
MAT2d_MiniPath::MAT2d_MiniPath()
{
}

//============================================================================
//function : Perform
//purpose  : Calcul du chemin reliant les differents elements de <aFigure>.
//           le chemin part de la ligne <IndStart>.
//           <Sense> = True les lignes sont orientes dans le sens trigo.
//============================================================================
void MAT2d_MiniPath::Perform
  (const MAT2d_SequenceOfSequenceOfGeometry& Figure,
   const Standard_Integer                    IndStart,
   const Standard_Boolean                    Sense)
{

  Standard_Integer         i,j;
  Standard_Integer         NbLines   = Figure.Length();
  MAT2d_Array2OfConnexion  Connexion (1,NbLines,1,NbLines);
  
  indStart     = IndStart;
  theDirection = 1.;
  if (Sense) theDirection = -1.;

  //----------------------------------------------------------------------
  // Calcul des connexions qui realisent le minimum de distance entre les
  // differents elements de la figure.
  //----------------------------------------------------------------------
  for (i = 1; i < NbLines; i++) {
    for (j =i+1; j <= NbLines; j++){
      Connexion(i,j) = MinimumL1L2(Figure,i,j);
      Connexion(j,i) = Connexion(i,j)->Reverse();
    }
  }
 
  TColStd_SequenceOfInteger Set1;
  TColStd_SequenceOfInteger Set2;
  Standard_Real             DistS1S2;
  Standard_Integer          IndiceLine1,IndiceLine2;
#ifndef DEB
  Standard_Integer          ISuiv =0,MinOnSet1 =0,MinOnSet2 =0;
#else
  Standard_Integer          ISuiv,MinOnSet1,MinOnSet2;
#endif
  //---------------------------------------------------------------------------
  // - 0 Set1 est initialise avec la ligne de depart.
  //     Set2 contient toutes les autres.
  //---------------------------------------------------------------------------

  Set1.Append(IndStart);

  for (i=1 ; i<=NbLines ; i++){
    if (i != IndStart){
      Set2.Append(i);
    }
  }

  //---------------------------------------------------------------------------
  // - 1 Recherche de la connexion C la plus courte entre Set1 et Set2.
  // - 2 La ligne de Set2 realisant le minimum de distance est inseree dans 
  //     Set1 et supprime dans Set2.
  // - 3 Insertion de la connexion dans l ensemble des connexions.
  // - 4 Si Set2 est non vide retour en 1.
  //---------------------------------------------------------------------------

  while (!Set2.IsEmpty()){
    DistS1S2 = RealLast();
    for (i = 1; i <= Set1.Length(); i++) {
      IndiceLine1 = Set1.Value(i);
      for (j = 1 ; j<= Set2.Length() ;j++) {
	IndiceLine2 = Set2.Value(j);
	if(Connexion(IndiceLine1,IndiceLine2)->Distance() < DistS1S2) {
	  ISuiv     = j;
	  DistS1S2  = Connexion(IndiceLine1,IndiceLine2)->Distance();
	  MinOnSet1 = IndiceLine1;
	  MinOnSet2 = IndiceLine2;
	}
      }
    }
    Set1.Append(Set2.Value(ISuiv)); 
    Set2.Remove(ISuiv);
    Append(Connexion(MinOnSet1,MinOnSet2));
  }

  //----------------------------------------------------------------
  // Construction du chemin en parcourant l ensemble des connexions.
  //----------------------------------------------------------------
  RunOnConnexions() ;
}


//============================================================================
//function : Append
//purpose  : Insertion d une nouvelle connexion dans le chemin.
//
//           Les connexions et les lignes constituent un arbre dont
//           - les noeuds sont les lignes.
//           - les connexions sont les branches.
//         
//============================================================================
void MAT2d_MiniPath::Append(const Handle(MAT2d_Connexion)& C)
{
  Handle(MAT2d_Connexion) CC;

  if (!theConnexions.IsBound(C->IndexFirstLine())) {
    MAT2d_SequenceOfConnexion Seq;
    theConnexions.Bind(C->IndexFirstLine(),Seq);
    theConnexions(C->IndexFirstLine()).Append(C);
    theFather.Bind(C->IndexSecondLine(),C);
    return;
  }

  MAT2d_SequenceOfConnexion& Seq  = theConnexions(C->IndexFirstLine());	
  Standard_Integer IndexAfter     = 0;
  Standard_Integer NbConnexions   = Seq.Length();

  for (Standard_Integer i = 1; i <= NbConnexions; i++) {
    CC = Seq.Value(i);
    if (CC->IsAfter(C,theDirection)){
      IndexAfter = i;
      break;
    }
  }
  //----------------------------------------------------------------------
  // Insertion de <C> avant <IAfter>.
  // Si <IAfter> = 0 => Pas de connexions apres <C> => <C> est la
  // derniere.
  //----------------------------------------------------------------------
  if (IndexAfter == 0) {
    Seq.Append(C);
  }
  else {
    Seq.InsertBefore(IndexAfter,C);
  }
  theFather.Bind(C->IndexSecondLine(),C);
  return;
}

//============================================================================
//function : Path
//purpose  : Retour de la sequence de connexions definissant le chemin.
//============================================================================
const MAT2d_SequenceOfConnexion& MAT2d_MiniPath::Path() const
{
  return thePath;
}

//============================================================================
//function : IsConnexionsFrom
//purpose :
//============================================================================
Standard_Boolean MAT2d_MiniPath::IsConnexionsFrom
  (const Standard_Integer i) const
{
  return (theConnexions.IsBound(i));
}

//============================================================================
//function : Connexions
//purpose  : Retour de la sequence de connexions issue de la ligne <i>.
//============================================================================
MAT2d_SequenceOfConnexion& MAT2d_MiniPath::ConnexionsFrom 
  (const Standard_Integer i)
{
  return theConnexions.ChangeFind(i);
}

//============================================================================
//function : IsRoot
//purpose  :
//============================================================================
Standard_Boolean MAT2d_MiniPath::IsRoot(const Standard_Integer ILine) const
{
  return (ILine == indStart);
}

//============================================================================
//function : Father
//purpose  : Retour de la premiere connexion qui arrive sur la ligne i
//============================================================================
Handle(MAT2d_Connexion) MAT2d_MiniPath::Father(const Standard_Integer ILine)
{
  return theFather.ChangeFind(ILine);
}


//============================================================================
//function : RunOnConnexions
//purpose  : Construction de <thePath> en parcourant <theConnexions>.
//============================================================================
void MAT2d_MiniPath::RunOnConnexions() 
{
  Standard_Integer                  i;
  Handle(MAT2d_Connexion)           C;
  const MAT2d_SequenceOfConnexion&  SC = theConnexions(indStart);

  thePath.Clear();
  
  for ( i = 1; i <= SC.Length(); i++) {
    C = SC.Value(i);
    thePath.Append(C);
    ExploSons(thePath,C);
    thePath.Append(C->Reverse());
  }
}

//============================================================================
//function : ExploSons
//purpose  : 
//============================================================================
void MAT2d_MiniPath::ExploSons(      MAT2d_SequenceOfConnexion& CResult,
			       const Handle(MAT2d_Connexion)&   CRef   ) 
{
  Standard_Integer                 i;  
  Standard_Integer                 Index = CRef->IndexSecondLine();

  if (!theConnexions.IsBound(Index)) return;

  const MAT2d_SequenceOfConnexion& SC    = theConnexions(Index);
  Handle(MAT2d_Connexion)          CRR   = CRef->Reverse();
  Handle(MAT2d_Connexion)          C;

  for ( i = 1; i <= SC.Length(); i++) {
    C = SC.Value(i);
    if (C->IsAfter(CRR,theDirection)) {
      CResult.Append(C);
      ExploSons(CResult,C);
      CResult.Append(C->Reverse());
    }
  }

  for ( i = 1; i <= SC.Length(); i++) {
    C = SC.Value(i);
    if (!C->IsAfter(CRR,theDirection)) {
      CResult.Append(C);
      ExploSons(CResult,C);
      CResult.Append(C->Reverse());
    }
    else {
      break;
    }
  }
}

      
//============================================================================
//function : MinimumL1L2
//purpose  : Calcul de la connexion realisant le minimum de distance entre les
//           lignes d indice <IL1> et <IL2> dans <Figure>.
//============================================================================
Handle(MAT2d_Connexion) MAT2d_MiniPath::MinimumL1L2
  (const MAT2d_SequenceOfSequenceOfGeometry& Figure,
   const Standard_Integer                    IL1,
   const Standard_Integer                    IL2) const
{
  Extrema_POnCurv2d              PointOnCurv1,PointOnCurv2;
#ifndef DEB
  Standard_Integer               IC1,IC2,IMinC1 =0,IMinC2 =0,i;
#else
  Standard_Integer               IC1,IC2,IMinC1,IMinC2,i;
#endif
  Standard_Real                  DistL1L2_2,DistP1P2_2;
#ifndef DEB
  Standard_Real                  ParameterOnC1 =0.,ParameterOnC2 =0.;
#else
  Standard_Real                  ParameterOnC1,ParameterOnC2;
#endif
  TColGeom2d_SequenceOfGeometry  L1,L2;
  gp_Pnt2d                       Point1,Point2,P1,P2;
  Handle(Geom2d_Curve)           Item1;
  Handle(Geom2d_Curve)           Item2;

  L1 = Figure.Value(IL1);
  L2 = Figure.Value(IL2);

  DistL1L2_2 = RealLast();

  //---------------------------------------------------------------------------
  // Calcul des extremas de distances entre les composants de L1 et de L2.
  //---------------------------------------------------------------------------

  for (IC1 = 1; IC1 <= L1.Length(); IC1++) {

    Handle(Standard_Type)   Type1 = L1.Value(IC1)->DynamicType();
    if (Type1 != STANDARD_TYPE(Geom2d_CartesianPoint)) {
      Item1 = Handle(Geom2d_Curve)::DownCast(L1.Value(IC1));
    }
    else {
	P1 = Handle(Geom2d_Point)::DownCast(L1.Value(IC1))->Pnt2d();
    }

    for (IC2 = 1; IC2 <= L2.Length(); IC2++) {

      Handle(Standard_Type)   Type2 = L2.Value(IC2)->DynamicType();
      if (Type2 != STANDARD_TYPE(Geom2d_CartesianPoint)) {
	Item2 = Handle(Geom2d_Curve)::DownCast(L2.Value(IC2));
      }
      else {
	P2 = Handle(Geom2d_Point)::DownCast(L2.Value(IC2))->Pnt2d();
      }

      if (Type1 == STANDARD_TYPE(Geom2d_CartesianPoint) &&
	  Type2 == STANDARD_TYPE(Geom2d_CartesianPoint)   ) {
	DistP1P2_2 = P1.SquareDistance(P2);
	if (DistP1P2_2 <= DistL1L2_2) {
	  DistL1L2_2      = DistP1P2_2;
	  IMinC1        = IC1;
	  IMinC2        = IC2;
	  Point1        = P1;
	  Point2        = P2;
	  ParameterOnC1 = 0.;
	  ParameterOnC2 = 0.;
	}
      }
      else if (Type1 == STANDARD_TYPE(Geom2d_CartesianPoint)) {
	Geom2dAdaptor_Curve C2(Item2);
	Extrema_ExtPC2d Extremas(P1,C2);
	if (Extremas.IsDone()){
	  for (i = 1; i <= Extremas.NbExt(); i++) {
	    if (Extremas.SquareDistance(i) < DistL1L2_2) {
	      DistL1L2_2    = Extremas.SquareDistance(i);
	      IMinC1        = IC1;
	      IMinC2        = IC2;
	      PointOnCurv2  = Extremas.Point(i);
	      ParameterOnC1 = 0.;
	      ParameterOnC2 = PointOnCurv2.Parameter();
	      Point1        = P1;
	      Point2        = PointOnCurv2.Value();
	    }
	  }
	}
      }
      else if (Type2 == STANDARD_TYPE(Geom2d_CartesianPoint)) {
	Geom2dAdaptor_Curve C1(Item1);
	Extrema_ExtPC2d Extremas(P2,C1);
	if (Extremas.IsDone()){
	  for (i=1;i<=Extremas.NbExt();i++) {
	    if (Extremas.SquareDistance(i) < DistL1L2_2) {
	      DistL1L2_2    = Extremas.SquareDistance(i);
	      IMinC1        = IC1;
	      IMinC2        = IC2;
	      PointOnCurv1  = Extremas.Point(i);
	      ParameterOnC2 = 0.;
	      ParameterOnC1 = PointOnCurv1.Parameter();
	      Point1        = PointOnCurv1.Value();
	      Point2        = P2;
	    }
	  }
	}
      }
      else {
	Geom2dAdaptor_Curve C1(Item1);
	Geom2dAdaptor_Curve C2(Item2);
	Extrema_ExtCC2d Extremas(C1,C2);
	if (!Extremas.IsParallel() && Extremas.IsDone()){
	  for ( i=1; i <= Extremas.NbExt(); i++) {
	    if (Extremas.SquareDistance(i) < DistL1L2_2) {
	      DistL1L2_2    = Extremas.SquareDistance(i);
	      IMinC1        = IC1;
	      IMinC2        = IC2;
	      Extremas.Points(i,PointOnCurv1,PointOnCurv2);
	      ParameterOnC1 = PointOnCurv1.Parameter();
	      ParameterOnC2 = PointOnCurv2.Parameter();
	      Point1        = PointOnCurv1.Value();
	      Point2        = PointOnCurv2.Value();
	    }
	  }
	}
      }
    }
  }
  Handle(MAT2d_Connexion) ConnexionL1L2;
  ConnexionL1L2 = new MAT2d_Connexion(IL1,IL2,IMinC1,IMinC2,sqrt (DistL1L2_2),
				      ParameterOnC1,ParameterOnC2,
				      Point1,Point2);
  return ConnexionL1L2;
}