简体   繁体   English

Qt:在QMap中找到最接近的QVector3D

[英]Qt: Find closest QVector3D in QMap

i have a QMap like this: 我有这样的QMap:

"1" (0.183,-0.232,0.747)
"2" (1.232, 1.322,-0.123) etc.

I need a function which input is a QVector3D and which output is the closest key to the input vector. 我需要一个函数,其输入是QVector3D,哪个输出是输入向量的最接近的键。

For Example: 例如:

InputVector(0.189,-0.234,0.755) -> Output: "1"

Any ideas how to solve this problem? 任何想法如何解决这个问题?

just iterate through the map and check the distances: 只需遍历地图并检查距离:

int getClosestKey(const QVector3D & ref, const QMap<int, QVector3D> & map)
{
   int closestKey = -1;
   double minDistance = std::numeric_limits<double>::max();
   for (auto itr = map.constBegin(); itr != map.constEnd(); ++itr)
   {
      double d = ref.distanceToPoint(itr.value());
      if (d > minDistance)
         continue;

      closestKey = itr.key();
      minDistance = d;
   }

   return closestKey;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM