bpp-phyl  2.2.0
AbstractTreeDrawing.cpp
Go to the documentation of this file.
1 //
2 // File: AbstractTreeDrawing.cpp
3 // Created by: Julien Dutheil
4 // Created on: Sun Oct 16 10:06 2006
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 16, 2004)
9 
10 This software is a computer program whose purpose is to provide
11 graphic components to develop bioinformatics applications.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #include "AbstractTreeDrawing.h"
41 #include "../TreeTemplateTools.h"
42 #include "../TreeTools.h"
43 
44 #include <Bpp/Text/TextTools.h>
45 #include <Bpp/Numeric/Number.h>
46 
47 // From the STL:
48 #include <algorithm>
49 
50 using namespace bpp;
51 using namespace std;
52 
54 
55 Point2D<double> AbstractTreeDrawing::getNodePosition(int nodeId) const
57 {
58  vector<INode *> nodes = tree_->getNodes();
59  for(unsigned int i = 0; i < nodes.size(); i++)
60  {
61  INode * node = nodes[i];
62  if(node->getId() == nodeId)
63  {
64  return node->getInfos().getPosition();
65  }
66  }
67  throw NodeNotFoundException("AbstractTreeDrawing::getNodePosition.", TextTools::toString(nodeId));
68 }
69 
70 int AbstractTreeDrawing::getNodeAt(const Point2D<double>& position) const
72 {
73  vector<INode *> nodes = tree_->getNodes();
74  for(unsigned int i = 0; i < nodes.size(); i++)
75  {
76  INode * node = nodes[i];
77  Point2D<double> nodePos = node->getInfos().getPosition();
78  if(belongsTo(position, nodePos))
79  {
80  return node->getId();
81  }
82  }
83  throw NodeNotFoundException("AbstractTreeDrawing::getNode.", "");
84 }
85 
86 bool AbstractTreeDrawing::belongsTo(const Point2D<double>& p1, const Point2D<double>& p2) const
87 {
88  return (p1.getX() >= p2.getX() - settings_->pointArea && p1.getX() <= p2.getX() + settings_->pointArea
89  && p1.getY() >= p2.getY() - settings_->pointArea && p1.getY() <= p2.getY() + settings_->pointArea);
90 }
91 
92 void AbstractTreeDrawing::drawAtNode(GraphicDevice& gDevice, const INode& node, const string& text, double xOffset, double yOffset, short hpos, short vpos, double angle) const
93 {
94  gDevice.drawText(node.getInfos().getX() + xOffset * xUnit_, node.getInfos().getY() + yOffset * yUnit_, text, hpos, vpos, angle);
95 }
96 
97 void AbstractTreeDrawing::drawAtBranch(GraphicDevice& gDevice, const INode& node, const string& text, double xOffset, double yOffset, short hpos, short vpos, double angle) const
98 {
99  if (node.hasFather()) {
100  gDevice.drawText((node.getInfos().getX() + node.getFather()->getInfos().getX()) / 2. + xOffset * xUnit_, node.getInfos().getY() + yOffset * yUnit_, text, hpos, vpos, angle);
101  }
102 }
103 
const NodeTemplate< NodeInfos > * getFather() const
Get the father of this node is there is one.
Definition: NodeTemplate.h:141
STL namespace.
Point2D< double > getNodePosition(int nodeId) const
Get the position of a node.
virtual bool hasFather() const
Tell if this node has a father node.
Definition: Node.h:379
virtual void drawAtNode(GraphicDevice &gDevice, const INode &node, const std::string &text, double xOffset=0, double yOffset=0, short hpos=GraphicDevice::TEXT_HORIZONTAL_LEFT, short vpos=GraphicDevice::TEXT_VERTICAL_CENTER, double angle=0) const
Draw some text at a particular node position.
virtual int getId() const
Get the node&#39;s id.
Definition: Node.h:203
A set of options to tune the display of a TreeDrawing object.
Definition: TreeDrawing.h:61
Exception thrown when something is wrong with a particular node.
virtual void drawAtBranch(GraphicDevice &gDevice, const INode &node, const std::string &text, double xOffset=0, double yOffset=0, short hpos=GraphicDevice::TEXT_HORIZONTAL_LEFT, short vpos=GraphicDevice::TEXT_VERTICAL_CENTER, double angle=0) const
Draw some text at a particular branch position.
bool belongsTo(const Point2D< double > &p1, const Point2D< double > &p2) const
Utilitary function, telling if a point belongs to a specified area.
int getNodeAt(const Point2D< double > &position) const
Get the node corresponding to a position on the device.
virtual const NodeInfos & getInfos() const
Definition: NodeTemplate.h:179
static const TreeDrawingSettings DEFAULT_SETTINGS
The NodeTemplate class.
Definition: NodeTemplate.h:73