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

Rename the triangles and vertices interface size() method to return an int. #3

Merged
merged 2 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/main/java/net/imglib2/mesh/Meshes.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static RealPoint center( final Mesh m )
p.move( v );

for ( int d = 0; d < 3; d++ )
p.setPosition( p.getDoublePosition( d ) / m.vertices().size(), d );
p.setPosition( p.getDoublePosition( d ) / m.vertices().sizel(), d );

return p;
}
Expand Down Expand Up @@ -366,7 +366,7 @@ public static Iterable< BufferMesh > connectedComponents( final Mesh mesh )
public static void scale( final Mesh mesh, final double[] scale )
{
final Vertices vertices = mesh.vertices();
final long nVertices = vertices.size();
final long nVertices = vertices.sizel();
for ( long i = 0; i < nVertices; i++ )
{
final double x = vertices.x( i );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static final int n( final Mesh mesh )
{
final Triangles triangles = mesh.triangles();
final Vertices vertices = mesh.vertices();
final int nVertices = ( int ) vertices.size();
final int nVertices = ( int ) vertices.sizel();
tinevez marked this conversation as resolved.
Show resolved Hide resolved

final TIntObjectHashMap< TIntArrayList > map = triangleMap( mesh );
final BitSet visited = new BitSet( nVertices );
Expand Down Expand Up @@ -122,9 +122,9 @@ private static final boolean visit( final int v, final BitSet visited, final TIn
private static final TIntObjectHashMap< TIntArrayList > triangleMap( final Mesh mesh )
{
final Triangles triangles = mesh.triangles();
final int nTriangles = ( int ) triangles.size();
final int nTriangles = ( int ) triangles.sizel();
final Vertices vertices = mesh.vertices();
final int nVertices = ( int ) vertices.size();
final int nVertices = ( int ) vertices.sizel();

final TIntObjectHashMap< TIntArrayList > map = new TIntObjectHashMap<>( nVertices );
for ( int tid = 0; tid < nTriangles; tid++ )
Expand Down Expand Up @@ -166,7 +166,7 @@ public MyCCIterator( final Mesh mesh )
{
this.mesh = mesh;
this.map = triangleMap( mesh );
this.nVertices = ( int ) mesh.vertices().size();
this.nVertices = ( int ) mesh.vertices().sizel();
this.visited = new BitSet( nVertices );
this.currentStartVertex = 0;
this.next = prefetch();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/imglib2/mesh/alg/MeshCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public long getLongPosition( final int d )
@Override
public Cursor< T > copyCursor()
{
final BufferMesh dest = new BufferMesh( mesh.vertices().isize(), mesh.triangles().isize() );
final BufferMesh dest = new BufferMesh( mesh.vertices().size(), mesh.triangles().size() );
Meshes.copy( mesh, dest );
return new MeshCursor<>( ra.copyRandomAccess(), dest, cal.clone() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class RemoveDuplicateVertices
public static Mesh calculate( final Mesh mesh, final int precision )
{
final Map< String, IndexedVertex > vertices = new LinkedHashMap<>();
final int[][] triangles = new int[ ( int ) mesh.triangles().size() ][ 3 ];
final int[][] triangles = new int[ ( int ) mesh.triangles().sizel() ][ 3 ];

int trianglesCount = 0;
for ( final net.imglib2.mesh.obj.Triangle triangle : mesh.triangles() )
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/imglib2/mesh/alg/SimplifyMesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ private void readMesh()
vertices.clear();
refs.clear();

final Point[] meshVerts = new Point[ ( int ) inMesh.vertices().size() ];
final Point[] meshVerts = new Point[ ( int ) inMesh.vertices().sizel() ];
final Iterator< net.imglib2.mesh.obj.Vertex > iterator = inMesh.vertices().iterator();
for ( int i = 0; i < inMesh.vertices().size(); i++ )
for ( int i = 0; i < inMesh.vertices().sizel(); i++ )
{
final Point simpleVertex = new Point();
simpleVertex.setPosition( iterator.next() );
Expand All @@ -256,7 +256,7 @@ private void readMesh()
int triIndex = 0;

final Iterator< net.imglib2.mesh.obj.Triangle > iteratorTriangles = inMesh.triangles().iterator();
for ( int i = 0; i < inMesh.triangles().size(); i++ )
for ( int i = 0; i < inMesh.triangles().sizel(); i++ )
{
final net.imglib2.mesh.obj.Triangle tria = iteratorTriangles.next();
final Triangle t = new Triangle(
Expand Down Expand Up @@ -288,7 +288,7 @@ private void readMesh()
public Mesh simplify( final float target_percent, final double agressiveness )
{

final int target_count = ( int ) ( inMesh.triangles().size() * target_percent );
final int target_count = ( int ) ( inMesh.triangles().sizel() * target_percent );
return simplify( target_count, agressiveness );
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/net/imglib2/mesh/alg/TaubinSmoothing.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public static final BufferMesh smooth( final Mesh mesh, final int iters, final d
public static final BufferMesh smooth( final Mesh mesh, final int iters, final double lambda, final double mu,
final TaubinWeightType weightType )
{
final int nvs = ( int ) mesh.vertices().size();
final int nts = ( int ) mesh.triangles().size();
final int nvs = ( int ) mesh.vertices().sizel();
final int nts = ( int ) mesh.triangles().sizel();
final BufferMesh meshA = new BufferMesh( nvs, nts );
Meshes.copy( mesh, meshA );
final BufferMesh meshB = new BufferMesh( nvs, nts );
Expand Down Expand Up @@ -114,7 +114,7 @@ public static final BufferMesh smooth( final Mesh mesh, final int iters, final d
throw new IllegalArgumentException( "Unhandled weight type: " + weightType );
}

final BufferMesh out = new BufferMesh( ( int ) meshA.vertices().size(), ( int ) meshA.triangles().size() );
final BufferMesh out = new BufferMesh( ( int ) meshA.vertices().sizel(), ( int ) meshA.triangles().sizel() );
Meshes.calculateNormals( meshA, out );
return out;
}
Expand All @@ -123,8 +123,8 @@ private static void smoothStepCotangent( final Triangles triangles, final Buffer
final double[] trace, final double weigth )
{

final int nvs = ( int ) source.vertices().size();
final int nts = ( int ) source.triangles().size();
final int nvs = ( int ) source.vertices().sizel();
final int nts = ( int ) source.triangles().sizel();

// Zero target.
for ( int i = 0; i < nvs; i++ )
Expand Down Expand Up @@ -205,8 +205,8 @@ private static void smoothStepNaive( final Triangles triangles, final BufferMesh
final double[] trace, final double weigth )
{

final int nvs = ( int ) source.vertices().size();
final int nts = ( int ) source.triangles().size();
final int nvs = ( int ) source.vertices().sizel();
final int nts = ( int ) source.triangles().sizel();

// Zero target.
for ( int i = 0; i < nvs; i++ )
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/imglib2/mesh/alg/zslicer/ZSlicer.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public static List< Slice > slices( final Mesh mesh, final double[] zs, final do
final Triangles triangles = mesh.triangles();
final Vertices vertices = mesh.vertices();

final double[] minZs = new double[ ( int ) triangles.size() ];
final double[] maxZs = new double[ ( int ) triangles.size() ];
for ( int t = 0; t < triangles.size(); t++ )
final double[] minZs = new double[ ( int ) triangles.sizel() ];
final double[] maxZs = new double[ ( int ) triangles.sizel() ];
for ( int t = 0; t < triangles.sizel(); t++ )
{
final long v0 = triangles.vertex0( t );
final long v1 = triangles.vertex1( t );
Expand Down Expand Up @@ -256,7 +256,7 @@ public static Slice slice( final Mesh mesh, final double z, final double zScale
final Vertices vertices = mesh.vertices();

final TIntArrayList intersecting = new TIntArrayList();
for ( long f = 0; f < triangles.size(); f++ )
for ( long f = 0; f < triangles.sizel(); f++ )
{
final long v0 = triangles.vertex0( f );
final long v1 = triangles.vertex1( f );
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/net/imglib2/mesh/io/ply/PLYMeshIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,17 @@ public static final byte[] writeBinary( final Mesh mesh )
"format binary_little_endian 1.0\n" + //
"comment This binary PLY mesh was created with imagej-mesh.\n";
final String vertexHeader = "" + //
"element vertex " + mesh.vertices().size() + "\n" + //
"element vertex " + mesh.vertices().sizel() + "\n" + //
"property float x\nproperty float y\nproperty float z\n" + //
"property float nx\nproperty float ny\nproperty float nz\n" + //
"property float u\nproperty float v\n";
final String triangleHeader = "element face " + mesh.triangles().size()
final String triangleHeader = "element face " + mesh.triangles().sizel()
+ "\nproperty list uchar int vertex_index\n";
final String endHeader = "end_header\n";
final long bytes = header.getBytes().length + //
vertexHeader.getBytes().length + triangleHeader.getBytes().length + endHeader.getBytes().length
+ mesh.vertices().size() * vertexBytes + //
mesh.triangles().size() * triangleBytes;
+ mesh.vertices().sizel() * vertexBytes + //
mesh.triangles().sizel() * triangleBytes;
if ( bytes > Integer.MAX_VALUE )
throw new IllegalArgumentException( "Mesh data too large: " + bytes );

Expand All @@ -246,12 +246,12 @@ public static final byte[] writeBinary( final Mesh mesh )
buffer.put( endHeader.getBytes() );

// Do not populate file if there are no vertices
if ( mesh.vertices().size() == 0 )
if ( mesh.vertices().sizel() == 0 )
return buffer.array();

// Write vertices
final TLongIntHashMap refToVertId = //
new TLongIntHashMap( ( int ) mesh.vertices().size() );
new TLongIntHashMap( ( int ) mesh.vertices().sizel() );
int vertId = 0;
for ( final Vertex v : mesh.vertices() )
{
Expand Down Expand Up @@ -282,21 +282,21 @@ public static final byte[] writeBinary( final Mesh mesh )
public static final byte[] writeAscii( final Mesh mesh ) throws IOException
{
final String header = "ply\nformat ascii 1.0\ncomment This binary PLY mesh was created with imagej-mesh.\n";
final String vertexHeader = "element vertex " + mesh.vertices().size()
final String vertexHeader = "element vertex " + mesh.vertices().sizel()
+ "\nproperty float x\nproperty float y\nproperty float z\nproperty float nx\nproperty float ny\nproperty float nz\nproperty float u\n property float v\n";
final String triangleHeader = "element face " + mesh.triangles().size()
final String triangleHeader = "element face " + mesh.triangles().sizel()
+ "\nproperty list uchar int vertex_index\n";
final String endHeader = "end_header\n";

// TODO: Fail fast more robustly if mesh is too large.
// But need to modify the API to not return a byte[].
if ( mesh.vertices().size() > Integer.MAX_VALUE )
if ( mesh.vertices().sizel() > Integer.MAX_VALUE )
throw new IllegalArgumentException( "Too many vertices: " + //
mesh.vertices().size() );
mesh.vertices().sizel() );

if ( mesh.triangles().size() > Integer.MAX_VALUE )
if ( mesh.triangles().sizel() > Integer.MAX_VALUE )
throw new IllegalArgumentException( "Too many triangles: " + //
mesh.triangles().size() );
mesh.triangles().sizel() );

final ByteArrayOutputStream os = new ByteArrayOutputStream();

Expand All @@ -308,14 +308,14 @@ public static final byte[] writeAscii( final Mesh mesh ) throws IOException
writer.write( endHeader );

// Do not populate file if there are no vertices
if ( mesh.vertices().size() == 0 )
if ( mesh.vertices().sizel() == 0 )
{
writer.flush();
return os.toByteArray();
}

// Write vertices
final TLongIntHashMap refToVertId = new TLongIntHashMap( ( int ) mesh.vertices().size() );
final TLongIntHashMap refToVertId = new TLongIntHashMap( ( int ) mesh.vertices().sizel() );
int vertId = 0;
for ( final Vertex v : mesh.vertices() )
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/imglib2/mesh/io/stl/STLMeshIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static final void read( final Mesh mesh, final byte[] data )
/** Writes the facets into a byte[] that can then be saved into a file */
public static final byte[] write( final Mesh mesh )
{
final long facetCount = mesh == null ? 0 : mesh.triangles().size();
final long facetCount = mesh == null ? 0 : mesh.triangles().sizel();
final long longBytes = HEADER_BYTES + COUNT_BYTES + facetCount * FACET_BYTES;
if ( longBytes > Integer.MAX_VALUE )
{ throw new IllegalArgumentException( "Too many triangles: " + facetCount ); }
Expand Down
40 changes: 7 additions & 33 deletions src/main/java/net/imglib2/mesh/obj/Triangles.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ public interface Triangles extends Iterable< Triangle >
Mesh mesh();

/**
* Number of triangles in the collection.
* Number of triangles in the collection as a <code>long</code> value.
*/
long size();
long sizel();

/**
* Number of triangles in the collection as an <code>int</code> value.
*
* @throws RuntimeException
* if the number of triangles exceeds {@link Integer#MAX_VALUE}.
*/
default int isize()
default int size()
{
final long size = size();
final long size = sizel();
if ( size >= Integer.MAX_VALUE )
throw new RuntimeException( "Too many triangles: " + size );
return ( int ) size;
Expand Down Expand Up @@ -200,7 +200,7 @@ default int addf( final int v0, final int v1, final int v2, final float nx, fina
* Index of triangle's third vertex.
* @return Index of newly added triangle.
*/
default long addf( long v0, long v1, long v2 )
default long addf( final long v0, final long v1, final long v2 )
{
// (v1 - v0) x (v2 - v0)

Expand Down Expand Up @@ -455,32 +455,6 @@ default long add( final long v0, final long v1, final long v2 )
return add( v0, v1, v2, nx / nmag, ny / nmag, nz / nmag );
}

/**
* Adds a triangle to the mesh's triangles list, using <code>int</code>
* indices.
* <p>
* Normal is computed with counterclockwise (i.e., right-hand) orientation.
* </p>
*
* @param v0
* Index of triangle's first vertex.
* @param v1
* Index of triangle's second vertex.
* @param v2
* Index of triangle's third vertex.
* @return Index of newly added triangle.
* @throws RuntimeException
* if the return triangle index exceeds
* {@link Integer#MAX_VALUE}.
*/
default int add( final int v0, final int v1, final int v2 )
{
final long t = add( ( long ) v0, ( long ) v1, ( long ) v2 );
if ( t >= Integer.MAX_VALUE )
throw new RuntimeException( "Index too large: " + t );
return ( int ) t;
}

/**
* Adds a triangle to the mesh's triangles list.
* <p>
Expand Down Expand Up @@ -574,7 +548,7 @@ default Iterator< Triangle > iterator()

private long index = -1;

private Triangle triangle = new Triangle()
private final Triangle triangle = new Triangle()
{

@Override
Expand All @@ -593,7 +567,7 @@ public long index()
@Override
public boolean hasNext()
{
return index + 1 < size();
return index + 1 < sizel();
}

@Override
Expand Down
Loading