|
| 1 | +/* |
| 2 | + * This library adds PovRAY export facility to toxiclibscore |
| 3 | + * Copyright (c) 2015 Martin Prout |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or modify it under |
| 6 | + * the terms of the GNU Lesser General Public License as published by the Free |
| 7 | + * Software Foundation; either version 2.1 of the License, or (at your option) |
| 8 | + * any later version. |
| 9 | + * |
| 10 | + * http://creativecommons.org/licenses/LGPL/2.1/ |
| 11 | + * |
| 12 | + * This library is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 14 | + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 15 | + * details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this library; if not, write to the Free Software Foundation, Inc., |
| 19 | + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
| 20 | + */ |
| 21 | +package toxi.processing; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import processing.core.PApplet; |
| 25 | +import toxi.geom.AABB; |
| 26 | +import toxi.geom.Vec3D; |
| 27 | + |
| 28 | +/** |
| 29 | + * This class provides access to underlying TriangleMesh parameters to allow |
| 30 | + * export in PovRAY mesh2 format (with or without normals) |
| 31 | + * |
| 32 | + * @author Martin Prout |
| 33 | + */ |
| 34 | +public class POVMesh { |
| 35 | + |
| 36 | + private POVWriter pov; |
| 37 | + private Textures opt; |
| 38 | + private final PApplet parent; |
| 39 | + /** |
| 40 | + * |
| 41 | + */ |
| 42 | + static String VERSION = "0.58"; |
| 43 | + |
| 44 | + /** |
| 45 | + * Default constructor this mesh no texture |
| 46 | + * |
| 47 | + * @param app |
| 48 | + */ |
| 49 | + public POVMesh(PApplet app) { |
| 50 | + parent = app; |
| 51 | + parent.registerMethod("dispose", this); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Allows the option to change texture option per mesh |
| 56 | + * |
| 57 | + * @param opt |
| 58 | + */ |
| 59 | + public void setTexture(Textures opt) { |
| 60 | + this.opt = opt; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Saves the mesh as PovRAY mesh2 format by appending it to the given mesh |
| 65 | + * {@link POVWriter} instance. Saves normals. |
| 66 | + * |
| 67 | + * @param meshArray |
| 68 | + */ |
| 69 | + public void saveAsPOV(toxi.geom.mesh.TriangleMesh[] meshArray) { |
| 70 | + saveAsMesh(meshArray, true); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Saves the mesh as PovRAY mesh2 format by appending it to the given mesh |
| 75 | + * {@link POVWriter} instance. Saves normals. |
| 76 | + * |
| 77 | + * @param mesh |
| 78 | + */ |
| 79 | + public void saveAsPOV(toxi.geom.mesh.TriangleMesh mesh) { |
| 80 | + saveAsPOV(mesh, true); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Saves the mesh as PovRAY mesh2 format to the given {@link PrintWriter}. |
| 85 | + * Without normals (when saveNormal is false), checks if option has changed, |
| 86 | + * changes option if required (implies serial use of saveAsPov) |
| 87 | + * |
| 88 | + * @param mesh |
| 89 | + * @param saveNormals boolean |
| 90 | + */ |
| 91 | + public void saveAsPOV(toxi.geom.mesh.TriangleMesh mesh, boolean saveNormals) { |
| 92 | + AABB boundingBox = mesh.getBoundingBox(); |
| 93 | + Vec3D min = boundingBox.getMin(); |
| 94 | + Vec3D max = boundingBox.getMax(); |
| 95 | + pov.boundingBox(min, max); |
| 96 | + if (opt != pov.getTexture()) { |
| 97 | + pov.setTexture(opt); |
| 98 | + } |
| 99 | + pov.beginMesh2(mesh.name); |
| 100 | + int vOffset = pov.getCurrVertexOffset(); |
| 101 | + // vertices |
| 102 | + pov.total(mesh.vertices.size()); |
| 103 | + mesh.vertices.values().stream().forEach((v) -> { |
| 104 | + pov.vertex(v); |
| 105 | + }); |
| 106 | + pov.endSection(); |
| 107 | + // faces |
| 108 | + if (saveNormals) { |
| 109 | + // normals |
| 110 | + pov.beginNormals(mesh.vertices.size()); |
| 111 | + mesh.vertices.values().stream().forEach((v) -> { |
| 112 | + pov.normal(v.normal.getNormalized()); |
| 113 | + }); |
| 114 | + pov.endSection(); |
| 115 | + } |
| 116 | + pov.beginIndices(mesh.faces.size()); |
| 117 | + mesh.faces.stream().forEach((f) -> { |
| 118 | + pov.face(f.b.id + vOffset, f.a.id + vOffset, f.c.id + vOffset); |
| 119 | + }); |
| 120 | + pov.endSection(); |
| 121 | + pov.endSave(); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Saves the mesh as PovRAY mesh2 format to the given {@link PrintWriter}. |
| 126 | + * Without normals (when saveNormal is false) Check on option is included |
| 127 | + * for completeness |
| 128 | + * |
| 129 | + * @param meshArray |
| 130 | + * @param saveNormals boolean |
| 131 | + */ |
| 132 | + public void saveAsMesh(toxi.geom.mesh.TriangleMesh[] meshArray, boolean saveNormals) { |
| 133 | + AABB boundingBox; |
| 134 | + Vec3D min; |
| 135 | + Vec3D max; |
| 136 | + |
| 137 | + if (opt != pov.getTexture()) { |
| 138 | + pov.setTexture(opt); |
| 139 | + } |
| 140 | + for (toxi.geom.mesh.TriangleMesh mesh : meshArray) { |
| 141 | + pov.beginMesh2(mesh.name); |
| 142 | + boundingBox = mesh.getBoundingBox(); |
| 143 | + min = boundingBox.getMin(); |
| 144 | + max = boundingBox.getMax(); |
| 145 | + pov.boundingBox(min, max); |
| 146 | + int vOffset = pov.getCurrVertexOffset(); |
| 147 | + // vertices |
| 148 | + pov.total(mesh.vertices.size()); |
| 149 | + mesh.vertices.values().stream().forEach((v) -> { |
| 150 | + pov.vertex(v); |
| 151 | + }); |
| 152 | + pov.endSection(); |
| 153 | + // faces |
| 154 | + if (saveNormals) { |
| 155 | + // normals |
| 156 | + pov.beginNormals(mesh.vertices.size()); |
| 157 | + mesh.vertices.values().stream().forEach((v) -> { |
| 158 | + pov.normal(v.normal.getNormalized()); |
| 159 | + }); |
| 160 | + pov.endSection(); |
| 161 | + } |
| 162 | + pov.beginIndices(mesh.faces.size()); |
| 163 | + mesh.faces.stream().forEach((f) -> { |
| 164 | + pov.face(f.b.id + vOffset, f.a.id + vOffset, f.c.id + vOffset); |
| 165 | + }); |
| 166 | + pov.endSection(); |
| 167 | + pov.endSave(); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Start writing *.inc file |
| 173 | + * |
| 174 | + * @param fileName main.inc File |
| 175 | + */ |
| 176 | + public void beginSave(File fileName) { |
| 177 | + opt = Textures.RAW; |
| 178 | + this.pov = new POVWriter(fileName); |
| 179 | + pov.beginForeground(); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Finish writing *.inc file and close PrintWriter |
| 184 | + */ |
| 185 | + public void endSave() { |
| 186 | + pov.endForeground(); |
| 187 | + } |
| 188 | + |
| 189 | +// /** |
| 190 | +// * |
| 191 | +// * @param e |
| 192 | +// */ |
| 193 | +// public void keyEvent(KeyEvent e) { |
| 194 | +// if (e.getAction() == KeyEvent.RELEASED) { |
| 195 | +// switch (e.getKey()) { |
| 196 | +// case 't': |
| 197 | +// case 'T': |
| 198 | +// status = Tracing.EXPORTING; |
| 199 | +// System.out.println("tracing"); |
| 200 | +// break; |
| 201 | +// } |
| 202 | +// } |
| 203 | +// } |
| 204 | + /** |
| 205 | + * Required by processing |
| 206 | + */ |
| 207 | + public void dispose() { |
| 208 | + endSave(); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Required by processing |
| 213 | + * |
| 214 | + * @return version String |
| 215 | + */ |
| 216 | + public String version() { |
| 217 | + return VERSION; |
| 218 | + } |
| 219 | +} |
0 commit comments