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
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <Poly_Triangulation.hxx>
20 #include <Poly_Triangle.hxx>
21 #include <Standard_DomainError.hxx>
22 #include <Standard_Dump.hxx>
23 #include <Standard_NullObject.hxx>
24 #include <Standard_Type.hxx>
26 IMPLEMENT_STANDARD_RTTIEXT (Poly_Triangulation, Standard_Transient)
28 //=======================================================================
29 //function : Poly_Triangulation
31 //=======================================================================
32 Poly_Triangulation::Poly_Triangulation(const Standard_Integer theNbNodes,
33 const Standard_Integer theNbTriangles,
34 const Standard_Boolean theHasUVNodes)
36 myNodes (1, theNbNodes),
37 myTriangles (1, theNbTriangles)
39 if (theHasUVNodes) myUVNodes = new TColgp_HArray1OfPnt2d(1, theNbNodes);
42 //=======================================================================
43 //function : Poly_Triangulation
45 //=======================================================================
47 Poly_Triangulation::Poly_Triangulation(const TColgp_Array1OfPnt& theNodes,
48 const Poly_Array1OfTriangle& theTriangles)
50 myNodes (1, theNodes.Length()),
51 myTriangles (1, theTriangles.Length())
54 myTriangles = theTriangles;
57 //=======================================================================
58 //function : Poly_Triangulation
60 //=======================================================================
62 Poly_Triangulation::Poly_Triangulation(const TColgp_Array1OfPnt& theNodes,
63 const TColgp_Array1OfPnt2d& theUVNodes,
64 const Poly_Array1OfTriangle& theTriangles)
66 myNodes (1, theNodes.Length()),
67 myTriangles (1, theTriangles.Length())
70 myTriangles = theTriangles;
71 myUVNodes = new TColgp_HArray1OfPnt2d (1, theNodes.Length());
72 myUVNodes->ChangeArray1() = theUVNodes;
75 //=======================================================================
78 //=======================================================================
80 Handle(Poly_Triangulation) Poly_Triangulation::Copy() const
82 Handle(Poly_Triangulation) aCopy;
84 aCopy = new Poly_Triangulation(Nodes(), UVNodes(), Triangles());
86 aCopy = new Poly_Triangulation(Nodes(), Triangles());
87 aCopy->Deflection(myDeflection);
89 aCopy->myNormals = new TShort_HArray1OfShortReal(myNormals->Array1());
94 //=======================================================================
95 //function : Poly_Triangulation
97 //=======================================================================
99 Poly_Triangulation::Poly_Triangulation (const Handle(Poly_Triangulation)& theTriangulation)
100 : myDeflection ( theTriangulation->myDeflection ),
101 myNodes(theTriangulation->Nodes()),
102 myTriangles(theTriangulation->Triangles())
104 if (theTriangulation->HasUVNodes())
106 myUVNodes = new TColgp_HArray1OfPnt2d(theTriangulation->myUVNodes->Array1());
108 if (theTriangulation->HasNormals())
110 myNormals = new TShort_HArray1OfShortReal(theTriangulation->myNormals->Array1());
114 //=======================================================================
115 //function : Deflection
117 //=======================================================================
119 void Poly_Triangulation::Deflection(const Standard_Real theDeflection)
121 myDeflection = theDeflection;
124 //=======================================================================
125 //function : RemoveUVNodes
127 //=======================================================================
129 void Poly_Triangulation::RemoveUVNodes()
134 //=======================================================================
137 //=======================================================================
139 const gp_Pnt& Poly_Triangulation::Node (const Standard_Integer theIndex) const
141 if (theIndex < 1 || theIndex > myNodes.Size())
143 throw Standard_OutOfRange ("Poly_Triangulation::Node : index out of range");
145 return myNodes.Value (theIndex);
148 //=======================================================================
149 //function : ChangeNode
151 //=======================================================================
153 gp_Pnt& Poly_Triangulation::ChangeNode (const Standard_Integer theIndex)
155 if (theIndex < 1 || theIndex > myNodes.Size())
157 throw Standard_OutOfRange ("Poly_Triangulation::ChangeNode : index out of range");
159 return myNodes.ChangeValue (theIndex);
162 //=======================================================================
165 //=======================================================================
167 const gp_Pnt2d& Poly_Triangulation::UVNode (const Standard_Integer theIndex) const
169 if (myUVNodes.IsNull() || theIndex < 1 || theIndex > myUVNodes->Size())
171 throw Standard_OutOfRange ("Poly_Triangulation::UVNode : index out of range");
173 return myUVNodes->Value (theIndex);
176 //=======================================================================
177 //function : ChangeUVNode
179 //=======================================================================
181 gp_Pnt2d& Poly_Triangulation::ChangeUVNode (const Standard_Integer theIndex)
183 if (myUVNodes.IsNull() || theIndex < 1 || theIndex > myUVNodes->Size())
185 throw Standard_OutOfRange ("Poly_Triangulation::ChangeUVNode : index out of range");
187 return myUVNodes->ChangeValue (theIndex);
190 //=======================================================================
191 //function : Triangle
193 //=======================================================================
195 const Poly_Triangle& Poly_Triangulation::Triangle (const Standard_Integer theIndex) const
197 if (theIndex < 1 || theIndex > myTriangles.Size())
199 throw Standard_OutOfRange ("Poly_Triangulation::Triangle : index out of range");
201 return myTriangles.Value (theIndex);
204 //=======================================================================
205 //function : ChangeTriangle
207 //=======================================================================
209 Poly_Triangle& Poly_Triangulation::ChangeTriangle (const Standard_Integer theIndex)
211 if (theIndex < 1 || theIndex > myTriangles.Size())
213 throw Standard_OutOfRange ("Poly_Triangulation::ChangeTriangle : index out of range");
215 return myTriangles.ChangeValue (theIndex);
218 //=======================================================================
219 //function : SetNormals
221 //=======================================================================
223 void Poly_Triangulation::SetNormals (const Handle(TShort_HArray1OfShortReal)& theNormals)
226 if(theNormals.IsNull() || theNormals->Length() != 3 * NbNodes()) {
227 throw Standard_DomainError("Poly_Triangulation::SetNormals : wrong length");
230 myNormals = theNormals;
233 //=======================================================================
236 //=======================================================================
238 const TShort_Array1OfShortReal& Poly_Triangulation::Normals() const
241 if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) {
242 throw Standard_NullObject("Poly_Triangulation::Normals : "
243 "wrong length or null array");
246 return myNormals->Array1();
249 //=======================================================================
250 //function : ChangeNormals
252 //=======================================================================
254 TShort_Array1OfShortReal& Poly_Triangulation::ChangeNormals()
257 if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) {
258 throw Standard_NullObject("Poly_Triangulation::ChangeNormals : "
259 "wrong length or null array");
262 return myNormals->ChangeArray1();
265 //=======================================================================
266 //function : HasNormals
268 //=======================================================================
270 Standard_Boolean Poly_Triangulation::HasNormals() const
272 if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) {
273 return Standard_False;
275 return Standard_True;
278 //=======================================================================
279 //function : SetNormal
281 //=======================================================================
283 void Poly_Triangulation::SetNormal (const Standard_Integer theIndex, const gp_Dir& theNormal)
285 if (myNormals.IsNull() || theIndex < 1 || theIndex > myNodes.Size())
287 throw Standard_NullObject ("Poly_Triangulation::SetNormal : empty array or index out of range");
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();
295 //=======================================================================
298 //=======================================================================
300 gp_Dir Poly_Triangulation::Normal (const Standard_Integer theIndex) const
302 if (myNormals.IsNull() || theIndex < 1 || theIndex > myNodes.Size())
304 throw Standard_NullObject ("Poly_Triangulation::Normal : empty array or index out of range");
307 gp_Dir N(myNormals->Value(theIndex * 3 - 2),
308 myNormals->Value(theIndex * 3 - 1),
309 myNormals->Value(theIndex * 3));
314 // =======================================================================
315 // function : DumpJson
317 // =======================================================================
318 void Poly_Triangulation::DumpJson (Standard_OStream& theOStream, Standard_Integer) const
320 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
322 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDeflection)
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())