SurfaceAreaWrapper extends BoneJCommand { static void writeBinarySTLFile(final String path, final Mesh mesh) throws IllegalArgumentException, IOException, NullPointerException { if (mesh == null) { throw new NullPointerException("Mesh cannot be null"); } if (StringUtils.isNullOrEmpty(path)) { throw new IllegalArgumentException("Filename cannot be null or empty"); } final Iterator<Triangle> triangles = mesh.triangles().iterator(); final int numTriangles = (int) mesh.triangles().size(); try (final FileOutputStream writer = new FileOutputStream(path)) { final byte[] header = STL_HEADER.getBytes(); writer.write(header); final byte[] facetBytes = ByteBuffer.allocate(4).order( ByteOrder.LITTLE_ENDIAN).putInt(numTriangles).array(); writer.write(facetBytes); final ByteBuffer buffer = ByteBuffer.allocate(50); buffer.order(ByteOrder.LITTLE_ENDIAN); while (triangles.hasNext()) { final Triangle triangle = triangles.next(); writeSTLFacet(buffer, triangle); writer.write(buffer.array()); buffer.clear(); } } }  @Override void run();  }
@Test public void testWriteBinarySTLFile() throws Exception { final int headerSize = 84; final int bytesPerFacet = 50; final Mesh mesh = new NaiveFloatMesh(); final Triangles triangles = mesh.triangles(); triangles.addf( 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f ); triangles.addf( 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f ); final int expectedLength = headerSize + 2 * bytesPerFacet; final String filePath = "./test_file.stl"; SurfaceAreaWrapper.writeBinarySTLFile(filePath, mesh); final Path path = Paths.get(filePath); final byte[] bytes = Files.readAllBytes(path); Files.delete(path); assertEquals("Size of STL file is incorrect", expectedLength, bytes.length); final String header = new String(Arrays.copyOfRange(bytes, 0, 80)); assertEquals("File header is incorrect", SurfaceAreaWrapper.STL_HEADER, header); final int numFacets = ByteBuffer.wrap(bytes, 80, 4).order( ByteOrder.LITTLE_ENDIAN).getInt(); assertEquals("Wrong number of facets in the file", 2, numFacets); final Iterator<Triangle> iterator = mesh.triangles().iterator(); final ByteBuffer buffer = ByteBuffer.wrap(bytes, headerSize, 2 * bytesPerFacet).order(ByteOrder.LITTLE_ENDIAN); while (iterator.hasNext()) { final Triangle triangle = iterator.next(); assertEquals(triangle.nxf(), buffer.getFloat(), 1e-12); assertEquals(triangle.nyf(), buffer.getFloat(), 1e-12); assertEquals(triangle.nzf(), buffer.getFloat(), 1e-12); assertEquals(triangle.v0xf(), buffer.getFloat(), 1e-12); assertEquals(triangle.v0yf(), buffer.getFloat(), 1e-12); assertEquals(triangle.v0zf(), buffer.getFloat(), 1e-12); assertEquals(triangle.v1xf(), buffer.getFloat(), 1e-12); assertEquals(triangle.v1yf(), buffer.getFloat(), 1e-12); assertEquals(triangle.v1zf(), buffer.getFloat(), 1e-12); assertEquals(triangle.v2xf(), buffer.getFloat(), 1e-12); assertEquals(triangle.v2yf(), buffer.getFloat(), 1e-12); assertEquals(triangle.v2zf(), buffer.getFloat(), 1e-12); buffer.getShort(); } }
@Test(expected = IllegalArgumentException.class) public void testWriteBinarySTLFileEmptyNameThrowsIAE() throws Exception { final Mesh mesh = new NaiveFloatMesh(); SurfaceAreaWrapper.writeBinarySTLFile("", mesh); }
@Test(expected = NullPointerException.class) public void testWriteBinarySTLFileNullMeshThrowsNPE() throws Exception { SurfaceAreaWrapper.writeBinarySTLFile("Mesh", null); }
@Test(expected = IllegalArgumentException.class) public void testWriteBinarySTLFileNullNameThrowsIAE() throws Exception { final Mesh mesh = new NaiveFloatMesh(); SurfaceAreaWrapper.writeBinarySTLFile(null, mesh); }