-
Notifications
You must be signed in to change notification settings - Fork 39
/
LidarTool.cpp
189 lines (158 loc) · 5.73 KB
/
LidarTool.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
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
/***********************************************************************
LidarTool - Vrui tool class to position a virtual input device at the
intersection of a ray and a LiDAR octree.
Copyright (c) 2008-2010 Oliver Kreylos
This file is part of the LiDAR processing and analysis package.
The LiDAR processing and analysis package 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 LiDAR processing and analysis package 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 LiDAR processing and analysis package; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
***********************************************************************/
#include "LidarTool.h"
#include <Cluster/MulticastPipe.h>
#include <Geometry/Ray.h>
#include <Geometry/OrthogonalTransformation.h>
#include <GL/gl.h>
#include <GL/GLGeometryWrappers.h>
#include <Vrui/InputDevice.h>
#include <Vrui/ToolManager.h>
#include <Vrui/InputGraphManager.h>
#include <Vrui/Vrui.h>
#include "LidarTypes.h"
#include "LidarOctree.h"
#include "LidarViewer.h"
/*********************************
Methods of class LidarToolFactory:
*********************************/
LidarToolFactory::LidarToolFactory(Vrui::ToolManager& toolManager)
:Vrui::ToolFactory("LidarTool",toolManager)
{
/* Insert class into class hierarchy: */
Vrui::TransformToolFactory* transformToolFactory=dynamic_cast<Vrui::TransformToolFactory*>(toolManager.loadClass("TransformTool"));
transformToolFactory->addChildClass(this);
addParentClass(transformToolFactory);
/* Initialize tool layout: */
layout.setNumButtons(0,true);
layout.setNumValuators(0,true);
/* Set the custom tool class' factory pointer: */
LidarTool::factory=this;
}
LidarToolFactory::~LidarToolFactory(void)
{
/* Reset the custom tool class' factory pointer: */
LidarTool::factory=0;
}
const char* LidarToolFactory::getName(void) const
{
return "Point Cloud Projector";
}
Vrui::Tool* LidarToolFactory::createTool(const Vrui::ToolInputAssignment& inputAssignment) const
{
/* Create a new object of the custom tool class: */
LidarTool* newTool=new LidarTool(this,inputAssignment);
return newTool;
}
void LidarToolFactory::destroyTool(Vrui::Tool* tool) const
{
/* Cast the tool pointer to the Lidar tool class (not really necessary): */
LidarTool* lidarTool=dynamic_cast<LidarTool*>(tool);
/* Destroy the tool: */
delete lidarTool;
}
/**********************************
Static elements of class LidarTool:
**********************************/
LidarToolFactory* LidarTool::factory=0;
/**************************
Methods of class LidarTool:
**************************/
LidarTool::LidarTool(const Vrui::ToolFactory* factory,const Vrui::ToolInputAssignment& inputAssignment)
:Vrui::TransformTool(factory,inputAssignment)
{
/* Set the source device: */
if(input.getNumButtonSlots()>0)
sourceDevice=getButtonDevice(0);
else
sourceDevice=getValuatorDevice(0);
}
void LidarTool::initialize(void)
{
/* Initialize the base tool: */
TransformTool::initialize();
/* Disable the transformed device's glyph: */
Vrui::getInputGraphManager()->getInputDeviceGlyph(transformedDevice).disable();
}
const Vrui::ToolFactory* LidarTool::getFactory(void) const
{
return factory;
}
void LidarTool::frame(void)
{
/* Calculate ray equation in navigation coordinates: */
LidarOctree::Ray deviceRay=sourceDevice->getRay();
LidarOctree::Ray modelRay=deviceRay;
modelRay.transform(Vrui::getInverseNavigationTransformation());
/* Intersect the ray with the LiDAR data set: */
Scalar rayParameter;
if(Vrui::isMaster())
{
/* Calculate the intersection: */
rayParameter=Scalar(-1);
LidarOctree::ConeIntersection cone(modelRay,Vrui::getRayPickCosine());
for(int i=0;i<application->numOctrees;++i)
{
application->octrees[i]->intersectCone(cone);
if(cone.isValid())
{
rayParameter=cone.getParameter();
cone.testLambda2=cone.testLambdaMin;
}
}
if(Vrui::getMainPipe()!=0)
{
/* Send the intersection to the slaves: */
Vrui::getMainPipe()->write<Scalar>(rayParameter);
// Vrui::getMainPipe()->flush();
}
}
else
{
/* Receive the intersection from the master: */
Vrui::getMainPipe()->read<Scalar>(rayParameter);
}
transformedDevice->setDeviceRay(sourceDevice->getDeviceRayDirection(),sourceDevice->getDeviceRayStart());
if(rayParameter>=Scalar(0))
{
/* Set the device position to the intersection point: */
Vrui::TrackerState ts(Vrui::getNavigationTransformation().transform(modelRay(rayParameter))-Vrui::Point::origin,sourceDevice->getOrientation());
transformedDevice->setTransformation(ts);
}
else
{
/* Move the device in the plane it currently inhabits: */
rayParameter=(deviceRay.getDirection()*Vector(transformedDevice->getPosition()-sourceDevice->getPosition()))/Geometry::sqr(deviceRay.getDirection());
Vrui::TrackerState ts(deviceRay(rayParameter)-Point::origin,sourceDevice->getOrientation());
transformedDevice->setTransformation(ts);
}
}
void LidarTool::display(GLContextData& contextData) const
{
/* Draw a line from the source device's position to the transformed device's position: */
glPushAttrib(GL_ENABLE_BIT|GL_LINE_BIT);
glDisable(GL_LIGHTING);
glLineWidth(1.0f);
glColor3f(0.0f,1.0f,0.0f);
glBegin(GL_LINES);
glVertex(sourceDevice->getPosition());
glVertex(transformedDevice->getPosition());
glEnd();
glPopAttrib();
}