Example usage for org.apache.commons.math3.geometry.euclidean.threed Vector3D subtract

List of usage examples for org.apache.commons.math3.geometry.euclidean.threed Vector3D subtract

Introduction

In this page you can find the example usage for org.apache.commons.math3.geometry.euclidean.threed Vector3D subtract.

Prototype

public Vector3D subtract(final Vector<Euclidean3D> v) 

Source Link

Usage

From source file:jtrace.object.transformation.Translation.java

@Override
public Vector3D applyInverse(Vector3D vec) {
    return vec.subtract(delta);
}

From source file:jtrace.Camera.java

/**
 * Retrieve coordinates of pixel corresponding to given point in an image
 * with the given width and height.//from w w w .j  ava  2  s  .  com
 * 
 * @param width
 * @param height
 * @param point 3d point to project
 * @return double[] containing x and y coordinates of pixel.
 */
public int[] getPixel(int width, int height, Vector3D point) {
    int[] coord = new int[2];

    Vector3D l = point.subtract(location);

    double lu = l.dotProduct(up);
    double lr = l.dotProduct(right);
    double lp = l.dotProduct(direction);

    coord[0] = (int) Math.round(((lr / lp) * (1.0 / fovRight) + 0.5) * width);
    coord[1] = (int) Math.round(((lu / lp) * (-1.0 / fovUp) + 0.5) * height);

    return coord;
}

From source file:jtrace.object.Cube.java

@Override
public double getFirstCollisionObjectFrame(Ray ray) {

    // Cube edges and vertices are the intersections of 6 planes.
    // Need to find points of intersection with each of these planes.

    double alpha = Double.POSITIVE_INFINITY;
    Ray collisionNormal = new Ray();
    for (int i = 0; i < 6; i++) {
        double thisAlpha = normals[i].direction.dotProduct(normals[i].origin.subtract(ray.origin))
                / normals[i].direction.dotProduct(ray.direction);

        if (thisAlpha <= 0)
            continue;

        Vector3D collisionLocation = ray.origin.add(thisAlpha, ray.direction);
        Vector3D delta = collisionLocation.subtract(normals[i].origin);
        if (Math.abs(delta.getX()) > 0.5 || Math.abs(delta.getY()) > 0.5 || Math.abs(delta.getZ()) > 0.5) {
            continue;
        }//from   w ww .ja va 2s  .  c o  m

        if (thisAlpha < alpha) {
            alpha = thisAlpha;
            collisionNormal.origin = collisionLocation;
            collisionNormal.direction = normals[i].direction;
        }
    }

    if (alpha < Double.POSITIVE_INFINITY) {
        incidentRay = ray;
        normalRay = collisionNormal;
    }

    return alpha;
}

From source file:jtrace.Camera.java

/**
 * Create camera at location directed at pointAt with the top of the
 * frame defined by the up vector and FOV defined by fovUp and fovRight.
 * /* ww  w .  ja v  a2 s  .com*/
 * @param location
 * @param pointAt
 * @param up
 * @param fovUp
 * @param fovRight 
 */
public Camera(Vector3D location, Vector3D pointAt, Vector3D up, double fovUp, double fovRight) {

    this.location = location;

    // Ensure up vector is normalized:
    up = up.normalize();

    // Obtain direction vector from pointAt location:
    this.direction = pointAt.subtract(location).normalize();

    // Use approximate up vector to determine true up and right vectors:
    this.right = direction.crossProduct(up).normalize();
    this.up = this.right.crossProduct(direction);

    this.fovUp = fovUp;
    this.fovRight = fovRight;
}

From source file:Tester2.java

private boolean isNightTime(final SpacecraftState s) {
    // https://celestrak.com/columns/v03n01/
    // when the sun's center is 6 degrees below the horizon, it is considered dark 
    //       enough to see earth satellites.

    try {/*from  w  w w.java2s. c o m*/
        // origin is the center of the Earth
        Vector3D curSunPos = sun.getPVCoordinates(s.getDate(), this.earthFrame).getPosition();

        // origin has been offset to the ground station
        Vector3D stationToSun = curSunPos.subtract(this.stationPos);
        Vector3D stationZenith = this.groundstationFrame.getZenith();
        double sunAngle = Vector3D.angle(stationToSun, stationZenith); // Sun center to station to zenith

        // angle required for darkness measured from observer's zenith
        double darkAngle = Math.PI / 2 + Math.toRadians(4.5);

        //System.out.println("--------------------------------- " + sunAngle + " >= " + darkAngle);
        return sunAngle >= darkAngle;
    } catch (OrekitException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("This broke");
        return false;
    }

}

