draw_skeleton_3d.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "draw_skeleton_3d.h"
  9. #include "PI.h"
  10. #include "OpenGL_convenience.h"
  11. #include "material_colors.h"
  12. #include <Eigen/Geometry>
  13. #include <iostream>
  14. template <typename DerivedC, typename DerivedBE>
  15. IGL_INLINE void igl::draw_skeleton_3d(
  16. const Eigen::PlainObjectBase<DerivedC> & C,
  17. const Eigen::PlainObjectBase<DerivedBE> & BE)
  18. {
  19. using namespace Eigen;
  20. typedef Eigen::Matrix<typename DerivedC::Scalar,Dynamic,Dynamic> Mat;
  21. Mat I = Mat::Identity(C.cols()+1,C.cols());
  22. Mat T = I.replicate(BE.rows(),1);
  23. return draw_skeleton_3d(C,BE,T);
  24. }
  25. template <typename DerivedC, typename DerivedBE, typename DerivedT>
  26. IGL_INLINE void igl::draw_skeleton_3d(
  27. const Eigen::PlainObjectBase<DerivedC> & C,
  28. const Eigen::PlainObjectBase<DerivedBE> & BE,
  29. const Eigen::PlainObjectBase<DerivedT> & T)
  30. {
  31. using namespace Eigen;
  32. using namespace std;
  33. glDisable(GL_LIGHTING);
  34. glColor4fv(MAYA_SEA_GREEN.data());
  35. glLineWidth(1.0);
  36. auto draw_sphere = [](const double r)
  37. {
  38. auto draw_circle = []()
  39. {
  40. glBegin(GL_LINE_STRIP);
  41. for(double th = 0;th<2.0*igl::PI;th+=(2.0*igl::PI/30.0))
  42. {
  43. glVertex3d(cos(th),sin(th),0.0);
  44. }
  45. glVertex3d(cos(0),sin(0),0.0);
  46. glEnd();
  47. };
  48. glPushMatrix();
  49. glScaled(r,r,r);
  50. draw_circle();
  51. glRotated(90.0,1.0,0.0,0.0);
  52. draw_circle();
  53. glRotated(90.0,0.0,1.0,0.0);
  54. draw_circle();
  55. glPopMatrix();
  56. };
  57. auto draw_pyramid = [](const double r)
  58. {
  59. glBegin(GL_LINE_STRIP);
  60. glVertex3d(0, 1,-1);
  61. glVertex3d(0,-1,-1);
  62. glVertex3d(0,-1, 1);
  63. glVertex3d(0, 1, 1);
  64. glVertex3d(0, 1,-1);
  65. glEnd();
  66. glBegin(GL_LINES);
  67. glVertex3d(0, 1,-1);
  68. glVertex3d(1,0,0);
  69. glVertex3d(0,-1,-1);
  70. glVertex3d(1,0,0);
  71. glVertex3d(0,-1, 1);
  72. glVertex3d(1,0,0);
  73. glVertex3d(0, 1, 1);
  74. glVertex3d(1,0,0);
  75. glEnd();
  76. };
  77. // Loop over bones
  78. for(int e = 0;e < BE.rows();e++)
  79. {
  80. // Draw a sphere
  81. auto s = C.row(BE(e,0));
  82. auto d = C.row(BE(e,1));
  83. auto b = (d-s).transpose().eval();
  84. double r = 0.02;
  85. Matrix4d Te = Matrix4d::Identity();
  86. Te.block(0,0,3,4) = T.block(e*4,0,4,3).transpose();
  87. Quaterniond q;
  88. q.setFromTwoVectors(Vector3d(1,0,0),b);
  89. glPushMatrix();
  90. glMultMatrixd(Te.data());
  91. glTranslated(s(0),s(1),s(2));
  92. draw_sphere(r);
  93. const double len = b.norm()-2.*r;
  94. if(len>=0)
  95. {
  96. auto u = b.normalized()*r;
  97. glPushMatrix();
  98. glTranslated(u(0),u(1),u(2));
  99. glMultMatrixd(Affine3d(q).matrix().data());
  100. glScaled(b.norm()-2.*r,r,r);
  101. draw_pyramid(r);
  102. glPopMatrix();
  103. }
  104. glTranslated(b(0),b(1),b(2));
  105. draw_sphere(r);
  106. glPopMatrix();
  107. }
  108. }
  109. #ifndef IGL_HEADER_ONLY
  110. // Explicit template instanciation
  111. template void igl::draw_skeleton_3d<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&);
  112. #endif