0030675: Visualization - remove redundant proxy classes in hierarchy of PrsMgr_Presen...
[occt.git] / src / BRepCheck / BRepCheck_Vertex.cxx
1 // Created on: 1995-12-07
2 // Created by: Jacques GOUSSARD
3 // Copyright (c) 1995-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 <BRep_CurveRepresentation.hxx>
19 #include <BRep_GCurve.hxx>
20 #include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
21 #include <BRep_ListIteratorOfListOfPointRepresentation.hxx>
22 #include <BRep_ListOfCurveRepresentation.hxx>
23 #include <BRep_ListOfPointRepresentation.hxx>
24 #include <BRep_PointRepresentation.hxx>
25 #include <BRep_TEdge.hxx>
26 #include <BRep_TFace.hxx>
27 #include <BRep_Tool.hxx>
28 #include <BRep_TVertex.hxx>
29 #include <BRepCheck.hxx>
30 #include <BRepCheck_ListOfStatus.hxx>
31 #include <BRepCheck_Vertex.hxx>
32 #include <Geom2d_Curve.hxx>
33 #include <Geom_Curve.hxx>
34 #include <Geom_Surface.hxx>
35 #include <gp_Pnt2d.hxx>
36 #include <Standard_Type.hxx>
37 #include <TopExp_Explorer.hxx>
38 #include <TopoDS.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Iterator.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Vertex.hxx>
43
44 IMPLEMENT_STANDARD_RTTIEXT(BRepCheck_Vertex,BRepCheck_Result)
45
46 //=======================================================================
47 //function : BRepCheck_Vertex
48 //purpose  : 
49 //=======================================================================
50 BRepCheck_Vertex::BRepCheck_Vertex(const TopoDS_Vertex& V)
51 {
52   Init(V);
53 }
54
55
56
57 //=======================================================================
58 //function : Minimum
59 //purpose  : 
60 //=======================================================================
61
62 void BRepCheck_Vertex::Minimum()
63 {
64   if (!myMin) {
65     // checks the existence of a point 3D
66     BRepCheck_ListOfStatus thelist;
67     myMap.Bind(myShape, thelist);
68     myMap(myShape).Append(BRepCheck_NoError);
69     myMin = Standard_True;
70   }
71 }
72
73
74 //=======================================================================
75 //function : InContext
76 //purpose  : 
77 //=======================================================================
78
79 void BRepCheck_Vertex::InContext(const TopoDS_Shape& S)
80 {
81   if (myMap.IsBound(S)) {
82     return;
83   }
84   BRepCheck_ListOfStatus thelist;
85   myMap.Bind(S, thelist);
86
87 //  for (TopExp_Explorer exp(S,TopAbs_VERTEX); exp.More(); exp.Next()) {
88   TopExp_Explorer exp(S,TopAbs_VERTEX) ;
89   for ( ; exp.More(); exp.Next()) {
90     if (exp.Current().IsSame(myShape)) {
91       break;
92     }
93   }
94   if (!exp.More()) {
95     BRepCheck::Add(myMap(S),BRepCheck_SubshapeNotInShape);
96     return; // leaves
97   }
98
99
100   Handle(BRep_TVertex)& TV = *((Handle(BRep_TVertex)*) &myShape.TShape());
101   const gp_Pnt& prep = TV->Pnt();
102   gp_Pnt Controlp;
103
104   TopAbs_ShapeEnum styp = S.ShapeType();
105   switch (styp) {
106
107   case TopAbs_EDGE:
108     {
109       // Try to find the vertex on the edge
110       
111       const TopoDS_Edge& E = TopoDS::Edge(S);
112       TopoDS_Iterator itv(E.Oriented(TopAbs_FORWARD));
113       TopoDS_Vertex VFind;
114       Standard_Boolean multiple = Standard_False;
115       while (itv.More()) {
116         const TopoDS_Vertex& VF = TopoDS::Vertex(itv.Value());
117         if (itv.Value().IsSame(myShape)) {
118           if (VFind.IsNull()) {
119             VFind = VF;
120           }
121           else {
122             if ((VFind.Orientation() == TopAbs_FORWARD && 
123                  VF.Orientation() == TopAbs_REVERSED) ||
124                 (VFind.Orientation() == TopAbs_REVERSED &&
125                  VF.Orientation() == TopAbs_FORWARD)) {
126               // the vertex on the edge is at once F and R
127               multiple = Standard_True; 
128             }
129             if (VFind.Orientation() != TopAbs_FORWARD && 
130                 VFind.Orientation() != TopAbs_REVERSED) {
131               if (VF.Orientation() == TopAbs_FORWARD ||
132                   VF.Orientation() == TopAbs_REVERSED) {
133                 VFind = VF;
134               }
135             }
136           }
137         }
138         itv.Next();
139       }
140
141       // VFind is not null for sure
142       TopAbs_Orientation orv = VFind.Orientation();
143
144       Standard_Real Tol  = BRep_Tool::Tolerance(TopoDS::Vertex(myShape));
145       Tol = Max(Tol,BRep_Tool::Tolerance(E)); // to check
146       Tol *= Tol;
147
148       Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&E.TShape());
149       BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->Curves());
150       const TopLoc_Location& Eloc = E.Location();
151
152       BRep_ListIteratorOfListOfPointRepresentation itpr;
153       while (itcr.More()) {
154         // For each CurveRepresentation, the provided parameter is checked
155         const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
156         const TopLoc_Location& loc = cr->Location();
157         TopLoc_Location L = (Eloc * loc).Predivided(myShape.Location());
158
159         if (cr->IsCurve3D()) {
160           const Handle(Geom_Curve)& C = cr->Curve3D();
161           if (!C.IsNull()) { // edge non degenerated
162             itpr.Initialize(TV->Points());
163             while (itpr.More()) {
164               const Handle(BRep_PointRepresentation)& pr = itpr.Value();
165               if (pr->IsPointOnCurve(C,L)) {
166                 Controlp = C->Value(pr->Parameter());
167                 Controlp.Transform(L.Transformation());
168                 if (prep.SquareDistance(Controlp)> Tol) {
169                   BRepCheck::Add(myMap(S),BRepCheck_InvalidPointOnCurve);
170                 }
171               }
172               itpr.Next();
173             }
174             if (orv == TopAbs_FORWARD || orv == TopAbs_REVERSED) {
175               Handle(BRep_GCurve) GC (Handle(BRep_GCurve)::DownCast (cr));
176               if (orv == TopAbs_FORWARD || multiple) {
177                 Controlp = C->Value(GC->First());
178                 Controlp.Transform(L.Transformation());
179                 if (prep.SquareDistance(Controlp)> Tol) {
180                   BRepCheck::Add(myMap(S),BRepCheck_InvalidPointOnCurve);
181                 }
182               }
183               if (orv == TopAbs_REVERSED || multiple) {
184                 Controlp = C->Value(GC->Last());
185                 Controlp.Transform(L.Transformation());
186                 if (prep.SquareDistance(Controlp)> Tol) {
187                   BRepCheck::Add(myMap(S),BRepCheck_InvalidPointOnCurve);
188                 }
189               }
190             }
191           }
192         }
193         else if (cr->IsCurveOnSurface()) {
194           const Handle(Geom_Surface)& Su = cr->Surface();
195           const Handle(Geom2d_Curve)& PC = cr->PCurve();
196           Handle(Geom2d_Curve) PC2;
197           if (cr->IsCurveOnClosedSurface()) {
198             PC2 = cr->PCurve2();
199           }
200           itpr.Initialize(TV->Points());
201           while (itpr.More()) {
202             const Handle(BRep_PointRepresentation)& pr = itpr.Value();
203             if (pr->IsPointOnCurveOnSurface(PC,Su,L)) {
204               gp_Pnt2d p2d = PC->Value(pr->Parameter());
205               Controlp = Su->Value(p2d.X(),p2d.Y());
206               Controlp.Transform(L.Transformation());
207               if (prep.SquareDistance(Controlp)> Tol) {
208                 BRepCheck::Add(myMap(S),
209                                BRepCheck_InvalidPointOnCurveOnSurface);
210               }
211             }
212             if (!PC2.IsNull() && pr->IsPointOnCurveOnSurface(PC2,Su,L)) {
213               gp_Pnt2d p2d = PC2->Value(pr->Parameter());
214               Controlp = Su->Value(p2d.X(),p2d.Y());
215               Controlp.Transform(L.Transformation());
216               if (prep.SquareDistance(Controlp)> Tol) {
217                 BRepCheck::Add(myMap(S),
218                                BRepCheck_InvalidPointOnCurveOnSurface);
219               }
220             }
221             itpr.Next();
222           }
223         }
224         itcr.Next();
225       }
226       if (myMap(S).IsEmpty()) {
227         myMap(S).Append(BRepCheck_NoError);
228       }
229
230     }
231     break;
232
233   case TopAbs_FACE:
234     {
235
236       Handle(BRep_TFace)& TF = *((Handle(BRep_TFace)*) &S.TShape());
237       const TopLoc_Location& Floc = S.Location();
238       const TopLoc_Location& TFloc = TF->Location();
239       const Handle(Geom_Surface)& Su = TF->Surface();
240       TopLoc_Location L = (Floc * TFloc).Predivided(myShape.Location());
241
242       Standard_Real Tol  = BRep_Tool::Tolerance(TopoDS::Vertex(myShape));
243       Tol = Max(Tol,BRep_Tool::Tolerance(TopoDS::Face(S))); // to check
244       Tol *= Tol;
245
246       BRep_ListIteratorOfListOfPointRepresentation itpr(TV->Points());
247       while (itpr.More()) {
248         const Handle(BRep_PointRepresentation)& pr = itpr.Value();
249         if (pr->IsPointOnSurface(Su,L)) {
250           Controlp = Su->Value(pr->Parameter(),pr->Parameter2());
251           Controlp.Transform(L.Transformation());
252           if (prep.SquareDistance(Controlp)> Tol) {
253             BRepCheck::Add(myMap(S),BRepCheck_InvalidPointOnSurface);
254           }
255         }
256         itpr.Next();
257       }
258       if (myMap(S).IsEmpty()) {
259         myMap(S).Append(BRepCheck_NoError);
260       }
261     }
262
263   default:
264     break;
265
266   }
267
268 }
269
270
271 //=======================================================================
272 //function : Blind
273 //purpose  : 
274 //=======================================================================
275
276 void BRepCheck_Vertex::Blind()
277 {
278   if (myBlind) {
279     return;
280   }
281 //   modified by NIZHNY-MKK  Fri May  7 16:43:38 2004.BEGIN
282 //   The body of this function is removed because of its useless 
283 //   (see specification "Substitution existing set of evaluation DRAW commands to one").
284
285 //   Check all the representations  of the vertex. (i-e checks the TVertex
286 //   BRepCheck_ListOfStatus& lst = myMap(myShape);
287 //   lst.Clear(); // there was NoError...
288
289 //   Handle(BRep_TVertex)& TV = *((Handle(BRep_TVertex)*) &myShape.TShape());
290 //   const gp_Pnt& prep = TV->Pnt();
291 //   Standard_Real Tol  = BRep_Tool::Tolerance(TopoDS::Vertex(myShape));
292 //   Tol *= Tol;
293
294 //   gp_Pnt Controlp;
295 //   BRep_ListIteratorOfListOfPointRepresentation itpr(TV->Points());
296 //   BRepCheck_Status stat=BRepCheck_NoError;
297 //   while (itpr.More()) {
298 //     const Handle(BRep_PointRepresentation)& pr = itpr.Value();
299 //     const TopLoc_Location& loc = pr->Location();
300 //     if (pr->IsPointOnCurve()) {
301 //       Controlp = pr->Curve()->Value(pr->Parameter());
302 //       stat = BRepCheck_InvalidPointOnCurve;
303 //     }
304 //     else if (pr->IsPointOnCurveOnSurface()) {
305 //       gp_Pnt2d Puv = pr->PCurve()->Value(pr->Parameter());
306 //       Controlp = pr->Surface()->Value(Puv.X(),Puv.Y());
307 //       stat = BRepCheck_InvalidPointOnCurveOnSurface;
308 //     }
309 //     else if (pr->IsPointOnSurface()) {
310 //       Controlp = pr->Surface()->Value(pr->Parameter(),pr->Parameter2());
311 //       stat = BRepCheck_InvalidPointOnSurface;
312 //     }
313 //     Controlp.Transform(loc.Transformation());
314 //     if (prep.SquareDistance(Controlp) > Tol) {
315 //       BRepCheck::Add(lst,stat);
316 //     }
317 //     itpr.Next();
318 //   }
319
320 //   if (lst.IsEmpty()) {
321 //     lst.Append(BRepCheck_NoError);
322 //   }
323 // modified by NIZHNY-MKK  Fri May  7 16:43:45 2004.END
324   myBlind = Standard_True;
325 }
326
327
328 //=======================================================================
329 //function : Tolerance
330 //purpose  : 
331 //=======================================================================
332
333 Standard_Real BRepCheck_Vertex::Tolerance()
334 {
335
336   // Check all the representations  of the vertex. (i-e checks the TVertex
337   Handle(BRep_TVertex)& TV = *((Handle(BRep_TVertex)*) &myShape.TShape());
338   const gp_Pnt& prep = TV->Pnt();
339   Standard_Real Tol  = BRep_Tool::Tolerance(TopoDS::Vertex(myShape));
340   Tol *= Tol;
341
342   gp_Pnt Controlp;
343   Controlp = prep;
344   BRep_ListIteratorOfListOfPointRepresentation itpr(TV->Points());
345   while (itpr.More()) {
346     const Handle(BRep_PointRepresentation)& pr = itpr.Value();
347     const TopLoc_Location& loc = pr->Location();
348     if (pr->IsPointOnCurve()) {
349       if (!pr->Curve().IsNull())
350         Controlp = pr->Curve()->Value(pr->Parameter());
351     }
352     else if (pr->IsPointOnCurveOnSurface()) {
353       gp_Pnt2d Puv = pr->PCurve()->Value(pr->Parameter());
354       Controlp = pr->Surface()->Value(Puv.X(),Puv.Y());
355     }
356     else if (pr->IsPointOnSurface()) {
357       Controlp = pr->Surface()->Value(pr->Parameter(),pr->Parameter2());
358     }
359     Controlp.Transform(loc.Transformation());
360     if (prep.SquareDistance(Controlp) > Tol) {
361       Tol = prep.SquareDistance(Controlp);
362     }
363     itpr.Next();
364   }
365   return sqrt(Tol*1.05);
366 }
367
368