0024023: Revamp the OCCT Handle -- ambiguity
[occt.git] / src / DrawTrSurf / DrawTrSurf_Triangulation.cxx
1 // Created on: 1995-03-06
2 // Created by: Laurent PAINNOT
3 // Copyright (c) 1995-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 <DrawTrSurf_Triangulation.ixx>
18 #include <Poly_Connect.hxx>
19 #include <Poly_Triangle.hxx>
20 #include <Poly_Array1OfTriangle.hxx>
21 #include <TColStd_Array1OfInteger.hxx>
22 #include <gp_Pnt.hxx>
23 #include <Draw_Color.hxx>
24 #include <Poly.hxx>
25
26 //#ifdef WNT
27 #include <stdio.h>
28 //#endif
29
30 //=======================================================================
31 //function : DrawTrSurf_Triangulation
32 //purpose  : 
33 //=======================================================================
34
35 DrawTrSurf_Triangulation::DrawTrSurf_Triangulation
36 (const Handle(Poly_Triangulation)& T): 
37     myTriangulation(T), 
38     myNodes(Standard_False), 
39     myTriangles(Standard_False)
40 {
41   // Build the connect tool
42   Poly_Connect pc(T);
43
44   Standard_Integer i,j,nFree, nInternal, nbTriangles = T->NbTriangles();
45   Standard_Integer t[3];
46
47   // count the free edges
48   nFree = 0;
49   for (i = 1; i <= nbTriangles; i++) {
50     pc.Triangles(i,t[0],t[1],t[2]);
51     for (j = 0; j < 3; j++)
52       if (t[j] == 0) nFree++;
53   }
54
55   // allocate the arrays
56   myFree = new TColStd_HArray1OfInteger(1,2*nFree);
57   nInternal = (3*nbTriangles - nFree) / 2;
58   myInternals = new TColStd_HArray1OfInteger(1,2*nInternal);
59
60   TColStd_Array1OfInteger& Free     = myFree->ChangeArray1();
61   TColStd_Array1OfInteger& Internal = myInternals->ChangeArray1();
62
63   Standard_Integer fr = 1, in = 1;
64   const Poly_Array1OfTriangle& triangles = T->Triangles();
65   Standard_Integer n[3];
66   for (i = 1; i <= nbTriangles; i++) {
67     pc.Triangles(i,t[0],t[1],t[2]);
68     triangles(i).Get(n[0],n[1],n[2]);
69     for (j = 0; j < 3; j++) {
70       Standard_Integer k = (j+1) % 3;
71       if (t[j] == 0) {
72         Free(fr)   = n[j];
73         Free(fr+1) = n[k];
74         fr += 2;
75       }
76       // internal edge if this triangle has a lower index than the adjacent
77       else if (i < t[j]) {
78         Internal(in)   = n[j];
79         Internal(in+1) = n[k];
80         in += 2;
81       }
82     }
83   }
84 }
85
86 //=======================================================================
87 //function : Triangulation
88 //purpose  : 
89 //=======================================================================
90
91 Handle(Poly_Triangulation) DrawTrSurf_Triangulation::Triangulation() const 
92 {
93   return myTriangulation;
94 }
95
96 //=======================================================================
97 //function : ShowNodes
98 //purpose  : 
99 //=======================================================================
100
101 void DrawTrSurf_Triangulation::ShowNodes(const Standard_Boolean B)
102 {
103   myNodes = B;
104 }
105
106 //=======================================================================
107 //function : ShowNodes
108 //purpose  : 
109 //=======================================================================
110
111 Standard_Boolean DrawTrSurf_Triangulation::ShowNodes() const 
112 {
113   return myNodes;
114 }
115
116 //=======================================================================
117 //function : ShowTriangles
118 //purpose  : 
119 //=======================================================================
120
121 void DrawTrSurf_Triangulation::ShowTriangles(const Standard_Boolean B)
122 {
123   myTriangles = B;
124 }
125
126 //=======================================================================
127 //function : ShowTriangles
128 //purpose  : 
129 //=======================================================================
130
131 Standard_Boolean DrawTrSurf_Triangulation::ShowTriangles() const 
132 {
133   return myTriangles;
134 }
135
136 //=======================================================================
137 //function : DrawOn
138 //purpose  : 
139 //=======================================================================
140
141 void DrawTrSurf_Triangulation::DrawOn(Draw_Display& dis) const 
142 {
143   // Display the edges
144   Standard_Integer i,n;
145
146   const TColgp_Array1OfPnt& Nodes = myTriangulation->Nodes();
147   
148   // free edges
149
150   dis.SetColor(Draw_rouge);
151   const TColStd_Array1OfInteger& Free = myFree->Array1();
152   n = Free.Length() / 2;
153   for (i = 1; i <= n; i++) {
154     dis.Draw(Nodes(Free(2*i-1)),Nodes(Free(2*i)));
155   }
156   
157   // internal edges
158
159   dis.SetColor(Draw_bleu);
160   const TColStd_Array1OfInteger& Internal = myInternals->Array1();
161   n = Internal.Length() / 2;
162   for (i = 1; i <= n; i++) {
163     dis.Draw(Nodes(Internal(2*i-1)),Nodes(Internal(2*i)));
164   }
165
166   // texts
167   char text[50];
168   if (myNodes) {
169     dis.SetColor(Draw_jaune);
170     n = myTriangulation->NbNodes();
171     for (i = 1; i <= n; i++) {
172       Sprintf(text,"%d",i);
173       dis.DrawString(Nodes(i),text);
174     }
175   }
176
177   if (myTriangles) {
178     dis.SetColor(Draw_vert);
179     n = myTriangulation->NbTriangles();
180     Standard_Integer t[3],j;
181     const Poly_Array1OfTriangle& triangle = myTriangulation->Triangles();
182     for (i = 1; i <= n; i++) {
183       triangle(i).Get(t[0],t[1],t[2]);
184       gp_Pnt P(0,0,0);
185       gp_XYZ& bary = P.ChangeCoord();
186       for (j = 0; j < 3; j++)
187         bary.Add(Nodes(t[j]).Coord());
188       bary.Multiply(1./3.);
189
190       Sprintf(text,"%d",i);
191       dis.DrawString(P,text);
192     }
193   }
194 }
195
196 //=======================================================================
197 //function : Copy
198 //purpose  : 
199 //=======================================================================
200
201 Handle(Draw_Drawable3D) DrawTrSurf_Triangulation::Copy() const 
202 {
203   return new DrawTrSurf_Triangulation(myTriangulation);
204 }
205
206 //=======================================================================
207 //function : Dump
208 //purpose  : 
209 //=======================================================================
210
211 void DrawTrSurf_Triangulation::Dump(Standard_OStream& S) const 
212 {
213   Poly::Dump(myTriangulation,S);
214 }
215
216 //=======================================================================
217 //function : Whatis
218 //purpose  : 
219 //=======================================================================
220
221 void DrawTrSurf_Triangulation::Whatis(Draw_Interpretor& I) const 
222 {
223   I << "triangulation";
224 }
225