summaryrefslogtreecommitdiff
path: root/src/MeshVS/MeshVS_VectorPrsBuilder.cxx
blob: 1c4ef62ec61aa1a5f93b4bac6db98305fae99151 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// File:	MeshVS_VectorPrsBuilder.cxx
// Created:	Fri Sep 19 2003
// Author:	Alexander SOLOVYOV
// Copyright:	 Open CASCADE 2003

#include <MeshVS_VectorPrsBuilder.ixx>

#include <MeshVS_DisplayModeFlags.hxx>

#include <Graphic3d_ArrayOfPolylines.hxx>
#include <Graphic3d_ArrayOfPolygons.hxx>
#include <Graphic3d_ArrayOfTriangleFans.hxx>
#include <Graphic3d_MaterialAspect.hxx>
#include <Graphic3d_AspectLine3d.hxx>
#include <Graphic3d_AspectFillArea3d.hxx>
#include <Graphic3d_Group.hxx>

#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <TColStd_HPackedMapOfInteger.hxx>

#include <Precision.hxx>

#include <gp_Ax3.hxx>

#include <Prs3d_Root.hxx>

#include <Aspect_TypeOfLine.hxx>

#include <MeshVS_DataSource.hxx>
#include <MeshVS_Drawer.hxx>
#include <MeshVS_DrawerAttribute.hxx>
#include <MeshVS_Mesh.hxx>
#include <MeshVS_EntityType.hxx>
#include <MeshVS_DataMapIteratorOfDataMapOfIntegerVector.hxx>
#include <MeshVS_Buffer.hxx>

//================================================================
// Function : Constructor MeshVS_VectorPrsBuilder
// Purpose  :
//================================================================
MeshVS_VectorPrsBuilder::MeshVS_VectorPrsBuilder ( const Handle(MeshVS_Mesh)& Parent,
                                                   const Standard_Real MaxLength,
                                                   const Quantity_Color& VectorColor,
                                                   const MeshVS_DisplayModeFlags& Flags,
                                                   const Handle (MeshVS_DataSource)& DS,
                                                   const Standard_Integer Id,
                                                   const MeshVS_BuilderPriority& Priority,
                                                   const Standard_Boolean IsSimplePrs )
: MeshVS_PrsBuilder ( Parent, Flags, DS, Id, Priority ),
myIsSimplePrs( IsSimplePrs ),
mySimpleWidthPrm( 2.5 ),
mySimpleStartPrm( 0.85 ),
mySimpleEndPrm( 0.95 )
{
  Handle ( MeshVS_Drawer ) aDrawer = GetDrawer();
  if ( !aDrawer.IsNull() )
  {
    aDrawer->SetDouble ( MeshVS_DA_VectorMaxLength, MaxLength );
    aDrawer->SetColor  ( MeshVS_DA_VectorColor, VectorColor );
    aDrawer->SetDouble ( MeshVS_DA_VectorArrowPart, 0.1 );
  }
}

//================================================================
// Function : GetVectors
// Purpose  :
//================================================================
const MeshVS_DataMapOfIntegerVector& MeshVS_VectorPrsBuilder::GetVectors
  ( const Standard_Boolean IsElements ) const
{
  if ( IsElements )
    return myElemVectorMap;
  else
    return myNodeVectorMap;
}

//================================================================
// Function : SetVectors
// Purpose  :
//================================================================
void MeshVS_VectorPrsBuilder::SetVectors ( const Standard_Boolean IsElements,
                                           const MeshVS_DataMapOfIntegerVector& theMap )
{
  if ( IsElements )
    myElemVectorMap = theMap;
  else
    myNodeVectorMap = theMap;
}

//================================================================
// Function : HasVectors
// Purpose  :
//================================================================
Standard_Boolean MeshVS_VectorPrsBuilder::HasVectors ( const Standard_Boolean IsElement ) const
{
  Standard_Boolean aRes = (myNodeVectorMap.Extent()>0);
  if ( IsElement )
    aRes = (myElemVectorMap.Extent()>0);
  return aRes;

}

//================================================================
// Function : GetVector
// Purpose  :
//================================================================
Standard_Boolean MeshVS_VectorPrsBuilder::GetVector ( const Standard_Boolean IsElement,
                                                      const Standard_Integer ID,
                                                      gp_Vec& Vect ) const
{
  const MeshVS_DataMapOfIntegerVector* aMap = &myNodeVectorMap;
  if ( IsElement )
    aMap = &myElemVectorMap;

  Standard_Boolean aRes = aMap->IsBound ( ID );
  if ( aRes )
    Vect = aMap->Find ( ID );

  return aRes;
}

