microscopyimagesegmentation.cpp 34.5 KB
Newer Older
Hossain Mahmud's avatar
Hossain Mahmud 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
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// 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
// 
// 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.
// 
// ================================================================================================

#include "microscopyimagesegmentation.h"
26
27
28
#include "cgt/logmanager.h"
#include "cgt/shadermanager.h"
#include "cgt/textureunit.h"
29
30
#include "cgt/event/keyevent.h"
#include "cgt/event/mouseevent.h"
Hossain Mahmud's avatar
Hossain Mahmud committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#include "core/datastructures/facegeometry.h"
#include "core/datastructures/geometrydatafactory.h"
#include "core/datastructures/renderdata.h"

#include "core/classification/simpletransferfunction.h"
#include "core/pipeline/processordecoratorbackground.h"
#include "core/tools/quadrenderer.h"

#include "modules/microscopysegmentation/ext/alglib/interpolation.h"

#include <QPainter>
#include "core/tools/stringutils.h"

namespace campvis {
    const std::string MicroscopyImageSegmentation::loggerCat_ = "CAMPVis.modules.vis.MicroscopyImageSegmentation";

    ContourObject::ContourObject(std::string name)
        : _objectName("objName", "Object Name", name)
50
        , _color("color", "Color", cgt::vec4(1,1,1,1), cgt::vec4(0,0,0,0), cgt::vec4(1,1,1,1))
Hossain Mahmud's avatar
Hossain Mahmud committed
51
52
53
54
55
56
        , _visibility("showContour", "Show Contour", true)
    {
    }
    ContourObject::~ContourObject() {
    }

57
    ContourObject& ContourObject::operator=(const ContourObject& rhs) {
Hossain Mahmud's avatar
Hossain Mahmud committed
58
59
60
61
        this->_objectName.setValue(rhs._objectName.getValue());
        this->_color.setValue(rhs._color.getValue());
        this->_points = rhs._points;
        this->_objectsCoordinates = rhs._objectsCoordinates;
62
        return *this;
Hossain Mahmud's avatar
Hossain Mahmud committed
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
    }

    void ContourObject::addObject() {
        _objectsCoordinates.push_back(_points);
        _points.clear();
    }

    void ProxyFaceGeometry::addGeometry(FaceGeometry geometry) {
        _geometries.push_back(geometry);
    }
    void ProxyFaceGeometry::render(GLenum mode) const {
        for (size_t i = 0; i < _geometries.size(); i++) {
            _geometries[i].render(mode);
        }
    }

    GenericOption<ContourObject> first[1] = {
        GenericOption<ContourObject>("Select", "Select An Object", ContourObject("Please Select"))
    };

    MicroscopyImageSegmentation::MicroscopyImageSegmentation(IVec2Property* viewportSizeProp, RaycastingProcessor* raycaster)
        : VisualizationProcessor(viewportSizeProp)
        , p_inputVolume("InputVolume", "Input Volume", "", DataNameProperty::READ)
        , p_outputImage("OutputImage", "Output Image", "ve.output", DataNameProperty::WRITE)
        , p_enableScribbling("EnableScribbling", "Enable Scribbling in Slice Views", false)
        , p_seProperties("SliceExtractorProperties", "Slice Extractor Properties")
        , p_vrProperties("VolumeRendererProperties", "Volume Renderer Properties")
90
        , _tcp(viewportSizeProp)
Hossain Mahmud's avatar
Hossain Mahmud committed
91
92
        , _vr(viewportSizeProp, raycaster)
        , _sliceExtractor(viewportSizeProp)
93
94
95
        , p_rightPaneBlockSize("SliceRenderSize", "Right Pane Block Size", cgt::ivec2(32), cgt::ivec2(0), cgt::ivec2(10000), cgt::ivec2(1))
        , p_leftPaneSize("VolumeRenderSize", "Left Pane Size Size", cgt::ivec2(32), cgt::ivec2(0), cgt::ivec2(10000), cgt::ivec2(1))
        , p_zSize("ZSliceSize", "Main Slice View Size", cgt::ivec2(32), cgt::ivec2(0), cgt::ivec2(10000), cgt::ivec2(1))
Hossain Mahmud's avatar
Hossain Mahmud committed
96
97
98
99
        , _xSliceHandler(&_sliceExtractor.p_xSliceNumber)
        , _ySliceHandler(&_sliceExtractor.p_ySliceNumber)
        , _zSliceHandler(&_sliceExtractor.p_zSliceNumber)
        , _windowingHandler(&_sliceExtractor.p_transferFunction)
100
101
        , p_paintColor("PaintColor", "Change Color", cgt::vec4(255), cgt::vec4(0.0f), cgt::vec4(255))
        , p_axisScaling("AxisScaling", "Axis Scale", cgt::vec3(1), cgt::vec3(1), cgt::vec3(25), cgt::vec3(1))
102
103
        , p_fitToWindow("FitToWindow", "Fit to Window", true)
        , p_scalingFactor("ScalingFactor", "Scaling Factor", 1.f, 0.f, 10.f, .1f, 2)
104
        , p_offset("Offset", "Offset", cgt::ivec2(0), cgt::ivec2(0), cgt::ivec2(100))
Hossain Mahmud's avatar
Hossain Mahmud committed
105
106
107
108
        , p_addObject("addObjectButton", "Add Object")
        , p_deleteObject("deleteObjectButton", "Delete Current Object")
        , p_csvPath("csvPath", "File Name", "", StringProperty::SAVE_FILENAME)
        , p_saveCSV("saveCSV", "Save")
109
110
        , p_objectList(0)
        , _mousePressedInRaycaster(false)        
Hossain Mahmud's avatar
Hossain Mahmud committed
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
        , _objectNamePrefix("Object ")
        , _objectNameSuffix(0) 

