[英]Weird behavior with max function
I have this code: 我有以下代码:
largestDeviation = max(max(max(angleBetweenVectors(robustNormal, normalAtA),
angleBetweenVectors(robustNormal, normalAtB)),
angleBetweenVectors(robustNormal, normalAtC)),
angleBetweenVectors(robustNormal, normalAtD)),
angleBetweenVectors(robustNormal, normalAtE);
The method: angleBetweenVectors
returns a float
. 方法:
angleBetweenVectors
返回float
。
I can't see what this code is doing and why it's actually building, the brackets aren't matching up properly and Im afraid its going to cause errors. 我看不到这段代码在做什么,以及为什么它实际上在构建,方括号无法正确匹配,恐怕它会导致错误。 How can I just get the maximum value of all of my calls to
angleBetweenVectors
? 如何获得我对
angleBetweenVectors
的所有调用的angleBetweenVectors
?
Add some indetation and you get: 添加一些说明,您将获得:
largestDeviation =
max(
max(
max(
angleBetweenVectors(robustNormal, normalAtA),
angleBetweenVectors(robustNormal, normalAtB)
),
angleBetweenVectors(robustNormal, normalAtC)
),
angleBetweenVectors(robustNormal, normalAtD)
),
angleBetweenVectors(robustNormal, normalAtE);
Ie It will return the value for angleBetweenVectors(robustNormal, normalAtE);
即它将返回
angleBetweenVectors(robustNormal, normalAtE);
的值angleBetweenVectors(robustNormal, normalAtE);
as you are missing a max
因为你错过了
max
You can use standard algorithm std::max that has as a parameter std::initializer_list
For example 您可以使用标准算法std :: max作为参数
std::initializer_list
作为参数
#include <algorithm>
//...
largestDeviation = std::max(
{
angleBetweenVectors( robustNormal, normalAtA ),
angleBetweenVectors( robustNormal, normalAtB ),
angleBetweenVectors( robustNormal, normalAtC ),
angleBetweenVectors( robustNormal, normalAtD ),
angleBetweenVectors( robustNormal, normalAtE )
} );
As for you code then it determine the maximum value among values returned by corresponding calls of function angleBetweenVectors. 对于您的代码,它将确定由angleBetweenVectors函数的相应调用返回的值中的最大值。 The code uses standard algorithm
std::max
that has two parameters and determinates the maximum between two arguments. 该代码使用标准算法
std::max
,该算法具有两个参数,并确定两个参数之间的最大值。
For example the inner call 例如内部调用
max( angleBetweenVectors(robustNormal, normalAtA ),
angleBetweenVectors(robustNormal, normalAtB ) ),
finds the maximum between two values returned by the function calls. 查找函数调用返回的两个值之间的最大值。 The result of the function
std::max
along with the next call if the function in turn is used in the enclosing std::max
call. 如果在封闭的
std::max
调用中依次使用了该函数,则函数std::max
以及下一个调用的结果。
By the way your code has a compilation error There should be one more call of std::max
顺便说一下,您的代码有编译错误。应该再调用一次
std::max
largestDeviation =
max(
max(
max(
max( angleBetweenVectors(robustNormal, normalAtA ),
angleBetweenVectors(robustNormal, normalAtB)
),
angleBetweenVectors(robustNormal, normalAtC)
),
angleBetweenVectors(robustNormal, normalAtD)
),
angleBetweenVectors(robustNormal, normalAtE)
);
You can write your code also like this: 您也可以这样编写代码:
maxAngle = angleBetweenVectors(robustNormal, normalAtA);
maxAngle = max(angleBetweenVectors(robustNormal, normalAtB), maxAngle);
maxAngle = max(angleBetweenVectors(robustNormal, normalAtC), maxAngle);
maxAngle = max(angleBetweenVectors(robustNormal, normalAtD), maxAngle);
maxAngle = max(angleBetweenVectors(robustNormal, normalAtE), maxAngle);
Or, more aligned, when choosing a good initial value for maxAngle
, eg I would say -2*M_PI
: 或者,当为
maxAngle
选择一个好的初始值时,例如,我会说-2*M_PI
:
maxAngle = -2 * M_PI;
maxAngle = max(angleBetweenVectors(robustNormal, normalAtA), maxAngle);
maxAngle = max(angleBetweenVectors(robustNormal, normalAtB), maxAngle);
maxAngle = max(angleBetweenVectors(robustNormal, normalAtC), maxAngle);
maxAngle = max(angleBetweenVectors(robustNormal, normalAtD), maxAngle);
maxAngle = max(angleBetweenVectors(robustNormal, normalAtE), maxAngle);
C++11 C ++ 11
#include <algorithm>
largestDeviation = std::max({
angleBetweenVectors(robustNormal, normalAtA),
angleBetweenVectors(robustNormal, normalAtB),
angleBetweenVectors(robustNormal, normalAtC),
angleBetweenVectors(robustNormal, normalAtD),
angleBetweenVectors(robustNormal, normalAtE)
});
// Store all your normals in a vector
std::vector<normal> normals;
normals.push_back(normalAtA);
normals.push_back(normalAtB);
normals.push_back(normalAtC);
normals.push_back(normalAtD);
normals.push_back(normalAtE);
// Iterate through your vector, and check each angle to see if it is the new max
max_val = 0;
vector<normal>::iterator it;
for (it = normals.begin(); it != normals.end(); ++it)
{
max_val = max(max_val, angleBetweenVectors(robustNormal, *it));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.