vtkBoostRandomSparseArraySource.cxx 4.05 KB
Newer Older
1 2 3 4
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkBoostRandomSparseArraySource.cxx
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
-------------------------------------------------------------------------
  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(
85 86
  vtkInformation*,
  vtkInformationVector**,
87 88 89 90 91 92 93 94 95 96 97 98 99
  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);

100
  vtkArrayCoordinates coordinates;
101
  for(vtkArray::SizeT n = 0; n != this->Extents.GetSize(); ++n)
102
  {
103
    this->Extents.GetRightToLeftCoordinatesN(n, coordinates);
104 105 106 107 108

    // 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())
109
    {
110 111
      array->AddValue(coordinates, value);
    }
112
  }
113 114

  vtkArrayData* const output = vtkArrayData::GetData(outputVector);
115 116
  output->ClearArrays();
  output->AddArray(array);
117 118 119 120 121
  array->Delete();

  return 1;
}