Normand Briere
2018-07-01 655810d1c4e710e7c85772b8dde96772dbcf274b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
 * JMOCAP
 * 
 * Developed by Michael Kipp, 2008-2011, DFKI Saarbrücken, Germany
 * E-Mail: mich.kipp@googlemail.com
 * 
 * This software has been released under the
 * GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007
 */
package mocap.gui;
 
import mocap.JMocap;
import mocap.figure.Bone;
import mocap.reader.ASFReader;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.vecmath.Point3d;
 
/**
 *
 * @author Michael Kipp
 */
public class JMocapController implements ActionListener
{
 
    private static final String PROP_DIR = "dir";
    public static final Point3d CAMERA = new Point3d(1, 3, 15);
    public static final Point3d CAMERA_TARGET = new Point3d(0, 1, 0);
    private static final String PROP_LAST_ASF = "file.asf";
    private static final String PROP_LAST_AMC = "file.amc";
    private static final String PROP_LAST_BVH = "file.bvh";
    private static final String PROPERTIES_FILE = ".jmocap";
    private JMocap _jMocap;
    private JMocapGUI _view;
    private Properties _prop = new Properties();
 
    public JMocapController()
    {
        _jMocap = new JMocap();
        _jMocap.setCameraView(CAMERA, CAMERA_TARGET);
        loadProp();
        _view = new JMocapGUI(this, _jMocap);
    }
 
    /**
     * Loads skeleton and anim from previous session.
     */
    private void loadPreviousSkeleton()
    {
        String fnASF = _prop.getProperty(PROP_LAST_ASF);
        String fnAMC = _prop.getProperty(PROP_LAST_AMC);
        if (fnASF != null && fnAMC != null) {
            try {
                File f = new File(fnASF);
                ASFReader rd = new ASFReader();
                Bone skel = rd.getSkeleton(f);
                _jMocap.initFigure(skel, f.getName());
                _view.updateSkeletonInfo(f.getName());
                f = new File(fnAMC);
                _jMocap.loadAMC(f);
                _view.updateAnimInfo(f.getName());
            } catch (IOException e) {
                System.out.println("ERROR! loadpreviousskeleton");
                e.printStackTrace();
                _jMocap.clearAll();
            }
        }
    }
 
    private File getPropFile()
    {
        return new File(System.getProperty("user.dir"), PROPERTIES_FILE);
    }
 
    private void loadProp()
    {
        File f = getPropFile();
        if (f.exists()) {
            try {
                _prop.load(new FileReader(f));
            } catch (IOException ex) {
                Logger.getLogger(JMocap.class.getName()).log(Level.SEVERE,
                        null, ex);
            }
        }
    }
 
    private void saveProp()
    {
        FileWriter w = null;
        try {
            w = new FileWriter(getPropFile());
            _prop.store(w, "DFKI-EMBOTS");
            w.close();
        } catch (IOException ex) {
            Logger.getLogger(JMocap.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                w.close();
            } catch (IOException ex) {
                Logger.getLogger(JMocap.class.getName()).log(Level.SEVERE,
                        null, ex);
            }
        }
    }
 
    private File promptForFile(String dirProp, final String ext)
    {
        FileFilter ff = new FileFilter()
        {
 
            @Override
            public boolean accept(File f)
            {
                return f.getName().toLowerCase().endsWith(ext) || f.isDirectory();
            }
 
            @Override
            public String getDescription()
            {
                return "All ." + ext + " files";
            }
        };
        String d = _prop.getProperty(dirProp, System.getProperty("user.dir"));
        JFileChooser fc = new JFileChooser(d);
        fc.setFileFilter(ff);
        int res = fc.showOpenDialog(null);
        if (res == JFileChooser.APPROVE_OPTION) {
            _prop.put(dirProp, fc.getSelectedFile().getParent().toString());
            saveProp();
            return fc.getSelectedFile();
        }
        return null;
    }
 
    protected void loadASFAction()
    {
        File f = promptForFile(PROP_DIR, "asf");
        if (f != null) {
            try {
                _jMocap.getFigureManager().pauseAll();
                ASFReader rd = new ASFReader();
                Bone skel = rd.getSkeleton(f);
                _jMocap.initFigure(skel, f.getName(), _view.getCursorPos());
                _view.updateSkeletonInfo(f.getName());
                _prop.put(PROP_LAST_ASF, f.toString());
                saveProp();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(
                        null,
                        "Couldn't load " + f.getName() + ": " + ex.getMessage(),
                        "loading error",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }
 
    protected void loadAMCAction()
    {
        File f = promptForFile(PROP_DIR, "amc");
        if (f != null) {
            // String result = JOptionPane.showInputDialog(null,
            // "Please enter layer number (0-2):", 0);
            // if (result != null) {
            try {
                // int layer = Integer.parseInt(result);
                _jMocap.loadAMC(f);
                _prop.put(PROP_LAST_AMC, f.toString());
                saveProp();
                _view.updateAnimInfo(f.getName());
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(
                        null,
                        "Couldn't load " + f.getName() + ": " + ex.getMessage(),
                        "loading error",
                        JOptionPane.ERROR_MESSAGE);
                // }
            }
        }
    }
 
    protected void loadBVHAction()
    {
        File f = promptForFile(PROP_DIR, "bvh");
        if (f != null) {
            String scale = JOptionPane.showInputDialog(
                    null,
                    "Please enter target height (or -1)",
                    5);
            if (scale != null) {
                try {
                    float sc = Float.parseFloat(scale);
                    _jMocap.loadBVH(f, sc, _view.getCursorPos());
 
                    // GUI related stuff:
                    _view.updateSkeletonInfo(f.getName());
                    _view.updateAnimInfo(f.getName());
                    _prop.put(PROP_LAST_BVH, f.toString());
                    saveProp();
                    _view.setFps((int) _jMocap.getFigure().getPlayer().getPlaybackFps());
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(
                            null,
                            "Couldn't load " + f.getName() + ": " + ex.getMessage(),
                            "loading error",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }
 
    @Override
    public void actionPerformed(ActionEvent e)
    {
        String c = e.getActionCommand();
        System.out.println("command=" + c);
        if (c.equals(JMocapGUI.LOAD_ASF)) {
            loadASFAction();
        } else if (c.equals(JMocapGUI.LOAD_AMC)) {
            loadAMCAction();
        } else if (c.equals(JMocapGUI.MENU_RESET_CAM)) {
            _jMocap.setCameraView(CAMERA, CAMERA_TARGET);
        } else if (c.equals(JMocapGUI.LOAD_BVH)) {
            loadBVHAction();
        }
    }
}