// K-3D
// Copyright (c) 1995-2004, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

/** \file
		\author Timothy M. Shead (tshead@k-3d.com)
*/

#include <k3dsdk/object.h>
#include <k3dsdk/persistence.h>
#include <k3dsdk/measurement.h>
#include <k3dsdk/mesh_source.h>
#include <k3dsdk/module.h>
#include <k3dsdk/selection.h>
#include <k3dsdk/transform.h>
#include <k3dsdk/plugins.h>

#include <boost/multi_array.hpp>
#include <iterator>

namespace libk3dmesh
{

/////////////////////////////////////////////////////////////////////////////
// merge_mesh_implementation

class merge_mesh_implementation :
	public k3d::mesh_source<k3d::persistent<k3d::object> >
{
	typedef k3d::mesh_source<k3d::persistent<k3d::object> > base;

public:
	merge_mesh_implementation(k3d::idocument& Document) :
		base(Document),
		m_input_mesh1(k3d::init_name("input_mesh1") + k3d::init_description("Input mesh 1") + k3d::init_value<k3d::mesh*>(0) + k3d::init_document(Document)),
		m_input_mesh2(k3d::init_name("input_mesh2") + k3d::init_description("Input mesh 2") + k3d::init_value<k3d::mesh*>(0) + k3d::init_document(Document))
	{
		register_property(m_input_mesh1);
		register_property(m_input_mesh2);
		
		m_input_mesh1.changed_signal().connect(SigC::slot(*this, &merge_mesh_implementation::on_reset_geometry));
		m_input_mesh2.changed_signal().connect(SigC::slot(*this, &merge_mesh_implementation::on_reset_geometry));
				
		m_output_mesh.need_data_signal().connect(SigC::slot(*this, &merge_mesh_implementation::on_create_geometry));
	}

	void on_reset_geometry()
	{
		m_output_mesh.reset();
	}

	k3d::mesh* on_create_geometry()
	{
		// Get the input geometry ...
		const k3d::mesh* const input_mesh1 = m_input_mesh1.property_value();
		const k3d::mesh* const input_mesh2 = m_input_mesh2.property_value();

		// Create output geometry ...
		k3d::mesh* const output = new k3d::mesh();
		if(input_mesh1)
			k3d::deep_copy(*input_mesh1, *output);

		if(input_mesh2)
			k3d::deep_copy(*input_mesh2, *output);
		
		return output;
	}

	k3d::iplugin_factory& factory()
	{
		return get_factory();
	}

	static k3d::iplugin_factory& get_factory()
	{
		static k3d::plugin_factory<k3d::document_plugin<merge_mesh_implementation> > factory(
			k3d::uuid(0x50aef311, 0xf5264da2, 0x9c5995e8, 0xdc2e4ddf),
			"MergeMesh",
			"Merges two meshes into one",
			"Objects",
			k3d::iplugin_factory::STABLE);

		return factory;
	}

private:
	k3d_data_property(k3d::mesh*, k3d::immutable_name, k3d::change_signal, k3d::no_undo, k3d::local_storage, k3d::no_constraint) m_input_mesh1;
	k3d_data_property(k3d::mesh*, k3d::immutable_name, k3d::change_signal, k3d::no_undo, k3d::local_storage, k3d::no_constraint) m_input_mesh2;
};

/////////////////////////////////////////////////////////////////////////////
// merge_mesh_factory

k3d::iplugin_factory& merge_mesh_factory()
{
	return merge_mesh_implementation::get_factory();
}

} // namespace libk3dmesh

