MeshObjectInfoPlugin.cc 28.7 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2 3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
*                           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 <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
33 34 35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
*                                                                            *
Jan Möbius's avatar
Jan Möbius committed
37 38 39
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
Jan Möbius's avatar
Jan Möbius committed
40
*                                                                            *
41 42
\*===========================================================================*/

Jan Möbius's avatar
 
Jan Möbius committed
43 44
//=============================================================================
//
45
//  CLASS InfoMeshObjectPlugin - IMPLEMENTATION
Jan Möbius's avatar
 
Jan Möbius committed
46 47 48 49 50 51
//
//=============================================================================


//== INCLUDES =================================================================

Matthias Möller's avatar
Matthias Möller committed
52 53 54 55 56 57
#if QT_VERSION >= 0x050000 
  #include <QtWidgets>
#else
  #include <QtGui>
#endif

Jan Möbius's avatar
 
Jan Möbius committed
58

59
#include "MeshObjectInfoPlugin.hh"
Jan Möbius's avatar
 
Jan Möbius committed
60 61 62 63

#include <iostream>
#include <ACG/GL/GLState.hh>
#include <QStringList>
Jan Möbius's avatar
Jan Möbius committed
64
#include <QLocale>
Jan Möbius's avatar
 
Jan Möbius committed
65 66 67 68 69 70 71 72

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <MeshTools/MeshInfoT.hh>
#include <OpenFlipper/common/BaseObject.hh>
#include <ACG/Geometry/Algorithms.hh>

#include <Math_Tools/Math_Tools.hh>

73
#include <cfloat>
Jan Möbius's avatar
 
Jan Möbius committed
74 75 76

//== IMPLEMENTATION ==========================================================

77 78 79 80 81 82 83

InfoMeshObjectPlugin::InfoMeshObjectPlugin() :
        info_(0),
        infoBar_(0)
{
}

Jan Möbius's avatar
Jan Möbius committed
84 85
InfoMeshObjectPlugin::~InfoMeshObjectPlugin() {

86
  //Info bar and dialog will be deleted by core widget
Jan Möbius's avatar
Jan Möbius committed
87 88
}

89

90
void InfoMeshObjectPlugin::initializePlugin() {
91

92 93
}

Jan Möbius's avatar
 
Jan Möbius committed
94
/// initialize the plugin
95
void InfoMeshObjectPlugin::pluginsInitialized() {
Jan Möbius's avatar
 
Jan Möbius committed
96 97 98

  //set the slot descriptions
  setDescriptions();
Jan Möbius's avatar
Jan Möbius committed
99

100
  if ( OpenFlipper::Options::gui()) {
101 102 103 104 105 106 107 108 109 110

    // Create info bar
    infoBar_ = new InfoBar();

    // Create info dialog
    info_ = new InfoDialog();

    // Set default pick mode in dialog box
    info_->pickMode->setCurrentIndex(0); // PICK_FACES

111 112 113
    emit addWidgetToStatusbar(infoBar_);
    infoBar_->hideCounts();
  }
Dirk Wilden's avatar
Dirk Wilden committed
114
  
Jan Möbius's avatar
 
Jan Möbius committed
115 116 117 118
}

//-----------------------------------------------------------------------------

119
DataType InfoMeshObjectPlugin::supportedDataTypes() {
120 121 122 123 124
    return DataType(DATA_POLY_MESH | DATA_TRIANGLE_MESH);
}

//-----------------------------------------------------------------------------

Jan Möbius's avatar
 
