summaryrefslogtreecommitdiff
path: root/src/MeshVS/MeshVS_DataSource.cxx
blob: 33f8c7d4c38662e6ce44269146ee091bf7c857e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// File:	MeshVS_DataSource.cxx
// Created:	Tue Sep 16 2003
// Author:	Alexander SOLOVYOV
// Copyright:	Open CASCADE 2003

#include <MeshVS_DataSource.ixx>
#include <MeshVS_Tool.hxx>
#include <MeshVS_Buffer.hxx>

#include <gp.hxx>
#include <gp_Vec.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>

//================================================================
// Function : Get3DGeom
// Purpose  :
//================================================================
Standard_Boolean MeshVS_DataSource::Get3DGeom( const Standard_Integer /*ID*/, 
                                               Standard_Integer& /*NbNodes*/,
                                               Handle( MeshVS_HArray1OfSequenceOfInteger )& /*Data*/ ) const
{
  return Standard_False;
}

//================================================================
// Function : GetNormal
// Purpose  :
//================================================================
Standard_Boolean MeshVS_DataSource::GetNormal ( const Standard_Integer Id,
                                                const Standard_Integer Max,
                                                Standard_Real &nx,
                                                Standard_Real &ny,
                                                Standard_Real &nz ) const
{
  if ( Max <= 0 )
    return Standard_False;

  MeshVS_Buffer aCoordsBuf (3*Max*sizeof(Standard_Real));
  TColStd_Array1OfReal Coords ( aCoordsBuf, 1, 3*Max );
  Standard_Integer nbNodes;
  MeshVS_EntityType Type;

  Standard_Boolean res = Standard_False;

  if ( !GetGeom ( Id, Standard_True, Coords, nbNodes, Type ) )
    return res;

  if ( Type == MeshVS_ET_Face && nbNodes >= 3 )
  {
    Standard_Real x1 = Coords( 1 );
    Standard_Real y1 = Coords( 2 );
    Standard_Real z1 = Coords( 3 );
    Standard_Real x2 = Coords( 4 );
    Standard_Real y2 = Coords( 5 );
    Standard_Real z2 = Coords( 6 );
    Standard_Real x3 = Coords( ( nbNodes - 1 ) * 3 + 1 );
    Standard_Real y3 = Coords( ( nbNodes - 1 ) * 3 + 2 );
    Standard_Real z3 = Coords( ( nbNodes - 1 ) * 3 + 3 );
    Standard_Real p1 = x2 - x1, p2 = y2 - y1, p3 = z2 - z1,
                  q1 = x3 - x1, q2 = y3 - y1, q3 = z3 - z1;
    nx = p2*q3 - p3*q2;
    ny = p3*q1 - p1*q3;
    nz = p1*q2 - p2*q1;
    Standard_Real len = sqrt ( nx*nx + ny*ny + nz*nz );
    if ( len <= gp::Resolution() )
    {
      nx = ny = nz = 0;
      return res;
    }
    nx /= len; ny /= len; nz /= len;
    res = Standard_True;
  }
  return res;
}

//================================================================
// Function : GetNodeNormal
// Purpose  :
//================================================================
Standard_Boolean MeshVS_DataSource::GetNodeNormal
                                ( const Standard_Integer /*ranknode*/,
                                  const Standard_Integer /*Id*/,
                                  Standard_Real &/*nx*/,
                                  Standard_Real &/*ny*/,
                                  Standard_Real &/*nz*/ ) const
{
  return Standard_False;
}

