Revert "Use fabs() for floats, not abs()".
[agianapa.git] / mkspdata / main.cpp
blob1302f517a72a50c9ba81404b676801a5fed311f9
1 #include <cstdlib> // For `EXIT_FAILURE' constant
2 #include <iostream>
3 #include <iomanip> // For std::precision
5 #include <getopt.h> // FIXME: make it portable
6 #include <math.h>
8 #include <QDataStream>
9 #include <QFile>
11 /* Some constants */
12 #define MAGIC_NUMBER 0xA0B0C0D0
13 #define PROT_VERSION 0x1
15 /* Function prototypes */
16 static void usage(const char *pname);
18 int main(int argc, char *argv[])
20 QFile file;
21 float duration, endTime, startTime, timeStep;
22 float x, y, z, xR, yR, zR, t;
23 quint32 i, cnt, magicNumber, numRecords, numReplays, protVersion;
24 int opt;
26 /* Parse arguments */
27 endTime = startTime = 0;
28 numReplays = 1;
29 timeStep = 1;
30 while ((opt = getopt(argc, argv, "f:s:e:t:r:")) != -1) {
31 switch (opt) {
32 case 'f':
33 file.setFileName(optarg);
34 break;
35 case 's':
36 startTime = atof(optarg);
37 break;
38 case 'e':
39 endTime = atof(optarg);
40 break;
41 case 't':
42 timeStep = atof(optarg);
43 break;
44 case 'r':
45 numReplays = atoi(optarg);
46 break;
47 default:
48 usage(argv[0]);
49 /* Never reached */
53 qDebug("Start time = %f\tEnd time = %f\tTime step = %f\tReplays = %d",
54 startTime, endTime, timeStep, numReplays);
56 // Validate input
57 duration = numReplays * (endTime - startTime);
58 if (duration <= 0) {
59 qDebug("Duration must be positive");
60 exit(EXIT_FAILURE);
63 // Open file for writing
64 if (file.exists()) {
65 qDebug("File already exists!");
66 exit(EXIT_FAILURE);
69 file.open(QIODevice::WriteOnly);
70 QDataStream out(&file);
72 // Write a header with a "magic number" and a protocol version
73 out << (quint32) MAGIC_NUMBER;
74 out << (quint32) PROT_VERSION;
75 out << (float) duration;
76 out << (quint32) (duration / timeStep);
78 // Write actual data
79 for (i = 0, cnt = 0; i < numReplays; i++) {
80 for (t = startTime; t < endTime; t += timeStep, cnt++) {
81 out << (float) cnt * timeStep
82 << (float) (5.0 * t * t) << 0.0f << 0.0f
83 << (float) (cos(t) * 180.0) << 0.0f << 0.0f;
87 // Done -- close file
88 file.close();
90 // Open file for reading
91 file.open(QIODevice::ReadOnly);
92 QDataStream in(&file);
94 // Read "magic number" and protocol version
95 in >> magicNumber;
96 if (magicNumber != MAGIC_NUMBER) {
97 qDebug("Magic number mismatch\n");
98 exit(EXIT_FAILURE);
101 in >> protVersion;
102 if (protVersion != PROT_VERSION) {
103 qDebug("Protocol version mismatch\n");
104 exit(EXIT_FAILURE);
107 in >> duration;
108 in >> numRecords;
110 std::cout << "Magic number = 0x" << std::hex << magicNumber << "\n"
111 << "Protocol version = 0x" << protVersion << "\n"
112 << "Duration(sec) = " << std::dec << duration << "\n"
113 << "# of records = " << numRecords << "\n";
115 // Read data
116 while (!in.atEnd()) {
117 in >> t >> x >> y >> z >> xR >> yR >> zR;
118 std::cout << std::setprecision(3) << std::fixed
119 <<"t = " << t
120 << "\tx = " << x
121 << "\ty = " << y
122 << "\tz = " << z
123 << "\txR = " << xR
124 << "\tyR = " << yR
125 << "\tzR = " << zR << "\n";
128 // Done -- close file
129 file.close();
131 return EXIT_SUCCESS;
134 static void usage(const char *pname)
136 qDebug("Usage: %s -f fileName -s startTime -e endTime -t timeStep"
137 " -r replays\n", pname);
138 exit(EXIT_FAILURE);