Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 * *
44 * $Revision: 21016 $ *
45 * $LastChangedBy: schultz $ *
46 * $Date: 2015-07-16 16:48:42 +0200 (Do, 16 Jul 2015) $ *
47 * *
48 \*===========================================================================*/
49 
50 #include "Ruler.hh"
51 
52 //------------------------------------------------------------------------------
53 Ruler::Ruler(BaseObjectData* _obj,const QString &_pluginName, unsigned index)
54 :pluginName_(_pluginName),
55  lineNodeName_(QString("Ruler-Plugin LineNode%1").arg(index).toStdString()),
56  textNodeName_(QString("Ruler-Plugin TextNode%1").arg(index).toStdString()),
57  textTransformNodeName_(QString("Ruler-Plugin TransformNode%1").arg(index).toStdString()),
58  lineNode_(0),
59  textNode_(0),
60  textTransformNode_(0),
61  offset_(),
62  obj_(_obj)
63 {
64  points_[0] = points_[1] = ACG::Vec3d(0.0,0.0,0.0);
65 
66  //create line, point, text and transformation nodes
67  if (!_obj->getAdditionalNode(lineNode_,_pluginName,lineNodeName_.c_str()))
68  {
69  lineNode_ = new ACG::SceneGraph::LineNode(ACG::SceneGraph::LineNode::LineSegmentsMode,
70  _obj->manipulatorNode(),lineNodeName_);
71  _obj->addAdditionalNode(lineNode_,_pluginName,lineNodeName_.c_str());
72  }
73 
74  if (!_obj->getAdditionalNode(textTransformNode_,_pluginName,textTransformNodeName_.c_str()))
75  {
76  textTransformNode_ = new ACG::SceneGraph::TransformNode(lineNode_,textTransformNodeName_.c_str());
77  _obj->addAdditionalNode(textTransformNode_,_pluginName,textTransformNodeName_.c_str());
78  }
79 
80  if (!_obj->getAdditionalNode(textNode_,_pluginName,textNodeName_.c_str()))
81  {
82  textNode_ = new ACG::SceneGraph::TextNode(textTransformNode_,textNodeName_,ACG::SceneGraph::TextNode::SCREEN_ALIGNED_STATIC_SIZE,true);
83  _obj->addAdditionalNode(textNode_,_pluginName,textNodeName_.c_str());
84 
85  }
86 }
87 //------------------------------------------------------------------------------
88 Ruler::~Ruler()
89 {
90  if (obj_->getAdditionalNode(textNode_,pluginName_,textNodeName_.c_str()))
91  {
92  obj_->removeAdditionalNode(textNode_,pluginName_,textNodeName_.c_str());
93  }
94  if (obj_->getAdditionalNode(textTransformNode_,pluginName_,textTransformNodeName_.c_str()))
95  {
96  obj_->removeAdditionalNode(textTransformNode_,pluginName_,textTransformNodeName_.c_str());
97  }
98  if (obj_->getAdditionalNode(lineNode_,pluginName_,lineNodeName_.c_str()))
99  {
100  obj_->removeAdditionalNode(lineNode_,pluginName_,lineNodeName_.c_str());
101  }
102 }
103 //------------------------------------------------------------------------------
104 void Ruler::setPoints(const ACG::Vec3d &_start,const ACG::Vec3d &_end)
105 {
106  points_[0] = _start;
107  points_[1] = _end;
108  updateNodes();
109 }
110 
111 void Ruler::setStartPoint(const ACG::Vec3d& _start)
112 {
113  points_[0] = _start;
114  updateNodes();
115 }
116 //------------------------------------------------------------------------------
117 void Ruler::setEndPoint(const ACG::Vec3d& _end)
118 {
119  points_[1] = _end;
120  updateNodes();
121 }
122 //------------------------------------------------------------------------------
123 
124 void Ruler::updateNodes()
125 {
126  ACG::Vec3d Point1 = points_[0];
127  ACG::Vec3d Point2 = points_[1];
128 
129  //creates the line
130  lineNode_->clear_points();
131  lineNode_->set_color(OpenMesh::Vec4f(1.0f,0.0f,0.0f,1.0f));
132  lineNode_->set_line_width(3);
133  lineNode_->add_line(Point1,Point2);
134  lineNode_->alwaysOnTop() = true;
135 
136  //set params for the text
137  ACG::Vec3d distVec = Point1 - Point2;
138  QString distanceStr = QString().number(distVec.length());
139  textNode_->setText(distanceStr.toStdString());
140  textNode_->multipassNodeSetActive(8, true);
141 
142  //translate
143  setTextOffset(offset_);
144 
145  emit updateView();
146 }
147 
148 void Ruler::setTextOffset(const ACG::Vec3d& offset)
149 {
150  offset_ = offset;
151  ACG::Vec3d distVec = points_[0] - points_[1];
152  ACG::Vec3d halfDist = distVec/2.f;
153  textTransformNode_->loadIdentity();
154 
155  textTransformNode_->translate((points_[0]-halfDist)+offset_);
156 }
bool removeAdditionalNode(NodeT *&_node, QString _pluginName, QString _nodeName, int _id=0)
remove an additional node from the object
void set_line_width(float _sz)
set line width (default: 1.0)
Definition: MeshNode2T.cc:459
void clear_points()
clear points/lines
Definition: LineNode.cc:115
void multipassNodeSetActive(const unsigned int _i, bool _active)
Set Node status to traverse in a specific pass.
Definition: BaseNode.cc:234
QtTranslationManipulatorNode * manipulatorNode()
bool getAdditionalNode(NodeT *&_node, QString _pluginName, QString _nodeName, int _id=0)
get an addition node from the object
VectorT< double, 3 > Vec3d
Definition: VectorT.hh:127
void setText(std::string _text)
sets the string that will be rendered
Definition: TextNode.cc:209
Ruler(BaseObjectData *_obj, const QString &_pluginName, unsigned _index)
Definition: Ruler.cc:53
bool & alwaysOnTop()
get and set always on top
Definition: LineNode.hh:202
void translate(const Vec3d &_v)
Add a translation to the current Transformation.
void set_color(const Vec4f &_c)
Override material node's set color function in order to locally add color.
Definition: LineNode.cc:136
bool addAdditionalNode(NodeT *_node, QString _pluginName, QString _nodeName, int _id=0)
add an additional node to the object
void add_line(const Vec3d &_v0, const Vec3d &_v1)
add line (for LineMode == LineSegmentsMode)
Definition: LineNode.cc:157
Text will always stay parallel to screen.
Definition: TextNode.hh:102