0025936: Reusable data structure for 2D tesselation (3- and 4-nodal mesh)
[occt.git] / src / MeshTest / MeshTest_DrawableMesh.cxx
CommitLineData
b311480e 1// Created on: 1994-08-03
2// Created by: Modeling
3// Copyright (c) 1994-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.
7fd59977 16
fc9b36d6 17#include <MeshTest_DrawableMesh.hxx>
7fd59977 18#include <TopoDS.hxx>
19#include <Draw.hxx>
20#include <Draw_ColorKind.hxx>
21#include <Draw_Color.hxx>
22#include <TColStd_ListIteratorOfListOfInteger.hxx>
fcf15f5c 23#include <TColStd_Array1OfInteger.hxx>
24#include <Poly_Triangulation.hxx>
7fd59977 25#include <Standard_RangeError.hxx>
0d88155b 26#include <BRepMesh_DegreeOfFreedom.hxx>
7fd59977 27#include <BRepMesh_Edge.hxx>
28#include <BRepMesh_Vertex.hxx>
29#include <BRepMesh_Triangle.hxx>
30#include <BRepMesh_DataStructureOfDelaun.hxx>
ceb418e1 31#include <TopExp_Explorer.hxx>
7fd59977 32
fc9b36d6 33IMPLEMENT_STANDARD_HANDLE (MeshTest_DrawableMesh, Draw_Drawable3D)
34IMPLEMENT_STANDARD_RTTIEXT(MeshTest_DrawableMesh, Draw_Drawable3D)
35
fcf15f5c 36typedef NCollection_Map<BRepMesh_Edge> BRepMesh_MapOfLinks;
37
38static 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
7fd59977 46//=======================================================================
47//function : MeshTest_DrawableMesh
48//purpose :
49//=======================================================================
ceb418e1 50MeshTest_DrawableMesh::MeshTest_DrawableMesh()
51 : myDeflection(1.)
7fd59977 52{
53}
54
55//=======================================================================
56//function : MeshTest_DrawableMesh
57//purpose :
58//=======================================================================
ceb418e1 59MeshTest_DrawableMesh::MeshTest_DrawableMesh(const TopoDS_Shape& theShape,
60 const Standard_Real theDeflection)
61 : myDeflection(theDeflection)
7fd59977 62{
ceb418e1 63 Add(theShape);
7fd59977 64}
65
7fd59977 66//=======================================================================
67//function : MeshTest_DrawableMesh
68//purpose :
69//=======================================================================
ceb418e1 70MeshTest_DrawableMesh::MeshTest_DrawableMesh(
71 const Handle(BRepMesh_IncrementalMesh)& theMesher)
72 : myDeflection(1.)
7fd59977 73{
ceb418e1 74 myMesher = theMesher;
75 if (!myMesher.IsNull())
76 myDeflection = myMesher->Deflection();
7fd59977 77}
78
7fd59977 79//=======================================================================
80//function : MeshTest_DrawableMesh
81//purpose :
82//=======================================================================
ceb418e1 83void MeshTest_DrawableMesh::Add(const TopoDS_Shape& theShape)
7fd59977 84{
ceb418e1 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();
7fd59977 94}
95
96//=======================================================================
97//function : DrawOn
98//purpose :
99//=======================================================================
100
35e08fe8 101void MeshTest_DrawableMesh::DrawOn(Draw_Display& /*D*/) const
7fd59977 102{
103 // should be reimplemented!!
104 /* Handle(BRepMesh_DataStructureOfDelaun) struc = myMesh->Result();
105 Standard_Integer nbc;
106 D.SetColor(Draw_vert);
0d88155b 107
7fd59977 108 for (Standard_Integer iLi=1; iLi<=myMesh->NbEdges(); iLi++) {
0d88155b
O
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 }
7fd59977 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++) {
0d88155b
O
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 }
7fd59977 145 }
146
147 // highlighted edges
148 D.SetColor(Draw_rouge);
149 for (i = 1; i <= myEdges.Length(); i++) {
0d88155b
O
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 }
7fd59977 155 }
156
157 // highlighted vertices
158 for (i = 1; i <= myVertices.Length(); i++) {
0d88155b 159 D.DrawMarker(myMesh->Pnt(myVertices(i)),Draw_Losange);
7fd59977 160 }
161
162 */
163
164}
165
166//=======================================================================
167//function : Copy
168//purpose :
169//=======================================================================
7fd59977 170Handle(Draw_Drawable3D) MeshTest_DrawableMesh::Copy() const
171{
ceb418e1 172 return new MeshTest_DrawableMesh(myMesher);
7fd59977 173}
174
175//=======================================================================
176//function : Dump
177//purpose :
178//=======================================================================
35e08fe8 179void MeshTest_DrawableMesh::Dump(Standard_OStream&) const
7fd59977 180{
181 // Should be reimplemented
0d88155b 182
7fd59977 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();
0d88155b 190
7fd59977 191 for (in=1; in<=nbn; in++) {
0d88155b
O
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";
7fd59977 201 }
202 S <<endl;
203 for (il=1; il<=nbl; il++) {
0d88155b
O
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";
7fd59977 213 }
214 S <<endl;
215 for (ie=1; ie<=nbe; ie++) {
0d88155b
O
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";
7fd59977 225 }
226 S << endl;
227 */
228}
229
230//=======================================================================
231//function : Whatis
232//purpose :
233//=======================================================================
ceb418e1 234void MeshTest_DrawableMesh::Whatis(Draw_Interpretor& theStream) const
7fd59977 235{
2caff0b3 236 const TopoDS_Shape& aShape = myMesher->Shape();
fcf15f5c 237
2caff0b3 238 Standard_Integer aPointsNb = 0;
ceb418e1 239 Standard_Integer aTrianglesNb = 0;
240 Standard_Integer aEdgesNb = 0;
241
fcf15f5c 242 TopLoc_Location aLocation;
243 Handle(Poly_Triangulation) aTriangulation;
244
ceb418e1 245 TopExp_Explorer aFaceIt(aShape, TopAbs_FACE);
246 for (; aFaceIt.More(); aFaceIt.Next())
247 {
248 const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current());
249
fcf15f5c 250 aTriangulation = BRep_Tool::Triangulation(aFace, aLocation);
251 if (aTriangulation.IsNull())
ceb418e1 252 continue;
253
fcf15f5c 254 // Count number of links
255 BRepMesh_MapOfLinks aMap;
6c1f47fd 256 for (Standard_Integer i = 1, v[3]; i <= aTriangulation->NbTriangles(); ++i)
fcf15f5c 257 {
6c1f47fd 258 aTriangulation->Triangle (i).Get(v[0], v[1], v[2]);
ceb418e1 259
fcf15f5c 260 addLink(v[0], v[1], aMap);
261 addLink(v[1], v[2], aMap);
262 addLink(v[2], v[0], aMap);
263 }
ceb418e1 264
fcf15f5c 265 aPointsNb += aTriangulation->NbNodes();
266 aTrianglesNb += aTriangulation->NbTriangles();
267 aEdgesNb += aMap.Extent();
ceb418e1 268 }
269
270 theStream << " 3d mesh\n";
271 theStream << " - Triangles : " << aTrianglesNb << "\n";
272 theStream << " - Edges : " << aEdgesNb << "\n";
273 theStream << " - Point3d : " << aPointsNb << "\n";
7fd59977 274}
275
276//=======================================================================
ceb418e1 277//function : Mesher
7fd59977 278//purpose :
279//=======================================================================
ceb418e1 280const Handle(BRepMesh_IncrementalMesh)& MeshTest_DrawableMesh::Mesher() const
7fd59977 281{
ceb418e1 282 return myMesher;
7fd59977 283}
284
285
286//=======================================================================
287//function : Edges
288//purpose :
289//=======================================================================
7fd59977 290TColStd_SequenceOfInteger& MeshTest_DrawableMesh::Edges()
291{
292 return myEdges;
293}
294
295
296//=======================================================================
297//function : Vertices
298//purpose :
299//=======================================================================
7fd59977 300TColStd_SequenceOfInteger& MeshTest_DrawableMesh::Vertices()
301{
302 return myVertices;
303}
304
305//=======================================================================
306//function : Triangles
307//purpose :
308//=======================================================================
7fd59977 309TColStd_SequenceOfInteger& MeshTest_DrawableMesh::Triangles()
310{
311 return myTriangles;
312}
313