Commit c5eae2cf authored by amandaghassaei's avatar amandaghassaei

eod

parent 7a1809f5
This diff is collapsed.
......@@ -353,8 +353,11 @@
<div class="modal-content">
<div class="modal-body">
<p><b>Origami Simulator</b><br/><br/>
lakfjsdlkfjladskj
<br/>
http://www2.eng.cam.ac.uk/~sdg/preprint/5OSME.pdf
http://origami.c.u-tokyo.ac.jp/~tachi/cg/SimulationOfRigidOrigami_tachi_4OSME.pdf
https://github.com/mapbox/earcut
<br/><br/>
Built by <a href="http://www.amandaghassaei.com/" target="_blank">Amanda Ghassaei</a> as a homework assignment for <a href="http://courses.csail.mit.edu/6.849/spring17/" target="_blank">Geometric Folding Algorithms</a>.
Code available on <a href="https://github.com/amandaghassaei/OrigamiSimulator" target="_blank">Github</a>.
</p>
......
......@@ -31,7 +31,7 @@ function initGlobals(){
density: 1,
//import pattern settings
vertTol: 0.01,//vertex merge tolerange
vertTol: 0.01//vertex merge tolerange
};
......
......@@ -6,6 +6,8 @@ function initPattern(globals){
var object3D = new THREE.Object3D();
globals.threeView.sceneAddModel(object3D);
// var intersections = new THREE.Object3D();
// object3D.add(intersections);
var verticesRaw = [];//list of vertex3's
//refs to vertex indices
......@@ -46,11 +48,13 @@ function initPattern(globals){
var stroke = $(this).attr("stroke").toLowerCase();
return stroke == "#ff0000" || stroke == "#f00";
});
$mountains.css({'stroke-dasharray':'12, 6, 3, 6'});
var $valleys = $paths.filter(function(){
var stroke = $(this).attr("stroke").toLowerCase();
return stroke == "#0000ff" || stroke == "#00f";
});
$valleys.css({'stroke-dasharray':'7, 6, 7, 6'});
var $cuts = $paths.filter(function(){
var stroke = $(this).attr("stroke").toLowerCase();
......@@ -138,26 +142,144 @@ function initPattern(globals){
parsePath(_verticesRaw, _valleysRaw, $valleys);
parsePath(_verticesRaw, _cutsRaw, $cuts);
findIntersections(_verticesRaw, _outlinesRaw, _mountainsRaw, _valleysRaw);
findIntersections(_verticesRaw, _outlinesRaw, _mountainsRaw, _valleysRaw, _cutsRaw);
verticesRaw = _verticesRaw;
outlinesRaw = _outlinesRaw;
mountainsRaw = _mountainsRaw;
valleysRaw = _valleysRaw;
cutsRaw = _cutsRaw;
mergeVertices(globals.vertTol);
mergeVertices();
drawPattern();
}
function mergeVertices(tol){
vertices = verticesRaw;
outlines = outlinesRaw;
mountains = mountainsRaw;
valleys = valleysRaw;
cuts = cutsRaw;
function mergeVertices(){
vertices = verticesRaw.slice();
var tolSq = globals.vertTol*globals.vertTol;
var combined = [];
var mergedVertices = [];
var diff = 1;
for (var i=vertices.length-1;i>=0;i--){
var _combined = [];
for (var j=i-diff;j>=0;j--){
if ((vertices[i].clone().sub(vertices[j])).lengthSq()<tolSq){
_combined.push(j);
}
}
var numCombined = _combined.length;
if (numCombined>0){
_combined.push(i);
mergedVertices.push(vertices[i]);
combined.push(_combined);
for (var k=0;k<numCombined-1;k++){
vertices.splice(_combined[k], 1);
}
diff -= numCombined
}
}
console.log(combined.length);
outlines = outlinesRaw.slice();
mountains = mountainsRaw.slice();
valleys = valleysRaw.slice();
cuts = cutsRaw.slice();
removeCombinedFromSet(combined, outlines);
removeCombinedFromSet(combined, mountains);
removeCombinedFromSet(combined, valleys);
removeCombinedFromSet(combined, cuts);
vertices = mergedVertices;
}
function removeCombinedFromSet(combined, set){
for (var i=0;i<combined.length;i++){
for (var j=0;j<set.length;j++){
if (combined[i].indexOf(set[j][0]) >= 0) set[j][0] = i;
if (combined[i].indexOf(set[j][1]) >= 0) set[j][1] = i;
}
}
}
function findIntersections(_verticesRaw, _outlinesRaw, _mountainsRaw, _valleysRaw, _cutsRaw){
findIntersectionsInSets(_verticesRaw, _outlinesRaw, _mountainsRaw);
findIntersectionsInSets(_verticesRaw, _outlinesRaw, _valleysRaw);
findIntersectionsInSets(_verticesRaw, _outlinesRaw, _cutsRaw);
findIntersectionsInSets(_verticesRaw, _mountainsRaw, _valleysRaw);
findIntersectionsInSets(_verticesRaw, _mountainsRaw, _cutsRaw);
findIntersectionsInSets(_verticesRaw, _valleysRaw, _cutsRaw);
}
function findIntersectionsInSets(_verticesRaw, set1, set2){
for (var i=set1.length-1;i>=0;i--){
for (var j=set2.length-1;j>=0;j--){
var v1 = _verticesRaw[set1[i][0]];
var v2 = _verticesRaw[set1[i][1]];
var v3 = _verticesRaw[set2[j][0]];
var v4 = _verticesRaw[set2[j][1]];
var data = line_intersect(v1, v2, v3, v4);
if (data) {
var d1 = getDistFromEnd(data.t1, v1, v2);
var d2 = getDistFromEnd(data.t2, v3, v4);
if (d1 === null || d2 === null) continue;
var seg1Int = d1>globals.vertTol;
var seg2Int = d2>globals.vertTol;
if (!seg1Int && !seg2Int) continue;
var vertIndex = _verticesRaw.length;
_verticesRaw.push(data.intersection);
if (seg1Int){
set1.splice(i+1, 0, [vertIndex, set1[i][0]]);
set1.splice(i+1, 0, [vertIndex, set1[i][1]]);
set1.splice(i, 1);
i++;
if (j==0) i++;
}
if (seg2Int){
set2.splice(j+1, 0, [vertIndex, set2[j][0]]);
set2.splice(j+1, 0, [vertIndex, set2[j][1]]);
set2.splice(j, 1);
j += 2;
}
}
}
}
}
function findIntersections(_verticesRaw, _outlinesRaw, _mountainsRaw, _valleysRaw){
function getDistFromEnd(t, v1, v2){
if (t>0.5) t = 1-t;
var length = (v2.clone().sub(v1)).length();
var dist = t*length;
if (dist < -globals.vertTol) return null;
if (dist > length+globals.vertTol) return null;
return dist;
}
//http://paulbourke.net/geometry/pointlineplane/
function line_intersect(v1, v2, v3, v4) {
var x1 = v1.x;
var y1 = v1.z;
var x2 = v2.x;
var y2 = v2.z;
var x3 = v3.x;
var y3 = v3.z;
var x4 = v4.x;
var y4 = v4.z;
var ua, ub, denom = (y4 - y3)*(x2 - x1) - (x4 - x3)*(y2 - y1);
if (denom == 0) {
return null;
}
ua = ((x4 - x3)*(y1 - y3) - (y4 - y3)*(x1 - x3))/denom;
ub = ((x2 - x1)*(y1 - y3) - (y2 - y1)*(x1 - x3))/denom;
return {
intersection: new THREE.Vector3(x1 + ua*(x2 - x1), 0, y1 + ua*(y2 - y1)),
t1: ua,
t2: ub
};
}
function drawPattern(){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment