|
Wavelength
Privacy-focused, cross-platform, and open-source communication application
|
Handles the physics simulation for the Blob animation. More...
#include <blob_physics.h>
Public Member Functions | |
| BlobPhysics () | |
| Constructs a BlobPhysics object. Initializes the internal thread pool and starts the physics timer. | |
| QVector2D | CalculateWindowVelocity (const QPointF ¤t_position) |
| Calculates the approximate velocity of the window based on position changes over time. Uses the internal physics_timer_ and applies smoothing. Clamps maximum velocity. | |
| QPointF | GetLastWindowPos () const |
| Gets the last recorded window position. | |
| QVector2D | GetLastWindowVelocity () const |
| Gets the last calculated window velocity. | |
| void | SetLastWindowPos (const QPointF &position) |
| Sets the last known position of the window. Used by CalculateWindowVelocity. | |
| void | UpdatePhysicsParallel (std::vector< QPointF > &control_points, std::vector< QPointF > &target_points, std::vector< QPointF > &velocity, const QPointF &blob_center, const BlobConfig::BlobParameters ¶ms, const BlobConfig::PhysicsParameters &physics_params) |
| Updates the blob physics using QtConcurrent for parallel processing across multiple threads. Divides the work into batches and processes them using the internal thread pool. Falls back to UpdatePhysicsOptimized or UpdatePhysics for different point counts. Includes safety checks for vector sizes and thread ranges. | |
Static Public Member Functions | |
| static void | ConstrainNeighborDistances (std::vector< QPointF > &control_points, std::vector< QPointF > &velocity, double min_distance, double max_distance) |
| Enforces minimum and maximum distance constraints between neighboring control points. Adjusts positions and applies small velocity changes to maintain shape integrity. | |
| static void | HandleBorderCollisions (std::vector< QPointF > &control_points, std::vector< QPointF > &velocity, QPointF &blob_center, int width, int height, double restitution, int padding) |
| Handles collisions between blob control points and the widget borders. Adjusts positions and reverses velocity components based on the restitution factor. | |
| static void | InitializeBlob (std::vector< QPointF > &control_points, std::vector< QPointF > &target_points, std::vector< QPointF > &velocity, QPointF &blob_center, const BlobConfig::BlobParameters ¶ms, int width, int height) |
| Initializes the blob's state vectors (control points, target points, velocity) and center position. Generates initial circular points. | |
| static void | SmoothBlobShape (std::vector< QPointF > &control_points) |
| Applies a smoothing filter to the blob's shape. Moves each control point slightly towards the average position of its neighbors. | |
| static void | StabilizeBlob (std::vector< QPointF > &control_points, const QPointF &blob_center, double blob_radius, double stabilization_rate) |
| Gradually moves control points towards an ideal circular/organic shape when the blob is idle. Helps prevent the blob from collapsing or drifting excessively when not moving. | |
| static void | UpdatePhysics (std::vector< QPointF > &control_points, const std::vector< QPointF > &target_points, std::vector< QPointF > &velocity, const QPointF &blob_center, const BlobConfig::BlobParameters ¶ms, const BlobConfig::PhysicsParameters &physics_params) |
| Updates the blob physics using a standard sequential approach. Iterates through each control point, calculates forces, applies damping, updates velocity and position. Includes safety checks for vector sizes. | |
| static void | UpdatePhysicsOptimized (std::vector< QPointF > &control_points, const std::vector< QPointF > &target_points, std::vector< QPointF > &velocity, const QPointF &blob_center, const BlobConfig::BlobParameters ¶ms, const BlobConfig::PhysicsParameters &physics_params) |
| Updates the blob physics using an optimized Structure-of-Arrays (SoA) approach with OpenMP parallelization. Suitable for a large number of control points. Converts data to separate float arrays for better cache performance and vectorization. Includes safety checks for vector sizes. | |
| static bool | ValidateAndRepairControlPoints (std::vector< QPointF > &control_points, std::vector< QPointF > &velocity, const QPointF &blob_center, double blob_radius) |
| Checks if any control points or velocities contain invalid values (NaN, infinity). If invalid points are found, reset the entire blob shape to a default organic form around the center. | |
Private Attributes | |
| QPointF | last_window_position_ |
| Stores the last known window position for velocity calculation. | |
| QVector2D | last_window_velocity_ |
| Stores the last calculated window velocity. | |
| QElapsedTimer | physics_timer_ |
| Timer used for calculating delta time in velocity calculations. | |
| QThreadPool | thread_pool_ |
| Thread pool used for parallel physics updates (UpdatePhysicsParallel). | |
Handles the physics simulation for the Blob animation.
This class encapsulates the logic for updating the positions and velocities of the blob's control points based on physical forces (springs, damping, viscosity), constraints (neighbor distances, border collisions), and shape smoothing. It provides different update methods, including optimized and parallel versions. It also calculates window velocity based on position changes over time.
| BlobPhysics::BlobPhysics | ( | ) |
Constructs a BlobPhysics object. Initializes the internal thread pool and starts the physics timer.
| QVector2D BlobPhysics::CalculateWindowVelocity | ( | const QPointF & | current_position | ) |
Calculates the approximate velocity of the window based on position changes over time. Uses the internal physics_timer_ and applies smoothing. Clamps maximum velocity.
| current_position | The current position of the window. |
|
static |
Enforces minimum and maximum distance constraints between neighboring control points. Adjusts positions and applies small velocity changes to maintain shape integrity.
| control_points | Reference to the vector of control point positions (modified). |
| velocity | Reference to the vector of control point velocities (modified). |
| min_distance | The minimum allowed distance between neighbors. |
| max_distance | The maximum allowed distance between neighbors. |
|
nodiscard |
Gets the last recorded window position.
|
nodiscard |
Gets the last calculated window velocity.
|
static |
Handles collisions between blob control points and the widget borders. Adjusts positions and reverses velocity components based on the restitution factor.
| control_points | Reference to the vector of control point positions (modified). |
| velocity | Reference to the vector of control point velocities (modified). |
| blob_center | Reference to the blob's center position (modified). |
| width | The width of the widget area. |
| height | The height of the widget area. |
| restitution | The bounciness factor (0.0 to 1.0). |
| padding | The distance from the edge where collisions occur. |
|
static |
Initializes the blob's state vectors (control points, target points, velocity) and center position. Generates initial circular points.
| control_points | Reference to the vector of control point positions (modified). |
| target_points | Reference to the vector of target control point positions (modified). |
| velocity | Reference to the vector of control point velocities (modified). |
| blob_center | Reference to the blob's center position (modified). |
| params | Blob appearance parameters (read-only). |
| width | The width of the widget area. |
| height | The height of the widget area. |
| void BlobPhysics::SetLastWindowPos | ( | const QPointF & | position | ) |
Sets the last known position of the window. Used by CalculateWindowVelocity.
| position | The window position. |
|
static |
Applies a smoothing filter to the blob's shape. Moves each control point slightly towards the average position of its neighbors.
| control_points | Reference to the vector of control point positions (modified). |
|
static |
Gradually moves control points towards an ideal circular/organic shape when the blob is idle. Helps prevent the blob from collapsing or drifting excessively when not moving.
| control_points | Reference to the vector of control point positions (modified). |
| blob_center | The current center position of the blob (read-only). |
| blob_radius | The target average radius of the blob. |
| stabilization_rate | The rate at which points move towards the ideal shape (0.0 to 1.0). |
|
static |
Updates the blob physics using a standard sequential approach. Iterates through each control point, calculates forces, applies damping, updates velocity and position. Includes safety checks for vector sizes.
| control_points | Reference to the vector of current control point positions (modified). |
| target_points | Reference to the vector of target control point positions (read-only). |
| velocity | Reference to the vector of current control point velocities (modified). |
| blob_center | The current center position of the blob (read-only). |
| params | Blob appearance parameters (read-only). |
| physics_params | Blob physics parameters (read-only). |
|
static |
Updates the blob physics using an optimized Structure-of-Arrays (SoA) approach with OpenMP parallelization. Suitable for a large number of control points. Converts data to separate float arrays for better cache performance and vectorization. Includes safety checks for vector sizes.
| control_points | Reference to the vector of current control point positions (modified). |
| target_points | Reference to the vector of target control point positions (read-only). |
| velocity | Reference to the vector of current control point velocities (modified). |
| blob_center | The current center position of the blob (read-only). |
| params | Blob appearance parameters (read-only). |
| physics_params | Blob physics parameters (read-only). |
| void BlobPhysics::UpdatePhysicsParallel | ( | std::vector< QPointF > & | control_points, |
| std::vector< QPointF > & | target_points, | ||
| std::vector< QPointF > & | velocity, | ||
| const QPointF & | blob_center, | ||
| const BlobConfig::BlobParameters & | params, | ||
| const BlobConfig::PhysicsParameters & | physics_params ) |
Updates the blob physics using QtConcurrent for parallel processing across multiple threads. Divides the work into batches and processes them using the internal thread pool. Falls back to UpdatePhysicsOptimized or UpdatePhysics for different point counts. Includes safety checks for vector sizes and thread ranges.
| control_points | Reference to the vector of current control point positions (modified). |
| target_points | Reference to the vector of target control point positions (read-only). |
| velocity | Reference to the vector of current control point velocities (modified). |
| blob_center | The current center position of the blob (read-only). |
| params | Blob appearance parameters (read-only). |
| physics_params | Blob physics parameters (read-only). |
|
static |
Checks if any control points or velocities contain invalid values (NaN, infinity). If invalid points are found, reset the entire blob shape to a default organic form around the center.
| control_points | Reference to the vector of control point positions (modified). |
| velocity | Reference to the vector of control point velocities (modified). |
| blob_center | The current center position of the blob (read-only). |
| blob_radius | The target average radius of the blob. |
|
private |
Stores the last known window position for velocity calculation.
|
private |
Stores the last calculated window velocity.
|
private |
Timer used for calculating delta time in velocity calculations.
|
private |
Thread pool used for parallel physics updates (UpdatePhysicsParallel).