Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New TriangleSplitter #194

Draft
wants to merge 75 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
dcc63b4
Add triangle graph splitter class
gkjohnson Oct 18, 2023
6c22443
Add classes
gkjohnson Oct 20, 2023
d1dda43
Merge branch 'main' into improved-tri-split
gkjohnson Jan 3, 2024
c9a6980
Updates
gkjohnson Jan 3, 2024
372466d
Fixed references
gkjohnson Jan 3, 2024
845c0e6
More utils
gkjohnson Jan 3, 2024
784bcd6
add fields
gkjohnson Jan 3, 2024
3639b60
Use the graph
gkjohnson Jan 3, 2024
6e0da7a
Fixes
gkjohnson Jan 3, 2024
16ccb1e
Fixes
gkjohnson Jan 3, 2024
c5b5a06
Add debug helper
gkjohnson Jan 3, 2024
07eacfd
rename
gkjohnson Jan 3, 2024
aad00b0
Classes
gkjohnson Jan 3, 2024
22c22bd
Get splitting working in one case
gkjohnson Jan 3, 2024
f76f68e
Add more triangle generation
gkjohnson Jan 4, 2024
889ef74
Add a swap edge
gkjohnson Jan 4, 2024
5306109
Add edge swap
gkjohnson Jan 4, 2024
74e501d
Confirm transformation
gkjohnson Jan 4, 2024
6ddbdc3
Small fixes
gkjohnson Jan 4, 2024
540b320
Add function for validating triangle state, fix added edges
gkjohnson Jan 4, 2024
663b023
bug fix
gkjohnson Jan 4, 2024
9ded1a1
Fix validation
gkjohnson Jan 4, 2024
b01457f
Fix edge swap
gkjohnson Jan 4, 2024
55ec2cf
Fix incorrect point
gkjohnson Jan 4, 2024
326aa06
Rename, update api
gkjohnson Jan 4, 2024
6a5e9e8
Fix coplanar points
gkjohnson Jan 4, 2024
013ee9f
Simplify edge swap
gkjohnson Jan 4, 2024
ab2c5fe
Fix API
gkjohnson Jan 4, 2024
3cc27e7
Rename old splitter
gkjohnson Jan 4, 2024
bc17df0
Add legacy splitter option
gkjohnson Jan 4, 2024
be9b240
Remove unused import
gkjohnson Jan 4, 2024
f99336a
comments
gkjohnson Jan 4, 2024
4e054cb
Comments
gkjohnson Jan 4, 2024
3f740ae
Update test
gkjohnson Jan 4, 2024
a3abea8
Fix simple example vertex colors without groups
gkjohnson Jan 4, 2024
a110137
Add pools
gkjohnson Jan 4, 2024
6eaef3e
Add global fields
gkjohnson Jan 4, 2024
8ec4a66
Updates
gkjohnson Jan 4, 2024
1b9fa19
Member name updates
gkjohnson Jan 4, 2024
23e40eb
Update initialize function
gkjohnson Jan 4, 2024
cd34e17
Use the pools
gkjohnson Jan 4, 2024
73278b3
small improvements
gkjohnson Jan 4, 2024
d50ab07
notes
gkjohnson Jan 4, 2024
5146263
Function rename
gkjohnson Jan 5, 2024
15c7ae5
Support parallel lines in the triangle / edge function
gkjohnson Jan 5, 2024
c12c26a
Update triangle line interscetion
gkjohnson Jan 5, 2024
13e0f20
debug example changes
gkjohnson Jan 5, 2024
421ce89
Ensure caches are reset
gkjohnson Jan 5, 2024
a3a5404
Fix edge cases
gkjohnson Jan 5, 2024
89f7fa1
Updates
gkjohnson Jan 5, 2024
a194eae
Dispose the cache data
gkjohnson Jan 5, 2024
893f493
Fix some todos
gkjohnson Jan 5, 2024
235b7b4
Fixes
gkjohnson Jan 5, 2024
e074adc
Update index
gkjohnson Jan 5, 2024
c8607cc
Lint fixes
gkjohnson Jan 5, 2024
ffd7e40
Reuse arrays
gkjohnson Jan 5, 2024
b9f7709
README and dts update
gkjohnson Jan 5, 2024
1abcd17
IMPL updates
gkjohnson Jan 5, 2024
8bba87b
Fix field name
gkjohnson Jan 5, 2024
5a99cfd
update three.js
gkjohnson Jan 5, 2024
374e7d4
Improve parallel line condition
gkjohnson Jan 5, 2024
a061997
Clean up logs
gkjohnson Jan 5, 2024
00b145e
Fix swap iterations
gkjohnson Jan 5, 2024
5a03e39
some improvements
gkjohnson Jan 5, 2024
59c54c0
Remove need for 2d transformations
gkjohnson Jan 5, 2024
5e09b97
comment
gkjohnson Jan 5, 2024
444f53d
Fix splitter
gkjohnson Jan 5, 2024
82713cb
Revert "Remove need for 2d transformations"
gkjohnson Jan 5, 2024
354ae1e
improvements
gkjohnson Jan 5, 2024
d10f2b4
Improvements
gkjohnson Jan 6, 2024
6bf56c0
Improvements
gkjohnson Jan 6, 2024
c67e7fa
Remove need for basis
gkjohnson Jan 6, 2024
9d42cd1
Remove unused fields
gkjohnson Jan 6, 2024
999ee18
Updates
gkjohnson Jan 6, 2024
8d28a2e
Add legacy splitter toggle to example
gkjohnson Jan 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/core/splitter/GraphConnection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class GraphConnection {

constructor() {

this.start = - 1;
this.end = - 1;
this.required = false;
this.triangle1 = null;
this.triangle2 = null;

}

}
9 changes: 9 additions & 0 deletions src/core/splitter/GraphTriangle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class GraphTriangle {

constructor() {

this.edges = [];

}

}
219 changes: 219 additions & 0 deletions src/core/splitter/TriangleGraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
import { Triangle, Matrix4, Line3, Plane, Vector3 } from 'three';
import { getIntersectedLine, transformToFrame } from './utils';

