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

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

Introduction

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

Prototype

public Vector3D(double x, double y, double z) 

Source Link

Document

Simple constructor.

Usage

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

public RenderableSpacePartitioningGrid(double sizeX, double sizeY, double sizeZ, double gridBlockSize,
        double viewDepth) {
    super(new Vector3D(sizeX, sizeY, sizeZ), gridBlockSize, viewDepth * 1.2);
}

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

public void cubesWithinRadiusOf(Vector3D centerInWorldUnits, Submitter<List<E>> submitter) {
    recursiveAlwaysVisibleGridCubeSubmit(submitter);
    final double[] startPoint = centerInWorldUnits
            .subtract(new Vector3D(radiusInWorldUnits, radiusInWorldUnits, radiusInWorldUnits)).toArray();
    int startRaw = worldSpaceRasterizer.adapt(new Vector3D(startPoint[0], startPoint[1], startPoint[2]));

    final int zEnd = startRaw + getSquaresX() * getSquaresY() * rawDiaZ + (rawDiaY * getSquaresX()) + (rawDiaX);
    for (int point = startRaw; point < zEnd; point += zProgression) {//Z
        final int yEnd = point + getSquaresX() * rawDiaY;
        for (; point < yEnd; point += yProgression) {//Y
            final int xEnd = point + rawDiaX;
            for (; point < xEnd; point += xProgression) {//X
                final int wrappedPoint = point % rolloverPoint;
                recursiveGridCubeSubmit(submitter, wrappedPoint);
            } //end for(X)
        } //end for(Y)
    } //end for(Z)
}

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

@SuppressWarnings("unchecked")
public void itemsWithinRadiusOf(Vector3D centerInWorldUnits, Submitter<E> submitter) {
    recursiveAlwaysVisibleSubmit(submitter);

    final double[] startPoint = centerInWorldUnits
            .subtract(new Vector3D(radiusInWorldUnits, radiusInWorldUnits, radiusInWorldUnits)).toArray();
    int startRaw = worldSpaceRasterizer.adapt(new Vector3D(startPoint[0], startPoint[1], startPoint[2]));

    final int zEnd = startRaw + getSquaresX() * getSquaresY() * rawDiaZ + (rawDiaY * getSquaresX()) + (rawDiaX);
    for (int point = startRaw; point < zEnd; point += zProgression) {//Z
        final int yEnd = point + getSquaresX() * rawDiaY;
        for (; point < yEnd; point += yProgression) {//Y
            final int xEnd = point + rawDiaX;
            for (; point < xEnd; point += xProgression) {//X
                final int wrappedPoint = point % rolloverPoint;
                recursiveBlockSubmit(submitter, wrappedPoint);
            } //end for(X)
        } //end for(Y)
    } //end for(Z)
}

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

public List<E> world2List(double x, double y, double z, boolean newListIfNull) {
    final int pos = worldSpaceRasterizer.adapt(new Vector3D(x, y, z));
    List<E> result = elements[pos];
    if (newListIfNull && result == null)
        result = elements[pos] = new ArrayList<E>(8);
    return result;
}

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

