Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
autra committed Jul 9, 2018
1 parent 371ae5a commit ea780e8
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 58 deletions.
122 changes: 64 additions & 58 deletions src/Provider/PointCloudProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,55 @@ function addPickingAttribute(points) {
return points;
}

function parseMetadata(layer, metadata) {
layer.metadata = metadata;

let bbox;
var customBinFormat = true;

// Lopocs pointcloud server can expose the same file structure as PotreeConverter output.
// The only difference is the metadata root file (cloud.js vs infos/sources), and we can
// check for the existence of a `scale` field.
// (if `scale` is defined => we're fetching files from PotreeConverter)
if (layer.metadata.scale != undefined) {
// PotreeConverter format
customBinFormat = layer.metadata.pointAttributes === 'CIN';
bbox = new THREE.Box3(
new THREE.Vector3(metadata.boundingBox.lx, metadata.boundingBox.ly, metadata.boundingBox.lz),
new THREE.Vector3(metadata.boundingBox.ux, metadata.boundingBox.uy, metadata.boundingBox.uz));

// do we have normal information
const normal = Array.isArray(layer.metadata.pointAttributes) &&
layer.metadata.pointAttributes.find(elem => elem.startsWith('NORMAL'));
if (normal) {
layer.material.defines[normal] = 1;
}
} else {
// Lopocs
layer.metadata.scale = 1;
layer.metadata.octreeDir = `itowns/${layer.table}.points`;
layer.metadata.hierarchyStepSize = 1000000; // ignore this with lopocs
customBinFormat = true;

let idx = 0;
for (const entry of metadata) {
if (entry.table == layer.table) {
break;
}
idx++;
}
bbox = new THREE.Box3(
new THREE.Vector3(metadata[idx].bbox.xmin, metadata[idx].bbox.ymin, metadata[idx].bbox.zmin),
new THREE.Vector3(metadata[idx].bbox.xmax, metadata[idx].bbox.ymax, metadata[idx].bbox.zmax));
}

layer.parse = customBinFormat ? PotreeCinParser.parse : PotreeBinParser.parse;
layer.extension = customBinFormat ? 'cin' : 'bin';
layer.supportsProgressiveDisplay = customBinFormat;

return bbox;
}

