00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "vtkRenderer.h"
00016 #include "vtkRenderWindow.h"
00017 #include "vtkRenderer.h"
00018 #include "vtkRenderWindowInteractor.h"
00019 #include "vtkInteractorStyleTrackballCamera.h"
00020 #include "vtkBoxWidget.h"
00021 #include "vtkTransform.h"
00022 #include "vtkConeSource.h"
00023 #include "vtkPolyDataMapper.h"
00024 #include "vtkCommand.h"
00025 #include "vtkOpenGLActor.h"
00026 #include "vtkActor2D.h"
00027 #include "vtkMapper2D.h"
00028 #include "vtkOpenGLProperty.h"
00029 #include "vtkCallbackCommand.h"
00030 #include "vtkPolyData.h"
00031 #include "vtkTimerLog.h"
00032
00034
00035 #include "vtkVolumeRayCastCompositeFunction.h"
00036 #include "vtkVolumeRayCastMapper.h"
00037 #include "vtkVolume.h"
00038 #include "vtkVolumeProperty.h"
00039 #include "vtkColorTransferFunction.h"
00040 #include "vtkPiecewiseFunction.h"
00041 #include "vtkStructuredPointsReader.h"
00043
00044 #include "DemoConfig.h"
00045
00046 #include <string>
00047
00048
00049
00050 class vtkMyCallback : public vtkCommand
00051 {
00052 public:
00053 static vtkMyCallback *New() { return new vtkMyCallback; }
00054 void Execute(vtkObject *caller, unsigned long, void*)
00055 {
00056 if (!this->actor || !this->transform) return;
00057 vtkBoxWidget *boxWidget = reinterpret_cast<vtkBoxWidget*>(caller);
00058 boxWidget->GetTransform(this->transform);
00059 this->actor->SetUserTransform(this->transform);
00060 }
00061 vtkMyCallback(): transform(0), actor(0) {}
00062
00063 void SetActor(vtkActor * actor) { this->actor = actor; }
00064 void SetTransform(vtkTransform * transform) { this->transform = transform; }
00065
00066 private:
00067 vtkActor * actor;
00068 vtkTransform * transform;
00069 };
00070
00071 class glwxWidgetMapper : public vtkMapper2D
00072 {
00073 public:
00074 static glwxWidgetMapper *New() { return new glwxWidgetMapper; }
00075 void Render(vtkRenderer *ren, vtkActor *act) { }
00076 void SetGui( GuiWrapper * gui ) { this->gui = gui; }
00077
00078 int RenderGui() {
00079
00080 double time = timer->GetUniversalTime();
00081 gui->Update(time-oldTime);
00082 oldTime = time;
00083
00084 if (gui->IsActive()) {
00085 gui->RenderScene();
00086 return 1;
00087 }
00088 return 0;
00089 }
00090
00091 ~glwxWidgetMapper()
00092 {
00093 timer->Delete();
00094 }
00095
00096 protected:
00097 glwxWidgetMapper()
00098 {
00099 timer = vtkTimerLog::New();
00100 oldTime = timer->GetUniversalTime();
00101 }
00102 private:
00103 GuiWrapper * gui;
00104 vtkTimerLog * timer;
00105 double oldTime;
00106
00107 };
00108
00109 class glwxWidgetActor : public vtkActor2D
00110 {
00111 public:
00112 static glwxWidgetActor * New()
00113 {
00114 return new glwxWidgetActor;
00115 }
00116
00117 int RenderOverlay (vtkViewport * vp)
00118 {
00119 glwxWidgetMapper * widgetMapper = (glwxWidgetMapper*)this->GetMapper();
00120 return widgetMapper->RenderGui();
00121 }
00122
00123
00124
00125
00126
00127 };
00128
00129
00130
00131
00132 void MyCallback( vtkObject* vtkNotUsed(object),
00133 unsigned long eid,
00134 void* clientdata,
00135 void* vtkNotUsed(calldata) );
00136
00137
00138 DemoEventHandler handler;
00139 GuiWrapper gui(&handler);
00140
00141 vtkCallbackCommand * eventCallback;
00142
00143 vtkRenderer *renderer;
00144 vtkInteractorStyleTrackballCamera *style;
00145 vtkRenderWindow *renWin;
00146 vtkRenderWindowInteractor *iren;
00147
00148 vtkConeSource *coneSource;
00149 vtkPolyDataMapper *coneMapper;
00150 vtkActor *coneActor;
00151
00152 glwxWidgetMapper * widgetMapper;
00153 glwxWidgetActor * widgetActor;
00154
00155 vtkBoxWidget *boxWidget;
00156 vtkTransform * widgetTransform;
00157 vtkMyCallback *myCallback;
00158
00159
00160 int main( int argc, char *argv[] )
00161 {
00162
00163 std::string DataDir = DEMO_DATA_DIR;
00164 if (argc == 2) DataDir = argv[1];
00165
00166
00167 renWin = vtkRenderWindow::New();
00168 renderer = vtkRenderer::New();
00169 iren = vtkRenderWindowInteractor::New();
00170 style = vtkInteractorStyleTrackballCamera::New();
00171 eventCallback = vtkCallbackCommand::New();
00172 coneSource = vtkConeSource::New();
00173 coneMapper = vtkPolyDataMapper::New();
00174 coneActor = vtkActor::New();
00175 widgetMapper = glwxWidgetMapper::New();
00176 widgetActor = glwxWidgetActor::New();
00177 boxWidget = vtkBoxWidget::New();
00178 widgetTransform = vtkTransform::New();
00179 myCallback = vtkMyCallback::New();
00180
00181 boxWidget->SetInteractor( iren );
00182
00183
00184 eventCallback->SetClientData(style);
00185 eventCallback->SetCallback(MyCallback);
00186 style->AddObserver(vtkCommand::MouseMoveEvent, eventCallback);
00187 style->AddObserver(vtkCommand::LeftButtonPressEvent, eventCallback);
00188 style->AddObserver(vtkCommand::LeftButtonReleaseEvent, eventCallback);
00189 style->AddObserver(vtkCommand::CharEvent, eventCallback);
00190 style->AddObserver(vtkCommand::KeyPressEvent, eventCallback);
00191
00192
00193 renWin->AddRenderer(renderer);
00194 iren->SetInteractorStyle(style);
00195 iren->SetRenderWindow(renWin);
00196
00197
00198
00199 boxWidget->SetPlaceFactor( 0.8 );
00200 boxWidget->SetProp3D(coneActor);
00201 boxWidget->PlaceWidget();
00202 myCallback->SetTransform(widgetTransform);
00203 myCallback->SetActor(coneActor);
00204 boxWidget->AddObserver(vtkCommand::InteractionEvent, myCallback);
00205
00206
00207 coneSource->SetResolution(6);
00208 coneMapper->SetInputConnection(coneSource->GetOutputPort());
00209 coneActor->SetMapper(coneMapper);
00210
00211
00212 widgetMapper->SetInputConnection(NULL);
00213 widgetMapper->SetGui(&gui);
00214 widgetActor->SetMapper(widgetMapper);
00215
00216
00217 renderer->AddActor(coneActor);
00218 renderer->AddActor(widgetActor);
00219
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00253
00254
00255 renderer->SetBackground(handler.red, handler.green, handler.blue);
00256 renWin->SetSize(700,500);
00257 renWin->Render();
00258
00259
00260
00261 gui.Init(DataDir.c_str());
00262 gui.SetDimensions(700,500);
00263
00264
00265 iren->Render();
00266 iren->Start();
00267
00268
00269 renderer->Delete();
00270 renWin->Delete();
00271 iren->Delete();
00272 style->Delete();
00273 eventCallback->Delete();
00274 coneSource->Delete();
00275 coneMapper->Delete();
00276 coneActor->Delete();
00277 widgetActor->Delete();
00278 widgetMapper->Delete();
00279 boxWidget->Delete();
00280 widgetTransform->Delete();
00281 myCallback->Delete();
00282
00283 return 0;
00284 }
00285
00286
00287
00288
00289
00290 void MyCallback( vtkObject* vtkNotUsed(object),
00291 unsigned long eid,
00292 void* clientdata,
00293 void* vtkNotUsed(calldata) )
00294 {
00295 int x = iren->GetEventPosition()[0];
00296 int y = 500 - iren->GetEventPosition()[1];
00297
00298 switch (eid) {
00299 case vtkCommand::MouseMoveEvent:
00300 gui.MouseMoved(x, y);
00301 style->OnMouseMove();
00302 break;
00303 case vtkCommand::LeftButtonPressEvent:
00304
00305
00306 if ( !gui.MouseClicked(1, true, x, y) )
00307 style->OnLeftButtonDown();
00308 break;
00309 case vtkCommand::LeftButtonReleaseEvent:
00310
00311
00312 if ( !gui.MouseClicked(1, false, x, y) )
00313 style->OnLeftButtonUp();
00314 break;
00315 case vtkCommand::CharEvent:
00316
00317
00318 if ( !gui.AsciiKeyDown( iren->GetKeyCode() ))
00319 style->OnChar();
00320 break;
00321 }
00322 renderer->SetBackground(handler.red, handler.green, handler.blue);
00323
00324
00325
00326
00327
00328 iren->Render();
00329 }
00330
00331