inradius.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "inradius.h"
  9. #include "edge_lengths.h"
  10. #include "doublearea.h"
  11. template <
  12. typename DerivedV,
  13. typename DerivedF,
  14. typename DerivedR>
  15. IGL_INLINE void igl::inradius(
  16. const Eigen::PlainObjectBase<DerivedV> & V,
  17. const Eigen::PlainObjectBase<DerivedF> & F,
  18. Eigen::PlainObjectBase<DerivedR> & r)
  19. {
  20. Eigen::Matrix<typename DerivedV::Scalar,Eigen::Dynamic,3> l;
  21. Eigen::Matrix<typename DerivedV::Scalar,Eigen::Dynamic,1> R;
  22. igl::edge_lengths(V,F,l);
  23. // If R is the circumradius,
  24. // R*r = (abc)/(2*(a+b+c))
  25. // R = abc/(4*area)
  26. // r(abc/(4*area)) = (abc)/(2*(a+b+c))
  27. // r/(4*area) = 1/(2*(a+b+c))
  28. // r = (2*area)/(a+b+c)
  29. DerivedR A;
  30. igl::doublearea(l,0.,A);
  31. r = A.array() /l.array().rowwise().sum();
  32. }