`
xiefeifeihu
  • 浏览: 97240 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

3x3矩阵类

J# 
阅读更多
   1:  package com.jme.math;
   2:   
   3:  import java.io.IOException;
   4:  import java.io.Serializable;
   5:  import java.nio.FloatBuffer;
   6:  import java.util.logging.Logger;
   7:   
   8:  import com.jme.system.JmeException;
   9:  import com.jme.util.export.InputCapsule;
  10:  import com.jme.util.export.JMEExporter;
  11:  import com.jme.util.export.JMEImporter;
  12:  import com.jme.util.export.OutputCapsule;
  13:  import com.jme.util.export.Savable;
  14:  import com.jme.util.geom.BufferUtils;
  15:   
  16:  /**
  17:   * <code>Matrix3f</code> defines a 3x3 matrix. Matrix data is maintained
  18:   * internally and is accessible via the get and set methods. Convenience methods
  19:   * are used for matrix operations as well as generating a matrix from a given
  20:   * set of values.<br/>
  21:   * 3X3矩阵类
  22:   * 
  23:   * @author Mark Powell
  24:   * @author Joshua Slack
  25:   */
  26:  public class Matrix3f implements Serializable, Savable, Cloneable {
  27:      private static final Logger logger = Logger.getLogger(Matrix3f.class
  28:              .getName());
  29:   
  30:      private static final long serialVersionUID = 1L;
  31:   
  32:      public float m00, m01, m02;
  33:      public float m10, m11, m12;
  34:      public float m20, m21, m22;
  35:   
  36:      /**
  37:       * Constructor instantiates a new <code>Matrix3f</code> object. The initial
  38:       * values for the matrix is that of the identity matrix.<br/>
  39:       * 默认构造单位矩阵
  40:       * 
  41:       */
  42:      public Matrix3f() {
  43:          loadIdentity();
  44:      }
  45:   
  46:      /**
  47:       * constructs a matrix with the given values.
  48:       * 
  49:       * @param m00
  50:       *            0x0 in the matrix.
  51:       * @param m01
  52:       *            0x1 in the matrix.
  53:       * @param m02
  54:       *            0x2 in the matrix.
  55:       * @param m10
  56:       *            1x0 in the matrix.
  57:       * @param m11
  58:       *            1x1 in the matrix.
  59:       * @param m12
  60:       *            1x2 in the matrix.
  61:       * @param m20
  62:       *            2x0 in the matrix.
  63:       * @param m21
  64:       *            2x1 in the matrix.
  65:       * @param m22
  66:       *            2x2 in the matrix.
  67:       */
  68:      public Matrix3f(float m00, float m01, float m02, float m10, float m11,
  69:              float m12, float m20, float m21, float m22) {
  70:   
  71:          this.m00 = m00;
  72:          this.m01 = m01;
  73:          this.m02 = m02;
  74:          this.m10 = m10;
  75:          this.m11 = m11;
  76:          this.m12 = m12;
  77:          this.m20 = m20;
  78:          this.m21 = m21;
  79:          this.m22 = m22;
  80:      }
  81:   
  82:      /**
  83:       * Copy constructor that creates a new <code>Matrix3f</code> object that is
  84:       * the same as the provided matrix.
  85:       * 
  86:       * @param mat
  87:       *            the matrix to copy.
  88:       */
  89:      public Matrix3f(Matrix3f mat) {
  90:          copy(mat);
  91:      }
  92:   
  93:      /**
  94:       * <code>copy</code> transfers the contents of a given matrix to this
  95:       * matrix. If a null matrix is supplied, this matrix is set to the identity
  96:       * matrix.
  97:       * 
  98:       * @param matrix
  99:       *            the matrix to copy.
 100:       */
 101:      public void copy(Matrix3f matrix) {
 102:          if (null == matrix) {
 103:              loadIdentity();
 104:          } else {
 105:              m00 = matrix.m00;
 106:              m01 = matrix.m01;
 107:              m02 = matrix.m02;
 108:              m10 = matrix.m10;
 109:              m11 = matrix.m11;
 110:              m12 = matrix.m12;
 111:              m20 = matrix.m20;
 112:              m21 = matrix.m21;
 113:              m22 = matrix.m22;
 114:          }
 115:      }
 116:   
 117:      /**
 118:       * <code>get</code> retrieves a value from the matrix at the given position.
 119:       * If the position is invalid a <code>JmeException</code> is thrown.<br/>
 120:       * 获取指定位置的元素值
 121:       * 
 122:       * @param i
 123:       *            the row index.行索引(取值范围:0、1、2)
 124:       * @param j
 125:       *            the colum index.列索引(取值范围:0、1、2)
 126:       * @return the value at (i, j).
 127:       */
 128:      public float get(int i, int j) {
 129:          switch (i) {
 130:          case 0:
 131:              switch (j) {
 132:              case 0:
 133:                  return m00;
 134:              case 1:
 135:                  return m01;
 136:              case 2:
 137:                  return m02;
 138:              }
 139:          case 1:
 140:              switch (j) {
 141:              case 0:
 142:                  return m10;
 143:              case 1:
 144:                  return m11;
 145:              case 2:
 146:                  return m12;
 147:              }
 148:          case 2:
 149:              switch (j) {
 150:              case 0:
 151:                  return m20;
 152:              case 1:
 153:                  return m21;
 154:              case 2:
 155:                  return m22;
 156:              }
 157:          }
 158:   
 159:          logger.warning("Invalid matrix index.");
 160:          throw new JmeException("Invalid indices into matrix.");
 161:      }
 162:   
 163:      /**
 164:       * <code>get(float[])</code> returns the matrix in row-major or column-major
 165:       * order.<br/>
 166:       * 将矩阵按行或列的顺序存入参数data数组中
 167:       * 
 168:       * @param data
 169:       *            The array to return the data into. This array can be 9 or 16
 170:       *            floats in size. Only the upper 3x3 are assigned to in the case
 171:       *            of a 16 element array.
 172:       * @param rowMajor
 173:       *            True for row major storage in the array (translation in
 174:       *            elements 3, 7, 11 for a 4x4), false for column major
 175:       *            (translation in elements 12, 13, 14 for a 4x4).
 176:       */
 177:      public void get(float[] data, boolean rowMajor) {
 178:          if (data.length == 9) {
 179:              if (rowMajor) {
 180:                  data[0] = m00;
 181:                  data[1] = m01;
 182:                  data[2] = m02;
 183:                  data[3] = m10;
 184:                  data[4] = m11;
 185:                  data[5] = m12;
 186:                  data[6] = m20;
 187:                  data[7] = m21;
 188:                  data[8] = m22;
 189:              } else {
 190:                  data[0] = m00;
 191:                  data[1] = m10;
 192:                  data[2] = m20;
 193:                  data[3] = m01;
 194:                  data[4] = m11;
 195:                  data[5] = m21;
 196:                  data[6] = m02;
 197:                  data[7] = m12;
 198:                  data[8] = m22;
 199:              }
 200:          } else if (data.length == 16) {
 201:              if (rowMajor) {
 202:                  data[0] = m00;
 203:                  data[1] = m01;
 204:                  data[2] = m02;
 205:                  data[4] = m10;
 206:                  data[5] = m11;
 207:                  data[6] = m12;
 208:                  data[8] = m20;
 209:                  data[9] = m21;
 210:                  data[10] = m22;
 211:              } else {
 212:                  data[0] = m00;
 213:                  data[1] = m10;
 214:                  data[2] = m20;
 215:                  data[4] = m01;
 216:                  data[5] = m11;
 217:                  data[6] = m21;
 218:                  data[8] = m02;
 219:                  data[9] = m12;
 220:                  data[10] = m22;
 221:              }
 222:          } else {
 223:              throw new JmeException(
 224:                      "Array size must be 9 or 16 in Matrix3f.get().");
 225:          }
 226:      }
 227:   
 228:      /**
 229:       * <code>getColumn</code> returns one of three columns specified by the
 230:       * parameter. This column is returned as a <code>Vector3f</code> object.<br/>
 231:       * 获取指定列构造的向量
 232:       * 
 233:       * @param i
 234:       *            the column to retrieve. Must be between 0 and 2.
 235:       * @return the column specified by the index.
 236:       */
 237:      public Vector3f getColumn(int i) {
 238:          return getColumn(i, null);
 239:      }
 240:   
 241:      /**
 242:       * <code>getColumn</code> returns one of three columns specified by the
 243:       * parameter. This column is returned as a <code>Vector3f</code> object.<br/>
 244:       * 获取指定列构造的向量
 245:       * 
 246:       * @param i
 247:       *            the column to retrieve. Must be between 0 and 2.
 248:       * @param store
 249:       *            the vector object to store the result in. if null, a new one
 250:       *            is created.
 251:       * @return the column specified by the index.
 252:       */
 253:      public Vector3f getColumn(int i, Vector3f store) {
 254:          if (store == null)
 255:              store = new Vector3f();
 256:          switch (i) {
 257:      

  


  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics