21.10.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

geometry2dtransferfunctioneditor.cpp 13 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22
23
24
25
26
// 
// ================================================================================================

#include "geometry2dtransferfunctioneditor.h"

27
28
29
30
#include "cgt/assert.h"
#include "cgt/shadermanager.h"
#include "cgt/glcontextmanager.h"
#include "cgt/qt/qtthreadedcanvas.h"
31
32
33
34
35

#include "application/gui/qtcolortools.h"
#include "application/gui/properties/tfgeometrymanipulator.h"

#include "core/classification/geometry2dtransferfunction.h"
schultezub's avatar
schultezub committed
36
#include "core/classification/tfgeometry2d.h"
37
#include "core/datastructures/imagerepresentationlocal.h"
38
39
40
41
42
43
44
#include "core/properties/transferfunctionproperty.h"

#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>

schultezub's avatar
schultezub committed
45
namespace campvis {
46

47
48
    Geometry2DTransferFunctionEditor::Geometry2DTransferFunctionEditor(TransferFunctionProperty* prop, Geometry2DTransferFunction* tf, QWidget* parent /*= 0*/)
        : AbstractTransferFunctionEditor(prop, tf, parent)
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        , _layout(0)
        , _canvas(0)
        , _lblIntensityLeft(0)
        , _lblIntensityRight(0)
        , _btnAddGeometry(0)
    {
        _selectedGeometry = 0;
        setupGUI();
        tf->s_geometryCollectionChanged.connect(this, &Geometry2DTransferFunctionEditor::onGeometryCollectionChanged);
        updateManipulators();
        setEventTypes(tgt::Event::MOUSEPRESSEVENT);
    }

    Geometry2DTransferFunctionEditor::~Geometry2DTransferFunctionEditor() {
63
        disconnectFromTf();
64
65
66

        if (tgt::GlContextManager::isInited())
            tgt::GlContextManager::getRef().removeContext(_canvas);
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
    }

    void Geometry2DTransferFunctionEditor::updateWidgetFromProperty() {
        Geometry2DTransferFunction* gtf = static_cast<Geometry2DTransferFunction*>(_transferFunction);
        _lblIntensityLeft->setText(QString::number(gtf->getIntensityDomain().x));
        _lblIntensityRight->setText(QString::number(gtf->getIntensityDomain().y));
        invalidate();
    }

    void Geometry2DTransferFunctionEditor::paint() {
        Geometry2DTransferFunction* gtf = static_cast<Geometry2DTransferFunction*>(_transferFunction);
        const std::vector<TFGeometry2D*>& geometries = gtf->getGeometries();

        // TODO: get rid of intermediate mode?
        glPushAttrib(GL_ALL_ATTRIB_BITS);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glViewport(0, 0, _canvas->width(), _canvas->height());

        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glOrtho(0, 1 , 0, 1, -1, 1);
        glClearColor(1.f, 1.f, 1.f, 1.f);
        glClear(GL_COLOR_BUFFER_BIT);
        LGL_ERROR;

        // renderIntoEditor TF geometries
94
95
96
97
98
        {
            tbb::mutex::scoped_lock lock(_localMutex);
            for (std::vector<TFGeometry2D*>::const_iterator it = geometries.begin(); it != geometries.end(); ++it) {
                (*it)->render();
            }
99
100
101
        }

        // render histogram if existent
102
        const TransferFunctionProperty::IntensityHistogramType* ih = getIntensityHistogram();
103
104
105
106
107
108
109
110
111
112
113
        if (ih != 0) {
            size_t numBuckets = ih->getNumBuckets(0);
            if (numBuckets > 0) {
                float maxFilling = static_cast<float>(ih->getMaxFilling());

                float xl = static_cast<float>(0.f) / static_cast<float>(numBuckets);
                float yl = static_cast<float>(ih->getNumElements(0)) / maxFilling;

                glBegin(GL_QUADS);
                glColor4f(1.f, .75f, 0.f, .5f);
                for (size_t i = 1; i < numBuckets; ++i) {
114
115
                    float xr = static_cast<float>(i) / static_cast<float>(numBuckets);
                    float yr = static_cast<float>(ih->getNumElements(i)) / maxFilling;
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
156
157
158
159
160

                    glVertex2f(xl, 0.f);
                    glVertex2f(xl, yl);
                    glVertex2f(xr, yr);
                    glVertex2f(xr, 0.f);

                    xl = xr;
                    yl = yr;
                }
                glEnd();
            }
        }

        {
            tbb::mutex::scoped_lock lock(_localMutex);

            // render selected geometry
            if (_selectedGeometry != 0) {
                const std::vector<tgt::vec2>& helperPoints = _selectedGeometry->getHelperPoints();
                glColor4ub(0, 0, 0, 196);
                glEnable(GL_LINE_STIPPLE);
                glLineStipple(1, 0xFAFA);
                glBegin(GL_LINE_LOOP);
                for (std::vector<tgt::vec2>::const_iterator it = helperPoints.begin(); it != helperPoints.end(); ++it)
                    glVertex2fv(it->elem);
                glEnd();
                glDisable(GL_LINE_STIPPLE);
            }

            glPopMatrix();

            glPushMatrix();
            glOrtho(0, _canvas->width(), 0, _canvas->height(), -1, 1);
            // render manipulators
            for (std::vector<AbstractTFGeometryManipulator*>::iterator it = _manipulators.begin(); it != _manipulators.end(); ++it) {
                (*it)->render();
            }
            glPopMatrix();
        }

        LGL_ERROR;
        glPopAttrib();
    }

