summaryrefslogtreecommitdiff
path: root/src/TDF/TDF_Tool.cxx
blob: 538a9d26f53a08cb4a9d76bb1845b5321c1c54f4 (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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// File:	TDF_Tool.cxx
//      	------------
// Author:	DAUTRY Philippe
//		<fid@fox.paris1.matra-dtv.fr>
// Copyright:	Matra Datavision 1997

// Version:	0.0
// History:	Version	Date		Purpose
//		0.0	Mar 13 1997	Creation



#include <TDF_Tool.ixx>

#include <TDF_MapIteratorOfLabelMap.hxx>
#include <TDF_MapIteratorOfAttributeMap.hxx>
#include <TDF_AttributeIterator.hxx>
#include <TDF_AttributeIndexedMap.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDF_DataSet.hxx>
#include <TDF_ListIteratorOfLabelList.hxx>

#include <TColStd_ListOfInteger.hxx>
#include <TColStd_ListIteratorOfListOfInteger.hxx>

#define TDF_TagSeparator ':'

static void TDF_Tool_ExtendedDeepDump(Standard_OStream& anOS, 
				      const TDF_Label& aLabel, 
				      const TDF_IDFilter& aFilter,
				      TDF_AttributeIndexedMap& aMap);

static Standard_Boolean TDF_Tool_DescendantRef(const TDF_Label& aRefLabel, 
					       const TDF_Label& aLabel,
					       const TDF_IDFilter& aFilter, 
					       const Handle(TDF_DataSet)& ds);

static void TDF_Tool_OutReferers(const TDF_Label& aRefLabel, 
				 const TDF_Label& aLabel,
				 TDF_AttributeMap& atts,  
				 const TDF_IDFilter& aFilterForReferers,
				 const TDF_IDFilter& aFilterForReferences,
				 const Handle(TDF_DataSet)& ds);

static void TDF_Tool_OutReferences(const TDF_Label& aRefLabel,
				   const TDF_Label& aLabel,
				   TDF_AttributeMap& atts,  
				   const TDF_IDFilter& aFilterForReferers,
				   const TDF_IDFilter& aFilterForReferences,
				   const Handle(TDF_DataSet)& ds);

//=======================================================================
//function : NbLabels
//purpose  : Returns the numbers of labels of the tree.
//=======================================================================

Standard_Integer TDF_Tool::NbLabels(const TDF_Label& aLabel)
{
  Standard_Integer n = 1;
  for (TDF_ChildIterator itr(aLabel,Standard_True); itr.More(); itr.Next())
    ++n;
  return n;
}


//=======================================================================
//function : NbAttributes
//purpose  : Returns the number of attributes of the tree.
//=======================================================================

Standard_Integer TDF_Tool::NbAttributes(const TDF_Label& aLabel)
{
  Standard_Integer n = aLabel.NbAttributes();
  for (TDF_ChildIterator itr(aLabel,Standard_True); itr.More(); itr.Next())
    n += itr.Value().NbAttributes();
  return n;
}


//=======================================================================
//function : NbAttributes
//purpose  : Returns the number of attributes of the tree,
//           selected by an IDFilter.
//=======================================================================

Standard_Integer TDF_Tool::NbAttributes
(const TDF_Label& aLabel,
 const TDF_IDFilter& aFilter)
{
  Standard_Integer n = 0;
  TDF_AttributeIterator it2;
  for (it2.Initialize(aLabel,Standard_True);it2.More();it2.Next())
    if (aFilter.IsKept(it2.Value())) ++n;
  for (TDF_ChildIterator it1(aLabel,Standard_True); it1.More(); it1.Next())
    for (it2.Initialize(it1.Value(),Standard_True);it2.More();it2.Next())
      if (aFilter.IsKept(it2.Value())) ++n;
  return n;
}


//=======================================================================
//function : IsSelfContained
//purpose  : 
//=======================================================================

Standard_Boolean TDF_Tool::IsSelfContained(const TDF_Label& aLabel)
{
  TDF_IDFilter filter(Standard_False); // Keep all.
  return IsSelfContained(aLabel,filter);
}

//=======================================================================
//function : IsSelfContained
//purpose  : 
//=======================================================================

Standard_Boolean TDF_Tool::IsSelfContained
(const TDF_Label& aLabel,
 const TDF_IDFilter& aFilter)
{
  Handle(TDF_DataSet) ds = new TDF_DataSet();

  if (!TDF_Tool_DescendantRef(aLabel,aLabel,aFilter,ds))
    return Standard_False;

  for (TDF_ChildIterator itr(aLabel,Standard_True);
       itr.More();
       itr.Next()) {
    if (!TDF_Tool_DescendantRef(aLabel,itr.Value(),aFilter,ds))
      return Standard_False;
  }
  return Standard_True;
}


//=======================================================================
//function : TDF_Tool_DescendantRef
//purpose  : 
//=======================================================================

static Standard_Boolean TDF_Tool_DescendantRef
(const TDF_Label& aRefLabel,
 const TDF_Label& aLabel,
 const TDF_IDFilter& aFilter,
 const Handle(TDF_DataSet)& ds)
{
  for (TDF_AttributeIterator itr(aLabel); itr.More(); itr.Next()) {
    // CLE
    // const Handle(TDF_Attribute)& labAtt = itr.Value();
    Handle(TDF_Attribute) labAtt = itr.Value();
    // ENDCLE
    if (aFilter.IsKept(labAtt)) {
      labAtt->References(ds);
      // First of all, the referenced labels.
      const TDF_LabelMap& labMap = ds->Labels();

      for (TDF_MapIteratorOfLabelMap labMItr(labMap);
	   labMItr.More(); labMItr.Next()) {
	if (!labMItr.Key().IsDescendant(aRefLabel))
	  return Standard_False;
      }
      // Then the referenced attributes.
      const TDF_AttributeMap& attMap = ds->Attributes();
      for (TDF_MapIteratorOfAttributeMap attMItr(attMap);
	   attMItr.More(); attMItr.Next()) {
	// CLE
	// const Handle(TDF_Attribute)& att = attMItr.Key();
	Handle(TDF_Attribute) att = attMItr.Key();
	// ENDCLE
	if (aFilter.IsKept(att) && !att->Label().IsDescendant(aRefLabel))
	  return Standard_False;
      }
      ds->Clear();
    }
  }
  return Standard_True;
}


//=======================================================================
//function : OutReferers
//purpose  : 
//=======================================================================

void TDF_Tool::OutReferers(const TDF_Label& aLabel,
			   TDF_AttributeMap& atts)
{
  TDF_IDFilter filter(Standard_False); // Keep all.
  OutReferers(aLabel,filter,filter,atts);
}


//=======================================================================
//function : OutReferers
//purpose  : 
//=======================================================================

void TDF_Tool::OutReferers(const TDF_Label& aLabel,
			   const TDF_IDFilter& aFilterForReferers,
			   const TDF_IDFilter& aFilterForReferences,
			   TDF_AttributeMap& atts)
{
  Handle(TDF_DataSet) ds = new TDF_DataSet();
  TDF_Tool_OutReferers(aLabel,aLabel,atts,aFilterForReferers,aFilterForReferences,ds);
  for (TDF_ChildIterator itr(aLabel,Standard_True);itr.More();itr.Next()) {
    TDF_Tool_OutReferers(aLabel,itr.Value(),atts,aFilterForReferers,aFilterForReferences,ds);
  }
}


//=======================================================================
//function : TDF_Tool_OutReferers
//purpose  : 
//=======================================================================

static void TDF_Tool_OutReferers(const TDF_Label& aRefLabel,
				 const TDF_Label& aLabel,
				 TDF_AttributeMap& atts,
				 const TDF_IDFilter& aFilterForReferers,
				 const TDF_IDFilter& aFilterForReferences,
				 const Handle(TDF_DataSet)& ds)
{
  Standard_Boolean outRefFound = Standard_False;

  for (TDF_AttributeIterator itr(aLabel); itr.More(); itr.Next()) {

    if (!aFilterForReferers.IsKept(itr.Value())) continue;
    itr.Value()->References(ds);

    const TDF_AttributeMap& attMap = ds->Attributes();
    for (TDF_MapIteratorOfAttributeMap attMItr(attMap);
	 attMItr.More(); attMItr.Next()) {
      // CLE
      // const Handle(TDF_Attribute)& att = attMItr.Key();
      Handle(TDF_Attribute) att = attMItr.Key();
      // ENDCLE
      if (aFilterForReferences.IsKept(att) && !att->Label().IsDescendant(aRefLabel)) {
	atts.Add(itr.Value());
	outRefFound = Standard_True;
	break;
      }
    }

    if (!outRefFound) {
      const TDF_LabelMap& labMap = ds->Labels();
      for (TDF_MapIteratorOfLabelMap labMItr(labMap);
	   labMItr.More(); labMItr.Next()) {
	if (!labMItr.Key().IsDescendant(aRefLabel)) {
	  atts.Add(itr.Value());
	  break;
	}
      }
    }

    outRefFound = Standard_False;
    ds->Clear();
  }
}

//=======================================================================
//function : OutReferences
//purpose  : 
//=======================================================================

void TDF_Tool::OutReferences(const TDF_Label& aLabel,
			     TDF_AttributeMap& atts)
{
  TDF_IDFilter filter(Standard_False); // Keep all.
  OutReferences(aLabel,filter,filter,atts);
}

//=======================================================================
//function : OutReferences
//purpose  : 
//=======================================================================

void TDF_Tool::OutReferences(const TDF_Label& aLabel,
			     const TDF_IDFilter& aFilterForReferers,
			     const TDF_IDFilter& aFilterForReferences,
			     TDF_AttributeMap& atts)
{
  Handle(TDF_DataSet) ds = new TDF_DataSet();
  TDF_Tool_OutReferences(aLabel,aLabel,atts,aFilterForReferers,aFilterForReferences,ds);
  for (TDF_ChildIterator itr(aLabel,Standard_True);itr.More();itr.Next()) {
    TDF_Tool_OutReferences(aLabel,itr.Value(),atts,aFilterForReferers,aFilterForReferences,ds);
  }
}

//=======================================================================
//function : TDF_Tool_OutReferences
//purpose  : 
//=======================================================================

static void TDF_Tool_OutReferences(const TDF_Label& aRefLabel,
				   const TDF_Label& aLabel,
				   TDF_AttributeMap& atts,
				   const TDF_IDFilter& aFilterForReferers,
				   const TDF_IDFilter& aFilterForReferences,
				   const Handle(TDF_DataSet)& ds)
{
  for (TDF_AttributeIterator itr(aLabel); itr.More(); itr.Next()) {
    if (!aFilterForReferers.IsKept(itr.Value())) continue;
    itr.Value()->References(ds);
    const TDF_AttributeMap& attMap = ds->Attributes();
    for (TDF_MapIteratorOfAttributeMap attMItr(attMap);attMItr.More();attMItr.Next()) {
      Handle(TDF_Attribute) att = attMItr.Key();
      if (aFilterForReferences.IsKept(att) && !att->Label().IsDescendant(aRefLabel)) {
	atts.Add(att);
      }
    }
    const TDF_LabelMap& labMap = ds->Labels();
    for (TDF_MapIteratorOfLabelMap labMItr(labMap);labMItr.More();labMItr.Next()) {
      if (!labMItr.Key().IsDescendant(aRefLabel)) {
	TDF_AttributeIterator itra(labMItr.Key());
	for (; itra.More(); itra.Next()) {
	  if (aFilterForReferences.IsKept(itra.Value())) {
	    atts.Add(itra.Value());
	  }
	}
      }
    }
  }
  ds->Clear();
}

//=======================================================================
//function : RelocateLabel
//purpose  : 
//=======================================================================

void TDF_Tool::RelocateLabel
(const TDF_Label& aSourceLabel,
 const TDF_Label& fromRoot,
 const TDF_Label& toRoot,
 TDF_Label& aTargetLabel,
 const Standard_Boolean create)
{
  if (!aSourceLabel.IsDescendant(fromRoot)) return;
  aTargetLabel.Nullify();
  TColStd_ListOfInteger labelTags;
  TDF_Tool::TagList(aSourceLabel,labelTags);
  TColStd_ListOfInteger toTags;
  TDF_Tool::TagList(toRoot,toTags);
  for (Standard_Integer i = fromRoot.Depth(); i >= 0; --i)
    labelTags.RemoveFirst();
  labelTags.Prepend(toTags);
  TDF_Tool::Label(toRoot.Data(),labelTags,aTargetLabel,create);
}


//=======================================================================
//function : Entry
//purpose  : Returns the entry as an ascii string.
//=======================================================================

void TDF_Tool::Entry
(const TDF_Label& aLabel,
 TCollection_AsciiString& anEntry)
{
  anEntry.Clear();
  if (!aLabel.IsNull()) {
    TColStd_ListOfInteger Tags;
    TDF_Tool::TagList(aLabel, Tags);
    anEntry += TCollection_AsciiString(Tags.First());
    Tags.RemoveFirst();
    if (Tags.IsEmpty()) {
      anEntry += TDF_TagSeparator; // It must be the root label case.
    }
    else {
      while (!Tags.IsEmpty()) {
	anEntry += TDF_TagSeparator;
	anEntry += TCollection_AsciiString(Tags.First());
	Tags.RemoveFirst();
      }
    }
  }
}


//=======================================================================
//function : TagList
//purpose  : Returns the entry of a label as a list of integers.
//=======================================================================

void TDF_Tool::TagList
(const TDF_Label& aLabel,
 TColStd_ListOfInteger& aTagList)
{
  aTagList.Clear();
  if (!aLabel.IsNull()) {
    TDF_Label Label = aLabel;
    for (;;) {
      aTagList.Prepend(Label.Tag());
      if (Label.IsRoot()) break;
      Label = Label.Father();
    }
  }
}


//=======================================================================
//function : TagList
//purpose  : Returns the entry expressed as a string as a list of integers.
//=======================================================================

void TDF_Tool::TagList
(const TCollection_AsciiString& anEntry,
 TColStd_ListOfInteger& aTagList)
{
  char* cc = (char *)anEntry.ToCString();
  Standard_Integer n = 0;
  aTagList.Clear();
  while (*cc != '\0') {
    while ( *cc >= '0' && *cc <= '9') {
      n = 10*n + (*cc - '0');
      ++cc;
    }
    if (*cc == TDF_TagSeparator || *cc == '\0') {
      aTagList.Append(n);
      n = 0;
      if (*cc != '\0') ++cc;
    }
    else { // Not an entry!
      aTagList.Clear();
      break;
    }
  }
}


//=======================================================================
//function : Label
//purpose  : Returns the label expressed by <anEntry>.
//=======================================================================

void TDF_Tool::Label
(const Handle(TDF_Data)& aDF,
 const TCollection_AsciiString& anEntry,
 TDF_Label& aLabel,
 const Standard_Boolean create) 
{ TDF_Tool::Label(aDF,anEntry.ToCString(),aLabel,create); }


//=======================================================================
//function : Label
//purpose  : Returns the label expressed by <anEntry>,
//           and creates it if <create> is true.
//=======================================================================

void TDF_Tool::Label
(const Handle(TDF_Data)& aDF,
 const Standard_CString  anEntry,
 TDF_Label&              aLabel,
 const Standard_Boolean  create) 
{
  TColStd_ListOfInteger tagList;
  TDF_Tool::TagList(anEntry,tagList);
  TDF_Tool::Label(aDF,tagList,aLabel,create);
}


//=======================================================================
//function : Label
//purpose  : Returns the label expressed by <anEntry>,
//           and creates it if <create> is true.
//=======================================================================

void TDF_Tool::Label
(const Handle(TDF_Data)& aDF,
 const TColStd_ListOfInteger& aTagList,
 TDF_Label& aLabel,
 const Standard_Boolean create) 
{
  if (aTagList.Extent() == 0) {
    aLabel.Nullify();
  }
  else {
    aLabel = aDF->Root();
    if (aTagList.Extent() == 1 && aTagList.First() == 0) return;
    else {
      TColStd_ListIteratorOfListOfInteger tagItr (aTagList);
      tagItr.Next(); // Suppresses root tag.
      for (; !aLabel.IsNull() && tagItr.More(); tagItr.Next()) {
	aLabel = aLabel.FindChild(tagItr.Value(),create);
      }
    }
  }
}


//=======================================================================
//function : CountLabels
//purpose  : 
//=======================================================================

void TDF_Tool::CountLabels
(TDF_LabelList& aLabelList,
 TDF_LabelIntegerMap& aLabelMap)
{
  if (aLabelList.IsEmpty()) return;
  Standard_Boolean next = Standard_True;
  TDF_ListIteratorOfLabelList itr(aLabelList);
  while (itr.More()) {
    const TDF_Label& lab = itr.Value();
    if (aLabelMap.IsBound(lab)) {
      aLabelMap(lab) += 1;
      aLabelList.Remove(itr);
      next = Standard_False;
    }
    else {
      aLabelMap.Bind(lab,1);
      next = itr.More();
    }
    if (next && !aLabelList.IsEmpty()) itr.Next();
  }
}


//=======================================================================
//function : DeductLabels
//purpose  : 
//=======================================================================

void TDF_Tool::DeductLabels
(TDF_LabelList& aLabelList,
 TDF_LabelIntegerMap& aLabelMap)
{
  if (aLabelList.IsEmpty()) return;
  Standard_Boolean next = Standard_True;
  TDF_ListIteratorOfLabelList itr(aLabelList);
  while (itr.More()) {
    const TDF_Label& lab = itr.Value();
    if (aLabelMap.IsBound(lab)) {
      aLabelMap(lab) -= 1;
      if (aLabelMap(lab) == 0) {
	aLabelMap.UnBind(lab);
	aLabelList.Remove(itr);
	next = Standard_False;
      }
    }
    else next = itr.More();
    if (next && !aLabelList.IsEmpty()) itr.Next();
  }
}


//=======================================================================
//function : DeepDump
//purpose  : Deep dump of a DF.
//=======================================================================

void TDF_Tool::DeepDump
(Standard_OStream& anOS,
 const Handle(TDF_Data)& aDF)
{
  anOS<<aDF;
  TDF_Tool::DeepDump(anOS,aDF->Root());
}


//=======================================================================
//function : ExtendedDeepDump
//purpose  : Extended deep dump of a DF.
//=======================================================================

void TDF_Tool::ExtendedDeepDump
(Standard_OStream& anOS,
 const Handle(TDF_Data)& aDF,
 const TDF_IDFilter& aFilter)
{
  anOS<<aDF;
  TDF_Tool::ExtendedDeepDump(anOS,aDF->Root(),aFilter);
}


//=======================================================================
//function : DeepDump
//purpose  : Deep dump of a label.
//=======================================================================

void TDF_Tool::DeepDump
(Standard_OStream& anOS,
 const TDF_Label& aLabel)
{
  // Dumps the label.
  anOS<<aLabel;
  // Its children
  for (TDF_ChildIterator ChildIt(aLabel); ChildIt.More (); ChildIt.Next ()) {
    TDF_Tool::DeepDump(anOS,ChildIt.Value());
  }
}


//=======================================================================
//function : ExtendedDeepDump
//purpose  : Extended deep dump of a label.
//=======================================================================

void TDF_Tool::ExtendedDeepDump
(Standard_OStream& anOS,
 const TDF_Label& aLabel,
 const TDF_IDFilter& aFilter)
{
  TDF_AttributeIndexedMap map;
  TDF_Tool_ExtendedDeepDump(anOS,aLabel,aFilter,map);

  anOS<<map.Extent()<<" attribute"; if (map.Extent()>1) anOS<<"s";
  anOS<<" referenced by the label structure."<<endl;

  anOS<<endl<<"Extended dump of filtered attribute(s):"<<endl;
  Standard_Integer nba = 0;
  TCollection_AsciiString entry;
  Standard_Integer i ;
  for ( i = 1; i<= map.Extent(); ++i) {
    const Handle(TDF_Attribute)& att = map.FindKey(i);
    if (aFilter.IsKept(att)) {
      ++nba;
      anOS<<"# "<<i;
      if (att->Label().IsNull()) {
	anOS<<" (no label)"<<endl;
      }
      else {
	TDF_Tool::Entry(att->Label(),entry);
	anOS<<" (label: "<<entry<<")"<<endl;
      }
      att->ExtendedDump(anOS,aFilter,map); anOS<<endl;
    }
  }
  anOS<<endl<<nba<<" attribute";
  if (nba>1) anOS<<"s";
  anOS<<" dumped between "<<--i<<endl;
}


//=======================================================================
//function : ExtendedDeepDump
//purpose  : Internal method.
//=======================================================================

static void TDF_Tool_ExtendedDeepDump
(Standard_OStream& anOS,
 const TDF_Label& aLabel,
 const TDF_IDFilter& aFilter,
 TDF_AttributeIndexedMap& aMap)
{
  // Dumps the label.
  aLabel.ExtendedDump(anOS,aFilter,aMap);
  // Its children
  for (TDF_ChildIterator ChildIt(aLabel); ChildIt.More (); ChildIt.Next ()) {
    TDF_Tool_ExtendedDeepDump(anOS,ChildIt.Value(),aFilter,aMap);
  }
}