Skip to content

Commit

Permalink
fix(Feature2Mesh): addapt indices array type from the size of polygon
Browse files Browse the repository at this point in the history
  • Loading branch information
mgermerie authored and gchoqueux committed Aug 18, 2021
1 parent 923d10c commit 378c092
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Converter/Feature2Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { FEATURE_TYPES } from 'Core/Feature';
import { deprecatedFeature2MeshOptions } from 'Core/Deprecated/Undeprecator';

const _color = new THREE.Color();
const maxValueUint8 = Math.pow(2, 8) - 1;
const maxValueUint16 = Math.pow(2, 16) - 1;
const maxValueUint32 = Math.pow(2, 32) - 1;

function toColor(color) {
if (color) {
Expand Down Expand Up @@ -33,6 +36,16 @@ function fillBatchIdArray(batchId, batchIdArray, start, end) {
}
}

function getIntArrayFromSize(data, size) {
if (size <= maxValueUint8) {
return new Uint8Array(data);
} else if (size <= maxValueUint16) {
return new Uint16Array(data);
} else {
return new Uint32Array(data);
}
}

/**
* Convert coordinates to vertices positionned at a given altitude
*
Expand Down Expand Up @@ -172,7 +185,7 @@ function featureToLine(feature, options) {
const globals = { stroke: true };
if (feature.geometries.length > 1) {
const countIndices = (count - feature.geometries.length) * 2;
const indices = new Uint16Array(countIndices);
const indices = getIntArrayFromSize(countIndices, count);
let i = 0;
// Multi line case
for (const geometry of feature.geometries) {
Expand Down Expand Up @@ -245,8 +258,8 @@ function featureToPolygon(feature, options) {

for (const geometry of feature.geometries) {
const start = geometry.indices[0].offset;
// To avoid integer overflow with indice value (16 bits)
if (start > 0xffff) {
// To avoid integer overflow with index value (32 bits)
if (start > maxValueUint32) {
console.warn('Feature to Polygon: integer overflow, too many points in polygons');
break;
}
Expand Down Expand Up @@ -282,7 +295,7 @@ function featureToPolygon(feature, options) {
geom.setAttribute('color', new THREE.BufferAttribute(colors, 3, true));
if (batchIds) { geom.setAttribute('batchId', new THREE.BufferAttribute(batchIds, 1)); }

geom.setIndex(new THREE.BufferAttribute(new Uint16Array(indices), 1));
geom.setIndex(new THREE.BufferAttribute(getIntArrayFromSize(indices, vertices.length / 3), 1));

return new THREE.Mesh(geom, material);
}
Expand Down Expand Up @@ -374,7 +387,7 @@ function featureToExtrudedPolygon(feature, options) {
geom.setAttribute('color', new THREE.BufferAttribute(colors, 3, true));
if (batchIds) { geom.setAttribute('batchId', new THREE.BufferAttribute(batchIds, 1)); }

geom.setIndex(new THREE.BufferAttribute(new Uint16Array(indices), 1));
geom.setIndex(new THREE.BufferAttribute(getIntArrayFromSize(indices, vertices.length / 3), 1));

const mesh = new THREE.Mesh(geom, material);
return mesh;
Expand Down

0 comments on commit 378c092

Please sign in to comment.