genericimagedatalocal.h 16.5 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, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
8
9
//      Chair for Computer Aided Medical Procedures
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, Germany
schultezub's avatar
schultezub committed
10
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 
// 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.
// 
// ================================================================================================

30
31
32
33
34
35
36
#ifndef GENERICIMAGEDATALOCAL_H__
#define GENERICIMAGEDATALOCAL_H__

#include "tgt/vector.h"
#include "core/datastructures/imagedatalocal.h"
#include "core/tools/typetraits.h"

37
#include <cstring>  // needed for memcpy
38

schultezub's avatar
schultezub committed
39
namespace campvis {
40

41
42
43
44
45
46
47
    /**
     * Templated version of ImageDataLocal, storing image data in the local memory.
     *
     * \sa      TypeTraits
     * \tparam  BASETYPE    Base type of the image data (type of a single channel of an image element)
     * \tparam  NUMCHANNELS Number of channels of the image data.
     */
48
49
50
    template<typename BASETYPE, size_t NUMCHANNELS>
    class GenericImageDataLocal : public ImageDataLocal {
    public:
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        /// Type of one single image element
        typedef typename TypeTraits<BASETYPE, NUMCHANNELS>::ElementType ElementType;
        /// Type of this template instantiation
        typedef GenericImageDataLocal<BASETYPE, NUMCHANNELS> ThisType;

        /**
         * Creates a new strongly typed ImageData object storing the image in the local memory.
         * 
         * \param   dimensionality  Dimensionality of data
         * \param   size            Size of this image (number of elements per dimension)
         * \param   data            Pointer to the image data, must not be 0, GenericImageDataLocal takes ownership of this pointer!
         */
        GenericImageDataLocal(size_t dimensionality, const tgt::svec3& size, ElementType* data);

        /**
         * Destructor
         */
68
69
70
71
        virtual ~GenericImageDataLocal();


        /// \see AbstractData::clone()
72
        virtual ThisType* clone() const;
73

74
75
76
77
78
79
        /// \see AbstractData::getLocalMemoryFootprint()
        virtual size_t getLocalMemoryFootprint() const;

        /// \see AbstractData::getVideoMemoryFootprint()
        virtual size_t getVideoMemoryFootprint() const;

80
        /// \see ImageData::getSubImage
81
82
        virtual ThisType* getSubImage(const tgt::svec3& llf, const tgt::svec3& urb) const;

83
84
85
86
87
        /**
         * Returns a WeaklyTypedPointer to the image data.
         * \note    The pointer is still owned by this ImageDataLocal. If you want a copy, use clone().
         * \return  A WeaklyTypedPointer to the image data.
         */
schultezub's avatar
schultezub committed
88
89
        virtual const WeaklyTypedPointer getWeaklyTypedPointer() const;

90
91
92
        /// \see ImageDataLocal::getElementNormalized
        virtual float getElementNormalized(size_t index, size_t channel) const;

93
94
95
        /// \see ImageDataLocal::getElementNormalized
        virtual float getElementNormalized(const tgt::svec3& position, size_t channel) const;

96
97
98
        /// \see ImageDataLocal::getElementNormalizedLinear
        virtual float getElementNormalizedLinear(const tgt::vec3& position, size_t channel) const;

99
100
101
        /// \see ImageDataLocal::setElementNormalized
        virtual void setElementNormalized(size_t index, size_t channel, float value);

102
103
104
105
106
107
108
109
110
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
        /// \see ImageDataLocal::setElementNormalized
        virtual void setElementNormalized(const tgt::svec3& position, size_t channel, float value);


        /**
         * Returns the image element at the given index \a index.
         * \param   index   Array index of the image element to return.
         * \return  Image element at index \a index.
         */
        ElementType& getElement(size_t index);

        /**
         * Returns the image element at the given position in image space.
         * \param   position    Pixel/voxel coordinates of the image element to return.
         * \return  Image element at the coordinates \a position.
         */
        ElementType& getElement(const tgt::svec3& position);

        /**
         * Returns the image element at the given index \a index.
         * \param   index   Array index of the image element to return.
         * \return  Image element at index \a index.
         */
        const ElementType& getElement(size_t index) const;

        /**
         * Returns the image element at the given position in image space.
         * \param   position    Pixel/voxel coordinates of the image element to return.
         * \return  Image element at the coordinates \a position.
         */
        const ElementType& getElement(const tgt::svec3& position) const;

        /**
         * Sets the image element at the given index to the value \a value.
         * \param   index   Array index of the image element to change.
         * \param   value   New value of the specified image element.
         */
        void setElement(size_t index, const ElementType& value);

