ExaFMM 1
Fast-multipole Method for exascale systems
include/vtk.h
Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2011 by Rio Yokota, Simon Layton, Lorena Barba
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 #ifndef vtk_h
00023 #define vtk_h
00024 #define VTK_EXCLUDE_STRSTREAM_HEADERS
00025 #include <vtkPoints.h>
00026 #include <vtkPolyData.h>
00027 #include <vtkVertexGlyphFilter.h>
00028 #include <vtkPolyDataMapper.h>
00029 #include <vtkHexahedron.h>
00030 #include <vtkUnstructuredGrid.h>
00031 #include <vtkDataSetMapper.h>
00032 #include <vtkActor.h>
00033 #include <vtkProperty.h>
00034 #include <vtkRenderer.h>
00035 #include <vtkRenderWindow.h>
00036 #include <vtkRenderWindowInteractor.h>
00037 #include <vtkInteractorStyleTrackballCamera.h>
00038 #include <vtkSliderRepresentation2D.h>
00039 #include <vtkSliderWidget.h>
00040 #include <vtkWidgetEvent.h>
00041 #include <vtkWidgetEventTranslator.h>
00042 #include <vtkCallbackCommand.h>
00043 #include <vtkCommand.h>
00044 #include "types.h"
00045 const int maxGroups = 1000000;
00046 
00047 //! Interactive VTK class
00048 class vtkSliderCallback : public vtkCommand {
00049 public:
00050   vtkPoints *points[maxGroups];
00051   vtkPolyData *polydata;
00052   vtkVertexGlyphFilter *filter;
00053   vtkSliderCallback() {}
00054   static vtkSliderCallback *New() {
00055     return new vtkSliderCallback();
00056   }
00057   virtual void Execute(vtkObject *caller, unsigned long, void*) {
00058     vtkSliderWidget *widget = reinterpret_cast<vtkSliderWidget*>(caller);
00059     int value = static_cast<int>(static_cast<vtkSliderRepresentation *>(widget->GetRepresentation())->GetValue());
00060     polydata->SetPoints(points[value]);
00061     filter->SetInputConnection(polydata->GetProducerPort());
00062     filter->Update();
00063   }
00064 };
00065 
00066 //! Base VTK class
00067 class vtkPlot {
00068   int I[maxGroups];
00069   vtkPoints *points[maxGroups];
00070   vtkPoints *hexPoints;
00071 public:
00072   void setDomain(const real r0, const vect x0) {
00073     hexPoints = vtkPoints::New();
00074     hexPoints->SetNumberOfPoints(8);
00075     hexPoints->SetPoint(0, x0[0]-r0, x0[1]-r0, x0[2]-r0);
00076     hexPoints->SetPoint(1, x0[0]+r0, x0[1]-r0, x0[2]-r0);
00077     hexPoints->SetPoint(2, x0[0]+r0, x0[1]+r0, x0[2]-r0);
00078     hexPoints->SetPoint(3, x0[0]-r0, x0[1]+r0, x0[2]-r0);
00079     hexPoints->SetPoint(4, x0[0]-r0, x0[1]-r0, x0[2]+r0);
00080     hexPoints->SetPoint(5, x0[0]+r0, x0[1]-r0, x0[2]+r0);
00081     hexPoints->SetPoint(6, x0[0]+r0, x0[1]+r0, x0[2]+r0);
00082     hexPoints->SetPoint(7, x0[0]-r0, x0[1]+r0, x0[2]+r0);
00083   }
00084 
00085   void setGroup(const int Igroup, const int Npoints) {
00086     I[Igroup] = 0;
00087     points[Igroup] = vtkPoints::New();
00088     points[Igroup]->SetNumberOfPoints(Npoints);
00089   }
00090 
00091   void setPoints(const int Igroup, const vect X) {
00092     points[Igroup]->SetPoint(I[Igroup],X[0],X[1],X[2]);
00093     I[Igroup]++;
00094   }
00095 
00096   void setGroupOfPoints(Bodies &bodies, int &Ncell) {
00097     int begin=0, size=0;
00098     bigint index = bodies[0].ICELL;
00099     for( B_iter B=bodies.begin(); B!=bodies.end(); ++B ) {
00100       if( B->ICELL != index ) {
00101         setGroup(Ncell,size);
00102         for( int i=begin; i!=begin+size; ++i ) {
00103           setPoints(Ncell,bodies[i].X);
00104         }
00105         begin = B-bodies.begin();
00106         size = 0;
00107         index = B->ICELL;
00108         Ncell++;
00109         assert(Ncell < maxGroups);
00110       }
00111       size++;
00112     }
00113     setGroup(Ncell,size);
00114     for( int i=begin; i!=begin+size; ++i ) {
00115       setPoints(Ncell,bodies[i].X);
00116     }
00117     Ncell++;
00118     assert(Ncell < maxGroups);
00119   }
00120 
00121   void plot(const int Ngroup) {
00122     //Create a polygon object for points
00123     vtkPolyData *polydata = vtkPolyData::New();
00124     polydata->SetPoints(points[0]);
00125 
00126     //Create a filter object for points
00127     vtkVertexGlyphFilter *filter = vtkVertexGlyphFilter::New();
00128     filter->SetInputConnection(polydata->GetProducerPort());
00129     filter->Update();
00130 
00131     //Create a mapper object for points
00132     vtkPolyDataMapper *pointMapper = vtkPolyDataMapper::New();
00133     pointMapper->SetInputConnection(filter->GetOutputPort());
00134 
00135     //Associate the mapper to an actor object for points
00136     vtkActor *pointActor = vtkActor::New();
00137     pointActor->SetMapper(pointMapper);
00138     pointActor->GetProperty()->SetColor(1,0,0);
00139 
00140     //Create a hexahedron for cells
00141     vtkHexahedron *hex = vtkHexahedron::New();
00142     hex->GetPointIds()->SetId(0,0);
00143     hex->GetPointIds()->SetId(1,1);
00144     hex->GetPointIds()->SetId(2,2);
00145     hex->GetPointIds()->SetId(3,3);
00146     hex->GetPointIds()->SetId(4,4);
00147     hex->GetPointIds()->SetId(5,5);
00148     hex->GetPointIds()->SetId(6,6);
00149     hex->GetPointIds()->SetId(7,7);
00150 
00151     //Create a grid for cells
00152     vtkUnstructuredGrid *grid = vtkUnstructuredGrid::New();
00153     grid->Allocate(1,1);
00154     grid->InsertNextCell(hex->GetCellType(),hex->GetPointIds());
00155     grid->SetPoints(hexPoints);
00156 
00157     //Create a mapper object for cells
00158     vtkDataSetMapper *hexMapper = vtkDataSetMapper::New();
00159     hexMapper->SetInput(grid);
00160 
00161     //Associate the mapper to an actor object for cells
00162     vtkActor *hexActor = vtkActor::New();
00163     hexActor->SetMapper(hexMapper);
00164     hexActor->GetProperty()->SetOpacity(.1);
00165 
00166     //Add that actor to the renderer
00167     vtkRenderer *renderer = vtkRenderer::New();
00168     renderer->AddActor(pointActor);
00169     renderer->AddActor(hexActor);
00170     renderer->SetBackground(0,0,0);
00171 
00172     //Create a render window
00173     vtkRenderWindow *window = vtkRenderWindow::New();
00174     window->AddRenderer(renderer);
00175     window->SetSize(700,700);
00176 
00177     //Create an interactor and associate it to the render window
00178     vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::New();
00179     interactor->SetRenderWindow(window);
00180 
00181     //Create a slider representation
00182     vtkSliderRepresentation2D *representation = vtkSliderRepresentation2D::New();
00183     representation->SetMinimumValue(0);
00184     representation->SetMaximumValue(Ngroup-1);
00185     representation->GetPoint1Coordinate()->SetCoordinateSystemToDisplay();
00186     representation->GetPoint1Coordinate()->SetValue(50,50);
00187     representation->GetPoint2Coordinate()->SetCoordinateSystemToDisplay();
00188     representation->GetPoint2Coordinate()->SetValue(650,50);
00189 
00190     //Create a slider widget
00191     vtkSliderWidget *widget = vtkSliderWidget::New();
00192     widget->SetInteractor(interactor);
00193     widget->SetRepresentation(representation);
00194     widget->SetAnimationModeToAnimate();
00195     widget->EnabledOn();
00196 
00197     //Create a slider callback
00198     vtkSliderCallback *callback = vtkSliderCallback::New();
00199     for( int i=0; i!=Ngroup; ++i ) {
00200       callback->points[i] = points[i];
00201     }
00202     callback->polydata = polydata;
00203     callback->filter = filter;
00204     widget->AddObserver(vtkCommand::InteractionEvent,callback);
00205 
00206     //Define the interacting style
00207     vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New();
00208     interactor->SetInteractorStyle(style);
00209 
00210     //Start to interact
00211     interactor->Initialize();
00212     interactor->Start();
00213   }
00214 };
00215 
00216 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines