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_NullObject.hxx>
23 #include <Standard_Type.hxx>
25 IMPLEMENT_STANDARD_RTTIEXT (Poly_Triangulation, Standard_Transient)
27 //=======================================================================
28 //function : Poly_Triangulation
30 //=======================================================================
31 Poly_Triangulation::Poly_Triangulation(const Standard_Integer theNbNodes,
32 const Standard_Integer theNbTriangles,
33 const Standard_Boolean theHasUVNodes)
35 myNodes (1, theNbNodes),
36 myTriangles (1, theNbTriangles)
38 if (theHasUVNodes) myUVNodes = new TColgp_HArray1OfPnt2d(1, theNbNodes);
41 //=======================================================================
42 //function : Poly_Triangulation
44 //=======================================================================
46 Poly_Triangulation::Poly_Triangulation(const TColgp_Array1OfPnt& theNodes,
47 const Poly_Array1OfTriangle& theTriangles)
49 myNodes (1, theNodes.Length()),
50 myTriangles (1, theTriangles.Length())
53 myTriangles = theTriangles;
56 //=======================================================================
57 //function : Poly_Triangulation
59 //=======================================================================
61 Poly_Triangulation::Poly_Triangulation(const TColgp_Array1OfPnt& theNodes,
62 const TColgp_Array1OfPnt2d& theUVNodes,
63 const Poly_Array1OfTriangle& theTriangles)
65 myNodes (1, theNodes.Length()),
66 myTriangles (1, theTriangles.Length())
69 myTriangles = theTriangles;
70 myUVNodes = new TColgp_HArray1OfPnt2d (1, theNodes.Length());
71 myUVNodes->ChangeArray1() = theUVNodes;
74 //=======================================================================
77 //=======================================================================
79 Handle(Poly_Triangulation) Poly_Triangulation::Copy() const
81 Handle(Poly_Triangulation) aCopy;
83 aCopy = new Poly_Triangulation(Nodes(), UVNodes(), Triangles());
85 aCopy = new Poly_Triangulation(Nodes(), Triangles());
86 aCopy->Deflection(myDeflection);
88 aCopy->myNormals = new TShort_HArray1OfShortReal(myNormals->Array1());
93 //=======================================================================
94 //function : Poly_Triangulation
96 //=======================================================================
98 Poly_Triangulation::Poly_Triangulation (const Handle(Poly_Triangulation)& theTriangulation)
99 : myDeflection ( theTriangulation->myDeflection ),
100 myNodes(theTriangulation->Nodes()),
101 myTriangles(theTriangulation->Triangles())
103 if (theTriangulation->HasUVNodes())
105 myUVNodes = new TColgp_HArray1OfPnt2d(theTriangulation->myUVNodes->Array1());
107 if (theTriangulation->HasNormals())
109 myNormals = new TShort_HArray1OfShortReal(theTriangulation->myNormals->Array1());
113 //=======================================================================
114 //function : Deflection
116 //=======================================================================
118 void Poly_Triangulation::Deflection(const Standard_Real theDeflection)
120 myDeflection = theDeflection;
123 //=======================================================================
124 //function : RemoveUVNodes
126 //=======================================================================
128 void Poly_Triangulation::RemoveUVNodes()
133 //=======================================================================
136 //=======================================================================
138 const gp_Pnt& Poly_Triangulation::Node (const Standard_Integer theIndex) const
140 if (theIndex < 1 || theIndex > myNodes.Size())
142 throw Standard_OutOfRange ("Poly_Triangulation::Node : index out of range");
144 return myNodes.Value (theIndex);
147 //=======================================================================
148 //function : ChangeNode
150 //=======================================================================
152 gp_Pnt& Poly_Triangulation::ChangeNode (const Standard_Integer theIndex)
154 if (theIndex < 1 || theIndex > myNodes.Size())
156 throw Standard_OutOfRange ("Poly_Triangulation::ChangeNode : index out of range");
158 return myNodes.ChangeValue (theIndex);
161 //=======================================================================
164 //=======================================================================
166 const gp_Pnt2d& Poly_Triangulation::UVNode (const Standard_Integer theIndex) const
168 if (myUVNodes.IsNull() || theIndex < 1 || theIndex > myUVNodes->Size())
170 throw Standard_OutOfRange ("Poly_Triangulation::UVNode : index out of range");
172 return myUVNodes->Value (theIndex);
175 //=======================================================================
176 //function : ChangeUVNode
178 //=======================================================================
180 gp_Pnt2d& Poly_Triangulation::ChangeUVNode (const Standard_Integer theIndex)
182 if (myUVNodes.IsNull() || theIndex < 1 || theIndex > myUVNodes->Size())
184 throw Standard_OutOfRange ("Poly_Triangulation::ChangeUVNode : index out of range");
186 return myUVNodes->ChangeValue (theIndex);
189 //=======================================================================
190 //function : Triangle
192 //=======================================================================
194 const Poly_Triangle& Poly_Triangulation::Triangle (const Standard_Integer theIndex) const
196 if (theIndex < 1 || theIndex > myTriangles.Size())
198 throw Standard_OutOfRange ("Poly_Triangulation::Triangle : index out of range");
200 return myTriangles.Value (theIndex);
203 //=======================================================================
204 //function : ChangeTriangle
206 //=======================================================================
208 Poly_Triangle& Poly_Triangulation::ChangeTriangle (const Standard_Integer theIndex)
210 if (theIndex < 1 || theIndex > myTriangles.Size())
212 throw Standard_OutOfRange ("Poly_Triangulation::ChangeTriangle : index out of range");
214 return myTriangles.ChangeValue (theIndex);
217 //=======================================================================
218 //function : SetNormals
220 //=======================================================================
222 void Poly_Triangulation::SetNormals (const Handle(TShort_HArray1OfShortReal)& theNormals)
225 if(theNormals.IsNull() || theNormals->Length() != 3 * NbNodes()) {
226 throw Standard_DomainError("Poly_Triangulation::SetNormals : wrong length");
229 myNormals = theNormals;
232 //=======================================================================
235 //=======================================================================
237 const TShort_Array1OfShortReal& Poly_Triangulation::Normals() const
240 if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) {
241 throw Standard_NullObject("Poly_Triangulation::Normals : "
242 "wrong length or null array");
245 return myNormals->Array1();
248 //=======================================================================
249 //function : ChangeNormals
251 //=======================================================================
253 TShort_Array1OfShortReal& Poly_Triangulation::ChangeNormals()
256 if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) {
257 throw Standard_NullObject("Poly_Triangulation::ChangeNormals : "
258 "wrong length or null array");
261 return myNormals->ChangeArray1();
264 //=======================================================================
265 //function : HasNormals
267 //=======================================================================
269 Standard_Boolean Poly_Triangulation::HasNormals() const
271 if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) {
272 return Standard_False;
274 return Standard_True;
277 //=======================================================================
278 //function : SetNormal
280 //=======================================================================
282 void Poly_Triangulation::SetNormal (const Standard_Integer theIndex, const gp_Dir& theNormal)
284 if (myNormals.IsNull() || theIndex < 1 || theIndex > myNodes.Size())
286 throw Standard_NullObject ("Poly_Triangulation::SetNormal : empty array or index out of range");
289 myNormals->ChangeValue (theIndex * 3 - 2) = (Standard_ShortReal) theNormal.X();
290 myNormals->ChangeValue (theIndex * 3 - 1) = (Standard_ShortReal) theNormal.Y();
291 myNormals->ChangeValue (theIndex * 3) = (Standard_ShortReal) theNormal.Z();
294 //=======================================================================
297 //=======================================================================
299 gp_Dir Poly_Triangulation::Normal (const Standard_Integer theIndex) const
301 if (myNormals.IsNull() || theIndex < 1 || theIndex > myNodes.Size())
303 throw Standard_NullObject ("Poly_Triangulation::Normal : empty array or index out of range");
306 gp_Dir N(myNormals->Value(theIndex * 3 - 2),
307 myNormals->Value(theIndex * 3 - 1),
308 myNormals->Value(theIndex * 3));