    void Geometry2DTransferFunctionEditor::sizeChanged(const tgt::ivec2& size) {
161
162
163
164
165
        {
            tbb::mutex::scoped_lock lock(_localMutex);
            for (std::vector<AbstractTFGeometryManipulator*>::iterator it = _manipulators.begin(); it != _manipulators.end(); ++it) {
                (*it)->setViewportSize(size);
            }
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
        }
        invalidate();
    }

    void Geometry2DTransferFunctionEditor::mousePressEvent(tgt::MouseEvent* e) {
        if (_selectedGeometry != 0 && e->modifiers() & tgt::Event::CTRL) {
//             TFGeometry2D* g = _selectedGeometry->getGeometry();
//             std::vector<TFGeometry2D::KeyPoint>& kpts = g->getKeyPoints();
//             TFGeometry2D::KeyPoint kp(static_cast<float>(e->x()) / static_cast<float>(_canvas->width()), tgt::col4(255));
//             std::vector<TFGeometry2D::KeyPoint>::const_iterator lb = std::upper_bound(kpts.begin(), kpts.end(), kp);
//             if (lb != kpts.end()) {
//                 kp._color = lb->_color;
//             }
//             else {
//                 kp._color = kpts.back()._color;
//             }
//             float alpha = tgt::clamp(static_cast<float>(_canvas->height() - e->y()) / static_cast<float>(_canvas->height()), 0.f, 1.f);
//             kp._color.a = static_cast<uint8_t>(alpha * 255.f);
//             kpts.insert(lb, kp);
//             updateManipulators();
//             g->s_changed();
        }
        else {
            _selectedGeometry = 0;
            invalidate();
            e->ignore();
        }
    }

195
196
197
198
    void Geometry2DTransferFunctionEditor::repaint() {
        invalidate();
    }

199
    void Geometry2DTransferFunctionEditor::invalidate() {
200
201
202
        // TODO: check, whether this should be done in an extra thread
        tgt::GLContextScopedLock lock(_canvas);
        paint();
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
    }

