RDKit: MolDraw2DDetails.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
MolDraw2DDetails.h
Go to the documentation of this file.
1//
2// Copyright (C) 2015-2022 Greg Landrum 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#include <RDGeneral/export.h>
12#ifndef RDKITMOLDRAW2DDETAILS_H
13#define RDKITMOLDRAW2DDETAILS_H
14
15#include <vector>
16
17#include <Geometry/point.h>
18#include <GraphMol/RDKitBase.h>
19#include <GraphMol/MolDraw2D/MolDraw2D.h>
20
21#include <tuple>
22#include <boost/format.hpp>
23
24// ****************************************************************************
25using RDGeom::Point2D;
26
27namespace RDKit {
28namespace MolDraw2D_detail {
29struct StringRect;
30
31// data taken from the helvetica font info in
32// $RDBASE/rdkit/sping/PDF/pdfmetrics.py
33 const int char_widths[] = {
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36 0, 0, 0, 0, 278, 278, 355, 556, 556, 889, 667, 222, 333, 333,
37 389, 584, 278, 333, 278, 278, 556, 556, 556, 556, 556, 556, 556, 556,
38 556, 556, 278, 278, 584, 584, 584, 556, 1015, 667, 667, 722, 722, 667,
39 611, 778, 722, 278, 500, 667, 556, 833, 722, 778, 667, 778, 722, 667,
40 611, 722, 667, 944, 667, 667, 611, 278, 278, 278, 469, 556, 222, 556,
41 556, 500, 556, 556, 278, 556, 556, 222, 222, 500, 222, 833, 556, 556,
42 556, 556, 333, 500, 278, 556, 500, 722, 500, 500, 500, 334, 260, 334,
43 584, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 333, 556, 556, 167, 556, 556, 556,
46 556, 191, 333, 556, 333, 333, 500, 500, 0, 556, 556, 556, 278, 0,
47 537, 350, 222, 333, 333, 556, 1000, 1000, 0, 611, 0, 333, 333, 333,
48 333, 333, 333, 333, 333, 0, 333, 333, 0, 333, 333, 333, 1000, 0,
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50 0, 1000, 0, 370, 0, 0, 0, 0, 556, 778, 1000, 365, 0, 0,
51 0, 0, 0, 889, 0, 0, 0, 278, 0, 0, 222, 611, 944, 611,
52 0, 0, 834};
53
54// angles in degrees.
55 RDKIT_MOLDRAW2D_EXPORT void arcPoints(const Point2D &cds1, const Point2D &cds2,
56 std::vector<Point2D> &res,
57 float startAng = 0, float extent = 360);
58
59//! add annotations with atom indices.
60 RDKIT_MOLDRAW2D_EXPORT inline void addAtomIndices(const ROMol &mol) {
61 // we don't need this in the global set of tags since it will only be used
62 // here
63 if (mol.hasProp("_atomIndicesAdded")) {
64 return;
65 }
66 bool computed = true;
67 mol.setProp("_atomIndicesAdded", 1, computed);
68 for (auto atom : mol.atoms()) {
69 auto lab = std::to_string(atom->getIdx());
70 if (atom->hasProp(common_properties::atomNote)) {
71 lab += "," + atom->getProp<std::string>(common_properties::atomNote);
72 }
73 atom->setProp(common_properties::atomNote, lab);
74 }
75};
76
77//! add annotations with bond indices.
78 RDKIT_MOLDRAW2D_EXPORT inline void addBondIndices(const ROMol &mol) {
79 // we don't need this in the global set of tags since it will only be used
80 // here
81 if (mol.hasProp("_bondIndicesAdded")) {
82 return;
83 }
84 bool computed = true;
85 mol.setProp("_bondIndicesAdded", 1, computed);
86 for (auto bond : mol.bonds()) {
87 auto lab = std::to_string(bond->getIdx());
88 if (bond->hasProp(common_properties::bondNote)) {
89 lab += "," + bond->getProp<std::string>(common_properties::bondNote);
90 }
91 bond->setProp(common_properties::bondNote, lab);
92 }
93};
94
95 RDKIT_MOLDRAW2D_EXPORT std::vector<Point2D> getBracketPoints(
96 const Point2D &p1, const Point2D &p2, const Point2D &refPt,
97 const std::vector<std::pair<Point2D, Point2D>> &bondSegments,
98 double bracketFrac = 0.1);
99// there are a several empirically determined constants here.
100 RDKIT_MOLDRAW2D_EXPORT std::vector<Point2D> handdrawnLine(
101 Point2D cds1, Point2D cds2, double scale, bool shiftBegin = false,
102 bool shiftEnd = false, unsigned nSteps = 4, double deviation = 0.03,
103 double endShift = 0.5);
104
105 inline std::string formatDouble(double val) {
106 return boost::str(boost::format("%.1f") % val);
107}
108
109 RDKIT_MOLDRAW2D_EXPORT bool doesLineIntersect(const StringRect &rect,
110 const Point2D &end1,
111 const Point2D &end2,
112 double padding);
113// returns true if any corner of triangle is inside the rectangle.
114 RDKIT_MOLDRAW2D_EXPORT bool doesTriangleIntersect(const StringRect &rect,
115 const Point2D &pt1,
116 const Point2D &pt2,
117 const Point2D &pt3,
118 double padding);
119 RDKIT_MOLDRAW2D_EXPORT bool doesLineIntersectEllipse(
120 const Point2D &centre, double xradius, double yradius, double padding,
121 const Point2D &end1, const Point2D &end2);
122// angles expected in degrees, between 0 and 360.
123 RDKIT_MOLDRAW2D_EXPORT bool doesLineIntersectArc(
124 const Point2D &centre, double xradius, double yradius, double start_ang,
125 double stop_ang, double padding, const Point2D &end1, const Point2D &end2);
126 RDKIT_MOLDRAW2D_EXPORT bool doLinesIntersect(const Point2D &l1s,
127 const Point2D &l1f,
128 const Point2D &l2s,
129 const Point2D &l2f, Point2D *ip);
130// This uses the barycentric coordinate system method from
131// http://totologic.blogspot.com/2014/01/accurate-point-in-triangle-test.html
132// where it notes and provides a solution for instabilities when the point
133// in exactly on one of the edges of the triangle. That refinement is not
134// implemented because it seems a bit of overkill for most uses. It is an
135// issue when, for example, two triangles share an edge and the point is on that
136// edge, when it might give the disappointing result that the point is in
137// neither triangle.
138 RDKIT_MOLDRAW2D_EXPORT bool isPointInTriangle(const Point2D &pt,
139 const Point2D &t1,
140 const Point2D &t2,
141 const Point2D &t3);
142
143// returns a vector of p1,c1,c2,p2 tuples for bezier curves
144 RDKIT_MOLDRAW2D_EXPORT
145std::vector<std::tuple<Point2D, Point2D, Point2D, Point2D>> getWavyLineSegments(
146 const Point2D &p1, const Point2D &p2, unsigned int nSegments,
147 double vertOffset);
148
149// calculate the points making up the arrowhead of a DrawShapeArrow, allowing
150// for the fact that in polygon mode the point can extend over the end
151// of the point, because of the mitring.
152 RDKIT_MOLDRAW2D_EXPORT void calcArrowHead(Point2D &arrowEnd, Point2D &arrow1,
153 Point2D &arrow2,
154 const Point2D &arrowBegin,
155 double frac, double lineWidth,
156 double angle);
157
158// adjust p2 so that the line from p1 to p2 stops where it intersects
159// the ellipse.
160 RDKIT_MOLDRAW2D_EXPORT void adjustLineEndForEllipse(const Point2D &centre,
161 double xradius,
162 double yradius, Point2D p1,
163 Point2D &p2);
164
165} // namespace MolDraw2D_detail
166} // namespace RDKit
167
168#endif
MolDraw2D.h
RDKitBase.h
pulls in the core RDKit functionality
RDGeom::Point2D
Definition point.h:284
RDKit::RDProps::hasProp
bool hasProp(const std::string &key) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition RDProps.h:126
RDKit::RDProps::setProp
void setProp(const std::string &key, T val, bool computed=false) const
sets a property value
Definition RDProps.h:77
RDKit::ROMol
Definition ROMol.h:199
RDKit::ROMol::atoms
CXXAtomIterator< MolGraph, Atom * > atoms()
C++11 Range iterator.
Definition ROMol.h:277
RDKit::ROMol::bonds
CXXBondIterator< MolGraph, Bond * > bonds()
Definition ROMol.h:316
export.h
RDKIT_MOLDRAW2D_EXPORT
#define RDKIT_MOLDRAW2D_EXPORT
Definition export.h:313
RDKit::MolDraw2D_detail
Definition AtomSymbol.h:30
RDKit::MolDraw2D_detail::adjustLineEndForEllipse
RDKIT_MOLDRAW2D_EXPORT void adjustLineEndForEllipse(const Point2D &centre, double xradius, double yradius, Point2D p1, Point2D &p2)
RDKit::MolDraw2D_detail::arcPoints
RDKIT_MOLDRAW2D_EXPORT void arcPoints(const Point2D &cds1, const Point2D &cds2, std::vector< Point2D > &res, float startAng=0, float extent=360)
RDKit::MolDraw2D_detail::doesLineIntersect
RDKIT_MOLDRAW2D_EXPORT bool doesLineIntersect(const StringRect &rect, const Point2D &end1, const Point2D &end2, double padding)
RDKit::MolDraw2D_detail::doLinesIntersect
RDKIT_MOLDRAW2D_EXPORT bool doLinesIntersect(const Point2D &l1s, const Point2D &l1f, const Point2D &l2s, const Point2D &l2f, Point2D *ip)
RDKit::MolDraw2D_detail::addBondIndices
RDKIT_MOLDRAW2D_EXPORT void addBondIndices(const ROMol &mol)
add annotations with bond indices.
Definition MolDraw2DDetails.h:78
RDKit::MolDraw2D_detail::calcArrowHead
RDKIT_MOLDRAW2D_EXPORT void calcArrowHead(Point2D &arrowEnd, Point2D &arrow1, Point2D &arrow2, const Point2D &arrowBegin, double frac, double lineWidth, double angle)
RDKit::MolDraw2D_detail::getBracketPoints
RDKIT_MOLDRAW2D_EXPORT std::vector< Point2D > getBracketPoints(const Point2D &p1, const Point2D &p2, const Point2D &refPt, const std::vector< std::pair< Point2D, Point2D > > &bondSegments, double bracketFrac=0.1)
RDKit::MolDraw2D_detail::handdrawnLine
RDKIT_MOLDRAW2D_EXPORT std::vector< Point2D > handdrawnLine(Point2D cds1, Point2D cds2, double scale, bool shiftBegin=false, bool shiftEnd=false, unsigned nSteps=4, double deviation=0.03, double endShift=0.5)
RDKit::MolDraw2D_detail::isPointInTriangle
RDKIT_MOLDRAW2D_EXPORT bool isPointInTriangle(const Point2D &pt, const Point2D &t1, const Point2D &t2, const Point2D &t3)
RDKit::MolDraw2D_detail::addAtomIndices
RDKIT_MOLDRAW2D_EXPORT void addAtomIndices(const ROMol &mol)
add annotations with atom indices.
Definition MolDraw2DDetails.h:60
RDKit::MolDraw2D_detail::char_widths
const int char_widths[]
Definition MolDraw2DDetails.h:33
RDKit::MolDraw2D_detail::doesTriangleIntersect
RDKIT_MOLDRAW2D_EXPORT bool doesTriangleIntersect(const StringRect &rect, const Point2D &pt1, const Point2D &pt2, const Point2D &pt3, double padding)
RDKit::MolDraw2D_detail::doesLineIntersectArc
RDKIT_MOLDRAW2D_EXPORT bool doesLineIntersectArc(const Point2D &centre, double xradius, double yradius, double start_ang, double stop_ang, double padding, const Point2D &end1, const Point2D &end2)
RDKit::MolDraw2D_detail::doesLineIntersectEllipse
RDKIT_MOLDRAW2D_EXPORT bool doesLineIntersectEllipse(const Point2D &centre, double xradius, double yradius, double padding, const Point2D &end1, const Point2D &end2)
RDKit::MolDraw2D_detail::formatDouble
std::string formatDouble(double val)
Definition MolDraw2DDetails.h:105
RDKit::MolDraw2D_detail::getWavyLineSegments
RDKIT_MOLDRAW2D_EXPORT std::vector< std::tuple< Point2D, Point2D, Point2D, Point2D > > getWavyLineSegments(const Point2D &p1, const Point2D &p2, unsigned int nSegments, double vertOffset)
RDKit::common_properties::bondNote
RDKIT_RDGENERAL_EXPORT const std::string bondNote
RDKit::common_properties::atomNote
RDKIT_RDGENERAL_EXPORT const std::string atomNote
RDKit
Std stuff.
Definition Abbreviations.h:19
point.h
RDKit::MolDraw2D_detail::StringRect
Definition StringRect.h:24