Normand Briere
2018-05-22 42107f9a01652cb2f47228d20c1148a2a22f6a63
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
package mocap.reader;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
 
import mocap.figure.AnimData;
import mocap.figure.Bone;
 
/**
 * Reads AMC motion files.
 * 
 * @author Michael Kipp
 */
public class AMCReader {
 
    public AMCReader() {
    }
 
//    public AnimData readAMC(File file, Bone skeletonRoot, int layer) {
    public AnimData readAMC(File file, Bone skeletonRoot) {
        System.out.println("Read AMC: " + file);
        Pattern num = Pattern.compile("[0-9]+");
        BufferedReader in = null;
        int keyframe = -1;
        try {
            HashMap<String, Bone> name2bone = new HashMap<String, Bone>();
            HashMap<String, List<Float>> name2data = new HashMap<String, List<Float>>();
            skeletonRoot.collectBones(name2bone);
            AnimData dat = new AnimData(name2bone.size());
            in = new BufferedReader(new FileReader(file));
            String line;
 
            // read data
            while ((line = in.readLine()) != null) {
 
                // ignore comments + acclaim info
                if (!(line.startsWith("#") || line.startsWith(":"))) {
                    if (num.matcher(line).matches()) {
                        keyframe++;
                    } else {
                        parseBoneKeyframe(line, name2data, name2bone);
                    }
                }
            }
 
            // put data in skeleton
            for (String key : name2bone.keySet()) {
                List<Float> ls = name2data.get(key);
                if (ls != null) {
//                    name2bone.get(key).loadAnimData(layer, ls);
                    dat.putBoneData(name2bone.get(key).getIndex(), ls);
                }
            }
            dat.setNumFrames(keyframe + 1);
            return dat;
        } catch (IOException ex) {
            Logger.getLogger(ASFReader.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                Logger.getLogger(ASFReader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
//        return keyframe + 1;
        return null;
    }
 
    private void parseBoneKeyframe(String line, HashMap<String, List<Float>> data,
            HashMap<String, Bone> name2bone) {
        String[] tok = line.split(" ");
        Bone b = name2bone.get(tok[0]);
        if (b == null) {
            System.out.println("WARNING: Bone " + tok[0] + " does not exist.");
            return;
        }
        List<Float> list = data.get(tok[0]);
        if (list == null) {
            list = new ArrayList<Float>();
            data.put(tok[0], list);
        }
        for (int i = 1; i < tok.length; i++) {
            try {
                Float fl = Float.parseFloat(tok[i]);
                if (b.getDOF()[i - 1] == Bone.TX || b.getDOF()[i - 1] == Bone.TY || b.getDOF()[i - 1] == Bone.TZ) {
                    list.add(fl);
                } else {
                    list.add((float) Math.toRadians(fl));
                }
            } catch (NumberFormatException e) {
                System.out.println("WARNING: Couldn't parse: " + tok[i]);
            }
        }
 
    }
}