bezier.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2020 Alec Jacobson <[email protected]>
  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 "bezier.h"
  9. #include "PlainMatrix.h"
  10. #include <cassert>
  11. // Adapted from main.c accompanying
  12. // An Algorithm for Automatically Fitting Digitized Curves
  13. // by Philip J. Schneider
  14. // from "Graphics Gems", Academic Press, 1990
  15. template <typename DerivedV, typename DerivedP>
  16. IGL_INLINE void igl::bezier(
  17. const Eigen::MatrixBase<DerivedV> & V,
  18. const typename DerivedV::Scalar t,
  19. Eigen::PlainObjectBase<DerivedP> & P)
  20. {
  21. // working local copy
  22. PlainMatrix<DerivedV> Vtemp = V;
  23. int degree = Vtemp.rows()-1;
  24. /* Triangle computation */
  25. for (int i = 1; i <= degree; i++)
  26. {
  27. for (int j = 0; j <= degree-i; j++)
  28. {
  29. Vtemp.row(j) = ((1.0 - t) * Vtemp.row(j) + t * Vtemp.row(j+1)).eval();
  30. }
  31. }
  32. P = Vtemp.row(0);
  33. }
  34. template <typename DerivedV, typename DerivedT, typename DerivedP>
  35. IGL_INLINE void igl::bezier(
  36. const Eigen::MatrixBase<DerivedV> & V,
  37. const Eigen::MatrixBase<DerivedT> & T,
  38. Eigen::PlainObjectBase<DerivedP> & P)
  39. {
  40. P.resize(T.size(),V.cols());
  41. for(int i = 0;i<T.size();i++)
  42. {
  43. Eigen::Matrix<typename DerivedV::Scalar,1,DerivedV::ColsAtCompileTime> Pi;
  44. bezier(V,T(i),Pi);
  45. P.row(i) = Pi;
  46. }
  47. }
  48. template <typename VMat, typename DerivedT, typename DerivedP>
  49. IGL_INLINE void igl::bezier(
  50. const std::vector<VMat> & spline,
  51. const Eigen::MatrixBase<DerivedT> & T,
  52. Eigen::PlainObjectBase<DerivedP> & P)
  53. {
  54. if(spline.size() == 0) return;
  55. const int m = T.rows();
  56. const int dim = spline[0].cols();
  57. P.resize(m*spline.size(),dim);
  58. for(int c = 0;c<spline.size();c++)
  59. {
  60. assert(dim == spline[c].cols() && "All curves must have same dimension");
  61. DerivedP Pc;
  62. bezier(spline[c],T,Pc);
  63. P.block(m*c,0,m,dim) = Pc;
  64. }
  65. }
  66. #ifdef IGL_STATIC_LIBRARY
  67. // Explicit template instantiation
  68. template void igl::bezier<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::vector<Eigen::Matrix<double, -1, -1, 0, -1, -1>, std::allocator<Eigen::Matrix<double, -1, -1, 0, -1, -1> > > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  69. template void igl::bezier<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  70. template void igl::bezier<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 1, -1, 1, 1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, -1, 1, 1, -1> >&);
  71. #endif