    {
        addProperty(p_inputVolume, INVALID_PROPERTIES);
        addProperty(p_outputImage);


        p_addObject.s_clicked.connect(this, &MicroscopyImageSegmentation::onAddButtonClicked);
        addProperty(p_addObject);
        p_objectList = new GenericOptionProperty<ContourObject>("Object List", "Object List", first, 1);
        p_objectList->s_changed.connect(this, &MicroscopyImageSegmentation::onObjectSelectionChanged);
        addProperty(*p_objectList);
        p_paintColor.s_changed.connect(this, &MicroscopyImageSegmentation::onPaintColorChanged);
        addProperty(p_paintColor);
        p_deleteObject.s_clicked.connect(this, &MicroscopyImageSegmentation::onDeleteButtonClicked);
        addProperty(p_deleteObject);

        _oldScaling = p_axisScaling.getValue();
        addProperty(p_axisScaling);

        p_enableScribbling.setValue(true);
        p_enableScribbling.setVisible(false);
        addProperty(p_enableScribbling, VALID);

        addDecorator(new ProcessorDecoratorBackground());
        decoratePropertyCollection(this);

        p_csvPath.s_changed.connect(this, &MicroscopyImageSegmentation::onCSVFileSelected);
        addProperty(p_csvPath);
        p_saveCSV.s_clicked.connect(this, &MicroscopyImageSegmentation::onSaveCSVButtonClicked);
        addProperty(p_saveCSV);

        p_fitToWindow.addSharedProperty(&_sliceExtractor.p_fitToWindow);
        p_fitToWindow.setValue(false);
        p_scalingFactor.addSharedProperty(&_sliceExtractor.p_scalingFactor);
        p_scalingFactor.setValue(0.3f);
        p_offset.addSharedProperty(&_sliceExtractor.p_offset);
        p_seProperties.addPropertyCollection(_sliceExtractor);

        _sliceExtractor.s_scribblePainted.connect(this, &MicroscopyImageSegmentation::onSliceExtractorScribblePainted);
        _sliceExtractor.p_geometryID.setValue(p_outputImage.getValue() + ".scribbles");

        _sliceExtractor.p_lqMode.setVisible(false);
        _sliceExtractor.p_sourceImageID.setVisible(false);
        _sliceExtractor.p_targetImageID.setVisible(false);
        _sliceExtractor.p_sliceOrientation.setVisible(false);
        _sliceExtractor.p_xSliceColor.setVisible(false);
        _sliceExtractor.p_ySliceColor.setVisible(false);
        _sliceExtractor.p_zSliceColor.setVisible(false);
        _sliceExtractor.p_fitToWindow.setVisible(false);
        _sliceExtractor.p_geometryRenderMode.selectByOption(GL_LINE_STRIP);
        addProperty(p_seProperties, VALID);

        p_vrProperties.addPropertyCollection(_vr);
        _vr.p_lqMode.setVisible(false);
        _vr.p_inputVolume.setVisible(false);
        _vr.p_outputImage.setVisible(false);
        addProperty(p_vrProperties, VALID);

        p_inputVolume.addSharedProperty(&_vr.p_inputVolume);
        p_inputVolume.addSharedProperty(&_sliceExtractor.p_sourceImageID);

        _vr.setViewportSizeProperty(&p_rightPaneBlockSize);
        p_zSize.setVisible(false);
        p_zSize.setValue(p_leftPaneSize.getValue().x < p_leftPaneSize.getValue().y ? 
177
178
            cgt::vec2(p_leftPaneSize.getValue().x, p_leftPaneSize.getValue().x) 
            : cgt::vec2(p_leftPaneSize.getValue().y, p_leftPaneSize.getValue().y));
Hossain Mahmud's avatar
Hossain Mahmud committed
179
180
181
182
183
184
185

        _sliceExtractor.setViewportSizeProperty(&p_zSize);

        addProperty(p_rightPaneBlockSize, VALID);
        addProperty(p_leftPaneSize, VALID);

        // Event-Handlers
186
        _tcp.addLqModeProcessor(&_vr);
Hossain Mahmud's avatar
Hossain Mahmud committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200

        editVoxel = false;
        insertNextVoxelAt = -1;
    }

    MicroscopyImageSegmentation::~MicroscopyImageSegmentation() {
        delete p_objectList;
    }

    void MicroscopyImageSegmentation::init() {
        VisualizationProcessor::init();
        _vr.init();
        _sliceExtractor.init();

201
        _shader = ShdrMgr.load("core/glsl/passthrough.vert", "modules/microscopysegmentation/glsl/microscopyimagesegmentation.frag", "");
Hossain Mahmud's avatar
Hossain Mahmud committed
202
203
204
205

        _sliceExtractor.s_invalidated.connect(this, &MicroscopyImageSegmentation::onProcessorInvalidated);
        _vr.s_invalidated.connect(this, &MicroscopyImageSegmentation::onProcessorInvalidated);

206
        _quad = GeometryDataFactory::createQuad(cgt::vec3(0.f), cgt::vec3(1.f), cgt::vec3(0.f), cgt::vec3(1.f));
Hossain Mahmud's avatar
Hossain Mahmud committed
207
208
209
210
211
212
213
214
215
216

        // force recalculation of p_sliceRenderSize and p_volumeRenderSize
        onPropertyChanged(_viewportSizeProperty);
    }

