fix(Task): get_target_rect 所有 target 类型均 normalize 负宽高#1372
Closed
ocsin1 wants to merge 1 commit into
Closed
Conversation
此前 normalize_rect 仅对 Region 类型生效。Self/PreTask/Anchor 类型 的 target_offset 若包含负宽高,会直接送入 std::clamp 变为为 0,导致 cv::Rect::empty() 命中和 "failed to get target rect" 错误。 将 normalize_rect 移至 offset 应用之后并对所有 target 类型生效,使 负宽高遵循 ROI 语义(取绝对值并反向调整 x/y),与 roi 字段行为一致。
Contributor
There was a problem hiding this comment.
Hey - 我发现了 1 个问题,并给出了一些整体反馈:
- 现在
normalize_rect已经在 offset 之后对所有 target 类型统一调用,那么在Region分支里的那个更早的normalize_rect调用就变得多余了,并且会导致该分支被归一化两次;建议移除第一次调用,只依赖 offset 之后的统一归一化,这样可以让行为更一致、逻辑更简单。 - 如果
MAA_VISION_NS::normalize_rect本身已经保证返回的矩形在图像范围内且非负,那么对raw额外调用的std::clamp可能是多余的;你可以考虑要么移除这些 clamp,要么写上注释说明为什么需要同时做归一化和 clamp,以避免后续产生困惑。
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Now that `normalize_rect` is applied after the offset to all target types, the earlier `normalize_rect` call inside the `Region` branch becomes redundant and causes a double-normalization for that case; consider removing the first call and relying on the unified post-offset normalization to keep behavior consistent and simpler.
- If `MAA_VISION_NS::normalize_rect` already guarantees in-bounds, non-negative rectangles, the extra `std::clamp` calls on `raw` may be unnecessary; you could consider either removing them or documenting why both normalization and clamping are needed to avoid future confusion.
## Individual Comments
### Comment 1
<location path="source/MaaFramework/Task/Component/ActionHelper.cpp" line_range="227-236" />
<code_context>
raw = MAA_VISION_NS::normalize_rect(raw, image.cols, image.rows);
}
- int x = std::clamp(raw.x + target.offset.x, 0, image.cols);
- int y = std::clamp(raw.y + target.offset.y, 0, image.rows);
- int width = std::clamp(raw.width + target.offset.width, 0, image.cols - x);
- int height = std::clamp(raw.height + target.offset.height, 0, image.rows - y);
+ raw.x += target.offset.x;
+ raw.y += target.offset.y;
+ raw.width += target.offset.width;
+ raw.height += target.offset.height;
- return cv::Rect(x, y, width, height);
+ // 对所有 target 类型,offset 后 normalize 以支持负宽高(ROI 语义)
+ raw = MAA_VISION_NS::normalize_rect(raw, image.cols, image.rows);
+
+ raw.x = std::clamp(raw.x, 0, image.cols);
+ raw.y = std::clamp(raw.y, 0, image.rows);
+ raw.width = std::clamp(raw.width, 0, image.cols - raw.x);
+ raw.height = std::clamp(raw.height, 0, image.rows - raw.y);
+
+ return raw;
</code_context>
<issue_to_address>
**question (bug_risk):** Consider whether the second normalize call changes semantics for targets already normalized earlier.
Some target types already pass `raw` through `normalize_rect` earlier in this function (pre-offset). With this change they’ll now be normalized twice: once before and once after applying the offset, which is a behavioral change and may affect edge-adjacent rectangles.
Please check whether you can rely on a single normalization after applying offsets (and then clamping), and remove the earlier call if it’s redundant. That would simplify the logic and avoid subtle differences from double-normalization.
</issue_to_address>Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Original comment in English
Hey - I've found 1 issue, and left some high level feedback:
- Now that
normalize_rectis applied after the offset to all target types, the earliernormalize_rectcall inside theRegionbranch becomes redundant and causes a double-normalization for that case; consider removing the first call and relying on the unified post-offset normalization to keep behavior consistent and simpler. - If
MAA_VISION_NS::normalize_rectalready guarantees in-bounds, non-negative rectangles, the extrastd::clampcalls onrawmay be unnecessary; you could consider either removing them or documenting why both normalization and clamping are needed to avoid future confusion.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Now that `normalize_rect` is applied after the offset to all target types, the earlier `normalize_rect` call inside the `Region` branch becomes redundant and causes a double-normalization for that case; consider removing the first call and relying on the unified post-offset normalization to keep behavior consistent and simpler.
- If `MAA_VISION_NS::normalize_rect` already guarantees in-bounds, non-negative rectangles, the extra `std::clamp` calls on `raw` may be unnecessary; you could consider either removing them or documenting why both normalization and clamping are needed to avoid future confusion.
## Individual Comments
### Comment 1
<location path="source/MaaFramework/Task/Component/ActionHelper.cpp" line_range="227-236" />
<code_context>
raw = MAA_VISION_NS::normalize_rect(raw, image.cols, image.rows);
}
- int x = std::clamp(raw.x + target.offset.x, 0, image.cols);
- int y = std::clamp(raw.y + target.offset.y, 0, image.rows);
- int width = std::clamp(raw.width + target.offset.width, 0, image.cols - x);
- int height = std::clamp(raw.height + target.offset.height, 0, image.rows - y);
+ raw.x += target.offset.x;
+ raw.y += target.offset.y;
+ raw.width += target.offset.width;
+ raw.height += target.offset.height;
- return cv::Rect(x, y, width, height);
+ // 对所有 target 类型,offset 后 normalize 以支持负宽高(ROI 语义)
+ raw = MAA_VISION_NS::normalize_rect(raw, image.cols, image.rows);
+
+ raw.x = std::clamp(raw.x, 0, image.cols);
+ raw.y = std::clamp(raw.y, 0, image.rows);
+ raw.width = std::clamp(raw.width, 0, image.cols - raw.x);
+ raw.height = std::clamp(raw.height, 0, image.rows - raw.y);
+
+ return raw;
</code_context>
<issue_to_address>
**question (bug_risk):** Consider whether the second normalize call changes semantics for targets already normalized earlier.
Some target types already pass `raw` through `normalize_rect` earlier in this function (pre-offset). With this change they’ll now be normalized twice: once before and once after applying the offset, which is a behavioral change and may affect edge-adjacent rectangles.
Please check whether you can rely on a single normalization after applying offsets (and then clamping), and remove the earlier call if it’s redundant. That would simplify the logic and avoid subtle differences from double-normalization.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
Author
|
第一次 normalize 不多余吧,处理的是 Region raw 自带的负宽高 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
此前 normalize_rect 仅对 Region 类型生效。Self/PreTask/Anchor 类型 的 target_offset 若包含负宽高,会直接送入 std::clamp 变为为 0,导致
cv::Rect::empty() 命中和 "failed to get target rect" 错误。
示例:
{ "1": { "roi": [ 790, 308, 154, 18 ], "action": "Click", // 这样会报错 "target_offset": [ 0, -50, 0, -30 ] }, "2": { "roi": [ 790, 258, 154, -12 ], "action": "Click" // 直接点击偏移后位置不会 } }将 normalize_rect 移至 offset 应用之后并对所有 target 类型生效,使
负宽高遵循 ROI 语义(取绝对值并反向调整 x/y),与 roi 字段行为一致。
Summary by Sourcery
Bug Fixes:
Original summary in English
Summary by Sourcery
Bug Fixes: