2007-12-10 20:59:17 +01:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2007-12-10 20:59:17 +01:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-10-23 15:04:25 +02:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2007-12-10 20:59:17 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-10-23 16:09:47 +02:00
|
|
|
#ifndef MAPNIK_OCTREE_HPP
|
|
|
|
#define MAPNIK_OCTREE_HPP
|
2007-12-10 20:59:17 +01:00
|
|
|
|
2009-02-11 17:57:46 +01:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/global.hpp>
|
2011-08-31 04:28:14 +02:00
|
|
|
#include <mapnik/palette.hpp>
|
2012-12-17 03:19:52 +01:00
|
|
|
#include <mapnik/noncopyable.hpp>
|
2009-02-11 16:23:45 +01:00
|
|
|
|
|
|
|
// stl
|
2007-12-10 20:59:17 +01:00
|
|
|
#include <vector>
|
2014-01-29 00:05:10 +01:00
|
|
|
#include <cstring>
|
2008-01-23 16:24:59 +01:00
|
|
|
#include <deque>
|
2012-01-20 22:43:05 +01:00
|
|
|
#include <algorithm>
|
2007-12-10 20:59:17 +01:00
|
|
|
|
|
|
|
namespace mapnik {
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
struct RGBPolicy
|
|
|
|
{
|
|
|
|
const static unsigned MAX_LEVELS = 6;
|
|
|
|
const static unsigned MIN_LEVELS = 2;
|
|
|
|
inline static unsigned index_from_level(unsigned level, rgb const& c)
|
|
|
|
{
|
|
|
|
unsigned shift = 7 - level;
|
2012-02-02 02:53:35 +01:00
|
|
|
return (((c.r >> shift) & 1) << 2)
|
|
|
|
| (((c.g >> shift) & 1) << 1)
|
2010-06-02 13:03:30 +02:00
|
|
|
| ((c.b >> shift) & 1);
|
|
|
|
}
|
|
|
|
};
|
2007-12-10 20:59:17 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
template <typename T, typename InsertPolicy = RGBPolicy >
|
2012-12-17 03:19:52 +01:00
|
|
|
class octree : private mapnik::noncopyable
|
2012-02-02 02:53:35 +01:00
|
|
|
{
|
|
|
|
struct node
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-11-04 12:13:17 +01:00
|
|
|
node()
|
2010-06-02 13:03:30 +02:00
|
|
|
: reds(0),
|
|
|
|
greens(0),
|
|
|
|
blues(0),
|
|
|
|
count(0),
|
|
|
|
count_cum(0),
|
|
|
|
children_count(0),
|
|
|
|
index(0)
|
|
|
|
{
|
2014-01-29 00:05:10 +01:00
|
|
|
std::memset(&children_[0],0,sizeof(children_));
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2009-04-28 09:29:48 +02:00
|
|
|
|
2012-11-04 12:13:17 +01:00
|
|
|
~node()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-11-04 12:13:17 +01:00
|
|
|
for (unsigned i = 0;i < 8; ++i)
|
|
|
|
{
|
|
|
|
if (children_[i] != 0)
|
|
|
|
{
|
|
|
|
delete children_[i];
|
|
|
|
children_[i]=0;
|
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
2009-04-28 09:29:48 +02:00
|
|
|
|
2012-11-04 12:13:17 +01:00
|
|
|
bool is_leaf() const
|
|
|
|
{
|
|
|
|
return count == 0;
|
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
node * children_[8];
|
|
|
|
boost::uint64_t reds;
|
|
|
|
boost::uint64_t greens;
|
|
|
|
boost::uint64_t blues;
|
|
|
|
unsigned count;
|
|
|
|
double reduce_cost;
|
|
|
|
unsigned count_cum;
|
2012-11-04 12:13:17 +01:00
|
|
|
byte children_count;
|
|
|
|
byte index;
|
2010-06-02 13:03:30 +02:00
|
|
|
};
|
|
|
|
struct node_cmp
|
|
|
|
{
|
|
|
|
bool operator() ( const node * lhs,const node* rhs) const
|
|
|
|
{
|
|
|
|
return lhs->reduce_cost < rhs->reduce_cost;
|
|
|
|
}
|
|
|
|
};
|
2009-04-28 09:29:48 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
std::deque<node*> reducible_[InsertPolicy::MAX_LEVELS];
|
|
|
|
unsigned max_colors_;
|
|
|
|
unsigned colors_;
|
|
|
|
unsigned offset_;
|
|
|
|
unsigned leaf_level_;
|
2009-04-28 09:29:48 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
public:
|
|
|
|
explicit octree(unsigned max_colors=256)
|
|
|
|
: max_colors_(max_colors),
|
|
|
|
colors_(0),
|
|
|
|
offset_(0),
|
|
|
|
leaf_level_(InsertPolicy::MAX_LEVELS),
|
|
|
|
root_(new node())
|
|
|
|
{}
|
2010-03-24 18:36:39 +01:00
|
|
|
|
2012-11-04 12:13:17 +01:00
|
|
|
~octree()
|
|
|
|
{
|
|
|
|
delete root_;
|
|
|
|
}
|
2010-01-05 16:54:25 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
unsigned colors()
|
|
|
|
{
|
|
|
|
return colors_;
|
|
|
|
}
|
2010-01-05 16:54:25 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void setMaxColors(unsigned max_colors)
|
|
|
|
{
|
|
|
|
max_colors_ = max_colors;
|
|
|
|
}
|
2010-01-05 16:54:25 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void setOffset(unsigned offset)
|
|
|
|
{
|
|
|
|
offset_ = offset;
|
|
|
|
}
|
2009-04-28 09:29:48 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
unsigned getOffset()
|
|
|
|
{
|
|
|
|
return offset_;
|
|
|
|
}
|
2009-04-28 09:29:48 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void insert(T const& data)
|
|
|
|
{
|
|
|
|
unsigned level = 0;
|
|
|
|
node * cur_node = root_;
|
2012-11-04 12:13:17 +01:00
|
|
|
while (true)
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
cur_node->count_cum++;
|
|
|
|
cur_node->reds += data.r;
|
|
|
|
cur_node->greens += data.g;
|
|
|
|
cur_node->blues += data.b;
|
2009-04-28 09:29:48 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
if ( cur_node->count > 0 || level == leaf_level_)
|
2007-12-10 20:59:17 +01:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
cur_node->count += 1;
|
|
|
|
if (cur_node->count == 1) ++colors_;
|
|
|
|
//if (colors_ >= max_colors_ - 1)
|
|
|
|
//reduce();
|
|
|
|
break;
|
2009-04-28 09:29:48 +02:00
|
|
|
}
|
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
unsigned idx = InsertPolicy::index_from_level(level,data);
|
2012-11-04 12:13:17 +01:00
|
|
|
if (cur_node->children_[idx] == 0)
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
cur_node->children_count++;
|
|
|
|
cur_node->children_[idx] = new node();
|
|
|
|
if (level < leaf_level_-1)
|
|
|
|
{
|
|
|
|
reducible_[level+1].push_back(cur_node->children_[idx]);
|
|
|
|
}
|
2009-04-28 09:29:48 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
cur_node = cur_node->children_[idx];
|
|
|
|
++level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-09 04:07:11 +01:00
|
|
|
int quantize(unsigned val) const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
unsigned level = 0;
|
2013-01-09 04:07:11 +01:00
|
|
|
rgb c(val);
|
2010-06-02 13:03:30 +02:00
|
|
|
node * cur_node = root_;
|
|
|
|
while (cur_node)
|
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
if (cur_node->children_count == 0)
|
2012-11-04 12:13:17 +01:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
return cur_node->index + offset_;
|
2012-11-04 12:13:17 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
unsigned idx = InsertPolicy::index_from_level(level,c);
|
|
|
|
cur_node = cur_node->children_[idx];
|
|
|
|
++level;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void create_palette(std::vector<rgb> & palette)
|
|
|
|
{
|
|
|
|
reduce();
|
|
|
|
palette.reserve(colors_);
|
|
|
|
create_palette(palette, root_);
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void computeCost(node *r)
|
|
|
|
{
|
|
|
|
r->reduce_cost = 0;
|
|
|
|
if (r->children_count==0)
|
2012-11-04 12:13:17 +01:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
return;
|
2012-11-04 12:13:17 +01:00
|
|
|
}
|
2010-03-06 12:13:44 +01:00
|
|
|
|
2012-02-25 03:58:34 +01:00
|
|
|
double mean_r = static_cast<double>(r->reds / r->count_cum);
|
|
|
|
double mean_g = static_cast<double>(r->greens / r->count_cum);
|
|
|
|
double mean_b = static_cast<double>(r->blues / r->count_cum);
|
2012-11-04 12:13:17 +01:00
|
|
|
for (unsigned idx=0; idx < 8; ++idx)
|
|
|
|
{
|
|
|
|
if (r->children_[idx] != 0)
|
|
|
|
{
|
|
|
|
double dr,dg,db;
|
|
|
|
computeCost(r->children_[idx]);
|
|
|
|
dr = r->children_[idx]->reds / r->children_[idx]->count_cum - mean_r;
|
|
|
|
dg = r->children_[idx]->greens / r->children_[idx]->count_cum - mean_g;
|
|
|
|
db = r->children_[idx]->blues / r->children_[idx]->count_cum - mean_b;
|
|
|
|
r->reduce_cost += r->children_[idx]->reduce_cost;
|
|
|
|
r->reduce_cost += (dr*dr + dg*dg + db*db) * r->children_[idx]->count_cum;
|
|
|
|
}
|
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2009-04-28 09:29:48 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void reduce()
|
|
|
|
{
|
|
|
|
computeCost(root_);
|
|
|
|
reducible_[0].push_back(root_);
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
// sort reducible by reduce_cost
|
|
|
|
for (unsigned i=0;i<InsertPolicy::MAX_LEVELS;++i)
|
|
|
|
{
|
|
|
|
std::sort(reducible_[i].begin(), reducible_[i].end(),node_cmp());
|
|
|
|
}
|
2012-11-04 12:13:17 +01:00
|
|
|
while (colors_ > max_colors_ && colors_ > 1)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
while (leaf_level_ >0 && reducible_[leaf_level_-1].size() == 0)
|
2007-12-10 20:59:17 +01:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
--leaf_level_;
|
2007-12-10 20:59:17 +01:00
|
|
|
}
|
2009-04-28 09:29:48 +02:00
|
|
|
|
2012-11-04 12:13:17 +01:00
|
|
|
if (leaf_level_ <= 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2009-04-28 09:29:48 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
// select best of all reducible:
|
|
|
|
unsigned red_idx = leaf_level_-1;
|
2012-12-13 00:59:27 +01:00
|
|
|
unsigned bestv = static_cast<unsigned>((*reducible_[red_idx].begin())->reduce_cost);
|
2012-11-04 12:13:17 +01:00
|
|
|
for(unsigned i=red_idx; i>=InsertPolicy::MIN_LEVELS; i--)
|
|
|
|
{
|
|
|
|
if (!reducible_[i].empty())
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
node *nd = *reducible_[i].begin();
|
|
|
|
unsigned gch = 0;
|
2012-11-04 12:13:17 +01:00
|
|
|
for(unsigned idx=0; idx<8; idx++)
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
if (nd->children_[idx])
|
|
|
|
gch += nd->children_[idx]->children_count;
|
|
|
|
}
|
2012-11-04 12:13:17 +01:00
|
|
|
if (gch==0 && nd->reduce_cost < bestv)
|
|
|
|
{
|
2012-02-25 03:58:34 +01:00
|
|
|
bestv = static_cast<unsigned>(nd->reduce_cost);
|
2010-06-02 13:03:30 +02:00
|
|
|
red_idx = i;
|
|
|
|
}
|
|
|
|
}
|
2012-11-04 12:13:17 +01:00
|
|
|
}
|
2009-03-29 13:05:20 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
typename std::deque<node*>::iterator pos = reducible_[red_idx].begin();
|
|
|
|
node * cur_node = *pos;
|
|
|
|
unsigned num_children = 0;
|
|
|
|
for (unsigned idx=0; idx < 8; ++idx)
|
2007-12-10 20:59:17 +01:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
if (cur_node->children_[idx] != 0)
|
|
|
|
{
|
|
|
|
cur_node->children_count--;
|
|
|
|
++num_children;
|
|
|
|
cur_node->count += cur_node->children_[idx]->count;
|
|
|
|
//todo: case of nonleaf children, if someday sorting by reduce_cost doesn't handle it
|
|
|
|
delete cur_node->children_[idx], cur_node->children_[idx]=0;
|
|
|
|
}
|
2007-12-10 20:59:17 +01:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
reducible_[red_idx].erase(pos);
|
2012-02-02 02:53:35 +01:00
|
|
|
if (num_children > 0 )
|
2007-12-10 20:59:17 +01:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
colors_ -= (num_children - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void create_palette(std::vector<rgb> & palette, node * itr) const
|
|
|
|
{
|
|
|
|
if (itr->count != 0)
|
|
|
|
{
|
|
|
|
unsigned count = itr->count;
|
|
|
|
palette.push_back(rgb(byte(itr->reds/float(count)),
|
|
|
|
byte(itr->greens/float(count)),
|
|
|
|
byte(itr->blues/float(count))));
|
2013-09-16 08:08:04 +02:00
|
|
|
itr->index = static_cast<unsigned>(palette.size()) - 1;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
for (unsigned i=0; i < 8 ;++i)
|
|
|
|
{
|
2012-02-02 02:53:35 +01:00
|
|
|
if (itr->children_[i] != 0)
|
2012-11-04 12:13:17 +01:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
create_palette(palette, itr->children_[i]);
|
2012-11-04 12:13:17 +01:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
private:
|
|
|
|
node * root_;
|
2010-06-02 13:03:30 +02:00
|
|
|
};
|
2011-10-23 16:09:47 +02:00
|
|
|
|
2007-12-10 20:59:17 +01:00
|
|
|
} // namespace mapnik
|
|
|
|
|
2011-10-23 16:09:47 +02:00
|
|
|
#endif // MAPNIK_OCTREE_HPP
|