1/*
2 * util.hpp
3 *
4 * Created on: May 7, 2015
5 * Author: Pietro Incardona
6 */
7
8#include "config.h"
9
10#include "util/SimpleRNG.hpp"
11
12#ifndef UTIL_HPP_
13#define UTIL_HPP_
14
15#include <boost/iostreams/device/mapped_file.hpp>
16
17
18/*! \brief Compare two files, return true if they match
19 *
20 * \param file1 path1
21 * \param file2 path2
22 *
23 * \return true if they match
24 *
25 */
26static inline bool compare(std::string file1, std::string file2)
27{
28 boost::iostreams::mapped_file_source f1(file1);
29 boost::iostreams::mapped_file_source f2(file2);
30
31 if( f1.size() == f2.size() && std::equal(f1.data(), f1.data() + f1.size(), f2.data()) )
32 return true;
33 else
34 return false;
35}
36
37//! RGB color struct
38struct RGB
39{
40 //! Red
41 float R;
42
43 //! Green
44 float G;
45
46 //! Blue
47 float B;
48
49 //! Return the color as string
50 std::string toString()
51 {
52 return std::to_string(R) + " " + std::to_string(G) + " " + std::to_string(B);
53 }
54};
55
56/*! \brief Return the color sampled from one group
57 *
58 * groups:
59 *
60 * 0: Red
61 * 1: Green
62 * 2: Blue
63 * 3: Yellow
64 * 4: Cyan
65 * 5: Magenta
66 * 6: Orange
67 * 7: Chartreuse-Green
68 * 8: Spring Green
69 * 9: Azure
70 * 10: Violet
71 * 11: Rose
72 *
73 * \param group
74 *
75 */
76
77static inline struct RGB getColor(int group, SimpleRNG & d)
78{
79 struct RGB col;
80
81 float s = (float)d.GetUniform();
82
83 group = group % 12;
84
85#ifdef ON_IO_UNIT_TESTS
86 s = 0.5;
87#endif
88
89 if (group == 0)
90 {
91 col.R = s/2 + 0.5;
92 col.G = 0.0;
93 col.B = 0.0;
94 }
95 else if (group == 1)
96 {
97 col.R = 0.0;
98 col.G = s/2 + 0.5;
99 col.B = 0.0;
100 }
101 else if (group == 2)
102 {
103 col.R = 0.0;
104 col.G = 0.0;
105 col.B = s;
106 }
107 else if (group == 3)
108 {
109 col.R = s/2 + 0.5;
110 col.G = s/2 + 0.5;
111 col.B = 0.0;
112 }
113 else if (group == 4)
114 {
115 col.R = s/2 + 0.5;
116 col.G = 0.0;
117 col.B = s/2 + 0.5;
118 }
119 else if (group == 5)
120 {
121 col.R = 0.0;
122 col.G = s/2 + 0.5;
123 col.B = s/2 + 0.5;
124 }
125 else if (group == 6)
126 {
127 col.R = s/2 + 0.5;
128 col.G = s/4 + 0.5;
129 col.B = 0.0;
130 }
131 else if (group == 7)
132 {
133 col.R = s/4 + 0.5;
134 col.G = s/2 + 0.5;
135 col.B = 0.0;
136 }
137 else if (group == 8)
138 {
139 col.R = 0.0;
140 col.G = s/2 + 0.5;
141 col.B = s/4 + 0.5;
142 }
143 else if (group == 9)
144 {
145 col.R = 0.0;
146 col.G = s/4 + 0.5;
147 col.B = s/2 + 0.5;
148 }
149 else if (group == 10)
150 {
151 col.R = s/4 + 0.5;
152 col.G = 0.0;
153 col.B = s/2 + 0.5;
154 }
155 else
156 {
157 col.R = s/2 + 0.5;
158 col.G = 0.0;
159 col.B = s/4 + 0.5;
160 }
161
162 return col;
163}
164
165/*! \brief Check if one string end with a particular string
166 *
167 * \param fullString string to check
168 * \param ending ending string to check
169 *
170 */
171static inline bool hasEnding (std::string const &fullString, std::string const &ending)
172{
173 if (fullString.length() >= ending.length())
174 {return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));}
175 else
176 {return false;}
177}
178
179#endif /* UTIL_HPP_ */
180