Skip to content

Commit

Permalink
Merge branch 'feature/geom-qe' into develop
Browse files Browse the repository at this point in the history
* feature/geom-qe:
  docs(geom): update readme
  feat(geom-voronoi): re-import & update QE delaunay/voronoi pkg (MBP2010)
  feat(quad-edge): re-import & update quad edge impl (MBP2010)
  refactor(geom): minor update clippedLine()
  refactor(geom-clip): minor update liangBarsky
  refactor(geom): swap Group ctor & factory arg order
  build(geom): update geom-clip dep & refs
  build(geom-clip): rename pkg geom-clip-convex => geom-clip, update deps
  refactor(geom-tessellate): update imports / deps
  feat(math): add simplifyRatio()
  refactor(geom): update pointInside & classifyPoint impls (delegate)
  feat(geom-poly-utils): add convexity(), remove obsolete/migrated point checks
  feat(geom-api): re-add Convexity enum
  feat(geom-isec): migrate point intersection/containment checks
  minor(geom-closest-point): minor updates
  docs(vectors): update docstrings
  • Loading branch information
postspectacular committed Jan 29, 2019
2 parents e2047df + b11837a commit 613ffeb
Show file tree
Hide file tree
Showing 53 changed files with 1,508 additions and 232 deletions.
9 changes: 9 additions & 0 deletions packages/geom-api/src/convex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Polygon convexity classifier.
*/
export const enum Convexity {
ILLEGAL = -1,
COLINEAR = 0,
CONVEX,
CONCAVE,
}
1 change: 1 addition & 0 deletions packages/geom-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./accel";
export * from "./convex";
export * from "./isec";
export * from "./path";
export * from "./sample";
Expand Down
75 changes: 0 additions & 75 deletions packages/geom-clip-convex/src/liang-barsky.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# @thi.ng/geom-clip-convex
# @thi.ng/geom-clip

