Search in sources :

Example 1 with GeometryInfo

use of org.scijava.java3d.utils.geometry.GeometryInfo in project GDSC-SMLM by aherbert.

the class Shape3DHelper method createGeometryArray.

/**
 * Creates the object used to draw a single localisation.
 *
 * @param rendering the rendering
 * @param colorDepth the color depth
 * @return the geometry array
 */
public static GeometryArray createGeometryArray(Rendering rendering, int colorDepth) {
    final GeometryInfo gi = createGeometryInfo(rendering, colorDepth);
    final boolean useCoordIndexOnly = gi.getUseCoordIndexOnly();
    final GeometryArray ga = (rendering.is2D()) ? gi.getGeometryArray() : gi.getIndexedGeometryArray(false, false, false, useCoordIndexOnly, false);
    return ga;
}
Also used : GeometryInfo(org.scijava.java3d.utils.geometry.GeometryInfo) GeometryArray(org.scijava.java3d.GeometryArray)

Example 2 with GeometryInfo

use of org.scijava.java3d.utils.geometry.GeometryInfo in project GDSC-SMLM by aherbert.

the class Shape3DHelper method getNumberOfTriangles.

/**
 * Gets the number of triangles.
 *
 * @param rendering the rendering
 * @return the number of triangles
 */
public static int getNumberOfTriangles(Rendering rendering) {
    if (rendering == Rendering.POINT) {
        return 0;
    }
    final int index = rendering.ordinal();
    if (numberOfTriangles[index] == 0) {
        final GeometryInfo gi = createGeometryInfo(rendering, 0);
        // Remove so the conversion is faster
        gi.setColors((Color3f[]) null);
        gi.setColorIndices(null);
        gi.setNormals((Vector3f[]) null);
        gi.setNormalIndices(null);
        // this is required before conversion
        gi.setUseCoordIndexOnly(false);
        gi.convertToIndexedTriangles();
        numberOfTriangles[index] = gi.getCoordinateIndices().length / 3;
    }
    return numberOfTriangles[index];
}
Also used : Color3f(org.scijava.vecmath.Color3f) Vector3f(org.scijava.vecmath.Vector3f) GeometryInfo(org.scijava.java3d.utils.geometry.GeometryInfo)

Example 3 with GeometryInfo

use of org.scijava.java3d.utils.geometry.GeometryInfo in project GDSC-SMLM by aherbert.

the class Shape3DHelper method getNormals.

/**
 * Gets the normals assuming triangle vertices.
 *
 * @param vertices the vertices
 * @param creaseAngle the crease angle (in degrees; 0=facet normals; 180=smooth shading)
 * @return the normals
 */
public static Vector3f[] getNormals(Point3f[] vertices, double creaseAngle) {
    final int nVertices = vertices.length;
    final Vector3f[] normals = new Vector3f[nVertices];
    final GeometryArray ta = new TriangleArray(nVertices, GeometryArray.COORDINATES);
    ta.setCoordinates(0, vertices);
    final GeometryInfo gi = new GeometryInfo(ta);
    final NormalGenerator ng = new NormalGenerator();
    if (creaseAngle >= 0 && creaseAngle <= 180) {
        ng.setCreaseAngle(Math.toRadians(creaseAngle));
    }
    ng.generateNormals(gi);
    final Vector3f[] n = gi.getNormals();
    final int[] indices = gi.getNormalIndices();
    for (int i = 0; i < nVertices; i++) {
        normals[i] = n[indices[i]];
    }
    return normals;
}
Also used : Vector3f(org.scijava.vecmath.Vector3f) GeometryInfo(org.scijava.java3d.utils.geometry.GeometryInfo) GeometryArray(org.scijava.java3d.GeometryArray) NormalGenerator(org.scijava.java3d.utils.geometry.NormalGenerator) TriangleArray(org.scijava.java3d.TriangleArray)

Example 4 with GeometryInfo

use of org.scijava.java3d.utils.geometry.GeometryInfo in project GDSC-SMLM by aherbert.

the class Shape3DHelper method createGeometryInfo.

/**
 * Creates the object used to draw a single localisation.
 *
 * @param rendering the rendering
 * @param colorDepth the color depth
 * @return the geometry info
 */
