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
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#include <BALL/STRUCTURE/geometricProperties.h>
#include <BALL/KERNEL/atom.h>
#include <BALL/KERNEL/fragment.h>
#include <BALL/KERNEL/residue.h>
#include <BALL/MATHS/matrix44.h>
#include <cstdio>
#include <cmath>
#include <deque>
#include <set>
namespace BALL
{
bool BoundingBoxProcessor::start()
{
lower_.set(std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
upper_.set(-std::numeric_limits<float>::max(), -std::numeric_limits<float>::max(), -std::numeric_limits<float>::max());
return true;
}
bool BoundingBoxProcessor::finish()
{
if ((lower_.x == std::numeric_limits<float>::max()) && (lower_.y == std::numeric_limits<float>::max()) && (lower_.z == std::numeric_limits<float>::max())
&& (upper_.x == -std::numeric_limits<float>::max()) && (upper_.y == -std::numeric_limits<float>::max()) && (upper_.z == -std::numeric_limits<float>::max()))
{
lower_.set(0, 0, 0);
upper_.set(0, 0, 0);
return false;
}
return true;
}
Processor::Result BoundingBoxProcessor::operator()(const Vector3& v)
{
if (lower_.x > v.x)
{
lower_.x = v.x;
}
if (lower_.y > v.y)
{
lower_.y = v.y;
}
if (lower_.z > v.z)
{
lower_.z = v.z;
}
if (upper_.x < v.x)
{
upper_.x = v.x;
}
if (upper_.y < v.y)
{
upper_.y = v.y;
}
if (upper_.z < v.z)
{
upper_.z = v.z;
}
return Processor::CONTINUE;
}
SimpleBox3 BoundingBoxProcessor::getBox() const
{
return SimpleBox3(lower_, upper_);
}
const Vector3& BoundingBoxProcessor::getLower() const
{
return lower_;
}
const Vector3& BoundingBoxProcessor::getUpper() const
{
return upper_;
}
// GeometricCenterProcessor
bool GeometricCenterProcessor::start()
{
center_.set(0, 0, 0);
n_ = 0;
return true;
}
bool GeometricCenterProcessor::finish()
{
if (n_ != 0)
{
center_ /= (float)n_;
}
return true;
}
Processor::Result GeometricCenterProcessor::operator()(const Vector3& v)
{
center_ += v;
n_++;
return Processor::CONTINUE;
}
Vector3& GeometricCenterProcessor::getCenter()
{
return center_;
}
// ---------- FragmentDistanceCollector -----------------
// default constructor
FragmentDistanceCollector::FragmentDistanceCollector()
: reference_composite_(0)
{
}
FragmentDistanceCollector::FragmentDistanceCollector(const Composite& composite)
: reference_composite_(&composite),
squared_distance_(0)
{
}
FragmentDistanceCollector::FragmentDistanceCollector(const Composite& composite,float distance)
: reference_composite_(&composite),
squared_distance_(distance * distance)
{
}
bool FragmentDistanceCollector::start()
{
// clear the array containing the collected fragments
fragments.clear();
// if no reference fragment is set, return immediately
return reference_composite_ != 0;
}
float FragmentDistanceCollector::getDistance() const
{
return sqrt(squared_distance_);
}
void FragmentDistanceCollector::setDistance(float distance)
{
squared_distance_ = distance * distance;
}
bool FragmentDistanceCollector::finish()
{
bool collect_it = false;
AtomConstIterator atom_iterator2;
const Fragment* mol_fragment;
Composite::CompositeConstIterator composite_it;
vector<const Atom*> reference_atoms;
const Atom* atom_ptr;
GeometricCenterProcessor center_processor;
Vector3 center;
float fragment_radius;
float difference;
Size i, j;
Size size;
for (composite_it = reference_composite_->beginComposite();
composite_it != reference_composite_->endComposite(); ++composite_it)
{
if (RTTI::isKindOf<Atom>(&*composite_it))
{
atom_ptr = RTTI::castTo<Atom>(*composite_it);
reference_atoms.push_back(atom_ptr);
}
}
Size number_of_atoms = 0;
Vector3 atom_position;
float distance = sqrt(squared_distance_);
number_of_atoms = (Size)reference_atoms.size();
size = (Size)all_fragments_.size();
for (i = 0; i < size; i++)
{
mol_fragment = all_fragments_[i];
mol_fragment->apply(center_processor);
center = center_processor.getCenter();
fragment_radius = 0;
for (atom_iterator2 = mol_fragment->beginAtom(); +atom_iterator2; ++ atom_iterator2)
{
if ((difference = (*atom_iterator2).getPosition().getSquareDistance(center)) > fragment_radius)
{
fragment_radius = difference;
}
}
fragment_radius = sqrt(fragment_radius);
for (j = 0, collect_it = false; j < number_of_atoms && !collect_it; j++)
{
atom_position = reference_atoms[j]->getPosition();
if (atom_position.getDistance(center) <= (distance + fragment_radius))
{
for (atom_iterator2 = mol_fragment->beginAtom();
+atom_iterator2 && (!collect_it); ++ atom_iterator2)
{
if ((*atom_iterator2).getPosition().getSquareDistance(atom_position) < squared_distance_)
{
fragments.push_back(mol_fragment);
collect_it = true;
}
}
}
}
}
all_fragments_.clear();
return true;
}
Processor::Result FragmentDistanceCollector::operator()(const Composite& composite)
{
const Fragment* mol_fragment = dynamic_cast<const Fragment*>(&composite);
if (mol_fragment)
{
all_fragments_.push_back(mol_fragment);
}
return Processor::CONTINUE;
}
Size FragmentDistanceCollector::getNumberOfFragments()
{
return (Size)fragments.size();
}
void FragmentDistanceCollector::setComposite(const Composite& composite)
{
reference_composite_ = &composite;
}
const Composite* FragmentDistanceCollector::getComposite() const
{
return reference_composite_;
}
// Calculate the torsion angle between four atoms
Angle calculateTorsionAngle(const Atom& a1, const Atom& a2, const Atom& a3, const Atom& a4)
{
Vector3 a12(a2.getPosition() - a1.getPosition());
Vector3 a23(a3.getPosition() - a2.getPosition());
Vector3 a34(a4.getPosition() - a3.getPosition());
Vector3 n12(a12 % a23);
Vector3 n34(a23 % a34);
if (n12 == Vector3::getZero() ||
n34 == Vector3::getZero())
{
throw(Exception::IllegalPosition(__FILE__, __LINE__, 0, 0, 0));
}
n12.normalize();
n34.normalize();
Vector3 cross_n12_n34(n12 % n34);
float direction = cross_n12_n34 * a23;
float scalar_product = n12 * n34;
if (scalar_product > 1.0)
{
scalar_product = 1.0;
}
if (scalar_product < -1.0)
{
scalar_product = -1.0;
}
Angle a(acos(scalar_product));
if (direction < 0.0)
{
a = -1.0 * (float)a;
}
return a;
}
bool setTorsionAngle(const Atom& a1, const Atom& a2, Atom& a3, const Atom& a4, Angle angle)
{
//We first need to determine the part of the molecule which needs to be
//rotated. this will be done by a simple BFS.
std::deque<Atom*> bfs_queue;
//This set containes the atoms which should be rotated
std::set<Atom*> component;
//The starting point needs to be handled explicitly, as
//we may not consider a2
component.insert(&a3);
for(unsigned int i = 0; i < a3.countBonds(); ++i)
{
if(a3.getPartnerAtom(i) != &a2)
{
component.insert(a3.getPartnerAtom(i));
bfs_queue.push_back(a3.getPartnerAtom(i));
}
}
//Perform the BFS
while(!bfs_queue.empty())
{
Atom* atom = bfs_queue.front();
bfs_queue.pop_front();
for(unsigned int i = 0; i < atom->countBonds(); ++i)
{
//If a2 is in the same connected component as a3, we cannot
//set the torision angle
if(atom->getPartnerAtom(i) == &a2)
{
return false;
}
if(component.find(atom->getPartnerAtom(i)) == component.end())
{
component.insert(atom->getPartnerAtom(i));
bfs_queue.push_back(atom->getPartnerAtom(i));
}
}
}
//Now compute the current torsion angle and compute the residual rotation
angle -= calculateTorsionAngle(a1, a2, a3, a4);
//Setup the rotation. We first need to make a3 the origin
//of the selected component, then apply the rotation and
//subsequently translate a3 to its original position.
Matrix4x4 rotation;
rotation.rotate(angle, a3.getPosition() - a2.getPosition());
Matrix4x4 trans;
trans.setTranslation(-a3.getPosition());
rotation *= trans;
trans.setTranslation(a3.getPosition());
rotation = trans * rotation;
for(std::set<Atom*>::iterator it = component.begin(); it != component.end(); ++it)
{
(*it)->setPosition(rotation * (*it)->getPosition());
}
return true;
}
// Calculate the bond angle between three atoms
Angle calculateBondAngle(const Atom& a1, const Atom& a2, const Atom& a3)
{
// two atoms can't have the same position
if (a1.getPosition() == a2.getPosition() ||
a3.getPosition() == a2.getPosition())
{
throw(Exception::IllegalPosition(__FILE__, __LINE__, a2.getPosition().x,
a2.getPosition().y, a2.getPosition().z));
}
Vector3 a12(a1.getPosition() - a2.getPosition());
Vector3 a23(a3.getPosition() - a2.getPosition());
a12.normalize();
a23.normalize();
float scalar_product = a12 * a23;
if (scalar_product > 1.0)
{
scalar_product = 1.0;
}
if (scalar_product < -1.0)
{
scalar_product = -1.0;
}
Angle a(acos(scalar_product));
return a;
}
} // namespace BALL
|