export default {
preprocessDataLayer(layer) {
if (!layer.file) {
Expand Down Expand Up @@ -179,64 +228,17 @@ export default {
// this probably needs to be moved to somewhere else
layer.pickObjectsAt = (view, mouse, radius) => Picking.pickPointsAt(view, mouse, radius, layer);

return Fetcher.json(`${layer.url}/${layer.file}`, layer.fetchOptions).then((cloud) => {
layer.metadata = cloud;

let bbox;
var customBinFormat = true;

// Lopocs pointcloud server can expose the same file structure as PotreeConverter output.
// The only difference is the metadata root file (cloud.js vs infos/sources), and we can
// check for the existence of a `scale` field.
// (if `scale` is defined => we're fetching files from PotreeConverter)
if (layer.metadata.scale != undefined) {
// PotreeConverter format
customBinFormat = layer.metadata.pointAttributes === 'CIN';
bbox = new THREE.Box3(
new THREE.Vector3(cloud.boundingBox.lx, cloud.boundingBox.ly, cloud.boundingBox.lz),
new THREE.Vector3(cloud.boundingBox.ux, cloud.boundingBox.uy, cloud.boundingBox.uz));

// do we have normal information
const normal = Array.isArray(layer.metadata.pointAttributes) &&
layer.metadata.pointAttributes.find(elem => elem.startsWith('NORMAL'));
if (normal) {
layer.material.defines[normal] = 1;
}
} else {
// Lopocs
layer.metadata.scale = 1;
layer.metadata.octreeDir = `itowns/${layer.table}.points`;
layer.metadata.hierarchyStepSize = 1000000; // ignore this with lopocs
customBinFormat = true;

let idx = 0;
for (const entry of cloud) {
if (entry.table == layer.table) {
break;
}
idx++;
}
bbox = new THREE.Box3(
new THREE.Vector3(cloud[idx].bbox.xmin, cloud[idx].bbox.ymin, cloud[idx].bbox.zmin),
new THREE.Vector3(cloud[idx].bbox.xmax, cloud[idx].bbox.ymax, cloud[idx].bbox.zmax));
}

layer.parse = customBinFormat ? PotreeCinParser.parse : PotreeBinParser.parse;
layer.extension = customBinFormat ? 'cin' : 'bin';
layer.supportsProgressiveDisplay = customBinFormat;

return parseOctree(
layer,
layer.metadata.hierarchyStepSize,
{ baseurl: `${layer.url}/${cloud.octreeDir}/r`, name: '', bbox });
}).then((root) => {
// eslint-disable-next-line no-console
console.log('LAYER metadata:', root);
layer.root = root;
root.findChildrenByName = findChildrenByName.bind(root, root);

return layer;
});
return Fetcher.json(`${layer.url}/${layer.file}`, layer.fetchOptions)
.then(metadata => parseMetadata(layer, metadata))
.then(bbox => parseOctree(layer, layer.metadata.hierarchyStepSize, { baseurl: `${layer.url}/${layer.metadata.octreeDir}/r`, name: '', bbox }))
.then((root) => {
// eslint-disable-next-line no-console
console.log('LAYER metadata:', root);
layer.root = root;
root.findChildrenByName = findChildrenByName.bind(root, root);

return layer;
});
},

executeCommand(command) {
Expand Down Expand Up @@ -269,3 +271,7 @@ export default {
});
},
};

export const _testing = {
parseMetadata,
};
53 changes: 53 additions & 0 deletions test/pointcloudprovider_unit_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* global describe, it */
import assert from 'assert';
import { _testing } from '../src/Provider/PointCloudProvider';


describe('PointCloudProvider', function () {
it('should correctly parse normal information in metadata', function () {
const layer = {
material: { defines: {} }
};

// no normals
const metadata = {
boundingBox: {
lx: 0,
ly: 1,
ux: 2,
uy: 3,
},
scale: 1.0,
pointAttributes: ['POSITION', 'RGB'],
};

let result = _testing.parseMetadata(layer, metadata);
const normalDefined = layer.material.defines['NORMAL'] || layer.material.defines['NORMAL_SPHEREMAPPED'] || layer.material.defines['NORMAL_OCT16'];
assert.ok(!normalDefined);

// normals as vector
layer.material = { defines: {} };
metadata.pointAttributes = ['POSITION', 'NORMAL', 'CLASSIFICATION'];
result = _testing.parseMetadata(layer, metadata);
assert.ok(layer.material.defines['NORMAL']);
assert.ok(!layer.material.defines['NORMAL_SPHEREMAPPED']);
assert.ok(!layer.material.defines['NORMAL_OCT16']);

// spheremapped normals
layer.material = { defines: {} };
metadata.pointAttributes = ['POSITION', 'COLOR_PACKED', 'NORMAL_SPHEREMAPPED'];
result = _testing.parseMetadata(layer, metadata);
assert.ok(!layer.material.defines['NORMAL']);
assert.ok(layer.material.defines['NORMAL_SPHEREMAPPED']);
assert.ok(!layer.material.defines['NORMAL_OCT16']);

// oct16 normals
layer.material = { defines: {} };
metadata.pointAttributes = ['POSITION', 'COLOR_PACKED', 'CLASSIFICATION', 'NORMAL_OCT16'];
result = _testing.parseMetadata(layer, metadata);
assert.ok(!layer.material.defines['NORMAL']);
assert.ok(!layer.material.defines['NORMAL_SPHEREMAPPED']);
assert.ok(layer.material.defines['NORMAL_OCT16']);
});
});

0 comments on commit ea780e8

Please sign in to comment.