Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2412 - User can't correctly save (or make a layout) to RDF/RXN reaction several products or with separate positioned molecules #2717

Open
wants to merge 27 commits into
base: master
Choose a base branch
from

Conversation

even1024
Copy link
Collaborator

Generic request

  • PR name follows the pattern #1234 – issue name
  • branch name does not contain '#'
  • base branch (master or release/xx) is correct
  • PR is linked with the issue
  • task status changed to "Code review"
  • code follows product standards
  • regression tests updated

@even1024 even1024 marked this pull request as ready for review January 9, 2025 11:28
@@ -822,5 +824,234 @@ namespace indigo
float _d;
};

inline bool isPointInPolygon(const Vec2f& p, const std::vector<Vec2f>& poly)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need more comments.
Also no need to calculate x if both vertices has x > px
So I suggest something like

inline bool isPointInPolygon(const Vec2f& p, const std::vector<Vec2f>& poly)
{
    // Ray casting algorithm
    bool in = false;
    for (size_t i = 0, n = poly.size(); i < n; ++i)
    {
        size_t j = (i + 1) % n;
        if (((poly[i].y > p.y) != (poly[j].y > p.y)) && // point y between vertices and
            (((p.x > poly[i].x) && (p.x < poly[j].x)) || // both vertices are at right of point or edge is at right of point
            (p.x < (poly[i].x + (p.y - poly[i].y) * (poly[j].x - poly[i].x) / (poly[j].y - poly[i].y))))) // py < (py-y0)*dx/dy
            in = !in;
    }
    return in;
}

return result;
}

inline float convexPolygonArea(const std::vector<Vec2f>& poly)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add comment that this is "Shoelace formula - triangle form"

return result;
}

inline bool isPointInConvexPolygon(const Vec2f& p, const std::vector<Vec2f>& poly)
Copy link
Collaborator

@AliaksandrDziarkach AliaksandrDziarkach Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this function computation is more complex tnan isPointInPolygon.
For convex polygon in most cases no edge crossing computation need - edge will be at left or at right from point.
Most complex case need cross calculation for one edge.

return std::abs(area) * 0.5f;
}

inline bool isInside(const Vec2f& edgeStart, const Vec2f& edgeEnd, const Vec2f& point)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be named isNormalNegative, and comment about "check sign of normal to (edgeStart,edgeEnd),(edgeStart,point)" will be usefull

@@ -822,5 +824,234 @@ namespace indigo
float _d;
};

