NCBI C++ Toolkit Cross Reference

  C++/src/gui/widgets/phylo_tree/phylo_tree_force.cpp


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 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
/* $Id: phylo_tree_force.cpp 37591 2017-01-25 19:05:32Z falkrb $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors: Bob Falk * * File Description: * */ #include <ncbi_pch.hpp> #include <corelib/ncbitime.hpp> #include <corelib/ncbistl.hpp> #include <gui/widgets/phylo_tree/phylo_tree_force.hpp> #include <gui/widgets/phylo_tree/phylo_tree_pane.hpp> #include <gui/widgets/phylo_tree/phylo_tree_ds.hpp> #include <gui/widgets/phylo_tree/phylo_tree_ps.hpp> #include <gui/opengl/glhelpers.hpp> #include <gui/widgets/gl/attrib_menu.hpp> #include <gui/utils/event_translator.hpp> #include <gui/utils/app_job_impl.hpp> #include <gui/utils/view_event.hpp> #include <math.h> BEGIN_NCBI_SCOPE #define PHYLO_RENDER_TIMER_ID 63001 BEGIN_EVENT_MAP(CPhyloForce, CEventHandler) ON_MESSAGE(CAppJobNotification, CAppJobNotification::eStateChanged, &CPhyloForce::OnAppJobNotification) ON_MESSAGE(CAppJobNotification, CAppJobNotification::eProgress, &CPhyloForce::OnAppJobProgress) END_EVENT_MAP() BEGIN_EVENT_TABLE(CPhyloForce, CPhyloRadial) EVT_TIMER(PHYLO_RENDER_TIMER_ID, CPhyloForce::OnTimer) END_EVENT_TABLE() CPhyloForce::CPhyloForce() : m_Timer(this, PHYLO_RENDER_TIMER_ID) , m_MainThreadPS(NULL) , m_UseMainThreadPhysics(true) { m_JobID = CAppJobDispatcher::eInvalidJobID; // If rendering within gbench, use thread for force calculations, // otherwise we are probably cgi and should use main thread. if (CGuiRegistry::GetInstance().HasField("GBENCH")) { m_UseMainThreadPhysics = false; } } CPhyloForce::CPhyloForce(double w, double h) : CPhyloRadial(w, h) , m_Timer(this, PHYLO_RENDER_TIMER_ID) , m_MainThreadPS(NULL) , m_UseMainThreadPhysics(true) { m_JobID = CAppJobDispatcher::eInvalidJobID; m_DistMode = false; // If rendering within gbench, use thread for force calculations, // otherwise we are probably cgi and should use main thread. if (CGuiRegistry::GetInstance().HasField("GBENCH")) { m_UseMainThreadPhysics = false; } #ifdef ATTRIB_MENU_SUPPORT // CAttribMenu& m = CAttribMenuInstance::GetInstance(); // CAttribMenu* sub_menu = m.AddSubMenuUnique("Force ext", this); // sub_menu->AddFloatReadOnly("Vis Left", &vis_left); // sub_menu->AddFloatReadOnly("Vis Right", &vis_right); // sub_menu->AddFloatReadOnly("Vis Bottom", &vis_bottom); // sub_menu->AddFloatReadOnly("Vis Top", &vis_top); // sub_menu->AddFloatReadOnly("Ext Left", &ext_left); // sub_menu->AddFloatReadOnly("Ext Right", &ext_right); // sub_menu->AddFloatReadOnly("Ext Bottom", &ext_bottom); // sub_menu->AddFloatReadOnly("Ext top", &ext_top); #endif } CPhyloForce::~CPhyloForce() { if (m_JobID != CAppJobDispatcher::eInvalidJobID) { CAppJobDispatcher& disp = CAppJobDispatcher::GetInstance(); disp.DeleteJob(m_JobID); } m_Timer.Stop(); } void CPhyloForce::x_Layout(CPhyloTreeDataSource& ds) { m_DistMode = false; // It is more efficient not to re-run the radial layout each time // if, for example, the user simply collapsed some nodes. Unfortuately // some user operations wind up setting all node position values to 0, // so be be sure we have to compute the radial tree first each time. //if (m_SwitchedLayout) CPhyloRadial::x_Layout(ds); Int4 leafs = ds.GetSize(); Int4 width = ds.GetWidth(); m_xStep = m_DimX / (double)width; // ystep is used to compute distance between nodes. Since nodes are forced apart, // about 4 times size gives a better approximation of average distance between nodes. m_yStep = 4.0*m_DimY / (double)((leafs>1)?(leafs-1):2); if (ds.GetNormDistance() > 0){ m_NormDistance = m_DimX / ds.GetNormDistance(); } m_Timer.Stop(); CPhyloTreePS::PhysicsParms p; bool retain_parms = false; // Set drawing options that are specific to the force layout CGlVboNode* edge_node = ds.GetModel().FindGeomNode("TreeEdges"); CGlVboNode* narrow_edge_node = ds.GetModel().FindGeomNode("NarrowTreeEdges"); if (edge_node != NULL && narrow_edge_node != NULL) { edge_node->GetState().Enable(GL_LINE_SMOOTH); edge_node->GetState().Enable(GL_BLEND); edge_node->GetState().BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); narrow_edge_node->GetState().Enable(GL_LINE_SMOOTH); narrow_edge_node->GetState().Enable(GL_BLEND); narrow_edge_node->GetState().BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); narrow_edge_node->GetState().ColorC(CRgbaColor(1.0f, 1.0f, 1.0f, 0.7f)); } CGlVboNode* filler_points_node = m_DS->GetModel().FindGeomNode("FillerPoints"); if (filler_points_node != NULL) filler_points_node->SetVisible(false); // Special case: no need for particle system if there is only // one node. if (ds.GetTree()->GetRoot().NumChildren() == 0) { TModelRect newRect = ds.GetBoundRect(); newRect.Inflate(1.0f, 1.0f); m_RasterRect = newRect; // This handles single node case too. ComputeViewingLimits(*m_pPane, m_ForceSquare); return; } if (!m_UseMainThreadPhysics) { try { CForceJob* j = m_Job.GetPointerOrNull(); // for debugging, copy the particle system parameters from the current // system to the new one (parameters may be updated via attribute // menus). if (j != NULL) { p = j->GetPS()->getPhysicParms(); retain_parms = true; } if (m_JobID != CAppJobDispatcher::eInvalidJobID) { CAppJobDispatcher& disp = CAppJobDispatcher::GetInstance(); IAppJob::EJobState state = disp.GetJobState(m_JobID); if (state == IAppJob::eRunning) disp.CancelJob(m_JobID); else disp.DeleteJob(m_JobID); m_Job.Reset(); } j = m_Job.GetPointerOrNull(); if (j == NULL) { // This is for debugging - if you run the force update several times, // you may change parameters each time and this retains those updates, // which probably occured through attribute menus, between updates. CForceJob *fj = new CForceJob(ds); if (retain_parms) { fj->GetPS()->SetPhysicsParms(p); #ifdef ATTRIB_MENU_SUPPORT CAttribMenuInstance::GetInstance().SynchToUserValue(); #endif } m_Job.Reset(fj); m_JobID = CAppJobDispatcher::GetInstance().StartJob(*m_Job, "ThreadPool", *this, 1, true); m_Timer.Start(200); } } // Unable to start a job - try to do physics in foreground catch (CAppJobException&) { m_UseMainThreadPhysics = true; } } else { if (!m_MainThreadPS) { m_MainThreadPS = new CPhyloTreePS(ds); // This makes the system end more quickly since the velocity threshold // below which we can stop is raised. m_MainThreadPS->SetVelocityThresholdK(0.2f); } else { m_MainThreadPS->Init(ds); } //m_Timer.Start(50); // no good for cgi... // This has to work for a batch, CGI version too - so there are // no progressive updates - we just want to iterate until it stabilizes // or we reach a maximum iteration count. for (int i=0; i<200 && !m_MainThreadPS->IsDone(); ++i) x_SingleThreadPhysics(); x_CalculateBoundary(); ComputeViewingLimits(*m_pPane, m_ForceSquare); // This is the rect for viewing limits used by cgi. m_RasterRect = m_pPane->GetModelLimitsRect(); return; } } void CPhyloForce::RemoveCurrentDataSource() { IPhyloTreeRender::RemoveCurrentDataSource(); StopLayout(); } bool CPhyloForce::Render(CGlPane& pane, CPhyloTreeDataSource& ds) { m_pPane = &pane; m_DS = &ds; // Is a job running? If so make sure we don't draw while the // particle system is syncing with the graphics model. Otherwise // just draw. if (m_JobID != CAppJobDispatcher::eInvalidJobID) { CForceJob* j = m_Job.GetPointerOrNull(); if (j != NULL ) { if (j->DataSynched() == CForceJob::eRenderInProgress) { j->SetDataSynched(CForceJob::eRenderInProgress); /// Need only to drop scale marker for force layout... IPhyloTreeRender::Render(pane,ds); j->SetDataSynched(CForceJob::eDataSynched); } // Unable to render since force thread is busy synchrnizing // the particle system data with the graphics data. else { //_TRACE("No render. Data synched: " << j->DataSynched() << " instead of: " << CForceJob::eDataSynched); return false; } } } else { IPhyloTreeRender::Render(pane,ds); } return true; } void CPhyloForce::StopLayout() { try { m_Timer.Stop(); if (m_JobID != CAppJobDispatcher::eInvalidJobID) { CAppJobDispatcher& disp = CAppJobDispatcher::GetInstance(); IAppJob::EJobState state = disp.GetJobState(m_JobID); if (state == IAppJob::eRunning) { disp.CancelJob(m_JobID); // Wait until job is really done (current iteration is finished) // to move on: int count = 0; while (count++<20 && disp.GetJobState(m_JobID) == IAppJob::eRunning) { wxMilliSleep(100); } } else { disp.DeleteJob(m_JobID); } m_JobID = CAppJobDispatcher::eInvalidJobID; m_Job.Reset(); } } catch (CAppJobException&) { _TRACE("Error deleting force layout job"); } m_JobID = CAppJobDispatcher::eInvalidJobID; m_Job.Reset(); } void CPhyloForce::x_SingleThreadPhysics() { if (!m_MainThreadPS) return; if (!m_MainThreadPS->IsDone()) { for (int j=0; j<10; ++j) { m_MainThreadPS->Update(); } m_MainThreadPS->UpdateAndSynch(); } else { m_Timer.Stop(); } } // Function to calculate node positions in tree. Essentially a recursive // function (implemented as a loop) using TreeDepthFirstEx. This prevents // very deep trees from the risk of stack overflow. class CCalcBoundary { public: typedef CPhyloTree::TTreeIdx TTreeIdx; public: CCalcBoundary(CPhyloForce* clad) : m_Clad(clad) , m_ComputeBoundary(0) { m_BoundaryPoints.push(CBoundaryPoints()); } ETreeTraverseCode operator()(CPhyloTree& tree, TTreeIdx node_idx, int delta) { CPhyloTree::TNodeType& node = tree[node_idx]; if (delta==1) { m_BoundaryPoints.push(CBoundaryPoints()); } if (delta==1 || delta==0) { if (node.GetValue().GetBoundedDisplay() == CPhyloNodeData::eBounded) m_ComputeBoundary += 1; m_Clad->InitLabel(&tree, node); if (node.IsLeafEx()) { CBoundaryPoints node_boundary_pts; m_Clad->x_ComputeNodeBoundary(&tree, node, node_boundary_pts, "ForceLayout"); if (node.GetValue().GetBoundedDisplay() == CPhyloNodeData::eBounded) m_ComputeBoundary -= 1; if (m_ComputeBoundary>0) { m_BoundaryPoints.top().AddBoundedPoints(node_boundary_pts); } } m_Clad->CalculateExtents(&tree, node); } else if (delta == -1) { // Each node that computes a boundary has to add in the points (and text rectangles) // for all nodes below it in the tree CBoundaryPoints pts = m_BoundaryPoints.top(); m_BoundaryPoints.pop(); m_Clad->x_ComputeNodeBoundary(&tree, node, pts, "ForceLayout"); if (node.GetValue().GetBoundedDisplay() == CPhyloNodeData::eBounded) { m_ComputeBoundary -= 1; } if (m_ComputeBoundary>0) { m_BoundaryPoints.top().AddBoundedPoints(pts); } } return eTreeTraverse; } private: CPhyloForce* m_Clad; int m_ComputeBoundary; stack<CBoundaryPoints> m_BoundaryPoints; }; void CPhyloForce::x_CalculateBoundary() { CCalcBoundary calc_tree(this); TreeDepthFirstEx(*m_DS->GetTree(), calc_tree); } void CPhyloForce::StartRendering(bool b) { IPhyloTreeRender::StartRendering(b); if (!b) { CAppJobDispatcher& disp = CAppJobDispatcher::GetInstance(); disp.DeleteJob(m_JobID); m_Timer.Stop(); } } /// handles "state changed" notification from CAppJobDispatcher void CPhyloForce::OnAppJobNotification(CEvent* evt) { CAppJobNotification* notn = dynamic_cast<CAppJobNotification*>(evt); _ASSERT(notn); IAppJob::EJobState state = IAppJob::eInvalid; int job_id = 0; if(notn) { job_id = notn->GetJobID(); if(job_id) { state = notn->GetState(); } } if (state != IAppJob::eRunning) { // Only redraw if job completed normally - not if it was cancellled. if (state == IAppJob::eCompleted) { // If job is Not NULL, we will only render if the status is eRenderInProgress if (!m_Job.IsNull()) m_Job->SetDataSynched(CForceJob::eRenderInProgress); x_CalculateBoundary(); ComputeViewingLimits(*m_pPane, m_ForceSquare); if (m_pHost != NULL) m_pHost->HMGH_UpdateLimits(true); } // Stop timer if job is not running. Can't just use job status since // when layout it called it stops the current job but then immediately // starts a new one, so this can be called from the cancelled job // even though the timer is needed for the newly created job. if (job_id == m_JobID) { m_Timer.Stop(); m_JobID = CAppJobDispatcher::eInvalidJobID; m_Job.Reset(); } } } void CPhyloForce::OnAppJobProgress(CEvent* evt) { CAppJobNotification* notn = dynamic_cast<CAppJobNotification*>(evt); _ASSERT(notn); if(notn) { // Could redraw on this with code below, but we use a separate timer // since this is called to infrequently to be helpful // (only called around 1/sec) /* CForceJob* j = m_Job.GetPointerOrNull(); if (j != NULL) { CMutexGuard guard(j->GetMutex()); x_CalculateBoundary(); ComputeViewingLimits(*m_pPane, m_ForceSquare); m_pHost->HMGH_UpdateLimits(true); //x_GetParent()->GetPort().SetModelLimitsRect(m_MatrixPane.GetModelLimitsRect()); //x_GetParent()->GetPort().SetVisibleRect(m_MatrixPane.GetVisibleRect()); m_pPane->OpenOrtho(); x_DrawBoundingAreas(m_DS->GetTree()); x_DrawTree( m_DS->GetTree() ); x_DrawNodes( m_DS->GetTree() ); m_pPane->Close(); } */ } } /////////////////////////////////////////////////////////////////////////////// void CPhyloForce::OnTimer(wxTimerEvent& /* evt */) { // If there is a valid (deform) job running and it has // synchronized the visual data with the physics data // since the last time called, recompute the tree boundaries // and redraw. if (!m_UseMainThreadPhysics && m_JobID != CAppJobDispatcher::eInvalidJobID) { CAppJobDispatcher& disp = CAppJobDispatcher::GetInstance(); IAppJob::EJobState state = disp.GetJobState(m_JobID); CForceJob* j = m_Job.GetPointerOrNull(); if (j != NULL ) { CMutexGuard guard(j->GetMutex()); if (state == IAppJob::eRunning && j->DataSynched() == CForceJob::eDataRefreshed) { j->SetDataSynched(CForceJob::eRenderInProgress); x_CalculateBoundary(); ComputeViewingLimits(*m_pPane, m_ForceSquare); m_pHost->HMGH_UpdateLimits(true); //_TRACE("Synched. " << j->DataSynched()); } } else if (m_UseMainThreadPhysics) { x_SingleThreadPhysics(); x_CalculateBoundary(); ComputeViewingLimits(*m_pPane, m_ForceSquare); if (m_pHost != NULL) m_pHost->HMGH_UpdateLimits(true); } } } // // Background job for running force update on a separate thread // /////////////////////////////////////////////////////////////////////////////// CForceJob::CForceJob(CPhyloTreeDataSource& ds) : m_PS(NULL) , m_DataSynched(eDataSynched) , m_UpdatesSinceLastSync(0) { m_PS = new CPhyloTreePS(ds); } CForceJob::~CForceJob() { delete m_PS; m_PS = NULL; } IAppJob::EJobState CForceJob::Run() { // Update data for rendering before starting so initial // image can show m_PS->UpdateAndSynch(); m_DataSynched = eRenderInProgress; // Loop a large number of times. Ideally, system will stop when // velocity drops (checked in x_Calculate) for (int i=0; i<20000 && !IsCanceled(); i++) x_Calculate(); return eCompleted; } void CForceJob::x_Calculate() { if (IsCanceled()) { return; } // updates should be timer based, but make sure that // a minimum number happen (otherwise it will slow down // progress of the particle system) //double i = 0.0f; CStopWatch sw; sw.Start(); // Do not update the system until at least 0.4 seconds has passed. while (sw.Elapsed() < 0.2) { m_PS->Update(); ++m_UpdatesSinceLastSync; } // Do a special update that synchronizes the tree data with the particle // system. To keep rendering (kind of ) smooth during updates of larger // trees, don't do this if there were less than a (low) number of iterations // since synch blocks rendering. if (!IsCanceled() && m_DataSynched != eRenderInProgress && m_UpdatesSinceLastSync > 5) { m_UpdatesSinceLastSync = 0; CMutexGuard guard(m_Mutex); m_DataSynched = eDataNotSynched; m_PS->UpdateAndSynch(); m_DataSynched = eDataRefreshed; /// If velocity has dropped low enough, stop system. if (m_PS->IsDone()) RequestCancel(); } } CForceJob::TDataSynchStatus CForceJob::DataSynched() { return m_DataSynched; } void CForceJob::SetDataSynched(TDataSynchStatus s) { CMutexGuard guard(m_Mutex); m_DataSynched = s; } CConstIRef<IAppJobProgress> CForceJob::GetProgress() { return CConstIRef<IAppJobProgress>(new CAppJobProgress(1.0, "Finished")); } CRef<CObject> CForceJob::GetResult() { CMutexGuard guard(m_Mutex); CRef<CObject> result(new CObject()); return result; } CConstIRef<IAppJobError> CForceJob::GetError() { return CConstIRef<IAppJobError>(new CAppJobError("No errors")); } string CForceJob::GetDescr() const { return "Building Force Layout"; } END_NCBI_SCOPE

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.