def testTo(self): self.assertEqual(zigZagEncode(-1), 1) self.assertEqual(zigZagEncode(-2), 3) self.assertEqual(zigZagEncode(0), 0) self.assertEqual(zigZagEncode(1), 2) self.assertEqual(zigZagEncode(2), 4) self.assertEqual(zigZagEncode(-1000000), 1999999) self.assertEqual(zigZagEncode(1000000), 2000000)
def _writeTo(self, f): # Header for k, v in TerrainTile.quantizedMeshHeader.iteritems(): f.write(packEntry(v, self.header[k])) # Delta decoding vertexCount = len(self.u) # Vertices f.write(packEntry(TerrainTile.vertexData['vertexCount'], vertexCount)) # Move the initial value f.write( packEntry(TerrainTile.vertexData['uVertexCount'], zigZagEncode(self.u[0])) ) for i in xrange(0, vertexCount - 1): ud = self.u[i + 1] - self.u[i] f.write(packEntry(TerrainTile.vertexData['uVertexCount'], zigZagEncode(ud))) f.write( packEntry(TerrainTile.vertexData['uVertexCount'], zigZagEncode(self.v[0])) ) for i in xrange(0, vertexCount - 1): vd = self.v[i + 1] - self.v[i] f.write(packEntry(TerrainTile.vertexData['vVertexCount'], zigZagEncode(vd))) f.write( packEntry(TerrainTile.vertexData['uVertexCount'], zigZagEncode(self.h[0])) ) for i in xrange(0, vertexCount - 1): hd = self.h[i + 1] - self.h[i] f.write( packEntry(TerrainTile.vertexData['heightVertexCount'], zigZagEncode(hd)) ) # Indices # TODO: verify padding meta = TerrainTile.indexData16 if vertexCount > TerrainTile.BYTESPLIT: meta = TerrainTile.indexData32 f.write(packEntry(meta['triangleCount'], len(self.indices) / 3)) ind = encodeIndices(self.indices) packIndices(f, meta['indices'], ind) meta = TerrainTile.EdgeIndices16 if vertexCount > TerrainTile.BYTESPLIT: meta = TerrainTile.EdgeIndices32 f.write(packEntry(meta['westVertexCount'], len(self.westI))) for wi in self.westI: f.write(packEntry(meta['westIndices'], wi)) f.write(packEntry(meta['southVertexCount'], len(self.southI))) for si in self.southI: f.write(packEntry(meta['southIndices'], si)) f.write(packEntry(meta['eastVertexCount'], len(self.eastI))) for ei in self.eastI: f.write(packEntry(meta['eastIndices'], ei)) f.write(packEntry(meta['northVertexCount'], len(self.northI))) for ni in self.northI: f.write(packEntry(meta['northIndices'], ni)) # Extension header for light if len(self.vLight) > 0: self.hasLighting = True meta = TerrainTile.ExtensionHeader # Extension header ID is 1 for lightening f.write(packEntry(meta['extensionId'], 1)) # Unsigned char size len is 1 f.write(packEntry(meta['extensionLength'], 2 * vertexCount)) # Add 2 bytes of padding f.write(packEntry('B', 1)) f.write(packEntry('B', 1)) metaV = TerrainTile.OctEncodedVertexNormals for i in xrange(0, vertexCount - 1): x, y = octEncode(self.vLight[i]) f.write(packEntry(metaV['xy'], x)) f.write(packEntry(metaV['xy'], y)) if len(self.watermask) > 0: self.hasWatermask = True # Extension header ID is 2 for lightening meta = TerrainTile.ExtensionHeader f.write(packEntry(meta['extensionId'], 2)) # Extension header meta nbRows = len(self.watermask) if nbRows > 1: # Unsigned char size len is 1 f.write(packEntry(meta['extensionLength'], TILEPXS)) if nbRows != 256: raise Exception( 'Unexpected number of rows for the watermask: %s' % nbRows ) # From North to South for i in xrange(0, nbRows): x = self.watermask[i] if len(x) != 256: raise Exception( 'Unexpected number of columns for the watermask: %s' % len(x) ) # From West to East for y in x: f.write(packEntry(TerrainTile.WaterMask['xy'], int(y))) else: f.write(packEntry(meta['extensionLength'], 1)) if self.watermask[0][0] is None: self.watermask[0][0] = 0 f.write(packEntry(TerrainTile.WaterMask['xy'], int(self.watermask[0][0])))
def testBoth(self): self.assertEqual(-1, zigZagDecode(zigZagEncode(-1))) self.assertEqual(-2, zigZagDecode(zigZagEncode(-2))) self.assertEqual(0, zigZagDecode(zigZagEncode(0))) self.assertEqual(1, zigZagDecode(zigZagEncode(1))) self.assertEqual(2, zigZagDecode(zigZagEncode(2))) self.assertEqual(-10000, zigZagDecode(zigZagEncode(-10000))) self.assertEqual(10000, zigZagDecode(zigZagEncode(10000))) self.assertEqual(0, zigZagEncode(zigZagDecode(0))) self.assertEqual(1, zigZagEncode(zigZagDecode(1))) self.assertEqual(2, zigZagEncode(zigZagDecode(2))) self.assertEqual(2000000, zigZagEncode(zigZagDecode(2000000)))