[![npm (scoped)](https://img.shields.io/npm/v/@thi.ng/geom-clip-convex.svg)](https://www.npmjs.com/package/@thi.ng/geom-clip-convex)
![npm downloads](https://img.shields.io/npm/dm/@thi.ng/geom-clip-convex.svg)
[![npm (scoped)](https://img.shields.io/npm/v/@thi.ng/geom-clip.svg)](https://www.npmjs.com/package/@thi.ng/geom-clip)
![npm downloads](https://img.shields.io/npm/dm/@thi.ng/geom-clip.svg)
[![Twitter Follow](https://img.shields.io/twitter/follow/thing_umbrella.svg?style=flat-square&label=twitter)](https://twitter.com/thing_umbrella)

This project is part of the
Expand All @@ -25,7 +25,7 @@ TODO...
## Installation

```bash
yarn add @thi.ng/geom-clip-convex
yarn add @thi.ng/geom-clip
```

## Dependencies
Expand All @@ -35,7 +35,7 @@ yarn add @thi.ng/geom-clip-convex
## Usage examples

```ts
import * as gc from "@thi.ng/geom-clip-convex";
import * as gc from "@thi.ng/geom-clip";
```

## Authors
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@thi.ng/geom-clip-convex",
"name": "@thi.ng/geom-clip",
"version": "0.0.1",
"description": "2D line & convex polygon clipping (Liang-Barsky / Sutherland-Hodgeman)",
"module": "./index.js",
Expand All @@ -10,7 +10,7 @@
"type": "git",
"url": "https://github.com/thi-ng/umbrella.git"
},
"homepage": "https://github.com/thi-ng/umbrella/tree/master/packages/geom-clip-convex",
"homepage": "https://github.com/thi-ng/umbrella/tree/master/packages/geom-clip",
"author": "Karsten Schmidt <k+npm@thi.ng>",
"license": "Apache-2.0",
"scripts": {
Expand All @@ -33,6 +33,7 @@
},
"dependencies": {
"@thi.ng/geom-isec": "^0.0.1",
"@thi.ng/geom-poly-utils": "^0.0.1",
"@thi.ng/math": "^1.0.1",
"@thi.ng/vectors": "^2.1.0"
},
Expand Down
File renamed without changes.
78 changes: 78 additions & 0 deletions packages/geom-clip/src/liang-barsky.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { EPS } from "@thi.ng/math";
import { Vec } from "@thi.ng/vectors";

/**
* Performs Liang-Barsky clipping of the line segment with endpoints
* `a`, `b` against the clipping rect defined by `min` and `max`. The
* optional `ca` and `cb` vectors can be given to store the result
* (clipped points). If omitted creates new vectors. Returns a tuple of
* `[ca, cb, a, b]`, where the latter two values represent the
* normalized distances of the clipped points relative to original given
* line segment. Returns `undefined` iff the line lies completely
* outside the rect.
*
* https://en.wikipedia.org/wiki/Liang%E2%80%93Barsky_algorithm
* https://github.com/thi-ng/c-thing/blob/master/src/geom/clip/liangbarsky.c
*
* @param a
* @param b
* @param min
* @param max
* @param ca
* @param cb
*/
export const liangBarsky2 = (
a: Vec,
b: Vec,
min: Vec,
max: Vec,
ca?: Vec,
cb?: Vec
): [Vec, Vec, number, number] => {
const ax = a[0];
const ay = a[1];
const dx = b[0] - ax;
const dy = b[1] - ay;
let alpha = 0;
let beta = 1;

const clip = (p: number, q: number) => {
if (q < 0 && Math.abs(p) < EPS) {
return false;
}
const r = q / p;
if (p < 0) {
if (r > beta) {
return false;
} else if (r > alpha) {
alpha = r;
}
} else {
if (r < alpha) {
return false;
} else if (r < beta) {
beta = r;
}
}
return true;
};

if (!(
clip(-dx, -(min[0] - ax)) &&
clip(dx, max[0] - ax) &&
clip(-dy, -(min[1] - ay)) &&
clip(dy, max[1] - ay)
)) {
return;
}

!ca && (ca = []);
!cb && (cb = []);

ca[0] = alpha * dx + ax;
ca[1] = alpha * dy + ay;
cb[0] = beta * dx + ax;
cb[1] = beta * dy + ay;

return [ca, cb, alpha, beta];
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { intersectLineLine } from "@thi.ng/geom-isec";
import { EPS, sign } from "@thi.ng/math";
import { ReadonlyVec, signedArea2 } from "@thi.ng/vectors";
import { centroid } from "@thi.ng/geom-poly-utils";
import { EPS } from "@thi.ng/math";
import { corner2, ReadonlyVec } from "@thi.ng/vectors";

/**
* Extended version of Sutherland-Hodgeman convex polygon clipping
* supporting any convex boundary (not only rects). Returns new array of
* clipped vertices.
* supporting any convex boundary polygon (not only rects). Returns new
* array of clipped vertices.
*
* https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm
*
Expand All @@ -15,17 +16,18 @@ import { ReadonlyVec, signedArea2 } from "@thi.ng/vectors";
* @param eps edge classification tolerance
*/
export const sutherlandHodgeman =
(pts: ReadonlyVec[], bounds: ReadonlyVec[], bc: ReadonlyVec, eps = 1e-4) => {
(pts: ReadonlyVec[], bounds: ReadonlyVec[], bc?: ReadonlyVec, eps = EPS) => {
bc = bc || centroid(bounds);
for (let ne = bounds.length, j = ne - 1, i = 0; i < ne; j = i, i++) {
const clipped = [];
const ca = bounds[j];
const cb = bounds[i];
const sign = corner(ca, cb, bc, eps);
const sign = corner2(ca, cb, bc, eps);
for (let np = pts.length, k = np - 1, l = 0; l < np; k = l, l++) {
const p = pts[k];
const q = pts[l];
const cqsign = corner(ca, cb, q, eps);
if (corner(ca, cb, p, eps) === sign) {
const cqsign = corner2(ca, cb, q, eps);
if (corner2(ca, cb, p, eps) === sign) {
clipped.push(
cqsign !== sign ?
intersectLineLine(ca, cb, p, q).isec :
Expand All @@ -43,6 +45,3 @@ export const sutherlandHodgeman =
return pts;
};

const corner =
(a: ReadonlyVec, b: ReadonlyVec, c: ReadonlyVec, eps = EPS) =>
sign(signedArea2(a, b, c), eps);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import * as assert from "assert";
// import * as gc from "../src/index";

describe("geom-clipconvex", () => {
describe("geom-clip", () => {
it("tests pending");
});
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 613ffeb

Please sign in to comment.