0024750: Replace instantiations of TCollection generic classes by NCollection templat...
[occt.git] / src / MeshVS / MeshVS_DataSource.cxx
CommitLineData
b311480e 1// Created on: 2003-09-16
2// Created by: Alexander SOLOVYOV
973c2be1 3// Copyright (c) 2003-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
16#include <MeshVS_DataSource.ixx>
17#include <MeshVS_Tool.hxx>
18#include <MeshVS_Buffer.hxx>
19
20#include <gp.hxx>
21#include <gp_Vec.hxx>
22#include <TColgp_Array1OfPnt.hxx>
23#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
24
25//================================================================
26// Function : Get3DGeom
27// Purpose :
28//================================================================
29Standard_Boolean MeshVS_DataSource::Get3DGeom( const Standard_Integer /*ID*/,
30 Standard_Integer& /*NbNodes*/,
31 Handle( MeshVS_HArray1OfSequenceOfInteger )& /*Data*/ ) const
32{
33 return Standard_False;
34}
35
36//================================================================
37// Function : GetNormal
38// Purpose :
39//================================================================
40Standard_Boolean MeshVS_DataSource::GetNormal ( const Standard_Integer Id,
41 const Standard_Integer Max,
42 Standard_Real &nx,
43 Standard_Real &ny,
44 Standard_Real &nz ) const
45{
46 if ( Max <= 0 )
47 return Standard_False;
48
49 MeshVS_Buffer aCoordsBuf (3*Max*sizeof(Standard_Real));
50 TColStd_Array1OfReal Coords ( aCoordsBuf, 1, 3*Max );
51 Standard_Integer nbNodes;
52 MeshVS_EntityType Type;
53
54 Standard_Boolean res = Standard_False;
55
56 if ( !GetGeom ( Id, Standard_True, Coords, nbNodes, Type ) )
57 return res;
58
59 if ( Type == MeshVS_ET_Face && nbNodes >= 3 )
60 {
61 Standard_Real x1 = Coords( 1 );
62 Standard_Real y1 = Coords( 2 );
63 Standard_Real z1 = Coords( 3 );
64 Standard_Real x2 = Coords( 4 );
65 Standard_Real y2 = Coords( 5 );
66 Standard_Real z2 = Coords( 6 );
67 Standard_Real x3 = Coords( ( nbNodes - 1 ) * 3 + 1 );
68 Standard_Real y3 = Coords( ( nbNodes - 1 ) * 3 + 2 );
69 Standard_Real z3 = Coords( ( nbNodes - 1 ) * 3 + 3 );
70 Standard_Real p1 = x2 - x1, p2 = y2 - y1, p3 = z2 - z1,
71 q1 = x3 - x1, q2 = y3 - y1, q3 = z3 - z1;
72 nx = p2*q3 - p3*q2;
73 ny = p3*q1 - p1*q3;
74 nz = p1*q2 - p2*q1;
75 Standard_Real len = sqrt ( nx*nx + ny*ny + nz*nz );
76 if ( len <= gp::Resolution() )
77 {
78 nx = ny = nz = 0;
79 return res;
80 }
81 nx /= len; ny /= len; nz /= len;
82 res = Standard_True;
83 }
84 return res;
85}
86
87//================================================================
88// Function : GetNodeNormal
89// Purpose :
90//================================================================
91Standard_Boolean MeshVS_DataSource::GetNodeNormal
92 ( const Standard_Integer /*ranknode*/,
93 const Standard_Integer /*Id*/,
94 Standard_Real &/*nx*/,
95 Standard_Real &/*ny*/,
96 Standard_Real &/*nz*/ ) const
97{
98 return Standard_False;
99}
100
101//================================================================
102// Function : GetNormalsByElement
103// Purpose :
104//================================================================
105Standard_Boolean MeshVS_DataSource::GetNormalsByElement(const Standard_Integer Id,
106 const Standard_Boolean IsNodal,
107 const Standard_Integer MaxNodes,
108 Handle(TColStd_HArray1OfReal)& Normals) const
109{
110 MeshVS_Buffer aCoordsBuf (3*MaxNodes*sizeof(Standard_Real));
111 TColStd_Array1OfReal Coords ( aCoordsBuf, 1, 3*MaxNodes );
112 Standard_Integer NbNodes;
113 MeshVS_EntityType Type;
114
115 Standard_Boolean res = Standard_False;
116 if ( MaxNodes <= 0 )
117 return res;
118
119 if ( !GetGeom ( Id, Standard_True, Coords, NbNodes, Type ) )
120 return res;
121
122 Standard_Integer aNbNormals = NbNodes;
123
124 Handle( MeshVS_HArray1OfSequenceOfInteger ) aTopo;
125 if ( Type == MeshVS_ET_Volume )
126 {
127 if( !Get3DGeom( Id, NbNodes, aTopo ) )
128 return res;
129 // calculate number of normals for faces of volume
130 aNbNormals = aTopo->Upper() - aTopo->Lower() + 1;
131 }
132
133 Handle(TColStd_HArray1OfReal) aNormals = new TColStd_HArray1OfReal(1, 3 * aNbNormals );
134
135 Standard_Boolean allNormals = ( Type == MeshVS_ET_Face && IsNodal );
136 // Try returning nodal normals if possible
137 for( Standard_Integer k=1; k<=NbNodes && allNormals; k++ )
138 allNormals = GetNodeNormal( k,
139 Id,
140 aNormals->ChangeValue(3 * k - 2),
141 aNormals->ChangeValue(3 * k - 1),
142 aNormals->ChangeValue(3 * k ) );
143
144 // Nodal normals not available or not needed
145 if ( !allNormals )
146 {
147 switch ( Type )
148 {
149 // Compute a face normal and duplicate it for all element`s nodes
150 case MeshVS_ET_Face:
151 res = GetNormal( Id,
152 MaxNodes,
153 aNormals->ChangeValue(1),
154 aNormals->ChangeValue(2),
155 aNormals->ChangeValue(3) );
156 if ( res )
157 {
158 for( Standard_Integer k=2; k<=NbNodes; k++ )
159 {
160 aNormals->ChangeValue(3 * k - 2) = aNormals->Value(1);
161 aNormals->ChangeValue(3 * k - 1) = aNormals->Value(2);
162 aNormals->ChangeValue(3 * k ) = aNormals->Value(3);
163 }
164 }
165 break;
166
167 // Compute normals for each of volum`s faces - not for each node!
168 case MeshVS_ET_Volume:
169 {
170 gp_Vec norm;
171 Standard_Integer low = Coords.Lower();
172 for ( Standard_Integer k = aTopo->Lower(), last = aTopo->Upper(), i = 1; k <= last; k++, i++ )
173 {
174 const TColStd_SequenceOfInteger& aSeq = aTopo->Value( k );
175 Standard_Integer m = aSeq.Length(), ind;
176
177 norm.SetCoord( 0, 0, 0 );
178 MeshVS_Buffer PolyNodesBuf (3*m*sizeof(Standard_Real));
179 TColStd_Array1OfReal PolyNodes( PolyNodesBuf, 0, 3*m );
180 PolyNodes.SetValue( 0, m );
181 for( Standard_Integer j=1; j<=m; j++ )
182 {
183 ind = aSeq.Value( j );
184 PolyNodes.SetValue( 3*j-2, Coords( low+3*ind ) );
185 PolyNodes.SetValue( 3*j-1, Coords( low+3*ind+1 ) );
186 PolyNodes.SetValue( 3*j, Coords( low+3*ind+2 ) );
187 }
188
189 MeshVS_Tool::GetAverageNormal( PolyNodes, norm );
190
191 aNormals->ChangeValue(i * 3 - 2) = norm.X();
192 aNormals->ChangeValue(i * 3 - 1) = norm.Y();
193 aNormals->ChangeValue(i * 3 ) = norm.Z();
194 }
195 res = Standard_True;
196 }
197 break;
198
199 default:
200 return res;
201 } // switch ( Type )
202 } // if ( !allNormals )
203
c1338f4f 204 if ( res || allNormals )
7fd59977 205 Normals = aNormals;
206
c1338f4f 207 return ( res || allNormals );
7fd59977 208}
209
210//================================================================
211// Function : GetAllGroups
212// Purpose :
213//================================================================
214void MeshVS_DataSource::GetAllGroups( TColStd_PackedMapOfInteger& /*Ids*/ ) const
215{
216}
217
218//================================================================
219// Function : GetGroup
220// Purpose :
221//================================================================
222Standard_Boolean MeshVS_DataSource::GetGroup( const Standard_Integer /*Id*/,
223 MeshVS_EntityType& Type,
224 TColStd_PackedMapOfInteger& /*Ids*/ ) const
225{
226 Type = MeshVS_ET_NONE;
227 return Standard_False;
228}
229
230
231//================================================================
232// Function : GetGroupAddr
233// Purpose :
234//================================================================
235Standard_Address MeshVS_DataSource::GetGroupAddr(const Standard_Integer /*ID*/) const
236{
237 return NULL;
238}
239
240//================================================================
241// Function : IsAdvancedSelectionEnabled
242// Purpose :
243//================================================================
244Standard_Boolean MeshVS_DataSource::IsAdvancedSelectionEnabled() const
245{
246 return Standard_False;
247}
248
249//================================================================
250// Function : GetDetectedEntities
251// Purpose :
252//================================================================
253Standard_Boolean MeshVS_DataSource::GetDetectedEntities(const Handle(MeshVS_Mesh)& /*theMesh*/,
254 const Standard_Real /*X*/,
255 const Standard_Real /*Y*/,
256 const Standard_Real /*aTol*/,
257 Handle(TColStd_HPackedMapOfInteger)& /*Nodes*/,
258 Handle(TColStd_HPackedMapOfInteger)& /*Elements*/,
259 Standard_Real& /*DMin*/)
260{
261 return Standard_False;
262}
263
264//================================================================
265// Function : GetDetectedEntities
266// Purpose :
267//================================================================
268Standard_Boolean MeshVS_DataSource::GetDetectedEntities(const Handle(MeshVS_Mesh)& /*theMesh*/,
269 const Standard_Real /*XMin*/,
270 const Standard_Real /*YMin*/,
271 const Standard_Real /*XMax*/,
272 const Standard_Real /*YMax*/,
273 const Standard_Real /*aTol*/,
274 Handle(TColStd_HPackedMapOfInteger)& /*Nodes*/,
275 Handle(TColStd_HPackedMapOfInteger)& /*Elements*/)
276{
277 return Standard_False;
278}
279
280//================================================================
281// Function : GetDetectedEntities
282// Purpose :
283//================================================================
284Standard_Boolean MeshVS_DataSource::GetDetectedEntities(const Handle(MeshVS_Mesh)& /*theMesh*/,
285 const TColgp_Array1OfPnt2d& /*Polyline*/,
286 const Bnd_Box2d& /*aBox*/,
287 const Standard_Real /*aTol*/,
288 Handle(TColStd_HPackedMapOfInteger)& /*Nodes*/,
289 Handle(TColStd_HPackedMapOfInteger)& /*Elements*/)
290{
291 return Standard_False;
292}
293
294//================================================================
295// Function : GetDetectedEntities
296// Purpose :
297//================================================================
298Standard_Boolean MeshVS_DataSource::GetDetectedEntities(const Handle(MeshVS_Mesh)& /*theMesh*/,
299 Handle(TColStd_HPackedMapOfInteger)& /*Nodes*/,
300 Handle(TColStd_HPackedMapOfInteger)& /*Elements*/)
301{
302 return Standard_False;
303}
304
305//================================================================
306// Function : GetBoundingBox
307// Purpose :
308//================================================================
309Bnd_Box MeshVS_DataSource::GetBoundingBox() const
310{
311 // Compute the 3D bounding box for mesh
312 Bnd_Box aBox;
313 const TColStd_PackedMapOfInteger& aNodes = GetAllNodes();
314 if( aNodes.Extent() )
315 {
316 Standard_Real aCoordsBuf[ 3 ];
317 TColStd_Array1OfReal aCoords (*aCoordsBuf, 1, 3);
318 Standard_Integer nbNodes;
319 MeshVS_EntityType aType;
320 TColStd_MapIteratorOfPackedMapOfInteger anIter (aNodes);
321 for ( ; anIter.More(); anIter.Next() )
322 {
323 Standard_Integer aKey = anIter.Key();
324 if ( !GetGeom ( aKey, Standard_False, aCoords, nbNodes, aType ) )
325 continue;
326 aBox.Add( gp_Pnt( aCoordsBuf[0], aCoordsBuf[1], aCoordsBuf[2] ) );
327 }
328 }
329 return aBox;
330}