RulerPlugin.cc 11.3 KB
Newer Older
Matthias Möller's avatar
Matthias Möller committed
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
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision: 14395 $                                                       *
*   $LastChangedBy: moeller $                                                *
*   $Date: 2012-04-16 14:46:02 +0200 (Mon, 16 Apr 2012) $                     *
*                                                                            *
\*===========================================================================*/


#include "RulerPlugin.hh"

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>

#include <MeshTools/MeshFunctions.hh>
#include <MeshTools/MeshInfoT.hh>
//------------------------------------------------------------------------------
RulerPlugin::RulerPlugin()
:
buttonAction_(0),
pickModeName_("MeasureDistance"),
lineNodeName_("Ruler Line"),
textNodeName_("Ruler Text"),
textTransformNodeName_("Ruler Text Transform"),
hitStage_(firstClick),
hitPoints_( ),
lineNode_(0),
textNode_(0),
textTransformNode_(0),
lineDrag_(-1)

{

}

//------------------------------------------------------------------------------
RulerPlugin::~RulerPlugin()
{

}
//------------------------------------------------------------------------------
void RulerPlugin::showDistance()
{
  ACG::Vec3d Point1 = hitPoints_[0];
  ACG::Vec3d Point2 = hitPoints_[1];

  lineNode_->clear_points();
  lineNode_->set_color(OpenMesh::Vec4f(1.0f,0.0f,0.0f,1.0f));
  lineNode_->set_line_width(3);
  lineNode_->add_line(Point1,Point2);

  ACG::Vec3d distVec = Point1 - Point2;
  QString distanceStr = QString().number((distVec).length());
  textNode_->setText(distanceStr.toStdString());
  textNode_->multipassNodeSetActive(8, true);
89
  textNode_->setSize(1);
Matthias Möller's avatar
Matthias Möller committed
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

  textTransformNode_->loadIdentity();
  textTransformNode_->translate(ACG::Vec3d(Point2.data()[0],Point2.data()[1],bbmax_.data()[2]));
  ACG::Vec3d halfDist = distVec/2.f;
  textTransformNode_->translate(ACG::Vec3d(halfDist.data()[0], halfDist.data()[1], 0.0));
  textTransformNode_->scale(std::max(distVec.length()*0.2, 1.0));

  emit updateView();
}

//------------------------------------------------------------------------------
void RulerPlugin::initializePlugin()
{
  QToolBar *button = new QToolBar();

  buttonAction_ = new QAction(tr("<B>Ruler</B><br> Display the distance between two points."),this);

  button->addAction(buttonAction_);
  buttonAction_->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"ruler.png"));
  buttonAction_->setCheckable(true);
  buttonAction_->setWhatsThis(tr("Measures the distance between two points.\n<LeftClick>: define/change the position.\n<DoubleClick>: reset the ruler."));

  buttonAction_->setChecked(false);

  connect(buttonAction_,SIGNAL(triggered()), this , SLOT(slotChangePickMode()) );
  emit addToolbar(button);
}

//------------------------------------------------------------------------------
void RulerPlugin::pluginsInitialized()
{
  emit addPickMode(pickModeName_);
}

