0028469: Visualization, StdPrs_ShadedShape - do not create redundant copy of normal...
[occt.git] / src / StdPrs / StdPrs_ToolTriangulatedShape.cxx
CommitLineData
b311480e 1// Created on: 1993-10-27
2// Created by: Jean-LOuis FRENKEL
3// Copyright (c) 1993-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
b311480e 16
5ad8c033 17#include <StdPrs_ToolTriangulatedShape.hxx>
9447f912 18
5ad8c033 19#include <BRepMesh_DiscretFactory.hxx>
20#include <BRepMesh_DiscretRoot.hxx>
21#include <BRepTools.hxx>
9447f912 22#include <BRep_Tool.hxx>
9447f912 23#include <GeomAbs_SurfaceType.hxx>
24#include <GeomLib.hxx>
5ad8c033 25#include <gp_XYZ.hxx>
9447f912 26#include <Poly_Connect.hxx>
7fd59977 27#include <Poly_Triangulation.hxx>
9447f912 28#include <Precision.hxx>
5ad8c033 29#include <Prs3d.hxx>
30#include <Prs3d_Drawer.hxx>
7fd59977 31#include <TColgp_Array1OfPnt.hxx>
32#include <TColgp_Array1OfPnt2d.hxx>
7fd59977 33#include <TopAbs_Orientation.hxx>
7fd59977 34#include <TopLoc_Location.hxx>
35#include <TShort_HArray1OfShortReal.hxx>
36#include <TShort_Array1OfShortReal.hxx>
fc9b36d6 37#include <TopExp_Explorer.hxx>
38#include <TopoDS.hxx>
5ad8c033 39#include <TopoDS_Face.hxx>
7fd59977 40
4769a395 41//=======================================================================
5ad8c033 42//function : IsTriangulated
4769a395 43//purpose :
44//=======================================================================
5ad8c033 45Standard_Boolean StdPrs_ToolTriangulatedShape::IsTriangulated (const TopoDS_Shape& theShape)
3b1817a9 46{
4769a395 47 TopLoc_Location aLocDummy;
48 for (TopExp_Explorer aFaceIter (theShape, TopAbs_FACE); aFaceIter.More(); aFaceIter.Next())
3b1817a9 49 {
4769a395 50 const TopoDS_Face& aFace = TopoDS::Face (aFaceIter.Current());
51 const Handle(Poly_Triangulation)& aTri = BRep_Tool::Triangulation (aFace, aLocDummy);
52 if (aTri.IsNull())
3b1817a9 53 {
4769a395 54 return Standard_False;
3b1817a9 55 }
3b1817a9 56 }
4769a395 57 return Standard_True;
3b1817a9 58}
59
7fd59977 60//=======================================================================
61//function : IsClosed
9447f912 62//purpose :
7fd59977 63//=======================================================================
5ad8c033 64Standard_Boolean StdPrs_ToolTriangulatedShape::IsClosed (const TopoDS_Shape& theShape)
7fd59977 65{
3b1817a9 66 if (theShape.IsNull())
67 {
68 return Standard_True;
69 }
70
71 switch (theShape.ShapeType())
72 {
73 case TopAbs_COMPOUND:
74 case TopAbs_COMPSOLID:
75 default:
76 {
77 // check that compound consists of closed solids
78 for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
79 {
80 const TopoDS_Shape& aShape = anIter.Value();
81 if (!IsClosed (aShape))
82 {
83 return Standard_False;
84 }
85 }
86 return Standard_True;
87 }
88 case TopAbs_SOLID:
89 {
4769a395 90 // Check for non-manifold topology first of all:
91 // have to use BRep_Tool::IsClosed() because it checks the face connectivity
92 // inside the shape
93 if (!BRep_Tool::IsClosed (theShape))
94 return Standard_False;
95
3b1817a9 96 for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
97 {
98 const TopoDS_Shape& aShape = anIter.Value();
99 if (aShape.IsNull())
100 {
101 continue;
102 }
103
4769a395 104 if (aShape.ShapeType() == TopAbs_FACE)
3b1817a9 105 {
106 // invalid solid
107 return Standard_False;
108 }
4769a395 109 else if (!IsTriangulated (aShape))
3b1817a9 110 {
111 // mesh contains holes
112 return Standard_False;
113 }
114 }
115 return Standard_True;
116 }
117 case TopAbs_SHELL:
118 case TopAbs_FACE:
119 {
120 // free faces / shell are not allowed
121 return Standard_False;
122 }
123 case TopAbs_WIRE:
124 case TopAbs_EDGE:
125 case TopAbs_VERTEX:
126 {
127 // ignore
128 return Standard_True;
129 }
130 }
7fd59977 131}
132
7fd59977 133//=======================================================================
134//function : Normal
9447f912 135//purpose :
7fd59977 136//=======================================================================
f4064435 137void StdPrs_ToolTriangulatedShape::Normal (const TopoDS_Face& theFace,
138 Poly_Connect& thePolyConnect)
7fd59977 139{
9447f912 140 const Handle(Poly_Triangulation)& aPolyTri = thePolyConnect.Triangulation();
f4064435 141 if (aPolyTri.IsNull()
142 || aPolyTri->HasNormals())
9447f912 143 {
9447f912 144 return;
7fd59977 145 }
7fd59977 146
9447f912 147 // take in face the surface location
148 const TopoDS_Face aZeroFace = TopoDS::Face (theFace.Located (TopLoc_Location()));
149 Handle(Geom_Surface) aSurf = BRep_Tool::Surface (aZeroFace);
150 const Standard_Real aTol = Precision::Confusion();
151 Handle(TShort_HArray1OfShortReal) aNormals = new TShort_HArray1OfShortReal (1, aPolyTri->NbNodes() * 3);
152 const Poly_Array1OfTriangle& aTriangles = aPolyTri->Triangles();
153 const TColgp_Array1OfPnt2d* aNodesUV = aPolyTri->HasUVNodes() && !aSurf.IsNull()
154 ? &aPolyTri->UVNodes()
155 : NULL;
156 Standard_Integer aTri[3];
f4064435 157 const TColgp_Array1OfPnt& aNodes = aPolyTri->Nodes();
158 gp_Dir aNorm;
9447f912 159 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
160 {
161 // try to retrieve normal from real surface first, when UV coordinates are available
162 if (aNodesUV == NULL
f4064435 163 || GeomLib::NormEstim (aSurf, aNodesUV->Value (aNodeIter), aTol, aNorm) > 1)
9447f912 164 {
165 // compute flat normals
166 gp_XYZ eqPlan (0.0, 0.0, 0.0);
167 for (thePolyConnect.Initialize (aNodeIter); thePolyConnect.More(); thePolyConnect.Next())
168 {
169 aTriangles (thePolyConnect.Value()).Get (aTri[0], aTri[1], aTri[2]);
170 const gp_XYZ v1 (aNodes (aTri[1]).Coord() - aNodes (aTri[0]).Coord());
171 const gp_XYZ v2 (aNodes (aTri[2]).Coord() - aNodes (aTri[1]).Coord());
172 const gp_XYZ vv = v1 ^ v2;
173 const Standard_Real aMod = vv.Modulus();
174 if (aMod >= aTol)
175 {
176 eqPlan += vv / aMod;
177 }
7fd59977 178 }
9447f912 179 const Standard_Real aModMax = eqPlan.Modulus();
f4064435 180 aNorm = (aModMax > aTol) ? gp_Dir (eqPlan) : gp::DZ();
7fd59977 181 }
182
9447f912 183 const Standard_Integer anId = (aNodeIter - aNodes.Lower()) * 3;
f4064435 184 aNormals->SetValue (anId + 1, (Standard_ShortReal )aNorm.X());
185 aNormals->SetValue (anId + 2, (Standard_ShortReal )aNorm.Y());
186 aNormals->SetValue (anId + 3, (Standard_ShortReal )aNorm.Z());
7fd59977 187 }
9447f912 188 aPolyTri->SetNormals (aNormals);
f4064435 189}
190
191//=======================================================================
192//function : Normal
193//purpose :
194//=======================================================================
195void StdPrs_ToolTriangulatedShape::Normal (const TopoDS_Face& theFace,
196 Poly_Connect& thePolyConnect,
197 TColgp_Array1OfDir& theNormals)
198{
199 const Handle(Poly_Triangulation)& aPolyTri = thePolyConnect.Triangulation();
200 if (!aPolyTri->HasNormals())
201 {
202 Normal (theFace, thePolyConnect);
203 }
204
205 const TColgp_Array1OfPnt& aNodes = aPolyTri->Nodes();
206 const TShort_Array1OfShortReal& aNormals = aPolyTri->Normals();
207 const Standard_ShortReal* aNormArr = &aNormals.First();
208 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
209 {
210 const Standard_Integer anId = 3 * (aNodeIter - aNodes.Lower());
211 const gp_Dir aNorm (aNormArr[anId + 0],
212 aNormArr[anId + 1],
213 aNormArr[anId + 2]);
214 theNormals (aNodeIter) = aNorm;
215 }
7fd59977 216
9447f912 217 if (theFace.Orientation() == TopAbs_REVERSED)
218 {
219 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
220 {
221 theNormals.ChangeValue (aNodeIter).Reverse();
7fd59977 222 }
7fd59977 223 }
7fd59977 224}
5ad8c033 225
226//=======================================================================
227//function : IsTessellated
228//purpose :
229//=======================================================================
230Standard_Boolean StdPrs_ToolTriangulatedShape::IsTessellated (const TopoDS_Shape& theShape,
231 const Handle(Prs3d_Drawer)& theDrawer)
232{
233 return BRepTools::Triangulation (theShape, Prs3d::GetDeflection (theShape, theDrawer));
234}
235
236// =======================================================================
237// function : Tessellate
238// purpose :
239// =======================================================================
240Standard_Boolean StdPrs_ToolTriangulatedShape::Tessellate (const TopoDS_Shape& theShape,
241 const Handle(Prs3d_Drawer)& theDrawer)
242{
243 Standard_Boolean wasRecomputed = Standard_False;
244 // Check if it is possible to avoid unnecessary recomputation of shape triangulation
245 if (IsTessellated (theShape, theDrawer))
246 {
247 return wasRecomputed;
248 }
249
250 Standard_Real aDeflection = Prs3d::GetDeflection (theShape, theDrawer);
251
252 // retrieve meshing tool from Factory
253 Handle(BRepMesh_DiscretRoot) aMeshAlgo = BRepMesh_DiscretFactory::Get().Discret (theShape,
254 aDeflection,
255 theDrawer->HLRAngle());
256 if (!aMeshAlgo.IsNull())
257 {
258 aMeshAlgo->Perform();
259 wasRecomputed = Standard_True;
260 }
261
262 return wasRecomputed;
263}
83b0f13a 264
265// =======================================================================
266// function : ClearOnOwnDeflectionChange
267// purpose :
268// =======================================================================
269void StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (const TopoDS_Shape& theShape,
270 const Handle(Prs3d_Drawer)& theDrawer,
271 const Standard_Boolean theToResetCoeff)
272{
273 if (!theDrawer->IsAutoTriangulation()
274 || theShape.IsNull())
275 {
276 return;
277 }
278
279 const Standard_Boolean isOwnDeviationAngle = theDrawer->HasOwnDeviationAngle();
280 const Standard_Boolean isOwnDeviationCoefficient = theDrawer->HasOwnDeviationCoefficient();
281 const Standard_Real anAngleNew = theDrawer->DeviationAngle();
282 const Standard_Real anAnglePrev = theDrawer->PreviousDeviationAngle();
283 const Standard_Real aCoeffNew = theDrawer->DeviationCoefficient();
284 const Standard_Real aCoeffPrev = theDrawer->PreviousDeviationCoefficient();
285 if ((!isOwnDeviationAngle || Abs (anAngleNew - anAnglePrev) <= Precision::Angular())
286 && (!isOwnDeviationCoefficient || Abs (aCoeffNew - aCoeffPrev) <= Precision::Confusion()))
287 {
288 return;
289 }
290
291 BRepTools::Clean (theShape);
292 if (theToResetCoeff)
293 {
294 theDrawer->UpdatePreviousDeviationAngle();
295 theDrawer->UpdatePreviousDeviationCoefficient();
296 }
297}