|
| 1 | +import * as THREE from 'three'; |
| 2 | +import assert from 'assert'; |
| 3 | +import GlobeView from 'Core/Prefab/GlobeView'; |
| 4 | +import FeatureGeometryLayer from 'Layer/FeatureGeometryLayer'; |
| 5 | +import FileSource from 'Source/FileSource'; |
| 6 | +import HttpsProxyAgent from 'https-proxy-agent'; |
| 7 | +import Extent from 'Core/Geographic/Extent'; |
| 8 | +import Coordinates from 'Core/Geographic/Coordinates'; |
| 9 | +import OBB from 'Renderer/OBB'; |
| 10 | +import TileMesh from 'Core/TileMesh'; |
| 11 | +import Renderer from './bootstrap'; |
| 12 | + |
| 13 | +const geojson_big = require('../data/geojson/map_big.geojson.json'); |
| 14 | +const geojson_a = require('../data/geojson/map.geojson.json'); |
| 15 | +const geojson_small = require('../data/geojson/map_small.geojson.json'); |
| 16 | + |
| 17 | +const files = [geojson_small, geojson_a, geojson_big]; |
| 18 | +const errors = [3e-4, 5e-2, 35]; |
| 19 | +const sizes = [70, 600, 20000]; |
| 20 | + |
| 21 | +files.forEach((geojson, i) => { |
| 22 | + describe(`Feature2Mesh, difference between proj4 and without proj4, for ${sizes[i]} meters extent dimension `, function () { |
| 23 | + const renderer = new Renderer(); |
| 24 | + const max_error = errors[i]; |
| 25 | + |
| 26 | + const placement = { coord: new Coordinates('EPSG:4326', 4.2, 48.2), range: 2000 }; |
| 27 | + const viewer = new GlobeView(renderer.domElement, placement, { renderer }); |
| 28 | + |
| 29 | + const source = new FileSource({ |
| 30 | + fetchedData: geojson, |
| 31 | + crs: 'EPSG:4326', |
| 32 | + format: 'application/json', |
| 33 | + networkOptions: process.env.HTTPS_PROXY ? { agent: new HttpsProxyAgent(process.env.HTTPS_PROXY) } : {}, |
| 34 | + }); |
| 35 | + |
| 36 | + const source2 = new FileSource(source); |
| 37 | + |
| 38 | + const layerProj4 = new FeatureGeometryLayer('proj4', { |
| 39 | + source, |
| 40 | + accurate: true, |
| 41 | + zoom: { min: 9 }, |
| 42 | + }); |
| 43 | + |
| 44 | + const layerNoProj4 = new FeatureGeometryLayer('layerNoProj4', { |
| 45 | + source: source2, |
| 46 | + accurate: false, |
| 47 | + zoom: { min: 9 }, |
| 48 | + }); |
| 49 | + |
| 50 | + const context = { |
| 51 | + camera: viewer.camera, |
| 52 | + engine: viewer.mainLoop.gfxEngine, |
| 53 | + scheduler: viewer.mainLoop.scheduler, |
| 54 | + geometryLayer: layerProj4, |
| 55 | + view: viewer, |
| 56 | + }; |
| 57 | + |
| 58 | + const extent = new Extent('EPSG:4326', 4.1, 4.3, 48.1, 48.3); |
| 59 | + const geom = new THREE.BufferGeometry(); |
| 60 | + geom.OBB = new OBB(new THREE.Vector3(), new THREE.Vector3(1, 1, 1)); |
| 61 | + const tile = new TileMesh(geom, new THREE.Material(), viewer.tileLayer, extent, 9); |
| 62 | + tile.parent = {}; |
| 63 | + |
| 64 | + viewer.addLayer(layerProj4); |
| 65 | + viewer.addLayer(layerNoProj4); |
| 66 | + |
| 67 | + it('update proj4', function (done) { |
| 68 | + layerProj4.whenReady.then(() => { |
| 69 | + tile.visible = true; |
| 70 | + layerProj4.update(context, layerProj4, tile) |
| 71 | + .then(() => { |
| 72 | + assert.equal(layerProj4.object3d.children.length, 1); |
| 73 | + done(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('update without proj4', function (done) { |
| 79 | + layerNoProj4.whenReady.then(() => { |
| 80 | + tile.visible = true; |
| 81 | + context.layer = layerNoProj4; |
| 82 | + layerNoProj4.update(context, layerNoProj4, tile) |
| 83 | + .then(() => { |
| 84 | + assert.equal(layerNoProj4.object3d.children.length, 1); |
| 85 | + done(); |
| 86 | + }); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it(`parsing error without proj4 should be inferior to ${max_error} meter`, function (done) { |
| 91 | + Promise.all([layerNoProj4.whenReady, layerProj4.whenReady]).then(() => { |
| 92 | + const meshNoProj4 = layerNoProj4.object3d.children[0].meshesCollection.children[0]; |
| 93 | + const mesh = layerProj4.object3d.children[0].meshesCollection.children[0]; |
| 94 | + const array = mesh.geometry.attributes.position.array; |
| 95 | + const arrayNoProj4 = meshNoProj4.geometry.attributes.position.array; |
| 96 | + const vectorNoProj4 = new THREE.Vector3(); |
| 97 | + const vectorProj4 = new THREE.Vector3(); |
| 98 | + let error = 0; |
| 99 | + |
| 100 | + for (var i = array.length - 3; i >= 0; i -= 3) { |
| 101 | + // transform proj4 vertex to final projection |
| 102 | + vectorProj4.fromArray(array, i); |
| 103 | + vectorProj4.applyMatrix4(mesh.matrixWorld); |
| 104 | + |
| 105 | + // transform proj4 vertex to final projection |
| 106 | + vectorNoProj4.fromArray(arrayNoProj4, i); |
| 107 | + vectorNoProj4.applyMatrix4(meshNoProj4.matrixWorld); |
| 108 | + |
| 109 | + // compute diff between proj4 vertex and no proj4 vertex |
| 110 | + const distance = vectorProj4.distanceTo(vectorNoProj4); |
| 111 | + error += distance; |
| 112 | + } |
| 113 | + |
| 114 | + error /= (array.length / 3); |
| 115 | + |
| 116 | + assert.ok(error < max_error); |
| 117 | + done(); |
| 118 | + }); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments