RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
DrawMol.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// Original author: David Cosgrove (CozChemIx Limited)
11//
12// This class is a helper class used by MolDraw2D to draw an ROMol.
13// It is not part of the public API and is not intended to be used except
14// by MolDraw2D.
15
16#ifndef RDKIT_DRAWMOL_H
17#define RDKIT_DRAWMOL_H
18
19#include <map>
20#include <string>
21#include <vector>
22
23#include <Geometry/point.h>
28
29namespace RDKit {
30
31class Atom;
32class Bond;
33class ROMol;
34class RWMol;
35class DrawText;
36
37namespace MolDraw2D_detail {
38
39class DrawMol {
40 public:
41 virtual ~DrawMol() = default;
42
43 // Make the object, scaled to a given pixel size.
44 /*!
45 \param mol : the molecule to draw
46 \param legend : the legend (to be drawn under the molecule)
47 \param width : width (in pixels) of the rendering
48 set this to -1 to have the canvas size set automatically
49 \param height : height (in pixels) of the rendering
50 set this to -1 to have the canvas size set automatically
51 \param drawOptions : a MolDrawOptions object from the owning MolDraw2D
52 \param textDrawer : a DrawText object from the owning MolDraw2D
53 \param highlightAtoms : (optional) vector of atom ids to highlight
54 \param highlightBonds : (optional) vector of bond ids to highlight
55 \param highlightAtomMap : (optional) map from atomId -> DrawColour
56 providing the highlight colors. If not provided the default highlight colour
57 from \c drawOptions() will be used.
58 \param highlightBondMap : (optional) map from bondId -> DrawColour
59 providing the highlight colors. If not provided the default highlight colour
60 from \c drawOptions() will be used.
61 \param highlightRadii : (optional) map from atomId -> radius (in molecule
62 coordinates) for the radii of atomic highlights. If not provided the default
63 value from \c drawOptions() will be used.
64 \param confId : (optional) conformer ID to be used for atomic
65 coordinates
66 */
67 DrawMol(const ROMol &mol, const std::string &legend, int width, int height,
68 const MolDrawOptions &drawOptions, DrawText &textDrawer,
69 const std::vector<int> *highlightAtoms = nullptr,
70 const std::vector<int> *highlightBonds = nullptr,
71 const std::map<int, DrawColour> *highlightAtomMap = nullptr,
72 const std::map<int, DrawColour> *highlightBondMap = nullptr,
73 const std::vector<std::pair<DrawColour, DrawColour>> *bondColours =
74 nullptr,
75 const std::map<int, double> *highlight_radii = nullptr,
76 bool includeAnnotations = true, int confId = -1,
77 bool isReactionMol = false);
78 /*!
79 Make a DrawMol when there's no molecule to draw, but we still want
80 a DrawMol in the MolDraw2D for scale, conversion of atom coords to
81 draw coords etc. And so DrawMol starts sounding like a poor name for
82 the class.
83 \param width : width (in pixels) of the rendering
84 \param height : height (in pixels) of the rendering
85 \param drawOptions : a MolDrawOptions object from the owning MolDraw2D
86 \param textDrawer : a DrawText object from the owning MolDraw2D
87 \param xmin : minimum value expected in X
88 \param xmax : miaximum value expected in X
89 \param ymin : minimum value expected in Y
90 \param ymax : maximum value expected in Y
91 \param scale : scale to use
92 \param fontscale : font scale to use
93 */
94 DrawMol(int width, int height, const MolDrawOptions &drawOptions,
95 DrawText &textDrawer, double xmin, double xmax, double ymin,
96 double ymax, double scale, double fontscale);
97 DrawMol(const DrawMol &) = delete;
98 DrawMol(DrawMol &&) = delete;
99 DrawMol &operator=(const DrawMol &) = delete;
100 DrawMol &operator=(DrawMol &&) = delete;
101
102 // this must be called before a drawing can be done.
104 // common bits used by createDrawObjects and setScale.
106 void initDrawMolecule(const ROMol &mol);
107 void extractAll(double scale);
111 virtual void extractHighlights(double scale);
123 // extractCloseContacts is to show where 2 atoms are drawn too close
124 // together and so needs the final drawing coords. It is therefore called
125 // at the end of changeToDrawCoords() and any necessary DrawShapePolyLines
126 // added to postShapes_ in drawCoords.
131 void draw(MolDraw2D &drawer) const;
134 // reduce width_ and height_ to just accomodate the Xrange_ and YRange_
135 // at the current scale. Recentres everything. So the DrawMol takes up
136 // no more screen real estate than it needs.
137 void shrinkToFit(bool withPadding = true);
138
139 // adds LaTeX-like annotation for super- and sub-script.
140 std::pair<std::string, OrientType> getAtomSymbolAndOrientation(
141 const Atom &atom) const;
142 std::string getAtomSymbol(const Atom &atom, OrientType orientation) const;
144
145 // if there's a legend, partition the height_ to accommodate it
147 // extractLegend is different from all the other extract... functions
148 // in that it needs to be called once the final scale has been found
149 // by calculateScale.
151
156 const std::pair<DrawColour, DrawColour> &cols);
158 const std::pair<DrawColour, DrawColour> &cols);
160 const std::pair<DrawColour, DrawColour> &cols);
161 void makeWavyBond(Bond *bond, double offset,
162 const std::pair<DrawColour, DrawColour> &cols);
163 void makeDativeBond(Bond *bond, double offset,
164 const std::pair<DrawColour, DrawColour> &cols);
165 void makeZeroBond(Bond *bond, const std::pair<DrawColour, DrawColour> &cols,
166 const DashPattern &dashPattern);
168 Point2D &endCds) const;
169 void newBondLine(const Point2D &pt1, const Point2D &pt2,
170 const DrawColour &col1, const DrawColour &col2, int atom1Idx,
171 int atom2Idx, int bondIdx, const DashPattern &dashPattern);
172 std::pair<DrawColour, DrawColour> getBondColours(Bond *bond);
173 void makeContinuousHighlights(double scale);
175 void makeAtomEllipseHighlights(double lineWidth);
176 // These are the lines for continuous highlights, that are
177 // now filled trapezoids rather than fat simple lines.
178 void makeBondHighlightLines(double lineWidth, double scale);
181 double getNoteStartAngle(const Atom *atom) const;
182 // see if the note will clash with anything else drawn on the molecule.
183 // Returns 0 if no clash, 1-4 if there is a clash, denoting what clashed.
185 int doesRectClash(const StringRect &rect, double padding) const;
188 Point2D &toCentre) const;
189 // Given some coords in molecule space (angstrom, probably) return the
190 // screen coords.
192 const Point2D &scaleFactor,
193 const Point2D &toCentre) const;
196 // and the other way.
199 double getScale() const { return scale_; }
200 double getFontScale() const { return fontScale_; }
201 // More often than not, newScale and newFontScale will be the same,
202 // but not if minFontScale of maxFontScale have become involved.
203 // The newFontScale will be used without checking the min and max.
204 void setScale(double newScale, double newFontScale,
205 bool ignoreFontLimits = true);
206 // Set all the transformation details from the incoming DrawMol to this
207 // one, so they can be overlaid properly. Doesn't change the offsets.
209
210 // For drawing into a grid, for example. Must be set before
211 // changeToDrawCoords is called for it to have effect.
212 void setOffsets(double xOffset, double yOffset);
213 // So we can add metadata later. Most likely used after changeToDrawCoords
214 // has been called.
216 // Apply the transformations to everything. trans and toCentre are added,
217 // scale is multiplied.
218 void transformAll(const Point2D *trans = nullptr, Point2D *scale = nullptr,
219 const Point2D *toCentre = nullptr);
220 // Apply the transformations to the given point and return a new one.
221 Point2D transformPoint(const Point2D &pt, const Point2D *trans = nullptr,
222 Point2D *scale = nullptr,
223 const Point2D *toCentre = nullptr) const;
224 void calcDoubleBondLines(double offset, const Bond &bond, Point2D &l1s,
225 Point2D &l1f, Point2D &l2s, Point2D &l2f) const;
226 void bondInsideRing(const Bond &bond, double offset, Point2D &l2s,
227 Point2D &l2f) const;
228 void bondNonRing(const Bond &bond, double offset, Point2D &l2s,
229 Point2D &l2f) const;
230 // deal with terminal double bond between at1 and at2, either to atoms of
231 // degree 2 or 3.
232 void doubleBondTerminal(Atom *at1, Atom *at2, double offset, Point2D &l1s,
233 Point2D &l1f, Point2D &l2s, Point2D &l2f) const;
234 // assuming at[1-3] are atoms where at1 is bonded to at2 and at2 is bonded
235 // to at3, find the position of the at2 end of a double bond between at2
236 // and at3. If trunc, it'll be along the vector that bisects the two bonds
237 // on the inside, otherwise it's perpendicular to the bond from at1 to at2.
238 Point2D doubleBondEnd(unsigned int at1, unsigned int at2, unsigned int at3,
239 double offset, bool trunc) const;
240 void calcTripleBondLines(double offset, const Bond &bond, Point2D &l1s,
242 // find the vectors of any atoms singly bonded to atom that aren't
243 // otherAtom.
244 void findOtherBondVecs(const Atom *atom, const Atom *otherAtom,
245 std::vector<Point2D> &otherBondVecs) const;
248 // If doing a continuous highlight, add to points the 2 or 3 points that
249 // will be for the end1 end of the highlight. The final highlight will
250 // be a 4-6 sided polygon formed by calling this twice, with the ends in
251 // opposite order the second time.
252 void makeHighlightEnd(const Atom *end1, const Atom *end2, double lineWidth,
253 const std::vector<Atom *> &end1HighNbrs,
254 std::vector<Point2D> &points);
256
259 // For drawing reactions, padding needs to be 0 irrespective
260 // of what drawOptions_ does elsewhere, so it is copied from drawOptions_
261 // on construction, and is then immune to changes in the outer program.
263 std::vector<int> highlightAtoms_;
264 std::vector<int> highlightBonds_;
265 std::map<int, DrawColour> highlightAtomMap_;
266 std::map<int, DrawColour> highlightBondMap_;
267 std::vector<std::pair<DrawColour, DrawColour>> bondColours_;
268 std::map<int, double> highlightRadii_;
271 std::string legend_;
272
273 std::unique_ptr<RWMol> drawMol_;
275 // atCds_ are as extracted from the molecule, except that the y is
276 // inverted and drawOptions_.rotate is applied.
277 std::vector<Point2D> atCds_;
278 std::vector<std::unique_ptr<DrawShape>> bonds_;
279 std::vector<std::unique_ptr<DrawShape>> preShapes_;
280 std::vector<std::unique_ptr<DrawShape>> postShapes_;
281 std::vector<int> atomicNums_;
282 std::vector<std::pair<std::string, OrientType>> atomSyms_;
283 std::vector<std::unique_ptr<AtomSymbol>> atomLabels_;
284 std::vector<std::unique_ptr<DrawShape>> highlights_;
285 std::vector<std::unique_ptr<DrawAnnotation>> annotations_;
286 std::vector<std::unique_ptr<DrawAnnotation>> legends_;
287 std::vector<std::tuple<StringRect, OrientType, int>> radicals_;
288 std::vector<int> singleBondLines_;
289
290 // The total width and height of the canvas. Either or both may be
291 // -1 initially, in which case they will be calculated so the molecule
292 // can be drawn within it at a fixed scale (the so-called flexiCanvas).
294 // The width and height of the drawing area within the canvas. This is
295 // width_ and height_ less the drawOptions._padding round the outside.
296 // Will always be <= width_, height_.
298 // to allow for min and max font sizes, the font scale needs to be
299 // independent of the main scale.
302 // offsets are for drawing molecules in grids, for example.
303 double xOffset_ = 0.0, yOffset_ = 0.0;
304 double meanBondLength_ = 0.0;
305 // if there's a legend, we reserve a bit for it. molHeight_ is the
306 // bit for drawing the molecule, legendHeight_ the bit under that
307 // for the legend. In pixels.
310 // when drawing the atoms and bonds in an SVG, they are given a class
311 // via MolDraw2D's activeAtmIdx[12]_ and activeBndIdx. We don't always want
312 // them to start from 0 for atom/bond 0.
314 bool flexiCanvasX_ = false;
315 bool flexiCanvasY_ = false;
316};
317
318void centerMolForDrawing(RWMol &mol, int confId = 1);
319bool isLinearAtom(const Atom &atom, const std::vector<Point2D> &atCds);
320std::string getAtomListText(const Atom &atom);
322 const MolDrawOptions &drawOptions);
324 const Bond *bond, const MolDrawOptions &drawOptions,
325 const std::vector<int> &highlightBonds,
326 const std::map<int, DrawColour> &highlightBondMap,
327 const std::vector<int> &highlightAtoms,
328 const std::map<int, DrawColour> &highlightAtomMap);
330 const MolDrawOptions &drawOptions, int bond_idx,
331 const std::map<int, int> *highlight_linewidth_multipliers);
332
335 const Point2D &cds3);
336// return a point that is moveEnd moved so as not to clash with any of the
337// rects of a label. moveEnd to end2 are the coords of 2 ends of a bond.
339 const Point2D &end2, double padding,
340 const std::vector<std::shared_ptr<StringRect>> &rects, Point2D &moveEnd);
342 const std::vector<std::tuple<StringRect, OrientType, int>> &radicals,
343 double &xmin, double &xmax, double &ymin, double &ymax);
345 double &xmin, double &xmax, double &ymin, double &ymax);
347 const std::vector<int> &highlight_atoms,
348 std::vector<int> &highlight_bonds);
349// returns true if the vector at2->at1 points in roughly the opposite
350// direction to at3->at4. Basically, if the dot product is negative.
351bool areBondsTrans(const Point2D &at1, const Point2D &at2, const Point2D &at3,
352 const Point2D &at4);
353// returns true if the vector at2->at1 points is roughly linear with
354// direction of at3->at4. Basically, if the dot product is 1.0 within the
355// given tolerance.
357 const Point2D &at3, const Point2D &at4,
358 double tol = 1.0e-4);
359
360// find the nborNum'th neighbour of firstAtom that isn't secondAtom
362 int nborNum, const ROMol &mol);
363} // namespace MolDraw2D_detail
364} // namespace RDKit
365
366#endif // RDKIT_DRAWMOL_H
The class for representing atoms.
Definition Atom.h:75
class for representing a bond
Definition Bond.h:47
std::pair< std::string, OrientType > getAtomSymbolAndOrientation(const Atom &atom) const
void newBondLine(const Point2D &pt1, const Point2D &pt2, const DrawColour &col1, const DrawColour &col2, int atom1Idx, int atom2Idx, int bondIdx, const DashPattern &dashPattern)
void makeDativeBond(Bond *bond, double offset, const std::pair< DrawColour, DrawColour > &cols)
void calcDoubleBondLines(double offset, const Bond &bond, Point2D &l1s, Point2D &l1f, Point2D &l2s, Point2D &l2f) const
void bondInsideRing(const Bond &bond, double offset, Point2D &l2s, Point2D &l2f) const
Point2D getDrawCoords(const Point2D &atCds) const
std::pair< DrawColour, DrawColour > getBondColours(Bond *bond)
DrawMol & operator=(DrawMol &&)=delete
void doubleBondTerminal(Atom *at1, Atom *at2, double offset, Point2D &l1s, Point2D &l1f, Point2D &l2s, Point2D &l2f) const
std::unique_ptr< RWMol > drawMol_
Definition DrawMol.h:273
std::map< int, DrawColour > highlightAtomMap_
Definition DrawMol.h:265
std::vector< int > singleBondLines_
Definition DrawMol.h:288
void setTransformation(const DrawMol &sourceMol)
std::vector< std::unique_ptr< AtomSymbol > > atomLabels_
Definition DrawMol.h:283
std::string getAtomSymbol(const Atom &atom, OrientType orientation) const
std::map< int, double > highlightRadii_
Definition DrawMol.h:268
void shrinkToFit(bool withPadding=true)
std::map< int, DrawColour > highlightBondMap_
Definition DrawMol.h:266
std::vector< std::pair< std::string, OrientType > > atomSyms_
Definition DrawMol.h:282
void drawRadicals(MolDraw2D &drawer) const
void setScale(double newScale, double newFontScale, bool ignoreFontLimits=true)
void bondNonRing(const Bond &bond, double offset, Point2D &l2s, Point2D &l2f) const
DrawColour getColour(int atom_idx) const
std::vector< std::unique_ptr< DrawAnnotation > > annotations_
Definition DrawMol.h:285
Point2D getAtomCoords(int atnum) const
std::vector< int > highlightAtoms_
Definition DrawMol.h:263
DrawMol(const DrawMol &)=delete
Point2D transformPoint(const Point2D &pt, const Point2D *trans=nullptr, Point2D *scale=nullptr, const Point2D *toCentre=nullptr) const
void makeWedgedBond(Bond *bond, const std::pair< DrawColour, DrawColour > &cols)
std::vector< std::unique_ptr< DrawShape > > preShapes_
Definition DrawMol.h:279
std::vector< std::pair< DrawColour, DrawColour > > bondColours_
Definition DrawMol.h:267
void setOffsets(double xOffset, double yOffset)
std::vector< std::unique_ptr< DrawAnnotation > > legends_
Definition DrawMol.h:286
Point2D doubleBondEnd(unsigned int at1, unsigned int at2, unsigned int at3, double offset, bool trunc) const
void makeStandardBond(Bond *bond, double doubleBondOffset)
void makeTripleBondLines(Bond *bond, double doubleBondOffset, const std::pair< DrawColour, DrawColour > &cols)
Point2D getAtomCoords(const Point2D &screenCds) const
double getNoteStartAngle(const Atom *atom) const
void makeAtomEllipseHighlights(double lineWidth)
const MolDrawOptions & drawOptions_
Definition DrawMol.h:257
std::vector< std::tuple< StringRect, OrientType, int > > radicals_
Definition DrawMol.h:287
void makeQueryBond(Bond *bond, double doubleBondOffset)
DrawMol(const ROMol &mol, const std::string &legend, int width, int height, const MolDrawOptions &drawOptions, DrawText &textDrawer, const std::vector< int > *highlightAtoms=nullptr, const std::vector< int > *highlightBonds=nullptr, const std::map< int, DrawColour > *highlightAtomMap=nullptr, const std::map< int, DrawColour > *highlightBondMap=nullptr, const std::vector< std::pair< DrawColour, DrawColour > > *bondColours=nullptr, const std::map< int, double > *highlight_radii=nullptr, bool includeAnnotations=true, int confId=-1, bool isReactionMol=false)
DrawMol & operator=(const DrawMol &)=delete
std::vector< std::unique_ptr< DrawShape > > postShapes_
Definition DrawMol.h:280
std::vector< Point2D > atCds_
Definition DrawMol.h:277
int doesRectClash(const StringRect &rect, double padding) const
std::vector< std::unique_ptr< DrawShape > > highlights_
Definition DrawMol.h:284
std::vector< int > atomicNums_
Definition DrawMol.h:281
std::vector< int > highlightBonds_
Definition DrawMol.h:264
void draw(MolDraw2D &drawer) const
void calcTripleBondLines(double offset, const Bond &bond, Point2D &l1s, Point2D &l1f, Point2D &l2s, Point2D &l2f)
void transformAll(const Point2D *trans=nullptr, Point2D *scale=nullptr, const Point2D *toCentre=nullptr)
void makeContinuousHighlights(double scale)
void findOtherBondVecs(const Atom *atom, const Atom *otherAtom, std::vector< Point2D > &otherBondVecs) const
void getDrawTransformers(Point2D &trans, Point2D &scale, Point2D &toCentre) const
OrientType calcRadicalRect(const Atom *atom, StringRect &rad_rect) const
void makeZeroBond(Bond *bond, const std::pair< DrawColour, DrawColour > &cols, const DashPattern &dashPattern)
void makeHighlightEnd(const Atom *end1, const Atom *end2, double lineWidth, const std::vector< Atom * > &end1HighNbrs, std::vector< Point2D > &points)
std::vector< std::unique_ptr< DrawShape > > bonds_
Definition DrawMol.h:278
void makeBondHighlightLines(double lineWidth, double scale)
int doesNoteClash(const DrawAnnotation &annot) const
void calcAnnotationPosition(const Atom *atom, DrawAnnotation &annot) const
void calcAnnotationPosition(const Bond *bond, DrawAnnotation &annot) const
Point2D getDrawCoords(int atnum) const
OrientType getAtomOrientation(const Atom &atom) const
Point2D getDrawCoords(const Point2D &atCds, const Point2D &trans, const Point2D &scaleFactor, const Point2D &toCentre) const
virtual void extractHighlights(double scale)
void makeDoubleBondLines(Bond *bond, double doubleBondOffset, const std::pair< DrawColour, DrawColour > &cols)
void adjustBondEndsForLabels(int begAtIdx, int endAtIdx, Point2D &begCds, Point2D &endCds) const
void initDrawMolecule(const ROMol &mol)
DrawMol(int width, int height, const MolDrawOptions &drawOptions, DrawText &textDrawer, double xmin, double xmax, double ymin, double ymax, double scale, double fontscale)
void makeWavyBond(Bond *bond, double offset, const std::pair< DrawColour, DrawColour > &cols)
MolDraw2D is the base class for doing 2D renderings of molecules.
Definition MolDraw2D.h:47
RWMol is a molecule class that is intended to be edited.
Definition RWMol.h:32
Point2D calcPerpendicular(const Point2D &cds1, const Point2D &cds2)
Point2D calcInnerPerpendicular(const Point2D &cds1, const Point2D &cds2, const Point2D &cds3)
void findRectExtremes(const StringRect &rect, const TextAlignType &align, double &xmin, double &xmax, double &ymin, double &ymax)
bool isLinearAtom(const Atom &atom, const std::vector< Point2D > &atCds)
void findRadicalExtremes(const std::vector< std::tuple< StringRect, OrientType, int > > &radicals, double &xmin, double &xmax, double &ymin, double &ymax)
void centerMolForDrawing(RWMol &mol, int confId=1)
bool areBondsTrans(const Point2D &at1, const Point2D &at2, const Point2D &at3, const Point2D &at4)
const Atom * otherNeighbor(const Atom *firstAtom, const Atom *secondAtom, int nborNum, const ROMol &mol)
void adjustBondEndForString(const Point2D &end2, double padding, const std::vector< std::shared_ptr< StringRect > > &rects, Point2D &moveEnd)
bool areBondsParallel(const Point2D &at1, const Point2D &at2, const Point2D &at3, const Point2D &at4, double tol=1.0e-4)
DrawColour getColourByAtomicNum(int atomicNum, const MolDrawOptions &drawOptions)
double getHighlightBondWidth(const MolDrawOptions &drawOptions, int bond_idx, const std::map< int, int > *highlight_linewidth_multipliers)
DrawColour getHighlightBondColour(const Bond *bond, const MolDrawOptions &drawOptions, const std::vector< int > &highlightBonds, const std::map< int, DrawColour > &highlightBondMap, const std::vector< int > &highlightAtoms, const std::map< int, DrawColour > &highlightAtomMap)
void getBondHighlightsForAtoms(const ROMol &mol, const std::vector< int > &highlight_atoms, std::vector< int > &highlight_bonds)
std::string getAtomListText(const Atom &atom)
Std stuff.
std::vector< double > DashPattern
bool rdvalue_is(const RDValue_cast_t)