Starting from 2021-07-01, all LRZ GitLab users will be required to explicitly accept the GitLab Terms of Service. Please see the detailed information at https://doku.lrz.de/display/PUBLIC/GitLab and make sure that your projects conform to the requirements.

genericimagerepresentationlocal.h 21.2 KB
Newer Older
1 2 3 4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
6 7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8 9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
11 12
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
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 27
// 
// ================================================================================================

#ifndef GENERICIMAGEREPRESENTATIONLOCAL_H__
#define GENERICIMAGEREPRESENTATIONLOCAL_H__

28
#include "core/datastructures/imagerepresentationlocal.h"
29 30 31 32 33 34 35
#include "core/tools/typetraits.h"

#include <cstring>  // needed for memcpy

namespace campvis {

    /**
36
     * Templated version of ImageRepresentationLocal, storing image data in the local memory.
37 38 39 40 41 42 43 44 45 46 47 48 49
     *
     * \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.
     */
    template<typename BASETYPE, size_t NUMCHANNELS>
    class GenericImageRepresentationLocal : public ImageRepresentationLocal {
    public:
        /// Type of one single image element
        typedef typename TypeTraits<BASETYPE, NUMCHANNELS>::ElementType ElementType;
        /// Type of this template instantiation
        typedef GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS> ThisType;

50 51 52 53 54 55 56 57 58 59 60
        /**
         * Template specialization of the ScopedRepresentation defined in GenericAbstractImageRepresentation<T>
         * since some compiler get confused by the nested templates.
         * \note    This is a simple copy and paste from GenericAbstractImageRepresentation<T> with
         *          explicitly named template parameter.
         */
        struct ScopedRepresentation {
            /**
             * Creates a new DataHandle to the data item with the key \a name in \a dc, that behaves like a const GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>*.
             * \param   dc      DataContainer to grab data from
             * \param   name    Key of the DataHandle to search for
61
             * \param   silent  Flag whether debug messages when no matching data is found should be silenced (defaults to false).
62
             */
63
            ScopedRepresentation(const DataContainer& dc, const std::string& name, bool silent = false)
64 65 66 67 68 69 70 71 72
                : dh(dc.getData(name))
                , data(0)
                , representation(0) 
            {
                if (dh.getData() != 0) {
                    data = dynamic_cast<const ImageData*>(dh.getData());
                    if (data != 0) {
                        representation = data->getRepresentation< GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS> >();
                    }
73 74 75 76 77
                    else {
                        if (!silent)
                            LDEBUGC("CAMPVis.core.ScopedTypedData", "Found DataHandle with id '" << name << "', but it is of wrong type (" << typeid(*dh.getData()).name() << " instead of " << typeid(GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>).name() << ").");
                    }

78 79 80
                    if (data == 0 || representation == 0) {
                        dh = DataHandle(0);
                    }
81
                }
82 83 84 85
                else {
                    if (!silent)
                        LDEBUGC("CAMPVis.core.ScopedRepresentation", "Could not find a DataHandle with id '" << name << "' in DataContainer '" << dc.getName() << "'.");
                }
86
            };
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
            
