Example usage for org.apache.commons.math3.geometry.euclidean.twod Vector2D Vector2D

List of usage examples for org.apache.commons.math3.geometry.euclidean.twod Vector2D Vector2D

Introduction

In this page you can find the example usage for org.apache.commons.math3.geometry.euclidean.twod Vector2D Vector2D.

Prototype

public Vector2D(double a, Vector2D u) 

Source Link

Document

Multiplicative constructor Build a vector from another one and a scale factor.

Usage

From source file:org.evors.rs.ui.sandpit.TrialViewer.java

@Override
public void draw() {
    if (buffer == null) {
        return;/*from   w w w. j  a v a  2s. c o  m*/
    }
    Graphics2D g2 = (Graphics2D) buffer.getDrawGraphics();
    camera.setWindowSize(new Vector2D(this.getWidth(), this.getHeight()));
    g2.setColor(Color.WHITE);
    g2.fillRect(0, 0, getWidth(), getHeight());
    AffineTransform prevTrans = g2.getTransform();
    g2.setTransform(camera.getTransform());
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    grid.draw(g2);
    if (world != null) {
        SandpitRenderer.drawWorld(g2, world);
    }
    if (path != null) {
        path.draw(g2);
    }
    if (robot != null) {
        SandpitRenderer.drawRobot(g2, robot);
    }
    g2.setTransform(prevTrans);
    drawText(g2,
            String.format("Time: %.2f\nRobot position: {%.2f,%.2f}\nRobot heading: %.2f\nInputs:%s\nNeurons:%s",
                    time, robot.getPosition().getX(), robot.getPosition().getY(), robot.getHeading(),
                    Arrays.toString(robot.getInput()),
                    Arrays.toString(((CTRNN) controller.getController()).getNeurons())));
}

From source file:org.evors.rs.ui.sandpit.WorldViewer.java

@Override
public void draw() {
    Graphics2D g2 = (Graphics2D) getGraphics();
    camera.setWindowSize(new Vector2D(this.getWidth(), this.getHeight()));
    g2.setColor(Color.WHITE);/*from   ww  w  . jav a  2  s .c  o  m*/
    g2.fillRect(0, 0, getWidth(), getHeight());
    AffineTransform prevTrans = g2.getTransform();
    g2.setTransform(camera.getTransform());
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    grid.draw(g2);
    if (world != null) {
        SandpitRenderer.drawWorld(g2, world);
    }
    g2.setTransform(prevTrans);
}

From source file:org.evors.rs.unibot.sim.SimulatedUnibot.java

/**
 * @return Where the base of the rangefinder is currently located.
 *//*from   w w  w .java2s. c  o m*/
private Vector2D getRangeFinderBase() {
    return rect.getLocalToWorldCoords(new Vector2D(size.getX() / 2, size.getY() / 2));
}

From source file:org.evors.rs.unibot.sim.SimulatedUnibotTest.java

@Before
public void setUp() {
    new Expectations() {
        {/*from  w  w  w. j a v  a2  s.  c om*/
            world.getBounds();
            result = new Vector2D(5, 5);
        }
    };
    robot = new SimulatedUnibot(world, new Vector2D(0.6, 0.6), 1f / 60f);
}

From source file:org.evors.rs.unibot.sim.SimulatedUnibotTest.java

public void setUpForRangeFinder() {
    new Expectations() {
        {/*from w ww  .  j  av a 2s .co m*/
            world.getBounds();
            result = new Vector2D(5, 5);
        }
    };
    robot = new SimulatedUnibot(world, new Vector2D(2, 2), 1f / 60f);
}

From source file:org.jtrfp.trcl.core.ResourceManager.java

