// Copyright (c) 2019, Christoph Peters
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Karlsruhe Institute of Technology nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#include "Grid3D.h"

/*! This utility function template reads the binary data of an object from a
	stream.*/
template<class PlainOldDataType>
void readPlainOldData(PlainOldDataType& rOutData, std::istream& rInputStream) {
	rInputStream.read(reinterpret_cast<char*>(&rOutData), sizeof(PlainOldDataType));
}

/*! This utility function template writes the binary data of an object to a
	stream.*/
template<class PlainOldDataType>
void writePlainOldData(const PlainOldDataType& rData, std::ostream& rOutputStream) {
	rOutputStream.write(reinterpret_cast<const char*>(&rData), sizeof(PlainOldDataType));
}


Grid3D::Grid3D(std::array<std::size_t, 3> resolution, std::array<float, 3> cuboidMin, std::array<float, 3> cuboidMax) :
	m_resolution(resolution),
	m_cuboidMin(cuboidMin),
	m_cuboidMax(cuboidMax),
	m_data(resolution[0] * resolution[1] * resolution[2])
{
	update();
}


Grid3D::Grid3D(std::istream& rInputStream) :
	m_resolution({ 0,0,0 })
{
	std::size_t version;
	readPlainOldData<std::size_t>(version, rInputStream);
	if(version != 0) {
		return;
	}
	readPlainOldData(m_resolution, rInputStream);
	readPlainOldData(m_cuboidMin, rInputStream);
	readPlainOldData(m_cuboidMax, rInputStream);
	m_data.resize(getCellCount());
	rInputStream.read(reinterpret_cast<char*>(m_data.data()), sizeof(std::array<float, 3>) * m_data.size());
	update();
}

void Grid3D::expandGrid(std::array<std::size_t, 3> resolution, std::array<float, 3>& rCuboidMin, std::array<float, 3>& rCuboidMax) {
	for(std::size_t i = 0; i != 3; ++i) {
		float newMin = ((1.0f - 2.0f * resolution[i]) * rCuboidMin[i] + rCuboidMax[i]) / (2.0f * (1.0f - resolution[i]));
		float newMax = ((1.0f - 2.0f * resolution[i]) * rCuboidMax[i] + rCuboidMin[i]) / (2.0f * (1.0f - resolution[i]));
		rCuboidMin[i] = newMin;
		rCuboidMax[i] = newMax;
	}
}


void Grid3D::store(std::ostream& rOutputStream) const {
	// File format version (has to be 0)
	writePlainOldData<std::size_t>(0, rOutputStream);
	// The resolution and cuboid extent
	writePlainOldData(m_resolution, rOutputStream);
	writePlainOldData(m_cuboidMin, rOutputStream);
	writePlainOldData(m_cuboidMax, rOutputStream);
	// A long array of floats. See ravelMultiIndex() for the order.
	rOutputStream.write(reinterpret_cast<const char*>(m_data.data()), sizeof(std::array<float, 3>) * m_data.size());
}


void Grid3D::update(){
	m_sliceSize = m_resolution[0] * m_resolution[1];
	for(std::size_t i = 0; i != 3; ++i) {
		float invResolution = 1.0f / static_cast<float>(m_resolution[i]);
		m_indexToCuboidFactor[i] = (m_cuboidMax[i] - m_cuboidMin[i]) * invResolution;
		m_indexToCuboidSummand[i] = m_cuboidMin[i] + 0.5f * m_indexToCuboidFactor[i];
	}
}
