RDKit
Open-source cheminformatics and machine learning.
Toggle main menu visibility
Loading...
Searching...
No Matches
StringRect.h
Go to the documentation of this file.
1
//
2
// Copyright (C) 2021-2022 David Cosgrove and other RDKit contributors
3
//
4
// @@ All Rights Reserved @@
5
// This file is part of the RDKit.
6
// The contents are covered by the terms of the BSD license
7
// which is included in the file license.txt, found at the root
8
// of the RDKit source tree.
9
//
10
//
11
// Original author: David Cosgrove (CozChemIx).
12
//
13
// This is used by DrawText classes. It is not intended for general use.
14
15
#ifndef RDKIT_STRINGRECT_H
16
#define RDKIT_STRINGRECT_H
17
18
#include <
Geometry/point.h
>
19
20
namespace
RDKit
{
21
namespace
MolDraw2D_detail
{
22
using
RDGeom::Point2D;
23
24
// for holding dimensions of the rectangle round a string.
25
struct
StringRect
{
26
Point2D
trans_
;
// Where to draw char relative to other chars in string
27
Point2D
offset_
;
// offset for draw coords so char is centred correctly
28
Point2D
g_centre_
;
// glyph centre relative to the origin of the char.
29
double
y_shift_
;
// shift the whole thing in y by this. For multi-line text.
30
double
width_
,
height_
;
// of the glyph itself, not the character cell
31
double
rect_corr_
;
// because if we move a char one way, we need to move the
32
// rectangle the other.
33
int
clash_score_
;
// rough measure of how badly it clashed with other things
34
// lower is better, 0 is no clash.
35
36
StringRect
()
37
:
trans_
(0.0, 0.0),
38
offset_
(0.0, 0.0),
39
g_centre_
(
offset_
),
40
y_shift_
(0.0),
41
width_
(0.0),
42
height_
(0.0),
43
rect_corr_
(0.0),
44
clash_score_
(0) {}
45
StringRect
(
const
Point2D
&offset,
const
Point2D
&g_centre,
double
w,
double
h)
46
:
trans_
(0.0, 0.0),
47
offset_
(offset),
48
g_centre_
(g_centre),
49
y_shift_
(0.0),
50
width_
(w),
51
height_
(h),
52
rect_corr_
(0.0),
53
clash_score_
(0) {}
54
// tl is top, left; br is bottom, right of the glyph, relative to the
55
// centre. Padding in draw coords.
56
void
calcCorners
(
Point2D
&tl,
Point2D
&tr,
Point2D
&br,
Point2D
&bl,
57
double
padding)
const
{
58
double
wb2 = padding +
width_
/ 2.0;
59
double
hb2 = padding +
height_
/ 2.0;
60
Point2D
c;
61
calcCentre
(c);
62
tl =
Point2D
(c.
x
- wb2, c.
y
- hb2);
63
tr =
Point2D
(c.
x
+ wb2, c.
y
- hb2);
64
br =
Point2D
(c.
x
+ wb2, c.
y
+ hb2);
65
bl =
Point2D
(c.
x
- wb2, c.
y
+ hb2);
66
}
67
void
calcCentre
(
Point2D
&c)
const
{
68
c =
trans_
+
g_centre_
-
offset_
;
69
c.
y
-=
y_shift_
;
70
}
71
bool
isPointInside
(
const
Point2D
&pt,
double
padding = 0.0)
const
{
72
Point2D
tl, tr, br, bl;
73
calcCorners
(tl, tr, br, bl, padding);
74
// is +ve y up or down?
75
if
(tl.
y
< bl.
y
) {
76
std::swap(tl, bl);
77
std::swap(tr, br);
78
}
79
return
pt.
x
>= tl.
x
&& pt.
x
<= br.
x
&& pt.
y
>= br.
y
&& pt.
y
<= tl.
y
;
80
}
81
bool
doesItIntersect
(
const
StringRect
&other,
double
padding = 0.0)
const
{
82
Point2D
ttl, ttr, tbr, tbl;
83
calcCorners
(ttl, ttr, tbr, tbl, padding);
84
// is +ve y up or down?
85
if
(ttl.
y
< tbl.
y
) {
86
std::swap(ttl, tbl);
87
std::swap(ttr, tbr);
88
}
89
Point2D
otl, otr, obr, obl;
90
other.
calcCorners
(otl, otr, obr, obl, padding);
91
if
(otl.
y
< obl.
y
) {
92
std::swap(otl, obl);
93
std::swap(otr, obr);
94
}
95
// This could be done with isPointInside, but that would recalculate
96
// the corners each time.
97
if
((otl.
x
>= ttl.
x
&& otl.
x
<= ttr.
x
&& otl.
y
>= tbl.
y
&&
98
otl.
y
<= ttl.
y
) ||
99
(otr.
x
>= ttl.
x
&& otr.
x
<= ttr.
x
&& otr.
y
>= tbl.
y
&&
100
otr.
y
<= ttl.
y
) ||
101
(obr.
x
>= ttl.
x
&& obr.
x
<= ttr.
x
&& obr.
y
>= tbl.
y
&&
102
obr.
y
<= ttl.
y
) ||
103
(obl.
x
>= ttl.
x
&& obl.
x
<= ttr.
x
&& obl.
y
>= tbl.
y
&&
104
obl.
y
<= ttl.
y
)) {
105
return
true
;
106
}
107
if
((ttl.
x
>= otl.
x
&& ttl.
x
<= otr.
x
&& ttl.
y
>= obl.
y
&&
108
ttl.
y
<= otl.
y
) ||
109
(ttr.
x
>= otl.
x
&& ttr.
x
<= otr.
x
&& ttr.
y
>= obl.
y
&&
110
ttr.
y
<= otl.
y
) ||
111
(tbr.
x
>= otl.
x
&& tbr.
x
<= otr.
x
&& tbr.
y
>= obl.
y
&&
112
tbr.
y
<= otl.
y
) ||
113
(tbl.
x
>= otl.
x
&& tbl.
x
<= otr.
x
&& tbl.
y
>= obl.
y
&&
114
tbl.
y
<= otl.
y
)) {
115
return
true
;
116
}
117
return
false
;
118
}
119
};
120
121
}
// namespace MolDraw2D_detail
122
}
// namespace RDKit
123
124
#endif
// RDKIT_STRINGRECT_H
RDGeom::Point2D
Definition
point.h:284
RDGeom::Point2D::y
double y
Definition
point.h:287
RDGeom::Point2D::x
double x
Definition
point.h:286
RDKit::MolDraw2D_detail
Definition
AtomSymbol.h:30
RDKit
Std stuff.
Definition
Abbreviations.h:19
point.h
RDKit::MolDraw2D_detail::StringRect::StringRect
StringRect()
Definition
StringRect.h:36
RDKit::MolDraw2D_detail::StringRect::width_
double width_
Definition
StringRect.h:30
RDKit::MolDraw2D_detail::StringRect::StringRect
StringRect(const Point2D &offset, const Point2D &g_centre, double w, double h)
Definition
StringRect.h:45
RDKit::MolDraw2D_detail::StringRect::trans_
Point2D trans_
Definition
StringRect.h:26
RDKit::MolDraw2D_detail::StringRect::doesItIntersect
bool doesItIntersect(const StringRect &other, double padding=0.0) const
Definition
StringRect.h:81
RDKit::MolDraw2D_detail::StringRect::offset_
Point2D offset_
Definition
StringRect.h:27
RDKit::MolDraw2D_detail::StringRect::clash_score_
int clash_score_
Definition
StringRect.h:33
RDKit::MolDraw2D_detail::StringRect::height_
double height_
Definition
StringRect.h:30
RDKit::MolDraw2D_detail::StringRect::g_centre_
Point2D g_centre_
Definition
StringRect.h:28
RDKit::MolDraw2D_detail::StringRect::rect_corr_
double rect_corr_
Definition
StringRect.h:31
RDKit::MolDraw2D_detail::StringRect::isPointInside
bool isPointInside(const Point2D &pt, double padding=0.0) const
Definition
StringRect.h:71
RDKit::MolDraw2D_detail::StringRect::y_shift_
double y_shift_
Definition
StringRect.h:29
RDKit::MolDraw2D_detail::StringRect::calcCorners
void calcCorners(Point2D &tl, Point2D &tr, Point2D &br, Point2D &bl, double padding) const
Definition
StringRect.h:56
RDKit::MolDraw2D_detail::StringRect::calcCentre
void calcCentre(Point2D &c) const
Definition
StringRect.h:67
GraphMol
MolDraw2D
StringRect.h
Generated on
for RDKit by
1.17.0