Search in sources :

Example 1 with GeometryUpdater

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

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

the class ItemPointMesh 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) {
        mesh.clear();
        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();
    // Reorder all things in the geometry: coordinates and colour
    final Point3f[] oldCoords = mesh.toArray(new Point3f[oldSize]);
    final float[] oldColors = new float[oldSize * 3];
    ga.getColors(0, oldColors);
    final Point3f[] coords = new Point3f[size];
    final float[] colors = new float[size * 3];
    for (int i = 0; i < size; i++) {
        final int j = indices[i];
        coords[i] = oldCoords[j];
        System.arraycopy(oldColors, j * 3, colors, i * 3, 3);
    }
    mesh = Arrays.asList(coords);
    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);
            ga.setColors(0, colors);
            ga.setValidVertexCount(coords.length);
        }
    });
// this.setGeometry(ga);
}
Also used : GeometryUpdater(org.scijava.java3d.GeometryUpdater) Geometry(org.scijava.java3d.Geometry) Point3f(org.scijava.vecmath.Point3f) GeometryArray(org.scijava.java3d.GeometryArray)

Aggregations

Geometry (org.scijava.java3d.Geometry)2 GeometryArray (org.scijava.java3d.GeometryArray)2 GeometryUpdater (org.scijava.java3d.GeometryUpdater)2 GeometryStripArray (org.scijava.java3d.GeometryStripArray)1 IndexedGeometryArray (org.scijava.java3d.IndexedGeometryArray)1 IndexedGeometryStripArray (org.scijava.java3d.IndexedGeometryStripArray)1 Point3f (org.scijava.vecmath.Point3f)1