Skip to content

Commit

Permalink
Refactor envelop methods of bounding box to enable chaining and re-fa…
Browse files Browse the repository at this point in the history
…ctor visualizer code to use it
  • Loading branch information
artem-ogre committed Feb 18, 2025
1 parent ec03b30 commit 28131dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
14 changes: 8 additions & 6 deletions CDT/include/CDTUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,26 +208,27 @@ struct CDT_EXPORT Box2d
{}

/// Envelop box around a point
void envelopPoint(const V2d<T>& p)
Box2d<T>& envelopPoint(const V2d<T>& p)
{
envelopPoint(p.x, p.y);
return envelopPoint(p.x, p.y);
}

/// Envelop box around a point with given coordinates
void envelopPoint(const T x, const T y)
Box2d<T>& envelopPoint(const T x, const T y)
{
min.x = std::min(x, min.x);
max.x = std::max(x, max.x);
min.y = std::min(y, min.y);
max.y = std::max(y, max.y);
return *this;
}

/// Envelop box around a collection of custom points
template <
typename TVertexIter,
typename TGetVertexCoordX,
typename TGetVertexCoordY>
void envelopPoints(
Box2d<T>& envelopPoints(
TVertexIter first,
TVertexIter last,
TGetVertexCoordX getX,
Expand All @@ -237,12 +238,13 @@ struct CDT_EXPORT Box2d
{
envelopPoint(getX(*first), getY(*first));
}
return *this;
}

/// Envelop box around a collection of points
void envelopPoints(const std::vector<V2d<T> >& vertices)
Box2d<T>& envelopPoints(const std::vector<V2d<T> >& vertices)
{
envelopPoints(
return envelopPoints(
vertices.begin(), vertices.end(), getX_V2d<T>, getY_V2d<T>);
}
};
Expand Down
12 changes: 6 additions & 6 deletions visualizer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public slots:
fout << m_cdt.vertices.size() << ' ' << m_cdt.triangles.size()
<< " 0\n";
// Write vertices
const Box2d box = envelopBox(m_points);
const Box2d box = Box2d().envelopPoints(m_points);
const CoordType stZ =
-std::fmax(box.max.x - box.min.x, box.max.y - box.min.y);
std::size_t counter = 0;
Expand Down Expand Up @@ -253,7 +253,7 @@ public slots:
{
CoordType x1, y1;
inStream >> x1 >> y1;
m_points.push_back(V2d::make(x1, y1));
m_points.push_back(V2d(x1, y1));
}
m_edges.clear();
for(std::size_t i = 0; i < nEdges; ++i)
Expand Down Expand Up @@ -381,9 +381,9 @@ public slots:
}
double calculateScale(const int w, const int h) const
{
const V2d sceneSize = V2d::make(
m_sceneBox.max.x - m_sceneBox.min.x,
m_sceneBox.max.y - m_sceneBox.min.y);
const V2d sceneSize =
V2d(m_sceneBox.max.x - m_sceneBox.min.x,
m_sceneBox.max.y - m_sceneBox.min.y);
const double sceneRatio = sceneSize.x / sceneSize.y;
const double screenRatio = static_cast<double>(w) / h;
double scale =
Expand All @@ -392,7 +392,7 @@ public slots:
}
void initTransform()
{
m_sceneBox = envelopBox(m_points);
m_sceneBox = Box2d().envelopPoints(m_points);
QPointF sceneCenter(
(m_sceneBox.min.x + m_sceneBox.max.x) / CoordType(2),
(m_sceneBox.min.y + m_sceneBox.max.y) / CoordType(2));
Expand Down

0 comments on commit 28131dc

Please sign in to comment.