Skip to content

Commit

Permalink
Merge pull request #40 from Oslandia/fix_issue_30
Browse files Browse the repository at this point in the history
Fix issue 30
  • Loading branch information
peppsac committed Oct 25, 2018
2 parents 85b1370 + 65d6f67 commit 6abad9c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
1 change: 0 additions & 1 deletion py3dtiles/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def write_tileset(in_folder, out_folder, octree_metadata, offset, scale, project
tileset = {
'asset': {
'version': '1.0',
'gltfUpAxis': 'Z',
},
'geometricError': np.linalg.norm(
octree_metadata.aabb[1] - octree_metadata.aabb[0]) / scale[0],
Expand Down
28 changes: 20 additions & 8 deletions py3dtiles/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def compute_bbox(self):
def to_tileset(self, transform):
self.compute_bbox()
tiles = {
"asset": {"version": "1.0", "gltfUpAxis": "Z"},
"asset": {"version": "1.0"},
"geometricError": 500, # TODO
"root": self.to_tileset_r(500)
}
Expand All @@ -71,9 +71,9 @@ def to_tileset(self, transform):
def to_tileset_r(self, error):
(c1, c2) = (self.box.min, self.box.max)
center = [(c1[i] + c2[i]) / 2 for i in range(0, 3)]
xAxis = [c2[0] - c1[0], 0, 0]
yAxis = [0, c2[1] - c1[1], 0]
zAxis = [0, 0, c2[2] - c1[2]]
xAxis = [(c2[0] - c1[0]) / 2, 0, 0]
yAxis = [0, (c2[1] - c1[1]) / 2, 0]
zAxis = [0, 0, (c2[2] - c1[2]) / 2]
box = [round(x, 3) for x in center + xAxis + yAxis + zAxis]
tile = {
"boundingVolume": {
Expand All @@ -85,7 +85,7 @@ def to_tileset_r(self, error):
}
if len(self.features) != 0:
tile["content"] = {
"url": "tiles/{0}.b3dm".format(self.id)
"uri": "tiles/{0}.b3dm".format(self.id)
}

return tile
Expand Down Expand Up @@ -113,11 +113,23 @@ def arrays2tileset(positions, normals, bboxes, transform, ids=None):
maxTileSize = 2000
featuresPerTile = 20
indices = [i for i in range(len(positions))]

# glTF is Y-up, so to get the bounding boxes in the 3D tiles
# coordinate system, we have to apply a Y-to-Z transform to the
# glTF bounding boxes
zUpBboxes = []
for bbox in bboxes:
tmp = m = bbox[0]
M = bbox[1]
m = [m[0], -m[2], m[1]]
M = [M[0], -tmp[2], M[1]]
zUpBboxes.append([m, M])

# Compute extent
xMin = yMin = float('inf')
xMax = yMax = - float('inf')

for bbox in bboxes:
for bbox in zUpBboxes:
xMin = min(xMin, bbox[0][0])
yMin = min(yMin, bbox[0][1])
xMax = max(xMax, bbox[1][0])
Expand All @@ -133,7 +145,7 @@ def arrays2tileset(positions, normals, bboxes, transform, ids=None):
tile = tile_extent(extent, maxTileSize, i, j)

geoms = []
for idx, box in zip(indices, bboxes):
for idx, box in zip(indices, zUpBboxes):
bbox = BoundingBox(box[0], box[1])

if tile.inside(bbox.center()):
Expand Down Expand Up @@ -240,7 +252,7 @@ def from_db(db_name, table_name, column_name, id_column_name, user_name):
id_statement = ""
if id_column_name is not None:
id_statement = "," + id_column_name
cur.execute("SELECT ST_AsBinary(ST_Translate({0}, {1}, {2}, {3})),"
cur.execute("SELECT ST_AsBinary(ST_RotateX(ST_Translate({0}, {1}, {2}, {3}), -pi() / 2)),"
"ST_Area(ST_Force2D({0})) AS weight{5} FROM {4} ORDER BY weight DESC"
.format(column_name, -offset[0], -offset[1], -offset[2],
table_name, id_statement))
Expand Down
20 changes: 9 additions & 11 deletions py3dtiles/merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def _get_root_tile(tileset, filename):

pnts_filename = os.path.join(
folder,
tileset['root']['content']['url'])
tileset['root']['content']['uri'])

return TileReader().read_file(pnts_filename)

Expand Down Expand Up @@ -175,7 +175,7 @@ def build_tileset_quadtree(out_folder, aabb, tilesets, base_transform, inv_base_
'geometricError': insides[0]['root']['geometricError'],
'boundingVolume': _3dtiles_bounding_box_from_aabb(box),
'content': {
'url': os.path.relpath(insides[0]['filename'], out_folder)
'uri': os.path.relpath(insides[0]['filename'], out_folder)
}
}
else:
Expand Down Expand Up @@ -225,23 +225,23 @@ def build_tileset_quadtree(out_folder, aabb, tilesets, base_transform, inv_base_
np.concatenate((xyz.view(np.uint8).ravel(), rgb.ravel())),
out_folder,
rgb.shape[0] > 0)[1]
result['content'] = { 'url': os.path.relpath(filename, out_folder) }
result['content'] = { 'uri': os.path.relpath(filename, out_folder) }
result['geometricError'] = sum([t['root']['geometricError'] for t in insides])
result['boundingVolume'] = _3dtiles_bounding_box_from_aabb(union_aabb, inv_base_transform)

return result


def extract_content_urls(tileset):
def extract_content_uris(tileset):
contents = []
for key in tileset:
if key == 'content':
contents += [tileset[key]['url']]
contents += [tileset[key]['uri']]
elif key == 'children':
for child in tileset['children']:
contents += extract_content_urls(child)
contents += extract_content_uris(child)
elif key == 'root':
contents += extract_content_urls(tileset['root'])
contents += extract_content_uris(tileset['root'])

return contents

Expand All @@ -250,7 +250,7 @@ def remove_tileset(tileset_filename):
folder = os.path.dirname(tileset_filename)
with open(tileset_filename, 'r') as f:
tileset = json.load(f)
contents = extract_content_urls(tileset)
contents = extract_content_uris(tileset)
for content in contents:
ext = os.path.splitext(content)[1][1:]
if ext == 'pnts':
Expand Down Expand Up @@ -309,8 +309,7 @@ def main(args):
result['transform'] = base_transform.T.reshape(16).tolist()
tileset = {
'asset': {
'version': '1.0',
'gltfUpAxis': 'Z',
'version': '1.0'
},
'refine': 'REPLACE',
'geometricError': np.linalg.norm((aabb[1] - aabb[0])[0:3]),
Expand All @@ -321,6 +320,5 @@ def main(args):
json.dump(tileset, f)



if __name__ == '__main__':
main()
5 changes: 2 additions & 3 deletions py3dtiles/points/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def to_tileset(executor, name, parent_aabb, parent_spacing, folder, scale):

children = []
if os.path.exists(ondisk_tile):
tile['content'] = {'url': os.path.relpath(ondisk_tile, folder)}
tile['content'] = {'uri': os.path.relpath(ondisk_tile, folder)}
for child in ['0', '1', '2', '3', '4', '5', '6', '7']:
child_name = '{}{}'.format(name.decode('ascii'), child).encode('ascii')
ondisk_tile = name_to_filename(folder, child_name, '.pnts')
Expand All @@ -236,7 +236,6 @@ def to_tileset(executor, name, parent_aabb, parent_spacing, folder, scale):
tile_root = {
'asset': {
'version': '1.0',
'gltfUpAxis': 'Z',
},
'refine': 'ADD',
'geometricError': tile['geometricError'],
Expand All @@ -245,7 +244,7 @@ def to_tileset(executor, name, parent_aabb, parent_spacing, folder, scale):
tileset_name = 'tileset.{}.json'.format(name.decode('ascii'))
with open('{}/{}'.format(folder, tileset_name), 'w') as f:
f.write(json.dumps(tile_root))
tile['content'] = {'url': tileset_name}
tile['content'] = {'uri': tileset_name}
tile['children'] = []

return tile
2 changes: 1 addition & 1 deletion tests/tileset.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"geometricError": 0,
"content": {
"url": "pointCloudRGB.pnts"
"uri": "pointCloudRGB.pnts"
}
}
}

0 comments on commit 6abad9c

Please sign in to comment.