forked from ssl-core/ssl-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds helper functions used throughout the code to derive ball information.
- Loading branch information
1 parent
61ae30b
commit 5d4962f
Showing
5 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
2 changes: 2 additions & 0 deletions
2
decision-ms/decision-guardiola/decision/processing/analyzer/CMakeLists.txt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
robocin_cpp_library( | ||
NAME analyzers | ||
HDRS field_analyzer.h | ||
ball_analyzer.h | ||
SRCS field_analyzer.cpp | ||
ball_analyzer.cpp | ||
DEPS common::geometry | ||
) |
108 changes: 108 additions & 0 deletions
108
decision-ms/decision-guardiola/decision/processing/analyzer/ball_analyzer.cpp
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include "decision/processing/analyzer/ball_analyzer.h" | ||
|
||
#include "decision/parameters/parameters.h" | ||
|
||
#include <robocin/geometry/mathematics.h> | ||
#include <robocin/geometry/point2d.h> | ||
|
||
namespace decision { | ||
|
||
bool BallAnalyzer::isBallStopped(BallMessage& ball) { | ||
robocin::Point2Df ball_velocity = robocin::Point2Df{ball.velocity->x, ball.velocity->y}; | ||
return ball_velocity.length() < pBallIsMovingVelocity(); | ||
} | ||
|
||
bool BallAnalyzer::isBallMovingFast(BallMessage& ball) { | ||
robocin::Point2Df ball_velocity = robocin::Point2Df{ball.velocity->x, ball.velocity->y}; | ||
return ball_velocity.length() > pBallIsMovingFastVelocity(); | ||
} | ||
|
||
bool BallAnalyzer::isBallMoving(BallMessage& ball) { | ||
robocin::Point2Df ball_velocity = robocin::Point2Df{ball.velocity->x, ball.velocity->y}; | ||
return ball_velocity.length() > pBallIsMovingVelocity(); | ||
} | ||
|
||
bool BallAnalyzer::isBallMovingSlowly(BallMessage& ball) { | ||
robocin::Point2Df ball_velocity = robocin::Point2Df{ball.velocity->x, ball.velocity->y}; | ||
return ball_velocity.length() > pBallIsMovingVelocity() | ||
&& ball_velocity.length() < pBallIsMovingFastVelocity(); | ||
} | ||
|
||
bool BallAnalyzer::isBallMovingWithVelocity(double min_velocity, BallMessage& ball) { | ||
robocin::Point2Df ball_velocity = robocin::Point2Df{ball.velocity->x, ball.velocity->y}; | ||
return ball_velocity.length() > min_velocity; | ||
} | ||
|
||
bool BallAnalyzer::isBallMovingTowards(const robocin::Point2Df& target, | ||
BallMessage& ball, | ||
double max_angle_difference) { | ||
if (!isBallMoving(ball)) { | ||
return false; | ||
} | ||
|
||
robocin::Point2Df ball_position = robocin::Point2Df{ball.position->x, ball.position->y}; | ||
robocin::Point2Df ball_velocity = robocin::Point2Df{ball.velocity->x, ball.velocity->y}; | ||
robocin::Point2Df ball_to_target_vector_direction = (target - ball_position).normalized(); | ||
|
||
double angle_difference | ||
= std::abs(ball_to_target_vector_direction.angleTo(ball_velocity.normalized())); | ||
return angle_difference < max_angle_difference; | ||
} | ||
|
||
bool BallAnalyzer::isBallMovingAway(const robocin::Point2Df& target, BallMessage& ball) { | ||
bool is_ball_moving_away_from_target = isBallMoving(ball) && !isBallMovingTowards(target, ball); | ||
return is_ball_moving_away_from_target; | ||
} | ||
|
||
bool BallAnalyzer::isBallMovingAwayWithVelocity(const robocin::Point2Df& target, | ||
double velocity, | ||
BallMessage& ball) { | ||
bool is_ball_moving_away_with_velocity | ||
= isBallMovingWithVelocity(velocity, ball) && !isBallMovingTowards(target, ball); | ||
return is_ball_moving_away_with_velocity; | ||
} | ||
|
||
bool BallAnalyzer::isBallMovingToOffensiveGoal(const FieldMessage& field, BallMessage& ball) { | ||
if (isBallStopped(ball)) { | ||
return false; | ||
} | ||
|
||
robocin::Point2Df ball_position = robocin::Point2Df{ball.position->x, ball.position->y}; | ||
robocin::Point2Df ball_velocity = robocin::Point2Df{ball.velocity->x, ball.velocity->y}; | ||
robocin::Point2Df offset_from_goal_center = robocin::Point2Df{0.0, *(field.width) / 2}; | ||
|
||
bool moving_to_field_bottom_line | ||
= mathematics::segmentsIntersect(field.enemyGoalOutsideCenter() + offset_from_goal_center, | ||
field.enemyGoalOutsideCenter() - offset_from_goal_center, | ||
ball_position, | ||
ball_position + ball_velocity.resized(*(field.length))); | ||
return moving_to_field_bottom_line; | ||
} | ||
|
||
bool BallAnalyzer::isBallMovingToDefensiveGoal(const FieldMessage& field, BallMessage& ball) { | ||
if (isBallStopped(ball)) { | ||
return false; | ||
} | ||
|
||
robocin::Point2Df ball_position = robocin::Point2Df{ball.position->x, ball.position->y}; | ||
robocin::Point2Df ball_velocity = robocin::Point2Df{ball.velocity->x, ball.velocity->y}; | ||
float offset_y = (*field.penalty_area_width) * sqrt(2) / 2; | ||
robocin::Point2Df offset_from_goal_center = {0, offset_y}; | ||
bool is_moving_to_defensive_goal | ||
= mathematics::segmentsIntersect(field.allyGoalOutsideCenter() + offset_from_goal_center, | ||
field.allyGoalOutsideCenter() - offset_from_goal_center, | ||
ball_position, | ||
ball_position + ball_velocity.resized(*(field.length))); | ||
return is_moving_to_defensive_goal; | ||
} | ||
|
||
bool BallAnalyzer::isBallMovingToEnemySide(const FieldMessage& field, BallMessage& ball) { | ||
if (isBallStopped(ball)) { | ||
return false; | ||
} | ||
|
||
bool is_moving_to_enemy_side = (ball.velocity->x * field.attackDirection().x) > 0; | ||
return is_moving_to_enemy_side; | ||
} | ||
|
||
} // namespace decision |
33 changes: 33 additions & 0 deletions
33
decision-ms/decision-guardiola/decision/processing/analyzer/ball_analyzer.h
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef DECISION_PROCESSING_ANALYZER_BALL_ANALYZER_H | ||
#define DECISION_PROCESSING_ANALYZER_BALL_ANALYZER_H | ||
|
||
#include "decision/processing/messages/perception/ball/ball_message.h" | ||
#include "decision/processing/messages/perception/field/field_message.h" | ||
|
||
#include <robocin/geometry/point2d.h> | ||
|
||
namespace decision { | ||
|
||
class BallAnalyzer { | ||
public: | ||
BallAnalyzer(); | ||
|
||
static bool isBallStopped(BallMessage& ball); | ||
static bool isBallMovingFast(BallMessage& ball); | ||
static bool isBallMoving(BallMessage& ball); | ||
static bool isBallMovingSlowly(BallMessage& ball); | ||
static bool isBallMovingWithVelocity(double min_velocity, BallMessage& ball); | ||
static bool isBallMovingTowards(const robocin::Point2Df& target, | ||
BallMessage& ball, | ||
double max_angle_difference = 1.047); | ||
static bool isBallMovingAway(const robocin::Point2Df& target, BallMessage& ball); | ||
static bool | ||
isBallMovingAwayWithVelocity(const robocin::Point2Df& target, double velocity, BallMessage& ball); | ||
static bool isBallMovingToOffensiveGoal(const FieldMessage& field, BallMessage& ball); | ||
static bool isBallMovingToDefensiveGoal(const FieldMessage& field, BallMessage& ball); | ||
static bool isBallMovingToEnemySide(const FieldMessage& field, BallMessage& ball); | ||
}; | ||
|
||
} // namespace decision | ||
|
||
#endif /* DECISION_PROCESSING_ANALYZER_BALL_ANALYZER_H */ |