Files
mc-mesh/plugin/src/main/java/com/aaaaahhhhhhh/bananapuncher714/minietest/MeshingTest7.java

99 lines
4.0 KiB
Java

package com.aaaaahhhhhhh.bananapuncher714.minietest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import com.aaaaahhhhhhh.bananapuncher714.mesh.Mesh;
import com.aaaaahhhhhhh.bananapuncher714.mesh.Point;
import com.aaaaahhhhhhh.bananapuncher714.mesh.Polygon;
import com.aaaaahhhhhhh.bananapuncher714.mesh.region.simple.RegionRuleWinding;
import com.aaaaahhhhhhh.bananapuncher714.mesh.region.simple.RegionSimple;
import com.aaaaahhhhhhh.bananapuncher714.mesh.region.simple.RegionSimple.GluWindingRule;
/**
* Mesh speed test for polygons
*/
public class MeshingTest7 {
private static final File BASE = new File( System.getProperty( "user.dir" ) );
private static final File POLYGON_DIR = new File( BASE, "polygons" );
public static void main( String[] args ) {
if ( POLYGON_DIR.exists() && POLYGON_DIR.isDirectory() ) {
System.out.println( "Found " + POLYGON_DIR.list().length + " files" );
final long allStart = System.currentTimeMillis();
final AtomicInteger index = new AtomicInteger( 0 );
final Collection< File > files = Arrays.asList( POLYGON_DIR.listFiles() );
files.parallelStream().forEach( f -> {
final StringBuilder builder = new StringBuilder();
builder.append( "Meshing file: " + f + "\n" );
final long start = System.currentTimeMillis();
final List< Polygon > polygons = mesh( f, builder );
Mesh< RegionSimple > mesh = new Mesh< RegionSimple >( () -> { return new RegionSimple( GluWindingRule.ODD ); } );
for ( Polygon poly : polygons ) {
mesh.addPolygon( poly, RegionRuleWinding.CLOCKWISE );
}
mesh.meshify();
long time = System.currentTimeMillis() - start;
builder.append( "\tTook " + time + "ms\n" );
builder.append( "\tCompleted " + index.incrementAndGet() + "/" + files.size() );
System.out.println( builder.toString() );
} );
System.out.println( "Took a total of " + ( System.currentTimeMillis() - allStart ) + "ms" );
} else {
System.err.println( "No such directory exists: " + POLYGON_DIR );
}
}
private static List< Polygon > mesh( File file, final StringBuilder stringBuilder ) {
// First parse the file to get a list of all polygons
final List< Polygon > polys = new ArrayList< Polygon >();
try ( BufferedReader reader = new BufferedReader( new FileReader( file ) ) ) {
String line;
while ( ( line = reader.readLine() ) != null ) {
if ( !line.isEmpty() ) {
final int pointCount = Integer.parseInt( line );
List< Point > points = new ArrayList< Point >( pointCount );
int point = 0;
while ( point < pointCount && ( line = reader.readLine() ) != null ) {
if ( !line.isEmpty() ) {
String[] values2 = line.split( " " );
final double px = Double.parseDouble( values2[ 0 ] );
final double py = Double.parseDouble( values2[ 1 ] );
points.add( new Point( px, py ) );
++point;
}
}
polys.add( new Polygon( points ) ) ;
}
}
} catch ( FileNotFoundException e ) {
e.printStackTrace();
return null;
} catch ( IOException e ) {
e.printStackTrace();
return null;
}
return polys;
}
}