| 1 |
///////////////////////////////////////////////////////////// |
| 2 |
// // |
| 3 |
// Copyright (c) 2007-2017 by The University of Queensland // |
| 4 |
// Centre for Geoscience Computing // |
| 5 |
// http://earth.uq.edu.au/centre-geoscience-computing // |
| 6 |
// // |
| 7 |
// Primary Business: Brisbane, Queensland, Australia // |
| 8 |
// Licensed under the Open Software License version 3.0 // |
| 9 |
// http://www.apache.org/licenses/LICENSE-2.0 // |
| 10 |
// // |
| 11 |
///////////////////////////////////////////////////////////// |
| 12 |
|
| 13 |
#include "GenericShape.h" |
| 14 |
|
| 15 |
#include <stdlib.h> |
| 16 |
#include <fstream> |
| 17 |
|
| 18 |
#ifdef DEBUG |
| 19 |
#include <stdio.h> |
| 20 |
#define DPRINT(...) printf("%s:%d ",__FUNCTION__,__LINE__);printf(__VA_ARGS__); |
| 21 |
#else |
| 22 |
#define DPRINT(...) |
| 23 |
#endif /* DEBUG */ |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#define NEXTLINE \ |
| 28 |
if ( valid == 1 ) { \ |
| 29 |
if ( std::getline(infile,line).eof()) { \ |
| 30 |
valid = 0; \ |
| 31 |
} \ |
| 32 |
} |
| 33 |
|
| 34 |
GenericShape::GenericShape(string fileDB, string name) { |
| 35 |
int valid; |
| 36 |
regex re; |
| 37 |
string line; |
| 38 |
std::ifstream infile(fileDB.c_str()); |
| 39 |
valid = 1; |
| 40 |
NEXTLINE |
| 41 |
try { |
| 42 |
// Find the shape in the file with the correct name |
| 43 |
re.assign("name=\"" + name + "\","); |
| 44 |
while ( (!regex_match(line,re)) && ( valid == 1)) { |
| 45 |
NEXTLINE // Find matching shape |
| 46 |
} |
| 47 |
if (regex_match(line,re)) { |
| 48 |
NEXTLINE // Will retrieve "sphereList = [" line |
| 49 |
NEXTLINE // Skip over "sphereList = [" line to first sphere |
| 50 |
// Extract out all following lines containing 4-tuples as origins and |
| 51 |
// radii |
| 52 |
regex retwo; |
| 53 |
retwo.assign("\\((-?[\\d\\.]+),(-?[\\d\\.]+),(-?[\\d\\.]+),([\\d\\.]+)\\),?"); |
| 54 |
boost::cmatch matches; |
| 55 |
while (regex_match(line.c_str(),matches,retwo)) { |
| 56 |
string xstr(matches[1].first, matches[1].second); |
| 57 |
double x = strtod(xstr.c_str(),NULL); |
| 58 |
string ystr(matches[2].first, matches[2].second); |
| 59 |
double y = strtod(ystr.c_str(),NULL); |
| 60 |
string zstr(matches[3].first, matches[3].second); |
| 61 |
double z = strtod(zstr.c_str(),NULL); |
| 62 |
|
| 63 |
string rstr(matches[4].first, matches[4].second); |
| 64 |
double r = strtod(rstr.c_str(),NULL); |
| 65 |
|
| 66 |
if (( sqrt(x*x+y*y+z*z) <= 1.0f ) && ( r > 0 )) { |
| 67 |
Vector3 center(x,y,z); |
| 68 |
origins.push_back(center); |
| 69 |
radii.push_back(r); |
| 70 |
} |
| 71 |
NEXTLINE // Next line in file |
| 72 |
} |
| 73 |
|
| 74 |
NEXTLINE // Skip over "]" line |
| 75 |
NEXTLINE // Skip over "bondList = [" line |
| 76 |
|
| 77 |
// Extract out all following lines containing tuples as bonds. |
| 78 |
retwo.assign("\\((\\d+),(\\d+)\\),?"); |
| 79 |
while (regex_match(line.c_str(),matches,retwo)) { |
| 80 |
string firststr(matches[1].first, matches[1].second); |
| 81 |
unsigned int first = atoi(firststr.c_str()); |
| 82 |
string secondstr(matches[2].first, matches[2].second); |
| 83 |
unsigned int second = atoi(secondstr.c_str()); |
| 84 |
if ((first <= radii.size()) &&( second <= radii.size()) && |
| 85 |
(first != second)) { |
| 86 |
vector<int> bond; |
| 87 |
bond.push_back(first); |
| 88 |
bond.push_back(second); |
| 89 |
std::cout << "Pair added: " << first << ", " << second << std::endl; |
| 90 |
bonds.push_back(bond); |
| 91 |
} |
| 92 |
NEXTLINE |
| 93 |
} |
| 94 |
// At this point we simply assume the shape is now valid |
| 95 |
// |
| 96 |
valid = 1; |
| 97 |
} |
| 98 |
} catch (boost::regex_error& e) { |
| 99 |
std::cout << "Regexp failed:" << e.code() << std::endl; |
| 100 |
} |
| 101 |
if ( origins.size() != radii.size() ) { |
| 102 |
std::cout << "Parsed " << radii.size() << " radii and " << origins.size() |
| 103 |
<< " origins. Something broken.\n"; |
| 104 |
} |
| 105 |
std::cout << "Parsed " << radii.size() << " spheres and " << bonds.size() |
| 106 |
<< " bonds.\n"; |
| 107 |
if ( valid != 1 ) { |
| 108 |
std::cout << "Error parsing." << std::endl; |
| 109 |
bonds.clear(); |
| 110 |
radii.clear(); |
| 111 |
origins.clear(); |
| 112 |
} |
| 113 |
infile.close(); |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
void GenericShape::insert(Vector3 pos, double radius, MNTable3D* table, int tag, int id) { |
| 120 |
int *ids; |
| 121 |
if ( ( ids = (int *)(malloc(radii.size()*sizeof(int)))) == NULL ) { |
| 122 |
std::cout << "Err: Cannot allocate memory for IDs" << std::endl; |
| 123 |
return; |
| 124 |
} |
| 125 |
if ( this->useRandomOrientation() ) { |
| 126 |
this->setRandomOrientation(); |
| 127 |
} |
| 128 |
unsigned int k; |
| 129 |
for(k=0;k<radii.size();k++){ |
| 130 |
Vector3 offset=origins[k]*radius; |
| 131 |
Sphere Sk(pos + rotatePoint(offset),radius*radii[k]); |
| 132 |
if(table->checkInsertable(Sk,id)){ |
| 133 |
Sk.setTag(this->getParticleTag()); |
| 134 |
table->insert(Sk,id); |
| 135 |
ids[k] = Sk.Id(); |
| 136 |
} else { |
| 137 |
ids[k] = 0; |
| 138 |
} |
| 139 |
} |
| 140 |
for(k=0;k<bonds.size();k++){ |
| 141 |
vector<int> bondpair = bonds[k]; |
| 142 |
int particleA = bondpair[0]; |
| 143 |
int particleB = bondpair[1]; |
| 144 |
if (( ids[particleA] == 0 )|| ( ids[particleB] == 0)) { |
| 145 |
} else { |
| 146 |
table->insertBond(ids[particleA],ids[particleB],this->getBondTag()); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
int GenericShape::bias() { |
| 153 |
return Shape::bias(); |
| 154 |
} |
| 155 |
|
| 156 |
void GenericShape::setBias(int i) { |
| 157 |
Shape::setBias(i); |
| 158 |
} |
| 159 |
|