SlicePlugin.cc 4.43 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius 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
89
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
128
129
130
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

#include <QtGui>

#include "SlicePlugin.hh"

#include <iostream>

#include "OpenFlipper/BasePlugin/PluginFunctions.hh"


void SlicePlugin::initializePlugin(){
  //init the slice node
  node_ = new ACG::SceneGraph::ClippingNode(0,"Clipping Node");

 
  PluginFunctions::addObjectRenderingNode( node_ );

  node_->set_status( ACG::SceneGraph::BaseNode::HideNode );
  node_->setMultipassStatus(ACG::SceneGraph::BaseNode::NOPASS);
  
  tool_ = new SliceToolBox();
  
  QSize size(300, 300);
  tool_->resize(size);
  
  bbGroup = new QButtonGroup();
  bbGroup->setExclusive( true );
  bbGroup->addButton( tool_->radioAll );
  bbGroup->addButton( tool_->radioTarget );
  
  axisGroup = new QButtonGroup();
  axisGroup->setExclusive( true );
  axisGroup->addButton( tool_->radioX );
  axisGroup->addButton( tool_->radioY );
  axisGroup->addButton( tool_->radioZ );
  
  tool_->radioAll->setChecked( true );
  tool_->radioX->setChecked( true );
  
  connect(tool_->radioAll, SIGNAL( released() ), this, SLOT( updateSlice() ) );
  connect(tool_->radioTarget, SIGNAL( released() ), this, SLOT( updateSlice() ) );
  connect(tool_->resetButton, SIGNAL( released() ), this, SLOT( resetParameters() ) );
  connect(tool_->enabled, SIGNAL( released() ), this, SLOT( updateSlice() ) );
  connect(tool_->radioX, SIGNAL( released() ), this, SLOT( updateSlice() ) );
  connect(tool_->radioY, SIGNAL( released() ), this, SLOT( updateSlice() ) );
  connect(tool_->radioZ, SIGNAL( released() ), this, SLOT( updateSlice() ) );
  connect(tool_->posSlider, SIGNAL( sliderMoved(int) ), this, SLOT( updateSlice(int) ) );
  connect(tool_->sizeSlider, SIGNAL( sliderMoved(int) ), this, SLOT( updateSlice(int) ) );
  
  emit addToolbox( tr("Slice") , tool_ );
}

void SlicePlugin::resetParameters(){
  tool_->posSlider->setValue(0);
  tool_->sizeSlider->setValue(102);
  updateSlice();
}

void SlicePlugin::updateSlice(int /*bla*/){
 updateSlice();
}

void SlicePlugin::updateSlice(){

  if ( tool_->enabled->isChecked() ) {
    node_->set_status( ACG::SceneGraph::BaseNode::Active );
    node_->setMultipassStatus(BaseNode::ALLPASSES);
  } else {
    node_->set_status( ACG::SceneGraph::BaseNode::HideNode );
    node_->setMultipassStatus(ACG::SceneGraph::BaseNode::NOPASS);
  }

  if ( tool_->enabled->isChecked() ){

    ACG::Vec3d bbmin;
    ACG::Vec3d bbmax;

    getBoundingBox( bbmin, bbmax);

    ACG::Vec3d center = (bbmin + bbmax) * 0.5;
    ACG::Vec3f pos (center[0], center[1], center[2]);

    //get the normal
    ACG::Vec3f normal(1.0, 0.0, 0.0);
    float eps;
    float offset = 0.0;

    eps = tool_->sizeSlider->value() / 100.0;

    if (eps == 0.0)
      eps = 0.01;

    if (tool_->radioX->isChecked()){
      normal = ACG::Vec3f(1.0, 0.0, 0.0);
      eps *= (bbmax[0] - bbmin[0]);
      offset = (bbmax[0] - bbmin[0]) * 0.5;
    }else
    if (tool_->radioY->isChecked()){
      normal = ACG::Vec3f(0.0, 1.0, 0.0);
      eps *= (bbmax[1] - bbmin[1]);
      offset = (bbmax[1] - bbmin[1]) * 0.5;
    }else
    if (tool_->radioZ->isChecked()){
      normal = ACG::Vec3f(0.0, 0.0, 1.0);
      eps *= (bbmax[2] - bbmin[2]);
      offset = (bbmax[2] - bbmin[2]) * 0.5;
    }

    pos += normal * ( (float)tool_->posSlider->value() / 100.0 ) * (offset + 0.1); //0.1 is just a little workarround

    node_->set_plane(pos, normal, eps);
  }
  emit updateView();
}

void SlicePlugin::getBoundingBox(ACG::Vec3d& bbmin, ACG::Vec3d& bbmax){

  bool firstRound = true;

  PluginFunctions::IteratorRestriction restriction;

  if (tool_->radioTarget->isChecked())
    restriction = PluginFunctions::TARGET_OBJECTS;
  else
    restriction = PluginFunctions::ALL_OBJECTS;

  for ( PluginFunctions::ObjectIterator o_it(restriction,DataType(DATA_ALL)) ;
                                        o_it != PluginFunctions::objectsEnd(); ++o_it)  {
    // get scene size
    ACG::Vec3d cur_min;
    ACG::Vec3d cur_max;

    o_it->getBoundingBox(cur_min, cur_max);

    if (firstRound){
      bbmin = cur_min;
      bbmax = cur_max;
      firstRound = false;
    }else{
      bbmin[0] = std::min( bbmin[0], cur_min[0]);
      bbmin[1] = std::min( bbmin[1], cur_min[1]);
      bbmin[2] = std::min( bbmin[2], cur_min[2]);
      bbmax[0] = std::max( bbmax[0], cur_max[0]);
      bbmax[1] = std::max( bbmax[1], cur_max[1]);
      bbmax[2] = std::max( bbmax[2], cur_max[2]);
    }
  }

  if ((bbmin[0] > bbmax[0]) || (bbmin[1] > bbmax[1]) || (bbmin[2] > bbmax[2]))
    std::cerr << "Error while computing bounding box!";

}

Q_EXPORT_PLUGIN2( slicePlugin , SlicePlugin );