-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelectAndDragTool.cpp
More file actions
201 lines (167 loc) · 7.57 KB
/
Copy pathSelectAndDragTool.cpp
File metadata and controls
201 lines (167 loc) · 7.57 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/***********************************************************************
SelectAndDragTool - Tool class to select single nodes from a network and
drag the currently selected nodes.
Copyright (c) 2018-2020 Oliver Kreylos
This file is part of the Network Viewer.
The Network Viewer is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
The Network Viewer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with the Network Viewer; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***********************************************************************/
#include "SelectAndDragTool.h"
#include <Geometry/OrthogonalTransformation.h>
#include <Geometry/PointPicker.h>
#include <Geometry/RayPicker.h>
#include <Vrui/Vrui.h>
#include <Vrui/ToolManager.h>
#include "Node.h"
#include "Network.h"
/*********************************************************
Static elements of class NetworkViewer::SelectAndDragTool:
*********************************************************/
NetworkViewer::SelectAndDragTool::Factory* NetworkViewer::SelectAndDragTool::factory=0;
/*************************************************
Methods of class NetworkViewer::SelectAndDragTool:
*************************************************/
void NetworkViewer::SelectAndDragTool::beginDrag(unsigned int pickedNodeIndex,const NetworkViewer::SelectAndDragTool::DragTransform& initialDragTransform)
{
Network* network=application->network;
const Network::NodeList& nodes=network->getNodes();
ParticleSystem& particles=application->particles;
/* Try locking the set of selected nodes: */
draggingSelection=application->lockSelection();
if(draggingSelection)
{
/* Check if the picked node is not currently selected: */
if(!network->isSelected(pickedNodeIndex))
{
/* Set the selection to the picked node: */
network->setSelection(pickedNodeIndex);
}
/* Begin dragging the set of selected nodes: */
const Network::Selection& selection=network->getSelection();
draggedParticles.reserve(selection.getNumEntries());
for(Network::Selection::ConstIterator sIt=selection.begin();!sIt.isFinished();++sIt)
{
DraggedParticle dp;
dp.particleIndex=nodes[sIt->getSource()].getParticleIndex();
/* Remember the particle's original mass and set it to infinity during dragging: */
dp.particleInvMass=application->particles.getParticleInvMass(dp.particleIndex);
particles.setParticleInvMass(dp.particleIndex,Scalar(0));
/* Calculate the particle's position in drag space: */
dp.dragPosition=initialDragTransform.inverseTransform(particles.getParticlePosition(dp.particleIndex));
draggedParticles.push_back(dp);
}
}
else if(!network->isSelected(pickedNodeIndex))
{
/* Begin dragging the picked node: */
DraggedParticle dp;
dp.particleIndex=nodes[pickedNodeIndex].getParticleIndex();
/* Remember the particle's original mass and set it to infinity during dragging: */
dp.particleInvMass=particles.getParticleInvMass(dp.particleIndex);
particles.setParticleInvMass(dp.particleIndex,Scalar(0));
/* Calculate the particle's position in drag space: */
dp.dragPosition=initialDragTransform.inverseTransform(particles.getParticlePosition(dp.particleIndex));
draggedParticles.push_back(dp);
}
}
void NetworkViewer::SelectAndDragTool::drag(const NetworkViewer::SelectAndDragTool::DragTransform& dragTransform)
{
/* Move all dragged particles: */
ParticleSystem& particles=application->particles;
for(std::vector<DraggedParticle>::iterator dpIt=draggedParticles.begin();dpIt!=draggedParticles.end();++dpIt)
particles.setParticlePosition(dpIt->particleIndex,dragTransform.transform(dpIt->dragPosition));
}
void NetworkViewer::SelectAndDragTool::endDrag(void)
{
/* Reset the mass of all dragged particles: */
ParticleSystem& particles=application->particles;
for(std::vector<DraggedParticle>::iterator dpIt=draggedParticles.begin();dpIt!=draggedParticles.end();++dpIt)
particles.setParticleInvMass(dpIt->particleIndex,dpIt->particleInvMass);
/* Clear the list of dragged particles: */
draggedParticles.clear();
/* Unlock the set of selected node if it was being dragged: */
if(draggingSelection)
application->unlockSelection();
draggingSelection=false;
}
void NetworkViewer::SelectAndDragTool::initClass(void)
{
/* Create a factory object for the custom tool class: */
factory=new Factory("SelectAndDragTool","Select & Drag Nodes",Tool::factory,*Vrui::getToolManager());
/* Set the tool class's input layout: */
factory->setNumButtons(1);
factory->setButtonFunction(0,"Select & Drag");
/* Register the tool class with Vrui's tool manager: */
Vrui::getToolManager()->addClass(factory,Vrui::ToolManager::defaultToolFactoryDestructor);
}
NetworkViewer::SelectAndDragTool::SelectAndDragTool(const Vrui::ToolFactory* factory,const Vrui::ToolInputAssignment& inputAssignment)
:Tool(factory,inputAssignment),
draggingSelection(false)
{
}
const Vrui::ToolFactory* NetworkViewer::SelectAndDragTool::getFactory(void) const
{
return factory;
}
void NetworkViewer::SelectAndDragTool::buttonCallback(int buttonSlotIndex,Vrui::InputDevice::ButtonCallbackData* cbData)
{
if(cbData->newButtonState) // Button has just been pressed
{
/* Pick a node: */
unsigned int pickedNodeIndex=pickNode(buttonSlotIndex);
if(pickedNodeIndex!=~0x0U)
{
/* Calculate the initial drag transformation for 6-DOF dragging: */
Vrui::NavTransform devTrans=Vrui::getInverseNavigationTransformation()*Vrui::NavTransform(getButtonDeviceTransformation(buttonSlotIndex));
DragTransform initialDragTransform(DragTransform::Vector(devTrans.getTranslation()),DragTransform::Rotation(devTrans.getRotation()));
/* Check whether the tool is ray-based: */
if(!getButtonDevice(buttonSlotIndex)->is6DOFDevice())
{
/* Adjust the initial drag transformation for ray-based dragging: */
initialDragTransform.leftMultiply(DragTransform::translate(pickRay(pickRayLambda)-initialDragTransform.getOrigin()));
}
/* Begin dragging nodes: */
beginDrag(pickedNodeIndex,initialDragTransform);
}
else if(application->lockSelection())
{
/* Clear the node selection: */
application->network->clearSelection();
application->unlockSelection();
}
}
else // Button has just been released
{
/* Stop dragging: */
endDrag();
}
}
void NetworkViewer::SelectAndDragTool::frame(void)
{
/* Check if the tool is dragging particles: */
if(!draggedParticles.empty())
{
/* Calculate the drag transformation: */
Vrui::NavTransform devTrans=Vrui::getInverseNavigationTransformation()*Vrui::NavTransform(getButtonDeviceTransformation(0));
DragTransform dragTransform(DragTransform::Vector(devTrans.getTranslation()),DragTransform::Rotation(devTrans.getRotation()));
/* Check whether the tool is ray-based: */
if(!getButtonDevice(0)->is6DOFDevice())
{
/* Offset the drag transformation along the tool's current selection ray: */
Vrui::Ray pickRay(getButtonDeviceRay(0));
pickRay.transform(Vrui::getInverseNavigationTransformation());
dragTransform.leftMultiply(DragTransform::translate(DragTransform::Point(pickRay(pickRayLambda))-dragTransform.getOrigin()));
}
/* Drag the dragged particles: */
drag(dragTransform);
}
}