Search in sources :

Example 11 with GeometryArray

use of org.scijava.java3d.GeometryArray in project GDSC-SMLM by aherbert.

the class TransparentItemPointMesh method setItemAlpha.

@Override
public void setItemAlpha(float[] alpha) {
    final int size = size();
    ItemHelper.checkSize(alpha.length, size);
    final GeometryArray ga = (GeometryArray) getGeometry();
    if (ga == null) {
        return;
    }
    final float[] colors = new float[4 * size];
    ga.getColors(0, colors);
    for (int i = 0; i < size; i++) {
        // Set only alpha
        colors[i * 4 + 3] = alpha[i];
    }
    ga.setColors(0, colors);
    changed = true;
}
Also used : GeometryArray(org.scijava.java3d.GeometryArray)

Example 12 with GeometryArray

use of org.scijava.java3d.GeometryArray in project GDSC-SMLM by aherbert.

the class ItemMesh method reorderFast.

@Override
public void reorderFast(int[] indices) {
    changed = true;
    final int oldSize = size();
    final int size = (indices == null) ? 0 : Math.min(oldSize, indices.length);
    if (size == 0 || indices == null) {
        points = new Point3f[0];
        sizes = new Point3f[0];
        this.setGeometry(null);
        return;
    }
    // From here on we assume the current geometry will not be null
    // as this only happens when the original size is zero. Size has
    // been checked at this point to be the smaller of new and old.
    final GeometryArray ga = (GeometryArray) getGeometry();
    points = reorderPoints(points, indices);
    // Sizes could be null or a single size
    if (sizes != null && sizes.length == points.length) {
        sizes = reorderPoints(sizes, indices);
    }
    // Reorder all things in the geometry: coordinates and colour.
    // The normals, indices, strip counts are are unchanged.
    // int objectSize = vertexCount;
    int countPerObject = vertexCount * 3;
    final float[] oldCoords = new float[oldSize * countPerObject];
    ga.getCoordinates(0, oldCoords);
    final float[] coords = new float[size * countPerObject];
    for (int i = 0; i < size; i++) {
        final int j = indices[i];
        final int ii = i * countPerObject;
        final int jj = j * countPerObject;
        System.arraycopy(oldCoords, jj, coords, ii, countPerObject);
    }
    final float[] colors;
    if (hasColor()) {
        countPerObject = colorUpdater.size();
        final int colorSize = oldSize * countPerObject;
        final float[] oldColors = (colorSize < oldCoords.length) ? oldCoords : new float[colorSize];
        ga.getColors(0, oldColors);
        colors = new float[size * countPerObject];
        for (int i = 0; i < size; i++) {
            final int j = indices[i];
            final int ii = i * countPerObject;
            final int jj = j * countPerObject;
            System.arraycopy(oldColors, jj, colors, ii, countPerObject);
        }
    } else {
        colors = null;
    }
    ga.updateData(new GeometryUpdater() {

        @Override
        public void updateData(Geometry geometry) {
            final GeometryArray ga = (GeometryArray) geometry;
            // We re-use the geometry and just truncate the vertex count
            ga.setCoordinates(0, coords);
            if (colors != null) {
                ga.setColors(0, colors);
            }
            if (size != oldSize) {
                if (isIndexGeometryArray()) {
                    if (isStripGeometryArray()) {
                        int[] indices = new int[indexCount * oldSize];
                        ((IndexedGeometryStripArray) ga).getStripIndexCounts(indices);
                        indices = Arrays.copyOf(indices, indexCount * size);
                        ((IndexedGeometryStripArray) ga).setStripIndexCounts(indices);
                    } else {
                        ((IndexedGeometryArray) ga).setValidIndexCount(size * indexCount);
                    }
                } else if (isStripGeometryArray()) {
                    int[] indices = new int[vertexCount * oldSize];
                    ((GeometryStripArray) ga).getStripVertexCounts(indices);
                    indices = Arrays.copyOf(indices, vertexCount * size);
                    ((GeometryStripArray) ga).setStripVertexCounts(indices);
                } else {
                    ga.setValidVertexCount(size * vertexCount);
                }
            }
        }
    });
}
Also used : GeometryUpdater(org.scijava.java3d.GeometryUpdater) Geometry(org.scijava.java3d.Geometry) GeometryStripArray(org.scijava.java3d.GeometryStripArray) IndexedGeometryStripArray(org.scijava.java3d.IndexedGeometryStripArray) IndexedGeometryArray(org.scijava.java3d.IndexedGeometryArray) GeometryArray(org.scijava.java3d.GeometryArray)