    void MicroscopyImageSegmentation::deinit() {
        _vr.deinit();
        _sliceExtractor.deinit();
        VisualizationProcessor::deinit();
        ShdrMgr.dispose(_shader);
217
        _quad = nullptr; // Object is automatically deleted
Hossain Mahmud's avatar
Hossain Mahmud committed
218
219
220
221
222
223
224
225
226
    }

    void MicroscopyImageSegmentation::updateResult(DataContainer& data) {
        // launch sub-renderers if necessary
        if (getInvalidationLevel() & SCRIBBLE_INVALID) {
            ContourObject& selectedObject = this->p_objectList->getOptionReference();
            if (!(selectedObject._objectName == first[0]._value._objectName)) {
                // Create a ProxyFaceGeometry and insert all geometry into that
                ProxyFaceGeometry *pg = new ProxyFaceGeometry();
227
228
                std::vector<cgt::vec3> vertices;
                std::vector<cgt::vec4> colors;
Hossain Mahmud's avatar
Hossain Mahmud committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

                size_t size = selectedObject._points.size();

                alglib::real_2d_array xy;
                alglib::pspline2interpolant p;
                size = size > 3? size : size == 0? 0 : 3;
                xy.setlength(size, size);

                for (size_t i = 0; i < selectedObject._points.size(); ++i) {
                    xy[i][0] = selectedObject._points.at(i).x;
                    xy[i][1] = selectedObject._points.at(i).y;
                }

                if(selectedObject._points.size()) {
                    for (size_t i = selectedObject._points.size(); i < 3; ++i) {
                        xy[i][0] = selectedObject._points.at(selectedObject._points.size()-1).x;
                        xy[i][1] = selectedObject._points.at(selectedObject._points.size()-1).y;
                    }
                    //xy[size-1][0] = selectedObject._points.at(0).x;
                    //xy[size-1][1] = selectedObject._points.at(0).y;

                    alglib::pspline2buildperiodic (xy, size, 2, 0, p);
                    float stepSize = 1.0f/ (selectedObject._points.size() * 5);
                    float pos = 0;
                    vertices.clear();
                    colors.clear();
255
                    for (size_t gg = 0; gg < selectedObject._points.size() * 5; gg++ ) {
Hossain Mahmud's avatar
Hossain Mahmud committed
256
257
258
                        double x, y, z = selectedObject._points[0].z;
                        alglib::pspline2calc(p, pos, x, y);
                        pos += stepSize;
259
                        vertices.push_back(cgt::vec3(x, y, z));
Hossain Mahmud's avatar
Hossain Mahmud committed
260
261
262
                        colors.push_back(selectedObject._color.getValue());
                    }

263
                    pg->addGeometry(FaceGeometry(vertices, std::vector<cgt::vec3>(), colors));
Hossain Mahmud's avatar
Hossain Mahmud committed
264
265
266
267
268
269
                }

                for (size_t i = 1; i < p_objectList->getOptionCount(); i++) {
                    for (size_t j = 0; j < p_objectList->getOptionReference(static_cast<int>(i))._objectsCoordinates.size(); j++) {
                        vertices.clear();
                        colors.clear();
270
                        const std::vector<cgt::ivec3 >& it = p_objectList->getOptionReference(static_cast<int>(i))._objectsCoordinates[j];
Hossain Mahmud's avatar
Hossain Mahmud committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
                        
                        alglib::real_2d_array xy;
                        alglib::pspline2interpolant p;
                        size = it.size() > 3? it.size() : it.size() == 0? 0 : 3;
                        xy.setlength(size+1, size+1);
                        for (size_t k = 0; k <  it.size(); k++) {
                            xy[k][0] = it[k].x;
                            xy[k][1] = it[k].y;                            
                        }
                        if (it.size()) {
                            for (size_t k = it.size(); k < 3; ++k) {
                                xy[k][0] = it[it.size()-1].x;
                                xy[k][1] = it[it.size()-1].y;
                            }
                            //xy[size-1][0] = it[0].x;
                            //xy[size-1][1] = it[0].y;

                            alglib::pspline2buildperiodic(xy, size, 2, 0, p);
                            float stepSize = 1.0f/ (it.size() * 5);
                            float pos = 0;
                            vertices.clear();
                            colors.clear();
293
                            for (size_t gg= 0; gg < it.size() * 5; gg++ ) {
Hossain Mahmud's avatar
Hossain Mahmud committed
294
295
296
                                double x, y, z = it[0].z;
                                alglib::pspline2calc(p, pos, x, y);
                                pos += stepSize;
297
                                vertices.push_back(cgt::vec3(x, y, z));
Hossain Mahmud's avatar
Hossain Mahmud committed
298
299
                                colors.push_back(p_objectList->getOptionReference(static_cast<int>(i))._color.getValue());
                            }
300
                            pg->addGeometry(FaceGeometry(vertices, std::vector<cgt::vec3>(), colors));
Hossain Mahmud's avatar
Hossain Mahmud committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
                        }
                    }
                }
                data.addData(p_outputImage.getValue() + ".scribbles", pg);

                validate(SCRIBBLE_INVALID);

            }

            // force update of slice renderer if necessary
            if (! (getInvalidationLevel() & VR_INVALID))
                invalidate(SLICES_INVALID);
        }
        if (getInvalidationLevel() & AXISSCALE_INVALID) {
            const ImageData* _copy = static_cast<const ImageData*>(data.getData(p_inputVolume.getValue()).getData());
            ImageData* copy = _copy->clone();
            copy->setMappingInformation(ImageMappingInformation(_copy->getSize(), _copy->getMappingInformation().getOffset(), _copy->getMappingInformation().getVoxelSize() * p_axisScaling.getValue() / _oldScaling ));
            data.addData(p_inputVolume.getValue(), copy);
            _oldScaling = p_axisScaling.getValue();
        }
        if (getInvalidationLevel() & VR_INVALID) {
322
            _tcp.process(data);
Hossain Mahmud's avatar
Hossain Mahmud committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
            _vr.process(data);
        }
        if (getInvalidationLevel() & SLICES_INVALID) {
            _sliceExtractor.p_sliceOrientation.selectById("x");
            _sliceExtractor.p_targetImageID.setValue(p_outputImage.getValue() + ".xSlice");
            _sliceExtractor.process(data);

            _sliceExtractor.p_sliceOrientation.selectById("y");
            _sliceExtractor.p_targetImageID.setValue(p_outputImage.getValue() + ".ySlice");
            _sliceExtractor.process(data);

            _sliceExtractor.p_sliceOrientation.selectById("z");
            _sliceExtractor.p_targetImageID.setValue(p_outputImage.getValue() + ".zSlice");
            _sliceExtractor.process(data);
        }

        composeFinalRendering(data);

        validate(INVALID_RESULT | VR_INVALID | SLICES_INVALID);
    }

