vtkWebUtilities.cxx 3.82 KB
Newer Older
Zach's avatar
Zach committed
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkWebUtilities.cxx

  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 "vtkPython.h" // Need to be first and used for Py_xxx macros
#include "vtkWebUtilities.h"

#include "vtkDataSet.h"
#include "vtkDataSetAttributes.h"
#include "vtkJavaScriptDataWriter.h"
#include "vtkMultiProcessController.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkSplitColumnComponents.h"
#include "vtkTable.h"

#include <vtksys/ios/sstream>

vtkStandardNewMacro(vtkWebUtilities);
//----------------------------------------------------------------------------
vtkWebUtilities::vtkWebUtilities()
{
}

//----------------------------------------------------------------------------
vtkWebUtilities::~vtkWebUtilities()
{
}

//----------------------------------------------------------------------------
std::string vtkWebUtilities::WriteAttributesToJavaScript(
  int field_type, vtkDataSet* dataset)
{
  if (dataset == NULL || (
      field_type != vtkDataObject::POINT &&
      field_type != vtkDataObject::CELL) )
    {
    return "[]";
    }

  vtksys_ios::ostringstream stream;

  vtkNew<vtkDataSetAttributes> clone;
  clone->PassData(dataset->GetAttributes(field_type));
  clone->RemoveArray("vtkValidPointMask");

  vtkNew<vtkTable> table;
  table->SetRowData(clone.GetPointer());

  vtkNew<vtkSplitColumnComponents> splitter;
  splitter->SetInputDataObject(table.GetPointer());
  splitter->Update();

  vtkNew<vtkJavaScriptDataWriter> writer;
  writer->SetOutputStream(&stream);
  writer->SetInputDataObject(splitter->GetOutputDataObject(0));
  writer->SetVariableName(NULL);
  writer->SetIncludeFieldNames(false);
  writer->Write();

  return stream.str();
}

//----------------------------------------------------------------------------
std::string vtkWebUtilities::WriteAttributeHeadersToJavaScript(
  int field_type, vtkDataSet* dataset)
{
  if (dataset == NULL || (
      field_type != vtkDataObject::POINT &&
      field_type != vtkDataObject::CELL) )
    {
    return "[]";
    }

  vtksys_ios::ostringstream stream;
  stream << "[";

  vtkDataSetAttributes* dsa = dataset->GetAttributes(field_type);
  vtkNew<vtkDataSetAttributes> clone;
  clone->CopyAllocate(dsa, 0);
  clone->RemoveArray("vtkValidPointMask");

  vtkNew<vtkTable> table;
  table->SetRowData(clone.GetPointer());

  vtkNew<vtkSplitColumnComponents> splitter;
  splitter->SetInputDataObject(table.GetPointer());
  splitter->Update();

  dsa = vtkTable::SafeDownCast(
    splitter->GetOutputDataObject(0))->GetRowData();

  for (int cc=0; cc < dsa->GetNumberOfArrays(); cc++)
    {
    const char* name = dsa->GetArrayName(cc);
    if (cc != 0)
      {
      stream << ", ";
      }
    stream << "\"" << (name? name : "") << "\"";
    }
  stream << "]";
  return stream.str();
}

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

//----------------------------------------------------------------------------
void vtkWebUtilities::ProcessRMIs()
{
  vtkWebUtilities::ProcessRMIs(1,0);
}

//----------------------------------------------------------------------------
void vtkWebUtilities::ProcessRMIs(int reportError, int dont_loop)
{
  Py_BEGIN_ALLOW_THREADS
  vtkMultiProcessController::GetGlobalController()->ProcessRMIs(reportError, dont_loop);
  Py_END_ALLOW_THREADS
}