プログラムの改善@99日目

この前のプログラムを改善してみた.日本語字幕も作ったので,より分かりやすくなったのではないでしょうか.

ちなみに今日の時点でのプログラムはこんな感じ.ゴミ変数とか残っているので,まだまだ改善の余地アリ.

[java]
// dec08a
// show each pictures as each nodes
// improve the remain problem
// show animation

import fullscreen.*;
import japplemenubar.*;

// we need to import the TUIO library
// and declare a TuioProcessing client variable
import TUIO.*;
TuioProcessing tuioClient;

// these are some helper variables which are used
// to create scalable graphical feedback
float cursor_size = 15; //15;
float object_size = 60;
float table_size = 760;
float scale_factor = 1;
PFont font;
//—————–
// for showing node graph
int nodeCount;
Node[] nodes = new Node[100];
HashMap nodeTable = new HashMap();
HashMap edgeTable = new HashMap(); //added bu Akira
int edgeCount;
Edge[] edges = new Edge[500];
static final color nodeColor =#FFFFFF;//#F0C070;
static final color selectColor =#FF3030;
static final color fixedColor =#FF8080;
static final color edgeColor =#FFFFFF;
static final color mainNodeColor =#FF5555;
static final color subNodeColor =#FFFFFF;
float maxConnection=0;
// int checkGenerationNum = 3; //selection of generation from root
int rootNode=1;
int orglen = 80;
Node selection;
//int ttd ;
int textOnOff = 0;
int maxImageSize = 50;

// for fullscreen
//FullScreen fs;

// for show images

//————————————-
//————————————-
//————————————-

//——————————————————————-
// for draw graph
void loadData()
{
addEdge ("A", "1910");
addEdge ("B", "1910");
addEdge ("C", "A");
addEdge ("D", "cC");
addEdge ("German", "A");
addEdge ("USA", "D");
addEdge ("E" , "D");
addEdge ("D", "1920");
addEdge ("Japan", "a");
addEdge ("C", "E");
addEdge ("USA", "A");
addEdge ("B", "Chair");
addEdge ("C", "1910");
}

void setup()
{
//size(screen.width,screen.height);
size(640,480);
noStroke();
fill(0);

loop();
frameRate(60);
//noLoop();

hint(ENABLE_NATIVE_FONTS);
font = createFont("Arial", 18);
scale_factor = height/table_size;

// we create an instance of the TuioProcessing client
// since we add "this" class as an argument the TuioProcessing class expects
// an implementation of the TUIO callback methods (see below)
tuioClient = new TuioProcessing(this);

// for draw graph
loadData();

smooth();

// fs = new FullScreen(this);
//fs.enter();
}
//————————————-
// within the draw method we retrieve a Vector (List) of TuioObject and TuioCursor (polling)
// from the TuioProcessing client and then loop over both lists to draw the graphical feedback.
void draw()
{
background(0);
textFont(font,18*scale_factor);
float obj_size = object_size*scale_factor;
float cur_size = cursor_size*scale_factor;
int nodeID;

Vector tuioObjectList = tuioClient.getTuioObjects();
for (int i=0;i<tuioObjectList.size();i++)
{
TuioObject tobj = (TuioObject)tuioObjectList.elementAt(i);
if(tobj.getSymbolID() != 0)
{
nodeID = tobj.getSymbolID() -1 ;
}
else
{
nodeID = 1;
}

// stroke(0);
// fill(100,200,100);
pushMatrix();
translate(tobj.getScreenX(width),tobj.getScreenY(height));
rotate(tobj.getAngle());
popMatrix();

if(tobj.getSymbolID() <= nodeCount && tobj.getSymbolID() !=0 ){
fill(255);
// text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
if(textOnOff == 1)
{
text("ttd="+ nodes[nodeID].ttd +", SymbolID is"+ nodeID+
", label=" + nodes[nodeID].label + ", subOrMain=" + nodes[nodeID].mainOrSub,
tobj.getScreenX(width), tobj.getScreenY(height));
}
//—————
println("@draw nodeID is ==>" + nodeID);
// set all objects
// println("@draw routine node’s mainOrSub is "+ nodes[tobj.getSymbolID()].mainOrSub);
nodes[nodeID].mainOrSub = 1;
nodes[nodeID].st = 1;
nodes[nodeID].showFlag = true;
// println("@draw after routine node’s mainOrSub is "+ nodes[tobj.getSymbolID()].mainOrSub);
nodes[nodeID].x = tobj.getScreenX(width);
nodes[nodeID].y = tobj.getScreenY(height);
// make alive concerned edges
aliveEdge(nodeID,true);
}else{
println("————————[ERROR] out of Range!! NodeID ="+nodeID);
}
}

//——graph draw
for(int i=0; i< edgeCount; i++)
{
if(edges[i].showFlag == true)
{
edges[i].relax();
}
}
for(int i=0; i< nodeCount; i++)
{
if(nodes[i].showFlag == true)
{
nodes[i].relax();
}
}
for(int i=0; i< nodeCount; i++)
{
if(nodes[i].showFlag == true)
{
nodes[i].update();
}
}
for(int i=0; i< edgeCount; i++)
{
if(edges[i].showFlag == true)
{
edges[i].draw();
}
}
for(int i=0; i< nodeCount; i++)
{
if(nodes[i].showFlag == true)
{
if (nodes[i].ttd == 255)
{
// println("usually draw i=" + i + " / MainOrSub="+ nodes[i].mainOrSub);
nodes[i].draw();
}
else if(nodes[i].ttd < 255 && nodes[i].ttd > 0)
{
println(">>>>>>>>>>>>>@draw -ttd routine");
if(nodes[i].st == 2)
{
nodes[i].ttd = nodes[i].ttd -7;
}
else if (nodes[i].st == 1)
{
nodes[i].ttd = 255;
}
nodes[i].draw();
}
else if (nodes[i].ttd <= 0)
{
println("@draw ttd reset routine");
nodes[i].showFlag = false;
nodes[i].st = 0;
nodes[i].mainOrSub = 0;
nodes[i].ttd = 255;
aliveEdge(i,false);
}else
{
println("why nothing??");
}
}
}
}

