TranslationalDataGenerator.java :  » Graphic-Library » apollo » org » apollo » motion » Java Open Source

Java Open Source » Graphic Library » apollo 
apollo » org » apollo » motion » TranslationalDataGenerator.java
/*
 * 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.motion;

import org.apollo.Visitor;
import org.apollo.bvh.Channel;
import org.apollo.bvh.Joint;
import org.apollo.datamodel.TrackingJoint;
import org.apollo.datamodel.TrackingVideo;

/**
 * Class generates motion data, based on relative position of the joints
 * @author Giovane.Kuhn on 02/06/2005
 */
public final class TranslationalDataGenerator extends DataGenerator {

    /** Maintain parent position */
    private double[] parentPosition;

    public TranslationalDataGenerator(TrackingVideo trackVideo) {
        super(trackVideo);
    }

    @Override
    double[] innerGenerate() {
        this.parentPosition = new double[] { 0, 0, 0 };
        modelRoot.visit(this);
        return data;
    }

    public boolean accept(Joint node) {

        // array to maintain position
        double[] position = new double[] { 0, 0, 0 };

        // get tracking joint
        TrackingJoint joint = frame.getTrackingJoint(node.getName());
        for (Channel c : node.getChannels()) {
            if (c == Channel.XPOS) {
                data[index] = (joint == null || joint.getGlobalX() == null ? node.getX() : joint.getGlobalX() - parentPosition[0]);
                position[0] = parentPosition[0] + data[index++];
            } else if (c == Channel.YPOS) {
                data[index] = (joint == null || joint.getGlobalY() == null ? node.getY() : joint.getGlobalY() - parentPosition[1]);
                position[1] = parentPosition[1] + data[index++];
            } else if (c == Channel.ZPOS) {
                data[index] = (joint == null || joint.getGlobalZ() == null ? node.getZ() : joint.getGlobalZ() - parentPosition[2]);
                position[2] = parentPosition[2] + data[index++];
            } else {
                throw new UnsupportedOperationException("Data generator not prepared for rotation channels");
            }
        }

        // calculate children position
        double[] temp = parentPosition;
        parentPosition = position;
        for (Joint child : node.getChildren()) {
            child.visit(this);
        }
        parentPosition = temp;

        return false;
    }

    @Override
    void createChannels(Joint root) {
        root.visit(new Visitor<Joint>() {

            public boolean accept(Joint node) {
                node.removeAllChannels();

                // all joints has position
                node.addChannel(Channel.XPOS);
                node.addChannel(Channel.YPOS);
                node.addChannel(Channel.ZPOS);

                return true;
            }

        });
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.