Jan Möbius committed
125
template< class MeshT >
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
void InfoMeshObjectPlugin::printMeshInfo( MeshT* _mesh , int _id, unsigned int _index, ACG::Vec3d& _hitPoint ) {

  bool face   = false;
  bool edge   = false;
  bool vertex = false;

  int closestVertexIndex = -1;
  int closestEdgeIndex   = -1;

  switch (info_->pickMode->currentIndex() ) {
    case 0 : //Face
      closestVertexIndex = getClosestVertexInFace(_mesh, _index, _hitPoint);
      closestEdgeIndex   = getClosestEdgeInFace  (_mesh, _index, _hitPoint);
      face = true;
      break;
    case 1 : //Edge
      closestVertexIndex = getClosestVertexFromEdge(_mesh, _index, _hitPoint);
      closestEdgeIndex   = _index;
      edge = true;
      break;
    case 2 : //Vertex
      closestVertexIndex = _index;
      vertex = true;
      break;
    default:
      emit log(LOGERR,"Error: unknown picking mode in printMeshInfo");
      return;
  }
Jan Möbius's avatar
Jan Möbius committed
154 155 156

  QLocale locale;

Dirk Wilden's avatar
Dirk Wilden committed
157 158 159 160 161
  QString name;

  // name
  BaseObject* obj = 0;
  if ( PluginFunctions::getObject(_id, obj) )
Dirk Wilden's avatar
Dirk Wilden committed
162
    info_->generalBox->setTitle( tr("General object information for %1").arg( obj->name() ) );
Dirk Wilden's avatar
Dirk Wilden committed
163

Jan Möbius's avatar
 
Jan Möbius committed
164
  // ID
Dirk Wilden's avatar
Dirk Wilden committed
165
  info_->id->setText( locale.toString(_id) );
Jan Möbius's avatar
 
Jan Möbius committed
166
  // Vertices
167
  info_->vertices->setText( locale.toString( qulonglong(_mesh->n_vertices() ) ) );
Jan Möbius's avatar
 
Jan Möbius committed
168
  // Faces
169
  info_->faces->setText( locale.toString( qulonglong( _mesh->n_faces() ) ) );
Jan Möbius's avatar
 
Jan Möbius committed
170
  // Edges
171
  info_->edges->setText( locale.toString( qulonglong( _mesh->n_edges() ) ) );
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

  if ( face ) {

    // Picked Face
    info_->closestFaceLabel->setText( tr("Picked Face:") );
    info_->closestFaceLabel->show();
    info_->faceHandle->setText( locale.toString( _index ) );
    info_->faceHandle->show();

    // Closest Vertex
    info_->closestVertexLabel->setText( tr("Closest Vertex:") );
    info_->vertexHandle->setText( locale.toString( closestVertexIndex ) );

    // Closest Edge
    info_->closestEdgeLabel->setText( tr("Closest Edge:") );
    info_->edgeHandle->setText( locale.toString( closestEdgeIndex ) );
    info_->closestEdgeLabel->show();
    info_->edgeHandle->show();

191 192 193 194 195 196 197 198
    // Closest Edge Length
    info_->edgeLengthLabel->setText( tr("Closest Edge Length:") );
    info_->edgeLengthLabel->show();
    const typename MeshT::Point from = _mesh->point(_mesh->from_vertex_handle( _mesh->halfedge_handle( _mesh->edge_handle(closestEdgeIndex),0 ) ));
    const typename MeshT::Point to   = _mesh->point(_mesh->to_vertex_handle( _mesh->halfedge_handle( _mesh->edge_handle(closestEdgeIndex),0 ) ));
    info_->edgeLength->setText( locale.toString( (to - from).norm() ) );
    info_->edgeLength->show();

199 200 201 202 203 204
    //adjacent vertex handles
    typename MeshT::FaceHandle fh = _mesh->face_handle(_index);

    typename MeshT::FaceVertexIter fv_it = _mesh->fv_iter(fh);
    QString adjacentVertices;

Jan Möbius's avatar
OM3  
Jan Möbius committed
205 206
    if ( fv_it.is_valid() ){
      adjacentVertices = QString::number( fv_it->idx() );
207 208 209
      ++fv_it;
    }

Jan Möbius's avatar
OM3  
Jan Möbius committed
210 211
    while( fv_it.is_valid() ){
      adjacentVertices += "; " + QString::number( fv_it->idx() );
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
      ++fv_it;
    }

    info_->adjVertexHandles->setText( adjacentVertices );
    info_->adjVertexHandles->show();
    info_->adjacentVertexLabel->show();

    //normal
    info_->normalLabel->setText(tr("Normal of picked face:"));
    info_->normalX->setText( QString::number( _mesh->normal(fh)[0],'f' ) );
    info_->normalY->setText( QString::number( _mesh->normal(fh)[1],'f' ) );
    info_->normalZ->setText( QString::number( _mesh->normal(fh)[2],'f' ) );
    info_->normalLabel->show();
    info_->normalLeft->show();
    info_->normalX->show();
    info_->normalY->show();
    info_->normalZ->show();
    info_->normalRight->show();

    // closest vertex coordinates
    info_->closestVertexPosLabel->setText(tr("Closest Vertex on the mesh:"));

  } else if (edge) {

    // Adjacent Faces
    info_->closestFaceLabel->setText( tr("Adjacent Faces:") );
    info_->closestFaceLabel->show();
    typename MeshT::HalfedgeHandle he1 = _mesh->halfedge_handle(_mesh->edge_handle(_index),0);
    typename MeshT::HalfedgeHandle he2 = _mesh->halfedge_handle(_mesh->edge_handle(_index),1);

    int fh1 = _mesh->face_handle(he1).idx();
    int fh2 = _mesh->face_handle(he2).idx();

    info_->faceHandle->setText( locale.toString( fh1 ) + ";" + locale.toString( fh2 ) );
    info_->faceHandle->show();

    // Adjacent vertices
    info_->adjVertexHandles->setText(QString::number( _mesh->from_vertex_handle(he1).idx() ) + ";" + QString::number( _mesh->to_vertex_handle(he1).idx() ));
    info_->adjVertexHandles->show();
    info_->adjacentVertexLabel->show();

    // Closest Vertex
    info_->closestVertexLabel->setText( tr("Closest Vertex:") );
    info_->vertexHandle->setText( locale.toString( closestVertexIndex ) );

    // Picked Edge
    info_->closestEdgeLabel->setText( tr("Picked Edge:") );
    info_->edgeHandle->setText( locale.toString( closestEdgeIndex ) );
    info_->closestEdgeLabel->show();
    info_->edgeHandle->show();

263 264 265 266 267 268 269 270
    // Edge Length
    info_->edgeLengthLabel->setText( tr("Edge Length:") );
    info_->edgeLengthLabel->show();
    const typename MeshT::Point from = _mesh->point(_mesh->from_vertex_handle( _mesh->halfedge_handle( _mesh->edge_handle(closestEdgeIndex),0 ) ));
    const typename MeshT::Point to   = _mesh->point(_mesh->to_vertex_handle( _mesh->halfedge_handle( _mesh->edge_handle(closestEdgeIndex),0 ) ));
    info_->edgeLength->setText( locale.toString( (to - from).norm() ) );
    info_->edgeLength->show();

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    // Normal
    info_->normalLabel->hide();
    info_->normalLeft->hide();
    info_->normalX->hide();
    info_->normalY->hide();
    info_->normalZ->hide();
    info_->normalRight->hide();

    // closest vertex coordinates
    info_->closestVertexPosLabel->setText(tr("Closest Vertex on the mesh:"));

  } else if (vertex) {

    // Faces
    info_->closestFaceLabel->hide();
    info_->faceHandle->hide();

    // Adjacent vertices
    info_->adjVertexHandles->hide();
    info_->adjacentVertexLabel->hide();

    // Closest Vertex
    info_->closestVertexLabel->setText( tr("Picked Vertex:") );
    info_->vertexHandle->setText( locale.toString( closestVertexIndex ) );

    // Closest Edge
    info_->closestEdgeLabel->hide();
    info_->edgeHandle->hide();

300 301 302 303
    // Edge Length
    info_->edgeLengthLabel->hide();
    info_->edgeLength->hide();

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    // Normal
    typename MeshT::VertexHandle vh = _mesh->vertex_handle(_index);
    info_->normalLabel->setText(tr("Normal of picked vertex:"));
    info_->normalX->setText( QString::number( _mesh->normal(vh)[0],'f' ) );
    info_->normalY->setText( QString::number( _mesh->normal(vh)[1],'f' ) );
    info_->normalZ->setText( QString::number( _mesh->normal(vh)[2],'f' ) );
    info_->normalLabel->show();
    info_->normalLeft->show();
    info_->normalX->show();
    info_->normalY->show();
    info_->normalZ->show();
    info_->normalRight->show();

    // closest vertex coordinates
    info_->closestVertexPosLabel->setText(tr("Picked Vertex on the mesh:"));

    // Adjacent Edges
    info_->closestFaceLabel->setText( tr("Adjacent Edges:") );
    info_->closestFaceLabel->show();

    //adjacent vertex handles

    typename MeshT::VertexEdgeIter ve_it = _mesh->ve_iter(vh);
    QString adjacentEdges;

Jan Möbius's avatar
OM3  
Jan Möbius committed
329 330
    if ( ve_it.is_valid() ){
      adjacentEdges = QString::number( ve_it->idx() );
331 332 333
      ++ve_it;
    }

Jan Möbius's avatar
OM3  
Jan Möbius committed
334 335
    while( ve_it.is_valid() ){
      adjacentEdges += "; " + QString::number( ve_it->idx() );
336 337 338 339 340 341 342 343 344 345 346 347 348
      ++ve_it;
    }

    info_->faceHandle->setText( adjacentEdges );
    info_->faceHandle->show();
  }

  // closest vertex coordinates
  info_->vertexX->setText( QString::number( _mesh->point( _mesh->vertex_handle(closestVertexIndex) )[0],'f' ) );
  info_->vertexY->setText( QString::number( _mesh->point( _mesh->vertex_handle(closestVertexIndex) )[1],'f' ) );
  info_->vertexZ->setText( QString::number( _mesh->point( _mesh->vertex_handle(closestVertexIndex) )[2],'f' ) );


Jan Möbius's avatar
 
Jan Möbius committed
349
  // Components
350 351
  int compo_count = MeshInfo::componentCount(_mesh);
  info_->components->setText( locale.toString(compo_count));
Jan Möbius's avatar
 
Jan Möbius committed
352
  // Boundaries
353 354
  int boundary_count = MeshInfo::boundaryCount(_mesh);
  info_->boundaries->setText( locale.toString(boundary_count) );
Jan Möbius's avatar
 
Jan Möbius committed
355
  // Genus
356 357 358 359 360
  int chi = _mesh->n_vertices();
  chi -= _mesh->n_edges();
  chi += _mesh->n_faces(); // chi = Euler characteristic
  // chi + n_holes = 2(n_components - genus) => genus = n_components - (chi + n_holes)/2;
  float genus = compo_count - 0.5*(chi + boundary_count);
361 362 363 364 365 366
  if(compo_count == 1 && boundary_count == 0)
    info_->genus->setText( QString::number(genus) );
  else if(compo_count != 1)
    info_->genus->setText( "(multiple components)" );
  else
    info_->genus->setText( "(not manifold)" );
Jan Möbius's avatar
 
Jan Möbius committed
367 368 369 370

  // Coordinates
  typename MeshT::VertexIter v_it;
  typename MeshT::VertexIter v_end = _mesh->vertices_end();
Jan Möbius's avatar
Jan Möbius committed
371

Jan Möbius's avatar
 
Jan Möbius committed
372 373
  float maxX = FLT_MIN;
  float minX = FLT_MAX;
Jan Möbius's avatar
Jan Möbius committed
374
  //float sumX = 0.0;
Jan Möbius's avatar
 
Jan Möbius committed
375 376
  float maxY = FLT_MIN;
  float minY = FLT_MAX;
Jan Möbius's avatar
Jan Möbius committed
377
  //float sumY = 0.0;
Jan Möbius's avatar
 
Jan Möbius committed
378 379
  float maxZ = FLT_MIN;
  float minZ = FLT_MAX;
Jan Möbius's avatar
Jan Möbius committed
380
  //float sumZ = 0.0;
Jan Möbius's avatar
 
Jan Möbius committed
381 382 383 384 385 386
  int minV = 999;
  int maxV = 0;
  int sumV = 0;
  float maxE = FLT_MIN;
  float minE = FLT_MAX;
  float sumE = 0.0;
Jan Möbius's avatar
Jan Möbius committed
387

Jan Möbius's avatar
 
Jan Möbius committed
388 389
  //iterate over all vertices
  for (v_it = _mesh->vertices_begin(); v_it != v_end; ++v_it){
Jan Möbius's avatar
OM3  
Jan Möbius committed
390
    typename MeshT::Point p = _mesh->point( *v_it );
Jan Möbius's avatar
 
Jan Möbius committed
391 392
    if (p[0] < minX) minX = p[0];
    if (p[0] > maxX) maxX = p[0];
Jan Möbius's avatar
Jan Möbius committed
393
    //sumX += p[0];
Jan Möbius's avatar
 
Jan Möbius committed
394 395
    if (p[1] < minY) minY = p[1];
    if (p[1] > maxY) maxY = p[1];
Jan Möbius's avatar
Jan Möbius committed
396
    //sumY += p[1];
Jan Möbius's avatar
 
Jan Möbius committed
397 398
    if (p[2] < minZ) minZ = p[2];
    if (p[2] > maxZ) maxZ = p[2];
Jan Möbius's avatar
Jan Möbius committed
399
    //sumZ += p[2];
Jan Möbius's avatar
 
Jan Möbius committed
400

Jan Möbius's avatar
Jan Möbius committed
401 402


Jan Möbius's avatar
 
Jan Möbius committed
403 404 405
    //check valence + edge length
    int valence = 0;
    typename MeshT::VertexVertexIter vv_it;
Jan Möbius's avatar
Jan Möbius committed
406

Jan Möbius's avatar
OM3  
Jan Möbius committed
407
    for (vv_it=_mesh->vv_iter( *v_it ); vv_it.is_valid(); ++vv_it){
Jan Möbius's avatar
 
Jan Möbius committed
408
      valence++;
Jan Möbius's avatar
Jan Möbius committed
409

Jan Möbius's avatar
OM3  
Jan Möbius committed
410
      typename MeshT::Point p2 = _mesh->point( *vv_it );
Jan Möbius's avatar
 
Jan Möbius committed
411 412 413 414 415 416 417 418 419 420 421
      typename MeshT::Scalar len = (p2 - p).norm();

      if (len < minE) minE = len;
      if (len > maxE) maxE = len;
      sumE += len;
    }

    if (valence < minV) minV = valence;
    if (valence > maxV) maxV = valence;
    sumV += valence;
  }
Jan Möbius's avatar
Jan Möbius committed
422

Dirk Wilden's avatar
Dirk Wilden committed
423 424 425 426 427 428 429 430 431 432
  
  //valence
  info_->valenceMin->setText( QString::number(minV) );
  info_->valenceMean->setText( QString::number( sumV / (float)_mesh->n_vertices(),'f' ) );
  info_->valenceMax->setText( QString::number(maxV) );

  //edge length
  info_->edgeMin->setText( QString::number(minE,'f') );
  info_->edgeMean->setText( QString::number( sumE / (_mesh->n_edges()*2),'f' )  );
  info_->edgeMax->setText( QString::number(maxE,'f') );
Jan Möbius's avatar
 
Jan Möbius committed
433 434 435 436 437 438 439 440 441 442 443

  //get aspect ratio

  typename MeshT::FaceIter f_it;
  typename MeshT::FaceIter f_end = _mesh->faces_end();

  float maxA = FLT_MIN;
  float minA = FLT_MAX;
  float sumA = 0.0;
  float maxI = FLT_MIN;
  float minI = FLT_MAX;
Jan Möbius's avatar
Jan Möbius committed
444
  //float sumI = 0.0;
Jan Möbius's avatar
 
Jan Möbius committed
445 446 447
  float maxD = FLT_MIN;
  float minD = FLT_MAX;
  float sumD = 0.0;
448
  int numD = 0;
Jan Möbius's avatar
 
Jan Möbius committed
449 450 451

  //iterate over all faces
  for (f_it = _mesh->faces_begin(); f_it != f_end; ++f_it){
Jan Möbius's avatar
OM3  
Jan Möbius committed
452
    typename MeshT::ConstFaceVertexIter cfv_it = _mesh->cfv_iter(*f_it);
Jan Möbius's avatar
 
Jan Möbius committed
453

Jan Möbius's avatar
OM3  
Jan Möbius committed
454
    typename MeshT::Point v0 = _mesh->point( *cfv_it );
Jan Möbius's avatar
 
Jan Möbius committed
455
    ++cfv_it;
Jan Möbius's avatar
OM3  
Jan Möbius committed
456
    typename MeshT::Point v1 = _mesh->point( *cfv_it );
Jan Möbius's avatar
 
Jan Möbius committed
457
    ++cfv_it;
Jan Möbius's avatar
OM3  
Jan Möbius committed
458
    typename MeshT::Point v2 = _mesh->point( *cfv_it );
Jan Möbius's avatar
 
Jan Möbius committed
459 460 461 462 463 464 465 466 467 468 469 470 471

    float aspect = ACG::Geometry::aspectRatio(v0, v1, v2);

    if (aspect < minA) minA = aspect;
    if (aspect > maxA) maxA = aspect;
    sumA += aspect;

    //inner triangle angles

    double angle = OpenMesh::rad_to_deg(acos(OpenMesh::sane_aarg( MathTools::sane_normalized(v2 - v0) | MathTools::sane_normalized(v1 - v0) )));

    if (angle < minI) minI = angle;
    if (angle > maxI) maxI = angle;
Jan Möbius's avatar
Jan Möbius committed
472
    //sumI += angle;
Jan Möbius's avatar
 
Jan Möbius committed
473 474 475 476 477

    angle = OpenMesh::rad_to_deg(acos(OpenMesh::sane_aarg( MathTools::sane_normalized(v2 - v1) | MathTools::sane_normalized(v0 - v1) )));

    if (angle < minI) minI = angle;
    if (angle > maxI) maxI = angle;
Jan Möbius's avatar
Jan Möbius committed
478
    //sumI += angle;
Jan Möbius's avatar
 
Jan Möbius committed
479 480 481 482 483

    angle = OpenMesh::rad_to_deg(acos(OpenMesh::sane_aarg( MathTools::sane_normalized(v1 - v2) | MathTools::sane_normalized(v0 - v2) )));

    if (angle < minI) minI = angle;
    if (angle > maxI) maxI = angle;
Jan Möbius's avatar
Jan Möbius committed
484
    //sumI += angle;
Jan Möbius's avatar
 
Jan Möbius committed
485 486 487

    //compute dihedral angles
    typename MeshT::FaceFaceIter ff_it;
Jan Möbius's avatar
OM3  
Jan Möbius committed
488
    typename MeshT::Normal n1 = _mesh->normal(*f_it);
Jan Möbius's avatar
 
Jan Möbius committed
489

Jan Möbius's avatar
OM3  
Jan Möbius committed
490
    for (ff_it = _mesh->ff_iter(*f_it); ff_it.is_valid(); ++ff_it){
491
      
Jan Möbius's avatar
OM3  
Jan Möbius committed
492
      typename MeshT::Normal n2 = _mesh->normal(*ff_it);
Jan Möbius's avatar
 
Jan Möbius committed
493 494 495 496 497 498

      angle = OpenMesh::rad_to_deg(acos(OpenMesh::sane_aarg( MathTools::sane_normalized(n1) | MathTools::sane_normalized(n2) )));

      if (angle < minD) minD = angle;
      if (angle > maxD) maxD = angle;
      sumD += angle;
499
      numD ++;
Jan Möbius's avatar
 
Jan Möbius committed
500 501 502
    }
  }

Dirk Wilden's avatar
Dirk Wilden committed
503 504 505
  info_->aspectMin->setText( QString::number(minA,'f') );
  info_->aspectMean->setText( QString::number( sumA / _mesh->n_faces(),'f' ) );
  info_->aspectMax->setText( QString::number(maxA,'f') );
Jan Möbius's avatar
 
Jan Möbius committed
506

Dirk Wilden's avatar
Dirk Wilden committed
507 508 509
  info_->angleMin->setText( QString::number(minI,'f') );
  info_->angleMean->setText( "-" );
  info_->angleMax->setText( QString::number(maxI,'f')  );
Jan Möbius's avatar
 
Jan Möbius committed
510

511 512 513 514
  
  // Only one face or no face -> don't output angles 
  if ( _mesh->n_faces() > 1 ) {
    info_->dihedralMin->setText( QString::number(minD,'f') );
515
    info_->dihedralMean->setText( QString::number( sumD / numD,'f' ) );
516 517 518 519 520 521
    info_->dihedralMax->setText( QString::number(maxD,'f') );
  } else {
    info_->dihedralMin->setText( "-" );
    info_->dihedralMean->setText( "-" );
    info_->dihedralMax->setText( "-" );
  }
Jan Möbius's avatar
 
Jan Möbius committed
522 523 524 525

  //Calculate Bounding Box(min,max,cog)
  ACG::Vec3d min;
  ACG::Vec3d max;
526
  MeshInfo::getBoundingBox(_mesh, min, max);
Jan Möbius's avatar
Jan Möbius committed
527

Jan Möbius's avatar
 
Jan Möbius committed
528 529
  //Bounding Box Size
  ACG::Vec3d diff = max-min;
Jan Möbius's avatar
Jan Möbius committed
530

Dirk Wilden's avatar
Dirk Wilden committed
531 532 533 534 535 536 537 538 539 540 541
  info_->bbMinX->setText( QString::number(min[0],'f') );
  info_->bbMinY->setText( QString::number(min[1],'f') );
  info_->bbMinZ->setText( QString::number(min[2],'f') );

  info_->bbMaxX->setText( QString::number(max[0],'f') );
  info_->bbMaxY->setText( QString::number(max[1],'f') );
  info_->bbMaxZ->setText( QString::number(max[2],'f') );

  info_->bbSizeX->setText( QString::number(diff[0],'f') );
  info_->bbSizeY->setText( QString::number(diff[1],'f') );
  info_->bbSizeZ->setText( QString::number(diff[2],'f') );
Jan Möbius's avatar
 
Jan Möbius committed
542

Jan Möbius's avatar
Jan Möbius committed
543
  //COG
544
  ACG::Vec3d cog = MeshInfo::cog(_mesh);
Jan Möbius's avatar
 
Jan Möbius committed
545

Dirk Wilden's avatar
Dirk Wilden committed
546 547 548
  info_->cogX->setText( QString::number(cog[0],'f') );
  info_->cogY->setText( QString::number(cog[1],'f') );
  info_->cogZ->setText( QString::number(cog[2],'f') );
Jan Möbius's avatar
 
Jan Möbius committed
549

Dirk Wilden's avatar
Dirk Wilden committed
550 551 552 553
  //hitpoint
  info_->pointX->setText( QString::number( _hitPoint[0],'f' ) );
  info_->pointY->setText( QString::number( _hitPoint[1],'f' ) );
  info_->pointZ->setText( QString::number( _hitPoint[2],'f' ) );
Jan Möbius's avatar
 
Jan Möbius committed
554 555 556

  info_->setWindowFlags(info_->windowFlags() | Qt::WindowStaysOnTopHint);

557
  info_->adjustSize();
Jan Möbius's avatar
 
Jan Möbius committed
558 559 560 561 562 563 564
  info_->show();
}