public static GeometryInfo createGeometryInfo(Rendering rendering, int colorDepth) {
    GeometryInfo gi;
    List<Point3f> coords;
    int primitive = GeometryInfo.TRIANGLE_ARRAY;
    int[] stripsCounts = null;
    final Vector3f normal = new Vector3f();
    boolean normalise = false;
    switch(rendering) {
        // 2D
        case SQUARE:
            primitive = GeometryInfo.QUAD_ARRAY;
            coords = new LocalList<>();
            for (int i = 0; i < 4; i++) {
                coords.add(new Point3f(cubeVertices[i][0], cubeVertices[i][1], 0));
            }
            normalise = true;
            normal.set(0, 0, 1);
            break;
        case HEXAGON:
        case LOW_RES_CIRCLE:
        case HIGH_RES_CIRCLE:
            primitive = GeometryInfo.TRIANGLE_FAN_ARRAY;
            coords = createDiscFan(0, 0, 0, 0, 0, 1, 1, getDiscSubdivisions(rendering));
            stripsCounts = new int[] { coords.size() };
            normal.set(0, 0, 1);
            break;
        // 3D
        case CUBE:
            primitive = GeometryInfo.QUAD_ARRAY;
            coords = new LocalList<>();
            final Point3f[] vertices = new Point3f[8];
            for (int i = 0; i < 8; i++) {
                vertices[i] = new Point3f(cubeVertices[i][0], cubeVertices[i][1], cubeVertices[i][2]);
            }
            for (final int[] face : cubeFaces4) {
                for (int i = 0; i < 4; i++) {
                    coords.add(vertices[face[i]]);
                }
            }
            normalise = true;
            break;
        // All spheres based on icosahedron for speed
        case ICOSAHEDRON:
        case LOW_RES_SPHERE:
        case HIGH_RES_SPHERE:
        case SUPER_HIGH_RES_SPHERE:
            coords = customnode.MeshMaker.createIcosahedron(getIcosahedronSubdivisions(rendering), 1f);
            break;
        case POINT:
        default:
            throw new IllegalStateException("Unknown rendering " + rendering);
    }
    gi = new GeometryInfo(primitive);
    gi.setUseCoordIndexOnly(true);
    if (normalise) {
        normalise(coords);
    }
    if (rendering.is2D()) {
        gi.setCoordinates(coords.toArray(new Point3f[0]));
        gi.setStripCounts(stripsCounts);
        if (colorDepth == 3) {
            gi.setColors3(new float[coords.size() * 3]);
        } else if (colorDepth == 4) {
            gi.setColors4(new float[coords.size() * 4]);
        }
        // Normals generated with the normal generator add extra normals for indexed arrays
        // so we do it manually
        final Vector3f[] normals = new Vector3f[coords.size()];
        Arrays.fill(normals, normal);
        gi.setNormals(normals);
    } else {
        // Generate indexes manually.
        // The GeometryInfo somehow does not do this correctly for all rendering modes.
        // E.g. the icosahedron gets extra indexes to normals that are 0,0,0.
        final Pair<Point3f[], int[]> p = createIndexedObject(coords);
        final Point3f[] iCoords = p.getKey();
        gi.setCoordinates(iCoords);
        gi.setCoordinateIndices(p.getValue());
        // Normals are just the vector from 0,0,0 to the vertex
        final Vector3f[] normals = new Vector3f[iCoords.length];
        for (int i = 0; i < normals.length; i++) {
            normal.set(iCoords[i]);
            normal.normalize();
            normals[i] = new Vector3f(normal);
        }
        gi.setNormals(normals);
        if (colorDepth == 3 || colorDepth == 4) {
            if (colorDepth == 3) {
                gi.setColors3(new float[iCoords.length * 3]);
            } else {
                gi.setColors4(new float[iCoords.length * 4]);
            // gi.setColorIndices(p.b);
            }
        }
    // final NormalGenerator ng = new NormalGenerator();
    // double creaseAngle = (rendering.isHighResolution()) ? Math.PI : 0;
    // ng.setCreaseAngle(creaseAngle);
    // //if (rendering== Rendering.ICOSAHEDRON)
    // // ng.setCreaseAngle(180);
    // ng.generateNormals(gi);
    }
    return gi;
}
Also used : Point3f(org.scijava.vecmath.Point3f) Vector3f(org.scijava.vecmath.Vector3f) GeometryInfo(org.scijava.java3d.utils.geometry.GeometryInfo)

Example 5 with GeometryInfo

use of org.scijava.java3d.utils.geometry.GeometryInfo in project GDSC-SMLM by aherbert.

the class ItemTriangleMesh method getNormals.

/**
 * Gets the normals assuming triangle vertices.
 *
 * @param vertices the vertices
 * @param creaseAngle the crease angle (in degrees)
 * @return the normals
 */
public static Vector3f[] getNormals(Point3f[] vertices, double creaseAngle) {
    final int nVertices = vertices.length;
    final Vector3f[] normals = new Vector3f[nVertices];
    final GeometryArray ta = new TriangleArray(nVertices, GeometryArray.COORDINATES | GeometryArray.NORMALS);
    ta.setCoordinates(0, vertices);
    final GeometryInfo gi = new GeometryInfo(ta);
    final NormalGenerator ng = new NormalGenerator();
    if (creaseAngle >= 0 && creaseAngle <= 180) {
        ng.setCreaseAngle(Math.toRadians(creaseAngle));
    }
    ng.generateNormals(gi);
    final Vector3f[] n = gi.getNormals();
    final int[] indices = gi.getNormalIndices();
    for (int i = 0; i < nVertices; i++) {
        normals[i] = n[indices[i]];
    }
    return normals;
}
Also used : Vector3f(org.scijava.vecmath.Vector3f) GeometryInfo(org.scijava.java3d.utils.geometry.GeometryInfo) GeometryArray(org.scijava.java3d.GeometryArray) NormalGenerator(org.scijava.java3d.utils.geometry.NormalGenerator) TriangleArray(org.scijava.java3d.TriangleArray)

Aggregations

GeometryInfo (org.scijava.java3d.utils.geometry.GeometryInfo)9 GeometryArray (org.scijava.java3d.GeometryArray)6 Vector3f (org.scijava.vecmath.Vector3f)6 TriangleArray (org.scijava.java3d.TriangleArray)5 NormalGenerator (org.scijava.java3d.utils.geometry.NormalGenerator)5 Point3f (org.scijava.vecmath.Point3f)3 Color3f (org.scijava.vecmath.Color3f)2 Color4f (org.scijava.vecmath.Color4f)1