Commit e084fa93 authored by amandaghassaei's avatar amandaghassaei
Browse files

fold format import

parent 1ebe7053
......@@ -108,7 +108,7 @@ function initControls(globals){
}
});
$("#fileSelector").change(function(e){
$("#fileSelector").change(function(e) {
var files = e.target.files; // FileList object
if (files.length < 1) {
console.warn("no files");
......@@ -118,24 +118,32 @@ function initControls(globals){
var file = files[0];
var name = file.name;
var extension = name.split(".");
extension = extension[extension.length-1];
extension = extension[extension.length - 1];
var reader = new FileReader();
if (extension == "txt"){
reader.onload = function(){
return function(e) {
if (extension == "txt") {
reader.onload = function () {
return function (e) {
if (!reader.result) return;
parseTXTjson(JSON.parse(reader.result));
}
}(file);
reader.readAsText(file);
} else if (extension == "svg"){
reader.onload = function(){
return function(e) {
} else if (extension == "svg") {
reader.onload = function () {
return function (e) {
globals.pattern.loadSVG(e.target.result);
}
}(file);
reader.readAsDataURL(file);
} else if (extension == "fold"){
reader.onload = function () {
return function (e) {
if (!reader.result) return;
parseFoldJSON(JSON.parse(reader.result));
}
}(file);
reader.readAsText(file);
} else {
console.warn("unknown extension: " + extension);
return null;
......@@ -143,11 +151,94 @@ function initControls(globals){
});
function parseFoldJSON(json){
_.each(json.vertices_coords, function(vertex, i){
json.vertices_coords[i] = new THREE.Vector3(vertex[0], vertex[1], vertex[2]);
});
var faceEdges = [];
_.each(json.faces_vertices, function(face){
var thisFaceEdge = [];
for (var i=0;i<face.length;i++){
thisFaceEdge.push(null);
}
for (var i=0;i<json.edges_vertices.length;i++){
var index1 = face.indexOf(json.edges_vertices[i][0]);
if (index1 >= 0){
var index2 = face.indexOf(json.edges_vertices[i][1]);
if (index2 >= 0){
for (var j=0;j<face.length;j++){
var nextJ = j+1;
if (nextJ == face.length) nextJ = 0;
if ((index1==j && index2 ==nextJ) || (index1==nextJ && index2 ==j)) thisFaceEdge[j] = i;
}
}
}
}
faceEdges.push(thisFaceEdge);
face.push(face[0]);
});
var faces = globals.pattern.triangulatePolys([json.faces_vertices, faceEdges], json.edges_vertices, json.vertices_coords, true);
var allCreaseParams = [];
for (var i=0;i<json.edges_vertices.length;i++){
var v1 = json.edges_vertices[i][0];
var v2 = json.edges_vertices[i][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) {//remove larger index first
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_assignment[i]){
case "B":
//outline
shouldSkip = true;
break;
case "M":
creaseParams.push(Math.PI);
break;
case "V":
creaseParams.push(-Math.PI);
break;
default:
creaseParams.push(0);
break;
}
if (!shouldSkip) allCreaseParams.push(creaseParams);
break;
}
}
}
}
}
globals.model.buildModel(faces, json.vertices_coords, json.edges_vertices, allCreaseParams);
}
function parseTXTjson(json){
_.each(json.faceNodeIndices, function(face, i){
json.faceNodeIndices[i] = new THREE.Face3(face[0], face[1], face[2]);
});
var faces = json.faceNodeIndices;
var allCreaseParams = [];
......
......@@ -315,17 +315,34 @@ function initPattern(globals){
vertices = mergedVertices;
}
function triangulatePolys(polygonData, allEdges){
function triangulatePolys(polygonData, allEdges, _vertices, shouldRotateFace){
if (_vertices === undefined) _vertices = vertices;
var polygons = polygonData[0];
var polygonEdges = polygonData[1];
var faces = [];
for (var i=0;i<polygons.length;i++){
var polyVerts = [];
for (var j=1;j<polygons[i].length;j++){
var vertex = vertices[polygons[i][j]];
polyVerts.push(vertex.x);
polyVerts.push(vertex.z);
if (shouldRotateFace){
var vecA = _vertices[polygons[i][1]].clone().sub(_vertices[polygons[i][0]]);
var vecB = _vertices[polygons[i][polygons[i].length-2]].clone().sub(_vertices[polygons[i][0]]);
var translation = _vertices[polygons[i][0]];
var normal = (vecA.cross(vecB)).normalize();
var axis = ((new THREE.Vector3(0,1,0)).cross(normal)).normalize();
var angle = -Math.acos((new THREE.Vector3(0,1,0)).dot(normal));
for (var j=1;j<polygons[i].length;j++){
var vertex = _vertices[polygons[i][j]];
vertex = (vertex.clone().sub(translation)).applyAxisAngle(axis, angle);
polyVerts.push(vertex.x);
polyVerts.push(vertex.z);
}
} else {
for (var j=1;j<polygons[i].length;j++){
var vertex = _vertices[polygons[i][j]];
polyVerts.push(vertex.x);
polyVerts.push(vertex.z);
}
}
var triangles = earcut(polyVerts);
for (var j=0;j<triangles.length;j+=3){
var face = new THREE.Face3(polygons[i][triangles[j+2]], polygons[i][triangles[j+1]], polygons[i][triangles[j]]);
......@@ -378,10 +395,11 @@ function initPattern(globals){
return faces;
}
function findPolygons(allEdges){
function findPolygons(allEdges, _vertices){
//collect all edges connected to vertices
if (_vertices === undefined) _vertices = vertices;
var vertEdges = [];
for (var i=0;i<vertices.length;i++){
for (var i=0;i<_vertices.length;i++){
vertEdges.push([]);
for (var j=0;j<allEdges.length;j++){
if (allEdges[j][0] == i) vertEdges[i].push(j);
......@@ -395,13 +413,13 @@ function initPattern(globals){
//order edges ccw
for (var i=0;i<vertEdges.length;i++){
var vertex = vertices[i];
var vertex = _vertices[i];
var thetas = [];
for (var j=0;j<vertEdges[i].length;j++){
var edgeIndex = vertEdges[i][j];
var edge;
if (allEdges[edgeIndex][0] != i) edge = vertices[allEdges[edgeIndex][0]].clone();
else edge = vertices[allEdges[edgeIndex][1]].clone();
if (allEdges[edgeIndex][0] != i) edge = _vertices[allEdges[edgeIndex][0]].clone();
else edge = _vertices[allEdges[edgeIndex][1]].clone();
edge.sub(vertex);
//find angle of each edge
......@@ -424,7 +442,7 @@ function initPattern(globals){
}
var polygons = [];
var polygonEdges = [];
for (var i=0;i<vertices.length;i++){
for (var i=0;i<_vertices.length;i++){
var edges = vertEdges[i];
for (var j=0;j<edges.length;j++){
......@@ -631,6 +649,7 @@ function initPattern(globals){
return {
loadSVG: loadSVG,
getFacesAndVerticesForEdges: getFacesAndVerticesForEdges
getFacesAndVerticesForEdges: getFacesAndVerticesForEdges,
triangulatePolys: triangulatePolys
}
}
\ No newline at end of file
......@@ -49,7 +49,7 @@ function initThreeView(globals) {
//scene.fog = new THREE.FogExp2(0xf4f4f4, 1.7);
//renderer.setClearColor(scene.fog.color);
camera.zoom = 1;
camera.zoom = 15;
camera.updateProjectionMatrix();
camera.position.x = 4000;
camera.position.y = 4000;
......
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