summaryrefslogtreecommitdiff
path: root/src/DrawTrSurf/DrawTrSurf_Triangulation.cxx
blob: 69559658c4c81f760832d2e1a0a8696022ae9567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// File:	DrawTrSurf_Triangulation.cxx
// Created:	Mon Mar  6 16:49:54 1995
// Author:	Laurent PAINNOT
//		<lpa@metrox>


#include <DrawTrSurf_Triangulation.ixx>
#include <Poly_Connect.hxx>
#include <Poly_Triangle.hxx>
#include <Poly_Array1OfTriangle.hxx>
#include <TColStd_Array1OfInteger.hxx>
#include <gp_Pnt.hxx>
#include <Draw_Color.hxx>
#include <Poly.hxx>

//#ifdef WNT
#include <stdio.h>
//#endif

//=======================================================================
//function : DrawTrSurf_Triangulation
//purpose  : 
//=======================================================================

DrawTrSurf_Triangulation::DrawTrSurf_Triangulation
(const Handle(Poly_Triangulation)& T): 
    myTriangulation(T), 
    myNodes(Standard_False), 
    myTriangles(Standard_False)
{
  // Build the connect tool
  Poly_Connect pc(T);

  Standard_Integer i,j,nFree, nInternal, nbTriangles = T->NbTriangles();
  Standard_Integer t[3];

  // count the free edges
  nFree = 0;
  for (i = 1; i <= nbTriangles; i++) {
    pc.Triangles(i,t[0],t[1],t[2]);
    for (j = 0; j < 3; j++)
      if (t[j] == 0) nFree++;
  }

  // allocate the arrays
  myFree = new TColStd_HArray1OfInteger(1,2*nFree);
  nInternal = (3*nbTriangles - nFree) / 2;
  myInternals = new TColStd_HArray1OfInteger(1,2*nInternal);

  TColStd_Array1OfInteger& Free     = myFree->ChangeArray1();
  TColStd_Array1OfInteger& Internal = myInternals->ChangeArray1();

  Standard_Integer fr = 1, in = 1;
  const Poly_Array1OfTriangle& triangles = T->Triangles();
  Standard_Integer n[3];
  for (i = 1; i <= nbTriangles; i++) {
    pc.Triangles(i,t[0],t[1],t[2]);
    triangles(i).Get(n[0],n[1],n[2]);
    for (j = 0; j < 3; j++) {
      Standard_Integer k = (j+1) % 3;
      if (t[j] == 0) {
	Free(fr)   = n[j];
	Free(fr+1) = n[k];
	fr += 2;
      }
      // internal edge if this triangle has a lower index than the adjacent
      else if (i < t[j]) {
	Internal(in)   = n[j];
	Internal(in+1) = n[k];
	in += 2;
      }
    }
  }
}

//=======================================================================
//function : Triangulation
//purpose  : 
//=======================================================================

Handle(Poly_Triangulation) DrawTrSurf_Triangulation::Triangulation() const 
{
  return myTriangulation;
}

//=======================================================================
//function : ShowNodes
//purpose  : 
//=======================================================================

void DrawTrSurf_Triangulation::ShowNodes(const Standard_Boolean B)
{
  myNodes = B;
}

//=======================================================================
//function : ShowNodes
//purpose  : 
//=======================================================================

Standard_Boolean DrawTrSurf_Triangulation::ShowNodes() const 
{
  return myNodes;
}

//=======================================================================
//function : ShowTriangles
//purpose  : 
//=======================================================================

void DrawTrSurf_Triangulation::ShowTriangles(const Standard_Boolean B)
{
  myTriangles = B;
}

//=======================================================================
//function : ShowTriangles
//purpose  : 
//=======================================================================

Standard_Boolean DrawTrSurf_Triangulation::ShowTriangles() const 
{
  return myTriangles;
}

//=======================================================================
//function : DrawOn
//purpose  : 
//=======================================================================

void DrawTrSurf_Triangulation::DrawOn(Draw_Display& dis) const 
{
  // Display the edges
  Standard_Integer i,n;

  const TColgp_Array1OfPnt& Nodes = myTriangulation->Nodes();
  
  // free edges

  dis.SetColor(Draw_rouge);
  const TColStd_Array1OfInteger& Free = myFree->Array1();
  n = Free.Length() / 2;
  for (i = 1; i <= n; i++) {
    dis.Draw(Nodes(Free(2*i-1)),Nodes(Free(2*i)));
  }
  
  // internal edges

  dis.SetColor(Draw_bleu);
  const TColStd_Array1OfInteger& Internal = myInternals->Array1();
  n = Internal.Length() / 2;
  for (i = 1; i <= n; i++) {
    dis.Draw(Nodes(Internal(2*i-1)),Nodes(Internal(2*i)));
  }

  // texts
  char text[50];
  if (myNodes) {
    dis.SetColor(Draw_jaune);
    n = myTriangulation->NbNodes();
    for (i = 1; i <= n; i++) {
      sprintf(text,"%d",i);
      dis.DrawString(Nodes(i),text);
    }
  }

  if (myTriangles) {
    dis.SetColor(Draw_vert);
    n = myTriangulation->NbTriangles();
    Standard_Integer t[3],j;
    const Poly_Array1OfTriangle& triangle = myTriangulation->Triangles();
    for (i = 1; i <= n; i++) {
      triangle(i).Get(t[0],t[1],t[2]);
      gp_Pnt P(0,0,0);
      gp_XYZ& bary = P.ChangeCoord();
      for (j = 0; j < 3; j++)
	bary.Add(Nodes(t[j]).Coord());
      bary.Multiply(1./3.);

      sprintf(text,"%d",i);
      dis.DrawString(P,text);
    }
  }
}

//=======================================================================
//function : Copy
//purpose  : 
//=======================================================================

Handle(Draw_Drawable3D) DrawTrSurf_Triangulation::Copy() const 
{
  return new DrawTrSurf_Triangulation(myTriangulation);
}

//=======================================================================
//function : Dump
//purpose  : 
//=======================================================================

void DrawTrSurf_Triangulation::Dump(Standard_OStream& S) const 
{
  Poly::Dump(myTriangulation,S);
}

//=======================================================================
//function : Whatis
//purpose  : 
//=======================================================================

void DrawTrSurf_Triangulation::Whatis(Draw_Interpretor& I) const 
{
  I << "triangulation";
}