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

Commit 207a1354 authored by schultezub's avatar schultezub

Performance improvenment in ImageData::getRepresentation<T> (using type_info...

Performance improvenment in ImageData::getRepresentation<T> (using type_info instead of dynamic_cast where possible)

git-svn-id: https://camplinux.in.tum.de/svn/campvis/trunk@459 bb408c1c-ae56-11e1-83d9-df6b3e0c105e
parent 063db290
......@@ -29,6 +29,12 @@
#include "imagedata.h"
#include "core/datastructures/imagerepresentationlocal.h"
#ifdef CAMPVIS_HAS_MODULE_ITK
#include "modules/itk/core/genericimagerepresentationitk.h"
#endif
namespace campvis {
const std::string ImageData::loggerCat_ = "CAMPVis.core.datastructures.ImageData";
......@@ -162,5 +168,36 @@ namespace campvis {
addRepresentation(representation);
}
template<>
const ImageRepresentationLocal* ImageData::getRepresentation<ImageRepresentationLocal>(bool performConversion /*= true*/) const {
// look, whether we already have a suitable representation
for (tbb::concurrent_vector<const AbstractImageRepresentation*>::const_iterator it = _representations.begin(); it != _representations.end(); ++it) {
if (const ImageRepresentationLocal* tester = dynamic_cast<const ImageRepresentationLocal*>(*it))
return tester;
}
if (performConversion) {
return tryPerformConversion<ImageRepresentationLocal>();
}
return 0;
}
#ifdef CAMPVIS_HAS_MODULE_ITK
template<>
const AbstractImageRepresentationItk* ImageData::getRepresentation<AbstractImageRepresentationItk>(bool performConversion /*= true*/) const {
// look, whether we already have a suitable representation
for (tbb::concurrent_vector<const AbstractImageRepresentation*>::const_iterator it = _representations.begin(); it != _representations.end(); ++it) {
if (const AbstractImageRepresentationItk* tester = dynamic_cast<const AbstractImageRepresentationItk*>(*it))
return tester;
}
if (performConversion) {
tgtAssert(false, "Conversion to AbstractImageRepresentationItk not implemented - is it really needed?");
LDEBUG("Could not convert to AbstractImageRepresentationItk");
}
return 0;
}
#endif
}
\ No newline at end of file
......@@ -42,6 +42,9 @@
#include <vector>
namespace campvis {
class ImageRepresentationLocal;
class AbstractImageRepresentationItk;
/**
* Stores basic information about one (semantic) image of arbitrary dimension.
* Different representations (e.g. local memory, OpenGL texture, OpenCL buffer) are
......@@ -166,7 +169,7 @@ namespace campvis {
/**
* Returns a representation of this image of type \a T.
* Looks, whether such a representations already exists, if not and \a performConversion is
* set, the method tries to create T::tryConvertFrom(). Returns 0 on failure.
* set, the method tries to create it via T::tryConvertFrom(). Returns 0 on failure.
* \note You do \b NOT have ownership of the returned pointer!
* The returned pointer is valid as long as this ImageData object exists.
* \note If \a T is OpenGL related, make sure to call this method from a valid and locked OpenGL context.
......@@ -178,6 +181,9 @@ namespace campvis {
const T* getRepresentation(bool performConversion = true) const;
protected:
template<typename T>
const T* tryPerformConversion() const;
/**
* Adds the given representation to the list of representations.
* \note Since this is non-publich method, no sanity checks are performed!
......@@ -215,32 +221,46 @@ namespace campvis {
// = Template definition ==========================================================================
template<typename T>
const T* campvis::ImageData::getRepresentation(bool performConversion /*= true*/) const {
const T* campvis::ImageData::getRepresentation(bool performConversion) const {
// look, whether we already have a suitable representation
for (tbb::concurrent_vector<const AbstractImageRepresentation*>::const_iterator it = _representations.begin(); it != _representations.end(); ++it) {
if (const T* tester = dynamic_cast<const T*>(*it))
return tester;
//if (typeid(T) == typeid(**it)) {
// return static_cast<const T*>(*it);
//}
//if (const T* tester = dynamic_cast<const T*>(*it))
// return tester;
if (typeid(T) == typeid(**it)) {
return static_cast<const T*>(*it);
}
}
if (performConversion) {
// no representation found, create a new one
for (tbb::concurrent_vector<const AbstractImageRepresentation*>::const_iterator it = _representations.begin(); it != _representations.end(); ++it) {
const T* tester = T::tryConvertFrom(*it);
if (tester != 0) {
return tester;
}
}
return tryPerformConversion<T>();
}
// could not create a suitable representation
LDEBUG("Could not create a " + std::string(typeid(T*).name()) + " representation.");
return 0;
return 0;
}
template<>
const campvis::ImageRepresentationLocal* campvis::ImageData::getRepresentation<ImageRepresentationLocal>(bool performConversion) const;
#ifdef CAMPVIS_HAS_MODULE_ITK
template<>
const campvis::AbstractImageRepresentationItk* campvis::ImageData::getRepresentation<AbstractImageRepresentationItk>(bool performConversion) const;
#endif
template<typename T>
const T* campvis::ImageData::tryPerformConversion() const {
// no representation found, create a new one
for (tbb::concurrent_vector<const AbstractImageRepresentation*>::const_iterator it = _representations.begin(); it != _representations.end(); ++it) {
const T* tester = T::tryConvertFrom(*it);
if (tester != 0) {
return tester;
}
}
// could not create a suitable representation
LDEBUG("Could not create a " + std::string(typeid(T*).name()) + " representation.");
return 0;
}
}
#endif // IMAGEDATA_H__
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment