Skip to content

Commit

Permalink
Unify the I/O logic
Browse files Browse the repository at this point in the history
All SciView I/O is now handled by IOPlugins:
* The Scenery-based OBJ reader is now wrapped in an IOPlugin.
* The XYZ reader is now provided by imagej-mesh-io.
* The Scenery-based STL reader is broken as of 0.3.1;
  we eschew its use in favor of imagej-mesh-io's STL reader.

Consequently, the format-specific SciView#add* methods have been removed
in favor of a unified open(String) method that invokes the IOService and
sports case logic for each sort of data that might be loaded.
  • Loading branch information
ctrueden committed May 3, 2018
1 parent 6eca517 commit 60894c9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 136 deletions.
59 changes: 26 additions & 33 deletions src/main/java/sc/iview/SciView.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@
import com.sun.javafx.application.PlatformImpl;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -66,10 +64,6 @@
import org.scijava.log.LogService;
import org.scijava.menu.MenuService;
import org.scijava.plugin.Parameter;

import org.scijava.ui.UIService;
import org.scijava.ui.behaviour.Behaviour;
import org.scijava.ui.behaviour.BehaviourMap;
import org.scijava.ui.behaviour.ClickBehaviour;

import sc.iview.javafx.JavaFXMenuCreator;
Expand Down Expand Up @@ -608,24 +602,6 @@ public void writeSCMesh( String filename, Mesh scMesh ) {

}

public graphics.scenery.Node addSTL( String filename ) {

Mesh scMesh = new Mesh();
scMesh.readFromSTL( filename );

addMesh( scMesh );
return scMesh;
}

public graphics.scenery.Node addObj( String filename ) {
Mesh scMesh = new Mesh();
scMesh.readFromOBJ( filename, false );// Could check if there is a MTL to use to toggle flag

addMesh( scMesh );

return scMesh;
}

public float getDefaultPointSize() {
return 0.025f;
}
Expand Down Expand Up @@ -653,16 +629,33 @@ public float[] makeNormalsFromVertices( ArrayList<RealPoint> verts ) {
return normals;
}

public graphics.scenery.Node addXyz( String filename ) throws IOException {
final Object data = io.open( filename );
if( !( data instanceof List ) || //
( ( List ) data ).isEmpty() || //
!( ( ( List ) data ).get( 0 ) instanceof RealLocalizable ) ) {
throw new IllegalArgumentException( "File '" + filename + "' is not a point collection." );
public void open( final String source ) throws IOException {
final Object data = io.open( source );
if( data instanceof net.imagej.mesh.Mesh ) addMesh( ( net.imagej.mesh.Mesh ) data );
else if( data instanceof graphics.scenery.Mesh ) addMesh( ( graphics.scenery.Mesh ) data );
else if( data instanceof List ) {
final List<?> list = ( List<?> ) data;
if( list.isEmpty() ) {
throw new IllegalArgumentException( "Data source '" + source + "' appears empty." );
}
final Object element = list.get( 0 );
if( element instanceof RealLocalizable ) {
// NB: For now, we assume all elements will be RealLocalizable.
// Highly likely to be the case, barring antagonistic importers.
@SuppressWarnings("unchecked")
final List<? extends RealLocalizable> points = ( List<? extends RealLocalizable> ) list;
addPointCloud( points, source );
}
else {
final String type = element == null ? "<null>" : element.getClass().getName();
throw new IllegalArgumentException( "Data source '" + source + //
"' contains elements of unknown type '" + type + "'" );
}
} else {
final String type = data == null ? "<null>" : data.getClass().getName();
throw new IllegalArgumentException( "Data source '" + source + //
"' contains data of unknown type '" + type + "'" );
}
@SuppressWarnings("unchecked")
final List<? extends RealLocalizable> points = ( List<? extends RealLocalizable> ) data;
return addPointCloud( points, filename );
}

public graphics.scenery.Node addPointCloud( Collection<? extends RealLocalizable> points ) {
Expand Down
65 changes: 0 additions & 65 deletions src/main/java/sc/iview/io/ImportXYZ.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,23 @@
*/
package sc.iview.io;

import java.io.File;

import org.scijava.command.Command;
import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.io.AbstractIOPlugin;
import org.scijava.io.IOPlugin;
import org.scijava.plugin.Plugin;

import sc.iview.SciView;

@Plugin(type = Command.class, menuRoot = "SciView", menuPath = "Import>STL", label = "Import STL")
public class ImportSTL implements Command {

@Parameter
private File stlFile;

@Parameter
SciView sciView;

@Parameter
private LogService logService;
/** {@link IOPlugin} adapter for Scenery OBJ reader. */
@Plugin(type = IOPlugin.class)
public class OBJMeshIO extends AbstractIOPlugin<graphics.scenery.Mesh> {

@Override
public void run() {
if( stlFile != null ) {
try {
sciView.addSTL( stlFile.getAbsolutePath() );
} catch( final Exception e ) {
logService.trace( e );
}
}
public graphics.scenery.Mesh open( final String source ) {
final graphics.scenery.Mesh mesh = new graphics.scenery.Mesh();
mesh.readFromOBJ( source, false );
return mesh;
}

@Override
public Class<graphics.scenery.Mesh> getDataType() {
return graphics.scenery.Mesh.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,42 @@
package sc.iview.io;

import java.io.File;
import java.io.IOException;
import java.util.List;

import net.imglib2.RealLocalizable;

import org.scijava.command.Command;
import org.scijava.io.IOService;
import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

import sc.iview.SciView;

@Plugin(type = Command.class, menuRoot = "SciView", menuPath = "Import>Obj")
public class ImportObj implements Command {
@Plugin(type = Command.class, menuRoot = "SciView", menuPath = "File>Open...")
public class OpenCommand implements Command {

@Parameter
private File objFile;
private IOService io;

@Parameter
SciView sciView;
private LogService log;

@Parameter
private LogService logService;
private SciView sciView;

// TODO: Find a more extensible way than hard-coding the extensions.
@Parameter(style = "open,extensions:obj/ply/stl/xyz")
private File file;

@Override
public void run() {
if( objFile != null ) {
try {
sciView.addObj( objFile.getAbsolutePath() );
} catch( final Exception e ) {
logService.trace( e );
}
try {
sciView.open( file.getAbsolutePath() );
}
catch (final IOException | IllegalArgumentException exc) {
log.error( exc );
}
}

}

0 comments on commit 60894c9

Please sign in to comment.