//================================================================
// Function : SetVector
// Purpose  :
//================================================================
void MeshVS_VectorPrsBuilder::SetVector ( const Standard_Boolean IsElement,
                                          const Standard_Integer ID,
                                          const gp_Vec& Vect )
{
  MeshVS_DataMapOfIntegerVector* aMap = &myNodeVectorMap;
  if ( IsElement )
    aMap = &myElemVectorMap;

  Standard_Boolean aRes = aMap->IsBound ( ID );
  if ( aRes )
    aMap->ChangeFind ( ID ) = Vect;
  else
    aMap->Bind ( ID, Vect );
}

//================================================================
// Function : GetMaxVectorValue
// Purpose  :
//================================================================
void MeshVS_VectorPrsBuilder::GetMinMaxVectorValue ( const Standard_Boolean IsElement,
                                                     Standard_Real& MinValue,
                                                     Standard_Real& MaxValue ) const
{
  const MeshVS_DataMapOfIntegerVector* aMap = &myNodeVectorMap;
  if ( IsElement )
    aMap = &myElemVectorMap;

  MeshVS_DataMapIteratorOfDataMapOfIntegerVector anIt ( *aMap );
  if ( anIt.More() )
    MinValue = MaxValue = anIt.Value().Magnitude();

  Standard_Real aCurValue;

  for ( ; anIt.More(); anIt.Next() )
  {
    aCurValue = anIt.Value().Magnitude();
    if ( MinValue > aCurValue )
      MinValue = aCurValue;
    if ( MaxValue < aCurValue )
      MaxValue = aCurValue;
  }
}

//================================================================
// Function : Build
// Purpose  :
//================================================================

#define NB_VERTICES 2
#define NB_BOUNDS 1
#define NB_TRIANGLES 6
#define NB_FANS 1

