Loading...
Logo
Processing Forum
I have a geometry that is constantly chaging. I would like to hit some key and record how the geometry was on that frame it was captured and store it in an array list where I can add more frozen geometry and eventually recall them. Does anybody has an idea of how I could do that?
Thanks a lot,
buzz

Replies(4)

Yes, I have an idea...

But since I have no idea of what your data looks like, I can't tell you much!
Phewww, thanks you have seen and reply to this...I am still struggling :(
the code is long but let me try and get the important bits for you... i need to be able to store and draw frozen individuals  let me go backwards to be easier to understand:
class Population
{
  ArrayList <Individual> m_popul;

  Population()
  {
    m_popul = new ArrayList<Individual>();
    m_popul.add(new Individual());
    Individual ind = m_popul.get(0);
    ind.evaluate();
    println(m_popul.size());
  }
}

class Individual
{
  Genotype m_genotype;
  Phenotype m_phenotype;
  float m_fitness;
 
  Individual()
  {
    m_genotype = new Genotype();
    m_phenotype = new Phenotype(m_genotype);
    m_fitness = 0.0;   
  } 
  void draw()
  {
    m_phenotype.draw();
     
  }
 
  void evaluate()
  {
    m_fitness = m_phenotype.evaluate();
  }
}

class Phenotype
{
  PVector n; // normal of the triangles
  PVector g; // centroid of the traingles
  // the real phenotype can only be defined with reference to its environment (Alasdair)
  PVector s; // sun position
  float angle; // agle between the normal of the traingles and the sun

  Phenotype(Genotype ge)
  {
    n = new PVector();
    g = new PVector();
    s = new PVector();
  }

  void draw()
  {
    for(int i = 0; i < gen.triangles.size(); i++)
    {
      Triangle tr = (Triangle)gen.triangles.get(i);

      // 1. calculate the cross product
      n.x = (tr.p2.y * tr.p1.z) - (tr.p2.z * tr.p1.y);
      n.y = (tr.p2.z * tr.p1.x) - (tr.p2.x * tr.p1.z);
      n.z = (tr.p2.x * tr.p1.y) - (tr.p2.y * tr.p1.x);
      n.normalize();

      // 2. find the centroid
      g.x = (tr.p1.x+tr.p2.x+tr.p3.x)/3;
      g.y = (tr.p1.y+tr.p2.y+tr.p3.y)/3;
      g.z = (tr.p1.z+tr.p2.z+tr.p3.z)/3;

      // 3. draw the normal
      stroke(0,100,100);
      //line(g.x*factor, g.y*factor, factor-g.z*factor, g.x*factor + 0.03*(n.x*factor), g.y*factor + 0.03*(n.y*factor), factor-g.z*factor + 0.03*(factor-n.z*factor) );

      // 4. pvector that follows the sun
      s.x = sunny.currentPos.x;
      s.y = sunny.currentPos.y;
      s.z = sunny.currentPos.z;
      s.normalize();
      stroke(0);
      //line(g.x*factor, g.y*factor, factor-g.z*factor, g.x*factor + 0.03*(s.x*factor), g.y*factor + 0.03*(s.y*factor), factor-g.z*factor + 0.03*(factor-s.z*factor));

      // 5. calculate the angle between sun and normal vector
      angle = PVector.angleBetween(n, s);
      //println(degrees(angle));

      // 6. map the angle to a color value anf fill the triangle to that value
      // if it is above the horizon
      if(angle <= HALF_PI)
      {
        float mapping = map(angle, 0, 90, 0, 255); //possible angles to possible colours
        //println(mapping);
        noStroke();
        fill(mapping, 100, 100);
      }
      else
      {
        noStroke();
        fill(0);
      }

      // 7. draw the mesh of triangles
      beginShape();
      vertex(tr.p1.x*factor, tr.p1.y*factor, factor-tr.p1.z*factor);
      vertex(tr.p2.x*factor, tr.p2.y*factor, factor-tr.p2.z*factor);
      vertex(tr.p3.x*factor, tr.p3.y*factor, factor-tr.p3.z*factor);
      endShape();
     
    }//normals for loop
  }// void display()

  float evaluate()
  {
    // to start with I will say that the best fit is the
    // geometry that has the least sun radiation over the year   
    float fitness = 0.0;
    fitness += degrees(sunny.yearlyAverage());
    return fitness;   
   
  }//float evaluate 
}//class Phenotype

class Genotype
{
  // create array list of  triangles for the delaunay
  ArrayList triangles;

  Genotype()
  {
    triangles = new ArrayList();
  }
 
  // had to put on a void to run all the points
  // because I need to add the triangles to the array list
  // because I dont know what size the array list has
  void getTriangles()
  {
    // get the triangulated mesh FROM
    // THE ARRAY LIST OF POINTS COMING FROM THE KINECT
    ///////////////////////////////////////////
    triangles = Triangulate.triangulate(points);
    //////////////////////////////////////////
   
    //println(triangles.size());
  }
}
hopefully its kind of understandable...let me know if not and thanks a lot :)

AH SORRY BTW HERE IS THE

void keyPressed()
{
if(key == ' ')
  {
    for (int i = 0; i < 1; i++)
    {

      if (pop.m_popul.size() < 100)
      {
        pop.m_popul.add(new Individual());
        Individual ind = pop.m_popul.get(i);
        ind.evaluate();
      }
    }
    //
  }
}
Well, it is quite simple (except for Triangle, whose type remains unknown).
You can make the serialization yourself, eg. in CSV with references, by iterating on Population, and saving each Individual.
Perhaps simpler (on the programming side), but needing more research, you can use the built in Java serialization capabilities (saving binary data), or use some serialization library like XStream, able to save data in XML form.
thanks phi.lho
the triangles come from:

// import delaunay triangulation library
import org.processing.wiki.triangulate.*;

the points that are being triangulated

    // THE ARRAY LIST OF POINTS COMING FROM THE KINECT
    ///////////////////////////////////////////
    triangles = Triangulate.triangulate(points);
    //////////////////////////////////////////

are vectors coming from an openKinect arrayList of points

ArrayList points = new ArrayList();
....
        if(rawDepth < threshold)
        {                  
          pushMatrix();
          translate(v.x*factor, v.y*factor, factor-v.z*factor);
          // draw a point
          stroke(0);
          strokeWeight(4);
          //point(0, 0);
          popMatrix();
          points.add(v); // key line of the code where the vectors are added to an array list of vectors
        }
...

Couldn't I output the data from the genotype, phenotype and fitness in a .txt file when I hit spacebar? And then to draw the frozen individual I could input the .txt file in the void draw()? I would need to have two screens, but that is another question i don't know how to do. what do you think?