Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some useful and interesting functions #290

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
138 changes: 138 additions & 0 deletions src/main/java/net/imglib2/position/Functions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* #%L
* ImgLib2: a general-purpose, multidimensional image processing library.
* %%
* Copyright (C) 2009 - 2020 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
* John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke,
* Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner,
* Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert,
* Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin,
* Jean-Yves Tinevez and Michael Zinsmaier.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package net.imglib2.position;

import net.imglib2.type.Type;

/**
* Convenience methods to create some typical functions in integer and real
* space.
*
* @author Stephan Saalfeld <[email protected]>
*
*/
public interface Functions
{
/**
* Creates a one pixel checker board pattern.
*
* @param <T>
* @param n number of dimensions
* @param on on value
* @param off off value (at 0^n)
*
* @return
*/
public static < T extends Type< T > > FunctionRandomAccessible< T > checker(
final int n,
final T on,
final T off)
{
return new FunctionRandomAccessible<>(
n,
(position, value ) -> {
boolean isOn = ( position.getLongPosition( 0 ) & 1 ) == 1;
for ( int d = 1; d < n; ++d ) {
isOn ^= ( position.getLongPosition( d ) & 1 ) == 1;
}
if ( isOn )
value.set( on );
else
value.set( off );
},
on::createVariable );
}

/**
* Creates a checker board pattern with a step size of 2<sup>bit</sup>.
*
* @param <T>
* @param n number of dimensions
* @param on on value
* @param off off value (at 0^n)
* @param bit the size of blocks is 2<sup>bit</sup>
*
* @return
*/
public static < T extends Type< T > > FunctionRandomAccessible< T > checker(
final int n,
final T on,
final T off,
final int bit )
{
return new FunctionRandomAccessible<>(
n,
(position, value ) -> {
boolean isOn = ( ( position.getLongPosition( 0 ) >> bit ) & 1 ) == 1;
for ( int d = 1; d < n; ++d ) {
isOn ^= ( ( position.getLongPosition( d ) >> bit ) & 1 ) == 1;
}
if ( isOn )
value.set( on );
else
value.set( off );
},
on::createVariable );
}

/**
* Creates a one pixel checker board pattern.
*
* @param <T>
* @param n number of dimensions
* @param on on value
* @param off off value (at 0^n)
*
* @return
*/
public static < T extends Type< T > > FunctionRealRandomAccessible< T > realChecker(
final int n,
final T on,
final T off)
{
return new FunctionRealRandomAccessible<>(
n,
(position, value ) -> {
boolean isOn = ( ( long )Math.floor( position.getDoublePosition( 0 ) ) & 1 ) == 1;
for ( int d = 1; d < n; ++d ) {
isOn ^= ( ( long )Math.floor( position.getDoublePosition( d ) ) & 1 ) == 1;
}
if ( isOn )
value.set( on );
else
value.set( off );
},
on::createVariable );
}
}
91 changes: 91 additions & 0 deletions src/test/java/net/imglib2/position/FunctionsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package net.imglib2.position;

import static org.junit.Assert.assertEquals;

import org.junit.BeforeClass;
import org.junit.Test;

import net.imglib2.type.numeric.integer.IntType;
import net.imglib2.util.Intervals;
import net.imglib2.view.Views;

public class FunctionsTest
{

private static final String checker1 = "01010";
private static final String checker2 = "01010101010101010101010101010101010";
private static final String checker3 = "01010101010101010101101010101010101010100101010101010101010110101010101010101010010101010101010101011010101010101010101001010101010101010101";

private static final String checker12 = "00110";
private static final String checker22 = "00001000010000100001111101111011110";
private static final String checker32 = "00110001101100111001001100011011001110011100111001001100011011001110010011000110001100011011001110010011000110110011100111001110010011000110";


@BeforeClass
public static void setUpBeforeClass() throws Exception
{}

@Test
public void testChecker()
{
final IntType on = new IntType( 1 );
final IntType off = new IntType( 0 );

int i = 0;
for ( final IntType t : Views.interval( Functions.checker( 1, on, off ), Intervals.createMinSize( 0, 5 ) ) )
{
assertEquals( Integer.parseInt( checker1.substring( i, ++i ) ), t.get() );
}

final int width = 5;
i = 0;
for ( final IntType t : Views.interval( Functions.checker( 2, on, off ), Intervals.createMinSize( 0, 0, width, 7 ) ) ) {
assertEquals( Integer.parseInt( checker2.substring( i, ++i ) ), t.get() );
}

final int height = 4;
i = 0;
for ( final IntType t : Views.interval( Functions.checker( 3, on, off ), Intervals.createMinSize( 0, 0, 0, width, height, 7 ) ) ) {
assertEquals( Integer.parseInt( checker3.substring( i, ++i ) ), t.get() );
}
}

@Test
public void testChecker2()
{
final IntType on = new IntType( 1 );
final IntType off = new IntType( 0 );

int i = 0;
for ( final IntType t : Views.interval( Functions.checker( 1, on, off, 1 ), Intervals.createMinSize( 0, 5 ) ) )
{
// System.out.print( t.get() );
assertEquals( Integer.parseInt( checker12.substring( i, ++i ) ), t.get() );
}

System.out.println( );

final int width = 5;
i = 0;
for ( final IntType t : Views.interval( Functions.checker( 2, on, off, 2 ), Intervals.createMinSize( 0, 0, width, 7 ) ) )
{
// if ( i % width == 0 )
// System.out.println( );
//
// System.out.print( t.get() );
// ++i;
assertEquals( Integer.parseInt( checker22.substring( i, ++i ) ), t.get() );
}

System.out.println( );

final int height = 4;
i = 0;
for ( final IntType t : Views.interval( Functions.checker( 3, on, off, 1 ), Intervals.createMinSize( 0, 0, 0, width, height, 7 ) ) )
{
// System.out.print( t.get() );
assertEquals( Integer.parseInt( checker32.substring( i, ++i ) ), t.get() );
}
}

}