-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLineTool.cpp
460 lines (404 loc) · 13.1 KB
/
LineTool.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/***********************************************************************
LineTool - Calibration tool for RawKinectViewer.
Copyright (c) 2010-2012 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 "LineTool.h"
#include <iostream>
#include <Geometry/Box.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 "RawKinectViewer.h"
/*********************************
Static elements of class LineTool:
*********************************/
LineToolFactory* LineTool::factory=0;
/*************************
Methods of class LineTool:
*************************/
std::pair<LineTool::LineSet::iterator,int> LineTool::findLine(const LineTool::Point& p)
{
std::pair<LineSet::iterator,int> result(lines.end(),-1);
/* Check all line endpoints: */
double maxDist2=Math::sqr(2.0);
for(LineSet::iterator lIt=lines.begin();lIt!=lines.end();++lIt)
{
double startDist2=Geometry::sqrDist(p,lIt->start);
if(maxDist2>startDist2)
{
result.first=lIt;
result.second=0;
maxDist2=startDist2;
}
double endDist2=Geometry::sqrDist(p,lIt->end);
if(maxDist2>endDist2)
{
result.first=lIt;
result.second=1;
maxDist2=endDist2;
}
}
if(result.second>=0)
return result;
/* Check all lines: */
double maxDist=1.0;
for(LineSet::iterator lIt=lines.begin();lIt!=lines.end();++lIt)
{
Vector dir=lIt->end-lIt->start;
Vector norm=Geometry::normal(dir);
Vector off=p-lIt->start;
double dist=Math::abs(norm*off)/Geometry::mag(norm);
if(maxDist>dist)
{
double len=dir*off;
if(len>=0.0&&len<=Geometry::sqr(dir))
{
result.first=lIt;
result.second=2;
maxDist=dist;
}
}
}
return result;
}
std::vector<LineTool::LineSet::iterator> LineTool::getIntersectingLines(LineTool::LineSet::iterator base,LineTool::LineSet& lines)
{
Vector n0=Geometry::normal(base->end-base->start);
double o0=n0*Geometry::mid(base->start,base->end);
/* Find all other lines intersecting the selected one: */
std::vector<std::pair<LineSet::iterator,double> > orderedLines;
for(LineSet::iterator lIt=lines.begin();lIt!=lines.end();++lIt)
if(lIt!=base)
{
Vector n1=Geometry::normal(lIt->end-lIt->start);
double o1=n1*Geometry::mid(lIt->start,lIt->end);
/* Check if the lines intersect: */
if((lIt->start*n0-o0)*(lIt->end*n0-o0)<0.0&&(base->start*n1-o1)*(base->end*n1-o1)<0.0)
{
/* Insert the line into the row list: */
double d1=base->end*n1-o1;
double d0=base->start*n1-o1;
double lambda=(0.0-d0)/(d1-d0);
orderedLines.push_back(std::pair<LineSet::iterator,double>(lIt,lambda));
for(std::vector<std::pair<LineSet::iterator,double> >::iterator olIt=orderedLines.end()-1;olIt!=orderedLines.begin();--olIt)
{
std::vector<std::pair<LineSet::iterator,double> >::iterator olIt2=olIt-1;
if(olIt2->second>olIt->second)
std::swap(*olIt,*olIt2);
}
}
}
/* Assemble the result set: */
std::vector<LineSet::iterator> result;
for(std::vector<std::pair<LineSet::iterator,double> >::iterator olIt=orderedLines.begin();olIt!=orderedLines.end();++olIt)
result.push_back(olIt->first);
return result;
}
void LineTool::makeCell(const LineTool::Line& l0,const LineTool::Line& l1,LineTool::Vector n[2],double o[2])
{
n[0]=Geometry::normal(l0.end-l0.start);
Point c0=Geometry::mid(l0.start,l0.end);
o[0]=c0*n[0];
n[1]=Geometry::normal(l1.end-l1.start);
Point c1=Geometry::mid(l1.start,l1.end);
o[1]=c1*n[1];
if(n[0]*c1<o[0])
{
n[0]=-n[0];
o[0]=-o[0];
}
if(n[1]*c0<o[1])
{
n[1]=-n[1];
o[1]=-o[1];
}
}
LineTool::Point LineTool::intersectLines(const LineTool::Line& l0,const LineTool::Line& l1)
{
Vector n0=Geometry::normal(l0.end-l0.start);
double o0=Geometry::mid(l0.start,l0.end)*n0;
double d1=l1.start*n0-o0;
double d2=l1.end*n0-o0;
double lambda=(0.0-d1)/(d2-d1);
return Geometry::affineCombination(l1.start,l1.end,lambda);
}
bool LineTool::inside(unsigned int x,unsigned int y,const LineTool::Vector ln[4],const double lo[4])
{
for(int i=0;i<4;++i)
if((double(x)+0.5)*ln[i][0]+(double(y)+0.5)*ln[i][1]<lo[i])
return false;
return true;
}
void LineTool::constructGrid(void)
{
/* Start with an arbitrary line: */
LineSet::iterator l0=lines.begin();
// Vector n0=l0->end-l0->start;
/* Find all other lines intersecting the selected one: */
std::vector<LineSet::iterator> rows=getIntersectingLines(l0,lines);
/* Build the column list by repeating the same process with the middle row: */
std::vector<LineSet::iterator> columns=getIntersectingLines(rows[rows.size()/2],lines);
/* Calculate the box containing all grid corners: */
Geometry::Box<double,2> box=Geometry::Box<double,2>::empty;
for(std::vector<LineSet::iterator>::iterator rIt=rows.begin();rIt!=rows.end();++rIt)
for(std::vector<LineSet::iterator>::iterator cIt=columns.begin();cIt!=columns.end();++cIt)
box.addPoint(intersectLines(**rIt,**cIt));
/* Check if the grid is in the color or depth frame: */
if(box.min[0]>=0.0)
{
/* Do something meaningful... */
}
else
{
/* Offset all rows and columns: */
for(std::vector<LineSet::iterator>::iterator rIt=rows.begin();rIt!=rows.end();++rIt)
{
(*rIt)->start[0]+=double(application->depthFrameSize[0]);
(*rIt)->end[0]+=double(application->depthFrameSize[0]);
}
for(std::vector<LineSet::iterator>::iterator cIt=columns.begin();cIt!=columns.end();++cIt)
{
(*cIt)->start[0]+=double(application->depthFrameSize[0]);
(*cIt)->end[0]+=double(application->depthFrameSize[0]);
}
/* Iterate through all grid cells: */
Geometry::PCACalculator<3> oddPca;
Geometry::PCACalculator<3> evenPca;
Vector ln[4];
double lo[4];
gridSquares.clear();
float fgCutoff=float(application->averageNumFrames)*0.5f;
for(unsigned int ri=1;ri<rows.size();++ri)
{
makeCell(*rows[ri-1],*rows[ri],ln,lo);
for(unsigned int ci=1;ci<columns.size();++ci)
{
makeCell(*columns[ci-1],*columns[ci],ln+2,lo+2);
/* Process all average depth frame pixels inside the grid cell: */
Point cellp[4];
cellp[0]=intersectLines(*rows[ri-1],*columns[ci-1]);
cellp[1]=intersectLines(*rows[ri-1],*columns[ci]);
cellp[2]=intersectLines(*rows[ri],*columns[ci]);
cellp[3]=intersectLines(*rows[ri],*columns[ci-1]);
Geometry::Box<double,2> box=Geometry::Box<double,2>::empty;
for(int i=0;i<4;++i)
box.addPoint(cellp[i]);
unsigned int x0=box.min[0]<=0.0?0:(unsigned int)(box.min[0]);
unsigned int x1=box.max[0]>=double(application->depthFrameSize[0]-1)?application->depthFrameSize[0]:(unsigned int)(box.max[0]+1.0);
unsigned int y0=box.min[1]<=0.0?0:(unsigned int)(box.min[1]);
unsigned int y1=box.max[1]>=double(application->depthFrameSize[1]-1)?application->depthFrameSize[1]:(unsigned int)(box.max[1]+1.0);
/* Accumulate the cell's foreground points in the PCA calculator: */
Geometry::PCACalculator<3>& pca=((ri-1)+(ci-1))%2==0?evenPca:oddPca;
const float* afdRowPtr=application->averageFrameDepth+y0*application->depthFrameSize[0];
const float* affRowPtr=application->averageFrameForeground+y0*application->depthFrameSize[0];
for(unsigned int y=y0;y<y1;++y,afdRowPtr+=application->depthFrameSize[0],affRowPtr+=application->depthFrameSize[0])
{
const float* afdPtr=afdRowPtr+x0;
const float* affPtr=affRowPtr+x0;
for(unsigned int x=x0;x<x1;++x,++afdPtr,++affPtr)
if(inside(x,y,ln,lo))
{
if(*affPtr>=fgCutoff)
{
Geometry::Point<double,3> p;
p[0]=x;
p[1]=y;
p[2]=(*afdPtr)/(*affPtr);
pca.accumulatePoint(p);
}
}
}
}
}
/* Calculate the odd and even plane equations: */
for(int i=0;i<2;++i)
{
Geometry::PCACalculator<3>& pca=i==0?evenPca:oddPca;
/* Calculate the grid's plane equation: */
Geometry::PCACalculator<3>::Point centroid=pca.calcCentroid();
pca.calcCovariance();
double evs[3];
pca.calcEigenvalues(evs);
Geometry::PCACalculator<3>::Vector normal=pca.calcEigenvector(evs[2]);
double offset=normal*centroid;
std::cout<<"Plane equation: "<<normal<<", "<<centroid<<", "<<offset<<std::endl;
std::cout<<"Eigenvalues: "<<evs[0]<<", "<<evs[1]<<", "<<evs[2]<<std::endl;
}
}
haveGrid=true;
}
LineToolFactory* LineTool::initClass(Vrui::ToolManager& toolManager)
{
/* Create the tool factory: */
factory=new LineToolFactory("LineTool","Draw Lines",0,toolManager);
/* Set up the tool class' input layout: */
factory->setNumButtons(3);
factory->setButtonFunction(0,"Drag / Create Line");
factory->setButtonFunction(1,"Delete Line");
factory->setButtonFunction(2,"Construct Grid");
/* Register and return the class: */
toolManager.addClass(factory,Vrui::ToolManager::defaultToolFactoryDestructor);
return factory;
}
LineTool::LineTool(const Vrui::ToolFactory* factory,const Vrui::ToolInputAssignment& inputAssignment)
:Vrui::Tool(factory,inputAssignment),
dragging(false),haveGrid(false)
{
}
LineTool::~LineTool(void)
{
}
const Vrui::ToolFactory* LineTool::getFactory(void) const
{
return factory;
}
void LineTool::buttonCallback(int buttonSlotIndex,Vrui::InputDevice::ButtonCallbackData* cbData)
{
if(buttonSlotIndex==0)
{
if(cbData->newButtonState)
{
/* Get the interaction point: */
Point p(application->calcImagePoint(getButtonDeviceRay(0)).getComponents());
/* Check it against all existing lines: */
std::pair<LineSet::iterator,int> lineHit=findLine(p);
dragging=true;
dragMode=lineHit.second;
if(dragMode>=0)
{
/* Drag an existing line: */
draggedLine=lineHit.first;
haveGrid=false;
/* Calculate the dragging offsets: */
startOffset=p-draggedLine->start;
endOffset=p-draggedLine->end;
}
else
{
/* Start dragging a new line: */
current.start=p;
haveGrid=false;
}
}
else if(dragging)
{
if(dragMode<0)
{
/* Finish the new line: */
lines.push_back(current);
haveGrid=false;
}
dragging=false;
}
}
else if(buttonSlotIndex==1)
{
if(cbData->newButtonState)
{
/* Get the interaction point: */
Point p(application->calcImagePoint(getButtonDeviceRay(0)).getComponents());
/* Check it against all existing lines: */
std::pair<LineSet::iterator,int> lineHit=findLine(p);
if(lineHit.first!=lines.end())
{
/* Delete the selected line: */
lines.erase(lineHit.first);
haveGrid=false;
}
}
}
else
{
constructGrid();
}
}
void LineTool::frame(void)
{
if(dragging)
{
/* Get the interaction point: */
Point p(application->calcImagePoint(getButtonDeviceRay(0)).getComponents());
switch(dragMode)
{
case -1:
current.end=p;
break;
case 0:
draggedLine->start=p-startOffset;
break;
case 1:
draggedLine->end=p-endOffset;
break;
case 2:
draggedLine->start=p-startOffset;
draggedLine->end=p-endOffset;
break;
}
}
}
void LineTool::display(GLContextData& contextData) const
{
glPushAttrib(GL_ENABLE_BIT|GL_LINE_BIT|GL_POINT_BIT);
glDisable(GL_LIGHTING);
glLineWidth(1.0f);
glPointSize(3.0f);
glColor3f(0.0f,1.0f,0.0f);
/* Go to navigation coordinates: */
glPushMatrix();
const Vrui::DisplayState& displayState=Vrui::getDisplayState(contextData);
glLoadMatrix(displayState.modelviewNavigational);
/* Draw all existing lines: */
glBegin(GL_LINES);
for(LineSet::const_iterator lIt=lines.begin();lIt!=lines.end();++lIt)
{
glVertex3d(lIt->start[0],lIt->start[1],0.01);
glVertex3d(lIt->end[0],lIt->end[1],0.01);
}
glEnd();
glBegin(GL_POINTS);
for(LineSet::const_iterator lIt=lines.begin();lIt!=lines.end();++lIt)
{
glVertex3d(lIt->start[0],lIt->start[1],0.01);
glVertex3d(lIt->end[0],lIt->end[1],0.01);
}
glEnd();
if(dragging&&dragMode==-1)
{
/* Draw the current line: */
glBegin(GL_LINES);
glVertex3d(current.start[0],current.start[1],0.01);
glVertex3d(current.end[0],current.end[1],0.01);
glEnd();
glBegin(GL_POINTS);
glVertex3d(current.start[0],current.start[1],0.01);
glVertex3d(current.end[0],current.end[1],0.01);
glEnd();
}
if(haveGrid)
{
/* Draw all grid squares: */
glBegin(GL_QUADS);
for(std::vector<Point>::const_iterator gsIt=gridSquares.begin();gsIt!=gridSquares.end();++gsIt)
glVertex3d((*gsIt)[0],(*gsIt)[1],0.01);
glEnd();
}
glPopMatrix();
glPopAttrib();
}