Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
Fix bounding box intersection calculation
  • Loading branch information
burghoff committed Oct 26, 2024
1 parent 85f5c2b commit be073e1
Show file tree
Hide file tree
Showing 5 changed files with 620 additions and 85 deletions.
38 changes: 20 additions & 18 deletions scientific_inkscape/dhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ def si_tmp(dirbase="", filename=None):


def bounding_box2(
self, dotransform=True, includestroke=True, roughpath=False, parsed=False
self, dotransform=True, includestroke=True, roughpath=False, parsed=False, includeclipmask=True
):
"""
Cached bounding box that requires no command call
Expand All @@ -1174,7 +1174,8 @@ def bounding_box2(
"""
if not (hasattr(self, "_cbbox")):
self._cbbox = dict()
if (dotransform, includestroke, roughpath, parsed) not in self._cbbox:
inputs = (dotransform, includestroke, roughpath, parsed, includeclipmask)
if inputs not in self._cbbox:
try:
ret = bbox(None)
if self.tag in ttags:
Expand Down Expand Up @@ -1256,20 +1257,21 @@ def bounding_box2(
ret = ret.transform(xyt @ lel.ctransform)

if not (ret.isnull):
for cmv in ["clip-path", "mask"]:
clip = self.get_link(cmv, llget=True)
if clip is not None:
cbb = bounding_box2(
clip,
dotransform=False,
includestroke=False,
roughpath=roughpath,
parsed=parsed,
)
if not (cbb.isnull):
ret = ret.intersection(cbb)
else:
ret = bbox(None)
if includeclipmask:
for cmv in ["clip-path", "mask"]:
clip = self.get_link(cmv, llget=True)
if clip is not None:
cbb = bounding_box2(
clip,
dotransform=False,
includestroke=False,
roughpath=roughpath,
parsed=parsed,
)
if not (cbb.isnull):
ret = ret.intersection(cbb)
else:
ret = bbox(None)

if dotransform:
if not (ret.isnull):
Expand All @@ -1279,8 +1281,8 @@ def bounding_box2(
import traceback

inkex.utils.debug(traceback.format_exc())
self._cbbox[(dotransform, includestroke, roughpath, parsed)] = ret
return self._cbbox[(dotransform, includestroke, roughpath, parsed)]
self._cbbox[inputs] = ret
return self._cbbox[inputs]


def set_cbbox(self, val):
Expand Down
49 changes: 26 additions & 23 deletions scientific_inkscape/flatten_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,30 +334,31 @@ def effect(self):

if self.options.revertpaths:
bb = dh.bounding_box2(
el, includestroke=False, dotransform=False
el, includestroke=False, dotransform=False, includeclipmask=False
)
if bb.w < bb.h / RECT_THRESHOLD and not sf.fill_isurl:
el.object_to_path()
np = "m {0},{1} v {2}".format(bb.xc, bb.y1, bb.h)
el.set("d", np)
el.cstyle["stroke"] = sf.fill.to_rgb()
if sf.fill.alpha != 1.0:
el.cstyle["stroke-opacity"] = sf.fill.alpha
el.cstyle["opacity"] = 1
el.cstyle["fill"] = "none"
el.cstyle["stroke-width"] = str(bb.w)
el.cstyle["stroke-linecap"] = "butt"
elif bb.h < bb.w / RECT_THRESHOLD and not sf.fill_isurl:
el.object_to_path()
np = "m {0},{1} h {2}".format(bb.x1, bb.yc, bb.w)
el.set("d", np)
el.cstyle["stroke"] = sf.fill.to_rgb()
if sf.fill.alpha != 1.0:
el.cstyle["stroke-opacity"] = sf.fill.alpha
el.cstyle["opacity"] = 1
el.cstyle["fill"] = "none"
el.cstyle["stroke-width"] = str(bb.h)
el.cstyle["stroke-linecap"] = "butt"
if not bb.isnull:
if bb.w < bb.h / RECT_THRESHOLD and not sf.fill_isurl:
el.object_to_path()
np = "m {0},{1} v {2}".format(bb.xc, bb.y1, bb.h)
el.set("d", np)
el.cstyle["stroke"] = sf.fill.to_rgb()
if sf.fill.alpha != 1.0:
el.cstyle["stroke-opacity"] = sf.fill.alpha
el.cstyle["opacity"] = 1
el.cstyle["fill"] = "none"
el.cstyle["stroke-width"] = str(bb.w)
el.cstyle["stroke-linecap"] = "butt"
elif bb.h < bb.w / RECT_THRESHOLD and not sf.fill_isurl:
el.object_to_path()
np = "m {0},{1} h {2}".format(bb.x1, bb.yc, bb.w)
el.set("d", np)
el.cstyle["stroke"] = sf.fill.to_rgb()
if sf.fill.alpha != 1.0:
el.cstyle["stroke-opacity"] = sf.fill.alpha
el.cstyle["opacity"] = 1
el.cstyle["fill"] = "none"
el.cstyle["stroke-width"] = str(bb.h)
el.cstyle["stroke-linecap"] = "butt"

if self.options.fixtext:
if setreplacement:
Expand Down Expand Up @@ -405,10 +406,12 @@ def effect(self):
]
bbs = dh.BB2(self, ngs2, roughpath=True, parsed=True)
ngs3 = [el for el in ngs2 if el.get_id() in bbs]
# dh.idebug(ngs3)
bbs = [dh.bbox(bbs.get(el.get_id())) for el in ngs3]
wriis = [ii for ii, el in enumerate(ngs3) if el in wrects]
wrbbs = [bbs[ii] for ii in wriis]
intrscts = dh.bb_intersects(bbs, wrbbs)
# dh.idebug(intrscts)
for jj, ii in enumerate(wriis):
if not any(intrscts[:ii, jj]):
ngs3[ii].delete(deleteup=True)
Expand Down
4 changes: 3 additions & 1 deletion scientific_inkscape/inkex1_3_0/inkex/text/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,9 @@ def intersection(self, bb2):
maxx = min([self.x2, bb2.x2])
miny = max([self.y1, bb2.y1])
maxy = min([self.y2, bb2.y2])
return bbox([minx, miny, abs(maxx - minx), abs(maxy - miny)])
if maxx < minx or maxy < miny:
return bbox(None)
return bbox([minx, miny, maxx - minx, maxy - miny])
return bbox(bb2.sbb)

def __mul__(self, scl):
Expand Down

Large diffs are not rendered by default.

Loading

0 comments on commit be073e1

Please sign in to comment.