0030686: Visualization, SelectMgr_ViewerSelector - sorting issues of transformation...
[occt.git] / src / TopOpeBRepDS / TopOpeBRepDS_TOOL.cxx
CommitLineData
b311480e 1// Created on: 1999-01-25
2// Created by: Xuan PHAM PHU
3// Copyright (c) 1999-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
42cf5bc1 17
7fd59977 18#include <BRep_Tool.hxx>
42cf5bc1 19#include <TopExp_Explorer.hxx>
20#include <TopoDS.hxx>
21#include <TopoDS_Edge.hxx>
22#include <TopoDS_Shape.hxx>
7fd59977 23#include <TopOpeBRepDS.hxx>
42cf5bc1 24#include <TopOpeBRepDS_DataMapOfShapeListOfShapeOn1State.hxx>
7fd59977 25#include <TopOpeBRepDS_define.hxx>
42cf5bc1 26#include <TopOpeBRepDS_HDataStructure.hxx>
7fd59977 27#include <TopOpeBRepDS_ListOfShapeOn1State.hxx>
42cf5bc1 28#include <TopOpeBRepDS_ProcessInterferencesTool.hxx>
ec357c5c 29#include <TopOpeBRepDS_ShapeShapeInterference.hxx>
42cf5bc1 30#include <TopOpeBRepDS_TOOL.hxx>
31#include <TopOpeBRepTool_EXPORT.hxx>
32#include <TopOpeBRepTool_TOOL.hxx>
7fd59977 33
7fd59977 34#define M_REVERSED(O) (O == TopAbs_REVERSED)
35
36static void FDS_sortGb(const Handle(TopOpeBRepDS_HDataStructure)& HDS,const TopOpeBRepDS_ListOfInterference& LI, TopOpeBRepDS_ListOfInterference& LIGb0,TopOpeBRepDS_ListOfInterference& LIGb1,TopOpeBRepDS_ListOfInterference& LIGbsd)
37{
38 const TopOpeBRepDS_DataStructure& BDS = HDS->DS();
39 LIGb0.Clear(); LIGb1.Clear(); LIGbsd.Clear();
40 TopOpeBRepDS_ListIteratorOfListOfInterference it(LI);
41 for (; it.More(); it.Next()){
42 const Handle(TopOpeBRepDS_Interference)& I = it.Value();
43 Handle(TopOpeBRepDS_ShapeShapeInterference) SSI = Handle(TopOpeBRepDS_ShapeShapeInterference)::DownCast(I);
44 if (SSI.IsNull()) {LIGb0.Append(I);continue;}
45
46 Standard_Boolean gb1 = SSI->GBound();
47 Standard_Integer G = I->Geometry();
48 Standard_Boolean hsd = HDS->HasSameDomain(BDS.Shape(G));
49 if (hsd) {LIGbsd.Append(I);continue;}
50
51 if (gb1) LIGb1.Append(I);
52 else LIGb0.Append(I);
53 }//it(LI)
54}
55
56//=======================================================================
57//function : EShareG
58//purpose :
59//=======================================================================
60
61#define FORWARD (1)
62#define REVERSED (2)
63#define INTERNAL (3)
64#define EXTERNAL (4)
65#define CLOSING (5)
66Standard_Integer TopOpeBRepDS_TOOL::EShareG(const Handle(TopOpeBRepDS_HDataStructure)& HDS,const TopoDS_Edge& E,TopTools_ListOfShape& lEsd)
67{
68 lEsd.Clear();
69 Standard_Boolean dgE = BRep_Tool::Degenerated(E);
70 if (dgE) {
71 Standard_Boolean hsd = HDS->HasSameDomain(E);
72 if (!hsd) return 0;
73 TopTools_ListIteratorOfListOfShape itsd(HDS->SameDomain(E));
74 for (; itsd.More(); itsd.Next()) lEsd.Append(itsd.Value());
75 return lEsd.Extent();
76 }
77
78 const TopOpeBRepDS_DataStructure& BDS = HDS->DS();
79 const TopOpeBRepDS_ListOfInterference& LI = BDS.ShapeInterferences(E);
80 TopOpeBRepDS_ListOfInterference LII; FDS_copy(LI,LII);
81 TopOpeBRepDS_ListOfInterference L1d; Standard_Integer n1d = FUN_selectTRASHAinterference(LII,TopAbs_EDGE,L1d);
82 if (n1d == 0) return 0;
83
84 TopTools_MapOfShape mapesd;
85 TopOpeBRepDS_ListOfInterference l1gb0,l1gb1,l1gbsd; FDS_sortGb(HDS,L1d,l1gb0,l1gb1,l1gbsd);
7fd59977 86 TopOpeBRepDS_ListIteratorOfListOfInterference it0(l1gb0);
87 for (; it0.More(); it0.Next()) mapesd.Add(BDS.Shape(it0.Value()->Support()));
88
89 TopOpeBRepDS_ListIteratorOfListOfInterference it1(l1gb1);
90 for (; it1.More(); it1.Next()) mapesd.Add(BDS.Shape(it1.Value()->Support()));
91
92 TopOpeBRepDS_ListIteratorOfListOfInterference itsd(l1gbsd);
93 for (; itsd.More(); itsd.Next()) {
94 const Handle(TopOpeBRepDS_Interference)& I = itsd.Value();
95 const TopoDS_Edge& Esd = TopoDS::Edge(BDS.Shape(I->Support()));
96 Standard_Boolean isb = mapesd.Contains(Esd);
97 if (isb) continue;
98
99 Standard_Integer G = I->Geometry();
100 const TopoDS_Vertex& vG = TopoDS::Vertex(BDS.Shape(G));
101 TopoDS_Vertex vsd; Standard_Boolean ok = FUN_ds_getoov(vG,BDS,vsd);
536a3cb8 102 if (!ok) continue;
7fd59977 103 Standard_Boolean Gb1 = Handle(TopOpeBRepDS_ShapeShapeInterference)::DownCast(I)->GBound();
104 TopoDS_Vertex vE = Gb1 ? vG : vsd;
105 TopoDS_Vertex vEsd = Gb1 ? vsd : vG;
106
107 Standard_Integer ovE; gp_Vec tgE;
108 ok = TopOpeBRepTool_TOOL::TgINSIDE(vE,E,tgE,ovE);
109 if (!ok) continue;
110 Standard_Integer ovEsd; gp_Vec tgEsd;
111 ok = TopOpeBRepTool_TOOL::TgINSIDE(vEsd,Esd,tgEsd,ovEsd);
112 if (!ok) continue;
113 Standard_Boolean inE = (ovE == CLOSING)||(ovE == INTERNAL);
114 Standard_Boolean inEsd = (ovEsd == CLOSING)||(ovEsd == INTERNAL);
115 if (inE || inEsd) {mapesd.Add(Esd); continue;}
116 Standard_Real dot = gp_Dir(tgE).Dot(gp_Dir(tgEsd));
117 if (dot > 0.) mapesd.Add(Esd);
118 }
119 TopTools_MapIteratorOfMapOfShape itm(mapesd);
120 for (; itm.More(); itm.Next()) lEsd.Append(itm.Key());
121 return (lEsd.Extent());
122}
123
124
125
126//=======================================================================
127//function : ShareG
128//purpose :
129//=======================================================================
130
131Standard_Boolean TopOpeBRepDS_TOOL::ShareG(const Handle(TopOpeBRepDS_HDataStructure)& HDS, const Standard_Integer i1, const Standard_Integer i2)
132{
133 const TopoDS_Shape& s1 = HDS->Shape(i1);
134 const TopoDS_Shape& s2 = HDS->Shape(i2);
135
136 Standard_Boolean hsd1 = HDS->HasSameDomain(s1);
137 if (!hsd1) return Standard_False;
138 TopTools_ListIteratorOfListOfShape it1(HDS->SameDomain(s1));
139 for (; it1.More(); it1.Next()){
140 Standard_Boolean same = it1.Value().IsSame(s2);
141 if (!same) continue;
142 return Standard_True;
143 }
144 return Standard_False;
145}
146
147
148
149//=======================================================================
150//function : GetEsd
151//purpose :
152//=======================================================================
153
154Standard_Boolean TopOpeBRepDS_TOOL::GetEsd(const Handle(TopOpeBRepDS_HDataStructure)& HDS,
155 const TopoDS_Shape& S, const Standard_Integer ie, Standard_Integer& iesd)
156{
157 // recall : method ::SameDomain(s) returns an iterator on the list of shapes
158 // sdm to s (ie actually sharing geometric domain with s)
159 iesd = 0;
160 TopTools_MapOfShape mesdS;
161 TopExp_Explorer ex(S, TopAbs_EDGE);
162 for (; ex.More(); ex.Next()){
163 const TopoDS_Shape& e = ex.Current();
164 Standard_Boolean hs = HDS->HasShape(e);
165 if (!hs) continue;
166 Standard_Boolean hsd = HDS->HasSameDomain(e);
167 if (!hsd) continue;
168 mesdS.Add(e);
169// TopTools_ListIteratorOfListOfShape itt(HDS->SameDomain(e));
170// for (; itt.More(); itt.Next()) mesdS.Add(itt.Value());
171 }
172
173 TopTools_ListIteratorOfListOfShape it(HDS->SameDomain(HDS->Shape(ie)));
174 for (; it.More(); it.Next()){
175 const TopoDS_Shape& esd = it.Value();
176 Standard_Boolean isb = mesdS.Contains(esd);
177 if (!isb) continue;
178 iesd = HDS->Shape(esd);
179 return Standard_True;
180 }
181 return Standard_False;
182}
183
184//=======================================================================
185//function : ShareSplitON
186//purpose :
187//=======================================================================
188
189Standard_Boolean TopOpeBRepDS_TOOL::ShareSplitON(const Handle(TopOpeBRepDS_HDataStructure)& HDS,
190 const TopOpeBRepDS_DataMapOfShapeListOfShapeOn1State& MEspON,
191 const Standard_Integer i1, const Standard_Integer i2, TopoDS_Shape& spON)
192{
193 spON.Nullify();
194 Standard_Boolean shareg = TopOpeBRepDS_TOOL::ShareG(HDS,i1,i2);
195 if (!shareg) return Standard_False;
196
197 const TopoDS_Shape& s1 = HDS->Shape(i1);
198 const TopoDS_Shape& s2 = HDS->Shape(i2);
199
200 const TopOpeBRepDS_ListOfShapeOn1State& los1 = MEspON.Find(s1);
201 Standard_Boolean issp = los1.IsSplit();
202 if (!issp) return Standard_False;
203 const TopTools_ListOfShape& lsp1 = los1.ListOnState();
204 Standard_Integer nsp1 = lsp1.Extent();
205 if (nsp1 == 0) return Standard_False;
206 TopTools_MapOfShape mesp1; // map of splits on of <s1>
207 TopTools_ListIteratorOfListOfShape it(lsp1);
208 for (; it.More(); it.Next()) mesp1.Add(it.Value());
209
210 const TopOpeBRepDS_ListOfShapeOn1State& los2 = MEspON.Find(s2);
211 Standard_Boolean issp2 = los2.IsSplit();
212 if (!issp2) return Standard_False;
213 const TopTools_ListOfShape& lsp2 = los2.ListOnState();
214 Standard_Integer nsp2 = lsp2.Extent();
215 if (nsp2 == 0) return Standard_False;
216
217 it.Initialize(lsp2);
218 for (; it.More(); it.Next()) {
219 const TopoDS_Shape& esp = it.Value();
220 Standard_Boolean isb = mesp1.Contains(esp);
221 if (!isb) continue;
222 spON = esp; return Standard_True;
223 }
224 return Standard_False;
225}
226
227
228
229//=======================================================================
230//function : GetConfig
231//purpose : returns relative geometric config
232//=======================================================================
233#define SAMEORIENTED (1)
234#define DIFFORIENTED (2)
235Standard_Boolean TopOpeBRepDS_TOOL::GetConfig(const Handle(TopOpeBRepDS_HDataStructure)& HDS,
236 const TopOpeBRepDS_DataMapOfShapeListOfShapeOn1State& MEspON,
237 const Standard_Integer ie,const Standard_Integer iesd,
238 Standard_Integer& config)
239{
240 config = 0;
241 Standard_Boolean shareg = TopOpeBRepDS_TOOL::ShareG(HDS,ie,iesd);
242 if (!shareg) return Standard_False;
243
244 const TopoDS_Edge& e = TopoDS::Edge(HDS->Shape(ie)); TopAbs_Orientation oe = e.Orientation();
245 const TopoDS_Edge& esd = TopoDS::Edge(HDS->Shape(iesd)); TopAbs_Orientation oesd = esd.Orientation();
246 TopOpeBRepDS_Config conf = HDS->SameDomainOrientation(e); Standard_Boolean unsh = (conf == TopOpeBRepDS_UNSHGEOMETRY);
247 TopOpeBRepDS_Config confsd = HDS->SameDomainOrientation(esd); Standard_Boolean unshsd = (confsd == TopOpeBRepDS_UNSHGEOMETRY);
248 if (!unsh && !unshsd) {
249 Standard_Boolean sameori = (conf == confsd);
250 if (M_REVERSED(oe)) sameori = !sameori;
251 if (M_REVERSED(oesd)) sameori = !sameori;
252 config = sameori ? SAMEORIENTED : DIFFORIENTED;
253 return Standard_True;
254 }
255
256 TopoDS_Shape eON; shareg = TopOpeBRepDS_TOOL::ShareSplitON(HDS,MEspON,ie,iesd,eON);
257 if (!shareg) return Standard_False;
258
259 Standard_Real f,l; FUN_tool_bounds(TopoDS::Edge(eON),f,l);
260 Standard_Real x = 0.45678; Standard_Real parON = (1-x)*f+x*l;
261 Standard_Real tole = BRep_Tool::Tolerance(TopoDS::Edge(e));
262 Standard_Real pare; Standard_Boolean ok = FUN_tool_parE(TopoDS::Edge(eON),parON, e,pare, tole);
263 if (!ok) return Standard_False;
264 Standard_Real tolesd = BRep_Tool::Tolerance(TopoDS::Edge(esd));
265 Standard_Real paresd; ok = FUN_tool_parE(TopoDS::Edge(eON),parON, esd,paresd, tolesd);
266 if (!ok) return Standard_False;
267 Standard_Boolean so; ok = FUN_tool_curvesSO(e,pare,esd,paresd,so);
268 if (!ok) return Standard_False;
269 config = (so)? SAMEORIENTED : DIFFORIENTED;
270 return Standard_True;
271}
272