function Page(a){this.patterns=[a];this.desc="page "+(parseInt(dispatcher.pages.length)+1);this.size=a.papersizereq.s;this.height=a.papersizereq.h;this.width=a.papersizereq.w}function Frontiersegment(){this.vidx=[];this.eidx=[];this.type=this.targetguid=this.ownerguid=""}Frontiersegment.prototype.set=function(a,b){this.vidx=[];this.eidx=[];this.ownerguid=a;this.targetguid=b;this.type="";return!0};
Page.prototype.out=function(a,b,c){$("#svg7").attr("viewBox","0 0 "+this.width+" "+this.height);renderplane.innerHTML="";for(a=0;a<this.patterns.length;a++){var d=document.createElementNS("http://www.w3.org/2000/svg","g");d.setAttribute("id",this.patterns[a].guid);d.setAttribute("transform","translate("+this.patterns[a].position.x+", "+this.patterns[a].position.y+")");for(var e="fill: #ccc; stroke: #444; stroke-width: 0.1px;",f=0;f<this.patterns[a].triangles.length;f++){this.patterns[a].guid==b&&
(e="fill: #fb6; stroke: #aaa; stroke-width: 0.1px;");-1<c&&this.patterns[a].triangles[f]==c&&(e="fill: #f94; stroke: #aaa; stroke-width: 0.1px;");var g=this.patterns[a].trianglesflatcoord[f];g=g[0].c[0]+", "+g[0].c[1]+" "+g[1].c[0]+", "+g[1].c[1]+" "+g[2].c[0]+", "+g[2].c[1];var h=document.createElementNS("http://www.w3.org/2000/svg","polygon");h.setAttribute("points",g);h.setAttribute("style",e);d.appendChild(h);d.appendChild(h)}e="fill: none; stroke: none;";this.patterns[a].guid==b&&(e="fill: none; stroke: #000; stroke-width: 0.5px;");
h=this.patterns[a];g=h.nodes[h.nodes.length-1].c[0]+", "+h.nodes[h.nodes.length-1].c[1];for(f=0;f<h.nodes.length;f++)g+=" "+h.nodes[f].c[0]+", "+h.nodes[f].c[1];f=document.createElementNS("http://www.w3.org/2000/svg","polygon");f.setAttribute("points",g);f.setAttribute("style",e);d.appendChild(f);renderplane.appendChild(d)}};
Page.prototype.collisionTest=function(a){var b=a.position.x,c=a.position.y,d=a.width;a=a.height;for(var e=0;e<this.patterns.length;e++){var f=this.patterns[e].position.x,g=this.patterns[e].position.y,h=this.patterns[e].height,m=!1,k=!1;b<f+this.patterns[e].width&&b+d>f&&(m=!0);c<g+h&&c+a>g&&(k=!0);if(m&&k||5>b|5>c)return!0}return!1};
Page.prototype.addPattern=function(a){a.position.x=this.width-a.width;a.position.y=this.height-a.height;for(var b=this.collisionTest(a),c=!1;0==b;)if(c=!0,a.position.x-=0,a.position.y-=10,b=this.collisionTest(a))a.position.y+=10;for(b=this.collisionTest(a);0==b;)if(c=!0,a.position.x-=10,b=this.collisionTest(a))a.position.x+=10;return 1==c?(this.patterns.push(a),0):-1};function Dispatcher(a){this.pages=[];this.p=a}
Dispatcher.prototype.nSize=function(a){for(var b=0,c=0;c<this.pages.length;c++)this.pages[c].size==a&&b++;return b};Dispatcher.prototype.updatePatterns=function(a){this.p=a};Dispatcher.prototype.queryPages=function(a){for(var b=[],c=0;c<this.pages.length;c++){var d=a.replace(/A/i,""),e=this.pages[c].size.replace(/A/i,"");d>=e&&b.push(this.pages[c])}return b};
Dispatcher.prototype.outPageTriangle=function(a,b){for(var c=0;c<this.pages.length;c++)for(var d=0;d<this.pages[c].patterns.length;d++)for(var e=0;e<this.pages[c].patterns[d].triangles.length;e++)this.pages[c].patterns[d].triangles[e]==a&&this.pages[c].out(b,this.pages[c].patterns[d].guid)};Dispatcher.prototype.getPagepattern=function(a){if(-1!=a)for(var b=0;b<this.pages.length;b++)for(var c=0;c<this.pages[b].patterns.length;c++)if(this.pages[b].patterns[c].guid==patterns.children[a].guid)return this.pages[b]};
Dispatcher.prototype.getPageIdxPatternIdx=function(a){if(-1==a)return-1;for(var b=0;b<this.pages.length;b++)for(var c=0;c<this.pages[b].patterns.length;c++)if(this.pages[b].patterns[c].guid==patterns.children[a].guid)return b;return-1};
Dispatcher.prototype.outPattern=function(a,b){renderplane.innerHTML="";if(-1!=a){var c=patterns.findTriangleOwner(a);if(-1<c){var d=patterns.children[c];$("#svg7").attr("viewBox","0 0 "+(d.width+10)+" "+(d.height+10));c=document.createElementNS("http://www.w3.org/2000/svg","g");c.setAttribute("id",d.guid);c.setAttribute("transform","translate(0, 0)");for(var e=0;e<d.triangles.length;e++){var f=d.trianglesflatcoord[e];f=f[0].c[0]+", "+f[0].c[1]+" "+f[1].c[0]+", "+f[1].c[1]+" "+f[2].c[0]+", "+f[2].c[1];
var g=document.createElementNS("http://www.w3.org/2000/svg","polygon");g.setAttribute("points",f);d.triangles[e]==a?g.setAttribute("style","fill: #f94; stroke: #aaa;"):g.setAttribute("style","fill: #ddd; stroke: #aaa;");c.appendChild(g)}f=d.nodes[d.nodes.length-1].c[0]+", "+d.nodes[d.nodes.length-1].c[1];for(e=0;e<d.nodes.length;e++)f+=" "+d.nodes[e].c[0]+", "+d.nodes[e].c[1];d=document.createElementNS("http://www.w3.org/2000/svg","polygon");d.setAttribute("points",f);d.setAttribute("style","fill: #ccc0; stroke: #000; stroke-width: 2px;");
c.appendChild(d);renderplane.appendChild(c)}}};Dispatcher.prototype.fullDispatch=function(){this.pages.splice(0,this.pages.length);for(var a=new processedelements,b=0;b<this.p.children.length;b++){for(var c=0,d=0,e=0;e<this.p.children.length;e++)c<this.p.children[e].height&&0==a.is(e)&&(c=this.p.children[e].height,d=e);dispatcher.dispatch(this.p.children[d]);a.add(d)}};
Dispatcher.prototype.dispatch=function(a){var b=this.queryPages(a.papersizereq.s);if(1>b.length)return this.pages.push(new Page(a)),0;b=this.queryPages(a.papersizereq.s);for(var c=0;c<b.length;c++)if(-1!=b[c].addPattern(a))return 0;a.position.x=0;a.position.y=0;this.pages.push(new Page(a));return 0};Dispatcher.prototype.sortChildren=function(){return 0};function Node(a,b,c){this.sid=a;this.tid=[b];this.c=c;this.guid=uuidv4()}
Node.prototype.shareFlatTriangleWith=function(a){for(var b=0;b<a.tid.length;b++)for(var c=0;c<this.tid.length;c++)if(this.tid[c]==a.tid[b])return!0;return!1};function Pattern(a){this.triangles=[];this.trianglesflatcoord=[];this.edges=[];this.nodes=[];this.frontier=[];this.guid=uuidv4();this.targetMesh=a;this.width=this.height=0;this.position={x:0,y:0}}function Patterns(a){this.targetMesh=a;this.children=[];this.guid=uuidv4();this.freezedEdgeHistory=[];this.history=[]}
Patterns.prototype.updateTargetMesh=function(a){this.targetMesh=a;this.guid=uuidv4()};Patterns.prototype.addPattern=function(a){this.children.push(a)};Patterns.prototype.removePattern=function(a){for(var b=0;b<this.children.length;b++)this.children[b].guid==a&&this.children.splice(b,1)};Patterns.prototype.flFreezed=function(){for(var a="\nfreezed :",b=0;b<this.targetMesh.edges.length;b++)"frozen"==edgestate(this.targetMesh,b)&&(a+=" "+b);fl(a)};
function roughSizeOfObject(a){for(var b=0,c=0;c<a.length;c++)b+=a[c].length;return 8*b}
Patterns.prototype.rebuild=function(a){fl(" # rebuild "+a+" c ");$("#processing-indicator").fadeIn(20);$("#main-app-dialog-title").text("Rebuild patterns");var b=$.extend(!0,[],this.children);void 0!=a&&("frozen"==edgestate(pobj,a)?this.history.push({date:c,action:"freeze",edges:[a]}):this.history.push({date:c,action:"unfreeze",edges:[a]}));var c=new Date;this.history.push({date:c,action:"freeze",edges:[a]});c=[];if(void 0!=a){var d=this.getFrontierOwnerChildren(a);if(void 0!=d[0]){for(var e=0;e<
d.length;e++){for(var f=0;f<d[e].edges.length;f++)c.push(d[e].edges[f]);this.removePattern(d[e].guid)}"frozen"==edgestate(pobj,a)&&c.push(a);this.removePattern(d[0].guid)}else if(e=this.getEdgeOwnerChildren(a),void 0!=e.guid){for(f=0;f<e.edges.length;f++)e.edges[f]!=a&&c.push(e.edges[f]);this.removePattern(e.guid)}else c.push(a)}else for(this.children.splice(0,this.children.length),f=0;f<this.targetMesh.edges.length;f++)"frozen"==edgestate(this.targetMesh,f)&&c.push(f);a=[];e=[];for(f=0;f<this.targetMesh.edges.length;f++)"frozen"==
edgestate(this.targetMesh,f)&&e.push(f);this.freezedEdgeHistory.push($.extend(!0,[],e));fl("history size : "+roughSizeOfObject(this.freezedEdgeHistory)+" bytes");fl(this.freezedEdgeHistory);for(d=c.length;0<c.length;){fl("dispatch frozen edges "+(d+1-c.length)+" / "+d);var g=-1;for(f=0;-1==g&&f<c.length;){for(e=0;-1==g&&e<this.children.length;)g=this.children[e].addEdge(c[f]),-1!=g&&c.splice(f,1),e++;f++}-1==g&&(e=new Pattern(this.targetMesh),e.addEdge(c[0]),e.gentriangles(),this.addPattern(e),c.splice(0,
1),a.push(e))}for(f=0;f<this.targetMesh.triangles.length;f++)-1==this.findTriangleOwner(f)&&"solid"==shapestate(this.targetMesh,f)&&(e=new Pattern(this.targetMesh),e.addTriangle(f),e.genFrontiers(),this.addPattern(e),a.push(e));d=a.length;fl(d+" island(s) founded");for(f=0;f<a.length;f++)if(fl("processing "+(1+f)+" / "+d),!a[f].flatten())return this.children=$.extend(!0,[],b),$("#processing-indicator").fadeOut(200),$("#processing-fail-indicator").fadeIn(1).delay(300).fadeOut(400),fl("# rebuild error"),
!1;dispatcher.updatePatterns(this);dispatcher.fullDispatch();$("#processing-indicator").fadeOut(200);$("#processing-success-indicator").fadeIn(100).delay(200).fadeOut(400);return!0};Patterns.prototype.findTriangleOwner=function(a){for(var b=0;b<this.children.length;b++)for(var c=0;c<this.children[b].triangles.length;c++)if(this.children[b].triangles[c]==a)return b;return-1};
Patterns.prototype.getFrontierOwnerChildren=function(a){for(var b=[],c=0;c<this.children.length;c++)for(var d=0;d<this.children[c].frontier.length;d++)this.children[c].frontier[d]==a&&b.push(this.children[c]);return b};Patterns.prototype.getEdgeOwnerChildren=function(a){for(var b={},c=0;c<this.children.length;c++)for(var d=0;d<this.children[c].edges.length;d++)this.children[c].edges[d]==a&&(b=this.children[c]);return b};Patterns.prototype.unHighlight=function(){for(var a=0;a<this.children.length;a++)this.children[a].unHighlight()};
Pattern.prototype.tryToReachRatio=function(a){$.extend(!0,{},this)};Pattern.prototype.owntriangle=function(a){for(var b=0;b<this.triangles.length;b++)if(this.triangles[b]==a)return b;return-1};Pattern.prototype.highlight=function(){for(var a=0;a<this.triangles.length;a++)setshapestate(pobj,this.triangles[a],"softlight")};Pattern.prototype.unHighlight=function(){for(var a=0;a<this.triangles.length;a++)setshapestate(pobj,this.triangles[a],"solid")};
Pattern.prototype.genNodes=function(){for(var a=[],b=0;b<this.trianglesflatcoord.length;b++)for(var c=0;c<this.trianglesflatcoord[b].length;c++){for(var d=!1,e=0;e<a.length;e++)distance(a[e].c,this.trianglesflatcoord[b][c].c)<tolerance&&a[e].sid==this.trianglesflatcoord[b][c].sid&&(d=!0,a[e].tid.push(this.triangles[b]));0==d&&a.push(new Node(this.trianglesflatcoord[b][c].sid,this.triangles[b],[this.trianglesflatcoord[b][c].c[0],this.trianglesflatcoord[b][c].c[1],this.trianglesflatcoord[b][c].c[2]]))}for(e=
0;e<a.length-1;e++)b=!1,a[e].shareFlatTriangleWith(a[e+1])&&(c=!1,d=getEdgeId(this.targetMesh,a[e+1].sid,a[e].sid),-1<d&&"frozen"==edgestate(this.targetMesh,d)&&(c=!0),0==c&&(b=!0)),0==b&&(b=$.extend(!0,{},a[e+1]),a.splice(e+1,1),a.push(b),e--);this.nodes=a};
Pattern.prototype.gentriangles=function(a){if(void 0!=a)for(var b=0;b<this.targetMesh.edges[a].tri.length;b++)this.addTriangle(this.targetMesh.edges[a].tri[b]);else{for(a=0;a<this.edges.length;a++)for(b=0;b<this.targetMesh.edges[this.edges[a]].tri.length;b++)-1==this.owntriangle(this.targetMesh.edges[this.edges[a]].tri[b])&&(this.addTriangle(this.targetMesh.edges[this.edges[a]].tri[b]),setshapestate(this.targetMesh,this.targetMesh.edges[this.edges[a]].tri[b],"solid"));this.genFrontiers()}};
Pattern.prototype.addTriangle=function(a){for(var b=0;b<this.triangles.length;b++)if(this.triangles[b]==a)return-1;this.triangles.push(a);return 0};
Pattern.prototype.addEdge=function(a){if(0==this.triangles.length&&0==this.edges.length)return this.edges.push(a),0;for(var b=this.targetMesh.edges[a].tri[0],c=this.targetMesh.edges[a].tri[1],d=0;d<this.edges.length;d++)if(this.edges[d]==a)return-2;for(d=0;d<this.triangles.length;d++)if(b==this.triangles[d]|c==this.triangles[d])return this.edges.push(a),this.gentriangles(),1;return-1};
Pattern.prototype.getTriangleIndex=function(a){for(var b=0;b<this.triangles.length;b++)if(a==this.triangles[b])return b;return-1};
Pattern.prototype.smartPositioning=function(){var a,b=this.nodes;var c=-1E3;var d=1E3;var e=-1E3;var f=1E3;for(a=0;a<b.length;a++)c<b[a].c[0]&&(c=b[a].c[0]),d>b[a].c[0]&&(d=b[a].c[0]),e<b[a].c[1]&&(e=b[a].c[1]),f>b[a].c[1]&&(f=b[a].c[1]);this.translate(0-d,0-f);c=-1E3;d=1E3;e=-1E3;f=1E3;for(a=0;a<b.length;a++)c<b[a].c[0]&&(c=b[a].c[0]),d>b[a].c[0]&&(d=b[a].c[0]),e<b[a].c[1]&&(e=b[a].c[1]),f>b[a].c[1]&&(f=b[a].c[1]);this.height=e-f;this.width=c-d;$.extend(!0,{},this);for(var g=[],h=0;360>h;h++){this.rotate(1);
c=-1E3;d=1E3;e=-1E3;f=1E3;for(a=0;a<b.length;a++)c<b[a].c[0]&&(c=b[a].c[0]),d>b[a].c[0]&&(d=b[a].c[0]),e<b[a].c[1]&&(e=b[a].c[1]),f>b[a].c[1]&&(f=b[a].c[1]);a=e-f;c-=d;d=c/a;g.push(d)}h=1E4;d=-1;for(a=0;a<g.length;a++)g[a]<h&&(d=a,h=g[a]);this.rotate(1*(d+1));c=-1E3;d=1E3;e=-1E3;f=1E3;for(a=0;a<b.length;a++)c<b[a].c[0]&&(c=b[a].c[0]),d>b[a].c[0]&&(d=b[a].c[0]),e<b[a].c[1]&&(e=b[a].c[1]),f>b[a].c[1]&&(f=b[a].c[1]);a=e-f;c-=d;this.height=a;this.width=c;this.translate(0-d,0-f);.707<c/a?(this.papersizereq=
{s:"A4",w:210,h:297},210<c&&(this.papersizereq={s:"A3",w:297,h:420}),297<c&&(this.papersizereq={s:"A2",w:420,h:594}),420<c&&(this.papersizereq={s:"A1",w:594,h:840}),594<c&&(this.papersizereq={s:"A0",w:840,h:1188})):(this.papersizereq={s:"A4",w:210,h:297},297<a&&(this.papersizereq={s:"A3",w:297,h:420}),420<a&&(this.papersizereq={s:"A2",w:420,h:594}),594<a&&(this.papersizereq={s:"A1",w:594,h:840}),840<a&&(this.papersizereq={s:"A0",w:840,h:1188}))};
Pattern.prototype.translate=function(a,b){for(var c=gentmat(a,b,0),d=0;d<this.nodes.length;d++)this.nodes[d].c=applymat(c,this.nodes[d].c);for(d=0;d<this.trianglesflatcoord.length;d++)for(var e=0;e<this.trianglesflatcoord[d].length;e++)this.trianglesflatcoord[d][e].c=applymat(c,this.trianglesflatcoord[d][e].c)};
Pattern.prototype.rotate=function(a){a=genrmat(0,0,a);for(var b=0;b<this.nodes.length;b++)this.nodes[b].c=applymat(a,this.nodes[b].c);for(b=0;b<this.trianglesflatcoord.length;b++)for(var c=0;c<this.trianglesflatcoord[b].length;c++)this.trianglesflatcoord[b][c].c=applymat(a,this.trianglesflatcoord[b][c].c)};Pattern.prototype.ownFrontier=function(a){for(var b=0;b<this.frontier.length;b++)if(this.frontier[b]==a)return b;return-1};
Pattern.prototype.genFrontiers=function(){l("## PATTERNgenfrontier ##");for(var a=0;a<this.triangles.length;a++)for(var b=TRIANGLEgetedges(this.targetMesh,this.triangles[a]),c=0;c<b.length;c++)-1==this.ownFrontier(b[c])&&"frozen"!=edgestate(this.targetMesh,b[c])&&(this.frontier.push(b[c]),setedgestate(this.targetMesh,b[c],"visible"))};
Pattern.prototype.flatten=function(){var a=new Date;this.genFlattenedTrianglesCoordinates();var b=new Date;fl(" * flattening "+(b.getTime()-a.getTime())+" ms");this.assembleFlattenedTriangles();var c=new Date;fl(" * assembling "+(c.getTime()-b.getTime())+" ms");if(!this.checkFreezedEdges())return!1;b=new Date;fl(" * checking"+(b.getTime()-c.getTime())+" ms");this.genNodes();c=new Date;fl(" * gen nodes "+(c.getTime()-b.getTime())+" ms");this.smartPositioning();b=new Date;fl(" * positioning "+(b.getTime()-
c.getTime())+" ms");fl(" total : "+(b.getTime()-a.getTime())+" ms");return!0};
Pattern.prototype.genFlattenedTrianglesCoordinates=function(){for(var a=0;a<this.triangles.length;a++){var b=this.triangles[a],c=this.targetMesh.trianglesnorm[b],d=new Vector([0,0,0],[0,0,1],1);c=new Vector([0,0,0],c,1);d=geninterpmat(c,d);b=[{c:[applymatNscale(d,pobj.vertices[pobj.triangles[b][0]])[0],applymatNscale(d,pobj.vertices[pobj.triangles[b][0]])[1],0],sid:this.targetMesh.triangles[b][0]},{c:[applymatNscale(d,pobj.vertices[pobj.triangles[b][1]])[0],applymatNscale(d,pobj.vertices[pobj.triangles[b][1]])[1],
0],sid:this.targetMesh.triangles[b][1]},{c:[applymatNscale(d,pobj.vertices[pobj.triangles[b][2]])[0],applymatNscale(d,pobj.vertices[pobj.triangles[b][2]])[1],0],sid:this.targetMesh.triangles[b][2]}];this.trianglesflatcoord.push(b)}};
Pattern.prototype.assembleFlattenedTriangles=function(){var a=new processedelements;a.add(this.triangles[0]);for(var b,c=this.targetMesh,d=0;d<this.edges.length;d++){var e,f,g,h;b=c.edges[this.edges[d]].som[0];for(var m=c.edges[this.edges[d]].som[1],k=0;k<c.triangles[c.edges[this.edges[d]].tri[0]].length;k++)c.triangles[c.edges[this.edges[d]].tri[0]][k]==b&&(e=k),c.triangles[c.edges[this.edges[d]].tri[0]][k]==m&&(f=k);for(k=0;k<c.triangles[c.edges[this.edges[d]].tri[1]].length;k++)c.triangles[c.edges[this.edges[d]].tri[1]][k]==
b&&(g=k),c.triangles[c.edges[this.edges[d]].tri[1]][k]==m&&(h=k);b=this.getTriangleIndex(c.edges[this.edges[d]].tri[0]);m=this.getTriangleIndex(c.edges[this.edges[d]].tri[1]);k=vectfromvertices(this.trianglesflatcoord[b][e].c,this.trianglesflatcoord[b][f].c);var p=vectfromvertices(this.trianglesflatcoord[m][g].c,this.trianglesflatcoord[m][h].c);if(a.is(c.edges[this.edges[d]].tri[0])){k=geninterpmat(p,k);p=$.extend(!0,[],this.trianglesflatcoord[m]);for(var n=0;3>n;n++)this.trianglesflatcoord[m][n].c=
applymat(k,p[n].c);b=1}else{k=geninterpmat(k,p);p=$.extend(!0,[],this.trianglesflatcoord[b]);for(n=0;3>n;n++)this.trianglesflatcoord[b][n].c=applymat(k,p[n].c);b=0}a.add(c.edges[this.edges[d]].tri[b])}};Pattern.prototype.getFlatTriangle=function(a){for(var b=0;b<this.triangles.length;b++)if(a==this.triangles[b])return this.trianglesflatcoord[b];return null};
Pattern.prototype.checkFreezedEdges=function(){for(var a=0,b=0;b<this.edges.length;b++)this.areFlatTriangleJoined(this.edges[b],this.getFlatTriangle(this.targetMesh.edges[this.edges[b]].tri[0]),this.getFlatTriangle(this.targetMesh.edges[this.edges[b]].tri[1]))||a++;return 0==a?!0:!1};Pattern.prototype.areFlatTriangleJoined=function(a,b,c){a=!0;for(var d=0;d<b.length;d++)for(var e=0;e<c.length;e++)b[d].sid==c[e].sid&&distance(b[d].c,c[e].c)>tolerance&&(a=!1);return a}
