VDPMSynthesizerViewerWidget.cc 16.9 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 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 264 265 266 267 268 269 270 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 300 301 302 303 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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
/*===========================================================================*\
 *                                                                           *
 *                               OpenMesh                                    *
 *      Copyright (C) 2001-2003 by Computer Graphics Group, RWTH Aachen      *
 *                           www.openmesh.org                                *
 *                                                                           *
 *---------------------------------------------------------------------------* 
 *                                                                           *
 *                                License                                    *
 *                                                                           *
 *  This library 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, version 2.1.                            *
 *                                                                           *
 *  This library 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 Lesser General Public         *
 *  License along with this library; if not, write to the Free Software      *
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                *
 *                                                                           *
\*===========================================================================*/

//=============================================================================
//
//  CLASS newClass - IMPLEMENTATION
//
//=============================================================================

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

#ifdef _MSC_VER
#  pragma warning(disable: 4267 4311)
#endif

#include <iostream>
#include <fstream>
#include <map>

#include <QApplication>
#include <QDateTime>
#include <QFileDialog>
#include <QDataStream>
#include <GL/glut.h>


#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/IO/BinaryHelper.hh>
#include <OpenMesh/Core/Utils/Endian.hh>
#include <OpenMesh/Tools/Utils/Timer.hh>
#include <OpenMesh/Apps/VDProgMesh/Synthesizer/VDPMSynthesizerViewerWidget.hh>


//== NAMESPACES ===============================================================