const EPSILON = 1e-10;

export class TriangleGraph {

constructor() {

this.points = [];
this.connections = [];
this.triangles = [];

this.initialTri = new Triangle();
this.plane = new Plane();
this.frame = new Matrix4();
this.invFrame = new Matrix4();

}

initialize( tri ) {

this.reset();

const norm = new Vector3();
const right = new Vector3();
const up = new Vector3();

tri.getNormal( norm );
right.subVectors( tri.a, tri.b ).normalize();
up.crossVectors( norm, right );

const { frame, invFrame, initialTri, points } = this;
frame.makeBasis( right, up, norm ).setPosition( tri.a );
invFrame.copy( frame ).invert();

initialTri.copy( tri );
transformToFrame( initialTri, frame );

points.push(
tri.a.clone(),
tri.b.clone(),
tri.c.clone(),
);

}

reset() {

// TODO: use a pool of objects here
this.points = [];
this.connections = [];
this.triangles = [];

}

splitBy( tri ) {

const { plane, frame, initialTri } = this;

tri = tri.clone();
transformToFrame( tri, frame );

const line = new Line3();
const hitPoint = new Vector3();
const arr = [ tri.a, tri.b, tri.c ];
const points = [];
let coplanarPoints = 0;

for ( let i = 0; i < 3; i ++ ) {

const ni = ( i + 1 ) % 3;
const p0 = arr[ i ];
const p1 = arr[ ni ];
const d0 = plane.distanceToPoint( p0 );

if ( d0 < EPSILON ) {

coplanarPoints ++;

}

line.start.copy( p0 );
line.end.copy( p1 );

if ( ! plane.intersectLine( line, hitPoint ) || hitPoint.distanceTo( p1 ) < EPSILON ) {

if ( d0 < EPSILON ) {

hitPoint.copy( p0 );

} else {

continue;

}

}

points.push( hitPoint.clone() );

}

const edges = [];
if ( coplanarPoints === 3 ) {

for ( let i = 0; i < 3; i ++ ) {

const ni = ( i + 1 ) % 3;
const p0 = arr[ i ];
const p1 = arr[ ni ];

const c0 = initialTri.containsPoint( p0 );
const c1 = initialTri.containsPoint( p1 );
if ( c0 && c1 ) {

const line = new Line3();
line.start.copy( p0 );
line.end.copy( p1 );
edges.push( line );

} else {

const result = new Line3();
const edge = new Line3();
edge.start.copy( p0 );
edge.end.copy( p1 );
if ( getIntersectedLine( edge, initialTri, result ) ) {

edges.push( result.clone() );

}

}

}

} else {

const result = new Line3();
const edge = new Line3();
edge.start.copy( points[ 0 ] );
edge.end.copy( points[ 1 ] );
if ( getIntersectedLine( edge, initialTri, result ) ) {

edges.push( result.clone() );

}

}

// deduplicate and add edges
for ( let i = 0, l = edges.length; i < l; i ++ ) {

const edge = edges[ i ];
let startIndex = this.findClosestPointIndex( edge.start );
if ( startIndex === null ) {

startIndex = points.length;
Fixed Show fixed Hide fixed
points.push( edge.start.clone() );

}

let endIndex = this.findClosestPointIndex( edge.start );
if ( endIndex === null ) {

endIndex = points.length;
Fixed Show fixed Hide fixed
points.push( edge.end.clone() );

}

}

// TODO: insert edges
// - detect crossings with other edges and swap them to insert
// - possibly need to store triangles to swap



// TODO
// - find intersection edge
// - transform tri or intersection edge into og tri frame w/ z up
// - which triangles the points line up on (or which edges they may lie on) to split and insert
// - possibly split the main triangle edges
// - triangulate the points
// - swap so the required edges are present
// - confirm on edges cross??
// OR
// - just clip into separate shapes? Use earcut to triangulate?

}

findClosestPointIndex( p ) {

const points = this.points;
let closestIndex = null;
let closestDist = Infinity;
for ( let i = 0, l = points.length; i < l; i ++ ) {

const d = p.distanceTo( points[ i ] );
if ( d < EPSILON && d < closestDist ) {

closestIndex = i;
closestDist = d;

}

}

return closestIndex;

}

forEachTriangle( cb ) {


}

}
Loading
Loading