Exemple #1
0
  public void toAt(Vector cam, Vector obj) {
    if (cam == null || obj == null) throw new NullPointerException();

    dir.set(0, 0, 0);
    dir.set(obj);
    dir.sub(cam);
    dir.mult(-1f);
    dir.norm();
    dir.get(dirArray);

    right.set(0, 0, 0);
    right.cross(worldUp, dir);
    right.norm();
    right.get(rightArray);

    up.set(0, 0, 0);
    up.cross(dir, right);
    up.norm();
    up.get(upArray);

    set(
        rightArray[0],
        rightArray[1],
        rightArray[2],
        upArray[0],
        upArray[1],
        upArray[2],
        dirArray[0],
        dirArray[1],
        dirArray[2]);
  }
 public void testGetDistanceSquared() {
   Vector other = new RandomAccessSparseVector(test.size());
   other.set(1, -2);
   other.set(2, -5);
   other.set(3, -9);
   other.set(4, 1);
   double expected = test.minus(other).getLengthSquared();
   assertTrue(
       "a.getDistanceSquared(b) != a.minus(b).getLengthSquared",
       Math.abs(expected - test.getDistanceSquared(other)) < 10.0E-7);
 }
  @Test
  public void testFindLargestCommonTermID() {
    // vector components must always be sorted descending
    Vector v1 = new Vector();
    v1.set(
        1,
        new VectorComponentArrayWritable(
            new VectorComponent[] {new VectorComponent(3, 0), new VectorComponent(1, 0)}));
    Vector v2 = new Vector();
    v2.set(
        2,
        new VectorComponentArrayWritable(
            new VectorComponent[] {new VectorComponent(4, 0), new VectorComponent(2, 0)}));
    assertEquals(-1, Vector.findSmallestCommonTermID(v1, v2));
    v2.set(
        2,
        new VectorComponentArrayWritable(
            new VectorComponent[] {new VectorComponent(3, 0), new VectorComponent(2, 0)}));
    assertEquals(3, Vector.findSmallestCommonTermID(v1, v2));
    v2.set(
        2,
        new VectorComponentArrayWritable(
            new VectorComponent[] {new VectorComponent(5, 0), new VectorComponent(1, 0)}));
    assertEquals(1, Vector.findSmallestCommonTermID(v1, v2));
    v2.set(
        2,
        new VectorComponentArrayWritable(
            new VectorComponent[] {
              new VectorComponent(5, 0), new VectorComponent(4, 0), new VectorComponent(1, 0)
            }));
    assertEquals(1, Vector.findSmallestCommonTermID(v1, v2));

    v1.set(
        1,
        new VectorComponentArrayWritable(
            new VectorComponent[] {
              new VectorComponent(5, 0),
              new VectorComponent(3, 0),
              new VectorComponent(2, 0),
              new VectorComponent(1, 0)
            }));
    v2.set(
        2,
        new VectorComponentArrayWritable(
            new VectorComponent[] {
              new VectorComponent(6, 0),
              new VectorComponent(4, 0),
              new VectorComponent(3, 0),
              new VectorComponent(1, 0)
            }));
    assertEquals(1, Vector.findSmallestCommonTermID(v1, v2));
    v2.set(
        2,
        new VectorComponentArrayWritable(
            new VectorComponent[] {
              new VectorComponent(6, 0), new VectorComponent(4, 0), new VectorComponent(2, 0)
            }));
    assertEquals(2, Vector.findSmallestCommonTermID(v1, v2));
  }
  private Vector addAnchorString(Vector list, String field, double diff) {
    Vector topIndex = new Vector();
    Hashtable topIndexParms, currEntry;
    double lastValue, currValue;

    if (list.size() == 0) return null;

    currEntry = (Hashtable) list.get(0);
    lastValue = Common.parseDouble((String) currEntry.get(field)) + diff;

    for (int i = 1; i < list.size(); i++) {
      currEntry = (Hashtable) list.get(i);
      currValue = Common.parseDouble((String) currEntry.get(field));
      if (currValue >= lastValue) {
        // Values for navigation line
        topIndexParms = new Hashtable();
        topIndexParms.put("HREF", Convert.toString(i));
        topIndexParms.put("TEXT", Convert.toString(lastValue));
        topIndex.add(topIndexParms);
        // add anchor entry to list
        currEntry.put("ANCHORNAME", Convert.toString(i));
        currEntry.put("ANCHORTEXT", Convert.toString(lastValue));
        lastValue = currValue + diff;
      } else {
        // clear value from previous run
        currEntry.put("ANCHORNAME", "");
        currEntry.put("ANCHORTEXT", "");
      }
      list.set(i, currEntry);
    }
    return topIndex;
  }
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   test = generateTestVector(2 * values.length + 1);
   for (int i = 0; i < values.length; i++) {
     test.set(2 * i + 1, values[i]);
   }
 }
