glMatrixArrayType=typeof Float32Array!="undefined"?Float32Array:typeof WebGLFloatArray!="undefined"?WebGLFloatArray:Array;var vec3={};vec3.create=function(e){var t=new glMatrixArrayType(3);return e&&(t[0]=e[0],t[1]=e[1],t[2]=e[2]),t},vec3.set=function(e,t){return t[0]=e[0],t[1]=e[1],t[2]=e[2],t},vec3.add=function(e,t,n){return!n||e==n?(e[0]+=t[0],e[1]+=t[1],e[2]+=t[2],e):(n[0]=e[0]+t[0],n[1]=e[1]+t[1],n[2]=e[2]+t[2],n)},vec3.subtract=function(e,t,n){return!n||e==n?(e[0]-=t[0],e[1]-=t[1],e[2]-=t[2],e):(n[0]=e[0]-t[0],n[1]=e[1]-t[1],n[2]=e[2]-t[2],n)},vec3.negate=function(e,t){return t||(t=e),t[0]=-e[0],t[1]=-e[1],t[2]=-e[2],t},vec3.scale=function(e,t,n){return!n||e==n?(e[0]*=t,e[1]*=t,e[2]*=t,e):(n[0]=e[0]*t,n[1]=e[1]*t,n[2]=e[2]*t,n)},vec3.normalize=function(e,t){t||(t=e);var n=e[0],r=e[1],i=e[2],s=Math.sqrt(n*n+r*r+i*i);return s?s==1?(t[0]=n,t[1]=r,t[2]=i,t):(s=1/s,t[0]=n*s,t[1]=r*s,t[2]=i*s,t):(t[0]=0,t[1]=0,t[2]=0,t)},vec3.cross=function(e,t,n){n||(n=e);var r=e[0],i=e[1];e=e[2];var s=t[0],o=t[1];return t=t[2],n[0]=i*t-e*o,n[1]=e*s-r*t,n[2]=r*o-i*s,n},vec3.length=function(e){var t=e[0],n=e[1];return e=e[2],Math.sqrt(t*t+n*n+e*e)},vec3.dot=function(e,t){return e[0]*t[0]+e[1]*t[1]+e[2]*t[2]},vec3.direction=function(e,t,n){n||(n=e);var r=e[0]-t[0],i=e[1]-t[1];return e=e[2]-t[2],t=Math.sqrt(r*r+i*i+e*e),t?(t=1/t,n[0]=r*t,n[1]=i*t,n[2]=e*t,n):(n[0]=0,n[1]=0,n[2]=0,n)},vec3.lerp=function(e,t,n,r){return r||(r=e),r[0]=e[0]+n*(t[0]-e[0]),r[1]=e[1]+n*(t[1]-e[1]),r[2]=e[2]+n*(t[2]-e[2]),r},vec3.str=function(e){return"["+e[0]+", "+e[1]+", "+e[2]+"]"};var mat3={};mat3.create=function(e){var t=new glMatrixArrayType(9);return e&&(t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=e[3],t[4]=e[4],t[5]=e[5],t[6]=e[6],t[7]=e[7],t[8]=e[8],t[9]=e[9]),t},mat3.set=function(e,t){return t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=e[3],t[4]=e[4],t[5]=e[5],t[6]=e[6],t[7]=e[7],t[8]=e[8],t},mat3.identity=function(e){return e[0]=1,e[1]=0,e[2]=0,e[3]=0,e[4]=1,e[5]=0,e[6]=0,e[7]=0,e[8]=1,e},mat3.transpose=function(e,t){if(!t||e==t){var n=e[1],r=e[2],i=e[5];return e[1]=e[3],e[2]=e[6],e[3]=n,e[5]=e[7],e[6]=r,e[7]=i,e}return t[0]=e[0],t[1]=e[3],t[2]=e[6],t[3]=e[1],t[4]=e[4],t[5]=e[7],t[6]=e[2],t[7]=e[5],t[8]=e[8],t},mat3.toMat4=function(e,t){return t||(t=mat4.create()),t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=0,t[4]=e[3],t[5]=e[4],t[6]=e[5],t[7]=0,t[8]=e[6],t[9]=e[7],t[10]=e[8],t[11]=0,t[12]=0,t[13]=0,t[14]=0,t[15]=1,t},mat3.str=function(e){return"["+e[0]+", "+e[1]+", "+e[2]+", "+e[3]+", "+e[4]+", "+e[5]+", "+e[6]+", "+e[7]+", "+e[8]+"]"};var mat4={};mat4.create=function(e){var t=new glMatrixArrayType(16);return e&&(t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=e[3],t[4]=e[4],t[5]=e[5],t[6]=e[6],t[7]=e[7],t[8]=e[8],t[9]=e[9],t[10]=e[10],t[11]=e[11],t[12]=e[12],t[13]=e[13],t[14]=e[14],t[15]=e[15]),t},mat4.set=function(e,t){return t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=e[3],t[4]=e[4],t[5]=e[5],t[6]=e[6],t[7]=e[7],t[8]=e[8],t[9]=e[9],t[10]=e[10],t[11]=e[11],t[12]=e[12],t[13]=e[13],t[14]=e[14],t[15]=e[15],t},mat4.identity=function(e){return e[0]=1,e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[5]=1,e[6]=0,e[7]=0,e[8]=0,e[9]=0,e[10]=1,e[11]=0,e[12]=0,e[13]=0,e[14]=0,e[15]=1,e},mat4.transpose=function(e,t){if(!t||e==t){var n=e[1],r=e[2],i=e[3],s=e[6],o=e[7],u=e[11];return e[1]=e[4],e[2]=e[8],e[3]=e[12],e[4]=n,e[6]=e[9],e[7]=e[13],e[8]=r,e[9]=s,e[11]=e[14],e[12]=i,e[13]=o,e[14]=u,e}return t[0]=e[0],t[1]=e[4],t[2]=e[8],t[3]=e[12],t[4]=e[1],t[5]=e[5],t[6]=e[9],t[7]=e[13],t[8]=e[2],t[9]=e[6],t[10]=e[10],t[11]=e[14],t[12]=e[3],t[13]=e[7],t[14]=e[11],t[15]=e[15],t},mat4.determinant=function(e){var t=e[0],n=e[1],r=e[2],i=e[3],s=e[4],o=e[5],u=e[6],a=e[7],f=e[8],l=e[9],c=e[10],h=e[11],p=e[12],d=e[13],v=e[14];return e=e[15],p*l*u*i-f*d*u*i-p*o*c*i+s*d*c*i+f*o*v*i-s*l*v*i-p*l*r*a+f*d*r*a+p*n*c*a-t*d*c*a-f*n*v*a+t*l*v*a+p*o*r*h-s*d*r*h-p*n*u*h+t*d*u*h+s*n*v*h-t*o*v*h-f*o*r*e+s*l*r*e+f*n*u*e-t*l*u*e-s*n*c*e+t*o*c*e},mat4.inverse=function(e,t){t||(t=e);var n=e[0],r=e[1],i=e[2],s=e[3],o=e[4],u=e[5],a=e[6],f=e[7],l=e[8],c=e[9],h=e[10],p=e[11],d=e[12],v=e[13],m=e[14],g=e[15],y=n*u-r*o,b=n*a-i*o,w=n*f-s*o,E=r*a-i*u,S=r*f-s*u,x=i*f-s*a,T=l*v-c*d,N=l*m-h*d,C=l*g-p*d,k=c*m-h*v,L=c*g-p*v,A=h*g-p*m,O=1/(y*A-b*L+w*k+E*C-S*N+x*T);return t[0]=(u*A-a*L+f*k)*O,t[1]=(-r*A+i*L-s*k)*O,t[2]=(v*x-m*S+g*E)*O,t[3]=(-c*x+h*S-p*E)*O,t[4]=(-o*A+a*C-f*N)*O,t[5]=(n*A-i*C+s*N)*O,t[6]=(-d*x+m*w-g*b)*O,t[7]=(l*x-h*w+p*b)*O,t[8]=(o*L-u*C+f*T)*O,t[9]=(-n*L+r*C-s*T)*O,t[10]=(d*S-v*w+g*y)*O,t[11]=(-l*S+c*w-p*y)*O,t[12]=(-o*k+u*N-a*T)*O,t[13]=(n*k-r*N+i*T)*O,t[14]=(-d*E+v*b-m*y)*O,t[15]=(l*E-c*b+h*y)*O,t},mat4.toRotationMat=function(e,t){return t||(t=mat4.create()),t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=e[3],t[4]=e[4],t[5]=e[5],t[6]=e[6],t[7]=e[7],t[8]=e[8],t[9]=e[9],t[10]=e[10],t[11]=e[11],t[12]=0,t[13]=0,t[14]=0,t[15]=1,t},mat4.toMat3=function(e,t){return t||(t=mat3.create()),t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=e[4],t[4]=e[5],t[5]=e[6],t[6]=e[8],t[7]=e[9],t[8]=e[10],t},mat4.toInverseMat3=function(e,t){var n=e[0],r=e[1],i=e[2],s=e[4],o=e[5],u=e[6],a=e[8],f=e[9],l=e[10],c=l*o-u*f,h=-l*s+u*a,p=f*s-o*a,d=n*c+r*h+i*p;return d?(d=1/d,t||(t=mat3.create()),t[0]=c*d,t[1]=(-l*r+i*f)*d,t[2]=(u*r-i*o)*d,t[3]=h*d,t[4]=(l*n-i*a)*d,t[5]=(-u*n+i*s)*d,t[6]=p*d,t[7]=(-f*n+r*a)*d,t[8]=(o*n-r*s)*d,t):null},mat4.multiply=function(e,t,n){n||(n=e);var r=e[0],i=e[1],s=e[2],o=e[3],u=e[4],a=e[5],f=e[6],l=e[7],c=e[8],h=e[9],p=e[10],d=e[11],v=e[12],m=e[13],g=e[14];e=e[15];var y=t[0],b=t[1],w=t[2],E=t[3],S=t[4],x=t[5],T=t[6],N=t[7],C=t[8],k=t[9],L=t[10],A=t[11],O=t[12],M=t[13],_=t[14];return t=t[15],n[0]=y*r+b*u+w*c+E*v,n[1]=y*i+b*a+w*h+E*m,n[2]=y*s+b*f+w*p+E*g,n[3]=y*o+b*l+w*d+E*e,n[4]=S*r+x*u+T*c+N*v,n[5]=S*i+x*a+T*h+N*m,n[6]=S*s+x*f+T*p+N*g,n[7]=S*o+x*l+T*d+N*e,n[8]=C*r+k*u+L*c+A*v,n[9]=C*i+k*a+L*h+A*m,n[10]=C*s+k*f+L*p+A*g,n[11]=C*o+k*l+L*d+A*e,n[12]=O*r+M*u+_*c+t*v,n[13]=O*i+M*a+_*h+t*m,n[14]=O*s+M*f+_*p+t*g,n[15]=O*o+M*l+_*d+t*e,n},mat4.multiplyVec3=function(e,t,n){n||(n=t);var r=t[0],i=t[1];return t=t[2],n[0]=e[0]*r+e[4]*i+e[8]*t+e[12],n[1]=e[1]*r+e[5]*i+e[9]*t+e[13],n[2]=e[2]*r+e[6]*i+e[10]*t+e[14],n},mat4.multiplyVec4=function(e,t,n){n||(n=t);var r=t[0],i=t[1],s=t[2];return t=t[3],n[0]=e[0]*r+e[4]*i+e[8]*s+e[12]*t,n[1]=e[1]*r+e[5]*i+e[9]*s+e[13]*t,n[2]=e[2]*r+e[6]*i+e[10]*s+e[14]*t,n[3]=e[3]*r+e[7]*i+e[11]*s+e[15]*t,n},mat4.translate=function(e,t,n){var r=t[0],i=t[1];t=t[2];if(!n||e==n)return e[12]=e[0]*r+e[4]*i+e[8]*t+e[12],e[13]=e[1]*r+e[5]*i+e[9]*t+e[13],e[14]=e[2]*r+e[6]*i+e[10]*t+e[14],e[15]=e[3]*r+e[7]*i+e[11]*t+e[15],e;var s=e[0],o=e[1],u=e[2],a=e[3],f=e[4],l=e[5],c=e[6],h=e[7],p=e[8],d=e[9],v=e[10],m=e[11];return n[0]=s,n[1]=o,n[2]=u,n[3]=a,n[4]=f,n[5]=l,n[6]=c,n[7]=h,n[8]=p,n[9]=d,n[10]=v,n[11]=m,n[12]=s*r+f*i+p*t+e[12],n[13]=o*r+l*i+d*t+e[13],n[14]=u*r+c*i+v*t+e[14],n[15]=a*r+h*i+m*t+e[15],n},mat4.scale=function(e,t,n){var r=t[0],i=t[1];return t=t[2],!n||e==n?(e[0]*=r,e[1]*=r,e[2]*=r,e[3]*=r,e[4]*=i,e[5]*=i,e[6]*=i,e[7]*=i,e[8]*=t,e[9]*=t,e[10]*=t,e[11]*=t,e):(n[0]=e[0]*r,n[1]=e[1]*r,n[2]=e[2]*r,n[3]=e[3]*r,n[4]=e[4]*i,n[5]=e[5]*i,n[6]=e[6]*i,n[7]=e[7]*i,n[8]=e[8]*t,n[9]=e[9]*t,n[10]=e[10]*t,n[11]=e[11]*t,n[12]=e[12],n[13]=e[13],n[14]=e[14],n[15]=e[15],n)},mat4.rotate=function(e,t,n,r){var i=n[0],s=n[1];n=n[2];var o=Math.sqrt(i*i+s*s+n*n);if(!o)return null;o!=1&&(o=1/o,i*=o,s*=o,n*=o);var u=Math.sin(t),a=Math.cos(t),f=1-a;t=e[0],o=e[1];var l=e[2],c=e[3],h=e[4],p=e[5],d=e[6],v=e[7],m=e[8],g=e[9],y=e[10],b=e[11],w=i*i*f+a,E=s*i*f+n*u,S=n*i*f-s*u,x=i*s*f-n*u,T=s*s*f+a,N=n*s*f+i*u,C=i*n*f+s*u;return i=s*n*f-i*u,s=n*n*f+a,r?e!=r&&(r[12]=e[12],r[13]=e[13],r[14]=e[14],r[15]=e[15]):r=e,r[0]=t*w+h*E+m*S,r[1]=o*w+p*E+g*S,r[2]=l*w+d*E+y*S,r[3]=c*w+v*E+b*S,r[4]=t*x+h*T+m*N,r[5]=o*x+p*T+g*N,r[6]=l*x+d*T+y*N,r[7]=c*x+v*T+b*N,r[8]=t*C+h*i+m*s,r[9]=o*C+p*i+g*s,r[10]=l*C+d*i+y*s,r[11]=c*C+v*i+b*s,r},mat4.rotateX=function(e,t,n){var r=Math.sin(t);t=Math.cos(t);var i=e[4],s=e[5],o=e[6],u=e[7],a=e[8],f=e[9],l=e[10],c=e[11];return n?e!=n&&(n[0]=e[0],n[1]=e[1],n[2]=e[2],n[3]=e[3],n[12]=e[12],n[13]=e[13],n[14]=e[14],n[15]=e[15]):n=e,n[4]=i*t+a*r,n[5]=s*t+f*r,n[6]=o*t+l*r,n[7]=u*t+c*r,n[8]=i*-r+a*t,n[9]=s*-r+f*t,n[10]=o*-r+l*t,n[11]=u*-r+c*t,n},mat4.rotateY=function(e,t,n){var r=Math.sin(t);t=Math.cos(t);var i=e[0],s=e[1],o=e[2],u=e[3],a=e[8],f=e[9],l=e[10],c=e[11];return n?e!=n&&(n[4]=e[4],n[5]=e[5],n[6]=e[6],n[7]=e[7],n[12]=e[12],n[13]=e[13],n[14]=e[14],n[15]=e[15]):n=e,n[0]=i*t+a*-r,n[1]=s*t+f*-r,n[2]=o*t+l*-r,n[3]=u*t+c*-r,n[8]=i*r+a*t,n[9]=s*r+f*t,n[10]=o*r+l*t,n[11]=u*r+c*t,n},mat4.rotateZ=function(e,t,n){var r=Math.sin(t);t=Math.cos(t);var i=e[0],s=e[1],o=e[2],u=e[3],a=e[4],f=e[5],l=e[6],c=e[7];return n?e!=n&&(n[8]=e[8],n[9]=e[9],n[10]=e[10],n[11]=e[11],n[12]=e[12],n[13]=e[13],n[14]=e[14],n[15]=e[15]):n=e,n[0]=i*t+a*r,n[1]=s*t+f*r,n[2]=o*t+l*r,n[3]=u*t+c*r,n[4]=i*-r+a*t,n[5]=s*-r+f*t,n[6]=o*-r+l*t,n[7]=u*-r+c*t,n},mat4.frustum=function(e,t,n,r,i,s,o){o||(o=mat4.create());var u=t-e,a=r-n,f=s-i;return o[0]=i*2/u,o[1]=0,o[2]=0,o[3]=0,o[4]=0,o[5]=i*2/a,o[6]=0,o[7]=0,o[8]=(t+e)/u,o[9]=(r+n)/a,o[10]=-(s+i)/f,o[11]=-1,o[12]=0,o[13]=0,o[14]=-(s*i*2)/f,o[15]=0,o},mat4.perspective=function(e,t,n,r,i){return e=n*Math.tan(e*Math.PI/360),t=e*t,mat4.frustum(-t,t,-e,e,n,r,i)},mat4.ortho=function(e,t,n,r,i,s,o){o||(o=mat4.create());var u=t-e,a=r-n,f=s-i;return o[0]=2/u,o[1]=0,o[2]=0,o[3]=0,o[4]=0,o[5]=2/a,o[6]=0,o[7]=0,o[8]=0,o[9]=0,o[10]=-2/f,o[11]=0,o[12]=-(e+t)/u,o[13]=-(r+n)/a,o[14]=-(s+i)/f,o[15]=1,o},mat4.lookAt=function(e,t,n,r){r||(r=mat4.create());var i=e[0],s=e[1];e=e[2];var o=n[0],u=n[1],a=n[2];n=t[1];var f=t[2];if(i==t[0]&&s==n&&e==f)return mat4.identity(r);var l,c,h,p;return n=i-t[0],f=s-t[1],t=e-t[2],p=1/Math.sqrt(n*n+f*f+t*t),n*=p,f*=p,t*=p,l=u*t-a*f,a=a*n-o*t,o=o*f-u*n,(p=Math.sqrt(l*l+a*a+o*o))?(p=1/p,l*=p,a*=p,o*=p):o=a=l=0,u=f*o-t*a,c=t*l-n*o,h=n*a-f*l,(p=Math.sqrt(u*u+c*c+h*h))?(p=1/p,u*=p,c*=p,h*=p):h=c=u=0,r[0]=l,r[1]=u,r[2]=n,r[3]=0,r[4]=a,r[5]=c,r[6]=f,r[7]=0,r[8]=o,r[9]=h,r[10]=t,r[11]=0,r[12]=-(l*i+a*s+o*e),r[13]=-(u*i+c*s+h*e),r[14]=-(n*i+f*s+t*e),r[15]=1,r},mat4.str=function(e){return"["+e[0]+", "+e[1]+", "+e[2]+", "+e[3]+", "+e[4]+", "+e[5]+", "+e[6]+", "+e[7]+", "+e[8]+", "+e[9]+", "+e[10]+", "+e[11]+", "+e[12]+", "+e[13]+", "+e[14]+", "+e[15]+"]"},quat4={},quat4.create=function(e){var t=new glMatrixArrayType(4);return e&&(t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=e[3]),t},quat4.set=function(e,t){return t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=e[3],t},quat4.calculateW=function(e,t){var n=e[0],r=e[1],i=e[2];return!t||e==t?(e[3]=-Math.sqrt(Math.abs(1-n*n-r*r-i*i)),e):(t[0]=n,t[1]=r,t[2]=i,t[3]=-Math.sqrt(Math.abs(1-n*n-r*r-i*i)),t)},quat4.inverse=function(e,t){return!t||e==t?(e[0]*=1,e[1]*=1,e[2]*=1,e):(t[0]=-e[0],t[1]=-e[1],t[2]=-e[2],t[3]=e[3],t)},quat4.length=function(e){var t=e[0],n=e[1],r=e[2];return e=e[3],Math.sqrt(t*t+n*n+r*r+e*e)},quat4.normalize=function(e,t){t||(t=e);var n=e[0],r=e[1],i=e[2],s=e[3],o=Math.sqrt(n*n+r*r+i*i+s*s);return o==0?(t[0]=0,t[1]=0,t[2]=0,t[3]=0,t):(o=1/o,t[0]=n*o,t[1]=r*o,t[2]=i*o,t[3]=s*o,t)},quat4.multiply=function(e,t,n){n||(n=e);var r=e[0],i=e[1],s=e[2];e=e[3];var o=t[0],u=t[1],a=t[2];return t=t[3],n[0]=r*t+e*o+i*a-s*u,n[1]=i*t+e*u+s*o-r*a,n[2]=s*t+e*a+r*u-i*o,n[3]=e*t-r*o-i*u-s*a,n},quat4.multiplyVec3=function(e,t,n){n||(n=t);var r=t[0],i=t[1],s=t[2];t=e[0];var o=e[1],u=e[2];e=e[3];var a=e*r+o*s-u*i,f=e*i+u*r-t*s,l=e*s+t*i-o*r;return r=-t*r-o*i-u*s,n[0]=a*e+r*-t+f*-u-l*-o,n[1]=f*e+r*-o+l*-t-a*-u,n[2]=l*e+r*-u+a*-o-f*-t,n},quat4.toMat3=function(e,t){t||(t=mat3.create());var n=e[0],r=e[1],i=e[2],s=e[3],o=n+n,u=r+r,a=i+i,f=n*o,l=n*u;n*=a;var c=r*u;return r*=a,i*=a,o=s*o,u=s*u,s*=a,t[0]=1-(c+i),t[1]=l-s,t[2]=n+u,t[3]=l+s,t[4]=1-(f+i),t[5]=r-o,t[6]=n-u,t[7]=r+o,t[8]=1-(f+c),t},quat4.toMat4=function(e,t){t||(t=mat4.create());var n=e[0],r=e[1],i=e[2],s=e[3],o=n+n,u=r+r,a=i+i,f=n*o,l=n*u;n*=a;var c=r*u;return r*=a,i*=a,o=s*o,u=s*u,s*=a,t[0]=1-(c+i),t[1]=l-s,t[2]=n+u,t[3]=0,t[4]=l+s,t[5]=1-(f+i),t[6]=r-o,t[7]=0,t[8]=n-u,t[9]=r+o,t[10]=1-(f+c),t[11]=0,t[12]=0,t[13]=0,t[14]=0,t[15]=1,t},quat4.slerp=function(e,t,n,r){r||(r=e);var i=n;return e[0]*t[0]+e[1]*t[1]+e[2]*t[2]+e[3]*t[3]<0&&(i=-1*n),r[0]=1-n*e[0]+i*t[0],r[1]=1-n*e[1]+i*t[1],r[2]=1-n*e[2]+i*t[2],r[3]=1-n*e[3]+i*t[3],r},quat4.str=function(e){return"["+e[0]+", "+e[1]+", "+e[2]+", "+e[3]+"]"};(function(){"use strict";function e(e){var t=this;this.LayerDiv=$("<div>").appendTo(e).css({position:"absolute",left:"0px",top:"0px","border-width":"0px",width:"100%",height:"100%","box-sizing":"border-box","z-index":"100"}).addClass("sa-resize");var t=this;this.LayerDiv.saOnResize(function(){t.UpdateSize()}),SAM.DebugLayer=this,this.AnnotationView=new SAM.View(this.LayerDiv),this.AnnotationView.Canvas.saOnResize(function(){t.UpdateCanvasSize()}),this.WidgetList=[],this.ActiveWidget=null,this.Visibility=!0,this.ScaleWidget=new SAM.ScaleWidget(this);var t=this,n=this.LayerDiv;n.on("mousedown.viewer",function(e){return t.HandleMouseDown(e)}),n.on("mousemove.viewer",function(e){return this.focus(),SA.FirefoxWhich(e),t.HandleMouseMove(e)}),$(document.body).on("mouseup.viewer",function(e){return t.HandleMouseUp(e),!0}),n.on("wheel.viewer",function(e){return t.HandleMouseWheel(e.originalEvent)}),n.on("touchstart.viewer",function(e){return t.HandleTouchStart(e.originalEvent)}),n.on("touchmove.viewer",function(e){return t.HandleTouchMove(e.originalEvent)}),n.on("touchend.viewer",function(e){return t.HandleTouchEnd(e.originalEvent),!0}),this.LayerDiv.attr("tabindex","1"),n.on("keydown.viewer",function(e){return t.HandleKeyDown(e)})}window.SAM=window.SAM||{},window.SAM.ImagePathUrl="/webgl-viewer/static/",window.SAM.MOBILE_DEVICE=!1,SAM.detectMobile=function(){return SAM.MOBILE_DEVICE?SAM.MOBILE_DEVICE:(SAM.MOBILE_DEVICE=!1,navigator.userAgent.match(/Android/i)&&(SAM.MOBILE_DEVICE="Andriod"),navigator.userAgent.match(/webOS/i)&&(SAM.MOBILE_DEVICE="webOS"),navigator.userAgent.match(/iPhone/i)&&(SAM.MOBILE_DEVICE="iPhone"),navigator.userAgent.match(/iPad/i)&&(SAM.MOBILE_DEVICE="iPad"),navigator.userAgent.match(/iPod/i)&&(SAM.MOBILE_DEVICE="iPod"),navigator.userAgent.match(/BlackBerry/i)&&(SAM.MOBILE_DEVICE="BlackBerry"),navigator.userAgent.match(/Windows Phone/i)&&(SAM.MOBILE_DEVICE="Windows Phone"),SA.MOBILE_DEVICE&&(SAM.MaximumNumberOfTiles=5e3),SAM.MOBILE_DEVICE)},SAM.areaSequence=function(e,t,n){var r=new SAM.Polyline,i=SA.RootNote.ViewerRecords,s=[];for(var o=0;o<i.length;++o){var u=0,a=i[o].Annotations;for(var f=0;f<a.length;++f){var l=a[f];l.type=="polyline"&&Math.round(l.outlinecolor[0]*255)==e&&Math.round(l.outlinecolor[1]*255)==t&&Math.round(l.outlinecolor[2]*255)==n&&(u!=0&&console.log("Found more than one in a section"),r.Points=l.points,u+=r.ComputeArea()*.25*.25,u=Math.round(u*100)/100)}s.push(u)}return s},SAM.pruneAnnotations=function(){var e=SA.VIEWER1.GetCamera().FocalPoint,t=SA.VIEWER1.GetCamera().GetWidth()/2,n=SA.VIEWER1.GetCamera().GetHeight()/2,r=[e[0]-t,e[0]+t,e[1]-n,e[1]+n],i=SA.VIEWER1.GetAnnotationLayer(),t=i.WidgetList,s=[],o=[t.length,0];for(var u=0;u<t.length;++u){var a=t[u];if(a.Polyline){var f=a.Polyline.GetBounds(),l=(f[0]+f[1])/2,c=(f[2]+f[3])/2;l<r[1]&&l>r[0]&&c<r[3]&&c>r[2]&&s.push(a)}}return o[1]=s.length,i.WidgetList=s,SA.display.NavigationWidget.NextNote(),o},SAM.ConvertColor=function(e){if(typeof e=="string"&&e[0]!="#"){if(e.slice(0,5)=="rgba(")return e=e.slice(5,-1).split(","),e[0]=e[0]/255,e[1]=e[1]/255,e[2]=e[1]/255,e;var t={aliceblue:"#f0f8ff",antiquewhite:"#faebd7",aqua:"#00ffff",aquamarine:"#7fffd4",azure:"#f0ffff",beige:"#f5f5dc",bisque:"#ffe4c4",black:"#000000",blanchedalmond:"#ffebcd",blue:"#0000ff",blueviolet:"#8a2be2",brown:"#a52a2a",burlywood:"#deb887",cadetblue:"#5f9ea0",chartreuse:"#7fff00",chocolate:"#d2691e",coral:"#ff7f50",cornflowerblue:"#6495ed",cornsilk:"#fff8dc",crimson:"#dc143c",cyan:"#00ffff",darkblue:"#00008b",darkcyan:"#008b8b",darkgoldenrod:"#b8860b",darkgray:"#a9a9a9",darkgreen:"#006400",darkkhaki:"#bdb76b",darkmagenta:"#8b008b",darkolivegreen:"#556b2f",darkorange:"#ff8c00",darkorchid:"#9932cc",darkred:"#8b0000",darksalmon:"#e9967a",darkseagreen:"#8fbc8f",darkslateblue:"#483d8b",darkslategray:"#2f4f4f",darkturquoise:"#00ced1",darkviolet:"#9400d3",deeppink:"#ff1493",deepskyblue:"#00bfff",dimgray:"#696969",dodgerblue:"#1e90ff",firebrick:"#b22222",floralwhite:"#fffaf0",forestgreen:"#228b22",fuchsia:"#ff00ff",gainsboro:"#dcdcdc",ghostwhite:"#f8f8ff",gold:"#ffd700",goldenrod:"#daa520",gray:"#808080",green:"#008000",greenyellow:"#adff2f",honeydew:"#f0fff0",hotpink:"#ff69b4","indianred ":"#cd5c5c","indigo ":"#4b0082",ivory:"#fffff0",khaki:"#f0e68c",lavender:"#e6e6fa",lavenderblush:"#fff0f5",lawngreen:"#7cfc00",lemonchiffon:"#fffacd",lightblue:"#add8e6",lightcoral:"#f08080",lightcyan:"#e0ffff",lightgoldenrodyellow:"#fafad2",lightgrey:"#d3d3d3",lightgreen:"#90ee90",lightpink:"#ffb6c1",lightsalmon:"#ffa07a",lightseagreen:"#20b2aa",lightskyblue:"#87cefa",lightslategray:"#778899",lightsteelblue:"#b0c4de",lightyellow:"#ffffe0",lime:"#00ff00",limegreen:"#32cd32",linen:"#faf0e6",magenta:"#ff00ff",maroon:"#800000",mediumaquamarine:"#66cdaa",mediumblue:"#0000cd",mediumorchid:"#ba55d3",mediumpurple:"#9370d8",mediumseagreen:"#3cb371",mediumslateblue:"#7b68ee",mediumspringgreen:"#00fa9a",mediumturquoise:"#48d1cc",mediumvioletred:"#c71585",midnightblue:"#191970",mintcream:"#f5fffa",mistyrose:"#ffe4e1",moccasin:"#ffe4b5",navajowhite:"#ffdead",navy:"#000080",oldlace:"#fdf5e6",olive:"#808000",olivedrab:"#6b8e23",orange:"#ffa500",orangered:"#ff4500",orchid:"#da70d6",palegoldenrod:"#eee8aa",palegreen:"#98fb98",paleturquoise:"#afeeee",palevioletred:"#d87093",papayawhip:"#ffefd5",peachpuff:"#ffdab9",peru:"#cd853f",pink:"#ffc0cb",plum:"#dda0dd",powderblue:"#b0e0e6",purple:"#800080",red:"#ff0000",rosybrown:"#bc8f8f",royalblue:"#4169e1",saddlebrown:"#8b4513",salmon:"#fa8072",sandybrown:"#f4a460",seagreen:"#2e8b57",seashell:"#fff5ee",sienna:"#a0522d",silver:"#c0c0c0",skyblue:"#87ceeb",slateblue:"#6a5acd",slategray:"#708090",snow:"#fffafa",springgreen:"#00ff7f",steelblue:"#4682b4",tan:"#d2b48c",teal:"#008080",thistle:"#d8bfd8",tomato:"#ff6347",turquoise:"#40e0d0",violet:"#ee82ee",wheat:"#f5deb3",white:"#ffffff",whitesmoke:"#f5f5f5",yellow:"#ffff00",yellowgreen:"#9acd32"};typeof t[e.toLowerCase()]!="undefined"?e=t[e.toLowerCase()]:alert("Unknown color "+e)}if(typeof e=="string"&&e.length==7&&e[0]=="#"){var n=[],r=1;for(var i=0;i<3;++i){var s=(16*SAM.HexDigitToInt(e[r++])+SAM.HexDigitToInt(e[r++]))/255;n.push(s)}return n}return e},SAM.ConvertColorToHex=function(e){if(typeof e=="string"){if(e.slice(0,5)=="rgba(")return e;e=SAM.ConvertColorNameToHex(e);if(e.substring(0,1)=="#")return e;e.substring(0,3)=="rgb"&&(i=e.substring(4,e.length-1).split(","),e=[parseInt(i[0])/255,parseInt(i[1])/255,parseInt(i[2])/255])}var t="0123456789abcdef",n="#";for(var r=0;r<3;++r){var i=e[r];for(var s=0;s<2;++s){i*=16;var o=Math.floor(i);o<0&&(o=0),o>15&&(o=15),i-=o,n+=t.charAt(o)}}return n},SAM.HexDigitToInt=function(e){return e=="1"?1:e=="2"?2:e=="3"?3:e=="4"?4:e=="5"?5:e=="6"?6:e=="7"?7:e=="8"?8:e=="9"?9:e=="a"||e=="A"?10:e=="b"||e=="B"?11:e=="c"||e=="C"?12:e=="d"||e=="D"?13:e=="e"||e=="E"?14:e=="f"||e=="F"?15:0},SAM.ConvertColorNameToHex=function(e){if(typeof e=="string"&&e[0]!="#"){var t={aliceblue:"#f0f8ff",antiquewhite:"#faebd7",aqua:"#00ffff",aquamarine:"#7fffd4",azure:"#f0ffff",beige:"#f5f5dc",bisque:"#ffe4c4",black:"#000000",blanchedalmond:"#ffebcd",blue:"#0000ff",blueviolet:"#8a2be2",brown:"#a52a2a",burlywood:"#deb887",cadetblue:"#5f9ea0",chartreuse:"#7fff00",chocolate:"#d2691e",coral:"#ff7f50",cornflowerblue:"#6495ed",cornsilk:"#fff8dc",crimson:"#dc143c",cyan:"#00ffff",darkblue:"#00008b",darkcyan:"#008b8b",darkgoldenrod:"#b8860b",darkgray:"#a9a9a9",darkgreen:"#006400",darkkhaki:"#bdb76b",darkmagenta:"#8b008b",darkolivegreen:"#556b2f",darkorange:"#ff8c00",darkorchid:"#9932cc",darkred:"#8b0000",darksalmon:"#e9967a",darkseagreen:"#8fbc8f",darkslateblue:"#483d8b",darkslategray:"#2f4f4f",darkturquoise:"#00ced1",darkviolet:"#9400d3",deeppink:"#ff1493",deepskyblue:"#00bfff",dimgray:"#696969",dodgerblue:"#1e90ff",firebrick:"#b22222",floralwhite:"#fffaf0",forestgreen:"#228b22",fuchsia:"#ff00ff",gainsboro:"#dcdcdc",ghostwhite:"#f8f8ff",gold:"#ffd700",goldenrod:"#daa520",gray:"#808080",green:"#008000",greenyellow:"#adff2f",honeydew:"#f0fff0",hotpink:"#ff69b4",indianred:"#cd5c5c","indigo ":"#4b0082",ivory:"#fffff0",khaki:"#f0e68c",lavender:"#e6e6fa",lavenderblush:"#fff0f5",lawngreen:"#7cfc00",lemonchiffon:"#fffacd",lightblue:"#add8e6",lightcoral:"#f08080",lightcyan:"#e0ffff",lightgoldenrodyellow:"#fafad2",lightgrey:"#d3d3d3",lightgreen:"#90ee90",lightpink:"#ffb6c1",lightsalmon:"#ffa07a",lightseagreen:"#20b2aa",lightskyblue:"#87cefa",lightslategray:"#778899",lightsteelblue:"#b0c4de",lightyellow:"#ffffe0",lime:"#00ff00",limegreen:"#32cd32",linen:"#faf0e6",magenta:"#ff00ff",maroon:"#800000",mediumaquamarine:"#66cdaa",mediumblue:"#0000cd",mediumorchid:"#ba55d3",mediumpurple:"#9370d8",mediumseagreen:"#3cb371",mediumslateblue:"#7b68ee",mediumspringgreen:"#00fa9a",mediumturquoise:"#48d1cc",mediumvioletred:"#c71585",midnightblue:"#191970",mintcream:"#f5fffa",mistyrose:"#ffe4e1",moccasin:"#ffe4b5",navajowhite:"#ffdead",navy:"#000080",oldlace:"#fdf5e6",olive:"#808000",olivedrab:"#6b8e23",orange:"#ffa500",orangered:"#ff4500",orchid:"#da70d6",palegoldenrod:"#eee8aa",palegreen:"#98fb98",paleturquoise:"#afeeee",palevioletred:"#d87093",papayawhip:"#ffefd5",peachpuff:"#ffdab9",peru:"#cd853f",pink:"#ffc0cb",plum:"#dda0dd",powderblue:"#b0e0e6",purple:"#800080",red:"#ff0000",rosybrown:"#bc8f8f",royalblue:"#4169e1",saddlebrown:"#8b4513",salmon:"#fa8072",sandybrown:"#f4a460",seagreen:"#2e8b57",seashell:"#fff5ee",sienna:"#a0522d",silver:"#c0c0c0",skyblue:"#87ceeb",slateblue:"#6a5acd",slategray:"#708090",snow:"#fffafa",springgreen:"#00ff7f",steelblue:"#4682b4",tan:"#d2b48c",teal:"#008080",thistle:"#d8bfd8",tomato:"#ff6347",turquoise:"#40e0d0",violet:"#ee82ee",wheat:"#f5deb3",white:"#ffffff",whitesmoke:"#f5f5f5",yellow:"#ffff00",yellowgreen:"#9acd32"};e=e.toLowerCase(),typeof t[e]!="undefined"&&(e=t[e])}return e},window.SAM.DistanceToString=function(e){var t="";return e<.001?t+=(e*1e6).toFixed(2)+" µm":e<.01?t+=(e*1e3).toFixed(2)+" mm":e<1?t+=(e*100).toFixed(2)+" cm":e<1e3?t+=e.toFixed(2)+" m":t+=e.toFixed(2)+" km",t},window.SAM.StringToDistance=function(e){var t=0;e=e.trim();var n=e.length;return e.substring(n-2,n)=="µm"?t=parseFloat(e.substring(0,n-2))/1e6:e.substring(n-2,n)=="mm"?t=parseFloat(e.substring(0,n-2))/1e3:e.substring(n-2,n)=="cm"?t=parseFloat(e.substring(0,n-2))/100:e.substring(n-2,n)==" m"?t=parseFloat(e.substring(0,n-2)):e.substring(n-2,n)=="km"&&(t=parseFloat(e.substring(0,n-2))*1e3),t},window.SAM.ConvertToMeters=function(e){return!e.units||e.units=="Units"?e.value:e.units.toLowerCase()=="nm"?(e.units="m",e.value*=1e-9):e.units.toLowerCase()=="µm"?(e.units="m",e.value*=1e-6):e.units.toLowerCase()=="mm"?(e.units="m",e.value*=.001):e.units.toLowerCase()=="cm"?(e.units="m",e.value*=.01):e.units.toLowerCase()=="dm"?(e.units="m",e.value*=.1):e.units.toLowerCase()=="m"?(e.units="m",e.value):e.units.toLowerCase()=="km"?(e.units="m",e.value*=1e3):(console.log("Unknown units: "+units),e.value)},window.SAM.ConvertForGui=function(e){if(!e.units){e.units="Units";return}SAM.ConvertToMeters(e);if(e.value>1e3){e.value=e.value/1e3,e.units="km";return}if(e.value>1){e.value=e.value,e.units="m";return}if(e.value>.01){e.value=e.value*100,e.units="cm";return}if(e.value>.001){e.value=e.value*1e3,e.units="mm";return}if(e.value>1e-7){e.value=e.value*1e6,e.units="µm";return}e.value=e.value*1e9,e.units="nm"},e.prototype.Delete=function(){this.AnnotationView.Delete()},e.prototype.GetVisibility=function(){return this.Visibility},e.prototype.SetVisibility=function(e){this.Visibility=e,this.EventuallyDraw()},e.prototype.GetCamera=function(){return this.AnnotationView.GetCamera()},e.prototype.GetViewport=function(){return this.AnnotationView.Viewport},e.prototype.UpdateCanvasSize=function(){this.AnnotationView.UpdateCanvasSize()},e.prototype.Clear=function(){this.AnnotationView.Clear()},e.prototype.GetCanvasDiv=function(){return this.AnnotationView.CanvasDiv},e.prototype.GetPixelsPerUnit=function(){return this.AnnotationView.GetPixelsPerUnit()},e.prototype.GetMetersPerUnit=function(){return this.AnnotationView.GetMetersPerUnit()},e.prototype.Draw=function(e){e=e||this.AnnotationView,this.AnnotationView.Clear();if(!this.Visibility)return;var t=e.Camera;this.AnnotationView.Camera.DeepCopy(t);for(var n=0;n<this.WidgetList.length;++n)this.WidgetList[n].Draw(this.AnnotationView);this.ScaleWidget&&this.ScaleWidget.Draw(this.AnnotationView)},e.prototype.EventuallyDraw=function(){if(!this.RenderPending){this.RenderPending=!0;var e=this;requestAnimFrame(function(){e.RenderPending=!1,e.Draw()})}},e.prototype.GetViewer=function(){return this.Viewer||SA.VIEWER1},e.prototype.LoadAnnotations=function(e){for(var t=0;t<e.length;++t){var n=this.LoadWidget(e[t]);n||(e.splice(t,1),--t)}this.LoadCallback&&this.LoadCallback()},e.prototype.LoadWidget=function(e){var t;switch(e.type){case"lasso":t=new SAM.LassoWidget(this,!1);break;case"pencil":t=new SAM.PencilWidget(this,!1);break;case"text":t=new SAM.TextWidget(this,"");break;case"circle":t=new SAM.CircleWidget(this,!1);break;case"polyline":t=new SAM.PolylineWidget(this,!1);break;case"stack_section":window.SA&&(t=new SA.StackSectionWidget(this));break;case"sections":window.SA&&(t=new SA.SectionsWidget(this,SA.VIEWER1));break;case"rect":t=new SAM.RectWidget(this,!1);break;case"rect_set":t=new SAM.RectSetWidget(this,!1);break;case"grid":t=new SAM.GridWidget(this,!1)}return t.Load(e),t.Type=="sections"&&t.IsEmpty()?undefined:t},e.prototype.ActivateWidget=function(e){this.LayerDiv.focus();if(this.ActiveWidget==e)return;for(var t=0;t<this.WidgetList.length;++t)this.WidgetList[t].Popup&&this.WidgetList[t].Popup.Hide();this.ActiveWidget=e,e.SetActive(!0)},e.prototype.DeactivateWidget=function(e){if(this.ActiveWidget!=e||e==null)return;this.LayerDiv.css({cursor:"default"}),this.EventuallyDraw(),this.ActiveWidget=null,e.SetActive(!1)},e.prototype.GetActiveWidget=function(){return this.ActiveWidget},e.prototype.Reset=function(){this.WidgetList=[]},e.prototype.ComputeMouseWorld=function(e){return this.MouseWorld=this.GetCamera().ConvertPointViewerToWorld(e.offsetX,e.offsetY),e.worldX=this.MouseWorld[0],e.worldY=this.MouseWorld[1],this.MouseWorld},e.prototype.HandleTouch=function(e,t){var n=new Date,r=n.getTime();if(r-this.Time<20&&!t)return!1;this.LastTime=this.Time,this.Time=r;if(!e)var e=event;var i=this.GetViewport();this.LastTouches=this.Touches,this.Touches=[];for(var s=0;s<e.targetTouches.length;++s){var o=this.AnnotationView.Canvas.offset(),u=e.targetTouches[s].pageX-o.left,a=e.targetTouches[s].pageY-o.top;this.Touches.push([u,a])}this.LastMouseX=this.MouseX,this.LastMouseY=this.MouseY;var f=this.Touches.length;this.MouseX=this.MouseY=0;for(var s=0;s<f;++s)this.MouseX+=this.Touches[s][0],this.MouseY+=this.Touches[s][1];return this.MouseX=this.MouseX/f,this.MouseY=this.MouseY/f,this.offsetX=this.MouseX,this.offsetY=this.MouseY,!0},e.prototype.HandleTouchStart=function(e){if(!this.GetVisibility())return!0;this.HandleTouch(e,!0);if(this.Visibility)for(var t=0;t<this.Touches.length;++t){e.offsetX=this.Touches[t][0],e.offsetY=this.Touches[t][1],this.ComputeMouseWorld(e);for(var n=0;n<this.WidgetList.length;++n)if(!this.WidgetList[n].GetActive()&&this.WidgetList[n].CheckActive(e))return this.ActivateWidget(this.WidgetList[n]),!0}},e.prototype.HandleTouchMove=function(e){if(!this.HandleTouch(e,!1))return;if(this.Touches.length==1)return this.HandleTouchPan(this);if(this.Touches.length==2)return this.HandleTouchPinch(this)},e.prototype.HandleTouchPan=function(e){return this.GetVisibility()?this.ActiveWidget&&this.ActiveWidget.HandleTouchPan?this.ActiveWidget.HandleTouchPan(e):!this.ActiveWidget:!0},e.prototype.HandleTouchPinch=function(e){return this.GetVisibility()?this.ActiveWidget&&this.ActiveWidget.HandleTouchPinch?this.ActiveWidget.HandleTouchPinch(e):!this.ActiveWidget:!0},e.prototype.HandleTouchEnd=function(e){return this.GetVisibility()?this.ActiveWidget&&this.ActiveWidget.HandleTouchEnd?this.ActiveWidget.HandleTouchEnd(e):!this.ActiveWidget:!0},e.prototype.SetMousePositionFromEvent=function(e){e.offsetX&&e.offsetY?(this.MouseX=e.offsetX,this.MouseY=e.offsetY,this.MouseTime=(new Date).getTime()):e.layerX&&e.layerY&&(this.MouseX=e.layerX,this.MouseY=e.layerY,this.MouseTime=(new Date).getTime(),e.offsetX=e.layerX,e.offsetY=e.layerY),this.MouseTime=(new Date).getTime()},e.prototype.HandleMouseDown=function(e){return this.GetVisibility()?(this.LastMouseDownTime=this.MouseDownTime||1,this.SetMousePositionFromEvent(e),this.MouseClick=!0,this.MouseDownX=this.MouseX,this.MouseDownY=this.MouseY,this.MouseDownTime=this.MouseTime,this.LastMouseDownTime&&this.MouseDownTime-this.LastMouseDownTime<200?(delete this.LastMouseDownTime,this.HandleDoubleClick(e)):this.ActiveWidget&&this.ActiveWidget.HandleMouseDown?this.ActiveWidget.HandleMouseDown(e):!0):!0},e.prototype.HandleDoubleClick=function(e){return this.GetVisibility()?this.ActiveWidget&&this.ActiveWidget.HandleDoubleClick?this.ActiveWidget.HandleDoubleClick(e):!this.ActiveWidget:!0},e.prototype.HandleClick=function(e){return this.GetVisibility()?this.ActiveWidget&&this.ActiveWidget.HandleClick?this.ActiveWidget.HandleClick(e):!0:!0},e.prototype.HandleMouseUp=function(e){return this.GetVisibility()?this.MouseClick?(this.MouseClick=!1,this.HandleClick(e)):this.ActiveWidget&&this.ActiveWidget.HandleMouseUp?this.ActiveWidget.HandleMouseUp(e):!this.ActiveWidget:!0},e.prototype.HandleMouseMove=function(e){if(!this.GetVisibility())return!0;this.SetMousePositionFromEvent(e);if(e.which!=0&&this.MouseClick)return Math.abs(this.MouseDownX-this.MouseX)>5&&(this.MouseClick=!1),Math.abs(this.MouseDownY-this.MouseY)>5&&(this.MouseClick=!1),this.MouseTime-this.MouseDownTime>400&&(this.MouseClick=!1),!1;if($(e.target).width()!=$(e.currentTarget).width())return!0;this.ComputeMouseWorld(e),e.which=e.buttons,e.which==2?e.which=3:e.which==3&&(e.which=2);if(this.ActiveWidget){if(this.ActiveWidget.HandleMouseMove)return this.ActiveWidget.HandleMouseMove(e)}else if(!e.which)return this.CheckActive(e),!0;return!0},e.prototype.HandleMouseWheel=function(e){return this.GetVisibility()?this.ActiveWidget&&this.ActiveWidget.HandleMouseWheel?this.ActiveWidget.HandleMouseWheel(e):!0:!0},e.prototype.HandleKeyDown=function(e){return this.GetVisibility()?this.ActiveWidget&&this.ActiveWidget.HandleKeyDown?this.ActiveWidget.HandleKeyDown(e):!0:!0},e.prototype.HandleKeyUp=function(e){return this.GetVisibility()?this.ActiveWidget&&this.ActiveWidget.HandleKeyUp?this.ActiveWidget.HandleKeyUp(e):!0:!0},e.prototype.CheckActive=function(e){if(!this.GetVisibility())return!0;if(this.ActiveWidget)return this.ActiveWidget.CheckActive(e);for(var t=0;t<this.WidgetList.length;++t)if(this.WidgetList[t].CheckActive(e))return this.ActivateWidget(this.WidgetList[t]),!0;return!1},e.prototype.GetNumberOfWidgets=function(){return this.WidgetList.length},e.prototype.GetWidget=function(e){return this.WidgetList[e]},e.prototype.GetWidgets=function(){return this.WidgetList},e.prototype.AddWidget=function(e){e.Layer=this,this.WidgetList.push(e),SAM.NotesWidget&&SAM.NotesWidget.MarkAsModified()},e.prototype.RemoveWidget=function(e){if(e.Layer==null)return;e.Layer=null;var t=this.WidgetList.indexOf(e);t!=-1&&this.WidgetList.splice(t,1),SAM.NotesWidget&&SAM.NotesWidget.MarkAsModified()},e.prototype.LoadGirderItem=function(e){var t="564e42fe3f24e538e9a20eb9",n={itemId:t,limit:50,offset:0,sort:"lowerName",sortdir:1};girder.restRequest({type:"get",url:"annotation",data:JSON.stringify(n),success:function(e,t){console.log("success")},error:function(){alert("AJAX - error() : annotation get")}});var r="572be29d3f24e53573aa8e91";girder.restRequest({path:"annotation/"+r,method:"GET",contentType:"application/json"}).done(function(e){console.log("done")})},e.prototype.SaveGirderItem=function(e){var t="572be29d3f24e53573aa8e91";data={name:"Test3",elements:[{type:"circle",lineColor:"#FFFF00",lineWidth:20,center:[5e3,5e3,0],radius:2e3}]},girder.restRequest({type:"post",url:"annotation",data:JSON.stringify(data),success:function(e,t){console.log("success")},error:function(){alert("AJAX - error() : annotation get")}}),data={name:"Test",elements:[{type:"polyline",points:[[6500,6600,0],[3300,5600,0],[10600,500,6]],closed:!0,fillColor:"rgba(0, 255, 0, 1)"}]},girder.restRequest({type:"put",url:"annotation/"+t,data:JSON.stringify(data),success:function(e,t){console.log("success2")},error:function(){alert("AJAX - error() : annotation get")}})},e.prototype.UpdateSize=function(){return this.AnnotationView?this.AnnotationView.UpdateCanvasSize()?(this.EventuallyDraw(),!0):!1:!1},SAM.AnnotationLayer=e})(),function(){"use strict";function e(){this.WorldToImage1=mat3.create(),this.Image1ToWorld=mat3.create(),this.WorldToImage2=mat3.create(),this.Image2ToWorld=mat3.create()}e.prototype.M2Invert=function(e){var t=e[0]*e[3]-e[1]*e[2];return[e[3]/t,-e[1]/t,-e[2]/t,e[0]/t]},e.prototype.M2Multiply=function(e,t){return[e[0]*t[0]+e[1]*t[2],e[0]*t[1]+e[1]*t[3],e[2]*t[0]+e[3]*t[2],e[2]*t[1]+e[3]*t[3]]},e.prototype.InitializeWithPoints=function(e,t,n,r,i,s){var o=mat3.create(),u=mat3.create();mat3.identity(o),mat3.identity(u),o[2]=e[0],o[5]=e[1],u[2]=t[0],u[5]=t[1];var a=[n[0]-e[0],i[0]-e[0],n[1]-e[1],i[1]-e[1]],f=[r[0]-t[0],s[0]-t[0],r[1]-t[1],s[1]-t[1]],l=this.M2Multiply(f,this.M2Invert(a));u[0]=l[0],u[1]=l[1],u[3]=l[2],u[4]=l[3],this.Initialize(o,u)},e.prototype.Initialize=function(e,t){mat3.set(e,this.WorldToImage1),mat3.set(t,this.WorldToImage2);var n=mat4.create(),r=mat4.create();mat3.toMat4(this.WorldToImage1,n),mat4.inverse(n,r),mat4.toMat3(r,this.Image1ToWorld),mat3.toMat4(this.WorldToImage2,n),mat4.inverse(n,r),mat4.toMat3(r,this.Image2ToWorld)},e.prototype.ForwardTransformPoint=function(e){var t=this.Image1ToWorld,n=e[0]*t[0]+e[1]*t[1]+t[2],r=e[0]*t[3]+e[1]*t[4]+t[5],i=e[0]*t[6]+e[1]*t[7]+t[8];t=this.WorldToImage2;var s=n*t[0]+r*t[1]+i*t[2],o=n*t[3]+r*t[4]+i*t[5],u=n*t[6]+r*t[7]+i*t[8];return[s/u,o/u]},e.prototype.ReverseTransformPoint=function(e){var t=this.Image2ToWorld,n=e[0]*t[0]+e[1]*t[1]+t[2],r=e[0]*t[3]+e[1]*t[4]+t[5],i=e[0]*t[6]+e[1]*t[7]+t[8];t=this.WorldToImage1;var s=n*t[0]+r*t[1]+i*t[2],o=n*t[3]+r*t[4]+i*t[5],u=n*t[6]+r*t[7]+i*t[8];return[s/u,o/u]},e.prototype.ForwardTransformCamera=function(e,t){var n=e.FocalPoint,r=t.FocalPoint,i=[n[0]+1,n[1]],s=this.ForwardTransformPoint(n);r[0]=s[0],r[1]=s[1];var o=this.ForwardTransformPoint(i);o[0]-=r[0],o[1]-=r[1];var u=Math.sqrt(o[0]*o[0]+o[1]*o[1]);t.SetHeight(e.GetHeight()*u);var a=Math.atan2(o[1],o[0]);t.Roll=e.Roll,t.ComputeMatrix()},e.prototype.ReverseTransformCamera=function(e,t){var n=e.FocalPoint,r=t.FocalPoint,i=[n[0]+1,n[1]],s=this.ReverseTransformPoint(n);r[0]=s[0],r[1]=s[1];var o=this.ReverseTransformPoint(i);o[0]-=r[0],o[1]-=r[1];var u=Math.sqrt(o[0]*o[0]+o[1]*o[1]);t.SetHeight(e.GetHeight()*u);var a=Math.atan2(o[1],o[0]);t.Roll=e.Roll,t.ComputeMatrix()},SAM.MatrixTransformation=e}();(function(){"use strict";function e(){this.Orientation=0,this.PositionCoordinateSystem=e.SLIDE,this.Origin=[1e4,1e4],this.FixedSize=!0,this.FixedOrientation=!0,this.LineWidth=0,this.Visibility=!0,this.Active=!1,this.ActiveColor=[1,1,0],this.ZOffset=.1}e.SLIDE=0,e.VIEWER=1,e.prototype.destructor=function(){},e.prototype.Draw=function(t){if(!this.Visibility)return;this.Matrix==undefined&&this.UpdateBuffers(t);if(t.gl){var n=mat4.create();mat4.identity(n);if(this.FixedSize){var r=t.Camera.ZRange[0]+.01;n[0]=2/t.Viewport[2],n[12]=-1,n[5]=-2/t.Viewport[3],n[13]=1,n[14]=r}var i=this.Orientation*3.1415926536/180;this.Matrix[0]=Math.cos(i),this.Matrix[1]=-Math.sin(i),this.Matrix[4]=Math.sin(i),this.Matrix[5]=Math.cos(i),o=this.Origin[0],u=this.Origin[1];if(this.FixedSize){var s=t.Camera.Matrix,o=(this.Origin[0]*s[0]+this.Origin[1]*s[4]+s[12])/s[15],u=(this.Origin[0]*s[1]+this.Origin[1]*s[5]+s[13])/s[15];o=t.Viewport[2]*.5*(1+o),u=t.Viewport[3]*.5*(1-u)}this.Matrix[12]=o,this.Matrix[13]=u,this.Matrix[14]=this.ZOffset;var a=polyProgram;t.gl.useProgram(a),t.gl.disable(t.gl.BLEND),t.gl.enable(t.gl.DEPTH_TEST),t.gl.bindBuffer(t.gl.ARRAY_BUFFER,this.VertexPositionBuffer),t.gl.vertexAttribPointer(a.vertexPositionAttribute,this.VertexPositionBuffer.itemSize,t.gl.FLOAT,!1,0,0),t.gl.viewport(t.Viewport[0],t.Viewport[1],t.Viewport[2],t.Viewport[3]),t.gl.uniformMatrix4fv(a.mvMatrixUniform,!1,this.Matrix),this.FixedSize?t.gl.uniformMatrix4fv(a.pMatrixUniform,!1,n):t.gl.uniformMatrix4fv(a.pMatrixUniform,!1,t.Camera.Matrix),this.FillColor!=undefined&&(this.Active?t.gl.uniform3f(a.colorUniform,this.ActiveColor[0],this.ActiveColor[1],this.ActiveColor[2]):t.gl.uniform3f(a.colorUniform,this.FillColor[0],this.FillColor[1],this.FillColor[2]),t.gl.bindBuffer(t.gl.ELEMENT_ARRAY_BUFFER,this.CellBuffer),t.gl.drawElements(t.gl.TRIANGLES,this.CellBuffer.numItems,t.gl.UNSIGNED_SHORT,0)),this.OutlineColor!=undefined&&(this.Active?t.gl.uniform3f(a.colorUniform,this.ActiveColor[0],this.ActiveColor[1],this.ActiveColor[2]):t.gl.uniform3f(a.colorUniform,this.OutlineColor[0],this.OutlineColor[1],this.OutlineColor[2]),this.LineWidth==0?this.WireFrame?(t.gl.bindBuffer(t.gl.ELEMENT_ARRAY_BUFFER,this.CellBuffer),t.gl.drawElements(t.gl.LINE_LOOP,this.CellBuffer.numItems,t.gl.UNSIGNED_SHORT,0)):t.gl.drawArrays(t.gl.LINE_STRIP,0,this.VertexPositionBuffer.numItems):(t.gl.bindBuffer(t.gl.ELEMENT_ARRAY_BUFFER,this.LineCellBuffer),t.gl.drawElements(t.gl.TRIANGLES,this.LineCellBuffer.numItems,t.gl.UNSIGNED_SHORT,0)))}else{t.Context2d.save(),t.Context2d.setTransform(1,0,0,1,0,0);if(this.PositionCoordinateSystem==e.SLIDE){var i=this.Orientation*3.1415926536/180;this.FixedSize||(i-=t.Camera.Roll),this.Matrix[0]=Math.cos(i),this.Matrix[1]=-Math.sin(i),this.Matrix[4]=Math.sin(i),this.Matrix[5]=Math.cos(i),o=this.Origin[0],u=this.Origin[1];var f=1;this.FixedSize||(f=t.Viewport[3]/t.Camera.GetHeight());var s=t.Camera.Matrix,o=(this.Origin[0]*s[0]+this.Origin[1]*s[4]+s[12])/s[15],u=(this.Origin[0]*s[1]+this.Origin[1]*s[5]+s[13])/s[15];o=t.Viewport[2]*.5*(1+o),u=t.Viewport[3]*.5*(1-u),t.Context2d.transform(this.Matrix[0],this.Matrix[1],this.Matrix[4],this.Matrix[5],o,u)}else if(this.PositionCoordinateSystem==e.VIEWER){var i=this.Orientation*3.1415926536/180;this.Matrix[0]=Math.cos(i),this.Matrix[1]=-Math.sin(i),this.Matrix[4]=Math.sin(i),this.Matrix[5]=Math.cos(i),o=this.Origin[0],u=this.Origin[1];var f=1;t.Context2d.transform(this.Matrix[0],this.Matrix[1],this.Matrix[4],this.Matrix[5],o,u)}var l=this.PointBuffer[0],c=this.PointBuffer[1];t.Context2d.beginPath(),t.Context2d.moveTo(l*f,c*f);var h=3;while(h<this.PointBuffer.length){var p=this.PointBuffer[h],d=this.PointBuffer[h+1];t.Context2d.lineTo(p*f,d*f),h+=3}if(this.OutlineColor!=undefined){var v=this.LineWidth*f;v==0&&(v=1),t.Context2d.lineWidth=v,this.Active?t.Context2d.strokeStyle=SAM.ConvertColorToHex(this.ActiveColor):t.Context2d.strokeStyle=SAM.ConvertColorToHex(this.OutlineColor),t.Context2d.stroke()}this.FillColor!=undefined&&(this.Active?t.Context2d.fillStyle=SAM.ConvertColorToHex(this.ActiveColor):t.Context2d.fillStyle=SAM.ConvertColorToHex(this.FillColor),t.Context2d.fill()),t.Context2d.restore()}},e.prototype.ChooseOutlineColor=function(){this.FillColor&&(this.OutlineColor=[1-this.FillColor[0],1-this.FillColor[1],1-this.FillColor[2]])},e.prototype.SetOutlineColor=function(e){this.OutlineColor=SAM.ConvertColor(e)},e.prototype.SetFillColor=function(e){this.FillColor=SAM.ConvertColor(e)},e.prototype.HandleMouseMove=function(e,t,n){return!1},e.prototype.IntersectPointLine=function(e,t,n,r){var i=e[0]-t[0],s=e[1]-t[1],o=n[0]-t[0],u=n[1]-t[1],a=Math.sqrt(o*o+u*u);o/=a,u=-u/a;var f=i*o-s*u,l=i*u+s*o;return Math.abs(l)>r||f<0||f>a?undefined:f/a},SAM.Shape=e})();(function(){"use strict";function e(){this.Shapes=[],this.Bounds=[0,-1,0,-1]}e.prototype.GetBounds=function(){return this.Bounds},e.prototype.ContainedInBounds=function(e){if(this.Shapes.length==0)return 0;var t=this.Shapes[0].ContainedInBounds(e);for(var n=1;n<this.Shapes.length;++n){if(t==1)return t;var r=this.Shapes[n].ContainedInBounds(e);t==0&&r!=0&&(t=1),t==2&&r!=2&&(t=1)}return t},e.prototype.PointOnShape=function(e,t){for(var n=0;n<this.Shapes.length;++n)if(this.Shapes[n].PointOnShape(e,t))return!0;return!1},e.prototype.UpdateBuffers=function(e){for(var t=0;t<this.Shapes.length;++t)this.Shapes.UpdateBuffers(e)},e.prototype.FindPopupPoint=function(e){if(this.Shapes.length==0)return;var t=e.Roll,n=Math.sin(t+Math.PI*.25),r=Math.cos(t+Math.PI*.25),i=this.Shapes[0].FindPopupPoint(e),s=r*i[0]-n*i[1];for(var o=1;o<this.Shapes.length;++o){var u=this.Shapes[o].FindPopupPoint(e),a=r*u[0]-n*u[1];a>s&&(s=a,i=u)}return i},e.prototype.Draw=function(e){for(var t=0;t<this.Shapes.length;++t)this.Shapes[t].Draw(e)},e.prototype.AddShape=function(e){this.Shapes.push(e)},e.prototype.GetNumberOfShapes=function(){return this.Shapes.length},e.prototype.GetShape=function(e){return this.Shapes[e]},e.prototype.SetActive=function(e){for(var t=0;t<this.Shapes.length;++t)this.Shapes[t].SetActive(e)},e.prototype.SetLineWidth=function(e){for(var t=0;t<this.Shapes.length;++t)this.Shapes[t].LineWidth=e},e.prototype.GetLineWidth=function(){return this.Shapes.length!=0?this.Shapes[0].GetLineWidth():0},e.prototype.SetOutlineColor=function(e){for(var t=0;t<this.Shapes.length;++t)this.Shapes[t].OutlineColor=e},e.prototype.GetOutlineColor=function(){return this.Shapes.length!=0?this.Shapes[0].OutlineColor:[0,0,0]},e.prototype.SetOrigin=function(e){for(var t=0;t<this.Shapes.length;++t)this.Shapes[t].SetOrigin(e)},e.prototype.ResetOrigin=function(){for(var e=0;e<this.Shapes.length;++e)this.Shapes[e].ResetOrigin()},e.prototype.GetOrigin=function(){return this.Shapes.length!=0?this.Shapes[0].Origin:[0,0,0]},e.prototype.UpdateBuffers=function(e){for(var t=0;t<this.Shapes.length;++t)this.Shapes[t].UpdateBuffers(e)},SAM.ShapeGroup=e})();(function(){"use strict";function e(e,t){this.Viewer=t,this.Layer=t.GetAnnotationLayer();var n=layer.GetCamera(),r=n.GetFocalPoint(),i=n.Height/4;this.Bounds=[r[0]-i,r[0]+i,r[1]-i,r[1]+i],this.DragBounds=[r[0]-i,r[0]+i,r[1]-i,r[1]+i],layer.AddWidget(this),eventuallyRender(),this.Active=0;var s=this;this.Div=$("<div>").appendTo(e).addClass("sa-view-cutout-div"),$("<button>").appendTo(this.Div).text("Cancel").addClass("sa-view-cutout-button").click(function(){s.Cancel()}),$("<button>").appendTo(this.Div).text("Download").addClass("sa-view-cutout-button").click(function(){s.Accept()}),this.Select=$("<select>").appendTo(this.Div),$("<option>").appendTo(this.Select).attr("value",0).text("tif"),$("<option>").appendTo(this.Select).attr("value",1).text("jpeg"),$("<option>").appendTo(this.Select).attr("value",2).text("png"),$("<option>").appendTo(this.Select).attr("value",3).text("svs"),this.Label=$("<div>").addClass("sa-view-cutout-label").appendTo(this.Div),this.UpdateBounds(),this.HandleMouseUp()}e.prototype.Accept=function(){this.Deactivate();var e=["tif","jpeg","png","svs"],t=this.Viewer.GetCache().Image;window.location="/cutout/"+t.database+"/"+t._id+"/image."+e[this.Select.val()]+"?bounds="+JSON.stringify(this.Bounds)},e.prototype.Cancel=function(){this.Deactivate()},e.prototype.Serialize=function(){return!1},e.prototype.Draw=function(e){var t=[(this.DragBounds[0]+this.DragBounds[1])*.5,(this.DragBounds[2]+this.DragBounds[3])*.5],n=e.Camera,r=e.Viewport;if(e.gl)alert("webGL cutout not supported");else{var i=e.Context2d,n=e.Camera;i.save(),i.setTransform(1,0,0,1,0,0),this.DrawRectangle(i,this.Bounds,n,"#00A",1,0),this.DrawRectangle(i,this.DragBounds,n,"#000",2,this.Active),this.DrawCenter(i,t,n,"#000"),i.restore()}},e.prototype.DrawRectangle=function(e,t,n,r,i,s){var o=n.ConvertPointWorldToViewer(t[0],t[2]),u=n.ConvertPointWorldToViewer(t[1],t[2]),a=n.ConvertPointWorldToViewer(t[1],t[3]),f=n.ConvertPointWorldToViewer(t[0],t[3]);e.lineWidth=i,e.beginPath(),e.strokeStyle=s&4?"#FF0":r,e.moveTo(o[0],o[1]),e.lineTo(u[0],u[1]),e.stroke(),e.beginPath(),e.strokeStyle=s&2?"#FF0":r,e.moveTo(u[0],u[1]),e.lineTo(a[0],a[1]),e.stroke(),e.beginPath(),e.strokeStyle=s&8?"#FF0":r,e.moveTo(a[0],a[1]),e.lineTo(f[0],f[1]),e.stroke(),e.beginPath(),e.strokeStyle=s&1?"#FF0":r,e.moveTo(f[0],f[1]),e.lineTo(o[0],o[1]),e.stroke()},e.prototype.DrawCenter=function(e,t,n,r){var i=n.ConvertPointWorldToViewer(t[0],t[1]);e.strokeStyle=this.Active&16?"#FF0":r,e.lineWidth=1,e.beginPath(),e.moveTo(i[0]-5,i[1]),e.lineTo(i[0]+5,i[1]),e.moveTo(i[0],i[1]-5),e.lineTo(i[0],i[1]+5),e.stroke()},e.prototype.HandleKeyPress=function(e,t){return event.keyCode==67&&alert("Accept"),event.keyCode==67&&alert("Cancel"),!0},e.prototype.HandleDoubleClick=function(e){return!0},e.prototype.HandleMouseDown=function(e){return e.which!=1?!1:!0},e.prototype.HandleMouseUp=function(){if(this.Bounds[0]>this.Bounds[1]){var e=this.Bounds[0];this.Bounds[0]=this.Bounds[1],this.Bounds[1]=e}if(this.Bounds[2]>this.Bounds[3]){var e=this.Bounds[2];this.Bounds[2]=this.Bounds[3],this.Bounds[3]=e}this.DragBounds=this.Bounds.slice(0),eventuallyRender()},e.prototype.HandleMouseMove=function(e){var t=e.offsetX,n=e.offsetY;if(e.which==0){this.CheckActive(e);return}if(this.Active){var r=this.Layer.GetCamera(),i=r.ConvertPointViewerToWorld(e.offsetX,e.offsetY);this.Active&1&&(this.DragBounds[0]=i[0]),this.Active&2&&(this.DragBounds[1]=i[0]),this.Active&4&&(this.DragBounds[2]=i[1]),this.Active&8&&(this.DragBounds[3]=i[1]);if(this.Active&16){var s=i[0]-.5*(this.DragBounds[0]+this.DragBounds[1]),o=i[1]-.5*(this.DragBounds[2]+this.DragBounds[3]);this.DragBounds[0]+=s,this.DragBounds[1]+=s,this.DragBounds[2]+=o,this.DragBounds[3]+=o}return this.UpdateBounds(),eventuallyRender(),!0}return!1},e.prototype.UpdateBounds=function(e){var t=this.Viewer.GetCache(),n=t.Image.TileSize,r=[0,0,0,0];r[0]=Math.round(this.DragBounds[0]/n)*n,r[1]=Math.round(this.DragBounds[1]/n)*n,r[2]=Math.round(this.DragBounds[2]/n)*n,r[3]=Math.round(this.DragBounds[3]/n)*n;var i=t.Image.bounds;r[0]<i[0]&&(r[0]=i[0]),r[1]<i[0]&&(r[1]=i[0]),r[2]<i[2]&&(r[2]=i[2]),r[3]<i[2]&&(r[3]=i[2]),r[0]>i[1]&&(r[0]=i[1]),r[1]>i[1]&&(r[1]=i[1]),r[2]>i[3]&&(r[2]=i[3]),r[3]>i[3]&&(r[3]=i[3]),r[0]!=r[1]&&(this.Bounds[0]=r[0],this.Bounds[1]=r[1]),r[2]!=r[3]&&(this.Bounds[2]=r[2],this.Bounds[3]=r[3]);var s=[this.Bounds[1]-this.Bounds[0],this.Bounds[3]-this.Bounds[2]];this.Label.text(s[0]+" x "+s[1]+" = "+this.FormatPixels(s[0]*s[1])+"pixels")},e.prototype.FormatPixels=function(e){return e>1e9?Math.round(e/1e9)+"G":e>1e6?Math.round(e/1e6)+"M":e>1e3?Math.round(e/1e3)+"k":e},e.prototype.HandleTouchPan=function(e){},e.prototype.HandleTouchPinch=function(e){},e.prototype.HandleTouchEnd=function(e){},e.prototype.CheckActive=function(e){var t=this.Layer.GetCamera(),n=t.Height/200,r=t.ConvertPointViewerToWorld(e.offsetX,e.offsetY),i=0,s=this.DragBounds[0]-n<r[0]&&r[0]<this.DragBounds[1]+n,o=this.DragBounds[2]-n<r[1]&&r[1]<this.DragBounds[3]+n;o&&Math.abs(r[0]-this.DragBounds[0])<n&&(i|=1),o&&Math.abs(r[0]-this.DragBounds[1])<n&&(i|=2),s&&Math.abs(r[1]-this.DragBounds[2])<n&&(i|=4),s&&Math.abs(r[1]-this.DragBounds[3])<n&&(i|=8);var u=[(this.DragBounds[0]+this.DragBounds[1])*.5,(this.DragBounds[2]+this.DragBounds[3])*.5];return n*=2,Math.abs(r[0]-u[0])<n&&Math.abs(r[1]-u[1])<n&&(i|=16),i!=this.Active&&(this.SetActive(i),eventuallyRender()),!1},e.prototype.GetActive=function(){return this.Active},e.prototype.Deactivate=function(){this.Div.remove();if(this.Layer==null)return;this.Layer.DeactivateWidget(this),this.Layer.RemoveWidget(this),eventuallyRender()},e.prototype.SetActive=function(e){if(this.Active==e)return;this.Active=e,e!=0?this.Layer.ActivateWidget(this):this.Layer.DeactivateWidget(this),eventuallyRender()},SAM.CutoutWidget=e})();(function(){"use strict";function n(e){return function(){e.HandleLoadedTexture()}}function r(){alert("Could not load font")}function i(e){e&&(this.gl=e,this.TextureLoaded=!1,this.Texture=e.createTexture(),this.Image=new Image,this.Image.onload=n(this),this.Image.src=SA.ImagePathUrl+"letters.gif"),this.Color=[.5,1,1],this.Size=12,this.Position=[100,100],this.Orientation=0,this.Anchor=[0,0],this.Active=!1,this.String=",./<>?[]{}|-=~!@#$%^&*()_+",this.PixelBounds=[0,0,0,0],this.BackgroundFlag=!1}function s(e,t,n,r,i,s){typeof s=="undefined"&&(s=5),e.fillStyle="#fff",e.strokeStyle="#666",e.beginPath(),e.moveTo(t+s,n),e.lineTo(t+r-s,n),e.quadraticCurveTo(t+r,n,t+r,n+s),e.lineTo(t+r,n+i-s),e.quadraticCurveTo(t+r,n+i,t+r-s,n+i),e.lineTo(t+s,n+i),e.quadraticCurveTo(t,n+i,t,n+i-s),e.lineTo(t,n+s),e.quadraticCurveTo(t,n,t+s,n),e.closePath(),e.stroke(),e.fill()}var e=1.3,t=[[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[900,17,30,98],[791,119,28,95],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[260,18,32,97],[292,18,32,97],[0,413,50,98],[0,413,50,98],[635,120,25,36],[783,17,37,57],[662,121,25,34],[687,121,46,96],[822,214,58,98],[881,214,50,98],[932,214,56,98],[0,114,53,98],[54,114,54,98],[109,114,54,98],[164,114,57,98],[222,114,49,98],[272,114,57,98],[330,114,56,98],[554,18,25,76],[579,121,28,73],[0,413,50,98],[412,120,62,69],[0,413,50,98],[733,10,53,106],[0,413,50,98],[263,314,67,98],[331,314,55,98],[387,314,59,98],[447,314,66,98],[514,314,52,98],[566,314,49,98],[616,314,67,98],[684,314,67,98],[752,314,24,98],[777,314,36,98],[814,314,58,98],[873,314,45,98],[919,314,88,98],[0,214,66,98],[69,214,72,98],[142,214,54,98],[197,214,76,98],[274,214,53,98],[328,214,49,98],[378,214,55,98],[434,214,66,98],[501,214,63,98],[565,214,96,98],[662,214,55,98],[718,214,53,98],[772,214,49,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[51,413,56,98],[108,413,50,98],[154,413,50,98],[210,413,50,98],[263,413,39,98],[301,413,50,98],[350,413,54,98],[406,413,22,98],[427,413,34,98],[458,413,50,98],[508,413,24,98],[532,413,88,98],[619,413,57,98],[675,413,60,98],[734,413,57,98],[790,413,57,98],[847,413,40,98],[886,413,42,98],[925,413,41,98],[966,413,56,98],[0,314,49,98],[50,314,77,98],[127,314,48,98],[173,314,52,98],[224,314,42,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98],[0,413,50,98]];i.prototype.destructor=function(){},i.prototype.HandleLoadedTexture=function(){if(this.gl){var e=this.Texture;this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL,!0),this.gl.bindTexture(this.gl.TEXTURE_2D,e),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,this.Image),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR_MIPMAP_NEAREST),this.gl.generateMipmap(this.gl.TEXTURE_2D),this.gl.bindTexture(this.gl.TEXTURE_2D,null),this.TextureLoaded=!0}eventuallyRender()},i.prototype.Draw=function(t){var n=this.Position[0],r=this.Position[1];if(this.PositionCoordinateSystem!=SAM.Shape.VIEWER){var i=t.Camera.Matrix;n=(this.Position[0]*i[0]+this.Position[1]*i[4]+i[12])/i[15],r=(this.Position[0]*i[1]+this.Position[1]*i[5]+i[13])/i[15],n=t.Viewport[2]*.5*(1+n),r=t.Viewport[3]*.5*(1-r)}if(Math.abs(this.Anchor[0])>1e3||Math.abs(this.Anchor[1])>1e3)this.Anchor=[-50,0];if(t.gl){if(this.TextureLoaded==0)return;this.Matrix==undefined&&(this.UpdateBuffers(t),this.Matrix=mat4.create(),mat4.identity(this.Matrix));var o=textProgram;t.gl.useProgram(o),t.gl.blendFunc(t.gl.SRC_ALPHA,t.gl.ONE_MINUS_SRC_ALPHA),t.gl.enable(t.gl.BLEND),t.gl.bindBuffer(t.gl.ARRAY_BUFFER,this.VertexPositionBuffer),t.gl.vertexAttribPointer(o.vertexPositionAttribute,this.VertexPositionBuffer.itemSize,t.gl.FLOAT,!1,0,0),t.gl.bindBuffer(t.gl.ARRAY_BUFFER,this.VertexTextureCoordBuffer),t.gl.vertexAttribPointer(o.textureCoordAttribute,this.VertexTextureCoordBuffer.itemSize,t.gl.FLOAT,!1,0,0),t.gl.bindBuffer(t.gl.ELEMENT_ARRAY_BUFFER,this.CellBuffer),this.Active?t.gl.uniform3f(o.colorUniform,1,1,0):t.gl.uniform3f(o.colorUniform,this.Color[0],this.Color[1],this.Color[2]),t.gl.viewport(t.Viewport[0],t.Viewport[1],t.Viewport[2],t.Viewport[3]);var u=t.Camera.ZRange[0]+.01,a=mat4.create();mat4.identity(a),a[0]=2/t.Viewport[2],a[12]=-1,a[5]=-2/t.Viewport[3],a[13]=1,a[14]=u,t.gl.uniformMatrix4fv(o.pMatrixUniform,!1,a),this.Matrix[12]=n-this.Anchor[0],this.Matrix[13]=r-this.Anchor[1],t.gl.uniformMatrix4fv(o.mvMatrixUniform,!1,this.Matrix),t.gl.activeTexture(t.gl.TEXTURE0),t.gl.bindTexture(t.gl.TEXTURE_2D,this.Texture),t.gl.uniform1i(o.samplerUniform,0),t.gl.drawElements(t.gl.TRIANGLES,this.CellBuffer.numItems,t.gl.UNSIGNED_SHORT,0)}else{var f=this.String.split("\n"),l=t.Context2d;l.save();var c=this.Orientation*Math.PI/180,h=Math.sin(c),p=Math.cos(c);l.setTransform(p,-h,h,p,n,r),n=-this.Anchor[0],r=-this.Anchor[1],l.font=this.Size+"pt Calibri";var d=this.PixelBounds[1],v=this.PixelBounds[3];this.BackgroundFlag&&s(l,n-2,r-2,d+6,v+2,this.Size/2,!0,!1),this.Active?l.fillStyle="#FF0":l.fillStyle=SAM.ConvertColorToHex(this.Color),r+=this.Size;for(var m=0;m<f.length;++m)l.fillText(f[m],n,r),r+=this.Size*e;l.stroke(),l.restore()}},i.prototype.UpdateBuffers=function(n){if(!n.gl){var r=this.String.split("\n"),i=this.Size*e*r.length,s=0,o=n.Context2d;o.save(),o.setTransform(1,0,0,1,0,0),o.font=this.Size+"pt Calibri";for(var u=0;u<r.length;++u){var a=o.measureText(r[u]).width;a>s&&(s=a)}this.PixelBounds=[0,s,0,i],o.restore();return}var f=[],l=[],c=[],h=98/128,p=0,d=0,v=0;this.PixelBounds=[0,0,0,this.Size];for(var u=0;u<this.String.length;++u){var m=this.String.charCodeAt(u);if(m==10||m==13)p=0,d+=this.Size;else{var g=t[m],y=g[0]/1024,b=(g[0]+g[2])/1024,w=g[1]/512,E=(g[1]+g[3])/512,S=p+g[2]*this.Size/98,x=d+g[3]*this.Size/98;this.PixelBounds[0]>p&&(this.PixelBounds[0]=p),this.PixelBounds[1]<S&&(this.PixelBounds[1]=S),this.PixelBounds[2]>d&&(this.PixelBounds[2]=d),this.PixelBounds[3]<x&&(this.PixelBounds[3]=x),l.push(y),l.push(w),f.push(p),f.push(x),f.push(0),l.push(b),l.push(w),f.push(S),f.push(x),f.push(0),l.push(y),l.push(E),f.push(p),f.push(d),f.push(0),l.push(b),l.push(E),f.push(S),f.push(d),f.push(0),p=S,c.push(0+v),c.push(1+v),c.push(2+v),c.push(2+v),c.push(1+v),c.push(3+v),v+=4}}this.VertexTextureCoordBuffer=n.gl.createBuffer(),n.gl.bindBuffer(n.gl.ARRAY_BUFFER,this.VertexTextureCoordBuffer),n.gl.bufferData(n.gl.ARRAY_BUFFER,new Float32Array(l),n.gl.STATIC_DRAW),this.VertexTextureCoordBuffer.itemSize=2,this.VertexTextureCoordBuffer.numItems=l.length/2,this.VertexPositionBuffer=n.gl.createBuffer(),n.gl.bindBuffer(n.gl.ARRAY_BUFFER,this.VertexPositionBuffer),n.gl.bufferData(n.gl.ARRAY_BUFFER,new Float32Array(f),n.gl.STATIC_DRAW),this.VertexPositionBuffer.itemSize=3,this.VertexPositionBuffer.numItems=f.length/3,this.CellBuffer=n.gl.createBuffer(),n.gl.bindBuffer(n.gl.ELEMENT_ARRAY_BUFFER,this.CellBuffer),n.gl.bufferData(n.gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(c),n.gl.STATIC_DRAW),this.CellBuffer.itemSize=1,this.CellBuffer.numItems=c.length},i.prototype.HandleMouseMove=function(e,t,n){return viewer=e.CurrentViewer,!1},i.prototype.SetColor=function(e){this.Color=SAM.ConvertColor(e)},SAM.Text=i})();(function(){"use strict";function u(n,r){this.UserNoteFlag=!SA.Edit,this.Type="text",e=this;if(n==null)return null;typeof r!="string"&&(r="");var i=this;this.Dialog=new SAM.Dialog(function(){i.DialogApplyCallback()}),this.Dialog.Title.text("Text Annotation Editor"),this.Dialog.Body.css({margin:"1em 2em"}),this.Dialog.TextInput=$("<textarea>").appendTo(this.Dialog.Body).css({width:"87%"}),this.Dialog.FontDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.FontLabel=$("<div>").appendTo(this.Dialog.FontDiv).text("Font (px):").css({display:"table-cell","text-align":"left"}),this.Dialog.FontInput=$('<input type="number">').appendTo(this.Dialog.FontDiv).val("12").css({display:"table-cell"}),this.Dialog.ColorDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.ColorLabel=$("<div>").appendTo(this.Dialog.ColorDiv).text("Color:").css({display:"table-cell","text-align":"left"}),this.Dialog.ColorInput=$('<input type="color">').appendTo(this.Dialog.ColorDiv).val("#30ff00").css({display:"table-cell"}),this.Dialog.VisibilityModeDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.VisibilityModeLabel=$("<div>").appendTo(this.Dialog.VisibilityModeDiv).text("Visibility:").css({display:"table-cell","text-align":"left"}),this.Dialog.VisibilityModeInputButtons=$("<div>").appendTo(this.Dialog.VisibilityModeDiv).attr("checked","false").css({display:"table-cell"}),this.Dialog.VisibilityModeInputs=[],this.Dialog.VisibilityModeInputs[0]=$('<input type="radio" name="visibilityoptions" value="0">Text only</input>').appendTo(this.Dialog.VisibilityModeInputButtons).attr("checked","false"),$("<br>").appendTo(this.Dialog.VisibilityModeInputButtons),this.Dialog.VisibilityModeInputs[1]=$('<input type="radio" name="visibilityoptions" value="1">Arrow only, text on hover</input>').appendTo(this.Dialog.VisibilityModeInputButtons).attr("checked","false"),$("<br>").appendTo(this.Dialog.VisibilityModeInputButtons),this.Dialog.VisibilityModeInputs[2]=$('<input type="radio" name="visibilityoptions" value="2">Arrow and text visible</input>').appendTo(this.Dialog.VisibilityModeInputButtons).attr("checked","true"),this.Dialog.BackgroundDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.BackgroundLabel=$("<div>").appendTo(this.Dialog.BackgroundDiv).text("Background:").css({display:"table-cell","text-align":"left"}),this.Dialog.BackgroundInput=$('<input type="checkbox">').appendTo(this.Dialog.BackgroundDiv).attr("checked","true").css({display:"table-cell"}),this.Layer=n,this.Popup=new SAM.WidgetPopup(this),this.State=t,this.CursorLocation=0;var s=this.Layer.GetCamera();this.Text=new SAM.Text,this.Text.String=r,this.Text.UpdateBuffers(this.Layer.AnnotationView),this.Text.Color=[0,0,1],this.Text.Anchor=[.5*(this.Text.PixelBounds[0]+this.Text.PixelBounds[1]),.5*(this.Text.PixelBounds[2]+this.Text.PixelBounds[3])],this.Text.Position=[s.FocalPoint[0],s.FocalPoint[1],0],this.Arrow=new SAM.Arrow,this.Arrow.Origin=this.Text.Position,this.Arrow.Length=50,this.Arrow.Width=10,this.Arrow.UpdateBuffers(this.Layer.AnnotationView),this.Arrow.Visibility=!0,this.Arrow.Orientation=45,this.Arrow.FillColor=[0,0,1],this.Arrow.OutlineColor=[1,1,0],this.Arrow.ZOffset=.2,this.Arrow.UpdateBuffers(this.Layer.AnnotationView),n.AddWidget(this),this.ActiveReason=1,this.SetTextOffset(50,0),this.VisibilityMode=2,this.Text.BackgroundFlag=!0,this.Dialog.BackgroundInput.prop("checked",!0);var o=SAM.ConvertColorToHex(this.Dialog.ColorInput.val());if(localStorage.TextWidgetDefaults){var u=JSON.parse(localStorage.TextWidgetDefaults);u.Color&&(o=SAM.ConvertColorToHex(u.Color)),u.FontSize&&(this.Text.Size=parseFloat(u.FontSize)),u.BackgroundFlag!==undefined&&(this.Text.BackgroundFlag=u.BackgroundFlag),u.VisibilityMode!==undefined&&this.SetVisibilityMode(u.VisibilityMode)}this.Text.Color=o,this.Arrow.SetFillColor(o),this.Arrow.ChooseOutlineColor(),this.CreationCamera=n.GetCamera().Serialize()}var e,t=0,n=1,r=2,i=3,s=4,o=5;u.prototype.Draw=function(e){this.VisibilityMode!=0&&this.Arrow.Draw(e),this.VisibilityMode!=1||this.State!=t?(this.Text.Draw(e),this.Text.Visibility=!0):this.Text.Visibility=!1},u.prototype.PasteCallback=function(e,t){this.Load(e),this.Text.Position[0]=t[0],this.Text.Position[1]=t[1],this.UpdateArrow(),this.Layer.EventuallyDraw(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote()},u.prototype.Serialize=function(){if(this.Text===undefined)return null;var e=new Object;return e.type="text",e.user_note_flag=this.UserNoteFlag,e.color=this.Text.Color,e.size=this.Text.Size,e.offset=[-this.Text.Anchor[0],-this.Text.Anchor[1]],e.position=this.Text.Position,e.string=this.Text.String,e.visibility=this.VisibilityMode,e.backgroundFlag=this.Text.BackgroundFlag,e.creation_camera=this.CreationCamera,e},u.prototype.Load=function(e){this.UserNoteFlag=e.user_note_flag;var t=e.string;if(e.string&&e.string==""){this.Layer.RemoveWidget(this);return}this.Text.String=e.string;var n=[parseFloat(e.color[0]),parseFloat(e.color[1]),parseFloat(e.color[2])];this.Text.Color=n,this.Text.Size=parseFloat(e.size),e.backgroundFlag!=undefined&&(this.Text.BackgroundFlag=e.backgroundFlag),this.Text.Position=[parseFloat(e.position[0]),parseFloat(e.position[1]),parseFloat(e.position[2])],this.Arrow.Origin=this.Text.Position,e.offset&&this.SetTextOffset(parseFloat(e.offset[0]),parseFloat(e.offset[1])),e.creation_camera!==undefined&&(this.CreationCamera=e.creation_camera),e.anchorVisibility!==undefined?e.anchorVisibility?this.SetVisibilityMode(1):this.SetVisibilityMode(0):e.visibility!==undefined&&this.SetVisibilityMode(e.visibility),this.Arrow.SetFillColor(n),this.Arrow.ChooseOutlineColor(),this.Text.UpdateBuffers(this.Layer.AnnotationView),this.UpdateArrow()},u.prototype.SetTextOffset=function(e,t){this.SavedTextAnchor=[-e,-t],this.Text.Anchor=this.SavedTextAnchor.slice(0),this.UpdateArrow()},u.prototype.SetPosition=function(e,t){this.Text.Position=[e,t,0],this.Arrow.Origin=this.Text.Position},u.prototype.SetVisibilityMode=function(e){if(this.VisibilityMode==e)return;this.VisibilityMode=e,e==2||e==1?(this.SavedTextAnchor==undefined&&(this.SavedTextAnchor=[-30,0]),this.Text.Anchor=this.SavedTextAnchor.slice(0),this.Arrow.Visibility=!0,this.Arrow.Origin=this.Text.Position,this.UpdateArrow()):e==0&&(this.SavedTextAnchor=this.Text.Anchor.slice(0),this.Text.UpdateBuffers(this.Layer.AnnotationView),this.Text.Anchor=[(this.Text.PixelBounds[0]+this.Text.PixelBounds[1])*.5,this.Text.PixelBounds[2]],this.Arrow.Visibility=!1),this.Layer.EventuallyDraw()},u.prototype.UpdateArrow=function(){var e=.5*(this.Text.PixelBounds[0]+this.Text.PixelBounds[1]),t=.5*(this.Text.PixelBounds[2]+this.Text.PixelBounds[3]),n=.5*(this.Text.PixelBounds[1]-this.Text.PixelBounds[0]),r=.5*(this.Text.PixelBounds[3]-this.Text.PixelBounds[2]),i=this.Text.Anchor[0]-e,s=this.Text.Anchor[1]-t;this.Arrow.Orientation=-(180+Math.atan2(s,i)*180/Math.PI);var o=Math.sqrt(i*i+s*s),u=o;if(s!=0){var a=Math.abs(o*r/s);u>a&&(u=a)}if(i!=0){var a=Math.abs(o*n/i);u>a&&(u=a)}o=o-u-5,o<5&&(o=5),this.Arrow.Length=o,this.Arrow.UpdateBuffers(this.Layer.AnnotationView)},u.prototype.HandleMouseWheel=function(e){return!1},u.prototype.HandleKeyDown=function(e){if(this.State==o)return!1;if(e.keyCode==67&&e.ctrlKey){var t={Type:"TextWidget",Data:this.Serialize()};return localStorage.ClipBoard=JSON.stringify(t),!1}return!0},u.prototype.HandleDoubleClick=function(e){return this.ShowPropertiesDialog(),!1},u.prototype.HandleMouseDown=function(e){if(e.which==1){var t=e.offsetX,o=e.offsetY,u=this.Layer.GetCamera();return this.LastMouse=[t,o],this.State==n?this.State=i:this.State==r&&(this.State=s),!1}return!0},u.prototype.HandleMouseUp=function(e){if(e.which==1)return this.State==i?(this.State=n,window.SA&&SA.RecordState(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote()):this.State==s&&(this.State=r,window.SA&&SA.RecordState(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote()),!1;if(e.which==3)if(this.State==n||this.State==r)return this.State=o,this.ShowPropertiesDialog(),!1;return!0},u.prototype.ScreenPixelToTextPixelPoint=function(e,t){var n=this.Layer.GetCamera(),r=n.ConvertPointWorldToViewer(this.Text.Position[0],this.Text.Position[1]);return e=e-r[0]+this.Text.Anchor[0],t=t-r[1]+this.Text.Anchor[1],[e,t]},u.prototype.HandleMouseMove=function(e){if(this.State==i){var t=this.Layer.GetCamera(),n=t.ConvertPointViewerToWorld(this.LastMouse[0],this.LastMouse[1]),r=t.ConvertPointViewerToWorld(e.offsetX,e.offsetY),u=r[0]-n[0],a=r[1]-n[1];return this.LastMouse=[e.offsetX,e.offsetY],this.Text.Position[0]+=u,this.Text.Position[1]+=a,this.Arrow.Origin=this.Text.Position,this.PlacePopup(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),this.Layer.EventuallyDraw(),!1}if(this.State==s){var f=e.offsetX-this.LastMouse[0],l=e.offsetY-this.LastMouse[1];return this.LastMouse=[e.offsetX,e.offsetY],this.Text.Anchor[0]-=f,this.Text.Anchor[1]-=l,this.UpdateArrow(),this.PlacePopup(),this.Layer.EventuallyDraw(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),!1}return this.State!=o?this.CheckActive(e):!0},u.prototype.HandleTouchPan=function(e,t){return this.State==n?this.State=i:this.State==r&&(this.State=s),t.MouseDeltaX=t.MouseX-t.LastMouseX,t.MouseDeltaY=t.MouseY-t.LastMouseY,this.HandleMouseMove(e),!1},u.prototype.HandleTouchEnd=function(e){return this.State=n,this.SetActive(!1),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),!1},u.prototype.CheckActive=function(e){var t=this.ScreenPixelToTextPixelPoint(e.offsetX,e.offsetY);return this.Arrow.Visibility&&this.Arrow.PointInShape(t[0]-this.Text.Anchor[0],t[1]-this.Text.Anchor[1])?(this.Arrow.Active=!0,this.Layer.EventuallyDraw(),this.SetActive(!0,n),!0):this.Text.Visibility&&t[0]>this.Text.PixelBounds[0]&&t[0]<this.Text.PixelBounds[1]&&t[1]>this.Text.PixelBounds[2]&&t[1]<this.Text.PixelBounds[3]?(this.SetActive(!0,r),!0):(this.SetActive(!1),!1)},u.prototype.GetActive=function(){return this.State==n||this.State==o?!0:!1},u.prototype.Deactivate=function(){this.Popup.StartHideTimer(),this.State=t,this.Text.Active=!1,this.Arrow.Active=!1,this.Layer.DeactivateWidget(this),this.DeactivateCallback&&this.DeactivateCallback(),this.Layer.EventuallyDraw()},u.prototype.SetActive=function(e,i){i=i||n,e||(i=t);if(i==this.State)return;this.State=i,i==n?(this.Text.Active=!1,this.Arrow.Active=!0,this.Layer.ActivateWidget(this),this.PlacePopup(),this.Layer.EventuallyDraw()):i==r?(this.Text.Active=!0,this.Arrow.Active=!1,this.Layer.ActivateWidget(this),this.PlacePopup(),this.Layer.EventuallyDraw()):i==t&&this.Deactivate()},u.prototype.PlacePopup=function(){var e=this.Text.Position[0],t=this.Text.Position[1],n=this.Layer.GetCamera().ConvertPointWorldToViewer(e,t);n[0]+=this.Text.PixelBounds[1]-this.Text.Anchor[0],n[1]-=this.Text.Anchor[1]+10,this.Popup.Show(n[0],n[1])},u.prototype.ShowPropertiesDialog=function(){this.Popup.Hide(),this.Dialog.ColorInput.val(SAM.ConvertColorToHex(this.Text.Color)),this.Dialog.FontInput.val(this.Text.Size.toFixed(0)),this.Dialog.BackgroundInput.prop("checked",this.Text.BackgroundFlag),this.Dialog.TextInput.val(this.Text.String),this.Dialog.VisibilityModeInputs[this.VisibilityMode].attr("checked",!0),this.State=o,this.Dialog.Show(!0),this.Dialog.TextInput.focus()},u.prototype.DialogApplyCallback=function(){this.SetActive(!1),this.Popup.Hide(),this.ApplyLineBreaks();var e=this.Dialog.TextInput.val();e=e.trim();if(e==""){alert("Empty String");return}var t=SAM.ConvertColorToHex(this.Dialog.ColorInput.val()),n=this.Dialog.FontInput.val();this.Text.String=e,this.Text.Size=parseFloat(n),this.Text.UpdateBuffers(this.Layer.AnnotationView),this.Dialog.VisibilityModeInputs[0].prop("checked")?this.SetVisibilityMode(0):this.Dialog.VisibilityModeInputs[1].prop("checked")?this.SetVisibilityMode(1):this.SetVisibilityMode(2);var r=this.Dialog.BackgroundInput.prop("checked");this.Text.SetColor(t),this.Arrow.SetFillColor(t),this.Arrow.ChooseOutlineColor(),this.Text.BackgroundFlag=r,localStorage.TextWidgetDefaults=JSON.stringify({Color:t,FontSize:this.Text.Size,VisibilityMode:this.VisibilityMode,BackgroundFlag:r}),window.SA&&SA.RecordState(),this.Layer.EventuallyDraw(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote()},u.prototype.ApplyLineBreaks=function(){var e=this.Dialog.TextInput[0];e.setAttribute("wrap","off");var t=e.value;e.value="";var n=e.scrollWidth,r=-1;for(var i=0;i<t.length;i++){var s=t.charAt(i);if(s==" "||s=="-"||s=="+")r=i;e.value+=s;if(e.scrollWidth>n){var o="";if(r>=0){for(var u=r+1;u<i;u++)o+=t.charAt(u);r=-1}o+=s,e.value=e.value.substr(0,e.value.length-o.length),e.value+="\n"+o}}e.setAttribute("wrap","")},SAM.TextWidget=u})();(function(){"use strict";function e(){SAM.Shape.call(this),this.Origin=[0,0],this.Points=[],this.Closed=!1,this.Bounds=[0,-1,0,-1]}e.prototype=new SAM.Shape,e.prototype.SetLineWidth=function(e){this.LineWidth=e},e.prototype.GetLineWidth=function(){return this.LineWidth},e.prototype.GetEdgeLength=function(e){if(e<0||e>this.Points.length-2)return 0;var t=this.Points[e+1][0]-this.Points[e][0],n=this.Points[e+1][1]-this.Points[e][1];return Math.sqrt(t*t+n*n)},e.prototype.GetNumberOfPoints=function(){return this.Points.length},e.prototype.GetBounds=function(){var e=this.Bounds.slice(0);return e[0]+=this.Origin[0],e[1]+=this.Origin[0],e[2]+=this.Origin[1],e[3]+=this.Origin[1],e},e.prototype.ContainedInBounds=function(e){var t=this.GetBounds();return e[1]<t[0]||e[0]>t[1]||e[3]<t[2]||e[2]>t[3]?0:e[1]>=t[0]&&e[0]<=t[1]&&e[3]>=t[2]&&e[2]<=t[3]?2:1},e.prototype.SetOrigin=function(e){this.Origin=e.slice(0)},e.prototype.ResetOrigin=function(e){for(var t=0;t<this.Points.length;++t){var n=this.Points[t];n[0]+=this.Origin[0],n[1]+=this.Origin[1]}this.Origin[0]=0,this.Origin[1]=0,this.UpdateBuffers(e)},e.prototype.PointOnVertex=function(e,t){t*=t;for(var n=0;n<this.Points.length;++n){var r=this.Points[n][0]-e[0],i=this.Points[n][1]-e[1];if(r*r+i*i<t)return n}return-1},e.prototype.PointOnShape=function(e,t){e=e.slice(0),e[0]-=this.Origin[0],e[1]-=this.Origin[1];if(e[0]+t<this.Bounds[0]||e[0]-t>this.Bounds[1]||e[1]+t<this.Bounds[2]||e[1]-t>this.Bounds[3])return undefined;for(var n=1;n<this.Points.length;++n){var r=this.IntersectPointLine(e,this.Points[n-1],this.Points[n],t);if(r!==undefined)return[n-1,n,r]}if(this.Closed){var r=this.IntersectPointLine(e,this.Points[this.Points.length-1],this.Points[0],t);if(r!==undefined)return[this.Points.length-1,0,r]}return undefined},e.prototype.FindPopupPoint=function(e){if(this.Points.length==0)return;var t=e.Roll,n=Math.sin(t+Math.PI*.25),r=Math.cos(t+Math.PI*.25),i=this.Points[0],s=r*i[0]-n*i[1];for(var o=1;o<this.Points.length;++o){var u=this.Points[o],a=r*u[0]-n*u[1];a>s&&(s=a,i=u)}return i[0]+=this.Origin[0],i[1]+=this.Origin[1],i},e.prototype.ComputeArea=function(){if(this.Points.length<3)return 0;var e=0,t=0;for(var n=0;n<this.Points.length;++n)e+=this.Points[n][0],t+=this.Points[n][1];e/=this.Points.length,t/=this.Points.length;var r=0,i=this.Points.length-1,s=this.Points[i][0]-e,o=this.Points[i][1]-t;for(var n=0;n<this.Points.length;++n){var u=s,a=o;s=this.Points[n][0]-e,o=this.Points[n][1]-t,r+=(s*a-u*o)*.5}return r<0&&(r=-r),r},e.prototype.MergePoints=function(e,t){e*=e;var n=!1;for(var r=1;r<this.Points.length;++r){var i=this.Points[r][0]-this.Points[r-1][0],s=this.Points[r][1]-this.Points[r-1][1];i*i+s*s<e&&(this.Points.splice(r,1),--r,n=!0)}n&&this.UpdateBuffers(t)},e.prototype.Decimate=function(e,t){var n=!0;while(n){n=!1;var r=[];r.push(this.Points[0]);var i=3;while(i<this.Points.length){var s=this.Points[i],o=this.Points[i-1],u=this.Points[i-2],a=this.Points[i-3],f=(o[0]+u[0])*.5,l=(o[1]+u[1])*.5,c=s[1]-a[1],h=-(s[0]-a[0]),p=Math.sqrt(c*c+h*h);c/=p,h/=p,p=Math.abs(c*(f-this.Points[i-3][0])+h*(l-this.Points[i-3][1]));var d=(s[0]-o[0])*(a[0]-o[0])+(s[1]-o[1])*(a[1]-o[1]),v=(s[0]-u[0])*(a[0]-u[0])+(s[1]-u[1])*(a[1]-u[1]);p<e&&d<0&&v<0?(r.push([f,l]),n=!0,i+=2):(r.push(this.Points[i-2]),++i)}i-=2;while(i<this.Points.length)r.push(this.Points[i]),++i;this.Points=r}this.UpdateBuffers(t)},e.prototype.AddPointToBounds=function(e,t){e[0]-t<this.Bounds[0]&&(this.Bounds[0]=e[0]-t),e[0]+t>this.Bounds[1]&&(this.Bounds[1]=e[0]+t),e[1]-t<this.Bounds[2]&&(this.Bounds[2]=e[1]-t),e[1]+t>this.Bounds[3]&&(this.Bounds[3]=e[1]+t)},e.prototype.UpdateBuffers=function(e){e=e||{};var t=this.Points.slice(0);this.Closed&&t.length>2&&t.push(t[0]),this.PointBuffer=[];var n=[],r=[];this.Matrix=mat4.create(),mat4.identity(this.Matrix);if(this.Points.length==0)return;this.Bounds=[t[0][0],t[0][0],t[0][1],t[0][1]];if(this.LineWidth==0||!e.gl){for(var i=0;i<t.length;++i)this.PointBuffer.push(t[i][0]),this.PointBuffer.push(t[i][1]),this.PointBuffer.push(0),this.AddPointToBounds(t[i],0);for(var i=2;i<t.length;++i)n.push(0),n.push(i-1),n.push(i)}else{var s=[],o,u,a,f=t.length-1;for(var i=0;i<f;++i)u=t[i+1][0]-t[i][0],a=t[i+1][1]-t[i][1],o=Math.sqrt(u*u+a*a),s.push([-a/o,u/o]);if(f>0){var l=this.LineWidth/2,c=s[0][0]*l,h=s[0][1]*l;this.PointBuffer.push(t[0][0]-c),this.PointBuffer.push(t[0][1]-h),this.PointBuffer.push(0),this.PointBuffer.push(t[0][0]+c),this.PointBuffer.push(t[0][1]+h),this.PointBuffer.push(0),this.AddPointToBounds(t[i],l);for(var i=1;i<f;++i)this.PointBuffer.push(t[i][0]-c),this.PointBuffer.push(t[i][1]-h),this.PointBuffer.push(0),this.PointBuffer.push(t[i][0]+c),this.PointBuffer.push(t[i][1]+h),this.PointBuffer.push(0),c=s[i][0]*l,h=s[i][1]*l,this.PointBuffer.push(t[i][0]-c),this.PointBuffer.push(t[i][1]-h),this.PointBuffer.push(0),this.PointBuffer.push(t[i][0]+c),this.PointBuffer.push(t[i][1]+h),this.PointBuffer.push(0);this.PointBuffer.push(t[f][0]-c),this.PointBuffer.push(t[f][1]-h),this.PointBuffer.push(0),this.PointBuffer.push(t[f][0]+c),this.PointBuffer.push(t[f][1]+h),this.PointBuffer.push(0)}for(var i=0;i<f;++i)r.push(0+4*i),r.push(1+4*i),r.push(3+4*i),r.push(0+4*i),r.push(3+4*i),r.push(2+4*i);for(var i=2;i<t.length;++i)n.push(0),n.push(2*i-1),n.push(2*i)}e.gl&&(this.VertexPositionBuffer=e.gl.createBuffer(),e.gl.bindBuffer(e.gl.ARRAY_BUFFER,this.VertexPositionBuffer),e.gl.bufferData(e.gl.ARRAY_BUFFER,new Float32Array(this.PointBuffer),e.gl.STATIC_DRAW),this.VertexPositionBuffer.itemSize=3,this.VertexPositionBuffer.numItems=this.PointBuffer.length/3,this.CellBuffer=e.gl.createBuffer(),e.gl.bindBuffer(e.gl.ELEMENT_ARRAY_BUFFER,this.CellBuffer),e.gl.bufferData(e.gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(n),e.gl.STATIC_DRAW),this.CellBuffer.itemSize=1,this.CellBuffer.numItems=n.length,this.LineWidth!=0&&(this.LineCellBuffer=e.gl.createBuffer(),e.gl.bindBuffer(e.gl.ELEMENT_ARRAY_BUFFER,this.LineCellBuffer),e.gl.bufferData(e.gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(r),e.gl.STATIC_DRAW),this.LineCellBuffer.itemSize=1,this.LineCellBuffer.numItems=r.length))};var t=0,n=2*Math.PI*0/24,r=0,i="edge",s=200;e.prototype.SampleEdge=function(e,t,n,r,i){var s=e.GetCamera(),o=s.GetHeight()/s.ViewportHeight;n*=o;var u=e.GetCache(),a=[t,t],f=n/2;this.RecursiveSampleEdge(this.Points.length-1,0,f,n,r,u,a,o,i)},e.prototype.RecursiveSampleEdge=function(e,o,u,a,f,l,c,h,p){var d=this.Points[e],v=this.Points[o],m=v[0]-d[0],g=v[1]-d[1],y=Math.sqrt(m*m+g*g);if(u>y)u-=y,e=o,o+=1,o<this.Points.length?this.RecursiveSampleEdge(e,o,u,a,f,l,c,h,p):p();else{var b=this,w=-Math.atan2(g,m)+n,E=u/y,S=d[0]+E*(v[0]-d[0]),x=d[1]+E*(v[1]-d[1]),T=-g,N=m,C=Math.sqrt(T*T+N*N);T=T/C*r*h,N=N/C*r*h,SA.GetCutoutImage(l,c,[S+T,x+N],h,w,i+f+".png",function(){setTimeout(function(){++f,t=f,u+=a,b.RecursiveSampleEdge(e,o,u,a,f,l,c,h,p)},s)})}},e.prototype.SetActive=function(e){this.Active=e},SAM.Polyline=e})();(function(){"use strict";function a(e,t){this.AddedToVolume=!1;if(e===undefined)return;this.UserNoteFlag=!SA.Edit,this.Type="polyline",this.Circle=new SAM.Circle,this.Polyline=new SAM.Polyline,this.InitializeDialog(),this.LineWidth=10,this.Polyline.Closed=!1;if(localStorage.PolylineWidgetDefaults){var r=JSON.parse(localStorage.PolylineWidgetDefaults);r.Color&&this.Dialog.ColorInput.val(SAM.ConvertColorToHex(r.Color)),r.LineWidth&&(this.LineWidth=r.LineWidth,this.Dialog.LineWidthInput.val(this.LineWidth))}this.Circle.FillColor=[1,1,.2],this.Circle.OutlineColor=[0,0,0],this.Circle.FixedSize=!1,this.Circle.ZOffset=-0.05,this.Polyline.OutlineColor=[0,0,0],this.Polyline.SetOutlineColor(this.Dialog.ColorInput.val()),this.Polyline.FixedSize=!1,this.Layer=e,this.Popup=new SAM.WidgetPopup(this);var s=e.GetCamera();this.Layer.AddWidget(this),this.Polyline.LineWidth=this.LineWidth,this.Circle.Radius=this.LineWidth,this.Circle.UpdateBuffers(this.Layer.AnnotationView),this.ActiveVertex=-1,this.ActiveEdge=undefined,this.DrawingVertex=-1,t?(this.State=n,this.SetCursorToDrawing(),this.Layer.ActivateWidget(this)):(this.State=i,this.Circle.Visibility=!1),this.CreationCamera=e.GetCamera().Serialize(),this.MinLine=1,this.Layer.EventuallyDraw(!1)}var e=8,t=4,n=0,r=1,i=2,s=3,o=5,u=6;a.prototype.InitializeDialog=function(){var e=this;this.Dialog=new SAM.Dialog(function(){e.DialogApplyCallback()}),this.Dialog.Title.text("Lasso Annotation Editor"),this.Dialog.Body.css({margin:"1em 2em"}),this.Dialog.ColorDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.ColorLabel=$("<div>").appendTo(this.Dialog.ColorDiv).text("Color:").css({display:"table-cell","text-align":"left"}),this.Dialog.ColorInput=$('<input type="color">').appendTo(this.Dialog.ColorDiv).val("#30ff00").css({display:"table-cell"}),this.Dialog.ClosedDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.ClosedLabel=$("<div>").appendTo(this.Dialog.ClosedDiv).text("Closed:").css({display:"table-cell","text-align":"left"}),this.Dialog.ClosedInput=$('<input type="checkbox">').appendTo(this.Dialog.ClosedDiv).attr("checked","false").css({display:"table-cell"}),this.Dialog.LineWidthDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.LineWidthLabel=$("<div>").appendTo(this.Dialog.LineWidthDiv).text("Line Width:").css({display:"table-cell","text-align":"left"}),this.Dialog.LineWidthInput=$('<input type="number">').appendTo(this.Dialog.LineWidthDiv).css({display:"table-cell"}).keypress(function(e){return e.keyCode!=13}),this.Dialog.LengthDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.LengthLabel=$("<div>").appendTo(this.Dialog.LengthDiv).text("Length:").css({display:"table-cell","text-align":"left"}),this.Dialog.Length=$("<div>").appendTo(this.Dialog.LengthDiv).css({display:"table-cell"}),this.Dialog.AreaDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.AreaLabel=$("<div>").appendTo(this.Dialog.AreaDiv).text("Area:").css({display:"table-cell","text-align":"left"}),this.Dialog.Area=$("<div>").appendTo(this.Dialog.AreaDiv).css({display:"table-cell"})},a.prototype.Draw=function(e){var t=this.Layer.GetCamera();this.MinLine=t.GetSpacing(),this.LineWidth<this.MinLine?this.Polyline.LineWidth=0:this.Polyline.LineWidth=this.LineWidth,this.Polyline.Draw(e),this.Circle.Draw(e),this.Text&&(this.PositionText(),this.Text.Draw(e))},a.prototype.PasteCallback=function(e,t){this.Load(e);var n=this.Polyline.GetBounds();if(!n){console.log("Warining: Pasting empty polyline");return}var r=t[0]-(n[0]+n[1])/2,i=t[1]-(n[2]+n[3])/2;for(var s=0;s<this.Polyline.GetNumberOfPoints();++s)this.Polyline.Points[s][0]+=r,this.Polyline.Points[s][1]+=i;this.Polyline.UpdateBuffers(this.Layer.AnnotationView),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),this.Layer.EventuallyDraw(!0)},a.prototype.Serialize=function(){if(this.Polyline===undefined)return null;var e=new Object;e.type="polyline",e.user_note_flag=this.UserNoteFlag,e.outlinecolor=this.Polyline.OutlineColor,e.linewidth=this.LineWidth,e.points=[];for(var t=0;t<this.Polyline.GetNumberOfPoints();++t)e.points.push([this.Polyline.Points[t][0],this.Polyline.Points[t][1]]);return e.creation_camera=this.CreationCamera,e.closedloop=this.Polyline.Closed,this.Text&&(e.text=this.Text.String),e},a.prototype.InitializeText=function(){if(this.Text)return;this.Text=new SAM.Text,this.Text.String="Hello",this.Text.UpdateBuffers(this.Layer.AnnotationView),this.Text.Color=[0,0,1],this.Text.Anchor=[.5*(this.Text.PixelBounds[0]+this.Text.PixelBounds[1]),.5*(this.Text.PixelBounds[2]+this.Text.PixelBounds[3])],this.Text.Position=[100,100,0],this.Text.BackgroundFlag=!1},a.prototype.PositionText=function(){var e=this.Polyline.GetBounds(),t=(e[0]+e[1])/2,n=e[2];this.Text.Position=[t,n-40,0]},a.prototype.Load=function(e){this.Polyline.OutlineColor[0]=parseFloat(e.outlinecolor[0]),this.Polyline.OutlineColor[1]=parseFloat(e.outlinecolor[1]),this.Polyline.OutlineColor[2]=parseFloat(e.outlinecolor[2]),this.LineWidth=parseFloat(e.linewidth),this.Polyline.LineWidth=this.LineWidth,this.Polyline.Points=[];for(var t=0;t<e.points.length;t++)this.Polyline.Points[t]=[parseFloat(e.points[t][0]),parseFloat(e.points[t][1])];this.Polyline.Closed=e.closedloop,this.Polyline.UpdateBuffers(this.Layer.AnnotationView),this.UserNoteFlag=e.user_note_flag,e.text&&(this.Text||this.InitializeText(),this.Text.String=e.text),e.view_height!==undefined&&(this.CreationCamera=e.creation_camera)},a.prototype.CityBlockDistance=function(e,t){return Math.abs(t[0]-e[0])+Math.abs(t[1]-e[1])},a.prototype.HandleKeyDown=function(e){if(e.keyCode==65&&!this.AddedToVolume){SAM.Volume||(SAM.Volume=0,SAM.VolumeArray=[]);var t=this.ComputeArea()*.25*.25;SAM.Volume+=t,SAM.VolumeArray.push(t),this.AddedToVolume=!0,console.log(SAM.VolumeArray.length.toString()+" : "+SAM.Volume.toString())}if(e.keyCode==67&&e.ctrlKey){var n={Type:"PolylineWidget",Data:this.Serialize()};return localStorage.ClipBoard=JSON.stringify(n),!1}return e.keyCode==27||e.keyCode==32||e.keyCode==13?(this.Layer.DeactivateWidget(this),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),window.SA&&SA.RecordState(),!1):!0},a.prototype.HandleDoubleClick=function(e){if(this.State==n||this.State==r)return this.Polyline.MergePoints(this.Circle.Radius),this.Layer.DeactivateWidget(this),!1;if(this.State==o){var t=e.offsetX,i=e.offsetY,s=this.Layer.GetCamera().ConvertPointViewerToWorld(t,i);if(this.ActiveVertex!=-1)this.Polyline.Points[this.ActiveVertex]=s,this.DrawingVertex=this.ActiveVertex,this.ActiveVertex=-1;else{if(!this.ActiveEdge)return console.log("No vertex or edge is active."),!1;this.Polyline.Points.splice(this.ActiveEdge[1],0,s),this.DrawingVertex=this.ActiveEdge[1],this.ActiveEdge=undefined}return this.Polyline.UpdateBuffers(this.Layer.AnnotationView),this.SetCursorToDrawing(),this.State=n,this.Layer.EventuallyDraw(!1),!1}},a.prototype.HandleMouseDown=function(e){return e.which==1&&this.State==o&&(this.Popup.Hide(),this.Circle.FillColor=this.Polyline.OutlineColor,this.Circle.Active=!1),!1},a.prototype.HandleClick=function(e){return this.HandleMouseUp(e)},a.prototype.HandleMouseUp=function(e){if(e.which==3)return this.State=u,this.ShowPropertiesDialog(),!1;if(e.which!=1)return!1;if(this.State==o)return this.Polyline.MergePoints(this.Circle.Radius),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),window.SA&&SA.RecordState(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),!1;var t=e.offsetX,i=e.offsetY,s=this.Layer.GetCamera().ConvertPointViewerToWorld(t,i);if(this.State==n){if(this.Polyline.GetNumberOfPoints()>0)return!1;this.Polyline.Points.push(s),this.DrawingVertex=this.Polyline.GetNumberOfPoints()-1,this.State=r}return this.State==r?this.Polyline.GetNumberOfPoints()>2&&this.ActiveVertex==0?(this.Polyline.Points.pop(),this.Polyline.Closed=!0,this.Layer.DeactivateWidget(this),window.SA&&SA.RecordState(),!1):(this.DrawingVertex+=1,this.Polyline.Points.splice(this.DrawingVertex,0,s),this.Polyline.UpdateBuffers(this.Layer.AnnotationView),this.Layer.EventuallyDraw(!0),!1):(this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),!1)},a.prototype.HandleDrag=function(e){if(this.ActiveEdge){var t=this.Polyline.Points[this.ActiveEdge[0]],n=this.Polyline.Points[this.ActiveEdge[1]],r=t[0]+this.ActiveEdge[2]*(n[0]-t[0]),i=t[1]+this.ActiveEdge[2]*(n[1]-t[1]);this.Polyline.Points.splice(this.ActiveEdge[1],0,[r,i]),this.ActiveVertex=this.ActiveEdge[1],this.ActiveEdge=undefined,this.HighlightVertex(this.ActiveVertex),this.Circle.Active=!1}if(this.ActiveVertex==-1)return!1;this.Circle.Active=!1,this.Polyline.Points[this.ActiveVertex]=e,this.ActiveVertex>0&&this.Polyline.GetEdgeLength(this.ActiveVertex-1)<this.Circle.Radius&&(this.Circle.Active=!0,e=this.Polyline.Points[this.ActiveVertex-1].slice(0)),this.ActiveVertex<this.Polyline.GetNumberOfPoints()-1&&this.Polyline.GetEdgeLength(this.ActiveVertex)<this.Circle.Radius&&(this.Circle.Active=!0,e=this.Polyline.Points[this.ActiveVertex+1].slice(0)),this.Polyline.Points[this.ActiveVertex]=e,this.Circle.Origin=e,this.Polyline.UpdateBuffers(this.Layer.AnnotationView),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),this.Layer.EventuallyDraw(!0)},a.prototype.StartDrawing=function(e){if(this.Polyline.GetNumberOfPoints()==0)return;this.DrawingVertex==0&&(this.Polyline.Points.reverse(),this.DrawingVertex=this.Polyline.GetNumberOfPoints()-1);if(this.DrawingVertex==this.Polyline.GetNumberOfPoints()-1){this.Polyline.Points.push(e),this.DrawingVertex+=1,this.State=r;return}var t=this.Polyline.Points[this.DrawingVertex-1],n=this.Polyline.Points[this.DrawingVertex],i=this.Polyline.Points[this.DrawingVertex+1],s=e[0]-n[0],o=e[1]-n[1],u=[t[0]-n[0],t[1]-n[1]],a=Math.sqrt(u[0]*u[0]+u[1]*u[1]);a=(s*u[0]+o*u[1])/a;var f=[i[0]-n[0],i[1]-n[1]],l=Math.sqrt(f[0]*f[0]+f[1]*f[1]);return l=(s*f[0]+o*f[1])/a,a>l&&(this.Polyline.Points.reverse(),this.DrawingVertex=this.Polyline.GetNumberOfPoints()-this.DrawingVertex-1),this.DrawingVertex+=1,this.Polyline.Points.splice(this.DrawingVertex,0,e),this.State=r,!1},a.prototype.HandleMouseMove=function(e){var t=e.offsetX,i=e.offsetY,s=this.Layer.GetCamera().ConvertPointViewerToWorld(t,i);if(this.State==n)return this.StartDrawing(s),!1;if(this.State==r){this.Polyline.Points[this.DrawingVertex]=s,this.Polyline.UpdateBuffers(this.Layer.AnnotationView);var u=this.Polyline.PointOnVertex(s,this.Circle.Radius);return this.DrawingVertex==this.Polyline.GetNumberOfPoints()-1&&u==0?this.HighlightVertex(0):this.HighlightVertex(-1),!1}if(this.State==o){if(e.which==0)return this.CheckActive(e)?this.UpdateActiveCircle():this.Layer.DeactivateWidget(this),!1;this.State==o&&e.which==1&&this.HandleDrag(s)}},a.prototype.CheckActive=function(n){var i=n.offsetX,s=n.offsetY,o=this.Layer.GetCamera().ConvertPointViewerToWorld(i,s),u;this.ActiveEdge=undefined,u=e/this.Layer.GetPixelsPerUnit(),u=Math.max(u,this.Polyline.GetLineWidth()),this.ActiveVertex=this.Polyline.PointOnVertex(o,u);if(this.State==r)return this.Polyline.GetNumberOfPoints()<2||this.ActiveVertex!=0?(this.ActiveVertex=-1,!1):!0;if(this.ActiveVertex==-1){u=t/this.Layer.GetPixelsPerUnit(),u=Math.max(u,this.Polyline.GetLineWidth()/2),this.ActiveEdge=this.Polyline.PointOnShape(o,u*2);if(!this.ActiveEdge)return!1}return!0},a.prototype.HighlightVertex=function(t){t<0||t>=this.Polyline.GetNumberOfPoints()?this.Circle.Visibility=!1:(this.Circle.Visibility=!0,this.Circle.Active=!0,this.Circle.Radius=e/this.Layer.GetPixelsPerUnit(),this.CircleRadius=Math.max(this.CircleRadius,this.Polyline.GetLineWidth()*1.5),this.Circle.UpdateBuffers(this.Layer.AnnotationView),this.Circle.Origin=this.Polyline.Points[t]),this.ActiveVertex=t,this.Layer.EventuallyDraw(!0)},a.prototype.UpdateActiveCircle=function(){if(this.ActiveVertex!=-1){this.HighlightVertex(this.ActiveVertex);return}if(this.ActiveEdge){this.Circle.Visibility=!0,this.Circle.Active=!0,this.Circle.Radius=t/this.Layer.GetPixelsPerUnit(),this.CircleRadius=Math.max(this.CircleRadius,this.Polyline.GetLineWidth());var e=this.Polyline.Points[this.ActiveEdge[0]],n=this.Polyline.Points[this.ActiveEdge[1]],r=e[0]+this.ActiveEdge[2]*(n[0]-e[0]),i=e[1]+this.ActiveEdge[2]*(n[1]-e[1]);this.Circle.Origin=[r,i,0],this.Circle.UpdateBuffers(this.Layer.AnnotationView)}else this.Circle.Visibility=!1;this.Layer.EventuallyDraw(!1)},a.prototype.GetActive=function(){return this.State==i?!1:!0},a.prototype.SetActive=function(e){if(e==this.GetActive())return;e?(this.State=o,this.UpdateActiveCircle(),this.PlacePopup()):(this.Popup.StartHideTimer(),this.State=i,this.DrawingVertex=-1,this.ActiveVertex=-1,this.ActiveEdge=undefined,this.Circle.Visibility=!1,this.DeactivateCallback&&this.DeactivateCallback(),this.Polyline.GetNumberOfPoints()<2&&this.Layer&&this.Layer.RemoveWidget(this)),this.Layer.EventuallyDraw(!1)},a.prototype.SetCursorToDrawing=function(){this.Popup.Hide(),this.Layer.GetCanvasDiv().css({cursor:"url("+SAM.ImagePathUrl+"dotCursor8.png) 4 4,crosshair"}),this.Layer.EventuallyDraw()},a.prototype.PlacePopup=function(){if(this.State==r||this.State==n)return;var e=this.Polyline.FindPopupPoint(this.Layer.GetCamera());e=this.Layer.GetCamera().ConvertPointWorldToViewer(e[0],e[1]),e[0]+=20,e[1]-=10,this.Popup.Show(e[0],e[1])};var f;a.prototype.ShowPropertiesDialog=function(){this.Dialog.ColorInput.val(SAM.ConvertColorToHex(this.Polyline.OutlineColor)),this.Dialog.ClosedInput.prop("checked",this.Polyline.Closed),this.Dialog.LineWidthInput.val(this.Polyline.LineWidth.toFixed(2));var e=this.ComputeLength()*.25,t="";this.Polyline.FixedSize?(t+=e.toFixed(2),t+=" px"):e>1e3?t+=(e/1e3).toFixed(2)+" mm":t+=e.toFixed(2)+" µm",this.Dialog.Length.text(t);if(this.Polyline.Closed){this.Dialog.AreaDiv.show();var n=this.ComputeArea()*.25*.25,r="";this.Polyline.FixedSize?(r+=n.toFixed(2),r+=" pixels^2"):n>1e6?r+=(n/1e6).toFixed(2)+" mm^2":r+=n.toFixed(2)+" µm^2",this.Dialog.Area.text(r)}else this.Dialog.AreaDiv.hide();this.Dialog.Show(!0)},a.prototype.DialogApplyCallback=function(){var e=this.Dialog.ColorInput.val();this.Polyline.SetOutlineColor(e),this.Polyline.Closed=this.Dialog.ClosedInput.prop("checked"),this.LineWidth=parseFloat(this.Dialog.LineWidthInput.val()),this.Polyline.UpdateBuffers(this.Layer.AnnotationView),this.SetActive(!1),window.SA&&SA.RecordState(),this.Layer.EventuallyDraw(!1),localStorage.PolylineWidgetDefaults=JSON.stringify({Color:e,ClosedLoop:this.Polyline.Closed,LineWidth:this.LineWidth}),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote()},a.prototype.ComputeArea=function(){return this.Polyline.ComputeArea()},a.prototype.ComputeLength=function(){if(this.Polyline.GetNumberOfPoints()<2)return 0;var e=0,t=this.Polyline.Points[0][0],n=this.Polyline.Points[0][1];for(var r=1;r<this.Polyline.GetNumberOfPoints();++r){var i=this.Polyline.Points[r][0],s=this.Polyline.Points[r][1],o=i-t,u=s-n;t=i,n=s,e+=Math.sqrt(o*o+u*u)}return e},a.prototype.PointInside=function(e,t){if(this.Polyline.Closed==0)return!1;var n,r,i=this.Polyline.GetNumberOfPoints()-1,s=0,o=0,u=this.Polyline.Points[i];u=[u[0]-e,u[1]-t];for(var a=0;a<=i;++a){var f=this.Polyline.Points[a];f=[f[0]-e,f[1]-t];var l;l=f[1]-u[1],l!=0&&(l=-u[1]/l,l>0&&l<=1&&(n=u[0]+l*(f[0]-u[0]),n>0&&(s+=1),n<0&&(o+=1))),u=f}return s%2&&o%2?!0:!1},a.prototype.Sample=function(e,t,n,r,i){var s=this.Polyline.GetBounds(),o=this.Layer.Context2d;for(var u=s[2];u<s[3];u+=n)for(var a=s[0];a<s[1];a+=n)if(this.PointInside(a,u)){ip=this.Layer.GetCamera().ConvertPointWorldToViewer(a,u),ip[0]=Math.round(ip[0]-e/2),ip[1]=Math.round(ip[1]-e/2);var f=o.getImageData(ip[0],ip[1],e,e);DownloadImageData(f,r+"_"+i+".png"),++i}},a.prototype.SampleStack=function(e,t,n,r,i){var s=LAYERS[0].GetCache(),o=this.Polyline.GetBounds();for(var u=o[2];u<o[3];u+=n)for(var a=o[0];a<o[1];a+=n)this.PointInside(a,u)&&SA.GetCutoutimage(s,e,[a,u],t,0,null,function(e){DownloadImageData(e,r+"_"+i+".png"),++i})},a.prototype.DownloadStack=function(e,t,n,r,i){var s=LAYERS[0].GetCache();for(var o=0;o<3;++o)levelSpacing=r<<o,SA.GetCutoutImage(s,n,[e,t],levelSpacing,0,i+o+".png",null)},SAM.PolylineWidget=a})();(function(){"use strict";function i(e,t){if(e==null)return;this.UserNoteFlag=!SA.Edit,this.Type="pencil";var n=this;this.Dialog=new SAM.Dialog(function(){n.DialogApplyCallback()}),this.Dialog.Title.text("Pencil Annotation Editor"),this.Dialog.Body.css({margin:"1em 2em"}),this.Dialog.ColorDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.ColorLabel=$("<div>").appendTo(this.Dialog.ColorDiv).text("Color:").css({display:"table-cell","text-align":"left"}),this.Dialog.ColorInput=$('<input type="color">').appendTo(this.Dialog.ColorDiv).val("#30ff00").css({display:"table-cell"}),this.Dialog.LineWidthDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.LineWidthLabel=$("<div>").appendTo(this.Dialog.LineWidthDiv).text("Line Width:").css({display:"table-cell","text-align":"left"}),this.Dialog.LineWidthInput=$('<input type="number">').appendTo(this.Dialog.LineWidthDiv).css({display:"table-cell"}).keypress(function(e){return e.keyCode!=13}),this.LineWidth=0;if(localStorage.PencilWidgetDefaults){var i=JSON.parse(localStorage.PencilWidgetDefaults);i.Color&&this.Dialog.ColorInput.val(SAM.ConvertColorToHex(i.Color)),i.LineWidth&&(this.LineWidth=i.LineWidth,this.Dialog.LineWidthInput.val(this.LineWidth))}this.Layer=e,this.Popup=new SAM.WidgetPopup(this),this.Layer.AddWidget(this);var n=this;this.Shapes=new SAM.ShapeGroup,this.SetStateToDrawing(),t||(this.State=r,this.Layer.GetCanvasDiv().css({cursor:"default"})),this.CreationCamera=e.GetCamera().Serialize()}var e=0,t=1,n=2,r=3;i.prototype.SetStateToDrawing=function(){this.State=e,this.Shapes.SetActive(!1),this.Popup.Hide(),this.Layer.GetCanvasDiv().css({cursor:"url("+SAM.ImagePathUrl+"Pencil-icon.png) 0 24,crosshair"}),this.Layer.EventuallyDraw()},i.prototype.Draw=function(e){this.Shapes.Draw(e)},i.prototype.Serialize=function(){var e=new Object;e.type="pencil",e.user_note_flag=this.UserNoteFlag,e.shapes=[];for(var t=0;t<this.Shapes.GetNumberOfShapes();++t){var n=this.Shapes.GetShape(t),r=[];for(var i=0;i<n.Points.length;++i)r.push([n.Points[i][0],n.Points[i][1]]);e.shapes.push(r),e.outlinecolor=n.OutlineColor,e.linewidth=n.LineWidth}return e.creation_camera=this.CreationCamera,e},i.prototype.Load=function(e){this.LineWidth=parseFloat(e.linewidth),this.UserNoteFlag=e.user_note_flag,e.linewidth&&(this.LineWidth=parseFloat(e.linewidth));var t=SAM.ConvertColor(this.Dialog.ColorInput.val());e.outlinecolor&&(t[0]=parseFloat(e.outlinecolor[0]),t[1]=parseFloat(e.outlinecolor[1]),t[2]=parseFloat(e.outlinecolor[2]));for(var n=0;n<e.shapes.length;n++){var r=e.shapes[n],i=new SAM.Polyline;i.SetOutlineColor(t),i.FixedSize=!1,i.LineWidth=this.LineWidth,this.Shapes.AddShape(i);for(var s=0;s<r.length;++s)i.Points[s]=[r[s][0],r[s][1]];i.UpdateBuffers(this.Layer.AnnotationView)}e.view_height!==undefined&&(this.CreationCamera=e.creation_camera)},i.prototype.Deactivate=function(){this.Popup.StartHideTimer(),this.Layer.GetCanvasDiv().css({cursor:"default"}),this.Layer.DeactivateWidget(this),this.State=r,this.Shapes.SetActive(!1),this.DeactivateCallback&&this.DeactivateCallback(),this.Layer.EventuallyDraw()},i.prototype.HandleKeyDown=function(t){if(this.State==e)if(t.keyCode==27||t.keyCode==32||t.keyCode==13)return this.Deactivate(),!1},i.prototype.HandleMouseWheel=function(n){if(this.State==e||this.State==t){if(this.Shapes.GetNumberOfShapes()<0)return;var r=0;n.deltaY?r=n.deltaY:n.wheelDelta&&(r=n.wheelDelta);var i=1/this.Layer.GetPixelsPerUnit(),s=this.Shapes.GetLineWidth();return s=s||i,r>0?s*=1.1:r<0&&(s/=1.1),s<=i&&(s=0),this.Dialog.LineWidthInput.val(s),this.Shapes.SetLineWidth(s),this.Shapes.UpdateBuffers(this.Layer.AnnotationView),this.Layer.EventuallyDraw(),!1}return!0},i.prototype.HandleMouseDown=function(n){var r=n.offsetX,i=n.offsetY;if(n.which==1){if(this.State==e){var s=new SAM.Polyline;s.OutlineColor=[0,0,0],s.SetOutlineColor(this.Dialog.ColorInput.val()),s.FixedSize=!1,s.LineWidth=0,s.LineWidth=this.Shapes.GetLineWidth(),this.Shapes.AddShape(s);var o=this.Layer.GetCamera().ConvertPointViewerToWorld(r,i);s.Points.push([o[0],o[1]])}if(this.State==t){var u=this.Layer.GetCamera();this.LastMouse=u.ConvertPointViewerToWorld(r,i)}}},i.prototype.HandleMouseUp=function(r){if(r.which==3)return this.ShowPropertiesDialog(),!1;if(r.which==2)return this.Deactivate(),!1;this.State==n&&(this.Shapes.ResetOrigin(),this.State=t);var i=this.Shapes.GetNumberOfShapes()-1;if(this.State==e&&r.which==1&&i>=0){var s=this.Layer.GetCamera().GetSpacing();this.Shapes.GetShape(i).Decimate(s),window.SA&&SA.RecordState(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote()}return!1},i.prototype.HandleDoubleClick=function(n){return this.State==e?(this.Deactivate(),!1):this.State==t?(this.SetStateToDrawing(),!1):!0},i.prototype.HandleMouseMove=function(r){var i=r.offsetX,s=r.offsetY;if(r.which==1&&this.State==e){var o=this.Shapes.GetNumberOfShapes()-1,u=this.Shapes.GetShape(o),a=this.Layer.GetCamera().ConvertPointViewerToWorld(i,s);return u.Points.push([a[0],a[1]]),u.UpdateBuffers(this.Layer.AnnotationView),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),this.Layer.EventuallyDraw(),!1}if(this.State==t&&r.which==0)return this.SetActive(this.CheckActive(r)),!1;this.State==t&&r.which==1&&(this.State=n);if(this.State==n){this.State=n,this.Popup.Hide();var f=this.Layer.GetCamera(),l=f.ConvertPointViewerToWorld(i,s),c=this.Shapes.GetOrigin();return c[0]+=l[0]-this.LastMouse[0],c[1]+=l[1]-this.LastMouse[1],this.Shapes.SetOrigin(c),this.LastMouse=l,this.Layer.EventuallyDraw(),!1}},i.prototype.PlacePopup=function(){var e=this.Shapes.FindPopupPoint(this.Layer.GetCamera());if(!e)return;e=this.Layer.GetCamera().ConvertPointWorldToViewer(e[0],e[1]),e[0]+=20,e[1]-=10,this.Popup.Show(e[0],e[1])},i.prototype.CheckActive=function(n){if(this.State==e)return!0;if(this.Shapes.GetNumberOfShapes()==0)return!1;var i=n.offsetX,s=n.offsetY,o=this.Layer.GetCamera().ConvertPointViewerToWorld(i,s),u=this.Shapes.GetLineWidth(),a=10/this.Layer.GetPixelsPerUnit();u<a&&(u=a);var f=this.Shapes.PointOnShape(o,u);return this.State==t&&!f?this.SetActive(f):this.State==r&&f&&(this.PlacePopup(),this.SetActive(f)),f},i.prototype.SetActive=function(e){if(e==this.GetActive())return;if(e)this.Layer.ActivateWidget(this),this.State=t,this.Shapes.SetActive(!0),this.PlacePopup(),this.Layer.EventuallyDraw();else{if(this.State!=t)return;this.Deactivate(),this.Layer.DeactivateWidget(this)}},i.prototype.GetActive=function(){return this.State!=r},i.prototype.RemoveFromLayer=function(){this.Layer&&this.Layer.RemoveWidget(this),this.Layer=null};var s;i.prototype.ShowPropertiesDialog=function(){this.Dialog.ColorInput.val(SAM.ConvertColorToHex(this.Shapes.GetOutlineColor())),this.Dialog.LineWidthInput.val(this.Shapes.GetLineWidth().toFixed(2)),this.Dialog.Show(!0)},i.prototype.DialogApplyCallback=function(){var e=this.Dialog.ColorInput.val();this.LineWidth=parseFloat(this.Dialog.LineWidthInput.val()),this.Shapes.SetOutlineColor(e),this.Shapes.SetLineWidth(parseFloat(this.Dialog.LineWidthInput.val())),this.Shapes.UpdateBuffers(this.Layer.AnnotationView),this.SetActive(!1),window.SA&&SA.RecordState(),this.Layer.EventuallyDraw(),localStorage.PencilWidgetDefaults=JSON.stringify({Color:e,LineWidth:this.LineWidth}),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified()},SAM.PencilWidget=i})();(function(){"use strict";function r(t,r){if(t==null)return;this.Dialog=new SAM.Dialog(this),this.Dialog.Title.text("Fill Annotation Editor"),this.Dialog.Body.css({margin:"1em 2em"}),this.Dialog.ColorDiv=$("<div>").appendTo(this.Dialog.Body).addClass("sa-view-fill-div"),this.Dialog.ColorLabel=$("<div>").appendTo(this.Dialog.ColorDiv).text("Color:").addClass("sa-view-fill-label"),this.Dialog.ColorInput=$('<input type="color">').appendTo(this.Dialog.ColorDiv).val("#30ff00").addClass("sa-view-fill-input"),this.Dialog.LineWidthDiv=$("<div>").appendTo(this.Dialog.Body).addClass("sa-view-fill-div"),this.Dialog.LineWidthLabel=$("<div>").appendTo(this.Dialog.LineWidthDiv).text("Line Width:").addClass("sa-view-fill-label"),this.Dialog.LineWidthInput=$('<input type="number">').appendTo(this.Dialog.LineWidthDiv).addClass("sa-view-fill-input").keypress(function(e){return e.keyCode!=13}),this.Popup=new SAM.WidgetPopup(this),this.Viewer=t,this.Viewer.AddWidget(this),this.Cursor=$("<img>").appendTo("body").addClass("sa-view-fill-cursor").attr("type","image").attr("src",SAM.ImagePathUrl+"brush1.jpg");var i=this;this.ActiveCenter=[0,0],this.State=e,r||(this.State=n),this.CreationCamera=t.GetCamera().Serialize}var e=0,t=1,n=2;r.prototype.Initialize=function(e){this.Segmentation=new Segmentation(this.Viewer)},r.prototype.Draw=function(e){this.Segmentation.ImageAnnotation.Draw(e)},r.prototype.Serialize=function(){},r.prototype.Load=function(e){},r.prototype.HandleKeyPress=function(e,t){return!1},r.prototype.Deactivate=function(){this.Popup.StartHideTimer(),this.Viewer.DeactivateWidget(this),this.State=n,this.DeactivateCallback&&this.DeactivateCallback(),eventuallyRender()},r.prototype.HandleMouseDown=function(e){var t=this.Viewer.MouseX,n=this.Viewer.MouseY;if(e.which==1){var r=this.Viewer.ConvertPointViewerToWorld(t,n);this.Cursor.attr("src",SAM.ImagePathUrl+"brush1.jpg"),this.Cursor.show(),this.Segmentation.AddPositive(r)}if(e.which==3){var r=this.Viewer.ConvertPointViewerToWorld(t,n);this.Cursor.attr("src",SAM.ImagePathUrl+"eraser1.jpg"),this.Cursor.show(),this.Segmentation.AddNegative(r)}},r.prototype.HandleMouseUp=function(e){e.which==2&&this.Deactivate();if(e.which==1||e.which==3)this.Cursor.hide(),this.Segmentation.Update(),this.Segmentation.Draw(),eventuallyRender()},r.prototype.HandleDoubleClick=function(e){},r.prototype.HandleMouseMove=function(t){var n=this.Viewer.MouseX,r=this.Viewer.MouseY;this.Cursor.css({left:n+4,top:r-32});if(this.Viewer.MouseDown==1&&this.State==e){if(t.which==1){var i=this.Viewer.ConvertPointViewerToWorld(n,r);this.Segmentation.AddPositive(i)}if(t.which==3){var i=this.Viewer.ConvertPointViewerToWorld(n,r);this.Segmentation.AddNegative(i)}return}},r.prototype.ComputeActiveCenter=function(){},r.prototype.PlacePopup=function(){},r.prototype.CheckActive=function(e){},r.prototype.GetActive=function(){return!1},r.prototype.SetActive=function(e){if(e){this.Viewer.ActivateWidget(this),this.State=t;for(var n=0;n<this.Shapes.length;++n)this.Shapes[n].Active=!0;this.PlacePopup(),eventuallyRender()}else this.Deactivate(),this.Viewer.DeactivateWidget(this)},r.prototype.RemoveFromViewer=function(){this.Viewer&&this.Viewer.RemoveWidget()};var i;r.prototype.ShowPropertiesDialog=function(){this.Dialog.ColorInput.val(SAM.ConvertColorToHex(this.Shapes[0].OutlineColor)),this.Dialog.LineWidthInput.val(this.Shapes[0].LineWidth.toFixed(2)),this.Dialog.Show(!0)},r.prototype.DialogApplyCallback=function(){var e=this.Dialog.ColorInput.val();for(var t=0;t<this.Shapes.length;++t)this.Shapes[t].SetOutlineColor(e),this.Shapes[t].LineWidth=parseFloat(this.Dialog.LineWidthInput.val()),this.Shapes[t].UpdateBuffers(this.Viewer.AnnotationView);this.SetActive(!1),window.SA&&SA.RecordState(),eventuallyRender()},SAM.FillWidget=r})();(function(){"use strict";function r(e,t){if(e==null)return;this.UserNoteFlag=!SA.Edit,this.Type="lasso";var r=this;this.Dialog=new SAM.Dialog(function(){r.DialogApplyCallback()}),this.Dialog.Title.text("Lasso Annotation Editor"),this.Dialog.Body.css({margin:"1em 2em"}),this.Dialog.ColorDiv=$("<div>").appendTo(this.Dialog.Body).addClass("sa-view-annotation-modal-div"),this.Dialog.ColorLabel=$("<div>").appendTo(this.Dialog.ColorDiv).text("Color:").addClass("sa-view-annotation-modal-input-label"),this.Dialog.ColorInput=$('<input type="color">').appendTo(this.Dialog.ColorDiv).val("#30ff00").addClass("sa-view-annotation-modal-input"),this.Dialog.LineWidthDiv=$("<div>").appendTo(this.Dialog.Body).addClass("sa-view-annotation-modal-div"),this.Dialog.LineWidthLabel=$("<div>").appendTo(this.Dialog.LineWidthDiv).text("Line Width:").addClass("sa-view-annotation-modal-input-label"),this.Dialog.LineWidthInput=$('<input type="number">').appendTo(this.Dialog.LineWidthDiv).addClass("sa-view-annotation-modal-input").keypress(function(e){return e.keyCode!=13}),this.Dialog.AreaDiv=$("<div>").appendTo(this.Dialog.Body).addClass("sa-view-annotation-modal-div"),this.Dialog.AreaLabel=$("<div>").appendTo(this.Dialog.AreaDiv).text("Area:").addClass("sa-view-annotation-modal-input-label"),this.Dialog.Area=$("<div>").appendTo(this.Dialog.AreaDiv).addClass("sa-view-annotation-modal-input");if(localStorage.LassoWidgetDefaults){var i=JSON.parse(localStorage.LassoWidgetDefaults);i.Color&&this.Dialog.ColorInput.val(SAM.ConvertColorToHex(i.Color)),i.LineWidth&&this.Dialog.LineWidthInput.val(i.LineWidth)}this.Layer=e,this.Popup=new SAM.WidgetPopup(this),this.Layer.AddWidget(this);var r=this;this.Loop=new SAM.Polyline,this.Loop.OutlineColor=[0,0,0],this.Loop.SetOutlineColor(this.Dialog.ColorInput.val()),this.Loop.FixedSize=!1,this.Loop.LineWidth=0,this.Loop.Closed=!0,this.Stroke=!1,this.ActiveCenter=[0,0],t?this.SetStateToDrawing():this.State=n}var e=0,t=1,n=2;r.prototype.Draw=function(e){this.Loop.Draw(e),this.Stroke&&this.Stroke.Draw(e)},r.prototype.Serialize=function(){var e=new Object;e.type="lasso",e.user_note_flag=this.UserNoteFlag,e.outlinecolor=this.Loop.OutlineColor,e.linewidth=this.Loop.GetLineWidth(),e.points=[];for(var t=0;t<this.Loop.Points.length;++t)e.points.push([this.Loop.Points[t][0],this.Loop.Points[t][1]]);return e.closedloop=!0,e},r.prototype.Load=function(e){this.UserNoteFlag=e.user_note_flag,e.outlinecolor!=undefined&&(this.Loop.OutlineColor[0]=parseFloat(e.outlinecolor[0]),this.Loop.OutlineColor[1]=parseFloat(e.outlinecolor[1]),this.Loop.OutlineColor[2]=parseFloat(e.outlinecolor[2])),e.outlinewidth!=undefined&&(this.Loop.LineWidth=e.linewidth);var t=[];e.points!=undefined&&(t=e.points),e.shape!=undefined&&(t=e.shapes[0]);for(var n=0;n<t.length;n++)this.Loop.Points[n]=[parseFloat(t[n][0]),parseFloat(t[n][1])];this.ComputeActiveCenter(),this.Loop.UpdateBuffers(this.Layer.AnnotationView)},r.prototype.HandleMouseWheel=function(n){if(this.State==e||this.State==t){if(!this.Loop)return!0;var r=0;n.deltaY?r=n.deltaY:n.wheelDelta&&(r=n.wheelDelta);var i=1/this.Layer.GetPixelsPerUnit(),s=this.Loop.GetLineWidth();return s=s||i,r>0?s*=1.1:r<0&&(s/=1.1),s<=i&&(s=0),this.Dialog.LineWidthInput.val(s),this.Loop.SetLineWidth(s),this.Loop.UpdateBuffers(this.Layer.AnnotationView),this.Layer.EventuallyDraw(),!1}return!0},r.prototype.Deactivate=function(){this.Popup.StartHideTimer(),this.Layer.DeactivateWidget(this),this.State=n,this.Loop.SetActive(!1),this.Stroke&&this.Stroke.SetActive(!1),this.DeactivateCallback&&this.DeactivateCallback(),this.Layer.EventuallyDraw()},r.prototype.HandleKeyDown=function(t){if(this.State==e)if(t.keyCode==27||t.keyCode==32||t.keyCode==13)return this.Deactivate(),!1},r.prototype.HandleMouseDown=function(e){var t=e.offsetX,n=e.offsetY;if(e.which==1){this.Stroke=new SAM.Polyline,this.Stroke.OutlineColor=[0,0,0],this.Stroke.SetOutlineColor(this.Loop.OutlineColor),this.Stroke.FixedSize=!1,this.Stroke.LineWidth=0;var r=this.Layer.GetCamera().ConvertPointViewerToWorld(t,n);return this.Stroke.Points=[],this.Stroke.Points.push([r[0],r[1]]),!1}return!0},r.prototype.HandleMouseUp=function(t){t.which==2&&this.Deactivate();if(t.which==1&&this.State==e){var n=this.Layer.GetCamera().GetSpacing();this.Stroke.Decimate(n),this.Loop&&this.Loop.Points.length>0?this.CombineStroke():(this.Stroke.Closed=!0,this.Stroke.UpdateBuffers(this.Layer.AnnotationView),this.Loop=this.Stroke,this.Stroke=!1),this.ComputeActiveCenter(),this.Layer.EventuallyDraw(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),window.SA&&SA.RecordState()}return!1},r.prototype.HandleDoubleClick=function(n){return this.State==e?(this.Deactivate(),!1):this.State==t?(this.SetStateToDrawing(),!1):!0},r.prototype.SetStateToDrawing=function(){this.State=e,this.Loop.SetActive(!1),this.Popup.Hide(),this.Layer.GetCanvasDiv().css({cursor:"url("+SAM.ImagePathUrl+"select_lasso.png) 5 30,crosshair"}),this.Layer.EventuallyDraw()},r.prototype.HandleMouseMove=function(n){var r=n.offsetX,i=n.offsetY;if(n.which==1&&this.State==e){var s=this.Stroke,o=this.Layer.GetCamera().ConvertPointViewerToWorld(r,i);return s.Points.push([o[0],o[1]]),s.UpdateBuffers(this.Layer.AnnotationView),SA.notesWidget&&!this.UserNoteFlag&&SA.notesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),this.Layer.EventuallyDraw(),!1}return this.State==t&&n.which==0?(this.SetActive(this.CheckActive(n)),!1):!0},r.prototype.ComputeActiveCenter=function(){var e=0,t=0,n=0,r=this.Loop,i=[];for(var s=0;s<r.Points.length;++s)t+=r.Points[s][0],n+=r.Points[s][1];this.ActiveCenter[0]=t/r.Points.length,this.ActiveCenter[1]=n/r.Points.length},r.prototype.PlacePopup=function(){var e=this.Loop.FindPopupPoint(this.Layer.GetCamera());e=this.Layer.GetCamera().ConvertPointWorldToViewer(e[0],e[1]),e[0]+=20,e[1]-=10,this.Popup.Show(e[0],e[1])},r.prototype.CheckActive=function(t){if(this.State==e)return;var n=t.offsetX,r=t.offsetY,i=this.Layer.GetCamera().ConvertPointViewerToWorld(n,r),s=this.Loop.GetLineWidth()/2,o=10/this.Layer.GetPixelsPerUnit();return s<o&&(s=o),this.Loop.PointOnShape(i,s)?!0:!1},r.prototype.GetActive=function(){return this.State!=n},r.prototype.SetActive=function(e){e?this.State==n&&(this.State=t,this.Loop.SetActive(!0),this.PlacePopup(),this.Layer.EventuallyDraw()):this.State!=n&&(this.Deactivate(),this.Layer.DeactivateWidget(this)),this.Layer.EventuallyDraw()},r.prototype.RemoveFromLayer=function(){this.Layer&&this.RemoveWidget(this)},r.prototype.ShowPropertiesDialog=function(){this.Dialog.ColorInput.val(SAM.ConvertColorToHex(this.Loop.OutlineColor)),this.Dialog.LineWidthInput.val(this.Loop.LineWidth.toFixed(2));var e=this.ComputeArea(),t=""+e.toFixed(2);this.Loop.FixedSize?t+=" pixels^2":t+=" units^2",this.Dialog.Area.text(t),this.Dialog.Show(!0)},r.prototype.DialogApplyCallback=function(){var e=this.Dialog.ColorInput.val();this.Loop.SetOutlineColor(e),this.Loop.LineWidth=parseFloat(this.Dialog.LineWidthInput.val()),this.Loop.UpdateBuffers(this.Layer.AnnotationView),this.SetActive(!1),window.SA&&SA.RecordState(),this.Layer.EventuallyDraw(),localStorage.LassoWidgetDefaults=JSON.stringify({Color:e,LineWidth:this.Loop.LineWidth}),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote()},r.prototype.CombineStroke=function(){this.Loop.Points.push(this.Loop.Points[0]);var e,t;for(var n=1;n<this.Stroke.Points.length;++n){var r=this.Stroke.Points[n-1],i=this.Stroke.Points[n],s=this.FindIntersection(r,i);s&&(this.Stroke.Points.splice(n,0,s.Point),e==undefined?(e=s,e.StrokeIndex=n):(s.LoopIndex<e.LoopIndex&&(e.LoopIndex+=1),t=s,t.StrokeIndex=n))}var o=0;if(t!=undefined){var u=[],a=0,f=[],l=0,n;for(n=e.StrokeIndex;n<t.StrokeIndex;++n)u.push(this.Stroke.Points[n]),f.push(this.Stroke.Points[n]);n=t.LoopIndex;while(n!=e.LoopIndex){if(++o>1e6){alert("Combine loop 1 is taking too long.");return}u.push(this.Loop.Points[n]);var c=this.Loop.Points[n][0],h=this.Loop.Points[n][1];--n==0&&(n=this.Loop.Points.length-1),c-=this.Loop.Points[n][0],h-=this.Loop.Points[n][1],a+=Math.sqrt(c*c+h*h)}u.push(e.Point),n=t.LoopIndex;while(n!=e.LoopIndex){if(++o>1e6){alert("Combine loop 2 is taking too long.");return}f.push(this.Loop.Points[n]);var c=this.Loop.Points[n][0],h=this.Loop.Points[n][1];++n==this.Loop.Points.length-1&&(n=0),c-=this.Loop.Points[n][0],h-=this.Loop.Points[n][1],l+=Math.sqrt(c*c+h*h)}f.push(e.Point),a>l?this.Loop.Points=u:this.Loop.Points=f,window.SA&&SA.RecordState()}this.Loop.Points.pop(),this.Loop.UpdateBuffers(this.Layer.AnnotationView),this.ComputeActiveCenter(),this.Stroke=!1,this.Layer.EventuallyDraw()},r.prototype.FindIntersection=function(e,t){var n=!1,r=[t[0]-e[0],t[1]-e[1]],i=Math.sqrt(r[0]*r[0]+r[1]*r[1]);if(i<0)return!1;r[0]=r[0]/i,r[1]=r[1]/i;var s=this.Loop.Points[0],o=[(s[0]-e[0])/i,(s[1]-e[1])/i],u=[o[0]*r[0]+o[1]*r[1],o[1]*r[0]-o[0]*r[1]];for(var a=1;a<this.Loop.Points.length;++a){var f=this.Loop.Points[a];if(e==s||e==f)continue;var l=[(f[0]-e[0])/i,(f[1]-e[1])/i],c=[l[0]*r[0]+l[1]*r[1],l[1]*r[0]-l[0]*r[1]];if(c[1]>=0&&u[1]<=0||c[1]<=0&&u[1]>=0){var h=u[1]/(u[1]-c[1]),p=u[0]+h*(c[0]-u[0]);if(p>0&&p<=1){var d=[s[0]+h*(f[0]-s[0]),s[1]+h*(f[1]-s[1])];if(!n||p<n.k)n={Point:d,LoopIndex:a,k:p}}}s=f,o=l,u=c}return n&&this.Loop.Points.splice(n.LoopIndex,0,n.Point),n},r.prototype.IsPointInsideLoop=function(e,t){var n=0,r=this.Loop.Points[this.Loop.length-1];for(var i=0;i<this.Loop.length;++i){var s=this.Loop.Points[i],o=[r[0]-e,r[1]-t],u=[s[0]-e,s[1]-t],a=Math.sqrt(o[0]*o[0]+o[1]*o[1]),f=Math.sqrt(u[0]*u[0]+u[1]*u[1]);n+=Math.arcsin((o[0]*u[1]-o[1]*u[0])/(a*f))}return n>3.14||n<-3.14},r.prototype.ComputeArea=function(){var e=0,t=this.Loop.Points[0][0]-this.ActiveCenter[0],n=this.Loop.Points[0][1]-this.ActiveCenter[1];for(var r=1;r<this.Loop.Points.length;++r){var i=t,s=n;t=this.Loop.Points[r][0]-this.ActiveCenter[0],n=this.Loop.Points[r][1]-this.ActiveCenter[1],e+=t*s-i*n}return e<0&&(e=-e),e},SAM.LassoWidget=r})();(function(){"use strict";function e(e){this.Widget=e,this.Visible=!1,this.HideTimerId=0;var t=e.Layer.GetCanvasDiv(),n=this;this.ButtonDiv=$("<div>").appendTo(t).hide().css({position:"absolute","z-index":"1"}).mouseenter(function(){n.CancelHideTimer()}).mouseleave(function(){n.StartHideTimer()}),this.DeleteButton=$("<img>").appendTo(this.ButtonDiv).css({height:"20px"}).attr("src",SA.ImagePathUrl+"deleteSmall.png").click(function(){n.DeleteCallback()}),this.PropertiesButton=$("<img>").appendTo(this.ButtonDiv).css({height:"20px"}).attr("src",SA.ImagePathUrl+"Menu.jpg").click(function(){n.PropertiesCallback()}),this.HideCallback=undefined}e.prototype.SetHideCallback=function(e){this.HideCllback=e},e.prototype.DeleteCallback=function(){this.Widget.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),this.Widget.SetActive(!1),this.Hide(),this.Widget.Layer.EventuallyDraw(),this.Widget.Layer.RemoveWidget(this.Widget),window.SA&&SA.RecordState()},e.prototype.PropertiesCallback=function(){this.Hide(),this.Widget.ShowPropertiesDialog()},e.prototype.Show=function(e,t){this.CancelHideTimer(),this.ButtonDiv.css({left:e+"px",top:t+"px"}).show()},e.prototype.Hide=function(){this.CancelHideTimer(),this.ButtonDiv.hide(),this.HideCallback&&this.HideCallback()},e.prototype.StartHideTimer=function(){if(!this.HideTimerId){var e=this;SAM.detectMobile()?this.HideTimerId=setTimeout(function(){e.HideTimerCallback()},1500):this.HideTimerId=setTimeout(function(){e.HideTimerCallback()},800)}},e.prototype.CancelHideTimer=function(){this.HideTimerId&&(clearTimeout(this.HideTimerId),this.HideTimerId=0)},e.prototype.HideTimerCallback=function(){this.ButtonDiv.hide(),this.HideTimerId=0},SAM.WidgetPopup=e})();(function(){"use strict";function e(){SAM.Shape.call(this),this.Length=50,this.Width=1,this.Origin=[1e4,1e4],this.FillColor=[0,0,0],this.OutlineColor=[1,1,1],this.PointBuffer=[]}e.prototype=new SAM.Shape,e.prototype.destructor=function(){},e.prototype.UpdateBuffers=function(e){this.PointBuffer=[];var t=[],n=this.Length*.5+.5,r=this.Width*.5+.5;this.Matrix=mat4.create(),mat4.identity(this.Matrix),this.PointBuffer.push(-r),this.PointBuffer.push(-r),this.PointBuffer.push(0),this.PointBuffer.push(-n),this.PointBuffer.push(-r),this.PointBuffer.push(0),this.PointBuffer.push(-n),this.PointBuffer.push(r),this.PointBuffer.push(0),this.PointBuffer.push(-r),this.PointBuffer.push(r),this.PointBuffer.push(0),this.PointBuffer.push(-r),this.PointBuffer.push(n),this.PointBuffer.push(0),this.PointBuffer.push(r),this.PointBuffer.push(n),this.PointBuffer.push(0),this.PointBuffer.push(r),this.PointBuffer.push(r),this.PointBuffer.push(0),this.PointBuffer.push(n),this.PointBuffer.push(r),this.PointBuffer.push(0),this.PointBuffer.push(n),this.PointBuffer.push(-r),this.PointBuffer.push(0),this.PointBuffer.push(r),this.PointBuffer.push(-r),this.PointBuffer.push(0),this.PointBuffer.push(r),this.PointBuffer.push(-n),this.PointBuffer.push(0),this.PointBuffer.push(-r),this.PointBuffer.push(-n),this.PointBuffer.push(0),this.PointBuffer.push(-r),this.PointBuffer.push(-r),this.PointBuffer.push(0),t.push(1),t.push(2),t.push(7),t.push(1),t.push(7),t.push(8),t.push(4),t.push(5),t.push(10),t.push(4),t.push(10),t.push(11),e.gl&&(this.VertexPositionBuffer=e.gl.createBuffer(),e.gl.bindBuffer(e.gl.ARRAY_BUFFER,this.VertexPositionBuffer),e.gl.bufferData(e.gl.ARRAY_BUFFER,new Float32Array(this.PointBuffer),e.gl.STATIC_DRAW),this.VertexPositionBuffer.itemSize=3,this.VertexPositionBuffer.numItems=this.PointBuffer.length/3,this.CellBuffer=e.gl.createBuffer(),e.gl.bindBuffer(e.gl.ELEMENT_ARRAY_BUFFER,this.CellBuffer),e.gl.bufferData(e.gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(t),e.gl.STATIC_DRAW),this.CellBuffer.itemSize=1,this.CellBuffer.numItems=t.length)},SAM.CrossHairs=e})();(function(){"use strict";function e(){SAM.Shape.call(this),this.Width=10,this.Length=50,this.Orientation=45,this.Origin=[1e4,1e4],this.OutlineColor=[0,0,0],this.ZOffset=-0.1}e.prototype=new SAM.Shape,e.prototype.destructor=function(){},e.prototype.PointInShape=function(e,t){var n=-(this.Orientation*Math.PI/180),r=Math.cos(n),i=Math.sin(n),s=e*r+t*i,o=-e*i+t*r;n=this.Width/2;if(s>0&&s<this.Length*1.3&&o<n*3&&o>-n*3)return!0},e.prototype.UpdateBuffers=function(e){this.PointBuffer=[];var t=[],n=this.Width*.5,r=this.Width*2;this.Matrix=mat4.create(),mat4.identity(this.Matrix),this.PointBuffer.push(0),this.PointBuffer.push(0),this.PointBuffer.push(0),this.PointBuffer.push(r),this.PointBuffer.push(this.Width),this.PointBuffer.push(0),this.PointBuffer.push(r),this.PointBuffer.push(n),this.PointBuffer.push(0),this.PointBuffer.push(this.Length),this.PointBuffer.push(n),this.PointBuffer.push(0),this.PointBuffer.push(this.Length),this.PointBuffer.push(-n),this.PointBuffer.push(0),this.PointBuffer.push(r),this.PointBuffer.push(-n),this.PointBuffer.push(0),this.PointBuffer.push(r),this.PointBuffer.push(-this.Width),this.PointBuffer.push(0),this.PointBuffer.push(0),this.PointBuffer.push(0),this.PointBuffer.push(0),e.gl&&(t.push(0),t.push(1),t.push(2),t.push(0),t.push(2),t.push(5),t.push(0),t.push(5),t.push(6),t.push(2),t.push(3),t.push(4),t.push(2),t.push(4),t.push(5),this.VertexPositionBuffer=e.gl.createBuffer(),e.gl.bindBuffer(e.gl.ARRAY_BUFFER,this.VertexPositionBuffer),e.gl.bufferData(e.gl.ARRAY_BUFFER,new Float32Array(this.PointBuffer),e.gl.STATIC_DRAW),this.VertexPositionBuffer.itemSize=3,this.VertexPositionBuffer.numItems=this.PointBuffer.length/3,this.CellBuffer=e.gl.createBuffer(),e.gl.bindBuffer(e.gl.ELEMENT_ARRAY_BUFFER,this.CellBuffer),e.gl.bufferData(e.gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(t),e.gl.STATIC_DRAW),this.CellBuffer.itemSize=1,this.CellBuffer.numItems=t.length)},SAM.Arrow=e})();(function(){"use strict";function u(t,n){if(t==null)return null;this.Layer=t,this.Shape=new Arrow,this.Shape.Origin=[0,0],this.Shape.SetFillColor([0,0,0]),this.Shape.OutlineColor=[1,1,1],this.Shape.Length=50,this.Shape.Width=8,this.TipPosition=[0,0],this.TipOffset=[0,0];if(t){t.AddWidget(this);if(n&&t){this.State=e,this.Layer.ActivateWidget(this);return}}this.State=i}function f(){var e=a;if(!e)return;var t=document.getElementById("arrowcolor").value;e.Shape.SetFillColor(t),e!=null&&e.SetActive(!1),eventuallyRender()}function l(){var e=a;e!=null&&e.SetActive(!1)}function c(){var e=a;e!=null&&(this.Layer.ActiveWidget=null,e.RemoveFromLayer(),eventuallyRender())}var e=0,t=1,n=2,r=3,i=4,s=5,o=6;u.prototype.Draw=function(e){this.Shape.Draw(e)},u.prototype.RemoveFromLayer=function(){this.Layer&&this.Layer.RemoveWidget(this)},u.prototype.Serialize=function(){if(this.Shape===undefined)return null;var e=new Object;return e.type="arrow",e.origin=this.Shape.Origin,e.fillcolor=this.Shape.FillColor,e.outlinecolor=this.Shape.OutlineColor,e.length=this.Shape.Length,e.width=this.Shape.Width,e.orientation=this.Shape.Orientation,e.fixedsize=this.Shape.FixedSize,e.fixedorientation=this.Shape.FixedOrientation,e},u.prototype.Load=function(e){this.Shape.Origin=[parseFloat(e.origin[0]),parseFloat(e.origin[1])],this.Shape.FillColor=[parseFloat(e.fillcolor[0]),parseFloat(e.fillcolor[1]),parseFloat(e.fillcolor[2])],this.Shape.OutlineColor=[parseFloat(e.outlinecolor[0]),parseFloat(e.outlinecolor[1]),parseFloat(e.outlinecolor[2])],this.Shape.Length=parseFloat(e.length),this.Shape.Width=parseFloat(e.width),this.Shape.Orientation=parseFloat(e.orientation),e.fixedsize===undefined?this.Shape.FixedSize=!0:this.Shape.FixedSize=e.fixedsize=="true",e.fixedorientation===undefined?this.Shape.FixedOrientation=!0:this.Shape.FixedOrientation=e.fixedorientation=="true",this.Shape.UpdateBuffers(this.Layer.AnnotationView)},u.prototype.SetFixedSize=function(e){if(this.Shape.FixedSize==e)return;var t=this.Layer.GetPixelsPerUnit();e?(this.Shape.Length*=t,this.Shape.Width*=t):(this.Shape.Length/=t,this.Shape.Width/=t),this.Shape.FixedSize=e,this.Shape.UpdateBuffers(),eventuallyRender()},u.prototype.HandleKeyPress=function(e,t){},u.prototype.HandleMouseDown=function(n){if(n.which!=1)return;this.State==e&&(this.TipPosition=[this.Layer.MouseX,this.Layer.MouseY],this.State=r);if(this.State==s)if(this.ActiveTail)this.TipPosition=this.Layer.ConvertPointWorldToViewer(this.Shape.Origin[0],this.Shape.Origin[1]),this.State=r;else{var i=this.Layer.ConvertPointWorldToViewer(this.Shape.Origin[0],this.Shape.Origin[1]);this.TipOffset[0]=i[0]-this.Layer.MouseX,this.TipOffset[1]=i[1]-this.Layer.MouseY,this.State=t}},u.prototype.HandleMouseUp=function(e){this.State==s&&e.which==3?(this.State=o,this.ShowPropertiesDialog()):this.State!=o&&this.SetActive(!1)},u.prototype.HandleMouseMove=function(n){var o=this.Layer.MouseX,u=this.Layer.MouseY;if(this.Layer.MouseDown==0&&this.State==s){this.CheckActive(n);return}if(this.State==e||this.State==t){var a=this.Layer.GetViewport();this.Shape.Origin=this.Layer.ConvertPointViewerToWorld(o+this.TipOffset[0],u+this.TipOffset[1]),eventuallyRender()}if(this.State==r){var f=o-this.TipPosition[0],l=u-this.TipPosition[1];if(!this.Shape.FixedSize){var c=this.Layer.GetPixelsPerUnit();f/=c,l/=c}this.Shape.Length=Math.sqrt(f*f+l*l),this.Shape.Orientation=Math.atan2(l,f)*180/Math.PI,this.Shape.UpdateBuffers(),eventuallyRender()}this.State==i&&this.CheckActive(n)},u.prototype.CheckActive=function(e){var t=this.Layer.GetViewport(),n=this.Layer.MainView.Camera,r=n.Matrix,i=this.Shape.Origin[0],s=this.Shape.Origin[1],o=i*r[3]+s*r[7]+r[15],u=(i*r[0]+s*r[4]+r[12])/o,a=(i*r[1]+s*r[5]+r[13])/o;u=(u+1)*.5*t[2]+t[0],a=(a+1)*.5*t[3]+t[1],i=this.Layer.MouseX-u,s=this.Layer.MouseY-a;var f=this.Shape.Orientation*Math.PI/180,l=Math.cos(f),c=Math.sin(f);u=i*l+s*c,a=-i*c+s*l;var h=this.Shape.Length,p=this.Shape.Width/2;if(!this.Shape.FixedSize){var d=this.Layer.GetPixelsPerUnit();h*=d,p*=d}return this.ActiveTail=!1,u>0&&u<h&&a>-p&&a<p?(this.SetActive(!0),u>h-p&&(this.ActiveTail=!0),!0):(this.SetActive(!1),!1)},u.prototype.GetActive=function(){return this.State==i?!1:!0},u.prototype.SetActive=function(e){if(e==this.GetActive())return;e?(this.State=s,this.Shape.Active=!0,this.Layer.ActivateWidget(this),eventuallyRender()):(this.State=i,this.Shape.Active=!1,this.Layer.DeactivateWidget(this),eventuallyRender())};var a;u.prototype.ShowPropertiesDialog=function(){var e=document.getElementById("arrowcolor");e.value=SAM.ConvertColorToHex(this.Shape.FillColor);var t=document.getElementById("ArrowLength");a=this,$("#arrow-properties-dialog").dialog("open")},u.prototype.SetColor=function(e){this.Shape.SetFillColor(e),eventuallyRender()},SAM.ArrowWidget=u})();(function(){"use strict";function e(){SAM.Shape.call(this),this.Radius=10,this.Origin=[1e4,1e4],this.OutlineColor=[0,0,0],this.PointBuffer=[]}e.prototype=new SAM.Shape,e.prototype.destructor=function(){},e.prototype.UpdateBuffers=function(e){this.PointBuffer=[];var t=[],n=[],r=Math.floor(this.Radius/2)+10;if(r>50||!this.FixedSize)r=50;this.Matrix=mat4.create(),mat4.identity(this.Matrix);if(e.gl)if(this.LineWidth==0){for(var i=0;i<=r;++i){var s=i*2*3.14159265359/r;this.PointBuffer.push(this.Radius*Math.cos(s)),this.PointBuffer.push(this.Radius*Math.sin(s)),this.PointBuffer.push(0)}for(var i=2;i<r;++i)t.push(0),t.push(i-1),t.push(i);this.VertexPositionBuffer=e.gl.createBuffer(),e.gl.bindBuffer(e.gl.ARRAY_BUFFER,this.VertexPositionBuffer),e.gl.bufferData(e.gl.ARRAY_BUFFER,new Float32Array(this.PointBuffer),e.gl.STATIC_DRAW),this.VertexPositionBuffer.itemSize=3,this.VertexPositionBuffer.numItems=this.PointBuffer.length/3,this.CellBuffer=e.gl.createBuffer(),e.gl.bindBuffer(e.gl.ELEMENT_ARRAY_BUFFER,this.CellBuffer),e.gl.bufferData(e.gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(t),e.gl.STATIC_DRAW),this.CellBuffer.itemSize=1,this.CellBuffer.numItems=t.length}else{var o=this.Radius,u=this.Radius+this.LineWidth;for(var i=0;i<=r;++i){var s=i*2*3.14159265359/r;this.PointBuffer.push(o*Math.cos(s)),this.PointBuffer.push(o*Math.sin(s)),this.PointBuffer.push(0),this.PointBuffer.push(u*Math.cos(s)),this.PointBuffer.push(u*Math.sin(s)),this.PointBuffer.push(0)}this.VertexPositionBuffer=e.gl.createBuffer(),e.gl.bindBuffer(e.gl.ARRAY_BUFFER,this.VertexPositionBuffer),e.gl.bufferData(e.gl.ARRAY_BUFFER,new Float32Array(this.PointBuffer),e.gl.STATIC_DRAW),this.VertexPositionBuffer.itemSize=3,this.VertexPositionBuffer.numItems=this.PointBuffer.length/3;for(var i=2;i<r;++i)t.push(0),t.push((i-1)*2),t.push(i*2);this.CellBuffer=e.gl.createBuffer(),e.gl.bindBuffer(e.gl.ELEMENT_ARRAY_BUFFER,this.CellBuffer),e.gl.bufferData(e.gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(t),e.gl.STATIC_DRAW),this.CellBuffer.itemSize=1,this.CellBuffer.numItems=t.length;for(var i=0;i<r;++i)n.push(0+i*2),n.push(1+i*2),n.push(2+i*2),n.push(1+i*2),n.push(3+i*2),n.push(2+i*2);this.LineCellBuffer=e.gl.createBuffer(),e.gl.bindBuffer(e.gl.ELEMENT_ARRAY_BUFFER,this.LineCellBuffer),e.gl.bufferData(e.gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(n),e.gl.STATIC_DRAW),this.LineCellBuffer.itemSize=1,this.LineCellBuffer.numItems=n.length}else for(var i=0;i<=r;++i){var s=i*2*3.14159265359/r;this.PointBuffer.push(this.Radius*Math.cos(s)),this.PointBuffer.push(this.Radius*Math.sin(s)),this.PointBuffer.push(0)}},SAM.Circle=e})();(function(){"use strict";function u(t,n){this.UserNoteFlag=!SA.Edit,this.Type="circle";var r=this;this.Dialog=new SAM.Dialog(function(){r.DialogApplyCallback()}),this.Dialog.Title.text("Circle Annotation Editor"),this.Dialog.Body.css({margin:"1em 2em"}),this.Dialog.ColorDiv=$("<div>").css({height:"24px"}).appendTo(this.Dialog.Body).addClass("sa-view-annotation-modal-div"),this.Dialog.ColorLabel=$("<div>").appendTo(this.Dialog.ColorDiv).text("Color:").addClass("sa-view-annotation-modal-input-label"),this.Dialog.ColorInput=$('<input type="color">').appendTo(this.Dialog.ColorDiv).val("#30ff00").addClass("sa-view-annotation-modal-input"),this.Dialog.LineWidthDiv=$("<div>").appendTo(this.Dialog.Body).addClass("sa-view-annotation-modal-div"),this.Dialog.LineWidthLabel=$("<div>").appendTo(this.Dialog.LineWidthDiv).text("Line Width:").addClass("sa-view-annotation-modal-input-label"),this.Dialog.LineWidthInput=$('<input type="number">').appendTo(this.Dialog.LineWidthDiv).addClass("sa-view-annotation-modal-input").keypress(function(e){return e.keyCode!=13}),this.Dialog.AreaDiv=$("<div>").appendTo(this.Dialog.Body).addClass("sa-view-annotation-modal-div"),this.Dialog.AreaLabel=$("<div>").appendTo(this.Dialog.AreaDiv).text("Area:").addClass("sa-view-annotation-modal-input-label"),this.Dialog.Area=$("<div>").appendTo(this.Dialog.AreaDiv).addClass("sa-view-annotation-modal-input");if(localStorage.CircleWidgetDefaults){var s=JSON.parse(localStorage.CircleWidgetDefaults);s.Color&&this.Dialog.ColorInput.val(SAM.ConvertColorToHex(s.Color)),s.LineWidth&&this.Dialog.LineWidthInput.val(s.LineWidth)}this.Tolerance=.05,SAM.MOBILE_DEVICE&&(this.Tolerance=.1);if(t==null)return;this.CreationCamera=t.GetCamera().Serialize(),this.Layer=t,this.Popup=new SAM.WidgetPopup(this);var o=t.GetCamera(),u=t.GetViewport();this.Shape=new SAM.Circle,this.Shape.Origin=[0,0],this.Shape.OutlineColor=[0,0,0],this.Shape.SetOutlineColor(this.Dialog.ColorInput.val()),this.Shape.Radius=50*o.Height/u[3],this.Shape.LineWidth=5*o.Height/u[3],this.Shape.FixedSize=!1,this.Layer.AddWidget(this);if(n){this.State=e,this.Layer.ActivateWidget(this);return}this.State=i}var e=0,t=1,n=2,r=3,i=4,s=5,o=6;u.prototype.Draw=function(t){this.State!=e&&this.Shape.Draw(t)},u.prototype.PasteCallback=function(e,t){this.Load(e),this.Shape.Origin=[t[0],t[1]],this.Layer.EventuallyDraw()},u.prototype.Serialize=function(){if(this.Shape===undefined)return null;var e=new Object;return e.type="circle",e.user_note_flag=this.UserNoteFlag,e.origin=this.Shape.Origin,e.outlinecolor=this.Shape.OutlineColor,e.radius=this.Shape.Radius,e.linewidth=this.Shape.LineWidth,e.creation_camera=this.CreationCamera,e},u.prototype.Load=function(e){this.Shape.Origin[0]=parseFloat(e.origin[0]),this.Shape.Origin[1]=parseFloat(e.origin[1]),this.Shape.OutlineColor[0]=parseFloat(e.outlinecolor[0]),this.Shape.OutlineColor[1]=parseFloat(e.outlinecolor[1]),this.Shape.OutlineColor[2]=parseFloat(e.outlinecolor[2]),this.Shape.Radius=parseFloat(e.radius),this.Shape.LineWidth=parseFloat(e.linewidth),this.Shape.FixedSize=!1,this.Shape.UpdateBuffers(this.Layer.AnnotationView),this.UserNoteFlag=e.user_note_flag,e.creation_camera!==undefined&&(this.CreationCamera=e.CreationCamera)},u.prototype.HandleMouseWheel=function(e){return!0},u.prototype.HandleKeyDown=function(e){if(this.State==o)return!1;if(event.keyCode==67&&event.ctrlKey){var t={Type:"CircleWidget",Data:this.Serialize()};return localStorage.ClipBoard=JSON.stringify(t),!1}return!0},u.prototype.HandleDoubleClick=function(e){return ShowPropertiesDialog(),!1},u.prototype.HandleMouseDown=function(e){if(e.which!=1)return!1;var i=this.Layer.GetCamera();return this.State==t&&(this.OriginViewer=i.ConvertPointWorldToViewer(this.Shape.Origin[0],this.Shape.Origin[1]),this.State=r),this.State==s&&(this.NormalizedActiveDistance<.5?this.State=n:(this.OriginViewer=i.ConvertPointWorldToViewer(this.Shape.Origin[0],this.Shape.Origin[1]),this.State=r)),!1},u.prototype.HandleMouseUp=function(e){if(this.State==n||this.State==r)this.SetActive(!1),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),window.SA&&SA.RecordState();return!1},u.prototype.HandleMouseMove=function(o){var u=o.offsetX,a=o.offsetY;if(o.which==0&&this.State==s)return this.SetActive(this.CheckActive(o)),!1;if(o.which!=0||this.State!=n&&this.State!=r){var f=this.Layer.GetCamera();this.State==e&&(this.State=t);if(this.State==t||this.State==n)SA&&SA.notesWidget&&!this.UserNoteFlag&&SA.notesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),this.Shape.Origin=f.ConvertPointViewerToWorld(u,a),this.PlacePopup(),this.Layer.EventuallyDraw();if(this.State==r){var l=this.Layer.GetViewport(),f=this.Layer.GetCamera(),c=u-this.OriginViewer[0],h=a-this.OriginViewer[1];this.Shape.Radius=Math.sqrt(c*c+h*h)*f.Height/l[3],this.Shape.UpdateBuffers(this.Layer.AnnotationView),SA&&SA.notesWidget&&!this.UserNoteFlag&&SA.notesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),this.PlacePopup(),this.Layer.EventuallyDraw()}return this.State==i&&this.CheckActive(o),!1}return this.SetActive(!1),!0},u.prototype.HandleTouchPan=function(e){var t=this.Layer.GetCamera();w0=t.ConvertPointViewerToWorld(this.Layer.LastMouseX,this.Layer.LastMouseY),w1=t.ConvertPointViewerToWorld(e.offsetX,e.offsetY);var n=w1[0]-w0[0],r=w1[1]-w0[1];return this.Shape.Origin[0]+=n,this.Shape.Origin[1]+=r,this.Layer.EventuallyDraw(),!1},u.prototype.HandleTouchPinch=function(e){return this.Shape.Radius*=e.PinchScale,this.Shape.UpdateBuffers(this.Layer.AnnotationView),SA&&SA.notesWidget&&!this.UserNoteFlag&&SA.notesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),this.Layer.EventuallyDraw(),!1},u.prototype.HandleTouchEnd=function(e){return this.SetActive(!1),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),!1},u.prototype.CheckActive=function(n){if(this.State==e||this.State==t)return!0;var r=n.offsetX,i=n.offsetY;this.FixedSize?(r=n.offsetX-this.Shape.Origin[0],i=n.offsetY-this.Shape.Origin[1]):(r=n.worldX-this.Shape.Origin[0],i=n.worldY-this.Shape.Origin[1]);var s=Math.sqrt(r*r+i*i)/this.Shape.Radius,o=!1,u=this.Shape.LineWidth/this.Shape.Radius;this.NormalizedActiveDistance=s;if(this.Shape.FillColor==undefined){if(s<1+this.Tolerance+u&&s>1-this.Tolerance||s<this.Tolerance+u)o=!0}else if(s<1+this.Tolerance+u&&s>this.Tolerance+u||s<u)o=!0;return o},u.prototype.GetActive=function(){return this.State==i?!1:!0},u.prototype.Deactivate=function(){if(this.State==e){this.Layer.RemoveWidget(this);return}this.Popup.StartHideTimer(),this.State=i,this.Shape.Active=!1,this.Layer.DeactivateWidget(this),this.DeactivateCallback&&this.DeactivateCallback(),this.Layer.EventuallyDraw()},u.prototype.SetActive=function(e){if(e==this.GetActive())return;e?(this.State=s,this.Shape.Active=!0,this.Layer.ActivateWidget(this),this.Layer.EventuallyDraw(),this.PlacePopup()):this.Deactivate(),this.Layer.EventuallyDraw()},u.prototype.PlacePopup=function(){var e=this.Layer.GetCamera(),t=e.Roll,n=this.Shape.Origin[0]+.8*this.Shape.Radius*(Math.cos(t)-Math.sin(t)),r=this.Shape.Origin[1]-.8*this.Shape.Radius*(Math.cos(t)+Math.sin(t)),i=e.ConvertPointWorldToViewer(n,r);this.Popup.Show(i[0],i[1])};var a;u.prototype.ShowPropertiesDialog=function(){this.Dialog.ColorInput.val(SAM.ConvertColorToHex(this.Shape.OutlineColor)),this.Dialog.LineWidthInput.val(this.Shape.LineWidth.toFixed(2));var e=2*Math.PI*this.Shape.Radius*this.Shape.Radius*.25*.25,t="";this.Shape.FixedSize?(t+=e.toFixed(2),t+=" pixels^2"):e>1e6?(t+=(e/1e6).toFixed(2),t+=" mm^2"):(t+=e.toFixed(2),t+=" um^2"),this.Dialog.Area.text(t),this.Dialog.Show(!0)},u.prototype.DialogApplyCallback=function(){var e=this.Dialog.ColorInput.val();this.Shape.SetOutlineColor(e),this.Shape.LineWidth=parseFloat(this.Dialog.LineWidthInput.val()),this.Shape.UpdateBuffers(this.Layer.AnnotationView),this.SetActive(!1),window.SA&&SA.RecordState(),this.Layer.EventuallyDraw(),localStorage.CircleWidgetDefaults=JSON.stringify({Color:e,LineWidth:this.Shape.LineWidth}),SA&&SA.notesWidget&&!this.UserNoteFlag&&SA.notesWidget.MarkAsModified(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote()},SAM.CircleWidget=u})();(function(){"use strict";function l(){SAM.Shape.call(this),this.Width=50,this.Height=50,this.Orientation=0,this.Origin=[1e4,1e4],this.OutlineColor=[0,0,0],this.PointBuffer=[]}function c(t,n){this.Visibility=!0,this.UserNoteFlag=!SA.Edit;var r=this;this.Dialog=new SAM.Dialog(function(){r.DialogApplyCallback()}),this.Dialog.Title.text("Rect Annotation Editor"),this.Dialog.ColorDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.ColorLabel=$("<div>").appendTo(this.Dialog.ColorDiv).text("Color:").css({display:"table-cell","text-align":"left"}),this.Dialog.ColorInput=$('<input type="color">').appendTo(this.Dialog.ColorDiv).val("#30ff00").css({display:"table-cell"}),this.Dialog.LineWidthDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.LineWidthLabel=$("<div>").appendTo(this.Dialog.LineWidthDiv).text("Line Width:").css({display:"table-cell","text-align":"left"}),this.Dialog.LineWidthInput=$('<input type="number">').appendTo(this.Dialog.LineWidthDiv).css({display:"table-cell"}).keypress(function(e){return e.keyCode!=13}),this.Dialog.AreaDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.AreaLabel=$("<div>").appendTo(this.Dialog.AreaDiv).text("Area:").css({display:"table-cell","text-align":"left"}),this.Dialog.Area=$("<div>").appendTo(this.Dialog.AreaDiv).css({display:"table-cell"});if(localStorage.RectWidgetDefaults){var i=JSON.parse(localStorage.RectWidgetDefaults);i.Color&&this.Dialog.ColorInput.val(SAM.ConvertColorToHex(i.Color)),this.Dialog.LineWidthInput.val(0),i.LineWidth&&this.Dialog.LineWidthInput.val(i.LineWidth)}this.Tolerance=.05,SAM.detectMobile()&&(this.Tolerance=.1);if(t===null)return;this.CreationCamera=t.GetCamera().Serialize(),this.Layer=t,this.Popup=new SAM.WidgetPopup(this);var o=t.GetCamera(),u=t.GetViewport();this.Shape=new l,this.Shape.Orientation=o.GetRotation(),this.Shape.Origin=[0,0],this.Shape.OutlineColor=[0,0,0],this.Shape.SetOutlineColor(this.Dialog.ColorInput.val()),a>0?(this.Shape.Height=f,this.Shape.Width=a):(this.Shape.Height=50*o.Height/u[3],this.Shape.Width=50*o.Height/u[3]),this.Shape.LineWidth=0,this.Shape.FixedSize=!1,this.Layer.AddWidget(this);if(n){this.State=e,this.Layer.ActivateWidget(this),this.Layer.GetCanvasDiv().css({cursor:"crosshair"});return}this.Layer.GetCanvasDiv().css({cursor:"default"}),this.State=s}var e=0,t=1,n=2,r=3,i=4,s=5,o=6,u=7,a=-1,f=-1;l.prototype=new SAM.Shape,l.prototype.destructor=function(){},l.prototype.UpdateBuffers=function(e){this.PointBuffer=[],this.Matrix=mat4.create(),mat4.identity(this.Matrix),mat4.rotateZ(this.Matrix,this.Orientation/180*3.14159),this.PointBuffer.push(1*this.Width/2),this.PointBuffer.push(1*this.Height/2),this.PointBuffer.push(0),this.PointBuffer.push(-1*this.Width/2),this.PointBuffer.push(1*this.Height/2),this.PointBuffer.push(0),this.PointBuffer.push(-1*this.Width/2),this.PointBuffer.push(-1*this.Height/2),this.PointBuffer.push(0),this.PointBuffer.push(1*this.Width/2),this.PointBuffer.push(-1*this.Height/2),this.PointBuffer.push(0),this.PointBuffer.push(1*this.Width/2),this.PointBuffer.push(1*this.Height/2),this.PointBuffer.push(0)},c.prototype.SetThreshold=function(e){this.confidence!==undefined&&(this.Visibility=this.confidence>=e)},c.prototype.Draw=function(t){this.State!=e&&this.Visibility&&this.Shape.Draw(t)},c.prototype.PasteCallback=function(e,t){this.Load(e),this.Shape.Origin=[t[0],t[1]],this.Layer.EventuallyDraw(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified()},c.prototype.Serialize=function(){if(this.Shape===undefined)return null;var e={};return e.type="rect",e.user_note_flag=this.UserNoteFlag,e.origin=this.Shape.Origin,e.origin[2]=0,e.outlinecolor=this.Shape.OutlineColor,e.height=this.Shape.Height,e.width=this.Shape.Width,e.orientation=this.Shape.Orientation,e.linewidth=this.Shape.LineWidth,e.creation_camera=this.CreationCamera,e},c.prototype.Load=function(e){this.UserNoteFlag=e.user_note_flag,this.Shape.Origin[0]=parseFloat(e.origin[0]),this.Shape.Origin[1]=parseFloat(e.origin[1]),e.outlinecolor&&(this.Shape.OutlineColor[0]=parseFloat(e.outlinecolor[0]),this.Shape.OutlineColor[1]=parseFloat(e.outlinecolor[1]),this.Shape.OutlineColor[2]=parseFloat(e.outlinecolor[2])),this.Shape.Width=parseFloat(e.width),e.confidence&&(this.confidence=parseFloat(e.confidence)),e.length&&(this.Shape.Height=parseFloat(e.length)),e.height&&(this.Shape.Height=parseFloat(e.height)),e.orientation&&(this.Shape.Orientation=parseFloat(e.orientation)),e.linewidth!==undefined&&(this.Shape.LineWidth=parseFloat(e.linewidth)),this.Shape.FixedSize=!1,this.Shape.UpdateBuffers(this.Layer.AnnotationView),e.creation_camera!==undefined&&(this.CreationCamera=e.CreationCamera)},c.prototype.HandleKeyDown=function(e,n){if(!this.Visibility)return!0;if(this.State==u)return!1;if(this.State==t)if(event.keyCode==27||event.keyCode==32||event.keyCode==13)return this.Deactivate(),this.RemoveFromLayer(),!1;if(event.keyCode==67&&event.ctrlKey){var r={Type:"RectWidget",Data:this.Serialize()};return localStorage.ClipBoard=JSON.stringify(r),!1}return!0},c.prototype.HandleDoubleClick=function(e){return this.Deactivate(),this.RemoveFromLayer(),!1},c.prototype.HandleMouseDown=function(e){return this.Visibility?e.which!=1?!1:this.State==t?(this.State=r,!1):(this.State==n&&(this.OriginViewer=this.Layer.GetCamera().ConvertPointWorldToViewer(this.Shape.Origin[0],this.Shape.Origin[1]),this.State=r),this.State==o&&(this.NormalizedActiveDistance<.5?this.State=i:(this.OriginViewer=this.Layer.GetCamera().ConvertPointWorldToViewer(this.Shape.Origin[0],this.Shape.Origin[1]),this.State=i)),!0):!0},c.prototype.HandleMouseUp=function(e){if(!this.Visibility)return!0;if(this.State==r){this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified();var n=new c(this.Layer,!1);n.Load(this.Serialize()),this.State=t,window.SA&&SA.RecordState(),a=this.Shape.Width,f=this.Shape.Height}},c.prototype.HandleMouseMove=function(n){if(!this.Visibility)return!0;var o=n.offsetX,u=n.offsetY;if(n.which===0)return this.State==e&&(this.State=t),this.State==t?(this.Shape.Origin=this.Layer.GetCamera().ConvertPointViewerToWorld(o,u),this.Layer.EventuallyDraw(),!1):!0;if(n.which!=1)return;if(this.State==r){var a=this.Layer.GetCamera().ConvertPointViewerToWorld(o,u),f=a[0]-this.Shape.Origin[0],l=a[1]-this.Shape.Origin[1];if(Math.abs(f)<2&&Math.abs(l)<2)return!1;var c=this.Shape.Orientation*Math.PI/180,h=Math.cos(c),p=Math.sin(c),d=f*h-l*p,v=l*h+f*p;return this.Shape.Width=Math.abs(2*d),this.Shape.Height=Math.abs(2*v),this.Shape.UpdateBuffers(),this.PlacePopup(),this.Layer.EventuallyDraw(),!1}if(this.State==i){var m=this.Layer.GetViewport(),g=this.Layer.GetCamera,f=o-this.OriginViewer[0],l=u-this.OriginViewer[1];this.Shape.UpdateBuffers(this.Layer.AnnotationView),this.PlacePopup(),this.Layer.EventuallyDraw()}this.State==s&&this.CheckActive(n)},c.prototype.HandleMouseWheel=function(e){if(!this.Visibility)return!0;var t=e.offsetX,n=e.offsetY;if(this.State==o&&this.NormalizedActiveDistance<.5){var r=1.05,i=1;e.wheelDelta<0&&(r=.95,i=-1),e.shiftKey&&(this.Shape.Height=this.Shape.Height*r),e.ctrlKey&&(this.Shape.Width=this.Shape.Width*r),!e.shiftKey&&!e.ctrlKey&&(this.Shape.Orientation=this.Shape.Orientation+3*i),this.Shape.UpdateBuffers(this.Layer.AnnotationView),this.PlacePopup(),this.Layer.EventuallyDraw()}},c.prototype.HandleTouchPan=function(e){if(!this.Visibility)return!0;w0=this.Layer.GetCamera().ConvertPointViewerToWorld(EVENT_MANAGER.LastMouseX,EVENT_MANAGER.LastMouseY),w1=this.Layer.GetCamera().ConvertPointViewerToWorld(e.offsetX,e.offsetY);var t=w1[0]-w0[0],n=w1[1]-w0[1];this.Shape.Origin[0]+=t,this.Shape.Origin[1]+=n,this.Layer.EventuallyDraw()},c.prototype.HandleTouchPinch=function(e){if(!this.Visibility)return!0;this.Shape.UpdateBuffers(this.Layer.AnnotationView),this.Layer.EventuallyDraw()},c.prototype.HandleTouchEnd=function(e){if(!this.Visibility)return!0;this.SetActive(!1),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified()},c.prototype.CheckActive=function(e){if(!this.Visibility)return!1;var t=e.offsetX,n=e.offsetY,r,i;this.FixedSize?(r=e.offsetX-this.Shape.Origin[0],i=e.offsetY-this.Shape.Origin[1]):(r=e.worldX-this.Shape.Origin[0],i=e.worldY-this.Shape.Origin[1]);var s=Math.sqrt(r*r+i*i)/(this.Shape.Width*.5),o=!1,u=this.Shape.LineWidth/(this.Shape.Width*.5);this.NormalizedActiveDistance=s;if(this.Shape.FillColor===undefined){if(s<1+this.Tolerance+u&&s>1-this.Tolerance||s<this.Tolerance+u)o=!0}else if(s<1+this.Tolerance+u&&s>this.Tolerance+u||s<u)o=!0;return this.SetActive(o),o},c.prototype.GetActive=function(){return this.State==s?!1:!0},c.prototype.RemoveFromLayer=function(){this.Layer&&this.Layer.RemoveWidget(this),this.Layer=null},c.prototype.Deactivate=function(){this.Popup.StartHideTimer(),this.Layer.GetCanvasDiv().css({cursor:"default"}),this.Layer.DeactivateWidget(this),this.State=s,this.Shape.Active=!1,this.DeactivateCallback&&this.DeactivateCallback(),this.Layer.EventuallyDraw()},c.prototype.SetActive=function(e){this.Visibility||(this.Visibility=!0);if(e==this.GetActive())return;e?(this.State=o,this.Shape.Active=!0,this.Layer.ActivateWidget(this),this.Layer.EventuallyDraw(),this.PlacePopup()):this.Deactivate(),this.Layer.EventuallyDraw()},c.prototype.PlacePopup=function(){if(!this.Visibility)return;var e=this.Layer.GetCamera().Roll,t=this.Shape.Width*.5,n=this.Shape.Origin[0]+.8*t*(Math.cos(e)-Math.sin(e)),r=this.Shape.Origin[1]-.8*t*(Math.cos(e)+Math.sin(e)),i=this.Layer.GetCamera().ConvertPointWorldToViewer(n,r);this.Popup.Show(i[0],i[1])};var h;c.prototype.ShowPropertiesDialog=function(){this.Dialog.ColorInput.val(SAM.ConvertColorToHex(this.Shape.OutlineColor)),this.Dialog.LineWidthInput.val(this.Shape.LineWidth.toFixed(2));var e=this.Shape.Width*.5,t=2*Math.PI*e*e*.25*.25,n="";this.Shape.FixedSize?(n+=t.toFixed(2),n+=" pixels^2"):t>1e6?(n+=(t/1e6).toFixed(2),n+=" mm^2"):(n+=t.toFixed(2),n+=" um^2"),this.Dialog.Area.text(n),this.Dialog.Show(!0)},c.prototype.DialogApplyCallback=function(){var e=this.Dialog.ColorInput.val();this.Shape.SetOutlineColor(e),this.Shape.LineWidth=parseFloat(this.Dialog.LineWidthInput.val()),this.Shape.UpdateBuffers(this.Layer.AnnotationView),this.SetActive(!1),window.SA&&SA.RecordState(),this.Layer.EventuallyDraw(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),localStorage.RectWidgetDefaults=JSON.stringify({Color:e,LineWidth:this.Shape.LineWidth})},SAM.RectWidget=c})();(function(){"use strict";function e(){this.Centers=[],this.Widths=[],this.Heights=[],this.Labels=[],this.Confidences=[],this.Threshold=0,this.ActiveIndex=-1}function t(t,n){this.Visibility=!0,this.UserNoteFlag=!SA.Edit;if(t===null)return;this.Shape=new e,t&&(this.Layer=t,this.Layer.AddWidget(this)),this.Active=!1}e.prototype.GetLength=function(){return this.Widths.length},e.prototype.GetCenter=function(e){return e*=2,[this.Centers[e],this.Centers[e+1]]},e.prototype.SetCenter=function(e,t){e*=2,this.Centers[e]=t[0],this.Centers[e+1]=t[1]},e.prototype.SetShape=function(e){for(var t=0;t<this.Widths.length;++t)this.Widths[t]=e[0],this.Heights[t]=e[1]},e.prototype.CopyRectangle=function(e,t,n){n===undefined&&(n=this.Labels.length);var r=t*2,i=n*2;this.Centers[i]=e.Centers[r],this.Centers[i+1]=e.Centers[r+1],this.Widths[n]=e.Widths[t],this.Heights[n]=e.Heights[t],this.Labels[n]=e.Labels[t],this.Confidences[n]=e.Confidences[t]},e.prototype.AddRectangle=function(e,t,n){var r=this.Labels.length;this.Centers[r*2]=e[0],this.Centers[r*2+1]=e[1],this.Widths[r]=t,this.Heights[r]=n,this.Labels[r]="",this.Confidences[r]=1},e.prototype.DeleteRectangle=function(e){if(e<0||e>=this.Widths.length)return;this.Centers.splice(2*e,2),this.Widths.splice(e,1),this.Heights.splice(e,1),this.Labels.splice(e,1),this.Confidences.splice(e,1),this.ActiveIndex==e&&(this.ActiveIndex=-1)},e.prototype.SetOutlineColor=function(e){this.Color=SAM.ConvertColorToHex(e)},e.prototype.Draw=function(e){e.Context2d.save(),e.Context2d.setTransform(1,0,0,1,0,0);var t=e.Camera.Roll,n=Math.cos(t),r=Math.sin(t),i=-Math.sin(t),s=Math.cos(t),o=e.Viewport[3]/e.Camera.GetHeight(),u=e.Camera.Matrix,a=u[12]/u[15],f=u[13]/u[15];a=e.Viewport[2]*.5*(1+a),f=e.Viewport[3]*.5*(1-f),e.Context2d.transform(n,r,i,s,a,f),e.Context2d.lineWidth=1;var l=!0,c=0,a=0,f=0;for(var h=0;h<this.Widths.length;++h)if(this.Confidences[h]>=this.Threshold){var p=this.Widths[h]/2,d=this.Heights[h]/2;a=this.Centers[c++],f=this.Centers[c++],e.Context2d.beginPath();if(this.LabelColors&&this.LabelColors[this.Labels[h]])e.Context2d.strokeStyle=this.LabelColors[this.Labels[h]];else if(this.Color)e.Context2d.strokeStyle=this.Color;else{var v=Math.floor(this.Confidences[h]*255);e.Context2d.strokeStyle="#"+v.toString(16)+"ff00",e.Context2d.beginPath()}e.Context2d.moveTo((a-p)*o,(f-d)*o),e.Context2d.lineTo((a+p)*o,(f-d)*o),e.Context2d.lineTo((a+p)*o,(f+d)*o),e.Context2d.lineTo((a-p)*o,(f+d)*o),e.Context2d.lineTo((a-p)*o,(f-d)*o),e.Context2d.stroke(),h==this.ActiveIndex&&(e.Context2d.beginPath(),e.Context2d.strokeStyle="#00ffff",e.Context2d.moveTo((a-p)*o,f*o),e.Context2d.lineTo((a-p/2)*o,f*o),e.Context2d.moveTo((a+p)*o,f*o),e.Context2d.lineTo((a+p/2)*o,f*o),e.Context2d.moveTo(a*o,(f-d)*o),e.Context2d.lineTo(a*o,(f-d/2)*o),e.Context2d.moveTo(a*o,(f+d)*o),e.Context2d.lineTo(a*o,(f+d/2)*o),e.Context2d.stroke())}else c+=2},t.prototype.GetLength=function(){return this.Shape.Widths.length},t.prototype.Sort=function(e){var t=new Array(this.Confidences.length),n=1;e&&(n=-1);for(var r=0;r<t.length;++r)t[r]={conf:n*this.Confidences[r],idx:r};t.sort(function(e,t){return e.conf>t.conf?1:e.conf<t.conf?-1:0});var i=new Array(this.Confidences.length),s=new Array(this.Centers.length),o=new Array(this.Centers.length);for(var r=0;r<i.length;++r){var u=t[r].idx;o[r]=this.Labels[u],i[r]=this.Confidences[u],u*=2,s[2*r]=this.Centers[u],s[2*r+1]=this.Centers[u+1]}this.Centers=s,this.Confidences=i,this.Labels=o},t.prototype.SetThreshold=function(e){this.Shape.Threshold=e},t.prototype.Draw=function(e){this.Visibility&&this.Shape.Draw(e)},t.prototype.Serialize=function(){if(this.Shape===undefined)return null;var e={type:"rect_set"};this.UserNoteFlag!==undefined&&(e.user_note_flag=this.UserNoteFlag),this.Shape.Color&&(e.color=SAM.ConvertColor(this.Shape.Color));var t=this.Shape.Widths.length;e.confidences=new Array(t),e.widths=new Array(t),e.heights=new Array(t),e.labels=new Array(t),e.centers=new Array(t*2);for(var n=0;n<t;++n)e.widths[n]=this.Shape.Widths[n],e.heights[n]=this.Shape.Heights[n],e.confidences[n]=this.Shape.Confidences[n],e.centers[n]=this.Shape.Centers[n],e.centers[n+t]=this.Shape.Centers[n+t],e.labels[n]=this.Shape.Labels[n];return e},t.prototype.Load=function(e){this.UserNoteFlag=e.user_note_flag,e.color&&(this.Shape.Color=[parseFloat(e.color[0]),parseFloat(e.color[1]),parseFloat(e.color[2])]);var t=e.widths.length;this.Shape.Confidences=new Array(t),this.Shape.Labels=new Array(t),this.Shape.Widths=new Array(t),this.Shape.Heights=new Array(t),this.Shape.Centers=new Array(t*2);for(var n=0;n<t;++n)this.Shape.Widths[n]=parseFloat(e.widths[n]),this.Shape.Heights[n]=parseFloat(e.heights[n]),this.Shape.Confidences[n]=parseFloat(e.confidences[n]),e.labels?this.Shape.Labels[n]=e.labels[n]:this.Shape.Labels[n]="",this.Shape.Centers[n]=parseFloat(e.centers[n]),this.Shape.Centers[n+t]=parseFloat(e.centers[n+t])},t.prototype.HandleDoubleClick=function(e){return!0},t.prototype.HandleMouseUp=function(e){return!0},t.prototype.HandleMouseMove=function(e){return!0},t.prototype.HandleMouseWheel=function(e){return!0},t.prototype.HandleTouchPan=function(e){return!0},t.prototype.HandleTouchPinch=function(e){return!0},t.prototype.HandleTouchEnd=function(e){return!0},t.prototype.CheckActive=function(e){return this.Active},t.prototype.GetActive=function(){return this.Active},t.prototype.RemoveFromLayer=function(){this.Layer&&this.Layer.RemoveWidget(this),this.Layer=null},t.prototype.Deactivate=function(){},t.prototype.PlacePopup=function(){},t.prototype.ShowPropertiesDialog=function(){},SAM.RectSetWidget=t,SAM.RectSet=e})();(function(){"use strict";function l(){SAM.Shape.call(this),this.BinWidth=20,this.BinHeight=20,this.Dimensions=[10,8],this.Orientation=0,this.Origin=[1e4,1e4],this.OutlineColor=[0,0,0],this.PointBuffer=[],this.ActiveIndex=undefined}function c(e,n){this.UserNoteFlag=!SA.Edit;var r=this;this.Dialog=new SAM.Dialog(function(){r.DialogApplyCallback()}),this.Dialog.Title.text("Grid Annotation Editor"),this.Dialog.BinWidthDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.BinWidthLabel=$("<div>").appendTo(this.Dialog.BinWidthDiv).text("Bin Width:").css({display:"table-cell","text-align":"left"}),this.Dialog.BinWidthInput=$("<input>").appendTo(this.Dialog.BinWidthDiv).css({display:"table-cell"}).keypress(function(e){return e.keyCode!=13}),this.Dialog.BinHeightDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.BinHeightLabel=$("<div>").appendTo(this.Dialog.BinHeightDiv).text("Bin Height:").css({display:"table-cell","text-align":"left"}),this.Dialog.BinHeightInput=$("<input>").appendTo(this.Dialog.BinHeightDiv).css({display:"table-cell"}).keypress(function(e){return e.keyCode!=13}),this.Dialog.RotationDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.RotationLabel=$("<div>").appendTo(this.Dialog.RotationDiv).text("Rotation:").css({display:"table-cell","text-align":"left"}),this.Dialog.RotationInput=$("<input>").appendTo(this.Dialog.RotationDiv).css({display:"table-cell"}).keypress(function(e){return e.keyCode!=13}),this.Dialog.ColorDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.ColorLabel=$("<div>").appendTo(this.Dialog.ColorDiv).text("Color:").css({display:"table-cell","text-align":"left"}),this.Dialog.ColorInput=$('<input type="color">').appendTo(this.Dialog.ColorDiv).val("#30ff00").css({display:"table-cell"}),this.Dialog.LineWidthDiv=$("<div>").appendTo(this.Dialog.Body).css({display:"table-row"}),this.Dialog.LineWidthLabel=$("<div>").appendTo(this.Dialog.LineWidthDiv).text("Line Width:").css({display:"table-cell","text-align":"left"}),this.Dialog.LineWidthInput=$('<input type="number">').appendTo(this.Dialog.LineWidthDiv).css({display:"table-cell"}).keypress(function(e){return e.keyCode!=13}),this.Tolerance=.05,SAM.MOBILE_DEVICE&&(this.Tolerance=.1);if(e===null)return;this.CreationCamera=e.GetCamera().Serialize(),this.Layer=e,this.Popup=new SAM.WidgetPopup(this);var i=e.AnnotationView.Camera,s=e.AnnotationView.Viewport;this.Grid=new l,this.Grid.Origin=[0,0],this.Grid.OutlineColor=[0,0,0],this.Grid.SetOutlineColor("#0A0F7A"),e.ScaleWidget?this.Grid.BinWidth=e.ScaleWidget.LengthWorld:this.Grid.BinWidth=30*i.Height/s[3],this.Grid.BinHeight=this.Grid.BinWidth,this.Grid.LineWidth=2*i.Height/s[3],this.Grid.FixedSize=!1;var o=.8*s[2]/e.GetPixelsPerUnit();this.Grid.Dimensions[0]=Math.floor(o/this.Grid.BinWidth);var u=.8*s[3]/e.GetPixelsPerUnit();this.Grid.Dimensions[1]=Math.floor(u/this.Grid.BinHeight),this.Grid.UpdateBuffers(this.Layer.AnnotationView),this.Text=new SAM.Text,this.Text.Position=this.Grid.Origin,this.Text.String=SAM.DistanceToString(this.Grid.BinWidth*2.5e-7),this.Text.Color=[0,0,.5],this.Text.Anchor=[0,0],this.Text.UpdateBuffers(this.Layer.AnnotationView);if(localStorage.GridWidgetDefaults){var a=JSON.parse(localStorage.GridWidgetDefaults);a.Color&&(this.Dialog.ColorInput.val(SAM.ConvertColorToHex(a.Color)),this.Grid.SetOutlineColor(this.Dialog.ColorInput.val())),a.LineWidth!=undefined&&(this.Dialog.LineWidthInput.val(a.LineWidth),this.Grid.LineWidth==a.LineWidth)}this.Layer.AddWidget(this),this.State=t}var e=0,t=3,n=4,r=5,i=6,s=7,o=8,u=9,a=10,f=11;l.prototype=new SAM.Shape,l.prototype.destructor=function(){},l.prototype.UpdateBuffers=function(e){this.PointBuffer=[],this.Matrix=mat4.create(),mat4.identity(this.Matrix);if(this.Dimensions[0]<1||this.Dimensions[1]<1||this.BinWidth<=0||this.BinHeight<=0)return;var t=this.BinWidth*this.Dimensions[0],n=this.BinHeight*this.Dimensions[1],r=t/2,i=n/2,s=this.Dimensions[1]%2?0:t,o=0;this.PointBuffer.push(s-r),this.PointBuffer.push(o-i),this.PointBuffer.push(0);for(var u=0;u<this.Dimensions[1];++u)s=s?0:t,this.PointBuffer.push(s-r),this.PointBuffer.push(o-i),this.PointBuffer.push(0),o+=this.BinHeight,this.PointBuffer.push(s-r),this.PointBuffer.push(o-i),this.PointBuffer.push(0);s=s?0:t,this.PointBuffer.push(s-r),this.PointBuffer.push(o-i),this.PointBuffer.push(0);for(var u=0;u<this.Dimensions[0];++u)o=o?0:n,this.PointBuffer.push(s-r),this.PointBuffer.push(o-i),this.PointBuffer.push(0),s+=this.BinWidth,this.PointBuffer.push(s-r),this.PointBuffer.push(o-i),this.PointBuffer.push(0);o=o?0:n,this.PointBuffer.push(s-r),this.PointBuffer.push(o-i),this.PointBuffer.push(0)},c.prototype.ComputeCorner=function(e,t,n,r){var i=this.Grid.BinWidth*this.Grid.Dimensions[0]/2,s=this.Grid.BinHeight*this.Grid.Dimensions[1]/2;i+=n,s+=r;var o=this.Grid.Origin[0],u=this.Grid.Origin[1],a=(this.Layer.GetCamera().GetRotation()-this.Grid.Orientation)/90;a=Math.round(a),a=(a%4+4)%4;var f=Math.cos(3.14156*this.Grid.Orientation/180),l=Math.sin(3.14156*this.Grid.Orientation/180),c,h;return a==0?(c=e*i,h=t*s):a==3?(c=e*i,h=-t*s):a==2?(c=-e*i,h=-t*s):a==1&&(c=-e*i,h=t*s),o=o+f*c+l*h,u=u+f*h-l*c,[o,u]},c.prototype.Draw=function(e){this.Grid.Draw(e);var t=-(this.Grid.BinWidth*this.Grid.Dimensions[0]/2),n=-(this.Grid.BinHeight*this.Grid.Dimensions[1]/2);this.Text.Anchor=[0,20],this.Text.Orientation=this.Grid.Orientation-this.Layer.GetCamera().GetRotation(),this.Text.Orientation=(this.Text.Orientation%360+360)%360,this.Text.Orientation>90&&this.Text.Orientation<270&&(this.Text.Orientation-=180,this.Text.Anchor=[this.Text.PixelBounds[1],0]);var r=this.Grid.Orientation*Math.PI/180,i=Math.cos(r),s=Math.sin(r),o=i*t+s*n,u=i*n-s*t;this.Text.Position=[this.Grid.Origin[0]+o,this.Grid.Origin[1]+u],this.Text.Draw(e)},c.prototype.PasteCallback=function(e,t,n){this.Load(e);var r=this.Layer.GetCamera().GetRotation()-n.GetRotation();this.Grid.Orientation+=r,this.Grid.Origin=[t[0],t[1]],this.Text.Position=[t[0],t[1]],this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),this.Layer.EventuallyDraw()},c.prototype.Serialize=function(){if(this.Grid===undefined)return null;var e={};return e.type="grid",e.user_note_flag=this.UserNoteFlag,e.origin=this.Grid.Origin,e.outlinecolor=this.Grid.OutlineColor,e.bin_width=this.Grid.BinWidth,e.bin_height=this.Grid.BinHeight,e.dimensions=this.Grid.Dimensions,e.orientation=this.Grid.Orientation,e.linewidth=this.Grid.LineWidth,e.creation_camera=this.CreationCamera,e},c.prototype.Load=function(e){this.UserNoteFlag=e.user_note_flag,this.Grid.Origin[0]=parseFloat(e.origin[0]),this.Grid.Origin[1]=parseFloat(e.origin[1]),this.Grid.OutlineColor[0]=parseFloat(e.outlinecolor[0]),this.Grid.OutlineColor[1]=parseFloat(e.outlinecolor[1]),this.Grid.OutlineColor[2]=parseFloat(e.outlinecolor[2]),e.width&&(this.Grid.BinWidth=parseFloat(e.width)),e.height&&(this.Grid.BinHeight=parseFloat(e.height)),e.bin_width&&(this.Grid.BinWidth=parseFloat(e.bin_width)),e.bin_height&&(this.Grid.BinHeight=parseFloat(e.bin_height)),this.Grid.Dimensions[0]=parseInt(e.dimensions[0]),this.Grid.Dimensions[1]=parseInt(e.dimensions[1]),this.Grid.Orientation=parseFloat(e.orientation),this.Grid.LineWidth=parseFloat(e.linewidth),this.Grid.FixedSize=!1,this.Grid.UpdateBuffers(this.Layer.AnnotationView),this.Text.String=SAM.DistanceToString(this.Grid.BinWidth*2.5e-7),this.Text.Position=this.Grid.Origin,this.Text.UpdateBuffers(this.Layer.AnnotationView),e.creation_camera!==undefined&&(this.CreationCamera=e.CreationCamera)},c.prototype.HandleKeyPress=function(e,t){if(this.State==r)return!1;if(event.keyCode==67&&event.ctrlKey){var n={Type:"GridWidget",Data:this.Serialize(),Camera:this.Layer.GetCamera().Serialize()};return localStorage.ClipBoard=JSON.stringify(n),!1}return!0},c.prototype.HandleDoubleClick=function(e){return!0},c.prototype.HandleMouseDown=function(e){if(e.which!=1)return!0;var t=this.Layer.GetCamera();return this.DragLast=t.ConvertPointViewerToWorld(e.offsetX,e.offsetY),!1},c.prototype.HandleMouseUp=function(e){return this.SetActive(!1),window.SA&&SA.RecordState(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),!0},c.prototype.HandleMouseMove=function(e){if(e.which==1){var t=this.Layer.GetCamera(),n=t.ConvertPointViewerToWorld(e.offsetX,e.offsetY),r,f;if(this.State==i)r=n[0]-this.DragLast[0],f=n[1]-this.DragLast[1],this.DragLast=n,this.Grid.Origin[0]+=r,this.Grid.Origin[1]+=f;else{r=n[0]-this.Grid.Origin[0],f=n[1]-this.Grid.Origin[1];var l=Math.cos(3.14156*this.Grid.Orientation/180),c=Math.sin(3.14156*this.Grid.Orientation/180),h=l*r-c*f,p=l*f+c*r;h=.5*this.Grid.Dimensions[0]+h/this.Grid.BinWidth,p=.5*this.Grid.Dimensions[1]+p/this.Grid.BinHeight;var d=Math.round(h),v=Math.round(p);r=f=0;var m=!1;this.State==o?(r=d-this.Grid.Dimensions[0],r&&(this.Grid.Dimensions[0]=d,r=.5*r*this.Grid.BinWidth,m=!0)):this.State==s?d&&(this.Grid.Dimensions[0]-=d,r=.5*d*this.Grid.BinWidth,m=!0):this.State==a?(f=v-this.Grid.Dimensions[1],f&&(this.Grid.Dimensions[1]=v,f=.5*f*this.Grid.BinHeight,m=!0)):this.State==u&&v&&(this.Grid.Dimensions[1]-=v,f=.5*v*this.Grid.BinHeight,m=!0),m&&(h=l*r+c*f,p=l*f-c*r,this.Grid.Origin[0]+=h,this.Grid.Origin[1]+=p,this.Grid.UpdateBuffers(this.Layer.AnnotationView))}this.Layer.EventuallyDraw();return}return e.which==0&&this.SetActive(this.CheckActive(e)),!0},c.prototype.HandleMouseWheel=function(e){},c.prototype.HandleTouchPan=function(e){return!0},c.prototype.HandleTouchPinch=function(e){return!0},c.prototype.HandleTouchEnd=function(e){this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),this.SetActive(!1)},c.prototype.CheckActive=function(e){var t,n;this.Grid.FixedSize?(t=e.offsetX,n=e.offsetY,pixelSize=1):(t=e.worldX,n=e.worldY),t-=this.Grid.Origin[0],n-=this.Grid.Origin[1];var r=Math.cos(3.14156*this.Grid.Orientation/180),i=Math.sin(3.14156*this.Grid.Orientation/180),s=r*t-i*n,o=r*n+i*t;t=.5*this.Grid.Dimensions[0]+s/this.Grid.BinWidth,n=.5*this.Grid.Dimensions[1]+o/this.Grid.BinHeight;var u=Math.round(t),a=Math.round(n);if(u<0||u>this.Grid.Dimensions[0]||a<0||a>this.Grid.Dimensions[1])return this.SetActive(!1),!1;t=(t-u)*this.Grid.BinWidth,n=(n-a)*this.Grid.BinHeight;var f=5/this.Layer.GetPixelsPerUnit();return Math.abs(t)<f||Math.abs(n)<f?(this.ActiveIndex=[u,a],!0):!1},c.prototype.GetActive=function(){return this.State==t?!1:!0},c.prototype.Deactivate=function(){this.Layer.AnnotationView.CanvasDiv.css({cursor:"default"}),this.Popup.StartHideTimer(),this.State=t,this.Grid.Active=!1,this.Layer.DeactivateWidget(this),this.DeactivateCallback&&this.DeactivateCallback(),this.Layer.EventuallyDraw()},c.prototype.SetActive=function(e){if(e){this.State=n,this.Grid.Active=!0;if(!this.ActiveIndex){console.log("No active index");return}this.ActiveIndex[0]==0?(this.State=s,this.Layer.AnnotationView.CanvasDiv.css({cursor:"col-resize"})):this.ActiveIndex[0]==this.Grid.Dimensions[0]?(this.State=o,this.Layer.AnnotationView.CanvasDiv.css({cursor:"col-resize"})):this.ActiveIndex[1]==0?(this.State=u,this.Layer.AnnotationView.CanvasDiv.css({cursor:"row-resize"})):this.ActiveIndex[1]==this.Grid.Dimensions[1]?(this.State=a,this.Layer.AnnotationView.CanvasDiv.css({cursor:"row-resize"})):(this.State=i,this.Layer.AnnotationView.CanvasDiv.css({cursor:"move"})),this.PlacePopup()}else this.Deactivate();this.Layer.EventuallyDraw()},c.prototype.PlacePopup=function(){var e=this.ComputeCorner(1,-1,0,0),t=this.Layer.GetCamera();e=t.ConvertPointWorldToViewer(e[0],e[1]),this.Popup.Show(e[0]+10,e[1]-30)};var h;c.prototype.ShowPropertiesDialog=function(){this.Dialog.ColorInput.val(SAM.ConvertColorToHex(this.Grid.OutlineColor)),this.Dialog.LineWidthInput.val(this.Grid.LineWidth.toFixed(2)),this.Dialog.BinWidthInput.val(SAM.DistanceToString(this.Grid.BinWidth*2.5e-7)),this.Dialog.BinHeightInput.val(SAM.DistanceToString(this.Grid.BinHeight*2.5e-7)),this.Dialog.RotationInput.val(this.Grid.Orientation),this.Dialog.Show(!0)},c.prototype.DialogApplyCallback=function(){var e=this.Dialog.ColorInput.val();this.Grid.SetOutlineColor(e),this.Grid.LineWidth=parseFloat(this.Dialog.LineWidthInput.val()),this.Grid.BinWidth=SAM.StringToDistance(this.Dialog.BinWidthInput.val())*4e6,this.Grid.BinHeight=SAM.StringToDistance(this.Dialog.BinHeightInput.val())*4e6,this.Grid.Orientation=parseFloat(this.Dialog.RotationInput.val()),this.Grid.UpdateBuffers(this.Layer.AnnotationView),this.SetActive(!1),this.Text.String=SAM.DistanceToString(this.Grid.BinWidth*2.5e-7),this.Text.UpdateBuffers(this.Layer.AnnotationView),window.SA&&SA.RecordState(),this.Layer.EventuallyDraw(),this.UserNoteFlag&&SA.notesWidget&&SA.notesWidget.EventuallySaveUserNote(),SAM.NotesWidget&&!this.UserNoteFlag&&SAM.NotesWidget.MarkAsModified(),localStorage.GridWidgetDefaults=JSON.stringify({Color:e,LineWidth:this.Grid.LineWidth})},SAM.GridWidget=c})();(function(){"use strict";function u(){SAM.Shape.call(this),this.BinLength=100,this.TickSize=6,this.NumberOfBins=1,this.Orientation=0,this.Origin=[1e4,1e4],this.OutlineColor=[0,0,0],this.PointBuffer=[],this.PositionCoordinateSystem=SAM.Shape.VIEWER}function a(e){var n=this;if(e===null)return;this.Layer=e,this.PixelsPerMeter=0,this.Shape=new u,this.Shape.OutlineColor=[0,0,0],this.Shape.Origin=[30,20],this.Shape.BinLength=200,this.Shape.FixedSize=!0,this.Text=new SAM.Text,this.Text.PositionCoordinateSystem=SAM.Shape.VIEWER,this.Text.Position=[30,5],this.Text.String="",this.Text.Color=[0,0,0],this.Text.Anchor=[20,0],this.Update(e.GetPixelsPerUnit()),this.State=t}var e=0,t=3,n=4,r=5,i=6,s=7,o=8;u.prototype=new SAM.Shape,u.prototype.destructor=function(){},u.prototype.UpdateBuffers=function(e){this.PointBuffer=[],this.Matrix=mat4.create(),mat4.identity(this.Matrix);var t=0,n=this.TickSize;this.PointBuffer.push(t),this.PointBuffer.push(n),this.PointBuffer.push(0),n=0,this.PointBuffer.push(t),this.PointBuffer.push(n),this.PointBuffer.push(0);for(var r=0;r<this.NumberOfBins;++r)t+=this.BinLength,this.PointBuffer.push(t),this.PointBuffer.push(n),this.PointBuffer.push(0),n=this.TickSize,this.PointBuffer.push(t),this.PointBuffer.push(n),this.PointBuffer.push(0),n=0,this.PointBuffer.push(t),this.PointBuffer.push(n),this.PointBuffer.push(0)},a.prototype.Update=function(){if(!this.View)return;var e=Math.round(this.View.GetPixelsPerUnit()/this.View.GetMetersPerUnit());if(this.PixelsPerMeter==e)return;this.PixelsPerMeter=e;var t=200,n=0,r=this.PixelsPerMeter;while(r>t)r/=10,--n;this.Units="nm";var i=1e-9;n>=-6&&(this.Units="µm",i=1e-6),n>=-3&&(this.Units="mm",i=.001),n>=-2&&(this.Units="cm",i=.01),n>=0&&(this.Units="m",i=1),n>=3&&(this.Units="km",i=1e3),this.Shape.BinLength=r,this.Shape.NumberOfBins=Math.floor(t/r);var s=r*this.Shape.NumberOfBins,o=s/this.PixelsPerMeter,u=Math.round(o/i);this.Label=u.toString()+this.Units,this.LengthWorld=o*4e6,this.Text.String=this.Label,this.Text.UpdateBuffers(this.Layer.AnnotationView),this.Text.Position=[this.Shape.Origin[0]+s/2,this.Shape.Origin[1]-15],this.Shape.UpdateBuffers(this.Layer.AnnotationView)},a.prototype.Draw=function(e){if(!this.View||!this.View.HasUnits())return;this.Update(),this.Shape.Draw(e),this.Text.Draw(e)},a.prototype.HandleKeyPress=function(e,t){return!0},a.prototype.HandleDoubleClick=function(e){return!0},a.prototype.HandleMouseDown=function(e){return!1},a.prototype.HandleMouseUp=function(e){return!0},a.prototype.HandleMouseMove=function(e){return!0},a.prototype.HandleMouseWheel=function(e){},a.prototype.HandleTouchPan=function(e){return!0},a.prototype.HandleTouchPinch=function(e){return!0},a.prototype.HandleTouchEnd=function(e){this.SetActive(!1)},a.prototype.CheckActive=function(e){return this.SetActive(!1),!1},a.prototype.GetActive=function(){return this.State==t?!1:!0},a.prototype.Deactivate=function(){this.Layer.AnnotationView.CanvasDiv.css({cursor:"default"}),this.Popup.StartHideTimer(),this.State=t,this.Shape.Active=!1,this.Layer.DeactivateWidget(this),this.DeactivateCallback&&this.DeactivateCallback(),eventuallyRender()},a.prototype.SetActive=function(e){if(e==this.GetActive())return;e?(this.State=n,this.Shape.Active=!0,this.Layer.ActivateWidget(this),eventuallyRender(),this.PlacePopup()):this.Deactivate(),eventuallyRender()},SAM.ScaleWidget=a})();(function(){"use strict";function e(e,t){this.Viewer=t,this.Layer=t.GetAnnotationLayer();var n=layer.GetCamera(),r=n.GetFocalPoint(),i=n.Height/4;this.Bounds=[r[0]-i,r[0]+i,r[1]-i,r[1]+i],this.DragBounds=[r[0]-i,r[0]+i,r[1]-i,r[1]+i],layer.AddWidget(this),eventuallyRender(),this.Active=0;var s=this;this.Div=$("<div>").appendTo(e).addClass("sa-view-cutout-div"),$("<button>").appendTo(this.Div).text("Cancel").addClass("sa-view-cutout-button").click(function(){s.Cancel()}),$("<button>").appendTo(this.Div).text("Download").addClass("sa-view-cutout-button").click(function(){s.Accept()}),this.Select=$("<select>").appendTo(this.Div),$("<option>").appendTo(this.Select).attr("value",0).text("tif"),$("<option>").appendTo(this.Select).attr("value",1).text("jpeg"),$("<option>").appendTo(this.Select).attr("value",2).text("png"),$("<option>").appendTo(this.Select).attr("value",3).text("svs"),this.Label=$("<div>").addClass("sa-view-cutout-label").appendTo(this.Div),this.UpdateBounds(),this.HandleMouseUp()}e.prototype.Accept=function(){this.Deactivate();var e=["tif","jpeg","png","svs"],t=this.Viewer.GetCache().Image;window.location="/cutout/"+t.database+"/"+t._id+"/image."+e[this.Select.val()]+"?bounds="+JSON.stringify(this.Bounds)},e.prototype.Cancel=function(){this.Deactivate()},e.prototype.Serialize=function(){return!1},e.prototype.Draw=function(e){var t=[(this.DragBounds[0]+this.DragBounds[1])*.5,(this.DragBounds[2]+this.DragBounds[3])*.5],n=e.Camera,r=e.Viewport;if(e.gl)alert("webGL cutout not supported");else{var i=e.Context2d,n=e.Camera;i.save(),i.setTransform(1,0,0,1,0,0),this.DrawRectangle(i,this.Bounds,n,"#00A",1,0),this.DrawRectangle(i,this.DragBounds,n,"#000",2,this.Active),this.DrawCenter(i,t,n,"#000"),i.restore()}},e.prototype.DrawRectangle=function(e,t,n,r,i,s){var o=n.ConvertPointWorldToViewer(t[0],t[2]),u=n.ConvertPointWorldToViewer(t[1],t[2]),a=n.ConvertPointWorldToViewer(t[1],t[3]),f=n.ConvertPointWorldToViewer(t[0],t[3]);e.lineWidth=i,e.beginPath(),e.strokeStyle=s&4?"#FF0":r,e.moveTo(o[0],o[1]),e.lineTo(u[0],u[1]),e.stroke(),e.beginPath(),e.strokeStyle=s&2?"#FF0":r,e.moveTo(u[0],u[1]),e.lineTo(a[0],a[1]),e.stroke(),e.beginPath(),e.strokeStyle=s&8?"#FF0":r,e.moveTo(a[0],a[1]),e.lineTo(f[0],f[1]),e.stroke(),e.beginPath(),e.strokeStyle=s&1?"#FF0":r,e.moveTo(f[0],f[1]),e.lineTo(o[0],o[1]),e.stroke()},e.prototype.DrawCenter=function(e,t,n,r){var i=n.ConvertPointWorldToViewer(t[0],t[1]);e.strokeStyle=this.Active&16?"#FF0":r,e.lineWidth=1,e.beginPath(),e.moveTo(i[0]-5,i[1]),e.lineTo(i[0]+5,i[1]),e.moveTo(i[0],i[1]-5),e.lineTo(i[0],i[1]+5),e.stroke()},e.prototype.HandleKeyPress=function(e,t){return event.keyCode==67&&alert("Accept"),event.keyCode==67&&alert("Cancel"),!0},e.prototype.HandleDoubleClick=function(e){return!0},e.prototype.HandleMouseDown=function(e){return e.which!=1?!1:!0},e.prototype.HandleMouseUp=function(){if(this.Bounds[0]>this.Bounds[1]){var e=this.Bounds[0];this.Bounds[0]=this.Bounds[1],this.Bounds[1]=e}if(this.Bounds[2]>this.Bounds[3]){var e=this.Bounds[2];this.Bounds[2]=this.Bounds[3],this.Bounds[3]=e}this.DragBounds=this.Bounds.slice(0),eventuallyRender()},e.prototype.HandleMouseMove=function(e){var t=e.offsetX,n=e.offsetY;if(e.which==0){this.CheckActive(e);return}if(this.Active){var r=this.Layer.GetCamera(),i=r.ConvertPointViewerToWorld(e.offsetX,e.offsetY);this.Active&1&&(this.DragBounds[0]=i[0]),this.Active&2&&(this.DragBounds[1]=i[0]),this.Active&4&&(this.DragBounds[2]=i[1]),this.Active&8&&(this.DragBounds[3]=i[1]);if(this.Active&16){var s=i[0]-.5*(this.DragBounds[0]+this.DragBounds[1]),o=i[1]-.5*(this.DragBounds[2]+this.DragBounds[3]);this.DragBounds[0]+=s,this.DragBounds[1]+=s,this.DragBounds[2]+=o,this.DragBounds[3]+=o}return this.UpdateBounds(),eventuallyRender(),!0}return!1},e.prototype.UpdateBounds=function(e){var t=this.Viewer.GetCache(),n=t.Image.TileSize,r=[0,0,0,0];r[0]=Math.round(this.DragBounds[0]/n)*n,r[1]=Math.round(this.DragBounds[1]/n)*n,r[2]=Math.round(this.DragBounds[2]/n)*n,r[3]=Math.round(this.DragBounds[3]/n)*n;var i=t.Image.bounds;r[0]<i[0]&&(r[0]=i[0]),r[1]<i[0]&&(r[1]=i[0]),r[2]<i[2]&&(r[2]=i[2]),r[3]<i[2]&&(r[3]=i[2]),r[0]>i[1]&&(r[0]=i[1]),r[1]>i[1]&&(r[1]=i[1]),r[2]>i[3]&&(r[2]=i[3]),r[3]>i[3]&&(r[3]=i[3]),r[0]!=r[1]&&(this.Bounds[0]=r[0],this.Bounds[1]=r[1]),r[2]!=r[3]&&(this.Bounds[2]=r[2],this.Bounds[3]=r[3]);var s=[this.Bounds[1]-this.Bounds[0],this.Bounds[3]-this.Bounds[2]];this.Label.text(s[0]+" x "+s[1]+" = "+this.FormatPixels(s[0]*s[1])+"pixels")},e.prototype.FormatPixels=function(e){return e>1e9?Math.round(e/1e9)+"G":e>1e6?Math.round(e/1e6)+"M":e>1e3?Math.round(e/1e3)+"k":e},e.prototype.HandleTouchPan=function(e){},e.prototype.HandleTouchPinch=function(e){},e.prototype.HandleTouchEnd=function(e){},e.prototype.CheckActive=function(e){var t=this.Layer.GetCamera(),n=t.Height/200,r=t.ConvertPointViewerToWorld(e.offsetX,e.offsetY),i=0,s=this.DragBounds[0]-n<r[0]&&r[0]<this.DragBounds[1]+n,o=this.DragBounds[2]-n<r[1]&&r[1]<this.DragBounds[3]+n;o&&Math.abs(r[0]-this.DragBounds[0])<n&&(i|=1),o&&Math.abs(r[0]-this.DragBounds[1])<n&&(i|=2),s&&Math.abs(r[1]-this.DragBounds[2])<n&&(i|=4),s&&Math.abs(r[1]-this.DragBounds[3])<n&&(i|=8);var u=[(this.DragBounds[0]+this.DragBounds[1])*.5,(this.DragBounds[2]+this.DragBounds[3])*.5];return n*=2,Math.abs(r[0]-u[0])<n&&Math.abs(r[1]-u[1])<n&&(i|=16),i!=this.Active&&(this.SetActive(i),eventuallyRender()),!1},e.prototype.GetActive=function(){return this.Active},e.prototype.Deactivate=function(){this.Div.remove();if(this.Layer==null)return;this.Layer.DeactivateWidget(this),this.Layer.RemoveWidget(this),eventuallyRender()},e.prototype.SetActive=function(e){if(this.Active==e)return;this.Active=e,e!=0?this.Layer.ActivateWidget(this):this.Layer.DeactivateWidget(this),eventuallyRender()},SAM.CutoutWidget=e})();(function(){"use strict";function e(){this.Visibility=!0,this.Origin=[0,0],this.Image=undefined,this.Height=5e3}e.prototype.destructor=function(){},e.prototype.Draw=function(e){if(!this.Visibility||!this.Image)return;var t=e.Context2d;t.save(),t.setTransform(1,0,0,1,0,0),t.transform(.5*e.Viewport[2],0,0,-0.5*e.Viewport[3],.5*e.Viewport[2],.5*e.Viewport[3]);var n=1/e.Camera.Matrix[15];t.transform(e.Camera.Matrix[0]*n,e.Camera.Matrix[1]*n,e.Camera.Matrix[4]*n,e.Camera.Matrix[5]*n,e.Camera.Matrix[12]*n,e.Camera.Matrix[13]*n);var r=this.Height/this.Image.height;t.transform(r,0,0,r,this.Origin[0],this.Origin[1]),t.drawImage(this.Image,0,0),t.restore()},SAM.ImageAnnotation=e})();(function(){"use strict";function e(e){SAM.DialogOverlay||(SAM.DialogOverlay=$("<div>").appendTo("body").css({position:"fixed",left:"0px",width:"100%","background-color":"#AAA",opacity:"0.4","z-index":"1010"}).saFullHeight().hide()),this.Dialog=$("<div>").appendTo("body").css({"z-index":"1011"}).addClass("sa-view-dialog-div"),this.Row1=$("<div>").addClass("sa-view-dialog-title").appendTo(this.Dialog).css({width:"100%",height:"2.25em","box-sizing":"border-box"}),this.Title=$("<div>").appendTo(this.Row1).css({"float":"left"}).addClass("sa-view-dialog-title").text("Title"),this.CloseButton=$("<div>").appendTo(this.Row1).css({"float":"right"}).addClass("sa-view-dialog-close").text("Close"),this.Body=$("<div>").appendTo(this.Dialog).css({width:"100%","box-sizing":"border-box","margin-bottom":"30px"}),this.ApplyButtonDiv=$("<div>").appendTo(this.Dialog).addClass("sa-view-dialog-apply-div"),this.ApplyButton=$("<button>").appendTo(this.ApplyButtonDiv).addClass("sa-view-dialog-apply-button").text("Apply");var t=this;(function(){t.CloseButton.click(function(e){return SA.ContentEditableHasFocus=!1,t.Hide(),!0}),t.ApplyButton.click(function(n){return SA.ContentEditableHasFocus=!1,t.Hide(),e(),!0})})()}e.prototype.Show=function(e){SA.ContentEditableHasFocus=!0;var t=this;SAM.DialogOverlay.show(),this.Dialog.fadeIn(300),e?SAM.DialogOverlay.off("click.dialog"):SAM.DialogOverlay.on("click.dialog",function(e){t.Hide()}),SAM.ContentEditableHasFocus=!0},e.prototype.Hide=function(){SAM.DialogOverlay.off("click.dialog"),SAM.DialogOverlay.hide(),this.Dialog.fadeOut(300),SAM.ContentEditableHasFocus=!1},SAM.Dialog=e})();(function(){"use strict";function e(e,t){t&&(this.ImageItemId=t,this.LoadGirderImageItem(t)),this.Radius=7,this.AnnotationLayer=e;var n=0,r=70+n*6*this.Radius,i=this;this.Plus=$("<img>").appendTo(this.AnnotationLayer.GetCanvasDiv()).attr("src",SA.ImagePathUrl+"bluePlus.png").css({position:"absolute",left:3*this.Radius+"px",top:r+"px",width:2*this.Radius+"px",height:2*this.Radius+"px",opacity:"0.6"}).prop("title","Add Annotation").hover(function(){$(this).css({opacity:"1"})},function(){$(this).css({opacity:"0.6"})}).click(function(){i.NewAnnotationItem()}),this.AnnotationObjects=[],this.Highlighted=undefined,this.MenuAnnotationObject=undefined,this.Menu=$("<div>").appendTo(this.AnnotationLayer.GetCanvasDiv()).hide().mouseleave(function(){$(this).hide()}).css({position:"absolute","background-color":"#FFFFFF",border:"1px solid #666666","box-sizing":"border-box",left:"-78px",width:"100px",padding:"0px 2px"}),$("<button>").appendTo(this.Menu).text("Snap Shot").css({margin:"2px 0px",width:"100%"}).prop("title","Replace Annotation").click(function(){i.SnapShotAnnotation(i.MenuAnnotationObject),i.Menu.hide()}),$("<button>").appendTo(this.Menu).text("Delete").css({margin:"2px 0px",width:"100%"}).click(function(){i.DeleteAnnotation(i.MenuAnnotationObject),i.Menu.hide()}),$("<button>").appendTo(this.Menu).text("Properties").css({margin:"2px 0px",width:"100%"}).click(function(){i.Menu.hide()})}e.prototype.NewAnnotationItem=function(){var e={elements:[]};e.name=this.AnnotationObjects.length.toString(),e.elements=this.RecordAnnotation();var t=this;window.girder?girder.restRequest({path:"annotation?itemId="+this.ImageItemId,method:"POST",contentType:"application/json",data:JSON.stringify(e)}).done(function(e){t.Highlight(t.AddAnnotation(e))}):t.Highlight(t.AddAnnotation({_id:"ABC",annotation:e,itemId:t.ImageItemId}))},e.prototype.LoadGirderImageItem=function(e){var t={limit:50,offset:0,sort:"lowerName",sortdir:0},n=this;girder.restRequest({path:"annotation?itemId="+e,method:"GET",data:JSON.stringify(t)}).done(function(e){for(var t=0;t<e.length;++t)n.LoadAnnotationItem(e[t]._id)})},e.prototype.LoadAnnotationItem=function(e){var t=this;girder.restRequest({path:"annotation/"+e,method:"GET",contentType:"application/json"}).done(function(e){t.AddAnnotation(e)})},e.prototype.RecordAnnotation=function(){var e=[],t=this.AnnotationLayer.GetCamera(),n={type:"view",center:t.GetFocalPoint(),height:t.GetHeight(),width:t.GetWidth(),rotation:t.Roll};n.center[2]=0,e.push(n),n=undefined;for(var r=0;r<this.AnnotationLayer.GetNumberOfWidgets();++r){var i=this.AnnotationLayer.GetWidget(r).Serialize();i.type=="circle"&&(i.origin[2]=0,n={type:"circle",center:i.origin,radius:i.radius});if(i.type=="text"){var s=[i.position,i.offset];s[1][0]+=i.position[0],s[1][1]+=i.position[1],s[0][2]=0,s[1][2]=0,n={type:"arrow",lineWidth:10,fillColor:SAM.ConvertColorToHex(i.color),points:s},n.label={value:i.string,fontSize:i.size,color:SAM.ConvertColorToHex(i.color)}}i.type=="grid"&&(n={type:"rectanglegrid",center:i.origin,width:i.bin_width*i.dimensions[0],height:i.bin_height*i.dimensions[1],rotation:i.orientation,normal:[0,0,1],widthSubdivisions:i.dimensions[0],heightSubdivisions:i.dimensions[1]}),i.type=="rect"&&(n={type:"rectangle",label:{value:"test"},center:i.origin,height:i.height,width:i.width,rotation:i.orientation});if(i.type=="rect_set"){var o=i.widths.length;for(var u=0;u<o;++u)n={type:"rectangle",label:{value:i.labels[u]},center:[i.centers[2*u],i.centers[2*u+1],0],height:i.heights[u],width:i.widths[u],rotation:0,scalar:i.confidences[u]},e.push(n);n=undefined}if(i.type=="polyline"){for(var u=0;u<i.points.length;++u)i.points[u][2]=0;n={type:"polyline",closed:i.closedloop,points:i.points}}if(i.type=="lasso"){for(var u=0;u<i.points.length;++u)i.points[u][2]=0;n={type:"polyline",closed:!0,points:i.points}}if(i.type=="pencil")for(var r=0;r<i.shapes.length;++r){var s=i.shapes[r];for(var u=0;u<s.length;++u)s[u][2]=0;n={type:"polyline",closed:!1,points:s},i.outlinecolor&&(n.lineColor=SAM.ConvertColorToHex(i.outlinecolor)),i.linewidth&&(n.lineWidth=Math.round(i.linewidth)),e.push(n),n=undefined}else n&&(i.outlinecolor&&(n.lineColor=SAM.ConvertColorToHex(i.outlinecolor)),i.linewidth&&(n.lineWidth=Math.round(i.linewidth)),e.push(n),n=undefined)}return e},e.prototype.SnapShotAnnotation=function(e){this.Highlight(e),e.Data.annotation.elements=this.RecordAnnotation(),window.girder&&girder.restRequest({path:"annotation/"+e.Data._id,method:"PUT",data:JSON.stringify(e.Data.annotation),contentType:"application/json"})},e.prototype.DeleteAnnotation=function(e){var t=!1,n=[];for(var r=0;r<this.AnnotationObjects.length;++r){var i=this.AnnotationObjects[r];if(t){var s=70+(r-1)*6*this.Radius;i.Circle.animate({top:s+"px"}),n.push(i)}else e==i?(t=!0,i.Circle.remove(),window.girder&&girder.restRequest({path:"annotation/"+i.Data._id,method:"DELETE",contentType:"application/json"})):n.push(i)}var s=70+(r-1)*6*this.Radius;this.Plus.animate({top:s+"px"}),this.AnnotationObjects=n},e.prototype.AddAnnotation=function(e){var t=this.AnnotationObjects.length,n=70+t*6*this.Radius,r=this,i=$("<div>").appendTo(this.AnnotationLayer.GetCanvasDiv()).css({position:"absolute",left:3*this.Radius+"px",top:n+"px","min-width":2*this.Radius+"px","min-height":2*this.Radius+"px","background-color":"#55BBFF",opacity:"0.6",border:"1px solid #666666","border-radius":this.Radius+"px"}).prop("title","Show Annotation").text(e.annotation.name).hide().hover(function(){$(this).css({opacity:"1"})},function(){$(this).css({opacity:"0.6"})}),s={Data:e,Circle:i};return this.AnnotationObjects.push(s),i.contextmenu(function(){return!1}),i.mousedown(function(e){if(e.button==0)return r.DisplayAnnotation(s),!1;if(e.button==2){r.MenuAnnotationObject=s;var t=$(this).position();return r.Menu.css({left:5+t.left+2*r.Radius+"px",top:t.top+"px"}).show(),!1}return!0}),this.Plus.animate({top:n+6*this.Radius+"px"},400,function(){i.show()}),s},e.prototype.Highlight=function(e){this.Highlighted&&this.Highlighted.Circle.css({"background-color":"#55BBFF"}),this.Highlighted=e,e&&e.Circle.css({"background-color":"#FFDD00"})},e.prototype.DisplayAnnotation=function(e){this.AnnotationLayer.SetVisibility(!0),this.Highlight(e),this.AnnotationLayer.Reset();var t={};t.type="rect_set",t.centers=[],t.widths=[],t.heights=[],t.confidences=[],t.labels=[];var n=e.Data.annotation;for(var r=0;r<n.elements.length;++r){var i=n.elements[r],s={};if(i.type=="view"){var o=this.AnnotationLayer.GetCamera();o.SetFocalPoint(i.center),o.SetHeight(i.height),i.rotation?o.Roll=i.rotation:o.Roll=0,o.ComputeMatrix(),this.AnnotationLayer.Viewer&&this.AnnotationLayer.Viewer.EventuallyRender()}i.type=="circle"&&(s.type=i.type,s.outlinecolor=SAM.ConvertColor(i.lineColor),s.linewidth=i.lineWidth,s.origin=i.center,s.radius=i.radius,this.AnnotationLayer.LoadWidget(s)),i.type=="arrow"&&(s.type="text",s.string=i.label.value,s.color=SAM.ConvertColor(i.fillColor),s.size=i.label.fontSize,s.position=i.points[0].slice(0),s.offset=i.points[1].slice(0),s.offset[0]-=s.position[0],s.offset[1]-=s.position[1],this.AnnotationLayer.LoadWidget(s)),i.type=="rectanglegrid"&&(s.type="grid",s.outlinecolor=SAM.ConvertColor(i.lineColor),s.linewidth=i.lineWidth,s.origin=i.center,s.bin_width=i.width/i.widthSubdivisions,s.bin_height=i.height/i.heightSubdivisions,s.orientation=i.rotation,s.dimensions=[i.widthSubdivisions,i.heightSubdivisions],this.AnnotationLayer.LoadWidget(s)),i.type=="rectangle"&&(t.widths.push(i.width),t.heights.push(i.height),t.centers.push(i.center[0]),t.centers.push(i.center[1]),i.scalar===undefined&&(i.scalar=1),t.confidences.push(i.scalar),i.label?t.labels.push(i.label.value):t.labels.push("")),i.type=="polyline"&&(s.type=i.type,s.closedloop=i.closed,s.outlinecolor=SAM.ConvertColor(i.lineColor),s.linewidth=i.lineWidth,s.points=i.points,this.AnnotationLayer.LoadWidget(s))}t.widths.length>0&&this.AnnotationLayer.LoadWidget(t),this.AnnotationLayer.EventuallyDraw()},SAM.GirderWidget=e})();(function(){"use strict";function o(t,r,i,s){this.Layer=s,this.InteractionState=e,this.ActionState=n;var o={label:"detection",annotation_id:r};this.Classes=[].concat([o],i);var u=this.Classes.length;u>0&&(this.Classes[0].color="#FFFF00"),u>1&&(this.Classes[1].color="#FF0000"),u>2&&(this.Classes[u-1].color="#00FF00");for(var a=2;a<u-1;++a){var f=(a-2)/(u-4);this.Classes[a].color=SAM.ConvertColorToHex([f,1-f,1])}this.InitializeGui(t,"GroundTruth"),this.InitializeAnnotation(),this.Layer.AddWidget(this),this.IteratorIndex=-1,this.HighlightedRect={widget:undefined,idx:-1},this.ActiveClassIndex=0,this.SetActiveClassIndex(u-1)}function u(){}var e=0,t=1,n=0,r=1,i=2,s=3;o.prototype.SetActiveClassIndex=function(e){return e<0||e>=this.Classes.length?!1:(this.Classes[this.ActiveClassIndex].gui.css({"background-color":"#FFF"}),this.ActiveClassIndex=e,this.Classes[e].gui.css({"background-color":"#DEF"}),this.SetCursorColor(this.Layer.GetCanvasDiv(),this.Classes[e].color),!1)},o.prototype.GetActive=function(){return!0},o.prototype.Draw=function(e){for(var t=0;t<this.Classes.length;++t)this.Classes[t].widget&&this.Classes[t].widget.Draw(e)},o.prototype.HandleMouseMove=function(e){if(e.which!=0)return;var t=this.Layer.GetCamera(),n=t.ConvertPointViewerToWorld(e.offsetX,e.offsetY),r=this.RectSize/2,i;for(var s=0;s<this.Classes.length;++s)if(this.Classes[s].widget){var o=this.Classes[s].widget.Hash.Get(n,r);o.classObj=this.Classes[s];if(!i||o.dist<i.dist)i=o}return i&&this.SetHighlightedRect(i.classObj,i.idx),!0},o.prototype.SetIteratorIndex=function(e){this.SetHighlightedRect(this.Classes[0],e),this.IteratorIndex=e,this.UpdateActiveView()},o.prototype.SetHighlightedRect=function(e,n){var r=e.widget;if(this.HighlightedRect&&this.HighlightedRect.idx==n&&this.HighlightedRect.widget==r)return;this.HighlightedRect.idx>-1&&(this.HighlightedRect.widget.Shape.ActiveIndex=-1,this.HighlightedRect.idx=-1),this.InteractionState==t&&n==-1&&(r=this.Classes[0].widget,n=this.IteratorIndex),n>-1&&(r.Shape.ActiveIndex=n,this.HighlightedRect={widget:r,idx:n},this.Layer.LayerDiv.focus()),this.Layer.EventuallyDraw()},o.prototype.HandleKeyDown=function(e){if(this.InteractionState==t){if(this.ActionState!=n)return!1;e.keyCode>48&&e.keyCode<48+this.Classes.length&&(this.ActionState=r)}var i=this.SetActiveClassIndex(e.keyCode-48);return i||this.InteractionState==t?!1:!0},o.prototype.HandleKeyUp=function(e){var r=0,o=this;if(this.InteractionState==t){if(this.ActionState==s)return this.ActionState=n,!1;if(this.ActionState==i)return setTimeout(function(){o.ChangeCurrent(1)},300),this.ActionState=n,!1;this.ActionState=n;if(e.keyCode==27)return this.Stop(),!1}var u=this.HighlightedRect.idx;if(u>-1){var a=this.HighlightedRect.widget,f=a.Shape,l=e.keyCode-48;if(l>=0&&l<this.Classes.length){var c=this.Classes[l].label;return f.Labels[u]=c,this.Layer.EventuallyDraw(),this.InteractionState==t&&a==this.Classes[0].widget&&u==this.IteratorIndex&&setTimeout(function(){o.ChangeCurrent(1)},300),!1}if(e.keyCode==46){f.DeleteRectangle(u);var h=this.Layer.GetViewer().GetOverViewBounds();return a.Hash.Build(a.Shape.Centers,h),this.InteractionState==t&&a==this.Classes[0].widget&&(u<this.IteratorIndex?this.SetIteratorIndex(this.IteratorIndex-1):u==this.IteratorIndex&&(this.IteratorIndex-=1,this.HighlightedRect.idx-=1,setTimeout(function(){o.ChangeCurrent(1)},300))),this.Layer.EventuallyDraw(),!1}}if(this.InteractionState==t){var f=this.Classes[0].widget.Shape,p=this.IteratorIndex;if(e.keyCode==40){while(p<f.Widths.length){if(f.Labels[p]=="detection")return this.SetIteratorIndex(p),!1;p+=1}return this.Layer.DeactivateWidget(this),!1}if(e.keyCode==37)return this.ChangeCurrent(-1),!1;if(e.keyCode==39)return this.ChangeCurrent(1),!1}return!0},o.prototype.UpdateActiveView=function(){if(this.Classes[0].widget===undefined)return!0;var e=this.Classes[0].widget.Shape,t=this.IteratorIndex;if(t<0){this.ActiveLabel.text("detection");return}this.ActiveLabel.text(t.toString()+" of "+e.Labels.length.toString()+", "+e.Confidences[t].toPrecision(2)+", "+e.Labels[t]);var n=this.Layer.GetViewer(),r=n.GetCamera();n.RollTarget=this.Layer.GetCamera().Roll,n.TranslateTarget=e.GetCenter(this.IteratorIndex),n.AnimateLast=(new Date).getTime(),n.AnimateDuration=200,n.EventuallyRender(!0)},o.prototype.ChangeCurrent=function(e){if(this.Classes[0].widget===undefined)return!0;var t=this.Classes[0].widget.Shape,n=this.IteratorIndex;for(;;){n+=e;if(n<0||n>=t.Widths.length){this.Stop();return}if(t.Confidences[n]>=t.Threshold){this.SetIteratorIndex(n);return}}},o.prototype.HandleClick=function(e){if(e.which!=1)return!0;var n=this.Layer.GetCamera(),i=n.ConvertPointViewerToWorld(e.offsetX,e.offsetY),o=this.ActiveClassIndex,u=this.Classes[o].label,a=this.HighlightedRect.idx,f=this.HighlightedRect.widget,l;if(f){l=f.Shape;var c=l.GetCenter(a),h=Math.abs(i[0]-c[0]),p=Math.abs(i[1]-c[1]);if(a>-1&&a<l.GetLength()&&h<this.RectSize/2&&p<this.RectSize/2){l.Labels[a]=u,l.SetCenter(a,i),this.Layer.EventuallyDraw();if(this.InteractionState==t&&f==this.Classes[0].widget&&a==this.IteratorIndex){var d=this;this.ActionState==r&&(this.ActionState=s),setTimeout(function(){d.ChangeCurrent(1)},300)}return!1}}if(o>=0&&o<this.Classes.length){f=this.Classes[o].widget,l=f.Shape,l.AddRectangle(i,this.RectSize,this.RectSize),l.Labels[l.GetLength()-1]=u,f.Hash.Add(i,f.GetLength()-1),this.Layer.EventuallyDraw();if(this.ActionState==r)return this.ActionState=s,!1}return!1},o.prototype.CheckActive=function(e){return!0},o.prototype.InitializeGui=function(e,t){var n=this,r=$("<div>").appendTo(e).css({border:"1px solid #CCC",width:"100%"});this.ActiveLabel=$("<div>").appendTo(r).prop("title","Start sorting detections").attr("contenteditable","false").text("");var i=$("<p>").appendTo(r);this.SizeLabel=$("<label>").appendTo(i).text("Size:  "),this.SizeInput=$('<input type="number">').appendTo(i).prop("title","Change the size of the glyphs").on("change",function(){n.ChangeSize()});var s=$("<p>").appendTo(r);this.StartStopButton=$("<button>").appendTo(s).text("Start").css({"background-color":"#5F5"}).prop("title","Start sorting detections").css({width:"5em"}).on("click",function(){n.Start()});var o=$("<button>").appendTo(s).text("Save").prop("title","Save annotations to server").click(function(){n.Save()}),u=$("<div>").appendTo(r).css({border:"1px solid #CCC",width:"100%",height:"50px"});this.Slider=$('<input type="range" min="0" max="100">').appendTo(u).on("input",function(){n.SliderCallback()});var a=$("<div>").appendTo(u).html("0%").css({"float":"left"}),f=$("<div>").appendTo(u).html("100%").css({"float":"right"}),l=$("<p>").appendTo(r);for(var c=0;c<this.Classes.length;++c)this.MakeClassButton(l,c)},o.prototype.MakeClassButton=function(e,t){var n=this,r=this.Classes[t];r.gui=$("<div>").appendTo(e).text(t.toString()+": "+r.label).css({color:r.color}).click(function(){n.SetActiveClassIndex(t)})},o.prototype.InitializeAnnotation=function(){this.RequestAnnotationItem(this.Classes[0]);for(var e=0;e<this.Classes.length;++e)this.RequestAnnotationItem(this.Classes[e])},o.prototype.UpdateHash=function(){var e=this.Layer.GetViewer().GetOverViewBounds();this.Classes[0].widget.Hash.Build(this.Classes[0].widget.Shape.Centers,e);for(var t=0;t<this.Classes.length;++t){var n=this.Classes[t].widget;n.Hash.Build(n.Shape.Centers,e)}},o.prototype.RequestAnnotationItem=function(e){var t=this;window.girder?girder.restRequest({path:"annotation/"+e.annotation_id,method:"GET",contentType:"application/json"}).done(function(n){t.LoadAnnotation(n,e)}):alert("No girder")},o.prototype.LoadAnnotation=function(e,t){t.annotation=e.annotation;var n={};n.type="rect_set",n.centers=[],n.widths=[],n.heights=[],n.confidences=[],n.labels=[];var r=e.annotation;for(var i=0;i<r.elements.length;++i){var s=r.elements[i],o={};s.type=="rectangle"&&(n.widths.push(s.width),n.heights.push(s.height),n.centers.push(s.center[0]),n.centers.push(s.center[1]),s.scalar===undefined&&(s.scalar=1),n.confidences.push(s.scalar),n.labels.push(t.label))}var a=new SAM.RectSetWidget;a.Load(n),a.Hash=new u;var f=this.Layer.GetViewer().GetOverViewBounds();a.Hash.Build(a.Shape.Centers,f);var l=a.Shape;if(!l.LabelColors){l.LabelColors={};for(var i=0;i<this.Classes.length;++i)l.LabelColors[this.Classes[i].label]=this.Classes[i].color}t.widget=a,a.Shape.SetOutlineColor(t.color),this.Layer.EventuallyDraw()},o.prototype.CheckCallback=function(){},o.prototype.SliderCallback=function(){},o.prototype.Start=function(){var e=this;this.InteractionState=t,this.Layer.LayerDiv.focus();var n=this.Layer.GetViewer(),r=n.GetCamera();n.ZoomTarget=500,this.SetIteratorIndex(0),this.StartStopButton.text("Stop").css({"background-color":"#F55"}).prop("title","Stop sorting detections").on("click",function(){e.Stop()})},o.prototype.Stop=function(){var t=this;this.InteractiveState=e,this.SetIteratorIndex(-1),this.StartStopButton.text("Start").css({"background-color":"#5F5"}).prop("title","Start sorting detections").on("click",function(){t.Start()})},o.prototype.SetSize=function(e){this.SizeInput.val(e.toString()),this.RectSize=e,this.ChangeSize()},o.prototype.ChangeSize=function(){var e=parseInt(this.SizeInput.val());this.RectSize=e,this.Classes[0].widget&&this.Classes[0].widget.Shape.SetShape([e,e]);for(var t=0;t<this.Classes.length;++t){var n=this.Classes[t].widget;n&&n.Shape.SetShape([e,e])}this.Layer.EventuallyDraw()},o.prototype.SplitDetections=function(){var e=this.Classes[0].widget.Shape,t={};for(var n=0;n<this.Classes.length;++n)t[this.Classes[n].label]=this.Classes[n],this.Classes[n].newRectSet=new SAM.RectSet,this.Classes[n].newRectSet.LabelColors=this.Classes[n].widget.Shape.LabelColors;for(var n=0;n<this.Classes.length;++n){var r=this.Classes[n].widget.Shape;for(var i=0;i<r.GetLength();++i){var s=r.Labels[i],o=t[s].newRectSet;o.CopyRectangle(r,i,o.GetLength())}}for(var n=0;n<this.Classes.length;++n)this.Classes[n].widget.Shape=this.Classes[n].newRectSet,delete this.Classes[n].newRectSet;this.UpdateHash()},o.prototype.Save=function(){this.Classes[0].widget.Deactivate(),this.SplitDetections();if(window.girder){var e=this.Classes[0].annotation;e.elements=this.RectSetToGirderElements(this.Classes[0].widget),SA.PushProgress(),girder.restRequest({path:"annotation/"+this.Classes[0].annotation_id,method:"PUT",data:JSON.stringify(e),contentType:"application/json"}).done(function(){SA.PopProgress()});for(var t=0;t<this.Classes.length;++t){var n=this.Classes[t].widget,e=this.Classes[t].annotation;e.elements=this.RectSetToGirderElements(n),SA.PushProgress(),girder.restRequest({path:"annotation/"+this.Classes[t].annotation_id,method:"PUT",data:JSON.stringify(e),contentType:"application/json"}).done(function(){SA.PopProgress()})}this.Layer.EventuallyDraw()}},o.prototype.RectSetToGirderElements=function(e){var t=[],n=e.Serialize(),r=n.widths.length;for(var i=0;i<r;++i){var s={type:"rectangle",label:{value:n.labels[i]},center:[n.centers[2*i],n.centers[2*i+1],0],height:n.heights[i],width:n.widths[i],rotation:0,scalar:n.confidences[i]};t.push(s)}return t},o.prototype.SetActive=function(e){if(e==this.Active)return;this.Active=e,this.Layer.EventuallyDraw()},o.prototype.SetCursorColor=function(e,t){var n=document.createElement("canvas"),r=n.getContext("2d");n.width=16,n.height=24,r.moveTo(0,18),r.lineTo(0,0),r.lineTo(12,12),r.lineTo(7,13),r.lineTo(11,21),r.lineTo(8,22),r.lineTo(4,14),r.closePath(),r.fillStyle=t,r.fill(),r.strokeStyle="#000",r.stroke(),e[0].style.cursor="url("+n.toDataURL()+"), auto"},SAM.GroundTruth=o,u.prototype.Initialize=function(e,t){this.Origin=[e[0],e[2]],this.BinSize=Math.sqrt((e[1]-e[0])*(e[3]-e[2])/(t+1)),this.XDim=Math.ceil((e[1]-e[0])/this.BinSize),this.YDim=Math.ceil((e[3]-e[2])/this.BinSize),this.Grid=new Array(this.YDim);for(var n=0;n<this.YDim;++n){var r=new Array(this.XDim);for(var i=0;i<this.XDim;++i)r[i]=[];this.Grid[n]=r}},u.prototype.Add=function(e,t){var n=Math.floor((e[1]-this.Origin[1])/this.BinSize);n=Math.max(Math.min(n,this.YDim-1),0);var r=Math.floor((e[0]-this.Origin[0])/this.BinSize);r=Math.max(Math.min(r,this.XDim-1),0),this.Grid[n][r].push({pt:e,idx:t})},u.prototype.Build=function(e,t){this.Initialize(t,e.length/2);for(var n=0;n<e.length;n+=2)this.Add([e[n],e[n+1]],n>>1)},u.prototype.Get=function(e,t){var n=[e[0]-t,e[0]+t,e[1]-t,e[1]+t];n[0]=Math.max(Math.min(Math.floor((n[0]-this.Origin[0])/this.BinSize),this.XDim-1),0),n[1]=Math.max(Math.min(Math.floor((n[1]-this.Origin[0])/this.BinSize),this.XDim-1),0),n[2]=Math.max(Math.min(Math.floor((n[2]-this.Origin[1])/this.BinSize),this.YDim-1),0),n[3]=Math.max(Math.min(Math.floor((n[3]-this.Origin[1])/this.BinSize),this.YDim-1),0);var r=t,i;for(var s=n[2];s<=n[3];++s)for(var o=n[0];o<=n[1];++o){var u=this.Grid[s][o];for(var a=0;a<u.length;++a){var f=u[a],l=Math.max(Math.abs(f.pt[0]-e[0]),Math.abs(f.pt[1]-e[1]));l<=r&&(r=l,i=f)}}return i?{pt:i.pt,idx:i.idx,dist:r}:{idx:-1,dist:2*t}}})();(function(){"use strict";function e(e,t){this.Viewport=[0,0,100,100],this.ShapeList=[],this.Camera=new SAM.Camera,this.OutlineColor=[0,.5,0],this.OutlineMatrix=mat4.create(),this.OutlineCamMatrix=mat4.create(),this.CanvasDiv=e,e?this.CanvasDiv=e:this.CanvasDiv=$("<div>"),this.Canvas=$("<canvas>"),t||(this.Context2d=this.Canvas[0].getContext("2d")),this.Canvas.appendTo(this.CanvasDiv).css({position:"absolute",left:"0%",top:"0%",width:"100%",height:"100%"}),this.CanvasDiv.addClass("sa-view-canvas-div")}function t(){this.IncX=1,this.IncY=1}e.prototype.Delete=function(){this.CanvasDiv.off("mousedown.viewer"),this.CanvasDiv.off("mousemove.viewer"),this.CanvasDiv.off("wheel.viewer"),this.CanvasDiv.off("touchstart.viewer"),this.CanvasDiv.off("touchmove.viewer"),this.CanvasDiv.off("touchend.viewer"),this.CanvasDiv.off("keydown.viewer"),this.CanvasDiv.off("wheel.viewer"),delete this.ShapeList,delete this.Camera,delete this.CanvasDiv,delete this.Canvas},e.prototype.GetCamera=function(){return this.Camera},e.prototype.GetImageData=function(){var e=this.Camera,t=Math.floor(e.ViewportWidth),n=Math.floor(e.ViewportHeight),r=this.Context2d,i=r.getImageData(0,0,t,n);return i.Camera=new SAM.Camera,i.Camera.DeepCopy(this.Camera),i.__proto__=new SAM.ImageData,i.IncX=4,i.width=t,i.height=n,i.IncY=i.IncX*i.width,i},e.prototype.GetPixelsPerUnit=function(){var e=this.Camera.Matrix;return.5*this.Viewport[2]/(e[3]+e[15])},e.prototype.HasUnits=function(){var e=this.GetCache();return!e||!e.Image||!e.Image.units?!1:e.Image.units!="Units"},e.prototype.GetMetersPerUnit=function(){var e=this.GetCache(),t;return e?t={value:e.Image.spacing[0],units:e.Image.units}:t={value:250,units:"nm"},SAM.ConvertToMeters(t),t.value},e.prototype.appendTo=function(e){return this.CanvasDiv.appendTo(e)},e.prototype.remove=function(e){return this.CanvasDiv.remove(e)},e.prototype.css=function(e){return this.CanvasDiv.css(e)},e.prototype.GetViewport=function(){return this.Viewport},e.prototype.GetWidth=function(){return this.CanvasDiv.width()},e.prototype.GetHeight=function(){return this.CanvasDiv.height()},e.prototype.UpdateSize=function(){this.UpdateCanvasSize()},e.prototype.UpdateCanvasSize=function(){if(!this.CanvasDiv.is(":visible"))return!1;var e=this.CanvasDiv.position(),t=this.CanvasDiv.width(),n=this.CanvasDiv.height();return t<=0||n<=0?!1:(this.SetViewport([e.left,e.top,t,n]),!0)},e.prototype.SetViewport=function(e){var t=e[2],n=e[3];this.Canvas.attr("width",t.toString()),this.Canvas.attr("height",n.toString()),this.Viewport=e,this.Camera.SetViewport(e)},e.prototype.CaptureImage=function(){var e=this.Canvas[0].toDataURL(),t=document.createElement("img");return t.src=e,t},e.prototype.AddShape=function(e){this.ShapeList.push(e)},e.prototype.DrawShapes=function(){if(!this.CanvasDiv.is(":visible"))return;for(var e=0;e<this.ShapeList.length;e++)this.ShapeList[e].Draw(this)},e.prototype.Clear=function(){this.Context2d.setTransform(1,0,0,1,0,0),this.Context2d.clearRect(0,0,this.Viewport[2],this.Viewport[3])},e.prototype.DrawHistory=function(e){if(this.gl)alert("Drawing history does not work with webGl yet.");else{var t=this.Context2d;t.save(),t.setTransform(1,0,0,1,0,0),t.setTransform(1,0,0,-1,0,this.Viewport[3]),t.transform(.5*this.Viewport[2],0,0,.5*this.Viewport[3],.5*this.Viewport[2],.5*this.Viewport[3]);var n=this.Camera,r=n.ViewportWidth/n.ViewportHeight,i=1/n.Matrix[15];t.transform(n.Matrix[0]*i,n.Matrix[1]*i,n.Matrix[4]*i,n.Matrix[5]*i,n.Matrix[12]*i,n.Matrix[13]*i);var s=SA.recorderWidget.TimeLine;for(var o=0;o<s.length;++o){var n=s[o].ViewerRecords[0].Camera,u=n.Height,a=n.Width,f=Math.cos(n.Roll),l=Math.sin(n.Roll);t.save(),t.transform(f,-l,l,f,n.FocalPoint[0],n.FocalPoint[1]);var c=2*e/u;c>1&&(c=1),t.fillStyle="rgba(0,128,0,"+c+")",t.fillRect(-a/2,-u/2,a,u),t.stroke(),t.restore()}t.restore()}},e.prototype.DrawFocalPoint=function(){if(this.gl)alert("Drawing focal point does not work with webGl yet.");else{var e=this.Viewport[2]*.5,t=this.Viewport[3]*.5,n=this.Context2d;n.save(),n.setTransform(1,0,0,1,0,0),n.strokeStyle="rgba(255,255,200,100)",n.fillStyle="rgba(0,0,50,100)",n.beginPath(),n.fillRect(e-30,t-1,60,3),n.rect(e-30,t-1,60,3),n.fillRect(e-1,t-30,3,60),n.rect(e-1,t-30,3,60);var r=t/2;n.beginPath(),n.moveTo(e-r,t-r+30),n.lineTo(e-r,t-r),n.lineTo(e-r+30,t-r),n.moveTo(e+r,t-r+30),n.lineTo(e+r,t-r),n.lineTo(e+r-30,t-r),n.moveTo(e+r,t+r-30),n.lineTo(e+r,t+r),n.lineTo(e+r-30,t+r),n.moveTo(e-r,t+r-30),n.lineTo(e-r,t+r),n.lineTo(e-r+30,t+r),n.stroke(),++r,n.beginPath(),n.strokeStyle="rgba(0,0,50,100)",n.moveTo(e-r,t-r+30),n.lineTo(e-r,t-r),n.lineTo(e-r+30,t-r),n.moveTo(e+r,t-r+30),n.lineTo(e+r,t-r),n.lineTo(e+r-30,t-r),n.moveTo(e+r,t+r-30),n.lineTo(e+r,t+r),n.lineTo(e+r-30,t+r),n.moveTo(e-r,t+r-30),n.lineTo(e-r,t+r),n.lineTo(e-r+30,t+r),n.stroke(),n.restore()}},e.prototype.DrawCorrelations=function(e,t){if(this.gl)alert("Drawing correlations does not work with webGl yet.");else{var n=this.Context2d;n.save(),n.setTransform(1,0,0,1,0,0),n.strokeStyle="rgba(200,255,255,100)",n.fillStyle="rgba(255,0,0,100)";for(var r=0;r<e.length;++r){var i=e[r].GetPoint(t),s=this.Camera.Matrix,o=(i[0]*s[0]+i[1]*s[4]+s[12])/s[15],u=(i[0]*s[1]+i[1]*s[5]+s[13])/s[15];o=(1+o)*this.Viewport[2]*.5,u=(1-u)*this.Viewport[3]*.5,n.beginPath(),n.fillRect(o-20,u-1,40,3),n.rect(o-20,u-1,40,3),n.fillRect(o-1,u-20,3,40),n.rect(o-1,u-20,3,40),n.stroke()}n.restore()}},e.prototype.DrawCopyright=function(e){if(e==undefined||MASK_HACK)return;if(!this.gl){this.Context2d.setTransform(1,0,0,1,0,0),this.Context2d.font="18px Arial";var t=this.Viewport[2]*.5-50,n=this.Viewport[3]-10;this.Context2d.fillStyle="rgba(128,128,128,0.5)",this.Context2d.fillText(e,t,n)}},e.prototype.DrawOutline=function(e){if(this.gl){var t=polyProgram;this.gl.useProgram(t),this.gl.viewport(this.Viewport[0],this.Viewport[3]-this.Viewport[1],this.Viewport[2],this.Viewport[3]),mat4.identity(this.OutlineCamMatrix),this.OutlineCamMatrix[0]=2,this.OutlineCamMatrix[5]=2,this.OutlineCamMatrix[10]=0,this.OutlineCamMatrix[12]=-1,this.OutlineCamMatrix[13]=-1;var n=this.Camera.ZRange[0]+.001,r=this.Camera.ZRange[1]-.001;this.OutlineCamMatrix[14]=n,mat4.identity(this.OutlineMatrix),this.gl.uniformMatrix4fv(t.mvMatrixUniform,!1,this.OutlineMatrix),e&&(this.OutlineCamMatrix[14]=r,this.gl.uniformMatrix4fv(t.pMatrixUniform,!1,this.OutlineCamMatrix),this.gl.uniform3f(t.colorUniform,1,1,1),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,squarePositionBuffer),this.gl.vertexAttribPointer(t.vertexPositionAttribute,squarePositionBuffer.itemSize,this.gl.FLOAT,!1,0,0),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,squarePositionBuffer.numItems)),this.OutlineCamMatrix[14]=n,this.gl.uniformMatrix4fv(t.pMatrixUniform,!1,this.OutlineCamMatrix),this.gl.uniform3f(t.colorUniform,this.OutlineColor[0],this.OutlineColor[1],this.OutlineColor[2]),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,squareOutlinePositionBuffer),this.gl.vertexAttribPointer(t.vertexPositionAttribute,squareOutlinePositionBuffer.itemSize,this.gl.FLOAT,!1,0,0),this.gl.drawArrays(this.gl.LINE_STRIP,0,squareOutlinePositionBuffer.numItems)}},t.prototype.GetIntensity=function(e,t){if(!this.data)return 0;e=Math.round(e),t=Math.round(t);var n=e*this.IncX+t*this.IncY;return(this.data[n]+this.data[n+1]+this.data[n+2])/3},t.prototype.InBounds=function(e,t){return this.data?e>=0&&e<this.width&&t>=0&&t<this.height:!1},t.prototype.MarkEdge=function(e,t,n,r){if(!this.EdgeMarks){var i=Math.round(this.width*this.height);this.EdgeMarks=new Array(i);for(var s=0;s<i;++s)this.EdgeMarks[s]=0}var o=0;e!=n?o=e<n?1:4:t!=r&&(o=t<r?2:8);var u=e+t*this.width,a=this.EdgeMarks[u];return a&o?!0:(this.EdgeMarks[u]=a|o,!1)},SAM.ImageData=t,SAM.View=e})();