fftCompressor.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file fftCompressor.h
  10. * @author drose
  11. * @date 2000-12-11
  12. */
  13. #ifndef FFTCOMPRESSOR_H
  14. #define FFTCOMPRESSOR_H
  15. #include "pandabase.h"
  16. #include "pvector.h"
  17. #include "pointerToArray.h"
  18. #include "vector_stdfloat.h"
  19. #include "vector_double.h"
  20. #include "luse.h"
  21. class Datagram;
  22. class DatagramIterator;
  23. /**
  24. * This class manages a lossy compression and decompression of a stream of
  25. * floating-point numbers to a datagram, based a fourier transform algorithm
  26. * (similar in principle to JPEG compression).
  27. *
  28. * Actually, it doesn't do any real compression on its own; it just outputs a
  29. * stream of integers that should compress much tighter via gzip than the
  30. * original stream of floats would have.
  31. *
  32. * This class depends on the external FFTW library; without it, it will fall
  33. * back on lossless output of the original data.
  34. */
  35. class EXPCL_PANDA_MATHUTIL FFTCompressor {
  36. public:
  37. FFTCompressor();
  38. static bool is_compression_available();
  39. void set_quality(int quality);
  40. int get_quality() const;
  41. void set_use_error_threshold(bool use_error_threshold);
  42. bool get_use_error_threshold() const;
  43. void set_transpose_quats(bool flag);
  44. bool get_transpose_quats() const;
  45. void write_header(Datagram &datagram);
  46. void write_reals(Datagram &datagram, const PN_stdfloat *array, int length);
  47. void write_hprs(Datagram &datagram, const LVecBase3 *array, int length);
  48. bool read_header(DatagramIterator &di, int bam_minor_version);
  49. bool read_reals(DatagramIterator &di, vector_stdfloat &array);
  50. bool read_hprs(DatagramIterator &di, pvector<LVecBase3> &array,
  51. bool new_hpr);
  52. bool read_hprs(DatagramIterator &di, pvector<LVecBase3> &array);
  53. static void free_storage();
  54. private:
  55. enum RunWidth {
  56. // We write a byte to the datagram at the beginning of each run to encode
  57. // the width and length of the run. The width is indicated by the top two
  58. // bits, while the length fits in the lower six, except RW_double, which
  59. // is a special case.
  60. RW_width_mask = 0xc0,
  61. RW_length_mask = 0x3f,
  62. RW_0 = 0x00,
  63. RW_8 = 0x40,
  64. RW_16 = 0x80,
  65. RW_32 = 0xc0,
  66. RW_double = 0xff,
  67. RW_invalid = 0x01
  68. };
  69. int write_run(Datagram &datagram, RunWidth run_width,
  70. const vector_double &run);
  71. int read_run(DatagramIterator &di, vector_double &run);
  72. double get_scale_factor(int i, int length) const;
  73. static double interpolate(double t, double a, double b);
  74. PN_stdfloat get_compressability(const PN_stdfloat *data, int length) const;
  75. int _bam_minor_version;
  76. int _quality;
  77. bool _use_error_threshold;
  78. double _fft_offset;
  79. double _fft_factor;
  80. double _fft_exponent;
  81. bool _transpose_quats;
  82. };
  83. #endif