/*
 * 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 .
 */
/*
 * MolecularVolumeAction.cpp
 *
 *  Created on: May 12, 2010
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include "config.hpp"
#include "CodePatterns/Log.hpp"
#include "CodePatterns/Verbose.hpp"
#include "LinkedCell/linkedcell.hpp"
#include "LinkedCell/PointCloudAdaptor.hpp"
#include "molecule.hpp"
#include "Tesselation/boundary.hpp"
#include "Tesselation/tesselation.hpp"
#include "World.hpp"
#include 
#include 
#include "Actions/AnalysisAction/MolecularVolumeAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "MolecularVolumeAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
ActionState::ptr AnalysisMolecularVolumeAction::performCall() {
  // execute action
  for (World::MoleculeSelectionIterator iter = World::getInstance().beginMoleculeSelection(); iter != World::getInstance().endMoleculeSelection(); ++iter) {
    molecule *mol = iter->second;
    class Tesselation *TesselStruct = NULL;
    const LinkedCell_deprecated *LCList = NULL;
    LOG(0, "Evaluating volume of the convex envelope.");
    PointCloudAdaptor< molecule > cloud(mol, mol->name);
    LCList = new LinkedCell_deprecated(cloud, 10.);
    config * const configuration = World::getInstance().getConfig();
    //Boundaries *BoundaryPoints = NULL;
    //FindConvexBorder(mol, BoundaryPoints, TesselStruct, LCList, argv[argptr]);
    FindNonConvexBorder(mol, TesselStruct, LCList, 5., NULL);
    //RemoveAllBoundaryPoints(TesselStruct, mol, argv[argptr]);
    double volumedifference = ConvexizeNonconvexEnvelope(TesselStruct, mol, NULL);
    double clustervolume = TesselStruct->getVolumeOfConvexEnvelope(configuration->GetIsAngstroem());
    LOG(0, "The tesselated volume area is " << clustervolume << " " << (configuration->GetIsAngstroem() ? "angstrom" : "atomiclength") << "^3.");
    LOG(0, "The non-convex tesselated volume area is " << clustervolume-volumedifference << " " << (configuration->GetIsAngstroem() ? "angstrom" : "atomiclength") << "^3.");
    delete(TesselStruct);
    delete(LCList);
  }
  return Action::success;
}
ActionState::ptr AnalysisMolecularVolumeAction::performUndo(ActionState::ptr _state) {
  return Action::success;
}
ActionState::ptr AnalysisMolecularVolumeAction::performRedo(ActionState::ptr _state){
  return Action::success;
}
bool AnalysisMolecularVolumeAction::canUndo() {
  return true;
}
bool AnalysisMolecularVolumeAction::shouldUndo() {
  return true;
}
/** =========== end of function ====================== */