    void MicroscopyImageSegmentation::onPropertyChanged(const AbstractProperty* prop) {
        if (prop == _viewportSizeProperty) {
346
347
            p_rightPaneBlockSize.setValue(cgt::ivec2(_viewportSizeProperty->getValue().y / 3, _viewportSizeProperty->getValue().y / 3));
            p_leftPaneSize.setValue(cgt::ivec2(_viewportSizeProperty->getValue().x - _viewportSizeProperty->getValue().y/3, _viewportSizeProperty->getValue().y));
Hossain Mahmud's avatar
Hossain Mahmud committed
348
349

            p_zSize.setValue(p_leftPaneSize.getValue().x < p_leftPaneSize.getValue().y ? 
350
351
                cgt::vec2(p_leftPaneSize.getValue().x, p_leftPaneSize.getValue().x) 
                : cgt::vec2(p_leftPaneSize.getValue().y, p_leftPaneSize.getValue().y));
Hossain Mahmud's avatar
Hossain Mahmud committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
        }
        if (prop == &p_outputImage) {
            _vr.p_outputImage.setValue(p_outputImage.getValue() + ".raycaster");
            _sliceExtractor.p_geometryID.setValue(p_outputImage.getValue() + ".scribbles");
        }
        if (prop == &p_inputVolume) {
            invalidate(VR_INVALID | SLICES_INVALID);
        }
        if (prop == &p_enableScribbling) {
        }
        if (prop == &p_axisScaling) {
            invalidate(VR_INVALID | AXISSCALE_INVALID | SLICES_INVALID);
        }

        VisualizationProcessor::onPropertyChanged(prop);
    }

