1ed1a7ea763ea78eb86d61ac24d3cd3f30b7c64d
[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 Standard_Boolean IsOpenResult)
62 {
63   MAT2d_Mat2d                        TheMAT;
64   Handle(MAT_ListOfBisector)         TheRoots = new MAT_ListOfBisector();
65   MAT2d_SequenceOfSequenceOfGeometry Figure;
66   Standard_Integer                   i;
67
68   nbSect.Clear();
69   nbContours = anExplo.NumberOfContours();
70
71   //---------------------------------
72   // Lecture des donnees de anExplo.
73   //---------------------------------
74   for (i = 1; i <= anExplo.NumberOfContours(); i++) {
75     TColGeom2d_SequenceOfGeometry      Line;
76     Figure.Append(Line);
77     for (anExplo.Init(i); anExplo.More(); anExplo.Next()) {
78       Figure.ChangeValue(i).Append(anExplo.Value());
79     }
80   }
81
82   //-----------------------
83   // Decoupage des courbes.
84   //-----------------------
85   CutSketch(Figure,nbSect);
86
87   //----------------------------------------------------------
88   // Construction du circuit sur lequel est calcule la carte.
89   //----------------------------------------------------------
90   Handle(MAT2d_Circuit) ACircuit = new MAT2d_Circuit(IsOpenResult);
91 //  Modified by Sergey KHROMOV - Wed Mar  6 17:43:47 2002 Begin
92 //   ACircuit->Perform(Figure,IndexLine,(aSide == MAT_Left));
93   ACircuit->Perform(Figure,anExplo.GetIsClosed(), IndexLine,(aSide == MAT_Left));
94 //  Modified by Sergey KHROMOV - Wed Mar  6 17:43:48 2002 End
95
96   // -----------------------
97   // Initialistion du Tool.
98   // -----------------------
99   theTool.Sense(aSide);
100   theTool.InitItems(ACircuit);
101
102   // --------------------------------------------
103   // Initialisation et execution de l algorithme.
104   // --------------------------------------------
105   TheMAT.CreateMat(theTool);
106
107   isDone = TheMAT.IsDone(); if (!isDone) return;
108
109   // ----------------------------------------------------------------
110   // Recuperation du resultat de l algorithme et creation du graphe.
111   // ----------------------------------------------------------------
112   for (TheMAT.Init(); TheMAT.More(); TheMAT.Next()) {
113     TheRoots->BackAdd(TheMAT.Bisector());
114   }
115
116   theGraph = new MAT_Graph();
117   theGraph->Perform(TheMAT.SemiInfinite(),
118                     TheRoots, 
119                     theTool.NumberOfItems(), 
120                     TheMAT.NumberOfBisectors());
121
122   //-----------------------------------------------------------------------
123   // Fusion des elements de base doubles si plusieurs lignes dans Exploset.
124   //-----------------------------------------------------------------------
125   if (anExplo.NumberOfContours() > 1) {
126     MAT_DataMapOfIntegerBasicElt NewMap;
127     Standard_Integer             IndexLast  = 1;
128
129     //-----------------------------------------------------------------------
130     // Construction de NewMap dont les elements sont ordonnes suivant les
131     // lignes du contour et qui ne contient pas d element dupliques.
132     // em meme temps fusion des arcs dupliques et mise a jour des noeuds.
133     //-----------------------------------------------------------------------
134     for ( i = 1; i <= anExplo.NumberOfContours(); i++) {
135       RenumerationAndFusion(i,
136                             theTool.Circuit()->LineLength(i),
137                             IndexLast,
138                             NewMap);
139     }
140
141     //-----------------------------------------------------------------------
142     // Chargement dans le graph de la nouvelle map.
143     // et compactage de la map des Arcs (ie  Elimination des trous du a la
144     // fusion d arcs ).et  de celle des Nodes.
145     //-----------------------------------------------------------------------
146     theGraph->ChangeBasicElts(NewMap);    
147     theGraph->CompactArcs();
148     theGraph->CompactNodes();
149   }
150 }
151
152 //=============================================================================
153 //function : RenumerationAndFusion
154 //purpose  :
155 //=============================================================================
156 void BRepMAT2d_BisectingLocus::RenumerationAndFusion
157   (const Standard_Integer              ILine,
158    const Standard_Integer              LengthLine,
159          Standard_Integer&             IndexLast,
160          MAT_DataMapOfIntegerBasicElt& NewMap)
161 {
162   Standard_Integer IndFirst;
163   Standard_Integer i,j;
164   Standard_Integer GeomIndexArc1,GeomIndexArc2,GeomIndexArc3,GeomIndexArc4;
165   Standard_Boolean MergeArc1,MergeArc2;
166
167   for ( i = 1; i <= LengthLine; i++) {
168     const TColStd_SequenceOfInteger& S = theTool.Circuit()->RefToEqui(ILine,i);
169
170     IndFirst = S.Value(1);
171     NewMap.Bind(IndexLast,theGraph->ChangeBasicElt(IndFirst));
172     IndexLast++;
173
174     for(j = 2; j <= S.Length(); j++){
175       theGraph->FusionOfBasicElts(IndFirst,
176                                   S.Value(j),
177                                   MergeArc1,
178                                   GeomIndexArc1,
179                                   GeomIndexArc2,
180                                   MergeArc2,
181                                   GeomIndexArc3,
182                                   GeomIndexArc4);
183       if(MergeArc1) {
184         theTool.BisecFusion(GeomIndexArc1,GeomIndexArc2);
185       }
186       if(MergeArc2) {
187         theTool.BisecFusion(GeomIndexArc3,GeomIndexArc4);
188       }
189     }
190   }
191 }
192
193 //=============================================================================
194 //function : IsDone
195 //Purpose  : 
196 //=============================================================================
197 Standard_Boolean BRepMAT2d_BisectingLocus::IsDone() const
198 {
199   return isDone;
200 }
201
202 //=============================================================================
203 //function : Graph
204 //
205 //=============================================================================
206 Handle(MAT_Graph) BRepMAT2d_BisectingLocus::Graph() const
207 {
208   return theGraph;
209 }
210
211 //=============================================================================
212 //function : NumberOfContours
213 //
214 //=============================================================================
215 Standard_Integer BRepMAT2d_BisectingLocus::NumberOfContours () const
216 {
217   return nbContours;
218 }
219
220 //=============================================================================
221 //function : NumberOfElts
222 //
223 //=============================================================================
224 Standard_Integer BRepMAT2d_BisectingLocus::NumberOfElts 
225  (const Standard_Integer IndLine) const
226 {
227   return theTool.Circuit()->LineLength(IndLine);
228 }
229
230 //=============================================================================
231 //function : NumberOfSect
232 //
233 //=============================================================================
234 Standard_Integer BRepMAT2d_BisectingLocus::NumberOfSections
235 (const Standard_Integer IndLine,
236  const Standard_Integer Index  ) 
237      const
238 {
239   MAT2d_BiInt B(IndLine,Index);
240   return nbSect(B);
241 }
242
243 //=============================================================================
244 //function : BasicElt
245 //
246 //=============================================================================
247 Handle(MAT_BasicElt) BRepMAT2d_BisectingLocus::BasicElt 
248        (const Standard_Integer IndLine,
249         const Standard_Integer Index  ) 
250      const
251 {
252   Standard_Integer i;
253   Standard_Integer Ind = Index;
254
255   for (i = 1 ; i < IndLine ; i++){
256     Ind = Ind + theTool.Circuit()->LineLength(i);
257   }
258   return theGraph->BasicElt(Ind);
259 }
260
261
262 //=============================================================================
263 //function : GeomBis
264 //
265 //=============================================================================
266 Bisector_Bisec  BRepMAT2d_BisectingLocus::GeomBis (const Handle(MAT_Arc)&  anArc,
267                                                      Standard_Boolean& Reverse) 
268 const 
269 {
270   Reverse = Standard_False;
271
272   Handle(Geom2d_Curve) Bis = theTool.GeomBis(anArc->GeomIndex()).Value();
273
274   if (Bis->FirstParameter() <= -Precision::Infinite()) {
275     Reverse = Standard_True;
276   }
277   else if (Bis->LastParameter() < Precision::Infinite()) {
278     gp_Pnt2d PF    = Bis->Value(Bis->FirstParameter());
279     gp_Pnt2d PL    = Bis->Value(Bis->LastParameter());
280     gp_Pnt2d PNode = GeomElt(anArc->FirstNode());
281     if (PNode.SquareDistance(PF) > PNode.SquareDistance(PL)) 
282       Reverse = Standard_True;
283   }
284   return theTool.GeomBis(anArc->GeomIndex());
285 }
286
287 //=============================================================================
288 //function : GeomElt
289 //
290 //=============================================================================
291 Handle(Geom2d_Geometry)  BRepMAT2d_BisectingLocus::GeomElt
292                            (const Handle(MAT_BasicElt)& aBasicElt) const
293 {
294   return  theTool.GeomElt(aBasicElt->GeomIndex());
295 }
296
297
298 //=============================================================================
299 //function : GeomElt
300 //
301 //=============================================================================
302 gp_Pnt2d  BRepMAT2d_BisectingLocus::GeomElt(const Handle(MAT_Node)& aNode) const
303 {
304   return theTool.GeomPnt(aNode->GeomIndex());
305 }
306
307
308 //=============================================================================
309 //function : CutSketch
310 //
311 //=============================================================================
312 static void CutSketch (MAT2d_SequenceOfSequenceOfGeometry&    Figure,
313                        MAT2d_DataMapOfBiIntInteger&           NbSect)
314 {
315   MAT2d_CutCurve   Cuter;
316   Standard_Integer i,j,k,ico;
317   Standard_Integer ICurveInit;
318   Standard_Integer NbSection;
319
320   for ( i = 1; i <= Figure.Length(); i++) {
321     TColGeom2d_SequenceOfGeometry& Contour = Figure.ChangeValue(i);  
322     ICurveInit = 0;
323
324     for ( j = 1; j <= Contour.Length(); j++) {
325       ICurveInit++;
326       Cuter.Perform(Handle(Geom2d_Curve)::DownCast(Contour.ChangeValue(j)));
327       NbSection = 1;
328       if (!Cuter.UnModified()) {
329         ico    = j;
330         NbSection = Cuter.NbCurves();
331         for ( k = 1; k <= NbSection; k++) {
332           Contour.InsertAfter(j,Cuter.Value(k));
333           j++;
334         }
335         Contour.Remove(ico);
336         j--;
337       }
338       MAT2d_BiInt B(i,ICurveInit);
339       NbSect.Bind(B,NbSection);
340     }
341   }
342 }  
343