//----------------------------------------------------------------------------------------------

/** \brief Find closest vertex to selection
 *
Jan Möbius's avatar
Jan Möbius committed
565
 * @param _mesh     Reference to the mesh
Jan Möbius's avatar
 
Jan Möbius committed
566
 * @param _face_idx Index of the face that has been clicked on
Jan Möbius's avatar
Jan Möbius committed
567 568 569
 * @param _hitPoint The point that is used as the reference
 *
 * @return index of the closest vertex of the face to the hitpoint
Jan Möbius's avatar
 
Jan Möbius committed
570 571 572
 */

template <class MeshT>
573
int InfoMeshObjectPlugin::getClosestVertexInFace(MeshT* _mesh, int _face_idx, ACG::Vec3d& _hitPoint) {
Jan Möbius's avatar
Jan Möbius committed
574

Jan Möbius's avatar
 
Jan Möbius committed
575
    typename MeshT::FaceVertexIter fv_it;
Jan Möbius's avatar
Jan Möbius committed
576

Jan Möbius's avatar
 
Jan Möbius committed
577 578
    int closest_v_idx = 0;
    double dist = DBL_MAX;
Jan Möbius's avatar
Jan Möbius committed
579

Jan Möbius's avatar
 
Jan Möbius committed
580 581
    ACG::Vec3d vTemp = ACG::Vec3d(0.0, 0.0, 0.0);
    typename MeshT::Point p;
Jan Möbius's avatar
Jan Möbius committed
582

Jan Möbius's avatar
OM3  
Jan Möbius committed
583
    for (fv_it = _mesh->fv_iter(_mesh->face_handle(_face_idx)); fv_it.is_valid(); ++fv_it){
Jan Möbius's avatar
Jan Möbius committed
584

Jan Möbius's avatar
OM3  
Jan Möbius committed
585
      p = _mesh->point( *fv_it );
Jan Möbius's avatar
Jan Möbius committed
586

Dirk Wilden's avatar
Dirk Wilden committed
587 588
      // Find closest vertex to selection
      vTemp = ACG::Vec3d(p[0], p[1], p[2]);
Jan Möbius's avatar
Jan Möbius committed
589
      const double temp_dist = (vTemp - _hitPoint).length();
Jan Möbius's avatar
 
Jan Möbius committed
590

Dirk Wilden's avatar
Dirk Wilden committed
591 592
      if (temp_dist < dist) {
          dist = temp_dist;
Jan Möbius's avatar
OM3  
Jan Möbius committed
593
          closest_v_idx = fv_it->idx();
Dirk Wilden's avatar
Dirk Wilden committed
594
      }
Jan Möbius's avatar
 
Jan Möbius committed
595 596 597 598 599 600 601 602 603

    }
    return closest_v_idx;
}

