transferfunctionproperty.cpp 6.73 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-2013, 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 Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, 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
// 
// ================================================================================================

schultezub's avatar
schultezub committed
25
#include "transferfunctionproperty.h"
26
27
#include "core/datastructures/imagedata.h"
#include "core/datastructures/imagerepresentationlocal.h"
28

29
30
#include <tbb/tbb.h>

schultezub's avatar
schultezub committed
31
namespace campvis {
32

schultezub's avatar
schultezub committed
33
    const std::string TransferFunctionProperty::loggerCat_ = "CAMPVis.core.datastructures.TransferFunctionProperty";
34

35
36
    TransferFunctionProperty::TransferFunctionProperty(const std::string& name, const std::string& title, AbstractTransferFunction* tf, int invalidationLevel /*= AbstractProcessor::INVALID_RESULT*/)
        : AbstractProperty(name, title, invalidationLevel)
37
        , _transferFunction(tf)
38
39
        , _imageHandle(0)
        , _autoFitWindowToData(true)
40
41
    {
        tgtAssert(tf != 0, "Assigned transfer function must not be 0.");
42
        
schultezub's avatar
schultezub committed
43
        tf->s_changed.connect(this, &TransferFunctionProperty::onTFChanged);
44
45
46
47
        tf->s_intensityDomainChanged.connect(this, &TransferFunctionProperty::onTfIntensityDomainChanged);

        _intensityHistogram = 0;
        _dirtyHistogram = false;
48
49
50
    }

    TransferFunctionProperty::~TransferFunctionProperty() {
schultezub's avatar
schultezub committed
51
        delete _transferFunction;
52
        delete _intensityHistogram;
53
54
55
56
57
58
59
    }

    AbstractTransferFunction* TransferFunctionProperty::getTF() {
        return _transferFunction;
    }

    void TransferFunctionProperty::onTFChanged() {
schultezub's avatar
schultezub committed
60
        s_changed(this);
61
62
    }

schultezub's avatar
schultezub committed
63
    void TransferFunctionProperty::deinit() {
64
65
        _transferFunction->s_changed.disconnect(this);
        _transferFunction->s_intensityDomainChanged.disconnect(this);
schultezub's avatar
schultezub committed
66
        _transferFunction->deinit();
67
        _imageHandle = DataHandle(0);
schultezub's avatar
schultezub committed
68
69
    }

70
71
72
    void TransferFunctionProperty::replaceTF(AbstractTransferFunction* tf) {
        tgtAssert(tf != 0, "Transfer function must not be 0.");
        s_BeforeTFReplace(_transferFunction);
73

74
        if (_transferFunction != 0) {
75
            _transferFunction->s_changed.disconnect(this);
76
            _transferFunction->s_intensityDomainChanged.disconnect(this);
77
78
            _transferFunction->deinit();
        }
79
        delete _transferFunction;
80
81


82
        _transferFunction = tf;
83
        if (_transferFunction != 0) {
84
            _transferFunction->s_changed.connect(this, &TransferFunctionProperty::onTFChanged);
85
86
            _transferFunction->s_intensityDomainChanged.connect(this, &TransferFunctionProperty::onTfIntensityDomainChanged);
        }
87

88
89
90
        s_AfterTFReplace(_transferFunction);
    }

91
92
93
94
    void TransferFunctionProperty::addSharedProperty(AbstractProperty* prop) {
        tgtAssert(false, "Sharing of TF properties not supported!");
    }

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    campvis::DataHandle TransferFunctionProperty::getImageHandle() const {
        return _imageHandle;
    }

    void TransferFunctionProperty::setImageHandle(DataHandle imageHandle) {
        tgtAssert(
            imageHandle.getData() == 0 || dynamic_cast<const ImageData*>(imageHandle.getData()) != 0, 
            "The data in the image handle must either be 0 or point to a valid ImageData object!");

        if (_autoFitWindowToData && imageHandle.getData() != 0) {
            if (const ImageData* id = dynamic_cast<const ImageData*>(imageHandle.getData())) {
            	const ImageRepresentationLocal* localRep = id->getRepresentation<ImageRepresentationLocal>();
                if (localRep != 0) {
                    const Interval<float>& ii = localRep->getNormalizedIntensityRange();
                    _transferFunction->setIntensityDomain(tgt::vec2(ii.getLeft(), ii.getRight()));
                }
            }
        }

        _imageHandle = imageHandle;
115
        _dirtyHistogram = true;
116
117
118
119
120
121
122
123
124
125
126
127
        s_imageHandleChanged();
    }

    void TransferFunctionProperty::setAutoFitWindowToData(bool newValue) {
        _autoFitWindowToData = newValue;
        s_autoFitWindowToDataChanged();
    }

    bool TransferFunctionProperty::getAutoFitWindowToData() const {
        return _autoFitWindowToData;
    }

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
    void TransferFunctionProperty::computeIntensityHistogram() const {
        IntensityHistogramType* newHistogram = 0;

        // create new histogram according to the current intensity domain
        ImageRepresentationLocal::ScopedRepresentation repLocal(_imageHandle);
        if (repLocal != 0) {
            float mins = _transferFunction->getIntensityDomain().x;
            float maxs = _transferFunction->getIntensityDomain().y;
            size_t numBuckets = std::min(WeaklyTypedPointer::numBytes(repLocal->getWeaklyTypedPointer()._baseType) << 8, static_cast<size_t>(512));
            newHistogram = new IntensityHistogramType(&mins, &maxs, &numBuckets);
            tbb::parallel_for(tbb::blocked_range<size_t>(0, repLocal->getNumElements()), [&] (const tbb::blocked_range<size_t>& range) {
                for (size_t i = range.begin(); i != range.end(); ++i) {
                    float value = repLocal->getElementNormalized(i, 0);
                    newHistogram->addSample(&value);
                }
            });
        }

        // atomically replace old histogram with the new one and delete the old one.
        IntensityHistogramType* oldHistogram = _intensityHistogram.fetch_and_store(newHistogram);
        delete oldHistogram;
        _dirtyHistogram = false;
    }

    const TransferFunctionProperty::IntensityHistogramType* TransferFunctionProperty::getIntensityHistogram() const {
        if (_dirtyHistogram) {
            computeIntensityHistogram();
        }

        return _intensityHistogram;
    }

    void TransferFunctionProperty::onTfIntensityDomainChanged() {
        _dirtyHistogram = true;
    }

164
}