EMAN2
volume_data.cpp
Go to the documentation of this file.
1// Copyright (C) 2005-2008 Washington University in St Louis, Baylor College of Medicine. All rights reserved
2// Author: Sasakthi S. Abeysinghe (sasakthi@gmail.com)
3// Description: Stores information of a density volume
4
5#include "volume_data.h"
6
7using namespace wustl_mm::SkeletonMaker;
8
9 VolumeData::VolumeData(EMData* em)
10 {
11 this->emdata = em;
12 owns_emdata = false;
13 }
14
15 VolumeData::VolumeData(int sizeX, int sizeY, int sizeZ) {
16 InitializeVolumeData(sizeX, sizeY, sizeZ, 1, 1, 1, 0, 0, 0, true, 0);
17 }
18
19 VolumeData::VolumeData(int sizeX, int sizeY, int sizeZ, float val) {
20 InitializeVolumeData(sizeX, sizeY, sizeZ, 1, 1, 1, 0, 0, 0, true, val);
21 }
22
23 VolumeData::VolumeData(int sizeX, int sizeY, int sizeZ, int offsetX, int offsetY, int offsetZ, VolumeData * data) {
24 InitializeVolumeData(sizeX, sizeY, sizeZ, data->GetSpacingX(), data->GetSpacingY(), data->GetSpacingZ(), data->GetOriginX(), data->GetOriginY(), data->GetOriginZ(), false, 0);
25 float value;
26 for ( int k = offsetZ; k < sizeZ + offsetZ; k++) {
27 for (int j = offsetY; j < sizeY + offsetY; j++ ) {
28 for (int i = offsetX; i < sizeX + offsetX; i++) {
29 value = data->GetDataAt(i,j,k);
30 SetDataAt(i-offsetX,j-offsetY,k-offsetZ, value);
31 }
32 }
33 }
34 }
35
37 {
38 emdata = new EMData( *obj.get_emdata() );
39 owns_emdata = true;
40 //SetSize( obj.GetSizeX(), obj.GetSizeY(), obj.GetSizeZ() );
41 //SetSpacing( obj.GetSpacingX(), obj.GetSpacingY(), obj.GetSpacingZ() );
42 //SetOrigin( obj.GetOriginX(), GetOriginY(), obj.GetOriginZ() );
43 }
45 if (owns_emdata)
46 delete emdata;
47 }
48
49 void VolumeData::InitializeVolumeData(int sizeX, int sizeY, int sizeZ, float spacingX, float spacingY, float spacingZ, float originX, float originY, float originZ, bool initializeData, float val) {
50 emdata = new EMData(sizeX, sizeY, sizeZ);
51 owns_emdata = true;
52 //SetSize(sizeX, sizeY, sizeZ);
53 SetSpacing(spacingX, spacingY, spacingZ);
54 SetOrigin(originX, originY, originZ);
55 if(initializeData) {
56 emdata->to_value(val);
57 }
58 }
59
60 int VolumeData::GetSize(int dimension) {
61 int ret = 0;
62 switch (dimension)
63 {
64 case 0: ret = GetSizeX();
65 break;
66 case 1: ret = GetSizeY();
67 break;
68 case 2: ret = GetSizeZ();
69 break;
70 default:
71 throw InvalidParameterException("VolumeData::GetSize requires an argument of 0, 1, or 2");
72 }
73
74 return ret;
75
76 }
77
79 return emdata->get_xsize();
80 }
81
83 return emdata->get_ysize();
84 }
85
87 return emdata->get_zsize();
88 }
89
90 float VolumeData::GetSpacing(int dimension) {
91 float ret = 0;
92 switch (dimension)
93 {
94 case 0: ret = GetSpacingX();
95 break;
96 case 1: ret = GetSpacingY();
97 break;
98 case 2: ret = GetSpacingZ();
99 break;
100 default:
101 throw InvalidParameterException("VolumeData::GetSpacing requires an argument of 0, 1, or 2");
102 }
103
104 return ret;
105 }
106
108 return emdata->get_attr("apix_x");
109 }
110
112 return emdata->get_attr("apix_y");
113 }
114
116 return emdata->get_attr("apix_y");
117 }
118
119 float VolumeData::GetOrigin(int dimension) {
120 float ret = 0;
121 switch (dimension)
122 {
123 case 0: ret = GetOriginX();
124 break;
125 case 1: ret = GetOriginY();
126 break;
127 case 2: ret = GetOriginZ();
128 break;
129 default:
130 throw InvalidParameterException("VolumeData::GetOrigin requires an argument of 0, 1, or 2");
131 }
132
133 return ret;
134 }
135
137 return emdata->get_attr("origin_x");
138 }
139
141 return emdata->get_attr("origin_y");
142 }
143
145 return emdata->get_attr("origin_z");
146 }
147
148
149 float VolumeData::GetDataAt(int x, int y, int z) {
150 return this->emdata->get_value_at(x, y, z);
151 }
152
153 float VolumeData::GetDataAt(int index) {
154 //TODO: This may be a problem because EMAN2 and Gorgon do indexing differently --> see if this causes problems
155 return emdata->get_value_at(index);
156 }
157
158 int VolumeData::GetIndex(int x, int y, int z) {
159 return (x + y * GetSizeX() + z * GetSizeX() * GetSizeY());
160 }
161
163 return GetSizeX() * GetSizeY() * GetSizeZ();
164 }
165
167 {
168 return emdata;
169 }
170
171 void VolumeData::SetSpacing(float spacingX, float spacingY, float spacingZ) {
172 emdata->set_attr("apix_x", spacingX);
173 emdata->set_attr("apix_y", spacingY);
174 emdata->set_attr("apix_z", spacingZ);
175 }
176
177 void VolumeData::SetOrigin(float originX, float originY, float originZ) {
178 emdata->set_attr("origin_x", originX);
179 emdata->set_attr("origin_y", originY);
180 emdata->set_attr("origin_z", originZ);
181 }
182
183
184 void VolumeData::SetSize(int sizeX, int sizeY, int sizeZ) {
185 emdata->set_size(sizeX, sizeY, sizeZ);
186 }
187
188 void VolumeData::SetDataAt(int x, int y, int z, float value) {
189 emdata->set_value_at(x, y, z, value);
190 }
191
192 void VolumeData::SetDataAt(int index, float value) {
193 //TODO: This may be a problem because EMAN2 and Gorgon do indexing differently --> see if this causes problems
194 emdata->get_data()[index] = value;
195 }
196 void VolumeData::Pad(int padBy, double padValue) {
197
198 int sizex = GetSizeX();
199 int sizey = GetSizeY();
200 int sizez = GetSizeZ();
201 int newSizeX = sizex + 2*padBy;
202 int newSizeY = sizey + 2*padBy;
203 int newSizeZ = sizez + 2*padBy;
204
205 // Method 1: Using get_clip ===========================================================
206// Region reg(0, 0, 0, newSizeX, newSizeY, newSizeZ);
207// EMData * new_emdata = emdata->get_clip(reg); //Extends the area x > sizex, y > sizey, z > sizez but does not translate (0,0,0)
208// new_emdata->translate(padBy,padBy,padBy); //We want equal padding on each side
209//
210// for(int z = 0; z < newSizeZ; z++)
211// for(int y = 0; y < newSizeY; y++)
212// for(int x = 0; x < newSizeX; x++)
213// if ((x < padBy) || (y < padBy) || (z < padBy) || (x >= padBy + sizex) || (y >= padBy + sizey) || (z >= padBy + sizez))
214// new_emdata->set_value_at(x, y, z, padValue);
215//
216// if (this->owns_emdata)
217// this->emdata->free_memory();
218// this->emdata = new_emdata;
219
220 // Method 2: making a copy =============================================================
221 float * newData = (float*) malloc ( newSizeX * newSizeY * newSizeZ * sizeof(float) );
222 double value;
223
224 for(int z = 0; z < newSizeZ; z++) {
225 for(int y = 0; y < newSizeY; y++) {
226 for(int x = 0; x < newSizeX; x++) {
227 if ((x < padBy) || (y < padBy) || (z < padBy) || (x >= padBy + sizex) || (y >= padBy + sizey) || (z >= padBy + sizez)) {
228 value = padValue;
229 } else {
230 value = GetDataAt(x-padBy, y-padBy, z-padBy);
231 }
232
233 newData[x + y * newSizeX + z * newSizeX * newSizeY] = static_cast<float>(value); //eman2
234 }
235 }
236 }
237
238 SetSize(newSizeX, newSizeY, newSizeZ);
239 emdata->set_data(newData, newSizeX, newSizeY, newSizeZ);
240 //free(data); //I think this is handled by set_data
241
242 // Method 3: doing in-place resize =====================================================
243// emdata->set_size(newSizeX, newSizeY, newSizeZ);
244// double val;
245// for (int k = newSizeZ - 1; k >= 0; k--)
246// for (int j = newSizeY - 1; k >= 0; j--)
247// for (int i = newSizeX - 1; k >=0; k--)
248// {
249// if ( i < padBy || i >= sizex+padBy || j < padBy || j >= sizey+padBy || k < padBy || k >= sizez+padBy)
250// emdata->set_value_at(i,j,k, padValue);
251// else
252// {
253// val = emdata->get_value_at(i-padBy, j-padBy, k-padBy);
254// emdata->set_value_at(i,j,k, float(val));
255// }
256// }
257 }
EMData stores an image's data and defines core image processing routines.
Definition: emdata.h:82
void InitializeVolumeData(int sizeX, int sizeY, int sizeZ, float spacingX, float spacingY, float spacingZ, float originX, float originY, float originZ, bool initializeData, float val)
Definition: volume_data.cpp:49
void SetOrigin(float originX, float originY, float originZ)
void SetSpacing(float spacingX, float spacingY, float spacingZ)
void Pad(int padBy, double padValue)
void SetDataAt(int x, int y, int z, float value)
void SetSize(int sizeX, int sizeY, int sizeZ)
float GetDataAt(int x, int y, int z)
int GetIndex(int x, int y, int z)
#define InvalidParameterException(desc)
Definition: exception.h:361
#define y(i, j)
Definition: projector.cpp:1516
#define x(i)
Definition: projector.cpp:1517