InfoPlugin.cc 18.3 KB
Newer Older
1 2 3 4 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 33 34 35 36
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 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 <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

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

Jan Möbius's avatar
 
Jan Möbius committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
//=============================================================================
//
//  CLASS InfoPlugin - IMPLEMENTATION
//
//=============================================================================


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

#include <QtGui>

#include "InfoPlugin.hh"

#include <iostream>
#include <ACG/GL/GLState.hh>
#include <QStringList>
Jan Möbius's avatar
Jan Möbius committed
59
#include <QLocale>
Jan Möbius's avatar
 
Jan Möbius committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

#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>

#include <float.h>

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

/// initialize the plugin
void InfoPlugin::pluginsInitialized() {

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

Jan Möbius's avatar
 
Jan Möbius committed
78 79 80 81 82 83 84
  // Initialize hit point
  hit_point_ = ACG::Vec3d(0.0, 0.0, 0.0);
}

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

template< class MeshT >
Dirk Wilden's avatar
Dirk Wilden committed
85
void InfoPlugin::printMeshInfo( MeshT* _mesh , int _id, unsigned int _face, ACG::Vec3d& _hitPoint ) {
Jan Möbius's avatar
Jan Möbius committed
86 87 88

  QLocale locale;

Jan Möbius's avatar
 
Jan Möbius committed
89 90 91
  if (info_ == 0){
    info_ = new InfoDialog();
  }
Jan Möbius's avatar
Jan Möbius committed
92

Jan Möbius's avatar
 
Jan Möbius committed
93 94
  int closest_v_idx = getClosestVertex(_mesh, _face);
  int closest_e_idx = getClosestEdge(_mesh, _face);
Jan Möbius's avatar
Jan Möbius committed
95

Jan Möbius's avatar
 
Jan Möbius committed
96
  // ID
Dirk Wilden's avatar
Dirk Wilden committed
97
  info_->id->setText( locale.toString(_id) );
Jan Möbius's avatar
 
Jan Möbius committed
98
  // Vertices
Dirk Wilden's avatar
Dirk Wilden committed
99
  info_->vertices->setText( locale.toString( _mesh->n_vertices() ) );
Jan Möbius's avatar
 
Jan Möbius committed
100
  // Faces
Dirk Wilden's avatar
Dirk Wilden committed
101
  info_->faces->setText( locale.toString( _mesh->n_faces() ) );
Jan Möbius's avatar
 
Jan Möbius committed
102
  // Edges
Dirk Wilden's avatar
Dirk Wilden committed
103
  info_->edges->setText( locale.toString( _mesh->n_edges() ) );
Jan Möbius's avatar
 
Jan Möbius committed
104
  // Closest Vertex
Dirk Wilden's avatar
Dirk Wilden committed
105
  info_->vertexHandle->setText( locale.toString( closest_v_idx ) );
Jan Möbius's avatar
 
Jan Möbius committed
106
  // Closest Edge
Dirk Wilden's avatar
Dirk Wilden committed
107
  info_->edgeHandle->setText( locale.toString( closest_e_idx ) );
Jan Möbius's avatar
Jan Möbius committed
108
  // Picked Vertex
Dirk Wilden's avatar
Dirk Wilden committed
109
  info_->faceHandle->setText( locale.toString( _face ) );
Jan Möbius's avatar
 
Jan Möbius committed
110
  // Components
Dirk Wilden's avatar
Dirk Wilden committed
111
  info_->components->setText( locale.toString(MeshInfo::componentCount(_mesh)));
Jan Möbius's avatar
 
Jan Möbius committed
112
  // Boundaries
Dirk Wilden's avatar
Dirk Wilden committed
113
  info_->boundaries->setText( locale.toString(MeshInfo::boundaryCount(_mesh)) );
Jan Möbius's avatar
 
Jan Möbius committed
114 115
  // Genus
  int genus = 1 - (_mesh->n_vertices() - _mesh->n_edges() + _mesh->n_faces() ) / 2;
Dirk Wilden's avatar
Dirk Wilden committed
116
  info_->genus->setText( QString::number(genus) );
Jan Möbius's avatar
 
Jan Möbius committed
117 118 119 120

  info_->table->clear();

  info_->table->setColumnCount ( 3 );
Dirk Wilden's avatar
Dirk Wilden committed
121
  info_->table->setRowCount ( 5 );
Jan Möbius's avatar
 
Jan Möbius committed
122 123 124


  QStringList headerdata;
Dirk Wilden's avatar
Dirk Wilden committed
125
  headerdata << tr("Minimum") << tr("Mean") << tr("Maximum");
Jan Möbius's avatar
 
Jan Möbius committed
126 127 128 129

  info_->table->setHorizontalHeaderLabels(headerdata);

  headerdata.clear();
Dirk Wilden's avatar
Dirk Wilden committed
130
  headerdata << tr("Vertex Valence") << tr("Edge Length") << tr("Aspect Ratio") << tr("Inner Face Angles") << tr("Dihedral Angles");
Jan Möbius's avatar
 
Jan Möbius committed
131 132 133 134

  info_->table->setVerticalHeaderLabels(headerdata);

  //set tooltips
Dirk Wilden's avatar
Dirk Wilden committed
135 136 137
  info_->table->verticalHeaderItem(0)->setToolTip(tr("minimum, maximum and arithmetic mean of the vertex valences"));
  info_->table->verticalHeaderItem(1)->setToolTip(tr("minimum, maximum and arithmetic mean of the edge lengthes"));
  info_->table->verticalHeaderItem(2)->setToolTip(tr("minimum, maximum and arithmetic mean of the aspect ratio."
Jan Möbius's avatar
Jan Möbius committed
138
                                                     " i.e. the ratio between longest and shortest edge in a triangle."));
Dirk Wilden's avatar
Dirk Wilden committed
139 140
  info_->table->verticalHeaderItem(3)->setToolTip(tr("minimum, maximum and arithmetic mean of the inner angles in a face."));
  info_->table->verticalHeaderItem(4)->setToolTip(tr("minimum, maximum and arithmetic mean of the dihedral angles"
Jan Möbius's avatar
Jan Möbius committed
141
                                                     " i.e. the angles between neighboring faces."));
Jan Möbius's avatar
 
Jan Möbius committed
142 143 144 145

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

Jan Möbius's avatar
 
Jan Möbius committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  float maxX = FLT_MIN;
  float minX = FLT_MAX;
  float sumX = 0.0;
  float maxY = FLT_MIN;
  float minY = FLT_MAX;
  float sumY = 0.0;
  float maxZ = FLT_MIN;
  float minZ = FLT_MAX;
  float sumZ = 0.0;
  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
162

Jan Möbius's avatar
 
Jan Möbius committed
163 164 165 166 167 168 169 170 171 172 173 174 175
  //iterate over all vertices
  for (v_it = _mesh->vertices_begin(); v_it != v_end; ++v_it){
    typename MeshT::Point p = _mesh->point( v_it.handle() );
    if (p[0] < minX) minX = p[0];
    if (p[0] > maxX) maxX = p[0];
    sumX += p[0];
    if (p[1] < minY) minY = p[1];
    if (p[1] > maxY) maxY = p[1];
    sumY += p[1];
    if (p[2] < minZ) minZ = p[2];
    if (p[2] > maxZ) maxZ = p[2];
    sumZ += p[2];

Jan Möbius's avatar
Jan Möbius committed
176 177


Jan Möbius's avatar
 
Jan Möbius committed
178 179 180
    //check valence + edge length
    int valence = 0;
    typename MeshT::VertexVertexIter vv_it;
Jan Möbius's avatar
Jan Möbius committed
181

Jan Möbius's avatar
 
Jan Möbius committed
182 183
    for (vv_it=_mesh->vv_iter( v_it ); vv_it; ++vv_it){
      valence++;
Jan Möbius's avatar
Jan Möbius committed
184

Jan Möbius's avatar
 
Jan Möbius committed
185 186 187 188 189 190 191 192 193 194 195 196
      typename MeshT::Point p2 = _mesh->point( vv_it.handle() );
      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
197

Jan Möbius's avatar
 
Jan Möbius committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 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 263
  int row=0;
  info_->table->setItem(row,0, new QTableWidgetItem( QString::number(minV) ) );
  info_->table->setItem(row,1, new QTableWidgetItem( QString::number( sumV / (float)_mesh->n_vertices(),'f' )) );
  info_->table->setItem(row,2, new QTableWidgetItem( QString::number(maxV) ) );
  row++;
  info_->table->setItem(row,0, new QTableWidgetItem( QString::number(minE,'f') ) );
  info_->table->setItem(row,1, new QTableWidgetItem( QString::number( sumE / (_mesh->n_edges()*2),'f' )) );
  info_->table->setItem(row,2, new QTableWidgetItem( QString::number(maxE,'f') ) );

  //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;
  float sumI = 0.0;
  float maxD = FLT_MIN;
  float minD = FLT_MAX;
  float sumD = 0.0;

  //iterate over all faces
  for (f_it = _mesh->faces_begin(); f_it != f_end; ++f_it){
    typename MeshT::ConstFaceVertexIter cfv_it = _mesh->cfv_iter(f_it);

    typename MeshT::Point v0 = _mesh->point( cfv_it.handle() );
    ++cfv_it;
    typename MeshT::Point v1 = _mesh->point( cfv_it.handle() );
    ++cfv_it;
    typename MeshT::Point v2 = _mesh->point( cfv_it.handle() );

    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;
    sumI += angle;

    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;
    sumI += angle;

    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;
    sumI += angle;

    //compute dihedral angles
    typename MeshT::FaceFaceIter ff_it;
    typename MeshT::Normal n1 = _mesh->normal(f_it);

    for (ff_it = _mesh->ff_iter(f_it); ff_it; ++ff_it){

Jan Möbius's avatar
Jan Möbius committed
264
      typename MeshT::Normal n2 = _mesh->normal(ff_it);
Jan Möbius's avatar
 
Jan Möbius committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

      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;

    }
  }

  row++;
  info_->table->setItem(row,0, new QTableWidgetItem( QString::number(minA,'f') ) );
  info_->table->setItem(row,1, new QTableWidgetItem( QString::number( sumA / _mesh->n_faces(),'f' )) );
  info_->table->setItem(row,2, new QTableWidgetItem( QString::number(maxA,'f') ) );
  row++;
  info_->table->setItem(row,0, new QTableWidgetItem( QString::number(minI,'f') ) );
  info_->table->setItem(row,1, new QTableWidgetItem( "-" ) );
  info_->table->setItem(row,2, new QTableWidgetItem( QString::number(maxI,'f') ) );
  row++;
Jan Möbius's avatar
Jan Möbius committed
284

Jan Möbius's avatar
 
Jan Möbius committed
285 286 287 288 289 290 291 292 293
  info_->table->setItem(row,0, new QTableWidgetItem( QString::number(minD,'f') ) );
  info_->table->setItem(row,1, new QTableWidgetItem( QString::number( sumD / (_mesh->n_faces()*3),'f' )) );
  info_->table->setItem(row,2, new QTableWidgetItem( QString::number(maxD,'f') ) );

  info_->table->resizeColumnsToContents();


  typename MeshT::FaceHandle fh = _mesh->face_handle(_face);

Dirk Wilden's avatar
Dirk Wilden committed
294
  //adjacent vertex handles
Jan Möbius's avatar
 
Jan Möbius committed
295
  typename MeshT::FaceVertexIter fv_it = _mesh->fv_iter(fh);
Dirk Wilden's avatar
Dirk Wilden committed
296 297 298 299
  QString adjacentVertices;

  if ( fv_it ){
    adjacentVertices = QString::number( fv_it.handle().idx() );
Jan Möbius's avatar
 
Jan Möbius committed
300 301 302
    ++fv_it;
  }

Dirk Wilden's avatar
Dirk Wilden committed
303 304 305 306
  while( fv_it ){
    adjacentVertices += "," + QString::number( fv_it.handle().idx() );
    ++fv_it;
  }
Jan Möbius's avatar
 
Jan Möbius committed
307

Dirk Wilden's avatar
Dirk Wilden committed
308
  info_->adjVertexHandles->setText( adjacentVertices );
Jan Möbius's avatar
 
Jan Möbius committed
309 310 311 312 313

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

Jan Möbius's avatar
 
Jan Möbius committed
315 316
  //Bounding Box Size
  ACG::Vec3d diff = max-min;
Jan Möbius's avatar
Jan Möbius committed
317

Dirk Wilden's avatar
Dirk Wilden committed
318 319 320 321 322 323 324 325 326 327 328
  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
329

Jan Möbius's avatar
Jan Möbius committed
330
  //COG
Jan Möbius's avatar
 
Jan Möbius committed
331 332
  ACG::Vec3d cog = MeshInfo::cog(*_mesh);

Dirk Wilden's avatar
Dirk Wilden committed
333 334 335
  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
336 337

  //face-normal
Dirk Wilden's avatar
Dirk Wilden committed
338 339 340
  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' ) );
Jan Möbius's avatar
Jan Möbius committed
341

Dirk Wilden's avatar
Dirk Wilden committed
342 343 344 345
  //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
346 347


Dirk Wilden's avatar
Dirk Wilden committed
348 349 350 351 352
  //closest vertex
  info_->vertexX->setText( QString::number( _mesh->point( _mesh->vertex_handle(closest_v_idx) )[0],'f' ) );
  info_->vertexY->setText( QString::number( _mesh->point( _mesh->vertex_handle(closest_v_idx) )[1],'f' ) );
  info_->vertexZ->setText( QString::number( _mesh->point( _mesh->vertex_handle(closest_v_idx) )[2],'f' ) );

Jan Möbius's avatar
 
Jan Möbius committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

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

  info_->show();
}

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

/** \brief Find closest vertex to selection
 *
 * @param _mesh Refernce to the mesh
 * @param _face_idx Index of the face that has been clicked on
 */

template <class MeshT>
int InfoPlugin::getClosestVertex(MeshT* _mesh, int _face_idx) {
Jan Möbius's avatar
Jan Möbius committed
369

Jan Möbius's avatar
 
Jan Möbius committed
370
    typename MeshT::FaceVertexIter fv_it;
Jan Möbius's avatar
Jan Möbius committed
371

Jan Möbius's avatar
 
Jan Möbius committed
372 373 374
    int closest_v_idx = 0;
    double dist = DBL_MAX;
    double temp_dist = 0.0;
Jan Möbius's avatar
Jan Möbius committed
375

Jan Möbius's avatar
 
Jan Möbius committed
376 377
    ACG::Vec3d vTemp = ACG::Vec3d(0.0, 0.0, 0.0);
    typename MeshT::Point p;
Jan Möbius's avatar
Jan Möbius committed
378

Jan Möbius's avatar
 
Jan Möbius committed
379
    for (fv_it = _mesh->fv_iter(_mesh->face_handle(_face_idx)); fv_it; ++fv_it){
Jan Möbius's avatar
Jan Möbius committed
380

Dirk Wilden's avatar
Dirk Wilden committed
381
      p = _mesh->point( fv_it.handle() );
Jan Möbius's avatar
Jan Möbius committed
382

Dirk Wilden's avatar
Dirk Wilden committed
383 384 385
      // Find closest vertex to selection
      vTemp = ACG::Vec3d(p[0], p[1], p[2]);
      temp_dist = (vTemp - hit_point_).length();
Jan Möbius's avatar
 
Jan Möbius committed
386

Dirk Wilden's avatar
Dirk Wilden committed
387 388 389 390
      if (temp_dist < dist) {
          dist = temp_dist;
          closest_v_idx = fv_it.handle().idx();
      }
Jan Möbius's avatar
 
Jan Möbius committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

    }
    return closest_v_idx;
}

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

/** \brief Find closest edge to selection
 *
 * @param _mesh Reference to the mesh
 * @param _face_idx Index of the face that has been clicked on
 */

template <class MeshT>
int InfoPlugin::getClosestEdge(MeshT* _mesh, int _face_idx) {
Jan Möbius's avatar
Jan Möbius committed
406

Jan Möbius's avatar
 
Jan Möbius committed
407 408 409
    typename MeshT::ConstFaceHalfedgeIter fh_it;
    typename MeshT::VertexHandle v1, v2;
    typename MeshT::Point p1, p2;
Jan Möbius's avatar
Jan Möbius committed
410

Jan Möbius's avatar
 
Jan Möbius committed
411 412 413 414
    ACG::Vec3d vp1, vp2, click, e, g, h;
    double x, temp_dist, dist = DBL_MAX;
    int closest_e_handle = 0;
    click = ACG::Vec3d(hit_point_[0], hit_point_[1], hit_point_[2]);
Jan Möbius's avatar
Jan Möbius committed
415

Jan Möbius's avatar
 
Jan Möbius committed
416
    for (fh_it = _mesh->fh_iter(_mesh->face_handle(_face_idx)); fh_it; ++fh_it){
Jan Möbius's avatar
Jan Möbius committed
417

Dirk Wilden's avatar
Dirk Wilden committed
418 419
      v1 = _mesh->from_vertex_handle(fh_it);
      v2 = _mesh->to_vertex_handle(fh_it);
Jan Möbius's avatar
Jan Möbius committed
420

Dirk Wilden's avatar
Dirk Wilden committed
421 422
      p1 = _mesh->point(v1);
      p2 = _mesh->point(v2);
Jan Möbius's avatar
Jan Möbius committed
423

Dirk Wilden's avatar
Dirk Wilden committed
424 425
      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
426

Dirk Wilden's avatar
Dirk Wilden committed
427 428 429 430
      e = vp2 - vp1;
      e.normalize();
      g = click - vp1;
      x = g | e;
Jan Möbius's avatar
Jan Möbius committed
431

Dirk Wilden's avatar
Dirk Wilden committed
432
      temp_dist = (click - (vp1 + x * e)).length();
Jan Möbius's avatar
Jan Möbius committed
433

Dirk Wilden's avatar
Dirk Wilden committed
434 435 436 437
      if (temp_dist < dist) {
          dist = temp_dist;
          closest_e_handle = _mesh->edge_handle(fh_it.handle()).idx();
      }
Jan Möbius's avatar
 
Jan Möbius committed
438
    }
Jan Möbius's avatar
Jan Möbius committed
439

Jan Möbius's avatar
 
Jan Möbius committed
440
    return closest_e_handle;
Jan Möbius's avatar
Jan Möbius committed
441 442
}

Jan Möbius's avatar
 
Jan Möbius committed
443 444
//----------------------------------------------------------------------------------------------

Jan Möbius's avatar
Jan Möbius committed
445
void
Jan Möbius's avatar
 
Jan Möbius committed
446
InfoPlugin::
Jan Möbius's avatar
Jan Möbius committed
447
  slotMouseEventIdentify( QMouseEvent* _event ) {
Jan Möbius's avatar
 
Jan Möbius committed
448 449 450 451
  if (_event->type() == QEvent::MouseButtonPress)
  {
    unsigned int   node_idx, target_idx;
    ACG::Vec3d     hit_point;
Jan Möbius's avatar
Jan Möbius committed
452

Jan Möbius's avatar
 
Jan Möbius committed
453 454 455 456
    if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_FACE, _event->pos(),node_idx, target_idx, &hit_point)) {
      BaseObjectData* object;

//     BaseObject* obj = dynamic_cast< BaseObject* > (object);
Jan Möbius's avatar
Jan Möbius committed
457

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

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

Dirk Wilden's avatar
Dirk Wilden committed
462 463
         // Set hit point
         hit_point_ = ACG::Vec3d(hit_point[0], hit_point[1], hit_point[2]);
Jan Möbius's avatar
Jan Möbius committed
464 465

         if ( object->picked(node_idx) && object->dataType(DATA_TRIANGLE_MESH) )
Dirk Wilden's avatar
Dirk Wilden committed
466
            printMeshInfo( PluginFunctions::triMesh(object) , object->id(), target_idx, hit_point_ );
Jan Möbius's avatar
Jan Möbius committed
467 468

         if ( object->picked(node_idx) && object->dataType(DATA_POLY_MESH) )
Dirk Wilden's avatar
Dirk Wilden committed
469
            printMeshInfo( PluginFunctions::polyMesh(object) , object->id(), target_idx, hit_point_ );
Jan Möbius's avatar
Jan Möbius committed
470

Jan Möbius's avatar
 
Jan Möbius committed
471 472 473 474 475 476 477
      } else return;
    }
  }
}

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