//-------------------------------------------------------------------------------------------

/** \brief Find closest edge to selection
 *
Jan Möbius's avatar
Jan Möbius committed
604
 * @param _mesh     Reference to the mesh
Jan Möbius's avatar
 
Jan Möbius committed
605
 * @param _face_idx Index of the face that has been clicked on
Jan Möbius's avatar
Jan Möbius committed
606
 * @param _hitPoint The point which will be tested
Jan Möbius's avatar
Jan Möbius committed
607 608
 *
 * @return index of the closest edge in the face to the hitpoint
Jan Möbius's avatar
 
Jan Möbius committed
609 610 611
 */

template <class MeshT>
Jan Möbius's avatar
Jan Möbius committed
612
int InfoMeshObjectPlugin::getClosestEdgeInFace(MeshT* _mesh, int _face_idx, const ACG::Vec3d& _hitPoint) {
Jan Möbius's avatar
Jan Möbius committed
613

Jan Möbius's avatar
 
Jan Möbius committed
614 615 616
    typename MeshT::ConstFaceHalfedgeIter fh_it;
    typename MeshT::VertexHandle v1, v2;
    typename MeshT::Point p1, p2;
Jan Möbius's avatar
Jan Möbius committed
617

Jan Möbius's avatar
Jan Möbius committed
618 619
    ACG::Vec3d vp1, vp2, h;
    double dist = DBL_MAX;
Jan Möbius's avatar
 
Jan Möbius committed
620
    int closest_e_handle = 0;
Jan Möbius's avatar
Jan Möbius committed
621

Jan Möbius's avatar
OM3  
Jan Möbius committed
622
    for (fh_it = _mesh->fh_iter(_mesh->face_handle(_face_idx)); fh_it.is_valid(); ++fh_it){
Jan Möbius's avatar
Jan Möbius committed
623

Jan Möbius's avatar
OM3  
Jan Möbius committed
624 625
      v1 = _mesh->from_vertex_handle(*fh_it);
      v2 = _mesh->to_vertex_handle(*fh_it);
Jan Möbius's avatar
Jan Möbius committed
626

Dirk Wilden's avatar
Dirk Wilden committed
627 628
      p1 = _mesh->point(v1);
      p2 = _mesh->point(v2);
Jan Möbius's avatar
Jan Möbius committed
629

Dirk Wilden's avatar
Dirk Wilden committed
630 631
      vp1 = ACG::Vec3d(p1[0], p1[1], p1[2]);
      vp2 = ACG::Vec3d(p2[0], p2[1], p2[2]);
Jan Möbius's avatar
Jan Möbius committed
632

Jan Möbius's avatar
Jan Möbius committed
633
      const ACG::Vec3d e = (vp2 - vp1).normalized();
Jan Möbius's avatar
Jan Möbius committed
634
      const ACG::Vec3d g = _hitPoint - vp1;
Jan Möbius's avatar
Jan Möbius committed
635
      const double x = g | e;
Jan Möbius's avatar
Jan Möbius committed
636

Jan Möbius's avatar
Jan Möbius committed
637
      const double temp_dist = (_hitPoint - (vp1 + x * e)).length();
Jan Möbius's avatar
Jan Möbius committed
638

Dirk Wilden's avatar
Dirk Wilden committed
639 640 641
      if (temp_dist < dist) {
          dist = temp_dist;
      }
Jan Möbius's avatar
 
Jan Möbius committed
642
    }
Jan Möbius's avatar
Jan Möbius committed
643

Jan Möbius's avatar
 
Jan Möbius committed
644
    return closest_e_handle;
Jan Möbius's avatar
Jan Möbius committed
645 646
}

