forked from CesiumGS/cesium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoplanarPolygonGeometryLibrary.js
132 lines (114 loc) · 3.75 KB
/
CoplanarPolygonGeometryLibrary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import Cartesian2 from "./Cartesian2.js";
import Cartesian3 from "./Cartesian3.js";
import Check from "./Check.js";
import Matrix3 from "./Matrix3.js";
import OrientedBoundingBox from "./OrientedBoundingBox.js";
/**
* @private
*/
const CoplanarPolygonGeometryLibrary = {};
const scratchIntersectionPoint = new Cartesian3();
const scratchXAxis = new Cartesian3();
const scratchYAxis = new Cartesian3();
const scratchZAxis = new Cartesian3();
const obbScratch = new OrientedBoundingBox();
CoplanarPolygonGeometryLibrary.validOutline = function (positions) {
//>>includeStart('debug', pragmas.debug);
Check.defined("positions", positions);
//>>includeEnd('debug');
const orientedBoundingBox = OrientedBoundingBox.fromPoints(
positions,
obbScratch
);
const halfAxes = orientedBoundingBox.halfAxes;
const xAxis = Matrix3.getColumn(halfAxes, 0, scratchXAxis);
const yAxis = Matrix3.getColumn(halfAxes, 1, scratchYAxis);
const zAxis = Matrix3.getColumn(halfAxes, 2, scratchZAxis);
const xMag = Cartesian3.magnitude(xAxis);
const yMag = Cartesian3.magnitude(yAxis);
const zMag = Cartesian3.magnitude(zAxis);
// If all the points are on a line return undefined because we can't draw a polygon
return !(
(xMag === 0 && (yMag === 0 || zMag === 0)) ||
(yMag === 0 && zMag === 0)
);
};
// call after removeDuplicates
CoplanarPolygonGeometryLibrary.computeProjectTo2DArguments = function (
positions,
centerResult,
planeAxis1Result,
planeAxis2Result
) {
//>>includeStart('debug', pragmas.debug);
Check.defined("positions", positions);
Check.defined("centerResult", centerResult);
Check.defined("planeAxis1Result", planeAxis1Result);
Check.defined("planeAxis2Result", planeAxis2Result);
//>>includeEnd('debug');
const orientedBoundingBox = OrientedBoundingBox.fromPoints(
positions,
obbScratch
);
const halfAxes = orientedBoundingBox.halfAxes;
const xAxis = Matrix3.getColumn(halfAxes, 0, scratchXAxis);
const yAxis = Matrix3.getColumn(halfAxes, 1, scratchYAxis);
const zAxis = Matrix3.getColumn(halfAxes, 2, scratchZAxis);
const xMag = Cartesian3.magnitude(xAxis);
const yMag = Cartesian3.magnitude(yAxis);
const zMag = Cartesian3.magnitude(zAxis);
const min = Math.min(xMag, yMag, zMag);
// If all the points are on a line return undefined because we can't draw a polygon
if (
(xMag === 0 && (yMag === 0 || zMag === 0)) ||
(yMag === 0 && zMag === 0)
) {
return false;
}
let planeAxis1;
let planeAxis2;
if (min === yMag || min === zMag) {
planeAxis1 = xAxis;
}
if (min === xMag) {
planeAxis1 = yAxis;
} else if (min === zMag) {
planeAxis2 = yAxis;
}
if (min === xMag || min === yMag) {
planeAxis2 = zAxis;
}
Cartesian3.normalize(planeAxis1, planeAxis1Result);
Cartesian3.normalize(planeAxis2, planeAxis2Result);
Cartesian3.clone(orientedBoundingBox.center, centerResult);
return true;
};
function projectTo2D(position, center, axis1, axis2, result) {
const v = Cartesian3.subtract(position, center, scratchIntersectionPoint);
const x = Cartesian3.dot(axis1, v);
const y = Cartesian3.dot(axis2, v);
return Cartesian2.fromElements(x, y, result);
}
CoplanarPolygonGeometryLibrary.createProjectPointsTo2DFunction = function (
center,
axis1,
axis2
) {
return function (positions) {
const positionResults = new Array(positions.length);
for (let i = 0; i < positions.length; i++) {
positionResults[i] = projectTo2D(positions[i], center, axis1, axis2);
}
return positionResults;
};
};
CoplanarPolygonGeometryLibrary.createProjectPointTo2DFunction = function (
center,
axis1,
axis2
) {
return function (position, result) {
return projectTo2D(position, center, axis1, axis2, result);
};
};
export default CoplanarPolygonGeometryLibrary;