            /**
             * Creates a new ScopedRepresentation for the given DataHandle, that behaves like a T*.
             * \param   dh  Source DataHandle
             */
            ScopedRepresentation(DataHandle dataHandle)
                : dh(dataHandle)
                , data(0)
                , representation(0) 
            {
                if (dh.getData() != 0) {
                    data = dynamic_cast<const ImageData*>(dh.getData());
                    if (data != 0) {
                        representation = data->getRepresentation<ThisType>();
                    }
102 103 104
                    if (data == 0 || representation == 0) {
                        dh = DataHandle(0);
                    }
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

            /**
             * Implicit conversion operator to const GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>*.
             * \return  The image representation of the image in the DataHandle, may be 0 when no DataHandle was found,
             *           the data is of the wrong type, or no suitable representation was found.
             */
            operator const GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>*() {
                return representation;
            }

            /**
             * Implicit arrow operator to const GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>*.
             * \return  The image representation of the image in the DataHandle, may be 0 when no DataHandle was found,
             *           the data is of the wrong type, or no suitable representation was found.
             */
            const GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>* operator->() const {
                return representation;
            }

            /**
             * Gets the DataHandle.
             * \return dh
             */
            DataHandle getDataHandle() const {
                return dh;
            }

            /**
             * Returns the parent ImageData of this image representation.
             * \return data
             */
            const ImageData* getImageData() const {
                return data;
            }

        private:
            /// Not copy-constructable
            ScopedRepresentation(const ScopedRepresentation& rhs);
            /// Not assignable
            ScopedRepresentation& operator=(const ScopedRepresentation& rhs);

            DataHandle dh;                  ///< DataHandle
            const ImageData* data;          ///< strongly-typed pointer to data, may be 0
            const GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>* representation;        ///< strongly-typed pointer to the image representation, may be 0
        };
152

153
        /**
154 155 156 157 158 159
         * Creates a new GenericImageRepresentationLocal with the given parameters and automatically
         * adds it to \a parent which will take ownerwhip.
         *
         * \note    You do \b not own the returned pointer.
         *
         * \param   parent     Image this representation represents, must not be 0, will take ownership of the returned pointer.
160
         * \param   data    Pointer to the image data, must not be 0, GenericImageRepresentationLocal takes ownership of this pointer!
161
         * \return  A pointer to the newly created ImageRepresentationDisk, you do \b not own this pointer!
162
         */
163
        static GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>* create(const ImageData* parent, ElementType* data);
164 165 166 167 168 169 170 171

        /**
         * Destructor
         */
        virtual ~GenericImageRepresentationLocal();


        /// \see AbstractImageRepresentation::clone()
172
        virtual ThisType* clone(ImageData* newParent) const;
173 174 175 176 177 178 179 180 181

        /// \see AbstractImageRepresentation::getLocalMemoryFootprint()
        virtual size_t getLocalMemoryFootprint() const;

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

        /**
         * Returns a WeaklyTypedPointer to the image data.
182
         * \note    The pointer is still owned by this ImageRepresentationLocal. If you want a copy, use clone().
183 184 185 186
         * \return  A WeaklyTypedPointer to the image data.
         */
        virtual const WeaklyTypedPointer getWeaklyTypedPointer() const;

187
        /// \see ImageRepresentationLocal::getElementNormalized
188 189
        virtual float getElementNormalized(size_t index, size_t channel) const;

190
        /// \see ImageRepresentationLocal::getElementNormalized
191
        virtual float getElementNormalized(const cgt::svec3& position, size_t channel) const;
192

193
        /// \see ImageRepresentationLocal::getElementNormalizedLinear
194
        virtual float getElementNormalizedLinear(const cgt::vec3& position, size_t channel) const;
195

196
        /// \see ImageRepresentationLocal::setElementNormalized
197 198
        virtual void setElementNormalized(size_t index, size_t channel, float value);

199
        /// \see ImageRepresentationLocal::setElementNormalized
200
        virtual void setElementNormalized(const cgt::svec3& position, size_t channel, float value);
201 202 203 204 205 206 207 208 209 210 211 212 213 214


        /**
         * 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.
         */
215
        ElementType& getElement(const cgt::svec3& position);
216 217 218 219 220 221 222 223 224 225 226 227 228

        /**
         * 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.
         */
229
        const ElementType& getElement(const cgt::svec3& position) const;
230 231 232 233 234 235 236 237 238 239 240 241 242

        /**
         * 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.
         */
243
        void setElement(const cgt::svec3& position, const ElementType& value);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

        /**
         * 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.
         */
263
        ElementType getElementLinear(const cgt::vec3 position) const;
264 265

    protected:
266 267 268 269 270 271 272
        /**
         * Creates a new strongly typed ImageData object storing the image in the local memory.
         * 
         * \param   parent  Image this representation represents, must not be 0.
         * \param   data    Pointer to the image data, must not be 0, GenericImageRepresentationLocal takes ownership of this pointer!
         */
        GenericImageRepresentationLocal(ImageData* parent, ElementType* data);
273 274 275

        ElementType* _data;

276 277
        static const std::string loggerCat_;

278 279 280 281
    };

// = Template implementation ======================================================================

282 283 284
    template<typename BASETYPE, size_t NUMCHANNELS>
    const std::string campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::loggerCat_ = "CAMPVis.core.datastructures.GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>";
    
285
    template<typename BASETYPE, size_t NUMCHANNELS>
286 287
    campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>* campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::create(const ImageData* parent, ElementType* data) {
        ThisType* toReturn = new ThisType(const_cast<ImageData*>(parent), data);
288 289 290 291
        toReturn->addToParent();
        return toReturn;
    }

292
    template<typename BASETYPE, size_t NUMCHANNELS>
293
    campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::GenericImageRepresentationLocal(ImageData* parent, ElementType* data)
294
        : ImageRepresentationLocal(parent, TypeTraits<BASETYPE, NUMCHANNELS>::weaklyTypedPointerBaseType)
295 296
        , _data(data)
    {
297
        cgtAssert(_parent->getNumChannels() == NUMCHANNELS, "Number of channels must match parent image's number of channels!");
298 299 300 301 302 303 304 305 306
        if (_data == 0) {
            size_t numElements = getNumElements();
            _data = new ElementType[numElements];
            memset(_data, 0, numElements * TypeTraits<BASETYPE, NUMCHANNELS>::elementSize);
        }
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
    campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::~GenericImageRepresentationLocal() {
307
        delete [] _data;
308 309 310
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
311
    GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>* campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::clone(ImageData* newParent) const {
312 313 314 315
        size_t numElements = getNumElements();
        ElementType* newData = new ElementType[numElements];
        memcpy(newData, _data, numElements * sizeof(ElementType));

316
        return ThisType::create(newParent, newData);
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
    size_t campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::getLocalMemoryFootprint() const {
        return sizeof(*this) + getNumElements() * sizeof(ElementType);
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
    size_t campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::getVideoMemoryFootprint() const {
        return 0;
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
    const WeaklyTypedPointer campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::getWeaklyTypedPointer() const {
        return WeaklyTypedPointer(TypeTraits<BASETYPE, NUMCHANNELS>::weaklyTypedPointerBaseType, NUMCHANNELS, _data);
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
    float campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::getElementNormalized(size_t index, size_t channel) const {
336
        cgtAssert(channel >= 0 && channel < NUMCHANNELS, "Channel out of bounds!");
337 338 339 340
        return TypeNormalizer::normalizeToFloat(TypeTraits<BASETYPE, NUMCHANNELS>::getChannel(getElement(index), channel));
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
341
    float campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::getElementNormalized(const cgt::svec3& position, size_t channel) const {
342 343 344 345 346
        return getElementNormalized(_parent->positionToIndex(position), channel);
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
    void campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::setElementNormalized(size_t index, size_t channel, float value) {
347
        cgtAssert(channel >= 0 && channel < NUMCHANNELS, "Channel out of bounds!");
348 349 350 351
        TypeTraits<BASETYPE, NUMCHANNELS>::setChannel(getElement(index), channel, TypeNormalizer::denormalizeFromFloat<BASETYPE>(value));
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
352
    void campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::setElementNormalized(const cgt::svec3& position, size_t channel, float value) {
353 354 355 356 357
        setElementNormalized(_parent->positionToIndex(position), channel, value);
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
    typename campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::ElementType& campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::getElement(size_t position) {
358
        cgtAssert(position >= 0 && position < getNumElements(), "Position out of bounds!");
359 360 361 362
        return _data[position];
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
363
    typename campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::ElementType& campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::getElement(const cgt::svec3& position) {
364 365 366 367 368
        return getElement(_parent->positionToIndex(position));
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
    const typename campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::ElementType& campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::getElement(size_t position) const {
369
        cgtAssert(position >= 0 && position < getNumElements(), "Position out of bounds!");
370 371 372 373
        return _data[position];
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
374
    const typename campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::ElementType& campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::getElement(const cgt::svec3& position) const {
375 376 377 378 379
        return getElement(_parent->positionToIndex(position));
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
    void campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::setElement(size_t position, const ElementType& value) {
380
        cgtAssert(position >= 0 && position < getNumElements(), "Position out of bounds!");
381 382 383 384 385
        _data[position] = value;

    }

    template<typename BASETYPE, size_t NUMCHANNELS>
386
    void campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::setElement(const cgt::svec3& position, const ElementType& value) {
387 388 389 390 391 392 393 394 395 396 397 398 399 400
        _data[_parent->positionToIndex(position)] = value;
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
    typename campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::ElementType* campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::getImageData() {
        return _data;
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
    const typename campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::ElementType* campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::getImageData() const {
        return _data;
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
401
    typename campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::ElementType campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::getElementLinear(const cgt::vec3 position) const {
402 403
        // yet to be implemented
        // TODO: Check wether pixel/voxel coordinates lie on the edges or on the center of the pixels/voxels
404
        cgtAssert(false, "Yet to be implemented!");
405 406 407 408 409
        return ElementType(0);
    }


    template<typename BASETYPE, size_t NUMCHANNELS>
410 411 412 413 414 415 416
    float campvis::GenericImageRepresentationLocal<BASETYPE, NUMCHANNELS>::getElementNormalizedLinear(const cgt::vec3& position, size_t channel) const {
        cgt::vec3 posAbs = cgt::max(position - 0.5f, cgt::vec3::zero);
        cgt::vec3 p = posAbs - floor(posAbs); // get decimal part
        cgt::svec3 llb = cgt::svec3(posAbs);
        cgt::svec3 urf = cgt::svec3(ceil(posAbs));
        urf = min(urf, getSize() - cgt::svec3(1)); // clamp so the lookups do not exceed the dimensions
        llb = min(llb, getSize() - cgt::svec3(1)); // dito
417 418 419 420

        /*
            interpolate linearly
        */
421 422 423 424 425 426 427 428
        return  getElementNormalized(cgt::svec3(llb.x, llb.y, llb.z), channel) * (1.f-p.x)*(1.f-p.y)*(1.f-p.z) // llB
              + getElementNormalized(cgt::svec3(urf.x, llb.y, llb.z), channel) * (    p.x)*(1.f-p.y)*(1.f-p.z) // lrB
              + getElementNormalized(cgt::svec3(urf.x, urf.y, llb.z), channel) * (    p.x)*(    p.y)*(1.f-p.z) // urB
              + getElementNormalized(cgt::svec3(llb.x, urf.y, llb.z), channel) * (1.f-p.x)*(    p.y)*(1.f-p.z) // ulB
              + getElementNormalized(cgt::svec3(llb.x, llb.y, urf.z), channel) * (1.f-p.x)*(1.f-p.y)*(    p.z) // llF
              + getElementNormalized(cgt::svec3(urf.x, llb.y, urf.z), channel) * (    p.x)*(1.f-p.y)*(    p.z) // lrF
              + getElementNormalized(cgt::svec3(urf.x, urf.y, urf.z), channel) * (    p.x)*(    p.y)*(    p.z) // urF
              + getElementNormalized(cgt::svec3(llb.x, urf.y, urf.z), channel) * (1.f-p.x)*(    p.y)*(    p.z);// ulF
429 430 431 432 433
    }

}

#endif // GENERICIMAGEREPRESENTATIONLOCAL_H__