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