0027555: Visualization, AIS_Shape - own deviation coefficient change is not considere...
[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//=======================================================================
7fd59977 134//function : Normal
9447f912 135//purpose :
7fd59977 136//=======================================================================
5ad8c033 137void StdPrs_ToolTriangulatedShape::Normal (const TopoDS_Face& theFace,
138 Poly_Connect& thePolyConnect,
139 TColgp_Array1OfDir& theNormals)
7fd59977 140{
9447f912 141 const Handle(Poly_Triangulation)& aPolyTri = thePolyConnect.Triangulation();
142 const TColgp_Array1OfPnt& aNodes = aPolyTri->Nodes();
143 if (aPolyTri->HasNormals())
144 {
145 // normals pre-computed in triangulation structure
146 const TShort_Array1OfShortReal& aNormals = aPolyTri->Normals();
147 const Standard_ShortReal* aNormArr = &(aNormals.Value (aNormals.Lower()));
148 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
149 {
150 const Standard_Integer anId = 3 * (aNodeIter - aNodes.Lower());
151 const gp_Dir aNorm (aNormArr[anId + 0],
152 aNormArr[anId + 1],
153 aNormArr[anId + 2]);
154 theNormals (aNodeIter) = aNorm;
7fd59977 155 }
156
9447f912 157 if (theFace.Orientation() == TopAbs_REVERSED)
158 {
159 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
160 {
161 theNormals.ChangeValue (aNodeIter).Reverse();
7fd59977 162 }
163 }
9447f912 164 return;
7fd59977 165 }
7fd59977 166
9447f912 167 // take in face the surface location
168 const TopoDS_Face aZeroFace = TopoDS::Face (theFace.Located (TopLoc_Location()));
169 Handle(Geom_Surface) aSurf = BRep_Tool::Surface (aZeroFace);
170 const Standard_Real aTol = Precision::Confusion();
171 Handle(TShort_HArray1OfShortReal) aNormals = new TShort_HArray1OfShortReal (1, aPolyTri->NbNodes() * 3);
172 const Poly_Array1OfTriangle& aTriangles = aPolyTri->Triangles();
173 const TColgp_Array1OfPnt2d* aNodesUV = aPolyTri->HasUVNodes() && !aSurf.IsNull()
174 ? &aPolyTri->UVNodes()
175 : NULL;
176 Standard_Integer aTri[3];
177 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
178 {
179 // try to retrieve normal from real surface first, when UV coordinates are available
180 if (aNodesUV == NULL
181 || GeomLib::NormEstim (aSurf, aNodesUV->Value (aNodeIter), aTol, theNormals (aNodeIter)) > 1)
182 {
183 // compute flat normals
184 gp_XYZ eqPlan (0.0, 0.0, 0.0);
185 for (thePolyConnect.Initialize (aNodeIter); thePolyConnect.More(); thePolyConnect.Next())
186 {
187 aTriangles (thePolyConnect.Value()).Get (aTri[0], aTri[1], aTri[2]);
188 const gp_XYZ v1 (aNodes (aTri[1]).Coord() - aNodes (aTri[0]).Coord());
189 const gp_XYZ v2 (aNodes (aTri[2]).Coord() - aNodes (aTri[1]).Coord());
190 const gp_XYZ vv = v1 ^ v2;
191 const Standard_Real aMod = vv.Modulus();
192 if (aMod >= aTol)
193 {
194 eqPlan += vv / aMod;
195 }
7fd59977 196 }
9447f912 197 const Standard_Real aModMax = eqPlan.Modulus();
198 theNormals (aNodeIter) = (aModMax > aTol) ? gp_Dir (eqPlan) : gp::DZ();
7fd59977 199 }
200
9447f912 201 const Standard_Integer anId = (aNodeIter - aNodes.Lower()) * 3;
202 aNormals->SetValue (anId + 1, (Standard_ShortReal )theNormals (aNodeIter).X());
203 aNormals->SetValue (anId + 2, (Standard_ShortReal )theNormals (aNodeIter).Y());
204 aNormals->SetValue (anId + 3, (Standard_ShortReal )theNormals (aNodeIter).Z());
7fd59977 205 }
9447f912 206 aPolyTri->SetNormals (aNormals);
7fd59977 207
9447f912 208 if (theFace.Orientation() == TopAbs_REVERSED)
209 {
210 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
211 {
212 theNormals.ChangeValue (aNodeIter).Reverse();
7fd59977 213 }
7fd59977 214 }
7fd59977 215}
5ad8c033 216
217//=======================================================================
218//function : IsTessellated
219//purpose :
220//=======================================================================
221Standard_Boolean StdPrs_ToolTriangulatedShape::IsTessellated (const TopoDS_Shape& theShape,
222 const Handle(Prs3d_Drawer)& theDrawer)
223{
224 return BRepTools::Triangulation (theShape, Prs3d::GetDeflection (theShape, theDrawer));
225}
226
227// =======================================================================
228// function : Tessellate
229// purpose :
230// =======================================================================
231Standard_Boolean StdPrs_ToolTriangulatedShape::Tessellate (const TopoDS_Shape& theShape,
232 const Handle(Prs3d_Drawer)& theDrawer)
233{
234 Standard_Boolean wasRecomputed = Standard_False;
235 // Check if it is possible to avoid unnecessary recomputation of shape triangulation
236 if (IsTessellated (theShape, theDrawer))
237 {
238 return wasRecomputed;
239 }
240
241 Standard_Real aDeflection = Prs3d::GetDeflection (theShape, theDrawer);
242
243 // retrieve meshing tool from Factory
244 Handle(BRepMesh_DiscretRoot) aMeshAlgo = BRepMesh_DiscretFactory::Get().Discret (theShape,
245 aDeflection,
246 theDrawer->HLRAngle());
247 if (!aMeshAlgo.IsNull())
248 {
249 aMeshAlgo->Perform();
250 wasRecomputed = Standard_True;
251 }
252
253 return wasRecomputed;
254}
83b0f13a 255
256// =======================================================================
257// function : ClearOnOwnDeflectionChange
258// purpose :
259// =======================================================================
260void StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (const TopoDS_Shape& theShape,
261 const Handle(Prs3d_Drawer)& theDrawer,
262 const Standard_Boolean theToResetCoeff)
263{
264 if (!theDrawer->IsAutoTriangulation()
265 || theShape.IsNull())
266 {
267 return;
268 }
269
270 const Standard_Boolean isOwnDeviationAngle = theDrawer->HasOwnDeviationAngle();
271 const Standard_Boolean isOwnDeviationCoefficient = theDrawer->HasOwnDeviationCoefficient();
272 const Standard_Real anAngleNew = theDrawer->DeviationAngle();
273 const Standard_Real anAnglePrev = theDrawer->PreviousDeviationAngle();
274 const Standard_Real aCoeffNew = theDrawer->DeviationCoefficient();
275 const Standard_Real aCoeffPrev = theDrawer->PreviousDeviationCoefficient();
276 if ((!isOwnDeviationAngle || Abs (anAngleNew - anAnglePrev) <= Precision::Angular())
277 && (!isOwnDeviationCoefficient || Abs (aCoeffNew - aCoeffPrev) <= Precision::Confusion()))
278 {
279 return;
280 }
281
282 BRepTools::Clean (theShape);
283 if (theToResetCoeff)
284 {
285 theDrawer->UpdatePreviousDeviationAngle();
286 theDrawer->UpdatePreviousDeviationCoefficient();
287 }
288}