Jan Möbius's avatar
 
Jan Möbius committed
647 648
//----------------------------------------------------------------------------------------------

649 650
/** \brief Find closest vertex on the edge (endpoint)
 *
Jan Möbius's avatar
Jan Möbius committed
651 652 653 654 655
 * @param _mesh     Reference to the mesh
 * @param _edge_idx Index of the edge that has been clicked on
 * @param _hitPoint The point which will be tested
 *
 * @return index of the closest vertex on the edge
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
 */

template <class MeshT>
int InfoMeshObjectPlugin::getClosestVertexFromEdge(MeshT* _mesh, int _edge_idx, ACG::Vec3d& _hitPoint) {

    ACG::Vec3d toVertex   = _mesh->point( _mesh->to_vertex_handle(   _mesh->halfedge_handle(_mesh->edge_handle(_edge_idx),0 )) );
    ACG::Vec3d fromVertex = _mesh->point( _mesh->from_vertex_handle( _mesh->halfedge_handle(_mesh->edge_handle(_edge_idx),0 )) );

    double distTo   = (_hitPoint - toVertex  ).norm();
    double distFrom = (_hitPoint - fromVertex).norm();

    if ( distTo > distFrom )
      return _mesh->from_vertex_handle( _mesh->halfedge_handle(_mesh->edge_handle(_edge_idx),0 ) ).idx();
    else
      return _mesh->to_vertex_handle(   _mesh->halfedge_handle(_mesh->edge_handle(_edge_idx),0 ) ).idx();

}

