draw_skeleton_3d.cpp 5.4 KB

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