0025334: BRepOffsetAPI_MakeOffset algorithm crashes on some customer's shape
[occt.git] / src / BRepMAT2d / BRepMAT2d_BisectingLocus.cxx
1 // Created on: 1993-07-13
2 // Created by: Yves FRICAUD
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 # include <BRepMAT2d_BisectingLocus.ixx>
18
19 # include <MAT2d_Mat2d.hxx>
20 # include <MAT2d_Tool2d.hxx>
21 # include <MAT2d_Circuit.hxx>
22 # include <MAT2d_CutCurve.hxx>
23 # include <MAT2d_BiInt.hxx>
24 # include <MAT2d_SequenceOfSequenceOfGeometry.hxx>
25 # include <MAT_Graph.hxx>
26 # include <MAT_Arc.hxx>
27 # include <MAT_BasicElt.hxx>
28 # include <MAT_Node.hxx>
29 # include <MAT_Bisector.hxx>
30 # include <MAT_ListOfBisector.hxx>
31 # include <MAT_DataMapOfIntegerBasicElt.hxx>
32 # include <MAT_DataMapIteratorOfDataMapOfIntegerBasicElt.hxx>
33 # include <Geom2d_Curve.hxx>
34 # include <gp_Pnt2d.hxx>
35 # include <TColGeom2d_SequenceOfGeometry.hxx>
36 # include <Precision.hxx>
37
38 #include <Standard_OutOfRange.hxx>
39
40 static void CutSketch (MAT2d_SequenceOfSequenceOfGeometry&    Figure,
41                        MAT2d_DataMapOfBiIntInteger&           NbSect);
42
43
44 //=============================================================================
45 //function : BRepMAT2d_BisectingLocus
46 //purpose  : Constructeur vide.
47 //=============================================================================
48 BRepMAT2d_BisectingLocus::BRepMAT2d_BisectingLocus()
49 {
50 }
51
52
53 //=============================================================================
54 //function : Compute
55 //purpose  : Calcul de la carte des lieux bisecteurs sur le contour defini par
56 //           <anExplo>.
57 //=============================================================================
58 void BRepMAT2d_BisectingLocus::Compute(BRepMAT2d_Explorer&        anExplo,
59                                        const Standard_Integer IndexLine,
60                                        const MAT_Side         aSide,
61                                        const GeomAbs_JoinType aJoinType,
62                                        const Standard_Boolean IsOpenResult)
63 {
64   MAT2d_Mat2d                        TheMAT( IsOpenResult );
65   Handle(MAT_ListOfBisector)         TheRoots = new MAT_ListOfBisector();
66   MAT2d_SequenceOfSequenceOfGeometry Figure;
67   Standard_Integer                   i;
68
69   nbSect.Clear();
70   nbContours = anExplo.NumberOfContours();
71
72   //---------------------------------
73   // Lecture des donnees de anExplo.
74   //---------------------------------
75   for (i = 1; i <= anExplo.NumberOfContours(); i++) {
76     TColGeom2d_SequenceOfGeometry      Line;
77     Figure.Append(Line);
78     for (anExplo.Init(i); anExplo.More(); anExplo.Next()) {
79       Figure.ChangeValue(i).Append(anExplo.Value());
80     }
81   }
82
83   //-----------------------
84   // Decoupage des courbes.
85   //-----------------------
86   CutSketch(Figure,nbSect);
87
88   //----------------------------------------------------------
89   // Construction du circuit sur lequel est calcule la carte.
90   //----------------------------------------------------------
91   Handle(MAT2d_Circuit) ACircuit = new MAT2d_Circuit(aJoinType, IsOpenResult);
92 //  Modified by Sergey KHROMOV - Wed Mar  6 17:43:47 2002 Begin
93 //   ACircuit->Perform(Figure,IndexLine,(aSide == MAT_Left));
94   ACircuit->Perform(Figure,anExplo.GetIsClosed(), IndexLine,(aSide == MAT_Left));
95 //  Modified by Sergey KHROMOV - Wed Mar  6 17:43:48 2002 End
96
97   // -----------------------
98   // Initialistion du Tool.
99   // -----------------------
100   theTool.Sense(aSide);
101   theTool.SetJoinType(aJoinType);
102   theTool.InitItems(ACircuit);
103
104   // --------------------------------------------
105   // Initialisation et execution de l algorithme.
106   // --------------------------------------------
107   if (IsOpenResult)
108     TheMAT.CreateMatOpen(theTool);
109   else
110     TheMAT.CreateMat(theTool);
111
112   isDone = TheMAT.IsDone(); if (!isDone) return;
113
114   // ----------------------------------------------------------------
115   // Recuperation du resultat de l algorithme et creation du graphe.
116   // ----------------------------------------------------------------
117   for (TheMAT.Init(); TheMAT.More(); TheMAT.Next()) {
118     TheRoots->BackAdd(TheMAT.Bisector());
119   }
120
121   theGraph = new MAT_Graph();
122   theGraph->Perform(TheMAT.SemiInfinite(),
123                     TheRoots, 
124                     theTool.NumberOfItems(), 
125                     TheMAT.NumberOfBisectors());
126
127   //-----------------------------------------------------------------------
128   // Fusion des elements de base doubles si plusieurs lignes dans Exploset.
129   //-----------------------------------------------------------------------
130   if (anExplo.NumberOfContours() > 1) {
131     MAT_DataMapOfIntegerBasicElt NewMap;
132     Standard_Integer             IndexLast  = 1;
133
134     //-----------------------------------------------------------------------
135     // Construction de NewMap dont les elements sont ordonnes suivant les
136     // lignes du contour et qui ne contient pas d element dupliques.
137     // em meme temps fusion des arcs dupliques et mise a jour des noeuds.
138     //-----------------------------------------------------------------------
139     for ( i = 1; i <= anExplo.NumberOfContours(); i++) {
140       RenumerationAndFusion(i,
141                             theTool.Circuit()->LineLength(i),
142                             IndexLast,
143                             NewMap);
144     }
145
146     //-----------------------------------------------------------------------
147     // Chargement dans le graph de la nouvelle map.
148     // et compactage de la map des Arcs (ie  Elimination des trous du a la
149     // fusion d arcs ).et  de celle des Nodes.
150     //-----------------------------------------------------------------------
151     theGraph->ChangeBasicElts(NewMap);    
152     theGraph->CompactArcs();
153     theGraph->CompactNodes();
154   }
155 }
156
157 //=============================================================================
158 //function : RenumerationAndFusion
159 //purpose  :
160 //=============================================================================
161 void BRepMAT2d_BisectingLocus::RenumerationAndFusion
162   (const Standard_Integer              ILine,
163    const Standard_Integer              LengthLine,
164          Standard_Integer&             IndexLast,
165          MAT_DataMapOfIntegerBasicElt& NewMap)
166 {
167   Standard_Integer IndFirst;
168   Standard_Integer i,j;
169   Standard_Integer GeomIndexArc1,GeomIndexArc2,GeomIndexArc3,GeomIndexArc4;
170   Standard_Boolean MergeArc1,MergeArc2;
171
172   for ( i = 1; i <= LengthLine; i++) {
173     const TColStd_SequenceOfInteger& S = theTool.Circuit()->RefToEqui(ILine,i);
174
175     IndFirst = S.Value(1);
176     NewMap.Bind(IndexLast,theGraph->ChangeBasicElt(IndFirst));
177     IndexLast++;
178
179     for(j = 2; j <= S.Length(); j++){
180       theGraph->FusionOfBasicElts(IndFirst,
181                                   S.Value(j),
182                                   MergeArc1,
183                                   GeomIndexArc1,
184                                   GeomIndexArc2,
185                                   MergeArc2,
186                                   GeomIndexArc3,
187                                   GeomIndexArc4);
188       if(MergeArc1) {
189         theTool.BisecFusion(GeomIndexArc1,GeomIndexArc2);
190       }
191       if(MergeArc2) {
192         theTool.BisecFusion(GeomIndexArc3,GeomIndexArc4);
193       }
194     }
195   }
196 }
197
198 //=============================================================================
199 //function : IsDone
200 //Purpose  : 
201 //=============================================================================
202 Standard_Boolean BRepMAT2d_BisectingLocus::IsDone() const
203 {
204   return isDone;
205 }
206
207 //=============================================================================
208 //function : Graph
209 //
210 //=============================================================================
211 Handle(MAT_Graph) BRepMAT2d_BisectingLocus::Graph() const
212 {
213   return theGraph;
214 }
215
216 //=============================================================================
217 //function : NumberOfContours
218 //
219 //=============================================================================
220 Standard_Integer BRepMAT2d_BisectingLocus::NumberOfContours () const
221 {
222   return nbContours;
223 }
224
225 //=============================================================================
226 //function : NumberOfElts
227 //
228 //=============================================================================
229 Standard_Integer BRepMAT2d_BisectingLocus::NumberOfElts 
230  (const Standard_Integer IndLine) const
231 {
232   return theTool.Circuit()->LineLength(IndLine);
233 }
234
235 //=============================================================================
236 //function : NumberOfSect
237 //
238 //=============================================================================
239 Standard_Integer BRepMAT2d_BisectingLocus::NumberOfSections
240 (const Standard_Integer IndLine,
241  const Standard_Integer Index  ) 
242      const
243 {
244   MAT2d_BiInt B(IndLine,Index);
245   return nbSect(B);
246 }
247
248 //=============================================================================
249 //function : BasicElt
250 //
251 //=============================================================================
252 Handle(MAT_BasicElt) BRepMAT2d_BisectingLocus::BasicElt 
253        (const Standard_Integer IndLine,
254         const Standard_Integer Index  ) 
255      const
256 {
257   Standard_Integer i;
258   Standard_Integer Ind = Index;
259
260   for (i = 1 ; i < IndLine ; i++){
261     Ind = Ind + theTool.Circuit()->LineLength(i);
262   }
263   return theGraph->BasicElt(Ind);
264 }
265
266
267 //=============================================================================
268 //function : GeomBis
269 //
270 //=============================================================================
271 Bisector_Bisec  BRepMAT2d_BisectingLocus::GeomBis (const Handle(MAT_Arc)&  anArc,
272                                                      Standard_Boolean& Reverse) 
273 const 
274 {
275   Reverse = Standard_False;
276
277   Handle(Geom2d_Curve) Bis = theTool.GeomBis(anArc->GeomIndex()).Value();
278
279   if (Bis->FirstParameter() <= -Precision::Infinite()) {
280     Reverse = Standard_True;
281   }
282   else if (Bis->LastParameter() < Precision::Infinite()) {
283     gp_Pnt2d PF    = Bis->Value(Bis->FirstParameter());
284     gp_Pnt2d PL    = Bis->Value(Bis->LastParameter());
285     gp_Pnt2d PNode = GeomElt(anArc->FirstNode());
286     if (PNode.SquareDistance(PF) > PNode.SquareDistance(PL)) 
287       Reverse = Standard_True;
288   }
289   return theTool.GeomBis(anArc->GeomIndex());
290 }
291
292 //=============================================================================
293 //function : GeomElt
294 //
295 //=============================================================================
296 Handle(Geom2d_Geometry)  BRepMAT2d_BisectingLocus::GeomElt
297                            (const Handle(MAT_BasicElt)& aBasicElt) const
298 {
299   return  theTool.GeomElt(aBasicElt->GeomIndex());
300 }
301
302
303 //=============================================================================
304 //function : GeomElt
305 //
306 //=============================================================================
307 gp_Pnt2d  BRepMAT2d_BisectingLocus::GeomElt(const Handle(MAT_Node)& aNode) const
308 {
309   return theTool.GeomPnt(aNode->GeomIndex());
310 }
311
312
313 //=============================================================================
314 //function : CutSketch
315 //
316 //=============================================================================
317 static void CutSketch (MAT2d_SequenceOfSequenceOfGeometry&    Figure,
318                        MAT2d_DataMapOfBiIntInteger&           NbSect)
319 {
320   MAT2d_CutCurve   Cuter;
321   Standard_Integer i,j,k,ico;
322   Standard_Integer ICurveInit;
323   Standard_Integer NbSection;
324
325   for ( i = 1; i <= Figure.Length(); i++) {
326     TColGeom2d_SequenceOfGeometry& Contour = Figure.ChangeValue(i);  
327     ICurveInit = 0;
328
329     for ( j = 1; j <= Contour.Length(); j++) {
330       ICurveInit++;
331       Cuter.Perform(Handle(Geom2d_Curve)::DownCast(Contour.ChangeValue(j)));
332       NbSection = 1;
333       if (!Cuter.UnModified()) {
334         ico    = j;
335         NbSection = Cuter.NbCurves();
336         for ( k = 1; k <= NbSection; k++) {
337           Contour.InsertAfter(j,Cuter.Value(k));
338           j++;
339         }
340         Contour.Remove(ico);
341         j--;
342       }
343       MAT2d_BiInt B(i,ICurveInit);
344       NbSect.Bind(B,NbSection);
345     }
346   }
347 }  
348