diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..0ef60c0 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,180 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - uses: j178/prek-action@v3.0.0 + + build: + name: build (${{ matrix.name }}) + runs-on: ${{ matrix.runner }} + strategy: + fail-fast: false + matrix: + include: + - name: windows-clang-cl + runner: windows-latest + # Meson drops a bare "ccache" prefix for clang-cl but keeps + # "ccache.exe" as part of the compiler command. + cc: ccache.exe clang-cl + cxx: ccache.exe clang-cl + upload: true + platform: windows-x86_64 + dal_name: mmgr_dal_SimulatedCamera.dll + - name: windows-msvc + runner: windows-latest + cc: ccache cl + cxx: ccache cl + upload: false + - name: linux-gcc + runner: ubuntu-22.04 + cc: ccache gcc + cxx: ccache g++ + ldflags: -static-libstdc++ -static-libgcc + upload: true + platform: linux-x86_64 + dal_name: mmgr_dal_SimulatedCamera.so.0 + strip: strip --strip-unneeded + - name: macos-arm64 + runner: macos-15 + cc: ccache clang + cxx: ccache clang++ + upload: true + platform: macos-arm64 + dal_name: mmgr_dal_SimulatedCamera + strip: strip -x + - name: macos-x86_64 + runner: macos-15-intel + cc: ccache clang + cxx: ccache clang++ + upload: true + platform: macos-x86_64 + dal_name: mmgr_dal_SimulatedCamera + strip: strip -x + env: + CC: ${{ matrix.cc }} + CXX: ${{ matrix.cxx }} + MACOSX_DEPLOYMENT_TARGET: "11.0" + BOOST_VERSION: 1.92.0 + BOOST_VERSION_UNDERSCORED: 1_92_0 + defaults: + run: + shell: bash + steps: + - uses: actions/checkout@v7 + + # Strawberry Perl's bin directories contain tools (including ccache.exe) + # that shadow the ones we want. + - name: Remove Strawberry Perl from PATH (Windows) + if: runner.os == 'Windows' + shell: pwsh + run: | + $path = ($env:PATH -split ';' | Where-Object { $_ -notlike 'C:\Strawberry\*' }) -join ';' + "PATH=$path" >> $env:GITHUB_ENV + + # Git Bash puts Git's usr\bin (which has a GNU link.exe) at the front of + # PATH, shadowing MSVC's link.exe. Just recipes run under Git's sh, too. + - name: Remove Git's link.exe (Windows) + if: runner.os == 'Windows' + run: rm /usr/bin/link.exe + + - uses: astral-sh/setup-uv@v10.2.0 + + - name: Install Meson and Ninja + run: | + uv tool install meson + uv tool install ninja + + - uses: extractions/setup-just@v4 + + - name: Set up MSVC dev environment (Windows) + if: runner.os == 'Windows' + uses: compnerd/gha-setup-vsdevenv@452f99effa381b81c025ccfbdccaca509bff1b29 # v7 + + # We only use Boost headers, but Meson's Boost detection fails unless at + # least one compiled Boost library is present. So we also install (on + # Windows, build) Boost.Random, which is not linked. + - name: Install Boost (Linux) + if: runner.os == 'Linux' + run: sudo apt-get update && sudo apt-get install -y libboost-random-dev + + - name: Install Boost (macOS) + if: runner.os == 'macOS' + run: brew install boost + + - name: Cache Boost (Windows) + if: runner.os == 'Windows' + id: cache-boost + uses: actions/cache@v6 + with: + path: ${{ runner.temp }}/boost + key: boost-random-${{ env.BOOST_VERSION }}-windows + + - name: Build Boost (Windows) + if: runner.os == 'Windows' && steps.cache-boost.outputs.cache-hit != 'true' + shell: pwsh + working-directory: ${{ runner.temp }} + run: | + $PSNativeCommandUseErrorActionPreference = $true + Remove-Item Env:CC, Env:CXX + curl.exe -fsSL -o boost.tar.gz "https://archives.boost.io/release/$env:BOOST_VERSION/source/boost_$env:BOOST_VERSION_UNDERSCORED.tar.gz" + tar -xzf boost.tar.gz + cd "boost_$env:BOOST_VERSION_UNDERSCORED" + .\bootstrap.bat + .\b2 -j"$env:NUMBER_OF_PROCESSORS" --with-random --layout=system ` + --prefix="$env:RUNNER_TEMP\boost" ` + link=static runtime-link=shared variant=release address-model=64 ` + install + + - name: Set BOOST_ROOT (Windows) + if: runner.os == 'Windows' + shell: pwsh + run: '"BOOST_ROOT=$env:RUNNER_TEMP\boost" >> $env:GITHUB_ENV' + + - uses: hendrikmuhs/ccache-action@v1.2 + with: + key: ${{ matrix.name }} + + - name: Enable ccache depend mode (Windows) + if: startsWith(matrix.runner, 'windows-') + run: ccache --set-config=depend_mode=true + + - name: Configure + env: + LDFLAGS: ${{ matrix.ldflags }} + run: just configure-for-release -Dbenchmarks=enabled + + - name: Build + run: just build + + - name: Test + run: meson test -C builddir --print-errorlogs + + - name: Stage artifact + if: matrix.upload + run: | + mkdir dist + cp builddir/SimulatedCamera.mmdev "dist/${{ matrix.dal_name }}" + + - name: Strip artifact + if: matrix.upload && matrix.strip + run: ${{ matrix.strip }} "dist/${{ matrix.dal_name }}" + + - uses: actions/upload-artifact@v7 + if: matrix.upload + with: + name: mmdev-SimulatedCamera-${{ matrix.platform }} + path: dist/ + if-no-files-found: error diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..ddd33a8 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,10 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: check-yaml + args: ["--allow-multiple-documents"] + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v23.1.1 + hooks: + - id: clang-format diff --git a/SimCam.h b/SimCam.h index 93a0186..c07c02b 100644 --- a/SimCam.h +++ b/SimCam.h @@ -107,15 +107,13 @@ class SimCam : public CCameraBase { // FOV center is -stagePosition (needed for tiles to align). const double fovCenterX = xy.first; const double fovCenterY = -xy.second; - const double x = - fovCenterX - umPerPx * (double(roiX_) - double(sensorWidth_) / 2.0); - const double y = - fovCenterY - - umPerPx * (double(roiY_) - double(sensorHeight_) / 2.0); + const double x = fovCenterX - umPerPx * (double(roiX_) - + double(sensorWidth_) / 2.0); + const double y = fovCenterY - umPerPx * (double(roiY_) - + double(sensorHeight_) / 2.0); // Derive intensity using epi-illumination formula - const double intensity = 2800.0 * GetExposure() * - GetBinning() * GetBinning() * - (na * na * na * na) / + const double intensity = 2800.0 * GetExposure() * GetBinning() * + GetBinning() * (na * na * na * na) / (magnification * magnification); specimen_.Draw(snapBuffer_.get(), x, y, z, roiWidth_, roiHeight_, umPerPx, na, intensity); diff --git a/SimHub.h b/SimHub.h index 6bff17b..8070936 100644 --- a/SimHub.h +++ b/SimHub.h @@ -31,22 +31,16 @@ class SimHub : public HubBase { getFocusUmFunc_ = f; } - template void SetGetXYUmFunction(F f) { - getXYUmFunc_ = f; - } + template void SetGetXYUmFunction(F f) { getXYUmFunc_ = f; } template void SetGetMagnificationFunction(F f) { getMagnificationFunc_ = f; } - template void SetGetNAFunction(F f) { - getNAFunc_ = f; - } + template void SetGetNAFunction(F f) { getNAFunc_ = f; } double GetFocusUm() { return getFocusUmFunc_(); } - std::pair GetXYUm() { - return getXYUmFunc_(); - } + std::pair GetXYUm() { return getXYUmFunc_(); } double GetMagnification() { return getMagnificationFunc_(); } double GetNA() { return getNAFunc_(); } }; \ No newline at end of file diff --git a/SimObjectiveTurret.h b/SimObjectiveTurret.h index e264843..869a8c0 100644 --- a/SimObjectiveTurret.h +++ b/SimObjectiveTurret.h @@ -1,7 +1,7 @@ #pragma once -#include "SimHub.h" #include "DeviceBase.h" +#include "SimHub.h" #include #include @@ -26,14 +26,12 @@ class SimObjectiveTurret : public CStateDeviceBase { {100, 1.4, "Oil"}, }}; -public: - SimObjectiveTurret(std::string name): name_(std::move(name)) { + public: + SimObjectiveTurret(std::string name) : name_(std::move(name)) { InitializeDefaultErrorMessages(); } - ~SimObjectiveTurret() { - Shutdown(); - } + ~SimObjectiveTurret() { Shutdown(); } int Initialize() final { int ret{}; @@ -54,14 +52,15 @@ class SimObjectiveTurret : public CStateDeviceBase { // State // ----- - CPropertyAction* pAct = new CPropertyAction (this, &SimObjectiveTurret::OnState); + CPropertyAction *pAct = + new CPropertyAction(this, &SimObjectiveTurret::OnState); ret = CreateIntegerProperty(MM::g_Keyword_State, 0, false, pAct); if (ret != DEVICE_OK) return ret; // Label // ----- - pAct = new CPropertyAction (this, &CStateBase::OnLabel); + pAct = new CPropertyAction(this, &CStateBase::OnLabel); ret = CreateStringProperty(MM::g_Keyword_Label, "", false, pAct); if (ret != DEVICE_OK) return ret; @@ -88,21 +87,24 @@ class SimObjectiveTurret : public CStateDeviceBase { return DEVICE_OK; } - void GetName(char* name) const final { + void GetName(char *name) const final { CDeviceUtils::CopyLimitedString(name, name_.c_str()); } - bool Busy() {return false;}; + bool Busy() { return false; }; - unsigned long GetNumberOfPositions() const {return static_cast(objectives_.size());} + unsigned long GetNumberOfPositions() const { + return static_cast(objectives_.size()); + } - int OnState(MM::PropertyBase* pProp, MM::ActionType eAct) { + int OnState(MM::PropertyBase *pProp, MM::ActionType eAct) { if (eAct == MM::BeforeGet) { pProp->Set(state_); } else if (eAct == MM::AfterSet) { long newState; pProp->Get(newState); - if (newState >= 0 && newState < static_cast(objectives_.size())) { + if (newState >= 0 && + newState < static_cast(objectives_.size())) { state_ = newState; } else { pProp->Set(state_); @@ -112,7 +114,7 @@ class SimObjectiveTurret : public CStateDeviceBase { return DEVICE_OK; } -private: + private: bool initialized_ = false; std::string name_; long state_ = 0; diff --git a/SimXY.h b/SimXY.h index 3b64831..6ef5b6d 100644 --- a/SimXY.h +++ b/SimXY.h @@ -163,8 +163,7 @@ class SimXY : public CXYStageBase> { int Shutdown() final { auto *hub = static_cast(this->GetParentHub()); - hub->SetGetXYUmFunction( - [] { return std::make_pair(0.0, 0.0); }); + hub->SetGetXYUmFunction([] { return std::make_pair(0.0, 0.0); }); model_.Halt(); delayer_.CancelAll(); return DEVICE_OK; diff --git a/SimulatedSpecimen.h b/SimulatedSpecimen.h index 7da9092..6da4877 100644 --- a/SimulatedSpecimen.h +++ b/SimulatedSpecimen.h @@ -131,7 +131,8 @@ template class SimulatedSpecimen { } // Defocus - const auto sigmaUm = GaussianSigmaForDefocus(float(z_um), float(na), 1.33f); + const auto sigmaUm = + GaussianSigmaForDefocus(float(z_um), float(na), 1.33f); const auto sigmaPixels = sigmaUm / float(um_per_px); FastGaussian2D(fImage.data(), width, height, sigmaPixels); diff --git a/justfile b/justfile index 26ab3d5..6ceda43 100644 --- a/justfile +++ b/justfile @@ -15,10 +15,11 @@ configure BUILDTYPE *FLAGS: {{FLAGS}} # Configure with optional features enabled -configure-for-release: +configure-for-release *FLAGS: @just configure release \ -Duse_boost=enabled \ - -Duse_simd=enabled -Dsimd_dynamic_dispatch=enabled + -Duse_simd=enabled -Dsimd_dynamic_dispatch=enabled \ + {{FLAGS}} _configure_if_not_configured: #!/usr/bin/env bash