Exemple #6
0
  /**
   * Creates a copy of a given {@code Vector}.
   *
   * @param source The {@code Vector} to copy.
   * @return A copy of {@code source} with the same type.
   */
  public static Vector copyOf(Vector source) {
    if (source instanceof DoubleVector) return copyOf((DoubleVector) source);
    if (source instanceof IntegerVector) return copyOf((IntegerVector) source);

    Vector result = new DenseVector(source.length());
    for (int i = 0; i < source.length(); ++i) result.set(i, source.getValue(i));
    return result;
  }
Exemple #7
0
  @Override
  public Vector transform(int i, VectorFunction function, Factory factory) {

    Vector result = copy(factory);
    result.set(i, function.evaluate(i, get(i)));

    return result;
  }
  private Vector addAnchorString(Vector list, String field, boolean fullCompare) {
    Vector topIndex = new Vector();
    Hashtable topIndexParms, currEntry;
    String lastValue, currValue;

    if (list.size() == 0) return null;

    currEntry = (Hashtable) list.get(0);
    lastValue = (String) currEntry.get(field);
    if (lastValue == null || lastValue.length() == 0) lastValue = "  ";
    lastValue = lastValue.toUpperCase();

    for (int i = 1; i < list.size(); i++) {
      currEntry = (Hashtable) list.get(i);
      currValue = (String) currEntry.get(field);
      currValue = currValue.toUpperCase();
      if (currValue == null || currValue == "") continue;
      try {
        if (fullCompare) {
          if (lastValue.compareTo(currValue) != 0) {
            // Values for navigation line
            topIndexParms = new Hashtable();
            topIndexParms.put("HREF", Convert.toString(i));
            topIndexParms.put("TEXT", currValue);
            topIndex.add(topIndexParms);
            // add anchor entry to list
            currEntry.put("ANCHORNAME", Convert.toString(i));
            currEntry.put("ANCHORTEXT", currValue);
          } else {
            // clear value from previous run
            currEntry.put("ANCHORNAME", "");
            currEntry.put("ANCHORTEXT", "");
          }
        } else {
          if (lastValue.charAt(0) != currValue.charAt(0)) {
            // Values for navigation line
            topIndexParms = new Hashtable();
            topIndexParms.put("HREF", Convert.toString(i));
            topIndexParms.put("TEXT", currValue.charAt(0) + " ");
            topIndex.add(topIndexParms);
            // add anchor entry to list
            currEntry.put("ANCHORNAME", Convert.toString(i));
            currEntry.put("ANCHORTEXT", currValue.charAt(0) + " ");
          } else {
            // clear value from previous run
            currEntry.put("ANCHORNAME", "");
            currEntry.put("ANCHORTEXT", "");
          }
        }
        list.set(i, currEntry);
        lastValue = currValue;
      } catch (Exception e) {
        continue;
      }
    }
    return topIndex;
  }
  /**
   * Calculate and populate the Azimuth, Pitch, and Roll.
   *
   * @param rotationMatrix Rotation matrix used in calculations.
   */
  public static synchronized void calcPitchBearing(Matrix rotationMatrix) {
    if (rotationMatrix == null) return;

    tempMatrix.set(rotationMatrix);
    tempMatrix.transpose();
    if (AugmentedReality.portrait) {
      looking.set(0, 1, 0);
    } else {
      looking.set(1, 0, 0);
    }
    looking.prod(tempMatrix);
    looking.get(lookingArray);
    Calculator.azimuth = ((getAngle(0, 0, lookingArray[0], lookingArray[2]) + 360) % 360);
    Calculator.roll = -(90 - Math.abs(getAngle(0, 0, lookingArray[1], lookingArray[2])));
    looking.set(0, 0, 1);
    looking.prod(tempMatrix);
    looking.get(lookingArray);
    Calculator.pitch = -(90 - Math.abs(getAngle(0, 0, lookingArray[1], lookingArray[2])));
  }
