draw_skeleton_3d.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 || color.size() == 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. assert(T.rows() == BE.rows()*(C.cols()+1));
  43. assert(T.cols() == C.cols());
  44. // old settings
  45. int old_lighting=0;
  46. double old_line_width=1;
  47. glGetIntegerv(GL_LIGHTING,&old_lighting);
  48. glGetDoublev(GL_LINE_WIDTH,&old_line_width);
  49. glDisable(GL_LIGHTING);
  50. glLineWidth(1.0);
  51. auto draw_sphere = [](const double r)
  52. {
  53. auto draw_circle = []()
  54. {
  55. glBegin(GL_LINE_STRIP);
  56. for(double th = 0;th<2.0*igl::PI;th+=(2.0*igl::PI/30.0))
  57. {
  58. glVertex3d(cos(th),sin(th),0.0);
  59. }
  60. glVertex3d(cos(0),sin(0),0.0);
  61. glEnd();
  62. };
  63. glPushMatrix();
  64. glScaled(r,r,r);
  65. draw_circle();
  66. glRotated(90.0,1.0,0.0,0.0);
  67. draw_circle();
  68. glRotated(90.0,0.0,1.0,0.0);
  69. draw_circle();
  70. glPopMatrix();
  71. };
  72. auto draw_pyramid = [](const double r)
  73. {
  74. glBegin(GL_LINE_STRIP);
  75. glVertex3d(0, 1,-1);
  76. glVertex3d(0,-1,-1);
  77. glVertex3d(0,-1, 1);
  78. glVertex3d(0, 1, 1);
  79. glVertex3d(0, 1,-1);
  80. glEnd();
  81. glBegin(GL_LINES);
  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. glVertex3d(0, 1, 1);
  89. glVertex3d(1,0,0);
  90. glEnd();
  91. };
  92. // Loop over bones
  93. for(int e = 0;e < BE.rows();e++)
  94. {
  95. // Draw a sphere
  96. auto s = C.row(BE(e,0));
  97. auto d = C.row(BE(e,1));
  98. auto b = (d-s).transpose().eval();
  99. double r = 0.02;
  100. Matrix4d Te = Matrix4d::Identity();
  101. Te.block(0,0,3,4) = T.block(e*4,0,4,3).transpose();
  102. Quaterniond q;
  103. q.setFromTwoVectors(Vector3d(1,0,0),b);
  104. glPushMatrix();
  105. glMultMatrixd(Te.data());
  106. glTranslated(s(0),s(1),s(2));
  107. if(color.size() == 4)
  108. {
  109. Vector4d d = color.template cast<double>();
  110. glColor4dv(d.data());
  111. }else
  112. {
  113. glColor4d( color(e,0), color(e,1), color(e,2), color(e,3));
  114. }
  115. draw_sphere(r);
  116. const double len = b.norm()-2.*r;
  117. if(len>=0)
  118. {
  119. auto u = b.normalized()*r;
  120. glPushMatrix();
  121. glTranslated(u(0),u(1),u(2));
  122. glMultMatrixd(Affine3d(q).matrix().data());
  123. glScaled(b.norm()-2.*r,r,r);
  124. draw_pyramid(r);
  125. glPopMatrix();
  126. }
  127. glTranslated(b(0),b(1),b(2));
  128. draw_sphere(r);
  129. glPopMatrix();
  130. }
  131. // Reset settings
  132. (old_lighting ? glEnable(GL_LIGHTING) : glDisable(GL_LIGHTING));
  133. glLineWidth(old_line_width);
  134. }
  135. template <typename DerivedC, typename DerivedBE, typename DerivedT>
  136. IGL_INLINE void igl::draw_skeleton_3d(
  137. const Eigen::PlainObjectBase<DerivedC> & C,
  138. const Eigen::PlainObjectBase<DerivedBE> & BE,
  139. const Eigen::PlainObjectBase<DerivedT> & T)
  140. {
  141. return draw_skeleton_3d(C,BE,T,MAYA_SEA_GREEN);
  142. }
  143. template <typename DerivedC, typename DerivedBE>
  144. IGL_INLINE void igl::draw_skeleton_3d(
  145. const Eigen::PlainObjectBase<DerivedC> & C,
  146. const Eigen::PlainObjectBase<DerivedBE> & BE)
  147. {
  148. return draw_skeleton_3d(C,BE,Eigen::MatrixXd(),MAYA_SEA_GREEN);
  149. }
  150. #ifndef IGL_HEADER_ONLY
  151. // Explicit template instanciation
  152. 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&);
  153. 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&);
  154. 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&);
  155. #endif