File: vtkSMChartSeriesSelectionDomain.cxx

package info (click to toggle)
paraview 5.1.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 221,108 kB
  • ctags: 236,092
  • sloc: cpp: 2,416,026; ansic: 190,891; python: 99,856; xml: 81,001; tcl: 46,915; yacc: 5,039; java: 4,413; perl: 3,108; sh: 1,974; lex: 1,926; f90: 748; asm: 471; pascal: 228; makefile: 198; objc: 83; fortran: 31
file content (526 lines) | stat: -rw-r--r-- 17,224 bytes parent folder | download | duplicates (2)
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*=========================================================================

  Program:   ParaView
  Module:    $RCSfile$

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkSMChartSeriesSelectionDomain.h"

#include "vtkChartRepresentation.h"
#include "vtkColorSeries.h"
#include "vtkCommand.h"
#include "vtkDataObject.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPVArrayInformation.h"
#include "vtkPVCompositeDataInformation.h"
#include "vtkPVDataInformation.h"
#include "vtkPVDataSetAttributesInformation.h"
#include "vtkPVXMLElement.h"
#include "vtkSMArrayListDomain.h"
#include "vtkSMSourceProxy.h"
#include "vtkSMStringVectorProperty.h"
#include "vtkSMUncheckedPropertyHelper.h"
#include "vtkStdString.h"
#include "vtkStringList.h"

#include <algorithm>
#include <assert.h>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <vtksys/RegularExpression.hxx>

vtkStandardNewMacro(vtkSMChartSeriesSelectionDomain);
namespace
{
  // match string like: "ACCL (0)" or "VEL (1)"
  static vtksys::RegularExpression PotentailComponentNameRe(".*\\([0-9]+\\)");

  typedef std::pair<vtksys::RegularExpression, bool> SeriesVisibilityPair;
  static std::vector<SeriesVisibilityPair> SeriesVisibilityDefaults;
  static void InitSeriesVisibilityDefaults()
    {
    // initialize SeriesVisibilityDefaults the first time.
    if (SeriesVisibilityDefaults.size() == 0)
      {
      const char* defaults[] = {
        "^arc_length",
        "^bin_extents",
        "^FileId",
        "^GlobalElementId",
        "^GlobalNodeId",
        "^ObjectId",
        "^Pedigree.*",
        "^Points_.*",
        "^Time",
        "^vtkOriginal.*",
        "^vtkValidPointMask",
        "^N .*",
        NULL
      };
      for (int cc=0; defaults[cc] != NULL; cc++)
        {
        SeriesVisibilityDefaults.push_back(SeriesVisibilityPair(
            vtksys::RegularExpression(defaults[cc]), false));
        }
      }
    }
  static void AddSeriesVisibilityDefault(const char* regex, bool value)
    {
    InitSeriesVisibilityDefaults();
    if (regex && regex[0])
      {
      SeriesVisibilityDefaults.push_back(SeriesVisibilityPair(
          vtksys::RegularExpression(regex), value));
      }
    }

  static bool GetSeriesVisibilityDefault(const char* regex, bool &value)
    {
    InitSeriesVisibilityDefaults();
    for (size_t cc=0; cc < SeriesVisibilityDefaults.size(); cc++)
      {
      if (SeriesVisibilityDefaults[cc].first.find(regex))
        {
        value = SeriesVisibilityDefaults[cc].second;
        return true;
        }
      }
    return false;
    }

}

class vtkSMChartSeriesSelectionDomain::vtkInternals
{
  int ColorCounter;
  vtkNew<vtkColorSeries> Colors;
public:
  std::map<std::string, bool> VisibilityOverrides;

  vtkInternals() : ColorCounter(0)
    {
    }

  vtkColor3ub GetNextColor()
    {
    return this->Colors->GetColorRepeating(this->ColorCounter++);
    }
};

//----------------------------------------------------------------------------
vtkSMChartSeriesSelectionDomain::vtkSMChartSeriesSelectionDomain() :
  Internals(new vtkSMChartSeriesSelectionDomain::vtkInternals())
{
  this->DefaultMode = vtkSMChartSeriesSelectionDomain::UNDEFINED;
  this->DefaultValue = 0;
  this->SetDefaultValue("");
  this->FlattenTable = true;

  this->AddObserver(vtkCommand::DomainModifiedEvent,
    this, &vtkSMChartSeriesSelectionDomain::OnDomainModified);
}

//----------------------------------------------------------------------------
vtkSMChartSeriesSelectionDomain::~vtkSMChartSeriesSelectionDomain()
{
  this->SetDefaultValue(NULL);
  delete this->Internals;
  this->Internals = NULL;
}

