forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkBoostRandomSparseArraySource.cxx
121 lines (94 loc) · 4.05 KB
/
vtkBoostRandomSparseArraySource.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkBoostRandomSparseArraySource.cxx
-------------------------------------------------------------------------
Copyright 2008 Sandia Corporation.
Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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 "vtkBoostRandomSparseArraySource.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkSparseArray.h"
#include <boost/random.hpp>
// ----------------------------------------------------------------------
vtkStandardNewMacro(vtkBoostRandomSparseArraySource);
// ----------------------------------------------------------------------
vtkBoostRandomSparseArraySource::vtkBoostRandomSparseArraySource() :
Extents(2, 2),
ElementProbabilitySeed(123),
ElementProbability(0.5),
ElementValueSeed(456),
MinValue(0.0),
MaxValue(1.0)
{
this->SetNumberOfInputPorts(0);
this->SetNumberOfOutputPorts(1);
}
// ----------------------------------------------------------------------
vtkBoostRandomSparseArraySource::~vtkBoostRandomSparseArraySource()
{
}
// ----------------------------------------------------------------------
void vtkBoostRandomSparseArraySource::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Extents: " << this->Extents << endl;
os << indent << "ElementProbabilitySeed: " << this->ElementProbabilitySeed << endl;
os << indent << "ElementProbability: " << this->ElementProbability << endl;
os << indent << "ElementValueSeed: " << this->ElementValueSeed << endl;
os << indent << "MinValue: " << this->MinValue << endl;
os << indent << "MaxValue: " << this->MaxValue << endl;
}
void vtkBoostRandomSparseArraySource::SetExtents(const vtkArrayExtents& extents)
{
if(extents == this->Extents)
return;
this->Extents = extents;
this->Modified();
}
vtkArrayExtents vtkBoostRandomSparseArraySource::GetExtents()
{
return this->Extents;
}
// ----------------------------------------------------------------------
int vtkBoostRandomSparseArraySource::RequestData(
vtkInformation*,
vtkInformationVector**,
vtkInformationVector* outputVector)
{
boost::mt19937 pattern_generator(static_cast<boost::uint32_t>(this->ElementProbabilitySeed));
boost::bernoulli_distribution<> pattern_distribution(this->ElementProbability);
boost::variate_generator<boost::mt19937&, boost::bernoulli_distribution<> > pattern(pattern_generator, pattern_distribution);
boost::mt19937 value_generator(static_cast<boost::uint32_t>(this->ElementValueSeed));
boost::uniform_real<> value_distribution(this->MinValue, this->MaxValue);
boost::variate_generator<boost::mt19937&, boost::uniform_real<> > values(value_generator, value_distribution);
vtkSparseArray<double>* const array = vtkSparseArray<double>::New();
array->Resize(this->Extents);
vtkArrayCoordinates coordinates;
for(vtkArray::SizeT n = 0; n != this->Extents.GetSize(); ++n)
{
this->Extents.GetRightToLeftCoordinatesN(n, coordinates);
// Although it seems wasteful, we calculate a value for every element in the array
// so the results stay consistent as the ElementProbability varies
const double value = values();
if(pattern())
{
array->AddValue(coordinates, value);
}
}
vtkArrayData* const output = vtkArrayData::GetData(outputVector);
output->ClearArrays();
output->AddArray(array);
array->Delete();
return 1;
}