libatomprobe
Library for Atom Probe Tomography (APT) computation
misc.h
Go to the documentation of this file.
1 /*
2  * misc.h - Miscellaneous helper functions
3  * Copyright (C) 2015 D Haley
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef ATOMPROBE_MISC_H
20 #define ATOMPROBE_MISC_H
21 #include "stringFuncs.h"
22 
23 namespace AtomProbe
24 {
26 class RGBf
27 {
28  public:
29  float red;
30  float green;
31  float blue;
32 
34  std::string toHex() const {
35  std:: string s;
36  genColString(red*255,green*255,blue*255,s);
37  return s;
38  };
39 
40  void fromHex(const std::string &s)
41  {
42  unsigned char c[4];
43  parseColString(s,c[0],c[1],c[2],c[3]);
44 
45  red=c[0]/255.0f;
46  green=c[1]/255.0f;
47  blue=c[2]/255.0f;
48  }
49 
50  inline bool operator==(const RGBf & oth) const
51  {
52  return (oth.red == red && oth.green == green && oth.blue==blue);
53  }
54 
55 };
56 
58 template<class T>
59 bool tolEqual(const T &a,const T &b, const T &f)
60 {
61  return abs(a-b) < f;
62 }
63 
64 
66 template<class T>
67 void selectElements(const std::vector<T> &in, const std::vector<unsigned int> &indices, std::vector<T>& out)
68 {
69  out.resize(indices.size());
70 #pragma omp parallel for
71  for(auto i=0u; i<indices.size(); ++i)
72  out[i]=in[indices[i]];
73 }
74 
75 
77 
80 template<class T>
81 void transposeVector(std::vector<std::vector<T> > &v)
82 {
83  if(!v.size())
84  return;
85 
86  std::vector<std::vector<T> > transposed;
87  transposed.resize(v[0].size());
88 
89  for(size_t ui=0;ui<transposed.size();ui++)
90  {
91  transposed[ui].resize(v.size());
92  }
93 
94 #pragma omp parallel for
95  for(size_t ui=0;ui<v.size();ui++)
96  {
97  for(size_t uj=0;uj<v[ui].size();uj++)
98  {
99  transposed[uj][ui]=v[ui][uj];
100  }
101  }
102 
103  v.swap(transposed);
104 }
105 
107 /* The pattern of removal will be unique with a given kill pattern.
108  the vectors must be the same size, and items for which the boolean vector
109  is true will be removed
110 */
111 template<class T>
112 void vectorMultiErase(std::vector<T> &vec, const std::vector<bool> &wantKill)
113 {
114  if(!vec.size())
115  return;
116  size_t shift=0;
117  for(size_t ui=0;ui<vec.size();ui++)
118  {
119  if(wantKill[ui])
120  shift++;
121  else if(shift)
122  vec[ui-shift] = vec[ui];
123  }
124  vec.resize(vec.size()-shift);
125 }
126 
127 #ifdef DEBUG
128 //Return true on success
129 bool runMiscMathsTests();
130 #endif
131 }
132 
133 #endif
Data holder for colour as float.
Definition: misc.h:26
void selectElements(const std::vector< T > &in, const std::vector< unsigned int > &indices, std::vector< T > &out)
Obtain the elements at positions indicies in the input vector, copy to output.
Definition: misc.h:67
float green
Definition: misc.h:30
void fromHex(const std::string &s)
Definition: misc.h:40
void vectorMultiErase(std::vector< T > &vec, const std::vector< bool > &wantKill)
Remove elements from the vector, without preserving order.
Definition: misc.h:112
void transposeVector(std::vector< std::vector< T > > &v)
Perform an out-of-place tranposition of a given vector.
Definition: misc.h:81
float blue
Definition: misc.h:31
void genColString(unsigned char r, unsigned char g, unsigned char b, unsigned char a, std::string &s)
std::string toHex() const
Convert to hex-256 representation #rrggbb.
Definition: misc.h:34
bool operator==(const RGBf &oth) const
Definition: misc.h:50
bool parseColString(const std::string &str, unsigned char &r, unsigned char &g, unsigned char &b, unsigned char &a)
Parse a colour string containing rgb[a]; hex for with leading #.
float red
Definition: misc.h:29
bool tolEqual(const T &a, const T &b, const T &f)
Test for equality within tolerance f (||a-b|| < f)
Definition: misc.h:59