0025364: BRepMesh is not able to triangulate the shape with fine deflection
[occt.git] / src / MeshTest / MeshTest_DrawableMesh.cxx
1 // Created on: 1994-08-03
2 // Created by: Modeling
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <MeshTest_DrawableMesh.hxx>
18 #include <TopoDS.hxx>
19 #include <Draw.hxx>
20 #include <Draw_ColorKind.hxx>
21 #include <Draw_Color.hxx>
22 #include <TColStd_ListIteratorOfListOfInteger.hxx>
23 #include <TColStd_Array1OfInteger.hxx>
24 #include <Poly_Triangulation.hxx>
25 #include <Standard_RangeError.hxx>
26 #include <BRepMesh_DegreeOfFreedom.hxx>
27 #include <BRepMesh_Edge.hxx>
28 #include <BRepMesh_Vertex.hxx>
29 #include <BRepMesh_Triangle.hxx>
30 #include <BRepMesh_DataStructureOfDelaun.hxx>
31 #include <TopExp_Explorer.hxx>
32
33 IMPLEMENT_STANDARD_HANDLE (MeshTest_DrawableMesh, Draw_Drawable3D)
34 IMPLEMENT_STANDARD_RTTIEXT(MeshTest_DrawableMesh, Draw_Drawable3D)
35
36 typedef NCollection_Map<BRepMesh_Edge> BRepMesh_MapOfLinks;
37
38 static inline void addLink(const Standard_Integer theIndex1,
39                            const Standard_Integer theIndex2,
40                            BRepMesh_MapOfLinks&   theMap)
41 {
42   BRepMesh_Edge anEdge(theIndex1, theIndex2, BRepMesh_Free);
43   theMap.Add(anEdge);
44 }
45
46 //=======================================================================
47 //function : MeshTest_DrawableMesh
48 //purpose  : 
49 //=======================================================================
50 MeshTest_DrawableMesh::MeshTest_DrawableMesh()
51   : myDeflection(1.)
52 {
53 }
54
55 //=======================================================================
56 //function : MeshTest_DrawableMesh
57 //purpose  : 
58 //=======================================================================
59 MeshTest_DrawableMesh::MeshTest_DrawableMesh(const TopoDS_Shape& theShape,
60                                              const Standard_Real theDeflection)
61   : myDeflection(theDeflection)
62 {
63   Add(theShape);
64 }
65
66 //=======================================================================
67 //function : MeshTest_DrawableMesh
68 //purpose  : 
69 //=======================================================================
70 MeshTest_DrawableMesh::MeshTest_DrawableMesh(
71   const Handle(BRepMesh_IncrementalMesh)& theMesher)
72   : myDeflection(1.)
73 {
74   myMesher = theMesher;
75   if (!myMesher.IsNull())
76     myDeflection = myMesher->Deflection();
77 }
78
79 //=======================================================================
80 //function : MeshTest_DrawableMesh
81 //purpose  : 
82 //=======================================================================
83 void MeshTest_DrawableMesh::Add(const TopoDS_Shape& theShape)
84 {
85   if (myMesher.IsNull())
86   {
87     myMesher = new BRepMesh_IncrementalMesh;
88     myMesher->SetDeflection(myDeflection);
89     myMesher->SetAngle(0.5);
90   }
91   
92   myMesher->SetShape(theShape);
93   myMesher->Perform();
94 }
95
96 //=======================================================================
97 //function : DrawOn
98 //purpose  : 
99 //=======================================================================
100
101 void MeshTest_DrawableMesh::DrawOn(Draw_Display& /*D*/) const 
102 {
103   // should be reimplemented!!
104   /*  Handle(BRepMesh_DataStructureOfDelaun) struc = myMesh->Result();
105   Standard_Integer nbc;
106   D.SetColor(Draw_vert);
107
108   for (Standard_Integer iLi=1; iLi<=myMesh->NbEdges(); iLi++) {
109   const BRepMesh_Edge& ed=myMesh->Edge(iLi);
110   if (ed.Movability()!=BRepMesh_Deleted) {
111   nbc=struc->ElemConnectedTo(iLi).Extent();
112   if (nbc<=0) D.SetColor(Draw_bleu);
113   else if (nbc==1) D.SetColor(Draw_jaune);
114   else if (nbc==2) D.SetColor(Draw_vert);
115   else D.SetColor(Draw_corail);
116   D.MoveTo(myMesh->Pnt(ed.FirstNode()));
117   D.DrawTo(myMesh->Pnt(ed.LastNode()));
118   }
119   }
120
121
122   // highlighted triangles
123   D.SetColor(Draw_blanc);
124   Standard_Integer e1, e2, e3, i;
125   Standard_Boolean o1, o2, o3;
126
127   for (i = 1; i <= myTriangles.Length(); i++) {
128   const BRepMesh_Triangle& tri=struc->GetElement(myTriangles(i));
129   tri.Edges(e1, e2, e3, o1, o2, o3); 
130   const BRepMesh_Edge& ed1=myMesh->Edge(e1);
131   if (ed1.Movability()!=BRepMesh_Deleted) {
132   D.MoveTo(myMesh->Pnt(ed1.FirstNode()));
133   D.DrawTo(myMesh->Pnt(ed1.LastNode()));
134   }
135   const BRepMesh_Edge& ed2=myMesh->Edge(e2);
136   if (ed2.Movability()!=BRepMesh_Deleted) {
137   D.MoveTo(myMesh->Pnt(ed2.FirstNode()));
138   D.DrawTo(myMesh->Pnt(ed2.LastNode()));
139   }
140   const BRepMesh_Edge& ed3=myMesh->Edge(e3);
141   if (ed3.Movability()!=BRepMesh_Deleted) {
142   D.MoveTo(myMesh->Pnt(ed3.FirstNode()));
143   D.DrawTo(myMesh->Pnt(ed3.LastNode()));
144   }
145   }
146
147   // highlighted edges
148   D.SetColor(Draw_rouge);
149   for (i = 1; i <= myEdges.Length(); i++) {
150   const BRepMesh_Edge& ed=myMesh->Edge(myEdges(i));
151   if (ed.Movability()!=BRepMesh_Deleted) {
152   D.MoveTo(myMesh->Pnt(ed.FirstNode()));
153   D.DrawTo(myMesh->Pnt(ed.LastNode()));
154   }
155   }
156
157   // highlighted vertices
158   for (i = 1; i <= myVertices.Length(); i++) {
159   D.DrawMarker(myMesh->Pnt(myVertices(i)),Draw_Losange);
160   }
161
162   */
163
164 }
165
166 //=======================================================================
167 //function : Copy
168 //purpose  : 
169 //=======================================================================
170 Handle(Draw_Drawable3D) MeshTest_DrawableMesh::Copy() const 
171 {
172   return new MeshTest_DrawableMesh(myMesher);
173 }
174
175 //=======================================================================
176 //function : Dump
177 //purpose  : 
178 //=======================================================================
179 void MeshTest_DrawableMesh::Dump(Standard_OStream&) const 
180 {
181   // Should be reimplemented
182
183   /*Handle(BRepMesh_DataStructureOfDelaun) struc=myMesh->Result();
184   Standard_Integer e1, e2, e3;
185   Standard_Boolean o1, o2, o3;
186   Standard_Integer in, il, ie;
187   Standard_Integer nbn=struc->NbNodes();
188   Standard_Integer nbl=struc->NbLinks();
189   Standard_Integer nbe=struc->NbElements();
190
191   for (in=1; in<=nbn; in++) {
192   BRepMesh_Vertex nod=struc->GetNode(in);
193   S<<"(node "<<in<<" (uv "<<nod.Coord().X()<<" "
194   <<nod.Coord().Y()<<") (3d "
195   <<nod.Location3d()<<") ";
196   printdegree(nod.Movability());
197   S<<" (edgeconex";
198   BRepMesh_ListOfInteger::Iterator tati(struc->LinkNeighboursOf(in));
199   for (; tati.More(); tati.Next()) S<<" "<<tati.Value();
200   S << "))\n";
201   }
202   S <<endl;
203   for (il=1; il<=nbl; il++) {
204   BRepMesh_Edge edg=struc->GetLink(il);
205   S << "(edge "<<il<<" ("<<edg.FirstNode()<<" "<<edg.LastNode()
206   <<" ";
207   printdegree(edg.Movability());
208   S<<") (triconex";
209   const BRepMesh_PairOfIndex& pair = struc->ElemConnectedTo(il);
210   for (Standard_Integer j = 1, jn = pair.Extent(); j <= jn; j++)
211   S<<" "<<pair.Index(j);
212   S << "))\n";
213   }
214   S <<endl;
215   for (ie=1; ie<=nbe; ie++) {
216   BRepMesh_Triangle tri=struc->GetElement(ie);
217   tri.Edges(e1, e2, e3, o1, o2, o3); 
218   if (!o1) e1=-e1;
219   if (!o2) e2=-e2;
220   if (!o3) e3=-e3;
221   S<<" (maille "<<ie<<" (links "<<e1<<" "
222   <<e2<<" "<<e3<<")";
223   printdegree(tri.Movability());
224   S<<")\n";
225   }
226   S << endl;
227   */
228 }
229
230 //=======================================================================
231 //function : Whatis
232 //purpose  : 
233 //=======================================================================
234 void MeshTest_DrawableMesh::Whatis(Draw_Interpretor& theStream) const 
235 {
236   const TopoDS_Shape& aShape = myMesher->Shape();
237
238   Standard_Integer aPointsNb    = 0;
239   Standard_Integer aTrianglesNb = 0;
240   Standard_Integer aEdgesNb     = 0;
241
242   TopLoc_Location aLocation;
243   Handle(Poly_Triangulation) aTriangulation;
244
245   TopExp_Explorer aFaceIt(aShape, TopAbs_FACE);
246   for (; aFaceIt.More(); aFaceIt.Next())
247   {
248     const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current());
249
250     aTriangulation = BRep_Tool::Triangulation(aFace, aLocation);
251     if (aTriangulation.IsNull())
252       continue;
253
254     // Count number of links
255     BRepMesh_MapOfLinks aMap;
256     const Poly_Array1OfTriangle& aTriangles = aTriangulation->Triangles();
257     for (Standard_Integer i = 1, v[3]; i <= aTriangles.Length(); ++i)
258     {
259       aTriangles(i).Get(v[0], v[1], v[2]);
260
261       addLink(v[0], v[1], aMap);
262       addLink(v[1], v[2], aMap);
263       addLink(v[2], v[0], aMap);
264     }
265
266     aPointsNb    += aTriangulation->NbNodes();
267     aTrianglesNb += aTriangulation->NbTriangles();
268     aEdgesNb     += aMap.Extent();
269   }
270
271   theStream << " 3d mesh\n";
272   theStream << "    - Triangles : " << aTrianglesNb << "\n";
273   theStream << "    - Edges     : " << aEdgesNb     << "\n";
274   theStream << "    - Point3d   : " << aPointsNb    << "\n";
275 }
276
277 //=======================================================================
278 //function : Mesher
279 //purpose  : 
280 //=======================================================================
281 const Handle(BRepMesh_IncrementalMesh)& MeshTest_DrawableMesh::Mesher() const 
282 {
283   return myMesher;
284 }
285
286
287 //=======================================================================
288 //function : Edges
289 //purpose  : 
290 //=======================================================================
291 TColStd_SequenceOfInteger& MeshTest_DrawableMesh::Edges()
292 {
293   return myEdges;
294 }
295
296
297 //=======================================================================
298 //function : Vertices
299 //purpose  : 
300 //=======================================================================
301 TColStd_SequenceOfInteger& MeshTest_DrawableMesh::Vertices()
302 {
303   return myVertices;
304 }
305
306 //=======================================================================
307 //function : Triangles
308 //purpose  : 
309 //=======================================================================
310 TColStd_SequenceOfInteger& MeshTest_DrawableMesh::Triangles()
311 {
312   return myTriangles;
313 }
314