draw_skeleton_3d.cpp 5.8 KB

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