    void MicroscopyImageSegmentation::composeFinalRendering(DataContainer& data) {
        ScopedTypedData<RenderData> vrImage(data, p_outputImage.getValue() + ".raycaster");
        ScopedTypedData<RenderData> xSliceImage(data, p_outputImage.getValue() + ".xSlice");
        ScopedTypedData<RenderData> ySliceImage(data, p_outputImage.getValue() + ".ySlice");
        ScopedTypedData<RenderData> zSliceImage(data, p_outputImage.getValue() + ".zSlice");

        if (vrImage == 0 && xSliceImage == 0 && ySliceImage == 0 && zSliceImage == 0)
            return;

        FramebufferActivationGuard fag(this);
        createAndAttachColorTexture();
        createAndAttachDepthTexture();

382
        cgt::TextureUnit colorUnit, depthUnit;
Hossain Mahmud's avatar
Hossain Mahmud committed
383
384
        _shader->activate();

385
386
387
        cgt::vec2 leftPane(p_leftPaneSize.getValue());
        cgt::vec2 blockSize(p_rightPaneBlockSize.getValue());
        cgt::vec2 zsize(p_zSize.getValue());
Hossain Mahmud's avatar
Hossain Mahmud committed
388

389
390
391
392
        cgt::vec2 zsPos(cgt::vec2(0 + (leftPane.x - zsize.x) / 2, 0 + (leftPane.y - zsize.y) / 2));
        cgt::vec2 xsPos(cgt::vec2(leftPane.x, blockSize.y*0));
        cgt::vec2 ysPos(cgt::vec2(leftPane.x, blockSize.y*1));
        cgt::vec2 vrPos(cgt::vec2(leftPane.x, blockSize.y*2));
Hossain Mahmud's avatar
Hossain Mahmud committed
393

394
        _shader->setUniform("_projectionMatrix", cgt::mat4::createOrtho(0, _viewportSizeProperty->getValue().x, _viewportSizeProperty->getValue().y, 0, -1, 1));
Hossain Mahmud's avatar
Hossain Mahmud committed
395
396
397
398
399
400
401
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        if (vrImage != 0) {
            decorateRenderProlog(data, _shader);
            _shader->setUniform("_renderBackground", true);

            vrImage->bind(_shader, colorUnit, depthUnit);
402
403
            _shader->setUniform("_modelMatrix", cgt::mat4::createScale(cgt::vec3(blockSize.x, blockSize.y, .5f)));
            _shader->setUniform("_viewMatrix", cgt::mat4::createTranslation(cgt::vec3(vrPos.x, vrPos.y, 0.f)));
Hossain Mahmud's avatar
Hossain Mahmud committed
404
405
406
407
408
409
410
            _quad->render(GL_POLYGON);

            _shader->setUniform("_renderBackground", false);
            decorateRenderEpilog(_shader);
        }
        if (zSliceImage != 0) {
            zSliceImage->bind(_shader, colorUnit, depthUnit);
411
412
            _shader->setUniform("_modelMatrix", cgt::mat4::createScale(cgt::vec3(zsize.x, zsize.y, .5f)));
            _shader->setUniform("_viewMatrix", cgt::mat4::createTranslation(cgt::vec3(zsPos.x, zsPos.y, 0.f)));
Hossain Mahmud's avatar
Hossain Mahmud committed
413
414
415
416
            _quad->render(GL_POLYGON);
        }
        if (ySliceImage != 0) {
            ySliceImage->bind(_shader, colorUnit, depthUnit);
417
418
            _shader->setUniform("_modelMatrix", cgt::mat4::createScale(cgt::vec3(blockSize.x, blockSize.y, .5f)));
            _shader->setUniform("_viewMatrix", cgt::mat4::createTranslation(cgt::vec3(ysPos.x, ysPos.y, 0.f)));
Hossain Mahmud's avatar
Hossain Mahmud committed
419
420
421
422
            _quad->render(GL_POLYGON);
        }
        if (xSliceImage != 0) {
            xSliceImage->bind(_shader, colorUnit, depthUnit);
423
424
            _shader->setUniform("_modelMatrix", cgt::mat4::createScale(cgt::vec3(blockSize.x, blockSize.y, .5f)));
            _shader->setUniform("_viewMatrix", cgt::mat4::createTranslation(cgt::vec3(xsPos.x, xsPos.y, 0.f)));
Hossain Mahmud's avatar
Hossain Mahmud committed
425
426
427
428
            _quad->render(GL_POLYGON);
        }

        _shader->deactivate();
429
        cgt::TextureUnit::setZeroUnit();
Hossain Mahmud's avatar
Hossain Mahmud committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
        LGL_ERROR;

        data.addData(p_outputImage.getValue(), new RenderData(_fbo));
    }

    void MicroscopyImageSegmentation::onProcessorInvalidated(AbstractProcessor* processor) {
        // make sure to only invalidate ourself if the invalidation is not triggered by us
        // => the _locked state is a trustworthy source for this information :)
        if (! isLocked()) {
            if (processor == &_vr) {
                invalidate(VR_INVALID);
            }
            if (processor == &_sliceExtractor) {
                invalidate(SLICES_INVALID);
            }

            invalidate(AbstractProcessor::INVALID_RESULT);
        }
    }