inline bool isPointInPolygon(const Vec2f& p, const std::vector<Vec2f>& poly)
{
bool in = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about next two corner cases?
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for edge:
image


inline bool convexPolygonsIntersect(const std::vector<Vec2f>& poly1, const std::vector<Vec2f>& poly2)
{
auto project = [](const std::vector<Vec2f>& poly, const Vec2f& axis) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comment that this is "Separating axis collision detection"

return {s.x + t * dx1, s.y + t * dy1};
}

inline std::vector<Vec2f> convexClip(const std::vector<Vec2f>& subject, const std::vector<Vec2f>& clip)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comment that this is "Sutherland–Hodgman algorithm"


inline bool isInside(const Vec2f& edgeStart, const Vec2f& edgeEnd, const Vec2f& point)
{
return (edgeEnd.x - edgeStart.x) * (point.y - edgeStart.y) - (edgeEnd.y - edgeStart.y) * (point.x - edgeStart.x) <= 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it just edgeStart.relaticeCross(edgeEnd, point) < 0, is'n it?

Vec2f dir = p2 - p1;
for (size_t i = 0, n = poly.size(); i < n; ++i)
{
Vec2f A = poly[i];
Copy link
Collaborator

@AliaksandrDziarkach AliaksandrDziarkach Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be "const Vect2f&" or just "vect2f a=poly[i] - p1"

@@ -4775,6 +4775,40 @@ void BaseMolecule::getBoundingBox(float font_size, LABEL_MODE label_mode, Rect2f
bbox = Rect2f(a, b);
}

std::vector<Vec2f> BaseMolecule::getConvexHull(const Vec2f& min_box) const
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comment that this is "Andrew's monotone chain convex hull algorithm"

{
float dist = computeConvexDistance(_components[i].second, _components[j].second);
auto& mdi = _mol_distances[i];
auto it = std::lower_bound(mdi.sorted_distances.begin(), mdi.sorted_distances.end(), std::make_pair(j, dist),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like sorted_distance used only here.
Do you really need this variable?

for (int i = 0; i < static_cast<int>(_zones.size()); ++i)
{
auto& zone = _zones[i];
std::pair<int, std::unordered_set<int>> cur_zone(i, {});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look like this variable unused

if (doesRayIntersectPolygon(tail_start, *c_it, hull1) && doesRayIntersectPolygon(tail_start, *c_it, hull2))
return zone1 ? zone1 : current_zone;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this redundant braces or move break inside

{
for (auto& section : cz.second)
{
if (!(_zones[cz.first].zone_type == ZoneType::EPathWay && section > 1) && it_oz2->second.count(section ^ 1))
Copy link
Collaborator

@AliaksandrDziarkach AliaksandrDziarkach Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add comment to this check?
For me not clear what is verified here.

return result;
}

std::optional<std::pair<int, int>> ReactionMultistepDetector::findMaxSpecialZone(size_t mol_idx, std::map<int, std::unordered_set<int>>& zones)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add comment that this function search sections intersected with component hull and fill zones with these sections and return max containment section in pair<zone,section>

_moleculeCount = (int)_components.size();
}

std::optional<std::pair<int, int>> ReactionMultistepDetector::isMergeable(size_t mol_idx1, size_t mol_idx2, std::optional<std::pair<int, int>> current_zone)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it should be std::optional<std::pair<int, int>>& current_zone


auto zone1 = findMaxSpecialZone(mol_idx1, other_zones1);
auto zone2 = findMaxSpecialZone(mol_idx2, other_zones2);
std::set_intersection(other_zones1.begin(), other_zones1.end(), other_zones2.begin(), other_zones2.end(), std::inserter(comm_zones, comm_zones.begin()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think intersection calc better move to line 573

{
case ZoneType::EPlus:
if ((doesVerticalLineIntersectPolygon(coords[0].x, hull1) && doesVerticalLineIntersectPolygon(coords[0].x, hull2)) ||
(doesHorizontalLineIntersectPolygon(coords[0].x, hull1) && doesHorizontalLineIntersectPolygon(coords[0].x, hull2)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it should be coord[0].y as parameter of doesHorizontalLineIntersectPolygon

{
for (int section1 : sections1)
{
if (zt == ZoneType::EPathWay && section1 > 1) // pathway has only 2 opposite agents sections
Copy link
Collaborator

@AliaksandrDziarkach AliaksandrDziarkach Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you use section index in such way I think better add constants like pathway_sections_count, pathway_section_top, pathway_section_bottom, pathway_max_opposite_section_index for all types and use it is this function and in zone creating functions like

    szd.zone_type = ZoneType::EPathWay;
    szd.zone_sections.resize(pathway_sections_count);
    szd.zone_sections[pathway_section_top] = top;
    szd.zone_sections[pathway_section_bottom] = bottom;
    szd.zone_sections[pathway_section_right] = right;

And add comment in constant definition about this loigic

Second option is to store opposite indexes in something like std::map<ZoneType,std::map<std::size_t,std::size_t>> to make logic more clear

auto c_it = coords.begin();
const Vec2f& head = *c_it++;
const Vec2f& spine_beg = *c_it++;
const Vec2f& spine_end = *c_it++;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you use spine_end? AFAIK spine is vertical so X coordinates of spine_end and spine_beg are same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants