/**
   * Updates the particle view. This should be called on each draw cycle in order to update the
   * positions of all nodes and edges in the viewer. If you need to update the positions of
   * particles without drawing it (e.g. to speed up movement, call updateParticles() instead.
   */
  public void draw() {
    parent.pushStyle();
    parent.pushMatrix();
    zoomer.transform();
    updateCentroid();
    centroid.tick();

    parent.translate(width / 2, height / 2);
    parent.scale(centroid.getZ());
    parent.translate(-centroid.getX(), -centroid.getY());

    if (!isPaused) {
      updateParticles();
    }

    // Ensure that any selected element is positioned at the mouse location.
    if (selectedNode != null) {
      Particle p = nodes.get(selectedNode);
      p.makeFixed();
      float mX = (zoomer.getMouseCoord().x - (width / 2)) / centroid.getZ() + centroid.getX();
      float mY = (zoomer.getMouseCoord().y - (height / 2)) / centroid.getZ() + centroid.getY();
      p.position().set(mX, mY, 0);
    }

    // Draw edges if we have positive stroke weight.
    if (parent.g.strokeWeight > 0) {
      parent.stroke(0, 180);
      parent.noFill();

      for (Map.Entry<E, Spring> row : edges.entrySet()) {
        E edge = row.getKey();
        Spring spring = row.getValue();
        Vector3D p1 = spring.getOneEnd().position();
        Vector3D p2 = spring.getTheOtherEnd().position();
        edge.draw(parent, p1.x(), p1.y(), p2.x(), p2.y());
      }
    }

    // Draw nodes.
    parent.noStroke();
    parent.fill(120, 50, 50, 180);

    for (Map.Entry<N, Particle> row : nodes.entrySet()) {
      N node = row.getKey();
      Vector3D p = row.getValue().position();
      node.draw(parent, p.x(), p.y());
    }

    parent.popMatrix();
    parent.popStyle();
  }
 /** Releases the mouse-selected node so that it readjusts in response to other node positions. */
 public void dropSelected() {
   if (!zoomer.isMouseCaptured()) {
     if (selectedNode != null) {
       nodes.get(selectedNode).makeFree();
       selectedNode = null;
     }
   }
 }
Example #3
0
  private void run() {

    try {
      CtrlMsg regReq = CtrlMsg.newBuilder().setCtrlMsgTyp(CtrlMsg.Type.REGISTER).setId(id).build();
      CtrlMsgRsp regRsp = visService.ctrl(cntr, regReq);

      ScnReq scReq = ScnReq.newBuilder().build();
      Scenario scRsp = visService.reqScn(cntr, scReq);
      this.drawer = new EventsBasedVisDebugger(scRsp, null);
      this.drawer.addAdditionalDrawer(new InfoBox());

      init();
      ZoomPan zoomer = this.drawer.zoomer;
      int w = this.drawer.getWidth();
      int h = this.drawer.getHeight();
      PVector br = new PVector(w, h);
      PVector tl = new PVector(0, 0);

      loadScenario(scRsp);

      // log.info(rsp);
      while (true) {

        PVector brC = zoomer.getDispToCoord(br);
        PVector tlC = zoomer.getDispToCoord(tl);

        FrameRqst frReq =
            FrameRqst.newBuilder()
                .setTlX(tlC.x - drawer.getOffsetX() - 100)
                .setTlY(-(drawer.getOffsetY() + tlC.y) + 100)
                .setBrX(brC.x - drawer.getOffsetX() + 100)
                .setBrY(-(drawer.getOffsetY() + brC.y) - 100)
                .setTime(0)
                .setId(id)
                .build();
        Frame frame = visService.reqFrame(cntr, frReq);
        handleFrame(frame);
      }

    } catch (ServiceException e) {
      throw new RuntimeException(e);
    }
  }
  /** Allows a node to be selected with the mouse. */
  public void selectNearestWithMouse() {
    if (!zoomer.isMouseCaptured()) {
      float mX = (zoomer.getMouseCoord().x - (width / 2)) / centroid.getZ() + centroid.getX();
      float mY = (zoomer.getMouseCoord().y - (height / 2)) / centroid.getZ() + centroid.getY();

      if (selectedNode == null) {
        float nearestDSq = Float.MAX_VALUE;

        for (Map.Entry<N, Particle> row : nodes.entrySet()) {
          N node = row.getKey();
          Particle p = row.getValue();

          float px = p.position().x();
          float py = p.position().y();
          float dSq = (px - mX) * (px - mX) + (py - mY) * (py - mY);
          if (dSq < nearestDSq) {
            nearestDSq = dSq;
            selectedNode = node;
          }
        }
      }
    }
  }
 /**
  * Initialises the particle viewer.
  *
  * @param parent Parent sketch in which this viewer is to be drawn.
  */
 public ParticleViewer(PApplet parent, int width, int height) {
   this.parent = parent;
   zoomer = new ZoomPan(parent);
   zoomer.setMouseMask(PConstants.SHIFT);
   centroid = new Smoother3D(0.9f);
   physics = new ParticleSystem(0, 0.75f); // No gravity with .75 drag.
   nodes = new HashMap<N, Particle>();
   edges = new HashMap<E, Spring>();
   stakes = new HashMap<N, Particle>();
   tethers = new HashMap<Particle, Spring>();
   this.width = width;
   this.height = height;
   isPaused = false;
   selectedNode = null;
 }
 /** Resets the zoomed view to show the entire network. */
 public void resetView() {
   zoomer.reset();
 }