From ec8ed1bdda8c50f2aec465b6279f010f3fe6f57e Mon Sep 17 00:00:00 2001 From: Aliaksandr Dziarkach <18146690+AliaksandrDziarkach@users.noreply.github.com> Date: Thu, 24 Oct 2024 23:16:54 +0300 Subject: [PATCH] Backmerge: #2478 - The arrow is displayed incorrect when import from rxn file Fix code. Updated UT --- .../tests/basic/ref/crazystereo.ket | 108 +++++++++--------- .../tests/formats/rxn_no_layout.py | 2 +- .../tests/reaction/reactions/issue_1205.ket | 64 +++++------ core/indigo-core/common/math/algebra.h | 2 +- core/indigo-core/layout/reaction_layout.h | 7 +- .../layout/src/reaction_layout.cpp | 18 +-- 6 files changed, 103 insertions(+), 98 deletions(-) diff --git a/api/tests/integration/tests/basic/ref/crazystereo.ket b/api/tests/integration/tests/basic/ref/crazystereo.ket index 31c229d90a..4e8bb3b987 100644 --- a/api/tests/integration/tests/basic/ref/crazystereo.ket +++ b/api/tests/integration/tests/basic/ref/crazystereo.ket @@ -13,13 +13,13 @@ "mode": "open-angle", "pos": [ { - "x": -5.2799, - "y": 0.5598, + "x": 9.542718, + "y": 0.0, "z": 0.0 }, { - "x": -4.3695, - "y": 0.85445, + "x": 11.542719, + "y": 0.0, "z": 0.0 } ] @@ -33,80 +33,80 @@ { "label": "C", "location": [ - -8.3089, - 0.9723, + 4.071359, + 0.79999, 0.0 ] }, { "label": "C", "location": [ - -9.0234, - 1.3848, + 2.685678, + 1.59998, 0.0 ] }, { "label": "C", "location": [ - -9.7379, - 0.9723, + 1.3, + 0.79999, 0.0 ] }, { "label": "C", "location": [ - -9.7379, - 0.1473, + 1.3, + -0.79999, 0.0 ] }, { "label": "C", "location": [ - -9.0234, - -0.2652, + 2.685678, + -1.59998, 0.0 ] }, { "label": "C", "location": [ - -8.3089, - 0.1473, + 4.071359, + -0.79999, 0.0 ] }, { "label": "C", "location": [ - -7.5945, - -0.2652, + 5.456844, + -1.59998, 0.0 ] }, { "label": "C", "location": [ - -6.8799, - 0.1473, + 6.842718, + -0.79999, 0.0 ] }, { "label": "C", "location": [ - -6.8799, - 0.9723, + 6.842718, + 0.79999, 0.0 ] }, { "label": "C", "location": [ - -7.5945, - 1.3848, + 5.456844, + 1.59998, 0.0 ] } @@ -197,24 +197,24 @@ { "label": "C", "location": [ - 0.0884, - 2.7107, + 19.785242, + 3.599955, 0.0 ] }, { "label": "C", "location": [ - -0.6261, - 2.2982, + 18.399563, + 2.799964, 0.0 ] }, { "label": "C", "location": [ - -0.6261, - 1.4732, + 18.399563, + 1.199985, 0.0 ], "stereoLabel": "&1" @@ -222,32 +222,32 @@ { "label": "C", "location": [ - 0.0884, - 1.0607, + 19.785242, + 0.399995, 0.0 ] }, { "label": "C", "location": [ - 0.8029, - 1.4732, + 21.170923, + 1.199985, 0.0 ] }, { "label": "C", "location": [ - 0.8029, - 2.2982, + 21.170923, + 2.799964, 0.0 ] }, { "label": "C", "location": [ - -1.3405, - 1.0607, + 17.014078, + 0.399995, 0.0 ], "stereoLabel": "&1" @@ -255,64 +255,64 @@ { "label": "C", "location": [ - -2.055, - 1.4732, + 15.628398, + 1.199985, 0.0 ] }, { "label": "C", "location": [ - -2.7695, - 1.0607, + 14.242719, + 0.399995, 0.0 ] }, { "label": "C", "location": [ - -2.7695, - 0.2357, + 14.242719, + -1.199985, 0.0 ] }, { "label": "C", "location": [ - -2.055, - -0.1768, + 15.628398, + -1.999975, 0.0 ] }, { "label": "C", "location": [ - -1.3405, - 0.2357, + 17.014078, + -1.199985, 0.0 ] }, { "label": "P", "location": [ - -0.6261, - -0.1768, + 18.399563, + -1.999975, 0.0 ] }, { "label": "C", "location": [ - 0.1989, - -0.1768, + 19.999542, + -1.999975, 0.0 ] }, { "label": "C", "location": [ - -0.6261, - -1.0018, + 18.399563, + -3.599955, 0.0 ] } diff --git a/api/tests/integration/tests/formats/rxn_no_layout.py b/api/tests/integration/tests/formats/rxn_no_layout.py index 1f7a1243c5..bb87b8f9b1 100644 --- a/api/tests/integration/tests/formats/rxn_no_layout.py +++ b/api/tests/integration/tests/formats/rxn_no_layout.py @@ -7,7 +7,7 @@ ) ) -from env_indigo import * # noqa +from env_indigo import Indigo, joinPathPy # noqa indigo = Indigo() indigo.setOption("json-saving-pretty", True) diff --git a/api/tests/integration/tests/reaction/reactions/issue_1205.ket b/api/tests/integration/tests/reaction/reactions/issue_1205.ket index c12051172b..4a2bb3b01a 100644 --- a/api/tests/integration/tests/reaction/reactions/issue_1205.ket +++ b/api/tests/integration/tests/reaction/reactions/issue_1205.ket @@ -19,13 +19,13 @@ "mode": "open-angle", "pos": [ { - "x": 11.115001, - "y": -8.3249, + "x": 6.768493, + "y": 0.0, "z": 0.0 }, { - "x": 14.434599, - "y": -8.2749, + "x": 16.168491, + "y": 0.0, "z": 0.0 } ] @@ -39,48 +39,48 @@ { "label": "C", "location": [ - 7.7847, - -7.825, + 1.3, + 0.799844, 0.0 ] }, { "label": "C", "location": [ - 9.515, - -7.8245, + 4.068493, + 0.800643, 0.0 ] }, { "label": "C", "location": [ - 8.6515, - -7.3249, + 2.686886, + 1.600007, 0.0 ] }, { "label": "C", "location": [ - 9.515, - -8.8254, + 4.068493, + -0.800804, 0.0 ] }, { "label": "C", "location": [ - 7.7847, - -8.8299, + 1.3, + -0.808003, 0.0 ] }, { "label": "C", "location": [ - 8.6537, - -9.3249, + 2.690406, + -1.600007, 0.0 ] } @@ -136,48 +136,48 @@ { "label": "C", "location": [ - 16.034599, - -7.775, + 18.868492, + 0.799844, 0.0 ] }, { "label": "C", "location": [ - 17.764999, - -7.7745, + 21.637144, + 0.800644, 0.0 ] }, { "label": "C", "location": [ - 16.9014, - -7.2749, + 20.255379, + 1.600007, 0.0 ] }, { "label": "C", "location": [ - 17.764999, - -8.7754, + 21.637144, + -0.800803, 0.0 ] }, { "label": "C", "location": [ - 16.034599, - -8.7799, + 18.868492, + -0.808003, 0.0 ] }, { "label": "C", "location": [ - 16.903601, - -9.2749, + 20.2589, + -1.600007, 0.0 ] } @@ -233,8 +233,8 @@ { "label": "Cl", "location": [ - 12.9748, - -8.8249, + 9.468493, + 2.2, 0.0 ] } @@ -247,8 +247,8 @@ { "label": "N", "location": [ - 12.5498, - -7.7249, + 13.468493, + 2.2, 0.0 ] } diff --git a/core/indigo-core/common/math/algebra.h b/core/indigo-core/common/math/algebra.h index 4ad0758735..5a9fa9be6c 100644 --- a/core/indigo-core/common/math/algebra.h +++ b/core/indigo-core/common/math/algebra.h @@ -63,7 +63,7 @@ namespace indigo static constexpr auto min_coord() { - return std::numeric_limits::min(); + return std::numeric_limits::lowest(); } static constexpr auto max_coord() diff --git a/core/indigo-core/layout/reaction_layout.h b/core/indigo-core/layout/reaction_layout.h index 5df591c04b..627a7bed47 100644 --- a/core/indigo-core/layout/reaction_layout.h +++ b/core/indigo-core/layout/reaction_layout.h @@ -67,6 +67,11 @@ namespace indigo preserve_molecule_layout = preserve; }; + inline float ReactionMarginSize() const + { + return reaction_margin_size + (_font_size < EPSILON ? atom_label_margin : 0); + }; + private: struct SweepEvent { @@ -105,7 +110,7 @@ namespace indigo LayoutOptions _options; BaseReaction& _r; Metalayout _ml; - float _font_size; + const float _font_size; }; } // namespace indigo diff --git a/core/indigo-core/layout/src/reaction_layout.cpp b/core/indigo-core/layout/src/reaction_layout.cpp index eafb1b3f37..047970f327 100644 --- a/core/indigo-core/layout/src/reaction_layout.cpp +++ b/core/indigo-core/layout/src/reaction_layout.cpp @@ -105,6 +105,7 @@ void ReactionLayout::fixLayout() if (arrows_count || simple_count || multi_count) return; + Vec2f rmax{Vec2f::min_coord(), Vec2f::min_coord()}, pmin{Vec2f::max_coord(), Vec2f::max_coord()}; Rect2f bb; std::vector bboxes; // Calculate rightTop of reactant bounding box @@ -115,6 +116,7 @@ void ReactionLayout::fixLayout() { _r.getBaseMolecule(i).getBoundingBox(bb); bboxes.push_back(bb); + rmax.max(bb.rightTop()); if (i == 0 || (bb.left() > cur_left && bb.right() > cur_right)) { cur_left = bb.left(); @@ -131,6 +133,7 @@ void ReactionLayout::fixLayout() { _r.getBaseMolecule(i).getBoundingBox(bb); bboxes.push_back(bb); + pmin.min(bb.leftBottom()); if (bb.left() > cur_left && bb.right() > cur_right) { if (first_after_arrow) @@ -155,8 +158,9 @@ void ReactionLayout::fixLayout() if (!invalid_layout) invalid_layout = hasAnyIntersect(bboxes) || !validVerticalRange(bboxes); + float arrow_len = pmin.x - rmax.x - (2 * ReactionMarginSize()); // if left side of product bb at left of right side of reactant bb - fix layout - if (invalid_layout) + if (invalid_layout || arrow_len != default_arrow_size) { ReactionLayout rl(_r, true); rl.preserve_molecule_layout = true; @@ -243,22 +247,18 @@ void ReactionLayout::_updateMetadata() } else { - const float ptab = /*first_single_product ? reaction_margin_size * 2 :*/ reaction_margin_size + (_font_size < EPSILON ? atom_label_margin : 0); - const float rtab = /*last_single_reactant ? reaction_margin_size * 2 :*/ reaction_margin_size + (_font_size < EPSILON ? atom_label_margin : 0); - ; - arrow_head.y = product_box.middleY(); arrow_tail.y = react_box.middleY(); if (product_box.left() > react_box.right()) { - arrow_head.x = product_box.left() - ptab; - arrow_tail.x = react_box.right() + rtab; + arrow_head.x = product_box.left() - ReactionMarginSize(); + arrow_tail.x = react_box.right() + ReactionMarginSize(); } else { - arrow_head.x = react_box.right() + rtab; - arrow_tail.x = product_box.left() - ptab; + arrow_head.x = react_box.right() + ReactionMarginSize(); + arrow_tail.x = product_box.left() - ReactionMarginSize(); } } _r.meta().addMetaObject(new KETReactionArrow(arrow_type, arrow_tail, arrow_head, arrow_height));