Example 13 with GeometryArray

use of org.scijava.java3d.GeometryArray in project GDSC-SMLM by aherbert.

the class ItemMesh method setItemColor.

@Override
public void setItemColor(Color3f color) {
    if (color == null) {
        color = DEFAULT_COLOR;
    }
    this.color = color;
    if (!hasColor()) {
        if (isColorByMaterial) {
            getAppearance().getMaterial().setDiffuseColor(color);
        } else {
            getAppearance().getColoringAttributes().setColor(color);
        }
        return;
    }
    final int size = size();
    final GeometryArray ga = (GeometryArray) getGeometry();
    if (ga == null) {
        return;
    }
    final int n = colorUpdater.size();
    final float[] colors = new float[size * n];
    if (hasColor3()) {
        final float[] tmp = new float[3];
        color.get(tmp);
        duplicate(tmp, 0, 3, colors.length / 3, colors, 0);
        ga.setColors(0, colors);
    } else {
        // Preserve alpha
        ga.getColors(0, colors);
        for (int i = 0; i < colors.length; i += 4) {
            colors[i] = color.x;
            colors[i + 1] = color.y;
            colors[i + 2] = color.z;
        }
        ga.setColors(0, colors);
    }
    changed = true;
}
Also used : IndexedGeometryArray(org.scijava.java3d.IndexedGeometryArray) GeometryArray(org.scijava.java3d.GeometryArray)

Example 14 with GeometryArray

use of org.scijava.java3d.GeometryArray in project GDSC-SMLM by aherbert.

the class ItemMesh method setItemColor4.

@Override
public void setItemColor4(Color4f[] color) {
    if (!hasColor4()) {
        throw new IllegalArgumentException("Per-item alpha not supported");
    }
    this.color = null;
    final int size = size();
    if (color.length != size) {
        throw new IllegalArgumentException("list of size " + size + " expected");
    }
    final GeometryArray ga = (GeometryArray) getGeometry();
    if (ga == null) {
        return;
    }
    final int n = colorUpdater.size();
    final float[] colors = new float[size * n];
    for (int i = 0; i < color.length; i++) {
        System.arraycopy(colorUpdater.getColors(color[i]), 0, colors, i * n, n);
    }
    ga.setColors(0, colors);
    changed = true;
}
Also used : IndexedGeometryArray(org.scijava.java3d.IndexedGeometryArray) GeometryArray(org.scijava.java3d.GeometryArray)

Example 15 with GeometryArray

use of org.scijava.java3d.GeometryArray in project GDSC-SMLM by aherbert.

the class ItemMesh method createGeometry.

/**
 * Creates the geometry.
 *
 * @param coords the coords
 * @param sourceGa the source geometry array
 * @return the geometry array
 */