//----------------------------------------------------------------------------
vtkPVDataInformation* vtkSMChartSeriesSelectionDomain::GetInputInformation()
{
  vtkSMProperty* inputProperty = this->GetRequiredProperty("Input");
  assert(inputProperty);

  vtkSMUncheckedPropertyHelper helper(inputProperty);
  if (helper.GetNumberOfElements() > 0)
    {
    vtkSMSourceProxy* sp = vtkSMSourceProxy::SafeDownCast(helper.GetAsProxy(0));
    if (sp)
      {
      return sp->GetDataInformation(helper.GetOutputPort());
      }
    }
  return NULL;
}

//----------------------------------------------------------------------------
int vtkSMChartSeriesSelectionDomain::ReadXMLAttributes(
  vtkSMProperty* prop, vtkPVXMLElement* element)
{
  if (!this->Superclass::ReadXMLAttributes(prop, element))
    {
    return 0;
    }

  const char* default_mode = element->GetAttribute("default_mode");
  if (default_mode)
    {
    if (strcmp(default_mode, "visibility") == 0)
      {
      this->DefaultMode = vtkSMChartSeriesSelectionDomain::VISIBILITY;
      }
    else if (strcmp(default_mode, "label") == 0)
      {
      this->DefaultMode = vtkSMChartSeriesSelectionDomain::LABEL;
      }
    else if (strcmp(default_mode, "color") == 0)
      {
      this->DefaultMode = vtkSMChartSeriesSelectionDomain::COLOR;
      }
    else if (strcmp(default_mode, "value") == 0)
      {
      this->DefaultMode = vtkSMChartSeriesSelectionDomain::VALUE;
      if (element->GetAttribute("default_value"))
        {
        this->SetDefaultValue(element->GetAttribute("default_value"));
        }
      }
    }

  const char* flatten_table = element->GetAttribute("flatten_table");
  if (flatten_table)
    {
    this->FlattenTable = (strcmp(default_mode, "true") == 0);
    }
  return 1;
}

//----------------------------------------------------------------------------
void vtkSMChartSeriesSelectionDomain::Update(vtkSMProperty*)
{
  vtkSMProperty* input = this->GetRequiredProperty("Input");
  vtkSMProperty* fieldDataSelection =
    this->GetRequiredProperty("FieldDataSelection");
  vtkSMVectorProperty* compositeIndex = vtkSMVectorProperty::SafeDownCast(
    this->GetRequiredProperty("CompositeIndexSelection"));

  if (!input || !fieldDataSelection)
    {
    vtkWarningMacro("Missing required properties. Update failed.");
    return;
    }

  // build strings based on the current domain.
  vtkPVDataInformation* dataInfo = this->GetInputInformation();
  if (!dataInfo)
    {
    return;
    }

  // clear old component names.
  this->Internals->VisibilityOverrides.clear();

  if (compositeIndex == NULL ||
    dataInfo->GetCompositeDataInformation()->GetDataIsComposite() == 0)
    {
    // since there's no way to choose which dataset from a composite one to use,
    // just look at the top-level array information (skipping partial arrays).
    std::vector<vtkStdString> column_names;
    int fieldAssociation = vtkSMUncheckedPropertyHelper(fieldDataSelection).GetAsInt(0);
    this->PopulateAvailableArrays(std::string(),
      column_names, dataInfo, fieldAssociation, this->FlattenTable);
    this->SetStrings(column_names);
    return;
    }

  std::vector<vtkStdString> column_names;
  int fieldAssociation = vtkSMUncheckedPropertyHelper(fieldDataSelection).GetAsInt(0);

  vtkSMUncheckedPropertyHelper compositeIndexHelper(compositeIndex);
  unsigned int numElems = compositeIndexHelper.GetNumberOfElements();
  for (unsigned int cc=0; cc < numElems; cc++)
    {
    vtkPVDataInformation* childInfo =
      dataInfo->GetDataInformationForCompositeIndex(compositeIndexHelper.GetAsInt(cc));
    if (!childInfo)
      {
      continue;
      }
    std::ostringstream blockNameStream;
    if (compositeIndex->GetRepeatCommand())
      {
      // we don't need to add blockName is the proxy doesn't support selecting
      // multiple blocks in the dataset.
      if (childInfo->GetCompositeDataSetName())
        {
        blockNameStream << childInfo->GetCompositeDataSetName();
        }
      else
        {
        blockNameStream << compositeIndexHelper.GetAsInt(cc);
        }
      }
    // if there is only 1 element, use the element to avoid having partial data.
    vtkPVDataInformation* dataInfoWithArrays =
      numElems == 1 ? childInfo : dataInfo;
    this->PopulateAvailableArrays(blockNameStream.str(),
      column_names, dataInfoWithArrays, fieldAssociation,
      this->FlattenTable);
    }
  this->SetStrings(column_names);
}

