RDKit
Open-source cheminformatics and machine learning.
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 
29 namespace RDKit {
30 
31 class Atom;
32 class Bond;
33 class ROMol;
34 class RWMol;
35 class DrawText;
36 
37 namespace MolDraw2D_detail {
38 
39 class 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);
110  void extractBonds();
111  virtual void extractHighlights(double scale);
122  // extractCloseContacts is to show where 2 atoms are drawn too close together
123  // and so needs the final drawing coords. It is therefore called at the end
124  // of changeToDrawCoords() and any necessary DrawShapePolyLines added to
125  // postShapes_ in drawCoords.
128  void findExtremes();
130  void draw(MolDraw2D &drawer) const;
131  void drawRadicals(MolDraw2D &drawer) const;
133  // reduce width_ and height_ to just accomodate the Xrange_ and YRange_
134  // at the current scale. Recentres everything. So the DrawMol takes up
135  // no more screen real estate than it needs.
136  void shrinkToFit(bool withPadding = true);
137 
138  // adds LaTeX-like annotation for super- and sub-script.
139  std::pair<std::string, OrientType> getAtomSymbolAndOrientation(
140  const Atom &atom) const;
141  std::string getAtomSymbol(const Atom &atom, OrientType orientation) const;
142  OrientType getAtomOrientation(const Atom &atom) const;
143 
144  // if there's a legend, partition the height_ to accommodate it
146  // extractLegend is different from all the other extract... functions
147  // in that it needs to be called once the final scale has been found
148  // by calculateScale.
150 
152  void makeStandardBond(Bond *bond, double doubleBondOffset);
153  void makeQueryBond(Bond *bond, double doubleBondOffset);
154  void makeDoubleBondLines(Bond *bond, double doubleBondOffset,
155  const std::pair<DrawColour, DrawColour> &cols);
156  void makeTripleBondLines(Bond *bond, double doubleBondOffset,
157  const std::pair<DrawColour, DrawColour> &cols);
158  void makeWedgedBond(Bond *bond,
159  const std::pair<DrawColour, DrawColour> &cols);
160  void makeWavyBond(Bond *bond, double offset,
161  const std::pair<DrawColour, DrawColour> &cols);
162  void makeDativeBond(Bond *bond,
163  const std::pair<DrawColour, DrawColour> &cols);
164  void makeZeroBond(Bond *bond, const std::pair<DrawColour, DrawColour> &cols,
165  const DashPattern &dashPattern);
166  void adjustBondEndsForLabels(int begAtIdx, int endAtIdx, Point2D &begCds,
167  Point2D &endCds) const;
168  void newBondLine(const Point2D &pt1, const Point2D &pt2,
169  const DrawColour &col1, const DrawColour &col2, int atom1Idx,
170  int atom2Idx, int bondIdx, const DashPattern &dashPattern);
171  std::pair<DrawColour, DrawColour> getBondColours(Bond *bond);
172  void makeContinuousHighlights(double scale);
174  void makeAtomEllipseHighlights(double lineWidth);
175  // These are the lines for continuous highlights, that are
176  // now filled trapezoids rather than fat simple lines.
177  void makeBondHighlightLines(double lineWidth, double scale);
178  void calcAnnotationPosition(const Atom *atom, DrawAnnotation &annot) const;
179  void calcAnnotationPosition(const Bond *bond, DrawAnnotation &annot) const;
180  double getNoteStartAngle(const Atom *atom) const;
181  void calcMolNotePosition(const std::vector<Point2D> atCds,
182  DrawAnnotation &annot) const;
183  // see if the note will clash with anything else drawn on the molecule.
184  // Returns 0 if no clash, 1-4 if there is a clash, denoting what clashed.
185  int doesNoteClash(const DrawAnnotation &annot) const;
186  int doesRectClash(const StringRect &rect, double padding) const;
187  OrientType calcRadicalRect(const Atom *atom, StringRect &rad_rect) const;
188  void getDrawTransformers(Point2D &trans, Point2D &scale,
189  Point2D &toCentre) const;
190  // Given some coords in molecule space (angstrom, probably) return the
191  // screen coords.
192  Point2D getDrawCoords(const Point2D &atCds, const Point2D &trans,
193  const Point2D &scaleFactor,
194  const Point2D &toCentre) const;
195  Point2D getDrawCoords(const Point2D &atCds) const;
196  Point2D getDrawCoords(int atnum) const;
197  // and the other way.
198  Point2D getAtomCoords(const Point2D &screenCds) const;
199  Point2D getAtomCoords(int atnum) const;
200  double getScale() const { return scale_; }
201  double getFontScale() const { return fontScale_; }
202  // More often than not, newScale and newFontScale will be the same,
203  // but not if minFontScale of maxFontScale have become involved.
204  // The newFontScale will be used without checking the min and max.
205  void setScale(double newScale, double newFontScale,
206  bool ignoreFontLimits = true);
207  // Set all the transformation details from the incoming DrawMol to this
208  // one, so they can be overlaid properly. Doesn't change the offsets.
209  void setTransformation(const DrawMol &sourceMol);
210 
211  // For drawing into a grid, for example. Must be set before
212  // changeToDrawCoords is called for it to have effect.
213  void setOffsets(double xOffset, double yOffset);
214  // So we can add metadata later. Most likely used after changeToDrawCoords
215  // has been called.
217  // Apply the transformations to everything. trans and toCentre are added,
218  // scale is multiplied.
219  void transformAll(const Point2D *trans = nullptr, Point2D *scale = nullptr,
220  const Point2D *toCentre = nullptr);
221  // Apply the transformations to the given point and return a new one.
222  Point2D transformPoint(const Point2D &pt, const Point2D *trans = nullptr,
223  Point2D *scale = nullptr,
224  const Point2D *toCentre = nullptr) const;
225  void calcDoubleBondLines(double offset, const Bond &bond, Point2D &l1s,
226  Point2D &l1f, Point2D &l2s, Point2D &l2f) const;
227  void bondInsideRing(const Bond &bond, double offset, Point2D &l2s,
228  Point2D &l2f) const;
229  void bondNonRing(const Bond &bond, double offset, Point2D &l2s,
230  Point2D &l2f) const;
231  // deal with terminal double bond between at1 and at2, either to atoms of
232  // degree 2 or 3.
233  void doubleBondTerminal(Atom *at1, Atom *at2, double offset, Point2D &l1s,
234  Point2D &l1f, Point2D &l2s, Point2D &l2f) const;
235  // assuming at[1-3] are atoms where at1 is bonded to at2 and at2 is bonded
236  // to at3, find the position of the at2 end of a double bond between at2
237  // and at3. If trunc, it'll be along the vector that bisects the two bonds on
238  // the inside, otherwise it's perpendicular to the bond from at1 to at2.
239  Point2D doubleBondEnd(unsigned int at1, unsigned int at2, unsigned int at3,
240  double offset, bool trunc) const;
241  void calcTripleBondLines(double offset, const Bond &bond, Point2D &l1s,
242  Point2D &l1f, Point2D &l2s, Point2D &l2f);
243  // find the vectors of any atoms singly bonded to atom that aren't 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);
255  DrawColour getColour(int atom_idx) const;
256 
259  std::vector<int> highlightAtoms_;
260  std::vector<int> highlightBonds_;
261  std::map<int, DrawColour> highlightAtomMap_;
262  std::map<int, DrawColour> highlightBondMap_;
263  std::vector<std::pair<DrawColour, DrawColour>> bondColours_;
264  std::map<int, double> highlightRadii_;
267  std::string legend_;
268 
269  std::unique_ptr<RWMol> drawMol_;
270  int confId_;
271  // atCds_ are as extracted from the molecule, except that the y is
272  // inverted and drawOptions_.rotate is applied.
273  std::vector<Point2D> atCds_;
274  std::vector<std::unique_ptr<DrawShape>> bonds_;
275  std::vector<std::unique_ptr<DrawShape>> preShapes_;
276  std::vector<std::unique_ptr<DrawShape>> postShapes_;
277  std::vector<int> atomicNums_;
278  std::vector<std::pair<std::string, OrientType>> atomSyms_;
279  std::vector<std::unique_ptr<AtomSymbol>> atomLabels_;
280  std::vector<std::unique_ptr<DrawShape>> highlights_;
281  std::vector<std::unique_ptr<DrawAnnotation>> annotations_;
282  std::vector<std::unique_ptr<DrawAnnotation>> legends_;
283  std::vector<std::tuple<StringRect, OrientType, int>> radicals_;
284  std::vector<int> singleBondLines_;
285 
287  // to allow for min and max font sizes, the font scale needs to be
288  // independent of the main scale.
291  // offsets are for drawing molecules in grids, for example.
292  double xOffset_ = 0.0, yOffset_ = 0.0;
293  double meanBondLength_ = 0.0;
294  // if there's a legend, we reserve a bit for it.
296  bool drawingInitialised_ = false;
297  // when drawing the atoms and bonds in an SVG, they are given a class
298  // via MolDraw2D's activeAtmIdx[12]_ and activeBndIdx. We don't always want
299  // them to start from 0 for atom/bond 0.
301  bool flexiCanvasX_ = false;
302  bool flexiCanvasY_ = false;
303 };
304 
305 void centerMolForDrawing(RWMol &mol, int confId = 1);
307 bool isLinearAtom(const Atom &atom, const std::vector<Point2D> &atCds);
308 std::string getAtomListText(const Atom &atom);
310  const MolDrawOptions &drawOptions);
312  int bondIdx, const MolDrawOptions &drawOptions,
313  const std::vector<int> &highlightBonds,
314  const std::map<int, DrawColour> &highlightBondMap);
316  const MolDrawOptions &drawOptions, int bond_idx,
317  const std::map<int, int> *highlight_linewidth_multipliers);
318 
319 Point2D calcPerpendicular(const Point2D &cds1, const Point2D &cds2);
321  const Point2D &cds3);
322 // return a point that is moveEnd moved so as not to clash with any of the
323 // rects of a label. moveEnd to end2 are the coords of 2 ends of a bond.
325  const Point2D &end2, double padding,
326  const std::vector<std::shared_ptr<StringRect>> &rects, Point2D &moveEnd);
328  const std::vector<std::tuple<StringRect, OrientType, int>> &radicals,
329  double &xmin, double &xmax, double &ymin, double &ymax);
330 void findRectExtremes(const StringRect &rect, const TextAlignType &align,
331  double &xmin, double &xmax, double &ymin, double &ymax);
333  const std::vector<int> &highlight_atoms,
334  std::vector<int> &highlight_bonds);
335 // returns true if the vector at2->at1 points in roughly the opposite
336 // direction to at3->at4. Basically, if the dot product is negative.
337 bool areBondsTrans(const Point2D &at1, const Point2D &at2, const Point2D &at3,
338  const Point2D &at4);
339 
340 // find the nborNum'th neighbour of firstAtom that isn't secondAtom
341 const Atom *otherNeighbor(const Atom *firstAtom, const Atom *secondAtom,
342  int nborNum, const ROMol &mol);
343 } // namespace MolDraw2D_detail
344 } // namespace RDKit
345 
346 #endif // RDKIT_DRAWMOL_H
The class for representing atoms.
Definition: Atom.h:68
class for representing a bond
Definition: Bond.h:47
void newBondLine(const Point2D &pt1, const Point2D &pt2, const DrawColour &col1, const DrawColour &col2, int atom1Idx, int atom2Idx, int bondIdx, const DashPattern &dashPattern)
std::pair< std::string, OrientType > getAtomSymbolAndOrientation(const Atom &atom) const
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
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:269
std::map< int, DrawColour > highlightAtomMap_
Definition: DrawMol.h:261
std::vector< int > singleBondLines_
Definition: DrawMol.h:284
void setTransformation(const DrawMol &sourceMol)
std::vector< std::unique_ptr< AtomSymbol > > atomLabels_
Definition: DrawMol.h:279
std::string getAtomSymbol(const Atom &atom, OrientType orientation) const
std::map< int, double > highlightRadii_
Definition: DrawMol.h:264
void shrinkToFit(bool withPadding=true)
std::map< int, DrawColour > highlightBondMap_
Definition: DrawMol.h:262
std::vector< std::pair< std::string, OrientType > > atomSyms_
Definition: DrawMol.h:278
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:281
Point2D getAtomCoords(int atnum) const
DrawMol & operator=(const DrawMol &)=delete
std::vector< int > highlightAtoms_
Definition: DrawMol.h:259
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:275
std::pair< DrawColour, DrawColour > getBondColours(Bond *bond)
std::vector< std::pair< DrawColour, DrawColour > > bondColours_
Definition: DrawMol.h:263
void setOffsets(double xOffset, double yOffset)
std::vector< std::unique_ptr< DrawAnnotation > > legends_
Definition: DrawMol.h:282
DrawMol & operator=(DrawMol &&)=delete
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
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)
void makeDativeBond(Bond *bond, const std::pair< DrawColour, DrawColour > &cols)
void makeAtomEllipseHighlights(double lineWidth)
const MolDrawOptions & drawOptions_
Definition: DrawMol.h:257
std::vector< std::tuple< StringRect, OrientType, int > > radicals_
Definition: DrawMol.h:283
void makeQueryBond(Bond *bond, double doubleBondOffset)
std::vector< std::unique_ptr< DrawShape > > postShapes_
Definition: DrawMol.h:276
std::vector< Point2D > atCds_
Definition: DrawMol.h:273
int doesRectClash(const StringRect &rect, double padding) const
std::vector< std::unique_ptr< DrawShape > > highlights_
Definition: DrawMol.h:280
std::vector< int > atomicNums_
Definition: DrawMol.h:277
std::vector< int > highlightBonds_
Definition: DrawMol.h:260
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:274
void makeBondHighlightLines(double lineWidth, double scale)
void calcMolNotePosition(const std::vector< Point2D > atCds, DrawAnnotation &annot) const
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 adjustBondEndForString(const Point2D &end2, double padding, const std::vector< std::shared_ptr< StringRect >> &rects, Point2D &moveEnd)
void prepareStereoGroups(RWMol &mol)
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)
DrawColour getColourByAtomicNum(int atomicNum, const MolDrawOptions &drawOptions)
const Atom * otherNeighbor(const Atom *firstAtom, const Atom *secondAtom, int nborNum, const ROMol &mol)
DrawColour getHighlightBondColour(int bondIdx, const MolDrawOptions &drawOptions, const std::vector< int > &highlightBonds, const std::map< int, DrawColour > &highlightBondMap)
double getHighlightBondWidth(const MolDrawOptions &drawOptions, int bond_idx, const std::map< int, int > *highlight_linewidth_multipliers)
void getBondHighlightsForAtoms(const ROMol &mol, const std::vector< int > &highlight_atoms, std::vector< int > &highlight_bonds)
std::string getAtomListText(const Atom &atom)
Std stuff.
Definition: Abbreviations.h:19
std::vector< double > DashPattern