-
Notifications
You must be signed in to change notification settings - Fork 23
/
PlaneTool.cpp
417 lines (379 loc) · 14 KB
/
PlaneTool.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/***********************************************************************
PlaneTool - Calibration tool for RawKinectViewer.
Copyright (c) 2012-2015 Oliver Kreylos
This file is part of the Kinect 3D Video Capture Project (Kinect).
The Kinect 3D Video Capture Project 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 Kinect 3D Video Capture Project 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 Kinect 3D Video Capture Project; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
***********************************************************************/
#include "PlaneTool.h"
#include <iostream>
#include <Math/Math.h>
#include <Geometry/Point.h>
#include <Geometry/Vector.h>
#include <Geometry/Box.h>
#include <Geometry/ProjectiveTransformation.h>
#include <Geometry/PCACalculator.h>
#include <Geometry/OutputOperators.h>
#include <GL/gl.h>
#include <GL/GLTransformationWrappers.h>
#include <Vrui/Vrui.h>
#include <Vrui/ToolManager.h>
#include <Vrui/DisplayState.h>
#include <Kinect/Camera.h>
#include "RawKinectViewer.h"
/**********************************
Static elements of class PlaneTool:
**********************************/
PlaneToolFactory* PlaneTool::factory=0;
/**************************
Methods of class PlaneTool:
**************************/
PlaneToolFactory* PlaneTool::initClass(Vrui::ToolManager& toolManager)
{
/* Create the tool factory: */
factory=new PlaneToolFactory("PlaneTool","Extract Planes",0,toolManager);
/* Set up the tool class' input layout: */
factory->setNumButtons(1);
factory->setButtonFunction(0,"Draw Rectangle");
/* Register and return the class: */
toolManager.addClass(factory,Vrui::ToolManager::defaultToolFactoryDestructor);
return factory;
}
PlaneTool::PlaneTool(const Vrui::ToolFactory* factory,const Vrui::ToolInputAssignment& inputAssignment)
:Vrui::Tool(factory,inputAssignment),
dragging(false)
{
}
PlaneTool::~PlaneTool(void)
{
}
const Vrui::ToolFactory* PlaneTool::getFactory(void) const
{
return factory;
}
void PlaneTool::buttonCallback(int buttonSlotIndex,Vrui::InputDevice::ButtonCallbackData* cbData)
{
if(cbData->newButtonState)
{
/* Get the initial rectangle point and start dragging: */
p0=Point(application->calcImagePoint(getButtonDeviceRay(0)).getComponents());
dragging=true;
}
else
{
/* Find the bounding box of the selected rectangle in distortion-corrected depth image space: */
Geometry::Box<double,2> rect=Geometry::Box<double,2>::empty;
Geometry::Box<double,2> imgRect=Geometry::Box<double,2>::empty;
if(application->intrinsicParameters.depthLensDistortion.isIdentity())
{
/* No lens distortion correction needed; build rectangle from corner points: */
rect.addPoint(Point(application->calcDepthImagePoint(p0).getComponents()));
rect.addPoint(Point(application->calcDepthImagePoint(p1).getComponents()));
}
else
{
/* Get the selected rectangle in image space: */
imgRect.addPoint(p0);
imgRect.addPoint(p1);
/* Calculate the lens distortion-corrected extents of the selected rectangle: */
Point::Vector vx=imgRect.max-imgRect.min;
vx[1]=0.0;
for(int x=0;x<64;++x)
{
/* A point along the bottom edge: */
Point b0=imgRect.min+vx*(double(x)/64.0);
rect.addPoint(Point(application->calcDepthImagePoint(b0).getComponents()));
/* A point along the top edge: */
Point b1=imgRect.max-vx*(double(x)/64.0);
rect.addPoint(Point(application->calcDepthImagePoint(b1).getComponents()));
}
Point::Vector vy=imgRect.max-imgRect.min;
vy[0]=0.0;
for(int y=0;y<64;++y)
{
/* A point along the left edge: */
Point b0=imgRect.min+vy*(double(y+1)/64.0);
rect.addPoint(Point(application->calcDepthImagePoint(b0).getComponents()));
/* A point along the right edge: */
Point b1=imgRect.max-vy*(double(y+1)/64.0);
rect.addPoint(Point(application->calcDepthImagePoint(b1).getComponents()));
}
}
/* Calculate the rectangle's pixel boundaries: */
int min[2],max[2];
for(int i=0;i<2;++i)
{
min[i]=Math::floor(rect.min[i]);
if(min[i]<0)
min[i]=0;
max[i]=Math::floor(rect.max[i])+1;
if(max[i]>int(application->depthFrameSize[i]))
max[i]=int(application->depthFrameSize[i]);
}
/* Calculate the selected pixels' plane equation in depth image space: */
typedef Geometry::PCACalculator<3>::Point PPoint;
typedef Geometry::PCACalculator<3>::Vector PVector;
Geometry::PCACalculator<3> pca;
const float* afdRow=application->averageFrameDepth+min[1]*application->depthFrameSize[0];
const float* affRow=application->averageFrameForeground+min[1]*application->depthFrameSize[0];
float foregroundCutoff=float(application->averageNumFrames)*0.5f;
if(application->intrinsicParameters.depthLensDistortion.isIdentity())
{
/* No lens distortion correction required: */
if(application->depthCorrection!=0)
{
const RawKinectViewer::PixelCorrection* dcRow=application->depthCorrection+min[1]*application->depthFrameSize[0];
for(int y=min[1];y<max[1];++y,afdRow+=application->depthFrameSize[0],affRow+=application->depthFrameSize[0],dcRow+=application->depthFrameSize[0])
{
double dy=double(y)+0.5;
const float* afdPtr=afdRow+min[0];
const float* affPtr=affRow+min[0];
const RawKinectViewer::PixelCorrection* dcPtr=dcRow+min[0];
for(int x=min[0];x<max[0];++x,++afdPtr,++affPtr,++dcPtr)
{
double dx=double(x)+0.5;
if(*affPtr>=foregroundCutoff)
pca.accumulatePoint(PPoint(dx,dy,dcPtr->correct((*afdPtr)/(*affPtr))));
}
}
}
else
{
for(int y=min[1];y<max[1];++y,afdRow+=application->depthFrameSize[0],affRow+=application->depthFrameSize[0])
{
double dy=double(y)+0.5;
const float* afdPtr=afdRow+min[0];
const float* affPtr=affRow+min[0];
for(int x=min[0];x<max[0];++x,++afdPtr,++affPtr)
{
double dx=double(x)+0.5;
if(*affPtr>=foregroundCutoff)
pca.accumulatePoint(PPoint(dx,dy,(*afdPtr)/(*affPtr)));
}
}
}
}
else
{
/* Account for lens distortion correction by checking every pixel against the selected rectangle: */
if(application->depthCorrection!=0)
{
const RawKinectViewer::PixelCorrection* dcRow=application->depthCorrection+min[1]*application->depthFrameSize[0];
for(int y=min[1];y<max[1];++y,afdRow+=application->depthFrameSize[0],affRow+=application->depthFrameSize[0],dcRow+=application->depthFrameSize[0])
{
double dy=double(y)+0.5;
const float* afdPtr=afdRow+min[0];
const float* affPtr=affRow+min[0];
const RawKinectViewer::PixelCorrection* dcPtr=dcRow+min[0];
for(int x=min[0];x<max[0];++x,++afdPtr,++affPtr,++dcPtr)
{
double dx=double(x)+0.5;
if(*affPtr>=foregroundCutoff)
{
/* Check if the pixel is inside the selected rectangle: */
if(imgRect.contains(Point(application->getDepthImagePoint(x,y).getComponents())))
pca.accumulatePoint(PPoint(dx,dy,dcPtr->correct((*afdPtr)/(*affPtr))));
}
}
}
}
else
{
for(int y=min[1];y<max[1];++y,afdRow+=application->depthFrameSize[0],affRow+=application->depthFrameSize[0])
{
double dy=double(y)+0.5;
const float* afdPtr=afdRow+min[0];
const float* affPtr=affRow+min[0];
for(int x=min[0];x<max[0];++x,++afdPtr,++affPtr)
{
double dx=double(x)+0.5;
if(*affPtr>=foregroundCutoff)
{
/* Check if the pixel is inside the selected rectangle: */
if(imgRect.contains(Point(application->getDepthImagePoint(x,y).getComponents())))
pca.accumulatePoint(PPoint(dx,dy,(*afdPtr)/(*affPtr)));
}
}
}
}
}
/* Calculate the selected pixels' plane equation: */
PPoint centroid=pca.calcCentroid();
pca.calcCovariance();
double evs[3];
pca.calcEigenvalues(evs);
PVector normal=pca.calcEigenvector(evs[2]);
/* Check for any nans or infs: */
bool allFinite=true;
for(int i=0;i<3;++i)
{
allFinite=allFinite&&Math::isFinite(normal[i]);
allFinite=allFinite&&Math::isFinite(centroid[i]);
}
if(allFinite)
{
/* Print the approximation residual: */
std::cout<<"Approximation residual: "<<evs[2]<<std::endl;
/* Print the plane equation in depth image space: */
std::cout<<"Depth-space plane equation: x * "<<normal<<" = "<<centroid*normal<<std::endl;
/* Get the camera's intrinsic parameters: */
Kinect::FrameSource::IntrinsicParameters ips=application->camera->getIntrinsicParameters();
/* Transform the plane equation to camera space: */
PVector v0=Geometry::normal(normal);
PVector v1=normal^v0;
PPoint p0=centroid+v0;
PPoint p1=centroid+v1;
PPoint cCentroid=ips.depthProjection.transform(centroid);
PPoint cP0=ips.depthProjection.transform(p0);
PPoint cP1=ips.depthProjection.transform(p1);
PVector cNormal=(cP0-cCentroid)^(cP1-cCentroid);
cNormal.normalize();
double rms2=0.0;
unsigned int numPoints=0;
{
const float* afdRow=application->averageFrameDepth+min[1]*application->depthFrameSize[0];
const float* affRow=application->averageFrameForeground+min[1]*application->depthFrameSize[0];
float foregroundCutoff=float(application->averageNumFrames)*0.5f;
if(application->intrinsicParameters.depthLensDistortion.isIdentity())
{
/* No lens distortion correction required: */
if(application->depthCorrection!=0)
{
const RawKinectViewer::PixelCorrection* dcRow=application->depthCorrection+min[1]*application->depthFrameSize[0];
for(int y=min[1];y<max[1];++y,afdRow+=application->depthFrameSize[0],affRow+=application->depthFrameSize[0],dcRow+=application->depthFrameSize[0])
{
double dy=double(y)+0.5;
const float* afdPtr=afdRow+min[0];
const float* affPtr=affRow+min[0];
const RawKinectViewer::PixelCorrection* dcPtr=dcRow+min[0];
for(int x=min[0];x<max[0];++x,++afdPtr,++affPtr,++dcPtr)
{
double dx=double(x)+0.5;
if(*affPtr>=foregroundCutoff)
{
/* Check if the pixel is inside the selected rectangle: */
if(imgRect.contains(Point(application->getDepthImagePoint(x,y).getComponents())))
{
rms2+=Math::sqr((ips.depthProjection.transform(PPoint(dx,dy,dcPtr->correct((*afdPtr)/(*affPtr))))-cCentroid)*cNormal);
++numPoints;
}
}
}
}
}
else
{
for(int y=min[1];y<max[1];++y,afdRow+=application->depthFrameSize[0],affRow+=application->depthFrameSize[0])
{
double dy=double(y)+0.5;
const float* afdPtr=afdRow+min[0];
const float* affPtr=affRow+min[0];
for(int x=min[0];x<max[0];++x,++afdPtr,++affPtr)
{
double dx=double(x)+0.5;
if(*affPtr>=foregroundCutoff)
{
/* Check if the pixel is inside the selected rectangle: */
if(imgRect.contains(Point(application->getDepthImagePoint(x,y).getComponents())))
{
rms2+=Math::sqr((ips.depthProjection.transform(PPoint(dx,dy,(*afdPtr)/(*affPtr)))-cCentroid)*cNormal);
++numPoints;
}
}
}
}
}
}
else
{
/* Account for lens distortion correction by checking every pixel against the selected rectangle: */
if(application->depthCorrection!=0)
{
const RawKinectViewer::PixelCorrection* dcRow=application->depthCorrection+min[1]*application->depthFrameSize[0];
for(int y=min[1];y<max[1];++y,afdRow+=application->depthFrameSize[0],affRow+=application->depthFrameSize[0],dcRow+=application->depthFrameSize[0])
{
double dy=double(y)+0.5;
const float* afdPtr=afdRow+min[0];
const float* affPtr=affRow+min[0];
const RawKinectViewer::PixelCorrection* dcPtr=dcRow+min[0];
for(int x=min[0];x<max[0];++x,++afdPtr,++affPtr,++dcPtr)
{
double dx=double(x)+0.5;
if(*affPtr>=foregroundCutoff)
{
rms2+=Math::sqr((ips.depthProjection.transform(PPoint(dx,dy,dcPtr->correct((*afdPtr)/(*affPtr))))-cCentroid)*cNormal);
++numPoints;
}
}
}
}
else
{
for(int y=min[1];y<max[1];++y,afdRow+=application->depthFrameSize[0],affRow+=application->depthFrameSize[0])
{
double dy=double(y)+0.5;
const float* afdPtr=afdRow+min[0];
const float* affPtr=affRow+min[0];
for(int x=min[0];x<max[0];++x,++afdPtr,++affPtr)
{
double dx=double(x)+0.5;
if(*affPtr>=foregroundCutoff)
{
rms2+=Math::sqr((ips.depthProjection.transform(PPoint(dx,dy,(*afdPtr)/(*affPtr)))-cCentroid)*cNormal);
++numPoints;
}
}
}
}
}
}
std::cout<<"Camera-space approximation RMS: "<<Math::sqrt(rms2/double(numPoints))<<std::endl;
/* Print the plane equation in camera space: */
std::cout<<"Camera-space plane equation: x * "<<cNormal<<" = "<<cCentroid*cNormal<<std::endl;
}
else
Vrui::showErrorMessage("PlaneTool","Could not extract plane equation");
dragging=false;
}
}
void PlaneTool::frame(void)
{
if(dragging)
{
/* Get the current rectangle point: */
p1=Point(application->calcImagePoint(getButtonDeviceRay(0)).getComponents());
}
}
void PlaneTool::display(GLContextData& contextData) const
{
if(dragging)
{
glPushAttrib(GL_ENABLE_BIT|GL_LINE_BIT|GL_POINT_BIT);
glDisable(GL_LIGHTING);
glLineWidth(1.0f);
/* Go to navigation coordinates: */
glPushMatrix();
const Vrui::DisplayState& displayState=Vrui::getDisplayState(contextData);
glLoadMatrix(displayState.modelviewNavigational);
/* Draw the current rectangle: */
glColor3f(0.0f,0.333f,0.0f);
glBegin(GL_LINE_LOOP);
glVertex3d(p0[0],p0[1],0.01);
glVertex3d(p1[0],p0[1],0.01);
glVertex3d(p1[0],p1[1],0.01);
glVertex3d(p0[0],p1[1],0.01);
glEnd();
glPopMatrix();
glPopAttrib();
}
}