From source file:Engine.Player.java

public void AttackPlayer(Player p, WorldMap worldMap, Screen screen) {
    Vector3D vf = new Vector3D(ViewFrom[0], ViewFrom[1], ViewFrom[2]);
    Vector3D pp = AlgebraUtils.ReturnClosestWectorMirror(p.ViewFrom, ViewFrom, worldMap);
    //Vector3D pp = new Vector3D(p.ViewFrom[0], p.ViewFrom[1], p.ViewFrom[2]);
    Vector3D sv = pp.subtract(vf);
    screen.shoot(vf, sv, this);
}

From source file:com.mapr.synth.drive.DriveTest.java

@Test
public void testPlanning() throws FileNotFoundException {
    Random rand = new Random(3);
    GeoPoint start = new GeoPoint((rand.nextDouble() - 0.5) * Math.PI / 2, rand.nextDouble() * Math.PI * 2);
    GeoPoint end = start.nearby(10, rand);

    Vector3D east = start.east();
    Vector3D north = start.north(east);
    try (PrintWriter out = new PrintWriter(new File("short-drive.csv"))) {
        out.printf("i, highway, x0, y0, x1, y1");
        double highway = 0;
        int n = 0;
        for (int i = 0; i < 50; i++) {
            List<Car.Segment> plan = Car.plan(start, end, rand);
            assertTrue(plan.size() < 20);
            Vector3D here = project(east, north, start.as3D());
            for (Car.Segment segment : plan) {
                Vector3D step = project(east, north, segment.getEnd().as3D());
                n++;//from   www .  j  a  v a2s.c om
                if (segment instanceof Car.Highway) {
                    highway++;
                }
                out.printf("%d, %d, %.4f, %.4f, %.4f, %.4f\n", i, (segment instanceof Car.Local) ? 1 : 0,
                        here.getX(), here.getY(), step.getX(), step.getY());
                here = step;
            }

            // should arrive at our destination!
            assertEquals(0.0, here.subtract(project(east, north, end.as3D())).getNorm(), 0.01);
        }
        // most steps should be local, not highway
        assertEquals(0.0, highway / n, 0.1);
    }
}

From source file:Engine.Player.java

