[ismrmrd] 84/281: valarray / ndarraycontainer inheritance ... seems to work. consistency checks missing.
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Wed Jan 14 20:00:59 UTC 2015
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to annotated tag ismrmrd0.5
in repository ismrmrd.
commit 18b089fc69bfa2298cb726f7b78945120e4ae16a
Author: kvahed <kaveh at vahedipour.de>
Date: Fri Jan 25 08:27:40 2013 +0100
valarray / ndarraycontainer inheritance ... seems to work. consistency checks missing.
---
examples/c++/test_create_dataset.cpp | 8 +-
examples/c++/test_recon_dataset.cpp | 4 +-
ismrmrd.h | 318 +++++++++++++----------------------
ismrmrd_hdf5.cpp | 19 ++-
4 files changed, 138 insertions(+), 211 deletions(-)
diff --git a/examples/c++/test_create_dataset.cpp b/examples/c++/test_create_dataset.cpp
index a67331d..983632f 100644
--- a/examples/c++/test_create_dataset.cpp
+++ b/examples/c++/test_create_dataset.cpp
@@ -110,11 +110,11 @@ int main(int argc, char** argv)
//Let's append the data to the file
ISMRMRD::Acquisition acq;
- acq.data_ = new float[readout*2];
- if (!acq.data_) {
+ acq.data_.resize(readout*2);
+/* if (!acq.data_) {
std::cout << "Error allocating memory for the acquisition" << std::endl;
}
-
+*/
for (unsigned int i = 0; i < phase_encoding_lines; i++) {
acq.head_.flags = 0;
//Set some flags
@@ -130,7 +130,7 @@ int main(int argc, char** argv)
acq.head_.number_of_samples = readout;
acq.head_.center_sample = (readout>>1);
acq.head_.sample_time_us = 5.0;
- memcpy(acq.data_,&img_test->data_[i*readout],sizeof(float)*readout*2);
+ memcpy(&acq.data_[0],&img_test->data_[i*readout],sizeof(float)*readout*2);
d.appendAcquisition(&acq);
}
diff --git a/examples/c++/test_recon_dataset.cpp b/examples/c++/test_recon_dataset.cpp
index 1e2e6e3..6bf87e6 100644
--- a/examples/c++/test_recon_dataset.cpp
+++ b/examples/c++/test_recon_dataset.cpp
@@ -87,12 +87,14 @@ int main(int argc, char** argv)
return -1;
}
+
//Allocate a buffer for the data
ISMRMRD::NDArrayContainer< std::complex<float> > buffer;
buffer.dimensions_.push_back(e_space.matrixSize().x());
buffer.dimensions_.push_back(e_space.matrixSize().y());
buffer.data_.resize(e_space.matrixSize().x()*e_space.matrixSize().y(), std::complex<float>(0.0,0.0));
+
//Now loop through and copy data
unsigned int number_of_acquisitions = d.getNumberOfAcquisitions();
for (unsigned int i = 0; i < number_of_acquisitions; i++) {
@@ -102,7 +104,7 @@ int main(int argc, char** argv)
//Copy data, we should probably be more careful here and do more tests....
//We are not considering multiple channels here.
unsigned int offset = acq->head_.idx.kspace_encode_step_1*buffer.dimensions_[0];
- memcpy(&buffer.data_[offset],acq->data_,sizeof(float)*2*buffer.dimensions_[0]);
+ memcpy(&buffer.data_[offset],&acq->data_[0],sizeof(float)*2*buffer.dimensions_[0]);
}
//Let's FFT the k-space to image
diff --git a/ismrmrd.h b/ismrmrd.h
index 081d443..6e3636f 100644
--- a/ismrmrd.h
+++ b/ismrmrd.h
@@ -149,113 +149,6 @@ struct AcquisitionHeader
float user_float[8]; /**< Free user parameters */
};
-#ifdef __cplusplus
-class Acquisition
-{
-
-public:
- Acquisition()
- : traj_(0)
- , data_(0)
- {
- memset(&head_,0,sizeof(AcquisitionHeader));
- head_.version = ISMRMRD_VERSION;
- }
-
- ~Acquisition() {
- deleteData();
- }
-
- bool isFlagSet(const FlagBit& f) const {
- return f.isSet(head_.flags);
- }
-
- void setFlag(const FlagBit& f) {
- head_.flags |= f.bitmask_;
- }
-
- Acquisition(const Acquisition& a) { // copy constructor
-
- if (this != &a) {
-
- head_ = a.head_;
- if (head_.trajectory_dimensions > 0) {
- size_t trajectory_elements = head_.number_of_samples*head_.trajectory_dimensions;
- try {
- traj_ = new float[trajectory_elements];
- } catch (std::exception& e) {
- std::cerr << "Unable to allocate trajectory in ISMRMRD::Acquisition: " << e.what() << std::endl;
- }
- memcpy(traj_,a.traj_,sizeof(float)*trajectory_elements);
- } else {
- traj_ = 0;
- }
-
- size_t data_elements = head_.number_of_samples*head_.active_channels;
- if (data_elements > 0) {
- try {
- data_ = new float[data_elements*2]; //*2 for complex
- } catch (std::exception& e) {
- std::cerr << "Unable to allocate trajectory in ISMRMRD::Acquisition: " << e.what() << std::endl;
- }
- memcpy(data_,a.data_,sizeof(float)*2*data_elements);
- } else {
- data_ = 0;
- }
-
- }
-
- }
-
- Acquisition& operator=(const Acquisition& a) {
-
- if (this != &a) {
-
- head_ = a.head_;
- deleteData(); // IMHO: Check if we can't use the old data and just overwrite. Way cheaper.
- if (head_.trajectory_dimensions > 0) {
- size_t trajectory_elements = head_.number_of_samples*head_.trajectory_dimensions;
- try {
- traj_ = new float[trajectory_elements];
- } catch (std::exception& e) {
- std::cerr << "Unable to allocate trajectory in ISMRMRD::Acquisition: " << e.what() << std::endl;
- }
- memcpy(traj_,a.traj_,sizeof(float)*trajectory_elements);
- }
-
- size_t data_elements = head_.number_of_samples*head_.active_channels;
- if (data_elements > 0) {
- try {
- data_ = new float[data_elements*2]; //*2 for complex
- } catch (std::exception& e) {
- std::cerr << "Unable to allocate trajectory in ISMRMRD::Acquisition: " << e.what() << std::endl;
- }
- memcpy(data_,a.data_,sizeof(float)*2*data_elements);
- }
-
- }
-
- return *this;
- }
-
- AcquisitionHeader head_; /**< Header, see above */
-
- float* traj_; /**< Trajectory, elements = head_.trajectory_dimensions*head_.number_of_samples
- [kx,ky,kx,ky.....] (for head_.trajectory_dimensions = 2)
- [kx,ky,kz,kx,ky,kz,.....] (for head_.trajectory_dimensions = 3) */
-
- float* data_; /**< Actual data, elements = head_.number_of_samples*head_.active_channels*2
- [re,im,re,im,.....,re,im,re,im,.....,re,im,re,im,.....]
- ---channel 1-------channel 2---------channel 3----- */
-
-protected:
- void deleteData() {
- if (traj_) {delete [] traj_; traj_ = 0;}
- if (data_) {delete [] data_; data_ = 0;}
- }
-};
-#endif //__cplusplus
-
enum ImageDataType
{
DATA_FLOAT = 1,
@@ -318,111 +211,32 @@ struct ImageHeader
float user_float[8]; /**< Free user parameters */
};
-#ifdef __cplusplus
-/**
- * Container for an image (header and data)
- */
-template <typename T> class Image {
-
-public:
- Image()
- : data_(0)
- {
- memset(&head_,0,sizeof(ImageHeader));
- head_.version = ISMRMRD_VERSION;
- }
-
- ~Image()
- {
- deleteData();
- }
-
- bool isFlagSet(FlagBit& f) const {
- return f.isSet(head_.flags);
- }
-
- void setFlag(FlagBit& f) {
- head_.flags |= f.bitmask_;
- }
-
- Image(const Image& a) { // copy constructor
-
- if (this &= &a) {
-
- head_ = a.head_;
-
- size_t elements = getNumberOfElements();
- if (elements > 0) {
- try {
- data_ = new T[elements];
- } catch (std::exception& e) {
- std::cerr << "Unable to allocate data in ISMRMRD::Image: " << e.what() << std::endl;
- }
- memcpy(data_,a.data_,sizeof(T)*elements);
- } else {
- data_ = 0;
- }
-
- }
-
- }
-
- Image& operator=(const Image& a) {
-
- if (this &= &a) {
- head_ = a.head_;
-
- deleteData();
- size_t elements = getNumberOfElements();
- if (elements > 0) {
- try {
- data_ = new float[elements];
- } catch (std::exception& e) {
- std::cerr << "Unable to allocate data in ISMRMRD::Image: " << e.what() << std::endl;
- }
- memcpy(data_,a.data_,sizeof(T)*elements);
- }
-
- }
-
- return *this;
- }
-
- size_t getNumberOfElements() const {
- return head_.matrix_size[0]*
- head_.matrix_size[1]*
- head_.matrix_size[2]*
- head_.channels;
- }
-
- ImageHeader head_; /**< ImageHeader as defined above */
- T* data_; /**< Data, array of size (matrix_size[0]*matrix_size[1]*matrix_size[2]*channels),
- first spatial dimension is fastest changing array index, channels outer most (slowest changing). */
-
-protected:
- void deleteData() {
- if (data_) {
- delete [] data_;
- data_ = 0;
- }
- }
-
-};
-
/**
* Container for generic array. This structure is used through the HDF5 file interaction.
*/
-template <typename T> class NDArrayContainer
-{
+template <typename T> class NDArrayContainer {
+
public:
NDArrayContainer() {}
+ /**
+ * @brief Construct with dimensions and data
+ */
NDArrayContainer(const std::vector<unsigned int>& dimensions, T* d) {
dimensions_ = dimensions;
data_.resize(elements());
memcpy(&data_[0],d,sizeof(T)*elements());
}
+ /**
+ * @brief Construct with dimensions and preset value
+ */
+ NDArrayContainer(const std::vector<unsigned int>& dimensions, T t = T(0)) {
+ dimensions_ = dimensions;
+ data_.resize(elements());
+ data_ = t;
+ }
+
virtual ~NDArrayContainer() {}
std::vector<unsigned int> dimensions_; /**< Array with dimensions of the array. First dimension is fastest moving in the array */
@@ -440,6 +254,14 @@ public:
return elements;
}
+ T& operator[] (const size_t& p) {
+ return data_[p];
+ }
+
+ T operator[] (const size_t& p) const {
+ return data_[p];
+ }
+
bool is_consistent() const {
return (elements() == data_.size());
}
@@ -453,6 +275,102 @@ public:
};
+#ifdef __cplusplus
+/**
+ * Container for an image (header and data)
+ */
+template <typename T>
+class Image : public NDArrayContainer<T> {
+
+public:
+ Image() {
+ memset(&head_,0,sizeof(ImageHeader));
+ head_.version = ISMRMRD_VERSION;
+ }
+
+ ~Image() {}
+
+ bool isFlagSet(FlagBit& f) const {
+ return f.isSet(head_.flags);
+ }
+
+ void setFlag(FlagBit& f) {
+ head_.flags |= f.bitmask_;
+ }
+
+ Image(const Image& a) { // copy constructor
+ if (this != &a) {
+ head_ = a.head_;
+ this->data_ = a.data_;
+ }
+ }
+
+ Image& operator=(const Image& a) {
+ if (this != &a) {
+ head_ = a.head_;
+ this->data_ = a.data_;
+ }
+ return *this;
+ }
+
+ size_t getNumberOfElements() const {
+ return head_.matrix_size[0]*
+ head_.matrix_size[1]*
+ head_.matrix_size[2]*
+ head_.channels;
+ }
+
+ ImageHeader head_; /**< ImageHeader as defined above */
+
+};
+
+/**
+ * @brief Single acquisition
+ */
+class Acquisition : public NDArrayContainer<float> {
+
+public:
+
+ Acquisition()
+ : traj_(), data_() {
+ memset(&head_,0,sizeof(AcquisitionHeader));
+ head_.version = ISMRMRD_VERSION;
+ }
+
+ ~Acquisition() {}
+
+ bool isFlagSet(const FlagBit& f) const {
+ return f.isSet(head_.flags);
+ }
+
+ void setFlag(const FlagBit& f) {
+ head_.flags |= f.bitmask_;
+ }
+
+ Acquisition (const Acquisition& a) { // copy constructor
+ if (this != &a) {
+ head_ = a.head_;
+ traj_ = a.traj_;
+ data_ = a.data_;
+ /* TODO: Consistency checks*/
+ }
+ }
+
+ Acquisition& operator=(const Acquisition& a) {
+ if (this != &a) {
+ head_ = a.head_;
+ traj_ = a.traj_;
+ data_ = a.data_;
+ }
+ return *this;
+ }
+
+ AcquisitionHeader head_; /**< Header, see above */
+
+ std::valarray<float> traj_;
+ std::valarray<float> data_;
+
+};
#endif //__cplusplus
diff --git a/ismrmrd_hdf5.cpp b/ismrmrd_hdf5.cpp
index 145d40a..5cbaf73 100644
--- a/ismrmrd_hdf5.cpp
+++ b/ismrmrd_hdf5.cpp
@@ -318,10 +318,10 @@ int IsmrmrdDataset::appendAcquisition(Acquisition* a)
AcquisitionHeader_with_data tmp;
tmp.head = a->head_;
tmp.traj.len = tmp.head.trajectory_dimensions*tmp.head.number_of_samples;
- tmp.traj.p = static_cast<void*>(a->traj_);
+ tmp.traj.p = static_cast<void*>(&a->traj_[0]);
tmp.data.len = tmp.head.active_channels*tmp.head.number_of_samples*2;
- tmp.data.p = static_cast<void*>(a->data_);
+ tmp.data.p = static_cast<void*>(&a->data_[0]);
DataSpace mspace1 = dataset_->getSpace();
rank = mspace1.getSimpleExtentNdims();
@@ -446,8 +446,12 @@ boost::shared_ptr< Acquisition > IsmrmrdDataset::readAcquisition(unsigned long i
ret = boost::shared_ptr<Acquisition>(new Acquisition());
ret->head_ = tmp.head;
- ret->traj_ = reinterpret_cast<float*>(tmp.traj.p);
- ret->data_ = reinterpret_cast<float*>(tmp.data.p);
+ size_t tl = tmp.traj.len;
+ size_t dl = tmp.data.len;
+ ret->traj_.resize(tl);
+ memcpy(&ret->traj_[0], tmp.traj.p, sizeof(float)*tl);
+ ret->data_.resize(dl);
+ memcpy(&ret->data_[0], tmp.data.p, sizeof(float)*dl);
} catch (...) {
std::cout << "Error caught while attempting to read HDF5 file" << std::endl;
@@ -514,7 +518,7 @@ template <typename T> int IsmrmrdDataset::appendImage(Image<T>& m, const char* v
ImageHeader_with_data<T> tmp;
tmp.head = m.head_;
tmp.data.len = m.getNumberOfElements();
- tmp.data.p = m.data_;
+ tmp.data.p = &m.data_[0];
std::vector<unsigned int> dims(1,1);
NDArrayContainer<ImageHeader_with_data<T> > cont(dims, &tmp);
return appendArray<ImageHeader_with_data<T> >(cont, varname);
@@ -534,7 +538,10 @@ template <typename T> boost::shared_ptr< Image<T> > IsmrmrdDataset::readImage(co
memcpy(&ret->head_, &(tmp->data_[0].head), sizeof(ImageHeader));
//Here we grab the data, which is part of the hvl_t member of ImageHeader_with_data, which does NOT get deallocated automatically.
- ret->data_ = reinterpret_cast<T*>(tmp->data_[0].data.p);
+
+ size_t dlen = tmp->data_[0].data.len;
+ ret->data_.resize(dlen);
+ memcpy (&ret->data_, tmp->data_[0].data.p, dlen*sizeof(T));
return ret;
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/ismrmrd.git
More information about the debian-science-commits
mailing list