namespace OpenMesh {


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



void
VDPMSynthesizerViewerWidget::
draw_scene(const std::string &_draw_mode)
{
  if (adaptive_mode_ == true)
  {    
    adaptive_refinement();
  }
  MeshViewerWidget::draw_scene(_draw_mode);  
}


void
VDPMSynthesizerViewerWidget::
adaptive_refinement()
{
  update_viewing_parameters();

  VDPMMesh::HalfedgeHandle v0v1;

  float fovy = viewing_parameters_.fovy();

  float tolerance_square = viewing_parameters_.tolerance_square();
  float	tan_value = tanf(fovy / 2.0f);
	
  kappa_square_ = 4.0f * tan_value * tan_value * tolerance_square;

  //assert( !vfront_.end() );

  for ( vfront_.begin(); !vfront_.end(); )
  {
    VHierarchyNodeHandle  
      node_handle   = vfront_.node_handle(),
      parent_handle = vhierarchy_.parent_handle(node_handle);
    
    if (vhierarchy_.is_leaf_node(node_handle) != true && 
        qrefine(node_handle) == true)
    {
      force_vsplit(node_handle);      
    }
    else if (vhierarchy_.is_root_node(node_handle) != true && 
	     ecol_legal(parent_handle, v0v1) == true       && 
	     qrefine(parent_handle) != true)
    {
      ecol(parent_handle, v0v1);
    }
    else
    {
      vfront_.next();
    }
  }

  // free memories tagged as 'deleted'
  mesh_.garbage_collection(false, true, true); 
  mesh_.update_face_normals();
}


bool
VDPMSynthesizerViewerWidget::
qrefine(VHierarchyNodeHandle _node_handle)
{
  VHierarchyNode &node    = vhierarchy_.node(_node_handle);
  Vec3f p       = mesh_.point(node.vertex_handle());  
  Vec3f eye_dir = p - viewing_parameters_.eye_pos();;

  float	distance = eye_dir.length();
  float	distance2 = distance * distance;
  float	product_value = dot(eye_dir, node.normal());

  if (outside_view_frustum(p, node.radius()) == true)
    return	false;

  if (oriented_away(node.sin_square(), distance2, product_value) == true)
    return	false;

  if (screen_space_error(node.mue_square(), 
			 node.sigma_square(), 
			 distance2, 
			 product_value) == true)
    return false;
  
  return true;
}


void 
VDPMSynthesizerViewerWidget::
force_vsplit(VHierarchyNodeHandle node_handle)
{
  VDPMMesh::VertexHandle  vl, vr;

  get_active_cuts(node_handle, vl, vr);

  while (vl == vr) 
  {
    force_vsplit(mesh_.data(vl).vhierarchy_node_handle());
    get_active_cuts(node_handle, vl, vr);
  }
	
  vsplit(node_handle, vl, vr);
}



void
VDPMSynthesizerViewerWidget::
vsplit(VHierarchyNodeHandle _node_handle, 
       VDPMMesh::VertexHandle vl, 
       VDPMMesh::VertexHandle vr)
{
  // refine
  VHierarchyNodeHandle  
    lchild_handle = vhierarchy_.lchild_handle(_node_handle),
    rchild_handle = vhierarchy_.rchild_handle(_node_handle);

  VDPMMesh::VertexHandle  v0 = vhierarchy_.vertex_handle(lchild_handle);
  VDPMMesh::VertexHandle  v1 = vhierarchy_.vertex_handle(rchild_handle);
  
  mesh_.vertex_split(v0, v1, vl, vr);
  mesh_.set_normal(v0, vhierarchy_.normal(lchild_handle));
  mesh_.set_normal(v1, vhierarchy_.normal(rchild_handle));
  mesh_.data(v0).set_vhierarchy_node_handle(lchild_handle);
  mesh_.data(v1).set_vhierarchy_node_handle(rchild_handle);
  mesh_.status(v0).set_deleted(false);
  mesh_.status(v1).set_deleted(false);
  
  vfront_.remove(_node_handle);
  vfront_.add(lchild_handle);
  vfront_.add(rchild_handle);  
}


void 
VDPMSynthesizerViewerWidget::
ecol(VHierarchyNodeHandle _node_handle, const VDPMMesh::HalfedgeHandle& v0v1)
{
  VHierarchyNodeHandle  
    lchild_handle = vhierarchy_.lchild_handle(_node_handle),
    rchild_handle = vhierarchy_.rchild_handle(_node_handle);

  VDPMMesh::VertexHandle  v0 = vhierarchy_.vertex_handle(lchild_handle);
  VDPMMesh::VertexHandle  v1 = vhierarchy_.vertex_handle(rchild_handle);

  // coarsen
  mesh_.collapse(v0v1);
  mesh_.set_normal(v1, vhierarchy_.normal(_node_handle));
  mesh_.data(v0).set_vhierarchy_node_handle(lchild_handle);
  mesh_.data(v1).set_vhierarchy_node_handle(_node_handle);
  mesh_.status(v0).set_deleted(false);
  mesh_.status(v1).set_deleted(false);

  vfront_.add(_node_handle);
  vfront_.remove(lchild_handle);
  vfront_.remove(rchild_handle);
}


bool
VDPMSynthesizerViewerWidget::
ecol_legal(VHierarchyNodeHandle _parent_handle, VDPMMesh::HalfedgeHandle& v0v1)
{
  VHierarchyNodeHandle
    lchild_handle = vhierarchy_.lchild_handle(_parent_handle),
    rchild_handle = vhierarchy_.rchild_handle(_parent_handle);

  // test whether lchild & rchild present in the current vfront
  if ( vfront_.is_active(lchild_handle) != true || 
       vfront_.is_active(rchild_handle) != true)
    return  false;  

  VDPMMesh::VertexHandle v0, v1;

  v0 = vhierarchy_.vertex_handle(lchild_handle);
  v1 = vhierarchy_.vertex_handle(rchild_handle);

  v0v1 = mesh_.find_halfedge(v0, v1);
  
  return  mesh_.is_collapse_ok(v0v1);
}

void
VDPMSynthesizerViewerWidget::
get_active_cuts(const VHierarchyNodeHandle _node_handle, 
		VDPMMesh::VertexHandle &vl, VDPMMesh::VertexHandle &vr)
{
  VDPMMesh::VertexVertexIter  vv_it;
  VHierarchyNodeHandle        nnode_handle;

  VHierarchyNodeIndex
    nnode_index, 
    fund_lcut_index = vhierarchy_.fund_lcut_index(_node_handle),
    fund_rcut_index = vhierarchy_.fund_rcut_index(_node_handle);

  vl = VDPMMesh::InvalidVertexHandle;
  vr = VDPMMesh::InvalidVertexHandle;
  
  for (vv_it=mesh_.vv_iter(vhierarchy_.vertex_handle(_node_handle)); 
       vv_it; ++vv_it)
  {
    nnode_handle = mesh_.data(vv_it.handle()).vhierarchy_node_handle();
    nnode_index = vhierarchy_.node_index(nnode_handle);

    if (vl == VDPMMesh::InvalidVertexHandle && 
	vhierarchy_.is_ancestor(nnode_index, fund_lcut_index) == true)
      vl = vv_it.handle();

    if (vr == VDPMMesh::InvalidVertexHandle && 
	vhierarchy_.is_ancestor(nnode_index, fund_rcut_index) == true)
      vr = vv_it.handle();

    /*if (vl == VDPMMesh::InvalidVertexHandle && nnode_index.is_ancestor_index(fund_lcut_index) == true)
      vl = vv_it.handle();
    if (vr == VDPMMesh::InvalidVertexHandle && nnode_index.is_ancestor_index(fund_rcut_index) == true)
      vr = vv_it.handle();*/

    if (vl != VDPMMesh::InvalidVertexHandle && 
	vr != VDPMMesh::InvalidVertexHandle)
      break;
  }
}


bool 
VDPMSynthesizerViewerWidget::
outside_view_frustum(const Vec3f &pos, float radius)
{  
#if 0
  return 
    (frustum_plane_[0].signed_distance(pos) < -radius) ||
    (frustum_plane_[1].signed_distance(pos) < -radius) ||
    (frustum_plane_[2].signed_distance(pos) < -radius) ||
    (frustum_plane_[3].signed_distance(pos) < -radius);
#else
  
  Plane3d   frustum_plane[4];
  
  viewing_parameters_.frustum_planes(frustum_plane);

  for (int i = 0; i < 4; i++) {
    if (frustum_plane[i].singed_distance(pos) < -radius)
      return true;
  }  
  return false;
#endif
}

bool 
VDPMSynthesizerViewerWidget::
oriented_away(float sin_square, float distance_square, float product_value)
{
#if 0
  return (product_value > 0)
    &&   ((product_value * product_value) > (distance_square * sin_square));
#else
  if (product_value > 0 && 
      product_value * product_value > distance_square * sin_square)
    return true;
  else
    return false;
#endif
}


bool 
VDPMSynthesizerViewerWidget::
screen_space_error(float mue_square, float sigma_square, 
		   float distance_square, float product_value)
{
#if 0
  float ks_ds = kappa_square_ * distance_square;
  float pv_pv = product_value * product_value;
  return (mue_square >= ks_ds) 
    ||   (sigma_square*( distance_square - pv_pv) >= ks_ds*distance_square);
#else
  if ((mue_square >= kappa_square_ * distance_square) || 
      (sigma_square * (distance_square - product_value * product_value) >= kappa_square_ * distance_square * distance_square))
    return	false;
  else
    return	true;
#endif
}

void
VDPMSynthesizerViewerWidget::
open_vd_prog_mesh(const char* _filename)
{
  unsigned int                    i;
  unsigned int                    value;
  unsigned int                    fvi[3];
  char                            fileformat[16];
  Vec3f                           p, normal;
  float                           radius, sin_square, mue_square, sigma_square;
  VHierarchyNodeHandleContainer   roots;
  VertexHandle                    vertex_handle;  
  VHierarchyNodeIndex             node_index;
  VHierarchyNodeIndex             lchild_node_index, rchild_node_index;
  VHierarchyNodeIndex             fund_lcut_index, fund_rcut_index;
  VHierarchyNodeHandle            node_handle;
  VHierarchyNodeHandle            lchild_node_handle, rchild_node_handle;  

  std::map<VHierarchyNodeIndex, VHierarchyNodeHandle> index2handle_map;

  std::ifstream ifs(_filename, std::ios::binary);

  if (!ifs)
  {
    std::cerr << "read error\n";
    exit(1);
  }

  //
  bool swap = Endian::local() != Endian::LSB;

  // read header  
  ifs.read(fileformat, 10); fileformat[10] = '\0';
  if (std::string(fileformat) != std::string("VDProgMesh"))
  {
    std::cerr << "Wrong file format.\n";
    ifs.close();
    exit(1);
  }

  IO::restore(ifs, n_base_vertices_, swap);
  IO::restore(ifs, n_base_faces_, swap);
  IO::restore(ifs, n_details_, swap);
  
  mesh_.clear();
  vfront_.clear();
  vhierarchy_.clear();

  vhierarchy_.set_num_roots(n_base_vertices_);

  // load base mesh
  for (i=0; i<n_base_vertices_; ++i)
  {
    IO::restore(ifs, p, swap);
    IO::restore(ifs, radius, swap);
    IO::restore(ifs, normal, swap);
    IO::restore(ifs, sin_square, swap);
    IO::restore(ifs, mue_square, swap);
    IO::restore(ifs, sigma_square, swap);

    vertex_handle = mesh_.add_vertex(p);
    node_index    = vhierarchy_.generate_node_index(i, 1);
    node_handle   = vhierarchy_.add_node();

    VHierarchyNode &node = vhierarchy_.node(node_handle);

    node.set_index(node_index);
    node.set_vertex_handle(vertex_handle);
    mesh_.data(vertex_handle).set_vhierarchy_node_handle(node_handle);
    
    node.set_radius(radius);
    node.set_normal(normal);
    node.set_sin_square(sin_square);
    node.set_mue_square(mue_square);
    node.set_sigma_square(sigma_square);
    mesh_.set_normal(vertex_handle, normal);

    index2handle_map[node_index] = node_handle;
    roots.push_back(node_handle);
  }
  vfront_.init(roots, n_details_);

  for (i=0; i<n_base_faces_; ++i)
  {
    IO::restore(ifs, fvi[0], swap);
    IO::restore(ifs, fvi[1], swap);
    IO::restore(ifs, fvi[2], swap);

    mesh_.add_face(mesh_.vertex_handle(fvi[0]),
		   mesh_.vertex_handle(fvi[1]),
		   mesh_.vertex_handle(fvi[2]));
  }

  // load details
  for (i=0; i<n_details_; ++i)
  {
    // position of v0
    IO::restore(ifs, p, swap);
    
    // vsplit info.
    IO::restore(ifs, value, swap);
    node_index = VHierarchyNodeIndex(value);

    IO::restore(ifs, value, swap);
    fund_lcut_index = VHierarchyNodeIndex(value);

    IO::restore(ifs, value, swap);
    fund_rcut_index = VHierarchyNodeIndex(value);


    node_handle = index2handle_map[node_index];
    vhierarchy_.make_children(node_handle);

    VHierarchyNode &node   = vhierarchy_.node(node_handle);
    VHierarchyNode &lchild = vhierarchy_.node(node.lchild_handle());
    VHierarchyNode &rchild = vhierarchy_.node(node.rchild_handle());

    node.set_fund_lcut(fund_lcut_index);
    node.set_fund_rcut(fund_rcut_index);
    
    vertex_handle = mesh_.add_vertex(p);
    lchild.set_vertex_handle(vertex_handle);
    rchild.set_vertex_handle(node.vertex_handle());    

    index2handle_map[lchild.node_index()] = node.lchild_handle();
    index2handle_map[rchild.node_index()] = node.rchild_handle();

    // view-dependent parameters
    IO::restore(ifs, radius, swap);
    IO::restore(ifs, normal, swap);
    IO::restore(ifs, sin_square, swap);
    IO::restore(ifs, mue_square, swap);
    IO::restore(ifs, sigma_square, swap);
    lchild.set_radius(radius);
    lchild.set_normal(normal);
    lchild.set_sin_square(sin_square);
    lchild.set_mue_square(mue_square);
    lchild.set_sigma_square(sigma_square);

    IO::restore(ifs, radius, swap);
    IO::restore(ifs, normal, swap);
    IO::restore(ifs, sin_square, swap);
    IO::restore(ifs, mue_square, swap);
    IO::restore(ifs, sigma_square, swap);
    rchild.set_radius(radius);
    rchild.set_normal(normal);
    rchild.set_sin_square(sin_square);
    rchild.set_mue_square(mue_square);
    rchild.set_sigma_square(sigma_square);
  }

  ifs.close();

  // update face and vertex normals
  mesh_.update_face_normals();

  // bounding box
  VDPMMesh::ConstVertexIter  
     vIt(mesh_.vertices_begin()), 
     vEnd(mesh_.vertices_end());

   VDPMMesh::Point bbMin, bbMax;

   bbMin = bbMax = mesh_.point(vIt);
   for (; vIt!=vEnd; ++vIt)
   {
      bbMin.minimize(mesh_.point(vIt));
      bbMax.maximize(mesh_.point(vIt));
   }
  
  // set center and radius
  set_scene_pos(0.5f*(bbMin + bbMax), 0.5*(bbMin - bbMax).norm());
    
  // info
  std::cerr << mesh_.n_vertices() << " vertices, "
    << mesh_.n_edges()    << " edge, "
    << mesh_.n_faces()    << " faces, "
    << n_details_ << " detail vertices\n";

  updateGL();
}




void VDPMSynthesizerViewerWidget::keyPressEvent(QKeyEvent* _event)
{
  switch (_event->key())
  {
    case Key_Home:
      updateGL();
      break;
    
    case Key_End:
      updateGL();
      break;
      
    case Key_Plus:
      viewing_parameters_.increase_tolerance();
      std::cout << "Scree-space error tolerance^2 is increased by " 
		<< viewing_parameters_.tolerance_square() << std::endl;
      updateGL();
      break;
      
    case Key_Minus:
      viewing_parameters_.decrease_tolerance();
      std::cout << "Screen-space error tolerance^2 is decreased by " 
		<< viewing_parameters_.tolerance_square() << std::endl;
      updateGL();
      break;
      
    case Key_A:
      adaptive_mode_ = !(adaptive_mode_);
      std::cout << "Adaptive refinement mode is " 
		<< (adaptive_mode_ ? "on" : "off") << std::endl;
      updateGL();
      break;

    case Key_O:
      qFilename_ = QFileDialog::getOpenFileName(0,"", "", "*.spm");
      open_vd_prog_mesh( qFilename_.toStdString().c_str() );
      break;
      
    default:
      MeshViewerWidget::keyPressEvent( _event );
  }
  
}





void
VDPMSynthesizerViewerWidget::
update_viewing_parameters()
{
  viewing_parameters_.set_modelview_matrix(modelview_matrix());
  viewing_parameters_.set_aspect((float) width()/ (float) height());
  viewing_parameters_.set_fovy(fovy());
  
  viewing_parameters_.update_viewing_configurations();
}


//=============================================================================
} // namespace OpenMesh
//=============================================================================