Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,47 @@

package org.scijava.ops.image.transform.project.project;

import java.util.Iterator;

import net.imglib2.RandomAccess;
import net.imglib2.FinalInterval;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.loops.LoopBuilder;
import net.imglib2.util.Intervals;

import net.imglib2.view.Views;
import org.scijava.function.Computers;

/**
* @param <T>
* @param <V>
* @implNote op name='transform.project', priority='99.'
* <b>Projection</b> is the act of creating 1-dimensional slices of an n-dimensional image,
* reducing that slice down to a single value, and combining those images back into a (n-1)-dimensional array
* <p>
* Note that this Op cannot be adapted because the output is necessarily of different dimensionality than the input.
* </p>
*
* @param <T> the type of input image elements
* @param <V> the type of output image elements
* @implNote op name='transform.project', priority='99.', hints="adaptation.FORBIDDEN"
* @see ProjectParallelFunction for an Op that creates its own output
*/
public class DefaultProjectParallel<T, V> implements
Computers.Arity3<RandomAccessibleInterval<T>, Computers.Arity1<Iterable<T>, V>, Integer, RandomAccessibleInterval<V>>
public class ProjectParallelComputer<T, V> implements
Computers.Arity3<
RandomAccessibleInterval<T>,
Computers.Arity1<? super RandomAccessibleInterval<T>, V>,
Integer,
RandomAccessibleInterval<V>
>
{

/**
* TODO
* Projects {@code op} along 1-dimensional slices (along dimension {@code dim}) of {@code input}
*
* @param input
* @param op
* @param dim
* @param output
* @param input the input {@code n}-dimensional dataset
* @param op the Op to project over {@code dim}
* @param dim the dimension along {@code input} to project
* @param output the output {@code n-1}-dimensional dataset
*/
@Override
public void compute(final RandomAccessibleInterval<T> input,
Computers.Arity1<Iterable<T>, V> op, Integer dim,
final RandomAccessibleInterval<V> output)
{
Computers.Arity1<? super RandomAccessibleInterval<T>, V> op, Integer dim,
final RandomAccessibleInterval<V> output) {
// TODO this first check is too simple, but for now ok
if (input.numDimensions() != output.numDimensions() + 1) //
throw new IllegalArgumentException(
Expand All @@ -70,61 +80,18 @@ public void compute(final RandomAccessibleInterval<T> input,

LoopBuilder.setImages(output, Intervals.positions(output)).multiThreaded()
.forEachChunk(chunk -> {
var chunkRA = input.randomAccess();
var min = new long[input.numDimensions()];
var max = new long[input.numDimensions()];
min[dim] = input.min(dim);
max[dim] = input.max(dim);
chunk.forEachPixel((pixel, position) -> {
for (var d = 0; d < input.numDimensions(); d++) {
if (d != dim) {
chunkRA.setPosition(position.getIntPosition(d - (d > dim ? 1
: 0)), d);
}
for (var d = 0; d < position.numDimensions(); d++) {
min[d >= dim ? d+1 : d] = position.getLongPosition(d);
max[d >= dim ? d+1 : d] = position.getLongPosition(d);
}

op.compute(new DimensionIterable(input.dimension(dim), dim, chunkRA),
pixel);

op.compute(Views.interval(input, new FinalInterval(min, max)), pixel);
});

return null;
return chunk;
});
}

final class DimensionIterable implements Iterable<T> {

private final long size;
private final int dim;
private final RandomAccess<T> access;

public DimensionIterable(final long size, final int dim,
final RandomAccess<T> access)
{
this.size = size;
this.dim = dim;
this.access = access;
}

@Override
public Iterator<T> iterator() {
return new Iterator<T>() {

int k = -1;

@Override
public boolean hasNext() {
return k < size - 1;
}

@Override
public T next() {
k++;
access.setPosition(k, dim);
return access.get();
}

@Override
public void remove() {
throw new UnsupportedOperationException("Not supported");
}
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.scijava.ops.image.transform.project.project;

import net.imglib2.FinalDimensions;
import net.imglib2.RandomAccessibleInterval;
import org.scijava.function.Computers;
import org.scijava.function.Functions;
import org.scijava.function.Producer;
import org.scijava.ops.spi.OpDependency;

import java.util.function.BiFunction;

/**
* Wraps {@link ProjectParallelComputer}, but creates a new output image in the process.
* @param <T> the type of input image elements
* @param <V> the type of output image elements
* @author Gabriel Selzer
* @implNote op name='transform.project', priority='99.'
*/
public class ProjectParallelFunction<T, V> implements
Functions.Arity3<RandomAccessibleInterval<T>, Computers.Arity1<? super RandomAccessibleInterval<T>, V>, Integer, RandomAccessibleInterval<V>>
{
@OpDependency(name="transform.project")
Computers.Arity3<RandomAccessibleInterval<T>, Computers.Arity1<? super RandomAccessibleInterval<T>, V>, Integer, RandomAccessibleInterval<V>> projector;

@OpDependency(name="transform.translateView")
BiFunction<RandomAccessibleInterval<V>, long[], RandomAccessibleInterval<V>> translator;

@OpDependency(name="create.type")
Producer<V> typeCreator;

@OpDependency(name="create.img")
BiFunction<FinalDimensions, V, RandomAccessibleInterval<V>> creator;


/**
* Projects {@code op} along 1-dimensional slices (along dimension {@code dim}) of {@code input}
*
* @param input the input {@code n}-dimensional dataset
* @param op the Op to project over {@code dim}
* @param dim the dimension along {@code input} to project
* @return a {@code n-1}-dimensional dataset
*/
@Override
public RandomAccessibleInterval<V> apply(RandomAccessibleInterval<T> input, Computers.Arity1<? super RandomAccessibleInterval<T>, V> op, Integer dim) {
var dims = new long[input.numDimensions() - 1];
var min = new long[input.numDimensions() - 1];
for(int i = 0; i < input.numDimensions() - 1; i++) {
Comment thread
gselzer marked this conversation as resolved.
dims[i] = input.dimension(i >= dim ? i+1 : i);
min[i] = input.min(i >= dim ? i+1 : i);
}
// Get an arbitrary instance of the output type
var outImg = creator.apply(new FinalDimensions(dims), typeCreator.create());
// translate by the minimum of the input img
var translated = translator.apply(outImg, min);

projector.compute(input, op, dim, translated);
return translated;
}
}

This file was deleted.

Loading
Loading