3
votes

In my application I often use data which contain two integer angles of spherical coordinates. They are phi (0 <= phi < 360 in degrees), and theta (0 <= theta <= 180 in degrees). Is there any way to compress these angles into 2 bytes? I have found solution which compresses them for 17 bits - it is enough for me, but maybe there any algorithm which allows to compress them for 16 bits? Unfortunately I don't know specific algorithms in such area (like compression theory)

2
@RoryDaulton, angles are integer values, which lie in specific range (I mentioned it in question)LmTinyToon
If the angles are given as integers, 360 * 180 = 64800 < 65535, so you can easily "compress" into 16 bits my the multiplication theta*360+phi, then "decompress" with the help of modulo and division operators.njuffa
Sneaky: theta can be 180, so it has 181 possible valuesharold
But 181*360 < 65536, so simple multiply-add still worksnjuffa
@njuffa, It sounds very interesting for me, I definitely should check itLmTinyToon

2 Answers

6
votes

Yes, you can do it: multiply the first angle phi by 181, and add the second angle theta. The result will fit in an unsigned 16-bit integer:

uint16_t compressed = 181 * phi + theta;

Given your constraints, the highest number that you are going to get is 65159 (359*181+180), which is barely under 216-1 (65535).

4
votes

If the values of phi and theta are integers in your ranges, there are 360 possible values of phi and 181 possible values of theta. You can combine them into a single value between 0 and 65159 with the expression

phi * 181 + theta

Given a "compressed value of n in the range, you can get back the original values with

phi = n // 181
theta = n % 181

Note that the values will fit into an unsigned 16-bit integer, which allows 0 to 65535, but not a signed one without further processing.