/*
* Apollo - Motion capture and animation system
* Copyright (c) 2005 Apollo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* http://www.gnu.org/copyleft/gpl.html
*
* @author Giovane.Kuhn - brain@netuno.com.br
*
*/
package org.apollo.bvh;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import org.apollo.ApolloUtil;
import org.apollo.Visitor;
/**
* Class to generate BVH files physically
* @author Giovane.Kuhn on 28/05/2005
*/
public final class BVHGenerator {
protected static final DecimalFormat formatter = new DecimalFormat("0.0000", new DecimalFormatSymbols(Locale.US));
/** BVH file to be generated */
private final BVHFile bvhFile;
/** BVH file name */
private final File file;
public BVHGenerator(BVHFile bvhFile, File file) {
this.bvhFile = bvhFile;
this.file = file;
}
public void generate(boolean onlyJoint) throws IOException {
final FileWriter fw;
fw = new FileWriter(file);
// write joint herarchy
fw.write("HIERARCHY\n");
bvhFile.getRoot().visit(new HierarchyWriteVisitor(fw, onlyJoint));
// write motion info
Motion m = bvhFile.getMotion();
fw.write("MOTION\n");
// frame count
fw.write("Frames: ");
fw.write(String.valueOf(m.getFrameCount()));
fw.write('\n');
// frame time
fw.write("Frame Time: ");
fw.write(formatter.format(m.getFrameTime()));
fw.write('\n');
// write motion data
double data[][] = m.getData();
for (int i = 0; i < m.getFrameCount(); i++) {
for (int j = 0; j < data[i].length; j++) {
fw.write(formatter.format(data[i][j]));
fw.write(' ');
}
fw.write('\n');
}
fw.close();
}
private static final class HierarchyWriteVisitor implements Visitor<Joint> {
/** File writer */
private final OutputStreamWriter writer;
/** Indicate that not exists end site */
private final boolean onlyJoint;
private int tabs = 0;
public HierarchyWriteVisitor(OutputStreamWriter writer, boolean onlyJoint) {
this.writer = writer;
this.onlyJoint = onlyJoint;
}
public boolean accept(Joint node) {
try {
// write node name
writer.write(ApolloUtil.repeatString(" ", tabs));
if (node.isRoot()) {
writer.write("ROOT ");
writer.write(node.getName());
writer.write('\n');
} else if (onlyJoint || !node.isEndSite()) {
writer.write("JOINT ");
writer.write(node.getName());
writer.write('\n');
} else {
writer.write("End Site\n");
}
writer.write(ApolloUtil.repeatString(" ", tabs));
writer.write("{\n");
++tabs;
// write node offset
writer.write(ApolloUtil.repeatString(" ", tabs));
writer.write("OFFSET ");
writer.write(formatter.format(node.getX().doubleValue()));
writer.write(' ');
writer.write(formatter.format(node.getY().doubleValue()));
writer.write(' ');
writer.write(formatter.format(node.getZ().doubleValue()));
writer.write('\n');
// write node channels
if (onlyJoint || !node.isEndSite()) {
writer.write(ApolloUtil.repeatString(" ", tabs));
writer.write("CHANNELS ");
writer.write(String.valueOf(node.getChannels().size()));
for (Channel c : node.getChannels()) {
writer.write(' ');
writer.write(c.getName());
}
writer.write('\n');
}
// write children
for (Joint child : node.getChildren()) {
child.visit(this);
}
--tabs;
writer.write(ApolloUtil.repeatString(" ", tabs));
writer.write("}\n");
return false;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
|