//----------------------------------------------------------------------------
// Add arrays from dataInfo to strings. If blockName is non-empty, then it's
// used to "uniquify" the array names.
void vtkSMChartSeriesSelectionDomain::PopulateAvailableArrays(
  const std::string& blockName,
  std::vector<vtkStdString>& strings,
  vtkPVDataInformation* dataInfo, int fieldAssociation, bool flattenTable)
{
  // this method is typically called for leaf nodes (or multi-piece).
  // assert((dataInfo->GetCompositeDataInformation()->GetDataIsComposite() == 0) ||
  //   (dataInfo->GetCompositeDataInformation()->GetDataIsMultiPiece() == 0));
  vtkChartRepresentation* chartRepr = vtkChartRepresentation::SafeDownCast(
    this->GetProperty()->GetParent()->GetClientSideObject());
  if (!chartRepr)
    {
    return;
    }

  // helps use avoid duplicates. duplicates may arise for plot types that treat
  // multiple columns as a single series/plot e.g. quartile plots.
  std::set<vtkStdString> uniquestrings;

  vtkPVDataSetAttributesInformation* dsa =
    dataInfo->GetAttributeInformation(fieldAssociation);
  for (int cc=0; dsa != NULL && cc < dsa->GetNumberOfArrays(); cc++)
    {
    vtkPVArrayInformation* arrayInfo = dsa->GetArrayInformation(cc);
    this->PopulateArrayComponents(chartRepr, blockName, strings, uniquestrings,
                                  arrayInfo, flattenTable);
    }

  if (fieldAssociation == vtkDataObject::FIELD_ASSOCIATION_POINTS)
    {
    vtkPVArrayInformation* pointArrayInfo = dataInfo->GetPointArrayInformation();
    this->PopulateArrayComponents(chartRepr, blockName, strings, uniquestrings,
                                  pointArrayInfo, flattenTable);
    }
}

//----------------------------------------------------------------------------
// Add array component from arrayInfo to strings. If blockName is non-empty, then it's
// used to "uniquify" the array names.
void vtkSMChartSeriesSelectionDomain::PopulateArrayComponents(  
  vtkChartRepresentation* chartRepr,
  const std::string& blockName, std::vector<vtkStdString>& strings, 
  std::set<vtkStdString>& uniquestrings,
  vtkPVArrayInformation* arrayInfo, bool flattenTable)
{
  if (arrayInfo != NULL && arrayInfo->GetIsPartial() != 1)
    {
    if (arrayInfo->GetNumberOfComponents() > 1 && flattenTable)
      {
      for (int kk=0; kk <= arrayInfo->GetNumberOfComponents(); kk++)
        {
        std::string component_name = vtkSMArrayListDomain::CreateMangledName(arrayInfo, kk);
        component_name = chartRepr->GetDefaultSeriesLabel(blockName, component_name);
        if (uniquestrings.find(component_name) == uniquestrings.end())
          {
          strings.push_back(component_name);
          uniquestrings.insert(component_name);
          }
        if (kk != arrayInfo->GetNumberOfComponents())
          {
          // save component names so we can detect them when setting defaults
          // later.
          this->SetDefaultVisibilityOverride(component_name, false);
          }
        }
      }
    else
      {
      char* arrayName = arrayInfo->GetName();
      if (arrayName != NULL)
        { 
        std::string seriesName = chartRepr->GetDefaultSeriesLabel(blockName, arrayName);
        if (uniquestrings.find(seriesName) == uniquestrings.end())
          {
          strings.push_back(seriesName);
          uniquestrings.insert(seriesName);

          // Special case for Quartile plots. PlotSelectionOverTime filter, when
          // produces stats, likes to pre-split components in an array into multiple
          // single component arrays. We still want those to be treated as
          // components and not be shown by default. Hence, we use this hack.
          // (See BUG #15512).
          std::string seriesNameWithoutTableName = chartRepr->GetDefaultSeriesLabel(
            std::string(), arrayName);
          if (PotentailComponentNameRe.find(seriesNameWithoutTableName))
            {
            this->SetDefaultVisibilityOverride(seriesName, false);
            }
          }
        }
      }
    }
}