Exemple #10
0
  @Override
  public Vector transform(VectorFunction function, Factory factory) {

    Vector result = blank(factory);

    for (int i = 0; i < length; i++) {
      result.set(i, function.evaluate(i, get(i)));
    }

    return result;
  }
 public void testSet() throws Exception {
   test.set(3, 4.5);
   for (int i = 0; i < test.size(); i++) {
     if (i % 2 == 0) {
       assertEquals("get [" + i + ']', 0.0, test.get(i));
     } else if (i == 3) {
       assertEquals("set [" + i + ']', 4.5, test.get(i));
     } else {
       assertEquals("set [" + i + ']', values[i / 2], test.get(i));
     }
   }
 }
Exemple #12
0
  @Override
  public Vector slice(int from, int until, Factory factory) {
    ensureFactoryIsNotNull(factory);

    Vector result = factory.createVector(until - from);

    for (int i = from; i < until; i++) {
      result.set(i - from, get(i));
    }

    return result;
  }
Exemple #13
0
  @Override
  public Vector add(double value, Factory factory) {
    ensureFactoryIsNotNull(factory);

    Vector result = blank(factory);

    for (int i = 0; i < length; i++) {
      result.set(i, get(i) + value);
    }

    return result;
  }
Exemple #14
0
  @Override
  public Vector resize(int length, Factory factory) {
    ensureFactoryIsNotNull(factory);

    Vector result = factory.createVector(length);

    for (int i = 0; i < Math.min(length, this.length); i++) {
      result.set(i, get(i));
    }

    return result;
  }
Exemple #15
0
  /**
   * Calculate and populate the Azimuth, Pitch, and Roll.
   *
   * @param rotationMatrix Rotation matrix used in calculations.
   */
  public static void calcPitchBearing(Matrix rotationMatrix) {
    if (rotationMatrix == null) return;

    tempMatrix.set(rotationMatrix);
    tempMatrix.transpose();

    float x = 0;
    float y = 0;
    int angle = ARData.getDeviceOrientationAngle();
    if (angle >= 0 && angle < 90) {
      x = (angle * unitPerDegree) - 1;
      y = 1 - (angle * unitPerDegree);
    } else if (angle >= 90 && angle < 180) {
      angle -= 90;
      x = (angle * unitPerDegree) - 1;
      y = (angle * unitPerDegree) - 1;
    } else if (angle >= 180 && angle < 270) {
      angle -= 180;
      x = 1 - (angle * unitPerDegree);
      y = (angle * unitPerDegree) - 1;
    } else {
      // >= 270 && < 360
      angle -= 270;
      x = 1 - (angle * unitPerDegree);
      y = 1 - (angle * unitPerDegree);
    }
    looking.set(x, y, 0);
    looking.prod(tempMatrix);
    looking.get(lookingArray);

    azimuth = ((getAngle(0, 0, lookingArray[0], lookingArray[2]) + 360) % 360);

    roll = -(90 - Math.abs(getAngle(0, 0, lookingArray[1], lookingArray[2])));

    looking.set(0, 0, 1);
    looking.prod(tempMatrix);
    looking.get(lookingArray);
    pitch = -(90 - Math.abs(getAngle(0, 0, lookingArray[1], lookingArray[2])));
  }
Exemple #16
0
  private List<? extends WeightedVector> cubishTestData(double radius) {
    List<WeightedVector> data = Lists.newArrayListWithCapacity(K1 + 5000);
    int row = 0;

    MultiNormal g = new MultiNormal(radius, new ConstantVector(0, 10));
    for (int i = 0; i < K1; i++) {
      data.add(new WeightedVector(g.sample(), 1, row++));
    }

    for (int i = 0; i < 5; i++) {
      Vector m = new DenseVector(10);
      m.set(i, i == 0 ? 6 : 6);
      MultiNormal gx = new MultiNormal(radius, m);
      for (int j = 0; j < 1000; j++) {
        data.add(new WeightedVector(gx.sample(), 1, row++));
      }
    }
    return data;
  }
