Henrik Holst added section_Code_begin_Language_C__.tex  almost 9 years ago

Commit id: 1dcb051d1a09761d7dc31f8b4edc92fbb11e5814

deletions | additions      

         

\section*{Code}  \begin[Language=C++]{lstlisting}  int EllipsoidSolver(Vec3 E, Vec3 y,  float& err, float& resid, Vec3& x, Vec3& n,  float tol=1e-4, float rtol=1e-4, int maxiter=10, bool useX=false)  {  const float Rmin = 1e-4;  const Vec3 One = Vec3(1.0f);  const Vec3 y2 = y*y;  const Vec3 E2 = E*E;  const float yn = norm(y);  const float R = min(E);  float t;  float rn;  int iter = 0;  bool conv;  if (yn < Rmin)  return -maxiter; /* Point y is too close to centre of ellipsoid. */  // Start guess.  if (useX)  t = norm(y-x)/norm(x/E2);  else  t = -R + norm(y);  while (iter < maxiter)  {  x = (E2/(Vec3(t)+E2))*y;  Vec3 xp = -x/(Vec3(t)+E2);  float F = dot(One, x*x/E2) - 1.0f;  float Fp = 2.0f*dot(One, x*xp/E2);  float dt = F/Fp;  t = t - dt;  iter = iter + 1;  err = std::fabs(dt);  resid = std::fabs(F);  conv = (err < tol && resid < rtol);  if (conv) break;  }  // Compute a normal from (1/2) grad(E); and normalize.  n = x/E2;  rn = 1.0f/norm(n);  n = rn*n;  return conv ? iter : -iter;  }  \end{lstlisting}