void MeshVS_VectorPrsBuilder::Build ( const Handle(Prs3d_Presentation)& Prs,
                                      const TColStd_PackedMapOfInteger& IDs,
                                      TColStd_PackedMapOfInteger& IDsToExclude,
                                      const Standard_Boolean IsElement,
                                      const Standard_Integer theDisplayMode ) const
{
  Handle ( MeshVS_Drawer ) aDrawer = GetDrawer();
  Handle (MeshVS_DataSource) aSource = GetDataSource();
  if ( aSource.IsNull() || aDrawer.IsNull() || !HasVectors( IsElement ) ||
       ( theDisplayMode & GetFlags() )==0 )
    return;

  Standard_Integer aMaxFaceNodes;
  Standard_Real aMaxLen, anArrowPart = 0.1;

  if ( !aDrawer->GetInteger ( MeshVS_DA_MaxFaceNodes, aMaxFaceNodes ) ||
       aMaxFaceNodes <= 0 ||
       !aDrawer->GetDouble  ( MeshVS_DA_VectorMaxLength, aMaxLen )    ||
       aMaxLen <= 0       ||
       !aDrawer->GetDouble ( MeshVS_DA_VectorArrowPart, anArrowPart ) ||
       anArrowPart <= 0
     )
    return;

  MeshVS_Buffer aCoordsBuf (3*aMaxFaceNodes*sizeof(Standard_Real));
  TColStd_Array1OfReal aCoords (aCoordsBuf, 1, 3*aMaxFaceNodes);
  Standard_Integer NbNodes;
  MeshVS_EntityType aType;

  // DECLARE ARRAYS OF PRIMITIVES
  const MeshVS_DataMapOfIntegerVector& aMap = GetVectors ( IsElement );
  Standard_Integer aNbVectors = aMap.Extent();

  if ( aNbVectors <= 0 )
    return;

  // polylines
  Standard_Integer aNbVertices         = aNbVectors  * NB_VERTICES;
  Standard_Integer aNbBounds           = aNbVectors  * NB_BOUNDS;

  // fans
  Standard_Integer aNbTriangleVertices = aNbVectors  * (NB_TRIANGLES + 2);
  Standard_Integer aNbFans             = aNbVectors  * NB_TRIANGLES;

  Handle(Graphic3d_ArrayOfPrimitives) aLineArray =
    new Graphic3d_ArrayOfPolylines(aNbVertices, aNbBounds);
  Handle(Graphic3d_ArrayOfPrimitives) aArrowLineArray =
    new Graphic3d_ArrayOfPolylines(aNbVertices, aNbBounds);

  Handle(Graphic3d_ArrayOfTriangleFans) aTriangleArray =
    new Graphic3d_ArrayOfTriangleFans(aNbTriangleVertices, aNbFans);

  TColgp_Array1OfPnt anArrowPnt(1,8);
  Standard_Real k, b, aMaxValue, aMinValue, aValue, X, Y, Z;
  
  Standard_Real aMinLength = calculateArrow( anArrowPnt, aMaxLen, anArrowPart );
  gp_Vec aVec; gp_Trsf aTrsf;

  GetMinMaxVectorValue ( IsElement, aMinValue, aMaxValue );

  if ( aMaxValue - aMinValue > Precision::Confusion() )
  {
    k = 0.8 * aMaxLen / ( aMaxValue - aMinValue );
    b = aMaxLen - k * aMaxValue;
  }
  else
  {
    k = 0;
    b = aMaxLen;
  }

  TColStd_PackedMapOfInteger aCustomElements;

  // subtract the hidden elements and ids to exclude (to minimise allocated memory)
  TColStd_PackedMapOfInteger anIDs;
  anIDs.Assign( IDs );
  if ( IsElement )
  {
    Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
    if ( !aHiddenElems.IsNull() )
      anIDs.Subtract( aHiddenElems->Map() );
  }
  anIDs.Subtract( IDsToExclude );

  TColStd_MapIteratorOfPackedMapOfInteger it (anIDs);
  for( ; it.More(); it.Next() )
  {
    Standard_Integer aKey = it.Key();
      if( GetVector ( IsElement, aKey, aVec ) )
      {
        aValue = aVec.Magnitude();

        if ( Abs( aValue ) < Precision::Confusion() )
          continue;

        if( aSource->GetGeom ( aKey, IsElement, aCoords, NbNodes, aType ) )
        {
          if( aType == MeshVS_ET_Node )
          {
            X = aCoords(1);
            Y = aCoords(2);
            Z = aCoords(3);
          }
          else if( aType == MeshVS_ET_Link || 
                   aType == MeshVS_ET_Face || 
                   aType == MeshVS_ET_Volume )
          {
            if( IsElement && IsExcludingOn() )
              IDsToExclude.Add( aKey );
            X = Y = Z = 0;
            for ( Standard_Integer i=1; i<=NbNodes; i++ )
            {
              X += aCoords (3*i-2);
              Y += aCoords (3*i-1);
              Z += aCoords (3*i);
            }
            X /= Standard_Real ( NbNodes );
            Y /= Standard_Real ( NbNodes );
            Z /= Standard_Real ( NbNodes );
          }
          else
          {
            aCustomElements.Add( aKey );
            continue;
          }

          aTrsf.SetDisplacement ( gp_Ax3 ( gp_Pnt ( 0, 0, 0 ), gp_Dir(0, 0, 1)),
                                  gp_Ax3 ( gp_Pnt ( X, Y, Z ), aVec ) );

          DrawVector ( aTrsf, Max( k * fabs( aValue ) + b, aMinLength), aMaxLen,
                       anArrowPnt, aLineArray, aArrowLineArray, aTriangleArray );
        }
      }
  }

  Prs3d_Root::NewGroup ( Prs );
  Handle (Graphic3d_Group) aVGroup = Prs3d_Root::CurrentGroup ( Prs );

  Quantity_Color aColor;
  aDrawer->GetColor ( MeshVS_DA_VectorColor, aColor );

  // Add primitive arrays to group
  Handle(Graphic3d_AspectLine3d) aLinAspect =
    new Graphic3d_AspectLine3d ( aColor, Aspect_TOL_SOLID, 1.5 );

  aVGroup->SetPrimitivesAspect( aLinAspect );

  aVGroup->BeginPrimitives();
  aVGroup->AddPrimitiveArray( aLineArray );
  aVGroup->EndPrimitives();
  if ( !myIsSimplePrs )
  {
    Graphic3d_MaterialAspect aMatAspect;
    aMatAspect.SetAmbient( 1 );
    aMatAspect.SetDiffuse( 0 );
    aMatAspect.SetEmissive( 0 );
    aMatAspect.SetShininess( 1 );
    aMatAspect.SetSpecular( 0 );
    Handle(Graphic3d_AspectFillArea3d) aFillAspect =
      new Graphic3d_AspectFillArea3d (Aspect_IS_HOLLOW, aColor, aColor, Aspect_TOL_SOLID,
                                      1., aMatAspect, aMatAspect );

    aVGroup->SetPrimitivesAspect( aFillAspect );

    aVGroup->BeginPrimitives();
    aVGroup->AddPrimitiveArray( aTriangleArray );
    aVGroup->EndPrimitives();
  }
  else
  {
    Handle(Graphic3d_AspectLine3d) anArrowLinAspect =
      new Graphic3d_AspectLine3d ( aColor, Aspect_TOL_SOLID, mySimpleWidthPrm * 1.5 );

    aVGroup->SetPrimitivesAspect( anArrowLinAspect );
    
    aVGroup->BeginPrimitives();
    aVGroup->AddPrimitiveArray( aArrowLineArray );
    aVGroup->EndPrimitives();
  }

  if( !aCustomElements.IsEmpty() )
    CustomBuild( Prs, aCustomElements, IDsToExclude, theDisplayMode );
}

//=======================================================================
// name    : DrawVector
// Purpose : Fill arrays of primitives for drawing force
//=======================================================================
void MeshVS_VectorPrsBuilder::DrawVector ( const gp_Trsf& theTrsf,
                                           const Standard_Real Length,
                                           const Standard_Real MaxLength,
                                           const TColgp_Array1OfPnt& ArrowPoints,
                                           const Handle(Graphic3d_ArrayOfPrimitives)& Lines,
                                           const Handle(Graphic3d_ArrayOfPrimitives)& ArrowLines,
                                           const Handle(Graphic3d_ArrayOfPrimitives)& Triangles) const
{
  const int PntNum = 8;

  const Standard_Real aMinLength = MaxLength * ( 1 - mySimpleStartPrm );
  const Standard_Real aLocalLength = ( !myIsSimplePrs || Length > aMinLength ? Length : aMinLength );
  // draw line
  gp_Pnt aLinePnt[ 2 ] = { gp_Pnt( 0, 0, 0 ) , gp_Pnt( 0, 0, aLocalLength ) };
  theTrsf.Transforms( aLinePnt[ 0 ].ChangeCoord() );
  theTrsf.Transforms( aLinePnt[ 1 ].ChangeCoord() );

  Lines->AddBound( 2 );
  Lines->AddVertex( aLinePnt[ 0 ] );
  Lines->AddVertex( aLinePnt[ 1 ] );

  // draw arrow
  if ( !myIsSimplePrs )
  {
    Standard_Integer l = ArrowPoints.Lower(),
                     u = ArrowPoints.Upper(),
                     i;
    if ( u-l < PntNum-1 )
      return;

    TColgp_Array1OfPnt anArrowPnt( l, u );
    for ( i = l; i < l+PntNum; i++ )
    {
      anArrowPnt( i ).ChangeCoord() = ArrowPoints ( i ).Coord() + gp_XYZ( 0, 0, aLocalLength );
      theTrsf.Transforms( anArrowPnt( i ).ChangeCoord() );
    }

    Triangles->AddBound(8);
    for ( i = 0; i < PntNum; i++ )
      Triangles->AddVertex( anArrowPnt( l+i ) );
  }
  else
  {
    const Standard_Real aEndPos = aLocalLength - MaxLength * ( 1 - mySimpleEndPrm );
    const Standard_Real aArrowLength = MaxLength * ( mySimpleEndPrm - mySimpleStartPrm );
    gp_Pnt aArrowPnt[ 2 ] = { gp_Pnt( 0, 0, aEndPos - aArrowLength ),
                              gp_Pnt( 0, 0, aEndPos ) };
    theTrsf.Transforms( aArrowPnt[ 0 ].ChangeCoord() );
    theTrsf.Transforms( aArrowPnt[ 1 ].ChangeCoord() );

    ArrowLines->AddBound( 2 );
    ArrowLines->AddVertex( aArrowPnt[ 0 ] );
    ArrowLines->AddVertex( aArrowPnt[ 1 ] );
  }
}

//=======================================================================
// name    : calculateArrow
// Purpose : Calculate points of arrow ( 8 pnts )
//=======================================================================
Standard_Real MeshVS_VectorPrsBuilder::calculateArrow ( TColgp_Array1OfPnt& Points,
                                                        const Standard_Real Length,
                                                        const Standard_Real ArrowPart )
{
  Standard_Real h = Length * ArrowPart;
  Standard_Real w = h / 5.;

  Standard_Integer f = Points.Lower();
  Points( f   ) = gp_Pnt( 0,  0, 0 );
  Points( f+1 ) = gp_Pnt( 0, -w, -h );
  Points( f+2 ) = gp_Pnt( w * 0.866, -w * 0.5, -h );
  Points( f+3 ) = gp_Pnt( w * 0.866,  w * 0.5, -h );
  Points( f+4 ) = gp_Pnt( 0 , w, -h );
  Points( f+5 ) = gp_Pnt( -w * 0.866,  w * 0.5, -h );
  Points( f+6 ) = gp_Pnt( -w * 0.866, -w * 0.5, -h );
  Points( f+7 ) = gp_Pnt( 0, -w, -h );

  return h;
}

//=======================================================================
// name    : SetSimplePrsMode
// Purpose : 
//=======================================================================
void MeshVS_VectorPrsBuilder::SetSimplePrsMode( const Standard_Boolean IsSimpleArrow )
{
  myIsSimplePrs = IsSimpleArrow;
}

//=======================================================================
// name    : SetSimplePrsParams
// Purpose : 
//=======================================================================
void MeshVS_VectorPrsBuilder::SetSimplePrsParams( const Standard_Real theLineWidthParam,
                                                  const Standard_Real theStartParam,
                                                  const Standard_Real theEndParam )
{
  mySimpleWidthPrm = theLineWidthParam;
  mySimpleStartPrm = theStartParam;
  mySimpleEndPrm   = theEndParam;
}