//------------------------------------------------------------------------------
void RulerPlugin::slotMouseEvent(QMouseEvent* _event)
{
  if ( PluginFunctions::pickMode() == pickModeName_)
Matthias Möller's avatar
doc    
Matthias Möller committed
128
129
  {//set one of the points, depending on the hit state (first, second or modifying)

Matthias Möller's avatar
Matthias Möller committed
130
    if (_event->type() == QEvent::MouseButtonPress )
Matthias Möller's avatar
Matthias Möller committed
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
    {
      unsigned int node_idx, target_idx;
      OpenMesh::Vec3d hitPoint;

      // Get picked object's identifier by picking in scenegraph
      if ( PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING ,_event->pos(), node_idx, target_idx, &hitPoint) )
      {
        if (hitStage_ == firstClick)
        {
          //get or create the line
          BaseObjectData* object;
          if ( PluginFunctions::getPickedObject(node_idx, object) )
          {

            //create line, text and transformation nodes
            if (!object->getAdditionalNode(lineNode_,name(),lineNodeName_.c_str()))
            {
              lineNode_ = new ACG::SceneGraph::LineNode(ACG::SceneGraph::LineNode::LineSegmentsMode,
                  object->manipulatorNode(),lineNodeName_);
              object->addAdditionalNode(lineNode_,name(),lineNodeName_.c_str());
            }

            if (!object->getAdditionalNode(textTransformNode_,name(),textTransformNodeName_.c_str()))
            {
              textTransformNode_ = new ACG::SceneGraph::TransformNode(lineNode_,textTransformNodeName_.c_str());
              object->addAdditionalNode(textTransformNode_,name(),textTransformNodeName_.c_str());
            }

            if (!object->getAdditionalNode(textNode_,name(),textNodeName_.c_str()))
            {
161
              textNode_ = new ACG::SceneGraph::TextNode(textTransformNode_,textNodeName_,ACG::SceneGraph::TextNode::OBJECT_ALIGNED);
Matthias Möller's avatar
Matthias Möller committed
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
              object->addAdditionalNode(textNode_,name(),textNodeName_.c_str());

            }
          }//end creating nodes

          hitPoints_[1] = hitPoints_[0] = hitPoint;
          hitStage_ = secondClick;

          ACG::Vec3d bbmin;
          object->getBoundingBox(bbmin,bbmax_);

          showDistance();
          //enable mouse tracking for line drawing
          lineDrag_ = 1;
          emit setPickModeMouseTracking(pickModeName_,true);
        }
        else if (hitStage_ == secondClick)
        {
          lineDrag_ = -1;
          emit setPickModeMouseTracking(pickModeName_,false);
          //second position was clicked, so distance can be computed and displayed
          hitPoints_[1] = hitPoint;

          // show picked object
          showDistance();

          //after this stage, the points can be modified by a simple click
          hitStage_ = modifyPoint;
        }
        else if ((hitStage_ == modifyPoint))
        {
          //two modes:
          //first: no point was dragged, so we can compute and change the position
          //of the nearest one
          if (lineDrag_ < 0)
          {
            float firstDist = (hitPoints_[0] - hitPoint).length();
            float secondDist = (hitPoints_[1] - hitPoint).length();
            if (firstDist < secondDist)
              hitPoints_[0] = hitPoint;
            else
              hitPoints_[1] = hitPoint;
          }
          else
            //second: drag mode is enabled so we know the point
          {
            hitPoints_[lineDrag_] = hitPoint;
            lineDrag_ = -1;
          }
          showDistance();
        }
      }
      else // if nothing was picked
      {
        reset();
      }
    }
    else if (_event->type() == QEvent::MouseButtonPress)
Matthias Möller's avatar
doc    
Matthias Möller committed
220
221
    {//enable drag mode

Matthias Möller's avatar
Matthias Möller committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
      if (hitStage_ == modifyPoint)
      {
        //decides which point is the nearest one, so
        //it can be dragged
        unsigned int node_idx, target_idx;
        OpenMesh::Vec3d hitPoint;

        // Get picked object's identifier by picking in scenegraph
        if ( PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING ,_event->pos(), node_idx, target_idx, &hitPoint) )
        {
          float firstDist = (hitPoints_[0] - hitPoint).length();
          float secondDist = (hitPoints_[1] - hitPoint).length();
          if (firstDist < secondDist)
            lineDrag_ = 0;
          else
            lineDrag_ = 1;

          showDistance();
        }
      }
    }
    else if (_event->type() == QEvent::MouseMove && lineDrag_ >= 0)
Matthias Möller's avatar
doc    
Matthias Möller committed
244
245
    {//mouse moved and drag mode is enabled

Matthias Möller's avatar
Matthias Möller committed
246
247
248
249
250
251
252
253
254
      unsigned int node_idx, target_idx;
      OpenMesh::Vec3d hitPoint;

      // Get picked object's identifier by picking in scenegraph
      if ( !PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING ,_event->pos(), node_idx, target_idx, &hitPoint) )
      {
        QPoint position = _event->pos();
        ACG::Vec3d viewCoords = ACG::Vec3d(position.x(), PluginFunctions::viewerProperties().glState().context_height() - position.y(), 0.5);
        hitPoint = PluginFunctions::viewerProperties().glState().unproject(viewCoords);
Matthias Möller's avatar
Matthias Möller committed
255
        hitPoints_[lineDrag_] = ACG::Vec3d(hitPoint.data()[0], hitPoint.data()[1], hitPoints_[lineDrag_].data()[2] );
Matthias Möller's avatar
Matthias Möller committed
256
      }
Matthias Möller's avatar
Matthias Möller committed
257
258
      else
        hitPoints_[lineDrag_] = hitPoint;
Matthias Möller's avatar
Matthias Möller committed
259
260
261
      showDistance();
    }
    else if (_event->type() == QEvent::MouseButtonDblClick)
Matthias Möller's avatar
doc    
Matthias Möller committed
262
    {//reset
Matthias Möller's avatar
Matthias Möller committed
263
264
265
266
267
268
269
270
271
272
      reset();
    }

  }
}

//------------------------------------------------------------------------------
void RulerPlugin::reset()
{
  hitStage_ = firstClick;
Matthias Möller's avatar
Matthias Möller committed
273
274
275
276
  if (lineNode_)
    lineNode_->clear_points();
  if (textNode_)
    textNode_->setText("");
Matthias Möller's avatar
Matthias Möller committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
}

//------------------------------------------------------------------------------
void RulerPlugin::slotChangePickMode()
{
  PluginFunctions::actionMode( Viewer::PickingMode );
  PluginFunctions::pickMode(pickModeName_);

}

//------------------------------------------------------------------------------
void RulerPlugin::slotPickModeChanged(const std::string& _mode)
{
  buttonAction_->setChecked(_mode == pickModeName_);
}

Q_EXPORT_PLUGIN2( rulerPlugin , RulerPlugin );