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