public Model getBINModel(String name, TextureDescription defaultTexture, double scale, boolean cache,
        ColorPaletteVectorList palette, ColorPaletteVectorList ESTuTvPalette)
        throws FileLoadException, IOException, IllegalAccessException {
    if (name == null)
        throw new NullPointerException("Name is intolerably null");
    if (palette == null)
        throw new NullPointerException("Palette is intolerably null");
    if (modelCache.containsKey(name) && cache)
        return modelCache.get(name);
    //The models like to set up two line segments where there should be one. 
    //This set is for identifying and culling redundant segs.
    final HashSet<Integer> alreadyVisitedLineSegs = new HashSet<Integer>();
    boolean hasAlpha = false;
    try {/*from w  w  w  .  j a  v  a2s  .c om*/
        BINFile.AnimationControl ac = null;
        Model result = new Model(true, tr);
        ac = aniBinNameMap.get(name);
        if (ac == null) {
            InputStream is = getInputStreamFromResource("MODELS\\" + name);
            //TODO: InputStream not guaranteed to close when exception is thrown. Wrap in try{}, close it, and re-throw.
            ac = new BINFile.AnimationControl(is);//This will throw an exception on and escape to the static model block
            is.close();
            aniBinNameMap.put(name, ac);
        }
        System.out.println("Recognized as animation control file.");
        //Build the Model from the BINFile.Model
        Model[] frames = new Model[ac.getNumFrames()];
        for (int i = 0; i < frames.length; i++) {
            frames[i] = getBINModel(ac.getBinFiles().get(i), defaultTexture, scale, cache, palette,
                    ESTuTvPalette);
        }
        result.setDebugName(name + " triangles: " + frames[0].getRawTriangleLists().get(0).size());
        //Consolidate the frames to one model
        for (int i = 0; i < frames.length; i++) {
            result.addFrame(frames[i]);
        }
        result.setFrameDelayInMillis((int) (((double) ac.getDelay() / 65535.) * 1000.));
        //result.finalizeModel();
        if (cache)
            modelCache.put(name, result);
        return result;
    } //end try{}
    catch (UnrecognizedFormatException e) {//ok fail. Static model
        try {
            BINFile.Model m = null;
            Model result = new Model(false, tr);
            result.setDebugName(name);
            m = modBinNameMap.get(name);
            if (m == null) {
                InputStream is = getInputStreamFromResource("MODELS\\" + name);
                m = new BINFile.Model(is);
                modBinNameMap.put(name, m);
            } //end if(null)
            final double cpScalar = (scale * TR.crossPlatformScalar * 256.) / (double) m.getScale();
            System.out.println("Recognized as model file.");
            List<org.jtrfp.trcl.gpu.Vertex> vertices = new ArrayList<org.jtrfp.trcl.gpu.Vertex>();
            for (BINFile.Model.Vertex binVtx : m.getVertices()) {
                vertices.add(new org.jtrfp.trcl.gpu.Vertex().setPosition(new Vector3D(binVtx.getX() * cpScalar,
                        binVtx.getY() * cpScalar, binVtx.getZ() * cpScalar)));
            } //end try{}

            TextureDescription currentTexture = null;
            final double[] u = new double[4];
            final double[] v = new double[4];
            for (ThirdPartyParseable b : m.getDataBlocks()) {
                //Sort out types of block
                if (b instanceof TextureBlock) {
                    TextureBlock tb = (TextureBlock) b;
                    if (hasAlpha)
                        currentTexture = getRAWAsTexture(tb.getTextureFileName(), palette, ESTuTvPalette,
                                hasAlpha);
                    else {
                        currentTexture = getRAWAsTexture(tb.getTextureFileName(), palette, ESTuTvPalette,
                                false);
                    }
                    System.out.println(
                            "ResourceManager: TextureBlock specifies texture: " + tb.getTextureFileName());
                } //end if(TextureBlock)
                else if (b instanceof FaceBlock) {
                    //System.out.println("FaceBlock found: "+b.getClass().getSimpleName());
                    FaceBlock block = (FaceBlock) b;
                    List<FaceBlockVertex> vertIndices = block.getVertices();
                    if (currentTexture == null) {
                        System.out.println("Warning: Face texture not specified. Using fallback texture.");
                        currentTexture = defaultTexture;
                    }
                    /*
                     * "The two vb_tex_coord values map the vertices of the face to the texture. 
                     * They are both in the range of 0x0 to 0xFF00, with u=0x0, v=0x0 being the upper 
                     * left corner of the texture, and u=0xFF00, v=0xFF00 being the lower right corner."
                     * - http://www.viaregio.de/pieper/mtm/bin_file_format.shtml
                     */
                    //// Note: It appears that Stefan's 0xFF0000 approach works rather than the 0xFF00 value. typo?
                    if (vertIndices.size() == 4) {//Quads
                        org.jtrfp.trcl.gpu.Vertex[] vtx = new org.jtrfp.trcl.gpu.Vertex[4];
                        for (int i = 0; i < 4; i++) {
                            vtx[i] = vertices.get(vertIndices.get(i).getVertexIndex()
                                    % (b instanceof FaceBlock05 ? 10 : Integer.MAX_VALUE));
                        }
                        Vector3D blockNormal = new Vector3D(block.getNormalX(), block.getNormalY(),
                                block.getNormalZ());
                        if (blockNormal.getNorm() == 0)
                            blockNormal = new Vector3D(1, 0, 0);//Use filler if zero norm.
                        if (vertIndices.get(0) instanceof FaceBlockVertexWithUV) {
                            for (int i = 0; i < 4; i++) {
                                final FaceBlockVertexWithUV fbvi = (FaceBlockVertexWithUV) vertIndices.get(i);
                                u[i] = (double) (fbvi).getTextureCoordinateU() / (double) 0xFF0000;
                                v[i] = (double) (fbvi).getTextureCoordinateV() / (double) 0xFF0000;
                            } //end for(4)
                        } else {
                            u[0] = BOX_U[0];
                            v[0] = BOX_V[0];
                            u[1] = BOX_U[1];
                            v[1] = BOX_V[1];
                            u[2] = BOX_U[2];
                            v[2] = BOX_V[2];
                            u[3] = BOX_U[3];
                            v[3] = BOX_V[3];
                        }
                        Triangle[] tris = Triangle.quad2Triangles(vtx,
                                new Vector2D[] { new Vector2D(u[0], 1. - v[0]), new Vector2D(u[1], 1. - v[1]),
                                        new Vector2D(u[2], 1. - v[2]), new Vector2D(u[3], 1. - v[3]) },
                                currentTexture, RenderMode.DYNAMIC, hasAlpha, blockNormal.normalize(),
                                "quad.BINmodel" + name);
                        result.addTriangle(tris[0]);
                        result.addTriangle(tris[1]);
                    } else if (vertIndices.size() == 3)//Triangles
                    {
                        Triangle t = new Triangle(currentTexture);
                        try {
                            t.setCentroidNormal(
                                    new Vector3D(block.getNormalX(), block.getNormalY(), block.getNormalZ())
                                            .normalize());
                        } catch (MathArithmeticException ee) {
                            t.setCentroidNormal(Vector3D.ZERO);
                        }
                        t.setAlphaBlended(hasAlpha);
                        t.setRenderMode(RenderMode.DYNAMIC);

                        for (int vi = 0; vi < 3; vi++) {
                            final org.jtrfp.trcl.gpu.Vertex vtx = vertices
                                    .get(vertIndices.get(vi).getVertexIndex()
                                            - (b instanceof FaceBlock05 ? m.getUnknown2() : 0));
                            t.setVertex(vtx, vi);
                            if (b instanceof FaceBlock05
                                    || !(vertIndices.get(0) instanceof FaceBlockVertexWithUV))
                                t.setUV(new Vector2D(BOX_U[vi], BOX_V[vi]), vi);
                            else {
                                t.setUV(new Vector2D(
                                        (double) ((FaceBlockVertexWithUV) vertIndices.get(vi))
                                                .getTextureCoordinateU() / (double) 0xFF0000,
                                        1. - (double) ((FaceBlockVertexWithUV) vertIndices.get(vi))
                                                .getTextureCoordinateV() / (double) 0xFF0000),
                                        vi);
                            }
                        } //end for(vi)
                        if (currentTexture == null) {
                            System.err.println("WARNING: Texture never set for " + name + ". Using fallback.");
                            currentTexture = tr.gpu.get().textureManager.get().getFallbackTexture();
                        }
                        result.addTriangle(t);
                    } //end if(3 vertices)
                    else {
                        System.err.println("ResourceManager: FaceBlock has " + vertIndices.size()
                                + " vertices. Only 3 or 4 supported.");
                    }
                } //end if(FaceBlock)
                else if (b instanceof ColorBlock) {
                    final ColorBlock cb = (ColorBlock) b;
                    final byte[] bytes = cb.getBytes();
                    final Color color = new Color(bytes[0] & 0xFF, bytes[1] & 0xFF, bytes[2] & 0xFF);
                    currentTexture = tr.gpu.get().textureManager.get().solidColor(color);
                } else if (b instanceof FaceBlock19) {
                    System.out.println(b.getClass().getSimpleName()
                            + " (solid colored faces) not yet implemented. Skipping...");
                } else if (b instanceof FaceBlock05) {
                } //TODO
                else if (b instanceof LineSegmentBlock) {
                    LineSegmentBlock block = (LineSegmentBlock) b;
                    org.jtrfp.trcl.gpu.Vertex v1 = vertices.get(block.getVertexID1());
                    org.jtrfp.trcl.gpu.Vertex v2 = vertices.get(block.getVertexID2());
                    if (!alreadyVisitedLineSegs.contains(v1.hashCode() * v2.hashCode())) {
                        Triangle[] newTris = new Triangle[6];

                        LineSegment.buildTriPipe(v1.getPosition(), v2.getPosition(),
                                tr.gpu.get().textureManager.get().getDefaultTriPipeTexture(), 200, newTris, 0);
                        result.addTriangles(newTris);
                        alreadyVisitedLineSegs.add(v1.hashCode() * v2.hashCode());
                    } //end if(not already visited)
                } //end if(LineSegmentBlock)
                else if (b instanceof Unknown12) {
                    System.out.println("Found unknown12. Assuming this is a tag for a transparent texture...");
                    hasAlpha = true;
                } else if (b instanceof AnimatedTextureBlock) {
                    System.out.println("Found animated texture block.");
                    AnimatedTextureBlock block = (AnimatedTextureBlock) b;
                    List<String> frames = block.getFrameNames();
                    double timeBetweenFramesInMillis = ((double) block.getDelay() / 65535.) * 1000.;
                    Texture[] subTextures = new Texture[frames.size()];
                    for (int ti = 0; ti < frames.size(); ti++) {
                        if (!hasAlpha)
                            subTextures[ti] = (Texture) getRAWAsTexture(frames.get(ti), palette, ESTuTvPalette,
                                    false);
                        else
                            subTextures[ti] = (Texture) getRAWAsTexture(frames.get(ti), palette, ESTuTvPalette,
                                    true);
                        //subTextures[ti]=tex instanceof Texture?new DummyTRFutureTask<Texture>((Texture)tex):(Texture)Texture.getFallbackTexture();
                    } //end for(frames) //fDelay, nFrames,interp
                    currentTexture = new AnimatedTexture(
                            new Sequencer((int) timeBetweenFramesInMillis, subTextures.length, false),
                            subTextures);
                } else if (b instanceof EOFBlock) {
                    System.out.println("...That's all, end of BIN");
                } else {
                    System.out.println("Failed to identify DataBlock: " + b.getClass().getName());
                }
            } //end for(dataBlocks)
            //result.finalizeModel();
            result.setDebugName(name);
            //if(result.getTriangleList()==null && result.getTransparentTriangleList()==null)
            //    throw new RuntimeException("Resulting BIN has no triangleList");
            if (cache)
                modelCache.put(name, result);
            return result;
        } //end try{}
        catch (UnrecognizedFormatException ee) {
            //Not-good fail
            throw new UnrecognizedFormatException(
                    "Can't figure out what this is: " + name + ". Giving up. Expect trouble ahead.");
        }
    } //end catch(ok fail)
    //Bad fail.
}