protected GeometryArray createGeometry(float[] coords, GeometryArray sourceGa) {
    final GeometryArray ga = createGeometryArray(sourceGa, 0);
    ga.setCoordinates(0, coords);
    ga.setCapability(GeometryArray.ALLOW_COORDINATE_WRITE);
    // Handle normals
    boolean doNormals = hasNormals();
    // Handle colors
    if (hasColor()) {
        ga.setCapability(GeometryArray.ALLOW_COLOR_READ);
        ga.setCapability(GeometryArray.ALLOW_COLOR_WRITE);
        colorUpdater = ArrayColorUpdater.create(vertexCount, hasColor4());
    }
    // Handle indexed array
    if (isIndexGeometryArray()) {
        final IndexedGeometryArray sourceIga = (IndexedGeometryArray) sourceGa;
        final IndexedGeometryArray iga = (IndexedGeometryArray) ga;
        final int objectIndexCount = sourceIga.getValidIndexCount();
        final int[] objectIndices = new int[objectIndexCount];
        final int[] allIndices = new int[objectIndices.length * points.length];
        sourceIga.getCoordinateIndices(0, objectIndices);
        duplicateIndices(objectIndices, allIndices);
        iga.setCoordinateIndices(0, allIndices);
        // Check if we need the color and normal indices
        if ((vertexFormat & GeometryArray.USE_COORD_INDEX_ONLY) != 0) {
            if (hasNormals()) {
            // Done later
            }
            if (hasColor()) {
            // Update the colour for each vertex as normal
            }
        } else {
            if (hasNormals()) {
                // Use the same index for all vertices for normals
                sourceIga.getNormalIndices(0, objectIndices);
                duplicate(objectIndices, 0, objectIndices.length, points.length, allIndices, 0);
                iga.setNormalIndices(0, allIndices);
                final float[] normals = new float[(MathUtils.max(objectIndices) + 1) * 3];
                sourceIga.getNormals(0, normals);
                iga.setNormals(0, normals);
                doNormals = false;
            }
            if (hasColor()) {
                // Use a single index per item for vertex colour
                for (int i = 0, k = 0; i < points.length; i++) {
                    for (int j = 0; j < objectIndexCount; j++) {
                        allIndices[k++] = i;
                    }
                }
                iga.setColorIndices(0, allIndices);
                // Only have to update a single colour per item
                colorUpdater = ArrayColorUpdater.create(1, hasColor4());
            }
        }
    }
    if (doNormals) {
        final float[] objectNormals = new float[vertexCount * 3];
        sourceGa.getNormals(0, objectNormals);
        final float[] allNormals = new float[objectNormals.length * points.length];
        duplicate(objectNormals, 0, objectNormals.length, points.length, allNormals, 0);
        ga.setNormals(0, allNormals);
    }
    return ga;
}
Also used : IndexedGeometryArray(org.scijava.java3d.IndexedGeometryArray) IndexedGeometryArray(org.scijava.java3d.IndexedGeometryArray) GeometryArray(org.scijava.java3d.GeometryArray)

Aggregations

GeometryArray (org.scijava.java3d.GeometryArray)52 IndexedGeometryArray (org.scijava.java3d.IndexedGeometryArray)21 Point3f (org.scijava.vecmath.Point3f)11 TriangleArray (org.scijava.java3d.TriangleArray)6 GeometryInfo (org.scijava.java3d.utils.geometry.GeometryInfo)6 NormalGenerator (org.scijava.java3d.utils.geometry.NormalGenerator)5 Color3f (org.scijava.vecmath.Color3f)5 Shape3D (org.scijava.java3d.Shape3D)4 Vector3f (org.scijava.vecmath.Vector3f)4 Appearance (org.scijava.java3d.Appearance)3 IndexedGeometryStripArray (org.scijava.java3d.IndexedGeometryStripArray)3 PointArray (org.scijava.java3d.PointArray)3 Color4f (org.scijava.vecmath.Color4f)3 Rendering (uk.ac.sussex.gdsc.smlm.ij.ij3d.Shape3DHelper.Rendering)3 CustomMesh (customnode.CustomMesh)2 Geometry (org.scijava.java3d.Geometry)2 GeometryStripArray (org.scijava.java3d.GeometryStripArray)2 GeometryUpdater (org.scijava.java3d.GeometryUpdater)2 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)2 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)2