public TerrainSystem(final InterpolatingAltitudeMap altitude, final TextureMesh textureMesh,
        final double gridSquareSize, final SpacePartitioningGrid parent,
        final RenderableSpacePartitioningGrid terrainMirror, final TR tr, final TDFFile tdf,
        final boolean flatShading, final LoadingProgressReporter terrainReporter) {
    super(parent);
    final int numCores = Runtime.getRuntime().availableProcessors();
    this.tr = tr;
    final int width = (int) altitude.getWidth();
    int height = (int) altitude.getHeight();
    this.gridSquareSize = gridSquareSize;
    this.heightScalar = tr.getWorld().sizeY / 2;
    final int chunkSideLength = TR.terrainChunkSideLengthInSquares;
    final double u[] = { 0, 1, 1, 0 };
    final double v[] = { 0, 0, 1, 1 };
    final double cu[] = { 0, 1, 1, 0 };
    final double cv[] = { 1, 1, 0, 0 };

    // Come up with a point list for tunnel entrances and exits
    TDFFile.Tunnel[] tunnels = tdf.getTunnels();
    final HashMap<Integer, TunnelPoint> points = new HashMap<Integer, TunnelPoint>();
    final HashMap<String, TDFFile.Tunnel> tunnelsByName = new HashMap<String, TDFFile.Tunnel>();
    if (tunnels != null) {// Null means no tunnels
        for (int i = 0; i < tunnels.length; i++) {
            final TDFFile.Tunnel tun = tunnels[i];
            if (tun.getEntranceLogic() != TunnelLogic.invisible) {
                final TunnelPoint tp = new TunnelPoint(tun, true);
                points.put(tp.hashCode(), tp);
            }//from w  ww .  jav a2  s. co  m
            if (tun.getExitLogic() != TunnelLogic.invisible) {
                final TunnelPoint tp = new TunnelPoint(tun, false);
                points.put(tp.hashCode(), tp);
                tunnelsByName.put(tun.getTunnelLVLFile(), tunnels[i]);
            } //end if(invisible)
        } // end for(tunnels)
    } // end if(tunnels)

    final LoadingProgressReporter[] reporters = terrainReporter.generateSubReporters(256 / chunkSideLength);
    int reporterIndex = 0;
    TRFutureTask<Void>[] rowTasks = new TRFutureTask[numCores * 2];
    int taskIdx = 0;
    // For each chunk
    for (int gZ = 0; gZ < height; gZ += chunkSideLength) {
        reporters[reporterIndex++].complete();
        final int _gZ = gZ;
        rowTasks[taskIdx++] = tr.getThreadManager().submitToThreadPool(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                for (int gX = 0; gX < width; gX += chunkSideLength) {
                    // GROUND
                    {// Start scope
                        final double objectX = Math
                                .round(((double) gX + ((double) chunkSideLength / 2.)) * gridSquareSize);
                        final double objectZ = Math
                                .round(((double) _gZ + ((double) chunkSideLength / 2.)) * gridSquareSize);
                        final double objectY = Math.round(altitude.heightAt(gX, _gZ) * heightScalar);
                        final Model m = new Model(false, tr);
                        // for each square
                        for (int cZ = _gZ; cZ < _gZ + chunkSideLength; cZ++) {
                            for (int cX = gX; cX < gX + chunkSideLength; cX++) {
                                final double hTL = altitude.heightAt(cX, cZ) * heightScalar;
                                final double hTR = altitude.heightAt((cX + 1), cZ) * heightScalar;
                                final double hBR = altitude.heightAt((cX + 1), (cZ + 1)) * heightScalar;
                                final double hBL = altitude.heightAt(cX, (cZ + 1)) * heightScalar;
                                final double xPos = cX * gridSquareSize;
                                final double zPos = cZ * gridSquareSize;

                                Vector3D norm0, norm1, norm2, norm3;
                                Vector3D norm = altitude.normalAt(cX, cZ);
                                norm3 = new Vector3D(norm.getX() * 3, norm.getY(), norm.getZ() * 3).normalize();
                                norm = altitude.normalAt(cX + 1, cZ);
                                norm2 = new Vector3D(norm.getX() * 3, norm.getY(), norm.getZ() * 3).normalize();
                                norm = altitude.normalAt(cX + 1, cZ + 1);
                                norm1 = new Vector3D(norm.getX() * 3, norm.getY(), norm.getZ() * 3).normalize();
                                norm = altitude.normalAt(cX, cZ + 1);
                                norm0 = new Vector3D(norm.getX() * 3, norm.getY(), norm.getZ() * 3).normalize();

                                if (flatShading)
                                    norm0 = norm1 = norm2 = norm3 = altitude.normalAt(cX + .5, cZ + .5);

                                final Integer tpi = cX + cZ * 256;
                                TextureDescription td = (TextureDescription) (points.containsKey(tpi)
                                        ? points.get(tpi).getTexture()
                                        : textureMesh.textureAt(cX, cZ));
                                Triangle[] tris = Triangle.quad2Triangles(
                                        // COUNTER-CLOCKWISE
                                        // //x
                                        new double[] { xPos - objectX, xPos + gridSquareSize - objectX,
                                                xPos + gridSquareSize - objectX, xPos - objectX },
                                        new double[] { hBL - objectY, hBR - objectY, hTR - objectY,
                                                hTL - objectY },
                                        new double[] { zPos + gridSquareSize - objectZ,
                                                zPos + gridSquareSize - objectZ, zPos - objectZ,
                                                zPos - objectZ },
                                        u, v, td, RenderMode.STATIC,
                                        new Vector3D[] { norm0, norm1, norm2, norm3 }, cX + cZ % 4);
                                m.addTriangle(tris[0]);
                                m.addTriangle(tris[1]);
                            } // end for(cX)
                        } // end for(cZ)
                          // Add to grid
                        if (m.finalizeModel().getTriangleList() != null) {
                            final TerrainChunk chunkToAdd = new TerrainChunk(tr, m, altitude);
                            final double[] chunkPos = chunkToAdd.getPosition();
                            chunkPos[0] = objectX;
                            chunkPos[1] = objectY;
                            chunkPos[2] = objectZ;
                            chunkToAdd.notifyPositionChange();
                            add(chunkToAdd);
                        } else {
                            System.out.println("Rejected chunk: " + m.getDebugName());
                        }
                    } // end scope

                    {// start scope ///// CEILING
                        final double objectX = Math
                                .round(((double) gX + ((double) chunkSideLength / 2.)) * gridSquareSize);
                        final double objectZ = Math
                                .round(((double) _gZ + ((double) chunkSideLength / 2.)) * gridSquareSize);
                        final double objectY = Math
                                .round((2. - altitude.heightAt(gX, _gZ)) * heightScalar + Y_NUDGE);
                        final Model m = new Model(false, tr);
                        // for each square
                        for (int cZ = _gZ; cZ < _gZ + chunkSideLength; cZ++) {
                            for (int cX = gX; cX < gX + chunkSideLength; cX++) {
                                final double hTL = (2. - altitude.heightAt(cX, cZ)) * heightScalar + Y_NUDGE;
                                final double hTR = (2. - altitude.heightAt((cX + 1), cZ)) * heightScalar
                                        + Y_NUDGE;
                                final double hBR = (2. - altitude.heightAt((cX + 1), (cZ + 1))) * heightScalar
                                        + Y_NUDGE;
                                final double hBL = (2. - altitude.heightAt(cX, (cZ + 1))) * heightScalar
                                        + Y_NUDGE;
                                final double xPos = cX * gridSquareSize;
                                final double zPos = cZ * gridSquareSize;

                                Vector3D norm0, norm1, norm2, norm3;
                                Vector3D norm = altitude.normalAt(cX, cZ);
                                norm3 = altitude.heightAt(cX, cZ) < .9
                                        ? new Vector3D(norm.getX() * 3, norm.getY() * -1, norm.getZ() * 3)
                                                .normalize()
                                        : new Vector3D(norm.getX() * 3, norm.getY(), norm.getZ() * 3)
                                                .normalize();
                                norm = altitude.normalAt(cX + 1, cZ);
                                norm2 = altitude.heightAt(cX + 1, cZ) < .9
                                        ? new Vector3D(norm.getX() * 3, norm.getY() * -1, norm.getZ() * 3)
                                                .normalize()
                                        : new Vector3D(norm.getX() * 3, norm.getY(), norm.getZ() * 3)
                                                .normalize();
                                norm = altitude.normalAt(cX + 1, cZ + 1);
                                norm1 = altitude.heightAt(cX + 1, cZ + 1) < .9
                                        ? new Vector3D(norm.getX() * 3, norm.getY() * -1, norm.getZ() * 3)
                                                .normalize()
                                        : new Vector3D(norm.getX() * 3, norm.getY(), norm.getZ() * 3)
                                                .normalize();
                                norm = altitude.normalAt(cX, cZ + 1);
                                norm0 = altitude.heightAt(cX, cZ + 1) < .9
                                        ? new Vector3D(norm.getX() * 3, norm.getY() * -1, norm.getZ() * 3)
                                                .normalize()
                                        : new Vector3D(norm.getX() * 3, norm.getY(), norm.getZ() * 3)
                                                .normalize();

                                if (flatShading)
                                    norm0 = norm1 = norm2 = norm3 = altitude.normalAt(cX + .5, cZ + .5);

                                // Ceiling texture cell X (Z in this engine) value
                                // is offset by 10.
                                // No tunnelpoints on ceiling
                                TextureDescription td = (TextureDescription) (textureMesh.textureAt(cX,
                                        cZ + 10));
                                norm = new Vector3D(norm.getX() * 3, norm.getY(), norm.getZ() * 3).normalize();// Exaggerate
                                // features.
                                Triangle[] tris = Triangle.quad2Triangles(
                                        // CLOCKWISE (else backface culling will eat
                                        // it)
                                        new double[] { xPos - objectX, xPos + gridSquareSize - objectX,
                                                xPos + gridSquareSize - objectX, xPos - objectX }, // x
                                        new double[] { hTL - objectY, hTR - objectY, hBR - objectY,
                                                hBL - objectY },
                                        new double[] { zPos - objectZ, zPos - objectZ,
                                                zPos + gridSquareSize - objectZ,
                                                zPos + gridSquareSize - objectZ },
                                        cu, cv, td, RenderMode.STATIC,
                                        new Vector3D[] { norm3, norm2, norm1, norm0 }, cX + cZ % 4);
                                m.addTriangle(tris[0]);
                                m.addTriangle(tris[1]);
                            } // end for(cX)
                        } // end for(cZ)
                          // Add to grid
                        if (m.finalizeModel().getTriangleList() != null) {
                            final TerrainChunk chunkToAdd = new TerrainChunk(tr, m, altitude);
                            final double[] chunkPos = chunkToAdd.getPosition();
                            chunkPos[0] = objectX;
                            chunkPos[1] = objectY;
                            chunkPos[2] = objectZ;
                            chunkToAdd.notifyPositionChange();
                            chunkToAdd.setCeiling(true);
                            terrainMirror.add(chunkToAdd);
                        } else {
                            System.out.println("Rejected chunk: " + m.getDebugName());
                        }
                    } // end scope(CEILING)
                } // end for(gX)
                return null;
            }
        });
        if (taskIdx >= rowTasks.length)
            taskIdx = 0;
    } // end for(gZ)
    terrainMirror.deactivate();
}

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.//w  w  w  . j a v  a 2  s. com
 * 
 */
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.
 * /*  www .  j  a  v a  2  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.jtrfp.trcl.TriangleList.java

public Vector3D getMaximumVertexDims() {
    if (isPrimitivesFinalized())
        return cachedMaximumVertexDims;
    Vector3D result = Vector3D.ZERO;
    Triangle[][] t = getPrimitives();/*from  w w w.j  a v  a  2  s  . c  om*/
    for (Triangle[] frame : t) {
        for (Triangle tri : frame) {
            for (int i = 0; i < 3; i++) {
                double v;
                final Vector3D pos = tri.getVertices()[i].getPosition();
                v = pos.getX();
                result = result.getX() < v ? new Vector3D(v, result.getY(), result.getZ()) : result;
                v = pos.getY();
                result = result.getY() < v ? new Vector3D(result.getX(), v, result.getZ()) : result;
                v = pos.getZ();
                result = result.getZ() < v ? new Vector3D(result.getX(), result.getY(), v) : result;
            } // end for(vertex)
        } // end for(triangle)
    } // end for(triangles)
    return result;
}

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

