Skip to content

Commit

Permalink
Merge pull request #8 from loicgasser/rolker_master
Browse files Browse the repository at this point in the history
Fix oct decode and add tests
  • Loading branch information
loicgasser authored Nov 4, 2016
2 parents eac317b + f675968 commit 9826da8
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 16 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: python
python: 2.7
before_install:
- sudo mv /etc/apt/sources.list.d/pgdg-source.list* /tmp
- sudo apt-get remove -y libgdal1
- sudo add-apt-repository -y ppa:ubuntugis/ubuntugis-unstable
- sudo apt-get update -y
Expand Down
4 changes: 2 additions & 2 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@
# built documents.
#
# The short X.Y version.
version = u'0.1'
version = u'0.2'
# The full version, including alpha/beta/rc tags.
release = u'0.1alpha'
release = u'0.2alpha'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
1 change: 1 addition & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Contributors:

* Loïc Gasser (https://github.com/loicgasser)
* Gilbert Jeiziner (https://github.com/gjn)
* Roland Arsenault (https://github.com/rolker)

Styling:

Expand Down
2 changes: 1 addition & 1 deletion quantized_mesh_tile/bbsphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, *args, **kwargs):
# Based on Ritter's algorithm
def fromPoints(self, points):

if len(points) < 1:
if len(points) < 2:
raise Exception('Your list of points must contain at least 2 points')

nbPositions = len(points)
Expand Down
3 changes: 2 additions & 1 deletion quantized_mesh_tile/terrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ def fromFile(self, filePath, hasLighting=False, hasWatermask=False, gzipped=Fals
if extensionId == 1:
extensionLength = unpackEntry(f, meta['extensionLength'])

# Consider padding of 2 bits, no idea why?
# Consider padding of 2 bits
# http://cesiumjs.org/data-and-assets/terrain/formats/quantized-mesh-1.0.html
f.read(2)

for i in xrange(0, (extensionLength / 2) - 1):
Expand Down
22 changes: 13 additions & 9 deletions quantized_mesh_tile/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from struct import pack, unpack, calcsize


EPSILON6 = 0.000001


def packEntry(type, value):
return pack('<%s' % type, value)

Expand Down Expand Up @@ -88,6 +91,8 @@ def fromSnorm(v):
# Compress x, y, z 96-bit floating point into x, z 16-bit representation (2 snorm values)
# https://github.com/AnalyticalGraphicsInc/cesium/blob/b161b6429b9201c99e5fb6f6e6283f3e8328b323/Source/Core/AttributeCompression.js#L43
def octEncode(vec):
if abs(c3d.magnitudeSquared(vec) - 1.0) > EPSILON6:
raise ValueError('Only normalized vectors are supported')
res = [0.0, 0.0]
l1Norm = float(abs(vec[0]) + abs(vec[1]) + abs(vec[2]))
res[0] = vec[0] / l1Norm
Expand All @@ -105,15 +110,17 @@ def octEncode(vec):


def octDecode(x, y):
if x < 0 or x > 255 or y < 0 or y > 255:
raise ValueError('x and y must be signed and normalized between 0 and 255')
res = [x, y, 0.0]
res[0] = fromSnorm(x)
res[1] = fromSnorm(y)
res[2] = 1.0 - (abs(res[1]) - abs(res[1]))
res[2] = 1.0 - (abs(res[0]) + abs(res[1]))

if res[2] < 0.0:
oldX = res[0]
res[0] = (1.0 - abs(res[1]) * signNotZero(oldX))
res[1] = (1.0 - abs(oldX) * signNotZero(res[1]))
res[0] = (1.0 - abs(res[1])) * signNotZero(oldX)
res[1] = (1.0 - abs(oldX)) * signNotZero(res[1])
return c3d.normalize(res)


Expand Down Expand Up @@ -154,9 +161,7 @@ def computeNormals(vertices, faces):
normalA = np.cross(v1A, v2A)
viewPointA = c3d.add(ctrd, normalA)

v1B = c3d.subtract(v0, v1)
v2B = c3d.subtract(v2, v1)
normalB = np.cross(v1B, v2B)
normalB = np.cross(v2A, v1A)
viewPointB = c3d.add(ctrd, normalB)

area = triangleArea(v0, v1)
Expand All @@ -172,10 +177,9 @@ def computeNormals(vertices, faces):

for i in xrange(0, numFaces):
face = faces[i]
weightedNormal = [c * areasPerFace[i] for c in normalsPerFace[i]]
for j in face:
weightedNormal = [c * areasPerFace[i] for c in normalsPerFace[i]]
normalsPerVertex[j] = c3d.add(
normalsPerVertex[j], weightedNormal)
normalsPerVertex[j] = c3d.add(normalsPerVertex[j], weightedNormal)

for i in xrange(0, numVertices):
normalsPerVertex[i] = c3d.normalize(normalsPerVertex[i])
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
requires = ['shapely', 'numpy']

setup(name='quantized-mesh-tile',
version='0.1alpha',
version='0.2alpha',
description='Quantized-Mesh format reader and writer',
author=u'Loic Gasser',
author_email='[email protected]',
Expand Down
3 changes: 2 additions & 1 deletion tests/test_bbsphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def testBoundingSphereOnePoint(self):
sphere = BoundingSphere()
point = [[1.1, 3.2, 4.9]]

self.assertRaises(Exception, sphere.fromPoints(point))
with self.assertRaises(Exception):
sphere.fromPoints(point)

def testBoundingSpherePrecision(self):
x = 533
Expand Down
57 changes: 57 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-

import unittest
from quantized_mesh_tile.utils import octDecode, octEncode


class TestUtils(unittest.TestCase):

def testOctDecode(self):
vec3 = octDecode(0, 0)
self.assertEqual(vec3[0], 0.0)
self.assertEqual(vec3[1], 0.0)
self.assertEqual(vec3[2], -1.0)

vec3 = octDecode(255, 255)
self.assertEqual(vec3[0], 0.0)
self.assertEqual(vec3[1], 0.0)
self.assertEqual(vec3[2], -1.0)

vec3 = octDecode(255, 0)
self.assertEqual(vec3[0], 0.0)
self.assertEqual(vec3[1], 0.0)
self.assertEqual(vec3[2], -1.0)

vec3 = octDecode(0, 255)
self.assertEqual(vec3[0], 0.0)
self.assertEqual(vec3[1], 0.0)
self.assertEqual(vec3[2], -1.0)

def testOctDecodeErrors(self):
with self.assertRaises(ValueError):
octDecode(-1, 0)

with self.assertRaises(ValueError):
octDecode(0, -1)

with self.assertRaises(ValueError):
octDecode(256, 0)

with self.assertRaises(ValueError):
octDecode(0, 256)

def testOctEncode(self):
vec2 = octEncode([0.0, 0.0, -1.0])
self.assertEqual(vec2[0], 255)
self.assertEqual(vec2[1], 255)

vec2 = octEncode([0.0, 0.0, 1.0])
self.assertEqual(vec2[0], 128)
self.assertEqual(vec2[1], 128)

def testOctEncodeErrors(self):
with self.assertRaises(ValueError):
octEncode([2.0, 0.0, 0.0])

with self.assertRaises(ValueError):
octEncode([0.0, 0.0, 0.0])

0 comments on commit 9826da8

Please sign in to comment.