Skip to main content

src/CorePlotting/Layout/CanvasCoordinateSystem.hpp

src/CorePlotting/Layout/CanvasCoordinateSystem.hpp

Canvas coordinate system and scaling utilities for overlay rendering. More…

Classes

Name
struct CanvasCoordinateSystem
The logical resolution that overlay data coordinates are interpreted against.
struct ScalingFactors
X and Y scaling factors for mapping data coordinates to canvas pixels.

Types

Name
enum class CoordinateMappingMode { ScaleToCanvas, SubsetOfCanvas}
How a data object’s ImageSize relates to the canvas coordinate system.
enum class CanvasCoordinateSource { Default, Media, DataObject, UserOverride}
Source that determined the canvas coordinate system resolution.

Functions

Name
ScalingFactors computeScalingFactors(int canvas_pixel_width, int canvas_pixel_height, CanvasCoordinateSystem const & coord_system, ImageSize const & data_image_size, CoordinateMappingMode mapping_mode)
Compute scaling factors for mapping data coordinates to canvas pixel space.
ScalingFactors computeInverseScalingFactors(int canvas_pixel_width, int canvas_pixel_height, CanvasCoordinateSystem const & coord_system)
Compute the inverse scaling (canvas pixels → coordinate system units).

Detailed Description

Canvas coordinate system and scaling utilities for overlay rendering.

Provides the CanvasCoordinateSystem struct (the logical resolution overlays are plotted into), CoordinateMappingMode (how per-data ImageSize relates to the canvas), and a free function computeScalingFactors() that replaces the ad-hoc getXAspect/ getYAspect pattern in Media_Window.

Types Documentation

enum CoordinateMappingMode

Enumerator Value Description
ScaleToCanvas Data covers the full canvas extent at its own resolution. xAspect = canvasPixelWidth / data.imageSize.width
SubsetOfCanvas Data occupies a spatial sub-region; coordinates are in canvas-coordinate-system units already. xAspect = canvasPixelWidth / canvasCoordSystem.width

How a data object’s ImageSize relates to the canvas coordinate system.

Controls whether overlay coordinates are stretched to fill the full canvas (ScaleToCanvas) or interpreted as already being in canvas-coordinate-system units (SubsetOfCanvas).

enum CanvasCoordinateSource

Enumerator Value Description
Default Fallback 640×480.
Media From active MediaData.
DataObject From first data object with defined ImageSize.
UserOverride Explicit user setting.

Source that determined the canvas coordinate system resolution.

Functions Documentation

function computeScalingFactors

inline ScalingFactors computeScalingFactors(
    int canvas_pixel_width,
    int canvas_pixel_height,
    CanvasCoordinateSystem const & coord_system,
    ImageSize const & data_image_size,
    CoordinateMappingMode mapping_mode
)

Compute scaling factors for mapping data coordinates to canvas pixel space.

Parameters:

  • canvas_pixel_width Width of the canvas in display pixels
  • canvas_pixel_height Height of the canvas in display pixels
  • coord_system The logical canvas coordinate system
  • data_image_size The data object’s own ImageSize (-1,-1 if undefined)
  • mapping_mode How the data’s ImageSize relates to the canvas

Return: ScalingFactors to multiply data coordinates by

Precondition: canvas_pixel_width > 0 && canvas_pixel_height > 0

Behavior:

  • If data_image_size is undefined (-1,-1): uses coord_system dimensions (equivalent to SubsetOfCanvas with matching dimensions)
  • If mapping_mode == ScaleToCanvas: data coordinates span [0, data_image_size) and are stretched to fill [0, canvas_pixel_width/height)
  • If mapping_mode == SubsetOfCanvas: data coordinates are in coord_system units and scaled the same way as the coordinate system itself

function computeInverseScalingFactors

inline ScalingFactors computeInverseScalingFactors(
    int canvas_pixel_width,
    int canvas_pixel_height,
    CanvasCoordinateSystem const & coord_system
)

Compute the inverse scaling (canvas pixels → coordinate system units).

Parameters:

  • canvas_pixel_width Width of the canvas in display pixels
  • canvas_pixel_height Height of the canvas in display pixels
  • coord_system The logical canvas coordinate system

Return: ScalingFactors where x,y are pixels-to-coord multipliers

Used for converting mouse click positions back to data/media coordinates.

Source code


#ifndef CANVAS_COORDINATE_SYSTEM_HPP
#define CANVAS_COORDINATE_SYSTEM_HPP

#include "CoreGeometry/ImageSize.hpp"

#include <string>

enum class CoordinateMappingMode {
    ScaleToCanvas,

    SubsetOfCanvas
};

enum class CanvasCoordinateSource {
    Default,    
    Media,      
    DataObject, 
    UserOverride
};

struct CanvasCoordinateSystem {
    int width = 640; 
    int height = 480;
    CanvasCoordinateSource source = CanvasCoordinateSource::Default;
    std::string source_key;

    bool operator==(CanvasCoordinateSystem const & other) const {
        return width == other.width && height == other.height;
    }

    bool operator!=(CanvasCoordinateSystem const & other) const {
        return !(*this == other);
    }

    [[nodiscard]] ImageSize toImageSize() const {
        return {width, height};
    }

    static CanvasCoordinateSystem fromImageSize(ImageSize const & size,
                                                CanvasCoordinateSource src = CanvasCoordinateSource::Default,
                                                std::string key = {}) {
        return {size.width, size.height, src, std::move(key)};
    }
};

struct ScalingFactors {
    float x = 1.0f;
    float y = 1.0f;
};

inline ScalingFactors computeScalingFactors(int canvas_pixel_width,
                                            int canvas_pixel_height,
                                            CanvasCoordinateSystem const & coord_system,
                                            ImageSize const & data_image_size,
                                            CoordinateMappingMode mapping_mode) {
    ScalingFactors factors;

    if (data_image_size.isDefined() && mapping_mode == CoordinateMappingMode::ScaleToCanvas) {
        // Data fills the full canvas at its own resolution
        factors.x = static_cast<float>(canvas_pixel_width) / static_cast<float>(data_image_size.width);
        factors.y = static_cast<float>(canvas_pixel_height) / static_cast<float>(data_image_size.height);
    } else {
        // Use the canvas coordinate system (either SubsetOfCanvas or undefined ImageSize)
        factors.x = static_cast<float>(canvas_pixel_width) / static_cast<float>(coord_system.width);
        factors.y = static_cast<float>(canvas_pixel_height) / static_cast<float>(coord_system.height);
    }

    return factors;
}

inline ScalingFactors computeInverseScalingFactors(int canvas_pixel_width,
                                                   int canvas_pixel_height,
                                                   CanvasCoordinateSystem const & coord_system) {
    ScalingFactors factors;
    factors.x = static_cast<float>(coord_system.width) / static_cast<float>(canvas_pixel_width);
    factors.y = static_cast<float>(coord_system.height) / static_cast<float>(canvas_pixel_height);
    return factors;
}

#endif// CANVAS_COORDINATE_SYSTEM_HPP

Updated on 2026-11-13 at 20:25:42 +0000