Commit 9de369f3 authored by Stefan Pfeifer's avatar Stefan Pfeifer

Implement mouse pan for the limb view

parent fda17f2d
......@@ -8,6 +8,8 @@
void LimbMesh::setData(const InputData& data)
{
vertex_data.clear();
aabb_min = QVector3D();
aabb_max = QVector3D();
// Todo: Abstract away the conversion data -> profile curve
std::vector<double> lengths = getEvalLengths(data, 100);
......@@ -26,21 +28,21 @@ void LimbMesh::setData(const InputData& data)
size_t n_layers = data.layers.size();
size_t n_sections = lengths.size()-1;
std::vector<Vector<3>> points_l_prev(n_layers+1);
std::vector<Vector<3>> points_l_next(n_layers+1);
std::vector<Vector<3>> points_r_prev(n_layers+1);
std::vector<Vector<3>> points_r_next(n_layers+1);
std::vector<QVector3D> points_l_prev(n_layers+1);
std::vector<QVector3D> points_l_next(n_layers+1);
std::vector<QVector3D> points_r_prev(n_layers+1);
std::vector<QVector3D> points_r_next(n_layers+1);
std::vector<size_t> layer_indices(n_layers);
for(size_t i = 0; i < n_sections; ++i)
{
Vector<3> center_prev {profile.x[i], profile.y[i], 0.0 };
Vector<3> normal_w_prev{ 0.0, 0.0, 1.0 };
Vector<3> normal_h_prev{-sin(profile.phi[i]), cos(profile.phi[i]), 0.0 };
QVector3D center_prev (profile.x[i], profile.y[i], 0.0 );
QVector3D normal_w_prev( 0.0, 0.0, 1.0 );
QVector3D normal_h_prev(-sin(profile.phi[i]), cos(profile.phi[i]), 0.0 );
Vector<3> center_next { profile.x[i+1], profile.y[i+1], 0.0 };
Vector<3> normal_w_next{ 0.0, 0.0, 1.0 };
Vector<3> normal_h_next{-sin(profile.phi[i+1]), cos(profile.phi[i+1]), 0.0 };
QVector3D center_next ( profile.x[i+1], profile.y[i+1], 0.0 );
QVector3D normal_w_next( 0.0, 0.0, 1.0 );
QVector3D normal_h_next(-sin(profile.phi[i+1]), cos(profile.phi[i+1]), 0.0 );
double p_prev = profile.s[i]/profile.s.maxCoeff();
double p_next = profile.s[i+1]/profile.s.maxCoeff();
......@@ -161,12 +163,22 @@ size_t LimbMesh::vertexCount() const
return vertex_data.size()/9;
}
void LimbMesh::addQuad(const Vector<3>& p0, const Vector<3>& p1, const Vector<3>& p2, const Vector<3>& p3, const QColor& color)
QVector3D LimbMesh::aabbCenter() const
{
Vector<3> n0 = (p1 - p0).cross(p3 - p0).normalized();
Vector<3> n1 = (p2 - p1).cross(p0 - p1).normalized();
Vector<3> n2 = (p3 - p2).cross(p1 - p2).normalized();
Vector<3> n3 = (p0 - p3).cross(p2 - p3).normalized();
return (aabb_min + aabb_max)/2.0f;
}
float LimbMesh::aabbDiagonal() const
{
return (aabb_max - aabb_min).length();
}
void LimbMesh::addQuad(const QVector3D& p0, const QVector3D& p1, const QVector3D& p2, const QVector3D& p3, const QColor& color)
{
QVector3D n0 = QVector3D::normal(p1 - p0, p3 - p0);
QVector3D n1 = QVector3D::normal(p2 - p1, p0 - p1);
QVector3D n2 = QVector3D::normal(p3 - p2, p1 - p2);
QVector3D n3 = QVector3D::normal(p0 - p3, p2 - p3);
addVertex(p0, n0, color);
addVertex(p1, n1, color);
......@@ -177,7 +189,7 @@ void LimbMesh::addQuad(const Vector<3>& p0, const Vector<3>& p1, const Vector<3>
addVertex(p3, n3, color);
}
void LimbMesh::addVertex(const Vector<3>& position, const Vector<3>& normal, const QColor& color)
void LimbMesh::addVertex(const QVector3D& position, const QVector3D& normal, const QColor& color)
{
vertex_data.push_back(position.x());
vertex_data.push_back(position.y());
......@@ -190,4 +202,14 @@ void LimbMesh::addVertex(const Vector<3>& position, const Vector<3>& normal, con
vertex_data.push_back(color.redF());
vertex_data.push_back(color.greenF());
vertex_data.push_back(color.blueF());
// Todo: Is there a vector operation for this in Eigen?
aabb_min.setX(std::min(aabb_min.x(), position.x()));
aabb_min.setY(std::min(aabb_min.y(), position.y()));
aabb_min.setZ(std::min(aabb_min.z(), position.z()));
// Todo: Is there a vector operation for this in Eigen?
aabb_max.setX(std::max(aabb_max.x(), position.x()));
aabb_max.setY(std::max(aabb_max.y(), position.y()));
aabb_max.setZ(std::max(aabb_max.z(), position.z()));
}
......@@ -14,10 +14,15 @@ public:
const std::vector<GLfloat>& vertexData() const;
size_t vertexCount() const;
QVector3D aabbCenter() const;
float aabbDiagonal() const;
private:
void addQuad(const Vector<3>& p0, const Vector<3>& p1, const Vector<3>& p2, const Vector<3>& p3, const QColor& color);
void addVertex(const Vector<3>& position, const Vector<3>& normal, const QColor& color);
void addQuad(const QVector3D& p0, const QVector3D& p1, const QVector3D& p2, const QVector3D& p3, const QColor& color);
void addVertex(const QVector3D& position, const QVector3D& normal, const QColor& color);
std::vector<double> getEvalLengths(const InputData& data, unsigned n);
std::vector<GLfloat> vertex_data;
QVector3D aabb_min;
QVector3D aabb_max;
};
......@@ -11,6 +11,8 @@ LimbView::LimbView()
m_yRot(0),
m_zRot(0),
m_zoom(1.0f),
shift_x(0.0f),
shift_y(0.0f),
m_program(nullptr)
{
auto button0 = new QToolButton();
......@@ -132,8 +134,7 @@ void LimbView::setZRotation(int angle)
void LimbView::zoom(float factor)
{
m_zoom *= factor;
update();
}
void LimbView::cleanup()
......@@ -237,13 +238,22 @@ void LimbView::paintGL()
m_world.rotate(180.0f - (m_xRot/16.0f), 1.0f, 0.0f, 0.0f);
m_world.rotate(m_yRot/16.0f, 0.0f, 1.0f, 0.0f);
m_world.rotate(m_zRot/16.0f, 0.0f, 0.0f, 1.0f);
QVector3D modelCenter(0.4f, 0.0f, 0.0f); // Todo: Get from limb mesh
m_world.translate(-modelCenter);
m_world.scale(1.0f/m_mesh.aabbDiagonal());
m_world.translate(-m_mesh.aabbCenter());
m_camera.setToIdentity();
m_camera.translate(0.0f, 0.0f, -1.0f);
m_camera.scale(m_zoom);
m_projection.setToIdentity();
float aspect_ratio = float(this->height())/this->width();
m_projection.ortho(-0.5f*m_zoom + shift_x,
0.5f*m_zoom + shift_x,
-0.5f*m_zoom*aspect_ratio + shift_y,
0.5f*m_zoom*aspect_ratio + shift_y,
0.001f, 100.0f);
// m_projection.translate(shift_x, shift_y*aspect_ratio, 0.0f);
// m_projection.scale(m_zoom);
m_program->bind();
m_program->setUniformValue(m_projectionMatrixLoc, m_projection);
......@@ -254,16 +264,6 @@ void LimbView::paintGL()
m_program->release();
}
void LimbView::resizeGL(int width, int height)
{
float extent = 0.8f; // Todo: Get from limb mesh
float rw = extent/2.0f;
float rh = rw*float(height)/width;
m_projection.setToIdentity();
m_projection.ortho(-rw, rw, -rh, rh, 0.01f, 100.0f);
}
void LimbView::mousePressEvent(QMouseEvent *event)
{
m_lastPos = event->pos();
......@@ -279,10 +279,11 @@ void LimbView::mouseMoveEvent(QMouseEvent *event)
setXRotation(m_xRot + 8*dy);
setYRotation(m_yRot + 8*dx);
}
else if(event->buttons() & Qt::RightButton)
else if(event->buttons() & Qt::MiddleButton)
{
setXRotation(m_xRot + 8*dy);
setZRotation(m_zRot + 8*dx);
shift_x -= float(dx)/this->width()*m_zoom;
shift_y += float(dy)/this->width()*m_zoom;
update();
}
m_lastPos = event->pos();
......@@ -290,11 +291,11 @@ void LimbView::mouseMoveEvent(QMouseEvent *event)
void LimbView::wheelEvent(QWheelEvent* event)
{
const float zoom_factor = 1.25;
const float zoom_speed = 1.25;
float delta = event->angleDelta().y()/120.0f;
float factor = (delta > 0.0f) ? delta*zoom_speed : -delta/zoom_speed;
m_zoom /= factor;
if(delta > 0.0f)
zoom( delta*zoom_factor);
else
zoom(-delta/zoom_factor);
update();
}
......@@ -35,7 +35,6 @@ private:
void initializeGL() override;
void paintGL() override;
void resizeGL(int width, int height) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void wheelEvent(QWheelEvent* event) override;
......@@ -46,6 +45,8 @@ private:
int m_yRot;
int m_zRot;
float m_zoom;
float shift_x;
float shift_y;
LimbMesh m_mesh;
QOpenGLBuffer m_meshVbo;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment