Developer Documentation
OVMPropertyVisualizerT.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 #ifdef ENABLE_OPENVOLUMEMESH_SUPPORT
51 
52 #define OVM_PROPERTY_VISUALIZER_CC
53 
54 #ifdef ENABLE_OPENVOLUMEMESH_POLYHEDRAL_SUPPORT
56 #endif
57 #ifdef ENABLE_OPENVOLUMEMESH_HEXAHEDRAL_SUPPORT
59 #endif
60 #ifdef ENABLE_OPENVOLUMEMESH_TETRAHEDRAL_SUPPORT
62 #endif
63 
64 #include "OVMPropertyVisualizer.hh"
65 
66 template <typename MeshT>
67 template <typename InnerType>
68 QString OVMPropertyVisualizer<MeshT>::getPropertyText_(unsigned int index)
69 {
70  if (PropertyVisualizer::propertyInfo.isCellProp())
71  {
72  OpenVolumeMesh::CellPropertyT<InnerType> prop = OVMPropertyVisualizer<MeshT>::mesh->template request_cell_property<InnerType>(OVMPropertyVisualizer<MeshT>::propertyInfo.propName());
73  return PropertyVisualizer::toStr(prop[OpenVolumeMesh::CellHandle(index)]);
74  }
75  else if (PropertyVisualizer::propertyInfo.isFaceProp())
76  {
77  OpenVolumeMesh::FacePropertyT<InnerType> prop = OVMPropertyVisualizer<MeshT>::mesh->template request_face_property<InnerType>(OVMPropertyVisualizer<MeshT>::propertyInfo.propName());
78  return PropertyVisualizer::toStr(prop[OpenVolumeMesh::FaceHandle(index)]);
79  }
80  else if (PropertyVisualizer::propertyInfo.isHalffaceProp())
81  {
82  OpenVolumeMesh::HalfFacePropertyT<InnerType> prop = OVMPropertyVisualizer<MeshT>::mesh->template request_halfface_property<InnerType>(OVMPropertyVisualizer<MeshT>::propertyInfo.propName());
83  return PropertyVisualizer::toStr(prop[OpenVolumeMesh::HalfFaceHandle(index)]);
84  }
85  else if (PropertyVisualizer::propertyInfo.isEdgeProp())
86  {
87  OpenVolumeMesh::EdgePropertyT<InnerType> prop = OVMPropertyVisualizer<MeshT>::mesh->template request_edge_property<InnerType>(OVMPropertyVisualizer<MeshT>::propertyInfo.propName());
88  return PropertyVisualizer::toStr(prop[OpenVolumeMesh::EdgeHandle(index)]);
89  }
90  else if (PropertyVisualizer::propertyInfo.isHalfedgeProp())
91  {
92  OpenVolumeMesh::HalfEdgePropertyT<InnerType> prop = OVMPropertyVisualizer<MeshT>::mesh->template request_halfedge_property<InnerType>(OVMPropertyVisualizer<MeshT>::propertyInfo.propName());
93  return PropertyVisualizer::toStr(prop[OpenVolumeMesh::HalfEdgeHandle(index)]);
94  }
95  else //if (propertyInfo.isVertexProp())
96  {
97  OpenVolumeMesh::VertexPropertyT<InnerType> prop = OVMPropertyVisualizer<MeshT>::mesh->template request_vertex_property<InnerType>(OVMPropertyVisualizer<MeshT>::propertyInfo.propName());
98  return PropertyVisualizer::toStr(prop[OpenVolumeMesh::VertexHandle(index)]);
99  }
100 }
101 
102 template <typename MeshT>
103 void OVMPropertyVisualizer<MeshT>::setPropertyFromText(unsigned int index, QString text)
104 {
105  if (propertyInfo.isCellProp())
106  setCellPropertyFromText(index, text);
107  else if (propertyInfo.isFaceProp())
108  setFacePropertyFromText(index, text);
109  else if (propertyInfo.isHalffaceProp())
110  setHalffacePropertyFromText(index, text);
111  else if (propertyInfo.isEdgeProp())
112  setEdgePropertyFromText(index, text);
113  else if (propertyInfo.isHalfedgeProp())
114  setHalfedgePropertyFromText(index, text);
115  else //if (propertyInfo.isVertexProp())
116  setVertexPropertyFromText(index, text);
117 }
118 
119 template <typename MeshT>
120 int OVMPropertyVisualizer<MeshT>::getEntityCount()
121 {
122  if (propertyInfo.isCellProp())
123  return mesh->n_cells();
124  if (propertyInfo.isFaceProp())
125  return mesh->n_faces();
126  if (propertyInfo.isHalffaceProp())
127  return mesh->n_halffaces();
128  else if (propertyInfo.isEdgeProp())
129  return mesh->n_edges();
130  else if (propertyInfo.isHalfedgeProp())
131  return mesh->n_halfedges();
132  else //if (propertyInfo.isVertexProp())
133  return mesh->n_vertices();
134 }
135 
136 template <typename MeshT>
137 QString OVMPropertyVisualizer<MeshT>::getHeader()
138 {
139  //Header: headerVersion, numberOfEntities, typeOfEntites, typeOfProperty, propertyName
140 
141  QString header = QObject::tr("1"); //version
142  header.append(QObject::tr(", %1").arg(getEntityCount())); //number of entities
143  header.append(QObject::tr(", %1").arg(propertyInfo.entityType())); //type of entities
144  header.append(", ").append(propertyInfo.friendlyTypeName()); //type of property
145  header.append(", ").append(propertyInfo.propName().c_str()); // name of property
146  return header;
147 }
148 
149 template <typename MeshT>
150 unsigned int OVMPropertyVisualizer<MeshT>::getClosestPrimitiveId(unsigned int _face, ACG::Vec3d& _hitPoint)
151 {
152  if (propertyInfo.isHalffaceProp())
153  return getClosestHalffaceId(_face, _hitPoint);
154  else// if (propertyInfo.isHalfedgeProp())
155  return getClosestHalfedgeId(_face, _hitPoint);
156 }
157 
158 template <typename MeshT>
159 unsigned int OVMPropertyVisualizer<MeshT>::getClosestHalffaceId(unsigned int _face, ACG::Vec3d& _hitPoint)
160 {
162 
163  OpenVolumeMesh::HalfFaceHandle hfh = mesh->halfface_handle(OpenVolumeMesh::FaceHandle(_face), 0);
164  OpenVolumeMesh::HalfFaceVertexIter hfv_it = mesh->hfv_iter(hfh);
165  ACG::Vec3d p1 = mesh->vertex(*(hfv_it+0));
166  ACG::Vec3d p2 = mesh->vertex(*(hfv_it+1));
167  ACG::Vec3d p3 = mesh->vertex(*(hfv_it+2));
168 
169  ACG::Vec3d normal = (p2-p1)%(p3-p1);
170 
171  if ((direction | normal) < 0)
172  return hfh.idx();
173  else
174  return mesh->halfface_handle(OpenVolumeMesh::FaceHandle(_face), 1).idx();
175 }
176 
177 template <typename MeshT>
178 unsigned int OVMPropertyVisualizer<MeshT>::getClosestHalfedgeId(unsigned int _face, ACG::Vec3d& _hitPoint)
179 {
180  unsigned int halfface = getClosestHalffaceId(_face, _hitPoint);
181 
182  OpenVolumeMesh::OpenVolumeMeshFace face = mesh->halfface(halfface);
183 
184  const std::vector<OpenVolumeMesh::HalfEdgeHandle> & halfedges = face.halfedges();
185 
186  double min_distance = DBL_MAX;
187  OpenVolumeMesh::HalfEdgeHandle closestHalfEdgeHandle;
188 
189  for (std::vector<OpenVolumeMesh::HalfEdgeHandle>::const_iterator he_it = halfedges.begin(); he_it != halfedges.end(); ++he_it)
190  {
191  OpenVolumeMesh::OpenVolumeMeshEdge edge = OVMPropertyVisualizer<MeshT>::mesh->halfedge(*he_it);
192  ACG::Vec3d v1 = mesh->vertex(edge.from_vertex());
193  ACG::Vec3d v2 = mesh->vertex(edge.to_vertex());
194  ACG::Vec3d p = 0.5 * (v1+v2);
195  double distance = (p-_hitPoint).length();
196  if (distance < min_distance)
197  {
198  min_distance = distance;
199  closestHalfEdgeHandle = *he_it;
200  }
201 
202  }
203 
204  return closestHalfEdgeHandle.idx();
205 }
206 
207 
208 template <typename MeshT>
209 void OVMPropertyVisualizer<MeshT>::visualize(bool _setDrawMode, QWidget* _widget)
210 {
211  QWidget* tmp;
212  if (_widget)
213  {
214  tmp = widget;
215  widget = _widget;
216  }
217 
218  if (propertyInfo.isCellProp())
219  visualizeCellProp(_setDrawMode);
220  else if (propertyInfo.isFaceProp())
221  visualizeFaceProp(_setDrawMode);
222  else if (propertyInfo.isHalffaceProp())
223  visualizeHalffaceProp(_setDrawMode);
224  else if (propertyInfo.isEdgeProp())
225  visualizeEdgeProp(_setDrawMode);
226  else if (propertyInfo.isHalfedgeProp())
227  visualizeHalfedgeProp(_setDrawMode);
228  else if (propertyInfo.isVertexProp())
229  visualizeVertexProp(_setDrawMode);
230 
231  if (_widget)
232  {
233  widget = tmp;
234  }
235 }
236 
237 template <typename MeshT>
238 OpenMesh::Vec4f OVMPropertyVisualizer<MeshT>::convertColor(const QColor _color){
239 
240  OpenMesh::Vec4f color;
241 
242  color[0] = _color.redF();
243  color[1] = _color.greenF();
244  color[2] = _color.blueF();
245  color[3] = _color.alphaF();
246 
247  return color;
248 }
249 
250 template <typename MeshT>
251 void OVMPropertyVisualizer<MeshT>::visualizeFaceProp(bool /*_setDrawMode*/)
252 {
253  emit log(LOGERR, "Visualizing FaceProp not implemented");
254 }
255 
256 template <typename MeshT>
257 void OVMPropertyVisualizer<MeshT>::visualizeEdgeProp(bool /*_setDrawMode*/)
258 {
259  emit log(LOGERR, "Visualizing EdgeProp not implemented");
260 }
261 
262 template <typename MeshT>
263 void OVMPropertyVisualizer<MeshT>::visualizeHalfedgeProp(bool /*_setDrawMode*/)
264 {
265  emit log(LOGERR, "Visualizing HalfedgeProp not implemented");
266 }
267 
268 template <typename MeshT>
269 void OVMPropertyVisualizer<MeshT>::visualizeVertexProp(bool /*_setDrawMode*/)
270 {
271  emit log(LOGERR, "Visualizing VertexProp not implemented");
272 }
273 
274 template <typename MeshT>
275 void OVMPropertyVisualizer<MeshT>::visualizeCellProp(bool /*_setDrawMode*/)
276 {
277  emit log(LOGERR, "Visualizing CellProp not implemented");
278 }
279 
280 template <typename MeshT>
281 void OVMPropertyVisualizer<MeshT>::visualizeHalffaceProp(bool /*_setDrawMode*/)
282 {
283  emit log(LOGERR, "Visualizing HalffaceProp not implemented");
284 }
285 
286 template<typename MeshT>
287 template<typename PropType>
288 inline void OVMPropertyVisualizer<MeshT>::duplicateProperty_stage1() {
289  std::string newPropertyName;
290  for (int i = 1;; ++i) {
291  std::ostringstream oss;
292  oss << propertyInfo.propName() << " Copy " << i;
293  newPropertyName = oss.str();
294 
295  if (propertyInfo.isCellProp())
296  {
297  if(!mesh->template cell_property_exists<PropType>(newPropertyName)) break;
298  }
299  else if (propertyInfo.isFaceProp())
300  {
301  if(!mesh->template face_property_exists<PropType>(newPropertyName)) break;
302  }
303  else if (propertyInfo.isHalffaceProp())
304  {
305  if(!mesh->template halfface_property_exists<PropType>(newPropertyName)) break;
306  }
307  else if (propertyInfo.isEdgeProp())
308  {
309  if(!mesh->template edge_property_exists<PropType>(newPropertyName)) break;
310  }
311  else if (propertyInfo.isHalfedgeProp())
312  {
313  if(!mesh->template halfedge_property_exists<PropType>(newPropertyName)) break;
314  }
315  else if (propertyInfo.isVertexProp())
316  {
317  if(!mesh->template vertex_property_exists<PropType>(newPropertyName)) break;
318  }
319  }
320 
321  if (propertyInfo.isCellProp())
322  {
323  OpenVolumeMesh::CellPropertyT<PropType> prop = mesh->template request_cell_property<PropType>(OVMPropertyVisualizer<MeshT>::propertyInfo.propName());
324  OpenVolumeMesh::CellPropertyT<PropType> newProp = mesh->template request_cell_property< PropType >(newPropertyName);
325  mesh->set_persistent(newProp, true);
326  std::for_each(mesh->cells_begin(), mesh->cells_end(), CopyProperty<OpenVolumeMesh::CellPropertyT<PropType> >(newProp, prop, mesh));
327  }
328  else if (propertyInfo.isFaceProp())
329  {
330  OpenVolumeMesh::FacePropertyT<PropType> prop = mesh->template request_face_property<PropType>(OVMPropertyVisualizer<MeshT>::propertyInfo.propName());
331  OpenVolumeMesh::FacePropertyT<PropType> newProp = mesh->template request_face_property< PropType >(newPropertyName);
332  mesh->set_persistent(newProp, true);
333  std::for_each(mesh->faces_begin(), mesh->faces_end(), CopyProperty<OpenVolumeMesh::FacePropertyT<PropType> >(newProp, prop, mesh));
334  }
335  else if (propertyInfo.isHalffaceProp())
336  {
337  OpenVolumeMesh::HalfFacePropertyT<PropType> prop = mesh->template request_halfface_property<PropType>(OVMPropertyVisualizer<MeshT>::propertyInfo.propName());
338  OpenVolumeMesh::HalfFacePropertyT<PropType> newProp = mesh->template request_halfface_property< PropType >(newPropertyName);
339  mesh->set_persistent(newProp, true);
340  std::for_each(mesh->halffaces_begin(), mesh->halffaces_end(), CopyProperty<OpenVolumeMesh::HalfFacePropertyT<PropType> >(newProp, prop, mesh));
341  }
342  else if (propertyInfo.isEdgeProp())
343  {
344  OpenVolumeMesh::EdgePropertyT<PropType> prop = mesh->template request_edge_property<PropType>(OVMPropertyVisualizer<MeshT>::propertyInfo.propName());
345  OpenVolumeMesh::EdgePropertyT<PropType> newProp = mesh->template request_edge_property< PropType >(newPropertyName);
346  mesh->set_persistent(newProp, true);
347  std::for_each(mesh->edges_begin(), mesh->edges_end(), CopyProperty<OpenVolumeMesh::EdgePropertyT<PropType> >(newProp, prop, mesh));
348  }
349  else if (propertyInfo.isHalfedgeProp())
350  {
351  OpenVolumeMesh::HalfEdgePropertyT<PropType> prop = mesh->template request_halfedge_property<PropType>(OVMPropertyVisualizer<MeshT>::propertyInfo.propName());
352  OpenVolumeMesh::HalfEdgePropertyT<PropType> newProp = mesh->template request_halfedge_property< PropType >(newPropertyName);
353  mesh->set_persistent(newProp, true);
354  std::for_each(mesh->halfedges_begin(), mesh->halfedges_end(), CopyProperty<OpenVolumeMesh::HalfEdgePropertyT<PropType> >(newProp, prop, mesh));
355  }
356  else if (propertyInfo.isVertexProp())
357  {
358  OpenVolumeMesh::VertexPropertyT<PropType> prop = mesh->template request_vertex_property<PropType>(OVMPropertyVisualizer<MeshT>::propertyInfo.propName());
359  OpenVolumeMesh::VertexPropertyT<PropType> newProp = mesh->template request_vertex_property< PropType >(newPropertyName);
360  mesh->set_persistent(newProp, true);
361  std::for_each(mesh->vertices_begin(), mesh->vertices_end(), CopyProperty<OpenVolumeMesh::VertexPropertyT<PropType> >(newProp, prop, mesh));
362  }
363 }
364 
365 template <typename MeshT>
366 void OVMPropertyVisualizer<MeshT>::clear()
367 {
368  VolumeMeshObject<MeshT>* object;
369  PluginFunctions::getObject(OVMPropertyVisualizer<MeshT>::mObjectID, object);
370 
371  if (propertyInfo.isCellProp())
372  object->colors().clear_cell_colors();
373  else if (propertyInfo.isFaceProp())
374  object->colors().clear_face_colors();
375  else if (propertyInfo.isHalffaceProp())
376  object->colors().clear_halfface_colors();
377  else if (propertyInfo.isEdgeProp())
378  object->colors().clear_edge_colors();
379  else if (propertyInfo.isHalfedgeProp())
380  object->colors().clear_halfedge_colors();
381  else if (propertyInfo.isVertexProp())
382  object->colors().clear_vertex_colors();
383 
384  object->setObjectDrawMode(drawModes.cellsFlatShaded);
385 }
386 
387 template <typename MeshT>
388 void OVMPropertyVisualizer<MeshT>::setCellPropertyFromText(unsigned int /*index*/, QString /*text*/)
389 {
390  emit log(LOGERR, "Setting CellProp not implemented for this property type");
391 }
392 
393 template <typename MeshT>
394 void OVMPropertyVisualizer<MeshT>::setFacePropertyFromText(unsigned int /*index*/, QString /*text*/)
395 {
396  emit log(LOGERR, "Setting FaceProp not implemented for this property type");
397 }
398 
399 template <typename MeshT>
400 void OVMPropertyVisualizer<MeshT>::setHalffacePropertyFromText(unsigned int /*index*/, QString /*text*/)
401 {
402  emit log(LOGERR, "Setting HalffaceProp not implemented for this property type");
403 }
404 
405 template <typename MeshT>
406 void OVMPropertyVisualizer<MeshT>::setEdgePropertyFromText(unsigned int /*index*/, QString /*text*/)
407 {
408  emit log(LOGERR, "Setting EdgeProp not implemented for this property type");
409 }
410 
411 template <typename MeshT>
412 void OVMPropertyVisualizer<MeshT>::setHalfedgePropertyFromText(unsigned int /*index*/, QString /*text*/)
413 {
414  emit log(LOGERR, "Setting HalfedgeProp not implemented for this property type");
415 }
416 
417 template <typename MeshT>
418 void OVMPropertyVisualizer<MeshT>::setVertexPropertyFromText(unsigned int /*index*/, QString /*text*/)
419 {
420  emit log(LOGERR, "Setting VertexProp not implemented for this property type");
421 }
422 
423 #endif /* ENABLE_OPENVOLUMEMESH_SUPPORT */
const ColorAttrib & colors() const
return a pointer to the mesh
bool getObject(int _identifier, BSplineCurveObject *&_object)
Property classes for the different entity types.
void viewingDirection(const ACG::Vec3d &_dir, const ACG::Vec3d &_up, int _viewer)
Set the viewing direction.