/******************************************************************************\

                     This file is part of the FAHViewer.

           The FAHViewer displays 3D views of Folding@home proteins.
                 Copyright (c) 2003-2013, Stanford University
                             All rights reserved.

    This program 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.

    This program 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 this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

                For information regarding this software email:
                               Joseph Coffland
                        joseph@cauldrondevelopment.com

\******************************************************************************/

#include "Positions.h"

using namespace std;
using namespace cb;
using namespace FAH;


void Positions::init() {
  // Compute radius as max distance from origin
  radius = 0;
  Vector3D origin;

  for (unsigned i = 0; i < size(); i++) {
    double r = at(i).distance(origin);
    if (radius < r) radius = r;
  }

  // Find bounds
  bounds = Rectangle3D();
  for (unsigned i = 0; i < size(); i++) bounds.add(at(i));
}


void Positions::translate(const Vector3D &offset) {
  for (unsigned i = 0; i < size(); i++)
    at(i) += offset;

  this->offset += offset;

  init();
}


Vector3D Positions::findCenterOfMass() const {
  Vector3D com;
  for (unsigned i = 0; i < size(); i++) com += at(i);
  return com / size();
}


void Positions::translateToCenterOfMass() {
  translate(-findCenterOfMass());
}


SmartPointer<PyON::Value> Positions::getPyON() const {
  SmartPointer<PyON::List> list = new PyON::List;

  for (const_iterator it = begin(); it != end(); it++) {
    SmartPointer<PyON::List> coord = new PyON::List;
    coord->append(it->x());
    coord->append(it->y());
    coord->append(it->z());
    list->push_back(coord);
  }

  return list;
}


void Positions::loadPyON(const PyON::Value &value) {
  clear();

  const PyON::List &list = value.getList();

  for (PyON::List::const_iterator it = list.begin(); it != list.end(); it++) {
    const PyON::List &coord = (*it)->getList();
    if (coord.size() != 3) THROWS("Position expected list of length 3");

    push_back(Vector3D(coord[0]->getNumber(),
                       coord[1]->getNumber(),
                       coord[2]->getNumber()));
  }

  init();
}
