forked from dgobbi/ToolCursor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtkTool.cxx
135 lines (112 loc) · 3.91 KB
/
vtkTool.cxx
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
/*=========================================================================
Program: ToolCursor
Module: vtkTool.cxx
Copyright (c) 2010 David Gobbi
All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkTool.h"
#include "vtkObjectFactory.h"
#include "vtkToolCursor.h"
#include "vtkRenderer.h"
#include "vtkCamera.h"
#include "vtkVolumePicker.h"
#include "vtkMath.h"
vtkStandardNewMacro(vtkTool);
//----------------------------------------------------------------------------
vtkTool::vtkTool()
{
this->ToolCursor = 0;
}
//----------------------------------------------------------------------------
vtkTool::~vtkTool()
{
// ToolCursor is not reference counted and therefore not deleted.
this->ToolCursor = 0;
}
//----------------------------------------------------------------------------
void vtkTool::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
//----------------------------------------------------------------------------
void vtkTool::SetToolCursor(vtkToolCursor *cursor)
{
if (cursor != this->ToolCursor)
{
this->ToolCursor = cursor;
this->Modified();
}
}
//----------------------------------------------------------------------------
void vtkTool::StartAction()
{
this->ToolCursor->GetPosition(this->StartPosition);
this->ToolCursor->GetDisplayPosition(this->StartDisplayPosition);
this->DisplayPosition[0] = this->StartDisplayPosition[0];
this->DisplayPosition[1] = this->StartDisplayPosition[1];
this->LastDisplayPosition[0] = this->DisplayPosition[0];
this->LastDisplayPosition[1] = this->DisplayPosition[1];
}
//----------------------------------------------------------------------------
void vtkTool::StopAction()
{
}
//----------------------------------------------------------------------------
void vtkTool::DoAction()
{
this->LastDisplayPosition[0] = this->DisplayPosition[0];
this->LastDisplayPosition[1] = this->DisplayPosition[1];
this->ToolCursor->GetDisplayPosition(this->DisplayPosition);
}
//----------------------------------------------------------------------------
void vtkTool::ConstrainCursor(double *, double *)
{
}
//----------------------------------------------------------------------------
void vtkTool::WorldToDisplay(const double world[3],
double &x, double &y, double &z)
{
vtkRenderer *renderer = this->ToolCursor->GetRenderer();
double hcoord[4];
hcoord[0] = world[0];
hcoord[1] = world[1];
hcoord[2] = world[2];
hcoord[3] = 1.0;
// Use the horrendous viewport interterface for conversions.
renderer->SetWorldPoint(hcoord);
renderer->WorldToDisplay();
renderer->GetDisplayPoint(hcoord);
x = hcoord[0];
y = hcoord[1];
z = hcoord[2];
}
//----------------------------------------------------------------------------
void vtkTool::DisplayToWorld(double x, double y, double z,
double world[3])
{
vtkRenderer *renderer = this->ToolCursor->GetRenderer();
double hcoord[4];
// Use the viewport interterface for conversions.
renderer->SetDisplayPoint(x, y, z);
renderer->DisplayToWorld();
renderer->GetWorldPoint(hcoord);
world[0] = hcoord[0]/hcoord[3];
world[1] = hcoord[1]/hcoord[3];
world[2] = hcoord[2]/hcoord[3];
}
//----------------------------------------------------------------------------
void vtkTool::GetViewRay(double x, double y, double z,
double p[3], double v[3])
{
double p1[3], p2[3];
this->DisplayToWorld(x, y, 0.0, p1);
this->DisplayToWorld(x, y, z, p);
this->DisplayToWorld(x, y, 1.0, p2);
v[0] = p2[0] - p1[0];
v[1] = p2[1] - p1[1];
v[2] = p2[2] - p1[2];
vtkMath::Normalize(v);
}