Commit f959419a authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Updating marching squares to support reducing polygons/points returned.

parent c3df30f3
Pipeline #16256 passed with stages
in 9 minutes and 11 seconds
......@@ -95,7 +95,8 @@ class MarchingSquares
* @return list of polygons for each isovalue [isovalue i][polygon i][point i]
*/
std::vector<std::vector<std::vector<std::pair<float, float>>>> march(
const std::vector<data_type>& isovalues);
const std::vector<data_type>& isovalues, size_t max_contour_polygons = 10,
size_t max_polygon_points = 5000);
void dump_component_map(std::ostream& os) const;
......
#include <queue>
#include <set>
#include <vector>
......@@ -314,7 +315,9 @@ bool MarchingSquares<data_type>::accepts(size_t r, size_t c, short label) const
}
template <typename data_type>
std::vector<std::vector<std::vector<std::pair<float, float>>>>
MarchingSquares<data_type>::march(const std::vector<data_type>& isovalues)
MarchingSquares<data_type>::march(const std::vector<data_type>& isovalues,
size_t max_contour_polygons,
size_t max_polygon_points)
{
std::pair<size_t, size_t> start, prev;
......@@ -335,8 +338,6 @@ MarchingSquares<data_type>::march(const std::vector<data_type>& isovalues)
// Initialize bit field
std::vector<size_t> label_counts(isovalues_size + 1, 0);
size_t data_size = mData.size();
// data_type min_value = 9999999999;
// data_type max_value = -9999999999;
for (size_t p_i = 0; p_i < data_size; ++p_i)
{
// select data as 1 if greater than isovalue
......@@ -345,26 +346,24 @@ MarchingSquares<data_type>::march(const std::vector<data_type>& isovalues)
{
if (value >= ordered_isovalues[isoi])
{
// min_value = std::min(min_value, value);
// max_value = std::max(max_value, value);
//
// save the category (aka there are only number of isovalue categories)
// specifically 1-N, 0 is background
mBit[p_i] = isovalues_size - isoi;
// label_counts[mBit[p_i]] = label_counts[mBit[p_i]] + 1;
// radix_tagged_line("assigning " << p_i << " " << value << " label("
// << mBit[p_i] << ")");
break;
}
}
}
// radix_tagged_line("Max value (" << max_value << ")");
// radix_tagged_line("Min value (" << min_value << ")");
// for (size_t i = 0; i < label_counts.size(); ++i)
//{
// radix_tagged_line("Label (" << i << ") count (" << label_counts[i] <<
// ")");
//}
typedef std::pair<float, float> Point;
typedef std::vector<Point> Polygon;
struct PolyComparator
{
bool operator()(const Polygon& a, const Polygon& b)
{
return a.size() < b.size();
}
};
for (size_t isoi = 0; isoi < isovalues_size; ++isoi)
{
......@@ -376,14 +375,15 @@ MarchingSquares<data_type>::march(const std::vector<data_type>& isovalues)
//
// Get a starting point first, because if there isn't a place to start
// then we don't care about going any further for this isovalue
size_t polygoni = 0;
// reset previous for each contour search
prev = {0, 0};
std::priority_queue<Polygon, std::vector<Polygon>, PolyComparator> pqueue;
while (starting_point(label, start, prev))
{
prev = start;
// we have a new polygon
contours[contouri].push_back(std::vector<std::pair<float, float>>());
std::vector<std::pair<float, float>> polygon;
radix_tagged_line("Starting point("
<< ordered_isovalues[isoi] << ") label (" << label
......@@ -401,7 +401,7 @@ MarchingSquares<data_type>::march(const std::vector<data_type>& isovalues)
// of rows and columns to allow for traveling the boundaries
if (column >= 0 && column <= mColumns && row >= 0 && row <= mRows)
{
contours[contouri][polygoni].push_back({row, column});
polygon.push_back({row, column});
}
switch (next_step)
......@@ -425,7 +425,8 @@ MarchingSquares<data_type>::march(const std::vector<data_type>& isovalues)
//
// Finish connecting the contour by making the last=first
contours[contouri][polygoni].push_back(start);
polygon.push_back(start);
pqueue.push(polygon);
radix_tagged_line("clearing label ("
<< mBit[mColumns * start.first + start.second]
......@@ -434,9 +435,31 @@ MarchingSquares<data_type>::march(const std::vector<data_type>& isovalues)
clear_isovalue_label(start.first, start.second,
mBit[mColumns * start.first + start.second],
ordered_isovalues);
// increment the polygon index
polygoni++;
}
//
// save the top N polygons
size_t count = std::min(max_contour_polygons, pqueue.size());
contours[contouri].resize(count);
for (size_t i = 0; i < count; ++i)
{
auto& top = pqueue.top();
auto& cpolygon = contours[contouri][i];
if (top.size() > max_polygon_points)
{
// std::cout << "Taking every other point given size(" << top.size()
// << ") exceeds " << max_polygon_points << std::endl;
// take every other point
size_t polygon_size = top.size() / 2;
for (size_t pi = 0; pi < polygon_size; ++pi)
{
cpolygon.push_back(top[pi * 2]);
}
}
else
{
cpolygon.resize(top.size());
std::copy(top.begin(), top.end(), cpolygon.begin());
}
}
}
return contours;
......@@ -467,17 +490,17 @@ void MarchingSquares<data_type>::clear_isovalue_label(
data_type value = mData[c_i];
if (label > 1) // not the last isovalue
{
radix_tagged_line("Comparing " << value
<< " >= " << ordered_isovalues[isoi]);
// radix_tagged_line("Comparing " << value
// << " >= " << ordered_isovalues[isoi]);
if (value >= ordered_isovalues[isoi])
{
//
// save the category (aka there are only number of isovalue categories)
// specifically 1-N, 0 is background
mBit[c_i] = label - 1;
radix_tagged_line("Found new isovalue (" << ordered_isovalues[isoi]
<< ") for [" << row << ","
<< col << "]");
// radix_tagged_line("Found new isovalue (" << ordered_isovalues[isoi]
// << ") for [" << row << ","
// << col << "]");
}
}
// we didn't find a lower level contour, so zero it out
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment