summaryrefslogtreecommitdiff
path: root/src/MeshVS/MeshVS_Tool.cxx
blob: b52f5ba89825e93fb6863b0d16a1de824376ec86 (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
// File:  MeshVS_Tool.cxx
// Created: Wed Dec 17 2003
// Author:  Alexander SOLOVYOV
// Copyright:  Open CASCADE 2003

#include <MeshVS_Tool.ixx>

#include <MeshVS_DrawerAttribute.hxx>
#include <Precision.hxx>
#include <Graphic3d_NameOfFont.hxx>

//================================================================
// Function : CreateAspectFillArea3d
// Purpose  :
//================================================================
Handle( Graphic3d_AspectFillArea3d ) MeshVS_Tool::CreateAspectFillArea3d
(  const Handle(MeshVS_Drawer)& theDr,
   const Graphic3d_MaterialAspect& Mat,
   const Standard_Boolean UseDefaults )
{
  Handle( Graphic3d_AspectFillArea3d ) anAsp;
  if ( theDr.IsNull() )
    return anAsp;

  Aspect_InteriorStyle     anIntStyle    = Aspect_IS_EMPTY;
  Quantity_Color           anIntColor    = Quantity_NOC_CYAN1,
                           anEdgeColor   = Quantity_NOC_WHITE;
  Aspect_TypeOfLine        anEdgeType    = Aspect_TOL_SOLID;
  Standard_Real            anEdgeWidth   = 1.0;
  Aspect_HatchStyle        aHStyle       = Aspect_HS_HORIZONTAL;
  Graphic3d_MaterialAspect aFrMat        = Mat,
                           aBackMat      = Mat;

  Standard_Integer         anIntStyleI   = (Standard_Integer)Aspect_IS_EMPTY;
  Standard_Integer         anEdgeTypeI   = (Standard_Integer)Aspect_TOL_SOLID;
  Standard_Integer         aHStyleI      = (Standard_Integer)Aspect_HS_HORIZONTAL;

  if ( !theDr->GetColor ( MeshVS_DA_InteriorColor, anIntColor ) && !UseDefaults )
    return anAsp;

  Quantity_Color aBackIntColor = anIntColor;
  if ( !theDr->GetColor ( MeshVS_DA_BackInteriorColor, aBackIntColor ) && !UseDefaults )
    return anAsp;

  if ( !theDr->GetColor ( MeshVS_DA_EdgeColor, anEdgeColor ) && !UseDefaults )
    return anAsp;

  if ( !theDr->GetDouble ( MeshVS_DA_EdgeWidth, anEdgeWidth ) && !UseDefaults )
    return anAsp;

  if ( !theDr->GetInteger ( MeshVS_DA_InteriorStyle, anIntStyleI ) && !UseDefaults )
    return anAsp;
  else
    anIntStyle = (Aspect_InteriorStyle) anIntStyleI;

  if ( !theDr->GetInteger ( MeshVS_DA_EdgeType, anEdgeTypeI ) && !UseDefaults )
    return anAsp;
  else
    anEdgeType = (Aspect_TypeOfLine) anEdgeTypeI;

  if ( !theDr->GetInteger ( MeshVS_DA_HatchStyle, aHStyleI ) && !UseDefaults )
    return anAsp;
  else
    aHStyle = (Aspect_HatchStyle) aHStyleI;

  anAsp = new Graphic3d_AspectFillArea3d ( anIntStyle, anIntColor, anEdgeColor, anEdgeType,
    anEdgeWidth, aFrMat, aBackMat );
  anAsp->SetBackInteriorColor ( aBackIntColor );
  anAsp->SetHatchStyle ( aHStyle );

  return anAsp;
}

//================================================================
// Function : CreateAspectFillArea3d
// Purpose  :
//================================================================
Handle( Graphic3d_AspectFillArea3d ) MeshVS_Tool::CreateAspectFillArea3d
(  const Handle(MeshVS_Drawer)& theDr,
   const Standard_Boolean UseDefaults )
{
  Graphic3d_MaterialAspect aFrMat   = Graphic3d_NOM_BRASS;
  Graphic3d_MaterialAspect aBackMat = Graphic3d_NOM_BRASS;
  Standard_Integer aFrMatI = (Standard_Integer)Graphic3d_NOM_BRASS;
  Standard_Integer aBackMatI = (Standard_Integer)Graphic3d_NOM_BRASS;

  if ( !theDr->GetInteger ( MeshVS_DA_FrontMaterial, aFrMatI ) && !UseDefaults )
    return 0;
  else
    aFrMat = (Graphic3d_MaterialAspect)(Graphic3d_NameOfMaterial)aFrMatI;

  if ( !theDr->GetInteger ( MeshVS_DA_BackMaterial, aBackMatI ) && !UseDefaults )
    return 0;
  else
    aBackMat = (Graphic3d_MaterialAspect)(Graphic3d_NameOfMaterial)aBackMatI;

  Handle( Graphic3d_AspectFillArea3d ) aFill =
    CreateAspectFillArea3d ( theDr, aFrMat, UseDefaults );
  aFill->SetBackMaterial ( aBackMat );

  return aFill;
}
//================================================================
// Function : CreateAspectLine3d
// Purpose  :
//================================================================
Handle( Graphic3d_AspectLine3d ) MeshVS_Tool::CreateAspectLine3d
(  const Handle(MeshVS_Drawer)& theDr,
 const Standard_Boolean UseDefaults )
{
  Handle( Graphic3d_AspectLine3d ) anAsp;
  if ( theDr.IsNull() )
    return anAsp;

  Quantity_Color           aBeamColor   = Quantity_NOC_YELLOW;
  Aspect_TypeOfLine        aBeamType    = Aspect_TOL_SOLID;
  Standard_Real            aBeamWidth   = 1.0;
  Standard_Integer         aBeamTypeI   = (Standard_Integer)Aspect_TOL_SOLID;

  if ( !theDr->GetColor ( MeshVS_DA_BeamColor, aBeamColor ) && !UseDefaults )
    return anAsp;

  if ( !theDr->GetDouble ( MeshVS_DA_BeamWidth, aBeamWidth ) && !UseDefaults )
    return anAsp;

  if ( !theDr->GetInteger ( MeshVS_DA_BeamType, aBeamTypeI ) && !UseDefaults )
    return anAsp;
  else
    aBeamType = (Aspect_TypeOfLine) aBeamTypeI;

  anAsp = new Graphic3d_AspectLine3d ( aBeamColor, aBeamType, aBeamWidth );

  return anAsp;
}

//================================================================
// Function : CreateAspectMarker3d
// Purpose  :
//================================================================
Handle( Graphic3d_AspectMarker3d ) MeshVS_Tool::CreateAspectMarker3d
(  const Handle(MeshVS_Drawer)& theDr,
 const Standard_Boolean UseDefaults )
{
  Handle( Graphic3d_AspectMarker3d ) anAsp;
  if ( theDr.IsNull() )
    return anAsp;

  Quantity_Color           aMColor   = Quantity_NOC_YELLOW;
  Aspect_TypeOfMarker      aMType    = Aspect_TOM_X;
  Standard_Real            aMScale   = 1.0;
  Standard_Integer         aMTypeI   = (Standard_Integer)Aspect_TOM_X;

  if ( !theDr->GetColor ( MeshVS_DA_MarkerColor, aMColor ) && !UseDefaults )
    return anAsp;

  if ( !theDr->GetDouble ( MeshVS_DA_MarkerScale, aMScale ) && !UseDefaults )
    return anAsp;

  if ( !theDr->GetInteger ( MeshVS_DA_MarkerType, aMTypeI ) && !UseDefaults )
    return anAsp;
  else
    aMType = (Aspect_TypeOfMarker) aMTypeI;

  anAsp = new Graphic3d_AspectMarker3d ( aMType, aMColor, aMScale );

  return anAsp;
}

//================================================================
// Function : CreateAspectText3d
// Purpose  :
//================================================================
Handle( Graphic3d_AspectText3d ) MeshVS_Tool::CreateAspectText3d
(  const Handle(MeshVS_Drawer)& theDr,
   const Standard_Boolean UseDefaults )
{
  Handle( Graphic3d_AspectText3d ) anAsp;
  if ( theDr.IsNull() )
    return anAsp;

  Quantity_Color            aTColor       = Quantity_NOC_YELLOW;
  Standard_Real             anExpFactor   = 1.0,
                            aSpace        = 0.0;
  Standard_CString          aFont         = Graphic3d_NOF_ASCII_MONO;
  Aspect_TypeOfStyleText    aStyle        = Aspect_TOST_NORMAL;
  Aspect_TypeOfDisplayText  aDispText     = Aspect_TODT_NORMAL;
  TCollection_AsciiString   aFontString   = Graphic3d_NOF_ASCII_MONO;
  OSD_FontAspect            aFontAspect   = OSD_FA_Regular;
  Standard_Integer          aStyleI       = (Standard_Integer)Aspect_TOST_NORMAL;
  Standard_Integer          aDispTextI    = (Standard_Integer)Aspect_TODT_NORMAL;
  // Bold font is used by default for better text readability
  Standard_Integer          aFontAspectI  = (Standard_Integer)OSD_FA_Bold;

  if ( !theDr->GetColor ( MeshVS_DA_TextColor, aTColor ) && !UseDefaults )
    return anAsp;

  if ( !theDr->GetDouble ( MeshVS_DA_TextExpansionFactor, anExpFactor ) && !UseDefaults )
    return anAsp;

  if ( !theDr->GetDouble ( MeshVS_DA_TextSpace, aSpace ) && !UseDefaults )
    return anAsp;

  if ( !theDr->GetAsciiString ( MeshVS_DA_TextFont, aFontString ) && !UseDefaults )
    return anAsp;
  else
    aFont = aFontString.ToCString();

  if ( !theDr->GetInteger ( MeshVS_DA_TextStyle, aStyleI ) && !UseDefaults )
    return anAsp;
  else
    aStyle = (Aspect_TypeOfStyleText) aStyleI;

  if ( !theDr->GetInteger ( MeshVS_DA_TextDisplayType, aDispTextI ) && !UseDefaults )
    return anAsp;
  else
    aDispText = (Aspect_TypeOfDisplayText) aDispTextI;

  if ( !theDr->GetInteger ( MeshVS_DA_TextFontAspect, aFontAspectI ) && !UseDefaults )
    return anAsp;
  else 
    aFontAspect = (OSD_FontAspect) aFontAspectI;

  anAsp = new Graphic3d_AspectText3d ( aTColor, aFont, anExpFactor, aSpace, aStyle, aDispText );
  anAsp->SetTextFontAspect( aFontAspect );
  return anAsp;
}

//================================================================
// Function : GetNormal
// Purpose  :
//================================================================
Standard_Boolean MeshVS_Tool::GetNormal( const TColStd_Array1OfReal& Nodes,
                                        gp_Vec& Norm )
{
  Standard_Integer first = Nodes.Lower(),
    last  = Nodes.Upper(),
    count = (last-first+1)/3, i, j;
  if( first==0 )
  {
    first = 1;
    count = Standard_Integer( Nodes.Value( 0 ) );
  }

  if( count<3 )
    return Standard_False;

  Standard_Boolean res = Standard_True;


  Standard_Real normal[3], first_vec[3], cur_vec[3], xx, yy, zz,
    conf = Precision::Confusion();

  for( i=0; i<3; i++ )
  {
    normal[i] = 0.0;
    first_vec[i] = Nodes.Value( first+3+i ) - Nodes.Value( first+i );
  }

  for( i=2; i<count; i++ )
  {
    for( j=0; j<3; j++ )
      cur_vec[j] = Nodes.Value( first+3*i+j ) - Nodes.Value( first+j );

    xx = first_vec[1] * cur_vec[2] - first_vec[2] * cur_vec[1];
    yy = first_vec[2] * cur_vec[0] - first_vec[0] * cur_vec[2];
    zz = first_vec[0] * cur_vec[1] - first_vec[1] * cur_vec[0];

    cur_vec[0] = xx;
    cur_vec[1] = yy;
    cur_vec[2] = zz;

    if( fabs( cur_vec[0] ) > conf ||
      fabs( cur_vec[1] ) > conf ||
      fabs( cur_vec[2] ) > conf )
    {
      Standard_Real cur = Sqrt( cur_vec[0]*cur_vec[0] + cur_vec[1]*cur_vec[1] + cur_vec[2]*cur_vec[2] );
      for( Standard_Integer k=0; k<3; k++ )
        cur_vec[k] /= cur;
    }

    if( fabs( normal[0] ) <= conf &&
      fabs( normal[1] ) <= conf &&
      fabs( normal[2] ) <= conf )
      for( Standard_Integer k=0; k<3; k++ )
        normal[k] = cur_vec[k];

    if( fabs( normal[0]-cur_vec[0] ) > conf ||
      fabs( normal[1]-cur_vec[1] ) > conf ||
      fabs( normal[2]-cur_vec[2] ) > conf    )
    {
      res = Standard_False;
      break;
    }
  }

  if( res )
    Norm.SetCoord( normal[0], normal[1], normal[2] );

  return res;
}


//================================================================
// Function : GetAverageNormal
// Purpose  :
//================================================================
Standard_Boolean MeshVS_Tool::GetAverageNormal( const TColStd_Array1OfReal& Nodes,
                                               gp_Vec& Norm )
{
  Standard_Integer first = Nodes.Lower(),
    last  = Nodes.Upper(),
    count = (last-first+1)/3, i, j;
  if( first==0 )
  {
    first = 1;
    count = Standard_Integer( Nodes.Value( 0 ) );
  }

  if( count<3 )
    return Standard_False;

  Standard_Boolean res = Standard_True;


  Standard_Real normal[3], first_vec[3], cur_vec[3], xx, yy, zz,
    conf = Precision::Confusion();

  for( i=0; i<3; i++ )
  {
    normal[i] = 0.0;
    first_vec[i] = Nodes.Value( first+3+i ) - Nodes.Value( first+i );
  }

  gp_XYZ* norm_vec = new gp_XYZ[count-2];
  for ( i = 0; i < count-2; i++ )
    norm_vec[i].SetCoord(0, 0, 0);

  for( i=2; i<count; i++ )
  {
    for( j=0; j<3; j++ )
      cur_vec[j] = Nodes.Value( first+3*i+j ) - Nodes.Value( first+j );

    xx = first_vec[1] * cur_vec[2] - first_vec[2] * cur_vec[1];
    yy = first_vec[2] * cur_vec[0] - first_vec[0] * cur_vec[2];
    zz = first_vec[0] * cur_vec[1] - first_vec[1] * cur_vec[0];

    cur_vec[0] = xx;
    cur_vec[1] = yy;
    cur_vec[2] = zz;

    if( fabs( cur_vec[0] ) > conf ||
      fabs( cur_vec[1] ) > conf ||
      fabs( cur_vec[2] ) > conf )
    {
      Standard_Real cur = Sqrt( cur_vec[0]*cur_vec[0] + cur_vec[1]*cur_vec[1] + cur_vec[2]*cur_vec[2] );
      for( Standard_Integer k=0; k<3; k++ )
        cur_vec[k] /= cur;
    }

    norm_vec[i-2].SetCoord(cur_vec[0], cur_vec[1], cur_vec[2]);

    if( fabs( normal[0] ) <= conf &&
      fabs( normal[1] ) <= conf &&
      fabs( normal[2] ) <= conf )
      for( Standard_Integer k=0; k<3; k++ )
        normal[k] = cur_vec[k];

    if( fabs( normal[0]-cur_vec[0] ) > conf ||
      fabs( normal[1]-cur_vec[1] ) > conf ||
      fabs( normal[2]-cur_vec[2] ) > conf    )
    {
      res = Standard_False;
    }
  }

  if( !res ) {
    for( j=0; j<3; j++ ) {
      normal[j] = 0.0;
      for (i = 0; i < count-2; i++)
        normal[j] += norm_vec[i].Coord(j+1);
      normal[j] /= (count-2);
    }
  }
  delete [] norm_vec;

  Norm.SetCoord( normal[0], normal[1], normal[2] );

  return res;
}