From source file:org.jtrfp.trcl.Triangle.java

/**
 * Converts supplied quad coordinates to a pair of triangles in clockwise
 * order, top-left being index zero.// ww w  .  j  a v a 2 s  .co m
 * 
 */
public static Triangle[] quad2Triangles(double[] x, double[] y, double[] z, double[] u, double[] v,
        TextureDescription texture, RenderMode mode, boolean hasAlpha, Vector3D centroidNormal, Triangle[] dest,
        int destOffset, String debugName) {
    final Vertex[] vertices = new Vertex[] { new Vertex().setPosition(new Vector3D(x[0], y[0], z[0])),
            new Vertex().setPosition(new Vector3D(x[1], y[1], z[1])),
            new Vertex().setPosition(new Vector3D(x[2], y[2], z[2])),
            new Vertex().setPosition(new Vector3D(x[3], y[3], z[3])), };
    Vector2D[] uvs = new Vector2D[4];
    for (int i = 0; i < 4; i++) {
        uvs[i] = new Vector2D(u[i], v[i]);
    }
    return quad2Triangles(vertices, uvs, texture, mode, hasAlpha, centroidNormal, dest, destOffset, debugName);
}

From source file:org.jtrfp.trcl.Triangle.java

/**
 * Converts supplied quad coordinates to a pair of triangles in clockwise
 * order, top-left being index zero+ringRotation.
 * //w  w w  .  ja  va2  s. c o  m
 * @param ringRotation
 * 
 */
