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