//================================================================
// Function : GetNormalsByElement
// Purpose  :
//================================================================
Standard_Boolean MeshVS_DataSource::GetNormalsByElement(const Standard_Integer Id,
                                                        const Standard_Boolean IsNodal,
                                                        const Standard_Integer MaxNodes,
                                                        Handle(TColStd_HArray1OfReal)& Normals) const
{
  MeshVS_Buffer aCoordsBuf (3*MaxNodes*sizeof(Standard_Real));
  TColStd_Array1OfReal Coords ( aCoordsBuf, 1, 3*MaxNodes );
  Standard_Integer NbNodes;
  MeshVS_EntityType Type;

  Standard_Boolean res = Standard_False;
  if ( MaxNodes <= 0 )
    return res;

  if ( !GetGeom ( Id, Standard_True, Coords, NbNodes, Type ) )
    return res;

  Standard_Integer aNbNormals = NbNodes;

  Handle( MeshVS_HArray1OfSequenceOfInteger ) aTopo;
  if ( Type == MeshVS_ET_Volume )
  {
    if( !Get3DGeom( Id, NbNodes, aTopo ) )
          return res;
    // calculate number of normals for faces of volume 
    aNbNormals = aTopo->Upper() - aTopo->Lower() + 1;
  }

  Handle(TColStd_HArray1OfReal) aNormals = new TColStd_HArray1OfReal(1, 3 * aNbNormals );

  Standard_Boolean allNormals = ( Type == MeshVS_ET_Face && IsNodal );
  // Try returning nodal normals if possible
  for( Standard_Integer k=1; k<=NbNodes && allNormals; k++ )
    allNormals = GetNodeNormal( k, 
                                Id, 
                                aNormals->ChangeValue(3 * k - 2), 
                                aNormals->ChangeValue(3 * k - 1), 
                                aNormals->ChangeValue(3 * k    ) );

  // Nodal normals not available or not needed
  if ( !allNormals )
  {
    switch ( Type )
    {
    // Compute a face normal and duplicate it for all element`s nodes
    case MeshVS_ET_Face:
      res = GetNormal( Id, 
                       MaxNodes, 
                       aNormals->ChangeValue(1), 
                       aNormals->ChangeValue(2), 
                       aNormals->ChangeValue(3) );
      if ( res )
      {
        for( Standard_Integer k=2; k<=NbNodes; k++ )
        {
          aNormals->ChangeValue(3 * k - 2) = aNormals->Value(1);
          aNormals->ChangeValue(3 * k - 1) = aNormals->Value(2);
          aNormals->ChangeValue(3 * k    ) = aNormals->Value(3);
        }
      }
      break;

    // Compute normals for each of volum`s faces - not for each node!
    case MeshVS_ET_Volume:
      {
        gp_Vec norm;
        Standard_Integer low = Coords.Lower();
        for ( Standard_Integer k = aTopo->Lower(), last = aTopo->Upper(), i = 1; k <= last; k++, i++ )
        {
          const TColStd_SequenceOfInteger& aSeq = aTopo->Value( k );
          Standard_Integer m = aSeq.Length(), ind;

          norm.SetCoord( 0, 0, 0 );
          MeshVS_Buffer PolyNodesBuf (3*m*sizeof(Standard_Real));
          TColStd_Array1OfReal PolyNodes( PolyNodesBuf, 0, 3*m );
          PolyNodes.SetValue( 0, m );
          for( Standard_Integer j=1; j<=m; j++ )
          {          
            ind = aSeq.Value( j );
            PolyNodes.SetValue( 3*j-2, Coords( low+3*ind   ) );
            PolyNodes.SetValue( 3*j-1, Coords( low+3*ind+1 ) );
            PolyNodes.SetValue( 3*j,   Coords( low+3*ind+2 ) );
          }

          MeshVS_Tool::GetAverageNormal( PolyNodes, norm );

          aNormals->ChangeValue(i * 3 - 2) = norm.X();
          aNormals->ChangeValue(i * 3 - 1) = norm.Y();
          aNormals->ChangeValue(i * 3    ) = norm.Z();
        }
        res = Standard_True;
      }
      break;

    default:
      return res;
    } // switch ( Type )
  } // if ( !allNormals )

  if ( res )
    Normals = aNormals;

  return res;
}

//================================================================
// Function : GetAllGroups
// Purpose  :
//================================================================
void MeshVS_DataSource::GetAllGroups( TColStd_PackedMapOfInteger& /*Ids*/ ) const
{
}

