Skip to content

Commit

Permalink
fix:ed fold reflaction bug with distance to plane
Browse files Browse the repository at this point in the history
  • Loading branch information
Georgios Kaleadis authored and Georgios Kaleadis committed Jun 11, 2017
1 parent 94c70fe commit 534e53f
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 53 deletions.
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome",
"url": "http://localhost:3000",
"webRoot": "${workspaceRoot}"
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place your settings in this file to overwrite default and user settings.
{
}
37 changes: 34 additions & 3 deletions LOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
## 170610
Ok. I feel like I am late to the party. But today I recognized that the Java Origami App has an diagnostics mode
where I can output the vertices and polygons. I discovered this when I started it the first time in IntelliJ because I finally wanted to compare the internal data to my data.
Now I will compare the results of all my methods to origian ones. I still need to set breakpoints in the Java source to see last cut polygon pairs and that all but ncie to know that the author even thought if this debug feature. But I just found a problem: It is only outputting the planar/2d vertices from the creasign pattern. I want to compare my real vertices in space. So that's the first customization I will do on the original origami code.

Results for the boat:

+ Step 1/9 (FOLD_REFLECTION) Ok that wasn't hart. Horziontal Reflect. Everything matches.
+ Step 2/9 (FOLD_REFLECTION) Bottom right corner to the center. Fien too. I only see a floating error at vertex 4.
+ Step 3/9 (FOLD_REFLECTION) Same with the left corner. Same rounding error there. Vertices Count, Polygon Indices, Everything matches.
+ Step 4/9 (FOLD_ROTATION) Yes. Same same.
+ Step 5/9 (CREASE). Creasing is on the outside border. So I didn't expect any new vertices and polygons. Let's see what happens. Creasing is changing the cut history.
+ Step 6/9 (FOLD_REFLECTION_P on index 5). here comes the difference. Well I already knew this. It's clearly visible in the UI that my app wasn't able to flip out the polygon in question. It jsut flipped everything.

Now I will examine step 5/9 and 6/9. I bet it's something with the polygon selection algorithm and maybe the innocent JS floating errors are the problem?
So what I alright knew: My selection selects everything, but it should only be vertices [5,7]. Alright let's go down the rabbit hole again. I will debug the select algorithm in both applications.

YES! Got it. I was too strict with my test if a vertex is on a plane inside the selection method.
```
//old: if (Math.abs(distance) > 0.0001) {
if (Math.abs(distance) > 1) {
selection.push(i);
break;
}
```
Now I the boat is working. Next journey will be the butterly or airplane. Both are running through with no index error but the result looks off.
Let's start with the airplane.

Ok airplane done. Wrong paper format. But I will take this as a tet and put in the other paper format.


## 170605
Another day, another bug catched 🤓
Look at this:
Expand Down Expand Up @@ -40,11 +71,11 @@ I wonder what is the *perfect* way to align my object to the camera to get the p
New day, new stupid bug. I am in the process of refactoring and want to put the cut method into a polygon class.
Process is good, it's a lot of cross testing to see if the results are the same as before without the parts ripped out of the shape class.

