/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 * 
 *
 *   This file is part of MoleCuilder.
 *
 *    MoleCuilder is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    MoleCuilder is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with MoleCuilder.  If not, see .
 */
/*
 * AtomShapeDescriptor.cpp
 *
 *  Created on: Jul 22, 2010
 *      Author: crueger
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include "AtomShapeDescriptor.hpp"
#include "AtomShapeDescriptor_impl.hpp"
#include "Atom/atom.hpp"
AtomShapeDescriptor_impl::AtomShapeDescriptor_impl(const Shape &_shape) :
  shape(_shape)
{}
AtomShapeDescriptor_impl::~AtomShapeDescriptor_impl(){}
bool AtomShapeDescriptor_impl::predicate(std::pair atom){
  return shape.isInside(atom.second->getPosition());
}
atom* AtomShapeDescriptor_impl::find(){
  LinkedCell::LinkedCell_View view = World::getInstance().getLinkedCell(shape.getRadius());
  LinkedCell::LinkedList list = view.getPointsInsideSphere(shape.getRadius(), shape.getCenter());
  for (LinkedCell::LinkedList::iterator iter = list.begin(); iter != list.end(); ++iter) {
    atom * const _atom = static_cast(const_cast(*iter));
    if (shape.isInside(_atom->getPosition()))
      return _atom;
  }
  return NULL;
}
std::vector AtomShapeDescriptor_impl::findAll(){
  LinkedCell::LinkedCell_View view = World::getInstance().getLinkedCell(shape.getRadius());
  LinkedCell::LinkedList list = view.getPointsInsideSphere(shape.getRadius(), shape.getCenter());
  std::vector res;
  for (LinkedCell::LinkedList::iterator iter = list.begin(); iter != list.end(); ++iter) {
    atom * const _atom = static_cast(const_cast(*iter));
    if (shape.isInside(_atom->getPosition()))
      res.push_back(_atom);
  }
  return res;
}
AtomDescriptor AtomsByShape(const Shape &shape){
  return AtomDescriptor(AtomDescriptor::impl_ptr(new AtomShapeDescriptor_impl(shape)));
}