summaryrefslogtreecommitdiff
path: root/src/TDF/TDF_ClosureTool.cxx
blob: 935795d9feb686585b3447fffc11ca5822e6bd3a (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
// File:	TDF_ClosureTool.cxx
//      	-------------------
// Author:	DAUTRY Philippe
//		<fid@fox.paris1.matra-dtv.fr>
// Copyright:	Matra Datavision 1998

// Version:	0.0
// History:	Version	Date		Purpose
//		0.0	Sep  8 1998	Creation


#include <TDF_ClosureTool.ixx>

//#include <TDF_Reference.hxx>
#include <TDF_Attribute.hxx>
#include <TDF_AttributeIterator.hxx>
#include <TDF_AttributeMap.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDF_Label.hxx>
#include <TDF_LabelMap.hxx>
#include <TDF_ListIteratorOfLabelList.hxx>
#include <TDF_MapIteratorOfAttributeMap.hxx>
#include <TDF_MapIteratorOfLabelMap.hxx>

#include <Standard_TypeMismatch.hxx>

#define DeclareAndSpeedCast(V,T,Vdown) Handle(T) Vdown = *((Handle(T)*)& V)
#define DeclareConstAndSpeedCast(V,T,Vdown) const Handle(T)& Vdown = (Handle(T)&) V
#define SpeedCast(V,T,Vdown) Vdown = *((Handle(T)*)& V)


//=======================================================================
//function : Closure
//purpose  : Builds the transitive closure whithout attribute filter.
//=======================================================================

void TDF_ClosureTool::Closure
(const Handle(TDF_DataSet)& aDataSet) 
{
  TDF_IDFilter Filter(Standard_False); // "Keep all"
  TDF_ClosureMode Mode; // All modes are set to true.
  TDF_ClosureTool::Closure(aDataSet, Filter, Mode);
}


//=======================================================================
//function : Closure
//purpose  : Builds the transitive closure with an attribute filter.
//=======================================================================

void TDF_ClosureTool::Closure
(const Handle(TDF_DataSet)& aDataSet,
 const TDF_IDFilter& aFilter,
 const TDF_ClosureMode& aMode) 
{
  TDF_LabelMap&     labMap = aDataSet->Labels();
  TDF_AttributeMap& attMap = aDataSet->Attributes();
  TDF_LabelList&    rootLst  = aDataSet->Roots();

  // Memorizes the roots for future uses.
  rootLst.Clear();
  TDF_MapIteratorOfLabelMap labMItr(labMap);
  for (; labMItr.More(); labMItr.Next()) rootLst.Append(labMItr.Key());

  // Iterates on roots.
  TDF_ListIteratorOfLabelList labLItr(rootLst);
  for (; labLItr.More(); labLItr.Next()) {
    const TDF_Label& lab = labLItr.Value();
    if (lab.HasAttribute())
      TDF_ClosureTool::LabelAttributes(lab,labMap,attMap,aFilter,aMode);
    TDF_ClosureTool::Closure(lab,labMap,attMap,aFilter,aMode); 
  }
}


//=======================================================================
//function : Closure
//purpose  : Internal closure method.
//=======================================================================

void TDF_ClosureTool::Closure
(const TDF_Label& aLabel,
 TDF_LabelMap& aLabMap,
 TDF_AttributeMap& anAttMap,
 const TDF_IDFilter& aFilter,
 const TDF_ClosureMode& aMode) 
{
  TDF_Label upLab;
  for (TDF_ChildIterator childItr(aLabel,Standard_True);
       childItr.More();childItr.Next()){
    const TDF_Label& locLab = childItr.Value();
    // On ne peut faire cette optimisation car il faudrait d'abord
    // qu'aucun label donne comme Root ne soit fils d'un autre label root!
    if (locLab.HasAttribute()) { // && aLabMap.Add(locLab)) {
      aLabMap.Add(locLab);
      upLab = locLab.Father();
      while (aLabMap.Add(upLab)) upLab = upLab.Father();
      TDF_ClosureTool::LabelAttributes(locLab,aLabMap,anAttMap,aFilter,aMode);
    }
  }
}




//=======================================================================
//function : LabelAttributes
//purpose  : Internal method: adds the attributes to <aDataSet>.
//=======================================================================

void TDF_ClosureTool::LabelAttributes
(const TDF_Label& aLabel,
 TDF_LabelMap& aLabMap,
 TDF_AttributeMap& anAttMap,
 const TDF_IDFilter& aFilter,
 const TDF_ClosureMode& aMode) 
{
  Handle(TDF_DataSet) tmpDataSet;
  Standard_Boolean BindLabel;
  TDF_MapIteratorOfAttributeMap attMItr;
  TDF_MapIteratorOfLabelMap labMItr;

  // Attributes directly attached to the label.
  for (TDF_AttributeIterator attItr(aLabel); attItr.More(); attItr.Next()) {
    const Handle(TDF_Attribute) locAtt1 = attItr.Value();
    if (aFilter.IsKept(locAtt1)) {
      if (anAttMap.Add(locAtt1)) {
	// locAtt1 not yet in the map.

	// Labels & Attributes referenced by the attribute.
	tmpDataSet = new TDF_DataSet();
	if (aMode.References()) {
	  // 1 - The referenced attributes
	  // 1.1 - A referenced attribute has a label : adds the label;
	  // 1.2 - A referenced attribute has no label : adds the attribute;
	  // 2 - Adds the referenced labels.

	  locAtt1->References(tmpDataSet);

	  // 1 - The referenced attributes
	  const TDF_AttributeMap& tmpAttMap = tmpDataSet->Attributes();
	  for (attMItr.Initialize(tmpAttMap);
	       attMItr.More(); attMItr.Next()) {
	    const Handle(TDF_Attribute)& locAtt2 = attMItr.Key();
	    BindLabel = Standard_False;
	    if (!locAtt2.IsNull()) {
	      const TDF_Label& locLab2 = locAtt2->Label();
	      BindLabel = !locLab2.IsNull();
	      if (BindLabel) {
		// 1.1 - A referenced attribute has a label.
		if (aLabMap.Add(locLab2))
		  TDF_ClosureTool::Closure(locLab2,
					   aLabMap,anAttMap,aFilter,aMode);
	      }
	      else {
		// 1.2 - A referenced attribute has no label.
		// We suppose locAtt2 has no referenced attribute itself.
		anAttMap.Add(locAtt2);
	      }
	    }
	  }

	  // 2 - Adds the referenced labels.
	  const TDF_LabelMap& tmpLabMap = tmpDataSet->Labels();
	  for (labMItr.Initialize(tmpLabMap);
	       labMItr.More(); labMItr.Next()) {
	    const TDF_Label& locLab1 = labMItr.Key();
	    if (aLabMap.Add(locLab1))
	      TDF_ClosureTool::Closure(locLab1,
				       aLabMap,anAttMap,aFilter,aMode);
	  }
	}
      }
    }
  }
}