Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
OMPropertyVisualizerVectorT.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #define OM_PROPERTY_VISUALIZER_VECTOR_CC
51 
52 #include "OMPropertyVisualizerVector.hh"
53 #include <ACG/Utils/ColorConversion.hh>
54 
55 template <typename MeshT>
57  : OMPropertyVisualizer<MeshT>(_mesh, _propertyInfo)
58 {
59  if (PropertyVisualizer::widget) delete PropertyVisualizer::widget;
60  VectorWidget* w = new VectorWidget();
61  w->paramVector->setTitle(QString("3D Vector Parameters of ").append(PropertyVisualizer::propertyInfo.propName().c_str()));
62  PropertyVisualizer::widget = w;
63 
64  lineNode = new ACG::SceneGraph::LineNode( ACG::SceneGraph::LineNode::LineSegmentsMode, dynamic_cast < ACG::SceneGraph::SeparatorNode* >( PluginFunctions::getRootNode() ) );
65 
66  if (!_propertyInfo.isFaceProp())
67  {
68  w->vectors_edges_rb->hide();
69  }
70 
71 }
72 
73 template <typename MeshT>
75 {
76  lineNode->clear();
78 }
79 
80 template <typename MeshT>
82 {
83  return OMPropertyVisualizer<MeshT>::template getPropertyText_<typename MeshT::Point>(index);
84 }
85 
86 
87 namespace {
88 
89 template<typename PROPTYPE, typename MeshT, typename ENTITY_IT, typename PROPINFO_TYPE>
90 void visualizeVectorAsColorForEntity(MeshT *mesh, const ENTITY_IT e_begin, const ENTITY_IT e_end,
91  const PROPINFO_TYPE &propinfo) {
92  PROPTYPE prop;
93  if (!mesh->get_property_handle(prop, propinfo.propName()))
94  throw VizException("Getting PropHandle from mesh for selected property failed.");
95  for (ENTITY_IT e_it = e_begin; e_it != e_end; ++e_it) {
96  typename MeshT::Point v = mesh->property(prop, *e_it).normalized() * .5 + typename MeshT::Point(.5, .5, .5);
97  mesh->set_color(*e_it, typename MeshT::Color(v[0], v[1], v[2], 1.0));
98  }
99 }
100 
101 template<typename PROPTYPE, typename MeshT, typename ENTITY_IT, typename PROPINFO_TYPE>
102 void visualizeVectorLengthAsColorForEntity(
103  MeshT *mesh, const ENTITY_IT e_begin, const ENTITY_IT e_end,
104  const PROPINFO_TYPE &propinfo) {
105  PROPTYPE prop;
106  if (!mesh->get_property_handle(prop, propinfo.propName()))
107  throw VizException("Getting PropHandle from mesh for selected "
108  "property failed.");
109 
110  double min = std::numeric_limits<double>::infinity();
111  double max = -std::numeric_limits<double>::infinity();
112 
113  for (ENTITY_IT e_it = e_begin; e_it != e_end; ++e_it) {
114  const double val = mesh->property(prop, *e_it).norm();
115  min = std::min(min, val);
116  max = std::max(max, val);
117  }
118 
119  ACG::ColorCoder color_coder(min, max);
120 
121  for (ENTITY_IT e_it = e_begin; e_it != e_end; ++e_it) {
122  mesh->set_color(*e_it, color_coder(mesh->property(prop, *e_it).norm()));
123  }
124 }
125 
126 }
127 
128 template <typename MeshT>
130 {
131  VectorWidget* const w = (VectorWidget*)PropertyVisualizer::widget;
132  if (w->vectors_edges_rb->isChecked()) {
133  visualizeFacePropOnEdges();
134  } else if (w->vectors_colors_rb->isChecked() ||
135  w->vectors_length_color_rb->isChecked()) {
136  if ( !OMPropertyVisualizer<MeshT>::mesh->has_face_colors() )
137  OMPropertyVisualizer<MeshT>::mesh->request_face_colors();
138 
139  if (w->vectors_colors_rb->isChecked()) {
140  visualizeVectorAsColorForEntity<OpenMesh::FPropHandleT<typename MeshT::Point> >(
142  OMPropertyVisualizer<MeshT>::mesh->faces_begin(),
143  OMPropertyVisualizer<MeshT>::mesh->faces_end(),
144  PropertyVisualizer::propertyInfo);
145  } else {
146  visualizeVectorLengthAsColorForEntity<OpenMesh::FPropHandleT<typename MeshT::Point> >(
147  OMPropertyVisualizer<MeshT>::mesh,
148  OMPropertyVisualizer<MeshT>::mesh->faces_begin(),
149  OMPropertyVisualizer<MeshT>::mesh->faces_end(),
150  PropertyVisualizer::propertyInfo);
151  }
152  if (_setDrawMode)
154  }
155  else visualizeFacePropAsStrokes();
156 }
157 
158 template <typename MeshT>
160 {
161  VectorWidget* const w = (VectorWidget*)PropertyVisualizer::widget;
162  if (w->vectors_colors_rb->isChecked() ||
163  w->vectors_length_color_rb->isChecked()) {
164  if ( !OMPropertyVisualizer<MeshT>::mesh->has_edge_colors() )
165  OMPropertyVisualizer<MeshT>::mesh->request_edge_colors();
166  MeshT* mesh = OMPropertyVisualizer<MeshT>::mesh;
167  if ( !mesh->has_edge_colors() )
168  mesh->request_edge_colors();
169  if (w->vectors_colors_rb->isChecked()) {
170  visualizeVectorAsColorForEntity<
172  mesh,
173  mesh->edges_begin(),
174  mesh->edges_end(),
175  PropertyVisualizer::propertyInfo);
176  } else {
177  visualizeVectorLengthAsColorForEntity<
179  mesh,
180  mesh->edges_begin(),
181  mesh->edges_end(),
182  PropertyVisualizer::propertyInfo);
183  }
184  if (_setDrawMode)
186  }
187  else visualizeEdgePropAsStrokes();
188 }
189 
190 template <typename MeshT>
192 {
193  VectorWidget* const w = (VectorWidget*)PropertyVisualizer::widget;
194  if (w->vectors_colors_rb->isChecked() ||
195  w->vectors_length_color_rb->isChecked()) {
196  if ( !OMPropertyVisualizer<MeshT>::mesh->has_halfedge_colors() )
197  OMPropertyVisualizer<MeshT>::mesh->request_halfedge_colors();
198  MeshT* mesh = OMPropertyVisualizer<MeshT>::mesh;
199  if ( ! mesh->has_halfedge_colors() )
200  mesh->request_halfedge_colors();
201 
202  if (w->vectors_colors_rb->isChecked()) {
203  visualizeVectorAsColorForEntity<
205  mesh,
206  mesh->halfedges_begin(),
207  mesh->halfedges_end(),
208  PropertyVisualizer::propertyInfo);
209  } else {
210  visualizeVectorLengthAsColorForEntity<
212  mesh,
213  mesh->halfedges_begin(),
214  mesh->halfedges_end(),
215  PropertyVisualizer::propertyInfo);
216  }
217 
218  if (_setDrawMode)
221  }
222  else visualizeHalfedgePropAsStrokes();
223 }
224 
225 template <typename MeshT>
227 {
228  VectorWidget* const w = (VectorWidget*)PropertyVisualizer::widget;
229  if (w->vectors_colors_rb->isChecked() ||
230  w->vectors_length_color_rb->isChecked()) {
231  if ( !OMPropertyVisualizer<MeshT>::mesh->has_vertex_colors() )
232  OMPropertyVisualizer<MeshT>::mesh->request_vertex_colors();
233 
234  if (w->vectors_colors_rb->isChecked()) {
235  visualizeVectorAsColorForEntity<
237  OMPropertyVisualizer<MeshT>::mesh,
238  OMPropertyVisualizer<MeshT>::mesh->vertices_begin(),
239  OMPropertyVisualizer<MeshT>::mesh->vertices_end(),
240  PropertyVisualizer::propertyInfo);
241  } else {
242  visualizeVectorLengthAsColorForEntity<
244  OMPropertyVisualizer<MeshT>::mesh,
245  OMPropertyVisualizer<MeshT>::mesh->vertices_begin(),
246  OMPropertyVisualizer<MeshT>::mesh->vertices_end(),
247  PropertyVisualizer::propertyInfo);
248  }
249  if (_setDrawMode)
252  }
253  else visualizeVertexPropAsStrokes();
254 }
255 
256 template <typename MeshT>
258 
259  VectorWidget* w = (VectorWidget*)PropertyVisualizer::widget;
260  MeshT* _mesh = OMPropertyVisualizer<MeshT>::mesh;
261 
262 
263  const double thresh_1 = w->vectors_edges_alpha->value();
264  const double thresh_2 = std::min(thresh_1, w->vectors_edges_alpha->value());
265 
267  if (!_mesh->get_property_handle(prop, PropertyVisualizer::propertyInfo.propName()))
268  throw VizException("Getting PropHandle from mesh for selected property failed.");
269 
270  if (!_mesh->has_edge_colors())
271  _mesh->request_edge_colors();
272  const ACG::Vec4f cold(0, 0, 0, 1.0), hot(0, 1, 0, 1.0), degen(1, 1, 0, 1.0);
273  for (typename MeshT::EdgeIter e_it = _mesh->edges_begin(), e_end = _mesh->edges_end();
274  e_it != e_end; ++e_it) {
275  typename MeshT::Point p1 = _mesh->property(prop, _mesh->face_handle(_mesh->halfedge_handle(*e_it, 0)));
276  typename MeshT::Point p2 = _mesh->property(prop, _mesh->face_handle(_mesh->halfedge_handle(*e_it, 1)));
277 
278  ACG::Vec4f color;
279 
280  const char degenerate = ((p1.sqrnorm() < 1e-6) ? 1 : 0) | ((p2.sqrnorm() < 1e-6) ? 2 : 0);
281  if (degenerate == 3) {
282  color = cold;
283  } else if (degenerate == 0) {
284  p1.normalize(); p2.normalize();
285  const double alpha = std::min(1.0, std::abs(p1 | p2));
286  if (alpha < thresh_1)
287  color = hot;
288  else if (alpha > thresh_2)
289  color = cold;
290  else {
291  const double beta = (alpha - thresh_1) / (thresh_2 - thresh_1);
292  color = cold * beta + hot * (1.0 - beta);
293  }
294  } else {
295  color = degen;
296  }
297  _mesh->set_color(*e_it, color);
298  }
300 }
301 
302 template <typename MeshT>
304  {
305  VectorWidget* vectorWidget = static_cast<VectorWidget*>(PropertyVisualizer::widget);
306 
307  lineNode->clear();
308 
309  typename MeshT::Color color = ACG::to_Vec4f(vectorWidget->lineColor->color());
310 
312 
313  if ( !OMPropertyVisualizer<MeshT>::mesh->get_property_handle(prop, OMPropertyVisualizer<MeshT>::propertyInfo.propName() ) )
314  return;
315 
316  for (typename MeshT::FaceIter f_it = OMPropertyVisualizer<MeshT>::mesh->faces_begin() ; f_it != OMPropertyVisualizer<MeshT>::mesh->faces_end() ; ++f_it){
317 
318  typename MeshT::Point center(0.0, 0.0, 0.0);
319  int vCount = 0;
320 
321  for (typename MeshT::FaceVertexIter fv_it(*(OMPropertyVisualizer<MeshT>::mesh),*f_it); fv_it.is_valid(); ++fv_it){
322  vCount++;
323  center += OMPropertyVisualizer<MeshT>::mesh->point(*fv_it);
324  }
325 
326  center /= vCount;
327 
328  typename MeshT::Point v = (OMPropertyVisualizer<MeshT>::mesh->property(prop, *f_it));
329 
330  if (vectorWidget->normalize->isChecked() && v.sqrnorm() > 1e-12)
331  v.normalize();
332 
333  if(vectorWidget->scale->isChecked())
334  v *= vectorWidget->scaleBox->value();
335 
336  lineNode->add_line( center, (center+v) );
337  lineNode->add_color(color);
338  }
339 }
340 
341 template <typename MeshT>
343 {
344  VectorWidget* vectorWidget = static_cast<VectorWidget*>(PropertyVisualizer::widget);
345 
346  lineNode->clear();
347 
348  typename MeshT::Color color = ACG::to_Vec4f(vectorWidget->lineColor->color());
349 
350  //TODO check if this also works if the property is Vec3f
352 
353  if ( !OMPropertyVisualizer<MeshT>::mesh->get_property_handle(prop, OMPropertyVisualizer<MeshT>::propertyInfo.propName() ) )
354  return;
355 
356  for (typename MeshT::EdgeIter e_it = OMPropertyVisualizer<MeshT>::mesh->edges_begin() ; e_it != OMPropertyVisualizer<MeshT>::mesh->edges_end() ; ++e_it){
357 
358  typename MeshT::HalfedgeHandle hh = OMPropertyVisualizer<MeshT>::mesh->halfedge_handle( *e_it, 0 );
359 
360  typename MeshT::VertexHandle vh0 = OMPropertyVisualizer<MeshT>::mesh->from_vertex_handle( hh );
361  typename MeshT::VertexHandle vh1 = OMPropertyVisualizer<MeshT>::mesh->to_vertex_handle( hh );
362 
363  typename MeshT::Point v1 = OMPropertyVisualizer<MeshT>::mesh->point(vh0) + 0.5 * (OMPropertyVisualizer<MeshT>::mesh->point(vh1) - OMPropertyVisualizer<MeshT>::mesh->point(vh0));
364  typename MeshT::Point v = OMPropertyVisualizer<MeshT>::mesh->property(prop, *e_it);
365 
366  if (vectorWidget->normalize->isChecked() && v.sqrnorm() > 1e-12)
367  v.normalize();
368 
369  if(vectorWidget->scale->isChecked())
370  v *= vectorWidget->scaleBox->value();
371 
372  lineNode->add_line( v1, (v1+v) );
373  lineNode->add_color(color);
374  }
375 }
376 
377 template <typename MeshT>
379 {
380  VectorWidget* vectorWidget = static_cast<VectorWidget*>(PropertyVisualizer::widget);
381 
382  lineNode->clear();
383 
384  typename MeshT::Color color = ACG::to_Vec4f(vectorWidget->lineColor->color());
385 
386  //TODO check if this also works if the property is Vec3f
388 
389  if ( !OMPropertyVisualizer<MeshT>::mesh->get_property_handle(prop, OMPropertyVisualizer<MeshT>::propertyInfo.propName() ) )
390  return;
391 
392  for (typename MeshT::HalfedgeIter he_it = OMPropertyVisualizer<MeshT>::mesh->halfedges_begin() ; he_it != OMPropertyVisualizer<MeshT>::mesh->halfedges_end() ; ++he_it){
393 
394  typename MeshT::VertexHandle vh0 = OMPropertyVisualizer<MeshT>::mesh->from_vertex_handle( *he_it );
395  typename MeshT::VertexHandle vh1 = OMPropertyVisualizer<MeshT>::mesh->to_vertex_handle( *he_it );
396 
397  typename MeshT::Point v1 = OMPropertyVisualizer<MeshT>::mesh->point(vh0) + 0.5 * (OMPropertyVisualizer<MeshT>::mesh->point(vh1) - OMPropertyVisualizer<MeshT>::mesh->point(vh0));
398  typename MeshT::Point v = OMPropertyVisualizer<MeshT>::mesh->property(prop, *he_it);
399 
400  if (vectorWidget->normalize->isChecked() && v.sqrnorm() > 1e-12)
401  v.normalize();
402 
403  if(vectorWidget->scale->isChecked())
404  v *= vectorWidget->scaleBox->value();
405 
406  lineNode->add_line( v1, (v1+v) );
407  lineNode->add_color(color);
408  }
409 }
410 
411 template <typename MeshT>
413 {
414  VectorWidget* vectorWidget = static_cast<VectorWidget*>(PropertyVisualizer::widget);
415 
416  lineNode->clear();
417 
418  typename MeshT::Color color = ACG::to_Vec4f(vectorWidget->lineColor->color());
419 
420  //TODO check if this also works if the property is Vec3f
422 
423  if ( !OMPropertyVisualizer<MeshT>::mesh->get_property_handle(prop, OMPropertyVisualizer<MeshT>::propertyInfo.propName() ) )
424  return;
425 
426  for (typename MeshT::VertexIter v_it = OMPropertyVisualizer<MeshT>::mesh->vertices_begin() ; v_it != OMPropertyVisualizer<MeshT>::mesh->vertices_end() ; ++v_it){
427 
428  typename MeshT::Point v1 = OMPropertyVisualizer<MeshT>::mesh->point( *v_it );
429  typename MeshT::Point v = OMPropertyVisualizer<MeshT>::mesh->property(prop, *v_it);
430 
431  if (vectorWidget->normalize->isChecked() && v.sqrnorm() > 1e-12)
432  v.normalize();
433 
434  if(vectorWidget->scale->isChecked())
435  v *= vectorWidget->scaleBox->value();
436 
437  lineNode->add_line( v1, (v1+v) );
438  lineNode->add_color(color);
439  }
440 }
441 
442 template <typename MeshT>
443 void OMPropertyVisualizerVector<MeshT>::setFacePropertyFromText(unsigned int index, QString text)
444 {
446  MeshT* mesh = OMPropertyVisualizer<MeshT>::mesh;
447 
448  if ( !mesh->get_property_handle(prop, PropertyVisualizer::propertyInfo.propName() ) )
449  emit this->log(LOGERR, QObject::tr("Error: No property with name ").append(PropertyVisualizer::propertyInfo.propName().c_str()));
450 
451 
452  typename MeshT::FaceHandle fh = mesh->face_handle(index);
453 
454  mesh->property(prop, fh) = this->strToVec3d(text);
455 }
456 
457 template <typename MeshT>
458 void OMPropertyVisualizerVector<MeshT>::setEdgePropertyFromText(unsigned int index, QString text)
459 {
461  MeshT* mesh = OMPropertyVisualizer<MeshT>::mesh;
462 
463  if ( !mesh->get_property_handle(prop, PropertyVisualizer::propertyInfo.propName() ) )
464  emit this->log(LOGERR, QObject::tr("Error: No property with name ").append(PropertyVisualizer::propertyInfo.propName().c_str()));
465 
466 
467  typename MeshT::EdgeHandle eh = mesh->edge_handle(index);
468 
469  mesh->property(prop, eh) = this->strToVec3d(text);
470 }
471 
472 template <typename MeshT>
473 void OMPropertyVisualizerVector<MeshT>::setHalfedgePropertyFromText(unsigned int index, QString text)
474 {
476  MeshT* mesh = OMPropertyVisualizer<MeshT>::mesh;
477 
478  if ( !mesh->get_property_handle(prop, PropertyVisualizer::propertyInfo.propName() ) )
479  emit this->log(LOGERR, QObject::tr("Error: No property with name ").append(PropertyVisualizer::propertyInfo.propName().c_str()));
480 
481 
482  typename MeshT::HalfedgeHandle heh = mesh->halfedge_handle(index);
483 
484  mesh->property(prop, heh) = this->strToVec3d(text);
485 }
486 
487 template <typename MeshT>
488 void OMPropertyVisualizerVector<MeshT>::setVertexPropertyFromText(unsigned int index, QString text)
489 {
491  MeshT* mesh = OMPropertyVisualizer<MeshT>::mesh;
492 
493  if ( !mesh->get_property_handle(prop, PropertyVisualizer::propertyInfo.propName() ) )
494  emit this->log(LOGERR, QObject::tr("Error: No property with name ").append(PropertyVisualizer::propertyInfo.propName().c_str()));
495 
496 
497  typename MeshT::VertexHandle vh = mesh->vertex_handle(index);
498 
499  mesh->property(prop, vh) = this->strToVec3d(text);
500 }
501 
502 
503 template<typename MeshT>
505 {
506  OMPropertyVisualizer<MeshT>::template removeProperty_stage1<typename MeshT::Point>();
507 }
508 
509 template<typename MeshT>
511 {
512  OMPropertyVisualizer<MeshT>::template duplicateProperty_stage1<typename MeshT::Point>();
513 }
514 
virtual QString getPropertyText(unsigned int index)
Returns the value of a property in text form.
Class for generating nice colors for doubles.
Definition: ColorCoder.hh:76
void setDrawMode(const ACG::SceneGraph::DrawModes::DrawMode &_mode, int _viewer)
Set the draw Mode of a Viewer. .
DrawMode SOLID_POINTS_COLORED
draw colored, but not lighted faces using interpolated vertex colors
Definition: DrawModes.cc:91
DrawMode EDGES_COLORED
draw edges with colors (without shading)
Definition: DrawModes.cc:83
ACG::SceneGraph::BaseNode * getRootNode()
Get the root node for data objects.
DrawMode HALFEDGES_COLORED
draw halfedges with colors (without shading)
Definition: DrawModes.cc:109
virtual void duplicateProperty()
Duplicates the property.
virtual void clear()
Clears the property.
virtual void clear()
Clears the property.
virtual void removeProperty()
Removes the property.
DrawMode SOLID_FLAT_SHADED
draw flat shaded faces (requires face normals)
Definition: DrawModes.cc:87
Add colors to mesh item (vertices/faces/edges)
Definition: Attributes.hh:88
Cellection of information about a property.
Definition: Utils.hh:115
DrawMode SOLID_FACES_COLORED
draw colored, but not lighted faces using face colors
Definition: DrawModes.cc:90