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
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.
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.
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.
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>
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>
41 //=======================================================================
44 //=======================================================================
46 Standard_Boolean StdPrs_ToolShadedShape::IsClosed(const TopoDS_Shape& aShape)
48 return aShape.Closed();
52 //=======================================================================
53 //function : Triangulation
55 //=======================================================================
57 Handle(Poly_Triangulation) StdPrs_ToolShadedShape::Triangulation
58 (const TopoDS_Face& aFace,
61 return BRep_Tool::Triangulation(aFace, loc);
65 //=======================================================================
68 //=======================================================================
70 void StdPrs_ToolShadedShape::Normal(const TopoDS_Face& aFace,
72 TColgp_Array1OfDir& Nor)
74 const Handle(Poly_Triangulation)& T = pc.Triangulation();
75 BRepAdaptor_Surface S;
76 Standard_Boolean hasUV = T->HasUVNodes();
80 TopoDS_Face zeroFace = TopoDS::Face(aFace.Located(TopLoc_Location()));
81 //take in face the surface location
83 //Handle(Geom_Surface) GS = BRep_Tool::Surface(aFace, l);
84 Handle(Geom_Surface) GS = BRep_Tool::Surface(zeroFace);
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]);
96 if (aFace.Orientation() == TopAbs_REVERSED) {
97 for( i = Nodes.Lower(); i <= Nodes.Upper(); i++) {
98 Nor.ChangeValue(i).Reverse();
104 else if (hasUV && !GS.IsNull()) {
105 Standard_Integer nbNormVal = T->NbNodes() * 3;
106 Handle(TShort_HArray1OfShortReal) Normals =
107 new TShort_HArray1OfShortReal(1, nbNormVal);
109 const TColgp_Array1OfPnt2d& UVNodes = T->UVNodes();
110 Standard_Real Tol = Precision::Confusion();
111 for (i = UVNodes.Lower(); i <= UVNodes.Upper(); i++) {
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();
118 gp_XYZ eqPlan(0, 0, 0);
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());
126 Standard_Real mod = vv.Modulus();
128 if(mod < Tol) continue;
133 modmax = eqPlan.Modulus();
134 if(modmax > Tol) Nor(i) = gp_Dir(eqPlan);
135 else Nor(i) = gp_Dir(0., 0., 1.);
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());
144 if (aFace.Orientation() == TopAbs_REVERSED) (Nor(i)).Reverse();
148 T->SetNormals(Normals);
151 Standard_Integer nbNormVal = T->NbNodes() * 3;
152 Handle(TShort_HArray1OfShortReal) Normals =
153 new TShort_HArray1OfShortReal(1, nbNormVal);
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();
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());
167 Standard_Real mod = vv.Modulus();
169 if(mod < Tol) continue;
174 Standard_Real modmax = eqPlan.Modulus();
176 if(modmax > Tol) Nor(i) = gp_Dir(eqPlan);
177 else Nor(i) = gp_Dir(0., 0., 1.);
179 Nor(i) = gp_Dir(eqPlan);
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());
186 if (aFace.Orientation() == TopAbs_REVERSED) (Nor(i)).Reverse();
190 T->SetNormals(Normals);