00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "vtkInteractorStyleCutter.h"
00019
00020 #include <vtkPoints.h>
00021 #include <vtkActor2D.h>
00022 #include <vtkObjectFactory.h>
00023 #include <vtkRenderer.h>
00024 #include <vtkRenderWindowInteractor.h>
00025 #include <vtkCellArray.h>
00026 #include <vtkPolyData.h>
00027 #include <vtkPolyDataMapper2D.h>
00028 #include <vtkProperty2D.h>
00029 #include <vtkCamera.h>
00030
00031 vtkCxxRevisionMacro(vtkInteractorStyleCutter, "$Revision: 1.1 $");
00032 vtkStandardNewMacro(vtkInteractorStyleCutter);
00033
00034
00035 vtkInteractorStyleCutter::vtkInteractorStyleCutter()
00036 {
00037 this->CurrentPosition[0] = this->CurrentPosition[1] = 0;
00038 this->Direction[0] = this->Direction[1] = this->Direction[2] = 0.;
00039 this->Moving = 0;
00040
00041 this->Points = vtkPoints::New();
00042 this->Lines = vtkCellArray::New();
00043 this->LoopPoints = vtkPoints::New();
00044
00045 vtkPolyData *pd = vtkPolyData::New();
00046 pd->SetPoints( Points );
00047 pd->SetLines( Lines );
00048
00049 vtkPolyDataMapper2D *bboxMapper = vtkPolyDataMapper2D::New();
00050 bboxMapper->SetInput( pd );
00051
00052 this->BboxActor = vtkActor2D::New();
00053 this->BboxActor->SetMapper( bboxMapper );
00054 this->BboxActor->GetProperty()->SetColor(1, 0, 0);
00055 this->BboxActor->VisibilityOff();
00056
00057
00058 pd->Delete();
00059 bboxMapper->Delete();
00060 }
00061
00062
00063 vtkInteractorStyleCutter::~vtkInteractorStyleCutter()
00064 {
00065 this->Points->Delete();
00066 this->BboxActor->Delete();
00067 this->Lines->Delete();
00068 this->LoopPoints->Delete();
00069 }
00070
00071
00072 void vtkInteractorStyleCutter::OnMouseMove()
00073 {
00074 if (!this->Interactor || !this->Moving)
00075 {
00076 return;
00077 }
00078
00079 this->CurrentPosition[0] = this->Interactor->GetEventPosition()[0];
00080 this->CurrentPosition[1] = this->Interactor->GetEventPosition()[1];
00081
00082
00083 this->Points->SetPoint(this->PointID, this->CurrentPosition[0],
00084 this->CurrentPosition[1], 0);
00085
00086 this->Interactor->Render();
00087 }
00088
00089
00090 void vtkInteractorStyleCutter::OnLeftButtonDown()
00091 {
00092 if (!this->Interactor)
00093 {
00094 return;
00095 }
00096
00097 this->CurrentPosition[0] = this->Interactor->GetEventPosition()[0];
00098 this->CurrentPosition[1] = this->Interactor->GetEventPosition()[1];
00099
00100 if(!this->Moving)
00101 {
00102 this->Initialize();
00103
00104
00105 this->FindPokedRenderer(this->CurrentPosition[0], this->CurrentPosition[1]);
00106 this->CurrentRenderer->AddProp( BboxActor );
00107 }
00108
00109 this->Moving = 1;
00110
00111 this->Points->SetPoint(this->PointID, this->CurrentPosition[0],
00112 this->CurrentPosition[1], 0);
00113 this->PointID = this->Points->InsertNextPoint( this->CurrentPosition[0],
00114 this->CurrentPosition[1], 0);
00115
00116 this->Lines->InsertCellPoint( this->PointID );
00117 this->Lines->UpdateCellCount( this->PointID + 1 );
00118 this->BboxActor->VisibilityOn();
00119
00120 this->Interactor->Render();
00121 }
00122
00123
00124 void vtkInteractorStyleCutter::OnRightButtonDown()
00125 {
00126 if (!this->Interactor || !this->Moving)
00127 {
00128 return;
00129 }
00130
00131 double xyz[3];
00132 this->Points->GetPoint( 0, xyz );
00133 this->Points->SetPoint(this->PointID, xyz);
00134
00135
00136 this->EndLoop();
00137
00138 this->Interactor->Render();
00139 this->Moving = 0;
00140 }
00141
00142
00143 void vtkInteractorStyleCutter::Initialize()
00144 {
00145 this->Points->Reset();
00146 this->Lines->Reset();
00147
00148 this->PointID = this->Points->InsertNextPoint( 0, 0, 0);
00149 this->Lines->InsertNextCell( 1 );
00150 this->Lines->InsertCellPoint( 0 );
00151 }
00152
00153 void vtkInteractorStyleCutter::EndLoop()
00154 {
00155 double pi[3],fpi[4];
00156 int numPts = this->Points->GetNumberOfPoints()-1;
00157 this->LoopPoints->SetNumberOfPoints( numPts );
00158 vtkCamera *camera = this->CurrentRenderer->GetActiveCamera();
00159 int state = camera->GetParallelProjection ();
00160 camera->ParallelProjectionOn();
00161
00162 for (int i=0; i < numPts; i++)
00163 {
00164 this->Points->GetPoint(i, pi);
00165 this->CurrentRenderer->SetDisplayPoint(pi[0], pi[1], 0);
00166 this->CurrentRenderer->DisplayToWorld();
00167
00168 this->CurrentRenderer->GetWorldPoint( fpi );
00169 if ( fpi[3] )
00170 {
00171 fpi[0] /= fpi[3];
00172 fpi[1] /= fpi[3];
00173 fpi[2] /= fpi[3];
00174 }
00175 this->LoopPoints->SetPoint( i, fpi[0], fpi[1], fpi[2] );
00176 }
00177
00178
00179 camera->GetDirectionOfProjection( this->Direction );
00180
00181 }
00182
00183
00184 void vtkInteractorStyleCutter::VisibilityOff()
00185 {
00186 this->BboxActor->VisibilityOff();
00187 }
00188
00189 void vtkInteractorStyleCutter::PrintSelf(ostream& os, vtkIndent indent)
00190 {
00191 this->Superclass::PrintSelf(os, indent);
00192 }