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