public static Triangle[] quad2Triangles(double[] x, double[] y, double[] z, double[] u, double[] v,
        TextureDescription texture, RenderMode mode, boolean hasAlpha, Vector3D[] normals, int ringRotation,
        Triangle[] dest, int destOffset) {
    final Vertex[] vertices = new Vertex[] {
            new Vertex().setPosition(new Vector3D(x[(0 + ringRotation) % 4], y[(0 + ringRotation) % 4],
                    z[(0 + ringRotation) % 4])).setNormal(normals[(0 + ringRotation) % 4]),
            new Vertex().setPosition(new Vector3D(x[(1 + ringRotation) % 4], y[(1 + ringRotation) % 4],
                    z[(1 + ringRotation) % 4])).setNormal(normals[(1 + ringRotation) % 4]),
            new Vertex().setPosition(new Vector3D(x[(2 + ringRotation) % 4], y[(2 + ringRotation) % 4],
                    z[(2 + ringRotation) % 4])).setNormal(normals[(2 + ringRotation) % 4]),
            new Vertex().setPosition(new Vector3D(x[(3 + ringRotation) % 4], y[(3 + ringRotation) % 4],
                    z[(3 + ringRotation) % 4])).setNormal(normals[(3 + ringRotation) % 4]), };
    Vector2D[] uvs = new Vector2D[4];
    for (int i = 0; i < 4; i++) {
        final int rotI = (i + ringRotation) % 4;
        uvs[i] = new Vector2D(u[rotI], v[rotI]);
    }
    return quad2Triangles(vertices, uvs, texture, mode, hasAlpha, dest, destOffset);
}