478 479 480 481 482 483
template< class MeshT >
void InfoPlugin::getEdgeLengths(MeshT* _mesh, double &min, double &max, double &mean)
{
  typename MeshT::ConstEdgeIter e_it(_mesh->edges_sbegin()),
                                e_end(_mesh->edges_end());

484 485
  min = FLT_MAX;
  max = FLT_MIN;
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
  mean = 0.0;
  for (; e_it!=e_end; ++e_it)
  {
    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 ();
    if (len < min) min = len;
    if (len > max) max = len;
    mean += len;
  }

  mean /= _mesh->n_edges();
}

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

bool InfoPlugin::getEdgeLengths(int _id, double &min, double &max, double &mean)
{
  BaseObjectData* object;
  if ( ! PluginFunctions::getObject(_id,object) )
    return false;

  if ( object == 0){
Jan Möbius's avatar
Jan Möbius committed
508
    emit log(LOGERR, tr("Unable to get object"));
509 510 511 512 513 514 515
    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
516
      emit log(LOGERR,tr("Unable to get mesh"));
517 518 519 520 521 522 523 524 525 526
      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
527
      emit log(LOGERR,tr("Unable to get mesh"));
528 529 530 531 532 533 534 535 536 537 538 539 540 541
      return false;
    }

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

  return false;
}

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



Jan Möbius's avatar
 
Jan Möbius committed
542 543
Q_EXPORT_PLUGIN2( InfoPlugin , InfoPlugin );

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