NCBI C++ ToolKit
phylo_tree_ps.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef __GUI_WIDGETS_PHYLO_TREE___PHYLO_TREE_PS__HPP
2 #define __GUI_WIDGETS_PHYLO_TREE___PHYLO_TREE_PS__HPP
3 
4 /* $Id: phylo_tree_ps.hpp 33847 2015-09-21 12:59:45Z falkrb $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Bob Falk
30  *
31  * File Description:
32  *
33  */
34 
35 #include <corelib/ncbiobj.hpp>
36 #include <gui/gui_export.h>
37 
38 #include <gui/utils/vect2.hpp>
39 #include <gui/utils/vect3.hpp>
40 
43 
44 #include <float.h>
45 
47 
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// class CPhyloTreePS
53 {
54 public:
56 
57  //
58  // Physics parameters - may be updated by another thread
59  //
60  struct PhysicsParms {
61  PhysicsParms();
62 
64  float m_Step;
65  float m_Damping;
66  float m_EdgeK;
68  /// factor to scale final velocity threshold
70  };
71 
72 public:
74  ~CPhyloTreePS();
75 
76  void Clear();
77 
78  void Init(CPhyloTreeDataSource& ds);
79 
80  ///
81  /// Get/Set particle system parameters. Two sets of parms are specified
82  /// (volatile and safe) so that other threads can access parms while
83  /// particle system is running.
84  ///
85 
86  /// Get/Set node-node repulsion force (between all nodes)
88  { m_PhysicsParmsVolatile.m_ElectricalRepulsion = r; }
89  float GetElectricalRepulsion() const
90  { return m_PhysicsParmsVolatile.m_ElectricalRepulsion; }
91 
92  /// Get/Set timestep
93  void SetStep(float s) { m_PhysicsParmsVolatile.m_Step = s; }
94  float GetStep() const { return m_PhysicsParmsVolatile.m_Step; }
95 
96  /// Get/Set damping parameter
97  void SetDamping(float d) { m_PhysicsParmsVolatile.m_Damping = d; }
98  float GetDamping() const { return m_PhysicsParmsVolatile.m_Damping; }
99 
100  /// Get/Set edge stiffness
101  void SetEdgeK(float k) { m_PhysicsParmsVolatile.m_EdgeK = k; }
102  float GetEdgeK() const { return m_PhysicsParmsVolatile.m_EdgeK; }
103 
104  /// Get/Set maximum effective repulsion distance
105  void SetRepulsionDist(float d)
106  { m_PhysicsParmsVolatile.m_RepulsionDist = d; }
107  float GetRepulsionDist() const
108  { return m_PhysicsParmsVolatile.m_RepulsionDist; }
109 
110  /// Get/Set scaling factor for velocity threshold below which system stops
111  void SetVelocityThresholdK(float k)
112  { m_PhysicsParmsVolatile.m_VelocityThresholdK = k; }
113  float GetVelocityThresholdK() const
114  { return m_PhysicsParmsVolatile.m_VelocityThresholdK; }
115 
116  /// Get/Set all the current physics parms
117  void SetPhysicsParms(const PhysicsParms& p) { m_PhysicsParmsVolatile = p; }
118  PhysicsParms getPhysicParms() { return m_PhysicsParmsVolatile; }
119 
120  /// Get max velocity from last call to UpdateAndSynch
121  float GetMaxVelocity() const { return m_MaxVelocity; }
122 
123  /// Return true if velocity has dropped below a threshold that
124  /// indicates the system is stable (and doesn't need to keep running)
125  bool IsDone() const { return m_IsDone; }
126 
127  /// Calculate forces for all nodes
128  void CalcForces();
129 
130  /// Calculate force then update positions
131  void Update();
132 
133  /// Calculate forces, update positions, and update underlying tree
134  void UpdateAndSynch();
135 
136  /// Visualize graph - debug rendering
137  void Draw();
138 
139  /// Data structure for an edge between two nodes
140  struct Edge {
141  Edge() : from_idx(-1), to_idx(-1), len(0.0f), rest_len_inv(0.0f), k(1.0f) {}
142  Edge(int idx1, int idx2)
143  : from_idx(idx1), to_idx(idx2), len(0.0f), rest_len_inv(0.0f), k(1.0f) {}
144 
145  int from_idx;
146  int to_idx;
147 
148  float len;
150  float k;
151  };
152 
153  /// Data structure for a node in the particle system
154  struct Node {
155  Node()
156  : tree_node_idx(CPhyloTree::Null())
157  , constrained(0.0f) {}
158 
163  bool is_leaf;
164 
165  float constrained;
166  };
167 
168  /// Default length for all edges in system
169  float& GetDefaultEdgeLen() { return m_DefaultEdgeLen; }
170 
171  /// Tracks bounding rectangle for all nodes
172  TVec& GetMinPos() { return m_MinPos; }
173  TVec& GetMaxPos() { return m_MaxPos; }
174  /// Set of all nodes
175  std::vector<Node>& GetNodes() { return m_Nodes; }
176  /// Set of all edges
177  std::vector<Edge>& GetEdges() { return m_Edges; }
178 
179 protected:
180 
181  /// Create particle system from tree - ignore collapsed
182  void x_Init(CPhyloTree* tree);
183  /// Update spatial subdivision of nodes
184  void x_UpdateVoxels();
185  /// Apply forces between nodes based on defined neighborhood size
186  void x_ApplyRepulsiveForcesHashed();
187  /// Apply repulsive forces between all nodes
188  void x_ApplyRepulsiveForces();
189  /// Called compute forces between nodes in 2 cells
190  void x_ApplyNeighborCellForces(std::vector<int>& cell_nodes,
191  const CVect2<int>& adjacent_idx);
192 
193  /// Set of all nodes
194  std::vector<Node> m_Nodes;
195  /// Set of all edges
196  std::vector<Edge> m_Edges;
197 
198  /// Grid that keeps track of adjacent nodes
200 
201  /// (potentially) updated from other thread(s)
203  /// Updated from m_PhysicsPamsVolatile when safe to do so
205 
206  /// Multiplier for m_PhysicsParmsSafe.m_Step to allow step size to be
207  /// adaptively lowered if system appears unstable.
209 
210  /// Square of current effective repulsion distance
212  /// Inverse of repulsion dist * electrical repulsion factor
214 
215  ///
216  /// For timer values
217  ///
218 
224 
230 
231  /// The maximum velocity is the maximum node velocity during the last
232  /// call to UpdateAndSynch()
234  /// Maximum velocity in previous update
236  /// If true, system has slowed down to the point where continuing to
237  /// update it is not necessary. Based on max velocity and tree size
238  bool m_IsDone;
239 
240 
241  /// Default length for all edges in system
243 
244  /// Root node of tree
246 
247  /// Tracks bounding rectangle for all nodes
250 
251  /// Only used for debugging
252  std::vector<CVect2<int> > m_ValidateGrid;
253  std::vector<CVect2<int> > m_ValidateNsq;
254 
255  /// For debug-drawing
258 
259 private:
260  // default ctor not available
262 };
263 
264 
266 
267 #endif //__GUI_WIDGETS_PHYLO_TREE___PHYLO_TREE_PS__HPP
class CPhyloTreePS
float m_RepulsionDist2
Square of current effective repulsion distance.
void SetPhysicsParms(const PhysicsParms &p)
Get/Set all the current physics parms.
float GetRepulsionDist() const
float m_RepulsionInv_x_ElectricalRepulsion
Inverse of repulsion dist * electrical repulsion factor.
void SetStep(float s)
Get/Set timestep.
PhysicsParms m_PhysicsParmsSafe
Updated from m_PhysicsPamsVolatile when safe to do so.
float m_MaxVelocity
The maximum velocity is the maximum node velocity during the last call to UpdateAndSynch()
float m_edge_forces_t
For timer values.
void SetVelocityThresholdK(float k)
Get/Set scaling factor for velocity threshold below which system stops.
float m_bound_update_safe_t
PhysicsParms getPhysicParms()
PhysicsParms m_PhysicsParmsVolatile
(potentially) updated from other thread(s)
void SetElectricalRepulsion(float r)
Get/Set particle system parameters.
void SetDamping(float d)
Get/Set damping parameter.
int m_node_node_interactions_safe_t
std::vector< CVect2< int > > m_ValidateGrid
Only used for debugging.
float m_edge_forces_safe_t
bool m_IsDone
If true, system has slowed down to the point where continuing to update it is not necessary.
CVect2< float > TVec
std::vector< Node > m_Nodes
Set of all nodes.
float GetVelocityThresholdK() const
float GetDamping() const
float m_DefaultEdgeLen
Default length for all edges in system.
TVec & GetMinPos()
Tracks bounding rectangle for all nodes.
float m_AdaptiveStep
Multiplier for m_PhysicsParmsSafe.m_Step to allow step size to be adaptively lowered if system appear...
float m_integrate_safe_t
void SetEdgeK(float k)
Get/Set edge stiffness.
float m_node_forces_safe_t
float GetMaxVelocity() const
Get max velocity from last call to UpdateAndSynch.
TVec m_MinPos
Tracks bounding rectangle for all nodes.
float m_LogDistMax
For debug-drawing.
TVec & GetMaxPos()
void SetRepulsionDist(float d)
Get/Set maximum effective repulsion distance.
float m_bound_update_t
std::vector< Edge > m_Edges
Set of all edges.
int m_node_node_interactions_t
float & GetDefaultEdgeLen()
Default length for all edges in system.
std::vector< Node > & GetNodes()
Set of all nodes.
std::vector< Edge > & GetEdges()
Set of all edges.
bool IsDone() const
Return true if velocity has dropped below a threshold that indicates the system is stable (and doesn'...
float GetElectricalRepulsion() const
float m_PrevMaxVelocity
Maximum velocity in previous update.
CSpatialHash2D< std::vector< int > > m_NodeGrid
Grid that keeps track of adjacent nodes.
float GetStep() const
CPhyloTree * m_Tree
Root node of tree.
float GetEdgeK() const
std::vector< CVect2< int > > m_ValidateNsq
Tree subclass also has functions and data needed for rendering and selection.
Definition: phylo_tree.hpp:52
class CSpatialHash2D Class for simple 2D spatial organization that allows for some collision checking...
static void Init(void)
Definition: cursor6.c:76
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define NCBI_GUIWIDGETS_PHYLO_TREE_EXPORT
Definition: gui_export.h:535
Defines to provide correct exporting from DLLs in Windows.
int len
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
Data structure for an edge between two nodes.
Edge(int idx1, int idx2)
Data structure for a node in the particle system.
CPhyloTree::TTreeIdx tree_node_idx
float m_VelocityThresholdK
factor to scale final velocity threshold
Modified on Sat Mar 02 10:54:07 2024 by modify_doxy.py rev. 669887