    void MicroscopyImageSegmentation::updateProperties(DataContainer& dc) {
        ScopedTypedData<ImageData> img(dc, p_inputVolume.getValue());
        _sliceExtractor.p_transferFunction.setImageHandle(img.getDataHandle());
        static_cast<TransferFunctionProperty*>(_vr.getNestedProperty("RaycasterProps::TransferFunction"))->setImageHandle(img.getDataHandle());

        if (img != 0) {
456
            const cgt::svec3& imgSize = img->getSize();
Hossain Mahmud's avatar
Hossain Mahmud committed
457
458
459
460
461
462
463
464
465
466
467
468
469
            if (_sliceExtractor.p_xSliceNumber.getMaxValue() != static_cast<int>(imgSize.x) - 1){
                _sliceExtractor.p_xSliceNumber.setMaxValue(static_cast<int>(imgSize.x) - 1);
                _sliceExtractor.p_xSliceNumber.setValue(static_cast<int>(imgSize.x) / 2);
            }
            if (_sliceExtractor.p_ySliceNumber.getMaxValue() != static_cast<int>(imgSize.y) - 1){
                _sliceExtractor.p_ySliceNumber.setMaxValue(static_cast<int>(imgSize.y) - 1);
                _sliceExtractor.p_ySliceNumber.setValue(static_cast<int>(imgSize.y) / 2);
            }
            if (_sliceExtractor.p_zSliceNumber.getMaxValue() != static_cast<int>(imgSize.z) - 1){
                _sliceExtractor.p_zSliceNumber.setMaxValue(static_cast<int>(imgSize.z) - 1);
                _sliceExtractor.p_zSliceNumber.setValue(static_cast<int>(imgSize.z) / 2);
            }

470
            _tcp.reinitializeCamera(img->getWorldBounds());
Hossain Mahmud's avatar
Hossain Mahmud committed
471
472
473
474
475
        }

        validate(AbstractProcessor::INVALID_PROPERTIES);
    }

476
477
478
479
    void MicroscopyImageSegmentation::onEvent(cgt::Event* e) {
        cgt::vec2 leftPane(p_leftPaneSize.getValue());
        cgt::vec2 blockSize(p_rightPaneBlockSize.getValue());
        cgt::vec2 zsize(p_zSize.getValue());
Hossain Mahmud's avatar
Hossain Mahmud committed
480

481
482
483
484
        cgt::vec2 zsPos(cgt::vec2(0 + (leftPane.x - zsize.x) / 2, 0 + (leftPane.y - zsize.y) / 2));
        cgt::vec2 xsPos(cgt::vec2(leftPane.x, blockSize.y*0));
        cgt::vec2 ysPos(cgt::vec2(leftPane.x, blockSize.y*1));
        cgt::vec2 vrPos(cgt::vec2(leftPane.x, blockSize.y*2));
Hossain Mahmud's avatar
Hossain Mahmud committed
485

486
487
        if (typeid(*e) == typeid(cgt::KeyEvent)) {
            cgt::KeyEvent* ke = static_cast<cgt::KeyEvent*>(e);
Hossain Mahmud's avatar
Hossain Mahmud committed
488
            ContourObject& selectedObject = this->p_objectList->getOptionReference();
489
            if (ke->modifiers() & cgt::Event::CTRL && ke->pressed() && ke->keyCode() == 'z') {
Hossain Mahmud's avatar
Hossain Mahmud committed
490
491
492
493
494
495
496
497
498
                if (selectedObject._points.size() == 0) {
                    if (selectedObject._objectsCoordinates.size() > 0) {
                        selectedObject._points = selectedObject._objectsCoordinates[selectedObject._objectsCoordinates.size() - 1];
                        selectedObject._objectsCoordinates.erase(selectedObject._objectsCoordinates.end() - 1);
                    }
                }
                if (selectedObject._points.size() > 0) 
                    selectedObject._points.erase(selectedObject._points.end()-1);
            }
499
            else if (ke->keyCode() == cgt::KeyEvent::K_DELETE) {
Hossain Mahmud's avatar
Hossain Mahmud committed
500
501
502
503
504
505
506
                if (true == editVoxel) {
                    selectedObject._points.erase(selectedObject._points.begin() + insertNextVoxelAt);
                    editVoxel = false;
                    insertNextVoxelAt = -1;
                    invalidate(INVALID_RESULT | SCRIBBLE_INVALID);
                }
            }
507
508
            else if (ke->keyCode() == cgt::KeyEvent::K_UP) {
                _sliceExtractor.p_offset.setValue(_sliceExtractor.p_offset.getValue() - cgt::ivec2(0, 5));
Hossain Mahmud's avatar
Hossain Mahmud committed
509
            }
510
511
            else if (ke->keyCode() == cgt::KeyEvent::K_DOWN) {
                _sliceExtractor.p_offset.setValue(_sliceExtractor.p_offset.getValue() + cgt::ivec2(0, 5));
Hossain Mahmud's avatar
Hossain Mahmud committed
512
            }
513
514
            else if (ke->keyCode() == cgt::KeyEvent::K_LEFT) {
                _sliceExtractor.p_offset.setValue(_sliceExtractor.p_offset.getValue() - cgt::ivec2(5, 0));
Hossain Mahmud's avatar
Hossain Mahmud committed
515
            }
516
517
            else if (ke->keyCode() == cgt::KeyEvent::K_RIGHT) {
                _sliceExtractor.p_offset.setValue(_sliceExtractor.p_offset.getValue() + cgt::ivec2(5, 0));
Hossain Mahmud's avatar
Hossain Mahmud committed
518
519
520
521
522
            }
            invalidate(INVALID_RESULT | SCRIBBLE_INVALID);
        }

        // forward the event to the corresponding event listeners depending on the mouse position
523
524
        if (typeid(*e) == typeid(cgt::MouseEvent)) {
            cgt::MouseEvent* me = static_cast<cgt::MouseEvent*>(e);
Hossain Mahmud's avatar
Hossain Mahmud committed
525
526
527
528
529
530

            ContourObject& selectedObject = this->p_objectList->getOptionReference();

            // we're currently on the slice view (left-hand) side and not in the process of changing the camera trackball
            if (!_mousePressedInRaycaster && (me->x() <= leftPane.x || me->y() >= blockSize.y)) {
                // Mouse wheel has changed -> cycle slices
531
                if (me->action() == cgt::MouseEvent::WHEEL && !(me->modifiers() & cgt::Event::CTRL)) {
Hossain Mahmud's avatar
Hossain Mahmud committed
532
533
534
535
536
537
538
539
                    if (me->y() >= blockSize.y*2 && me->x() >= leftPane.x)
                        _xSliceHandler.onEvent(e);
                    else if (me->y() >= blockSize.y && me->x() >= leftPane.x)
                        _ySliceHandler.onEvent(e);
                    else
                        _zSliceHandler.onEvent(e);
                }
                // CTRL+wheel -> adjust zooming
540
                else if (me->action() == cgt::MouseEvent::WHEEL && me->modifiers() & cgt::Event::CTRL) {
Hossain Mahmud's avatar
Hossain Mahmud committed
541
                    if (_sliceExtractor.p_fitToWindow.getValue() == false) {
542
                        if (me->button() & cgt::MouseEvent::MOUSE_WHEEL_DOWN)
Hossain Mahmud's avatar
Hossain Mahmud committed
543
544
545
546
547
548
                            _sliceExtractor.p_scalingFactor.setValue(_sliceExtractor.p_scalingFactor.getValue() * 0.9f);
                        else 
                            _sliceExtractor.p_scalingFactor.setValue(_sliceExtractor.p_scalingFactor.getValue() * 1.11f);
                    }
                }
                else if (p_enableScribbling.getValue() ) {
549
550
                    if (me->action() == cgt::MouseEvent::PRESSED) {
                        if (me->button() & cgt::MouseEvent::MOUSE_BUTTON_RIGHT) {
Hossain Mahmud's avatar
Hossain Mahmud committed
551
552
553
554
555
556
557
                            selectedObject.addObject();
                            //_sliceExtractor.onEvent(me);
                            invalidate(INVALID_RESULT | SCRIBBLE_INVALID);
                        }
                    }

                    // lock this processor, so that the slice orientation's setting does not change
558
                    AbstractProcessor::ScopedLock lock(this);
Hossain Mahmud's avatar
Hossain Mahmud committed
559
560
561

                    if (me->y() <= blockSize.y * 2 && me->x() >= leftPane.x) {
                        _sliceExtractor.p_sliceOrientation.selectByOption(SliceExtractor::XZ_PLANE);//xz
562
                        cgt::MouseEvent adjustedMe(
Hossain Mahmud's avatar
Hossain Mahmud committed
563
564
565
566
567
568
569
570
571
572
                            me->x() - p_leftPaneSize.getValue().x, me->y() - p_rightPaneBlockSize.getValue().y,
                            me->action(), me->modifiers(), me->button(),
                            p_rightPaneBlockSize.getValue()
                            );
                        _sliceExtractor.setViewportSizeProperty(&p_rightPaneBlockSize);
                        _sliceExtractor.onEvent(&adjustedMe);
                        _sliceExtractor.setViewportSizeProperty(&p_zSize);
                    }
                    else if (me->y() >= blockSize.y * 2 && me->x() >= leftPane.x) {
                        _sliceExtractor.p_sliceOrientation.selectByOption(SliceExtractor::YZ_PLANE);//yz
573
                        cgt::MouseEvent adjustedMe(
Hossain Mahmud's avatar
Hossain Mahmud committed
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
                            me->x() - p_leftPaneSize.getValue().x, me->y() - p_rightPaneBlockSize.getValue().y *2,
                            me->action(), me->modifiers(), me->button(),
                            p_rightPaneBlockSize.getValue()
                            );

                        _sliceExtractor.setViewportSizeProperty(&p_rightPaneBlockSize);
                        _sliceExtractor.onEvent(&adjustedMe);
                        _sliceExtractor.setViewportSizeProperty(&p_zSize);
                    }
                    else if (me->x() <  (leftPane.x - zsize.x) / 2 || me->y() <  (leftPane.y - zsize.y) / 2
                        || me->x() >  (leftPane.x - zsize.x) / 2 + zsize.x || me->y() >  (leftPane.y - zsize.y) / 2 +zsize.y){
                            // action in blank space
                    }
                    else {
                        // If ALT is pressed mark for edit. next scribble is edit scribble. the location of insert is insertNextVoxelAt
589
                        if (me->modifiers() & cgt::Event::ALT)
Hossain Mahmud's avatar
Hossain Mahmud committed
590
591
592
                            editVoxel = true;

                        _sliceExtractor.p_sliceOrientation.selectByOption(SliceExtractor::XY_PLANE);
593
                        cgt::MouseEvent adjustedMe(
Hossain Mahmud's avatar
Hossain Mahmud committed
594
595
596
597
598
599
600
601
602
603
                            me->x() -  (leftPane.x - zsize.x) / 2, me->y() -  (leftPane.y - zsize.y) / 2,
                            me->action(), me->modifiers(), me->button(),
                            p_zSize.getValue()
                            );
                        _sliceExtractor.onEvent(&adjustedMe);
                    }
                }
            }
            else {
                // raycasting trackball navigation
604
                if (me->action() == cgt::MouseEvent::PRESSED)
Hossain Mahmud's avatar
Hossain Mahmud committed
605
                    _mousePressedInRaycaster = true;
606
                else if (me->action() == cgt::MouseEvent::RELEASED)
Hossain Mahmud's avatar
Hossain Mahmud committed
607
608
                    _mousePressedInRaycaster = false;

609
                cgt::MouseEvent adjustedMe(
Hossain Mahmud's avatar
Hossain Mahmud committed
610
611
612
613
614
                    me->x() - p_leftPaneSize.getValue().x,
                    me->y(),
                    me->action(),
                    me->modifiers(),
                    me->button(),
615
                    //me->viewport() - cgt::ivec2(p_sliceRenderSize.getValue().x, 0)
Hossain Mahmud's avatar
Hossain Mahmud committed
616
617
                    //me->viewport() - p_volumeRenderSize.getValue());
                    p_rightPaneBlockSize.getValue());
618
                _tcp.onEvent(&adjustedMe);
Hossain Mahmud's avatar
Hossain Mahmud committed
619
620
621
622
            }
        }
    }

623
    void MicroscopyImageSegmentation::onSliceExtractorScribblePainted(cgt::vec3 voxel) {
Hossain Mahmud's avatar
Hossain Mahmud committed
624
625
626
627
628
629
630
631
632
633
        ContourObject& selectedObject = this->p_objectList->getOptionReference();

        if (!(selectedObject._objectName == first[0]._value._objectName)) {
            if (true == editVoxel && insertNextVoxelAt == -1) {
                unsigned long dist = -1;
                if (!selectedObject._points.size()) {
                    editVoxel = false;
                    return;
                }

634
                for (size_t i = 0; i < selectedObject._points.size(); i++) {
Hossain Mahmud's avatar
Hossain Mahmud committed
635
636
                    unsigned long dist2 = distanceSqr(selectedObject._points[i], voxel);
                    if (dist > dist2) {
637
                        insertNextVoxelAt = static_cast<int>(i);
Hossain Mahmud's avatar
Hossain Mahmud committed
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
                        dist = dist2;
                    }
                }
            }
            else if (true == editVoxel) {
                selectedObject._points.erase(selectedObject._points.begin() + insertNextVoxelAt);
                selectedObject._points.insert(selectedObject._points.begin() + insertNextVoxelAt, voxel);
                editVoxel = false;
                insertNextVoxelAt = -1;
                invalidate(INVALID_RESULT | SCRIBBLE_INVALID);
            }
            else {
                selectedObject._points.push_back(voxel);
                invalidate(INVALID_RESULT | SCRIBBLE_INVALID);
            }
        }
    }

