1 // Simplify_Presentation.cpp: implementation of the Simplify_Presentation class.
2 // Simplify shape -> create a new shape based on triangulation of a given shape
3 //////////////////////////////////////////////////////////////////////
6 #include "Simplify_Presentation.h"
8 #include <GeomPlate_Surface.hxx>
9 #include <GeomPlate_MakeApprox.hxx>
10 #include <GeomPlate_PointConstraint.hxx>
11 #include <GeomPlate_BuildPlateSurface.hxx>
12 #include <Precision.hxx>
14 #include <TopoDS_Face.hxx>
15 #include <TopoDS_Compound.hxx>
16 #include <ShapeAnalysis_FreeBounds.hxx>
17 #include <BRep_Builder.hxx>
18 #include <BRepTools.hxx>
19 #include <BRep_Tool.hxx>
20 #include <BRepMesh.hxx>
21 #include <TopExp_Explorer.hxx>
22 #include <TopLoc_Location.hxx>
23 #include <TColgp_SequenceOfPnt.hxx>
24 #include <Poly_Triangulation.hxx>
25 #include <TColgp_Array1OfPnt.hxx>
26 #include <TopoDS_Wire.hxx>
27 #include <TopTools_IndexedMapOfShape.hxx>
29 #include <TopLoc_Location.hxx>
30 #include <ShapeFix_Shape.hxx>
40 // Initialization of global variable with an instance of this class
41 OCCDemo_Presentation* OCCDemo_Presentation::Current = new Simplify_Presentation;
44 //////////////////////////////////////////////////////////////////////
45 // Construction/Destruction
46 //////////////////////////////////////////////////////////////////////
48 Simplify_Presentation::Simplify_Presentation()
50 setName ("Simplify shape");
53 //=========================================================================================
55 //=========================================================================================
57 void Simplify_Presentation::DoSample()
59 getAISContext()->EraseAll();
60 sample("shell1.brep");
64 //////////////////////////////////////////////////////////////////////
66 //////////////////////////////////////////////////////////////////////
67 //================================================================
68 // Function : Simplify_Presentation::simplify
70 //================================================================
71 void Simplify_Presentation::simplify(const TopoDS_Shape& aShape)
73 setResultTitle("Simplify Face");
75 " TopoDS_Shape aShape;" EOL
77 " // initialize aShape" EOL
80 " // define parameter triangulation" EOL
81 " Standard_Real aDeflection = 0.1;" EOL
83 " // removes all the triangulations of the faces ," EOL
84 " //and all the polygons on the triangulations of the edges" EOL
85 " BRepTools::Clean(aShape);" EOL
86 " // adds a triangulation of the shape aShape with the deflection aDeflection" EOL
87 " BRepMesh::Mesh(aShape,aDeflection);" EOL
89 " Standard_Integer aIndex = 1, nbNodes = 0;" EOL
91 " // define two sequence of points" EOL
92 " TColgp_SequenceOfPnt aPoints, aPoints1;" EOL
94 " // triangulation" EOL
95 " for(TopExp_Explorer aExpFace(aShape,TopAbs_FACE); aExpFace.More(); aExpFace.Next())" EOL
97 " TopoDS_Face aFace = TopoDS::Face(aExpFace.Current());" EOL
98 " TopLoc_Location aLocation;" EOL
100 " // takes the triangulation of the face aFace" EOL
101 " Handle(Poly_Triangulation) aTr = BRep_Tool::Triangulation(aFace,aLocation);" EOL
103 " if(!aTr.IsNull())" EOL
105 " // takes the array of nodes for this triangulation" EOL
106 " const TColgp_Array1OfPnt& aNodes = aTr->Nodes(); " EOL
107 " nbNodes = aNodes.Length();" EOL
109 " for( Standard_Integer i = 1; i <= nbNodes; i++)" EOL
111 " // create seguence of node points in absolute coordinate system" EOL
112 " gp_Pnt aPnt = aNodes(i).Transformed(aLocation);" EOL
113 " aPoints.Append(aPnt);" EOL
119 " // remove double points" EOL
120 " nbNodes = aPoints.Length();" EOL
121 " for( Standard_Integer i = 1; i <= nbNodes; i++)" EOL
123 " gp_Pnt aPi = aPoints(i);" EOL
124 " for( Standard_Integer j = i + 1; j < nbNodes; j++)" EOL
126 " gp_Pnt aPj = aPoints(j);" EOL
127 " if(!aPi.IsEqual(aPj,0.9))" EOL
130 " if(aIndex == j - 1)" EOL
131 " aPoints1.Append(aPi);" EOL
133 " aIndex = i + 1;" EOL
136 " // find max point" EOL
138 " gp_Pnt aPntMax = aPoints1(1);" EOL
139 " nbNodes = aPoints1.Length();" EOL
140 " for(i = 2; i <= nbNodes; i++)" EOL
142 " if(aPoints1(i).X() > aPntMax.X())" EOL
145 " aPntMax = aPoints1(aIndex); " EOL
149 " // clear seguence" EOL
150 " aPoints.Clear();" EOL
152 " Standard_Integer nbLeftNodes = nbNodes;" EOL
154 " // ascending sort - fill aPoints with ascending " EOL
155 " // by X coordinate points from aPoints1" EOL
156 " for(i = 1; i < nbNodes; i++)" EOL
158 " Standard_Real aMin = aPntMax.X();" EOL
160 " for( Standard_Integer j = 1; j <= nbLeftNodes; j++)" EOL
162 " if(aPoints1(j).X() < aMin)" EOL
164 " aMin = aPoints1(j).X();" EOL
168 " aPoints.Append(aPoints1(aIndex));" EOL
169 " aPoints1.Remove(aIndex);" EOL
170 " nbLeftNodes = aPoints1.Length();" EOL
172 " aPoints.Append(aPntMax);" EOL
174 " // define parameters GeomPlate_BuildPlateSurface" EOL
175 " Standard_Integer Degree = 3;" EOL
176 " Standard_Integer NbPtsOnCur = 10;" EOL
177 " Standard_Integer NbIter = 3;" EOL
178 " Standard_Integer Order = 0;" EOL
179 " Standard_Integer MaxSeg = 9;" EOL
180 " Standard_Integer MaxDegree = 5;" EOL
181 " Standard_Real dmax, anApproxTol = 0.001;" EOL
182 " Standard_Real aConstrTol = Precision::Confusion();" EOL
184 " // define object BuildPlateSurface" EOL
185 " GeomPlate_BuildPlateSurface BPSurf(Degree,NbPtsOnCur,NbIter);" EOL
187 " // add point constraints to GeomPlate_BuildPlateSurface object" EOL
188 " nbNodes = aPoints.Length();" EOL
189 " for (i = 1; i <= nbNodes; i++)" EOL
190 " BPSurf.Add(new GeomPlate_PointConstraint(aPoints(i), Order, aConstrTol));" EOL
192 " BPSurf.Perform();" EOL
194 " // make PlateSurface" EOL
195 " Handle(GeomPlate_Surface) PSurf;" EOL
196 " Handle(Geom_Surface) aSurf;" EOL
198 " if (BPSurf.IsDone())" EOL
200 " PSurf = BPSurf.Surface();" EOL
202 " // define parameter approximation" EOL
203 " dmax = Max(0.01,10*BPSurf.G0Error());" EOL
205 " // make approximation" EOL
206 " GeomPlate_MakeApprox Mapp(PSurf,anApproxTol, MaxSeg,MaxDegree,dmax);" EOL
207 " aSurf = Mapp.Surface();" EOL
212 " ShapeAnalysis_FreeBounds aFreeBounds(aShape, Standard_False, Standard_True);" EOL
213 " TopoDS_Compound aClosedWires = aFreeBounds.GetClosedWires();" EOL
214 " TopTools_IndexedMapOfShape aWires;" EOL
215 " TopExp::MapShapes(aClosedWires, TopAbs_WIRE, aWires);" EOL
216 " TopoDS_Wire aWire;" EOL
217 " Standard_Integer nbWires = aWires.Extent();" EOL
219 " aWire = TopoDS::Wire(aWires(1));" EOL
223 " BRep_Builder B;" EOL
224 " TopoDS_Face aFace;" EOL
225 " B.MakeFace(aFace, aSurf, Precision::Confusion());" EOL
226 " B.Add(aFace, aWire);" EOL
227 " Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape(aFace);" EOL
228 " sfs->Perform();" EOL
229 " TopoDS_Shape aFixedFace = sfs->Shape();" EOL
230 " if (aFixedFace.IsNull()) " EOL
233 // define parameter triangulation
234 Standard_Real aDeflection = 0.1;
236 // removes all the triangulations of the faces ,
237 //and all the polygons on the triangulations of the edges
238 BRepTools::Clean(aShape);
239 // adds a triangulation of the shape aShape with the deflection aDeflection
240 BRepMesh::Mesh(aShape,aDeflection);
242 Standard_Integer aIndex = 1, nbNodes = 0;
244 // define two sequence of points
245 TColgp_SequenceOfPnt aPoints, aPoints1;
248 for(TopExp_Explorer aExpFace(aShape,TopAbs_FACE); aExpFace.More(); aExpFace.Next())
250 TopoDS_Face aFace = TopoDS::Face(aExpFace.Current());
251 TopLoc_Location aLocation;
253 // takes the triangulation of the face aFace
254 Handle(Poly_Triangulation) aTr = BRep_Tool::Triangulation(aFace,aLocation);
258 // takes the array of nodes for this triangulation
259 const TColgp_Array1OfPnt& aNodes = aTr->Nodes();
260 nbNodes = aNodes.Length();
262 for( Standard_Integer i = 1; i <= nbNodes; i++)
264 // create seguence of node points in absolute coordinate system
265 gp_Pnt aPnt = aNodes(i).Transformed(aLocation);
266 aPoints.Append(aPnt);
272 // remove double points
273 nbNodes = aPoints.Length();
274 for( Standard_Integer i = 1; i <= nbNodes; i++)
276 gp_Pnt aPi = aPoints(i);
277 for( Standard_Integer j = i + 1; j < nbNodes; j++)
279 gp_Pnt aPj = aPoints(j);
280 if(!aPi.IsEqual(aPj,0.9))
284 aPoints1.Append(aPi);
291 gp_Pnt aPntMax = aPoints1(1);
292 nbNodes = aPoints1.Length();
293 for(i = 2; i <= nbNodes; i++)
295 if(aPoints1(i).X() > aPntMax.X())
298 aPntMax = aPoints1(aIndex);
305 Standard_Integer nbLeftNodes = nbNodes;
307 // ascending sort - fill aPoints with ascending
308 // by X coordinate points from aPoints1
309 for(i = 1; i < nbNodes; i++)
311 Standard_Real aMin = aPntMax.X();
313 for( Standard_Integer j = 1; j <= nbLeftNodes; j++)
315 if(aPoints1(j).X() < aMin)
317 aMin = aPoints1(j).X();
321 aPoints.Append(aPoints1(aIndex));
322 aPoints1.Remove(aIndex);
323 nbLeftNodes = aPoints1.Length();
325 aPoints.Append(aPntMax);
327 // define parameters GeomPlate_BuildPlateSurface
328 Standard_Integer Degree = 3;
329 Standard_Integer NbPtsOnCur = 10;
330 Standard_Integer NbIter = 3;
331 Standard_Integer Order = 0;
332 Standard_Integer MaxSeg = 9;
333 Standard_Integer MaxDegree = 5;
334 Standard_Real dmax, anApproxTol = 0.001;
335 Standard_Real aConstrTol = Precision::Confusion();
337 // define object BuildPlateSurface
338 GeomPlate_BuildPlateSurface BPSurf(Degree,NbPtsOnCur,NbIter);
340 // add point constraints to GeomPlate_BuildPlateSurface object
341 nbNodes = aPoints.Length();
342 for (i = 1; i <= nbNodes; i++)
343 BPSurf.Add(new GeomPlate_PointConstraint(aPoints(i), Order, aConstrTol));
348 Handle(GeomPlate_Surface) PSurf;
349 Handle(Geom_Surface) aSurf;
353 PSurf = BPSurf.Surface();
355 // define parameter approximation
356 dmax = Max(0.01,10*BPSurf.G0Error());
358 // make approximation
359 GeomPlate_MakeApprox Mapp(PSurf,anApproxTol, MaxSeg,MaxDegree,dmax);
360 aSurf = Mapp.Surface();
365 ShapeAnalysis_FreeBounds aFreeBounds(aShape, Standard_False, Standard_True);
366 TopoDS_Compound aClosedWires = aFreeBounds.GetClosedWires();
367 TopTools_IndexedMapOfShape aWires;
368 TopExp::MapShapes(aClosedWires, TopAbs_WIRE, aWires);
370 Standard_Integer nbWires = aWires.Extent();
372 aWire = TopoDS::Wire(aWires(1));
378 B.MakeFace(aFace, aSurf, Precision::Confusion());
380 Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape(aFace);
382 TopoDS_Shape aFixedFace = sfs->Shape();
383 if (aFixedFace.IsNull())
386 // output surface, make it half transparent
387 Handle(AIS_InteractiveObject) aSurfIO = drawSurface(
388 aSurf, Quantity_NOC_LEMONCHIFFON3, Standard_False);
389 aSurfIO->SetTransparency(0.5);
390 getAISContext()->Display(aSurfIO, Standard_False);
393 if(WAIT_A_LITTLE) return;
396 for(i = 1; i <= nbNodes; i++)
397 drawPoint(aPoints(i));
399 if(WAIT_A_LITTLE) return;
401 // output resulting face
402 drawShape(aFixedFace);
407 //================================================================
408 // Function : Simplify_Presentation::sample
410 //================================================================
411 void Simplify_Presentation::sample(const Standard_CString aFileName)
413 TCollection_AsciiString aPath(GetDataDir());
414 aPath = aPath + "\\" + aFileName;
418 Standard_Boolean isRead = BRepTools::Read (aShape, aPath.ToCString(), aBld);
421 aPath += " was not found. The sample can not be shown.";
422 setResultText(aPath.ToCString());