首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >qglviewer上鼠标下的坐标错误

qglviewer上鼠标下的坐标错误
EN

Stack Overflow用户
提问于 2018-12-25 23:05:41
回答 1查看 130关注 0票数 1

我在qglviewer上画了一些线,现在我需要计算从鼠标位置到曲线的最短路径。

代码语言:javascript
复制
void viewer::mouseMoveEvent(QMouseEvent *e) 
{
qglviewer::Vec xx(e->pos().x(), e->pos().y(), 1);
qglviewer::Vec xxx = this->camera()->unprojectedCoordinatesOf(xx);
float dist1track = std::numeric_limits<float>::max();
for(int i = 0; i < wtrjF.size(); i++)
{
   Atom atom = wtrjF[i];
   for(float t = 0; t < atom.pos.size(); t++)
   {
       if(dist1track > qSqrt(qPow(atom.pos[t][0] - xxx[0], 2) + qPow(atom.pos[t][1] - xxx[1], 2)))
       {
           dist1track = qSqrt(qPow(atom.pos[t][0] - xxx[0], 2) + qPow(atom.pos[t][1] - xxx[1], 2));
           name = atom.wname;
           wid = atom.wid;
           pos = QString::number(atom.pos[0][0]) + "_" + QString::number(atom.pos[0][1]);
       }
   }
}
qDebug()<<name<<dist1track;
}

但是它给了我错误的曲线我想我在鼠标光标下得到了错误的坐标,但不知道如何修复它。我也尝试过像这样获取坐标:

代码语言:javascript
复制
qglviewer::Vec xx = camera()->pointUnderPixel(e->pos(), found);
qglviewer::Vec xx(e->pos().x(), e->pos().y(), 0);
glReadPixels(e->pos().x(), view[3] - e->pos().y(), 1, 1, GL_DEPTH_COMPONENT, 
GL_FLOAT, &z1);
qglviewer::Vec xx(e->pos().x(), e->pos().y(), z1);

有什么建议吗?

鼠标位置屏幕

是的,现在我画了一条从光标到最近点的线。错误的和弦

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-26 04:31:38

您应该计算从Atom点到由相机原点和鼠标单击点形成的光线的距离。

Source

下面是一个示例:

代码语言:javascript
复制
qreal distance_point_to_ray( qglviewer::Vec P, qglviewer::Vec orig, qglviewer::Vec dir ){

    qreal dotP_Ray = (P - orig) * dir; //<-- dot product of P on ray
    qreal dot_dir_dir = dir * dir;   //we need this when dir is not normalized.  

    qreal t0 = dotP_Ray / dot_dir_dir;
    qglviewer::Vec BP = P - (orig + dir * t0 );  //B is the projection of P on ray.

    return sqrt( BP * BP );
}

void viewer::mouseMoveEvent(QMouseEvent *e)
{
    qglviewer::Vec orig, dir;

    //get mouse ray in real world coordinate.
    camera()->convertClickToLine(e->pos(), orig, dir);

    float dist1track = std::numeric_limits<float>::max();
    for(int i = 0; i < wtrjF.size(); i++)
    {
        Atom atom = wtrjF[i];
        for(int t = 0; t < atom.pos.size(); t++)
        {
            //assum that atom.pos is an array of qglviewer::Vec
            float d = distance_point_to_ray( atom.pos[t], orig, dir );

            if(dist1track > d )
            {
                dist1track = d;
                name = atom.wname;
                wid = atom.wid;

                pos = QString("POS: [%1, %2, %3]").arg(atom.pos[t][0], 0, 'g', 3 )
                        .arg(atom.pos[t][1], 0, 'g', 3 )
                        .arg(atom.pos[t][2], 0, 'g', 3 );
            }
        }
    }

    qDebug() << name << dist1track;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53923480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档