Skip to content

Commit

Permalink
Merge pull request The-OpenROAD-Project#5575 from gadfort/odb-oct-bloat
Browse files Browse the repository at this point in the history
odb: add boost to Oct and Polygon
  • Loading branch information
maliberty authored Aug 15, 2024
2 parents 254a553 + 4a90f23 commit c26260c
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 27 deletions.
30 changes: 21 additions & 9 deletions src/odb/include/odb/geom.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace odb {

class dbIStream;
class dbOStream;
class Rect;

class Point
{
Expand Down Expand Up @@ -176,6 +177,9 @@ class Oct
int yMax() const;
std::vector<Point> getPoints() const;

Oct bloat(int margin) const;
Rect getEnclosingRect() const;

friend dbIStream& operator>>(dbIStream& stream, Oct& o);
friend dbOStream& operator<<(dbOStream& stream, const Oct& o);

Expand Down Expand Up @@ -335,9 +339,12 @@ class Polygon
bool operator<=(const Polygon& p) const { return !(*this > p); }
bool operator>=(const Polygon& p) const { return !(*this < p); }

Rect getRect() const;
int dx() const { return getRect().dx(); }
int dy() const { return getRect().dy(); }
Rect getEnclosingRect() const;
int dx() const { return getEnclosingRect().dx(); }
int dy() const { return getEnclosingRect().dy(); }

// returns a corrected Polygon with a closed form and counter-clockwise points
Polygon bloat(int margin) const;

friend dbIStream& operator>>(dbIStream& stream, Polygon& p);
friend dbOStream& operator<<(dbOStream& stream, const Polygon& p);
Expand Down Expand Up @@ -878,6 +885,16 @@ inline std::vector<Point> Oct::getPoints() const
return points;
}

inline Oct Oct::bloat(int margin) const
{
return Oct(center_low_, center_high_, 2 * (A_ + margin));
}

inline Rect Oct::getEnclosingRect() const
{
return Rect(xMin(), yMin(), xMax(), yMax());
}

inline Polygon::Polygon(const std::vector<Point>& points)
{
setPoints(points);
Expand All @@ -898,12 +915,7 @@ inline std::vector<Point> Polygon::getPoints() const
return points_;
}

inline void Polygon::setPoints(const std::vector<Point>& points)
{
points_ = points;
}

inline Rect Polygon::getRect() const
inline Rect Polygon::getEnclosingRect() const
{
Rect rect;
rect.mergeInit();
Expand Down
116 changes: 116 additions & 0 deletions src/odb/include/odb/geom_boost.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/geometries/register/ring.hpp>
#include <boost/polygon/polygon.hpp>

#include "odb/geom.h"
Expand Down Expand Up @@ -95,6 +96,10 @@ BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET(odb::Point,
setX,
setY);

// Register odb's Point vector as ring.

BOOST_GEOMETRY_REGISTER_RING(std::vector<odb::Point>);

// Make odb's Rect work with boost polgyon

template <>
Expand Down Expand Up @@ -207,4 +212,115 @@ struct indexed_access<odb::Rect, max_corner, Dimension>
}
};

//
// Make odb's Oct work with boost geometry.
//

template <>
struct tag<odb::Oct>
{
using type = polygon_tag;
};

template <>
struct ring_mutable_type<odb::Oct>
{
using type = std::vector<odb::Point>;
};

template <>
struct ring_const_type<odb::Oct>
{
using type = const std::vector<odb::Point>;
};

template <>
struct interior_const_type<odb::Oct>
{
using type = const std::vector<std::vector<odb::Point>>;
};

template <>
struct interior_mutable_type<odb::Oct>
{
using type = std::vector<std::vector<odb::Point>>;
};

template <>
struct exterior_ring<odb::Oct>
{
static std::vector<odb::Point> get(odb::Oct& o) { return o.getPoints(); }
static const std::vector<odb::Point> get(const odb::Oct& o)
{
return o.getPoints();
}
};

template <>
struct interior_rings<odb::Oct>
{
static std::vector<std::vector<odb::Point>> get(odb::Oct& o) { return {}; }
static const std::vector<std::vector<odb::Point>> get(const odb::Oct& o)
{
return {};
}
};

//
// Make odb's Polygon work with boost geometry.
//

template <>
struct tag<odb::Polygon>
{
using type = polygon_tag;
};

template <>
struct ring_mutable_type<odb::Polygon>
{
using type = std::vector<odb::Point>;
};

template <>
struct ring_const_type<odb::Polygon>
{
using type = const std::vector<odb::Point>;
};

template <>
struct interior_const_type<odb::Polygon>
{
using type = const std::vector<std::vector<odb::Point>>;
};

template <>
struct interior_mutable_type<odb::Polygon>
{
using type = std::vector<std::vector<odb::Point>>;
};

template <>
struct exterior_ring<odb::Polygon>
{
static std::vector<odb::Point> get(odb::Polygon& p) { return p.getPoints(); }
static const std::vector<odb::Point> get(const odb::Polygon& p)
{
return p.getPoints();
}
};

template <>
struct interior_rings<odb::Polygon>
{
static std::vector<std::vector<odb::Point>> get(odb::Polygon& p)
{
return {};
}
static const std::vector<std::vector<odb::Point>> get(const odb::Polygon& p)
{
return {};
}
};

} // namespace boost::geometry::traits
1 change: 1 addition & 0 deletions src/odb/src/db/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ add_library(db
dbNameCache.cpp
dbProperty.cpp
dbPropertyItr.cpp
geom.cpp
tmg_conn.cpp
tmg_conn_g.cpp
tmg_conn_s.cpp
Expand Down
16 changes: 1 addition & 15 deletions src/odb/src/db/dbPolygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,22 +261,8 @@ Polygon _dbPolygon::checkPolygon(std::vector<Point> polygon)

void _dbPolygon::decompose()
{
std::vector<Point> polygon = polygon_.getPoints();

if (polygon[0] == polygon[polygon.size() - 1]) {
polygon.pop_back();
}

if (polygon.size() < 4) {
return;
}

if (!polygon_is_clockwise(polygon)) {
std::reverse(polygon.begin(), polygon.end());
}

std::vector<Rect> rects;
decompose_polygon(polygon, rects);
decompose_polygon(polygon_.getPoints(), rects);

std::vector<Rect>::iterator itr;

Expand Down
78 changes: 78 additions & 0 deletions src/odb/src/db/geom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
///////////////////////////////////////////////////////////////////////////////
// BSD 3-Clause License
//
// Copyright (c) 2024, The Regents of the University of California
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#include "odb/geom.h"

#include "odb/geom_boost.h"

namespace odb {

void Polygon::setPoints(const std::vector<Point>& points)
{
points_ = points;
boost::geometry::correct(points_);
}

Polygon Polygon::bloat(int margin) const
{
// convert to native boost types to avoid needed a mutable access
// to odb::Polygon
using BoostPolygon = boost::polygon::polygon_data<int>;
using BoostPolygonSet = boost::polygon::polygon_set_data<int>;
using boost::polygon::operators::operator+=;
using boost::polygon::operators::operator+;

// convert to boost polygon
const BoostPolygon polygon_in(points_.begin(), points_.end());

// add to polygon set
BoostPolygonSet poly_in_set;
poly_in_set += polygon_in;

// bloat polygon set
const BoostPolygonSet poly_out_set = poly_in_set + margin;

// extract new polygon
std::vector<BoostPolygon> output_polygons;
poly_out_set.get(output_polygons);
const BoostPolygon& polygon_out = output_polygons[0];

std::vector<odb::Point> new_coord;
new_coord.reserve(polygon_out.coords_.size());
for (const auto& pt : polygon_out.coords_) {
new_coord.emplace_back(pt.x(), pt.y());
}

return Polygon(new_coord);
}

} // namespace odb
6 changes: 3 additions & 3 deletions src/odb/test/write_lef_polygon.lefok
Original file line number Diff line number Diff line change
Expand Up @@ -2910,14 +2910,14 @@ MACRO BUMP
USE SIGNAL ;
PORT
LAYER metal5 ;
POLYGON 14.5 5.42 5.42 14.5 -5.42 14.5 -14.5 5.42 -14.5 -5.42 -5.42 -14.5 5.42 -14.5 14.5 -5.42 14.5 5.42 ;
POLYGON 14.5 5.42 14.5 -5.42 5.42 -14.5 -5.42 -14.5 -14.5 -5.42 -14.5 5.42 -5.42 14.5 5.42 14.5 14.5 5.42 ;
LAYER metal6 ;
POLYGON 14.5 5.42 5.42 14.5 -5.42 14.5 -14.5 5.42 -14.5 -5.42 -5.42 -14.5 5.42 -14.5 14.5 -5.42 ;
POLYGON 14.5 5.42 14.5 -5.42 5.42 -14.5 -5.42 -14.5 -14.5 -5.42 -14.5 5.42 -5.42 14.5 5.42 14.5 14.5 5.42 ;
END
END PAD
OBS
LAYER via5 ;
POLYGON 14.5 5.42 5.42 14.5 -5.42 14.5 -14.5 5.42 -14.5 -5.42 -5.42 -14.5 5.42 -14.5 14.5 -5.42 ;
POLYGON 14.5 5.42 14.5 -5.42 5.42 -14.5 -5.42 -14.5 -14.5 -5.42 -14.5 5.42 -5.42 14.5 5.42 14.5 14.5 5.42 ;
END
END BUMP
END LIBRARY

0 comments on commit c26260c

Please sign in to comment.