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
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestMatlabEngineInterface.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 <vtkMatlabEngineInterface.h>
#include <vtkSmartPointer.h>
#include <vtkDoubleArray.h>
#include <vtkArray.h>
#include <vtkDenseArray.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <stdio.h>
#include <string.h>
#include <cassert>
namespace
{
#define test_expression(expression) \
{ \
if(!(expression)) \
{ \
std::ostringstream buffer; \
buffer << "Expression failed at line " << __LINE__ << ": " << #expression; \
throw std::runtime_error(buffer.str()); \
} \
}
bool doubleEquals(double left, double right, double epsilon) {
return (fabs(left - right) < epsilon);
}
}
int TestMatlabEngineInterface(int vtkNotUsed(argc), char *vtkNotUsed(argv)[])
{
try
{
int buf_size = 2000;
char* out_buffer = new char[buf_size];
out_buffer[0] = '\0';
vtkDoubleArray* da = vtkDoubleArray::New();
vtkDenseArray<double>* dda = vtkDenseArray<double>::New();
vtkMatlabEngineInterface* mei = vtkMatlabEngineInterface::New();
mei->SetVisibleOff();
mei->OutputBuffer(out_buffer, buf_size);
mei->EvalString("1:10\n");
test_expression(strlen(out_buffer) > 10);
da->SetNumberOfComponents(3);
for( int cc = 0; cc < 10; cc ++ )
{
da->InsertNextTuple3( cc + 0.1, cc + 0.2, cc + 0.3);
}
mei->PutVtkDataArray("d",da);
mei->EvalString("d(:,1) = d(:,1) - 0.1;\n\
d(:,2) = d(:,2) - 0.2;\n\
d(:,3) = d(:,3) - 0.3;\n");
cout << out_buffer << endl;
vtkDoubleArray* rda = vtkArrayDownCast<vtkDoubleArray>(mei->GetVtkDataArray("d"));
test_expression(rda);
for(int i = 0;i<rda->GetNumberOfTuples();i++)
{
double* iv = da->GetTuple3(i);
double* rv = rda->GetTuple3(i);
test_expression(doubleEquals(iv[0] - 0.1,rv[0],0.001));
test_expression(doubleEquals(iv[1] - 0.2,rv[1],0.001));
test_expression(doubleEquals(iv[2] - 0.3,rv[2],0.001));
}
dda->Resize(vtkArrayExtents(3, 3, 5));
dda->Fill(64.0);
mei->PutVtkArray("a",dda);
mei->EvalString("a = sqrt(a);\n");
vtkDenseArray<double>* rdda = vtkDenseArray<double>::SafeDownCast(mei->GetVtkArray("a"));
assert(rdda->GetExtents().ZeroBased());
const vtkArrayExtents extents = rdda->GetExtents();
for(int i = 0; i != extents[0].GetSize(); ++i)
{
for(int j = 0; j != extents[1].GetSize(); ++j)
{
for(int k = 0; k != extents[2].GetSize(); ++k)
{
test_expression(doubleEquals(sqrt(dda->GetValue(vtkArrayCoordinates(i, j, k))),
rdda->GetValue(vtkArrayCoordinates(i, j, k)),
0.001));
}
}
}
dda->Delete();
da->Delete();
mei->Delete();
delete [] out_buffer;
return 0;
}
catch(std::exception& e)
{
cerr << e.what() << endl;
return 1;
}
}
|