public ArrayList<DPolygon> GetPolygons(double x, double y) {
    /*Cube head = new Cube(- halfHeadSize + x, - halfHeadSize + y, ViewFrom[2] - halfHeadSize + epsBody,
    halfHeadSize * 2.0, halfHeadSize * 2.0, halfHeadSize * 2.0, Color.YELLOW, PlayerId);
    head.Polys[2].DrawablePolygon.texture = GenerateTerrain.img;
            //from   w  ww  .j  a  v  a 2 s. c  o m
            
    Cube neck = new Cube(- halfNeckSize + x, - halfNeckSize + y, ViewFrom[2] - headSize + halfNeckSize,
        halfNeckSize * 2.0, halfNeckSize * 2.0, halfNeckSize, Color.PINK, PlayerId);
    Cube corpus = new Cube(- halfCorupsXSize + x, - halfCorupsYSize + y, ViewFrom[2] - corpusZSize - headSize + halfNeckSize - epsBody,
        halfCorupsXSize * 2.0, halfCorupsYSize * 2.0, 2 * halfCorpusZSize, Color.BLUE, PlayerId);
            
    Cube legs = new Cube(- halfLegSizeX + x, - halfLegSizeY + y, ViewFrom[2] - legZSize - corpusZSize - headSize + halfNeckSize - epsBody - epsBody,
        2 * halfLegSizeX, 2 * halfLegSizeY, legZSize, Color.MAGENTA, PlayerId);
            
    Cube hands = new Cube(- halfHandsXSize + x, - halfHandsYSize + y, ViewFrom[2] - handsZSize - headSize + halfNeckSize - epsBody,
        halfHandsXSize * 2.0, halfHandsYSize * 2.0, handsZSize, Color.CYAN, PlayerId);
            
            
            
    ArrayList<DPolygon> all = new ArrayList<DPolygon>();
            
    rot += 0.01;
            
    head.rotation = rot;
    head.setRotAdd();
    head.updatePoly();
            
    neck.rotation = rot;
    neck.updatePoly();
            
    corpus.rotation = rot;
    corpus.updatePoly();
            
    legs.rotation = rot;
    legs.updatePoly();
            
    hands.rotation = rot;
    hands.updatePoly();
            
            
    AddArrayToArrayList(all, head.GetPolygons());
    AddArrayToArrayList(all, neck.GetPolygons());
    AddArrayToArrayList(all, corpus.GetPolygons());
    AddArrayToArrayList(all, hands.GetPolygons());
    AddArrayToArrayList(all, legs.GetPolygons());*/

    //rot = Math.PI*0.75;
    //rot += 0.01;

    /*if (MoveDirection != null)
    {
    //0
    if (Math.abs(MoveDirection.getX()) < 0.1 && MoveDirection.getY() > 0.1) rot = 0;
    //1/4
    if (MoveDirection.getX() > 0.1 && MoveDirection.getY() > 0.1) rot = -Math.PI / 4.0;
    //1/2
    if (MoveDirection.getX() > 0.1 && Math.abs(MoveDirection.getY()) < 0.1) rot = -Math.PI / 2.0;
    //3/4
    if (MoveDirection.getX() > 0.1 && MoveDirection.getY() < 0.1) rot = -3.0 * Math.PI / 4.0;
    //1
    if (Math.abs(MoveDirection.getX()) < 0.1 && MoveDirection.getY() < 0.1) rot = -Math.PI;
    //5/4
    if (MoveDirection.getX() < 0.1 && MoveDirection.getY() < 0.1) rot = -5.0 * Math.PI / 4.0;
    //3/2
    if (MoveDirection.getX() < 0.1 && Math.abs(MoveDirection.getY()) < 0.1) rot = -3.0 * Math.PI / 2.0;
    //7/4
    if (MoveDirection.getX() < 0.1 && MoveDirection.getY() > 0.1) rot = -7.0 * Math.PI / 4.0;
            
    rot += 2 *  Math.PI / 4;
    }*/
    if (PositionIFace != null) {
        rot = Math.PI / 4.0;

        Vector3D v1 = new Vector3D(PositionIFace.getX(), PositionIFace.getY(), 0);
        Vector3D v2 = new Vector3D(ViewFrom[0], ViewFrom[1], 0);
        Vector3D v3 = (v1.subtract(v2)).normalize();

        Vector3D cross = Vector3D.crossProduct(v3, new Vector3D(1, 0, 0));
        double dot = Vector3D.dotProduct(v3, new Vector3D(1, 0, 0));

        double angle = Math.atan2(cross.getZ(), dot);
        rot += -angle;
    }

    double xx = -halfHeadSize + x;
    double yy = -halfHeadSize + y;
    double[] r = rotatePoint(xx, yy, xx, yy, rot - Math.PI * 0.75);

    Cube head = new Cube(r[0], r[1], ViewFrom[2] - halfHeadSize + epsBody, halfHeadSize * 2.0,
            halfHeadSize * 2.0, halfHeadSize * 2.0, Color.YELLOW, PlayerId);

    head.Polys[2].DrawablePolygon.texture = GenerateTerrain.img;

    Cube neck = new Cube(-halfNeckSize + x, -halfNeckSize + y, ViewFrom[2] - headSize + halfNeckSize,
            halfNeckSize * 2.0, halfNeckSize * 2.0, halfNeckSize, Color.PINK, PlayerId);
    Cube corpus = new Cube(-halfCorupsXSize + x, -halfCorupsYSize + y,
            ViewFrom[2] - corpusZSize - headSize + halfNeckSize - epsBody, halfCorupsXSize * 2.0,
            halfCorupsYSize * 2.0, 2 * halfCorpusZSize, Color.BLUE, PlayerId);

    Cube legs = new Cube(-halfLegSizeX + x, -halfLegSizeY + y,
            ViewFrom[2] - legZSize - corpusZSize - headSize + halfNeckSize - epsBody - epsBody,
            2 * halfLegSizeX, 2 * halfLegSizeY, legZSize, Color.MAGENTA, PlayerId);

    xx = -halfHandsXSize + x;
    yy = -halfHandsYSize + y;//- halfHandsYSize + (halfHandsYSize / 2.0) + y;
    r = rotatePoint(xx, yy, x - 0.5, y - halfHandsYSize, rot - Math.PI * 0.75);

    Cube handLeft = new Cube(r[0], r[1], ViewFrom[2] - handsZSize - headSize + halfNeckSize - epsBody, 1,
            halfHandsYSize * 2.0, handsZSize, Color.CYAN, PlayerId);

    xx = halfHandsXSize + x - 1;
    yy = -halfHandsYSize + y;//- halfHandsYSize + (halfHandsYSize / 2.0) + y;
    r = rotatePoint(xx, yy, x - 0.5, y - halfHandsYSize, rot - Math.PI * 0.75);

    Cube handRight = new Cube(r[0], r[1], ViewFrom[2] - handsZSize - headSize + halfNeckSize - epsBody, 1,
            halfHandsYSize * 2.0, handsZSize, Color.CYAN, PlayerId);

    xx = -2 + x;
    yy = -halfLegSizeY + y;//- halfHandsYSize + (halfHandsYSize / 2.0) + y;
    r = rotatePoint(xx, yy, x - 0.75, y - halfLegSizeY, rot - Math.PI * 0.75);

    Cube legLeft = new Cube(r[0], r[1],
            ViewFrom[2] - legZSize - corpusZSize - headSize + halfNeckSize - epsBody - epsBody, 1.5,
            halfLegSizeY * 2.0, legZSize, Color.CYAN, PlayerId);

    xx = 2 + x - 1.5;
    yy = -halfLegSizeY + y;//- halfHandsYSize + (halfHandsYSize / 2.0) + y;
    r = rotatePoint(xx, yy, x - 0.75, y - halfLegSizeY, rot - Math.PI * 0.75);

    Cube legRight = new Cube(r[0], r[1],
            ViewFrom[2] - legZSize - corpusZSize - headSize + halfNeckSize - epsBody - epsBody, 1.5,
            halfLegSizeY * 2.0, legZSize, Color.CYAN, PlayerId);
    //Cube legs = new Cube(- halfLegSizeX + x, - halfLegSizeY + y, ViewFrom[2] - legZSize - corpusZSize - headSize + halfNeckSize - epsBody - epsBody,
    //      2 * halfLegSizeX, 2 * halfLegSizeY, legZSize, Color.MAGENTA, PlayerId);

    ArrayList<DPolygon> all = new ArrayList<DPolygon>();

    //rot += 0.00;

    head.rotation = rot;
    head.setRotAdd();
    head.updatePoly();

    neck.rotation = rot;
    neck.updatePoly();

    corpus.rotation = rot;
    corpus.updatePoly();

    legs.rotation = rot;
    legs.updatePoly();

    handLeft.rotation = rot;
    handLeft.updatePoly();

    handRight.rotation = rot;
    handRight.updatePoly();

    legLeft.rotation = rot;
    legLeft.updatePoly();

    legRight.rotation = rot;
    legRight.updatePoly();

    AddArrayToArrayList(all, head.GetPolygons());
    AddArrayToArrayList(all, neck.GetPolygons());
    AddArrayToArrayList(all, corpus.GetPolygons());
    AddArrayToArrayList(all, handLeft.GetPolygons());
    AddArrayToArrayList(all, handRight.GetPolygons());
    AddArrayToArrayList(all, legLeft.GetPolygons());
    AddArrayToArrayList(all, legRight.GetPolygons());

    /*
            
            
            
            
            
    //rot = Math.PI*0.75;
    rot += 0.01;
    double xx = - halfHeadSize + x;
    double yy = - halfHeadSize + y;
    double[]r = rotatePoint(xx, yy, xx, yy, rot - Math.PI*0.75);
            
    Cube head = new Cube(r[0], r[1], ViewFrom[2] - halfHeadSize + epsBody,
    halfHeadSize * 2.0, halfHeadSize * 2.0, halfHeadSize * 2.0, Color.YELLOW, PlayerId, x, y, rot - Math.PI*0.75);
            
    head.Polys[2].DrawablePolygon.texture = GenerateTerrain.img;
            
            
    Cube neck = new Cube(- halfNeckSize + x, - halfNeckSize + y, ViewFrom[2] - headSize + halfNeckSize,
        halfNeckSize * 2.0, halfNeckSize * 2.0, halfNeckSize, Color.PINK, PlayerId, x, y, rot - Math.PI*0.75);
    Cube corpus = new Cube(- halfCorupsXSize + x, - halfCorupsYSize + y, ViewFrom[2] - corpusZSize - headSize + halfNeckSize - epsBody,
        halfCorupsXSize * 2.0, halfCorupsYSize * 2.0, 2 * halfCorpusZSize, Color.BLUE, PlayerId, x, y, rot - Math.PI*0.75);
            
    Cube legs = new Cube(- halfLegSizeX + x, - halfLegSizeY + y, ViewFrom[2] - legZSize - corpusZSize - headSize + halfNeckSize - epsBody - epsBody,
        2 * halfLegSizeX, 2 * halfLegSizeY, legZSize, Color.MAGENTA, PlayerId, x, y, rot - Math.PI*0.75);
            
    //xx = - halfHandsXSize + 0.5 + x;
    //yy = - halfHandsYSize + (halfHandsYSize / 2.0) + y;
            
    Cube hands = new Cube(- halfHandsXSize + x, - halfHandsYSize + y, ViewFrom[2] - handsZSize - headSize + halfNeckSize - epsBody,
        1, halfHandsYSize * 2.0, handsZSize, Color.CYAN, PlayerId, x, y, rot - Math.PI*0.75);
            
     ArrayList<DPolygon> all = new ArrayList<DPolygon>();
            
    AddArrayToArrayList(all, head.GetPolygons());
    AddArrayToArrayList(all, neck.GetPolygons());
    AddArrayToArrayList(all, corpus.GetPolygons());
    AddArrayToArrayList(all, hands.GetPolygons());
    AddArrayToArrayList(all, legs.GetPolygons());*/
    return all;
}

