draw_skeleton_3d.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <
  15. typename DerivedC,
  16. typename DerivedBE,
  17. typename DerivedT,
  18. typename Derivedcolor>
  19. IGL_INLINE void igl::draw_skeleton_3d(
  20. const Eigen::PlainObjectBase<DerivedC> & C,
  21. const Eigen::PlainObjectBase<DerivedBE> & BE,
  22. const Eigen::PlainObjectBase<DerivedT> & T,
  23. const Eigen::PlainObjectBase<Derivedcolor> & color)
  24. {
  25. // Note: Maya's skeleton *does* scale with the mesh suggesting a scale
  26. // parameter. Further, its joint balls are not rotated with the bones.
  27. using namespace Eigen;
  28. using namespace std;
  29. assert(color.cols() == 4);
  30. if(T.size() == 0)
  31. {
  32. typedef Eigen::Matrix<typename DerivedT::Scalar,Dynamic,Dynamic> Mat;
  33. Mat I = Mat::Identity(C.cols()+1,C.cols());
  34. Mat T = I.replicate(BE.rows(),1);
  35. // insane base case
  36. if(T.size() == 0)
  37. {
  38. return;
  39. }
  40. return draw_skeleton_3d(C,BE,T,MAYA_SEA_GREEN);
  41. }
  42. // old settings
  43. int old_lighting=0;
  44. double old_line_width=1;
  45. glGetIntegerv(GL_LIGHTING,&old_lighting);
  46. glGetDoublev(GL_LINE_WIDTH,&old_line_width);
  47. glDisable(GL_LIGHTING);
  48. glLineWidth(1.0);
  49. auto draw_sphere = [](const double r)
  50. {
  51. auto draw_circle = []()
  52. {
  53. glBegin(GL_LINE_STRIP);
  54. for(double th = 0;th<2.0*igl::PI;th+=(2.0*igl::PI/30.0))
  55. {
  56. glVertex3d(cos(th),sin(th),0.0);
  57. }
  58. glVertex3d(cos(0),sin(0),0.0);
  59. glEnd();
  60. };
  61. glPushMatrix();
  62. glScaled(r,r,r);
  63. draw_circle();
  64. glRotated(90.0,1.0,0.0,0.0);
  65. draw_circle();
  66. glRotated(90.0,0.0,1.0,0.0);
  67. draw_circle();
  68. glPopMatrix();
  69. };
  70. auto draw_pyramid = [](const double r)
  71. {
  72. glBegin(GL_LINE_STRIP);
  73. glVertex3d(0, 1,-1);
  74. glVertex3d(0,-1,-1);
  75. glVertex3d(0,-1, 1);
  76. glVertex3d(0, 1, 1);
  77. glVertex3d(0, 1,-1);
  78. glEnd();
  79. glBegin(GL_LINES);
  80. glVertex3d(0, 1,-1);
  81. glVertex3d(1,0,0);
  82. glVertex3d(0,-1,-1);
  83. glVertex3d(1,0,0);
  84. glVertex3d(0,-1, 1);
  85. glVertex3d(1,0,0);
  86. glVertex3d(0, 1, 1);
  87. glVertex3d(1,0,0);
  88. glEnd();
  89. };
  90. // Loop over bones
  91. for(int e = 0;e < BE.rows();e++)
  92. {
  93. // Draw a sphere
  94. auto s = C.row(BE(e,0));
  95. auto d = C.row(BE(e,1));
  96. auto b = (d-s).transpose().eval();
  97. double r = 0.02;
  98. Matrix4d Te = Matrix4d::Identity();
  99. Te.block(0,0,3,4) = T.block(e*4,0,4,3).transpose();
  100. Quaterniond q;
  101. q.setFromTwoVectors(Vector3d(1,0,0),b);
  102. glPushMatrix();
  103. glMultMatrixd(Te.data());
  104. glTranslated(s(0),s(1),s(2));
  105. if(color.rows() > 1)
  106. {
  107. glColor4d( color(e,0), color(e,1), color(e,2), color(e,3));
  108. }else
  109. {
  110. glColor4d( color(0,0), color(0,1), color(0,2), color(0,3));
  111. }
  112. draw_sphere(r);
  113. const double len = b.norm()-2.*r;
  114. if(len>=0)
  115. {
  116. auto u = b.normalized()*r;
  117. glPushMatrix();
  118. glTranslated(u(0),u(1),u(2));
  119. glMultMatrixd(Affine3d(q).matrix().data());
  120. glScaled(b.norm()-2.*r,r,r);
  121. draw_pyramid(r);
  122. glPopMatrix();
  123. }
  124. glTranslated(b(0),b(1),b(2));
  125. draw_sphere(r);
  126. glPopMatrix();
  127. }
  128. // Reset settings
  129. (old_lighting ? glEnable(GL_LIGHTING) : glDisable(GL_LIGHTING));
  130. glLineWidth(old_line_width);
  131. }
  132. template <typename DerivedC, typename DerivedBE, typename DerivedT>
  133. IGL_INLINE void igl::draw_skeleton_3d(
  134. const Eigen::PlainObjectBase<DerivedC> & C,
  135. const Eigen::PlainObjectBase<DerivedBE> & BE,
  136. const Eigen::PlainObjectBase<DerivedT> & T)
  137. {
  138. return draw_skeleton_3d(C,BE,T,MAYA_SEA_GREEN);
  139. }
  140. template <typename DerivedC, typename DerivedBE>
  141. IGL_INLINE void igl::draw_skeleton_3d(
  142. const Eigen::PlainObjectBase<DerivedC> & C,
  143. const Eigen::PlainObjectBase<DerivedBE> & BE)
  144. {
  145. return draw_skeleton_3d(C,BE,Eigen::MatrixXd(),MAYA_SEA_GREEN);
  146. }
  147. #ifndef IGL_HEADER_ONLY
  148. // Explicit template instanciation
  149. 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&);
  150. 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&);
  151. 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::Matrix<float, -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&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&);
  152. #endif