Developer Documentation
SlicePlugin.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$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 #include "SlicePlugin.hh"
52 
55 
56 SlicePlugin::SlicePlugin() :
57  tool_(0),
58  toolIcon_(0),
59  node_(0)
60 {
61 
62 }
63 
64 void SlicePlugin::initializePlugin(){
65  //init the slice node
66  node_ = new ACG::SceneGraph::ClippingNode(0,"Clipping Node");
67 
68 
70 
71  node_->set_status( ACG::SceneGraph::BaseNode::HideNode );
72  node_->setMultipassStatus(ACG::SceneGraph::BaseNode::NOPASS);
73 
74  tool_ = new SliceToolBox();
75 
76  QSize size(300, 300);
77  tool_->resize(size);
78 
79  QButtonGroup* bbGroup = new QButtonGroup();
80  bbGroup->setExclusive( true );
81  bbGroup->addButton( tool_->radioAll );
82  bbGroup->addButton( tool_->radioTarget );
83 
84  QButtonGroup* axisGroup = new QButtonGroup();
85  axisGroup->setExclusive( true );
86  axisGroup->addButton( tool_->radioX );
87  axisGroup->addButton( tool_->radioY );
88  axisGroup->addButton( tool_->radioZ );
89 
90  tool_->radioAll->setChecked( true );
91  tool_->radioX->setChecked( true );
92 
93  connect(tool_->radioAll, SIGNAL( released() ), this, SLOT( updateSlice() ) );
94  connect(tool_->radioTarget, SIGNAL( released() ), this, SLOT( updateSlice() ) );
95  connect(tool_->resetButton, SIGNAL( released() ), this, SLOT( resetParameters() ) );
96  connect(tool_->enabled, SIGNAL( released() ), this, SLOT( updateSlice() ) );
97  connect(tool_->radioX, SIGNAL( released() ), this, SLOT( updateSlice() ) );
98  connect(tool_->radioY, SIGNAL( released() ), this, SLOT( updateSlice() ) );
99  connect(tool_->radioZ, SIGNAL( released() ), this, SLOT( updateSlice() ) );
100  connect(tool_->posSlider, SIGNAL( valueChanged(int) ), this, SLOT( updateSlice(int) ) );
101  connect(tool_->sizeSlider, SIGNAL( valueChanged(int) ), this, SLOT( updateSlice(int) ) );
102 
103  toolIcon_ = new QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"slice.png");
104  emit addToolbox( tr("Slice") , tool_, toolIcon_);
105 }
106 
107 void SlicePlugin::resetParameters(){
108  tool_->posSlider->setValue(0);
109  tool_->sizeSlider->setValue(102);
110  updateSlice();
111 }
112 
113 void SlicePlugin::updateSlice(int /*bla*/){
114  updateSlice();
115 }
116 
117 void SlicePlugin::updateSlice(){
118 
119  if ( tool_->enabled->isChecked() ) {
120  node_->set_status( ACG::SceneGraph::BaseNode::Active );
121  node_->setMultipassStatus(BaseNode::ALLPASSES);
122  } else {
123  node_->set_status( ACG::SceneGraph::BaseNode::HideNode );
124  node_->setMultipassStatus(ACG::SceneGraph::BaseNode::NOPASS);
125  }
126 
127  if ( tool_->enabled->isChecked() ){
128 
129  ACG::Vec3d bbmin;
130  ACG::Vec3d bbmax;
131 
132  getBoundingBox( bbmin, bbmax);
133 
134  ACG::Vec3d center = (bbmin + bbmax) * 0.5;
135  ACG::Vec3f pos (center[0], center[1], center[2]);
136 
137  //get the normal
138  ACG::Vec3f normal(1.0, 0.0, 0.0);
139  float eps;
140  float offset = 0.0;
141 
142  eps = tool_->sizeSlider->value() / 100.0;
143 
144  if (eps == 0.0)
145  eps = 0.01f;
146 
147  if (tool_->radioX->isChecked()){
148  normal = ACG::Vec3f(1.0, 0.0, 0.0);
149  eps *= (bbmax[0] - bbmin[0]);
150  offset = (bbmax[0] - bbmin[0]) * 0.5;
151  }else
152  if (tool_->radioY->isChecked()){
153  normal = ACG::Vec3f(0.0, 1.0, 0.0);
154  eps *= (bbmax[1] - bbmin[1]);
155  offset = (bbmax[1] - bbmin[1]) * 0.5;
156  }else
157  if (tool_->radioZ->isChecked()){
158  normal = ACG::Vec3f(0.0, 0.0, 1.0);
159  eps *= (bbmax[2] - bbmin[2]);
160  offset = (bbmax[2] - bbmin[2]) * 0.5;
161  }
162 
163  pos += normal * ( (float)tool_->posSlider->value() / 100.0 ) * (offset + 0.1); //0.1 is just a little workarround
164 
165  node_->set_plane(pos, normal, eps);
166  }
167  emit updateView();
168 }
169 
170 void SlicePlugin::getBoundingBox(ACG::Vec3d& bbmin, ACG::Vec3d& bbmax){
171 
172  bool firstRound = true;
173 
175 
176  if (tool_->radioTarget->isChecked())
177  restriction = PluginFunctions::TARGET_OBJECTS;
178  else
179  restriction = PluginFunctions::ALL_OBJECTS;
180 
181  for ( PluginFunctions::ObjectIterator o_it(restriction,DataType(DATA_ALL)) ;
182  o_it != PluginFunctions::objectsEnd(); ++o_it) {
183  // get scene size
184  ACG::Vec3d cur_min;
185  ACG::Vec3d cur_max;
186 
187  o_it->getBoundingBox(cur_min, cur_max);
188 
189  if (firstRound){
190  bbmin = cur_min;
191  bbmax = cur_max;
192  firstRound = false;
193  }else{
194  bbmin[0] = std::min( bbmin[0], cur_min[0]);
195  bbmin[1] = std::min( bbmin[1], cur_min[1]);
196  bbmin[2] = std::min( bbmin[2], cur_min[2]);
197  bbmax[0] = std::max( bbmax[0], cur_max[0]);
198  bbmax[1] = std::max( bbmax[1], cur_max[1]);
199  bbmax[2] = std::max( bbmax[2], cur_max[2]);
200  }
201  }
202 
203  if ((bbmin[0] > bbmax[0]) || (bbmin[1] > bbmax[1]) || (bbmin[2] > bbmax[2]))
204  std::cerr << "Error while computing bounding box!";
205 
206 }
207 
208 #if QT_VERSION < 0x050000
209  Q_EXPORT_PLUGIN2( slicePlugin , SlicePlugin );
210 #endif
211 
QStringList IteratorRestriction
Iterable object range.
Predefined datatypes.
Definition: DataTypes.hh:96
Draw node & children.
Definition: BaseNode.hh:423
const QStringList TARGET_OBJECTS("target")
Iterable object range.
const QStringList ALL_OBJECTS
Iterable object range.
VectorT< float, 3 > Vec3f
Definition: VectorT.hh:125
const DataType DATA_ALL(UINT_MAX)
Identifier for all available objects.
Hide this node, but draw children.
Definition: BaseNode.hh:425
void addObjectRenderingNode(ACG::SceneGraph::BaseNode *_node)
Add scenegraph node modifing object rendering.
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.