Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.22 KB | None | 0 0
  1.  
  2. // MarkerModel.java
  3. // Andrew Davison, ad@fivedots.coe.psu.ac.th, July 2013
  4.  
  5. /* Holds NYARToolkit marker information and the Java3D scene graph for its
  6.    associated model.
  7.  
  8.    The model is loaded using the PropManager class, which is described in Chapter 16
  9.    of "Killer Game Programming in Java" (http://fivedots.coe.psu.ac.th/~ad/jg/ch9/)
  10. */
  11.  
  12. import java.io.*;
  13. import javax.media.j3d.*;
  14. import javax.vecmath.*;
  15. import jp.nyatla.nyartoolkit.core.*;
  16. import jp.nyatla.nyartoolkit.core.types.matrix.*;
  17.  
  18.  
  19.  
  20. public class MarkerModel
  21. {
  22.  
  23.   private final String MARKER_DIR = "C:/Users/LIRKIS PC/Desktop/pokusNytoolkit/src/data/";
  24.   private final double MARKER_SIZE = 0.095;    // 95 cm width and height in Java 3D world units
  25.  
  26.  
  27.   private String markerName, modelName;
  28.   private NyARCode markerInfo = null;     // NYArToolkit marker details
  29.  
  30.   private TransformGroup moveTg;      // for moving the marker model
  31.  
  32.   private Switch visSwitch;           // for changing the model's visibility
  33.   private boolean isVisible;
  34.  
  35.   private SmoothMatrix sMat;          // for smoothing the transforms applied to the model
  36.  
  37.   // details about a model's position and orientation (in degrees)
  38.   private Point3d posInfo = null;
  39.   private Point3d rotsInfo = null;
  40.  
  41.   private int numTimesLost = 0;   // number of times marker for this model not detected
  42.  
  43.  
  44.  
  45.   public MarkerModel(String markerFnm, String modelFnm, double scale, boolean hasCoords)
  46.   {
  47.     markerName = markerFnm;
  48.     modelName = modelFnm.substring(0, modelFnm.lastIndexOf('.'));   // remove filename extension
  49.  
  50.     // build a branch for the model: TG --> Switch --> model
  51.  
  52.     // load the model, with scale and coords info
  53.     TransformGroup modelTG = loadModel(modelFnm, scale, hasCoords);
  54.  
  55.     // create switch for model visibility
  56.     visSwitch = new Switch();
  57.     visSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
  58.     visSwitch.addChild( modelTG );
  59.     visSwitch.setWhichChild( Switch.CHILD_NONE );   // make invisible
  60.     isVisible = false;
  61.  
  62.     // create transform group for positioning the model
  63.     moveTg = new TransformGroup();
  64.     moveTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);  // so this tg can change
  65.     moveTg.addChild(visSwitch);
  66.  
  67.     // load marker info
  68.     try {
  69.       markerInfo = NyARCode.createFromARPattFile(
  70.                           new FileInputStream(MARKER_DIR+markerName),16, 16);
  71.     }
  72.     catch(Exception e)
  73.     {  System.out.println(e);  
  74.        markerInfo = null;
  75.     }
  76.  
  77.     sMat = new SmoothMatrix();
  78.   }  // end of MarkerModel()
  79.  
  80.  
  81.  
  82.  
  83.   private TransformGroup loadModel(String modelFnm, double scale, boolean hasCoords)
  84.   // load the model, rotating and scaling it
  85.   {
  86.     PropManager propMan = new PropManager(modelFnm, hasCoords);  
  87.  
  88.     // get the TG for the prop (model)
  89.     TransformGroup propTG = propMan.getTG();
  90.  
  91.     // rotate and scale the prop
  92.     Transform3D modelT3d = new Transform3D();
  93.     modelT3d.rotX( Math.PI/2.0 );    
  94.          // the prop lies flat on the marker; rotate forwards 90 degrees so it is standing
  95.     Vector3d scaleVec = calcScaleFactor(propTG, scale);   // scale the prop
  96.     modelT3d.setScale( scaleVec );
  97.  
  98.     TransformGroup modelTG = new TransformGroup(modelT3d);
  99.     modelTG.addChild(propTG);
  100.  
  101.     return modelTG;
  102.   }  // end of loadModel()
  103.  
  104.  
  105.  
  106.   private Vector3d calcScaleFactor(TransformGroup modelTG, double scale)
  107.   // Scale the prop based on its original bounding box size
  108.   {
  109.      BoundingBox boundbox = new BoundingBox( modelTG.getBounds() );
  110.      System.out.println(boundbox);
  111.  
  112.      // obtain the upper and lower coordinates of the box
  113.      Point3d lower = new Point3d();
  114.      boundbox.getLower( lower );
  115.      Point3d upper = new Point3d();
  116.      boundbox.getUpper( upper );
  117.  
  118.      // store the largest X, Y, or Z dimension and calculate a scale factor
  119.      double max = 0.0;
  120.      if( Math.abs(upper.x - lower.x) > max)
  121.        max = Math.abs(upper.x - lower.x);
  122.  
  123.      if( Math.abs(upper.y - lower.y) > max)
  124.        max = Math.abs(upper.y - lower.y);
  125.  
  126.      if( Math.abs(upper.z - lower.z) > max)
  127.        max = Math.abs(upper.z - lower.z);
  128.  
  129.      double scaleFactor = scale/max;
  130.      System.out.printf("max dimension: %.3f;  scale factor: %.3f\n", max, scaleFactor);
  131.  
  132.      // limit the scaling so that a big model isn't scaled too much
  133.      if( scaleFactor < 0.0005 )
  134.          scaleFactor = 0.0005;
  135.  
  136.      return new Vector3d(scaleFactor, scaleFactor, scaleFactor);
  137.   }  // end of calcScaleFactor()
  138.  
  139.  
  140.  
  141.   public String getNameInfo()
  142.   { return markerName + " / " + modelName; }
  143.  
  144.   public NyARCode getMarkerInfo()
  145.   {  return markerInfo;  }
  146.  
  147.   public double getMarkerWidth()
  148.   {  return MARKER_SIZE;   }
  149.  
  150.  
  151.   public TransformGroup getMoveTg()
  152.   {  return moveTg;  }
  153.  
  154.  
  155.   public void moveModel(NyARDoubleMatrix44 transMat)
  156.   // detected marker so update model's moveTG
  157.   {
  158.     visSwitch.setWhichChild( Switch.CHILD_ALL );   // make visible
  159.     isVisible = true;
  160.  
  161.     sMat.add(transMat);
  162.  
  163.     Matrix4d mat = sMat.get();
  164.     Transform3D t3d = new Transform3D(mat);
  165.  
  166.     int flags = t3d.getType();
  167.     if ((flags & Transform3D.AFFINE) == 0)
  168.       System.out.println("Ignoring non-affine transformation");
  169.     else {
  170.       if (moveTg != null)
  171.         moveTg.setTransform(t3d);
  172.  
  173.       // System.out.println("transformation matrix: " + mat);
  174.       calcPosition(mat);
  175.       calcEulerRots(mat);
  176.     }
  177.   }  // end of moveModel()
  178.  
  179.  
  180.  
  181.  
  182.   private void calcPosition(Matrix4d mat)
  183.   // extract the (x,y,z) position vals stored in the matrix
  184.   {
  185.     // convert to cm and round
  186.     double x = roundToNumPlaces( mat.getElement(0,3)*100, 1);
  187.     double y = roundToNumPlaces( mat.getElement(1,3)*100, 1);
  188.     double z = roundToNumPlaces( mat.getElement(2,3)*100, 1);
  189.     // System.out.println(getNameInfo() + " (" + x + ", " + y + ", " + z + ")");
  190.     posInfo = new Point3d(x, y, z);
  191.   }  // end of reportPosition()
  192.  
  193.  
  194.  
  195.   private double roundToNumPlaces(double val, int numPlaces)
  196.   {
  197.     double power = Math.pow(10, numPlaces);
  198.     long temp = Math.round(val*power);
  199.     return temp/power;
  200.   }
  201.  
  202.  
  203.   public Point3d getPos()
  204.   {  return posInfo;  }
  205.  
  206.  
  207.  
  208.   private void calcEulerRots(Matrix4d mat)
  209.   /* calculate the Euler rotation angles from the upper 3x3 rotation
  210.      components of the 4x4 transformation matrix.
  211.      Based on code by Daniel Selman, December 1999
  212.      which is based on pseudo-code in "Matrix and Quaternion FAQ", Q37
  213.        http://www.j3d.org/matrix_faq/matrfaq_latest.html
  214.   */
  215.   {
  216.     rotsInfo = new Point3d();
  217.  
  218.     rotsInfo.y = -Math.asin(mat.getElement(2,0));
  219.     double c = Math.cos(rotsInfo.y);
  220.  
  221.     double tRx, tRy, tRz;
  222.     if(Math.abs(rotsInfo.y) > 0.00001) {
  223.       tRx = mat.getElement(2,2)/c;
  224.       tRy = -mat.getElement(2,1)/c;
  225.       rotsInfo.x = Math.atan2(tRy, tRx);
  226.  
  227.       tRx = mat.getElement(0,0)/c;
  228.       tRy = -mat.getElement(1,0)/c;
  229.       rotsInfo.z = Math.atan2(tRy, tRx);
  230.     }
  231.     else {
  232.       rotsInfo.x  = 0.0;
  233.  
  234.       tRx = mat.getElement(1,1);
  235.       tRy = mat.getElement(0,1);
  236.       rotsInfo.z = Math.atan2(tRy, tRx);
  237.     }
  238.  
  239.     rotsInfo.x = -rotsInfo.x;
  240.     rotsInfo.z = -rotsInfo.z;
  241.  
  242.     // ensure the values are positive by adding 2*PI if necessary...
  243.     if(rotsInfo.x < 0.0)
  244.     rotsInfo.x += 2*Math.PI;
  245.  
  246.     if(rotsInfo.y < 0.0)
  247.       rotsInfo.y += 2*Math.PI;
  248.  
  249.     if(rotsInfo.z < 0.0)
  250.       rotsInfo.z += 2*Math.PI;
  251.  
  252.     // convert to degrees and round
  253.     rotsInfo.x = roundToNumPlaces( Math.toDegrees(rotsInfo.x), 0);
  254.     rotsInfo.y = roundToNumPlaces( Math.toDegrees(rotsInfo.y), 0);
  255.     rotsInfo.z = roundToNumPlaces( Math.toDegrees(rotsInfo.z), 0);
  256.  
  257.     // System.out.println(getNameInfo() + " rots (" +
  258.     //         rotsInfo.x + ", " + rotsInfo.y + ", " + rotsInfo.z + ")");
  259.   }  // end of calcEulerRots()
  260.  
  261.  
  262.   public Point3d getRots()
  263.   {  return rotsInfo; }
  264.  
  265.  
  266.  
  267.   public void resetNumTimesLost()
  268.   {  numTimesLost = 0;  }
  269.  
  270.   public void incrNumTimesLost()
  271.   {  numTimesLost++;  }
  272.  
  273.   public int getNumTimesLost()
  274.   {  return numTimesLost;  }
  275.  
  276.  
  277.  
  278.   public void hideModel()
  279.   {
  280.     visSwitch.setWhichChild( Switch.CHILD_NONE );   // make model invisible
  281.     isVisible = false;
  282.   }
  283.  
  284.   public boolean isVisible()
  285.   {  return isVisible; }
  286.  
  287.  
  288. }  // end of MarkerModel class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement