tfgeometrymanipulator.cpp 11 KB
Newer Older
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
// ================================================================================================
// 
// This file is part of the TUMVis Visualization Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
//      Christian Schulte zu Berge (christian.szb@in.tum.de)
//      Chair for Computer Aided Medical Procedures
//      Technische Universitt Mnchen
//      Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
// 
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
// 
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// ================================================================================================

#include "tfgeometrymanipulator.h"

#include "tgt/assert.h"
#include "tgt/tgt_gl.h"
#include "tgt/event/mouseevent.h"

#include "application/gui/qtcolortools.h"
schultezub's avatar
schultezub committed
36
37
#include "core/classification/geometry1dtransferfunction.h"
#include "core/classification/tfgeometry1d.h"
38
#include "core/tools/algorithmicgeometry.h"
39
40

#include <QColorDialog>
41
#include <algorithm>
42
43
44

namespace TUMVis {

45
    AbstractTFGeometryManipulator::AbstractTFGeometryManipulator(const tgt::ivec2& viewportSize)
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        : _viewportSize(viewportSize)
    {
    }

    void AbstractTFGeometryManipulator::setViewportSize(const tgt::ivec2& viewportSize) {
        _viewportSize = viewportSize;
    }

    tgt::ivec2 AbstractTFGeometryManipulator::tfToViewport(const tgt::vec2& pos) const {
        // TF coordinate system is expected to be [0, 1]^n
        return tgt::ivec2(tgt::iround(pos.x * _viewportSize.x), tgt::iround(pos.y * _viewportSize.y));
    }

    tgt::vec2 AbstractTFGeometryManipulator::viewportToTF(const tgt::ivec2& pos) const {
        // TF coordinate system is expected to be [0, 1]^n
        return tgt::vec2(static_cast<float>(pos.x) / static_cast<float>(_viewportSize.x), static_cast<float>(pos.y) / static_cast<float>(_viewportSize.y));
    }

// ================================================================================================

66
67
    KeyPointManipulator::KeyPointManipulator(const tgt::ivec2& viewportSize, TFGeometry1D* geometry, const std::vector<TFGeometry1D::KeyPoint>::iterator& keyPoint)
        : AbstractTFGeometryManipulator(viewportSize)
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
        , _geometry(geometry)
        , _keyPoint(keyPoint)
        , _mousePressed(false)
    {
        tgtAssert(geometry != 0, "Geometry must not be 0.");
    }

    void KeyPointManipulator::render() {
        tgt::ivec2 pos = tfToViewport(tgt::vec2(_keyPoint->_position, static_cast<float>(_keyPoint->_color.a) / 255.f));
        glColor3ub(0, 0, 0);
        glBegin(GL_LINE_LOOP);
            glVertex2i(pos.x - MANIPULATOR_SIZE, pos.y - MANIPULATOR_SIZE);
            glVertex2i(pos.x + MANIPULATOR_SIZE, pos.y - MANIPULATOR_SIZE);
            glVertex2i(pos.x + MANIPULATOR_SIZE, pos.y + MANIPULATOR_SIZE);
            glVertex2i(pos.x - MANIPULATOR_SIZE, pos.y + MANIPULATOR_SIZE);
        glEnd();
    }

    void KeyPointManipulator::mousePressEvent(tgt::MouseEvent* e) {
        tgt::ivec2 kppos = tfToViewport(tgt::vec2(_keyPoint->_position, static_cast<float>(_keyPoint->_color.a) / 255.f));
        if ((abs(kppos.x - e->coord().x) < MANIPULATOR_SIZE) && (abs(kppos.y - _viewportSize.y + e->coord().y) < MANIPULATOR_SIZE)) {
            //_valueWhenPressed = *_keyPoint;
            _mousePressed = true;
            e->accept();
        }
93
94
95
        else {
            e->ignore();
        }
96
97
98
99
    }

    void KeyPointManipulator::mouseReleaseEvent(tgt::MouseEvent* e) {
        _mousePressed = false;
100
101
        // ignore here, because other listeners probably need this signal as well
        e->ignore();
102
103
104
105
106
107
108
109
110
    }

    void KeyPointManipulator::mouseMoveEvent(tgt::MouseEvent* e) {
        if (_mousePressed) {
            tgt::ivec2 currentPosition = tgt::clamp(tgt::ivec2(e->coord().x, _viewportSize.y - e->coord().y), tgt::ivec2(0, 0), _viewportSize);
            tgt::vec2 tfCoords = viewportToTF(currentPosition);

            _keyPoint->_position = tfCoords.x;
            _keyPoint->_color.a = static_cast<uint8_t>(tfCoords.y * 255.f);
111
            std::sort(_geometry->getKeyPoints().begin(), _geometry->getKeyPoints().end());
112
113
            _geometry->s_changed();
        }
114
115
        // ignore here, because other listeners probably need this signal as well
        e->ignore();
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    }

