bezier.cpp 3.1 KB

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