summaryrefslogtreecommitdiff
path: root/src/IntCurvesFace/IntCurvesFace_ShapeIntersector.cxx
blob: bd5d95c691f89b2d4a6fa447d5b5090daadfd07d (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
// File:	IntCurvesFace_ShapeIntersector.cxx
// Created:	Tue Jan 27 16:58:07 1998
// Author:	Laurent BUCHARD
//		<lbr@cracbox.paris1.matra-dtv.fr>


#include <IntCurvesFace_ShapeIntersector.ixx>

#include <TopExp_Explorer.hxx>
#include <TopoDS.hxx>
#include <IntCurvesFace_Intersector.hxx>
#include <Bnd_BoundSortBox.hxx>
#include <Bnd_HArray1OfBox.hxx>
#include <ElCLib.hxx>
#include <TColStd_ListOfInteger.hxx>
#include <TColStd_ListIteratorOfListOfInteger.hxx>

//-- ================================================================================
IntCurvesFace_ShapeIntersector::IntCurvesFace_ShapeIntersector() {
  nbfaces=0;
  done=Standard_False;
  PtrJetons=NULL;
  PtrJetonsIndex=NULL;
}
//-- ================================================================================
void IntCurvesFace_ShapeIntersector::Load(const TopoDS_Shape& shape,
					  const Standard_Real tol) { 
  PtrJetons=NULL;
  PtrJetonsIndex=NULL;
  if(nbfaces) { 
    Destroy();
  }
  TopExp_Explorer Ex;
  Standard_Integer i;
  for(nbfaces=0,i=0,Ex.Init(shape,TopAbs_FACE); Ex.More(); i++,Ex.Next()) { 
    nbfaces++;
    TopoDS_Face currentface = TopoDS::Face(Ex.Current());
    PtrIntersector.Append((void *)(new IntCurvesFace_Intersector(currentface,tol)));
  }  
}
//-- ================================================================================
void IntCurvesFace_ShapeIntersector::Destroy() { 
  if(PtrJetons) { 
    delete ((Standard_Integer *)PtrJetons);
    PtrJetons=NULL;
  }
  if(PtrJetonsIndex) { 
    delete ((Standard_Integer *)PtrJetonsIndex);
    PtrJetonsIndex=NULL;
  }
  for(Standard_Integer i=1; i<=nbfaces; i++) { 
    IntCurvesFace_Intersector *Ptr = (IntCurvesFace_Intersector *)PtrIntersector.ChangeValue(i);
    delete Ptr;
  }
  done=Standard_False;
  nbfaces=0;
  PtrIntersector.Clear(); 
  IndexPt.Clear();  IndexFace.Clear();   IndexIntPnt.Clear();IndexPar.Clear();
}
//-- ================================================================================
void IntCurvesFace_ShapeIntersector::Perform(const gp_Lin& L,
					     const Standard_Real ParMin,
					     const Standard_Real ParMax) { 
  done = Standard_False;
  for(Standard_Integer i=1; i<=nbfaces; i++) { 
    IntCurvesFace_Intersector *Ptr = (IntCurvesFace_Intersector *)PtrIntersector.ChangeValue(i);
    Ptr->Perform(L,ParMin,ParMax);
  }
  SortResult();
}
//-- ================================================================================
void IntCurvesFace_ShapeIntersector::PerformNearest(const gp_Lin& L,
						    const Standard_Real ParMin,
						    const Standard_Real _ParMax) { 

  Standard_Integer i;
  Standard_Integer* _PtrJetons=(Standard_Integer *)PtrJetons;
  Standard_Integer* _PtrJetonsIndex=(Standard_Integer *)PtrJetonsIndex;


  if(nbfaces>2) { 
    if(PtrJetons==NULL) { 
      PtrJetons      = (void *) new Standard_Integer [nbfaces];
      PtrJetonsIndex = (void *) new Standard_Integer [nbfaces];
      Standard_Integer *Ptr =(Standard_Integer *)PtrJetons;
      Standard_Integer *PtrI=(Standard_Integer *)PtrJetonsIndex;
      for(i=0;i<nbfaces;i++) { 
	Ptr[i]=0;
	PtrI[i]=i+1;
      }
      _PtrJetons=(Standard_Integer *)PtrJetons;
      _PtrJetonsIndex=(Standard_Integer *)PtrJetonsIndex;
    }
  }

  
  Standard_Integer Indexface=-1;  
  Standard_Real ParMax=_ParMax;
  

  done = Standard_False;
  for(Standard_Integer ii=1; ii<=nbfaces; ii++) { 
    if(_PtrJetons) { 
      i=_PtrJetonsIndex[ii-1];
    }
    else { 
      i=ii;
    }
      
    IntCurvesFace_Intersector *Ptr = (IntCurvesFace_Intersector *)PtrIntersector.ChangeValue(i);
    if(ParMin<ParMax) { 
      Ptr->Perform(L,ParMin,ParMax);
      if(Ptr->IsDone()) {
	Standard_Integer n=Ptr->NbPnt();
	for(Standard_Integer j=1;j<=n;j++) { 
	  Standard_Real w=Ptr->WParameter(j);
	  if(w<ParMax) { 
	    ParMax=w;	    
	    Indexface=ii-1;
	  }
	}
      }
      else { 
	done = Standard_False;
	return;
      }
    }
  }
  if(PtrJetons && Indexface>=0) { 
    _PtrJetons[Indexface]++;

    Standard_Integer im1;
    for(im1=Indexface-1,i=Indexface; i>=1 && _PtrJetons[i]>_PtrJetons[i-1]; i--,im1--) { 
      Standard_Integer t=_PtrJetonsIndex[i];
      _PtrJetonsIndex[i]=_PtrJetonsIndex[im1];
      _PtrJetonsIndex[im1]=t;
      t=_PtrJetons[i];
      _PtrJetons[i]=_PtrJetons[im1];
      _PtrJetons[im1]=t;
    }
    //--for(Standard_Integer dd=0; dd<nbfaces;dd++) { if(_PtrJetons[dd]) { printf("\n<%3d %3d %3d>",dd,_PtrJetons[dd],_PtrJetonsIndex[dd]); }  } 
    //--printf("\n");
  }
  SortResult();
}
//-- ================================================================================
void IntCurvesFace_ShapeIntersector::Perform(const Handle(Adaptor3d_HCurve)& HCu,
					     const Standard_Real ParMin,
					     const Standard_Real ParMax) { 
  done = Standard_False;
  for(Standard_Integer i=1; i<=nbfaces; i++) { 
    IntCurvesFace_Intersector *Ptr = (IntCurvesFace_Intersector *)PtrIntersector.ChangeValue(i);
    Ptr->Perform(HCu,ParMin,ParMax);
  }
  SortResult();
}
//-- ================================================================================
//-- PtrIntersector   : Sequence d addresses 
//-- IndexPt          : 1 2 3 .... n   points avant le tri 
//-- IndexFace        : Numero de la face (de l intersector) du point IndexPt(i)
//-- IndexIntPnt      : Numero du point  IndexPt(i) dans l'intersector IndexFace(IndexPt(i))
//-- IndexPar         : W parameter du point IndexPt(i)
//--
//-- En resume, pour chaque point indice par K = IndexPt(i) on a 
//--      * la face a laquelle il appartient                  : IndexFace(K)
//--      * le numero du point dans l'intersecteur FaceCurve  : IndexIntPnt(K)
//--      * le parametre W du point sur la courbe             : IndexPar(K)
//--
//-- SortResult Trie les points par ordre croissant de W 
//-- (remet a jour le tableau d index TabPt(.))
//-- 
//-- ================================================================================
void IntCurvesFace_ShapeIntersector::SortResult() { 
  done = Standard_True;
  Standard_Integer nbpnt=0;
  IndexPt.Clear();  IndexFace.Clear();  IndexIntPnt.Clear();IndexPar.Clear();
  //-- -----------------------------------------------------
  //-- r e c u p e r a t i o n   d e s   r e s u l t a t s  
  //--
  for(Standard_Integer f=1; f<=nbfaces; f++) { 
    IntCurvesFace_Intersector *Ptr = (IntCurvesFace_Intersector *)PtrIntersector.ChangeValue(f);
    if(Ptr->IsDone()) { 
      Standard_Integer n=Ptr->NbPnt();
      for(Standard_Integer j=1;j<=n;j++) { 
	IndexPt.Append(++nbpnt);
	IndexFace.Append(f);
	IndexIntPnt.Append(j);
	IndexPar.Append(Ptr->WParameter(j));
      }
    }
    else { 
      done = Standard_False;
      return;
    }
  }
  //-- -----------------------------------------------------
  //-- t r i   s e l o n   l  e   p a r a m e t r e   w 
  //--
  Standard_Boolean triok;
  do { 
    triok=Standard_True;
    for(Standard_Integer ind0=1;ind0<nbpnt;ind0++) { 
      Standard_Integer ind  =IndexPt(ind0);
      Standard_Integer indp1=IndexPt(ind0+1);
      if(IndexPar(ind) > IndexPar(indp1)) { 
	IndexPt(ind0)  =indp1;
	IndexPt(ind0+1)=ind;
	triok=Standard_False;
      }
    }
  }
  while(triok==Standard_False);
}
//-- ================================================================================
//-- Creation le 28 jan 98
//--