256 lines
9.6 KiB
JavaScript
256 lines
9.6 KiB
JavaScript
|
/**
|
||
|
* Cesium - https://github.com/CesiumGS/cesium
|
||
|
*
|
||
|
* Copyright 2011-2020 Cesium Contributors
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*
|
||
|
* Columbus View (Pat. Pend.)
|
||
|
*
|
||
|
* Portions licensed separately.
|
||
|
* See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
|
||
|
*/
|
||
|
|
||
|
define(['./arrayRemoveDuplicates-65de6756', './Transforms-8b90e17c', './Matrix2-265d9610', './RuntimeError-5b082e8f', './ComponentDatatype-aad54330', './CoplanarPolygonGeometryLibrary-b973f66f', './when-4bbc8319', './GeometryAttribute-4bcb785f', './GeometryAttributes-7827a6c2', './GeometryInstance-d57564f8', './GeometryPipeline-e93f6439', './IndexDatatype-6739e544', './PolygonGeometryLibrary-e329b948', './combine-e9466e32', './WebGLConstants-508b9636', './OrientedBoundingBox-1e433348', './EllipsoidTangentPlane-f1a69a20', './AxisAlignedBoundingBox-2a0ca7ef', './IntersectionTests-596e31ec', './Plane-616c9c0a', './AttributeCompression-442278a0', './EncodedCartesian3-da8f96bc', './ArcType-fc72c06c', './EllipsoidRhumbLine-d09d563f', './PolygonPipeline-5fd67ae2'], (function (arrayRemoveDuplicates, Transforms, Matrix2, RuntimeError, ComponentDatatype, CoplanarPolygonGeometryLibrary, when, GeometryAttribute, GeometryAttributes, GeometryInstance, GeometryPipeline, IndexDatatype, PolygonGeometryLibrary, combine, WebGLConstants, OrientedBoundingBox, EllipsoidTangentPlane, AxisAlignedBoundingBox, IntersectionTests, Plane, AttributeCompression, EncodedCartesian3, ArcType, EllipsoidRhumbLine, PolygonPipeline) { 'use strict';
|
||
|
|
||
|
function createGeometryFromPositions(positions) {
|
||
|
const length = positions.length;
|
||
|
const flatPositions = new Float64Array(length * 3);
|
||
|
const indices = IndexDatatype.IndexDatatype.createTypedArray(length, length * 2);
|
||
|
|
||
|
let positionIndex = 0;
|
||
|
let index = 0;
|
||
|
|
||
|
for (let i = 0; i < length; i++) {
|
||
|
const position = positions[i];
|
||
|
flatPositions[positionIndex++] = position.x;
|
||
|
flatPositions[positionIndex++] = position.y;
|
||
|
flatPositions[positionIndex++] = position.z;
|
||
|
|
||
|
indices[index++] = i;
|
||
|
indices[index++] = (i + 1) % length;
|
||
|
}
|
||
|
|
||
|
const attributes = new GeometryAttributes.GeometryAttributes({
|
||
|
position: new GeometryAttribute.GeometryAttribute({
|
||
|
componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
|
||
|
componentsPerAttribute: 3,
|
||
|
values: flatPositions,
|
||
|
}),
|
||
|
});
|
||
|
|
||
|
return new GeometryAttribute.Geometry({
|
||
|
attributes: attributes,
|
||
|
indices: indices,
|
||
|
primitiveType: GeometryAttribute.PrimitiveType.LINES,
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* A description of the outline of a polygon composed of arbitrary coplanar positions.
|
||
|
*
|
||
|
* @alias CoplanarPolygonOutlineGeometry
|
||
|
* @constructor
|
||
|
*
|
||
|
* @param {Object} options Object with the following properties:
|
||
|
* @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
|
||
|
*
|
||
|
* @see CoplanarPolygonOutlineGeometry.createGeometry
|
||
|
*
|
||
|
* @example
|
||
|
* const polygonOutline = new Cesium.CoplanarPolygonOutlineGeometry({
|
||
|
* positions : Cesium.Cartesian3.fromDegreesArrayHeights([
|
||
|
* -90.0, 30.0, 0.0,
|
||
|
* -90.0, 30.0, 1000.0,
|
||
|
* -80.0, 30.0, 1000.0,
|
||
|
* -80.0, 30.0, 0.0
|
||
|
* ])
|
||
|
* });
|
||
|
* const geometry = Cesium.CoplanarPolygonOutlineGeometry.createGeometry(polygonOutline);
|
||
|
*/
|
||
|
function CoplanarPolygonOutlineGeometry(options) {
|
||
|
options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
|
||
|
const polygonHierarchy = options.polygonHierarchy;
|
||
|
//>>includeStart('debug', pragmas.debug);
|
||
|
RuntimeError.Check.defined("options.polygonHierarchy", polygonHierarchy);
|
||
|
//>>includeEnd('debug');
|
||
|
|
||
|
this._polygonHierarchy = polygonHierarchy;
|
||
|
this._workerName = "createCoplanarPolygonOutlineGeometry";
|
||
|
|
||
|
/**
|
||
|
* The number of elements used to pack the object into an array.
|
||
|
* @type {Number}
|
||
|
*/
|
||
|
this.packedLength =
|
||
|
PolygonGeometryLibrary.PolygonGeometryLibrary.computeHierarchyPackedLength(polygonHierarchy) + 1;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* A description of a coplanar polygon outline from an array of positions.
|
||
|
*
|
||
|
* @param {Object} options Object with the following properties:
|
||
|
* @param {Cartesian3[]} options.positions An array of positions that defined the corner points of the polygon.
|
||
|
* @returns {CoplanarPolygonOutlineGeometry}
|
||
|
*/
|
||
|
CoplanarPolygonOutlineGeometry.fromPositions = function (options) {
|
||
|
options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
|
||
|
|
||
|
//>>includeStart('debug', pragmas.debug);
|
||
|
RuntimeError.Check.defined("options.positions", options.positions);
|
||
|
//>>includeEnd('debug');
|
||
|
|
||
|
const newOptions = {
|
||
|
polygonHierarchy: {
|
||
|
positions: options.positions,
|
||
|
},
|
||
|
};
|
||
|
return new CoplanarPolygonOutlineGeometry(newOptions);
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Stores the provided instance into the provided array.
|
||
|
*
|
||
|
* @param {CoplanarPolygonOutlineGeometry} value The value to pack.
|
||
|
* @param {Number[]} array The array to pack into.
|
||
|
* @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
|
||
|
*
|
||
|
* @returns {Number[]} The array that was packed into
|
||
|
*/
|
||
|
CoplanarPolygonOutlineGeometry.pack = function (value, array, startingIndex) {
|
||
|
//>>includeStart('debug', pragmas.debug);
|
||
|
RuntimeError.Check.typeOf.object("value", value);
|
||
|
RuntimeError.Check.defined("array", array);
|
||
|
//>>includeEnd('debug');
|
||
|
|
||
|
startingIndex = when.defaultValue(startingIndex, 0);
|
||
|
|
||
|
startingIndex = PolygonGeometryLibrary.PolygonGeometryLibrary.packPolygonHierarchy(
|
||
|
value._polygonHierarchy,
|
||
|
array,
|
||
|
startingIndex
|
||
|
);
|
||
|
|
||
|
array[startingIndex] = value.packedLength;
|
||
|
|
||
|
return array;
|
||
|
};
|
||
|
|
||
|
const scratchOptions = {
|
||
|
polygonHierarchy: {},
|
||
|
};
|
||
|
/**
|
||
|
* Retrieves an instance from a packed array.
|
||
|
*
|
||
|
* @param {Number[]} array The packed array.
|
||
|
* @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
|
||
|
* @param {CoplanarPolygonOutlineGeometry} [result] The object into which to store the result.
|
||
|
* @returns {CoplanarPolygonOutlineGeometry} The modified result parameter or a new CoplanarPolygonOutlineGeometry instance if one was not provided.
|
||
|
*/
|
||
|
CoplanarPolygonOutlineGeometry.unpack = function (
|
||
|
array,
|
||
|
startingIndex,
|
||
|
result
|
||
|
) {
|
||
|
//>>includeStart('debug', pragmas.debug);
|
||
|
RuntimeError.Check.defined("array", array);
|
||
|
//>>includeEnd('debug');
|
||
|
|
||
|
startingIndex = when.defaultValue(startingIndex, 0);
|
||
|
|
||
|
const polygonHierarchy = PolygonGeometryLibrary.PolygonGeometryLibrary.unpackPolygonHierarchy(
|
||
|
array,
|
||
|
startingIndex
|
||
|
);
|
||
|
startingIndex = polygonHierarchy.startingIndex;
|
||
|
delete polygonHierarchy.startingIndex;
|
||
|
const packedLength = array[startingIndex];
|
||
|
|
||
|
if (!when.defined(result)) {
|
||
|
result = new CoplanarPolygonOutlineGeometry(scratchOptions);
|
||
|
}
|
||
|
|
||
|
result._polygonHierarchy = polygonHierarchy;
|
||
|
result.packedLength = packedLength;
|
||
|
|
||
|
return result;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Computes the geometric representation of an arbitrary coplanar polygon, including its vertices, indices, and a bounding sphere.
|
||
|
*
|
||
|
* @param {CoplanarPolygonOutlineGeometry} polygonGeometry A description of the polygon.
|
||
|
* @returns {Geometry|undefined} The computed vertices and indices.
|
||
|
*/
|
||
|
CoplanarPolygonOutlineGeometry.createGeometry = function (polygonGeometry) {
|
||
|
const polygonHierarchy = polygonGeometry._polygonHierarchy;
|
||
|
|
||
|
let outerPositions = polygonHierarchy.positions;
|
||
|
outerPositions = arrayRemoveDuplicates.arrayRemoveDuplicates(
|
||
|
outerPositions,
|
||
|
Matrix2.Cartesian3.equalsEpsilon,
|
||
|
true
|
||
|
);
|
||
|
if (outerPositions.length < 3) {
|
||
|
return;
|
||
|
}
|
||
|
const isValid = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.validOutline(outerPositions);
|
||
|
if (!isValid) {
|
||
|
return undefined;
|
||
|
}
|
||
|
|
||
|
const polygons = PolygonGeometryLibrary.PolygonGeometryLibrary.polygonOutlinesFromHierarchy(
|
||
|
polygonHierarchy,
|
||
|
false
|
||
|
);
|
||
|
|
||
|
if (polygons.length === 0) {
|
||
|
return undefined;
|
||
|
}
|
||
|
|
||
|
const geometries = [];
|
||
|
|
||
|
for (let i = 0; i < polygons.length; i++) {
|
||
|
const geometryInstance = new GeometryInstance.GeometryInstance({
|
||
|
geometry: createGeometryFromPositions(polygons[i]),
|
||
|
});
|
||
|
geometries.push(geometryInstance);
|
||
|
}
|
||
|
|
||
|
const geometry = GeometryPipeline.GeometryPipeline.combineInstances(geometries)[0];
|
||
|
const boundingSphere = Transforms.BoundingSphere.fromPoints(polygonHierarchy.positions);
|
||
|
|
||
|
return new GeometryAttribute.Geometry({
|
||
|
attributes: geometry.attributes,
|
||
|
indices: geometry.indices,
|
||
|
primitiveType: geometry.primitiveType,
|
||
|
boundingSphere: boundingSphere,
|
||
|
});
|
||
|
};
|
||
|
|
||
|
function createCoplanarPolygonOutlineGeometry(polygonGeometry, offset) {
|
||
|
if (when.defined(offset)) {
|
||
|
polygonGeometry = CoplanarPolygonOutlineGeometry.unpack(
|
||
|
polygonGeometry,
|
||
|
offset
|
||
|
);
|
||
|
}
|
||
|
polygonGeometry._ellipsoid = Matrix2.Ellipsoid.clone(polygonGeometry._ellipsoid);
|
||
|
return CoplanarPolygonOutlineGeometry.createGeometry(polygonGeometry);
|
||
|
}
|
||
|
|
||
|
return createCoplanarPolygonOutlineGeometry;
|
||
|
|
||
|
}));
|
||
|
//# sourceMappingURL=createCoplanarPolygonOutlineGeometry.js.map
|