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