Skip to content

Commit

Permalink
using more unique ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
cvvergara committed Aug 19, 2018
1 parent 5c15183 commit f6c12cf
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 55 deletions.
9 changes: 4 additions & 5 deletions include/geos/operation/polygonize/EdgeRing.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,12 @@ namespace polygonize { // geos::operation::polygonize
* a ring of a polygon. The ring may be either an outer shell or a hole.
*/
class GEOS_DLL EdgeRing {
#if 0
typedef DirectedEdge* DirectedEdgePtr;
#else
typedef std::shared_ptr<DirectedEdge> DirectedEdgePtr;
#endif
typedef std::vector<DirectedEdgePtr> DirectedEdges;
typedef std::vector<geom::Geometry*> GeomVect;

public:
using EdgeRingPtr = std::unique_ptr<EdgeRing>;
/**
* \brief
* Find the innermost enclosing shell EdgeRing
Expand All @@ -88,12 +85,14 @@ class GEOS_DLL EdgeRing {
*/

EdgeRing* findEdgeRingContaining(
const std::vector<EdgeRing*> shellList);
const std::vector<EdgeRingPtr> &shellList);

#if 0
//[[deprecated]]
static EdgeRing* findEdgeRingContaining(
EdgeRing *testEr,
std::vector<EdgeRing*> *shellList);
#endif

/**
* \brief
Expand Down
10 changes: 7 additions & 3 deletions include/geos/operation/polygonize/PolygonizeGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ namespace polygonize {
// Forward declarations
class EdgeRing;


/** \brief
* Represents a planar graph of edges that can be used to compute a
* polygonization, and implements the algorithms to compute the
Expand All @@ -69,6 +70,8 @@ class EdgeRing;
*/
class GEOS_DLL PolygonizeGraph: public planargraph::PlanarGraph {
public:
using EdgeRingPtr = std::unique_ptr<EdgeRing>;

bool empty() const {return m_nodeMap.empty();}

/**
Expand All @@ -91,6 +94,7 @@ class GEOS_DLL PolygonizeGraph: public planargraph::PlanarGraph {
*/
void addEdge(const LineString *line);

#if 0
/**
* \brief
* Computes the EdgeRings formed by the edges in this graph.
Expand All @@ -105,14 +109,15 @@ class GEOS_DLL PolygonizeGraph: public planargraph::PlanarGraph {
*/
// [[deprecated]]
void getEdgeRings(std::vector<EdgeRing*>& edgeRingList);
#endif

/**
* \brief
* Computes the EdgeRings formed by the edges in this graph.
*
* @return edgeRingList: the EdgeRings found by the polygonization process.
*/
std::vector<EdgeRing*> getEdgeRings();
std::vector<EdgeRingPtr> getEdgeRings();

/**
* \brief
Expand Down Expand Up @@ -209,7 +214,7 @@ class GEOS_DLL PolygonizeGraph: public planargraph::PlanarGraph {
findDirEdgesInRing(DirectedEdgePtr startDE) const;

/* has side effect of saving the Edge Ring found */
EdgeRing* findEdgeRing(DirectedEdgePtr startDE) const;
EdgeRingPtr findEdgeRing(DirectedEdgePtr startDE) const;

/*
* Data members
Expand All @@ -220,7 +225,6 @@ class GEOS_DLL PolygonizeGraph: public planargraph::PlanarGraph {
/*
* These are for memory management
*/
mutable std::vector<EdgeRing *> m_newEdgeRings;
std::vector<geom::CoordinateSequence *> m_newCoords;
};

Expand Down
58 changes: 47 additions & 11 deletions include/geos/operation/polygonize/Polygonizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ class GEOS_DLL Polygonizer {


private:
using EdgeRingPtr = std::unique_ptr<EdgeRing>;

/**
* Add every linear element in a geometry into the polygonizer graph.
*/
Expand All @@ -209,16 +211,50 @@ class GEOS_DLL Polygonizer {
void clear();


std::vector<EdgeRing*> findValidRings(const std::vector<EdgeRing*>& edgeRingList) const;

void findShellsAndHoles(const std::vector<EdgeRing*>& edgeRingList);

static void assignHolesToShells(const std::vector<EdgeRing*>& holeList,
const std::vector<EdgeRing*>& shellList);

/** @brief
* separates into valid & invalid rings
*
* invalid rings are saved as LineStrings (they are not rings)
*
* post condition: The edgeRingList does not own the rings
*
* @return the valid rings
*/
std::vector<EdgeRingPtr> findValidRings(std::vector<EdgeRingPtr>& edgeRingList) const;


/** @brief
*
* Separates the rings as: Holes or Shells
*
* The edge rings are owned by
* * holeList
* * shellList
*
* post condition: The edgeRingList does not own the rings (size == 0)
*/
void findShellsAndHoles(std::vector<EdgeRingPtr>& edgeRingList);

/** @brief
*
* precondition: a holes must belong to a shell
* (use NDEBUG to check this assertion)
*
* * Holes are assigned to shells they belong to
*
* post condition:
* * The holeList does not own the hole rings (size == 0)
* * for all holes: A shell owns the hole as Linestring*
*/
static void assignHolesToShells(
std::vector<EdgeRingPtr>& holeList,
std::vector<EdgeRingPtr>& shellList);

#if 0
static void assignHoleToShell(
EdgeRing &holeER,
const std::vector<EdgeRing*>& shellList);
EdgeRingPtr holeER,
const std::vector<EdgeRingPtr>& shellList);
#endif

/*
* Data
Expand All @@ -232,8 +268,8 @@ class GEOS_DLL Polygonizer {
std::vector<const geom::LineString*> cutEdges;
mutable std::vector<geom::LineString*> invalidRingLines;

std::vector<EdgeRing*> holeList;
std::vector<EdgeRing*> shellList;
std::vector<EdgeRingPtr> holeList;
std::vector<EdgeRingPtr> shellList;
std::vector<geom::Polygon*> polyList;
};

Expand Down
8 changes: 5 additions & 3 deletions src/operation/polygonize/EdgeRing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace polygonize { // geos.operation.polygonize
/*public*/
EdgeRing *
EdgeRing::findEdgeRingContaining(
const vector<EdgeRing*> shellList)
const vector<EdgeRingPtr> &shellList)
{
if ( !getRingInternal()) return nullptr;

Expand All @@ -60,7 +60,7 @@ EdgeRing::findEdgeRingContaining(
EdgeRing *minShell = nullptr;
const Envelope *minEnv = nullptr;

for(auto tryShell : shellList) {
for(auto &tryShell : shellList) {
auto tryRing = tryShell->getRingInternal();
const Envelope *tryEnv = tryRing->getEnvelopeInternal();

Expand Down Expand Up @@ -90,20 +90,22 @@ EdgeRing::findEdgeRingContaining(
// than the current minimum ring
if (isContained) {
if (!minShell || minEnv->contains(tryEnv)) {
minShell = tryShell;
minShell = tryShell.get();
}
}
}
return minShell;
}


#if 0
/*public static deprecated*/
EdgeRing *
EdgeRing::findEdgeRingContaining(EdgeRing *testEr,
vector<EdgeRing*> *shellList) {
return testEr->findEdgeRingContaining(*shellList);
}
#endif

/*public static*/
const Coordinate
Expand Down
24 changes: 12 additions & 12 deletions src/operation/polygonize/PolygonizeGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ PolygonizeGraph::PolygonizeGraph(const GeometryFactory *newFactory)
* Destroy a PolygonizeGraph
*/
PolygonizeGraph::~PolygonizeGraph() {
for (auto e : m_newEdgeRings) delete e;
//for (auto e : m_newEdgeRings) delete e;
for (auto c : m_newCoords) delete c;
}

Expand Down Expand Up @@ -144,9 +144,9 @@ PolygonizeGraph::findIntersectionNodes(
}

/* public */
std::vector<EdgeRing*>
std::vector<PolygonizeGraph::EdgeRingPtr>
PolygonizeGraph::getEdgeRings() {
std::vector<EdgeRing*> edgeRingList;
std::vector<EdgeRingPtr> edgeRingList;
// maybe could optimize this, since most of these pointers should
// be set correctly already
// by deleteCutEdges()
Expand All @@ -165,18 +165,18 @@ PolygonizeGraph::getEdgeRings() {
if (de->isMarked()) continue;
if (safe_cast<PolygonizeDirectedEdge*>(de)->isInRing()) continue;

auto er = findEdgeRing(de);
edgeRingList.push_back(er);
edgeRingList.emplace_back(findEdgeRing(de));
}
return edgeRingList;
}

#if 0
/* public [[deprecated]] */
void
PolygonizeGraph::getEdgeRings(std::vector<EdgeRing*>& edgeRingList) {
edgeRingList = getEdgeRings();
}

#endif

PolygonizeGraph::DirectedEdges
PolygonizeGraph::findLabeledEdgeRings(
Expand Down Expand Up @@ -327,24 +327,24 @@ PolygonizeGraph::findDirEdgesInRing(DirectedEdgePtr startDE) const {
return edges;
}

EdgeRing *
PolygonizeGraph::EdgeRingPtr
PolygonizeGraph::findEdgeRing(DirectedEdgePtr startDE) const {
auto de = startDE;
EdgeRing *er = new EdgeRing(*m_factory);
// Now, when will we delete those EdgeRings ?
m_newEdgeRings.push_back(er);

EdgeRingPtr er( new EdgeRing(*m_factory) );

do {
er->add(de);

auto e = safe_cast<PolygonizeDirectedEdge *>(de);
e->setRing(er);
e->setRing(er.get());

de = de->getNext();
assert(de); // found NULL DE in ring
assert(de == startDE || !safe_cast<PolygonizeDirectedEdge *>(de)->isInRing()); // found DE already in ring
} while (de != startDE);

return er;
return std::move(er);
}

/* public */
Expand Down
63 changes: 42 additions & 21 deletions src/operation/polygonize/Polygonizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,62 +280,83 @@ Polygonizer::polygonize()
}

/* private */
vector<EdgeRing*>
Polygonizer::findValidRings(const vector<EdgeRing*>& edgeRingList) const
vector<Polygonizer::EdgeRingPtr>
Polygonizer::findValidRings(vector<EdgeRingPtr>& edgeRingList) const
{
vector<EdgeRing*> validEdgeRingList;
for (const auto &er : edgeRingList)
vector<EdgeRingPtr> validEdgeRingList;
for (auto &er : edgeRingList)
{
if (er->isValid())
{
validEdgeRingList.push_back(er);
validEdgeRingList.push_back(std::move(er));
}
else
{
invalidRingLines.push_back(er->getLineString());
/* TODO check how the linestirngs are build */
invalidRingLines.push_back(er.release()->getLineString());
}
GEOS_CHECK_FOR_INTERRUPTS();
}
edgeRingList.clear();
return validEdgeRingList;
}

/* private */
void
Polygonizer::findShellsAndHoles(const vector<EdgeRing*>& edgeRingList)
Polygonizer::findShellsAndHoles(vector<EdgeRingPtr>& edgeRingList)
{
holeList.clear();
shellList.clear();
for (const auto &er : edgeRingList) {
for (auto &er : edgeRingList) {
if (er->isHole())
holeList.push_back(er);
holeList.push_back(std::move(er));
else
shellList.push_back(er);
shellList.push_back(std::move(er));

GEOS_CHECK_FOR_INTERRUPTS();
}
edgeRingList.clear();
}

/* private */
void
Polygonizer::assignHolesToShells(const vector<EdgeRing*>& holeList, const vector<EdgeRing*>& shellList)
Polygonizer::assignHolesToShells(vector<EdgeRingPtr>& holeList, vector<EdgeRingPtr>& shellList)
{
for (const auto &holeER : holeList) {
assignHoleToShell(*holeER, shellList);
GEOS_CHECK_FOR_INTERRUPTS();
}
#ifndef NDEBUG
size_t count(0);
#endif
for (auto &holeER : holeList) {
auto shell = holeER->findEdgeRingContaining(shellList);

if (shell) {
shell->addHole(holeER.release()->getRingOwnership());
}
GEOS_CHECK_FOR_INTERRUPTS();
#ifndef NDEBUG
++count;
#endif
}

#ifndef NDEBUG
assert(holeList.size() == count);
#endif
holeList.clear();
}

#if 0
/* private */
void
Polygonizer::assignHoleToShell(EdgeRing &holeER,
const vector<EdgeRing*>& shellList)
Polygonizer::assignHoleToShell(
EdgeRingPtr holeER,
const vector<EdgeRingPtr>& shellList)
{
auto shell = holeER.findEdgeRingContaining(shellList);
auto shell = holeER->findEdgeRingContaining(shellList);

if (shell)
shell->addHole(holeER.getRingOwnership());
if (shell) {
shell->addHole(holeER.release()->getRingOwnership());
}
}

#endif

} // namespace geos.operation.polygonize
} // namespace geos.operation
Expand Down

0 comments on commit f6c12cf

Please sign in to comment.