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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 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
|
#ifdef HAVE_VTK
#include "VTKWrap.hpp"
#include "VTKWindow.hpp"
#include <QtCore/QString>
#include <QtGui/QLabel>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QAction>
#include <QtGui/QFrame>
#include <QtGui/QComboBox>
#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>
#include <QtGui/QCompleter>
#include <QtGui/QFileSystemModel>
#include <QtGui/QPushButton>
#include <QtGui/QSplitter>
#include <QtGui/QGroupBox>
#include <QtCore/QDebug>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include "vtkCylinderSource.h"
#include <vtkPolyDataMapper.h>
#include <vtkStructuredPointsReader.h>
#include <vtkStructuredPoints.h>
#include <vtkVolumeRayCastMapper.h>
#include <vtkPiecewiseFunction.h>
#include <vtkColorTransferFunction.h>
#include <vtkVolumeProperty.h>
#include <vtkVolumeRayCastCompositeFunction.h>
#include <vtkVolumeTextureMapper3D.h>
#include <vtkOpenGLVolumeTextureMapper3D.h>
#include <vtkFixedPointVolumeRayCastMapper.h>
#include <vtkImagePlaneWidget.h>
#include <vtkActor2D.h>
#include <vtkOpenGLImageMapper.h>
#include <vtkImageReslice.h>
#include <vtkPNGWriter.h>
#include <vtkProperty.h>
#include <vtkImageImport.h>
void VTKViewer::LoadData(const Array &dp,
const Array &Opacity,
const Array &ColorTransfer,
const Array &Material) {
source = vtkImageImport::New();
volume = dp.asDenseArray().toClass(UInt16);
source->SetImportVoidPointer(volume.getVoidPointer());
source->SetDataScalarTypeToUnsignedShort();
source->SetDataExtent(0,volume.dimensions()[0]-1,
0,volume.dimensions()[1]-1,
0,volume.dimensions()[2]-1);
// Create transfer mapping scalar value to opacity
vtkPiecewiseFunction* opacityTransferFunction = vtkPiecewiseFunction::New();
const BasicArray<double>& opt = Opacity.constReal<double>();
for (int i=0;i<opt.cols();i++)
opacityTransferFunction->AddPoint(opt[NTuple(1,i+1)],opt[NTuple(2,i+1)]);
// Create transfer mapping scalar value to color
vtkColorTransferFunction* colorTransferFunction = vtkColorTransferFunction::New();
const BasicArray<double> &cpt = ColorTransfer.constReal<double>();
for (int i=0;i<cpt.cols();i++)
colorTransferFunction->AddHSVPoint(cpt[NTuple(1,i+1)],
cpt[NTuple(2,i+1)],
cpt[NTuple(3,i+1)],
cpt[NTuple(4,i+1)]);
// The property describes how the data will look
vtkVolumeProperty* volumeProperty = vtkVolumeProperty::New();
volumeProperty->SetColor ( colorTransferFunction );
volumeProperty->SetScalarOpacity ( opacityTransferFunction );
volumeProperty->ShadeOn();
const BasicArray<double> &mpt = Material.constReal<double>();
volumeProperty->SetDiffuse ( mpt[1] );
volumeProperty->SetAmbient ( mpt[2] );
volumeProperty->SetSpecular ( mpt[3] );
volumeProperty->SetSpecularPower ( mpt[4] );
volumeProperty->SetInterpolationTypeToLinear();
// The mapper / ray cast function know how to render the data
vtkVolumeRayCastCompositeFunction* compositeFunction = vtkVolumeRayCastCompositeFunction::New();
// Mapper
// mapper = vtkOpenGLVolumeTextureMapper3D::New();
mapper = vtkFixedPointVolumeRayCastMapper::New();
//mapper->ImmediateModeRenderingOn();
//mapper->SetVolumeRayCastFunction ( compositeFunction );
mapper->SetInputConnection ( source->GetOutputPort() );
source->Update();
qDebug() << source->GetOutput()->GetDimensions() [0];
qDebug() << source->GetOutput()->GetDimensions() [2];
// Actor in scene
actor = vtkVolume::New();
actor->SetMapper ( mapper );
actor->SetProperty ( volumeProperty );
// Add Actor to renderer
ren->AddVolume ( actor );
// Reset camera
ren->ResetCamera();
ren->GetRenderWindow()->Render();
}
VTKViewer::VTKViewer() {
actor = NULL;
QVBoxLayout *layout = new QVBoxLayout;
// QT/VTK interact
vtkWidget = new QVTKWidget ( this );
vtkWidget->setMinimumSize ( 400, 400 );
ren = vtkRenderer::New();
ren->SetBackground ( 1, 1, 1 );
vtkWidget->GetRenderWindow()->AddRenderer ( ren );
layout->addWidget(vtkWidget);
setLayout(layout);
source = NULL;
mapper = NULL;
actor = NULL;
}
VTKViewer::~VTKViewer() {
ren->Delete();
if ( source ) {
source->Delete();
}
if ( mapper ) {
mapper->Delete();
}
if ( actor ) {
actor->Delete();
}
}
// Help for volrender
//
// volrender(volume, opacityfunction, colortransferfunction, matlpropvec)
//@@Signature
//sgfunction vtkfigure VTKFigureFunction
//input render
//output none
ArrayVector VTKFigureFunction(int nargout, const ArrayVector& arg, Interpreter *)
{
if (arg.size() != 1) return ArrayVector();
vtkRenderer *r = GetVTKPointer<vtkRenderer>(arg[0]);
QVTKWidget *p = new QVTKWidget;
p->setMinimumSize(400,400);
p->GetRenderWindow()->AddRenderer(r);
p->show();
return ArrayVector();
}
//@@Signature
//sgfunction volrender VolRenderFunction
//input volume opacity ctransfer material
//output none
ArrayVector VolRenderFunction(int nargout, const ArrayVector& arg, Interpreter *)
{
if (arg.size() < 4)
throw Exception("volrender requires at least 4 arguments (volume, opacity, colortransfer, material)");
// Validate the data
if (arg[0].isReferenceType())
throw Exception("volrender cannot accept non-numeric volumes");
if (!arg[0].allReal())
throw Exception("volrender dataset cannot be complex");
Array Opacity = arg[1].asDenseArray().toClass(Double);
if (!Opacity.is2D() || Opacity.rows() != 2)
throw Exception("opacity argument must be a 2 X N array");
Array ColorTransfer = arg[2].asDenseArray().toClass(Double);
if (!ColorTransfer.is2D() || ColorTransfer.rows() != 4)
throw Exception("color transfer function must be a 4 X N array");
Array Material = arg[3].asDenseArray().toClass(Double);
if (Material.length() != 4)
throw Exception("material properties vector must be a length 4 vector");
VTKViewer *foo = new VTKViewer();
foo->LoadData(arg[0],Opacity,ColorTransfer,Material);
foo->show();
return ArrayVector();
}
//**********************************************************************
//#include "/home/sbasu/Devel/fm4/tools/vtkwrap/tst.c"
//**********************************************************************
#else
#include "Array.hpp"
ArrayVector VolRenderFunction(int nargout, const ArrayVector& arg, Interpreter *)
{
throw Exception("volrender function requires VTK support, which was not enabled at compile time");
}
ArrayVector VTKFigureFunction(int nargout, const ArrayVector& arg, Interpreter *)
{
throw Exception("vtkfigure function requries VTK support, which was not enabled at compile time");
}
#endif
|