/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*--------------------------------------------------------------------------- *
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see . *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision$ *
* $LastChangedBy$ *
* $Date$ *
* *
\*===========================================================================*/
#define OM_PROPERTY_VISUALIZER_VECTOR_CC
#include "OMPropertyVisualizerVector.hh"
template
OMPropertyVisualizerVector::OMPropertyVisualizerVector(MeshT* _mesh, PropertyInfo _propertyInfo)
: OMPropertyVisualizer(_mesh, _propertyInfo)
{
if (PropertyVisualizer::widget) delete PropertyVisualizer::widget;
VectorWidget* w = new VectorWidget();
w->paramVector->setTitle(QString("3D Vector Parameters of ").append(PropertyVisualizer::propertyInfo.propName().c_str()));
PropertyVisualizer::widget = w;
lineNode = new ACG::SceneGraph::LineNode( ACG::SceneGraph::LineNode::LineSegmentsMode, dynamic_cast < ACG::SceneGraph::SeparatorNode* >( PluginFunctions::getRootNode() ) );
if (!_propertyInfo.isFaceProp())
{
w->vectors_edges_rb->hide();
}
}
template
void OMPropertyVisualizerVector::clear()
{
lineNode->clear();
OMPropertyVisualizer::clear();
}
template
QString OMPropertyVisualizerVector::getPropertyText(unsigned int index)
{
return OMPropertyVisualizer::template getPropertyText_(index);
}
namespace {
template
void visualizeVectorAsColorForEntity(MeshT *mesh, const ENTITY_IT e_begin, const ENTITY_IT e_end,
const PROPINFO_TYPE &propinfo) {
PROPTYPE prop;
if (!mesh->get_property_handle(prop, propinfo.propName()))
throw VizException("Getting PropHandle from mesh for selected property failed.");
for (ENTITY_IT e_it = e_begin; e_it != e_end; ++e_it) {
typename MeshT::Point v = mesh->property(prop, *e_it).normalized() * .5 + typename MeshT::Point(.5, .5, .5);
mesh->set_color(*e_it, typename MeshT::Color(v[0], v[1], v[2], 1.0));
}
}
}
template
void OMPropertyVisualizerVector::visualizeFaceProp(bool _setDrawMode)
{
VectorWidget* w = (VectorWidget*)PropertyVisualizer::widget;
if (w->vectors_edges_rb->isChecked()) visualizeFacePropOnEdges();
else if (w->vectors_colors_rb->isChecked())
{
if ( !OMPropertyVisualizer::mesh->has_face_colors() )
OMPropertyVisualizer::mesh->request_face_colors();
visualizeVectorAsColorForEntity >(
OMPropertyVisualizer::mesh,
OMPropertyVisualizer::mesh->faces_begin(),
OMPropertyVisualizer::mesh->faces_end(),
PropertyVisualizer::propertyInfo);
if (_setDrawMode)
PluginFunctions::setDrawMode(ACG::SceneGraph::DrawModes::SOLID_FACES_COLORED);
}
else visualizeFacePropAsStrokes();
}
template
void OMPropertyVisualizerVector::visualizeEdgeProp(bool _setDrawMode)
{
VectorWidget* w = (VectorWidget*)PropertyVisualizer::widget;
if (w->vectors_colors_rb->isChecked())
{
if ( !OMPropertyVisualizer::mesh->has_edge_colors() )
OMPropertyVisualizer::mesh->request_edge_colors();
MeshT* mesh = OMPropertyVisualizer::mesh;
if ( !mesh->has_edge_colors() )
mesh->request_edge_colors();
visualizeVectorAsColorForEntity >(mesh,mesh->edges_begin(),mesh->edges_end(), PropertyVisualizer::propertyInfo);
if (_setDrawMode)
PluginFunctions::setDrawMode(ACG::SceneGraph::DrawModes::EDGES_COLORED);
}
else visualizeEdgePropAsStrokes();
}
template
void OMPropertyVisualizerVector::visualizeHalfedgeProp(bool _setDrawMode)
{
VectorWidget* w = (VectorWidget*)PropertyVisualizer::widget;
if (w->vectors_colors_rb->isChecked())
{
if ( !OMPropertyVisualizer::mesh->has_halfedge_colors() )
OMPropertyVisualizer::mesh->request_halfedge_colors();
MeshT* mesh = OMPropertyVisualizer::mesh;
if ( ! mesh->has_halfedge_colors() )
mesh->request_halfedge_colors();
visualizeVectorAsColorForEntity >(mesh,mesh->halfedges_begin(),mesh->halfedges_end(),PropertyVisualizer::propertyInfo);
if (_setDrawMode)
PluginFunctions::setDrawMode(ACG::SceneGraph::DrawModes::HALFEDGES_COLORED);
}
else visualizeHalfedgePropAsStrokes();
}
template
void OMPropertyVisualizerVector::visualizeVertexProp(bool _setDrawMode)
{
VectorWidget* w = (VectorWidget*)PropertyVisualizer::widget;
if (w->vectors_colors_rb->isChecked())
{
if ( !OMPropertyVisualizer::mesh->has_vertex_colors() )
OMPropertyVisualizer::mesh->request_vertex_colors();
visualizeVectorAsColorForEntity >(
OMPropertyVisualizer::mesh,
OMPropertyVisualizer::mesh->vertices_begin(),
OMPropertyVisualizer::mesh->vertices_end(),
PropertyVisualizer::propertyInfo);
if (_setDrawMode)
PluginFunctions::setDrawMode(ACG::SceneGraph::DrawModes::SOLID_POINTS_COLORED);
}
else visualizeVertexPropAsStrokes();
}
template
void OMPropertyVisualizerVector::visualizeFacePropOnEdges() {
VectorWidget* w = (VectorWidget*)PropertyVisualizer::widget;
MeshT* _mesh = OMPropertyVisualizer::mesh;
const double thresh_1 = w->vectors_edges_alpha->value();
const double thresh_2 = std::min(thresh_1, w->vectors_edges_alpha->value());
OpenMesh::FPropHandleT prop;
if (!_mesh->get_property_handle(prop, PropertyVisualizer::propertyInfo.propName()))
throw VizException("Getting PropHandle from mesh for selected property failed.");
if (!_mesh->has_edge_colors())
_mesh->request_edge_colors();
const ACG::Vec4f cold(0, 0, 0, 1.0), hot(0, 1, 0, 1.0), degen(1, 1, 0, 1.0);
for (typename MeshT::EdgeIter e_it = _mesh->edges_begin(), e_end = _mesh->edges_end();
e_it != e_end; ++e_it) {
typename MeshT::Point p1 = _mesh->property(prop, _mesh->face_handle(_mesh->halfedge_handle(*e_it, 0)));
typename MeshT::Point p2 = _mesh->property(prop, _mesh->face_handle(_mesh->halfedge_handle(*e_it, 1)));
ACG::Vec4f color;
const char degenerate = ((p1.sqrnorm() < 1e-6) ? 1 : 0) | ((p2.sqrnorm() < 1e-6) ? 2 : 0);
if (degenerate == 3) {
color = cold;
} else if (degenerate == 0) {
p1.normalize(); p2.normalize();
const double alpha = std::min(1.0, std::abs(p1 | p2));
if (alpha < thresh_1)
color = hot;
else if (alpha > thresh_2)
color = cold;
else {
const double beta = (alpha - thresh_1) / (thresh_2 - thresh_1);
color = cold * beta + hot * (1.0 - beta);
}
} else {
color = degen;
}
_mesh->set_color(*e_it, color);
}
PluginFunctions::setDrawMode(ACG::SceneGraph::DrawModes::SOLID_FLAT_SHADED | ACG::SceneGraph::DrawModes::EDGES_COLORED);
}
template
void OMPropertyVisualizerVector::visualizeFacePropAsStrokes()
{
VectorWidget* vectorWidget = static_cast(PropertyVisualizer::widget);
lineNode->clear();
typename MeshT::Color color = OMPropertyVisualizer::convertColor(vectorWidget->lineColor->color());
OpenMesh::FPropHandleT< typename MeshT::Point > prop;
if ( !OMPropertyVisualizer::mesh->get_property_handle(prop, OMPropertyVisualizer::propertyInfo.propName() ) )
return;
for (typename MeshT::FaceIter f_it = OMPropertyVisualizer::mesh->faces_begin() ; f_it != OMPropertyVisualizer::mesh->faces_end() ; ++f_it){
typename MeshT::Point center(0.0, 0.0, 0.0);
int vCount = 0;
for (typename MeshT::FaceVertexIter fv_it(*(OMPropertyVisualizer::mesh),*f_it); fv_it.is_valid(); ++fv_it){
vCount++;
center += OMPropertyVisualizer::mesh->point(*fv_it);
}
center /= vCount;
typename MeshT::Point v = (OMPropertyVisualizer::mesh->property(prop, *f_it));
if (vectorWidget->normalize->isChecked() && v.sqrnorm() > 1e-12)
v.normalize();
if(vectorWidget->scale->isChecked())
v *= vectorWidget->scaleBox->value();
lineNode->add_line( center, (center+v) );
lineNode->add_color(color);
}
}
template
void OMPropertyVisualizerVector::visualizeEdgePropAsStrokes()
{
VectorWidget* vectorWidget = static_cast(PropertyVisualizer::widget);
lineNode->clear();
typename MeshT::Color color = OMPropertyVisualizer::convertColor(vectorWidget->lineColor->color());
//TODO check if this also works if the property is Vec3f
OpenMesh::EPropHandleT< typename MeshT::Point > prop;
if ( !OMPropertyVisualizer::mesh->get_property_handle(prop, OMPropertyVisualizer::propertyInfo.propName() ) )
return;
for (typename MeshT::EdgeIter e_it = OMPropertyVisualizer::mesh->edges_begin() ; e_it != OMPropertyVisualizer::mesh->edges_end() ; ++e_it){
typename MeshT::HalfedgeHandle hh = OMPropertyVisualizer::mesh->halfedge_handle( *e_it, 0 );
typename MeshT::VertexHandle vh0 = OMPropertyVisualizer::mesh->from_vertex_handle( hh );
typename MeshT::VertexHandle vh1 = OMPropertyVisualizer::mesh->to_vertex_handle( hh );
typename MeshT::Point v1 = OMPropertyVisualizer::mesh->point(vh0) + 0.5 * (OMPropertyVisualizer::mesh->point(vh1) - OMPropertyVisualizer::mesh->point(vh0));
typename MeshT::Point v = OMPropertyVisualizer::mesh->property(prop, *e_it);
if (vectorWidget->normalize->isChecked() && v.sqrnorm() > 1e-12)
v.normalize();
if(vectorWidget->scale->isChecked())
v *= vectorWidget->scaleBox->value();
lineNode->add_line( v1, (v1+v) );
lineNode->add_color(color);
}
}
template
void OMPropertyVisualizerVector::visualizeHalfedgePropAsStrokes()
{
VectorWidget* vectorWidget = static_cast(PropertyVisualizer::widget);
lineNode->clear();
typename MeshT::Color color = OMPropertyVisualizer::convertColor(vectorWidget->lineColor->color());
//TODO check if this also works if the property is Vec3f
OpenMesh::HPropHandleT< typename MeshT::Point > prop;
if ( !OMPropertyVisualizer::mesh->get_property_handle(prop, OMPropertyVisualizer::propertyInfo.propName() ) )
return;
for (typename MeshT::HalfedgeIter he_it = OMPropertyVisualizer::mesh->halfedges_begin() ; he_it != OMPropertyVisualizer::mesh->halfedges_end() ; ++he_it){
typename MeshT::VertexHandle vh0 = OMPropertyVisualizer::mesh->from_vertex_handle( *he_it );
typename MeshT::VertexHandle vh1 = OMPropertyVisualizer::mesh->to_vertex_handle( *he_it );
typename MeshT::Point v1 = OMPropertyVisualizer::mesh->point(vh0) + 0.5 * (OMPropertyVisualizer::mesh->point(vh1) - OMPropertyVisualizer::mesh->point(vh0));
typename MeshT::Point v = OMPropertyVisualizer::mesh->property(prop, *he_it);
if (vectorWidget->normalize->isChecked() && v.sqrnorm() > 1e-12)
v.normalize();
if(vectorWidget->scale->isChecked())
v *= vectorWidget->scaleBox->value();
lineNode->add_line( v1, (v1+v) );
lineNode->add_color(color);
}
}
template
void OMPropertyVisualizerVector::visualizeVertexPropAsStrokes()
{
VectorWidget* vectorWidget = static_cast(PropertyVisualizer::widget);
lineNode->clear();
typename MeshT::Color color = OMPropertyVisualizer::convertColor(vectorWidget->lineColor->color());
//TODO check if this also works if the property is Vec3f
OpenMesh::VPropHandleT< typename MeshT::Point > prop;
if ( !OMPropertyVisualizer::mesh->get_property_handle(prop, OMPropertyVisualizer::propertyInfo.propName() ) )
return;
for (typename MeshT::VertexIter v_it = OMPropertyVisualizer::mesh->vertices_begin() ; v_it != OMPropertyVisualizer::mesh->vertices_end() ; ++v_it){
typename MeshT::Point v1 = OMPropertyVisualizer::mesh->point( *v_it );
typename MeshT::Point v = OMPropertyVisualizer::mesh->property(prop, *v_it);
if (vectorWidget->normalize->isChecked() && v.sqrnorm() > 1e-12)
v.normalize();
if(vectorWidget->scale->isChecked())
v *= vectorWidget->scaleBox->value();
lineNode->add_line( v1, (v1+v) );
lineNode->add_color(color);
}
}
template
void OMPropertyVisualizerVector::setFacePropertyFromText(unsigned int index, QString text)
{
OpenMesh::FPropHandleT< typename MeshT::Point > prop;
MeshT* mesh = OMPropertyVisualizer::mesh;
if ( !mesh->get_property_handle(prop, PropertyVisualizer::propertyInfo.propName() ) )
emit this->log(LOGERR, QObject::tr("Error: No property with name ").append(PropertyVisualizer::propertyInfo.propName().c_str()));
typename MeshT::FaceHandle fh = mesh->face_handle(index);
mesh->property(prop, fh) = this->strToVec3d(text);
}
template
void OMPropertyVisualizerVector::setEdgePropertyFromText(unsigned int index, QString text)
{
OpenMesh::EPropHandleT< typename MeshT::Point > prop;
MeshT* mesh = OMPropertyVisualizer::mesh;
if ( !mesh->get_property_handle(prop, PropertyVisualizer::propertyInfo.propName() ) )
emit this->log(LOGERR, QObject::tr("Error: No property with name ").append(PropertyVisualizer::propertyInfo.propName().c_str()));
typename MeshT::EdgeHandle eh = mesh->edge_handle(index);
mesh->property(prop, eh) = this->strToVec3d(text);
}
template
void OMPropertyVisualizerVector::setHalfedgePropertyFromText(unsigned int index, QString text)
{
OpenMesh::HPropHandleT< typename MeshT::Point > prop;
MeshT* mesh = OMPropertyVisualizer::mesh;
if ( !mesh->get_property_handle(prop, PropertyVisualizer::propertyInfo.propName() ) )
emit this->log(LOGERR, QObject::tr("Error: No property with name ").append(PropertyVisualizer::propertyInfo.propName().c_str()));
typename MeshT::HalfedgeHandle heh = mesh->halfedge_handle(index);
mesh->property(prop, heh) = this->strToVec3d(text);
}
template
void OMPropertyVisualizerVector::setVertexPropertyFromText(unsigned int index, QString text)
{
OpenMesh::VPropHandleT< typename MeshT::Point > prop;
MeshT* mesh = OMPropertyVisualizer::mesh;
if ( !mesh->get_property_handle(prop, PropertyVisualizer::propertyInfo.propName() ) )
emit this->log(LOGERR, QObject::tr("Error: No property with name ").append(PropertyVisualizer::propertyInfo.propName().c_str()));
typename MeshT::VertexHandle vh = mesh->vertex_handle(index);
mesh->property(prop, vh) = this->strToVec3d(text);
}
template
void OMPropertyVisualizerVector::removeProperty()
{
OMPropertyVisualizer::template removeProperty_stage1();
}
template
void OMPropertyVisualizerVector::duplicateProperty()
{
OMPropertyVisualizer::template duplicateProperty_stage1();
}