Vec3Packed 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* -*-c++-*-
  2. */
  3. //****************************************************************************//
  4. // loader.cpp //
  5. // Copyright (C) 2001, 2002 Bruno 'Beosil' Heidelberger //
  6. //****************************************************************************//
  7. // This library is free software; you can redistribute it and/or modify it //
  8. // under the terms of the GNU Lesser General Public License as published by //
  9. // the Free Software Foundation; either version 2.1 of the License, or (at //
  10. // your option) any later version. //
  11. //****************************************************************************//
  12. /*****************************************************************************/
  13. /* Loads a core compressed keyframe instance.
  14. *
  15. * This function loads a core compressed keyframe instance from a data source.
  16. *
  17. * @param dataSrc The data source to load the core compressed keyframe instance from.
  18. *
  19. * @return One of the following values:
  20. * \li a pointer to the core keyframe
  21. * \li \b 0 if an error happened
  22. * Authors
  23. * Igor Kravchenko
  24. * Cedric Pinson <mornifle@plopbyte.net>
  25. *****************************************************************************/
  26. #ifndef OSGANIMATION_PACKED_H
  27. #define OSGANIMATION_PACKED_H
  28. #include <float.h>
  29. #include <vector>
  30. #include <osg/Vec3>
  31. #include <osg/Math>
  32. namespace osgAnimation
  33. {
  34. struct Vec3Packed
  35. {
  36. typedef unsigned int uint32_t;
  37. uint32_t m32bits;
  38. Vec3Packed(uint32_t val): m32bits(val) {}
  39. Vec3Packed(): m32bits(0) {}
  40. void uncompress(const osg::Vec3& scale, const osg::Vec3& min, osg::Vec3& result) const
  41. {
  42. uint32_t pt[3];
  43. pt[0] = m32bits & 0x7ff;
  44. pt[1] = (m32bits >> 11) & 0x7ff;
  45. pt[2] = m32bits >> 22;
  46. result[0] = scale[0] * pt[0] + min[0];
  47. result[1] = scale[1] * pt[1] + min[1];
  48. result[2] = scale[2] * pt[2] + min[2];
  49. }
  50. void compress(const osg::Vec3f& src, const osg::Vec3f& min, const osg::Vec3f& scaleInv)
  51. {
  52. uint32_t srci[3];
  53. srci[0] = osg::minimum(static_cast<uint32_t>(((src[0] - min[0] )*scaleInv[0])), uint32_t(2047));
  54. srci[1] = osg::minimum(static_cast<uint32_t>(((src[1] - min[1] )*scaleInv[1])), uint32_t(2047));
  55. srci[2] = osg::minimum(static_cast<uint32_t>(((src[2] - min[2] )*scaleInv[2])), uint32_t(1023));
  56. m32bits = srci[0] + (srci[1] << 11) + (srci[2] << 22);
  57. }
  58. };
  59. struct Vec3ArrayPacked
  60. {
  61. std::vector<Vec3Packed> mVecCompressed;
  62. osg::Vec3 mMin;
  63. osg::Vec3 mScale;
  64. osg::Vec3 mScaleInv;
  65. void analyze(const std::vector<osg::Vec3>& src)
  66. {
  67. //analyze the keys
  68. mMin.set(FLT_MAX, FLT_MAX, FLT_MAX);
  69. osg::Vec3 maxp(-FLT_MAX, -FLT_MAX, -FLT_MAX);
  70. int nb = (int)src.size();
  71. for(int i = 0; i < nb; i++)
  72. {
  73. const osg::Vec3 &pos = src[i];
  74. for(int j = 0; j < 3; j++)
  75. {
  76. maxp[j] = osg::maximum(pos[j],maxp[j]);
  77. mMin[j] = osg::minimum(pos[j],mMin[j]);
  78. }
  79. }
  80. osg::Vec3 diff = maxp - mMin;
  81. mScaleInv.set(0,0,0);
  82. if (diff[0] != 0)
  83. mScaleInv[0] = 2047.0/diff[0];
  84. if (diff[1] != 0)
  85. mScaleInv[1] = 2047.0/diff[1];
  86. if (diff[2] != 0)
  87. mScaleInv[2] = 1023.0/diff[2];
  88. mScale[0] = diff[0] / 2047;
  89. mScale[1] = diff[1] / 2047;
  90. mScale[2] = diff[2] / 1023;
  91. }
  92. void compress(const std::vector<osg::Vec3>& src)
  93. {
  94. mVecCompressed.resize(src.size());
  95. // save all core keyframes
  96. for(int i = 0; i < (int)src.size(); i++)
  97. mVecCompressed[i].compress(src[i], mMin, mScaleInv);
  98. }
  99. };
  100. }
  101. #endif