1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: amiraMeshFile.C
//
#include <BALL/FORMAT/amiraMeshFile.h>
#include <cstdio>
namespace BALL
{
AmiraMeshFile::AmiraMeshFile()
: File(),
binary_(false),
idx_start_data_(0),
num_components_(0)
{
}
AmiraMeshFile::AmiraMeshFile(const String& name, File::OpenMode open_mode)
: File(name, open_mode),
binary_(false),
idx_start_data_(0),
num_components_(0)
{
}
AmiraMeshFile::~AmiraMeshFile()
{
close();
clear();
}
void AmiraMeshFile::clear()
{
File::clear();
binary_= false;
idx_start_data_ = 0;
num_components_ = 0;
}
bool AmiraMeshFile::operator == (const AmiraMeshFile& file) const
{
return ( (File::operator == (file))
&&(binary_ == file.binary_));
}
bool AmiraMeshFile::open(const String& name, File::OpenMode open_mode)
{
if (!(open_mode |= std::ios::binary))
{
open_mode = open_mode | std::ios::binary;
}
if (!File::open(name, open_mode))
{
return(false);
}
return true;
}
bool AmiraMeshFile::readHeader()
{
const size_t MAX_READ_SIZE = 2048;
//We read the first 2k bytes into memory to parse the header.
//The fixed buffer size looks a bit like a hack, and it is one, but it gets the job done.
char buffer[MAX_READ_SIZE + 1];
std::fstream::read(buffer, MAX_READ_SIZE);
buffer[std::fstream::gcount()] = '\0'; //The following string routines prefer null-terminated strings
if (strstr(buffer, "# AmiraMesh BINARY-LITTLE-ENDIAN 2.1"))
{
binary_ = true;
}
else if (strstr(buffer, "# AmiraMesh ASCII 1.0"))
{
binary_ = false;
}
else
{
return false;
}
sscanf(findAndJump_(buffer, "define Lattice"), "%g %g %g", &extent_.x, &extent_.y, &extent_.z);
sscanf(findAndJump_(buffer, "BoundingBox"), "%g %g %g %g %g %g", &min_.x, &max_.x,
&min_.y, &max_.y,
&min_.z, &max_.z);
//Is it a uniform grid? We need this only for the sanity check below.
const bool bIsUniform = (strstr(buffer, "CoordType \"uniform\"") != NULL);
//Type of the field: scalar, vector
num_components_ = 0;
if (strstr(buffer, "Lattice { float Data }"))
{
//Scalar field
num_components_ = 1;
}
else
{
//A field with more than one component, i.e., a vector field
sscanf(findAndJump_(buffer, "Lattice { float["), "%u", &num_components_);
Log.error() << "Sorry, currently, we do not support Amira files containing" << std::endl;
Log.error() << "multiple component fields (e.g. vector fields)\n" << std::endl;
return false;
}
//Sanity check
if (extent_.x <= 0 || extent_.y <= 0 || extent_.z <= 0
|| min_.x > max_.x || min_.y > max_.y || min_.z > max_.z
|| !bIsUniform || num_components_ <= 0)
{
Log.error() << "Something went wrong\n" << std::endl;
return false;
}
idx_start_data_ = strstr(buffer, "@1") - buffer;
return true;
}
bool AmiraMeshFile::read(RegularData3D& map)
{
// first read the header
if (!readHeader())
{
Log.error() << "AmiraMeshFile::read(): readHeader() failed. Aborting read." << std::endl;
return false;
}
RegularData3D::IndexType size;
size.x = (Size) extent_.x;
size.y = (Size) extent_.y;
size.z = (Size) extent_.z;
map = RegularData3D(size, min_, max_);
Size num_to_read = size.x * size.y * size.z * num_components_;
char file_buffer[2048];
if (idx_start_data_ > 0)
{
size_t idx_actual_read = 0;
std::fstream::seekg( idx_start_data_);
if (binary_)
{
//Consume this line, which is the Lattice definition
std::fstream::getline(file_buffer, 2048);
//Consume this line, which is an empty line
std::fstream::getline(file_buffer, 2048);
//Consume this line, which is "# Data section follows"
std::fstream::getline(file_buffer, 2048);
//Consume the next line, which is "@1"
std::fstream::getline(file_buffer, 2048);
//char* data = new char[(4 * 192)];
const Size char_to_read = num_to_read * sizeof(float);
char* data = new char[char_to_read];
std::fstream::read(data, char_to_read);
while (idx_actual_read < num_to_read)
{
map[idx_actual_read] = static_cast<float>(*(data + 4*idx_actual_read));
idx_actual_read++;
}
delete [] data;
}
else
{
//Consume this line, which is the Lattice definition
std::fstream::getline(file_buffer, 2048);
//Consume this line, which is an empty line
std::fstream::getline(file_buffer, 2048);
//Consume the next line, which is "@1"
std::fstream::getline(file_buffer, 2048);
float a, b, c, d, e, f = 0.0;
//while (!std::fstream::eof())
while (idx_actual_read < num_to_read)
{
std::fstream::getline(file_buffer, 2048);
sscanf(file_buffer, "%e %e %e %e %e %e", &a, &b, &c, &d, &e, &f);
map[idx_actual_read++] = a;
map[idx_actual_read++] = b;
map[idx_actual_read++] = c;
map[idx_actual_read++] = d;
map[idx_actual_read++] = e;
map[idx_actual_read++] = f;
}
}
}
else
{
Log.error() << "Error: Corrupted data section. ";
}
return true;
}
bool AmiraMeshFile::writeHeader()
{
// construct a correct header array and write it.
// TODO: implement
return false;
}
bool AmiraMeshFile::write(RegularData3D& /*map*/)
{
// Write the content of a RegularData3D dataset to a CCP4 file.
// TODO: implement
return false;
}
const char* AmiraMeshFile::findAndJump_(const char* buffer, const char* search_string)
{
const char* found_loc = strstr(buffer, search_string);
if (found_loc) return found_loc + strlen(search_string);
return buffer;
}
} // namespace BALL
|