    void KeyPointManipulator::mouseDoubleClickEvent(tgt::MouseEvent* e) {
        tgt::ivec2 kppos = tfToViewport(tgt::vec2(_keyPoint->_position, static_cast<float>(_keyPoint->_color.a) / 255.f));
        if ((abs(kppos.x - e->coord().x) < MANIPULATOR_SIZE) && (abs(kppos.y - _viewportSize.y + e->coord().y) < MANIPULATOR_SIZE)) {
            // launch a color picker dialog and set new color on success
            QColor newColor = QColorDialog::getColor(QtColorTools::toQColor(_keyPoint->_color), 0, "Select New Color");
            if(newColor.isValid()) {
                tgt::col4 tmp = QtColorTools::toTgtColor(newColor);
                _keyPoint->_color = tgt::col4(tmp.xyz(), _keyPoint->_color.a);
                _geometry->s_changed();
            }
            e->accept();
        }
130
131
132
        else {
            e->ignore();
        }
133
134
    }

135
    const int KeyPointManipulator::MANIPULATOR_SIZE = 5;
136
137
138

// ================================================================================================

139
140
    WholeTFGeometryManipulator::WholeTFGeometryManipulator(const tgt::ivec2& viewportSize, TFGeometry1D* geometry)
        : AbstractTFGeometryManipulator(viewportSize)
141
        , _geometry(geometry)
142
        , _mousePressed(false)
143
144
    {
        tgtAssert(geometry != 0, "Geometry must not be 0.");
145
146
147
148
149
150
        _geometry->s_changed.connect(this, &WholeTFGeometryManipulator::onGeometryChanged);
        updateHelperPoints();
    }

    WholeTFGeometryManipulator::~WholeTFGeometryManipulator() {
        _geometry->s_changed.disconnect(this);
151
152
153
154
155
156
    }

    void WholeTFGeometryManipulator::render() {

    }

schultezub's avatar
schultezub committed
157
    TFGeometry1D* WholeTFGeometryManipulator::getGeometry() const {
158
159
160
        return _geometry;
    }

161
162
163
164
    const std::vector<tgt::vec2>& WholeTFGeometryManipulator::getHelperPoints() const {
        return _helperPoints;
    }

165
166
167
168
169
    void WholeTFGeometryManipulator::mousePressEvent(tgt::MouseEvent* e) {
        _pressedPosition = viewportToTF(tgt::ivec2(e->coord().x, _viewportSize.y - e->coord().y));
        if (insideGeometry(_pressedPosition)) {
            _mousePressed = true;
            _valuesWhenPressed = _geometry->getKeyPoints();
170
            s_selected(this);
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
            e->accept();
        }
        else { 
            e->ignore();
        }
    }

    void WholeTFGeometryManipulator::mouseReleaseEvent(tgt::MouseEvent* e) {
        _mousePressed = false;
        // ignore here, because other listeners probably need this signal as well
        e->ignore();
    }

    void WholeTFGeometryManipulator::mouseMoveEvent(tgt::MouseEvent* e) {
        if (_mousePressed) {
            tgt::vec2 currentPosition = viewportToTF(tgt::clamp(tgt::ivec2(e->coord().x, _viewportSize.y - e->coord().y), tgt::ivec2(0, 0), _viewportSize));
            tgt::vec2 displacement = currentPosition - _pressedPosition;

            for (size_t i = 0; i < _valuesWhenPressed.size(); ++i) {
                _geometry->getKeyPoints()[i]._position = _valuesWhenPressed[i]._position + displacement.x;
            }

            _geometry->s_changed();
        }
        // ignore here, because other listeners probably need this signal as well
        e->ignore();
    }

    void WholeTFGeometryManipulator::mouseDoubleClickEvent(tgt::MouseEvent* e) {
        tgt::vec2 pos = viewportToTF(tgt::ivec2(e->coord().x, _viewportSize.y - e->coord().y));
        if (insideGeometry(pos)) {
            // launch a color picker dialog and set new color on success
            QColor newColor = QColorDialog::getColor(QtColorTools::toQColor(_geometry->getKeyPoints().front()._color), 0, "Select New Color");
            if(newColor.isValid()) {
                tgt::col4 tmp = QtColorTools::toTgtColor(newColor);

schultezub's avatar
schultezub committed
207
                for (std::vector<TFGeometry1D::KeyPoint>::iterator it = _geometry->getKeyPoints().begin(); it != _geometry->getKeyPoints().end(); ++it) {
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
                    it->_color = tgt::col4(tmp.xyz(), it->_color.a);
                }
                _geometry->s_changed();
            }
            e->accept();
        }
        else {
            e->ignore();
        }
    }

    void WholeTFGeometryManipulator::onGeometryChanged() {
        updateHelperPoints();
    }

223
224
225
226
227
228
229
230
    namespace {
        struct XComparer {
            bool operator() (const tgt::vec2& left, const tgt::vec2& right) {
                return left.x < right.x;
            };
        };
    }

231
    bool WholeTFGeometryManipulator::insideGeometry(const tgt::vec2& position) const {
232
        if (_helperPoints.size() < 2)
233
234
            return false;

235
236
237
        std::vector<tgt::vec2>::const_iterator lb = std::upper_bound(_helperPoints.begin(), _helperPoints.end(), position, XComparer());
        if (lb == _helperPoints.begin() || lb == _helperPoints.end())
            return false;
238

239
        return AlgorithmicGeometry::rightTurn2D(*(lb-1), *lb, position);
240
241
242
243
    }

    void WholeTFGeometryManipulator::updateHelperPoints() {
        _helperPoints.clear();
schultezub's avatar
schultezub committed
244
        const std::vector<TFGeometry1D::KeyPoint>& keyPoints = _geometry->getKeyPoints();
245

246
247
248
249
        if (keyPoints.front()._color.w > 0) {
            _helperPoints.push_back(tgt::vec2(keyPoints.front()._position, 0.f));
        }

schultezub's avatar
schultezub committed
250
        for (std::vector<TFGeometry1D::KeyPoint>::const_iterator it = keyPoints.begin(); it != keyPoints.end(); ++it) {
251
252
253
            float y = static_cast<float>(it->_color.a) / 255.f;
            _helperPoints.push_back(tgt::vec2(it->_position, y));
        }
254
255
256
257

        if (keyPoints.back()._color.w > 0) {
            _helperPoints.push_back(tgt::vec2(keyPoints.back()._position, 0.f));
        }
258
259
    }

260
}