        /**
         * Sets the image element at the given position in image space to the value \a value.
         * \param   position    Pixel/voxel coordinates of the image element to change.
         * \param   value       New value of the specified image element.
         */
        void setElement(const tgt::svec3& position, const ElementType& value);

        /**
         * Returns a pointer to the image data.
         * \return  _data
         */
        ElementType* getImageData();

        /**
         * Returns a const pointer to the image data.
         * \return  _data
         */
        const ElementType* getImageData() const;


        /**
         * Returns the image element at the given coordinates \a position using bi-/trilinear filtering.
         * \param   position    Pixel/voxel coordinates of the image element to return.
         * \return  Bi-/Trilinear filtered image element at the specified coordinates.
         */
        ElementType getElementLinear(const tgt::vec3 position) const;

168
169
    protected:

170
        ElementType* _data;
171
172
173

    };

174
175
176
// = Template implementation ======================================================================

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
177
    campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::GenericImageDataLocal(size_t dimensionality, const tgt::svec3& size, ElementType* data)
178
179
180
        : ImageDataLocal(dimensionality, size, TypeTraits<BASETYPE, NUMCHANNELS>::weaklyTypedPointerBaseType, NUMCHANNELS)
        , _data(data)
    {
181
182
183
184
185
        if (_data == 0) {
            size_t numElements = tgt::hmul(_size);
            _data = new ElementType[numElements];
            memset(_data, 0, numElements * TypeTraits<BASETYPE, NUMCHANNELS>::elementSize);
        }
186
187
188
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
189
    campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::~GenericImageDataLocal() {
190
191
192
193
        delete _data;
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
194
    GenericImageDataLocal<BASETYPE, NUMCHANNELS>* campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::clone() const {
195
196
197
198
199
200
201
        size_t numElements = tgt::hmul(_size);
        ElementType* newData = new ElementType[numElements];
        memcpy(newData, _data, numElements * sizeof(ElementType));

        return new ThisType(_dimensionality, _size, newData);
    }

202
    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
203
    size_t campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getLocalMemoryFootprint() const {
204
205
206
207
        return sizeof(*this) + _numElements * sizeof(ElementType);
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
208
    size_t campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getVideoMemoryFootprint() const {
209
210
211
        return 0;
    }

212
    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
213
    GenericImageDataLocal<BASETYPE, NUMCHANNELS>* campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getSubImage(const tgt::svec3& llf, const tgt::svec3& urb) const {
schultezub's avatar
schultezub committed
214
        tgtAssert(tgt::hor(tgt::lessThan(llf, urb)), "Coordinates in LLF must be component-wise smaller than the ones in URB!");
215

schultezub's avatar
schultezub committed
216
        tgt::svec3 newSize = urb - llf + tgt::svec3(1);
217
218
219
220
221
        if (newSize == _size) {
            // nothing has changed, just provide a copy:
            return clone();
        }

schultezub's avatar
schultezub committed
222
223
224
225
226
227
228
        tgt::bvec3 tmp(tgt::greaterThan(newSize, tgt::svec3(1)));
        size_t newDimensionality = 0;
        for (size_t i = 0; i < 3; ++i) {
            if (tmp[i] == true)
                ++newDimensionality;
        }

229
        size_t numBytesPerElement = sizeof(ElementType);
230
        size_t numElements = tgt::hmul(newSize);
231
232
233
234
        ElementType* newData = new ElementType[numElements];

        // slice image data into new array
        size_t index = 0;
schultezub's avatar
schultezub committed
235
236
        for (size_t z = llf.z; z <= urb.z; ++z) {
            for (size_t y = llf.y; y <= urb.y; ++y) {
237
238
239
240
241
242
                size_t offset = llf.x + (y * _size.x) + (z * _size.y * _size.x);
                memcpy(newData + index, _data + offset, newSize.x * numBytesPerElement);
                index += newSize.x;
            }
        }        

schultezub's avatar
schultezub committed
243
244
245
246
        return new ThisType(newDimensionality, newSize, newData);
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
247
    const WeaklyTypedPointer campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getWeaklyTypedPointer() const {
schultezub's avatar
schultezub committed
248
        return WeaklyTypedPointer(TypeTraits<BASETYPE, NUMCHANNELS>::weaklyTypedPointerBaseType, NUMCHANNELS, _data);
249
250
    }

251
    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
252
    float campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getElementNormalized(size_t index, size_t channel) const {
253
254
255
256
        tgtAssert(channel >= 0 && channel < NUMCHANNELS, "Channel out of bounds!");
        return TypeNormalizer::normalizeToFloat(TypeTraits<BASETYPE, NUMCHANNELS>::getChannel(getElement(index), channel));
    }

257
    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
258
    float campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getElementNormalized(const tgt::svec3& position, size_t channel) const {
259
260
261
262
        return getElementNormalized(positionToIndex(position), channel);
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
263
    void campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::setElementNormalized(size_t index, size_t channel, float value) {
264
        tgtAssert(channel >= 0 && channel < NUMCHANNELS, "Channel out of bounds!");
265
        TypeTraits<BASETYPE, NUMCHANNELS>::setChannel(getElement(index), channel, TypeNormalizer::denormalizeFromFloat<BASETYPE>(value));
266
267
268
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
269
    void campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::setElementNormalized(const tgt::svec3& position, size_t channel, float value) {
270
        setElementNormalized(positionToIndex(position), channel, value);
271
272
273
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
274
    typename campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::ElementType& campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getElement(size_t position) {
275
276
277
        tgtAssert(position >= 0 && position < _numElements, "Position out of bounds!");
        return _data[position];
    }
278

279
    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
280
    typename campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::ElementType& campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getElement(const tgt::svec3& position) {
281
282
283
284
        return getElement(positionToIndex(position));
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
285
    const typename campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::ElementType& campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getElement(size_t position) const {
286
287
288
289
290
        tgtAssert(position >= 0 && position < _numElements, "Position out of bounds!");
        return _data[position];
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
291
    const typename campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::ElementType& campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getElement(const tgt::svec3& position) const {
292
293
294
295
        return getElement(positionToIndex(position));
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
296
    void campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::setElement(size_t position, const ElementType& value) {
297
298
        tgtAssert(position >= 0 && position < _numElements, "Position out of bounds!");
        _data[position] = value;
299

300
301
302
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
303
    void campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::setElement(const tgt::svec3& position, const ElementType& value) {
304
305
306
307
        _data[positionToIndex(position)] = value;
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
308
    typename campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::ElementType* campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getImageData() {
309
310
311
312
        return _data;
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
313
    const typename campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::ElementType* campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getImageData() const {
314
315
316
317
        return _data;
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
318
    typename campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::ElementType campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getElementLinear(const tgt::vec3 position) const {
319
320
321
322
323
        // yet to be implemented
        // TODO: Check wether pixel/voxel coordinates lie on the edges or on the center of the pixels/voxels
        tgtAssert(false, "Yet to be implemented!");
        return ElementType(0);
    }
324
325
326


    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
327
    float campvis::GenericImageDataLocal<BASETYPE, NUMCHANNELS>::getElementNormalizedLinear(const tgt::vec3& position, size_t channel) const {
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
        tgt::vec3 posAbs = tgt::max(position - 0.5f, tgt::vec3::zero);
        tgt::vec3 p = posAbs - floor(posAbs); // get decimal part
        tgt::svec3 llb = tgt::svec3(posAbs);
        tgt::svec3 urf = tgt::svec3(ceil(posAbs));
        urf = min(urf, _size - tgt::svec3(1)); // clamp so the lookups do not exceed the dimensions
        llb = min(llb, _size - tgt::svec3(1)); // dito

        /*
            interpolate linearly
        */
        return  getElementNormalized(tgt::svec3(llb.x, llb.y, llb.z), channel) * (1.f-p.x)*(1.f-p.y)*(1.f-p.z) // llB
              + getElementNormalized(tgt::svec3(urf.x, llb.y, llb.z), channel) * (    p.x)*(1.f-p.y)*(1.f-p.z) // lrB
              + getElementNormalized(tgt::svec3(urf.x, urf.y, llb.z), channel) * (    p.x)*(    p.y)*(1.f-p.z) // urB
              + getElementNormalized(tgt::svec3(llb.x, urf.y, llb.z), channel) * (1.f-p.x)*(    p.y)*(1.f-p.z) // ulB
              + getElementNormalized(tgt::svec3(llb.x, llb.y, urf.z), channel) * (1.f-p.x)*(1.f-p.y)*(    p.z) // llF
              + getElementNormalized(tgt::svec3(urf.x, llb.y, urf.z), channel) * (    p.x)*(1.f-p.y)*(    p.z) // lrF
              + getElementNormalized(tgt::svec3(urf.x, urf.y, urf.z), channel) * (    p.x)*(    p.y)*(    p.z) // urF
              + getElementNormalized(tgt::svec3(llb.x, urf.y, urf.z), channel) * (1.f-p.x)*(    p.y)*(    p.z);// ulF
    }

348
349
350
}

#endif // GENERICIMAGEDATALOCAL_H__