vtkCompleteArrays.cxx 7.64 KB
Newer Older
1 2
/*=========================================================================

3
  Program:   ParaView
4 5
  Module:    vtkCompleteArrays.cxx

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

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 13 14 15 16 17
     PURPOSE.  See the above copyright notice for more information.

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

#include "vtkCellData.h"
18 19
#include "vtkCharArray.h"
#include "vtkClientServerStream.h"
20
#include "vtkDataArray.h"
21
#include "vtkDataSet.h"
22
#include "vtkDoubleArray.h"
23 24
#include "vtkFloatArray.h"
#include "vtkIdTypeArray.h"
Berk Geveci's avatar
Berk Geveci committed
25 26
#include "vtkInformation.h"
#include "vtkInformationVector.h"
27 28
#include "vtkIntArray.h"
#include "vtkLongArray.h"
29 30 31
#include "vtkMultiProcessController.h"
#include "vtkObjectFactory.h"
#include "vtkPVArrayInformation.h"
32 33
#include "vtkPVDataInformation.h"
#include "vtkPVDataSetAttributesInformation.h"
34
#include "vtkPointData.h"
35
#include "vtkPointSet.h"
36 37 38 39 40
#include "vtkShortArray.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnsignedIntArray.h"
#include "vtkUnsignedLongArray.h"
#include "vtkUnsignedShortArray.h"
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

vtkStandardNewMacro(vtkCompleteArrays);

vtkCxxSetObjectMacro(vtkCompleteArrays,Controller,vtkMultiProcessController);

//-----------------------------------------------------------------------------
vtkCompleteArrays::vtkCompleteArrays()
{
  this->Controller = vtkMultiProcessController::GetGlobalController();
  if (this->Controller)
    {
    this->Controller->Register(this);
    }
}

//-----------------------------------------------------------------------------
vtkCompleteArrays::~vtkCompleteArrays()
{
  if (this->Controller)
    {
    this->Controller->UnRegister(this);
    this->Controller = NULL;
    }
}

//-----------------------------------------------------------------------------
Berk Geveci's avatar
Berk Geveci committed
67 68 69 70
int vtkCompleteArrays::RequestData(
  vtkInformation*,
  vtkInformationVector** inputVector,
  vtkInformationVector* outputVector)
71
{
Berk Geveci's avatar
Berk Geveci committed
72 73 74 75 76 77 78 79
  vtkInformation* info = outputVector->GetInformationObject(0);
  vtkDataSet *output = vtkDataSet::SafeDownCast(
    info->Get(vtkDataObject::DATA_OBJECT()));

  vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
  vtkDataSet *input = vtkDataSet::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));

80 81 82 83
  int noNeed = 0;
  int myProcId;
  int numProcs;
  int idx;
84
  vtkClientServerStream css;
85 86 87

  // Initialize
  //
Mathieu Malaterre's avatar
Mathieu Malaterre committed
88
  vtkDebugMacro(<<"Completing array");
89

Berk Geveci's avatar
Berk Geveci committed
90 91 92
  output->CopyStructure( input );
  output->GetPointData()->PassData(input->GetPointData());
  output->GetCellData()->PassData(input->GetCellData());
93 94 95

  myProcId = this->Controller->GetLocalProcessId();
  numProcs = this->Controller->GetNumberOfProcesses();
96 97 98 99
  if (numProcs <= 1)
    {
    return 1;
    }
100 101 102 103 104 105 106

  if (myProcId == 0)
    {
    if (input->GetNumberOfPoints() > 0 && input->GetNumberOfCells() > 0)
      {
      noNeed = 1;
      }
107
    this->Controller->Broadcast(&noNeed, 1, 0);
108 109
    if (noNeed)
      {
Berk Geveci's avatar
Berk Geveci committed
110
      return 1;
111 112 113 114 115 116
      }
    // Receive and collected information from the remote processes.
    vtkPVDataInformation* dataInfo = vtkPVDataInformation::New();
    vtkPVDataInformation* tmpInfo = vtkPVDataInformation::New();
    for (idx = 1; idx < numProcs; ++idx)
      {
117
      int length = 0;
118
      this->Controller->Receive(&length, 1, idx, 3389002);
119 120 121 122 123
      unsigned char* data = new unsigned char[length];
      this->Controller->Receive(data, length, idx, 3389003);
      css.SetData(data, length);
      tmpInfo->CopyFromStream(&css);
      delete [] data;
124 125
      dataInfo->AddInformation(tmpInfo);
      }
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    this->FillArrays(
      output->GetPointData(), dataInfo->GetPointDataInformation());
    this->FillArrays(
      output->GetCellData(), dataInfo->GetCellDataInformation());
    vtkPointSet* ps = vtkPointSet::SafeDownCast(output);
    if (ps)
      {
      vtkDataArray* pointArray = this->CreateArray(
        dataInfo->GetPointArrayInformation());
      if (!pointArray)
        {
        vtkErrorMacro("Could not create point array. "
                      "The output will not contain points");
        }
      else
        {
        vtkPoints* pts = vtkPoints::New();
        pts->SetData(pointArray);
        pointArray->Delete();
        ps->SetPoints(pts);
        pts->Delete();
        }
      }
149 150 151 152 153
    dataInfo->Delete();
    tmpInfo->Delete();
    }
  else
    { // remote processes
154
    this->Controller->Broadcast(&noNeed, 1, 0);
155 156
    if (noNeed)
      {
Berk Geveci's avatar
Berk Geveci committed
157
      return 1;
158
      }
159
    vtkPVDataInformation* dataInfo = vtkPVDataInformation::New();
160
    dataInfo->CopyFromObject(output);
161 162 163 164 165 166 167
    dataInfo->CopyToStream(&css);
    size_t length;
    const unsigned char* data;
    css.GetData(&data, &length);
    int len = static_cast<int>(length);
    this->Controller->Send(&len, 1, 0, 3389002);
    this->Controller->Send(const_cast<unsigned char*>(data), len, 0, 3389003);
168 169
    dataInfo->Delete();
    }
Berk Geveci's avatar
Berk Geveci committed
170 171

  return 1;
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
//-----------------------------------------------------------------------------
vtkDataArray* vtkCompleteArrays::CreateArray(vtkPVArrayInformation* aInfo)
{
  vtkDataArray* array = 0;
  switch (aInfo->GetDataType())
    {
    case VTK_FLOAT:
      array = vtkFloatArray::New();
      break;
    case VTK_DOUBLE:
      array = vtkDoubleArray::New();
      break;
    case VTK_INT:
      array = vtkIntArray::New();
      break;
    case VTK_CHAR:
      array = vtkCharArray::New();
      break;
    case VTK_ID_TYPE:
      array = vtkIdTypeArray::New();
      break;
    case VTK_LONG:
      array = vtkLongArray::New();
      break;
    case VTK_SHORT:
      array = vtkShortArray::New();
      break;
    case VTK_UNSIGNED_CHAR:
      array = vtkUnsignedCharArray::New();
      break;
    case VTK_UNSIGNED_INT:
      array = vtkUnsignedIntArray::New();
      break;
    case VTK_UNSIGNED_LONG:
      array = vtkUnsignedLongArray::New();
      break;
    case VTK_UNSIGNED_SHORT:
      array = vtkUnsignedShortArray::New();
      break;
    default:
      array = NULL;
    }
  if (array)
    {
    array->SetNumberOfComponents(aInfo->GetNumberOfComponents());
    array->SetName(aInfo->GetName());
    }

  return array;

}
225 226 227 228 229 230 231 232 233 234 235 236 237 238

//-----------------------------------------------------------------------------
void vtkCompleteArrays::FillArrays(vtkDataSetAttributes* da, 
                                   vtkPVDataSetAttributesInformation* attrInfo)
{
  int num, idx;
  vtkPVArrayInformation* arrayInfo;
  vtkDataArray* array;

  da->Initialize();
  num = attrInfo->GetNumberOfArrays();
  for (idx = 0; idx < num; ++idx)
    {
    arrayInfo = attrInfo->GetArrayInformation(idx);
239
    array = this->CreateArray(arrayInfo);
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
    if (array)
      {
      switch (attrInfo->IsArrayAnAttribute(idx))
        {
        case vtkDataSetAttributes::SCALARS:
          da->SetScalars(array);
          break;
        case vtkDataSetAttributes::VECTORS:
          da->SetVectors(array);
          break;
        case vtkDataSetAttributes::NORMALS:
          da->SetNormals(array);
          break;
        case vtkDataSetAttributes::TENSORS:
          da->SetTensors(array);
          break;
        case vtkDataSetAttributes::TCOORDS:
          da->SetTCoords(array);
          break;
        default:
          da->AddArray(array);
        }
      array->Delete();
      array = NULL;
      }
    }
}


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

  if (this->Controller)
    {
    os << indent << "Controller: " << this->Controller << endl;
    }
Mathieu Malaterre's avatar
Mathieu Malaterre committed
278 279 280 281
  else
    {
    os << indent << "Controller: (none)\n";
    }
282
}