tfgeometrymanipulator.cpp 10.6 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
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

#include <QColorDialog>

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();
        }
95
96
97
        else {
            e->ignore();
        }
98
99
100
101
    }

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

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

    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();
        }
131
132
133
        else {
            e->ignore();
        }
134
135
136
137
138
139
140
141
142
143
144
145
    }


    const int KeyPointManipulator::MANIPULATOR_SIZE = 6;

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

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

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

    void WholeTFGeometryManipulator::render() {

    }

158
159
160
161
162
163
164
165
166
167
168
169
170
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
    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();
            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();
    }

    bool WholeTFGeometryManipulator::insideGeometry(const tgt::vec2& position) const {
        if (_helperPoints.size() < 3)
            return false;

        // simple approach: check for left turn for every triple of points
        for (std::vector<tgt::vec2>::const_iterator it = _helperPoints.begin()+1; it != _helperPoints.end(); ++it) {
            if (! AlgorithmicGeometry::rightTurn2D(*(it-1), *it, position))
                return false;
        }

        return true;
    }

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

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

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

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

246
}