point cloud and peasyCam

edited March 2014 in How To...

Hi, I have a problem. drawing 4000 points and lines every frame is really slowing down my frameRate.. I wanted to know, since I using peasyCam for this 3d visualization, is there a way I can render fast? I don't believe I can call it in setup() can I? since I wanna rotate around the object in 3d.. (using peasyCam). Any suggestions welcome!

Answers

  • edited May 2014 Answer ✓

    Wow, 4000 points! I've got a sketch here using 5000 points. Perhaps it can give you some ideas? >-)

    /**
     * Vector Dot Lines (v3.59)
     * by GokPotter (2013/Feb)
     * modders v.k., Thomas.Diewald, GoToLoop
     *
     * works faster when compiled in P1.5.1!
     *
     * http://forum.processing.org/topic/gradual-movement-within-a-for-loop
     * http://forum.processing.org/topic/drawing-a-network-of-nodes-and-links
     * http://forum.processing.org/topic/interaction-among-objects-in-an-array
     *
     * http://studio.processingtogether.com/sp/pad/export/ro.9Kzw4QVZGKeFZ/latest
     */
    
    final static color BG = -1, FG = 0;
    final static short DOT_DIST  = 50, DOT_OPAC = 60;
    final static float DOT_SPEED = 3, FPS = 60, BOLD = 1;
    
    final static short NUM_DOTS = 5000;
    final static Point[] points = new Point[NUM_DOTS];
    
    final static String GFX = P2D;  // Use P2D or JAVA2D
    
    static int dotConnects;
    
    void setup() {
      size(1024, 800, GFX);
      frameRate(FPS);
      noSmooth();
      strokeWeight(BOLD);
      stroke(FG, DOT_OPAC);
    
      for ( int i = 0; i != NUM_DOTS; 
      points[i++] = new Point(random(.5, DOT_SPEED)) );
    }
    
    void draw() {
      background(BG);
    
      java.util.Arrays.sort(points);
    
      final float minDist = map(mouseX, 0, width, 1, DOT_DIST);
      dotConnects = 0;
    
      for (int i = 0; i != NUM_DOTS;) {
        final Point p = points[i++];
    
        p.update();
        p.drawPoint();
    
        nearestNeighbors(p, i, minDist);
      }
    
      frame.setTitle(" Nearest neighbor"
        + " | fps "         + nf(frameRate, 0, 2)
        + " | minDist "     + nf(minDist, 0, 2)
        + " | numPoints "   + NUM_DOTS
        + " | numConnects " + dotConnects);
    }
    
    static final void nearestNeighbors(Point p1, int i, float minDist) {
      final float minDistSq = minDist * minDist;
      final float x = p1.x, y = p1.y;
    
      for (int j = i; j != NUM_DOTS;) {
        final Point p2 = points[j++];
    
        final float dx = p2.x - x;
        if (dx > minDist)   return;
    
        final float dy = abs(p2.y - y);
        if (dy > minDist)   continue;
    
        if (dx*dx + dy*dy < minDistSq) {
          //if (dy < minDist && abs(dx) < minDist) {
          p1.drawLine(p2);
          dotConnects++;
        }
      }
    }
    
    final class Point extends PVector implements Comparable<Point> {
      final PVector dir = new PVector();
      final float spd;
    
      Point(float speed) {
        spd = speed;
        init();
      }
    
      void init() {
        final float a = random(TWO_PI);
        dir.set(spd*cos(a), spd*sin(a), 0);
        set(random(width), random(height), 0);
      }
    
      void update() {
        add(dir);
        if ( isOffScreen() )   init();
      }
    
      boolean isOffScreen() {
        return x < 0 | x > width | y < 0 | y > height;
      }
    
      void drawPoint() {
        point(x, y);
      }
    
      void drawLine(Point other) {
        line(x, y, other.x, other.y);
      }
    
      int compareTo(Point other) {
        return (int) Math.signum(x - other.x);
      }
    }
    
  • edited March 2014

    That is bloody nice sketch! Thank you! I'm sure I ll use this later but I am working with 3d visualization! and sketch getting slowww.. Here's a screenshot

Sign In or Register to comment.