//----------------------------------------------------------------------------------------------

Jan Möbius's avatar
Jan Möbius committed
676
void
677
InfoMeshObjectPlugin::
678 679 680 681 682
  slotInformationRequested(const QPoint _clickedPoint, DataType _type) {

    // Only respond on mesh objects
    if((_type != DATA_TRIANGLE_MESH) && (_type != DATA_POLY_MESH)) return;

683 684 685 686
    ACG::SceneGraph::PickTarget target = ACG::SceneGraph::PICK_FACE;

    unsigned int   node_idx, target_idx;
    ACG::Vec3d     hit_point;
687

688 689
    if (info_->isHidden())
    {
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
      //user couldn't select the pick mode,
      //so we have to do this
      target = ACG::SceneGraph::PICK_ANYTHING;
      if (!PluginFunctions::scenegraphPick(target, _clickedPoint, node_idx, target_idx, &hit_point))
        return;

      BaseObjectData* object;
      if (!PluginFunctions::getPickedObject(node_idx, object) )
        return;

      //object is picked, now we can decide, what the user wants to pick
      //priority: face > edge > vertex
      if ( object->dataType(DATA_TRIANGLE_MESH) )
      {
        TriMesh* mesh = PluginFunctions::triMesh(object);
        if (mesh->n_faces() != 0)
          info_->pickMode->setCurrentIndex(0);
        else if (mesh->n_edges() != 0)
          info_->pickMode->setCurrentIndex(1);
        else
          info_->pickMode->setCurrentIndex(2);
      }
      else if ( object->dataType(DATA_POLY_MESH) )
      {
        PolyMesh* mesh = PluginFunctions::polyMesh(object);
        if (mesh->n_faces() != 0)
          info_->pickMode->setCurrentIndex(0);
        else if (mesh->n_edges() != 0)
          info_->pickMode->setCurrentIndex(1);
        else
          info_->pickMode->setCurrentIndex(2);
      }
722 723
    }

724 725 726 727 728
    target = ACG::SceneGraph::PICK_FACE;
    if (info_->pickMode->currentIndex() == 1 )
      target = ACG::SceneGraph::PICK_EDGE;
    else if (info_->pickMode->currentIndex() == 2 )
      target = ACG::SceneGraph::PICK_VERTEX;
Jan Möbius's avatar
Jan Möbius committed
729

730
    if (PluginFunctions::scenegraphPick(target, _clickedPoint, node_idx, target_idx, &hit_point)) {
Jan Möbius's avatar
 
Jan Möbius committed
731 732 733
      BaseObjectData* object;

      if ( PluginFunctions::getPickedObject(node_idx, object) ) {
Jan Möbius's avatar
Jan Möbius committed
734

Jan Möbius's avatar
 
Jan Möbius committed
735
         emit log( LOGINFO , object->getObjectinfo() );
Jan Möbius's avatar
Jan Möbius committed
736

737 738 739 740
         if ( object->dataType(DATA_TRIANGLE_MESH) )
           printMeshInfo( PluginFunctions::triMesh(object) , object->id(), target_idx, hit_point );

         if ( object->dataType(DATA_POLY_MESH) )
741
           printMeshInfo( PluginFunctions::polyMesh(object) , object->id(), target_idx, hit_point );
Jan Möbius's avatar
 
Jan Möbius committed
742 743
      } else return;
    }
744 745 746 747
    else
    {
      emit log( LOGERR , tr("Unable to pick object.") );
    }
Jan Möbius's avatar
 
Jan Möbius committed
748 749 750 751
}

