整数坐标的多边形压缩

时间:2012-11-11 18:12:30

标签: java compression polygon

我在栅格中有多边形,带有“signed int”2D坐标。多边形的顶点按顺时针方向排列。

我希望以一种更“空间友好”的方式存储这个多边形,它只存储int x,y坐标的序列。如果我用rar压缩这个文件,我的大小比未压缩的小3.5倍。 有没有比简单地在序列中存储x,y更好的表示?

压缩应该是无损的。

2 个答案:

答案 0 :(得分:7)

如果你的多边形是(x1,y1),(x2,y2)...(xn,yn),你可以把它写成,

x1,x2-x1,x3-x2,xn-xn-1,y1,y2-y1 ...,yn-yn-1

通常,相邻x,y值之间的差异将小于绝对值,因此您可以将增量存储为variable length ints。通过这样做,每个x,y对通常可以存储在2或4个字节而不是8。

答案 1 :(得分:1)

很难提供明确的答案,因为结果取决于很多因素。我用GZIPOutputStream进行了快速测试以压缩单个多边形,结果很大程度上取决于多边形的点数,也取决于其面积。

基本上我创建了具有随机分布在预定义区域中的越来越多顶点的多边形。然后我顺时针对顶点进行排序并用GZIP压缩差异

对于适合标准屏幕(1440x900)的多边形:

D Npoints: 3 Uncompressed: 24 Compressed: 41 Ratio: 0.58536583
D Npoints: 4 Uncompressed: 32 Compressed: 49 Ratio: 0.6530612
D Npoints: 5 Uncompressed: 40 Compressed: 58 Ratio: 0.6896552
D Npoints: 6 Uncompressed: 48 Compressed: 61 Ratio: 0.78688526
D Npoints: 7 Uncompressed: 56 Compressed: 66 Ratio: 0.8484849
D Npoints: 8 Uncompressed: 64 Compressed: 72 Ratio: 0.8888889
D Npoints: 9 Uncompressed: 72 Compressed: 80 Ratio: 0.9
D Npoints: 10 Uncompressed: 80 Compressed: 84 Ratio: 0.95238096
D Npoints: 11 Uncompressed: 88 Compressed: 89 Ratio: 0.98876405
D Npoints: 12 Uncompressed: 96 Compressed: 94 Ratio: 1.0212766
D Npoints: 13 Uncompressed: 104 Compressed: 96 Ratio: 1.0833334
D Npoints: 14 Uncompressed: 112 Compressed: 102 Ratio: 1.0980393
D Npoints: 15 Uncompressed: 120 Compressed: 108 Ratio: 1.1111112
. . .
D Npoints: 99 Uncompressed: 792 Compressed: 457 Ratio: 1.7330415

对于较大区域(1440 * 4x900 * 4),压缩比会降低:

D Npoints: 3 Uncompressed: 24 Compressed: 45 Ratio: 0.53333336
D Npoints: 4 Uncompressed: 32 Compressed: 55 Ratio: 0.58181816
D Npoints: 5 Uncompressed: 40 Compressed: 60 Ratio: 0.6666667
D Npoints: 6 Uncompressed: 48 Compressed: 67 Ratio: 0.7164179
D Npoints: 7 Uncompressed: 56 Compressed: 70 Ratio: 0.8
D Npoints: 8 Uncompressed: 64 Compressed: 79 Ratio: 0.8101266
D Npoints: 9 Uncompressed: 72 Compressed: 87 Ratio: 0.82758623
D Npoints: 10 Uncompressed: 80 Compressed: 93 Ratio: 0.86021507
D Npoints: 11 Uncompressed: 88 Compressed: 102 Ratio: 0.8627451
D Npoints: 12 Uncompressed: 96 Compressed: 109 Ratio: 0.88073397
D Npoints: 13 Uncompressed: 104 Compressed: 113 Ratio: 0.920354
D Npoints: 14 Uncompressed: 112 Compressed: 119 Ratio: 0.9411765
D Npoints: 15 Uncompressed: 120 Compressed: 125 Ratio: 0.96
D Npoints: 16 Uncompressed: 128 Compressed: 135 Ratio: 0.94814813
D Npoints: 17 Uncompressed: 136 Compressed: 137 Ratio: 0.99270076
D Npoints: 18 Uncompressed: 144 Compressed: 137 Ratio: 1.0510949
D Npoints: 19 Uncompressed: 152 Compressed: 148 Ratio: 1.027027
D Npoints: 20 Uncompressed: 160 Compressed: 148 Ratio: 1.081081
D Npoints: 21 Uncompressed: 168 Compressed: 154 Ratio: 1.0909091
D Npoints: 22 Uncompressed: 176 Compressed: 160 Ratio: 1.1
    . . . 
D Npoints: 99 Uncompressed: 792 Compressed: 520 Ratio: 1.5230769

我不确定如何获得3.5小的尺寸,但我认为你压缩了一堆多边形。将多个多边形压缩在一起会明显提高压缩比。

当然,这不是程序的可用方法,因为它必须不断解压缩多边形以将它们绘制到需要更多内存的屏幕中,并且CPU超出了整个目的。我只想得到一些数据......