//----------------------------------------------------------------------------
std::vector<vtkStdString> vtkSMChartSeriesSelectionDomain::GetDefaultValue(const char* series)
{
  std::vector<vtkStdString> values;
  if (this->DefaultMode == VISIBILITY)
    {
    values.push_back(
      this->GetDefaultSeriesVisibility(series)?
      "1" : "0");
    }
  else if (this->DefaultMode == LABEL)
    {
    // by default, label is same as the name of the series.
    values.push_back(series);
    }
  else if (this->DefaultMode == COLOR)
    {
    vtkColor3ub color = this->Internals->GetNextColor();
    for (int kk=0; kk < 3; kk++)
      {
      std::ostringstream stream;
      stream << std::setprecision(2) << color.GetData()[kk]/255.0;
      values.push_back(stream.str());
      }
    }
  else if (this->DefaultMode == VALUE)
    {
    values.push_back(this->DefaultValue);
    }
  return values;
}

//----------------------------------------------------------------------------
int vtkSMChartSeriesSelectionDomain::SetDefaultValues(
  vtkSMProperty* property, bool use_unchecked_values)
{
  vtkSMStringVectorProperty* vp = vtkSMStringVectorProperty::SafeDownCast(property);
  if (!vp)
    {
    vtkErrorMacro("Property must be a vtkSMVectorProperty subclass.");
    return 0;
    }
  if (use_unchecked_values)
    {
    vtkWarningMacro("Developer warning: use_unchecked_values not implemented yet.");
    }

  this->UpdateDefaultValues(property, false);
  return 1;
}

//----------------------------------------------------------------------------
void vtkSMChartSeriesSelectionDomain::UpdateDefaultValues(
  vtkSMProperty* property, bool preserve_previous_values)
{
  vtkSMStringVectorProperty* vp = vtkSMStringVectorProperty::SafeDownCast(property);
  assert(vp != NULL);

  vtkNew<vtkStringList> values;
  std::set<std::string> seriesNames;
  if (preserve_previous_values)
    {
    // capture old values.
    unsigned int numElems = vp->GetNumberOfElements();
    int stepSize = vp->GetNumberOfElementsPerCommand() > 0?
      vp->GetNumberOfElementsPerCommand() : 1;

    for (unsigned int cc=0; (cc+stepSize) <= numElems; cc+=stepSize)
      {
      seriesNames.insert(vp->GetElement(cc)? vp->GetElement(cc) : "");
      for (int kk=0; kk < stepSize; kk++)
        {
        values->AddString(vp->GetElement(cc+kk));
        }
      }
    }

  const std::vector<vtkStdString>& domain_strings = this->GetStrings();
  for (size_t cc=0; cc < domain_strings.size(); cc++)
    {
    if (preserve_previous_values &&
      seriesNames.find(domain_strings[cc]) != seriesNames.end())
      {
      // skip this. This series had a value set already which we are requested
      // to preserve.
      continue;
      }
    std::vector<vtkStdString> cur_values =
      this->GetDefaultValue(domain_strings[cc].c_str());
    if (cur_values.size() > 0)
      {
      values->AddString(domain_strings[cc].c_str());
      for (size_t kk=0; kk < cur_values.size(); kk++)
        {
        values->AddString(cur_values[kk].c_str());
        }
      }
    }

  vp->SetElements(values.GetPointer());
}

//----------------------------------------------------------------------------
void vtkSMChartSeriesSelectionDomain::AddSeriesVisibilityDefault(
  const char* name, bool value)
{
  ::AddSeriesVisibilityDefault(name, value);
}

//----------------------------------------------------------------------------
bool vtkSMChartSeriesSelectionDomain::GetDefaultSeriesVisibility(const char* name)
{
  bool result;
  if (::GetSeriesVisibilityDefault(name, result))
    {
    return result;
    }

  if (this->Internals->VisibilityOverrides.find(name) !=
    this->Internals->VisibilityOverrides.end())
    {
    //  hide components by default, we'll show the magnitudes for them.
    return this->Internals->VisibilityOverrides[name];
    }

  return true;
}

//----------------------------------------------------------------------------
void vtkSMChartSeriesSelectionDomain::OnDomainModified()
{
  vtkSMProperty* prop = this->GetProperty();
  this->UpdateDefaultValues(prop, true);
  if (prop->GetParent())
    {
    // FIXME:
    // prop->GetParent()->UpdateProperty(prop);
    prop->GetParent()->UpdateVTKObjects();
    }
}

//----------------------------------------------------------------------------
void vtkSMChartSeriesSelectionDomain::SetDefaultVisibilityOverride(
  const vtkStdString& arrayname, bool visibility)
{
  this->Internals->VisibilityOverrides[arrayname] = visibility;
}

//----------------------------------------------------------------------------
void vtkSMChartSeriesSelectionDomain::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}