//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/Particle/PolyhedralUtil.cpp
//! @brief     Implements class interface Formfactor.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "Sample/Particle/PolyhedralUtil.h"
#include "Base/Util/Assert.h"
#include "Base/Vector/RotMatrix.h"
#include "Sample/Particle/Rotations.h"

namespace {

double vertexZ(const R3& v, const Rotation* rotation)
{
    ASSERT(std::isfinite(v.x()));
    ASSERT(std::isfinite(v.y()));
    ASSERT(std::isfinite(v.z()));
    double z = (rotation ? rotation->transformed(v) : v).z();
    ASSERT(std::isfinite(z));
    return z;
}

} // namespace


double PolyhedralUtil::zMin(const std::vector<R3>& vertices, const Rotation* rotation)
{
    ASSERT(!vertices.empty());
    double result = ::vertexZ(vertices[0], rotation);
    for (size_t i = 1; i < vertices.size(); ++i)
        result = std::min(result, ::vertexZ(vertices[i], rotation));
    return result;
}

double PolyhedralUtil::zMax(const std::vector<R3>& vertices, const Rotation* rotation)
{
    ASSERT(!vertices.empty());
    double result = ::vertexZ(vertices[0], rotation);
    for (size_t i = 1; i < vertices.size(); ++i)
        result = std::max(result, ::vertexZ(vertices[i], rotation));
    return result;
}
