CAVAPA-GUI  30.5.2014
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
results.h
1 /****************************************************************************
2  * Copyright (c) 2014, Joel Kivelä, Erkki Koskenkorva, Oskari Leppäaho,
3  * Mika Lehtinen and Petri Partanen.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 ****************************************************************************/
30 #ifndef RESULTS_H
31 #define RESULTS_H
32 
33 #include <fstream>
34 #include <set>
35 #include <string>
36 #include <tuple>
37 #include <utility>
38 
39 #include "common.h"
40 
41 namespace cavapa_gui
42 {
88 class Results
89 {
90 public:
103  const std::vector<cavapa::Sightings> &sightings);
104 
108  void clear() { results.clear(); }
109 
114  bool empty() const { return results.empty(); }
115 
124  bool exportToCSV(const ExportOptions& options) const;
125 
130  unsigned int getCount() const { return results.size(); }
131 
138  { return results.size() == 0 ? 0 : getStats(results.at(0)).time; }
139 
146  int getFrameNumber(FrameTime time) const;
147 
153  std::vector<cavapa::Sightings> getSightings(FrameTime time) const;
154 
170  std::vector<FrameStats> getStatistics(FrameTime start,
171  FrameTime stop, int points) const;
172 
179  bool load(const std::string& path);
180 
186  bool save(const std::string& path) const;
187 
188 private:
196  struct SightingFixed
197  {
198  SightingFixed() {}
199 
204  SightingFixed(const cavapa::Sighting& s) : id(s.id), world_x(s.world.x),
205  world_y(s.world.y), certainty(s.certainty), topleft_x(s.topleft.x),
206  topleft_y(s.topleft.y), bottomright_x(s.bottomright.x),
207  bottomright_y(s.bottomright.y) {}
208 
209  std::int32_t id;
210  float world_x;
211  float world_y;
212  float certainty;
213  std::int16_t topleft_x;
214  std::int16_t topleft_y;
215  std::int16_t bottomright_x;
216  std::int16_t bottomright_y;
217 
218  // This check is performed in order to check the correct member size in
219  // structure to maintain 32bit & 64bit file format integrity.
220  static_assert(sizeof(SightingFixed::id) == 4
221  && sizeof(SightingFixed::world_x) == 4
222  && sizeof(SightingFixed::world_y) == 4
223  && sizeof(SightingFixed::certainty) == 4
224  && sizeof(SightingFixed::topleft_x) == 2
225  && sizeof(SightingFixed::topleft_y) == 2
226  && sizeof(SightingFixed::bottomright_x) == 2
227  && sizeof(SightingFixed::bottomright_y) == 2,
228  "The size of the structure members is not correct.");
229  };
230 
236  static float calculateActivity(const std::vector<cavapa::Sightings>& s);
237 
243  static int calculateTotalSightings(const std::vector<cavapa::Sightings>& s);
244 
248  using FrameResults = std::tuple<FrameStats,
249  std::vector<std::uint32_t>, std::vector<SightingFixed>>;
250 
254  using MarkerSet = std::set<Marker, std::function<bool (Marker, Marker)>>;
255 
265  static inline FrameStats getAverage(
266  std::vector<FrameResults>::const_iterator a,
267  std::vector<FrameResults>::const_iterator b);
268 
280  static std::string getMarkerTexts(FrameTime stop, MarkerSet& markers);
281 
290  static std::vector<SightingFixed> getSightings(const FrameResults& r)
291  { return std::get<2>(r); }
292 
301  static std::vector<SightingFixed> getSightings(
302  const std::vector<FrameResults>::const_iterator& it)
303  { return getSightings(*it); }
304 
313  static std::vector<std::uint32_t> getSightingCounts(const FrameResults& r)
314  { return std::get<1>(r); }
315 
322  inline std::vector<FrameResults>::const_iterator
323  getStartPoint(FrameTime ft) const;
324 
333  static FrameStats getStats(const FrameResults& r)
334  { return std::get<0>(r); }
335 
344  static FrameStats getStats(
345  const std::vector<FrameResults>::const_iterator& it)
346  { return getStats(*it); }
347 
354  inline std::vector<FrameResults>::const_iterator
355  getStopPoint(FrameTime ft) const;
356 
365  static MarkerSet sortMarkers(FrameTime start, FrameTime stop,
366  const std::vector<Marker>& markers);
367 
368  std::vector<FrameResults> results; // Holds information about each frame.
369 };
370 } // namespace
371 
372 #endif // RESULTS_H
FrameTime getFirstFrameTime() const
Retrieves the time of the first frame in Results.
Definition: results.h:137
int getFrameNumber(FrameTime time) const
Returns the frame number for the given FrameTime.
Definition: results.cpp:260
std::vector< FrameStats > getStatistics(FrameTime start, FrameTime stop, int points) const
Returns the calculation statistics from the given period.
Definition: results.cpp:334
bool exportToCSV(const ExportOptions &options) const
Exports the results to the CSV file.
Definition: results.cpp:124
std::vector< cavapa::Sightings > getSightings(FrameTime time) const
Retrieves the sighting information from the frame.
Definition: results.cpp:299
Structure for holding frame statistics on activity.
Definition: common.h:285
bool empty() const
Checks if the are no results.
Definition: results.h:114
std::uint64_t FrameTime
Used to store milliseconds interval in frame times.
Definition: common.h:138
FrameStats addFrame(FrameTime time, const std::vector< cavapa::Sightings > &sightings)
Adds the new frame to the results.
Definition: results.cpp:52
bool load(const std::string &path)
Loads the calculation results from the file.
Definition: results.cpp:384
The structure holds CSV export options.
Definition: common.h:183
unsigned int getCount() const
Returns the number of the frames saved.
Definition: results.h:130
void clear()
Deletes all the stored calculation results.
Definition: results.h:108
The class holds the calculation results for each frame.
Definition: results.h:88
bool save(const std::string &path) const
Saves the calculation results to the file.
Definition: results.cpp:450