RDKit
Open-source cheminformatics and machine learning.
DrawShape.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 
13 // A set of shapes used in 2D drawing.d Not part of the public API.
14 
15 #ifndef RDKIT_DRAWSHAPE_H
16 #define RDKIT_DRAWSHAPE_H
17 
18 #include <vector>
19 
20 #include <Geometry/point.h>
22 
23 namespace RDKit {
24 
25 class MolDraw2D;
27 const DashPattern dots{2.0, 6.0};
28 const DashPattern dashes{6.0, 4.0};
29 const DashPattern shortDashes{2.0, 2.0};
30 
31 namespace MolDraw2D_detail {
32 
33 struct StringRect;
34 
35 class DrawShape {
36  public:
37  DrawShape(const std::vector<Point2D> &points, double lineWidth = 2.0,
38  bool scaleLineWidth = false,
39  DrawColour lineColour = DrawColour(0, 0, 0), bool fill = false,
40  int atom1 = -1, int atom2 = -1, int bond = -1);
41  DrawShape(const DrawShape &) = delete;
42  DrawShape(DrawShape &&) = delete;
43  virtual ~DrawShape() = default;
44  DrawShape &operator=(const DrawShape &) = delete;
45  DrawShape &operator=(DrawShape &&) = delete;
46 
47  void draw(MolDraw2D &drawer);
48  virtual void myDraw(MolDraw2D &drawer) const = 0;
49  virtual void findExtremes(double &xmin, double &xmax, double &ymin,
50  double &ymax) const;
51  virtual void scale(const Point2D &scale_factor);
52  virtual void move(const Point2D &trans);
53  virtual bool doesRectClash(const StringRect &rect, double padding) const;
54 
55  std::vector<Point2D> points_;
56  double lineWidth_;
59  bool fill_;
61 };
62 
63 class DrawShapeArrow : public DrawShape {
64  public:
65  DrawShapeArrow(const std::vector<Point2D> &points, double lineWidth = 2.0,
66  bool scaleLineWidth = false,
67  DrawColour lineColour = DrawColour(0, 0, 0), bool fill = false,
68  int atom1 = -1, int atom2 = -1, int bond = -1,
69  double frac = 0.2, double angle = M_PI / 6);
70  DrawShapeArrow(const DrawShapeArrow &) = delete;
72  ~DrawShapeArrow() = default;
75  void myDraw(MolDraw2D &drawer) const override;
76  bool doesRectClash(const StringRect &rect, double padding) const override;
77 
78  double frac_;
79  double angle_;
80 };
81 
82 class DrawShapeEllipse : public DrawShape {
83  public:
84  // Points should be size 2 - the first entry is the centre, the second
85  // gives the x and y radii of the ellipse.
86  DrawShapeEllipse(const std::vector<Point2D> &points, double lineWidth = 2,
87  bool scaleLineWidth = false,
88  DrawColour lineColour = DrawColour(0, 0, 0),
89  bool fill = false, int atom1 = -1);
92  ~DrawShapeEllipse() = default;
95  void myDraw(MolDraw2D &drawer) const override;
96  void findExtremes(double &xmin, double &xmax, double &ymin,
97  double &ymax) const override;
98  void move(const Point2D &trans) override;
99  bool doesRectClash(const StringRect &rect, double padding) const override;
100 };
101 
103  public:
104  DrawShapeSimpleLine(const std::vector<Point2D> &points,
105  double lineWidth = 2.0, bool scaleLineWidth = false,
106  DrawColour lineColour = DrawColour(0, 0, 0),
107  int atom1 = -1, int atom2 = -1, int bond = -1,
108  DashPattern dashPattern = noDash);
111  ~DrawShapeSimpleLine() = default;
114  void myDraw(MolDraw2D &drawer) const override;
115  bool doesRectClash(const StringRect &rect, double padding) const override;
116 
118 };
119 
120 class DrawShapePolyLine : public DrawShape {
121  public:
122  DrawShapePolyLine(const std::vector<Point2D> &points, double lineWidth = 2.0,
123  bool scaleLineWidth = false,
124  DrawColour lineColour = DrawColour(0, 0, 0),
125  bool fill = false, int atom1 = -1, int atom2 = -1,
126  int bond = -1, DashPattern dashPattern = noDash);
129  ~DrawShapePolyLine() = default;
132  void myDraw(MolDraw2D &drawer) const override;
133  bool doesRectClash(const StringRect &rect, double padding) const override;
134 
136 };
137 
139  public:
140  DrawShapeSolidWedge(const std::vector<Point2D> points, const DrawColour &col1,
141  const DrawColour &col2, bool splitBonds,
142  std::vector<Point2D> &otherBondVecs,
143  double lineWidth = 1.0, int atom1 = -1, int atom2 = -1,
144  int bond = -1);
147  ~DrawShapeSolidWedge() = default;
153  void myDraw(MolDraw2D &drawer) const override;
154  bool doesRectClash(const StringRect &rect, double padding) const override;
155  // if otherBondVecs_.size() > 2, then we only want the two vecs with the
156  // widest angle between them.
158  // if there are 2 otherBondVecs_ (assuming we've already trimmed down to 2 if
159  // necessary) make sure the first is on the points_[1] side, the second on
160  // the points_[2] side, so that the merging of the triangles to the bond
161  // lines is correct.
163 
166  std::vector<Point2D> otherBondVecs_;
167 };
168 
170  public:
171  // oneLessDash means that the last dash at the fat end of the wedge
172  // isn't drawn. The wedge will be as fat as it would have been with
173  // the extra dash. This is so that bonds coming out of the fat end
174  // of the wedge aren't directly incident on a dash.
175  DrawShapeDashedWedge(const std::vector<Point2D> points,
176  const DrawColour &col1, const DrawColour &col2,
177  bool oneLessDash = true, double lineWidth = 1.0,
178  int atom1 = -1, int atom2 = -1, int bond = -1);
184  void buildLines();
185  void myDraw(MolDraw2D &drawer) const override;
186  void scale(const Point2D &scale_factor) override;
187  void move(const Point2D &trans) override;
188  void findExtremes(double &xmin, double &xmax, double &ymin,
189  double &ymax) const override;
190  bool doesRectClash(const StringRect &rect, double padding) const override;
191 
194  std::vector<DrawColour> lineColours_;
195  // for when we re-create the lines, such as after scaling, this is
196  // the initial points[0-2] from the c'tor.
198 };
199 
200 class DrawShapeWavyLine : public DrawShape {
201  public:
202  DrawShapeWavyLine(const std::vector<Point2D> points, double lineWidth = 2.0,
203  bool scaleLineWidth = false,
204  const DrawColour &col1 = DrawColour(0, 0, 0),
205  const DrawColour &col2 = DrawColour(0, 0, 0),
206  double offset = 0.05, int atom1 = -1, int atom2 = -1,
207  int bond = -1);
210  ~DrawShapeWavyLine() = default;
213  void myDraw(MolDraw2D &drawer) const override;
214  void scale(const Point2D &scaleFactor) override;
215  bool doesRectClash(const StringRect &rect, double padding) const override;
216 
218  double offset_;
219 };
220 
221 class DrawShapeArc : public DrawShape {
222  public:
223  // draw the arc of an ellipse between ang1 and ang2. Note that 0 is
224  // at 3 o-clock and 90 at 12 o'clock as you'd expect from your maths.
225  // ang2 must be > ang1 - it won't draw backwards. Angles in degrees,
226  // between 0 and 360.0.
227  // Points should be size 2 - the first entry is the centre, the second
228  // gives the x and y radii of the ellipse.
229  DrawShapeArc(const std::vector<Point2D> points, double ang1, double ang2,
230  double lineWidth = 2.0, bool scaleLineWidth = false,
231  const DrawColour &col1 = DrawColour(0, 0, 0), bool fill = false,
232  int atom1 = -1);
233  DrawShapeArc(const DrawShapeArc &) = delete;
235  ~DrawShapeArc() = default;
236  DrawShapeArc &operator=(const DrawShapeArc &) = delete;
238 
239  void myDraw(MolDraw2D &drawer) const override;
240  void findExtremes(double &xmin, double &xmax, double &ymin,
241  double &ymax) const override;
242  void move(const Point2D &trans) override;
243  bool doesRectClash(const StringRect &rect, double padding) const override;
244 
245  double ang1_, ang2_;
246 };
247 
248 } // namespace MolDraw2D_detail
249 } // namespace RDKit
250 
251 #endif // RDKIT_DRAWSHAPE_H
#define M_PI
Definition: MMFF/Params.h:27
DrawShapeArc(DrawShapeArc &&)=delete
DrawShapeArc & operator=(DrawShapeArc &&)=delete
DrawShapeArc & operator=(const DrawShapeArc &)=delete
bool doesRectClash(const StringRect &rect, double padding) const override
void move(const Point2D &trans) override
DrawShapeArc(const DrawShapeArc &)=delete
DrawShapeArc(const std::vector< Point2D > points, double ang1, double ang2, double lineWidth=2.0, bool scaleLineWidth=false, const DrawColour &col1=DrawColour(0, 0, 0), bool fill=false, int atom1=-1)
void myDraw(MolDraw2D &drawer) const override
void findExtremes(double &xmin, double &xmax, double &ymin, double &ymax) const override
DrawShapeArrow & operator=(const DrawShapeArrow &)=delete
DrawShapeArrow & operator=(DrawShapeArrow &&)=delete
bool doesRectClash(const StringRect &rect, double padding) const override
void myDraw(MolDraw2D &drawer) const override
DrawShapeArrow(const DrawShapeArrow &)=delete
DrawShapeArrow(const std::vector< Point2D > &points, double lineWidth=2.0, bool scaleLineWidth=false, DrawColour lineColour=DrawColour(0, 0, 0), bool fill=false, int atom1=-1, int atom2=-1, int bond=-1, double frac=0.2, double angle=M_PI/6)
DrawShapeArrow(DrawShapeArrow &&)=delete
void findExtremes(double &xmin, double &xmax, double &ymin, double &ymax) const override
void myDraw(MolDraw2D &drawer) const override
void scale(const Point2D &scale_factor) override
DrawShapeDashedWedge & operator=(const DrawShapeDashedWedge &)=delete
void move(const Point2D &trans) override
bool doesRectClash(const StringRect &rect, double padding) const override
std::vector< DrawColour > lineColours_
Definition: DrawShape.h:194
DrawShapeDashedWedge(const DrawShapeDashedWedge &)=delete
DrawShapeDashedWedge & operator=(DrawShapeDashedWedge &&)=delete
DrawShapeDashedWedge(DrawShapeDashedWedge &&)=delete
DrawShapeDashedWedge(const std::vector< Point2D > points, const DrawColour &col1, const DrawColour &col2, bool oneLessDash=true, double lineWidth=1.0, int atom1=-1, int atom2=-1, int bond=-1)
DrawShapeEllipse(const std::vector< Point2D > &points, double lineWidth=2, bool scaleLineWidth=false, DrawColour lineColour=DrawColour(0, 0, 0), bool fill=false, int atom1=-1)
DrawShapeEllipse(DrawShapeEllipse &&)=delete
void move(const Point2D &trans) override
bool doesRectClash(const StringRect &rect, double padding) const override
void myDraw(MolDraw2D &drawer) const override
DrawShapeEllipse(const DrawShapeEllipse &)=delete
DrawShapeEllipse & operator=(DrawShapeEllipse &&)=delete
DrawShapeEllipse & operator=(const DrawShapeEllipse &)=delete
void findExtremes(double &xmin, double &xmax, double &ymin, double &ymax) const override
DrawShapePolyLine(const std::vector< Point2D > &points, double lineWidth=2.0, bool scaleLineWidth=false, DrawColour lineColour=DrawColour(0, 0, 0), bool fill=false, int atom1=-1, int atom2=-1, int bond=-1, DashPattern dashPattern=noDash)
bool doesRectClash(const StringRect &rect, double padding) const override
void myDraw(MolDraw2D &drawer) const override
DrawShapePolyLine & operator=(DrawShapePolyLine &&)=delete
DrawShapePolyLine(const DrawShapePolyLine &)=delete
DrawShapePolyLine(DrawShapePolyLine &&)=delete
DrawShapePolyLine & operator=(const DrawShapePolyLine &)=delete
DrawShapeSimpleLine & operator=(DrawShapeSimpleLine &&)=delete
DrawShapeSimpleLine(const std::vector< Point2D > &points, double lineWidth=2.0, bool scaleLineWidth=false, DrawColour lineColour=DrawColour(0, 0, 0), int atom1=-1, int atom2=-1, int bond=-1, DashPattern dashPattern=noDash)
bool doesRectClash(const StringRect &rect, double padding) const override
DrawShapeSimpleLine & operator=(const DrawShapeSimpleLine &)=delete
DrawShapeSimpleLine(DrawShapeSimpleLine &&)=delete
DrawShapeSimpleLine(const DrawShapeSimpleLine &)=delete
void myDraw(MolDraw2D &drawer) const override
DrawShapeSolidWedge & operator=(const DrawShapeSolidWedge &)=delete
bool doesRectClash(const StringRect &rect, double padding) const override
void myDraw(MolDraw2D &drawer) const override
DrawShapeSolidWedge(const std::vector< Point2D > points, const DrawColour &col1, const DrawColour &col2, bool splitBonds, std::vector< Point2D > &otherBondVecs, double lineWidth=1.0, int atom1=-1, int atom2=-1, int bond=-1)
DrawShapeSolidWedge(DrawShapeSolidWedge &&)=delete
DrawShapeSolidWedge & operator=(DrawShapeSolidWedge &&)=delete
DrawShapeSolidWedge(const DrawShapeSolidWedge &)=delete
bool doesRectClash(const StringRect &rect, double padding) const override
DrawShapeWavyLine(const DrawShapeWavyLine &)=delete
DrawShapeWavyLine & operator=(DrawShapeWavyLine &&)=delete
void myDraw(MolDraw2D &drawer) const override
DrawShapeWavyLine(DrawShapeWavyLine &&)=delete
void scale(const Point2D &scaleFactor) override
DrawShapeWavyLine & operator=(const DrawShapeWavyLine &)=delete
DrawShapeWavyLine(const std::vector< Point2D > points, double lineWidth=2.0, bool scaleLineWidth=false, const DrawColour &col1=DrawColour(0, 0, 0), const DrawColour &col2=DrawColour(0, 0, 0), double offset=0.05, int atom1=-1, int atom2=-1, int bond=-1)
virtual void myDraw(MolDraw2D &drawer) const =0
virtual void findExtremes(double &xmin, double &xmax, double &ymin, double &ymax) const
virtual bool doesRectClash(const StringRect &rect, double padding) const
void draw(MolDraw2D &drawer)
std::vector< Point2D > points_
Definition: DrawShape.h:55
DrawShape(const std::vector< Point2D > &points, double lineWidth=2.0, bool scaleLineWidth=false, DrawColour lineColour=DrawColour(0, 0, 0), bool fill=false, int atom1=-1, int atom2=-1, int bond=-1)
DrawShape & operator=(const DrawShape &)=delete
virtual void scale(const Point2D &scale_factor)
virtual void move(const Point2D &trans)
DrawShape(const DrawShape &)=delete
DrawShape(DrawShape &&)=delete
DrawShape & operator=(DrawShape &&)=delete
MolDraw2D is the base class for doing 2D renderings of molecules.
Definition: MolDraw2D.h:47
Std stuff.
Definition: Abbreviations.h:19
const DashPattern dots
Definition: DrawShape.h:27
std::vector< double > DashPattern
const DashPattern dashes
Definition: DrawShape.h:28
const DashPattern noDash
Definition: DrawShape.h:25
const DashPattern shortDashes
Definition: DrawShape.h:29