//Kohonen Self-Organising-Map //adapted from // Tracer make; SOM net; int state = 0; int netSize = 10; Buttons [] button; PFont font; int exportState = 0; boolean rotation,rand; int w,h; void setup(){ size(800,800,P3D); font = loadFont("Kartika-48.vlw"); textFont(font,26); button = new Buttons[4]; button[0] = new Buttons(width-100, 100, 100, 20, color(240,240,240), "train", null); button[1] = new Buttons(width-100, 120, 100, 20, color(240,240,240), "clear image", null); button[2] = new Buttons(width-100, 140, 100, 20, color(240,240,240), "export data", null); button[3] = new Buttons(width-100, 160, 100, 20, color(240,240,240), "cyclic vectors", null); make = new Tracer(); ellipseMode(CENTER); w = width/2; h = height/2; rand = false; } void draw(){ background(240,240,240); pushMatrix(); translate(w,h); if(rotation){ rotateY((TWO_PI / width) * mouseX); rotateX((TWO_PI / height) * mouseY); } translate(0,0,-100); make.draw(); switch(state){ case 0: break; case 1: net = new SOM(netSize,make.xVectors(),make.yVectors(),make.zVectors(),rand); state++; break; case 2: net.run(); net.draw(); if(net.stable){ state++; } break; case 3: net.draw(); break; } popMatrix(); for(int i = 0; i < button.length; i++) button[i].draw(); } void mousePressed(){ int choice = -1; for(int i = 0; i < button.length; i++) if(button[i].over()) choice = i; switch(choice){ case 0: state = 1; break; case 1: make = new Tracer(); state = 0; break; case 2: saveObjects("net.obj"); break; case 3: rand = !rand; if(rand) button[choice].label = "random vectors"; else button[choice].label = "cyclic vectors"; break; default: Vec3 temp = new Vec3((1.0/width)*mouseX,(1.0/height)*mouseY, random(0.5)); make.trace(temp); } } void keyPressed(){ rotation = !rotation; } Vec3 [] concatVector(Vec3 [] vectors, Vec3 [] addVectors) { Vec3 [] temp = new Vec3[vectors.length + addVectors.length]; System.arraycopy(vectors, 0, temp, 0, vectors.length); System.arraycopy(addVectors, 0, temp, vectors.length, addVectors.length); return temp; } Vec3 [] addVector(Vec3 [] vectors, Vec3 newVector) { Vec3 [] temp = new Vec3[vectors.length + 1]; System.arraycopy(vectors, 0, temp, 0, vectors.length); temp[vectors.length] = newVector; return temp; }