Ogen
Ogen

Reputation: 6709

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.

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?

Upvotes: 0

Views: 107

Answers (5)

Andrei Bozantan
Andrei Bozantan

Reputation: 3941

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, e.g. I would say -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);

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

You can use standard algorithm std::max that has as a parameter std::initializer_list For example

#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. The code uses standard algorithm std::max that has two parameters and determinates the maximum between two arguments.

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.

By the way your code has a compilation error There should be one more call of std::max

largestDeviation = 

max(
   max(
      max(
         max( angleBetweenVectors(robustNormal, normalAtA ),
              angleBetweenVectors(robustNormal, normalAtB) 
         ),
         angleBetweenVectors(robustNormal, normalAtC)
      ),
      angleBetweenVectors(robustNormal, normalAtD) 
   ),
   angleBetweenVectors(robustNormal, normalAtE)
);

Upvotes: 3

Cory Kramer
Cory Kramer

Reputation: 118011

// 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));
}

Upvotes: 0

user2249683
user2249683

Reputation:

C++11

#include <algorithm>

largestDeviation = std::max({
   angleBetweenVectors(robustNormal, normalAtA),
   angleBetweenVectors(robustNormal, normalAtB),
   angleBetweenVectors(robustNormal, normalAtC),
   angleBetweenVectors(robustNormal, normalAtD),
   angleBetweenVectors(robustNormal, normalAtE)
});

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 60037

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);

I.e. It will return the value for angleBetweenVectors(robustNormal, normalAtE); as you are missing a max

Upvotes: 4

Related Questions