Wavelength
Privacy-focused, cross-platform, and open-source communication application
Loading...
Searching...
No Matches
BlobPhysics Class Reference

Handles the physics simulation for the Blob animation. More...

#include <blob_physics.h>

Collaboration diagram for BlobPhysics:

Public Member Functions

 BlobPhysics ()
 Constructs a BlobPhysics object. Initializes the internal thread pool and starts the physics timer.
 
QVector2D 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.
 
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 &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.
 

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 &params, 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 &params, 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 &params, 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).
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ BlobPhysics()

BlobPhysics::BlobPhysics ( )

Constructs a BlobPhysics object. Initializes the internal thread pool and starts the physics timer.

Member Function Documentation

◆ CalculateWindowVelocity()

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.

Parameters
current_positionThe current position of the window.
Returns
The calculated window velocity as a QVector2D.

◆ ConstrainNeighborDistances()

void BlobPhysics::ConstrainNeighborDistances ( std::vector< QPointF > & control_points,
std::vector< QPointF > & velocity,
double min_distance,
double max_distance )
static

Enforces minimum and maximum distance constraints between neighboring control points. Adjusts positions and applies small velocity changes to maintain shape integrity.

Parameters
control_pointsReference to the vector of control point positions (modified).
velocityReference to the vector of control point velocities (modified).
min_distanceThe minimum allowed distance between neighbors.
max_distanceThe maximum allowed distance between neighbors.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetLastWindowPos()

QPointF BlobPhysics::GetLastWindowPos ( ) const
nodiscard

Gets the last recorded window position.

Returns
The last window position as a QPointF.

◆ GetLastWindowVelocity()

QVector2D BlobPhysics::GetLastWindowVelocity ( ) const
nodiscard

Gets the last calculated window velocity.

Returns
The last window velocity as a QVector2D.

◆ HandleBorderCollisions()

void BlobPhysics::HandleBorderCollisions ( std::vector< QPointF > & control_points,
std::vector< QPointF > & velocity,
QPointF & blob_center,
int width,
int height,
double restitution,
int padding )
static

Handles collisions between blob control points and the widget borders. Adjusts positions and reverses velocity components based on the restitution factor.

Parameters
control_pointsReference to the vector of control point positions (modified).
velocityReference to the vector of control point velocities (modified).
blob_centerReference to the blob's center position (modified).
widthThe width of the widget area.
heightThe height of the widget area.
restitutionThe bounciness factor (0.0 to 1.0).
paddingThe distance from the edge where collisions occur.
Here is the caller graph for this function:

◆ InitializeBlob()

void BlobPhysics::InitializeBlob ( std::vector< QPointF > & control_points,
std::vector< QPointF > & target_points,
std::vector< QPointF > & velocity,
QPointF & blob_center,
const BlobConfig::BlobParameters & params,
int width,
int height )
static

Initializes the blob's state vectors (control points, target points, velocity) and center position. Generates initial circular points.

Parameters
control_pointsReference to the vector of control point positions (modified).
target_pointsReference to the vector of target control point positions (modified).
velocityReference to the vector of control point velocities (modified).
blob_centerReference to the blob's center position (modified).
paramsBlob appearance parameters (read-only).
widthThe width of the widget area.
heightThe height of the widget area.
Here is the call graph for this function:

◆ SetLastWindowPos()

void BlobPhysics::SetLastWindowPos ( const QPointF & position)

Sets the last known position of the window. Used by CalculateWindowVelocity.

Parameters
positionThe window position.

◆ SmoothBlobShape()

void BlobPhysics::SmoothBlobShape ( std::vector< QPointF > & control_points)
static

Applies a smoothing filter to the blob's shape. Moves each control point slightly towards the average position of its neighbors.

Parameters
control_pointsReference to the vector of control point positions (modified).
Here is the caller graph for this function:

◆ StabilizeBlob()

void BlobPhysics::StabilizeBlob ( std::vector< QPointF > & control_points,
const QPointF & blob_center,
double blob_radius,
double stabilization_rate )
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.

Parameters
control_pointsReference to the vector of control point positions (modified).
blob_centerThe current center position of the blob (read-only).
blob_radiusThe target average radius of the blob.
stabilization_rateThe rate at which points move towards the ideal shape (0.0 to 1.0).
Here is the caller graph for this function:

◆ UpdatePhysics()

void BlobPhysics::UpdatePhysics ( std::vector< QPointF > & control_points,
const std::vector< QPointF > & target_points,
std::vector< QPointF > & velocity,
const QPointF & blob_center,
const BlobConfig::BlobParameters & params,
const BlobConfig::PhysicsParameters & physics_params )
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.

Parameters
control_pointsReference to the vector of current control point positions (modified).
target_pointsReference to the vector of target control point positions (read-only).
velocityReference to the vector of current control point velocities (modified).
blob_centerThe current center position of the blob (read-only).
paramsBlob appearance parameters (read-only).
physics_paramsBlob physics parameters (read-only).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdatePhysicsOptimized()

void BlobPhysics::UpdatePhysicsOptimized ( std::vector< QPointF > & control_points,
const std::vector< QPointF > & target_points,
std::vector< QPointF > & velocity,
const QPointF & blob_center,
const BlobConfig::BlobParameters & params,
const BlobConfig::PhysicsParameters & physics_params )
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.

Parameters
control_pointsReference to the vector of current control point positions (modified).
target_pointsReference to the vector of target control point positions (read-only).
velocityReference to the vector of current control point velocities (modified).
blob_centerThe current center position of the blob (read-only).
paramsBlob appearance parameters (read-only).
physics_paramsBlob physics parameters (read-only).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdatePhysicsParallel()

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.

Parameters
control_pointsReference to the vector of current control point positions (modified).
target_pointsReference to the vector of target control point positions (read-only).
velocityReference to the vector of current control point velocities (modified).
blob_centerThe current center position of the blob (read-only).
paramsBlob appearance parameters (read-only).
physics_paramsBlob physics parameters (read-only).
Here is the call graph for this function:

◆ ValidateAndRepairControlPoints()

bool BlobPhysics::ValidateAndRepairControlPoints ( std::vector< QPointF > & control_points,
std::vector< QPointF > & velocity,
const QPointF & blob_center,
double blob_radius )
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.

Parameters
control_pointsReference to the vector of control point positions (modified).
velocityReference to the vector of control point velocities (modified).
blob_centerThe current center position of the blob (read-only).
blob_radiusThe target average radius of the blob.
Returns
True if invalid points were found and repaired, false otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ last_window_position_

QPointF BlobPhysics::last_window_position_
private

Stores the last known window position for velocity calculation.

◆ last_window_velocity_

QVector2D BlobPhysics::last_window_velocity_
private

Stores the last calculated window velocity.

◆ physics_timer_

QElapsedTimer BlobPhysics::physics_timer_
private

Timer used for calculating delta time in velocity calculations.

◆ thread_pool_

QThreadPool BlobPhysics::thread_pool_
private

Thread pool used for parallel physics updates (UpdatePhysicsParallel).


The documentation for this class was generated from the following files: