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