0023948: Wrong intersection between a surface of revolution and a plane.
[occt.git] / src / DrawTrSurf / DrawTrSurf_Triangulation2D.cxx
1 // Created on: 1997-07-22
2 // Created by: Laurent PAINNOT
3 // Copyright (c) 1997-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_Triangulation2D.ixx>
18 #include <Poly_Connect.hxx>
19 #include <Poly_Triangle.hxx>
20 #include <Poly_Array1OfTriangle.hxx>
21 #include <TColStd_Array1OfInteger.hxx>
22 #include <TColgp_Array1OfPnt2d.hxx>
23 #include <gp_Pnt2d.hxx>
24 #include <Draw_Color.hxx>
25 #include <Poly.hxx>
26
27 #ifdef WNT
28 #include <stdio.h>
29 #endif
30
31 //=======================================================================
32 //function : DrawTrSurf_Triangulation2D
33 //purpose  : 
34 //=======================================================================
35
36 DrawTrSurf_Triangulation2D::DrawTrSurf_Triangulation2D
37 (const Handle(Poly_Triangulation)& T): 
38     myTriangulation(T)
39 {
40   // Build the connect tool
41   Poly_Connect pc(T);
42
43   Standard_Integer i,j,nFree, nInternal, nbTriangles = T->NbTriangles();
44   Standard_Integer t[3];
45
46   // count the free edges
47   nFree = 0;
48   for (i = 1; i <= nbTriangles; i++) {
49     pc.Triangles(i,t[0],t[1],t[2]);
50     for (j = 0; j < 3; j++)
51       if (t[j] == 0) nFree++;
52   }
53
54   // allocate the arrays
55   myFree = new TColStd_HArray1OfInteger(1,2*nFree);
56   nInternal = (3*nbTriangles - nFree) / 2;
57   myInternals = new TColStd_HArray1OfInteger(1,2*nInternal);
58
59   TColStd_Array1OfInteger& Free     = myFree->ChangeArray1();
60   TColStd_Array1OfInteger& Internal = myInternals->ChangeArray1();
61
62   Standard_Integer fr = 1, in = 1;
63   const Poly_Array1OfTriangle& triangles = T->Triangles();
64   Standard_Integer n[3];
65   for (i = 1; i <= nbTriangles; i++) {
66     pc.Triangles(i,t[0],t[1],t[2]);
67     triangles(i).Get(n[0],n[1],n[2]);
68     for (j = 0; j < 3; j++) {
69       Standard_Integer k = (j+1) % 3;
70       if (t[j] == 0) {
71         Free(fr)   = n[j];
72         Free(fr+1) = n[k];
73         fr += 2;
74       }
75       // internal edge if this triangle has a lower index than the adjacent
76       else if (i < t[j]) {
77         Internal(in)   = n[j];
78         Internal(in+1) = n[k];
79         in += 2;
80       }
81     }
82   }
83 }
84
85 //=======================================================================
86 //function : Triangulation
87 //purpose  : 
88 //=======================================================================
89
90 Handle(Poly_Triangulation) DrawTrSurf_Triangulation2D::Triangulation() const 
91 {
92   return myTriangulation;
93 }
94
95 //=======================================================================
96 //function : DrawOn
97 //purpose  : 
98 //=======================================================================
99
100 void DrawTrSurf_Triangulation2D::DrawOn(Draw_Display& dis) const 
101 {
102   // Display the edges
103   Standard_Integer i,n;
104   if (myTriangulation->HasUVNodes()) {
105     
106     const TColgp_Array1OfPnt2d& Nodes = myTriangulation->UVNodes();
107     
108     // free edges
109     
110     dis.SetColor(Draw_rouge);
111     const TColStd_Array1OfInteger& Free = myFree->Array1();
112     n = Free.Length() / 2;
113     for (i = 1; i <= n; i++) {
114       dis.Draw(Nodes(Free(2*i-1)),Nodes(Free(2*i)));
115     }
116     
117     // internal edges
118     
119     dis.SetColor(Draw_bleu);
120     const TColStd_Array1OfInteger& Internal = myInternals->Array1();
121     n = Internal.Length() / 2;
122     for (i = 1; i <= n; i++) {
123       dis.Draw(Nodes(Internal(2*i-1)),Nodes(Internal(2*i)));
124     }
125     
126   }
127 }
128
129 //=======================================================================
130 //function : Copy
131 //purpose  : 
132 //=======================================================================
133
134 Handle(Draw_Drawable3D) DrawTrSurf_Triangulation2D::Copy() const 
135 {
136   return new DrawTrSurf_Triangulation2D(myTriangulation);
137 }
138
139 //=======================================================================
140 //function : Dump
141 //purpose  : 
142 //=======================================================================
143
144 void DrawTrSurf_Triangulation2D::Dump(Standard_OStream& S) const 
145 {
146   Poly::Dump(myTriangulation,S);
147 }
148
149 //=======================================================================
150 //function : Whatis
151 //purpose  : 
152 //=======================================================================
153
154 void DrawTrSurf_Triangulation2D::Whatis(Draw_Interpretor& I) const 
155 {
156   I << "triangulation";
157 }
158