//================================================================
// Function : GetGroup
// Purpose  :
//================================================================
Standard_Boolean MeshVS_DataSource::GetGroup( const Standard_Integer /*Id*/,
                                              MeshVS_EntityType& Type,
                                              TColStd_PackedMapOfInteger& /*Ids*/ )  const
{
  Type = MeshVS_ET_NONE;
  return Standard_False;
}


//================================================================
// Function : GetGroupAddr
// Purpose  :
//================================================================
Standard_Address MeshVS_DataSource::GetGroupAddr(const Standard_Integer /*ID*/) const
{
  return NULL;
}

//================================================================
// Function : IsAdvancedSelectionEnabled
// Purpose  :
//================================================================
Standard_Boolean MeshVS_DataSource::IsAdvancedSelectionEnabled() const
{
  return Standard_False;
}

//================================================================
// Function : GetDetectedEntities
// Purpose  :
//================================================================
Standard_Boolean MeshVS_DataSource::GetDetectedEntities(const Handle(MeshVS_Mesh)& /*theMesh*/, 
                                                        const Standard_Real     /*X*/,
                                                        const Standard_Real     /*Y*/,
                                                        const Standard_Real     /*aTol*/,
                                                        Handle(TColStd_HPackedMapOfInteger)& /*Nodes*/,
                                                        Handle(TColStd_HPackedMapOfInteger)& /*Elements*/,
                                                        Standard_Real& /*DMin*/)
{
  return Standard_False;
}

//================================================================
// Function : GetDetectedEntities
// Purpose  :
//================================================================
Standard_Boolean MeshVS_DataSource::GetDetectedEntities(const Handle(MeshVS_Mesh)& /*theMesh*/, 
							const Standard_Real     /*XMin*/,
							const Standard_Real     /*YMin*/,
							const Standard_Real     /*XMax*/,
							const Standard_Real     /*YMax*/,
							const Standard_Real     /*aTol*/,
							Handle(TColStd_HPackedMapOfInteger)& /*Nodes*/,
							Handle(TColStd_HPackedMapOfInteger)& /*Elements*/) 
{
  return Standard_False;
}

//================================================================
// Function : GetDetectedEntities
// Purpose  :
//================================================================
Standard_Boolean MeshVS_DataSource::GetDetectedEntities(const Handle(MeshVS_Mesh)& /*theMesh*/, 
							const TColgp_Array1OfPnt2d& /*Polyline*/,
							const Bnd_Box2d&            /*aBox*/,
							const Standard_Real         /*aTol*/,
							Handle(TColStd_HPackedMapOfInteger)& /*Nodes*/,
							Handle(TColStd_HPackedMapOfInteger)& /*Elements*/)
{
  return Standard_False;
}

//================================================================
// Function : GetDetectedEntities
// Purpose  :
//================================================================
Standard_Boolean MeshVS_DataSource::GetDetectedEntities(const Handle(MeshVS_Mesh)& /*theMesh*/,
                                                        Handle(TColStd_HPackedMapOfInteger)& /*Nodes*/,
                                                        Handle(TColStd_HPackedMapOfInteger)& /*Elements*/)
{
  return Standard_False;
}

//================================================================
// Function : GetBoundingBox
// Purpose  :
//================================================================
Bnd_Box MeshVS_DataSource::GetBoundingBox() const
{
  // Compute the 3D bounding box for mesh
  Bnd_Box aBox;
  const TColStd_PackedMapOfInteger& aNodes = GetAllNodes();
  if( aNodes.Extent() )
  {
    Standard_Real aCoordsBuf[ 3 ];
    TColStd_Array1OfReal aCoords (*aCoordsBuf, 1, 3);
    Standard_Integer nbNodes;
    MeshVS_EntityType aType;
    TColStd_MapIteratorOfPackedMapOfInteger anIter (aNodes);
    for ( ; anIter.More(); anIter.Next() )
    {
      Standard_Integer aKey = anIter.Key();
      if ( !GetGeom ( aKey, Standard_False, aCoords, nbNodes, aType ) )
        continue;
      aBox.Add( gp_Pnt( aCoordsBuf[0], aCoordsBuf[1], aCoordsBuf[2] ) );
    }
  }
  return aBox;
}