    void MicroscopyImageSegmentation::onAddButtonClicked() {
        std::string name = _objectNamePrefix;
        name += StringUtils::toString(++_objectNameSuffix, 2, '0');
        ContourObject obj(name);
660
        obj._color.setValue(cgt::vec4(
661
662
663
            static_cast<float>(0.9 - 0.2 * (_objectNameSuffix%3) + 0.15 *(_objectNameSuffix/3)),
            static_cast<float>(0.2 + 0.2 * (_objectNameSuffix%3) - 0.15 *(_objectNameSuffix/3)), 
            static_cast<float>(0.5 + 0.2 * (_objectNameSuffix%3) - 0.15 *(_objectNameSuffix/3)), 1));
Hossain Mahmud's avatar
Hossain Mahmud committed
664
665
666
667
668
669
670
671
672
673
        p_objectList->addOption(GenericOption<ContourObject>(name, name, obj));
        p_objectList->selectById(name);
    }

    void MicroscopyImageSegmentation::onDeleteButtonClicked() {
        p_objectList->removeCurrent();
        invalidate(INVALID_RESULT | SCRIBBLE_INVALID);
    }

    void MicroscopyImageSegmentation::onPaintColorChanged(const AbstractProperty *prop) {
674
        ContourObject &obj = this->p_objectList->getOptionReference();
Hossain Mahmud's avatar
Hossain Mahmud committed
675
676
677
678
679
680
        obj._color.setValue(p_paintColor.getValue());
        this->p_objectList->updateCurrent(obj);

        invalidate(INVALID_RESULT | SCRIBBLE_INVALID);
    }
    void MicroscopyImageSegmentation::onObjectSelectionChanged (const AbstractProperty *prop) {
681
        const ContourObject &obj = this->p_objectList->getOptionValue();
Hossain Mahmud's avatar
Hossain Mahmud committed
682
683
684
685
686
        this->p_paintColor.setValue(obj._color.getValue());

        invalidate(INVALID_RESULT | SCRIBBLE_INVALID);
    }

687
    double MicroscopyImageSegmentation::distanceSqr(cgt::vec3 src, cgt::vec3 dest) {
Hossain Mahmud's avatar
Hossain Mahmud committed
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
        return (src.x - dest.x)*(src.x - dest.x) 
            + (src.y - dest.y)*(src.y - dest.y) 
            + (src.z - dest.z)*(src.z - dest.z);
    }

    void MicroscopyImageSegmentation::onCSVFileSelected(const AbstractProperty *prop) {
        onSaveCSVButtonClicked();
    }
    void MicroscopyImageSegmentation::onSaveCSVButtonClicked() {
        std::ofstream csvFile;
        csvFile.open(p_csvPath.getValue());

        csvFile << "ObjectNo,ContourNo,X,Y,Z\n";
        if (csvFile.is_open()) {
            for (size_t i = 1; i < p_objectList->getOptionCount(); i++) {
                csvFile << i<<",,,,\n";
                for (size_t j = 0; j < p_objectList->getOptionReference(static_cast<int>(i))._objectsCoordinates.size(); j++) {
705
                    const std::vector<cgt::ivec3 >& it = p_objectList->getOptionReference(static_cast<int>(i))._objectsCoordinates[j];
Hossain Mahmud's avatar
Hossain Mahmud committed
706
707
708
709
710
711
712
713
714
715
716
717

                    csvFile << "," <<j << ",,,\n";
                    for (size_t k = 0; k <  it.size(); k++) {
                        csvFile << ",," << it[k].x << "," << it[k].y << "," << it[k].z <<",\n";
                    }                    
                }
            }
        }
        csvFile.close();
    }
}