Skip to content

Instantly share code, notes, and snippets.

@monkstone
Created February 7, 2012 20:38
Show Gist options
  • Save monkstone/1761795 to your computer and use it in GitHub Desktop.
Save monkstone/1761795 to your computer and use it in GitHub Desktop.
Refactored POvRAY export from toxiclibs (as and external library)
/*
* This library adds PovRAY export facility to toxiclibscore
* Copyright (c) 2012 Martin Prout
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* http://creativecommons.org/licenses/LGPL/2.1/
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
package toxi.geom.mesh;
import java.io.PrintWriter;
/**
*
* @author Martin Prout
*/
public interface POVInterface {
/**
*
* @param name
*/
void setName(String name);
/**
* Saves the mesh as PovRAY mesh2 format to the given {@link PrintWriter}. Currently
* no texture coordinates are supported or written.
*
* @param pw PrintWriter (save to a PovRAY *.inc file)
*/
void saveAsPOV(PrintWriter pw);
/**
* Saves the mesh as PovRAY mesh2 format to the given {@link PrintWriter}. Currently
* no texture coordinates are supported or written.
*
* @param pw PrintWriter (save to a PovRAY *.inc file)
* @param normals
*/
void saveAsPOV(PrintWriter pw, boolean normals);
}
/*
* This library adds PovRAY export facility to toxiclibscore
* Copyright (c) 2012 Martin Prout
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* http://creativecommons.org/licenses/LGPL/2.1/
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
package toxi.geom.mesh;
import java.io.PrintWriter;
/**
* This class provides access to underlying TriangleMesh parameters
* to allow export in PovRAY mesh2 format (with or without normals)
* @author Martin Prout
*/
public class POVMesh implements POVInterface {
/**
* private instance of TriangleMesh
*/
private TriangleMesh mesh;
/**
* Default constructor this mesh should not be null
*/
public POVMesh() {
mesh = new TriangleMesh();
}
/**
* Constructor gives local access to TriangleMesh parameters
* @param mesh WETriangleMesh
*/
public POVMesh(WETriangleMesh mesh) {
this.mesh = mesh;
}
/**
* Constructor gives local access to TriangleMesh parameters
* @param mesh TriangleMesh
*/
public POVMesh(TriangleMesh mesh) {
this.mesh = mesh;
}
/**
* Saves the mesh as PovRAY mesh2 format by appending it to the given mesh
* {@link POVWriter} instance. Saves normals.
*
* @param obj POVWriter
*/
protected void saveAsPOV(POVWriter obj) {
saveAsPOV(obj, true);
}
/**
* Saves the mesh as PovRAY mesh2 format to the given {@link PrintWriter}.
* Without normals (when saveNormal is false)
* @param pw PrintWriter
* @param saveNormals boolean
*/
protected void saveAsPOV(POVWriter pw, boolean saveNormals) {
int vOffset = pw.getCurrVertexOffset();
pw.beginMesh2(mesh.name);
// vertices
pw.total(mesh.vertices.size());
for (Vertex v : mesh.vertices.values()) {
pw.vertex(v);
}
pw.endSection();
// faces
if (saveNormals) {
// normals
pw.beginNormals(mesh.vertices.size());
for (Vertex v : mesh.vertices.values()) {
pw.normal(v.normal.getNormalized());
}
pw.endSection();
}
pw.beginIndices(mesh.faces.size());
for (Face f : mesh.faces) {
pw.face(f.b.id + vOffset, f.a.id + vOffset, f.c.id + vOffset);
}
pw.endSection();
}
/**
* Saves the mesh as PovRAY mesh2 format to the given {@link PrintWriter}.
* With normals as default (save to a PovRAY *.inc file, where name is
* used to declare a mesh2 object, for inclusion in a PovRAY *.pov file)
*
* @param pw PrintWriter
*/
@Override
public void saveAsPOV(PrintWriter pw) {
POVWriter obj = new POVWriter();
obj.beginSave(pw);
saveAsPOV(obj);
obj.endSave();
}
/**
* Saves the mesh as PovRAY mesh2 format to the given {@link PrintWriter}.
* Without normals (when saveNormal is false).
*
* @param pw PrintWriter (save to a PovRAY *.inc file)
* @param normals
*/
@Override
public void saveAsPOV(PrintWriter pw, boolean normals) {
POVWriter obj = new POVWriter();
obj.beginSave(pw);
saveAsPOV(obj, normals);
obj.endSave();
}
/**
* Explicitly set the mesh name rather than use default which can vary
* from 'aabb' to 'untitled' NB: a mesh name is required in *.pov file
* @param name
*/
@Override
public void setName(String name) {
mesh.setName(name);
}
}
/*
* This library adds PovRAY export facility to toxiclibscore
* Copyright (c) 2012 Martin Prout
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* http://creativecommons.org/licenses/LGPL/2.1/
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
package toxi.geom.mesh;
import java.io.PrintWriter;
import toxi.geom.Vec3D;
/**
* Extremely bare bones PovRAY 3D format exporter. Purely handles the writing of
* data to the .inc file, as a mesh2 object. See {@link TriangleMesh}
* for details. Logic heavily borrowed from toxis STL/obj writer,
* but adjusted for PovRAY and external use
*
* @see TriangleMesh#saveAsPOV(povWriter)
*/
public class POVWriter {
final String COMMA = ", ";
/**
*
*/
public final String VERSION = "0.4";
/**
*
*/
protected PrintWriter povWriter;
/**
* Track the number of vertices written to file
*/
protected int numVerticesWritten = 0;
/**
* Track the number of normals written to file
*/
protected int numNormalsWritten = 0;
/**
* Handles PrintWriter input
* @param pw
*/
public void beginSave(PrintWriter pw) {
povWriter = pw;
handleBeginSave();
}
/**
* close the mesh declaration
*/
public void endSave() {
povWriter.println("}"); // end of mesh2 declaration
}
/**
* Write face indices as as vector
* @param a
* @param b
* @param c
*/
public void face(int a, int b, int c) {
povWriter.println(buildVector(a, b, c));
}
/**
* Track the number of normals written to file
* @return
*/
public int getCurrNormalOffset() {
return numNormalsWritten;
}
/**
* Track the number of vertices written to file
* @return
*/
public int getCurrVertexOffset() {
return numVerticesWritten;
}
/**
* Print a header, and initialise counts
*/
protected void handleBeginSave() {
povWriter.println("// generated by POVExport v" + VERSION);
numVerticesWritten = 0;
numNormalsWritten = 0;
}
/**
* Begin the mesh2 output as a PovRAY declaration
* @param name
*/
public void beginMesh2(String name) {
StringBuilder pov = new StringBuilder("#declare ");
pov.append(name);
pov.append(" = mesh2{\n");
pov.append("\tvertex_vectors {");
povWriter.println(pov);
}
/**
* End the current section ie vertex_vector, normal_vector or face_indices
*/
public void endSection() {
povWriter.println("\t}");
}
/**
* Output start of normal_vectors
* @param count
*/
public void beginNormals(int count) {
povWriter.println("\tnormal_vectors{");
total(count);
}
/**
* Output start of face_indices
* @param count
*/
public void beginIndices(int count) {
povWriter.println("\tface_indices{");
total(count);
}
/**
* Used to output total count vertex_vector, normal_vector & face_indices
* @param count
*/
public void total(int count) {
povWriter.println(String.format("\t%d,", count));
}
/**
* Write normal as PovRAY vector
* @param n
*/
public void normal(Vec3D n) {
povWriter.println(buildVector(n));
numNormalsWritten++;
}
/**
* Write vertex as PovRAY vector
* @param v
*/
public void vertex(Vec3D v) {
povWriter.println(buildVector(v));
numVerticesWritten++;
}
private StringBuilder buildVector(int a, int b, int c) {
StringBuilder my_vector = new StringBuilder(120);
my_vector.append('\t').append('<');
my_vector.append(a).append(COMMA);
my_vector.append(b).append(COMMA);
my_vector.append(c).append('>');
return my_vector.append(COMMA);
}
/**
* The Y and Z coordinates are multiplied by -1 to handle the conversion of
* processing coordinate system to PovRAY coordinate system.
* @param v
* @return
*/
private StringBuilder buildVector(Vec3D v) {
StringBuilder my_vector = new StringBuilder(120);
my_vector.append('\t').append('<');
my_vector.append(v.x).append(COMMA);
my_vector.append(v.y * -1).append(COMMA);
my_vector.append(v.z * -1).append('>');
return my_vector.append(COMMA);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment