0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / Poly / Poly_Triangulation.cxx
CommitLineData
b311480e 1// Created on: 1995-03-06
2// Created by: Laurent PAINNOT
3// Copyright (c) 1995-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
624f732f 17#include <Poly_Triangulation.hxx>
42cf5bc1 18
7fd59977 19#include <gp_Pnt.hxx>
20#include <Poly_Triangle.hxx>
42cf5bc1 21#include <Standard_DomainError.hxx>
bc73b006 22#include <Standard_Dump.hxx>
42cf5bc1 23#include <Standard_NullObject.hxx>
24#include <Standard_Type.hxx>
7fd59977 25
25e59720 26IMPLEMENT_STANDARD_RTTIEXT (Poly_Triangulation, Standard_Transient)
92efcf78 27
7fd59977 28//=======================================================================
29//function : Poly_Triangulation
30//purpose :
31//=======================================================================
4954e497 32Poly_Triangulation::Poly_Triangulation(const Standard_Integer theNbNodes,
33 const Standard_Integer theNbTriangles,
34 const Standard_Boolean theHasUVNodes)
35: myDeflection(0),
36 myNodes (1, theNbNodes),
37 myTriangles (1, theNbTriangles)
7fd59977 38{
4954e497 39 if (theHasUVNodes) myUVNodes = new TColgp_HArray1OfPnt2d(1, theNbNodes);
7fd59977 40}
41
42//=======================================================================
43//function : Poly_Triangulation
44//purpose :
45//=======================================================================
46
4954e497 47Poly_Triangulation::Poly_Triangulation(const TColgp_Array1OfPnt& theNodes,
48 const Poly_Array1OfTriangle& theTriangles)
49: myDeflection(0),
50 myNodes (1, theNodes.Length()),
51 myTriangles (1, theTriangles.Length())
7fd59977 52{
4954e497 53 myNodes = theNodes;
54 myTriangles = theTriangles;
7fd59977 55}
56
7fd59977 57//=======================================================================
58//function : Poly_Triangulation
59//purpose :
60//=======================================================================
61
4954e497 62Poly_Triangulation::Poly_Triangulation(const TColgp_Array1OfPnt& theNodes,
63 const TColgp_Array1OfPnt2d& theUVNodes,
64 const Poly_Array1OfTriangle& theTriangles)
65: myDeflection(0),
66 myNodes (1, theNodes.Length()),
67 myTriangles (1, theTriangles.Length())
7fd59977 68{
4954e497 69 myNodes = theNodes;
70 myTriangles = theTriangles;
71 myUVNodes = new TColgp_HArray1OfPnt2d (1, theNodes.Length());
72 myUVNodes->ChangeArray1() = theUVNodes;
7fd59977 73}
74
55e738d2 75//=======================================================================
76//function : Copy
77//purpose :
78//=======================================================================
79
8156dddd 80Handle(Poly_Triangulation) Poly_Triangulation::Copy() const
55e738d2 81{
82 Handle(Poly_Triangulation) aCopy;
83 if (HasUVNodes())
84 aCopy = new Poly_Triangulation(Nodes(), UVNodes(), Triangles());
85 else
86 aCopy = new Poly_Triangulation(Nodes(), Triangles());
87 aCopy->Deflection(myDeflection);
88 if (HasNormals())
89 aCopy->myNormals = new TShort_HArray1OfShortReal(myNormals->Array1());
90
91 return aCopy;
92}
93
7fd59977 94//=======================================================================
624f732f 95//function : Poly_Triangulation
7fd59977 96//purpose :
97//=======================================================================
98
624f732f 99Poly_Triangulation::Poly_Triangulation (const Handle(Poly_Triangulation)& theTriangulation)
100: myDeflection ( theTriangulation->myDeflection ),
101 myNodes(theTriangulation->Nodes()),
102 myTriangles(theTriangulation->Triangles())
7fd59977 103{
624f732f 104 if (theTriangulation->HasUVNodes())
105 {
106 myUVNodes = new TColgp_HArray1OfPnt2d(theTriangulation->myUVNodes->Array1());
107 }
108 if (theTriangulation->HasNormals())
109 {
110 myNormals = new TShort_HArray1OfShortReal(theTriangulation->myNormals->Array1());
111 }
7fd59977 112}
113
114//=======================================================================
115//function : Deflection
116//purpose :
117//=======================================================================
118
4954e497 119void Poly_Triangulation::Deflection(const Standard_Real theDeflection)
624f732f 120{
121 myDeflection = theDeflection;
122}
7fd59977 123
124//=======================================================================
125//function : RemoveUVNodes
126//purpose :
127//=======================================================================
128
129void Poly_Triangulation::RemoveUVNodes()
130{
131 myUVNodes.Nullify();
132}
133
624f732f 134//=======================================================================
135//function : Node
136//purpose :
137//=======================================================================
138
139const gp_Pnt& Poly_Triangulation::Node (const Standard_Integer theIndex) const
140{
141 if (theIndex < 1 || theIndex > myNodes.Size())
142 {
9775fa61 143 throw Standard_OutOfRange ("Poly_Triangulation::Node : index out of range");
624f732f 144 }
145 return myNodes.Value (theIndex);
146}
147
148//=======================================================================
149//function : ChangeNode
150//purpose :
151//=======================================================================
152
153gp_Pnt& Poly_Triangulation::ChangeNode (const Standard_Integer theIndex)
154{
155 if (theIndex < 1 || theIndex > myNodes.Size())
156 {
9775fa61 157 throw Standard_OutOfRange ("Poly_Triangulation::ChangeNode : index out of range");
624f732f 158 }
159 return myNodes.ChangeValue (theIndex);
160}
161
624f732f 162//=======================================================================
163//function : UVNode
164//purpose :
165//=======================================================================
166
167const gp_Pnt2d& Poly_Triangulation::UVNode (const Standard_Integer theIndex) const
168{
169 if (myUVNodes.IsNull() || theIndex < 1 || theIndex > myUVNodes->Size())
170 {
9775fa61 171 throw Standard_OutOfRange ("Poly_Triangulation::UVNode : index out of range");
624f732f 172 }
173 return myUVNodes->Value (theIndex);
174}
175
176//=======================================================================
177//function : ChangeUVNode
178//purpose :
179//=======================================================================
180
181gp_Pnt2d& Poly_Triangulation::ChangeUVNode (const Standard_Integer theIndex)
182{
183 if (myUVNodes.IsNull() || theIndex < 1 || theIndex > myUVNodes->Size())
184 {
9775fa61 185 throw Standard_OutOfRange ("Poly_Triangulation::ChangeUVNode : index out of range");
624f732f 186 }
187 return myUVNodes->ChangeValue (theIndex);
188}
189
624f732f 190//=======================================================================
191//function : Triangle
192//purpose :
193//=======================================================================
194
195const Poly_Triangle& Poly_Triangulation::Triangle (const Standard_Integer theIndex) const
196{
197 if (theIndex < 1 || theIndex > myTriangles.Size())
198 {
9775fa61 199 throw Standard_OutOfRange ("Poly_Triangulation::Triangle : index out of range");
624f732f 200 }
201 return myTriangles.Value (theIndex);
202}
203
204//=======================================================================
205//function : ChangeTriangle
206//purpose :
207//=======================================================================
208
209Poly_Triangle& Poly_Triangulation::ChangeTriangle (const Standard_Integer theIndex)
210{
211 if (theIndex < 1 || theIndex > myTriangles.Size())
212 {
9775fa61 213 throw Standard_OutOfRange ("Poly_Triangulation::ChangeTriangle : index out of range");
624f732f 214 }
215 return myTriangles.ChangeValue (theIndex);
216}
7fd59977 217
218//=======================================================================
219//function : SetNormals
220//purpose :
221//=======================================================================
222
624f732f 223void Poly_Triangulation::SetNormals (const Handle(TShort_HArray1OfShortReal)& theNormals)
7fd59977 224{
225
4954e497 226 if(theNormals.IsNull() || theNormals->Length() != 3 * NbNodes()) {
9775fa61 227 throw Standard_DomainError("Poly_Triangulation::SetNormals : wrong length");
7fd59977 228 }
229
230 myNormals = theNormals;
7fd59977 231}
232
233//=======================================================================
234//function : Normals
235//purpose :
236//=======================================================================
237
238const TShort_Array1OfShortReal& Poly_Triangulation::Normals() const
239{
240
4954e497 241 if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) {
9775fa61 242 throw Standard_NullObject("Poly_Triangulation::Normals : "
243 "wrong length or null array");
7fd59977 244 }
245
246 return myNormals->Array1();
7fd59977 247}
248
249//=======================================================================
250//function : ChangeNormals
251//purpose :
252//=======================================================================
253
624f732f 254TShort_Array1OfShortReal& Poly_Triangulation::ChangeNormals()
7fd59977 255{
256
4954e497 257 if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) {
9775fa61 258 throw Standard_NullObject("Poly_Triangulation::ChangeNormals : "
259 "wrong length or null array");
7fd59977 260 }
261
262 return myNormals->ChangeArray1();
7fd59977 263}
264
265//=======================================================================
266//function : HasNormals
267//purpose :
268//=======================================================================
269
270Standard_Boolean Poly_Triangulation::HasNormals() const
271{
4954e497 272 if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) {
7fd59977 273 return Standard_False;
274 }
275 return Standard_True;
276}
277
624f732f 278//=======================================================================
279//function : SetNormal
280//purpose :
281//=======================================================================
282
283void Poly_Triangulation::SetNormal (const Standard_Integer theIndex, const gp_Dir& theNormal)
284{
285 if (myNormals.IsNull() || theIndex < 1 || theIndex > myNodes.Size())
286 {
9775fa61 287 throw Standard_NullObject ("Poly_Triangulation::SetNormal : empty array or index out of range");
624f732f 288 }
289
290 myNormals->ChangeValue (theIndex * 3 - 2) = (Standard_ShortReal) theNormal.X();
291 myNormals->ChangeValue (theIndex * 3 - 1) = (Standard_ShortReal) theNormal.Y();
292 myNormals->ChangeValue (theIndex * 3) = (Standard_ShortReal) theNormal.Z();
293}
294
295//=======================================================================
296//function : Normal
297//purpose :
298//=======================================================================
299
4ea76aea 300gp_Dir Poly_Triangulation::Normal (const Standard_Integer theIndex) const
624f732f 301{
302 if (myNormals.IsNull() || theIndex < 1 || theIndex > myNodes.Size())
303 {
9775fa61 304 throw Standard_NullObject ("Poly_Triangulation::Normal : empty array or index out of range");
624f732f 305 }
7fd59977 306
4ea76aea 307 gp_Dir N(myNormals->Value(theIndex * 3 - 2),
308 myNormals->Value(theIndex * 3 - 1),
309 myNormals->Value(theIndex * 3));
7fd59977 310
624f732f 311 return N;
312}
bc73b006 313
314// =======================================================================
315// function : DumpJson
316// purpose :
317// =======================================================================
318void Poly_Triangulation::DumpJson (Standard_OStream& theOStream, Standard_Integer) const
319{
320 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
321
322 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDeflection)
323
324 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myNodes.Size())
325 if (!myUVNodes.IsNull())
326 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myUVNodes->Size())
327 if (!myNormals.IsNull())
328 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myNormals->Size())
329 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myTriangles.Size())
330}