Adjusting test unstable testing cases in Debug mode for current state of OCCT
[occt.git] / src / StdPrs / StdPrs_ToolShadedShape.cxx
1 // Created on: 1993-10-27
2 // Created by: Jean-LOuis FRENKEL
3 // Copyright (c) 1993-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 <StdPrs_ToolShadedShape.ixx>
24 #include <Poly_Triangulation.hxx>
25 #include <TColgp_HArray1OfPnt.hxx>
26 #include <TColgp_Array1OfPnt.hxx>
27 #include <TColgp_Array1OfPnt2d.hxx>
28 #include <Poly_Connect.hxx>
29 #include <TopAbs_Orientation.hxx>
30 #include <GeomAbs_SurfaceType.hxx>
31 //#include <CSLib.hxx>
32 #include <GeomLib.hxx>
33 #include <gp_Vec.hxx>
34 #include <Precision.hxx>
35 #include <BRepAdaptor_Surface.hxx>
36 #include <BRep_Tool.hxx>
37 #include <TopLoc_Location.hxx>
38 #include <TShort_HArray1OfShortReal.hxx>
39 #include <TShort_Array1OfShortReal.hxx>
40
41 //=======================================================================
42 //function : IsClosed
43 //purpose  : 
44 //=======================================================================
45
46 Standard_Boolean StdPrs_ToolShadedShape::IsClosed(const TopoDS_Shape& aShape) 
47 {
48   return aShape.Closed();
49 }
50
51
52 //=======================================================================
53 //function : Triangulation
54 //purpose  : 
55 //=======================================================================
56
57 Handle(Poly_Triangulation) StdPrs_ToolShadedShape::Triangulation
58    (const TopoDS_Face& aFace,
59     TopLoc_Location&   loc)
60 {
61   return BRep_Tool::Triangulation(aFace, loc);
62 }
63
64
65 //=======================================================================
66 //function : Normal
67 //purpose  : 
68 //=======================================================================
69
70 void StdPrs_ToolShadedShape::Normal(const TopoDS_Face&  aFace,
71                                     Poly_Connect&       pc,
72                                     TColgp_Array1OfDir& Nor)
73 {
74   const Handle(Poly_Triangulation)& T = pc.Triangulation();
75   BRepAdaptor_Surface S;
76   Standard_Boolean hasUV = T->HasUVNodes();
77   Standard_Integer i;
78   TopLoc_Location l;
79   // move to zero
80   TopoDS_Face zeroFace = TopoDS::Face(aFace.Located(TopLoc_Location()));
81   //take in face the surface location
82   
83   //Handle(Geom_Surface) GS = BRep_Tool::Surface(aFace, l);
84   Handle(Geom_Surface) GS = BRep_Tool::Surface(zeroFace);
85
86   if(T->HasNormals()) {
87     const TColgp_Array1OfPnt& Nodes = T->Nodes();
88     const TShort_Array1OfShortReal& Normals = T->Normals();
89     const Standard_ShortReal * arrN = &(Normals.Value(Normals.Lower()));
90     for( i = Nodes.Lower(); i <= Nodes.Upper(); i++) {
91       Standard_Integer in = 3*(i-Nodes.Lower());
92       gp_Dir N(arrN[in + 0], arrN[in + 1], arrN[in + 2]);
93       Nor(i) = N;
94     }
95
96     if (aFace.Orientation() == TopAbs_REVERSED) {
97       for( i = Nodes.Lower(); i <= Nodes.Upper(); i++) {
98         Nor.ChangeValue(i).Reverse();
99       }
100     }
101   
102     
103   }
104   else if (hasUV && !GS.IsNull()) {
105     Standard_Integer nbNormVal  = T->NbNodes() * 3; 
106     Handle(TShort_HArray1OfShortReal) Normals = 
107       new TShort_HArray1OfShortReal(1, nbNormVal);
108
109     const TColgp_Array1OfPnt2d& UVNodes = T->UVNodes();
110     Standard_Real Tol = Precision::Confusion();
111     for (i = UVNodes.Lower(); i <= UVNodes.Upper(); i++) {
112
113       if(GeomLib::NormEstim(GS, UVNodes(i), Tol, Nor(i)) > 1) {
114         const TColgp_Array1OfPnt& Nodes = T->Nodes();
115         Standard_Integer n[3];
116         const Poly_Array1OfTriangle& triangles = T->Triangles();
117
118         gp_XYZ eqPlan(0, 0, 0);
119         
120         Standard_Real modmax = 0.;
121         for (pc.Initialize(i);  pc.More(); pc.Next()) {
122           triangles(pc.Value()).Get(n[0], n[1], n[2]);
123           gp_XYZ v1(Nodes(n[1]).Coord()-Nodes(n[0]).Coord());
124           gp_XYZ v2(Nodes(n[2]).Coord()-Nodes(n[1]).Coord());
125           gp_XYZ vv = v1^v2;
126           Standard_Real mod = vv.Modulus();
127
128           if(mod < Tol) continue;
129
130           eqPlan += vv/mod;
131         }
132
133         modmax = eqPlan.Modulus();
134         if(modmax > Tol) Nor(i) = gp_Dir(eqPlan);
135         else Nor(i) = gp_Dir(0., 0., 1.);
136
137       }
138
139       Standard_Integer j = (i - UVNodes.Lower()) * 3;
140       Normals->SetValue(j + 1, (Standard_ShortReal)Nor(i).X());
141       Normals->SetValue(j + 2, (Standard_ShortReal)Nor(i).Y());
142       Normals->SetValue(j + 3, (Standard_ShortReal)Nor(i).Z());
143
144       if (aFace.Orientation() == TopAbs_REVERSED) (Nor(i)).Reverse();
145
146     }
147
148     T->SetNormals(Normals);    
149   }
150   else {
151     Standard_Integer nbNormVal  = T->NbNodes() * 3; 
152     Handle(TShort_HArray1OfShortReal) Normals = 
153       new TShort_HArray1OfShortReal(1, nbNormVal);
154
155     const TColgp_Array1OfPnt& Nodes = T->Nodes();
156     Standard_Integer n[3];
157     const Poly_Array1OfTriangle& triangles = T->Triangles();
158     Standard_Real Tol = Precision::Confusion();
159
160     for (i = Nodes.Lower(); i <= Nodes.Upper(); i++) {
161       gp_XYZ eqPlan(0, 0, 0);
162       for (pc.Initialize(i);  pc.More(); pc.Next()) {
163         triangles(pc.Value()).Get(n[0], n[1], n[2]);
164         gp_XYZ v1(Nodes(n[1]).Coord()-Nodes(n[0]).Coord());
165         gp_XYZ v2(Nodes(n[2]).Coord()-Nodes(n[1]).Coord());
166         gp_XYZ vv = v1^v2;
167         Standard_Real mod = vv.Modulus();
168
169         if(mod < Tol) continue;
170
171         eqPlan += vv/mod;
172       }
173
174       Standard_Real modmax = eqPlan.Modulus();
175
176       if(modmax > Tol) Nor(i) = gp_Dir(eqPlan);
177       else Nor(i) = gp_Dir(0., 0., 1.);
178
179       Nor(i) = gp_Dir(eqPlan);
180
181       Standard_Integer j = (i - Nodes.Lower()) * 3;
182       Normals->SetValue(j + 1, (Standard_ShortReal)Nor(i).X());
183       Normals->SetValue(j + 2, (Standard_ShortReal)Nor(i).Y());
184       Normals->SetValue(j + 3, (Standard_ShortReal)Nor(i).Z());
185
186       if (aFace.Orientation() == TopAbs_REVERSED) (Nor(i)).Reverse();
187
188     }
189
190     T->SetNormals(Normals);
191   }
192
193   
194 }
195