From source file:org.esa.s2tbx.dataio.s2.l1b.CoordinateUtils.java

public static double distanceToSegment(Vector3D v, Vector3D w, Vector3D p) {
    // Return minimum distance between line segment vw and point p
    final double l2 = Vector3D.distanceSq(v, w); // i.e. |w-v|^2 -  avoid a sqrt
    if (l2 == 0.0)
        return Vector3D.distance(p, v); // v == w case
    // Consider the line extending the segment, parameterized as v + t (w - v).
    // We find projection of point p onto the line.
    // It falls where t = [(p-v) . (w-v)] / |w-v|^2
    double t = Vector3D.dotProduct(p.subtract(v), w.subtract(v)) / l2;
    if (t < 0.0)
        return Vector3D.distance(p, v); // Beyond the 'v' end of the segment
    else if (t > 1.0)
        return Vector3D.distance(p, w); // Beyond the 'w' end of the segment
    Vector3D projection = v.add(w.subtract(v).scalarMultiply(t)); // Projection falls on the segment
    return Vector3D.distance(p, projection);
}

From source file:org.gearvrf.keyboard.util.Util.java

public static void rotateWithOpenGLLookAt(Vector3D cameraVector, Vector3D parentVector, GVRSceneObject object) {
    Vector3D globalUpVector = new Vector3D(0, 1, 0);
    Vector3D lookVector = parentVector.normalize();
    Vector3D rightVector = lookVector.crossProduct(globalUpVector);
    Vector3D upVector = rightVector.crossProduct(lookVector);
    Vector3D zAxis = cameraVector.subtract(parentVector).normalize();
    // Vector3D xAxis = upVector.crossProduct(zAxis).normalize();
    Vector3D xAxis = zAxis.crossProduct(upVector).normalize();
    Vector3D yAxis = xAxis.crossProduct(zAxis).normalize();
    // Vector3D yAxis = xAxis.crossProduct(zAxis).normalize();
    zAxis = zAxis.scalarMultiply(-1.f);//from   w w  w  .  j  ava  2s.  c  om

    float angle = (float) Vector3D.angle(parentVector, cameraVector);
    angle = (float) Math.toDegrees(angle);

    object.getTransform().rotateByAxis(angle, (float) xAxis.getX(), (float) xAxis.getY(), (float) xAxis.getZ());
    object.getTransform().rotateByAxis(angle, (float) yAxis.getX(), (float) yAxis.getY(), (float) yAxis.getZ());
    object.getTransform().rotateByAxis(angle, (float) zAxis.getX(), (float) zAxis.getY(), (float) zAxis.getZ());
}