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