public Vector3D getMinimumVertexDims() {
    if (isPrimitivesFinalized())
        return cachedMinimumVertexDims;
    Vector3D result = new Vector3D(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY,
            Double.POSITIVE_INFINITY);
    Triangle[][] t = getPrimitives();/*  w  w  w.j  a v a2  s  . c o  m*/
    for (Triangle[] frame : t) {
        for (Triangle tri : frame) {
            for (int i = 0; i < 3; i++) {
                double v;
                final Vector3D pos = tri.getVertices()[i].getPosition();
                v = pos.getX();
                result = result.getX() > v ? new Vector3D(v, result.getY(), result.getZ()) : result;
                v = pos.getY();
                result = result.getY() > v ? new Vector3D(result.getX(), v, result.getZ()) : result;
                v = pos.getZ();
                result = result.getZ() > v ? new Vector3D(result.getX(), result.getY(), v) : result;
            } // end for(vertex)
        } // end for(triangle)
    } // end for(triangles)
    return result;
}

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

public Tunnel(TR tr, TDFFile.Tunnel sourceTunnel, LoadingProgressReporter rootReporter) {
    super(tr.getDefaultGrid());
    this.world = tr.getWorld();
    reporters = rootReporter.generateSubReporters(2);
    this.sourceTunnel = sourceTunnel;
    this.tr = tr;
    gl = tr.gpu.get().getGl();/*from www . j a  v a2  s  . co m*/
    tunnelAssemblyReporter = reporters[0];
    Vector3D tunnelEnd = null;
    deactivate();// Sleep until activated by tunnel entrance
    try {
        lvl = tr.getResourceManager().getLVL(sourceTunnel.getTunnelLVLFile());
        final Vector3D entranceVector = TUNNEL_START_DIRECTION.getHeading();
        palette = tr.getResourceManager().getPalette(lvl.getGlobalPaletteFile());
        palette[0] = new Color(0, 0, 0, 0);//KLUDGE: Color zero must be transparent.
        paletteVL = new ColorPaletteVectorList(palette);
        ESTuTvPalette = new ColorPaletteVectorList(
                tr.getResourceManager().getLTE("FOG\\" + lvl.getLuminanceMapFile()).toColors(palette));
        tunnelEnd = buildTunnel(sourceTunnel, entranceVector, false);
    } catch (Exception e) {
        e.printStackTrace();
    }
    exitObject = new TunnelExitObject(tr, this);
    exitObject.setMirrorTerrain(sourceTunnel.getExitMode() == ExitMode.exitToChamber);
    exitObject.setPosition(tunnelEnd.add(new Vector3D(50000, 0, 0)).toArray());
    exitObject.notifyPositionChange();
    add(exitObject);
    // X is tunnel depth, Z is left-right
    try {
        objectSystem = new ObjectSystem(this, tr, lvl, null, Vector3D.MINUS_I,
                TUNNEL_START_POS.add(TUNNEL_OBJECT_POS_OFFSET), reporters[1]);
    } catch (Exception e) {
        e.printStackTrace();
    }
    /*tr.getGame().getCurrentMission().getOverworldSystem().add(
       entranceObject = new TunnelEntranceObject(tr, this));*/
}