Commit ee7c55d0 authored by amandaghassaei's avatar amandaghassaei
Browse files

add .txt import

parent 2086f3a4
......@@ -111,13 +111,100 @@ function initControls(globals){
}
var file = files[0];
var name = file.name;
var extension = name.split(".");
extension = extension[extension.length-1];
var reader = new FileReader();
reader.onload = function(){
return function(e) {
globals.pattern.loadSVG(e.target.result);
}
}(file);
reader.readAsDataURL(file);
if (extension == "txt"){
reader.onload = function(){
return function(e) {
if (!reader.result) return;
var json = JSON.parse(reader.result);
_.each(json.faceNodeIndices, function(face, i){
json.faceNodeIndices[i] = new THREE.Face3(face[0], face[1], face[2]);
});
var faces = json.faceNodeIndices;
var allCreaseParams = [];
for (var i=0;i<json.edges.length;i++){
var v1 = json.edges[i].vertices[0];
var v2 = json.edges[i].vertices[1];
var creaseParams = [];
for (var j=0;j<faces.length;j++){
var face = faces[j];
var faceVerts = [face.a, face.b, face.c];
var v1Index = faceVerts.indexOf(v1);
if (v1Index>=0){
var v2Index = faceVerts.indexOf(v2);
if (v2Index>=0){
creaseParams.push(j);
if (v2Index>v1Index) {
faceVerts.splice(v2Index, 1);
faceVerts.splice(v1Index, 1);
} else {
faceVerts.splice(v1Index, 1);
faceVerts.splice(v2Index, 1);
}
creaseParams.push(faceVerts[0]);
if (creaseParams.length == 4) {
if (v2Index-v1Index == 1 || v2Index-v1Index == -2) {
creaseParams = [creaseParams[2], creaseParams[3], creaseParams[0], creaseParams[1]];
}
creaseParams.push(i);
var shouldSkip = false;
switch (json.edges[i].type){
case 0:
//rule lines
creaseParams.push(0);
break;
case 1:
//quad panels
creaseParams.push(null);//flag to set driven
break;
case 2:
//outline
shouldSkip = true;
break;
case 3:
//crease
creaseParams.push(Math.PI);
break;
}
if (!shouldSkip) allCreaseParams.push(creaseParams);
break;
}
}
}
}
}
_.each(json.nodes, function(node, i){
json.nodes[i] = new THREE.Vector3(node.x, node.y, node.z);
});
_.each(json.edges, function(edge, i){
json.edges[i] = [edge.vertices[0], edge.vertices[1]];
});
globals.model.buildModel(faces, json.nodes, json.edges, allCreaseParams);
}
}(file);
reader.readAsText(file);
} else if (extension == "svg"){
reader.onload = function(){
return function(e) {
globals.pattern.loadSVG(e.target.result);
}
}(file);
reader.readAsDataURL(file);
} else {
console.warn("unknown extension: " + extension);
return null;
}
});
......
......@@ -3,7 +3,7 @@
*/
function Crease(edge, face1Index, face2Index, targetTheta, type, node1, node2, index){
//type = 0 panel, 1 crease
//type = 0 panel, 1 crease, -1 = driven
//face1 corresponds to node1, face2 to node2
this.edge = edge;
......@@ -42,6 +42,7 @@ Crease.prototype.getTargetTheta = function(){
};
Crease.prototype.getK = function(){
if (this.type == -1) return 0;
var length = this.getLength();
if (this.type == 0) return globals.panelStiffness*length;
return globals.creaseStiffness*length;
......
......@@ -142,7 +142,9 @@ function initModel(globals){
var _creases = [];
for (var i=0;i<allCreaseParams.length;i++) {//allCreaseParams.length
var creaseParams = allCreaseParams[i];//face1Ind, vertInd, face2Ind, ver2Ind, edgeInd, angle
_creases.push(new Crease(_edges[creaseParams[4]], creaseParams[0], creaseParams[2], creaseParams[5], creaseParams[5]!=0, _nodes[creaseParams[1]], _nodes[creaseParams[3]], _creases.length));
var type = creaseParams[5]!=0 ? 1:0;
if (creaseParams[5] === null) type = -1;//driven
_creases.push(new Crease(_edges[creaseParams[4]], creaseParams[0], creaseParams[2], creaseParams[5], type, _nodes[creaseParams[1]], _nodes[creaseParams[3]], _creases.length));
}
globals.threeView.sceneClearModel();
......
......@@ -161,7 +161,6 @@ function initPattern(globals){
var faces = triangulatePolys(findPolygons(allEdges), allEdges);
var allCreaseParams = getFacesAndVerticesForEdges(faces, allEdges);
globals.model.buildModel(faces, vertices, allEdges, allCreaseParams);
}
......@@ -233,6 +232,11 @@ function initPattern(globals){
}
creaseParams.push(faceVerts[0]);
if (creaseParams.length == 4) {
if (v2Index-v1Index == 1 || v2Index-v1Index == -2) {
creaseParams = [creaseParams[2], creaseParams[3], creaseParams[0], creaseParams[1]];
}
creaseParams.push(i);
creaseParams.push(0);//zero target angle
allCreaseParams.push(creaseParams);
......@@ -609,6 +613,7 @@ function initPattern(globals){
}
return {
loadSVG: loadSVG
loadSVG: loadSVG,
getFacesAndVerticesForEdges: getFacesAndVerticesForEdges
}
}
\ No newline at end of file
Supports Markdown
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