Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f920bf

Browse files
committed
path: Simplify extent_limits implementation a bit
By using the existing `XY` type to replace x/y pairs, and taking advantage of struct methods.
1 parent 5ba391b commit 3f920bf

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

src/_path.h

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -312,43 +312,39 @@ inline bool point_on_path(
312312

313313
struct extent_limits
314314
{
315-
double x0;
316-
double y0;
317-
double x1;
318-
double y1;
319-
double xm;
320-
double ym;
321-
};
315+
XY start;
316+
XY end;
317+
/* minpos is the minimum positive values in the data; used by log scaling. */
318+
XY minpos;
322319

323-
void reset_limits(extent_limits &e)
324-
{
325-
e.x0 = std::numeric_limits<double>::infinity();
326-
e.y0 = std::numeric_limits<double>::infinity();
327-
e.x1 = -std::numeric_limits<double>::infinity();
328-
e.y1 = -std::numeric_limits<double>::infinity();
329-
/* xm and ym are the minimum positive values in the data, used
330-
by log scaling */
331-
e.xm = std::numeric_limits<double>::infinity();
332-
e.ym = std::numeric_limits<double>::infinity();
333-
}
320+
extent_limits() : start{0,0}, end{0,0}, minpos{0,0} {
321+
reset();
322+
}
334323

335-
inline void update_limits(double x, double y, extent_limits &e)
336-
{
337-
if (x < e.x0)
338-
e.x0 = x;
339-
if (y < e.y0)
340-
e.y0 = y;
341-
if (x > e.x1)
342-
e.x1 = x;
343-
if (y > e.y1)
344-
e.y1 = y;
345-
/* xm and ym are the minimum positive values in the data, used
346-
by log scaling */
347-
if (x > 0.0 && x < e.xm)
348-
e.xm = x;
349-
if (y > 0.0 && y < e.ym)
350-
e.ym = y;
351-
}
324+
void reset()
325+
{
326+
start.x = std::numeric_limits<double>::infinity();
327+
start.y = std::numeric_limits<double>::infinity();
328+
end.x = -std::numeric_limits<double>::infinity();
329+
end.y = -std::numeric_limits<double>::infinity();
330+
minpos.x = std::numeric_limits<double>::infinity();
331+
minpos.y = std::numeric_limits<double>::infinity();
332+
}
333+
334+
void update(double x, double y)
335+
{
336+
start.x = std::min(start.x, x);
337+
start.y = std::min(start.y, y);
338+
end.x = std::max(end.x, x);
339+
end.y = std::max(end.y, y);
340+
if (x > 0.0) {
341+
minpos.x = std::min(minpos.x, x);
342+
}
343+
if (y > 0.0) {
344+
minpos.y = std::min(minpos.y, y);
345+
}
346+
}
347+
};
352348

353349
template <class PathIterator>
354350
void update_path_extents(PathIterator &path, agg::trans_affine &trans, extent_limits &extents)
@@ -367,7 +363,7 @@ void update_path_extents(PathIterator &path, agg::trans_affine &trans, extent_li
367363
if ((code & agg::path_cmd_end_poly) == agg::path_cmd_end_poly) {
368364
continue;
369365
}
370-
update_limits(x, y, extents);
366+
extents.update(x, y);
371367
}
372368
}
373369

@@ -390,7 +386,7 @@ void get_path_collection_extents(agg::trans_affine &master_transform,
390386

391387
agg::trans_affine trans;
392388

393-
reset_limits(extent);
389+
extent.reset();
394390

395391
for (auto i = 0; i < N; ++i) {
396392
typename PathGenerator::path_iterator path(paths(i % Npaths));

src/_path_wrapper.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ Py_get_path_collection_extents(agg::trans_affine master_transform,
6868

6969
py::ssize_t dims[] = { 2, 2 };
7070
py::array_t<double> extents(dims);
71-
*extents.mutable_data(0, 0) = e.x0;
72-
*extents.mutable_data(0, 1) = e.y0;
73-
*extents.mutable_data(1, 0) = e.x1;
74-
*extents.mutable_data(1, 1) = e.y1;
71+
*extents.mutable_data(0, 0) = e.start.x;
72+
*extents.mutable_data(0, 1) = e.start.y;
73+
*extents.mutable_data(1, 0) = e.end.x;
74+
*extents.mutable_data(1, 1) = e.end.y;
7575

7676
py::ssize_t minposdims[] = { 2 };
7777
py::array_t<double> minpos(minposdims);
78-
*minpos.mutable_data(0) = e.xm;
79-
*minpos.mutable_data(1) = e.ym;
78+
*minpos.mutable_data(0) = e.minpos.x;
79+
*minpos.mutable_data(1) = e.minpos.y;
8080

8181
return py::make_tuple(extents, minpos);
8282
}

0 commit comments

Comments
 (0)