| 1 | import {sqrt} from "../math.js";
|
|---|
| 2 |
|
|---|
| 3 | // TODO Enforce positive area for exterior, negative area for interior?
|
|---|
| 4 |
|
|---|
| 5 | var X0 = 0,
|
|---|
| 6 | Y0 = 0,
|
|---|
| 7 | Z0 = 0,
|
|---|
| 8 | X1 = 0,
|
|---|
| 9 | Y1 = 0,
|
|---|
| 10 | Z1 = 0,
|
|---|
| 11 | X2 = 0,
|
|---|
| 12 | Y2 = 0,
|
|---|
| 13 | Z2 = 0,
|
|---|
| 14 | x00,
|
|---|
| 15 | y00,
|
|---|
| 16 | x0,
|
|---|
| 17 | y0;
|
|---|
| 18 |
|
|---|
| 19 | var centroidStream = {
|
|---|
| 20 | point: centroidPoint,
|
|---|
| 21 | lineStart: centroidLineStart,
|
|---|
| 22 | lineEnd: centroidLineEnd,
|
|---|
| 23 | polygonStart: function() {
|
|---|
| 24 | centroidStream.lineStart = centroidRingStart;
|
|---|
| 25 | centroidStream.lineEnd = centroidRingEnd;
|
|---|
| 26 | },
|
|---|
| 27 | polygonEnd: function() {
|
|---|
| 28 | centroidStream.point = centroidPoint;
|
|---|
| 29 | centroidStream.lineStart = centroidLineStart;
|
|---|
| 30 | centroidStream.lineEnd = centroidLineEnd;
|
|---|
| 31 | },
|
|---|
| 32 | result: function() {
|
|---|
| 33 | var centroid = Z2 ? [X2 / Z2, Y2 / Z2]
|
|---|
| 34 | : Z1 ? [X1 / Z1, Y1 / Z1]
|
|---|
| 35 | : Z0 ? [X0 / Z0, Y0 / Z0]
|
|---|
| 36 | : [NaN, NaN];
|
|---|
| 37 | X0 = Y0 = Z0 =
|
|---|
| 38 | X1 = Y1 = Z1 =
|
|---|
| 39 | X2 = Y2 = Z2 = 0;
|
|---|
| 40 | return centroid;
|
|---|
| 41 | }
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | function centroidPoint(x, y) {
|
|---|
| 45 | X0 += x;
|
|---|
| 46 | Y0 += y;
|
|---|
| 47 | ++Z0;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | function centroidLineStart() {
|
|---|
| 51 | centroidStream.point = centroidPointFirstLine;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | function centroidPointFirstLine(x, y) {
|
|---|
| 55 | centroidStream.point = centroidPointLine;
|
|---|
| 56 | centroidPoint(x0 = x, y0 = y);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | function centroidPointLine(x, y) {
|
|---|
| 60 | var dx = x - x0, dy = y - y0, z = sqrt(dx * dx + dy * dy);
|
|---|
| 61 | X1 += z * (x0 + x) / 2;
|
|---|
| 62 | Y1 += z * (y0 + y) / 2;
|
|---|
| 63 | Z1 += z;
|
|---|
| 64 | centroidPoint(x0 = x, y0 = y);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | function centroidLineEnd() {
|
|---|
| 68 | centroidStream.point = centroidPoint;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | function centroidRingStart() {
|
|---|
| 72 | centroidStream.point = centroidPointFirstRing;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | function centroidRingEnd() {
|
|---|
| 76 | centroidPointRing(x00, y00);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | function centroidPointFirstRing(x, y) {
|
|---|
| 80 | centroidStream.point = centroidPointRing;
|
|---|
| 81 | centroidPoint(x00 = x0 = x, y00 = y0 = y);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | function centroidPointRing(x, y) {
|
|---|
| 85 | var dx = x - x0,
|
|---|
| 86 | dy = y - y0,
|
|---|
| 87 | z = sqrt(dx * dx + dy * dy);
|
|---|
| 88 |
|
|---|
| 89 | X1 += z * (x0 + x) / 2;
|
|---|
| 90 | Y1 += z * (y0 + y) / 2;
|
|---|
| 91 | Z1 += z;
|
|---|
| 92 |
|
|---|
| 93 | z = y0 * x - x0 * y;
|
|---|
| 94 | X2 += z * (x0 + x);
|
|---|
| 95 | Y2 += z * (y0 + y);
|
|---|
| 96 | Z2 += z * 3;
|
|---|
| 97 | centroidPoint(x0 = x, y0 = y);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | export default centroidStream;
|
|---|