Exemple #17
0
  @Override
  public Vector add(Vector vector, Factory factory) {
    ensureFactoryIsNotNull(factory);

    if (vector == null) {
      throw new IllegalArgumentException("Vector can't be null.");
    }

    if (length != vector.length()) {
      throw new IllegalArgumentException("Worong vector length: " + vector.length());
    }

    Vector result = blank(factory);

    for (int i = 0; i < length; i++) {
      result.set(i, get(i) + vector.get(i));
    }

    return result;
  }
  private void calculateVectorOperations(TextView tv) {
    String text = "Vector operations:\n";

    Vector vec1 = new Vector(3);
    vec1.set(0, 3.0f);
    vec1.set(1, -1.0f);
    vec1.set(2, 5.0f);

    text += "Vec1: " + vec1.toString() + "\n";

    Vector vec1Copy = new Vector(vec1);
    vec1Copy.multipleByScalar(5.0f);
    text += "3 * Vec1: " + vec1Copy.toString() + "\n";

    Vector vec2 = new Vector(4);
    vec2.set(0, -2.0f);
    vec2.set(1, 4.0f);
    vec2.set(2, 7.0f);
    vec2.set(3, -9.0f);

    text += "Vec2: " + vec2.toString() + "\n";

    tv.append(text);
  }
  @Override
  public final BoundingBox getBounds() {
    BoundingBox boundingBox = BoundingBox.newInstance();

    double minimum = Double.MAX_VALUE;

    Vector u = Vector.newInstance(0.0D, 0.0D, 0.0D);
    Vector v;
    Vector d = Vector.newInstance(0.0D, 0.0D, 0.0D);

    int size = vectors.size();

    for (int i = 0; i < size; i++) {
      Vector vector1 = vectors.get((i + 1) % size);
      Vector vector2 = vectors.get(i);

      u.set(vector1);
      u.subtract(vector2);
      u.normalize();

      v = Vector.toCrossProduct(getNormal(), u);
      v.normalize();

      double uMaximum = 0.0D;
      double uMinimum = 0.0D;
      double vMaximum = 0.0D;
      double vMinimum = 0.0D;

      for (int j = 0; j < size; j++) {
        if (i != j) {
          Vector vector3 = vectors.get(j);

          d.set(vector3);
          d.subtract(vector2);

          double uLength = d.getDotProduct(u);
          double vLength = d.getDotProduct(v);

          uMaximum = Math.max(uLength, uMaximum);
          uMinimum = Math.min(uLength, uMinimum);
          vMaximum = Math.max(vLength, vMaximum);
          vMinimum = Math.min(vLength, vMinimum);
        }
      }

      double current = (uMaximum - uMinimum) * (vMaximum - vMinimum);

      if (current < minimum) {
        minimum = current;

        Vector origin = boundingBox.getOrigin();

        origin.set(vector2);

        d.set(u);
        d.multiply(uMinimum);

        origin.add(d);

        d.set(v);
        d.multiply(vMinimum);

        origin.add(d);

        boundingBox.getU().set(u);
        boundingBox.getV().set(v);
        boundingBox.setHeight(vMaximum - vMinimum);
        boundingBox.setWidth(uMaximum - uMinimum);
      }
    }

    return boundingBox;
  }
Exemple #20
0
 /**
  * Copies all of the values from one {@code Vector} into another. After the operation, all of the
  * values in {@code dest} will be the same as that of {@code source}. The legnth of {@code dest}
  * must be as long as the length of {@code source}. Once completed {@code dest} is returned.
  *
  * @param dest The {@code Vector} to copy values into.
  * @param source The {@code Vector} to copy values from.
  * @return {@code dest} after being copied from {@code source}.
  * @throws IllegalArgumentException if the length of {@code dest} is less than that of {@code
  *     source}.
  */
 public static Vector copy(Vector dest, Vector source) {
   for (int i = 0; i < source.length(); ++i) dest.set(i, source.getValue(i).doubleValue());
   return dest;
 }
Exemple #21
0
 public static void unCheckedCrossPorduct3D(Vector A, Vector B, Vector C) {
   C.set(0, A.get(1) * B.get(2) - A.get(2) * B.get(1));
   C.set(1, A.get(2) * B.get(0) - A.get(0) * B.get(2));
   C.set(2, A.get(0) * B.get(1) - A.get(1) * B.get(0));
 }
 public void testDot2() throws CloneNotSupportedException {
   Vector test2 = test.clone();
   test2.set(1, 0.0);
   test2.set(3, 0.0);
   assertEquals(3.3 * 3.3, test2.dot(test), EPSILON);
 }