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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#include <BALL/FORMAT/HMOFile.h>
namespace BALL
{
HMOFile::HMOFile()
: LineBasedFile(),
charges_(),
comments_()
{
}
HMOFile::HMOFile(const String& filename, File::OpenMode open_mode)
: LineBasedFile(filename, open_mode),
charges_(),
comments_()
{
}
HMOFile::~HMOFile()
{
}
void HMOFile::clear()
{
LineBasedFile::clear();
charges_.clear();
comments_.clear();
}
bool HMOFile::operator == (const HMOFile& file)
{
// ignore the comments for comparison
return ( (LineBasedFile::operator == (file)
&& (charges_ == file.charges_)));
}
bool HMOFile::open(const String& name, File::OpenMode open_mode)
{
return LineBasedFile::open(name, open_mode);
}
bool HMOFile::read(Surface& surface)
{
// we can only read something if the file has been opened correctly
if (!isOpen() || getOpenMode() != MODE_IN)
{
return false;
}
// now make sure that there is no old crap lying around
charges_.clear();
comments_.clear();
// find the first block of interest (NODL_DATA)
if (!readUntil_("BEG_NODL_DATA"))
{
throw(Exception::ParseError(__FILE__, __LINE__, "HMOFile::read", "file does not contain node data"));
}
readNodeData_(surface);
// now, find the geometric elements (ELEM_DATA)
if (!readUntil_("BEG_ELEM_DATA"))
{
throw(Exception::ParseError(__FILE__, __LINE__, "HMOFile::read", "file does not contain element data"));
}
readElementData_(surface);
// now, see if there is additional charge data (CHARGE_DATA); if not, we are done
if (readUntil_("BEG_CHARGE_DATA"))
{
readChargeData_();
}
return true;
}
bool HMOFile::write(Surface const& surface)
{
// we can only write something if the file has been opened correctly
if (!isOpen() || !(getOpenMode() & MODE_OUT))
{
throw File::CannotWrite(__FILE__, __LINE__, name_);
}
// write a comment as a header replacement
if (comments_.size() == 0)
{
getFileStream() << "# HYPERMESH file written by BALL::HMOFile" << std::endl << std::endl;
}
else
{
for (Position i=0; i<comments_.size(); ++i)
{
getFileStream() << comments_[i] << std::endl;
}
getFileStream() << std::endl;
}
writeNodes_(surface);
writeElements_(surface);
return true;
}
bool HMOFile::write(Surface const& surface, AtomContainer const& ac)
{
if (!isOpen() || !(getOpenMode() & MODE_OUT))
{
throw File::CannotWrite(__FILE__, __LINE__, name_);
}
// write a comment as a header replacement
if (comments_.size() == 0)
{
getFileStream() << "# HYPERMESH file for " << ac.getName() << ", written by BALL::HMOFile" << std::endl << std::endl;
}
else
{
for (Position i=0; i<comments_.size(); ++i)
{
getFileStream() << comments_[i] << std::endl;
}
getFileStream() << std::endl;
}
writeNodes_(surface);
writeElements_(surface);
writeCharges_(ac);
return true;
}
void HMOFile::readNodeData_(Surface& surface)
{
// at this point, we know that we are at the BEG_NODL_DATA line
bool parsing_done = false;
// the next line will contain the number of nodes
if (!readLine())
{
throw Exception::ParseError(__FILE__, __LINE__, "HMOFile::readNodeData_", "node data does not contain number of entries");
}
Size num_nodes = 0;
try
{
num_nodes = line_.toInt();
}
catch (...)
{
throw Exception::ParseError(__FILE__, __LINE__, "HMOFile::readNodeData_", "node data does not contain number of entries");
}
Size current_node = 0;
std::vector<String> split;
while (!parsing_done && readLine())
{
line_.split(split);
if (line_.hasSubstring("END_NODL_DATA"))
{
parsing_done = true;
break;
}
try
{
surface.vertex.push_back(Vector3(split[1].toFloat(), split[2].toFloat(), split[3].toFloat()));
}
catch (...)
{
throw Exception::ParseError(__FILE__, __LINE__, "HMOFile::readNodeData_", "invalid node entry");
}
++current_node;
}
if (current_node != num_nodes)
{
Log.warn() << "HMOFile::readNodeData_: number of nodes found ("
<< current_node
<< ") does not match number of nodes expected ("
<< num_nodes
<< ")!" << std::endl;
}
if (!parsing_done)
{
throw Exception::ParseError(__FILE__, __LINE__, "HMOFile::readNodeData_", "node data not correctly terminated");
}
}
void HMOFile::readElementData_(Surface& surface)
{
// at this point, we know that we are at the BEG_ELEM_DATA line
bool parsing_done = false;
// the next line will contain the number of elements of the different supported kinds
if (!readLine())
{
throw Exception::ParseError(__FILE__, __LINE__, "HMOFile::readElementData_", "element data does not contain number of entries");
}
std::vector<String> elem_numbers;
line_.split(elem_numbers);
if (elem_numbers.size() != 13)
{
throw Exception::ParseError(__FILE__, __LINE__, "HMOFile::readElementData_", "format error in first line of elements block (expected 13 numbers)");
}
Size total_num_elements = 0;
try
{
total_num_elements = elem_numbers[0].toInt();
Size sum_elements = elem_numbers[1].toInt();
for (Position i=2; i<13; ++i)
sum_elements += elem_numbers[i].toInt();
if (total_num_elements != sum_elements)
{
Log.warn() << "HMOFile::readElementData_: number of total elements ("
<< total_num_elements
<< ") does not match sum of elements by type ("
<< sum_elements
<< ")!" << std::endl;
}
}
catch (...)
{
throw Exception::ParseError(__FILE__, __LINE__, "HMOFile::readElementData_", "format error in number of elements");
}
Size current_elem = 0;
std::vector<String> split;
while (!parsing_done && readLine())
{
line_.split(split);
if (line_.hasSubstring("END_ELEM_DATA"))
{
parsing_done = true;
break;
}
try
{
// TODO: generate different surfaces for different components. so far, we just ignore it
Size component_number = split[1].toInt();
if (component_number != 1)
{
Log.warn() << "HMOFile::readElementData_: support for multiple components is not implemented yet" << std::endl;
}
Size element_type = split[2].toInt();
if (element_type != T3)
{
Log.warn() << "HMOFile::readElementData_: support for non-triangular elements not implemented yet; ignoring element" << std::endl;
continue;
}
Surface::Triangle t;
t.v1 = split[3].toInt();
t.v2 = split[4].toInt();
t.v3 = split[5].toInt();
surface.triangle.push_back(t);
}
catch (...)
{
throw Exception::ParseError(__FILE__, __LINE__, "HMOFile::readNodeData_", "invalid element entry");
}
++current_elem;
}
if (current_elem != total_num_elements)
{
Log.warn() << "HMOFile::readElementData_: number of elements found ("
<< current_elem
<< ") does not match number of elements expected ("
<< total_num_elements
<< ")!" << std::endl;
}
if (!parsing_done)
{
throw Exception::ParseError(__FILE__, __LINE__, "HMOFile::readElementData_", "element data not correctly terminated");
}
}
void HMOFile::readChargeData_()
{
// at this point, we know that we are at the BEG_CHARGE_DATA line
bool parsing_done = false;
// the next line will contain the number of charges
if (!readLine())
{
throw Exception::ParseError(__FILE__, __LINE__, "HMOFile::readChargeData_", "charge data does not contain number of entries");
}
Size num_charges = 0;
try
{
num_charges = line_.toInt();
}
catch (...)
{
throw Exception::ParseError(__FILE__, __LINE__, "HMOFile::readChargeData_", "charge data does not contain number of entries");
}
Size current_charge = 0;
std::vector<String> split;
while (!parsing_done && readLine())
{
line_.split(split);
if (line_.hasSubstring("END_CHARGE_DATA"))
{
parsing_done = true;
break;
}
try
{
HMOCharge c;
c.position.x = split[1].toFloat();
c.position.y = split[2].toFloat();
c.position.z = split[3].toFloat();
c.value = split[4].toFloat();
charges_.push_back(c);
}
catch (...)
{
throw Exception::ParseError(__FILE__, __LINE__, "HMOFile::readChargeData_", "invalid charge entry");
}
++current_charge;
}
if (current_charge != num_charges)
{
Log.warn() << "HMOFile::readChargeData_: number of charges found ("
<< current_charge
<< ") does not match number of charges expected ("
<< num_charges
<< ")!" << std::endl;
}
if (!parsing_done)
{
throw Exception::ParseError(__FILE__, __LINE__, "HMOFile::readChargeData_", "charge data not correctly terminated");
}
}
bool HMOFile::readUntil_(String const& pattern)
{
bool pattern_found = false;
while (!pattern_found && readLine())
{
// is this a comment?
if (line_.hasPrefix("#"))
{
comments_.push_back(line_);
continue;
}
// nope. does it match the pattern?
if (line_.hasSubstring(pattern))
{
pattern_found = true;
}
}
return pattern_found;
}
void HMOFile::writeNodes_(Surface const& surface)
{
getFileStream() << "BEG_NODL_DATA" << std::endl;
getFileStream() << "\t" << surface.vertex.size() << std::endl;
for (Position i=0; i<surface.vertex.size(); ++i)
{
Vector3 const& v = surface.vertex[i];
getFileStream() << "\t" << i+1 << " " << v.x << " " << v.y << " " << v.z << std::endl;
}
getFileStream() << "END_NODL_DATA" << std::endl << std::endl;
}
void HMOFile::writeElements_(Surface const& surface)
{
getFileStream() << "BEG_ELEM_DATA" << std::endl;
// NOTE: so far, we only support elements of type T3, and single components
Size num_triangles = surface.triangle.size();
getFileStream() << "\t" << num_triangles << " 0 0 " << num_triangles << " 0 0 0 0 0 0 0 0 0" << std::endl;
for (Position i=0; i<num_triangles; ++i)
{
Surface::Triangle const& t = surface.triangle[i];
getFileStream() << "\t" << i+1 << "\t" << 1 << " " << T3 << "\t";
getFileStream() << t.v1 << " " << t.v2 << " " << t.v3 << std::endl;
}
getFileStream() << "END_ELEM_DATA" << std::endl << std::endl;
}
void HMOFile::writeCharges_(AtomContainer const& ac)
{
getFileStream() << "BEG_CHARGE_DATA" << std::endl;
Size num_charges = ac.countAtoms();
getFileStream() << "\t" << num_charges << std::endl;
Size current_atom = 1;
for (AtomConstIterator at_it = ac.beginAtom(); +at_it; ++at_it)
{
Vector3 const& pos = at_it->getPosition();
getFileStream() << "\t" << current_atom << " " << pos.x << " " << pos.y << " " << pos.z << " " << at_it->getCharge() << std::endl;
++current_atom;
}
getFileStream() << "END_CHARGE_DATA" << std::endl;
}
}
|