Developer Documentation
Ruler.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 #include "Ruler.hh"
44 
45 #include <ACG/Scenegraph/LineNode.hh>
46 #include <ACG/Scenegraph/SeparatorNode.hh>
47 #include <ACG/QtScenegraph/QtTranslationManipulatorNode.hh>
48 
49 //------------------------------------------------------------------------------
50 Ruler::Ruler(BaseObjectData* _obj,const QString &_pluginName, unsigned index)
51 :pluginName_(_pluginName),
52  lineNodeName_(QString("Ruler-Plugin LineNode%1").arg(index).toStdString()),
53  textNodeName_(QString("Ruler-Plugin TextNode%1").arg(index).toStdString()),
54  textTransformNodeName_(QString("Ruler-Plugin TransformNode%1").arg(index).toStdString()),
55  lineNode_(0),
56  textNode_(0),
57  textTransformNode_(0),
58  offset_(),
59  obj_(_obj)
60 {
61  points_[0] = points_[1] = ACG::Vec3d(0.0,0.0,0.0);
62 
63  //create line, point, text and transformation nodes
64  if (!_obj->getAdditionalNode(lineNode_,_pluginName,lineNodeName_.c_str()))
65  {
66  lineNode_ = new ACG::SceneGraph::LineNode(ACG::SceneGraph::LineNode::LineSegmentsMode,
67  _obj->manipulatorNode(),lineNodeName_);
68  _obj->addAdditionalNode(lineNode_,_pluginName,lineNodeName_.c_str());
69  }
70 
71  if (!_obj->getAdditionalNode(textTransformNode_,_pluginName,textTransformNodeName_.c_str()))
72  {
73  textTransformNode_ = new ACG::SceneGraph::TransformNode(lineNode_,textTransformNodeName_.c_str());
74  _obj->addAdditionalNode(textTransformNode_,_pluginName,textTransformNodeName_.c_str());
75  }
76 
77  if (!_obj->getAdditionalNode(textNode_,_pluginName,textNodeName_.c_str()))
78  {
79  textNode_ = new ACG::SceneGraph::TextNode(textTransformNode_,textNodeName_,ACG::SceneGraph::TextNode::SCREEN_ALIGNED_STATIC_SIZE,true);
80  _obj->addAdditionalNode(textNode_,_pluginName,textNodeName_.c_str());
81 
82  }
83 }
84 //------------------------------------------------------------------------------
85 Ruler::~Ruler()
86 {
87  if (obj_->getAdditionalNode(textNode_,pluginName_,textNodeName_.c_str()))
88  {
89  obj_->removeAdditionalNode(textNode_,pluginName_,textNodeName_.c_str());
90  }
91  if (obj_->getAdditionalNode(textTransformNode_,pluginName_,textTransformNodeName_.c_str()))
92  {
93  obj_->removeAdditionalNode(textTransformNode_,pluginName_,textTransformNodeName_.c_str());
94  }
95  if (obj_->getAdditionalNode(lineNode_,pluginName_,lineNodeName_.c_str()))
96  {
97  obj_->removeAdditionalNode(lineNode_,pluginName_,lineNodeName_.c_str());
98  }
99 }
100 //------------------------------------------------------------------------------
101 void Ruler::setPoints(const ACG::Vec3d &_start,const ACG::Vec3d &_end)
102 {
103  points_[0] = _start;
104  points_[1] = _end;
105  updateNodes();
106 }
107 
108 void Ruler::setStartPoint(const ACG::Vec3d& _start)
109 {
110  points_[0] = _start;
111  updateNodes();
112 }
113 //------------------------------------------------------------------------------
114 void Ruler::setEndPoint(const ACG::Vec3d& _end)
115 {
116  points_[1] = _end;
117  updateNodes();
118 }
119 //------------------------------------------------------------------------------
120 
121 void Ruler::updateNodes()
122 {
123  ACG::Vec3d Point1 = points_[0];
124  ACG::Vec3d Point2 = points_[1];
125 
126  //creates the line
127  lineNode_->clear_points();
128  lineNode_->set_color(OpenMesh::Vec4f(1.0f,0.0f,0.0f,1.0f));
129  lineNode_->set_line_width(3);
130  lineNode_->add_line(Point1,Point2);
131  lineNode_->alwaysOnTop() = true;
132 
133  //set params for the text
134  ACG::Vec3d distVec = Point1 - Point2;
135  QString distanceStr = QString().number(distVec.length());
136  textNode_->setText(distanceStr.toStdString());
137  textNode_->multipassNodeSetActive(8, true);
138 
139  //translate
140  setTextOffset(offset_);
141 
142  emit updateView();
143 }
144 
145 void Ruler::setTextOffset(const ACG::Vec3d& offset)
146 {
147  offset_ = offset;
148  ACG::Vec3d distVec = points_[0] - points_[1];
149  ACG::Vec3d halfDist = distVec/2.f;
150  textTransformNode_->loadIdentity();
151 
152  textTransformNode_->translate((points_[0]-halfDist)+offset_);
153 }
void translate(const Vec3d &_v)
Add a translation to the current Transformation.
void setText(std::string _text)
sets the string that will be rendered
Definition: TextNode.cc:204
Ruler(BaseObjectData *_obj, const QString &_pluginName, unsigned _index)
Definition: Ruler.cc:50
void clear_points()
clear points/lines
Definition: LineNode.cc:109
bool getAdditionalNode(NodeT *&_node, QString _pluginName, QString _nodeName, int _id=0)
get an addition node from the object
bool addAdditionalNode(NodeT *_node, QString _pluginName, QString _nodeName, int _id=0)
add an additional node to the object
bool removeAdditionalNode(NodeT *&_node, QString _pluginName, QString _nodeName, int _id=0)
remove an additional node from the object
void add_line(const Vec3d &_v0, const Vec3d &_v1)
add line (for LineMode == LineSegmentsMode)
Definition: LineNode.cc:151
VectorT< double, 3 > Vec3d
Definition: VectorT.hh:121
void set_line_width(float _sz)
set line width (default: 1.0)
Text will always stay parallel to screen.
Definition: TextNode.hh:93
auto length() const -> decltype(std::declval< VectorT< S, DIM >>().norm())
compute squared euclidean norm
Definition: Vector11T.hh:418
void set_color(const Vec4f &_c)
Override material node&#39;s set color function in order to locally add color.
Definition: LineNode.cc:130
void multipassNodeSetActive(const unsigned int _i, bool _active)
Set Node status to traverse in a specific pass.
Definition: BaseNode.cc:228
bool & alwaysOnTop()
get and set always on top
Definition: LineNode.hh:198
QtTranslationManipulatorNode * manipulatorNode()