b367b5a37d22ba43a520b223db6b76de41a93bf0
[occt.git] / src / Poly / Poly_Triangulation.cxx
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
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 <Poly_Triangulation.hxx>
18
19 #include <gp_Pnt.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>
25
26 IMPLEMENT_STANDARD_RTTIEXT (Poly_Triangulation, Standard_Transient)
27
28 //=======================================================================
29 //function : Poly_Triangulation
30 //purpose  : 
31 //=======================================================================
32 Poly_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)
38 {
39   if (theHasUVNodes) myUVNodes = new TColgp_HArray1OfPnt2d(1, theNbNodes);
40 }
41
42 //=======================================================================
43 //function : Poly_Triangulation
44 //purpose  : 
45 //=======================================================================
46
47 Poly_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())
52 {
53   myNodes = theNodes;
54   myTriangles = theTriangles;
55 }
56
57 //=======================================================================
58 //function : Poly_Triangulation
59 //purpose  : 
60 //=======================================================================
61
62 Poly_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())
68 {
69   myNodes = theNodes;
70   myTriangles = theTriangles;
71   myUVNodes = new TColgp_HArray1OfPnt2d (1, theNodes.Length());
72   myUVNodes->ChangeArray1() = theUVNodes;
73 }
74
75 //=======================================================================
76 //function : Copy
77 //purpose  : 
78 //=======================================================================
79
80 Handle(Poly_Triangulation) Poly_Triangulation::Copy() const
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
94 //=======================================================================
95 //function : Poly_Triangulation
96 //purpose  : 
97 //=======================================================================
98
99 Poly_Triangulation::Poly_Triangulation (const Handle(Poly_Triangulation)& theTriangulation)
100 : myDeflection ( theTriangulation->myDeflection ),
101   myNodes(theTriangulation->Nodes()),
102   myTriangles(theTriangulation->Triangles())
103 {
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   }
112 }
113
114 //=======================================================================
115 //function : Deflection
116 //purpose  : 
117 //=======================================================================
118
119 void Poly_Triangulation::Deflection(const Standard_Real theDeflection)
120 {
121   myDeflection = theDeflection;
122 }
123
124 //=======================================================================
125 //function : RemoveUVNodes
126 //purpose  : 
127 //=======================================================================
128
129 void Poly_Triangulation::RemoveUVNodes()
130 {
131   myUVNodes.Nullify();
132 }
133
134 //=======================================================================
135 //function : Node
136 //purpose  : 
137 //=======================================================================
138
139 const gp_Pnt& Poly_Triangulation::Node (const Standard_Integer theIndex) const
140 {
141   if (theIndex < 1 || theIndex > myNodes.Size())
142   {
143     throw Standard_OutOfRange ("Poly_Triangulation::Node : index out of range");
144   }
145   return myNodes.Value (theIndex);
146 }
147
148 //=======================================================================
149 //function : ChangeNode
150 //purpose  : 
151 //=======================================================================
152
153 gp_Pnt& Poly_Triangulation::ChangeNode (const Standard_Integer theIndex)
154 {
155   if (theIndex < 1 || theIndex > myNodes.Size())
156   {
157     throw Standard_OutOfRange ("Poly_Triangulation::ChangeNode : index out of range");
158   }
159   return myNodes.ChangeValue (theIndex);
160 }
161
162 //=======================================================================
163 //function : UVNode
164 //purpose  : 
165 //=======================================================================
166
167 const gp_Pnt2d& Poly_Triangulation::UVNode (const Standard_Integer theIndex) const
168 {
169   if (myUVNodes.IsNull() || theIndex < 1 || theIndex > myUVNodes->Size())
170   {
171     throw Standard_OutOfRange ("Poly_Triangulation::UVNode : index out of range");
172   }
173   return myUVNodes->Value (theIndex);
174 }
175
176 //=======================================================================
177 //function : ChangeUVNode
178 //purpose  : 
179 //=======================================================================
180
181 gp_Pnt2d& Poly_Triangulation::ChangeUVNode (const Standard_Integer theIndex)
182 {
183   if (myUVNodes.IsNull() || theIndex < 1 || theIndex > myUVNodes->Size())
184   {
185     throw Standard_OutOfRange ("Poly_Triangulation::ChangeUVNode : index out of range");
186   }
187   return myUVNodes->ChangeValue (theIndex);
188 }
189
190 //=======================================================================
191 //function : Triangle
192 //purpose  : 
193 //=======================================================================
194
195 const Poly_Triangle& Poly_Triangulation::Triangle (const Standard_Integer theIndex) const
196 {
197   if (theIndex < 1 || theIndex > myTriangles.Size())
198   {
199     throw Standard_OutOfRange ("Poly_Triangulation::Triangle : index out of range");
200   }
201   return myTriangles.Value (theIndex);
202 }
203
204 //=======================================================================
205 //function : ChangeTriangle
206 //purpose  : 
207 //=======================================================================
208
209 Poly_Triangle& Poly_Triangulation::ChangeTriangle (const Standard_Integer theIndex)
210 {
211   if (theIndex < 1 || theIndex > myTriangles.Size())
212   {
213     throw Standard_OutOfRange ("Poly_Triangulation::ChangeTriangle : index out of range");
214   }
215   return myTriangles.ChangeValue (theIndex);
216 }
217
218 //=======================================================================
219 //function : SetNormals
220 //purpose  : 
221 //=======================================================================
222
223 void Poly_Triangulation::SetNormals (const Handle(TShort_HArray1OfShortReal)& theNormals)
224 {
225
226   if(theNormals.IsNull() || theNormals->Length() != 3 * NbNodes()) {
227     throw Standard_DomainError("Poly_Triangulation::SetNormals : wrong length");
228   }
229
230   myNormals = theNormals;
231 }
232
233 //=======================================================================
234 //function : Normals
235 //purpose  : 
236 //=======================================================================
237
238 const TShort_Array1OfShortReal& Poly_Triangulation::Normals() const
239 {
240
241   if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) {
242     throw Standard_NullObject("Poly_Triangulation::Normals : "
243                               "wrong length or null array");
244   }
245
246   return myNormals->Array1();
247 }
248
249 //=======================================================================
250 //function : ChangeNormals
251 //purpose  : 
252 //=======================================================================
253
254 TShort_Array1OfShortReal& Poly_Triangulation::ChangeNormals()
255 {
256
257   if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) {
258     throw Standard_NullObject("Poly_Triangulation::ChangeNormals : "
259                               "wrong length or null array");
260   }
261
262   return myNormals->ChangeArray1();
263 }
264
265 //=======================================================================
266 //function : HasNormals
267 //purpose  : 
268 //=======================================================================
269
270 Standard_Boolean Poly_Triangulation::HasNormals() const
271 {
272   if(myNormals.IsNull() || myNormals->Length() != 3 * NbNodes()) {
273     return Standard_False;
274   }
275   return Standard_True;
276 }
277
278 //=======================================================================
279 //function : SetNormal
280 //purpose  : 
281 //=======================================================================
282
283 void Poly_Triangulation::SetNormal (const Standard_Integer theIndex, const gp_Dir& theNormal)
284 {
285   if (myNormals.IsNull() || theIndex < 1 || theIndex > myNodes.Size())
286   {
287     throw Standard_NullObject ("Poly_Triangulation::SetNormal : empty array or index out of range");
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
300 gp_Dir Poly_Triangulation::Normal (const Standard_Integer theIndex) const
301 {
302   if (myNormals.IsNull() || theIndex < 1 || theIndex > myNodes.Size())
303   {
304     throw Standard_NullObject ("Poly_Triangulation::Normal : empty array or index out of range");
305   }
306
307   gp_Dir N(myNormals->Value(theIndex * 3 - 2),
308            myNormals->Value(theIndex * 3 - 1),
309            myNormals->Value(theIndex * 3));
310
311   return N;
312 }
313
314 // =======================================================================
315 // function : DumpJson
316 // purpose  :
317 // =======================================================================
318 void 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 }