⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propmanager.java

📁 java 3D
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }
    if (count != 3)
      System.out.println("Insufficient position data in coords file");

    // apply the moves to the loaded object
    doMove( new Vector3d( vals[0], vals[1], vals[2]) );

  }  // end of setCurrentPosn()



  private void setCurrentRotation(String line)
  // extract the rotation info from the coords file,
  // and apply it to the loaded object
  {
    int rotNum;
    StringTokenizer tokens = new StringTokenizer(line);
    String token = tokens.nextToken();    // skip command label
    if (!tokens.hasMoreTokens())   // there may not be any rotation numbers
      return;
    token = tokens.nextToken();
    for (int i=0; i < token.length(); i++) {
      try {
        rotNum = Character.digit(token.charAt(i),10);
        // rotInfo.add( new Integer(rotNum));
      }
      catch (NumberFormatException ex){ 
        System.out.println("Incorrect format for rotation data in coords file"); 
        break;
      }
      if (rotNum == 1)         // positive x-axis rotation
        rotate(X_AXIS, INCR);   
      else if (rotNum == 2)    // negative
        rotate(X_AXIS, DECR);
      else if (rotNum == 3)    // positive y-axis rotation
        rotate(Y_AXIS, INCR);   
      else if (rotNum == 4)    // negative
        rotate(Y_AXIS, DECR);
      else if (rotNum == 5)    // positive z-axis rotation
        rotate(Z_AXIS, INCR);   
      else if (rotNum == 6)    // negative
        rotate(Z_AXIS, DECR);
      else
        System.out.println("Did not recognise the rotation info in the coords file");
    }
  }  // end of setCurrentRotation()




  private void setCurrentScale(String line)
  // extract the scale info from the coords file,
  // and apply it to the loaded object
  {
    StringTokenizer tokens = new StringTokenizer(line);
    String token = tokens.nextToken();    // skip command label
    double startScale;

    token = tokens.nextToken();    // should be the scale value
    try {
      startScale = Double.parseDouble(token);
    }
    catch (NumberFormatException ex){ 
      System.out.println("Incorrect format for scale data in coords file");
      startScale = 1.0;
    }
    // System.out.println("Loaded start scale: " + startScale);
    if (startScale != 1.0) {
      scale(startScale);
    }
  }  // end of setCurrentScale()



  //---------------------------------------------------------
  // modify the position/rotation/scale of the loaded object

  /* These methods are called when applying the coords file
     information *and* when user commands sent from the GUI 
     are being processed.
  */


  public void move(int axis, int change)
  // move the object along an axis
  {
    double moveStep = (change == INCR) ? MOVE_INCR : -MOVE_INCR ;
    Vector3d moveVec;
    if (axis == X_AXIS)
      moveVec = new Vector3d(moveStep,0,0);
    else if (axis == Y_AXIS)
      moveVec = new Vector3d(0,moveStep,0);
    else   // Z_AXIS
      moveVec = new Vector3d(0,0,moveStep);
    doMove( moveVec );
  }  // end of move()


  private void doMove(Vector3d theMove)
  // move the object by theMove amount
  {
    moveTG.getTransform(t3d);        // get current position from TG
    chgT3d.setIdentity();            // reset change Trans
    chgT3d.setTranslation(theMove);  // setup move
    t3d.mul(chgT3d);                 // 'add' move to current position
    moveTG.setTransform(t3d);        // update TG
  }  // end of doMove()



  public void rotate(int axis, int change)
  // rotate the object about an axis, and remember the change
  {
    doRotate(axis, change);
    storeRotate(axis, change);
  }  // end of rotate()


  private void doRotate(int axis, int change)
  // rotate the object about the axis by radians amount
  {
    double radians = (change == INCR) ? ROT_AMT : -ROT_AMT;
    rotTG.getTransform(t3d);     // get current rotation from TG
    chgT3d.setIdentity();        // reset change Trans
    switch (axis) {              // setup new rotation
      case X_AXIS: chgT3d.rotX(radians); break;
      case Y_AXIS: chgT3d.rotY(radians); break;
      case Z_AXIS: chgT3d.rotZ(radians); break;
      default: System.out.println("Unknown axis of rotation"); break;
    }
    t3d.mul(chgT3d);     // 'add' new rotation to current one
    rotTG.setTransform(t3d);     // update the TG
  }  // end of doRotate()


  private void storeRotate(int axis, int change)
  // store the rotation information
  {
    double degrees = (change == INCR) ? ROT_INCR : -ROT_INCR;
    switch (axis) {
      case X_AXIS: storeRotateX(degrees); break;
      case Y_AXIS: storeRotateY(degrees); break;
      case Z_AXIS: storeRotateZ(degrees); break;
      default: System.out.println("Unknown storage axis of rotation"); break;
    }
  }  // end of storeRotate() 


  private void storeRotateX(double degrees)
  // record the x-axis rotation
  {
    xRot = (xRot+degrees)%360;   // update x-axis total rotation
    if (degrees == ROT_INCR)
      rotInfo.add(new Integer(1));  // rotation number
    else if (degrees == -ROT_INCR)
       rotInfo.add(new Integer(2));
    else 
      System.out.println("No X-axis rotation number for " + degrees);
  } // end of storeRotateX()


  private void storeRotateY(double degrees)
  // record the y-axis rotation
  {
    yRot = (yRot+degrees)%360;   // update y-axis total rotation
    if (degrees == ROT_INCR)
      rotInfo.add(new Integer(3));  // rotation number
    else if (degrees == -ROT_INCR)
       rotInfo.add(new Integer(4));
    else 
      System.out.println("No Y-axis rotation number for " + degrees);
  } // end of storeRotateY()


  private void storeRotateZ(double degrees)
  // record the z-axis rotation
  {
    zRot = (zRot+degrees)%360;   // update z-axis total rotation
    if (degrees == ROT_INCR)
      rotInfo.add(new Integer(5));  // rotation number
    else if (degrees == -ROT_INCR)
       rotInfo.add(new Integer(6));
    else 
      System.out.println("No Z-axis rotation number for " + degrees);
  } // end of storeRotateZ()


  public void scale(double d)
  // Scale the object by d units
  {
    scaleTG.getTransform(t3d);    // get current scale from TG
    chgT3d.setIdentity();         // reset change Trans
    chgT3d.setScale(d);           // set up new scale
    t3d.mul(chgT3d);              // multiply new scale to current one
	scaleTG.setTransform(t3d);    // update the TG

    scale *= d;    // update scale variable
  }  // end of scale()



  // ----------------------------------------------------------
  // return current position/rotation/scale information
  // Used by the GUI interface


  public Vector3d getLoc()
  { 
    moveTG.getTransform(t3d);
    Vector3d trans = new Vector3d();
    t3d.get(trans);
    // printTuple(trans, "getLoc");
    return trans;
  } // end of getLoc()


  public Point3d getRotations()
  {  return new Point3d(xRot, yRot, zRot);  }

  public double getScale()
  {  return scale;  }


  // ------------------------------ storing ---------------------------


  public void saveCoordFile()
  // create a coords file for this object
  {
    String coordFnm = "models/" + getName(filename) + "Coords.txt";
    try {
      PrintWriter out = new PrintWriter( new FileWriter(coordFnm));

      out.println(filename);     // object filename
      Vector3d currLoc = getLoc();
      out.println("-p " + df.format(currLoc.x) + " " + df.format(currLoc.y) +
						" " + df.format(currLoc.z) );
      out.print("-r ");
      for (int i=0; i < rotInfo.size(); i++)
         out.print( ""+((Integer) rotInfo.get(i)).intValue() );
      out.println("");

      out.println("-s " + df.format(scale) );

      out.close();
      System.out.println("Saved to coord file: " + coordFnm);
    }
    catch(IOException e)
    { System.out.println("Error writing to coord file: " + coordFnm); }
  }  // end of saveCoordFile()



  // --------------------- methods used for debugging --------------------------


  private void printTG(TransformGroup tg, String id)
  // print the translation stored in tg
  {
    Transform3D currt3d = new Transform3D( );
    tg.getTransform( currt3d );
    Vector3d currTrans = new Vector3d( );
    currt3d.get( currTrans );
    printTuple( currTrans, id);
  }  // end of printTG()


  private void printTuple(Tuple3d t, String id)
  // used to print Vector3d, Point3d objects
  {
    System.out.println(id + " x: " + df.format(t.x) + 
				", " + id + " y: " + df.format(t.y) +
				", " + id + " z: " + df.format(t.z));
  }  // end of printTuple()


}  // end of PropManager class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -