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.List; import org.bukkit.util.Vector; 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; import com.aaaaahhhhhhh.bananapuncher714.minietest.MeshingTest2.AABB; import com.aaaaahhhhhhh.bananapuncher714.minietest.objects.ChunkLocation; import com.aaaaahhhhhhh.bananapuncher714.minietest.objects.mesh.Facet; import com.aaaaahhhhhhh.bananapuncher714.minietest.objects.mesh.MeshBuilder; import com.aaaaahhhhhhh.bananapuncher714.minietest.objects.mesh.Plane; public class MeshingTest3 { private static final File BASE = new File( System.getProperty( "user.dir" ) ); private static final File CHUNK_DIR = new File( BASE, "chunks" ); public static void main( String[] args ) { if ( CHUNK_DIR.exists() && CHUNK_DIR.isDirectory() ) { System.out.println( "Found " + CHUNK_DIR.list().length + " files" ); for ( int i = 0; i < CHUNK_DIR.listFiles().length; ++i ) { final File file = CHUNK_DIR.listFiles()[ i ]; System.out.println( "Meshing " + file + "\t" + ( i + 1 ) + "/" + CHUNK_DIR.listFiles().length ); final List< Plane > planes = mesh( file ); planes.parallelStream().forEach( p -> { try { process( p ); } catch ( IllegalStateException e ) { e.printStackTrace(); System.exit( 1 ); } } ); } // final List< Plane > planes = mesh( new File( CHUNK_DIR, "world,-4,-6" ) ); // 123 // for ( int i = 0; i < planes.size(); ++i ) { // System.out.println( "Meshing plane " + i ); // process( planes.get( i ) ); // } // process( planes.get( 177 ) ); } else { System.err.println( "No such directory exists: " + CHUNK_DIR ); } } private static void process( final Plane plane ) { if ( plane != null ) { Mesh< RegionSimple > mesh = new Mesh< RegionSimple >( () -> { return new RegionSimple( GluWindingRule.ODD ); } ); for ( Polygon poly : plane.polygons ) { mesh.addPolygon( poly, RegionRuleWinding.CLOCKWISE ); } mesh.meshify(); } else { System.out.println( "No data!" ); } } private static List< Plane > mesh( File file ) { String name = file.getName(); String[] split = name.split( "," ); ChunkLocation location = new ChunkLocation( split[ 0 ], Integer.parseInt( split[ 1 ] ), Integer.parseInt( split[ 2 ] ) ); System.out.println( "Parsing chunk " + location ); // First parse the file to get a list of all bounding boxes that we can use List< AABB > boxes = new ArrayList< AABB >(); try ( BufferedReader reader = new BufferedReader( new FileReader( file ) ) ) { String line; while ( ( line = reader.readLine() ) != null ) { if ( !line.isEmpty() ) { String[] values = line.split( "," ); double minX = Double.parseDouble( values[ 0 ] ); double minY = Double.parseDouble( values[ 1 ] ); double minZ = Double.parseDouble( values[ 2 ] ); double maxX = Double.parseDouble( values[ 3 ] ); double maxY = Double.parseDouble( values[ 4 ] ); double maxZ = Double.parseDouble( values[ 5 ] ); boxes.add( new AABB( minX, minY, minZ, maxX, maxY, maxZ ) ); } } } catch ( FileNotFoundException e ) { e.printStackTrace(); return null; } catch ( IOException e ) { e.printStackTrace(); return null; } System.out.println( "Found " + boxes.size() + " boxes" ); // Now that we have a bunch of bounding boxes, do whatever MeshBuilder builder = new MeshBuilder(); for ( AABB box : boxes ) { for ( Facet facet : generateFacetsFor( box ) ) { builder.addFacet( facet ); } } return builder.planes; } private static List< Facet > generateFacetsFor( AABB box ) { List< Facet > facets = new ArrayList< Facet >(); Vector p1 = new Vector( box.xmin, box.ymin, box.zmin ); Vector p2 = new Vector( box.xmin, box.ymin, box.zmax ); Vector p3 = new Vector( box.xmin, box.ymax, box.zmin ); Vector p4 = new Vector( box.xmin, box.ymax, box.zmax ); Vector p5 = new Vector( box.xmax, box.ymin, box.zmin ); Vector p6 = new Vector( box.xmax, box.ymin, box.zmax ); Vector p7 = new Vector( box.xmax, box.ymax, box.zmin ); Vector p8 = new Vector( box.xmax, box.ymax, box.zmax ); { Facet facet = new Facet(); facet.points.add( p1 ); facet.points.add( p2 ); facet.points.add( p4 ); facet.points.add( p3 ); facet.normal = new Vector( -1, 0, 0 ); facets.add( facet ); } { Facet facet = new Facet(); facet.points.add( p5 ); facet.points.add( p6 ); facet.points.add( p8 ); facet.points.add( p7 ); facet.normal = new Vector( 1, 0, 0 ); facets.add( facet ); } { Facet facet = new Facet(); facet.points.add( p1 ); facet.points.add( p2 ); facet.points.add( p6 ); facet.points.add( p5 ); facet.normal = new Vector( 0, -1, 0 ); facets.add( facet ); } { Facet facet = new Facet(); facet.points.add( p3 ); facet.points.add( p4 ); facet.points.add( p8 ); facet.points.add( p7 ); facet.normal = new Vector( 0, 1, 0 ); facets.add( facet ); } { Facet facet = new Facet(); facet.points.add( p1 ); facet.points.add( p3 ); facet.points.add( p7 ); facet.points.add( p5 ); facet.normal = new Vector( 0, 0, -1 ); facets.add( facet ); } { Facet facet = new Facet(); facet.points.add( p2 ); facet.points.add( p4 ); facet.points.add( p8 ); facet.points.add( p6 ); facet.normal = new Vector( 0, 0, 1 ); facets.add( facet ); } return facets; } }