From 6763e9e71fcb8e41f207a2b81af972789944c327 Mon Sep 17 00:00:00 2001 From: Gabriel Selzer Date: Fri, 11 Sep 2026 15:10:32 -0500 Subject: [PATCH 1/4] Add in a Puncta channel --- SimCam.h | 43 ++++++- SimulatedSpecimen.h | 165 ------------------------- Specimen.h | 240 ++++++++++++++++++++++++++++++++++++ bench_SimulatedSpecimen.cpp | 24 +++- test_SimulatedCamera.cpp | 2 +- 5 files changed, 300 insertions(+), 174 deletions(-) delete mode 100644 SimulatedSpecimen.h create mode 100644 Specimen.h diff --git a/SimCam.h b/SimCam.h index 93a0186..27119ce 100644 --- a/SimCam.h +++ b/SimCam.h @@ -1,7 +1,7 @@ #pragma once #include "SimHub.h" -#include "SimulatedSpecimen.h" +#include "Specimen.h" #include "DeviceBase.h" @@ -24,7 +24,16 @@ class SimCam : public CCameraBase { static constexpr unsigned sensorWidth_ = 512; static constexpr unsigned sensorHeight_ = 512; - SimulatedSpecimen specimen_; + // Allowed values for the MM::g_Keyword_Channel ("Channel") property, + // which selects which simulated specimen is imaged. Both specimens are + // always kept around (and keep their own state across switches), so + // toggling the property is just a choice of which one to render. + static constexpr const char *channelFilaments_ = "Filaments"; + static constexpr const char *channelPuncta_ = "Puncta"; + + FilamentSpecimen filamentSpecimen_; + PunctaSpecimen punctaSpecimen_; + std::string channel_ = channelFilaments_; // Camera state double exposure_ms_ = 100.0; @@ -72,6 +81,25 @@ class SimCam : public CCameraBase { assert(ret == DEVICE_OK); ret = AddAllowedValue(MM::g_Keyword_Binning, "1"); assert(ret == DEVICE_OK); + + ret = CreateProperty( + MM::g_Keyword_Channel, channel_.c_str(), MM::String, false, + new MM::ActionLambda( + [this](MM::PropertyBase *pProp, MM::ActionType eAct) { + if (eAct == MM::BeforeGet) { + pProp->Set(channel_.c_str()); + } else if (eAct == MM::AfterSet) { + std::string value; + pProp->Get(value); + channel_ = value; + } + return DEVICE_OK; + })); + assert(ret == DEVICE_OK); + ret = AddAllowedValue(MM::g_Keyword_Channel, channelFilaments_); + assert(ret == DEVICE_OK); + ret = AddAllowedValue(MM::g_Keyword_Channel, channelPuncta_); + assert(ret == DEVICE_OK); (void)ret; return DEVICE_OK; @@ -117,8 +145,15 @@ class SimCam : public CCameraBase { GetBinning() * GetBinning() * (na * na * na * na) / (magnification * magnification); - specimen_.Draw(snapBuffer_.get(), x, y, z, roiWidth_, roiHeight_, - umPerPx, na, intensity); + if (channel_ == channelPuncta_) { + punctaSpecimen_.Draw(snapBuffer_.get(), x, y, z, roiWidth_, + roiHeight_, umPerPx, na, + intensity); + } else { + filamentSpecimen_.Draw(snapBuffer_.get(), x, y, z, roiWidth_, + roiHeight_, umPerPx, na, + intensity); + } std::chrono::duration exposure(GetExposure()); const auto finishTime = diff --git a/SimulatedSpecimen.h b/SimulatedSpecimen.h deleted file mode 100644 index 7da9092..0000000 --- a/SimulatedSpecimen.h +++ /dev/null @@ -1,165 +0,0 @@ -#pragma once - -#include "Gaussian2DFilter.h" - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef USE_BOOST_RANDOM -#include -namespace rnd = boost::random; -#else -#include -namespace rnd = std; -#endif - -constexpr double PI = 3.1415926535897; - -template -inline F GaussianSigmaForDefocus(F defocus_um, F numericalAperture, - F refractiveIndex) { - const auto radius = - numericalAperture * std::fabs(defocus_um) / refractiveIndex; - // Apply an (arbitrary) multiplier and minimum. - return F(0.5) * radius + F(1.0); -} - -// Fast Poisson sampling (avoid std::poisson_distribution, which is slow to -// construct for each sample of different lambda). -// - For small lambda: Knuth DE, The Art of Computer Programming, Volume 2: -// Seminumerical Algorithms, 3rd ed, section 3.4.1.F.3. -// - For large lambda: Gaussian approximation N(lambda, sqrt(lambda)) -template -F FastPoisson(F lambda, RNG &rng, - rnd::uniform_real_distribution &uniformDist) { - if (lambda > F(10.0)) { - rnd::normal_distribution gaussianDist(lambda, std::sqrt(lambda)); - return std::max(F(0), gaussianDist(rng)); - } else { - F const L = std::exp(-lambda); - F p = 1.0; - int k = 0; - do { - ++k; - p *= uniformDist(rng); - } while (p > L); - return static_cast(k - 1); - } -} - -template class SimulatedSpecimen { - struct Filament { - double x0, y0, x1, y1; - }; - - rnd::mt19937 rng_; - std::vector filaments_; - - public: - explicit SimulatedSpecimen() { - using std::cos; - using std::sin; - rnd::normal_distribution<> xy0Distrib(0.0, 1000.0); - rnd::uniform_real_distribution<> thetaDistrib(0.0, 2.0 * PI); - rnd::exponential_distribution<> lenDistrib(1e-3); - for (int i = 0; i < 1000; ++i) { - const double x0 = xy0Distrib(rng_); - const double y0 = xy0Distrib(rng_); - const double theta = thetaDistrib(rng_); - const double len = lenDistrib(rng_); - const double x1 = x0 + len * cos(theta); - const double y1 = y0 + len * sin(theta); - filaments_.push_back({x0, y0, x1, y1}); - } - } - - void Draw(T *buffer, double x_um, double y_um, double z_um, - std::size_t width, std::size_t height, double um_per_px, - double na, double intensity) { - const auto &filaments = filaments_; - - BLImage img(static_cast(width), static_cast(height), - BL_FORMAT_XRGB32); - BLContext ctx(img); - ctx.clearAll(); - - ctx.scale(1.0 / um_per_px); - ctx.translate(x_um, y_um); - - BLPath origin_marker; - origin_marker.moveTo(0.0, 0.0); - origin_marker.lineTo(50.0, 50.0); - origin_marker.addCircle(BLCircle(0.0, 0.0, 50.0)); - ctx.strokePath(origin_marker, BLRgba32(0xaaaaaaaa)); - - for (const Filament &f : filaments) { - BLPath path; - path.moveTo(f.x0, f.y0); - path.lineTo(f.x1, f.y1); - ctx.strokePath(path, BLRgba32(0xffffffff)); - } - - ctx.end(); - - BLImageData data; - BLResult status = img.getData(&data); - if (status != BL_SUCCESS) { - std::memset(buffer, 0, sizeof(T) * width * height); - return; // Give up (shouldn't happen). - } - - const auto *pix = static_cast(data.pixelData); - - // Stride is the stride of scanlines; negative stride means bottom-up. - std::vector fImage(width * height); - const std::intptr_t stride = data.stride / sizeof(std::uint32_t); - const std::intptr_t start = stride >= 0 ? 0 : (height - 1) * (-stride); - for (std::intptr_t j = 0; j < std::intptr_t(height); ++j) { - for (std::intptr_t i = 0; i < std::intptr_t(width); ++i) { - const auto p = pix[start + i + j * stride]; - // Green sample - fImage[i + j * width] = static_cast((p >> 8) & 0xff); - } - } - - // Defocus - 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); - - // Scale by intensity - std::transform(fImage.begin(), fImage.end(), fImage.begin(), - [i = float(intensity)](float p) { return p * i; }); - - // Shot noise - auto uniformDistForPoisson = - rnd::uniform_real_distribution(0.0, 1.0); - std::transform( - fImage.begin(), fImage.end(), fImage.begin(), [&](float p) { - return p > 0.0f ? FastPoisson(p, rng_, uniformDistForPoisson) - : p; - }); - - // Gaussian (~read) noise (TODO Adjustable? Scale?) - // and dark offset (TODO adjustable?) - auto noiseDistrib = rnd::normal_distribution(0.0, 50.0); - const float darkOffset = 100.0f; - std::transform( - fImage.begin(), fImage.end(), fImage.begin(), - [&](float p) { return p + noiseDistrib(rng_) + darkOffset; }); - - // Clamp to pixel type range - std::transform(fImage.begin(), fImage.end(), buffer, [](float v) { - return static_cast(std::clamp( - std::round(v), 0.0f, float(std::numeric_limits::max()))); - }); - } -}; diff --git a/Specimen.h b/Specimen.h new file mode 100644 index 0000000..17569fb --- /dev/null +++ b/Specimen.h @@ -0,0 +1,240 @@ +#pragma once + +#include "Gaussian2DFilter.h" + +#include + +#include +#include +#include +#include +#include +#include + +#ifdef USE_BOOST_RANDOM +#include +namespace rnd = boost::random; +#else +#include +namespace rnd = std; +#endif + +constexpr double PI = 3.1415926535897; + +template +inline F GaussianSigmaForDefocus(F defocus_um, F numericalAperture, + F refractiveIndex) { + const auto radius = + numericalAperture * std::fabs(defocus_um) / refractiveIndex; + // Apply an (arbitrary) multiplier and minimum. + return F(0.5) * radius + F(1.0); +} + +// Fast Poisson sampling (avoid std::poisson_distribution, which is slow to +// construct for each sample of different lambda). +// - For small lambda: Knuth DE, The Art of Computer Programming, Volume 2: +// Seminumerical Algorithms, 3rd ed, section 3.4.1.F.3. +// - For large lambda: Gaussian approximation N(lambda, sqrt(lambda)) +template +F FastPoisson(F lambda, RNG &rng, + rnd::uniform_real_distribution &uniformDist) { + if (lambda > F(10.0)) { + rnd::normal_distribution gaussianDist(lambda, std::sqrt(lambda)); + return std::max(F(0), gaussianDist(rng)); + } else { + F const L = std::exp(-lambda); + F p = 1.0; + int k = 0; + do { + ++k; + p *= uniformDist(rng); + } while (p > L); + return static_cast(k - 1); + } +} + +// Renders a specimen image: sets up the blend2d canvas (scaled/translated to +// specimen space and marked with an origin cross), invokes drawContent(ctx) +// to paint the specimen-specific structure, then applies the shared +// defocus blur, intensity scaling, and shot/read noise pipeline common to +// all simulated specimens. rng is used for the noise (and may also be used +// by drawContent). +template +void RenderSpecimenImage(T *buffer, double x_um, double y_um, double z_um, + std::size_t width, std::size_t height, + double um_per_px, double na, double intensity, + rnd::mt19937 &rng, ContentFn &&drawContent) { + BLImage img(static_cast(width), static_cast(height), + BL_FORMAT_XRGB32); + BLContext ctx(img); + ctx.clearAll(); + + ctx.scale(1.0 / um_per_px); + ctx.translate(x_um, y_um); + + BLPath origin_marker; + origin_marker.moveTo(0.0, 0.0); + origin_marker.lineTo(50.0, 50.0); + origin_marker.addCircle(BLCircle(0.0, 0.0, 50.0)); + ctx.strokePath(origin_marker, BLRgba32(0xaaaaaaaa)); + + drawContent(ctx); + + ctx.end(); + + BLImageData data; + BLResult status = img.getData(&data); + if (status != BL_SUCCESS) { + std::memset(buffer, 0, sizeof(T) * width * height); + return; // Give up (shouldn't happen). + } + + const auto *pix = static_cast(data.pixelData); + + // Stride is the stride of scanlines; negative stride means bottom-up. + std::vector fImage(width * height); + const std::intptr_t stride = data.stride / sizeof(std::uint32_t); + const std::intptr_t start = stride >= 0 ? 0 : (height - 1) * (-stride); + for (std::intptr_t j = 0; j < std::intptr_t(height); ++j) { + for (std::intptr_t i = 0; i < std::intptr_t(width); ++i) { + const auto p = pix[start + i + j * stride]; + // Green sample + fImage[i + j * width] = static_cast((p >> 8) & 0xff); + } + } + + // Defocus + 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); + + // Scale by intensity + std::transform(fImage.begin(), fImage.end(), fImage.begin(), + [i = float(intensity)](float p) { return p * i; }); + + // Shot noise + auto uniformDistForPoisson = + rnd::uniform_real_distribution(0.0, 1.0); + std::transform(fImage.begin(), fImage.end(), fImage.begin(), [&](float p) { + return p > 0.0f ? FastPoisson(p, rng, uniformDistForPoisson) : p; + }); + + // Gaussian (~read) noise (TODO Adjustable? Scale?) + // and dark offset (TODO adjustable?) + auto noiseDistrib = rnd::normal_distribution(0.0, 50.0); + const float darkOffset = 100.0f; + std::transform( + fImage.begin(), fImage.end(), fImage.begin(), + [&](float p) { return p + noiseDistrib(rng) + darkOffset; }); + + // Clamp to pixel type range + std::transform(fImage.begin(), fImage.end(), buffer, [](float v) { + return static_cast(std::clamp(std::round(v), 0.0f, + float(std::numeric_limits::max()))); + }); +} + +template class FilamentSpecimen { + struct Filament { + double x0, y0, x1, y1; + }; + + rnd::mt19937 rng_; + std::vector filaments_; + + public: + explicit FilamentSpecimen() { + using std::cos; + using std::sin; + rnd::normal_distribution<> xy0Distrib(0.0, 1000.0); + rnd::uniform_real_distribution<> thetaDistrib(0.0, 2.0 * PI); + rnd::exponential_distribution<> lenDistrib(1e-3); + for (int i = 0; i < 1000; ++i) { + const double x0 = xy0Distrib(rng_); + const double y0 = xy0Distrib(rng_); + const double theta = thetaDistrib(rng_); + const double len = lenDistrib(rng_); + const double x1 = x0 + len * cos(theta); + const double y1 = y0 + len * sin(theta); + filaments_.push_back({x0, y0, x1, y1}); + } + } + + void Draw(T *buffer, double x_um, double y_um, double z_um, + std::size_t width, std::size_t height, double um_per_px, + double na, double intensity) { + const auto &filaments = filaments_; + RenderSpecimenImage( + buffer, x_um, y_um, z_um, width, height, um_per_px, na, intensity, + rng_, [&](BLContext &ctx) { + for (const Filament &f : filaments) { + BLPath path; + path.moveTo(f.x0, f.y0); + path.lineTo(f.x1, f.y1); + ctx.strokePath(path, BLRgba32(0xffffffff)); + } + }); + } +}; + +template class PunctaSpecimen { + struct Focus { + double x, y, radius, brightness; // brightness in [0, 1] + }; + struct Nucleus { + double x, y, radius; + std::vector foci; + }; + + rnd::mt19937 rng_; + std::vector nuclei_; + + public: + explicit PunctaSpecimen() { + // Same field extent as FilamentSpecimen, so both channels image the + // same region of the (notional) coverslip. + rnd::uniform_real_distribution<> xy0Distrib(-2000.0, 2000.0); + rnd::uniform_real_distribution<> nucleusRadiusDistrib(15.0, 30.0); + rnd::uniform_int_distribution<> focusCountDistrib(1, 6); + rnd::uniform_real_distribution<> focusRadiusDistrib(0.5, 1.5); + rnd::uniform_real_distribution<> focusBrightnessDistrib(0.4, 1.0); + + for (int i = 0; i < 20; ++i) { + Nucleus nucleus; + nucleus.x = xy0Distrib(rng_); + nucleus.y = xy0Distrib(rng_); + nucleus.radius = nucleusRadiusDistrib(rng_); + + rnd::normal_distribution<> offsetDistrib(0.0, + nucleus.radius / 3.0); + const int nFoci = focusCountDistrib(rng_); + for (int f = 0; f < nFoci; ++f) { + nucleus.foci.push_back({nucleus.x + offsetDistrib(rng_), + nucleus.y + offsetDistrib(rng_), + focusRadiusDistrib(rng_), + focusBrightnessDistrib(rng_)}); + } + nuclei_.push_back(std::move(nucleus)); + } + } + + void Draw(T *buffer, double x_um, double y_um, double z_um, + std::size_t width, std::size_t height, double um_per_px, + double na, double intensity) { + const auto &nuclei = nuclei_; + RenderSpecimenImage( + buffer, x_um, y_um, z_um, width, height, um_per_px, na, intensity, + rng_, [&](BLContext &ctx) { + for (const Nucleus &n : nuclei) { + ctx.fillCircle(BLCircle(n.x, n.y, n.radius), + BLRgba32(40, 40, 40)); + for (const Focus &f : n.foci) { + const auto v = static_cast( + std::clamp(255.0 * f.brightness, 0.0, 255.0)); + ctx.fillCircle(BLCircle(f.x, f.y, f.radius), + BLRgba32(v, v, v)); + } + } + }); + } +}; diff --git a/bench_SimulatedSpecimen.cpp b/bench_SimulatedSpecimen.cpp index d6a4c53..d4f9112 100644 --- a/bench_SimulatedSpecimen.cpp +++ b/bench_SimulatedSpecimen.cpp @@ -1,13 +1,13 @@ #include "Gaussian2DFilter.h" -#include "SimulatedSpecimen.h" +#include "Specimen.h" #include #include #include -void bm_simulated_specimen_draw(benchmark::State &state) { - SimulatedSpecimen specimen; +void bm_filament_specimen_draw(benchmark::State &state) { + FilamentSpecimen specimen; const auto z_um = double(state.range(0)); const std::size_t width = 512, height = 512; std::vector buffer(width * height); @@ -17,7 +17,23 @@ void bm_simulated_specimen_draw(benchmark::State &state) { benchmark::DoNotOptimize(data); } } -BENCHMARK(bm_simulated_specimen_draw) +BENCHMARK(bm_filament_specimen_draw) + ->Arg(0) + ->Arg(50) + ->Unit(benchmark::kMillisecond); + +void bm_puncta_specimen_draw(benchmark::State &state) { + PunctaSpecimen specimen; + const auto z_um = double(state.range(0)); + const std::size_t width = 512, height = 512; + std::vector buffer(width * height); + auto *data = buffer.data(); + for ([[maybe_unused]] auto _ : state) { + specimen.Draw(data, 0.0, 0.0, z_um, width, height, 0.2, 1.4f, 1000.0); + benchmark::DoNotOptimize(data); + } +} +BENCHMARK(bm_puncta_specimen_draw) ->Arg(0) ->Arg(50) ->Unit(benchmark::kMillisecond); diff --git a/test_SimulatedCamera.cpp b/test_SimulatedCamera.cpp index 2250574..dd0e974 100644 --- a/test_SimulatedCamera.cpp +++ b/test_SimulatedCamera.cpp @@ -1,5 +1,5 @@ #include "Gaussian2DFilter.h" -#include "SimulatedSpecimen.h" +#include "Specimen.h" #include #include From d3206328c0f6dc516d02758f26e32ece3355452d Mon Sep 17 00:00:00 2001 From: Gabriel Selzer Date: Fri, 11 Sep 2026 15:18:56 -0500 Subject: [PATCH 2/4] Rename "Channel" property as "Mode" matching the demo camera --- SimCam.h | 22 +++++++++------------- Specimen.h | 4 +--- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/SimCam.h b/SimCam.h index 27119ce..978eba5 100644 --- a/SimCam.h +++ b/SimCam.h @@ -24,16 +24,12 @@ class SimCam : public CCameraBase { static constexpr unsigned sensorWidth_ = 512; static constexpr unsigned sensorHeight_ = 512; - // Allowed values for the MM::g_Keyword_Channel ("Channel") property, - // which selects which simulated specimen is imaged. Both specimens are - // always kept around (and keep their own state across switches), so - // toggling the property is just a choice of which one to render. - static constexpr const char *channelFilaments_ = "Filaments"; - static constexpr const char *channelPuncta_ = "Puncta"; + static constexpr const char *modeFilaments_ = "Filaments"; + static constexpr const char *modePuncta_ = "Puncta"; FilamentSpecimen filamentSpecimen_; PunctaSpecimen punctaSpecimen_; - std::string channel_ = channelFilaments_; + std::string mode_ = modeFilaments_; // Camera state double exposure_ms_ = 100.0; @@ -83,22 +79,22 @@ class SimCam : public CCameraBase { assert(ret == DEVICE_OK); ret = CreateProperty( - MM::g_Keyword_Channel, channel_.c_str(), MM::String, false, + "Mode", mode_.c_str(), MM::String, false, new MM::ActionLambda( [this](MM::PropertyBase *pProp, MM::ActionType eAct) { if (eAct == MM::BeforeGet) { - pProp->Set(channel_.c_str()); + pProp->Set(mode_.c_str()); } else if (eAct == MM::AfterSet) { std::string value; pProp->Get(value); - channel_ = value; + mode_ = value; } return DEVICE_OK; })); assert(ret == DEVICE_OK); - ret = AddAllowedValue(MM::g_Keyword_Channel, channelFilaments_); + ret = AddAllowedValue("Mode", modeFilaments_); assert(ret == DEVICE_OK); - ret = AddAllowedValue(MM::g_Keyword_Channel, channelPuncta_); + ret = AddAllowedValue("Mode", modePuncta_); assert(ret == DEVICE_OK); (void)ret; @@ -145,7 +141,7 @@ class SimCam : public CCameraBase { GetBinning() * GetBinning() * (na * na * na * na) / (magnification * magnification); - if (channel_ == channelPuncta_) { + if (mode_ == modePuncta_) { punctaSpecimen_.Draw(snapBuffer_.get(), x, y, z, roiWidth_, roiHeight_, umPerPx, na, intensity); diff --git a/Specimen.h b/Specimen.h index 17569fb..32336a0 100644 --- a/Specimen.h +++ b/Specimen.h @@ -179,7 +179,7 @@ template class FilamentSpecimen { template class PunctaSpecimen { struct Focus { - double x, y, radius, brightness; // brightness in [0, 1] + double x, y, radius, brightness; }; struct Nucleus { double x, y, radius; @@ -191,8 +191,6 @@ template class PunctaSpecimen { public: explicit PunctaSpecimen() { - // Same field extent as FilamentSpecimen, so both channels image the - // same region of the (notional) coverslip. rnd::uniform_real_distribution<> xy0Distrib(-2000.0, 2000.0); rnd::uniform_real_distribution<> nucleusRadiusDistrib(15.0, 30.0); rnd::uniform_int_distribution<> focusCountDistrib(1, 6); From acf49f1d15b37bd6fdfc92ef7f9d8b6c92167b25 Mon Sep 17 00:00:00 2001 From: Gabriel Selzer Date: Fri, 11 Sep 2026 15:57:11 -0500 Subject: [PATCH 3/4] More renaming --- SimCam.h | 18 +++++++++--------- Specimen.h | 36 ++++++++++++++++++------------------ bench_SimulatedSpecimen.cpp | 12 ++++++------ 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/SimCam.h b/SimCam.h index 978eba5..34ecb60 100644 --- a/SimCam.h +++ b/SimCam.h @@ -25,10 +25,10 @@ class SimCam : public CCameraBase { static constexpr unsigned sensorHeight_ = 512; static constexpr const char *modeFilaments_ = "Filaments"; - static constexpr const char *modePuncta_ = "Puncta"; + static constexpr const char *modeNuclei_ = "Nuclei"; - FilamentSpecimen filamentSpecimen_; - PunctaSpecimen punctaSpecimen_; + FilamentsSpecimen filamentsSpecimen_; + NucleiSpecimen nucleiSpecimen_; std::string mode_ = modeFilaments_; // Camera state @@ -94,7 +94,7 @@ class SimCam : public CCameraBase { assert(ret == DEVICE_OK); ret = AddAllowedValue("Mode", modeFilaments_); assert(ret == DEVICE_OK); - ret = AddAllowedValue("Mode", modePuncta_); + ret = AddAllowedValue("Mode", modeNuclei_); assert(ret == DEVICE_OK); (void)ret; @@ -141,14 +141,14 @@ class SimCam : public CCameraBase { GetBinning() * GetBinning() * (na * na * na * na) / (magnification * magnification); - if (mode_ == modePuncta_) { - punctaSpecimen_.Draw(snapBuffer_.get(), x, y, z, roiWidth_, + if (mode_ == modeNuclei_) { + nucleiSpecimen_.Draw(snapBuffer_.get(), x, y, z, roiWidth_, roiHeight_, umPerPx, na, intensity); } else { - filamentSpecimen_.Draw(snapBuffer_.get(), x, y, z, roiWidth_, - roiHeight_, umPerPx, na, - intensity); + filamentsSpecimen_.Draw(snapBuffer_.get(), x, y, z, roiWidth_, + roiHeight_, umPerPx, na, + intensity); } std::chrono::duration exposure(GetExposure()); diff --git a/Specimen.h b/Specimen.h index 32336a0..f90f3b9 100644 --- a/Specimen.h +++ b/Specimen.h @@ -134,7 +134,7 @@ void RenderSpecimenImage(T *buffer, double x_um, double y_um, double z_um, }); } -template class FilamentSpecimen { +template class FilamentsSpecimen { struct Filament { double x0, y0, x1, y1; }; @@ -143,7 +143,7 @@ template class FilamentSpecimen { std::vector filaments_; public: - explicit FilamentSpecimen() { + explicit FilamentsSpecimen() { using std::cos; using std::sin; rnd::normal_distribution<> xy0Distrib(0.0, 1000.0); @@ -177,25 +177,25 @@ template class FilamentSpecimen { } }; -template class PunctaSpecimen { - struct Focus { +template class NucleiSpecimen { + struct Punctum { double x, y, radius, brightness; }; struct Nucleus { double x, y, radius; - std::vector foci; + std::vector puncta; }; rnd::mt19937 rng_; std::vector nuclei_; public: - explicit PunctaSpecimen() { + explicit NucleiSpecimen() { rnd::uniform_real_distribution<> xy0Distrib(-2000.0, 2000.0); rnd::uniform_real_distribution<> nucleusRadiusDistrib(15.0, 30.0); - rnd::uniform_int_distribution<> focusCountDistrib(1, 6); - rnd::uniform_real_distribution<> focusRadiusDistrib(0.5, 1.5); - rnd::uniform_real_distribution<> focusBrightnessDistrib(0.4, 1.0); + rnd::uniform_int_distribution<> punctumCountDistrib(1, 6); + rnd::uniform_real_distribution<> punctumRadiusDistrib(0.5, 1.5); + rnd::uniform_real_distribution<> punctumBrightnessDistrib(0.4, 1.0); for (int i = 0; i < 20; ++i) { Nucleus nucleus; @@ -205,12 +205,12 @@ template class PunctaSpecimen { rnd::normal_distribution<> offsetDistrib(0.0, nucleus.radius / 3.0); - const int nFoci = focusCountDistrib(rng_); - for (int f = 0; f < nFoci; ++f) { - nucleus.foci.push_back({nucleus.x + offsetDistrib(rng_), - nucleus.y + offsetDistrib(rng_), - focusRadiusDistrib(rng_), - focusBrightnessDistrib(rng_)}); + const int nPuncta = punctumCountDistrib(rng_); + for (int p = 0; p < nPuncta; ++p) { + nucleus.puncta.push_back({nucleus.x + offsetDistrib(rng_), + nucleus.y + offsetDistrib(rng_), + punctumRadiusDistrib(rng_), + punctumBrightnessDistrib(rng_)}); } nuclei_.push_back(std::move(nucleus)); } @@ -226,10 +226,10 @@ template class PunctaSpecimen { for (const Nucleus &n : nuclei) { ctx.fillCircle(BLCircle(n.x, n.y, n.radius), BLRgba32(40, 40, 40)); - for (const Focus &f : n.foci) { + for (const Punctum &p : n.puncta) { const auto v = static_cast( - std::clamp(255.0 * f.brightness, 0.0, 255.0)); - ctx.fillCircle(BLCircle(f.x, f.y, f.radius), + std::clamp(255.0 * p.brightness, 0.0, 255.0)); + ctx.fillCircle(BLCircle(p.x, p.y, p.radius), BLRgba32(v, v, v)); } } diff --git a/bench_SimulatedSpecimen.cpp b/bench_SimulatedSpecimen.cpp index d4f9112..1321537 100644 --- a/bench_SimulatedSpecimen.cpp +++ b/bench_SimulatedSpecimen.cpp @@ -6,8 +6,8 @@ #include #include -void bm_filament_specimen_draw(benchmark::State &state) { - FilamentSpecimen specimen; +void bm_filaments_specimen_draw(benchmark::State &state) { + FilamentsSpecimen specimen; const auto z_um = double(state.range(0)); const std::size_t width = 512, height = 512; std::vector buffer(width * height); @@ -17,13 +17,13 @@ void bm_filament_specimen_draw(benchmark::State &state) { benchmark::DoNotOptimize(data); } } -BENCHMARK(bm_filament_specimen_draw) +BENCHMARK(bm_filaments_specimen_draw) ->Arg(0) ->Arg(50) ->Unit(benchmark::kMillisecond); -void bm_puncta_specimen_draw(benchmark::State &state) { - PunctaSpecimen specimen; +void bm_nuclei_specimen_draw(benchmark::State &state) { + NucleiSpecimen specimen; const auto z_um = double(state.range(0)); const std::size_t width = 512, height = 512; std::vector buffer(width * height); @@ -33,7 +33,7 @@ void bm_puncta_specimen_draw(benchmark::State &state) { benchmark::DoNotOptimize(data); } } -BENCHMARK(bm_puncta_specimen_draw) +BENCHMARK(bm_nuclei_specimen_draw) ->Arg(0) ->Arg(50) ->Unit(benchmark::kMillisecond); From b89c0a6539b7ecb3e85a69b1228454f58118ae84 Mon Sep 17 00:00:00 2001 From: "Mark A. Tsuchida" Date: Fri, 25 Sep 2026 12:19:05 -0500 Subject: [PATCH 4/4] Reformat with pre-commit settings --- SimCam.h | 32 +++++++++++++++----------------- Specimen.h | 38 +++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/SimCam.h b/SimCam.h index 81fb0f6..e3eb7cd 100644 --- a/SimCam.h +++ b/SimCam.h @@ -78,19 +78,19 @@ class SimCam : public CCameraBase { ret = AddAllowedValue(MM::g_Keyword_Binning, "1"); assert(ret == DEVICE_OK); - ret = CreateProperty( - "Mode", mode_.c_str(), MM::String, false, - new MM::ActionLambda( - [this](MM::PropertyBase *pProp, MM::ActionType eAct) { - if (eAct == MM::BeforeGet) { - pProp->Set(mode_.c_str()); - } else if (eAct == MM::AfterSet) { - std::string value; - pProp->Get(value); - mode_ = value; - } - return DEVICE_OK; - })); + ret = + CreateProperty("Mode", mode_.c_str(), MM::String, false, + new MM::ActionLambda([this](MM::PropertyBase *pProp, + MM::ActionType eAct) { + if (eAct == MM::BeforeGet) { + pProp->Set(mode_.c_str()); + } else if (eAct == MM::AfterSet) { + std::string value; + pProp->Get(value); + mode_ = value; + } + return DEVICE_OK; + })); assert(ret == DEVICE_OK); ret = AddAllowedValue("Mode", modeFilaments_); assert(ret == DEVICE_OK); @@ -141,12 +141,10 @@ class SimCam : public CCameraBase { (magnification * magnification); if (mode_ == modeNuclei_) { nucleiSpecimen_.Draw(snapBuffer_.get(), x, y, z, roiWidth_, - roiHeight_, umPerPx, na, - intensity); + roiHeight_, umPerPx, na, intensity); } else { filamentsSpecimen_.Draw(snapBuffer_.get(), x, y, z, roiWidth_, - roiHeight_, umPerPx, na, - intensity); + roiHeight_, umPerPx, na, intensity); } std::chrono::duration exposure(GetExposure()); diff --git a/Specimen.h b/Specimen.h index f90f3b9..024c3de 100644 --- a/Specimen.h +++ b/Specimen.h @@ -104,7 +104,8 @@ void RenderSpecimenImage(T *buffer, double x_um, double y_um, double z_um, } // 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); @@ -123,14 +124,14 @@ void RenderSpecimenImage(T *buffer, double x_um, double y_um, double z_um, // and dark offset (TODO adjustable?) auto noiseDistrib = rnd::normal_distribution(0.0, 50.0); const float darkOffset = 100.0f; - std::transform( - fImage.begin(), fImage.end(), fImage.begin(), - [&](float p) { return p + noiseDistrib(rng) + darkOffset; }); + std::transform(fImage.begin(), fImage.end(), fImage.begin(), [&](float p) { + return p + noiseDistrib(rng) + darkOffset; + }); // Clamp to pixel type range std::transform(fImage.begin(), fImage.end(), buffer, [](float v) { - return static_cast(std::clamp(std::round(v), 0.0f, - float(std::numeric_limits::max()))); + return static_cast(std::clamp( + std::round(v), 0.0f, float(std::numeric_limits::max()))); }); } @@ -164,16 +165,15 @@ template class FilamentsSpecimen { std::size_t width, std::size_t height, double um_per_px, double na, double intensity) { const auto &filaments = filaments_; - RenderSpecimenImage( - buffer, x_um, y_um, z_um, width, height, um_per_px, na, intensity, - rng_, [&](BLContext &ctx) { - for (const Filament &f : filaments) { - BLPath path; - path.moveTo(f.x0, f.y0); - path.lineTo(f.x1, f.y1); - ctx.strokePath(path, BLRgba32(0xffffffff)); - } - }); + RenderSpecimenImage(buffer, x_um, y_um, z_um, width, height, um_per_px, + na, intensity, rng_, [&](BLContext &ctx) { + for (const Filament &f : filaments) { + BLPath path; + path.moveTo(f.x0, f.y0); + path.lineTo(f.x1, f.y1); + ctx.strokePath(path, BLRgba32(0xffffffff)); + } + }); } }; @@ -204,7 +204,7 @@ template class NucleiSpecimen { nucleus.radius = nucleusRadiusDistrib(rng_); rnd::normal_distribution<> offsetDistrib(0.0, - nucleus.radius / 3.0); + nucleus.radius / 3.0); const int nPuncta = punctumCountDistrib(rng_); for (int p = 0; p < nPuncta; ++p) { nucleus.puncta.push_back({nucleus.x + offsetDistrib(rng_), @@ -225,12 +225,12 @@ template class NucleiSpecimen { rng_, [&](BLContext &ctx) { for (const Nucleus &n : nuclei) { ctx.fillCircle(BLCircle(n.x, n.y, n.radius), - BLRgba32(40, 40, 40)); + BLRgba32(40, 40, 40)); for (const Punctum &p : n.puncta) { const auto v = static_cast( std::clamp(255.0 * p.brightness, 0.0, 255.0)); ctx.fillCircle(BLCircle(p.x, p.y, p.radius), - BLRgba32(v, v, v)); + BLRgba32(v, v, v)); } } });