tfgeometrymanipulator.cpp 11.1 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
36
37
// ================================================================================================
// 
// 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"
#include "core/classification/geometrytransferfunction.h"
#include "core/classification/tfgeometry.h"
38
#include "core/tools/algorithmicgeometry.h"
39
40

#include <QColorDialog>
41
#include <algorithm>
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

namespace TUMVis {

    AbstractTFGeometryManipulator::AbstractTFGeometryManipulator(const tgt::ivec2& viewportSize, GeometryTransferFunction* tf)
        : _viewportSize(viewportSize)
        , _tf(tf)
    {
        tgtAssert(tf != 0, "Transfer Function must not be 0.");
    }

    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));
    }

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

    KeyPointManipulator::KeyPointManipulator(const tgt::ivec2& viewportSize, GeometryTransferFunction* tf, TFGeometry* geometry, const std::vector<TFGeometry::KeyPoint>::iterator& keyPoint)
        : AbstractTFGeometryManipulator(viewportSize, tf)
        , _geometry(geometry)
        , _keyPoint(keyPoint)
        , _mousePressed(false)
        //, _pressedPosition(0, 0)
    {
        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();
        }
96
97
98
        else {
            e->ignore();
        }
99
100
101
102
    }

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

    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);
114
            std::sort(_geometry->getKeyPoints().begin(), _geometry->getKeyPoints().end());
115
116
            _geometry->s_changed();
        }
117
118
        // ignore here, because other listeners probably need this signal as well
        e->ignore();
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    }

    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();
        }
133
134
135
        else {
            e->ignore();
        }
136
137
    }

138
    const int KeyPointManipulator::MANIPULATOR_SIZE = 5;
139
140
141
142
143
144

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

    WholeTFGeometryManipulator::WholeTFGeometryManipulator(const tgt::ivec2& viewportSize, GeometryTransferFunction* tf, TFGeometry* geometry)
        : AbstractTFGeometryManipulator(viewportSize, tf)
        , _geometry(geometry)
145
        , _mousePressed(false)
146
147
    {
        tgtAssert(geometry != 0, "Geometry must not be 0.");
148
149
150
151
152
153
        _geometry->s_changed.connect(this, &WholeTFGeometryManipulator::onGeometryChanged);
        updateHelperPoints();
    }

    WholeTFGeometryManipulator::~WholeTFGeometryManipulator() {
        _geometry->s_changed.disconnect(this);
154
155
156
157
158
159
    }

    void WholeTFGeometryManipulator::render() {

    }

160
161
162
163
    TFGeometry* WholeTFGeometryManipulator::getGeometry() const {
        return _geometry;
    }

164
165
166
167
    const std::vector<tgt::vec2>& WholeTFGeometryManipulator::getHelperPoints() const {
        return _helperPoints;
    }

168
169
170
171
172
    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();
173
            s_selected(this);
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
220
221
222
223
224
225
            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);

                for (std::vector<TFGeometry::KeyPoint>::iterator it = _geometry->getKeyPoints().begin(); it != _geometry->getKeyPoints().end(); ++it) {
                    it->_color = tgt::col4(tmp.xyz(), it->_color.a);
                }
                _geometry->s_changed();
            }
            e->accept();
        }
        else {
            e->ignore();
        }
    }

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

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

234
    bool WholeTFGeometryManipulator::insideGeometry(const tgt::vec2& position) const {
235
        if (_helperPoints.size() < 2)
236
237
            return false;

238
239
240
        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;
241

242
        return AlgorithmicGeometry::rightTurn2D(*(lb-1), *lb, position);
243
244
245
246
247
248
    }

    void WholeTFGeometryManipulator::updateHelperPoints() {
        _helperPoints.clear();
        const std::vector<TFGeometry::KeyPoint>& keyPoints = _geometry->getKeyPoints();

249
250
251
252
        if (keyPoints.front()._color.w > 0) {
            _helperPoints.push_back(tgt::vec2(keyPoints.front()._position, 0.f));
        }

253
254
255
256
        for (std::vector<TFGeometry::KeyPoint>::const_iterator it = keyPoints.begin(); it != keyPoints.end(); ++it) {
            float y = static_cast<float>(it->_color.a) / 255.f;
            _helperPoints.push_back(tgt::vec2(it->_position, y));
        }
257
258
259
260

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

263
}