Qt
to implement a graphical user interface
(GUI) for testing the kd-tree's
insert()
and
nn()
,
knn()
, and
range()
(query) functions.
GLTexobj
object, subclassed (derived) from
Qt's
QGLWidget
readily provides these mouse event handlers (that you have to
override). Your derived GLTexobj
class just needs to provide the following "event handles":
void GLTexobj::mousePressEvent(QMouseEvent* e)
void GLTexobj::mouseReleaseEvent(QMouseEvent* e)
When the mouse is pressed, you check to see which button triggered
the event by testing against a pre-defined enumerated type, e.g.,
if the left mouse button was pressed,
if(e->button() == Qt::LeftButton) {
// left mouse button pressed
}
you should add a new point_t(x,y)
to the list of points
"owned" by the GLTexobj
.
If the right mouse button was pressed,
if(e->button() ==Qt::RightButton) {
// right mouse button pressed
}
you should turn on a box drawing flag (a simple boolean) and set
the starting box coordinates to the mouse coordinates:
// starting to draw the range query bounding box
drawingBox = true;
x1 = x; y1 = y;
x2 = x; y2 = y;
When the mouse button is released, and it's the right button that
was released, set the final coordinates of the range box,
"sort" both sets of coordinates into their minimum and maximum:
stop drawing the range box and call the kd-tree range query:
x2 = x; y2 = y;
point_t min,max;
min[0] = x1 < x2 ? x1 : x2; max[0] = x1 > x2 ? x1 : x2;
min[1] = y1 < y2 ? y1 : y2; max[1] = y1 > y2 ? y1 : y2;
Modifier keyboard keys can be checked by similarly testing
the event
drawingBox = false;
kdtree.range(min,max,range);
modifiers()
against pre-defined enumerated
types, e.g.,
or if there are two that you want to be pressed simultanesouly,
if(e->modifiers() == Qt::ShiftModifier) {
// SHIFT key pressed when button pressed
}
or maybe you don't want any modifiers pressed at all:
if(e->modifiers() == (Qt::ShiftModifier & Qt::AltModifier)) {
// ALT SHIFT key pressed when button pressed
}
if(e->modifiers() == Qt::NoModifier) {
// no modifier key pressed when button pressed
}
GLTexobj
window's mouse coordinates are
usually measured within the window dimensions:
[0,width()],[0,height()]
with (0,0)
at
upper-left. To map these coordinates so that (0,0)
is at the window center, use this handy conversion (with
QMouseEvent* e
):
// handle Qt to OpenGL y-coordinate flip
x = e->x();
y = height() - e->y();
// normalize coordinates (puts them in range [0,1])
double dx = (double)x/width();
double dy = (double)y/height();
// scale coorindates by 2 then shift to the right by 1, scale again by w,h
dx = width()*(2.0*dx - 1.0);
dy = height()*(2.0*dy - 1.0);
// set integer coordinates
x = (int)dx;
y = (int)dy;
tar.gz
archive of your asg##/ directory, including:
README
file containing
Makefile
.h
headers and .cpp
source)
make clean
before tar
)
handin
notes