OMToolkit  1.0
The polygonal mesh processing tool.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
OMVisualiser.hxx
Go to the documentation of this file.
1 //==============================================================================
15 #ifndef _OM_VISUALISER_HXX_
16 #define _OM_VISUALISER_HXX_
17 
19 // Constructor - creates a link to a mesh and initializes variables
20 // @param mesh Pointer to a mesh
22 template <class Mesh, class Scalar>
23 OMVisualiser<Mesh, Scalar>::OMVisualiser(Mesh *mesh) : importer(*mesh)
24 {
25  m_mesh = mesh;
26  m_maxBlue = 0.0;
27  m_maxGreen = 0.0;
28  m_maxRed = 0.0;
29 }
30 
32 // Computes colors from a vertex vector property and saves them into mesh
33 // @param vertexProperty Handle to a property, from which we will compute colors
34 // @param vectorComponent Number of vector component to be visualised
35 // @param ommitExtremaPercent Number of percent which will be ommited from extrema (for ex. value 2.0 ommits 2% of brightest and darkest points)
37 template <class Mesh, class Vector>
38 bool OMVectorVisualiser<Mesh, Vector>::ComputeColors(OpenMesh::VPropHandleT<Vector> vertexProperty, unsigned int vectorComponent, Scalar ommitExtremaPercent)
39 {
40  if (!m_mesh->has_vertex_colors())
41  m_mesh->request_vertex_colors();
42 
43  Mesh::VertexIter end = m_mesh->vertices_end();
44 
45  std::vector<Scalar> sorted;
46 
47  // sort all values
48  for (Mesh::VertexIter vertex = m_mesh->vertices_begin(); vertex != end; ++vertex)
49  {
50  if (m_mesh->property(vertexProperty, vertex).size() <= vectorComponent)
51  return false;
52  sorted.push_back(m_mesh->property(vertexProperty, vertex)[vectorComponent]);
53  }
54  std::sort(sorted.begin(), sorted.end());
55 
56  Scalar percent = 1.0;
57  if (ommitExtremaPercent > 0)
58  {
59  percent = 100 / ommitExtremaPercent;
60  m_maxBlue = sorted[sorted.size()/percent];
61  m_maxRed = sorted[sorted.size()-sorted.size()/percent];
62  }
63  else
64  {
65  m_maxBlue = sorted[0];
66  m_maxRed = sorted[sorted.size()];
67  }
68 
69  /*
70  _________ _________
71  \ /\ /
72  \ / \ /
73  \/ \/
74  / \ /\
75  / \ / \
76  / \/ \
77  maxB maxG maxR
78  */
79 
80  // set minimum, maximum and mean
81  m_maxGreen = (m_maxBlue + m_maxRed) / 2;
82 
83  m_Difference = m_maxBlue;
84  m_maxBlue -= m_Difference;
85  m_maxRed -= m_Difference;
86  m_maxGreen -= m_Difference;
87 
88  for (Mesh::VertexIter vertex = m_mesh->vertices_begin(); vertex != end; ++vertex)
89  {
90  importer.set_color(vertex, getColor(m_mesh->property(vertexProperty, vertex)[vectorComponent]));
91  }
92  return true;
93 }
95 // Computes colors from a vertex scalar property and saves them into mesh
96 // @param vertexProperty Handle to a property, from which we will compute colors
97 // @param ommitExtremaPercent Number of percent which will be ommited from extrema (for ex. value 2.0 ommits 2% of brightest and darkest points)
99 template <class Mesh, class Scalar>
100 void OMVisualiser<Mesh, Scalar>::ComputeColors(OpenMesh::VPropHandleT<Scalar> vertexProperty, Scalar ommitExtremaPercent)
101 {
102  if (!m_mesh->has_vertex_colors())
103  m_mesh->request_vertex_colors();
104 
105  Mesh::VertexIter end = m_mesh->vertices_end();
106 
107  std::vector<Scalar> sorted;
108 
109  // sort all values
110  for (Mesh::VertexIter vertex = m_mesh->vertices_begin(); vertex != end; ++vertex)
111  sorted.push_back((Scalar)m_mesh->property(vertexProperty, vertex));
112  std::sort(sorted.begin(), sorted.end());
113 
114  Scalar percent = 1.0;
115  if (ommitExtremaPercent > 0)
116  {
117  percent = 100 / ommitExtremaPercent;
118  m_maxBlue = sorted[sorted.size()/percent];
119  m_maxRed = sorted[sorted.size()-sorted.size()/percent];
120  }
121  else
122  {
123  m_maxBlue = sorted[0];
124  m_maxRed = sorted[sorted.size()];
125  }
126 
127  /*
128  _________ _________
129  \ /\ /
130  \ / \ /
131  \/ \/
132  / \ /\
133  / \ / \
134  / \/ \
135  maxB maxG maxR
136  */
137 
138  // set minimum, maximum and mean
139  m_maxGreen = (m_maxBlue + m_maxRed) / 2;
140 
141  m_Difference = m_maxBlue;
142  m_maxBlue -= m_Difference;
143  m_maxRed -= m_Difference;
144  m_maxGreen -= m_Difference;
145 
146  for (Mesh::VertexIter vertex = m_mesh->vertices_begin(); vertex != end; ++vertex)
147  {
148  importer.set_color(vertex, getColor(m_mesh->property(vertexProperty, vertex)));
149  }
150 }
151 
153 // Computes colors from a face scalar property and saves them into mesh
154 // @param vertexProperty Handle to a property, from which we will compute colors
155 // @param ommitExtremaPercent Number of percent which will be ommited from extrema (for ex. value 2.0 ommits 2% of brightest and darkest points)
157 template <class Mesh, class Scalar>
158 void OMVisualiser<Mesh, Scalar>::ComputeColors(OpenMesh::FPropHandleT<Scalar> faceProperty, Scalar ommitExtremaPercent)
159 {
160  if (!m_mesh->has_face_colors())
161  m_mesh->request_face_colors();
162 
163  Mesh::FaceIter end = m_mesh->faces_end();
164 
165  std::vector<Scalar> sorted;
166 
167  // sort all values
168  for (Mesh::FaceIter face = m_mesh->faces_begin(); face != end; ++face)
169  sorted.push_back((Scalar)m_mesh->property(faceProperty, face));
170  std::sort(sorted.begin(), sorted.end());
171 
172  Scalar percent = 1.0;
173  if (ommitExtremaPercent > 0)
174  {
175  percent = 100 / ommitExtremaPercent;
176  m_maxBlue = sorted[sorted.size()/percent];
177  m_maxRed = sorted[sorted.size()-sorted.size()/percent];
178  }
179  else
180  {
181  m_maxBlue = sorted[0];
182  m_maxRed = sorted[sorted.size()];
183  }
184  /*
185  _________ _________
186  \ /\ /
187  \ / \ /
188  \/ \/
189  / \ /\
190  / \ / \
191  / \/ \
192  maxB maxG maxR
193  */
194 
195  // set minimum, maximum and mean
196 
197  m_maxGreen = (m_maxBlue + m_maxRed) / 2;
198 
199  m_Difference = m_maxBlue;
200  m_maxBlue -= m_Difference;
201  m_maxRed -= m_Difference;
202  m_maxGreen -= m_Difference;
203 
204  for (Mesh::FaceIter face = m_mesh->faces_begin(); face != end; ++face)
205  {
206  importer.set_color(face, getColor(m_mesh->property(faceProperty, face)));
207  }
208 }
209 
211 // Method computes a color from given value
212 // @param current Property value
213 // @return Color vector
215 template <class Mesh, class Scalar>
216 OpenMesh::Vec4uc OMVisualiser<Mesh, Scalar>::getColor(Scalar current)
217 {
218  current -= m_Difference;
219 
220  Scalar r = 0.0;
221  Scalar g = 0.0;
222  Scalar b = 0.0;
223 
224  // blue and green
225  if (current > m_maxGreen)
226  {
227  r = (255.0/(m_maxRed-m_maxGreen)) * current - ((255.0 / (m_maxRed-m_maxGreen))*m_maxRed) + 255.0;
228  g = (-255.0 / (m_maxRed-m_maxGreen)) * current + ((255.0 / (m_maxRed-m_maxGreen))*m_maxRed);
229  }
230  // red and green
231  else
232  {
233  g = (255.0/(m_maxGreen-m_maxBlue)) * current - ((255.0 / (m_maxGreen-m_maxBlue))*m_maxGreen) + 255.0;
234  b = (-255.0 / (m_maxGreen-m_maxBlue)) * current +((255.0 / (m_maxGreen-m_maxBlue))*m_maxGreen);
235  }
236 
237  /*std::ofstream file;
238  file.open("output.csv", std::ios_base::app);
239  if (m_maxBlue > current) file << m_maxBlue << std::endl;
240  else if (m_maxRed < current) file << m_maxRed << std::endl;
241  else file << current << std::endl;
242  file.close();*/
243 
244  // control due to ommiting some values
245  if (r < 0) r = 0;
246  if (g < 0) g = 0;
247  if (b < 0) b = 0;
248  if (r > 255) r = 255;
249  if (g > 255) g = 255;
250  if (b > 255) b = 255;
251 
252  return OpenMesh::Vec4uc((unsigned char)r, (unsigned char)g, (unsigned char)b, 255);
253 }
254 
256 // Returns values of Red, Green and Blue extremas (for painting a legend)
257 // @param maxBlue Property value for (0, 0, 255, 255) color
258 // @param maxGreen Property value for (0, 255, 0, 255) color
259 // @param maxRed Property value for (255, 0, 0, 255) color
261 template <class Mesh, class Scalar>
262 void OMVisualiser<Mesh, Scalar>::getLegend(Scalar &maxBlue, Scalar &maxGreen, Scalar &maxRed)
263 {
264  maxBlue = m_maxBlue + m_Difference;
265  maxGreen = m_maxGreen + m_Difference;
266  maxRed = m_maxRed + m_Difference;
267 }
268 
269 #endif