//————————
void aliveEdge(int alivedNode, Boolean DorA)
{
int al = alivedNode;
//println("make alive node" + al );
// check the label of clickedNode
// println("label is " + nodes[al].label);
// use for routain to find the linked nodes
for (int i=0; i<edgeCount; i++)
{
if (edges[i].from == nodes[al])
{
edges[i].showFlag = DorA;
findNode(edges[i].to.label).showFlag = DorA;
if(findNode(edges[i].to.label).mainOrSub != 1)
{
findNode(edges[i].to.label).mainOrSub = 2;
}
}
else if(edges[i].to == nodes[al])
{
edges[i].showFlag = DorA;
findNode(edges[i].from.label).showFlag = DorA;
if(findNode(edges[i].from.label).mainOrSub !=1)
{
findNode(edges[i].from.label).mainOrSub = 2;
}
}
}
}

// these callback methods are called whenever a TUIO event occurs
// called when an object is added to the scene
void addTuioObject(TuioObject tobj)
{
if(tobj.getSymbolID() > nodeCount)
{
println("————————recognition error!!");
// removeTuioObject(tobj);
}
// println("add object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
}

// called when an object is removed from the scene
void removeTuioObject(TuioObject tobj)
{
int nodeID;
nodeID = tobj.getSymbolID() -1;
println("******************remove object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
//show off removed nodes
//println("node off");
if(nodeID <= nodeCount && nodeID >=0)
{

// set fade out gradually
if(nodes[nodeID].ttd ==255)
{
println("*-*-*-*-*-*-*-*-*-*-*-remove object ttd reduce");
nodes[nodeID].ttd –;
nodes[nodeID].st = 2;
}
}
else
{
println("————–Error @ removeTuioObject");
}
}

// called when an object is moved
void updateTuioObject (TuioObject tobj)
{
// println("update object "+tobj.getSymbolID()+" (tobj.getSessionID()="+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()
// +" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel());
}

// called after each message bundle
// representing the end of an image frame
void refresh(TuioTime bundleTime)
{
redraw();
}

//———NODE——————————-
class Node
{
float x,y;
float dx, dy;
boolean fixed;
String label;
int count;
boolean showFlag=false;
int mainOrSub;
PImage img;
int ttd;
int st; // 0 – hide, 1 – visible, 2- fade,
String loadImageName;
float imgWidth, imgHeight;

// node’s constructor
Node (String label)
{
this.label = label;
x = random(width);
y = random(height);
mainOrSub = 0; //all nodes are 0, and Recognized = 1, childNode =2;
// load each images
// images name are all small capital
loadImageName = label.toLowerCase();
if(loadImage(loadImageName+".jpg")!= null)
{
img = loadImage(loadImageName+".jpg");
}
else if(loadImage(loadImageName+".gif")!=null)
{
img = loadImage(loadImageName+".gif");
}
else if (loadImage(loadImageName+".png")!=null)
{
img = loadImage(loadImageName+".png");
}
else
{
img =loadImage("noimage.gif");
}

//img = loadImage(loadImageName+".gif");
ttd = 255;
st = 0;
imgWidth = img.width;
imgHeight = img.height;
if (imgWidth >= imgHeight)
{
imgHeight = maxImageSize*(imgWidth/imgHeight) ;
imgWidth = maxImageSize;
}else{
imgWidth = maxImageSize*(imgHeight/imgWidth ) ;
imgHeight = maxImageSize;
}
}

void increment()
{
count++;
}

// methoad
void relax()
{
float ddx = 0;
float ddy = 0;
for (int j=0; j< nodeCount ; j++)
{
Node n = nodes[j];
if( n!= this)
{
float vx = x – n.x;
float vy = y – n.y;
float lensq = vx * vx + vy*vy;
if (lensq == 0)
{
ddx += random(1);
ddy += random(1);
}
else if (lensq < 100*100)
{
ddx += vx / lensq;
ddy += vy / lensq;
}
}
}
float dlen = mag(ddx, ddy) /2;
if(dlen > 0)
{
dx += ddx / dlen;
dy += ddy / dlen;
}
}

void update()
{
if (!fixed)
{
x += constrain(dx, -5, 5);
y += constrain(dy, -5, 5);
x = constrain(x,0,width);
y= constrain (y,0,height);
}
dx /= 2;
dy /= 2;
}

// nodedraw
void draw()
{
fill (nodeColor);
float nodeSize = 20;//(this.count / maxConnection ) * 30 + 10;
//float nodeAlpha =(this.count / maxConnection ) *200 +30;
stroke(0);
strokeWeight(0.5);

println("mainOrSub="+mainOrSub);

if(textOnOff ==1){
// "ttd="+ ttd +",
text("label=" + label + ", mainOrSub=" + mainOrSub,x, y-20);
}

if(mainOrSub == 1)
{
// as a main (marker object)
// show images
println ("————-@node_draw main");
imageMode (CENTER);
// set alpha
tint(255,255,255,ttd);
image(img,x,y,imgHeight,imgWidth);
}
else if(mainOrSub == 2)
{
println ("@node_draw sub");
fill(subNodeColor);
ellipse(x,y,nodeSize,nodeSize);
}
else
{
println("————–@node_draw nothing");
}

float w = textWidth(label) + 10;
textAlign(CENTER,CENTER);
fill(0,102,153);
//show the label
text(label,x, y+32);
}
}
//——–end of Class "node" ———–

//——-CLASS EDGE———————-
class Edge
{
Node from;
Node to;
float len;
int count;
boolean showFlag=false;
boolean fromShowFlag = false;
boolean toShowFlag = false;

// constructer
Edge(Node from, Node to)
{
this.from = from;
this.to = to;
this.len = orglen;
from.increment();
to.increment();
}

void increment()
{
count++;
}

void relax()
{
float vx = to.x – from.x;
float vy = to.y – from.y;
float d = mag(vx, vy);
if( d>0 )
{
float f =(len -d) / (d*3);
float dx = f*vx;
float dy = f*vy;
to.dx += dx;
to.dy += dy;
from.dx -= dx;
from.dy -= dy;
}
}

void draw()
{
stroke(edgeColor);
strokeWeight(0.5); // org size = 0.35
line (from.x, from.y, to.x, to.y);
}
}

//———————–
void addEdge(String fromLabel, String toLabel)
{
Node from = findNode(fromLabel); //return n
Node to = findNode(toLabel); //retune n
//add edgeTable
edgeTable.put(from, to);
from.increment();
to.increment();
if (from.count > to.count)
{
if (maxConnection < from.count)
{
maxConnection = from.count;
}
}
else
{
if (maxConnection < to.count)
{
maxConnection = to.count;
}
}

Edge e = new Edge(from, to); //create new edge "e"
if(edgeCount == edges.length)
{
//edgegount = int //edges is Array
edges = (Edge[]) expand(edges);
}
edges[edgeCount++] = e; //add edgesList
// add edgeHashTable
}

//——————-
//return n
Node findNode(String label)
{
label = label.toLowerCase(); // make all strings into small letteres
Node n = (Node) nodeTable.get(label);// ??
if(n == null)
{
return addNode(label); // retun n
}
return n; //return Node
}
//——————
Node addNode(String label)
{
Node n = new Node(label);
if (nodeCount == nodes.length-1)
{
nodes = (Node[]) expand(nodes);
}
nodeTable.put(label, n); //add infromation to hashtable
nodes[nodeCount++] = n; //add nodes Array
println("—-Added Node "+nodeCount +" / label =" + label);
return n;
}

[/java]

Follow me!

プログラムの改善@99日目” に対して1件のコメントがあります。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA