draw_skeleton_3d.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // old settings
  34. int old_lighting=0;
  35. double old_line_width=1;
  36. glGetIntegerv(GL_LIGHTING,&old_lighting);
  37. glGetDoublev(GL_LINE_WIDTH,&old_line_width);
  38. glDisable(GL_LIGHTING);
  39. glLineWidth(1.0);
  40. glColor4fv(MAYA_SEA_GREEN.data());
  41. auto draw_sphere = [](const double r)
  42. {
  43. auto draw_circle = []()
  44. {
  45. glBegin(GL_LINE_STRIP);
  46. for(double th = 0;th<2.0*igl::PI;th+=(2.0*igl::PI/30.0))
  47. {
  48. glVertex3d(cos(th),sin(th),0.0);
  49. }
  50. glVertex3d(cos(0),sin(0),0.0);
  51. glEnd();
  52. };
  53. glPushMatrix();
  54. glScaled(r,r,r);
  55. draw_circle();
  56. glRotated(90.0,1.0,0.0,0.0);
  57. draw_circle();
  58. glRotated(90.0,0.0,1.0,0.0);
  59. draw_circle();
  60. glPopMatrix();
  61. };
  62. auto draw_pyramid = [](const double r)
  63. {
  64. glBegin(GL_LINE_STRIP);
  65. glVertex3d(0, 1,-1);
  66. glVertex3d(0,-1,-1);
  67. glVertex3d(0,-1, 1);
  68. glVertex3d(0, 1, 1);
  69. glVertex3d(0, 1,-1);
  70. glEnd();
  71. glBegin(GL_LINES);
  72. glVertex3d(0, 1,-1);
  73. glVertex3d(1,0,0);
  74. glVertex3d(0,-1,-1);
  75. glVertex3d(1,0,0);
  76. glVertex3d(0,-1, 1);
  77. glVertex3d(1,0,0);
  78. glVertex3d(0, 1, 1);
  79. glVertex3d(1,0,0);
  80. glEnd();
  81. };
  82. // Loop over bones
  83. for(int e = 0;e < BE.rows();e++)
  84. {
  85. // Draw a sphere
  86. auto s = C.row(BE(e,0));
  87. auto d = C.row(BE(e,1));
  88. auto b = (d-s).transpose().eval();
  89. double r = 0.02;
  90. Matrix4d Te = Matrix4d::Identity();
  91. Te.block(0,0,3,4) = T.block(e*4,0,4,3).transpose();
  92. Quaterniond q;
  93. q.setFromTwoVectors(Vector3d(1,0,0),b);
  94. glPushMatrix();
  95. glMultMatrixd(Te.data());
  96. glTranslated(s(0),s(1),s(2));
  97. draw_sphere(r);
  98. const double len = b.norm()-2.*r;
  99. if(len>=0)
  100. {
  101. auto u = b.normalized()*r;
  102. glPushMatrix();
  103. glTranslated(u(0),u(1),u(2));
  104. glMultMatrixd(Affine3d(q).matrix().data());
  105. glScaled(b.norm()-2.*r,r,r);
  106. draw_pyramid(r);
  107. glPopMatrix();
  108. }
  109. glTranslated(b(0),b(1),b(2));
  110. draw_sphere(r);
  111. glPopMatrix();
  112. }
  113. // Reset settings
  114. (old_lighting ? glEnable(GL_LIGHTING) : glDisable(GL_LIGHTING));
  115. glLineWidth(old_line_width);
  116. }
  117. #ifndef IGL_HEADER_ONLY
  118. // Explicit template instanciation
  119. 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&);
  120. template void igl::draw_skeleton_3d<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -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&);
  121. #endif