1 // Created on: 2003-09-16
2 // Created by: Alexander SOLOVYOV
3 // Copyright (c) 2003-2012 OPEN CASCADE SAS
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.
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.
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.
21 #include <MeshVS_DataSource.ixx>
22 #include <MeshVS_Tool.hxx>
23 #include <MeshVS_Buffer.hxx>
27 #include <TColgp_Array1OfPnt.hxx>
28 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
30 //================================================================
31 // Function : Get3DGeom
33 //================================================================
34 Standard_Boolean MeshVS_DataSource::Get3DGeom( const Standard_Integer /*ID*/,
35 Standard_Integer& /*NbNodes*/,
36 Handle( MeshVS_HArray1OfSequenceOfInteger )& /*Data*/ ) const
38 return Standard_False;
41 //================================================================
42 // Function : GetNormal
44 //================================================================
45 Standard_Boolean MeshVS_DataSource::GetNormal ( const Standard_Integer Id,
46 const Standard_Integer Max,
49 Standard_Real &nz ) const
52 return Standard_False;
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;
59 Standard_Boolean res = Standard_False;
61 if ( !GetGeom ( Id, Standard_True, Coords, nbNodes, Type ) )
64 if ( Type == MeshVS_ET_Face && nbNodes >= 3 )
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;
80 Standard_Real len = sqrt ( nx*nx + ny*ny + nz*nz );
81 if ( len <= gp::Resolution() )
86 nx /= len; ny /= len; nz /= len;
92 //================================================================
93 // Function : GetNodeNormal
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
103 return Standard_False;
106 //================================================================
107 // Function : GetNormalsByElement
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
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;
120 Standard_Boolean res = Standard_False;
124 if ( !GetGeom ( Id, Standard_True, Coords, NbNodes, Type ) )
127 Standard_Integer aNbNormals = NbNodes;
129 Handle( MeshVS_HArray1OfSequenceOfInteger ) aTopo;
130 if ( Type == MeshVS_ET_Volume )
132 if( !Get3DGeom( Id, NbNodes, aTopo ) )
134 // calculate number of normals for faces of volume
135 aNbNormals = aTopo->Upper() - aTopo->Lower() + 1;
138 Handle(TColStd_HArray1OfReal) aNormals = new TColStd_HArray1OfReal(1, 3 * aNbNormals );
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,
145 aNormals->ChangeValue(3 * k - 2),
146 aNormals->ChangeValue(3 * k - 1),
147 aNormals->ChangeValue(3 * k ) );
149 // Nodal normals not available or not needed
154 // Compute a face normal and duplicate it for all element`s nodes
158 aNormals->ChangeValue(1),
159 aNormals->ChangeValue(2),
160 aNormals->ChangeValue(3) );
163 for( Standard_Integer k=2; k<=NbNodes; k++ )
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);
172 // Compute normals for each of volum`s faces - not for each node!
173 case MeshVS_ET_Volume:
176 Standard_Integer low = Coords.Lower();
177 for ( Standard_Integer k = aTopo->Lower(), last = aTopo->Upper(), i = 1; k <= last; k++, i++ )
179 const TColStd_SequenceOfInteger& aSeq = aTopo->Value( k );
180 Standard_Integer m = aSeq.Length(), ind;
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++ )
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 ) );
194 MeshVS_Tool::GetAverageNormal( PolyNodes, norm );
196 aNormals->ChangeValue(i * 3 - 2) = norm.X();
197 aNormals->ChangeValue(i * 3 - 1) = norm.Y();
198 aNormals->ChangeValue(i * 3 ) = norm.Z();
207 } // if ( !allNormals )
215 //================================================================
216 // Function : GetAllGroups
218 //================================================================
219 void MeshVS_DataSource::GetAllGroups( TColStd_PackedMapOfInteger& /*Ids*/ ) const
223 //================================================================
224 // Function : GetGroup
226 //================================================================
227 Standard_Boolean MeshVS_DataSource::GetGroup( const Standard_Integer /*Id*/,
228 MeshVS_EntityType& Type,
229 TColStd_PackedMapOfInteger& /*Ids*/ ) const
231 Type = MeshVS_ET_NONE;
232 return Standard_False;
236 //================================================================
237 // Function : GetGroupAddr
239 //================================================================
240 Standard_Address MeshVS_DataSource::GetGroupAddr(const Standard_Integer /*ID*/) const
245 //================================================================
246 // Function : IsAdvancedSelectionEnabled
248 //================================================================
249 Standard_Boolean MeshVS_DataSource::IsAdvancedSelectionEnabled() const
251 return Standard_False;
254 //================================================================
255 // Function : GetDetectedEntities
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*/)
266 return Standard_False;
269 //================================================================
270 // Function : GetDetectedEntities
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*/)
282 return Standard_False;
285 //================================================================
286 // Function : GetDetectedEntities
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*/)
296 return Standard_False;
299 //================================================================
300 // Function : GetDetectedEntities
302 //================================================================
303 Standard_Boolean MeshVS_DataSource::GetDetectedEntities(const Handle(MeshVS_Mesh)& /*theMesh*/,
304 Handle(TColStd_HPackedMapOfInteger)& /*Nodes*/,
305 Handle(TColStd_HPackedMapOfInteger)& /*Elements*/)
307 return Standard_False;
310 //================================================================
311 // Function : GetBoundingBox
313 //================================================================
314 Bnd_Box MeshVS_DataSource::GetBoundingBox() const
316 // Compute the 3D bounding box for mesh
318 const TColStd_PackedMapOfInteger& aNodes = GetAllNodes();
319 if( aNodes.Extent() )
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() )
328 Standard_Integer aKey = anIter.Key();
329 if ( !GetGeom ( aKey, Standard_False, aCoords, nbNodes, aType ) )
331 aBox.Add( gp_Pnt( aCoordsBuf[0], aCoordsBuf[1], aCoordsBuf[2] ) );