This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

anar+

by   LaBelle + Nembrini
©2008

built with ( )
   examples index
Rotate: middle click or key[1]
Zoom in|out: wheel button or key[2]
AutoRotate: key[5]
(First Click inside the applet to enable keys)

This applet use OpenGL, you might have to install extra jogl libraries once to view this applet. You may have a look at image capture and video at the bottom of this page. You might accept security security permenently to remove the security prompts on each pages.



sourcecode


   PDE Download:   exPso00aEasy.pde
   JAVA Download:   exPso00aEasy.java


Click on anar+ terms to get the documentation.

import processing.opengl.*;
import anar.*;
 
 
 
import java.util.Iterator;
 
import pso.*;
 
 
/*
 * Example for Anar library by Guillaume LaBelle + Julien Nembrini
 * http://anar.ch
 */
 
 
Obj           myObject;
 
/** list of points forming the base face */
Face          faceBase;
 
/** volume height */
final float   h      = 120;
/** volume width */
final float   L      = 200;
/** volume length */
final float   LL     = 200;
 
/** pso engine */
PSOEngine     pso;
 
/** height parameters the length of the array defines the parameter space */
final float[] height = {.2f, .4f, .6f, .8f, 1f, .8f, .6f, .4f, .2f};
 
// final float[] height = { .2f, .4f, .6f, .8f, 1f };
// final float[] height = { .2f, .4f, };
 
void setup(){
 
  // size(screen.width,screen.height, P3D);
    size(1000,500,OPENGL);
  // size(1000,500, P3D);
  background(55,33,6);
 
  pso = new PSOEngine(height.length,this);
  // use contraction
  PSOEngine.contract = true;
 
  myObject = generateVolume(height);
 
  Anar.init(this);
  Anar.drawAxis(true);
  Anar.camTarget(0f,0f);
 
}
 
 
void draw(){
  if(frameCount%2==0)
    background(255);
  else
    background(254);
 
  if(runPSO){
    pso.computeCurrParticle();
 
    if(PSOEngine.nStep%10==0){
      print("step "+PSOEngine.nStep);
      println("   best "+PSOEngine.globalBest.perform);
    }
    if(displayBest)
      changeVolume(PSOEngine.globalBest);
    else
      changeVolume(pso.swarm.get(0));
  }
  myObject.draw();
}
 
/**
 * creates basic volume according to height array
 * 
 * @param height
 * @return
 */
Obj generateVolume(float[] height){
 
  Obj obj = new Obj();
 
  // create points from height[]
  // no symmetry enforced
  faceBase = new Face();
 
  Pt p = Anar.Pt( -L/2, -LL/2,0f,"A");
  faceBase.add(p);
  for (int i = 0; i<height.length; i++){
    p = Anar.Pt( -L/2+ (i+1)*L/ (height.length+1), -LL/2,h*height[i],"n"+i);
    faceBase.add(p);
  }
  p = Anar.Pt(L/2, -LL/2,0f,"B");
  faceBase.add(p);
 
  // create face from points
  obj.add(faceBase);
 
  // derive the points
  // define translation
  p = Anar.Pt(0f,LL,0f,"long");
  Translate t = new Translate(p);
 
  Face faceDer = new Face();
  Iterator it = faceBase.iterator();
  while (it.hasNext()){
    Pt pd = Anar.Pt((Pt)it.next(),t);
    faceDer.add(pd);
  }
  obj.add(faceDer);
 
  // create faces from all original points by rotating around faceBase
  for (int i = 0; i<faceBase.numOfPts()-1; i++){
    Face faceTemp = new Face();
    faceTemp.add(faceBase.pt(i));
    faceTemp.add(faceDer.pt(i));
    faceTemp.add(faceDer.pt(i+1));
    faceTemp.add(faceBase.pt(i+1));
    obj.add(faceTemp);
  }
 
  return obj;
}
 
 
/**
 * change volume according to candidate position in parameter space
 * 
 * @param _p
 */
void changeVolume(Candidate _p){
 
  for (int i = 0; i<height.length; i++){
    Pt pt = faceBase.pt(i+1);
    pt.set(pt.x(),pt.y(),h*_p.pos.get(i).floatValue());
  }
 
}
 
 
// /**
// * computes performance of particle p
// *
// * implements signature of Performance interface
// * as specified the performance is always positive
// */
// @Override
void computePerformance(pso.PsoParticle p){
  p.perform = (float)Math.log(computeVolume(p))-(float)Math.log(computeSurface(p));
  // p.perform = computeVolume( p) / computeSurface( p);
  // p.perform = computeSurface( p);
  // p.perform = computeVolume( p);
 
  pso.updateCurrParticle(p.perform);
 
  // compute the whole swarm ("en rafale")
  if(pso.swarm.indexOf(pso.currParticle)<pso.swarm.size()-1){
    pso.computeCurrParticle();
  }
}
 
 
/**
 * computes the volume of the form defined by particle p
 * 
 * @param p
 * @return
 */
float computeVolume(pso.PsoParticle p){
  float volume = 0f;
 
  for (int i = 0; i<p.pos.size()-1; i++){
    volume += LL* (L/ (p.pos.size()-1))*h* (p.pos.get(i)+p.pos.get(i+1))/2;
  }
 
  return volume;
}
 
/**
 * computes the surface of the form defined by particle p
 * 
 * @param p
 * @return
 */
float computeSurface(pso.PsoParticle p){
  float area = 0f;
 
  // add roof surface
  for (int i = 0; i<p.pos.size()-1; i++){
    // add roof surface
    area += LL*(float)Math.sqrt((float)Math.pow( (L/ (2* (p.pos.size()-1))),2f)+h*h*(float)Math.pow(p.pos.get(i+1)-p.pos.get(i),2f));
    // add lateral vertical sides
    area += (L/ (p.pos.size()-1))*h* (p.pos.get(i+1)+p.pos.get(i))/2;
 
  }
  // add first vertical side
  area += LL*h*p.pos.get(0);
  // add last vertical side
  area += LL*h*p.pos.get(p.pos.size()-1);
 
 
  // add floor
  area += LL*L;
 
  return area;
}
 
 
boolean displayBest = true;
boolean runPSO      = false;
 
void keyPressed(){
 
  switch(key){
    case 'q':
      runPSO = !runPSO;
    break;
    case ' ':
    break;
    case 'w':
      pso.initialise();
    break;
    case 'm':
    break;
    case 'n':
    break;
    case 'e':
      displayBest = !displayBest;
    break;
    case 'p':
      Scene.autoSeek = false;
    break;
    case 'o':
    break;
    case 'r':
    break;
    case 't':
    break;
    case 'a':
      TextIO.write( ((Object)this).getClass().getName()+".lsp",myObject.toAutocad());
    break;
    case 'b':
    break;
    case 'f':
    break;
 
 
  }
 
}
 
 
 



screenshots