//------------------------------------------------------------------------------

752
template< class MeshT >
753
void InfoMeshObjectPlugin::getEdgeLengths(MeshT* _mesh, double &min, double &max, double &mean)
754 755 756 757
{
  typename MeshT::ConstEdgeIter e_it(_mesh->edges_sbegin()),
                                e_end(_mesh->edges_end());

758 759
  min = FLT_MAX;
  max = FLT_MIN;
760 761 762
  mean = 0.0;
  for (; e_it!=e_end; ++e_it)
  {
Jan Möbius's avatar
OM3  
Jan Möbius committed
763 764
    typename MeshT::Scalar len = (_mesh->point(_mesh->to_vertex_handle(_mesh->halfedge_handle(*e_it, 0))) -
                                  _mesh->point(_mesh->to_vertex_handle(_mesh->halfedge_handle(*e_it, 1)))).norm ();
765 766 767 768 769 770 771 772 773 774
    if (len < min) min = len;
    if (len > max) max = len;
    mean += len;
  }

  mean /= _mesh->n_edges();
}

//------------------------------------------------------------------------------

775
bool InfoMeshObjectPlugin::getEdgeLengths(int _id, double &min, double &max, double &mean)
776 777 778 779 780 781
{
  BaseObjectData* object;
  if ( ! PluginFunctions::getObject(_id,object) )
    return false;

  if ( object == 0){
Jan Möbius's avatar
Jan Möbius committed
782
    emit log(LOGERR, tr("Unable to get object"));
783 784 785 786 787 788 789
    return false;
  }

  if ( object->dataType(DATA_TRIANGLE_MESH) ) {
    TriMesh* mesh = PluginFunctions::triMesh(object);

    if ( mesh == 0 ) {
Jan Möbius's avatar
Jan Möbius committed
790
      emit log(LOGERR,tr("Unable to get mesh"));
791 792 793 794 795 796 797 798 799 800
      return false;
    }

    getEdgeLengths (mesh, min, max, mean);
    return true;

  } else {
    PolyMesh* mesh = PluginFunctions::polyMesh(object);

    if ( mesh == 0 ) {
Jan Möbius's avatar
Jan Möbius committed
801
      emit log(LOGERR,tr("Unable to get mesh"));
802 803 804 805 806 807 808 809 810 811 812 813
      return false;
    }

    getEdgeLengths (mesh, min, max, mean);
    return true;
  }

  return false;
}