But I wasted an hour hunting down a bug where my vertices2d went crazy very early. I knew it
But I wasted an hour hunting down a bug where my vertices2d went crazy very early. I knew it
must be somtehing in the new Polygon class or Model Class where I manage all global vertices and polygons.
I created a new method amendVertices2d to update the 2d vertices for the creasing view in a separate function
instead of being baked in the cut method.
This method defines `v1`, `v2` and `vertex2D_1`,`vertex2D_1`. Yeah stupid naming and that was the reason for the
This method defines `v1`, `v2` and `vertex2D_1`,`vertex2D_1`. Yeah stupid naming and that was the reason for the
bug. Those are retrieved from the global vertices array with index. Same index means same vertext in 3d and 2d.
I have the methods getVertex and getVertex2d in place to do so. Guess what? I retrieved both with getVertex2d. So it went nuts
pretty quickly. I had those errors. It is just because you're not focussed enough.
Expand Down Expand Up @@ -95,7 +126,7 @@ for( let i = 0, l = polygonIndices.length; i < l) {

currentIndex and followIndex will hold the actual pointers to the vertices. To always get a pair I use modulo. But I use it on the wrong dimension. I should modulo the accessor of the `polygonIndices` array - otherwise I get some unrelated vertex not beloinging to the current polygon.

---
---
I started with the refactoring. Created a new model to hold my polygons, vertices and vertices2d. Nothing more.
I recognized that I created a polygonList to do the same before. But I rememeber that I introduced bugs with this so I just reverted everything. I must have forgotten that file. So now: More carefully working on this. My goal is to get a nice class compound to work with polygons & points. Both with indices and the actual value. I plan to try out some es6 generator patterns to easily loop over all or parts of them.

Expand Down
10 changes: 5 additions & 5 deletions src/cut-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ export class CutResult {
this.cutpolygonNodes = cutpolygonNodes;
this.newVertices = newVertices;
}

public expandIndex(baseSize) {
return object => {
if(object.added !== undefined) {
return (object) => {
if (object.added !== undefined) {
return object.added + baseSize; // expand
} else {
return object;
}
};
}

// get through all lists and search for special format the references local indices, expand them to global
public updateReferences(baseSize) {
this.newpoly1 = this.newpoly1.map(this.expandIndex(baseSize));
this.newpoly2 = this.newpoly2.map(this.expandIndex(baseSize));
this.cutpolygonNodes = this.cutpolygonNodes.map(node =>
this.cutpolygonNodes = this.cutpolygonNodes.map( (node) =>
Object.assign(node, {result: node.result.added + baseSize}
));
}
Expand Down
57 changes: 40 additions & 17 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,72 @@ const SNAP_INTERVALS = [1 / 2];

export class OrigamiModel {
public data: OrigamiGeometryData;

constructor(data = null) {
this.data = data || new OrigamiGeometryData();
}


public diagnostics() {
console.log('Vertices Count', this.data.vertices.length);
console.log('Polygon Count', this.data.polygons.length);

console.group('Vertices 2D:');
this.data.vertices2d.map((vertex: THREE.Vector3, index) => {
console.log(index + ':', vertex.x, vertex.y, vertex.z);
});
console.groupEnd();

console.group('Vertices:');
this.data.vertices.map((vertex: THREE.Vector3, index) => {
console.log(index + ':', vertex.x, vertex.y, vertex.z);
});
console.groupEnd();

console.group('Polygons:');
this.data.polygons.map((polygon: number[]) => {
console.log(polygon);
});
console.groupEnd();
}

public processCutResult(index, result) {
const countBefore = this.data.verticesCount;

result = new CutResult(result);
result.updateReferences(countBefore);

// 1. add all new vertices and update references
// to point to the correct global vertex index
this.data.addVertices(...result.newVertices);

result.cutpolygonNodes.forEach((node) => {
this.amendVertices2d(node.v1, node.v2, node.result);
});

this.data.setPolygon(index, result.newpoly1);
this.data.addPolygon(result.newpoly2);

return result.cutpolygonNodes;
}

public reset(polygons = [], vertices = [], vertices2d = []) {
this.data.polygons = polygons;
this.data.vertices = vertices;
this.data.vertices2d = vertices2d;
}

public replaceAllPolygons(polygons) {
this.data.polygons = polygons;
}

public clone() {
const polygons = this.data.polygons.concat([]);
const vertices = this.data.vertices.map((v) => v.clone());
const vertices2d = this.data.vertices2d.map((v) => v.clone());

const model = new OrigamiModel();
model.reset(polygons, vertices, vertices2d);

return model;
}

Expand Down Expand Up @@ -97,7 +120,7 @@ export class OrigamiModel {
while (index > this.data.getPolygons().length) {
this.data.addPolygon([]);
}

this.data.replacePolygon(index, tmp);

console.log('shrinkWithIndex', countBefore, '->', this.data.getPolygons().length);
Expand All @@ -106,11 +129,11 @@ export class OrigamiModel {
public getPolygonVertices(index) {
return this.data.getVerticesForPolygon(index);
}

public getPolygonVertices2d(index) {
return this.data.getVertices2dForPolygon(index);
}

public findPolygon2D(point) {
const polygons = this.data.getPolygons();
const vertices2d = this.data.getVertices2d();
Expand All @@ -129,7 +152,7 @@ export class OrigamiModel {

public getPointOnOrigami(point) {
const polygonIndex = this.findPolygon2D(point);

if (polygonIndex < 0) {
return null;
}
Expand Down Expand Up @@ -216,11 +239,11 @@ export class OrigamiModel {
const polygons = this.data.getPolygons();
return polygons;
}

// Return all raw polygons wrapped in a Polygon class to access polygon scoped methods
public getPolygonWrapped(): Polygon[] {
const polygons = this.getPolygons();

return polygons.map((indices, polygonIndex) => {
const polygon = new Polygon();
polygon.points = this.data.getVerticesForPolygon(polygonIndex);
Expand All @@ -247,7 +270,7 @@ export class OrigamiModel {
(vertex2D1.y * weight1 + vertex2D2.y * weight2) / (weight1 + weight2),
0
);

this.data.addVertex2d(vector2d);
}
}
2 changes: 1 addition & 1 deletion src/origami-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class OrigamiApp {
public test() {
const playbook = new Playbook(this.origami);
playbook.set(playbooks.pending.boat);
playbook.play(5);
playbook.play();

// plane.setFromNormalAndCoplanarPoint(new THREE.Vector3(200.0,0.0,0.0), new THREE.Vector3(200.0,0.0,0.0));
// this.origami.reflect(new THREE.Plane(new THREE.Vector3(1,0.0,0.0), 0));
Expand Down
29 changes: 17 additions & 12 deletions src/origami-shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ export class OrigamiShape {

constructor() {
this.model = new OrigamiModel();
window.addEventListener('keydown', (event) => {
if ( event.keyCode === 68 ) { // Key: D
this.model.diagnostics();
}
});
}

public resetCutHistory() {
this.cutpolygonNodes = [];
this.cutpolygonPairs = [];
Expand All @@ -46,14 +51,14 @@ export class OrigamiShape {
public cut(plane: THREE.Plane) {
const polygons = this.model.getPolygons();

polygons.forEach((polygon, index) => {
polygons.forEach((polygon, index) => {
this.cutPolygon(index, plane);
});
}

public cutPolygon(index, plane) {
const polygon = new Polygon(this.model.getPolygonVertices(index), this.model.data.getPolygon(index));

if (polygon.canCut(plane) === false) {
// console.warn('cant cut polygon #', index);
return false;
Expand All @@ -62,15 +67,15 @@ export class OrigamiShape {

this.cutpolygonPairs.push([index, this.model.getPolygons().length]);
this.lastCutPolygons.push(this.getPolygon(index));

const cutResult = polygon.cut(plane, this.cutpolygonNodes);

// this will update our overall model with new indices, vertices and polygons
const newCutPolygonNodes = this.model.processCutResult(index, cutResult);
this.cutpolygonNodes = this.cutpolygonNodes.concat(newCutPolygonNodes);
}
}

public reflect(plane) {
this.model.shrink();
this.resetCutHistory();
Expand All @@ -96,9 +101,9 @@ export class OrigamiShape {
if (polygon.indexOf(index) !== -1) {
const vertexReflected = this.reflectVertex(vertex, plane);
vertex.copy(vertexReflected);

// break the loop
return false;
return false;
}

return true;
Expand Down Expand Up @@ -166,7 +171,7 @@ export class OrigamiShape {
const v2 = vertex
.clone().sub( referencePoint )
.applyAxisAngle( axis, angle * Math.PI / 180 ).add( referencePoint );

vertex.copy(v2);
}
});
Expand All @@ -181,7 +186,7 @@ export class OrigamiShape {
const foldingpoints = this.getVertices().filter((vertex, index) => {
const distance = plane.distanceToPoint(vertex);
if (Math.abs(distance) < 0.01) {

for (let i = 0; i < selection.length; i++) {
const polygon = this.getPolygon(selection[i]);
return polygon.indexOf(index) !== -1;
Expand Down Expand Up @@ -242,7 +247,7 @@ export class OrigamiShape {
.clone().sub( referencePoint )
.applyAxisAngle( axis, angle * Math.PI / 180 )
.add( referencePoint );

vertex.copy(v2);
break;
}
Expand Down Expand Up @@ -283,7 +288,7 @@ export class OrigamiShape {
const vertex = this.getVertex(polygon[ii]);
const distance = plane.distanceToPoint(vertex);

if (Math.abs(distance) > 0.0001) {
if (Math.abs(distance) > 1) {
selection.push(i);
break;
}
Expand Down
Loading

0 comments on commit 534e53f

Please sign in to comment.