    void Geometry2DTransferFunctionEditor::setupGUI() {
        Geometry2DTransferFunction* gtf = static_cast<Geometry2DTransferFunction*>(_transferFunction);

        _layout = new QGridLayout(this);
        setLayout(_layout);

        QLabel* lblOpacityTop = new QLabel(tr("100%"), this);
        _layout->addWidget(lblOpacityTop, 1, 0, 1, 1, Qt::AlignRight);
        QLabel* lblOpacity = new QLabel(tr("Opacity"), this);
        _layout->addWidget(lblOpacity, 2, 0, 1, 1, Qt::AlignRight);
        QLabel* lblOpacityBottom = new QLabel(tr("0%"), this);
        _layout->addWidget(lblOpacityBottom, 3, 0, 1, 1, Qt::AlignRight);

218
        _canvas = new tgt::QtThreadedCanvas("", tgt::ivec2(256, 128), tgt::GLCanvas::RGBA_BUFFER, 0, false);
219
220
        GLCtxtMgr.registerContextAndInitGlew(_canvas, "Geometry2DTransferFunctionEditor");
        GLCtxtMgr.releaseContext(_canvas, false);
221

222
223
224
225
226
227
228
229
230
231
        _canvas->setPainter(this, false);
        _layout->addWidget(_canvas, 1, 1, 3, 3);

        _lblIntensityLeft = new QLabel(QString::number(gtf->getIntensityDomain().x), this);
        _layout->addWidget(_lblIntensityLeft, 4, 1, 1, 1, Qt::AlignLeft);
        QLabel* lblIntensity = new QLabel(tr("Intensity"), this);
        _layout->addWidget(lblIntensity, 4, 2, 1, 1, Qt::AlignHCenter);
        _lblIntensityRight = new QLabel(QString::number(gtf->getIntensityDomain().y), this);
        _layout->addWidget(_lblIntensityRight, 4, 3, 1, 1, Qt::AlignRight);

232
        QVBoxLayout* buttonLayout = new QVBoxLayout();
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
        _layout->addLayout(buttonLayout, 1, 4, 1, 3, Qt::AlignTop);

        _btnAddGeometry = new QPushButton(tr("Add Geometry"), this);
        buttonLayout->addWidget(_btnAddGeometry);
        connect(_btnAddGeometry, SIGNAL(clicked()), this, SLOT(onBtnAddGeometryClicked()));
        _btnRemoveGeometry = new QPushButton(tr("Remove Geometry"), this);
        buttonLayout->addWidget(_btnRemoveGeometry);
        connect(_btnRemoveGeometry, SIGNAL(clicked()), this, SLOT(onBtnRemoveGeometryClicked()));

        _layout->setColumnStretch(2, 1);
        _layout->setRowStretch(2, 1);
    }

    void Geometry2DTransferFunctionEditor::updateManipulators() {
        tbb::mutex::scoped_lock lock(_localMutex);

249
        _canvas->getEventHandler()->addEventListenerToFront(this);
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    }

    void Geometry2DTransferFunctionEditor::onGeometryCollectionChanged() {
        updateManipulators();
    }

    void Geometry2DTransferFunctionEditor::onWholeTFGeometryManipulatorSelected(WholeTFGeometryManipulator* wtf /* :) */) {
        _selectedGeometry = wtf;
        invalidate();
    }

    void Geometry2DTransferFunctionEditor::onBtnAddGeometryClicked() {
        Geometry2DTransferFunction* gtf = static_cast<Geometry2DTransferFunction*>(_transferFunction);
        gtf->addGeometry(TFGeometry2D::createQuad(tgt::vec2(.4f, .6f), tgt::vec2(0.f, .4f), tgt::col4(196)));
    }

    void Geometry2DTransferFunctionEditor::onBtnRemoveGeometryClicked() {
        if (_selectedGeometry != 0) {
            // to get the signal-slots disconnected in the correct order and avoid double deletion,
            // this is getting a little messy and cumbersome:
//             Geometry2DTransferFunction* gtf = static_cast<Geometry2DTransferFunction*>(_transferFunction);
//             TFGeometry2D* geometryToRemove = _selectedGeometry->getGeometry();
// 
//             {
//                 tbb::mutex::scoped_lock lock(_localMutex);
// 
//                 for (std::vector<AbstractTFGeometryManipulator*>::iterator it = _manipulators.begin(); it != _manipulators.end(); ++it) {
//                     if (*it == _selectedGeometry) {
//                         _manipulators.erase(it);
//                         break;
//                     }
//                 }
//                 delete _selectedGeometry;
//                 _selectedGeometry = 0;
//             }
// 
//             gtf->removeGeometry(geometryToRemove);
        }
    }

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
    void Geometry2DTransferFunctionEditor::onTfAboutToBeDeleted() {
        disconnectFromTf();
    }

    void Geometry2DTransferFunctionEditor::disconnectFromTf() {
        tbb::mutex::scoped_lock lock(_localMutex);

        // clear and delete former stuff
        _selectedGeometry = 0;
        for (std::vector<AbstractTFGeometryManipulator*>::iterator it = _manipulators.begin(); it != _manipulators.end(); ++it) {
            if (WholeTFGeometryManipulator* tester = dynamic_cast<WholeTFGeometryManipulator*>(*it)) {
                tester->s_selected.disconnect(this);
            }
            delete *it;
        }
        _manipulators.clear();

        Geometry2DTransferFunction* gtf = static_cast<Geometry2DTransferFunction*>(_transferFunction);
        if (gtf != nullptr) {
            gtf->s_geometryCollectionChanged.disconnect(this);
            gtf->s_aboutToBeDeleted.disconnect(this);
            _transferFunction->s_changed.disconnect(this);
            _transferFunction = nullptr;
        }
    }

316

317
}