From source file:org.kalypso.model.wspm.ewawi.utils.profiles.EwawiProfilePoint.java

/**
 * This function returns the point as shape.
 * /*from w w w  .  ja v a 2  s  .  c om*/
 * @return The point as shape.
 */
public SHPPoint getShape() {
    /* g Rechtswert (Projektion auf Abszisse) von links nach rechts aufsteigend */
    /* h Hochwert (Abweichung von der Achse) rechts positiv, links negativer Wert. */
    final double rechtswert = m_proLine.getRechtswert().doubleValue();
    final double hochwert = m_proLine.getHochwert().doubleValue();

    /* Get the normalized vector of the fix points (length 1). */
    final Vector2D normalizedProfileAxis = getNormalizedProfileAxis();

    /* Get the projection vector. */
    final Vector2D projectionVector = normalizedProfileAxis.scalarMultiply(rechtswert);

    /* Create the start point. */
    final double xLeft = m_left.getRechtswert().doubleValue();
    final double yLeft = m_left.getHochwert().doubleValue();
    final Vector2D startPoint = new Vector2D(xLeft, yLeft);

    /* Create the projection point. */
    final Vector2D projectionPoint = startPoint.add(projectionVector);

    /* Create the orthogonal vector from the normalized profile axis (has then also length 1). */
    final Vector2D orthVector = new Vector2D(normalizedProfileAxis.getY(), -normalizedProfileAxis.getX());

    /* Add hochwert * orthVector to the projection point. */
    final Vector2D targetPoint = projectionPoint.add(hochwert, orthVector);

    return new SHPPoint(targetPoint.getX(), targetPoint.getY());
}

From source file:org.kalypso.model.wspm.ewawi.utils.profiles.EwawiProfilePoint.java

/**
 * This function returns the point as shape.
 * It ignores the hochwert, so that the point will be the projected point on the idealized line (left fixpoint to right fixpoint).
 * //from   w  ww  .ja v a 2 s.c o  m
 * @return The point as shape.
 */
public SHPPoint getShapeIdealized() {
    /* g Rechtswert (Projektion auf Abszisse) von links nach rechts aufsteigend */
    final double rechtswert = m_proLine.getRechtswert().doubleValue();

    /* Get the normalized vector of the fix points (length 1). */
    final Vector2D normalizedProfileAxis = getNormalizedProfileAxis();

    /* Get the projection vector. */
    final Vector2D projectionVector = normalizedProfileAxis.scalarMultiply(rechtswert);

    /* Create the start point. */
    final double xLeft = m_left.getRechtswert().doubleValue();
    final double yLeft = m_left.getHochwert().doubleValue();
    final Vector2D startPoint = new Vector2D(xLeft, yLeft);

    /* Create the projection point. */
    final Vector2D projectionPoint = startPoint.add(projectionVector);

    return new SHPPoint(projectionPoint.getX(), projectionPoint.getY());
}