Search in sources :

Example 1 with NormalGenerator

use of org.scijava.java3d.utils.geometry.NormalGenerator 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 2 with NormalGenerator

use of org.scijava.java3d.utils.geometry.NormalGenerator 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)

Example 3 with NormalGenerator

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

the class ItemTriangleMesh method createGeometry.

@Override
protected GeometryArray createGeometry() {
    if (mesh == null || mesh.size() < 3) {
        return null;
    }
    final int vertexCount = mesh.size();
    final Point3f[] coords = new Point3f[vertexCount];
    mesh.toArray(coords);
    // Do not try to get the colour back from the geometry as is done
    // in the super-class. That will only work if the size is the same
    // and this method is likely to be called when the size changes.
    final Color3f[] colors = new Color3f[vertexCount];
    Arrays.fill(colors, (color == null) ? DEFAULT_COLOR : color);
    final GeometryArray ta = new TriangleArray(vertexCount, GeometryArray.COORDINATES | GeometryArray.COLOR_3 | GeometryArray.NORMALS);
    ta.setCoordinates(0, coords);
    ta.setColors(0, colors);
    // generate normals
    final GeometryArray result;
    if (dirty) {
        final GeometryInfo gi = new GeometryInfo(ta);
        final NormalGenerator ng = new NormalGenerator();
        ng.generateNormals(gi);
        result = gi.getGeometryArray();
    } else {
        // Use the same normals for each repeated object
        final Vector3f[] normals = new Vector3f[vertexCount];
        // Binary fill
        int fill = objectNormals.length;
        System.arraycopy(objectNormals, 0, normals, 0, fill);
        for (int i = 2; i < points.length; i *= 2) {
            System.arraycopy(normals, 0, normals, fill, fill);
            fill *= 2;
        }
        // Final fill
        System.arraycopy(normals, 0, normals, fill, normals.length - fill);
        ta.setNormals(0, normals);
        result = ta;
    }
    result.setCapability(GeometryArray.ALLOW_NORMAL_WRITE);
    result.setCapability(GeometryArray.ALLOW_COLOR_WRITE);
    result.setCapability(GeometryArray.ALLOW_COORDINATE_WRITE);
    result.setCapability(GeometryArray.ALLOW_COUNT_WRITE);
    result.setCapability(GeometryArray.ALLOW_COUNT_READ);
    result.setCapability(GeometryArray.ALLOW_FORMAT_READ);
    result.setCapability(Geometry.ALLOW_INTERSECT);
    result.setValidVertexCount(vertexCount);
    return result;
}
Also used : Point3f(org.scijava.vecmath.Point3f) Color3f(org.scijava.vecmath.Color3f) 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 NormalGenerator

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

the class TransparentItemTriangleMesh method createGeometry.

@Override
protected GeometryArray createGeometry() {
    if (mesh == null || mesh.size() < 3) {
        return null;
    }
    final int vertexCount = mesh.size();
    final Point3f[] coords = new Point3f[vertexCount];
    mesh.toArray(coords);
    final Color4f[] colors = new Color4f[vertexCount];
    if (color == null) {
        color = DEFAULT_COLOR;
    }
    Arrays.fill(colors, new Color4f(color.x, color.y, color.z, 1));
    final GeometryArray ta = new TriangleArray(vertexCount, GeometryArray.COORDINATES | GeometryArray.COLOR_4 | GeometryArray.NORMALS);
    ta.setCoordinates(0, coords);
    ta.setColors(0, colors);
    // generate normals
    final GeometryArray result;
    if (dirty) {
        final GeometryInfo gi = new GeometryInfo(ta);
        final NormalGenerator ng = new NormalGenerator();
        ng.generateNormals(gi);
        result = gi.getGeometryArray();
    } else {
        // Use the same normals for each repeated object
        final Vector3f[] normals = new Vector3f[vertexCount];
        // Binary fill
        int fill = objectNormals.length;
        System.arraycopy(objectNormals, 0, normals, 0, fill);
        for (int i = 2; i < points.length; i *= 2) {
            System.arraycopy(normals, 0, normals, fill, fill);
            fill *= 2;
        }
        // Final fill
        System.arraycopy(normals, 0, normals, fill, normals.length - fill);
        ta.setNormals(0, normals);
        result = ta;
    }
    result.setCapability(GeometryArray.ALLOW_NORMAL_WRITE);
    result.setCapability(GeometryArray.ALLOW_COLOR_WRITE);
    result.setCapability(GeometryArray.ALLOW_COORDINATE_WRITE);
    result.setCapability(GeometryArray.ALLOW_COUNT_WRITE);
    result.setCapability(GeometryArray.ALLOW_COUNT_READ);
    result.setCapability(GeometryArray.ALLOW_FORMAT_READ);
    result.setCapability(Geometry.ALLOW_INTERSECT);
    result.setValidVertexCount(vertexCount);
    return result;
}
Also used : Point3f(org.scijava.vecmath.Point3f) Color4f(org.scijava.vecmath.Color4f) 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 5 with NormalGenerator

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

the class Shape3DHelper method getIndexedNormals.

/**
 * 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 Pair<Vector3f[], int[]> getIndexedNormals(Point3f[] vertices, double creaseAngle) {
    final int nVertices = vertices.length;
    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);
    return Pair.of(gi.getNormals(), gi.getNormalIndices());
}
Also used : 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

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