Useless comupations in vtkMath

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Useless comupations in vtkMath

al.gry
Hi everyone,

I posted this thread last week but it wasn't accepted so Im trying again.

I just looked into vtkMath source code provided via VTK.git and I found things like this:

inline double vtkMath::Normalize(double x[3])
{
  double den;
  if ( ( den = vtkMath::Norm( x ) ) != 0.0 )
    {
    for (int i=0; i < 3; i++)
      {
      x[i] /= den;
      }
    }
  return den;
}

I guess the for-loop can be erased since it's useless... btw, if-instruction doesn't do anything so it's also useless.

Regards, Al
Reply | Threaded
Open this post in threaded view
|

Re: Useless comupations in vtkMath

Dženan Zukić
I just took a look at vtk 5.6.1, and the Norm function looks like this:
  // Description:
  // Compute the norm of 3-vector.
  static float Norm(const float x[3]) {
    return static_cast<float> (sqrt( x[0] * x[0] + x[1] * x[1] + x[2] * x[2] ) );};

That means it returns the magnitude of the vector, and the Normalize method works as it should:
1. calculate magnitude
2. if it is not zero
  3. divide all vector components by the magnitude
4. return magnitude

Nothing can be erased here, only made more clear, like this:

inline double vtkMath::Normalize(double x[3])
{
 double den = vtkMath::Norm( x );
 if ( den  != 0.0 )
   {
   for (int i=0; i < 3; i++)
     {
     x[i] /= den;
     }
   }
 return den;
}


On Mon, Aug 15, 2011 at 08:58, al.gry <[hidden email]> wrote:
Hi everyone,

I posted this thread last week but it wasn't accepted so Im trying again.

I just looked into vtkMath source code provided via VTK.git and I found
things like this:

inline double vtkMath::Normalize(double x[3])
{
 double den;
 if ( ( den = vtkMath::Norm( x ) ) != 0.0 )
   {
   for (int i=0; i < 3; i++)
     {
     x[i] /= den;
     }
   }
 return den;
}

I guess the for-loop can be erased since it's useless... btw, if-instruction
doesn't do anything so it's also useless.

Regards, Al

--
View this message in context: http://vtk.1045678.n5.nabble.com/Useless-comupations-in-vtkMath-tp4700083p4700083.html
Sent from the VTK - Users mailing list archive at Nabble.com.
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers


_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

Re: Useless comupations in vtkMath

al.gry
but why do you need to divide?
x[i]/=den equals x[i] = x[i]/den, right? so it changes x[i] locally and x isn't used anywhere in further computation. or did I miss some reference?
Reply | Threaded
Open this post in threaded view
|

Re: Useless comupations in vtkMath

al.gry
stupid I, sorry... where is my "c++ for dummies"?..