-
Notifications
You must be signed in to change notification settings - Fork 1
/
tspline_gl_5.min.js
1 lines (1 loc) · 14.1 KB
/
tspline_gl_5.min.js
1
var w_arr,points,pointsize_arr,mouseymin,mousexmax,on_point,degree,s_tmesh,t_tmesh,knot_vecs,sfContainer,knotTContainer,knotTst,knotSContainer,knotSst,t_step,s_step,t_min,t_max,s_min,s_max,t_points_dy,s_points_dy,t_vec,s_vec,abs_s,abs_t,tcrawl,scp_arr,cam,zunit,FELB,knotFelb,puff_arr,sugar,szog1,szog2,drawcn,drawcp,drawkp,drawdm,notPopup,entered_pop,dmx,dmy,shift_x,shift_y,shift_z,point_changed,no_tmesh,knot_net_s_display_array,knot_net_t_display_array,surface_display_points,numpoints=34,coffh=.9*(document.getElementById("cvas").offsetHeight-4);function nurbs_weight(t,e){for(var s=[0,0,0,0],n=degree;n>=0;--n)if(t>=e[n]){if(e[n]==e[n+1])return 0;s[n]=1;break}var _=1;for(n=degree;n>=1;--n){for(var o=0;o<n;++o)0!=s[o]&&(s[o]=(t-e[o])/(e[o+_]-e[o])*s[o]),0!=s[o+1]&&(s[o]+=(e[o+_+1]-t)/(e[o+_+1]-e[o+1])*s[o+1]);++_}return s[0]}function init_tmesh(){t_tmesh=[[0,1,2,3,4,5,6,7,8,9,10],[0,1,2,3,4,5,6,7,8,9,10],[0,1,2,3,4,5,6,8,9,10],[0,1,2,3,9,10],[0,1,3,4,9,10],[0,1,5,6,8,9,10],[0,1,2,3,4,5,9,10],[0,1,2,3,5,7,8,9,10],[0,1,3,5,9,10],[0,1,2,3,5,7,8,9,10],[0,1,2,3,5,7,8,9,10],[0,1,2,3,4,5,6,7,8,9,10],[0,1,2,3,4,5,6,7,8,9,10]],s_tmesh=[[0,1,2,3,4,5,6,7,8,9,10,11,12],[0,1,2,3,4,5,6,7,8,9,10,11,12],[0,1,2,3,6,7,9,10,11,12],[0,1,2,3,4,6,7,8,9,10,11,12],[0,1,2,4,6,11,12],[0,1,2,5,6,7,8,9,10,11,12],[0,1,2,5,11,12],[0,1,7,9,10,11,12],[0,1,2,5,7,9,10,11,12],[0,1,2,3,4,5,6,7,8,9,10,11,12],[0,1,2,3,4,5,6,7,8,9,10,11,12]],abs_s=[1,1,2,3,3.5,4,5,6,7,8,9,10,10],abs_t=[1,1,2,4,5,6,7,8,9,10,10],s_points_dy=[[!0,!0,!0,!0,!0,!0],[!0,!0,!0,!0,!0,!0,!0,!0],[!0,!0,!1],[!0,!0,!0,!0,!0,!0,!0],[!0,!1],[!0,!0,!0],[!0,!0,!0,!0,!0]],t_points_dy=[[!0,!0,!0,!0,!0,!0],[!0,!1],[!0,!1],[!0,!0,!0],[!0,!0,!0,!1],[!0,!0,!0,!0,!0],[!0,!1],[!0,!0,!0,!0,!0],[!0,!0,!0,!0,!0]],knot_vecs=new Array(abs_t.length-4);for(let t=2;t<s_tmesh.length-2;++t){let e=t-2;knot_vecs[e]=new Array(10*(s_tmesh[t].length-4));for(let s=2;s<s_tmesh[t].length-2;++s){let n=s-2;knot_vecs[e][10*n+2]=abs_s[s_tmesh[t][s]];let _=0;for(let o=s_tmesh[t][s]-1;o>=0;--o){for(let s=2;s<t_tmesh[o].length-2;++s){if(abs_t[t_tmesh[o][s]]==abs_t[t]){++_,knot_vecs[e][10*n+2-_]=abs_s[o];break}if(abs_t[t_tmesh[o][s]]>abs_t[t]){s>2&&t_points_dy[o-2][s-3]&&(++_,knot_vecs[e][10*n+2-_]=abs_s[o]);break}}if(2==_)break}_=0;for(let o=s_tmesh[t][s]+1;o<abs_s.length;++o){for(let s=2;s<t_tmesh[o].length-2;++s){if(abs_t[t_tmesh[o][s]]==abs_t[t]){++_,knot_vecs[e][10*n+2+_]=abs_s[o];break}if(abs_t[t_tmesh[o][s]]>abs_t[t]){s>2&&t_points_dy[o-2][s-3]&&(++_,knot_vecs[e][10*n+2+_]=abs_s[o]);break}}if(2==_)break}knot_vecs[e][10*n+7]=abs_t[t],_=0;for(let o=t-1;o>=0;--o){for(let i=2;i<s_tmesh[o].length-2;++i){if(abs_s[s_tmesh[o][i]]==abs_s[s_tmesh[t][s]]){++_,knot_vecs[e][10*n+7-_]=abs_t[o];break}if(abs_s[s_tmesh[o][i]]>abs_s[s_tmesh[t][s]]){i>2&&s_points_dy[o-2][i-3]&&(++_,knot_vecs[e][10*n+7-_]=abs_t[o]);break}}if(2==_)break}_=0;for(let o=t+1;o<abs_t.length;++o){for(let i=2;i<s_tmesh[o].length-2;++i){if(abs_s[s_tmesh[o][i]]==abs_s[s_tmesh[t][s]]){++_,knot_vecs[e][10*n+7+_]=abs_t[o];break}if(abs_s[s_tmesh[o][i]]>abs_s[s_tmesh[t][s]]){i>2&&s_points_dy[o-2][i-3]&&(++_,knot_vecs[e][10*n+7+_]=abs_t[o]);break}}if(2==_)break}}}let t=new Array(s_points_dy.length);t[0]=0;for(let e=0;e<s_points_dy.length-1;++e)t[e+1]=s_points_dy[e].length+t[e];tcrawl=new Array(t_points_dy.length);for(let e=0;e<t_points_dy.length;++e){tcrawl[e]=new Array(t_points_dy[e].length);for(let s=0;s<t_points_dy[e].length;++s)tcrawl[e][s]=t[t_tmesh[e+2][s+2]-2]+s_tmesh[t_tmesh[e+2][s+2]].indexOf(e+2)-2}}function setup(){setAttributes("antialias",!0),degree=3,numpoints=34,w_arr=new Array(numpoints).fill(1),pointsize_arr=new Array(numpoints).fill(1),s_min=abs_s[2],s_max=abs_s[abs_s.length-3],t_min=abs_t[2],t_max=abs_t[abs_t.length-3],knotFelb=15,s_step=(s_max-s_min)/(FELB=33),t_step=(t_max-t_min)/FELB,t_vec=new Array(FELB+1),s_vec=new Array(FELB+1);for(let t=0;t<=FELB;++t)t_vec[t]=t_min+t*t_step,s_vec[t]=s_min+t*s_step;puff_arr=new Array(3*(FELB+1));var t=document.getElementById("cvas");createCanvas(t.offsetWidth,t.offsetHeight-4,WEBGL).parent("cvas"),cam=createCamera(),addScreenPositionFunction(),mousexmax=windowWidth-document.getElementById("two").offsetWidth,mouseymin=document.getElementById("leiras").offsetHeight,zunit=.875*width/6,points=[.15*width,.15*height,3*zunit,.25*width,.15*height,3*zunit,.45*width,.15*height,3*zunit,.55*width,.15*height,3*zunit,.75*width,.15*height,3*zunit,.85*width,.15*height,3*zunit,.15*width,.5*height,2*zunit,.25*width,.5*height,2*zunit,.3*width,.5*height,2*zunit,.45*width,.5*height,2*zunit,.55*width,.5*height,2*zunit,.65*width,.5*height,2*zunit,.75*width,.5*height,2*zunit,.85*width,.5*height,2*zunit,.15*width,.6*height,1*zunit,.3*width,.6*height,1*zunit,.45*width,.6*height,1*zunit,.15*width,.1*height,0,.35*width,.1*height,0,.45*width,.1*height,0,.55*width,.1*height,0,.65*width,.1*height,0,.75*width,.1*height,0,.85*width,.1*height,0,.15*width,.6*height,-1*zunit,.35*width,.6*height,-1*zunit,.55*width,.8*height,-2*zunit,.75*width,.8*height,-2*zunit,.85*width,.8*height,-2*zunit,.15*width,.15*height,-3*zunit,.35*width,.15*height,-3*zunit,.55*width,.15*height,-3*zunit,.75*width,.15*height,-3*zunit,.85*width,.15*height,-3*zunit],sugar=height/2*2/tan(60*PI/360),szog1=PI/6,szog2=PI/4,dmx=0,dmy=0,shift_x=0,shift_y=0,shift_z=0,drawcn=!0,drawcp=!0,drawkp=!0,notPopup=!0,entered_pop=!1,on_point=-1,no_tmesh=!0,point_changed=!0,sfContainer=new Array(numpoints*(FELB+1)*(FELB+1));let e=FELB+1;for(let t=0;t<=FELB;++t)for(let s=0;s<=FELB;++s){let n=0;for(let _=0;_<knot_vecs.length;++_)for(let o=0;o<knot_vecs[_].length;o+=10)s_vec[s]>=knot_vecs[_][o]&&s_vec[s]<knot_vecs[_][o+4]&&t_vec[t]>=knot_vecs[_][o+5]&&t_vec[t]<knot_vecs[_][o+9]?sfContainer[t*e*numpoints+s*numpoints+n]=nurbs_weight(s_vec[s],knot_vecs[_].slice(o,o+5))*nurbs_weight(t_vec[t],knot_vecs[_].slice(o+5,o+10)):sfContainer[t*e*numpoints+s*numpoints+n]=0,++n}surface_display_points=new Array(FELB+1);for(let t=0;t<=FELB;++t)surface_display_points[t]=new Array(3*(FELB+1));knot_net_s_display_array=new Array(s_points_dy.length);for(let t=0;t<s_points_dy.length;++t){knot_net_s_display_array[t]=new Array(s_points_dy[t].length-1);for(let s=0;s<s_points_dy[t].length-1;++s)s_points_dy[t][s]&&(knot_net_s_display_array[t][s]=new Array(3*e))}knot_net_t_display_array=new Array(t_points_dy.length);for(let t=0;t<t_points_dy.length;++t){knot_net_t_display_array[t]=new Array(t_points_dy[t].length-1);for(let s=0;s<t_points_dy[t].length-1;++s)t_points_dy[t][s]&&(knot_net_t_display_array[t][s]=new Array(3*e))}knotSContainer=new Array(s_points_dy.length),knotSst=new Array(s_points_dy.length),e=knotFelb+1;for(let t=0;t<s_points_dy.length;++t){knotSContainer[t]=new Array(s_points_dy[t].length-1),knotSst[t]=new Array(s_points_dy[t].length-1);for(let s=0;s<s_points_dy[t].length-1;++s)if(s_points_dy[t][s]){let n=abs_t[t+2];knotSst[t][s]=new Array(2*e);for(let _=1;_<2*e;_+=2)knotSst[t][s][_]=n;if(n>=t_min&&n<=t_max){knotSContainer[t][s]=new Array(e);let _=(abs_s[s_tmesh[t+2][s+3]]-abs_s[s_tmesh[t+2][s+2]])/knotFelb;for(let o=0;o<e;++o){knotSContainer[t][s][o]=new Array(numpoints);let e=abs_s[s_tmesh[t+2][s+2]]+o*_;if(knotSst[t][s][2*o]=e,e>=s_min&&e<=s_max){let _=0;for(let i=0;i<knot_vecs.length;++i)for(let r=0;r<knot_vecs[i].length;r+=10)e>=knot_vecs[i][r]&&e<knot_vecs[i][r+4]&&n>=knot_vecs[i][r+5]&&n<knot_vecs[i][r+9]?knotSContainer[t][s][o][_]=nurbs_weight(e,knot_vecs[i].slice(r,r+5))*nurbs_weight(n,knot_vecs[i].slice(r+5,r+10)):knotSContainer[t][s][o][_]=0,++_}}}}}knotTContainer=new Array(t_points_dy.length),knotTst=new Array(t_points_dy.length);for(let t=0;t<t_points_dy.length;++t){knotTContainer[t]=new Array(t_points_dy[t].length-1),knotTst[t]=new Array(t_points_dy[t].length-1);for(let s=0;s<t_points_dy[t].length-1;++s)if(t_points_dy[t][s]){let n=abs_s[t+2];knotTst[t][s]=new Array(2*e);for(let _=1;_<2*e;_+=2)knotTst[t][s][_]=n;if(n>=s_min&&n<=s_max){knotTContainer[t][s]=new Array(e);let _=(abs_t[t_tmesh[t+2][s+3]]-abs_t[t_tmesh[t+2][s+2]])/knotFelb;for(let o=0;o<e;++o){knotTContainer[t][s][o]=new Array(numpoints);let e=abs_t[t_tmesh[t+2][s+2]]+o*_;if(knotTst[t][s][2*o]=e,e>=t_min&&e<=t_max){let _=0;for(let i=0;i<knot_vecs.length;++i)for(let r=0;r<knot_vecs[i].length;r+=10)n>=knot_vecs[i][r]&&n<knot_vecs[i][r+4]&&e>=knot_vecs[i][r+5]&&e<knot_vecs[i][r+9]?knotTContainer[t][s][o][_]=nurbs_weight(n,knot_vecs[i].slice(r,r+5))*nurbs_weight(e,knot_vecs[i].slice(r+5,r+10)):knotTContainer[t][s][o][_]=0,++_}}}}}noLoop()}function draw(){if(background(111),cam.setPosition(-sugar*sin(szog2)*cos(szog1),-sugar*sin(szog1),sugar*cos(szog1)*cos(szog2)),cam.lookAt(0,0,0),cam.move(shift_x,shift_y,shift_z),translate(-width/2,-height/2),drawcn){stroke(180,180,180),strokeWeight(1),noFill();let t=0;for(let e=0;e<s_points_dy.length;++e){for(let s=0;s<s_points_dy[e].length-1;++s)s_points_dy[e][s]&&line(points[t],points[t+1],points[t+2],points[t+3],points[t+4],points[t+5]),t+=3;t+=3}for(let t=0;t<t_points_dy.length;++t)for(let e=0;e<t_points_dy[t].length-1;++e)if(t_points_dy[t][e]){let s=3*tcrawl[t][e],n=3*tcrawl[t][e+1];line(points[s],points[s+1],points[s+2],points[n],points[n+1],points[n+2])}}stroke(0),fill(0,255,0),drawdm?strokeWeight(1):noStroke(),ambientLight(130,130,130),pointLight(255,200,255,cam.eyeX,cam.eyeY+100,1e3),ambientMaterial(50,220,50);point_changed&&(calc_surface_points(),knot_net_display_points(),point_changed=!1);let t=FELB+1;for(let t=1;t<=FELB;++t){beginShape(TRIANGLE_STRIP);for(let e=0;e<=FELB;++e)vertex(surface_display_points[t-1][3*e],surface_display_points[t-1][3*e+1],surface_display_points[t-1][3*e+2]),9999999!=surface_display_points[t][3*e]&&vertex(surface_display_points[t][3*e],surface_display_points[t][3*e+1],surface_display_points[t][3*e+2]);endShape()}if(drawkp){stroke(0),strokeWeight(3),noFill(),t=knotFelb+1;for(let e=0;e<s_points_dy.length;++e)for(let s=0;s<s_points_dy[e].length-1;++s)if(s_points_dy[e][s]){beginShape();for(let n=0;n<t;++n)knotSst[e][s][2*n]>=s_min&&knotSst[e][s][2*n]<=s_max&&knotSst[e][s][2*n+1]>=t_min&&knotSst[e][s][2*n+1]<=t_max&&9999999!=knot_net_s_display_array[e][s][3*n]&&vertex(knot_net_s_display_array[e][s][3*n],knot_net_s_display_array[e][s][3*n+1],knot_net_s_display_array[e][s][3*n+2]);endShape()}for(let e=0;e<t_points_dy.length;++e)for(let s=0;s<t_points_dy[e].length-1;++s)if(t_points_dy[e][s]){beginShape();for(let n=0;n<t;++n)knotTst[e][s][2*n]>=t_min&&knotTst[e][s][2*n]<=t_max&&knotTst[e][s][2*n+1]>=s_min&&knotTst[e][s][2*n+1]<=s_max&&9999999!=knot_net_t_display_array[e][s][3*n]&&vertex(knot_net_t_display_array[e][s][3*n],knot_net_t_display_array[e][s][3*n+1],knot_net_t_display_array[e][s][3*n+2]);endShape()}}if(drawcp){stroke(50,255,50),scp_arr=[];for(var e=0;e<points.length;e+=3)if(0!=pointsize_arr[e/3]){strokeWeight(20*pointsize_arr[e/3]),point(points[e],points[e+1],points[e+2]);let t=screenPosition(points[e],points[e+1],points[e+2]);scp_arr.push(t.x+width/2,t.y+height/2)}}}function calc_surface_points(){let t=FELB+1;for(let e=0;e<=FELB;++e)for(let s=0;s<=FELB;++s){let n=0,_=0,o=0,i=0;for(let r=0;r<numpoints;++r){let a=sfContainer[e*t*numpoints+s*numpoints+r]*w_arr[r];i+=a,n+=a*points[3*r],_+=a*points[3*r+1],o+=a*points[3*r+2]}if(i>0){let t=1/i;surface_display_points[e][3*s]=n*t,surface_display_points[e][3*s+1]=_*t,surface_display_points[e][3*s+2]=o*t}else surface_display_points[e][3*s]=9999999,surface_display_points[e][3*s+1]=0,surface_display_points[e][3*s+2]=0}}function knot_net_display_points(){felbp1=knotFelb+1;for(let t=0;t<s_points_dy.length;++t)for(let e=0;e<s_points_dy[t].length-1;++e)if(s_points_dy[t][e])for(let s=0;s<felbp1;++s)if(knotSst[t][e][2*s]>=s_min&&knotSst[t][e][2*s]<=s_max&&knotSst[t][e][2*s+1]>=t_min&&knotSst[t][e][2*s+1]<=t_max){let n=0,_=0,o=0,i=0;for(let r=0;r<numpoints;++r){let a=knotSContainer[t][e][s][r]*w_arr[r];i+=a,n+=a*points[3*r],_+=a*points[3*r+1]-.05,o+=a*points[3*r+2]}if(i>0){let r=1/i;knot_net_s_display_array[t][e][3*s]=n*r,knot_net_s_display_array[t][e][3*s+1]=_*r,knot_net_s_display_array[t][e][3*s+2]=o*r}else knot_net_s_display_array[t][e][3*s]=9999999,knot_net_s_display_array[t][e][3*s+1]=0,knot_net_s_display_array[t][e][3*s+2]=0}for(let t=0;t<t_points_dy.length;++t)for(let e=0;e<t_points_dy[t].length-1;++e)if(t_points_dy[t][e])for(let s=0;s<felbp1;++s)if(knotTst[t][e][2*s]>=t_min&&knotTst[t][e][2*s]<=t_max&&knotTst[t][e][2*s+1]>=s_min&&knotTst[t][e][2*s+1]<=s_max){let n=0,_=0,o=0,i=0;for(let r=0;r<numpoints;++r){let a=knotTContainer[t][e][s][r]*w_arr[r];i+=a,n+=a*points[3*r],_+=a*points[3*r+1]-.05,o+=a*points[3*r+2]}if(i>0){let r=1/i;knot_net_t_display_array[t][e][3*s]=n*r,knot_net_t_display_array[t][e][3*s+1]=_*r,knot_net_t_display_array[t][e][3*s+2]=o*r}else knot_net_t_display_array[t][e][3*s]=9999999,knot_net_t_display_array[t][e][3*s+1]=0,knot_net_t_display_array[t][e][3*s+2]=0}}function distance(t,e,s,n){let _=t-s,o=e-n;return _*_+o*o}function mouseMoved(){if(no_tmesh&&mouseX<=mousexmax&&mouseY>mouseymin){let e=0;for(var t=0;t<points.length;t+=3,++e)if(distance(scp_arr[2*e],scp_arr[2*e+1],mouseX,mouseY)<=100){if(on_point!=t/3&¬Popup){on_point=t/3;let e=document.getElementById("myPopup");e.style.left=mouseX-e.offsetWidth/2+"px",e.style.top=mouseY+"px",e.classList.add("show"),notPopup=!1}break}}}function mousePressed(){if(mouseX<=mousexmax&&mouseY>mouseymin){let t=document.getElementById("myPopup");t.classList.contains("show")&&(t.classList.remove("show"),on_point=-1,notPopup=!0),mouseButton===CENTER?(shift_x=0,shift_y=0,shift_z=0,redraw()):(dmx=mouseX,dmy=mouseY)}}function mouseDragged(){if(mouseX<=mousexmax&&mouseY>mouseymin){if(mouseButton===LEFT){let t=mouseX-dmx,e=mouseY-dmy;t>0?(szog2+=t/width*PI,redraw()):t<0&&(szog2+=t/width*PI,redraw()),e>0?((szog1+=e/height*PI)>PI/2&&(szog1=PI/2),redraw()):e<0&&((szog1+=e/height*PI)<-PI/2&&(szog1=-PI/2),redraw())}else if(mouseButton===RIGHT){let t=mouseX-dmx,e=mouseY-dmy;t>0?(shift_x+=t,redraw()):t<0&&(shift_x+=t,redraw()),e>0?(shift_y+=e,redraw()):e<0&&(shift_y+=e,redraw())}dmx=mouseX,dmy=mouseY}}function mouseWheel(t){mouseX<=mousexmax&&mouseY>mouseymin&&(entered_pop||(t.delta>0?(sugar*=1.1,redraw()):(sugar*=.9,redraw())))}function keyPressed(){40===keyCode&&(shift_y+=10,redraw()),38===keyCode&&(shift_y-=10,redraw()),39===keyCode&&(shift_x+=10,redraw()),37===keyCode&&(shift_x-=10,redraw()),74===keyCode&&(shift_z+=10,redraw()),70===keyCode&&(shift_z-=10,redraw()),13===keyCode&&(shift_x=0,shift_y=0,shift_z=0,redraw())}init_tmesh();