/* -*- mona-c++ -*-
* Copyright (c) Leipzig, Madrid 2004 - 2008
* Max-Planck-Institute for Human Cognitive and Brain Science
* Max-Planck-Institute for Evolutionary Anthropology
* BIT, ETSI Telecomunicacion, UPM
*
* 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
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include <libmona/shapes.hh>
#include <libmona/plugin_handler.cxx>
MONA_NAMESPACE_BEGIN
using namespace std;
static char const *plugin_type = "creator";
char const image2d_data[] = "2dshape";
char const image3d_data[] = "3dshape";
template <>
struct less_then< T2DVector<int> > {
bool operator() (const T2DVector<int>& a, const T2DVector<int>& b)const
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
};
template <>
struct less_then< T3DVector<int> > {
bool operator() (const T3DVector<int>& a, const T3DVector<int>& b)const
{
return a.x < b.x || (a.x == b.x && (a.y < b.y || a.y == b.y && a.z < b.z ));
}
};
template <template <typename> class T, typename M>
CShape<T,M>::CShape():
_M_shape(less_then<T<int> >())
{
}
template <template <typename> class T, typename M>
CShape<T,M>::~CShape()
{
}
template <template <typename> class T, typename M>
typename CShape<T,M>::const_iterator CShape<T,M>::begin() const
{
return _M_shape.begin();
}
template <template <typename> class T, typename M>
typename CShape<T,M>::const_iterator CShape<T,M>::end() const
{
return _M_shape.end();
}
static void adjust(T2DVector<int>& size, const T2DVector<int>& p)
{
int x = (p.x < 0 ? -2 * p.x : 2 * p.x) + 1;
int y = (p.y < 0 ? -2 * p.y : 2 * p.y) + 1;
if (size.x < x)
size.x = x;
if (size.y < y)
size.y = y;
}
static void adjust(T3DVector<int>& size, const T3DVector<int>& p)
{
int x = (p.x < 0 ? -2 * p.x : 2 * p.x) + 1;
int y = (p.y < 0 ? -2 * p.y : 2 * p.y) + 1;
int z = (p.z < 0 ? -2 * p.z : 2 * p.z) + 1;
if (size.x < x)
size.x = x;
if (size.y < y)
size.y = y;
if (size.z < z)
size.z = z;
}
template <template <typename> class T, typename M>
typename CShape<T,M>::Size CShape<T,M>::get_size() const
{
return typename CShape<T,M>::Size(_M_size);
}
template <template <typename> class T, typename M>
void CShape<T,M>::insert(const T<int>& p)
{
_M_shape.insert(p);
adjust(_M_size,p);
}
template <template <typename> class T, typename M>
typename CShape<T,M>::Mask CShape<T,M>::get_mask()const
{
typename CShape<T,M>::Mask result(get_size());
result.clear();
T<int> half_size = _M_size / 2;
for (const_iterator i = begin(), e = end(); i != e; ++i){
result(T<unsigned int>(*i + half_size)) = true;
}
return result;
}
template <typename S, const char *P, const char *D>
CShapeFactory<S,P, D>::CShapeFactory(const char *name):
TFactory<S>(name, plugin_type, D)
{
}
template <typename S, const char *P, const char *D>
CShapeFactory<S, P, D>::~CShapeFactory()
{
}
const char plugin_shape_pattern_2d[] = ".*_2dshape_creator\\.so";
const char plugin_shape_pattern_3d[] = ".*_3dshape_creator\\.so";
template class CShape<T2DVector, C2DBitImage>;
template class CShape<T3DVector, C3DBitImage>;
template class CShapeFactory<C2DShape, plugin_shape_pattern_2d, image2d_data>;
template class CShapeFactory<C3DShape, plugin_shape_pattern_3d, image3d_data>;
template class TPluginHandler<C2DShapeFactory>;
template class TPluginHandler<C3DShapeFactory>;
MONA_NAMESPACE_END