dust3d/thirdparty/cgal/CGAL-5.1/include/CGAL/Classification/Image.h

130 lines
3.0 KiB
C
Raw Normal View History

// Copyright (c) 2017 GeometryFactory Sarl (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
2020-10-13 12:44:25 +00:00
// $URL: https://github.com/CGAL/cgal/blob/v5.1/Classification/include/CGAL/Classification/Image.h $
// $Id: Image.h 0779373 2020-03-26T13:31:46+01:00 Sébastien Loriot
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s) : Simon Giraudot
#ifndef CGAL_CLASSIFICATION_IMAGE_H
#define CGAL_CLASSIFICATION_IMAGE_H
#include <CGAL/license/Classification.h>
#include <boost/shared_ptr.hpp>
#include <map>
#define CGAL_CLASSIFICATION_IMAGE_SIZE_LIMIT 100000000
namespace CGAL {
namespace Classification {
/// \cond SKIP_IN_MANUAL
2020-10-13 12:44:25 +00:00
template <typename Type>
class Image
{
typedef std::vector<Type> Vector;
typedef std::map<std::size_t, Type> Map;
2020-10-13 12:44:25 +00:00
std::size_t m_width;
std::size_t m_height;
2020-10-13 12:44:25 +00:00
std::size_t m_depth;
boost::shared_ptr<Vector> m_raw;
boost::shared_ptr<Map> m_sparse;
Type m_default;
// Forbid using copy constructor
Image (const Image&)
{
}
2020-10-13 12:44:25 +00:00
public:
2020-10-13 12:44:25 +00:00
Image () : m_width(0), m_height(0), m_depth(0), m_raw (nullptr)
{
}
2020-10-13 12:44:25 +00:00
Image (std::size_t width, std::size_t height, std::size_t depth = 1)
: m_width (width)
, m_height (height)
, m_depth (depth)
{
2020-10-13 12:44:25 +00:00
if (m_width * m_height * m_depth > 0)
{
2020-10-13 12:44:25 +00:00
if (m_width * m_height * m_depth < CGAL_CLASSIFICATION_IMAGE_SIZE_LIMIT)
m_raw = boost::shared_ptr<Vector> (new Vector(m_width * m_height * m_depth));
else
m_sparse = boost::shared_ptr<Map> (new Map());
}
}
2020-10-13 12:44:25 +00:00
~Image ()
{
}
void free()
{
m_raw = boost::shared_ptr<Vector>();
m_sparse = boost::shared_ptr<Map>();
}
Image& operator= (const Image& other)
{
m_raw = other.m_raw;
m_sparse = other.m_sparse;
m_width = other.width();
m_height = other.height();
2020-10-13 12:44:25 +00:00
m_depth = other.depth();
return *this;
}
2020-10-13 12:44:25 +00:00
std::size_t width() const { return m_width; }
std::size_t height() const { return m_height; }
2020-10-13 12:44:25 +00:00
std::size_t depth() const { return m_depth; }
inline std::size_t coord (const std::size_t& x, const std::size_t& y, const std::size_t& z) const
{
return z + (m_depth * y) + (m_depth * m_height * x);
}
2020-10-13 12:44:25 +00:00
Type& operator() (const std::size_t& x, const std::size_t& y, const std::size_t& z = 0)
{
if (m_raw == boost::shared_ptr<Vector>()) // sparse case
{
2020-10-13 12:44:25 +00:00
typename Map::iterator inserted = m_sparse->insert
(std::make_pair (coord(x,y,z), Type())).first;
return inserted->second;
}
2020-10-13 12:44:25 +00:00
return (*m_raw)[coord(x,y,z)];
}
2020-10-13 12:44:25 +00:00
const Type& operator() (const std::size_t& x, const std::size_t& y, const std::size_t& z = 0) const
{
if (m_raw == boost::shared_ptr<Vector>()) // sparse case
{
2020-10-13 12:44:25 +00:00
typename Map::iterator found = m_sparse->find (coord(x,y,z));
if (found != m_sparse->end())
return found->second;
return m_default;
}
2020-10-13 12:44:25 +00:00
return (*m_raw)[coord(x,y,z)];
}
2020-10-13 12:44:25 +00:00
};
/// \endcond
} // namespace Classification
} // namespace CGAL
#endif // CGAL_CLASSIFICATION_IMAGE_H