1 // Copyright (c) 2017-2019 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <RWMesh_FaceIterator.hxx>
16 #include <BRepAdaptor_Surface.hxx>
17 #include <BRep_Tool.hxx>
20 #include <XCAFDoc_ShapeTool.hxx>
21 #include <XCAFPrs.hxx>
23 // =======================================================================
24 // function : RWMesh_FaceIterator
26 // =======================================================================
27 RWMesh_FaceIterator::RWMesh_FaceIterator (const TDF_Label& theLabel,
28 const TopLoc_Location& theLocation,
29 const Standard_Boolean theToMapColors,
30 const XCAFPrs_Style& theStyle)
31 : myDefStyle (theStyle),
32 myToMapColors (theToMapColors),
39 myHasFaceColor (false)
42 if (!XCAFDoc_ShapeTool::GetShape (theLabel, aShape)
48 aShape.Location (theLocation);
49 myFaceIter.Init (aShape, TopAbs_FACE);
53 dispatchStyles (theLabel, theLocation, theStyle);
59 // =======================================================================
60 // function : dispatchStyles
62 // =======================================================================
63 void RWMesh_FaceIterator::dispatchStyles (const TDF_Label& theLabel,
64 const TopLoc_Location& theLocation,
65 const XCAFPrs_Style& theStyle)
67 TopLoc_Location aDummyLoc;
68 XCAFPrs_IndexedDataMapOfShapeStyle aStyles;
69 XCAFPrs::CollectStyleSettings (theLabel, aDummyLoc, aStyles);
71 Standard_Integer aNbTypes[TopAbs_SHAPE] = {};
72 for (Standard_Integer aTypeIter = TopAbs_FACE; aTypeIter >= TopAbs_COMPOUND; --aTypeIter)
74 if (aTypeIter != TopAbs_FACE
75 && aNbTypes[aTypeIter] == 0)
80 for (XCAFPrs_IndexedDataMapOfShapeStyle::Iterator aStyleIter (aStyles); aStyleIter.More(); aStyleIter.Next())
82 const TopoDS_Shape& aKeyShape = aStyleIter.Key();
83 const TopAbs_ShapeEnum aKeyShapeType = aKeyShape.ShapeType();
84 if (aTypeIter == TopAbs_FACE)
86 ++aNbTypes[aKeyShapeType];
88 if (aTypeIter != aKeyShapeType)
93 XCAFPrs_Style aCafStyle = aStyleIter.Value();
94 if (!aCafStyle.IsSetColorCurv()
95 && theStyle.IsSetColorCurv())
97 aCafStyle.SetColorCurv (theStyle.GetColorCurv());
99 if (!aCafStyle.IsSetColorSurf()
100 && theStyle.IsSetColorSurf())
102 aCafStyle.SetColorSurf (theStyle.GetColorSurfRGBA());
104 if (aCafStyle.Material().IsNull()
105 && !theStyle.Material().IsNull())
107 aCafStyle.SetMaterial (theStyle.Material());
110 TopoDS_Shape aKeyShapeLocated = aKeyShape.Located (theLocation);
111 if (aKeyShapeType == TopAbs_FACE)
113 myStyles.Bind (aKeyShapeLocated, aCafStyle);
117 for (TopExp_Explorer aFaceIter (aKeyShapeLocated, TopAbs_FACE); aFaceIter.More(); aFaceIter.Next())
119 if (!myStyles.IsBound (aFaceIter.Current()))
121 myStyles.Bind (aFaceIter.Current(), aCafStyle);
129 // =======================================================================
132 // =======================================================================
133 gp_Dir RWMesh_FaceIterator::normal (Standard_Integer theNode)
135 gp_Dir aNormal (gp::DZ());
136 if (myNormals != NULL)
138 const Standard_Integer aNodeIndex = theNode - myNodes->Lower();
139 const Graphic3d_Vec3 aNormVec3 (myNormals->Value (myNormals->Lower() + aNodeIndex * 3),
140 myNormals->Value (myNormals->Lower() + aNodeIndex * 3 + 1),
141 myNormals->Value (myNormals->Lower() + aNodeIndex * 3 + 2));
142 if (aNormVec3.Modulus() != 0.0f)
144 aNormal.SetCoord (aNormVec3.x(), aNormVec3.y(), aNormVec3.z());
147 else if (myHasNormals
148 && myNodeUVs != NULL)
150 const gp_XY& anUV = myNodeUVs->Value (theNode).XY();
151 mySLTool.SetParameters (anUV.X(), anUV.Y());
152 if (mySLTool.IsNormalDefined())
154 aNormal = mySLTool.Normal();
160 // =======================================================================
163 // =======================================================================
164 void RWMesh_FaceIterator::Next()
166 for (; myFaceIter.More(); myFaceIter.Next())
168 myFace = TopoDS::Face (myFaceIter.Current());
169 myPolyTriang = BRep_Tool::Triangulation (myFace, myFaceLocation);
170 myTrsf = myFaceLocation.Transformation();
171 if (myPolyTriang.IsNull()
172 || myPolyTriang->Triangles().Length() == 0)
186 // =======================================================================
187 // function : initFace
189 // =======================================================================
190 void RWMesh_FaceIterator::initFace()
192 myHasNormals = false;
193 myHasFaceColor = false;
194 myIsMirrored = myTrsf.VectorialPart().Determinant() < 0.0;
198 myNodes = &myPolyTriang->Nodes();
199 if (myPolyTriang->HasNormals())
201 myNormals = &myPolyTriang->Normals();
204 if (myPolyTriang->HasUVNodes())
206 myNodeUVs = &myPolyTriang->UVNodes();
209 TopoDS_Face aFaceFwd = TopoDS::Face (myFace.Oriented (TopAbs_FORWARD));
210 aFaceFwd.Location (TopLoc_Location());
211 TopLoc_Location aLoc;
212 if (!BRep_Tool::Surface (aFaceFwd, aLoc).IsNull())
214 myFaceAdaptor.Initialize (aFaceFwd, false);
215 mySLTool.SetSurface (myFaceAdaptor);
225 if (!myStyles.Find (myFace, myFaceStyle))
227 myFaceStyle = myDefStyle;
230 if (!myFaceStyle.Material().IsNull())
232 myHasFaceColor = true;
233 myFaceColor = myFaceStyle.Material()->BaseColor();
235 else if (myFaceStyle.IsSetColorSurf())
237 myHasFaceColor = true;
238 myFaceColor = myFaceStyle.GetColorSurfRGBA();