forked from daviddoria/SelectCorrespondences2D3D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointSelectionStyle2D.cpp
124 lines (106 loc) · 4.17 KB
/
PointSelectionStyle2D.cpp
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
/*=========================================================================
*
* Copyright David Doria 2011 daviddoria@gmail.com
*
* 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.txt
*
* 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 "PointSelectionStyle2D.h"
#include <vtkAbstractPicker.h>
#include <vtkActor2D.h>
#include <vtkCaptionActor2D.h>
#include <vtkCoordinate.h>
#include <vtkFollower.h>
#include <vtkObjectFactory.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataMapper2D.h>
#include <vtkProperty.h>
#include <vtkProperty2D.h>
#include <vtkRendererCollection.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkTextProperty.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkVectorText.h>
#include <sstream>
vtkStandardNewMacro(PointSelectionStyle2D);
void PointSelectionStyle2D::OnLeftButtonDown()
{
//std::cout << "Picking pixel: " << this->Interactor->GetEventPosition()[0] << " " << this->Interactor->GetEventPosition()[1] << std::endl;
this->Interactor->GetPicker()->Pick(this->Interactor->GetEventPosition()[0],
this->Interactor->GetEventPosition()[1],
0, // always zero.
this->CurrentRenderer);
double picked[3];
this->Interactor->GetPicker()->GetPickPosition(picked);
//std::cout << "Picked point with coordinate: " << picked[0] << " " << picked[1] << " " << picked[2] << std::endl;
AddNumber(picked);
// Forward events
vtkInteractorStyleImage::OnLeftButtonDown();
}
void PointSelectionStyle2D::RemoveAllPoints()
{
for(unsigned int i = 0; i < Coordinates.size(); ++i)
{
this->CurrentRenderer->RemoveViewProp( Numbers[i]);
this->CurrentRenderer->RemoveViewProp( Points[i]);
}
Numbers.clear();
Points.clear();
Coordinates.clear();
}
void PointSelectionStyle2D::AddNumber(double p[3])
{
// Convert the current number to a string
std::stringstream ss;
ss << Coordinates.size();
Coord2D coord;
coord.x = p[0];
coord.y = p[1];
Coordinates.push_back(coord);
p[0] = static_cast<int>( p[0] + 0.5 );
p[1] = static_cast<int>( p[1] + 0.5 );
p[2] = 0;
std::cout << "Adding marker at " << p[0] << " " << p[1] << " " << p[2] << std::endl;
// Create the number
// Create the text
vtkSmartPointer<vtkCaptionActor2D> captionActor = vtkSmartPointer<vtkCaptionActor2D>::New();
captionActor->SetCaption( ss.str().c_str() );
captionActor->SetAttachmentPoint(p);
captionActor->BorderOff();
captionActor->GetCaptionTextProperty()->BoldOff();
captionActor->GetCaptionTextProperty()->ItalicOff();
captionActor->GetCaptionTextProperty()->ShadowOff();
captionActor->ThreeDimensionalLeaderOff();
this->Numbers.push_back(captionActor);
this->CurrentRenderer->AddViewProp( captionActor );
// Create the dot
// Create a sphere
vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetRadius(.5);
sphereSource->SetCenter(p);
sphereSource->Update();
// Create a mapper
vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection( sphereSource->GetOutputPort() );
// Create a subclass of vtkActor: a vtkFollower that remains facing the camera
vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();
sphereActor->SetMapper( sphereMapper );
sphereActor->GetProperty()->SetColor( 1, 0, 0 ); // red
this->Points.push_back(sphereActor);
//this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor( sphereActor );
this->CurrentRenderer->AddViewProp( sphereActor );
}