1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
|
//
// Copyright (C) 2021-2022 David Cosgrove and other RDKit contributors
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
// Original author: David Cosgrove (CozChemIx Limited)
//
#include <cmath>
#include <GraphMol/MolDraw2D/MolDraw2DDetails.h>
#include <GraphMol/MolDraw2D/DrawShape.h>
#include <GraphMol/MolDraw2D/DrawText.h>
#include <GraphMol/MolDraw2D/MolDraw2D.h>
#include <GraphMol/MolDraw2D/StringRect.h>
namespace RDKit {
namespace MolDraw2D_detail {
// ****************************************************************************
DrawShape::DrawShape(const std::vector<Point2D> &points, double lineWidth,
bool scaleLineWidth, DrawColour lineColour, bool fill,
int atom1, int atom2, int bond)
: points_(points),
lineWidth_(lineWidth),
scaleLineWidth_(scaleLineWidth),
lineColour_(lineColour),
fill_(fill),
atom1_(atom1),
atom2_(atom2),
bond_(bond) {}
// ****************************************************************************
void DrawShape::draw(MolDraw2D &drawer) {
// the various myDraw functions may over-ride these.
const auto ocolour = drawer.colour();
drawer.setColour(lineColour_);
const auto olw = drawer.lineWidth();
drawer.setLineWidth(lineWidth_);
const auto ofill = drawer.fillPolys();
drawer.setFillPolys(fill_);
const auto lineScale = drawer.drawOptions().scaleBondWidth;
drawer.drawOptions().scaleBondWidth = scaleLineWidth_;
drawer.setActiveAtmIdx(atom1_, atom2_);
drawer.setActiveBndIdx(bond_);
myDraw(drawer);
drawer.setActiveAtmIdx();
drawer.setActiveBndIdx();
drawer.setColour(ocolour);
drawer.setLineWidth(olw);
drawer.setFillPolys(ofill);
drawer.drawOptions().scaleBondWidth = lineScale;
}
// ****************************************************************************
void DrawShape::findExtremes(double &xmin, double &xmax, double &ymin,
double &ymax) const {
for (const auto &p : points_) {
xmin = std::min(xmin, p.x);
xmax = std::max(xmax, p.x);
ymin = std::min(ymin, p.y);
ymax = std::max(ymax, p.y);
}
}
// ****************************************************************************
void DrawShape::scale(const Point2D &scale_factor) {
for (auto &p : points_) {
p.x *= scale_factor.x;
p.y *= scale_factor.y;
}
}
// ****************************************************************************
void DrawShape::move(const Point2D &trans) {
for (auto &p : points_) {
p += trans;
}
}
// ****************************************************************************
bool DrawShape::doesRectClash(const StringRect &, double) const {
return false;
}
// ****************************************************************************
DrawShapeArrow::DrawShapeArrow(const std::vector<Point2D> &points,
double lineWidth, bool scaleLineWidth,
DrawColour lineColour, bool fill, int atom1,
int atom2, int bond, double frac, double angle)
: DrawShape(points, lineWidth, scaleLineWidth, lineColour, fill, atom1,
atom2, bond),
frac_(frac),
angle_(angle) {
PRECONDITION(points_.size() == 2, "arrow bad points size");
// the two ends of the arrowhead are used for collision detection so we
// may as well store them.
Point2D ab(points_[1]), p1, p2;
MolDraw2D_detail::calcArrowHead(ab, p1, p2, points_[0], fill_, frac_, angle_);
points_.push_back(p1);
points_.push_back(p2);
}
// ****************************************************************************
void DrawShapeArrow::myDraw(MolDraw2D &drawer) const {
if (drawer.drawOptions().splitBonds) {
drawer.setActiveAtmIdx(atom2_);
} else {
drawer.setActiveAtmIdx(atom1_, atom2_);
}
drawer.setActiveBndIdx(bond_);
drawer.drawArrow(points_[0], points_[1], fill_, frac_, angle_, lineColour_,
true);
}
// ****************************************************************************
bool DrawShapeArrow::doesRectClash(const StringRect &rect,
double padding) const {
padding = scaleLineWidth_ ? padding * lineWidth_ : padding;
if (doesLineIntersect(rect, points_[0], points_[1], padding)) {
return true;
}
if (doesLineIntersect(rect, points_[1], points_[2], padding)) {
return true;
}
if (doesLineIntersect(rect, points_[1], points_[3], padding)) {
return true;
}
if (doesLineIntersect(rect, points_[2], points_[3], padding)) {
return true;
}
return false;
}
// ****************************************************************************
DrawShapeEllipse::DrawShapeEllipse(const std::vector<Point2D> &points,
double lineWidth, bool scaleLineWidth,
DrawColour lineColour, bool fill, int atom1)
: DrawShape(points, lineWidth, scaleLineWidth, lineColour, fill, atom1) {
PRECONDITION(points_.size() == 2, "ellipse wrong points");
}
// ****************************************************************************
void DrawShapeEllipse::myDraw(MolDraw2D &drawer) const {
if (fill_) {
drawer.setLineWidth(1);
drawer.drawOptions().scaleBondWidth = false;
}
auto p1 = points_[0] - points_[1];
auto p2 = points_[0] + points_[1];
drawer.drawEllipse(p1, p2, true);
}
// ****************************************************************************
void DrawShapeEllipse::findExtremes(double &xmin, double &xmax, double &ymin,
double &ymax) const {
// points_[0] is the centre, points_[1] the radii
xmin = std::min(points_[0].x - points_[1].x, xmin);
xmax = std::max(points_[0].x + points_[1].x, xmax);
ymin = std::min(points_[0].y - points_[1].y, ymin);
ymax = std::max(points_[0].y + points_[1].y, ymax);
}
// ****************************************************************************
void DrawShapeEllipse::move(const Point2D &trans) { points_[0] += trans; }
// ****************************************************************************
bool DrawShapeEllipse::doesRectClash(const StringRect &rect,
double padding) const {
padding = scaleLineWidth_ ? padding * lineWidth_ : padding;
Point2D tl, tr, br, bl;
rect.calcCorners(tl, tr, br, bl, padding);
auto w = points_[1].x;
auto h = points_[1].y;
auto cx = points_[0].x;
auto cy = points_[0].y;
w = w > 0 ? w : -1 * w;
h = h > 0 ? h : -1 * h;
Point2D centre{cx, cy};
if (doesLineIntersectEllipse(centre, w / 2.0, h / 2.0, padding, tl, tr)) {
return true;
}
if (doesLineIntersectEllipse(centre, w / 2.0, h / 2.0, padding, tr, br)) {
return true;
}
if (doesLineIntersectEllipse(centre, w / 2.0, h / 2.0, padding, br, bl)) {
return true;
}
if (doesLineIntersectEllipse(centre, w / 2.0, h / 2.0, padding, bl, tl)) {
return true;
}
return false;
}
// ****************************************************************************
DrawShapeSimpleLine::DrawShapeSimpleLine(const std::vector<Point2D> &points,
double lineWidth, bool scaleLineWidth,
DrawColour lineColour, int atom1,
int atom2, int bond,
DashPattern dashPattern)
: DrawShape(points, lineWidth, scaleLineWidth, lineColour, false, atom1,
atom2, bond),
dashPattern_(dashPattern) {
PRECONDITION(points_.size() == 2, "simple line wrong number of points");
}
// ****************************************************************************
void DrawShapeSimpleLine::myDraw(MolDraw2D &drawer) const {
auto od = drawer.dash();
auto dp = dashPattern_;
if (dp == shortDashes) {
// these are roughly equivalent to the original checks on scale,
// which we don't have any more.
double sq_len = (points_[0] - points_[1]).lengthSq();
if (sq_len < 55.0) {
dp[0] /= 4;
dp[1] /= 3;
} else if (sq_len < 900.0) {
dp[0] /= 2;
dp[1] /= 1.5;
}
}
drawer.setDash(dp);
drawer.setActiveAtmIdx(atom1_, atom2_);
drawer.setActiveBndIdx(bond_);
drawer.drawLine(points_[0], points_[1], lineColour_, lineColour_, true);
drawer.setActiveAtmIdx();
drawer.setActiveBndIdx();
drawer.setDash(od);
}
// ****************************************************************************
bool DrawShapeSimpleLine::doesRectClash(const StringRect &rect,
double padding) const {
padding = scaleLineWidth_ ? padding * lineWidth_ : padding;
return doesLineIntersect(rect, points_[0], points_[1], padding);
}
// ****************************************************************************
DrawShapePolyLine::DrawShapePolyLine(const std::vector<Point2D> &points,
double lineWidth, bool scaleLineWidth,
DrawColour lineColour, bool fill,
int atom1, int atom2, int bond,
DashPattern dashPattern)
: DrawShape(points, lineWidth, scaleLineWidth, lineColour, fill, atom1,
atom2, bond),
dashPattern_(dashPattern) {
PRECONDITION(points_.size() > 2, "polyline not enough points");
}
// ****************************************************************************
void DrawShapePolyLine::myDraw(MolDraw2D &drawer) const {
drawer.drawPolygon(points_, true);
}
// ****************************************************************************
bool DrawShapePolyLine::doesRectClash(const StringRect &rect,
double padding) const {
padding = scaleLineWidth_ ? padding * lineWidth_ : padding;
Point2D tl, tr, br, bl;
rect.calcCorners(tl, tr, br, bl, padding);
for (size_t i = 1; i < points_.size(); ++i) {
if (doesLineIntersect(rect, points_[i - 1], points_[i], padding)) {
return true;
}
}
return doesLineIntersect(rect, points_.front(), points_.back(), padding);
}
// ****************************************************************************
DrawShapeSolidWedge::DrawShapeSolidWedge(const std::vector<Point2D> points,
const DrawColour &col1,
const DrawColour &col2,
bool splitBonds,
std::vector<Point2D> &otherBondVecs,
double lineWidth, int atom1, int atom2,
int bond)
: DrawShape(points, lineWidth / 2.0, false, col1, false, atom1, atom2,
bond),
col2_(col2),
splitBonds_(splitBonds),
otherBondVecs_(otherBondVecs) {
PRECONDITION(points_.size() == 3, "solid wedge wrong points");
if (otherBondVecs_.size() > 2) {
trimOtherBondVecs();
}
if (otherBondVecs_.size() == 2) {
orderOtherBondVecs();
}
buildTriangles();
}
// ****************************************************************************
void DrawShapeSolidWedge::buildTriangles() {
if (!(lineColour_ == col2_) || splitBonds_) {
buildTwoColorTriangles();
} else {
buildSingleColorTriangles();
}
}
// ****************************************************************************
void DrawShapeSolidWedge::buildSingleColorTriangles() {
auto point = points_[0];
auto end1 = points_[1];
auto end2 = points_[2];
auto midEnd = (end1 + end2) / 2.0;
auto adjend1 = end1;
auto adjend2 = end2;
points_.clear();
// adjust adjend1 and adjend2 to line up with otherBondVecs_.
if (otherBondVecs_.empty()) {
points_.push_back(point);
points_.push_back(adjend1);
points_.push_back(adjend2);
} else if (otherBondVecs_.size() == 1) {
auto side1 = (end1 - point) * 2.0;
if (!doLinesIntersect(point, point + side1, midEnd - otherBondVecs_[0],
midEnd + otherBondVecs_[0], &adjend1)) {
adjend1 = end1;
}
auto side2 = (end2 - point) * 2.0;
if (!doLinesIntersect(point, point + side2, midEnd - otherBondVecs_[0],
midEnd + otherBondVecs_[0], &adjend2)) {
adjend2 = end2;
}
points_.push_back(point);
points_.push_back(adjend1);
points_.push_back(adjend2);
} else if (otherBondVecs_.size() == 2) {
auto side1 = (end1 - point) * 2.0;
if (!doLinesIntersect(point, point + side1, midEnd - otherBondVecs_[0],
midEnd + otherBondVecs_[0], &adjend1)) {
adjend1 = end1;
}
points_.push_back(point);
points_.push_back(adjend1);
points_.push_back(midEnd);
auto side2 = (end2 - point) * 2.0;
if (!doLinesIntersect(point, point + side2, midEnd - otherBondVecs_[1],
midEnd + otherBondVecs_[1], &adjend2)) {
adjend2 = end2;
}
points_.push_back(point);
points_.push_back(midEnd);
points_.push_back(adjend2);
}
}
// ****************************************************************************
void DrawShapeSolidWedge::buildTwoColorTriangles() {
auto point = points_[0];
auto end1 = points_[1];
auto end2 = points_[2];
auto midEnd = (end1 + end2) / 2.0;
auto adjend1 = end1;
auto adjend2 = end2;
points_.clear();
auto e1 = end1 - point;
auto e2 = end2 - point;
auto mid1 = point + e1 * 0.5;
auto mid2 = point + e2 * 0.5;
points_.push_back(point);
points_.push_back(mid1);
points_.push_back(mid2);
if (otherBondVecs_.empty()) {
points_.push_back(mid1);
points_.push_back(adjend2);
points_.push_back(adjend1);
points_.push_back(mid1);
points_.push_back(mid2);
points_.push_back(adjend2);
} else if (otherBondVecs_.size() == 1) {
auto side1 = (end1 - point) * 2.0;
if (!doLinesIntersect(point, point + side1, midEnd - otherBondVecs_[0],
midEnd + otherBondVecs_[0], &adjend1)) {
adjend1 = end1;
}
auto side2 = (end2 - point) * 2.0;
if (!doLinesIntersect(point, point + side2, midEnd - otherBondVecs_[0],
midEnd + otherBondVecs_[0], &adjend2)) {
adjend2 = end2;
}
points_.push_back(mid1);
points_.push_back(adjend2);
points_.push_back(adjend1);
points_.push_back(mid1);
points_.push_back(mid2);
points_.push_back(adjend2);
} else if (otherBondVecs_.size() == 2) {
auto side1 = (end1 - point) * 2.0;
if (!doLinesIntersect(point, point + side1, midEnd - otherBondVecs_[0],
midEnd + otherBondVecs_[0], &adjend1)) {
adjend1 = end1;
}
auto side2 = (end2 - point) * 2.0;
if (!doLinesIntersect(point, point + side2, midEnd - otherBondVecs_[1],
midEnd + otherBondVecs_[1], &adjend2)) {
adjend2 = end2;
}
points_.push_back(mid1);
points_.push_back(adjend1);
points_.push_back(midEnd);
points_.push_back(midEnd);
points_.push_back(mid2);
points_.push_back(mid1);
points_.push_back(midEnd);
points_.push_back(adjend2);
points_.push_back(mid2);
}
}
// ****************************************************************************
void DrawShapeSolidWedge::myDraw(MolDraw2D &drawer) const {
drawer.setFillPolys(true);
if (drawer.drawOptions().splitBonds) {
drawer.setActiveAtmIdx(atom1_);
} else {
drawer.setActiveAtmIdx(atom1_, atom2_);
}
drawer.setActiveBndIdx(bond_);
drawer.drawTriangle(points_[0], points_[1], points_[2], true);
if (points_.size() > 3) {
if (drawer.drawOptions().splitBonds) {
drawer.setActiveAtmIdx(atom2_);
}
drawer.setColour(col2_);
}
for (unsigned int i = 3; i < points_.size(); i += 3) {
drawer.drawTriangle(points_[i], points_[i + 1], points_[i + 2], true);
}
}
// ****************************************************************************
bool DrawShapeSolidWedge::doesRectClash(const StringRect &rect,
double padding) const {
padding = scaleLineWidth_ ? padding * lineWidth_ : padding;
if (doesTriangleIntersect(rect, points_[0], points_[1], points_[2],
padding)) {
return true;
}
if (points_.size() >= 6) {
if (doesTriangleIntersect(rect, points_[3], points_[4], points_[5],
padding)) {
return true;
}
if (points_.size() >= 9) {
if (doesTriangleIntersect(rect, points_[6], points_[7], points_[8],
padding)) {
return true;
}
}
}
return false;
}
// ****************************************************************************
void DrawShapeSolidWedge::trimOtherBondVecs() {
if (otherBondVecs_.size() < 3) {
return;
}
int firstVec = 0, secondVec = 1;
double largestAng = -361.0;
for (unsigned int i = 0; i < otherBondVecs_.size() - 1; ++i) {
for (unsigned int j = i + 1; j < otherBondVecs_.size(); ++j) {
auto ang = otherBondVecs_[i].angleTo(otherBondVecs_[j]);
if (ang > largestAng) {
firstVec = i;
secondVec = j;
largestAng = ang;
}
}
}
std::vector<Point2D> newVecs{otherBondVecs_[firstVec],
otherBondVecs_[secondVec]};
otherBondVecs_ = newVecs;
}
// ****************************************************************************
void DrawShapeSolidWedge::orderOtherBondVecs() {
if (otherBondVecs_.size() < 2) {
return;
}
// otherBondVecs_[0] needs to be on the same side as points_[1], which
// implies the larger dot product between it and the vector along the
// triangle edge.
auto side1 = (points_[0] - points_[1]);
auto mid = (points_[1] + points_[2]) / 2.0;
auto midp1 = mid.directionVector(points_[1]);
auto dot1 = midp1.dotProduct(otherBondVecs_[0]);
auto dot2 = midp1.dotProduct(otherBondVecs_[1]);
if (dot1 < dot2) {
std::swap(otherBondVecs_[0], otherBondVecs_[1]);
}
}
// ****************************************************************************
DrawShapeDashedWedge::DrawShapeDashedWedge(const std::vector<Point2D> points,
const DrawColour &col1,
const DrawColour &col2,
bool oneLessDash, double lineWidth,
int atom1, int atom2, int bond)
: DrawShape(points, lineWidth, false, col1, false, atom1, atom2, bond),
col2_(col2),
oneLessDash_(oneLessDash) {
PRECONDITION(points_.size() == 3, "dashed wedge wrong points");
at1Cds_ = points[0];
end1Cds_ = points[1];
end2Cds_ = points[2];
buildLines();
}
// ****************************************************************************
void DrawShapeDashedWedge::buildLines() {
auto midend = (end1Cds_ + end2Cds_) * 0.5;
points_.clear();
lineColours_.clear();
auto e1 = at1Cds_.directionVector(end1Cds_);
auto e2 = at1Cds_.directionVector(end2Cds_);
// the ACS1996 rules say the dash separation should be 2.5px. It seems
// like a good result for all of them.
// It appears that this means a 2.5px gap between each line, so we need
// to take the line width into account. Each line that the gap is
// between will contribute half a width.
double dashSep = 2.5 + lineWidth_;
double centralLen = (at1Cds_ - midend).length();
unsigned int nDashes = rdcast<unsigned int>(std::round(centralLen / dashSep));
// There should be at least 3 dashes so we can see which way the wedge
// is going (Github6041b).
unsigned int numDashesNeeded = oneLessDash_ ? 4 : 3;
if (nDashes < numDashesNeeded) {
nDashes = numDashesNeeded;
}
if (!nDashes) {
points_.push_back(end1Cds_);
points_.push_back(end2Cds_);
lineColours_.push_back(lineColour_);
} else {
// re-adjust so the last dash is on the end of the wedge.
dashSep = centralLen / rdcast<double>(nDashes);
// if doing one less dash, we want a shorter wedge that is just as wide
// at the end as it would have been.
if (oneLessDash_) {
double endlenb2 = (end1Cds_ - end2Cds_).length() / 2.0;
auto centralLine = at1Cds_.directionVector(midend);
Point2D centralPerp{-centralLine.y, centralLine.x};
Point2D newEnd1 = at1Cds_ + centralLine * (centralLen - dashSep) +
centralPerp * endlenb2;
Point2D newEnd2 = at1Cds_ + centralLine * (centralLen - dashSep) -
centralPerp * endlenb2;
e1 = at1Cds_.directionVector(newEnd1);
e2 = at1Cds_.directionVector(newEnd2);
}
// we want the separation down the sides of the triangle, so use
// similar triangles to scale.
dashSep *= (end1Cds_ - at1Cds_).length() / centralLen;
int extra = oneLessDash_ ? 0 : 1;
for (unsigned int i = 1; i < nDashes + extra; ++i) {
auto e11 = at1Cds_ + e1 * rdcast<double>(i) * dashSep;
auto e22 = at1Cds_ + e2 * rdcast<double>(i) * dashSep;
points_.push_back(e11);
points_.push_back(e22);
if (i > nDashes / 2) {
lineColours_.push_back(col2_);
} else {
lineColours_.push_back(lineColour_);
}
}
}
}
// ****************************************************************************
void DrawShapeDashedWedge::myDraw(MolDraw2D &drawer) const {
drawer.setFillPolys(false);
drawer.setActiveAtmIdx(atom1_, atom2_);
drawer.setActiveBndIdx(bond_);
for (size_t i = 0, j = 0; i < points_.size(); i += 2, ++j) {
if (drawer.drawOptions().splitBonds) {
if (i < points_.size() / 2) {
drawer.setActiveAtmIdx(atom1_);
} else {
drawer.setActiveAtmIdx(atom2_);
}
}
drawer.drawLine(points_[i], points_[i + 1], lineColours_[j],
lineColours_[j], true);
}
}
// ****************************************************************************
void DrawShapeDashedWedge::scale(const Point2D &scale_factor) {
DrawShape::scale(scale_factor);
at1Cds_.x *= scale_factor.x;
at1Cds_.y *= scale_factor.y;
end1Cds_.x *= scale_factor.x;
end1Cds_.y *= scale_factor.y;
end2Cds_.x *= scale_factor.x;
end2Cds_.y *= scale_factor.y;
buildLines();
}
// ****************************************************************************
void DrawShapeDashedWedge::move(const Point2D &trans) {
DrawShape::move(trans);
at1Cds_ += trans;
end1Cds_ += trans;
end2Cds_ += trans;
}
// ****************************************************************************
void DrawShapeDashedWedge::findExtremes(double &xmin, double &xmax,
double &ymin, double &ymax) const {
xmin = std::min({at1Cds_.x, end1Cds_.x, end2Cds_.x, xmin});
xmax = std::max({at1Cds_.x, end1Cds_.x, end2Cds_.x, xmax});
ymin = std::min({at1Cds_.y, end1Cds_.y, end2Cds_.y, ymin});
ymax = std::max({at1Cds_.y, end1Cds_.y, end2Cds_.y, ymax});
}
// ****************************************************************************
bool DrawShapeDashedWedge::doesRectClash(const StringRect &rect,
double padding) const {
padding = scaleLineWidth_ ? padding * lineWidth_ : padding;
return doesTriangleIntersect(rect, at1Cds_, end1Cds_, end2Cds_, padding);
}
// ****************************************************************************
DrawShapeWavyLine::DrawShapeWavyLine(const std::vector<Point2D> points,
double lineWidth, bool scaleLineWidth,
const DrawColour &col1,
const DrawColour &col2, double offset,
int atom1, int atom2, int bond)
: DrawShape(points, lineWidth, scaleLineWidth, col1, false, atom1, atom2,
bond),
col2_(col2),
offset_(offset) {
PRECONDITION(points_.size() == 2, "wavy line wrong points");
}
// ****************************************************************************
void DrawShapeWavyLine::myDraw(MolDraw2D &drawer) const {
// nSegments is 16 by default in MolDraw2D.
// use a negative offset because of inverted y coords to make it look the
// same as it used to.
int nsegs = int(
std::round((points_[0] - points_[1]).length() / (offset_ * 2.0 / 3.0)));
drawer.drawWavyLine(points_[0], points_[1], lineColour_, col2_, nsegs,
-offset_ / 2.0, true);
}
// ****************************************************************************
void DrawShapeWavyLine::scale(const Point2D &scaleFactor) {
DrawShape::scale(scaleFactor);
offset_ *= scaleFactor.x;
}
// ****************************************************************************
bool DrawShapeWavyLine::doesRectClash(const StringRect &rect,
double padding) const {
padding = scaleLineWidth_ ? padding * lineWidth_ : padding;
padding += offset_;
return doesLineIntersect(rect, points_[0], points_[1], padding);
}
// ****************************************************************************
DrawShapeArc::DrawShapeArc(const std::vector<Point2D> points, double ang1,
double ang2, double lineWidth, bool scaleLineWidth,
const DrawColour &col1, bool fill, int atom1)
: DrawShape(points, lineWidth, scaleLineWidth, col1, fill, atom1),
ang1_(ang1),
ang2_(ang2) {
PRECONDITION(points_.size() == 2, "arc wrong points");
RANGE_CHECK(0., ang1_, 360.);
RANGE_CHECK(0., ang2_, 360.);
}
// ****************************************************************************
void DrawShapeArc::myDraw(MolDraw2D &drawer) const {
if (fill_) {
drawer.setLineWidth(1);
drawer.drawOptions().scaleBondWidth = false;
}
double start_ang = ang1_ > ang2_ ? ang1_ - 360.0 : ang1_;
drawer.drawArc(points_[0], points_[1].x, points_[1].y, start_ang, ang2_,
true);
}
// ****************************************************************************
void DrawShapeArc::findExtremes(double &xmin, double &xmax, double &ymin,
double &ymax) const {
xmin = std::min(xmin, points_[0].x - points_[1].x);
xmax = std::max(xmax, points_[0].x + points_[1].x);
ymin = std::min(ymin, points_[0].y - points_[1].y);
ymax = std::max(ymax, points_[0].y + points_[1].y);
}
// ****************************************************************************
void DrawShapeArc::move(const Point2D &trans) { points_[0] += trans; }
// ****************************************************************************
bool DrawShapeArc::doesRectClash(const StringRect &rect, double padding) const {
padding = scaleLineWidth_ ? padding * lineWidth_ : padding;
Point2D tl, tr, br, bl;
rect.calcCorners(tl, tr, br, bl, padding);
if (doesLineIntersectArc(points_[0], points_[1].x, points_[1].y, ang1_, ang2_,
padding, tl, tr)) {
return true;
}
if (doesLineIntersectArc(points_[0], points_[1].x, points_[1].y, ang1_, ang2_,
padding, tr, br)) {
return true;
}
if (doesLineIntersectArc(points_[0], points_[1].x, points_[1].y, ang1_, ang2_,
padding, br, bl)) {
return true;
}
if (doesLineIntersectArc(points_[0], points_[1].x, points_[1].y, ang1_, ang2_,
padding, bl, tl)) {
return true;
}
return false;
}
} // namespace MolDraw2D_detail
} // namespace RDKit
|