//------------------------------------------------------------------------------

814
void InfoMeshObjectPlugin::slotObjectUpdated( int _identifier , const UpdateType& _type){
Jan Möbius's avatar
Jan Möbius committed
815

816 817 818 819
  if ( !infoBar_ ) {
    return;
  }

820 821 822
  // We only show the information in the status bar if one target mesh is selected.
  if ( PluginFunctions::targetCount() ==1 ) {

823
    BaseObjectData* object;
824 825 826
    PluginFunctions::getObject(_identifier,object);

    // We only need to update something, if the updated object is the target object
827
    if (object && object->target() ) {
828

829
      if (object->dataType(DATA_TRIANGLE_MESH)){
830

831
        TriMesh* mesh = PluginFunctions::triMesh(object);
832

833 834 835
        infoBar_->vertices->setText( QLocale::system().toString( qulonglong(mesh->n_vertices()) ) );
        infoBar_->edges->setText( QLocale::system().toString( qulonglong(mesh->n_edges()) ) );
        infoBar_->faces->setText( QLocale::system().toString( qulonglong(mesh->n_faces()) ) );
836 837 838 839

        infoBar_->showCounts();

        return;
Dirk Wilden's avatar
Dirk Wilden committed
840
      }
841

842 843 844 845
      if (object->dataType(DATA_POLY_MESH)){

        PolyMesh* mesh = PluginFunctions::polyMesh(object);

846 847 848
        infoBar_->vertices->setText( QLocale::system().toString( qulonglong(mesh->n_vertices()) ) );
        infoBar_->edges->setText( QLocale::system().toString( qulonglong(mesh->n_edges()) ) );
        infoBar_->faces->setText( QLocale::system().toString( qulonglong(mesh->n_faces()) ) );
849 850 851

        infoBar_->showCounts();
        return;
Dirk Wilden's avatar
Dirk Wilden committed
852
      }
853

Dirk Wilden's avatar
Dirk Wilden committed
854
    }
855

Dirk Wilden's avatar
Dirk Wilden committed
856
  } else {
857 858
    // Display only count information
    if ( PluginFunctions::targetCount() > 0 ) {
Dirk Wilden's avatar
Dirk Wilden committed
859
      infoBar_->showTargetCount( PluginFunctions::targetCount() );
860
    } else
Dirk Wilden's avatar
Dirk Wilden committed
861 862
      infoBar_->hideCounts();
  }
863

Dirk Wilden's avatar
Dirk Wilden committed
864
}
865

Dirk Wilden's avatar
Dirk Wilden committed
866 867
//------------------------------------------------------------------------------

868
void InfoMeshObjectPlugin::slotObjectSelectionChanged( int _identifier ){
869
  slotObjectUpdated( _identifier , UPDATE_ALL );
Dirk Wilden's avatar
Dirk Wilden committed
870
}
871

Dirk Wilden's avatar
Dirk Wilden committed
872 873
//------------------------------------------------------------------------------

874
void InfoMeshObjectPlugin::slotAllCleared(){
875 876
  if ( infoBar_ )
    infoBar_->hideCounts();
Dirk Wilden's avatar
Dirk Wilden committed
877 878
}

Matthias Möller's avatar
Matthias Möller committed
879 880 881
#if QT_VERSION < 0x050000
  Q_EXPORT_PLUGIN2( InfoMeshObjectPlugin , InfoMeshObjectPlugin );
#endif
Jan Möbius's avatar
 
Jan Möbius committed
882

Jan Möbius's avatar
Jan Möbius committed
883