0030480: Visualization - Clear of Select3D_SensitiveGroup does not update internal...
[occt.git] / src / TDataXtd / TDataXtd_Triangulation.cxx
1 // Created on: 2016-11-10
2 // Created by: Anton KOZULIN
3 // Copyright (c) 2016 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <TDataXtd_Triangulation.hxx>
17 #include <Standard_GUID.hxx>
18 #include <Standard_Type.hxx>
19 #include <TDF_Attribute.hxx>
20 #include <TDF_Label.hxx>
21 #include <TDF_RelocationTable.hxx>
22
23 //=======================================================================
24 //function : GetID
25 //purpose  : Returns the ID of the triangulation attribute.
26 //=======================================================================
27 const Standard_GUID& TDataXtd_Triangulation::GetID()
28 {
29   static Standard_GUID TDataXtd_TriangulationID ("27AE2C44-60B0-41AE-AC18-BA3FDA538D03");
30   return TDataXtd_TriangulationID; 
31 }
32
33 //=======================================================================
34 //function : Set
35 //purpose  : Finds or creates a triangulation attribute.
36 //=======================================================================
37 Handle(TDataXtd_Triangulation) TDataXtd_Triangulation::Set(const TDF_Label& theLabel)
38 {
39   Handle(TDataXtd_Triangulation) A;
40   if (!theLabel.FindAttribute (TDataXtd_Triangulation::GetID(), A))
41   {
42     A = new TDataXtd_Triangulation;
43     theLabel.AddAttribute(A);
44   }
45   return A;
46 }
47
48 //=======================================================================
49 //function : Set
50 //purpose  : Finds or creates a triangulation attribute.
51 //           Initializes the attribute by a Poly_Triangulation object.
52 //=======================================================================
53 Handle(TDataXtd_Triangulation) TDataXtd_Triangulation::Set(const TDF_Label& theLabel, const Handle(Poly_Triangulation)& theMesh)
54 {
55   Handle(TDataXtd_Triangulation) M = TDataXtd_Triangulation::Set(theLabel);
56   M->Set(theMesh);
57   return M;
58 }
59
60 //=======================================================================
61 //function : TDataXtd_Triangulation
62 //purpose  : A constructor.
63 //           Don't use it directly, 
64 //           use please the static method Set(),
65 //           which returns the attribute attached to a label.
66 //=======================================================================
67 TDataXtd_Triangulation::TDataXtd_Triangulation()
68 {
69
70 }
71
72 //=======================================================================
73 //function : TDataXtd_Triangulation
74 //purpose  : Sets the triangulation.
75 //=======================================================================
76 void TDataXtd_Triangulation::Set(const Handle(Poly_Triangulation)& theTriangulation)
77 {
78   Backup();
79   myTriangulation = theTriangulation;
80 }
81
82 //=======================================================================
83 //function : TDataXtd_Triangulation
84 //purpose  : Returns the underlying mesh.
85 //=======================================================================
86 const Handle(Poly_Triangulation)& TDataXtd_Triangulation::Get() const
87 {
88   return myTriangulation;
89 }
90
91 // Poly_Triangulation methods
92
93 // The methods are "covered" by this attribute to prevent direct modification of the mesh.
94 // There is no performance problem to call Poly_Triangulation method through this attribute.
95 // The most of the methods are considered as "inline" by the compiler in release mode.
96
97 //=======================================================================
98 //function : Deflection
99 //purpose  : Returns the deflection of this triangulation.
100 //=======================================================================
101 Standard_Real TDataXtd_Triangulation::Deflection() const
102 {
103   return myTriangulation->Deflection();
104 }
105
106 //=======================================================================
107 //function : Deflection
108 //purpose  : Sets the deflection of this triangulation to theDeflection.
109 //           See more on deflection in Polygon2D
110 //=======================================================================
111 void TDataXtd_Triangulation::Deflection (const Standard_Real theDeflection)
112 {
113   Backup();
114   myTriangulation->Deflection(theDeflection);
115 }
116
117 //=======================================================================
118 //function : RemoveUVNodes
119 //purpose  : Deallocates the UV nodes.
120 //=======================================================================
121 void TDataXtd_Triangulation::RemoveUVNodes()
122 {
123   Backup();
124   myTriangulation->RemoveUVNodes();
125 }
126
127 //=======================================================================
128 //function : NbNodes
129 //purpose  : return the number of nodes for this triangulation.
130 //=======================================================================
131 Standard_Integer TDataXtd_Triangulation::NbNodes() const
132 {
133   return myTriangulation->NbNodes();
134 }
135
136 //=======================================================================
137 //function : NbTriangles
138 //purpose  : return the number of triangles for this triangulation.
139 //=======================================================================
140 Standard_Integer TDataXtd_Triangulation::NbTriangles() const
141 {
142   return myTriangulation->NbTriangles();
143 }
144
145 //=======================================================================
146 //function : HasUVNodes
147 //purpose  : return Standard_True if 2D nodes are associated with 3D nodes for this triangulation.
148 //=======================================================================
149 Standard_Boolean TDataXtd_Triangulation::HasUVNodes() const
150 {
151   return myTriangulation->HasUVNodes();
152 }
153
154 //=======================================================================
155 //function : Node
156 //purpose  : return node at the given index.
157 //           Raises Standard_OutOfRange exception if theIndex is less than 1 or greater than NbNodes.
158 //=======================================================================
159 const gp_Pnt& TDataXtd_Triangulation::Node (const Standard_Integer theIndex) const
160 {
161   return myTriangulation->Node(theIndex);
162 }
163
164 //=======================================================================
165 //function : SetNode
166 //purpose  : The method differs from Poly_Triangulation
167 //           Sets a node at the given index.
168 //           Raises Standard_OutOfRange exception if theIndex is less than 1 or greater than NbNodes.
169 //=======================================================================
170 void TDataXtd_Triangulation::SetNode (const Standard_Integer theIndex, const gp_Pnt& theNode)
171 {
172   Backup();
173   myTriangulation->ChangeNode(theIndex) = theNode;
174 }
175
176 //=======================================================================
177 //function : UVNode
178 //purpose  : return UVNode at the given index.
179 //           Raises Standard_OutOfRange exception if theIndex is less than 1 or greater than NbNodes.
180 //=======================================================================
181 const gp_Pnt2d& TDataXtd_Triangulation::UVNode (const Standard_Integer theIndex) const
182 {
183   return myTriangulation->UVNode(theIndex);
184 }
185
186 //=======================================================================
187 //function : SetUVNode
188 //purpose  : The method differs from Poly_Triangulation
189 //           Sets a UVNode at the given index.
190 //           Raises Standard_OutOfRange exception if theIndex is less than 1 or greater than NbNodes.
191 //=======================================================================
192 void TDataXtd_Triangulation::SetUVNode (const Standard_Integer theIndex, const gp_Pnt2d& theUVNode)
193 {
194   Backup();
195   myTriangulation->ChangeUVNode(theIndex) = theUVNode;
196 }
197
198 //=======================================================================
199 //function : Triangle
200 //purpose  : return triangle at the given index.
201 //           Raises Standard_OutOfRange exception if theIndex is less than 1 or greater than NbTriangles.
202 //=======================================================================
203 const Poly_Triangle& TDataXtd_Triangulation::Triangle (const Standard_Integer theIndex) const
204 {
205   return myTriangulation->Triangle(theIndex);
206 }
207
208 //=======================================================================
209 //function : SetTriangle
210 //purpose  : The method differs from Poly_Triangulation
211 //           Sets a triangle at the given index.
212 //           Raises Standard_OutOfRange exception if theIndex is less than 1 or greater than NbTriangles.
213 //=======================================================================
214 void TDataXtd_Triangulation::SetTriangle (const Standard_Integer theIndex, const Poly_Triangle& theTriangle)
215 {
216   Backup();
217   myTriangulation->ChangeTriangle(theIndex) = theTriangle;
218 }
219
220 //=======================================================================
221 //function : SetNormals
222 //purpose  : Sets the table of node normals.
223 //           Raises exception if length of theNormals = 3 * NbNodes
224 //=======================================================================
225 void TDataXtd_Triangulation::SetNormals (const Handle(TShort_HArray1OfShortReal)& theNormals)
226 {
227   Backup();
228   myTriangulation->SetNormals(theNormals);
229 }
230
231 //=======================================================================
232 //function : SetNormal
233 //purpose  : Changes normal at the given index.
234 //           Raises Standard_OutOfRange exception.
235 //=======================================================================
236 void TDataXtd_Triangulation::SetNormal (const Standard_Integer theIndex,
237                                const gp_Dir&          theNormal)
238 {
239   Backup();
240   myTriangulation->SetNormal(theIndex, theNormal);
241 }
242
243 //=======================================================================
244 //function : HasNormals
245 //purpose  : Returns Standard_True if nodal normals are defined.
246 //=======================================================================
247 Standard_Boolean TDataXtd_Triangulation::HasNormals() const
248 {
249   return myTriangulation->HasNormals();
250 }
251
252 //=======================================================================
253 //function : Normal
254 //purpose  : return normal at the given index.
255 //           Raises Standard_OutOfRange exception.
256 //=======================================================================
257 const gp_Dir TDataXtd_Triangulation::Normal (const Standard_Integer theIndex) const
258 {
259   return myTriangulation->Normal(theIndex);
260 }
261
262 //=======================================================================
263 //function : ID
264 //purpose  : 
265 //=======================================================================
266 const Standard_GUID& TDataXtd_Triangulation::ID () const
267 {
268   return GetID();
269 }
270
271 //=======================================================================
272 //function : NewEmpty
273 //purpose  : 
274 //=======================================================================
275 Handle(TDF_Attribute) TDataXtd_Triangulation::NewEmpty () const
276 {  
277   return new TDataXtd_Triangulation(); 
278 }
279
280 //=======================================================================
281 //function : Restore
282 //purpose  : 
283 //=======================================================================
284 void TDataXtd_Triangulation::Restore(const Handle(TDF_Attribute)& theAttribute)
285 {
286   myTriangulation.Nullify();
287   Handle(TDataXtd_Triangulation) M = Handle(TDataXtd_Triangulation)::DownCast(theAttribute);
288   if (!M->myTriangulation.IsNull())
289   {
290     Handle(Poly_Triangulation) T = M->myTriangulation->Copy();
291     if (!T.IsNull())
292       myTriangulation = T;
293   }
294 }
295
296 //=======================================================================
297 //function : Paste
298 //purpose  : 
299 //=======================================================================
300 void TDataXtd_Triangulation::Paste (const Handle(TDF_Attribute)& theIntoAttribute,
301                                     const Handle(TDF_RelocationTable)& ) const
302 {
303   Handle(TDataXtd_Triangulation) M = Handle(TDataXtd_Triangulation)::DownCast(theIntoAttribute);
304   M->myTriangulation.Nullify();
305   if (!myTriangulation.IsNull())
306   {
307       Handle(Poly_Triangulation) T = myTriangulation->Copy();
308       if (!T.IsNull())
309         M->myTriangulation = T;
310   }
311 }
312
313 //=======================================================================
314 //function : Dump
315 //purpose  : 
316 //=======================================================================
317 Standard_OStream& TDataXtd_Triangulation::Dump (Standard_OStream& anOS) const
318 {
319   anOS << "Triangulation";
320   //TODO: Make a good dump.
321   return anOS;
322 }