RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
MolDraw2DHelpers.h
Go to the documentation of this file.
1//
2// Copyright (C) 2014-2021 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// Original author: David Cosgrove (CozChemIx Limited)
11//
12// A load of helper classes used by MolDraw2D.
13
14#ifndef RDKIT_MOLDRAW2DHELPERS_H
15#define RDKIT_MOLDRAW2DHELPERS_H
16
17#include <Geometry/point.h>
19
20using RDGeom::Point2D;
21
22namespace RDKit {
23
24namespace MolDraw2D_detail {
25// for aligning the drawing of text to the passed in coords.
26enum class OrientType : unsigned char {
27 C = 0,
32};
33enum class TextAlignType : unsigned char {
34 MIDDLE = 0,
37};
38} // namespace MolDraw2D_detail
39
40struct DrawColour {
41 double r = 0.0, g = 0.0, b = 0.0, a = 1.0;
42 DrawColour() = default;
43 DrawColour(double r, double g, double b, double a = 1.0)
44 : r(r), g(g), b(b), a(a) {}
45 bool operator==(const DrawColour &other) const {
46 return r == other.r && g == other.g && b == other.b && a == other.a;
47 }
48 bool operator!=(const DrawColour &other) const { return !(*this == other); }
49 bool feq(const DrawColour &other, double tol = 0.001,
50 bool ignoreAlpha = true) const {
51 return fabs(r - other.r) <= tol && fabs(g - other.g) <= tol &&
52 fabs(b - other.b) <= tol &&
53 (ignoreAlpha || fabs(a - other.a) <= tol);
54 }
55 DrawColour operator+(const DrawColour &other) const {
56 return {r + other.r, g + other.g, b + other.b, a + other.a};
57 }
58 DrawColour operator-(const DrawColour &other) const {
59 return {r - other.r, g - other.g, b - other.b, a - other.a};
60 }
61 DrawColour operator/(double v) const {
62 PRECONDITION(v != 0.0, "divide by zero");
63 return {r / v, g / v, b / v, a / v};
64 }
65 DrawColour operator*(double v) const { return {r * v, g * v, b * v, a * v}; }
66};
67
68typedef std::map<int, DrawColour> ColourPalette;
69typedef std::vector<double> DashPattern;
70
71// This is used to convert the line width into something that SVG and
72// Cairo use. It was picked by eye, and was formerly hidden in
73// MolDraw2D::getDrawLineWidth().
74static const double lineWidthScaleFactor = 0.02;
75
76//! use the RDKit's default palette r
77// 201 is for hydrogens when atom symbols are not being drawn.
78inline void assignDefaultPalette(ColourPalette &palette) {
79 palette.clear();
80 palette[-1] = DrawColour(0, 0, 0);
81 palette[0] = DrawColour(0.1, 0.1, 0.1);
82 palette[1] = palette[6] = DrawColour(0.0, 0.0, 0.0);
83 palette[7] = DrawColour(0.0, 0.0, 1.0);
84 palette[8] = DrawColour(1.0, 0.0, 0.0);
85 palette[9] = DrawColour(0.2, 0.8, 0.8);
86 palette[15] = DrawColour(1.0, 0.5, 0.0);
87 palette[16] = DrawColour(0.8, 0.8, 0.0);
88 palette[17] = DrawColour(0.0, 0.802, 0.0);
89 palette[35] = DrawColour(0.5, 0.3, 0.1);
90 palette[53] = DrawColour(0.63, 0.12, 0.94);
91 palette[201] = DrawColour(0.68, 0.85, 0.90);
92};
93
94//! use the color palette from the Avalon renderer
95// 201 is for hydrogens when atom symbols are not being drawn.
96inline void assignAvalonPalette(ColourPalette &palette) {
97 palette.clear();
98 palette[-1] = DrawColour(0, 0, 0);
99 palette[0] = DrawColour(0.1, 0.1, 0.1);
100 palette[1] = palette[6] = DrawColour(0.0, 0.0, 0.0);
101 palette[7] = DrawColour(0.0, 0.0, 1.0);
102 palette[8] = DrawColour(1.0, 0.0, 0.0);
103 palette[9] = DrawColour(0.0, 0.498, 0.0);
104 palette[15] = DrawColour(0.498, 0.0, 0.498);
105 palette[16] = DrawColour(0.498, 0.247, 0.0);
106 palette[17] = DrawColour(0.0, 0.498, 0.0);
107 palette[35] = DrawColour(0.0, 0.498, 0.0);
108 palette[53] = DrawColour(0.247, 0.0, 0.498);
109 palette[201] = DrawColour(0.68, 0.85, 0.90);
110};
111
112//! use (part of) the CDK color palette
113/*!
114 data source:
115 https://github.com/cdk/cdk/blob/master/display/render/src/main/java/org/openscience/cdk/renderer/color/CDK2DAtomColors.java
116*/
117// 201 is for hydrogens when atom symbols are not being drawn.
118inline void assignCDKPalette(ColourPalette &palette) {
119 palette.clear();
120 palette[-1] = DrawColour(0, 0, 0);
121 palette[0] = DrawColour(0.1, 0.1, 0.1);
122 palette[1] = palette[6] = DrawColour(0.0, 0.0, 0.0);
123 palette[7] = DrawColour(0.188, 0.314, 0.972);
124 palette[8] = DrawColour(1.0, 0.051, 0.051);
125 palette[9] = DrawColour(0.565, 0.878, 0.314);
126 palette[15] = DrawColour(1.0, 0.5, 0.0);
127 palette[16] = DrawColour(0.776, 0.776, 0.173);
128 palette[17] = DrawColour(0.122, 0.498, 0.122);
129 palette[35] = DrawColour(0.651, 0.161, 0.161);
130 palette[53] = DrawColour(0.580, 0.0, 0.580);
131 palette[5] = DrawColour(1.000, 0.710, 0.710);
132 palette[201] = DrawColour(0.68, 0.85, 0.90);
133};
134
135// 201 is for hydrogens when atom symbols are not being drawn.
137 palette.clear();
138 palette[-1] = DrawColour(0.8, 0.8, 0.8);
139 palette[0] = DrawColour(0.9, 0.9, 0.9);
140 palette[1] = palette[6] = DrawColour(0.9, 0.9, 0.9);
141 palette[7] = DrawColour(0.33, 0.41, 0.92);
142 palette[8] = DrawColour(1.0, 0.2, 0.2);
143 palette[9] = DrawColour(0.2, 0.8, 0.8);
144 palette[15] = DrawColour(1.0, 0.5, 0.0);
145 palette[16] = DrawColour(0.8, 0.8, 0.0);
146 palette[17] = DrawColour(0.0, 0.802, 0.0);
147 palette[35] = DrawColour(0.71, 0.4, 0.07);
148 palette[53] = DrawColour(0.89, 0.004, 1);
149 palette[201] = DrawColour(0.68, 0.85, 0.90);
150};
151
152inline void assignBWPalette(ColourPalette &palette) {
153 palette.clear();
154 palette[-1] = DrawColour(0, 0, 0);
155};
156
161
162BETTER_ENUM(DrawElement, uint32_t, // clang-format off
163 NONE = 0,
164 PRESHAPES = 1 << 0,
165 BONDS = 1 << 1,
166 ATOMLABELS = 1 << 2,
167 HIGHLIGHTS = 1 << 3,
168 ANNOTATIONS = 1 << 4,
169 RADICALS = 1 << 5,
170 POSTSHAPES = 1 << 6,
171 ALL = 0x7fffffff
172);
173
176 false; // toggles replacing 2H with D and 3H with T
177 bool dummiesAreAttachments = false; // draws "breaks" at dummy atoms
178 bool circleAtoms = true; // draws circles under highlighted atoms
179 bool splitBonds = false; // split bonds into per atom segments
180 // most useful for dynamic manipulation of drawing
181 // especially for svg
182 DrawColour highlightColour{1.0, 0.5, 0.5, 1.0}; // default highlight color
183 bool continuousHighlight = true; // highlight by drawing an outline
184 // *underneath* the molecule
185 bool fillHighlights = true; // fill the areas used to highlight atoms and
186 // atom regions
187 double highlightRadius = 0.3; // default if nothing given for a particular
188 // atom. units are "Angstrom"
189 int flagCloseContactsDist = 3; // if positive, this will be used as a cutoff
190 // (in pixels) for highlighting close contacts
192 false; // toggles inclusion of atom tags in the output. does
193 // not make sense for all renderers.
194 bool clearBackground = true; // toggles clearing the background before
195 // drawing a molecule
197 1.0, 1.0, 1.0, 1.0}; // color to be used while clearing the background
198 DrawColour queryColour{0.5, 0.5, 0.5,
199 1.0}; // color to be used for query bonds
200 int legendFontSize = 16; // font size (in pixels) to be used for the legend
201 // (if present)
203 0.1; // fraction of the draw panel to be used for the legend if present
204 int maxFontSize = 40; // maximum size in pixels for font in drawn molecule.
205 // -1 means no max.
206 int minFontSize = 6; // likewise for -1.
208 -1; // font size to use, in pixels. Default -1 means not fixed. If set,
209 // always used irrespective of scale, minFontSize and maxFontSize.
210 double annotationFontScale = 0.5; // scales font relative to atom labels for
211 // atom and bond annotation.
212 std::string fontFile = ""; // name of font file for freetype rendering. If
213 // given, over-rides default
214 // (BuiltinTelexRegular). Can also be
215 // BuiltinRobotoRegular.
217 0}; // color to be used for the legend (if present)
218 double multipleBondOffset = 0.15; // offset for the extra lines
219 // in a multiple bond as a fraction of
220 // mean bond length
221 double padding =
222 0.05; // fraction of empty space to leave around the molecule
223 double componentPadding = 0.0; // fraction of empty space to leave around
224 // each component in a reaction drawing
225 double additionalAtomLabelPadding = 0.0; // additional padding to leave
226 // around atom labels. Expressed as
227 // a fraction of the font size.
228 std::map<int, std::string> atomLabels; // replacement labels for atoms
230 false; // disables inclusion of atom labels in the rendering
231 std::vector<std::vector<int>> atomRegions; // regions
233 0.0, 0.0, 0.0,
234 1.0}; // color to be used for the symbols and arrows in reactions
236 1.0}; // color to be used for annotations
238 0.0, 0.0, 0.0, 1.0}; // color to be used for atom indices and notes
240 0.5, 0.5, 1.0, 1.0}; // color to be used for bond indices and notes
241 double bondLineWidth = 2.0; // default line width when drawing bonds
242 bool scaleBondWidth = false; // whether to apply scale() to the bond width
243 bool scaleHighlightBondWidth = true; // likewise with bond highlights.
244 int highlightBondWidthMultiplier = 8; // what to multiply standard bond width
245 // by for highlighting.
246 bool prepareMolsBeforeDrawing = true; // call prepareMolForDrawing() on each
247 // molecule passed to drawMolecules()
248 std::vector<DrawColour> highlightColourPalette; // defining 10 default colors
249 // for highlighting atoms and bonds
250 // or reactants in a reactions
251 ColourPalette atomColourPalette; // the palette used to assign
252 // colors to atoms based on
253 // atomic number.
254 double fixedScale =
255 -1.0; // fixes scale to this fraction of draw window width, so
256 // an average bond is this fraction of the width. If
257 // scale comes out smaller than this, reduces scale, but
258 // won't make it larger. The default of -1.0 means no fix.
260 -1.0; // fixes the bond length (and hence the scale) to
261 // always be this number of pixels. Assuming a bond
262 // length in coordinates is 1, as is normal. If
263 // scale comes out smaller than this, reduces scale,
264 // but won't make it larger. The default -1.0 means no
265 // fix. If both fixedScale and fixedBondLength are >
266 // 0.0, fixedScale wins.
267 double rotate = 0.0; // angle in degrees to rotate coords by about centre
268 // before drawing.
269 bool addAtomIndices = false; // adds atom indices to drawings.
270 bool addBondIndices = false; // adds bond indices to drawings.
271 bool isotopeLabels = true; // adds isotope to non-dummy atoms.
272 bool dummyIsotopeLabels = true; // adds isotope labels to dummy atoms.
273
274 bool addStereoAnnotation = false; // adds E/Z and R/S to drawings.
275 bool showAllCIPCodes = false; // show CIP codes for 'or' & 'and'
276 // StereoGroups that are normally hidden
277 bool atomHighlightsAreCircles = false; // forces atom highlights always to be
278 // circles. Default (false) is to put
279 // ellipses round longer labels.
282 bool centreMoleculesBeforeDrawing = false; // moves the centre of the drawn
283 // molecule to (0,0)
284 std::uint32_t drawingExtentsInclude =
285 DrawElement::ALL; // which elements should be included
286 // when computing drawing extents
287 bool explicitMethyl = false; // draw terminal methyl and related as CH3
289 true; // include radicals in the drawing (it can be useful to turn this
290 // off for reactions and queries)
292 true; // when possible include metadata about molecules and reactions in
293 // the output to allow them to be reconstructed
294 bool comicMode = false; // simulate hand-drawn lines for bonds. When combined
295 // with a font like Comic-Sans or Comic-Neue, this
296 // gives xkcd-like drawings.
297 int variableBondWidthMultiplier = 16; // what to multiply standard bond width
298 // by for variable attachment points.
299 double variableAtomRadius = 0.4; // radius value to use for atoms involved in
300 // variable attachment points.
302 0.8, 0.8, 0.8, 1.0}; // colour to use for variable attachment points
304 false; // add a molecule annotation with "ABS" if the chiral flag is set
306 false; // if all specified stereocenters are in a single StereoGroup,
307 // show a molecule-level annotation instead of the individual
308 // labels
309 bool unspecifiedStereoIsUnknown = false; // if true, double bonds with
310 // unspecified stereo are drawn
311 // crossed, potential stereocenters
312 // with unspecified stereo are drawn
313 // with a wavy bond.
315 false; // if true wedged and dashed bonds are drawn
316 // using symbolColour rather than inheriting
317 // their colour from the atoms
318 bool useMolBlockWedging = false; // If the molecule came from a MolBlock,
319 // prefer the wedging information that
320 // provides. If false, use RDKit rules.
321 double scalingFactor = 20.0; // scaling factor used for pixels->angstrom
322 // when auto scaling is being used
324 -1.0; // when > 0 this is used to set the baseFontSize used for text
325 // drawing. As a reference point: the default value for
326 // DrawText::baseFontSize is 0.6
327 bool drawMolsSameScale = true; // when drawing multiple molecules with
328 // DrawMolecules, forces them to use the same
329 // scale. Default is true.
331 true; // replace any atom, any hetero, any halo queries
332 // with complex query symbols A, Q, X, M, optionally followed
333 // by H if hydrogen is included (except for AH, which stays *).
334 // Default is true.
335 bool bracketsAroundAtomLists = true; // If true, puts brackets round atom
336 // lists in query atoms.
338 false; // If true, highlighted hetero atoms are drawn in standard colours
339 // rather than black.
340
342 highlightColourPalette.emplace_back(
343 DrawColour(1., 1., .67)); // popcorn yellow
344 highlightColourPalette.emplace_back(DrawColour(1., .8, .6)); // sand
345 highlightColourPalette.emplace_back(
346 DrawColour(1., .71, .76)); // light pink
347 highlightColourPalette.emplace_back(
348 DrawColour(.8, 1., .8)); // offwhitegreen
349 highlightColourPalette.emplace_back(DrawColour(.87, .63, .87)); // plum
350 highlightColourPalette.emplace_back(
351 DrawColour(.76, .94, .96)); // pastel blue
352 highlightColourPalette.emplace_back(
353 DrawColour(.67, .67, 1.)); // periwinkle
354 highlightColourPalette.emplace_back(DrawColour(.64, .76, .34)); // avocado
355 highlightColourPalette.emplace_back(
356 DrawColour(.56, .93, .56)); // light green
357 highlightColourPalette.emplace_back(DrawColour(.20, .63, .79)); // peacock
359 }
360};
361
362} // namespace RDKit
363
364#endif // RDKIT_MOLDRAW2DHELPERS_H
#define BETTER_ENUM(Enum, Underlying,...)
Definition BetterEnums.h:17
#define PRECONDITION(expr, mess)
Definition Invariant.h:108
#define RDKIT_MOLDRAW2D_EXPORT
Definition export.h:313
Std stuff.
std::vector< double > DashPattern
static const double lineWidthScaleFactor
MultiColourHighlightStyle
void assignDefaultPalette(ColourPalette &palette)
use the RDKit's default palette r
void assignAvalonPalette(ColourPalette &palette)
use the color palette from the Avalon renderer
void assignDarkModePalette(ColourPalette &palette)
std::map< int, DrawColour > ColourPalette
void assignBWPalette(ColourPalette &palette)
void assignCDKPalette(ColourPalette &palette)
use (part of) the CDK color palette
bool feq(const DrawColour &other, double tol=0.001, bool ignoreAlpha=true) const
DrawColour()=default
DrawColour operator+(const DrawColour &other) const
DrawColour operator/(double v) const
DrawColour operator*(double v) const
DrawColour(double r, double g, double b, double a=1.0)
bool operator!=(const DrawColour &other) const
DrawColour operator-(const DrawColour &other) const
bool operator==(const DrawColour &other) const
std::vector< std::vector< int > > atomRegions
DrawColour variableAttachmentColour
std::uint32_t drawingExtentsInclude
std::map< int, std::string > atomLabels
ColourPalette atomColourPalette
MultiColourHighlightStyle multiColourHighlightStyle
std::vector< DrawColour > highlightColourPalette