Commit ba23bd0a authored by Phillip Lino Rall's avatar Phillip Lino Rall
Browse files

added vtk reader files

parent 283dc91b
/**********************************************************************************
* Copyright 2010 Christoph Pflaum
* Department Informatik Lehrstuhl 10 - Systemsimulation
* Friedrich-Alexander Universität Erlangen-Nürnberg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**********************************************************************************/
#ifdef _VTK
#include <vtkOpenGLRenderer.h>
#include <vector>
#include <string>
#include "output.h"
#include <assert.h>
#include <QMutex>
using namespace std;
class vtkTIPWCallback : public vtkCommand
{
public:
static vtkTIPWCallback *New()
{ return new vtkTIPWCallback; }
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
vtkImplicitPlaneWidget *planeWidget =
reinterpret_cast<vtkImplicitPlaneWidget*>(caller);
planeWidget->GetPlane(this->Plane);
this->Actor->VisibilityOn();
this->normalActor->VisibilityOff();
}
vtkTIPWCallback():Plane(0),Actor(0) {}
vtkPlane *Plane;
vtkActor *Actor;
vtkActor *normalActor;
};
Output::~Output()
{}
OutputVTK::OutputVTK(vector<vector<double> > Points,
vector<std::vector<double> > Cells,
vector<double> Values, int Celltype, string Name)
: Output(Name)
{
path = "";
fromFile = false;
v_Points = Points;
v_Cells = Cells;
v_Values = Values;
celltype = Celltype;
max = v_Values[0];
min = v_Values[0];
maxPoint[0] = v_Points[0][0];
maxPoint[1] = v_Points[0][1];
maxPoint[2] = v_Points[0][2];
for (unsigned int j=1;j<v_Values.size();++j) {
if (v_Values[j]>max) {
max = v_Values[i];
maxPoint[0] = v_Points[j][0];
maxPoint[1] = v_Points[j][1];
maxPoint[2] = v_Points[j][2];
}
if (v_Values[j]<min) min = v_Values[j];
}
npts = v_Points.size();
////////// unstructured grid objectOutputVTK::getBounds( double[6] bounds)
dataSet = vtkUnstructuredGrid::New();
newPts = vtkPoints::New();
newScalars = vtkFloatArray::New();
for (i=0; i<npts; i++) {
xyz[0] = v_Points[i][0]; xyz[1] = v_Points[i][1]; xyz[2] = v_Points[i][2];
newPts->InsertPoint(i, xyz);
newScalars->InsertValue(i, v_Values[i]);
}
dataSet->SetPoints(newPts);
dataSet->GetPointData()->SetScalars(newScalars);
for(unsigned int j=0; j< v_Cells.size();++j) {
//int* cellcoord = new int[8];
vtkIdType cellcoord[8];
for (int l=0;l<8;++l)cellcoord[l] = v_Cells[j][l];
dataSet->InsertNextCell(celltype,vtkIdType(8),cellcoord);
}
Init();
newScalars->Delete();
newPts->Delete();
}
OutputVTK::OutputVTK(const char* vtkFilepath, string Name)
: Output(Name),
renderer(0),
renWin(0),
iren(0)
#ifdef _WIN32
,m_threadHandle(0)
#endif // _WIN32
{
fromFile = true;
string str(vtkFilepath);
path = str;
newPts = NULL;
newScalars = NULL;
reader = vtkUnstructuredGridReader::New();
reader->SetFileName(vtkFilepath);
reader->Update();
dataSet = reader->GetOutput();
npts = dataSet->GetNumberOfPoints();
double tmp[2];
dataSet->GetScalarRange(tmp);
min = tmp[0];
max = tmp[1];
Init();
}
string OutputVTK::getPath()
{
return path;
}
double OutputVTK::getValue(double x, double y, double z)
{
double scalar = implicitDataSet->EvaluateFunction(x,y,z);
return scalar;
}
void OutputVTK::display()
{
#ifndef _WIN32
pthread_t thread1;
pthread_create( &thread1, NULL, t_displayHelper, this );
pthread_join( thread1, NULL );
#else
//HANDLE hThread;
DWORD dwThreadId;
m_threadHandle = CreateThread(
NULL, // default security attributes
0, // use default stack size
t_displayHelper, // thread function
this, // argument to thread function
0, // use default creation flags
&dwThreadId // returns the thread identifier
);
#endif // _WIN32
}
#ifndef _WIN32
void* OutputVTK::t_displayHelper(void* This)
{
return ((OutputVTK*)This)->t_display();
}
#else
DWORD WINAPI OutputVTK::t_displayHelper( LPVOID lpParam )
{
return (DWORD)((OutputVTK*)lpParam)->t_display();
}
#endif // _WIN32
void* OutputVTK::t_display()
{
m_mutex.lock();
/////////renderer and window
renderer = vtkOpenGLRenderer::New();
renWin = vtkRenderWindow::New();
iren = vtkRenderWindowInteractor::New();
//////normal plot
mapper->SetInput(dataSet);
mapper->SetLookupTable(lut);
mapper->SetColorModeToMapScalars();
mapper->UseLookupTableScalarRangeOn();
mapper->ScalarVisibilityOn();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(color);
actor->SetOrigin(dataSet->GetCenter());
actor->GetProperty()->SetInterpolationToPhong();
//actor->GetProperty()->FrontfaceCullingOn();
plane->SetInput(dataSet);
plane->SetClipFunction(implicitFunction);
plane->InsideOutOn();
selectMapper->SetInputConnection(plane->GetOutputPort());
selectMapper->SetLookupTable(lut);
selectMapper->SetColorModeToMapScalars();
selectMapper->UseLookupTableScalarRangeOn();
selectMapper->ScalarVisibilityOn();
selectActor->SetMapper(selectMapper);
selectActor->GetProperty()->SetColor(0,1,0);
selectActor->VisibilityOff();
selectActor->GetProperty()->SetInterpolationToPhong();
vtkTIPWCallback* myCallback = vtkTIPWCallback::New();
myCallback->Plane = implicitFunction;
myCallback->Actor = selectActor;
myCallback->normalActor = actor;
planeWidget->SetInteractor(iren);
planeWidget->SetPlaceFactor(1.25);
planeWidget->ScaleEnabledOff();
planeWidget->OutlineTranslationOff();
planeWidget->TubingOff();
planeWidget->DrawPlaneOff();
planeWidget->SetInput(dataSet);
planeWidget->PlaceWidget(dataSet->GetBounds());
planeWidget->AddObserver(vtkCommand::InteractionEvent,myCallback);
//////scalar bar
scalarbar->SetLookupTable(lut);
scalarbarWidget->SetScalarBarActor(scalarbar);
scalarbarWidget->SetPriority(1.0);
//scalarbarWidget->SetKeyPressActivation(1);
//scalarbarWidget->SetKeyPressActivationValue('s');
//scalarbarWidget->SetInteractor(iren);
scalarbarWidget->SetDefaultRenderer(renderer);
///////legend
legend->GetTextProperty()->SetColor(0.0,0.0,0.0);
legend->SetText(0,"Keys:\n"
"- I toggle cutting plane\n"
//"- S toggle value scale\n"
"- R reset zoom");
legend->SetText(2,name.c_str());
///////axes
axes->SetInput(dataSet);
axes->SetCamera(renderer->GetActiveCamera());
///////outline
outline->SetInput(dataSet);
mapperOutline->SetInput(outline->GetOutput());
actorOutline->SetMapper(mapperOutline);
actorOutline->GetProperty()->SetColor(0,0,0);
actorOutline->SetOrigin(dataSet->GetCenter());
/////////renderer and window
renWin->AddRenderer(renderer);
iren->SetRenderWindow(renWin);
//renderer->GetActiveCamera()->SetFocalPoint(dataSet->GetCenter());
//renderer->GetActiveCamera()->Zoom(0.5);
renderer->AddActor(axes);
renderer->AddActor(legend);
renderer->AddActor(scalarbar);
renderer->AddActor(actor);
renderer->AddActor(actorOutline);
renderer->AddActor(selectActor);
renderer->SetBackground(0.9,0.9,0.9);
renWin->SetSize(800,600);
iren->Initialize();
renWin->LineSmoothingOn();
renWin->PointSmoothingOn();
renWin->PolygonSmoothingOn();
renWin->Start();
renWin->Render();
renWin->SetWindowName("DMA - 3D Preview");
renderer->ResetCamera();
m_mutex.unlock();
iren->Start();
m_mutex.lock();
renWin->Finalize();
iren->Delete();
iren=0;
renWin->Delete();
renWin=0;
renderer->Delete();
m_mutex.unlock();
implicitFunction->Delete();
plane->Delete();
planeWidget->Delete();
selectMapper->Delete();
selectActor->Delete();
actorOutline->Delete();
mapperOutline->Delete();
outline->Delete();
axes->Delete();
legend->Delete();
scalarbar->Delete();
scalarbarWidget->Delete();
actor->Delete();
mapper->Delete();
lut->Delete();
if (fromFile)
reader->Delete();
else
dataSet->Delete();
#ifdef _WIN32
ExitThread(0);
#endif // _WIN32
return 0;
}
OutputVTK::~OutputVTK()
{
// FIXME: The whole thread handling is COMPLETELY BOGUS!
// FIXME: This will delete the object on non-windows while
// the thread is still running
#ifdef _WIN32
m_mutex.lock();
vtkRenderWindow * tmpRenWin = renWin;
m_mutex.unlock();
if(tmpRenWin) {
HWND winHdl = (HWND) renWin->GetGenericWindowId();
PostMessage(winHdl,WM_CLOSE,0,0);
WaitForSingleObject(m_threadHandle,INFINITE);
}
#endif // _WIN32
}
void OutputVTK::Init() {
implicitDataSet = vtkImplicitDataSet::New();
implicitDataSet->SetDataSet(dataSet);
implicitDataSet->SetOutValue(0.);
implicitDataSet->SetOutGradient(0.,0.,0.);
////////////// lookup table
int colors = npts;
double step;
step = 1.0/colors*4.0;
lut = vtkLookupTable::New();
lut->SetTableRange(min, max);
lut->SetRange(min,max);
lut->SetScaleToLinear();
lut->SetNumberOfTableValues(colors);
lut->SetNumberOfColors(colors);
for (int j=0; j<colors/4.0;++j)
{
lut->SetTableValue(j,0.0,0.0+(j*step),1.0,1.0);
}
for (int j=0; j<colors/4.0;++j)
{
lut->SetTableValue(j+(colors/4.0),0.0,1.0,1.0-(j*step),1.0);
}
for (int j=0; j<colors/4.0;++j)
{
lut->SetTableValue(j+(colors/4.0)*2,0.0+(j*step),1.0,0.0,1.0);
}
for (int j=0; j<colors/4.0;++j)
{
lut->SetTableValue(j+(colors/4.0)*3,1.0,1.0-(j*step),0.0,1.0);
}
lut->GetColor(min,color);
///////normal plot
mapper = vtkDataSetMapper::New();
actor = vtkActor::New();
/////scalar bar
scalarbar = vtkScalarBarActor::New();
//// scalar bar widget
scalarbarWidget = vtkScalarBarWidget::New();
//// legend box
legend = vtkCornerAnnotation::New();
//// axes
axes = vtkCubeAxesActor2D::New();
////////outline
outline = vtkOutlineFilter::New();
mapperOutline = vtkPolyDataMapper::New();
actorOutline = vtkActor::New();
///////plane widget
planeWidget = vtkImplicitPlaneWidget::New();
///////cut to plane
plane = vtkClipDataSet::New();
implicitFunction = vtkPlane::New();
selectMapper = vtkDataSetMapper::New();
selectActor = vtkLODActor::New();
}
#endif // _VTK
/**********************************************************************************
* Copyright 2010 Christoph Pflaum
* Department Informatik Lehrstuhl 10 - Systemsimulation
* Friedrich-Alexander Universität Erlangen-Nürnberg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**********************************************************************************/
#ifndef OUTPUT_H
#define OUTPUT_H
#ifdef _VTK
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <QtCore>
#include <QApplication>
#ifndef _WIN32
#include <pthread.h>
#else
#include <windows.h>
#include <strsafe.h>
#endif // _WIN32
#include <vtkActor.h>
#include <vtkCubeAxesActor2D.h>
#include <vtkContourFilter.h>
#include <vtkDataSet.h>
#include <vtkFloatArray.h>
#include <vtkImageData.h>
#include <vtkGaussianSplatter.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyDataMapper.h>
#include <vtkDataSetMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTubeFilter.h>
#include <vtkUnstructuredGrid.h>
#include <vtkLookupTable.h>
#include <vtkOutlineFilter.h>
#include <vtkImplicitPlaneWidget.h>
#include <vtk3DWidget.h>
#include <vtkLODActor.h>
#include <vtkInteractorEventRecorder.h>
#include <vtkDataSetToPolyDataFilter.h>
#include <vtkCommand.h>
#include <vtkClipDataSet.h>
#include <vtkPlane.h>
#include <vtkCallbackCommand.h>
#include <vtkScalarBarActor.h>
#include <vtkImplicitDataSet.h>
#include <vtkUnstructuredGridReader.h>
#include <vtkScalarBarWidget.h>
#include <vtkCornerAnnotation.h>
#include <vtkTextProperty.h>
#include <vtkCamera.h>
/***
@TODO Output Klasse und viele Gui-Klassen weg
**/
class Output
{
public:
std::string getName() {return name;}
virtual void display() = 0;
virtual ~Output();
protected:
Output(std::string name_) : name(name_) {};
Output() {};
std::string name;
};
class OutputVTK : public Output
{
public:
OutputVTK(std::vector<std::vector<double> > Points,
std::vector<std::vector<double> > Cells,
std::vector<double> Values,
int Celltype,
std::string Name);
OutputVTK(const char* vtkFilepath, std::string Name);
virtual ~OutputVTK();
double getValue(double x,double y, double z);
void display();
std::string getPath();
private:
void Init();
void* t_display();
#ifndef _WIN32
static void* t_displayHelper(void* This);
// FIXME: need some thread handle for cleanup (doh!)
#else
static DWORD WINAPI t_displayHelper( LPVOID lpParam );
HANDLE m_threadHandle;
#endif // _WIN32
std::vector<std::vector<double> > v_Points;
std::vector<std::vector<double> > v_Cells;
std::vector<double> v_Values;
int celltype;
std::string path;
double min,max;
float xyz[3];
double maxPoint[3];
double color[3];
int i, npts;
bool fromFile;
vtkImplicitDataSet *implicitDataSet;
////dataset
vtkUnstructuredGrid *dataSet;
vtkPoints *newPts;
vtkFloatArray *newScalars;
vtkLookupTable *lut;
////normal plot
vtkDataSetMapper *mapper;
vtkActor *actor;
////scalar bar
vtkScalarBarActor *scalarbar;
vtkScalarBarWidget * scalarbarWidget;
/////legend
vtkCornerAnnotation *legend;
/////axes
vtkCubeAxesActor2D *axes;
//////outline
vtkOutlineFilter *outline;
vtkPolyDataMapper *mapperOutline;
vtkActor *actorOutline;
//////plane widget
vtkImplicitPlaneWidget *planeWidget;
/////cut to plane
vtkClipDataSet *plane;
vtkPlane *implicitFunction;
vtkLODActor *selectActor;
vtkDataSetMapper *selectMapper;
vtkUnstructuredGridReader* reader;
/////window
vtkRenderer *renderer;
vtkRenderWindow *renWin;
vtkRenderWindowInteractor *iren;
QMutex m_mutex;
};
#endif // _VTK
#endif /* OUTPUT_H */
/**********************************************************************************
* Copyright 2010 Christoph Pflaum
* Department Informatik Lehrstuhl 10 - Systemsimulation
* Friedrich-Alexander Universität Erlangen-Nürnberg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**********************************************************************************/
#include "../mympi.h"
#include "../abbrevi.h"
#include "../parameter.h"
#include "../math_lib/math_lib.h"
#include "../basics/basic.h"
#include "elements.h"
#include "parti.h"
#include "ug.h"
#include "examples_ug.h"
#include "vtkReader.h"
VTK_Reader::VTK_Reader(QString DateiName) {
//Phillips job!!
////////////////////////////////////
TypeOfUG typ;
QString path = QString("/home/rzlin/er96apow/UGBLOCKS/trunk/UGBlocks_V2_1/program/");
QFile inputFile(path+DateiName);
int stringElement;
int degrees;
int numberConstructParameters;