Klein Bottle

Klein Bottle

Log in to post a comment.

// Klein Bottle
// https://en.wikipedia.org/wiki/Klein_bottle

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(0.3);

// Global code will be evaluated once.
const turtle = new Turtle();

// Quaternion from Eular angles
// https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
function quat(yaw,pitch,roll)
{
    let cy = Math.cos(yaw*0.5);
    let sy = Math.sin(yaw*0.5);
    let cp = Math.cos(pitch*0.5);
    let sp = Math.sin(pitch*0.5);
    let cr = Math.cos(roll*0.5);
    let sr = Math.sin(roll*0.5);
    
    let w = cy * cp * cr + sy * sp * sr;
    let x = cy * cp * sr - sy * sp * cr;
    let y = sy * cp * sr + cy * sp * cr;
    let z = sy * cp * cr - cy * sp * sr;
    
    return [x,y,z,w];
}

// ref https://github.com/toji/gl-matrix/blob/master/src/mat4.js
// fromRotationTranslationScale
// matrix is stored in the row pattern
function make_transform(o,pos,quat,scale)
{
    // scale temp vars
    let sx = scale[0];
    let sy = scale[1];
    let sz = scale[2];
    
    // translation temp vars
    let tx = pos[0];
    let ty = pos[1];
    let tz = pos[2];
    
    //Quat calc temp variables
    let x = quat[0];
    let y = quat[1];
    let z = quat[2];
    let w = quat[3];
    
    let xx = x*x*2.0;
    let xy = x*y*2.0;
    let xz = x*z*2.0;
    let wx = x*w*2.0;
    
    let yy = y*y*2.0;
    let yz = y*z*2.0;
    let wy = y*w*2.0;
    
    let zz = z*z*2.0;
    let wz = z*w*2.0;

    o[0] = (1-yy-zz)*sx;
    o[1] = (xy-wz)*sy;
    o[2] = (xz+wy)*sz;
    o[3] = tx;
    o[4] = (xy+wz)*sx;
    o[5] = (1-xx-zz)*sy;
    o[6] = (yz-wx)*sz;
    o[7] = ty;
    o[8] = (xz-wy)*sx;
    o[9] = (yz+wx)*sy;
    o[10] = (1-xx-yy)*sz;
    o[11] = tz;
    o[12] = 0;
    o[13] = 0;
    o[14] = 0;
    o[15] = 1;
    
    return o;
}

function transform_vector(v,m)
{
    let x = v[0], y = v[1], z = v[2], w = v[3];
    let o = [0, 0, 0, 0];
    o[0] = m[0] * x + m[1] * y + m[2] * z + m[3] * w;
    o[1] = m[4] * x + m[5] * y + m[6] * z + m[7] * w;
    o[2] = m[8] * x + m[9] * y + m[10] * z + m[11] * w;
    o[3] = m[12] * x + m[13] * y + m[14] * z + m[15] * w;
    return o;
}

let focus = 200;
function project(v){
    let x = v[0];
    let y = v[1];
    let z = v[2];
    let s = focus/z;
    return [x*s,y*s];
}

//transfrom matrix
let transform = new Float32Array(16);

let Mesh = function(verts,edges,tsf)
{
    let out = {}
    out.verts = verts;
    out.edges = edges;
    out.transform = tsf
    return out;
}

let PI = Math.PI;
let PI2 = Math.PI*2;

// fill in the bottle vertice and edge list
function make_bottle(nu,nv)
{
    let o = {};
    o.edges = [];
    o.verts = [];
    let du = 1.0/nu;
    let dv = 1.0/nv;
    nu +=1;
    nv +=1;
    let u = 0,v = 0;
    let i = 0,j = 0;
    
    // push vertice
    for(let i=0;i<nu;i++)
    {
        u = i*du;
        for(let j=0;j<nv;j++)
        {
            v = j*dv;
            
            let cu = Math.cos(u*PI);
            let su = Math.sin(u*PI);
            let cv = Math.cos(v*PI2);
            let sv = Math.sin(v*PI2);
            
            let cu2 = cu*cu;
            let cu3 = cu2*cu;
            let cu4 = cu2*cu2;
            let cu5 = cu4*cu;
            let cu6 = cu5*cu;
            let cu7 = cu6*cu;
            
            let k = 2./15;
            
            let x = -k*cu*(3*cv-30*su+90*cu4*su-60*cu6*su +5*cu*cv*su);
            let y = -1./15*su*(3*cv - 3*cu2*cv-48*cu4*cv +48*cu6*cv - 60*su+5*cu*cv*su-5*cu3*cv*su-80*cu5*cv*su + 80*cu7*cv*su);
            let z = k*(3+5*cu*su)*sv;
            
            o.verts.push([x,y,z,1.0]);
            
            //o.verts.push([u,v,1.0,1.0]);
        }
    }
    
    
    //link edges
    for(let i=0;i<nu;i++)
    {
        u = i*du;
        for(let j=0;j<nv;j++)
        {
            v = j*dv;
            let id = i*nv+j;
            let inext = (i+1)%nu;
            let jnext = (j+1)%nv;
            let id_down = i*nv+jnext;
            let id_right = inext*nv+j;
            
            o.edges.push(id,id_down);
            o.edges.push(id,id_right);
        }
        
    }
    return o;
}

function draw_mesh(tctx,mesh)
{
    let edges = mesh.edges;
    let verts = mesh.verts;
    let tsf = mesh.transform;
    
    for (let i = 0, len = edges.length/2; i < len; i++) {
        
        let i0 = edges[2*i];
        let i1 = edges[2*i+1];
        
        // perform transform
        let v0 = transform_vector(verts[i0],tsf);
        let v1 = transform_vector(verts[i1],tsf);
        
        // projection
        v0 = project(v0);
        v1 = project(v1);
        
        // draw edge
        tctx.penup();
		tctx.goto([v0[0],v0[1]]);
		tctx.pendown();
		tctx.goto([v1[0],v1[1]]);
		
    }
}


function draw_mesh_substep(tctx,mesh,step)
{
    let edges = mesh.edges;
    let len = edges.length/2;
    if(step>=len)
    {
        return false;
    }
    
    
    let verts = mesh.verts;
    let tsf = mesh.transform;
    
    let i0 = edges[2*step];
    let i1 = edges[2*step+1];
        
        // perform transform
    let v0 = transform_vector(verts[i0],tsf);
    let v1 = transform_vector(verts[i1],tsf);
        
    // projection
    v0 = project(v0);
    v1 = project(v1);
        
    // draw edge
    tctx.penup();
	tctx.goto([v0[0],v0[1]]);
	tctx.pendown();
	tctx.goto([v1[0],v1[1]]);
	return true;
}


let scale = [1.0,1.0,1.0]
let rotation = quat(0.2,0.6,0.);
let position = [0,-2.1,5.];

make_transform(transform,position,rotation,scale);

let btl = make_bottle(100,100);
let bottle = Mesh(btl.verts,btl.edges,transform);

// The walk function will be called until it returns false.
function walk(i) {
   
    //draw_mesh(turtle,bottle);
    let stop = draw_mesh_substep(turtle,bottle,i);
    return stop;
}