diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 21c588073..92f163f4f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -24,6 +24,7 @@ jobs: sudo apt update sudo apt install libegl1 sudo apt install libgl1 libxext6 libx11-6 + sudo apt install libtiff-dev libtiff-tools python -m pip install --upgrade pip pip install pylint pip install -e . diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d27a94982..15fc5c5ac 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -12,13 +12,11 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-latest, windows-latest, macos-14, macos-15-intel, macos-26] + os: [ubuntu-latest, windows-latest, macos-14, macos-26] env-type: [pip] include: - os: macos-14 llvm: llvm@15 - - os: macos-15-intel - llvm: llvm@18 - os: macos-26 llvm: llvm@20 steps: @@ -54,8 +52,10 @@ jobs: - name: Install libtiff (Windows) if: startsWith(matrix.os, 'windows') + shell: pwsh run: | - vcpkg install tiff + vcpkg install tiff:x64-windows-static + echo "CMAKE_ARGS=-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static" >> $env:GITHUB_ENV - name: Install libtiff (Ubuntu) if: startsWith(matrix.os, 'ubuntu') diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index c368beadb..e4ff0fd23 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -15,14 +15,11 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-latest, ubuntu-24.04-arm, windows-latest, macos-14, macos-15-intel, macos-26] + os: [ubuntu-latest, ubuntu-24.04-arm, windows-latest, macos-14, macos-26] include: - os: macos-14 llvm: llvm@15 macos_deployment_target: "14.0" - - os: macos-15-intel - llvm: llvm@18 - macos_deployment_target: "15.0" - os: macos-26 llvm: llvm@20 macos_deployment_target: "26.0" diff --git a/.github/workflows/wheels_testpypi.yml b/.github/workflows/wheels_testpypi.yml index 9c89a61de..9ebfbaba2 100644 --- a/.github/workflows/wheels_testpypi.yml +++ b/.github/workflows/wheels_testpypi.yml @@ -9,14 +9,12 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-latest, ubuntu-24.04-arm, windows-latest, macos-14, macos-15-intel, macos-latest] + os: [ubuntu-latest, ubuntu-24.04-arm, windows-latest, macos-14, macos-26] include: - os: macos-14 llvm: llvm@15 - - os: macos-15-intel - llvm: llvm@18 - - os: macos-15-latest - llvm: llvm@18 + - os: macos-26 + llvm: llvm@20 steps: - uses: actions/checkout@v4 diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c6b3a93d..646da7123 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,8 +5,9 @@ project(${SKBUILD_PROJECT_NAME} LANGUAGES CXX) # ---------------------------- # C++ standard # ---------------------------- -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # ---------------------------- # Ensure CMAKE_BUILD_TYPE and install config @@ -24,7 +25,11 @@ message(STATUS "CMake install config name: ${CMAKE_INSTALL_CONFIG_NAME}") # ---------------------------- find_package(pybind11 REQUIRED) find_package(OpenMP REQUIRED) - +#find_package(OpenCV REQUIRED) +find_package(TIFF REQUIRED) +message(STATUS "TIFF_FOUND = ${TIFF_FOUND}") +message(STATUS "TIFF_INCLUDE_DIR = ${TIFF_INCLUDE_DIR}") +message(STATUS "TIFF_LIBRARIES = ${TIFF_LIBRARIES}") # ---------------------------- # Eigen3 # ---------------------------- @@ -61,7 +66,7 @@ endif() function(apply_compile_flags target_name) if(MSVC) # Windows (MSVC) - target_compile_options(${target_name} PRIVATE /std:c++17 /openmp) + target_compile_options(${target_name} PRIVATE /openmp) if(CMAKE_BUILD_TYPE STREQUAL "Debug") message(STATUS "Applying MSVC debug flags for ${target_name}") target_compile_options(${target_name} PRIVATE /Od /Zi) @@ -71,7 +76,7 @@ function(apply_compile_flags target_name) endif() else() # Linux / macOS - target_compile_options(${target_name} PRIVATE -std=c++17 -fopenmp) + target_compile_options(${target_name} PRIVATE -fopenmp) if(CMAKE_BUILD_TYPE STREQUAL "Debug") message(STATUS "Applying debug flags for ${target_name}") target_compile_options(${target_name} PRIVATE -O0 -g) @@ -90,8 +95,8 @@ endfunction() file(GLOB COMMON_SRC_FILES src/pyvale/common_cpp/*.cpp) pybind11_add_module(common_cpp MODULE ${COMMON_SRC_FILES}) -target_include_directories(common_cpp PRIVATE src/pyvale/common_cpp) -target_link_libraries(common_cpp PRIVATE OpenMP::OpenMP_CXX Eigen3::Eigen) +target_include_directories(common_cpp PRIVATE src/pyvale/common_cpp ${TIFF_INCLUDE_DIR}) +target_link_libraries(common_cpp PRIVATE OpenMP::OpenMP_CXX Eigen3::Eigen ${TIFF_LIBRARIES}) apply_compile_flags(common_cpp) install(TARGETS common_cpp LIBRARY DESTINATION pyvale/common_cpp) @@ -100,11 +105,11 @@ install(TARGETS common_cpp LIBRARY DESTINATION pyvale/common_cpp) # ---------------------------- file(GLOB DIC_SRC_FILES src/pyvale/dic/cpp/*.cpp) -pybind11_add_module(dic2dcpp MODULE ${DIC_SRC_FILES} ${COMMON_SRC_FILES}) -target_include_directories(dic2dcpp PRIVATE src/pyvale/common_cpp src/pyvale/dic) -target_link_libraries(dic2dcpp PRIVATE OpenMP::OpenMP_CXX Eigen3::Eigen) -apply_compile_flags(dic2dcpp) -install(TARGETS dic2dcpp LIBRARY DESTINATION pyvale/dic) +pybind11_add_module(diccpp MODULE ${DIC_SRC_FILES} ${COMMON_SRC_FILES}) +target_include_directories(diccpp PRIVATE src/pyvale/common_cpp src/pyvale/dic ${TIFF_INCLUDE_DIR}) +target_link_libraries(diccpp PRIVATE OpenMP::OpenMP_CXX Eigen3::Eigen ${TIFF_LIBRARIES}) +apply_compile_flags(diccpp) +install(TARGETS diccpp LIBRARY DESTINATION pyvale/dic) # ---------------------------- # DIC Calibration @@ -112,8 +117,8 @@ install(TARGETS dic2dcpp LIBRARY DESTINATION pyvale/dic) file(GLOB CALIB_SRC_FILES src/pyvale/calib/cpp/*.cpp) pybind11_add_module(calibcpp MODULE ${CALIB_SRC_FILES} ${COMMON_SRC_FILES}) -target_include_directories(calibcpp PRIVATE src/pyvale/common_cpp src/pyvale/calib) -target_link_libraries(calibcpp PRIVATE OpenMP::OpenMP_CXX Eigen3::Eigen) +target_include_directories(calibcpp PRIVATE src/pyvale/common_cpp src/pyvale/calib ${TIFF_INCLUDE_DIR}) +target_link_libraries(calibcpp PRIVATE OpenMP::OpenMP_CXX Eigen3::Eigen ${TIFF_LIBRARIES}) apply_compile_flags(calibcpp) install(TARGETS calibcpp LIBRARY DESTINATION pyvale/calib) @@ -123,8 +128,8 @@ install(TARGETS calibcpp LIBRARY DESTINATION pyvale/calib) file(GLOB STRAIN_SRC_FILES src/pyvale/strain/cpp/*.cpp) pybind11_add_module(strain_cpp MODULE ${STRAIN_SRC_FILES} ${COMMON_SRC_FILES}) -target_include_directories(strain_cpp PRIVATE src/pyvale/common_cpp src/pyvale/strain) -target_link_libraries(strain_cpp PRIVATE OpenMP::OpenMP_CXX Eigen3::Eigen) +target_include_directories(strain_cpp PRIVATE src/pyvale/common_cpp src/pyvale/strain ${TIFF_INCLUDE_DIR}) +target_link_libraries(strain_cpp PRIVATE OpenMP::OpenMP_CXX Eigen3::Eigen ${TIFF_LIBRARIES}) apply_compile_flags(strain_cpp) install(TARGETS strain_cpp LIBRARY DESTINATION pyvale/strain) diff --git a/README.md b/README.md index ade4d5227..2aa480a08 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,57 @@ The python validation engine (`pyvale`) is your virtual engineering laboratory: We are actively developing dedicated tools for simulation and uncertainty quantification of imaging sensors including digital image correlation (DIC) and infra-red thermography (IRT). Check out the [documentation](https://computer-aided-validation-laboratory.github.io/pyvale/index.html) to get started with some of our examples. +## Quick Install +We recommend installing `pyvale` into a virtual environment of your choice as `pyvale` requires python 3.13. If you need help setting up your virtual environment and installing `pyvale` head over to the [installation guide](https://computer-aided-validation-laboratory.github.io/pyvale/install/install.html) in our docs. + +`pyvale` can be installed from pypi: +```shell +pip install pyvale +``` + +## Quick Demo: Digital Image Correlation +Below is a really quick example for setting up a DIC calculation. It's highly likely that your case will require a more tailored calculation configuration. + +**For further details please see the DIC [examples](https://computer-aided-validation-laboratory.github.io/pyvale/examples/examples_dic.html), [theory guide](https://computer-aided-validation-laboratory.github.io/pyvale/guide_theory/guide_theory_dic.html), [user guide](https://computer-aided-validation-laboratory.github.io/pyvale/guide_user/guide_dic.html) and [API](https://computer-aided-validation-laboratory.github.io/pyvale/pyvale.dic.html).** + +Define the Region of Interest (ROI): + +```python +import pyvale.dic as dic + +roi = dic.RegionOfInterest(ref_image="image0000.tiff") +roi.interactive_selection() +``` +run the DIC: + +```python +# use dic.calculate_3d for stereo +dic.calculate_2d(reference="image0000.tiff", + deformed="image*.tiff", + roi_mask=roi.mask, # built using ROI tool + seed=roi.seed, # built using ROI tool + subset_size=21, + subset_step=10) +``` +Import the results for any analysis/plotting: + +```python +dicdata = dic.import_2d(data="dic_results*.csv", # default result files prefix + delimiter=",") + + +import matplotlib.pyplot as plt +plt.pcolor(dicdata.ss_x, + dicdata.ss_y, + dicdata.u_px[0]) # horizontal displacement for 0th image +plt.show() +``` + + + + ## Quick Demo: Simulating Point Sensors -Here we demonstrate how `pyvale` can be used to simulate thermocouples and strain gauges applied to a [MOOSE](https://mooseframework.inl.gov/index.html) thermo-mechanical simulation of a fusion divertor armour heatsink. The figures below show visualisations of the virtual thermocouple and strain gauge locations on the simualtion mesh as well as time traces for each sensor over a series of simulated experiments. +`/pyvale` can be used to simulate thermocouples and strain gauges applied to a [MOOSE](https://mooseframework.inl.gov/index.html) thermo-mechanical simulation of a fusion divertor armour heatsink. The figures below show visualisations of the virtual thermocouple and strain gauge locations on the simualtion mesh as well as time traces for each sensor over a series of simulated experiments. The code to run the simulated experiments and produce the output shown here comes from [this example](https://computer-aided-validation-laboratory.github.io/pyvale/examples/basicsensorsim/ex0_quickstart.html). You can find more examples and details of `pyvale` python API in the `pyvale` [documentation](https://computer-aided-validation-laboratory.github.io/pyvale/index.html). @@ -18,15 +67,6 @@ The code to run the simulated experiments and produce the output shown here come |--|--| |*Thermocouple time traces over a series of simulated experiments.*|*Strain gauge time traces over a series of simulated experiments.*| - -## Quick Install -`pyvale` can be installed from pypi: -```shell -pip install pyvale -``` - -We recommend installing `pyvale` into a virtual environment of your choice as `pyvale` requires python 3.13. If you need help setting up your virtual environment and installing `pyvale` head over to the [installation guide](https://computer-aided-validation-laboratory.github.io/pyvale/install/install.html) in our docs. - ## Contributors The Computer Aided Validation Team at UKAEA: - Lloyd Fletcher ([ScepticalRabbit](https://github.com/ScepticalRabbit)), UK Atomic Energy Authority diff --git a/docs/source/_static/cal_target_flipped.png b/docs/source/_static/cal_target_flipped.png new file mode 120000 index 000000000..7672ce4e4 --- /dev/null +++ b/docs/source/_static/cal_target_flipped.png @@ -0,0 +1 @@ +../../../images/cal_target_flipped.png \ No newline at end of file diff --git a/docs/source/_static/dic_ex10_3d.png b/docs/source/_static/dic_ex10_3d.png new file mode 120000 index 000000000..2fb49f32c --- /dev/null +++ b/docs/source/_static/dic_ex10_3d.png @@ -0,0 +1 @@ +../../../images/dic_ex10_3d.png \ No newline at end of file diff --git a/docs/source/_static/dic_ex11_3d.png b/docs/source/_static/dic_ex11_3d.png new file mode 120000 index 000000000..8819317bf --- /dev/null +++ b/docs/source/_static/dic_ex11_3d.png @@ -0,0 +1 @@ +../../../images/dic_ex11_3d.png \ No newline at end of file diff --git a/docs/source/_static/dic_ex11_roi.png b/docs/source/_static/dic_ex11_roi.png new file mode 120000 index 000000000..de0cc7c42 --- /dev/null +++ b/docs/source/_static/dic_ex11_roi.png @@ -0,0 +1 @@ +../../../images/dic_ex11_roi.png \ No newline at end of file diff --git a/docs/source/_static/incremental_dark.png b/docs/source/_static/incremental_dark.png new file mode 120000 index 000000000..8f39bed3a --- /dev/null +++ b/docs/source/_static/incremental_dark.png @@ -0,0 +1 @@ +../../../images/incremental_dark.png \ No newline at end of file diff --git a/docs/source/_static/incremental_light.png b/docs/source/_static/incremental_light.png new file mode 120000 index 000000000..7775b0b45 --- /dev/null +++ b/docs/source/_static/incremental_light.png @@ -0,0 +1 @@ +../../../images/incremental_light.png \ No newline at end of file diff --git a/docs/source/api_py.rst b/docs/source/api_py.rst index b24792e01..e6da15e57 100644 --- a/docs/source/api_py.rst +++ b/docs/source/api_py.rst @@ -9,6 +9,7 @@ Detailed Python API pyvale.sensorsim pyvale.blender + pyvale.calib pyvale.dic pyvale.strain pyvale.mooseherder diff --git a/docs/source/examples/examples_dic.rst b/docs/source/examples/examples_dic.rst index 150543b07..ffcfc8818 100644 --- a/docs/source/examples/examples_dic.rst +++ b/docs/source/examples/examples_dic.rst @@ -6,10 +6,15 @@ DIC & Strain Calculations .. toctree:: :maxdepth: 1 - dic/ex1_region_of_interest.rst - dic/ex2_plate_with_hole.rst - dic/ex3_plate_with_hole_strain.rst - dic/ex4_dic_blender.rst - dic/ex5_dic_challenge.rst - dic/ex6_hrdic.rst + dic/ex01_region_of_interest.rst + dic/ex02_plate_with_hole.rst + dic/ex03_plate_with_hole_strain.rst + dic/ex04_dic_blender.rst + dic/ex05_dic_challenge.rst + dic/ex06_hrdic.rst + dic/ex07_incremental.rst + dic/ex08_calibration.rst + dic/ex09_stereo.rst + dic/ex10_stereo_platehole.rst + dic/ex11_dic_chal.rst diff --git a/docs/source/guide_theory/guide_theory.rst b/docs/source/guide_theory/guide_theory.rst index f22e2bc1b..c172f15e0 100644 --- a/docs/source/guide_theory/guide_theory.rst +++ b/docs/source/guide_theory/guide_theory.rst @@ -10,5 +10,6 @@ Module user guides :maxdepth: 1 guide_theory_dic.rst + guide_theory_dic_stereo.rst diff --git a/docs/source/guide_theory/guide_theory_dic.rst b/docs/source/guide_theory/guide_theory_dic.rst index 51fd44c64..c09e9d56b 100644 --- a/docs/source/guide_theory/guide_theory_dic.rst +++ b/docs/source/guide_theory/guide_theory_dic.rst @@ -1,6 +1,6 @@ .. _guide_theory_dic: -DIC Theory Overview +DIC ====================================== Digital Image Correlation (DIC) is a technique for measuring @@ -147,15 +147,17 @@ further updates would produce only negligible changes in the parameters and the cost function. -Sub-pixel Accuracy With Interpolation --------------------------------------- +Interpolation Methods for Sub-pixel Accuracy +--------------------------------------------- DIC aims for precision beyond the integer values defined by the image's pixel grid. Integer-pixel matching is a good start, but physical displacements do not perfectly map to pixel boundaries. To capture this, we refine the measurement to *sub-pixel* -accuracy using interpolation. Instead of treating the image as a discrete -array, we approximate it as a smooth surface. This is done using **cubic B-spline -interpolation**. +accuracy using interpolation. Pyvale supports two interpolation methods selectable via the +:code:`interpolation_routine` parameter in :code:`calculate_2d()` and :code:`calculate_3d()`: + +- **BSPLINE** (default): Cubic B-spline interpolation (described below). +- **HERMITE**: Hermite cubic interpolation, an alternative method that can be faster for some use cases. Cubic B-spline Interpolation ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/docs/source/guide_theory/guide_theory_dic_stereo.rst b/docs/source/guide_theory/guide_theory_dic_stereo.rst new file mode 100644 index 000000000..377112b4f --- /dev/null +++ b/docs/source/guide_theory/guide_theory_dic_stereo.rst @@ -0,0 +1,311 @@ +.. _guide_theory_dic_stereo: + +Stereo DIC +====================================== + +Stereo Digital Image Correlation (Stereo DIC) extends 2D DIC by using two +synchronised cameras instead of one. The idea is to measure the same material +point in the left and right image, then use the calibrated camera geometry to +triangulate its 3D position. Repeating this for each deformed image gives the +3D displacement field. + +In Pyvale, Stereo DIC still uses the same subset matching, shape functions, +correlation criteria, interpolation, and Levenberg-Marquardt optimization +covered in the :ref:`DIC theory overview `. The extra pieces +are the stereo camera geometry, the left-right matching strategy, and the +conversion from pixel coordinates to world coordinates. + +Stereo Processing Strategy +-------------------------- +The stereo workflow can be thought of as two linked DIC problems. The left +camera is used for the usual temporal correlation between the reference and +current deformed image. Once the left-image displacement is known, Pyvale uses +that result to find the matching subset in the right camera image. + +For a subset with reference left-image centre :math:`(x_l^0,y_l^0)`, the left +camera temporal DIC gives a deformed left-image centre + +.. math:: + + x_l^k = x_l^0 + u_l^k, \qquad + y_l^k = y_l^0 + v_l^k, + +where :math:`u_l^k` and :math:`v_l^k` are the 2D DIC displacement components for +image :math:`k`. The stereo match then searches for the corresponding point in +the right image, + +.. math:: + + x_r^k = x_l^0 + u_r^k, \qquad + y_r^k = y_l^0 + v_r^k. + +Here :math:`u_r^k` and :math:`v_r^k` are stored as the stereo pixel displacement +components. They describe where the matching right-image subset is relative to +the original left-image subset grid. + +.. image:: guide_theory_dic_stereo_light.png + :class: only-light + :alt: Diagram (light) + +.. image:: guide_theory_dic_stereo_dark.png + :class: only-dark + :alt: Diagram (dark) + +Camera Geometry +--------------- +Stereo DIC needs a calibration for both cameras. Pyvale uses each camera's +intrinsic matrix, + +.. math:: + + \mathbf{K} = + \begin{bmatrix} + f_x & f_s & c_x \\ + 0 & f_y & c_y \\ + 0 & 0 & 1 + \end{bmatrix}, + +where :math:`f_x` and :math:`f_y` are the focal lengths in pixels, +:math:`f_s` is the skew term, and :math:`(c_x,c_y)` is the principal point. The +relative camera pose is defined by a rotation matrix :math:`\mathbf{R}` and a +translation vector :math:`\mathbf{t}` from the left camera to the right camera. +In the current implementation the rotation matrix is built from the calibration +Euler angles using the order + +.. math:: + + \mathbf{R} = \mathbf{R}_z\mathbf{R}_y\mathbf{R}_x. + +From this calibration, Pyvale forms the fundamental matrix + +.. math:: + + \mathbf{F} + = \mathbf{K}_1^{-\mathsf{T}}[\mathbf{t}]_\times + \mathbf{R}\mathbf{K}_0^{-1}, + +where :math:`[\mathbf{t}]_\times` is the skew-symmetric matrix for the stereo +translation. The fundamental matrix defines the epipolar constraint between the +left and right images. + +Epipolar Constraint +------------------- +For a point in the left image written in homogeneous coordinates as +:math:`\mathbf{x}_l = [x_l, y_l, 1]^\mathsf{T}`, the corresponding point in the +right image must lie on the epipolar line + +.. math:: + + \mathbf{l}_r = \mathbf{F}\mathbf{x}_l. + +If :math:`\mathbf{l}_r = [a,b,c]^\mathsf{T}`, then any matching right-image point +:math:`(x_r,y_r)` satisfies + +.. math:: + + a x_r + b y_r + c = 0. + +This is useful because it reduces the initial stereo search from a 2D image +search to a search along a line. Pyvale computes the closest point on the +epipolar line to the left-image subset centre, + +.. math:: + + \mathbf{P} + = + \begin{bmatrix} x_l \\ y_l \end{bmatrix} + - + \frac{a x_l + b y_l + c}{a^2+b^2} + \begin{bmatrix} a \\ b \end{bmatrix}, + +and the unit direction of the epipolar line, + +.. math:: + + \mathbf{d} + = + \frac{1}{\sqrt{a^2+b^2}} + \begin{bmatrix} -b \\ a \end{bmatrix}. + +This line direction is also used to create a local rectified search coordinate +system. The perpendicular direction is + +.. math:: + + \mathbf{d}_{\perp} = + \begin{bmatrix} d_y \\ -d_x \end{bmatrix}. + +Initial Guess From Rectified FFT +-------------------------------- +Just as 2D DIC benefits from a good initial guess, stereo matching also needs a +starting point that is close to the correct right-image subset. Pyvale does this +by building a small rectified search window around the epipolar line. + +The left subset is placed in the centre of an FFT correlation window. The right +image is then sampled in a coordinate system aligned with the epipolar line, + +.. math:: + + \mathbf{s}(i,j) + = \mathbf{P} + i\mathbf{d} - j\mathbf{d}_{\perp}, + +where :math:`i` moves along the epipolar line and :math:`j` moves perpendicular +to it. This gives an unrectified patch from the right image, sampled as if the +local search region had been rectified. + +FFT cross-correlation is then used to estimate the translation between the left +subset and this rectified right-image window. If the correlation peak is +:math:`(\Delta i, \Delta j)`, the estimated right-image point is + +.. math:: + + \mathbf{x}_r + = \mathbf{P} + \Delta i\mathbf{d} - \Delta j\mathbf{d}_{\perp}. + +This gives the initial rigid translation + +.. math:: + + p_0 = x_r - x_l, \qquad p_1 = y_r - y_l. + +For affine or quadratic shape functions, the initial parameters also include the +local coordinate transformation implied by the epipolar-line basis, + +.. math:: + + \begin{bmatrix} + 1+p_2 & p_3 \\ + p_4 & 1+p_5 + \end{bmatrix} + = + \begin{bmatrix} + d_x & -d_{\perp,x} \\ + d_y & -d_{\perp,y} + \end{bmatrix}. + +This is only an initial estimate. The final match is still obtained using the +same nonlinear subset optimization described in the 2D DIC theory guide. + +Reliability-Guided Stereo Matching +---------------------------------- +Pyvale uses a reliability-guided strategy for stereo matching in the same spirit +as RG-DIC. Seed subsets are matched first, then the solution expands through the +subset grid using previously matched neighbours as initial guesses. + +The algorithm proceeds as follows: + +#. The left-image temporal DIC result is used to locate the current subset centre + in the deformed left image. +#. The current left subset is built using the temporal left-camera shape-function + parameters. +#. For each seed point, an initial stereo guess is estimated from rectified FFT + correlation along the epipolar line. +#. The optimizer refines the left-to-right subset match in the right image. +#. Neighbouring subsets are added to a queue ordered by their correlation cost. +#. When a subset is processed, Pyvale uses a successful neighbouring stereo + result as the initial guess where possible. If the neighbour did not pass the + threshold, the epipolar FFT estimate is used again. +#. The process expands through the active subset grid until all reachable subsets + have been attempted. + +For deformed images after the first one, the stereo shape-function parameters are +composed with the left-camera temporal parameters. This means the stored stereo +parameters describe the mapping from the original left reference subset through +to the current right-image subset, rather than only the incremental left-to-right +match for that frame. + +Triangulation +------------- +Once a left-image point and right-image point have been matched, the 3D position +can be calculated. Pyvale first undistorts both image points using the calibrated +radial and tangential distortion parameters. The distorted pixel point is +converted to normalized camera coordinates, and then an iterative update is used +to remove distortion. + +The normalized left and right image points are written as + +.. math:: + + \mathbf{x}_l = [x_l, y_l, 1]^\mathsf{T}, \qquad + \mathbf{x}_r = [x_r, y_r, 1]^\mathsf{T}. + +Pyvale then uses linear triangulation with projection matrices + +.. math:: + + \mathbf{P}_0 = [\mathbf{I}\mid\mathbf{0}], \qquad + \mathbf{P}_1 = [\mathbf{R}\mid\mathbf{t}]. + +The world point :math:`\mathbf{X} = [X,Y,Z,1]^\mathsf{T}` is found by solving the +homogeneous DLT system + +.. math:: + + \mathbf{A}\mathbf{X} = \mathbf{0}, + +where + +.. math:: + + \mathbf{A} = + \begin{bmatrix} + x_l\mathbf{P}_{0,3} - \mathbf{P}_{0,1} \\ + y_l\mathbf{P}_{0,3} - \mathbf{P}_{0,2} \\ + x_r\mathbf{P}_{1,3} - \mathbf{P}_{1,1} \\ + y_r\mathbf{P}_{1,3} - \mathbf{P}_{1,2} + \end{bmatrix}. + +Here :math:`\mathbf{P}_{m,n}` denotes row :math:`n` of projection matrix +:math:`\mathbf{P}_m`. The solution is the right singular vector corresponding to +the smallest singular value of :math:`\mathbf{A}`. After converting from +homogeneous coordinates, Pyvale stores the 3D coordinates +:math:`(X,Y,Z)`. If the stereo translation is supplied in millimetres, these +coordinates are also in millimetres. + +3D Displacement +--------------- +For the first image pair, the triangulated coordinates are stored as the stereo +reference coordinates and the world displacement is set to zero. For later image +pairs, the 3D displacement is calculated by subtracting the reference stereo +coordinates from the current triangulated coordinates, + +.. math:: + + u_X^k = X^k - X^0, \qquad + u_Y^k = Y^k - Y^0, \qquad + u_Z^k = Z^k - Z^0. + +These are the displacement components written by Pyvale as the stereo +millimetre displacement fields. Subsets that do not pass the temporal or stereo +correlation threshold are not used for the world-coordinate calculation. + +Practical Notes +--------------- +Stereo DIC is more sensitive to calibration quality than 2D DIC. A poor +intrinsic calibration, stereo rotation, or translation vector will give poor +epipolar lines and therefore poor initial guesses. It will also directly affect +the triangulated world coordinates. Spend time ensuring your calibration is of +good quality - a lot of stereo DIC issues stem from a poor camera calibration. + +Good speckle texture remains important. The left and right cameras see the same +surface from different viewpoints, so subsets should have enough unique texture +to match reliably even when perspective and lighting change slightly. Avoid +regions with repetitive patterns that may cause ambiguous left-to-right matches. + +The epipolar FFT estimate is used to get close to the correct match, but the +final result still depends on the nonlinear subset optimizer. As with standard +DIC, the threshold, subset size, subset step, shape function, and interpolation +routine all influence the final result. Parameters may need to be adjusted +differently for stereo DIC compared to 2D DIC due to the additional complexity +of the stereo matching process. + +When using ``calculate_3d()``, ensure that the :code:`epi_distance` parameter is +set appropriately for your stereo baseline and resolution. This parameter controls +the search distance along epipolar lines and affects both computation time and +matching robustness. The default value of 300 pixels is suitable for typical stereo +setups, but may need adjustment for very wide or very narrow baselines. + +For incremental stereo DIC, Pyvale carries forward the previous stereo match as +the updated reference for 3D reconstruction. This helps maintain accuracy across +large deformation sequences while ensuring displacements remain relative to the +original reference position. diff --git a/docs/source/guide_user/guide_dic.rst b/docs/source/guide_user/guide_dic.rst index a29904fb9..573b0f21f 100644 --- a/docs/source/guide_user/guide_dic.rst +++ b/docs/source/guide_user/guide_dic.rst @@ -219,9 +219,9 @@ Understanding Output files The next step is to understand the output. By default the results will be saved in the users current working directory in human readable .CSV format with a filename prefix of -``dic_results_`` followed by the name of the deformed image. +``dic_results_`` followed by the name of the deformed image. -**The output will have the following columns:** +For 2D DIC, text output files contain the following columns: - ``subset_x``: X-coordinate of the center of the subset (or window) used in displacement tracking or correlation analysis. @@ -229,22 +229,22 @@ in the users current working directory in human readable .CSV format with a file - ``subset_y``: Y-coordinate of the center of the subset used in displacement tracking or correlation analysis. -- ``displacement_u``: +- ``disp_u``: Displacement in the X-direction (horizontal) calculated for the subset. -- ``displacement_v``: +- ``disp_v``: Displacement in the Y-direction (vertical) calculated for the subset. -- ``displacement_mag``: +- ``disp_mag``: Magnitude of the displacement vector. - ``converged``: Boolean flag indicating whether the displacement calculation algorithm converged for this subset. -- ``cost``: +- ``cost_zncc``: The final value of the cost function used during the displacement calculation. The reported value is always given as the ZNCC value no matter if the SSD, NSSD or ZNSSD has been chosen as the correlation function. - The ZNCC is calculated with the final parameter values from the last optimizer iteration. + The ZNCC is calculated with the final parameter values from the last optimizer iteration. - ``ftol``: The final value of the function tolerance, a measure of how much the cost function changed between iterations at convergence. @@ -252,13 +252,73 @@ in the users current working directory in human readable .CSV format with a file - ``xtol``: The final value of the solution tolerance, a measure of how much the solution (displacement) changed between iterations at convergence. -- ``num_iterations``: +- ``num_iter``: The number of iterations the algorithm took to converge for this subset. +When a calibration is provided, additional physical-unit columns may be included: + +- ``disp_u_mm``: + Displacement in the X-direction in millimetres in the camera 0 world coordinate system. + +- ``disp_v_mm``: + Displacement in the Y-direction in millimetres in the camera 0 world coordinate system. + +- ``coord_x_mm``: + X-coordinate in millimetres in the camera 0 world coordinate system. + +- ``coord_y_mm``: + Y-coordinate in millimetres in the camera 0 world coordinate system. + +For stereo DIC, output files contain the same 2D temporal DIC columns followed +by stereo matching and 3D reconstruction columns. The additional stereo columns are: + +- ``stereo_disp_u_px``: + Horizontal pixel displacement from the left image subset to the corresponding right image subset. + +- ``stereo_disp_v_px``: + Vertical pixel displacement from the left image subset to the corresponding right image subset. + +- ``stereo_disp_mag_px``: + Magnitude of the left-to-right stereo pixel displacement. + +- ``stereo_disp_u_mm``: + Reconstructed X-direction displacement in millimetres in the camera 0 world coordinate system. + +- ``stereo_disp_v_mm``: + Reconstructed Y-direction displacement in millimetres in the camera 0 world coordinate system. + +- ``stereo_disp_w_mm``: + Reconstructed Z-direction displacement in millimetres in the camera 0 world coordinate system. + +- ``stereo_x_mm``: + Reconstructed X-coordinate in millimetres in the camera 0 world coordinate system. + +- ``stereo_y_mm``: + Reconstructed Y-coordinate in millimetres in the camera 0 world coordinate system. + +- ``stereo_z_mm``: + Reconstructed Z-coordinate in millimetres in the camera 0 world coordinate system. + +- ``stereo_converged``: + Boolean flag indicating whether the stereo left-to-right subset match converged. + +- ``stereo_cost_zncc``: + Final ZNCC value for the stereo left-to-right subset match. + +- ``stereo_ftol``: + Final function tolerance value for the stereo optimizer. + +- ``stereo_xtol``: + Final solution tolerance value for the stereo optimizer. + +- ``stereo_num_iter``: + Number of optimizer iterations used by the stereo subset match. + You can alter the path, delimiter and filename prefix of the output file using the arguments :code:`output_basepath`, :code:`output_delimiter` and :code:`output_prefix`. You can also opt to save results in binary format. This can be done by setting -:code:`output_binary=True`. +:code:`output_binary=True`. Binary 2D DIC files use the ``.dic2d`` extension and +binary stereo DIC files use the ``.dic3d`` extension. Importing DIC Results ^^^^^^^^^^^^^^^^^^^^^ @@ -274,16 +334,18 @@ create a simple plot of the displacement: import matplotlib.pyplot as plt - dic_data = dic.import_2d(data="./dic_results_*") + dic_data = dic.import_2d(data="./dic_results_*", delimiter=",") # plot of vertical displacement for first deformation image. plt.pcolor(dic_data.ss_x, dic_data.ss_y, - dic_data.u_y[0]) # [image, y, x] + dic_data.v_px[0]) # [image, y, x] The import will find all files in the current working directory with that filname prefix. If you have changed :code:`output_delimiter` prior to the -correlation you will also need to specify the delimiter when importing the data. +correlation you will also need to specify the same delimiter when importing the data. +To read binary files (with ``.dic2d`` or ``.dic3d`` extension), pass :code:`binary=True` +to the import function. Strain Calculation ^^^^^^^^^^^^^^^^^^^ @@ -327,16 +389,34 @@ in the users current working directory in human readable .CSV format with a file **The output will have the following columns:** -* ``window_x``: X-coordinate of the strain window center. -* ``window_y``: Y-coordinate of the strain window center. -* ``def_grad_00``: Deformation gradient component, :math:`F_{00}`. -* ``def_grad_01``: Deformation gradient component, :math:`F_{01}`. -* ``def_grad_10``: Deformation gradient component, :math:`F_{10}`. -* ``def_grad_11``: Deformation gradient component, :math:`F_{11}`. -* ``eps_00``: Strain tensor component :math:`\eps_{00} (normal strain in x-direction). -* ``eps_01``: Strain tensor component :math:`\eps_{01} (shear strain xy). -* ``eps_10``: Strain tensor component :math:`\eps_{10} (shear strain yx). -* ``eps_11``: Strain tensor component :math:`\eps_{11} (normal strain in y-direction). +* ``window_x``: X-coordinate of the strain window center (pixels). +* ``window_y``: Y-coordinate of the strain window center (pixels). + +**Deformation Gradient Components** (3×2 matrix for 2D surface strain): + +* ``def_grad_00``: Deformation gradient component, :math:`F_{00} = 1 + \partial u / \partial x`. +* ``def_grad_01``: Deformation gradient component, :math:`F_{01} = \partial u / \partial y`. +* ``def_grad_10``: Deformation gradient component, :math:`F_{10} = \partial v / \partial x`. +* ``def_grad_11``: Deformation gradient component, :math:`F_{11} = 1 + \partial v / \partial y`. +* ``def_grad_20``: Deformation gradient component, :math:`F_{20} = \partial w / \partial x` (3D-aware). +* ``def_grad_21``: Deformation gradient component, :math:`F_{21} = \partial w / \partial y` (3D-aware). + +**Strain Tensor Components** (2×2 in-plane strain): + +* ``eps_xx``: Strain tensor component, :math:`\varepsilon_{xx}` (normal strain in x-direction). +* ``eps_xy``: Strain tensor component, :math:`\varepsilon_{xy}` (shear strain). +* ``eps_yx``: Strain tensor component, :math:`\varepsilon_{yx}` (shear strain). +* ``eps_yy``: Strain tensor component, :math:`\varepsilon_{yy}` (normal strain in y-direction). + +**Optional Physical Coordinates** (when stereo DIC calibration is available): + +* ``coord_x_mm``: X-coordinate of the strain window center in millimetres in the camera 0 world coordinate system. +* ``coord_y_mm``: Y-coordinate of the strain window center in millimetres in the camera 0 world coordinate system. +* ``coord_z_mm``: Z-coordinate of the strain window center in millimetres in the camera 0 world coordinate system. + +The strain formulation (e.g., Hencky, Green-Lagrange, Almansi) can be selected using the +:code:`strain_formulation` parameter in :code:`strain.calculate_2d()`. See the theory guide +for details on available strain formulations. You can alter the path, delimiter and filename prefix of the output file using the arguments :code:`output_basepath`, :code:`output_delimiter` and :code:`output_prefix`. You @@ -346,7 +426,9 @@ can also opt to save results in binary format. This can be done by setting Importing Strain Data ^^^^^^^^^^^^^^^^^^^^^^^^ -Importing Strain data is done with the ``strain.import_2d`` command. +Importing Strain data is done with the ``strain.import_2d`` or ``strain.import_3d`` commands. +The ``import_2d`` function imports legacy strain data and can accept data with or without +3D coordinates. The ``import_3d`` function requires 3D coordinates to be present. The below highlights how to import data and create a simple plot of the normal strain in the x-direction: @@ -354,20 +436,111 @@ create a simple plot of the normal strain in the x-direction: import matplotlib.pyplot as plt - strain_data = strain.import_2d(data="./dic_results_*") + strain_data = strain.import_2d(data="./strain_*", delimiter=",") - # plot of vertical displacement for first deformation image. + # plot of normal strain in x-direction for first deformation image. plt.pcolor(strain_data.window_x, strain_data.window_y, strain_data.eps_xx[0]) # [image, y, x] The import will find all files in the current working directory with that -filname prefix. If you have changed :code:`output_delimiter` prior to the -correlation you will also need to specify the delimiter when importing the data. +filename prefix. If you have changed :code:`output_delimiter` prior to the +strain calculation you will also need to specify the same delimiter when importing the data. +To read binary files, pass :code:`binary=True` to the import function. + +Incremental DIC +---------------- + +By default Pyvale performs all temporal DIC calculations against the original +reference image. This is usually the simplest interpretation of the results, but +it can become unreliable when the deformation between the reference image and a +later image is too large for a robust subset match. + +Incremental DIC changes the reference image during a sequence: + +.. code-block:: Python + :emphasize-lines: 3-5 + + dic.calculate_2d( + ..., + incremental=True, + incremental_update_condition="IMAGE", + incremental_update_value=1, + ..., + ) + +When ``incremental=True``, Pyvale may replace the active reference image with +the previous deformed image before processing the next frame. This reduces the +image-to-image deformation that the optimizer has to solve. The displacement +values written to disk are still accumulated relative to the first reference +image, so ``disp_u`` and ``disp_v`` remain total temporal displacements rather +than only frame-to-frame increments. The correlation quality fields, such as +``cost_zncc``, ``ftol``, ``xtol`` and ``num_iter``, describe the match against +the active, possibly updated reference image. + +The update rule is controlled with ``incremental_update_condition`` and +``incremental_update_value``: + +- ``"IMAGE"``: + Update after every ``N`` deformed images, where ``N`` is + ``incremental_update_value``. For example, ``incremental_update_value=1`` + updates the reference at every available opportunity after the first deformed + image. + +- ``"ITER"``: + Update when the average ``num_iter`` value from the previous image is greater + than ``incremental_update_value``. + +- ``"COST"``: + Update when the average ``cost_zncc`` value from the previous image is greater + than ``incremental_update_value``. + +The first deformed image is always matched against the original reference image. +When the reference is updated, subsets that failed the threshold check in the +previous frame are removed from the active subset set for later frames. For +stereo DIC, the same temporal reference update is applied to the left camera +stream; Pyvale also carries the previous right-camera stereo result as the +updated stereo reference used for reconstructing 3D displacements. + +Performing Stereo DIC +---------------------- + +Stereo Digital Image Correlation extends 2D DIC by using two synchronised cameras +to triangulate 3D positions of material points. To perform stereo DIC, you will need +calibrated camera intrinsics and the relative pose (rotation and translation) between +the left and right cameras. Stereo DIC uses the ``dic.calculate_3d`` function instead +of ``dic.calculate_2d``: + +.. code-block:: Python + + dic.calculate_3d( + reference=[left_ref_image, right_ref_image], + deformed=[left_def_images, right_def_images], + roi_mask=roi_mask, + seed=seed, + calibration=calib, # Calib dataclass from pyvale.calib + subset_size=31, + subset_step=15, + ... + ) + +The key differences from 2D DIC are: + +- ``reference`` and ``deformed`` are lists of image pairs ``[left_image, right_image]``. +- A ``calibration`` parameter (of type ``Calib``) must be provided with camera intrinsics and stereo geometry. +- An additional ``epi_distance`` parameter controls the search distance along epipolar lines (default: 300 pixels). + +Stereo DIC output includes all standard 2D columns for the left camera temporal correlation, +plus stereo-specific columns for the left-to-right matching and 3D reconstruction. +See "Understanding Output files" above for a complete list of stereo output columns. + +For details on the stereo DIC theory including camera calibration, epipolar geometry, +and triangulation, see the :ref:`Stereo DIC theory guide `. DIC with Large Images/Displacements ------------------------------------ + Setting a Maximum Displacement ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -403,28 +576,30 @@ sometimes be problematic when multiple windows are involved as incorrect estimates can be propogated through the smaller windows, leading to a wildly incorrect initial rigid estimate for the displacements. -Pyvale has a Median Absolute Deviation (MAD) outlier removal -flag that, when enabled, will kill likely incorrect spikes in the rigid -estimates or each FFTCC window size. This can be enabled with the following +Pyvale has an FFT displacement outlier removal filter +that, when enabled, will kill likely incorrect spikes in the rigid +estimates for each FFTCC window size. This can be enabled with the following arguments when calling the DIC engine: .. code-block:: Python - :emphasize-lines: 3-4 + :emphasize-lines: 3-6 dic.calculate_2d( ..., - fft_mad=True, - fft_mad_scale=3.0, # <-- Default value + fft_filter=True, + fft_filter_threshold=3.0, # <-- Default value + fft_filter_radius=3, + fft_filter_corr_power=2.0, ... ) -The MAD outlier removal works in the following way: +The FFT displacement outlier removal works in the following way: #. Looks at nearby subsets in a 2D neighborhood -#. Computes the median of their shifts -#. Computes the MAD (median absolute deviation) -#. A value is replaced if the the condition :math:`| x − \mathrm{median}| > \mathrm{fft\_mad\_scale} \times \mathrm{MAD}` is met. - A larger :code:`fft_mad_scale` is therefore more *tolerant*, while a smaller value kills larger deviations. +#. Computes a correlation-weighted vector median of their shifts +#. Computes a weighted median vector residual +#. A vector is replaced if its normalized residual is larger than the adaptive threshold. + A larger :code:`fft_filter_threshold` is therefore more *tolerant*, while a smaller value kills larger deviations. Sequential Image Loading diff --git a/docs/source/install/install_mac.rst b/docs/source/install/install_mac.rst index 1e716af17..28d4a8415 100644 --- a/docs/source/install/install_mac.rst +++ b/docs/source/install/install_mac.rst @@ -109,8 +109,8 @@ used during the build process: .. code-block:: bash - export CC=/opt/homebrew/opt/gcc/bin/gcc-15 - export CXX=/opt/homebrew/opt/gcc/bin/g++-15 + export CC=$(brew --prefix gcc)/bin/gcc-15 + export CXX=$(brew --prefix gcc)/bin/g++-15 You might need to change the number at the end depending on which version of ``gcc`` you have installed. @@ -128,8 +128,8 @@ used during the build process: .. code-block:: bash - export CC=/opt/homebrew/opt/llvm/bin/clang - export CXX=/opt/homebrew/opt/llvm/bin/clang++ + export CC=$(brew --prefix llvm)/bin/clang + export CXX=$(brew --prefix llvm)/bin/clang++ Clone and Install the Github Repository ======================================= diff --git a/images/cal_target_flipped.png b/images/cal_target_flipped.png new file mode 100644 index 000000000..56fd04b9d Binary files /dev/null and b/images/cal_target_flipped.png differ diff --git a/images/dic_ex10_3d.png b/images/dic_ex10_3d.png new file mode 100644 index 000000000..22db31805 Binary files /dev/null and b/images/dic_ex10_3d.png differ diff --git a/images/dic_ex11_3d.png b/images/dic_ex11_3d.png new file mode 100644 index 000000000..fd4e1eafe Binary files /dev/null and b/images/dic_ex11_3d.png differ diff --git a/images/dic_ex11_roi.png b/images/dic_ex11_roi.png new file mode 100644 index 000000000..df5e32c6f Binary files /dev/null and b/images/dic_ex11_roi.png differ diff --git a/images/incremental_dark.png b/images/incremental_dark.png new file mode 100644 index 000000000..3b73d6f63 Binary files /dev/null and b/images/incremental_dark.png differ diff --git a/images/incremental_light.png b/images/incremental_light.png new file mode 100644 index 000000000..7b43c26a7 Binary files /dev/null and b/images/incremental_light.png differ diff --git a/images/stereo_dark.png b/images/stereo_dark.png new file mode 100644 index 000000000..9226d16e8 Binary files /dev/null and b/images/stereo_dark.png differ diff --git a/images/stereo_light.png b/images/stereo_light.png new file mode 100644 index 000000000..f07ae21bc Binary files /dev/null and b/images/stereo_light.png differ diff --git a/pyproject.toml b/pyproject.toml index 7b1d082e1..3db6d2269 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -88,7 +88,7 @@ filterwarnings = [ "Issue Tracker" = "https://github.com/Computer-Aided-Validation-Laboratory/pyvale/issues" [tool.scikit-build] -build.verbose = false +build.verbose = true logging.level = "INFO" ninja.make-fallback = false build-dir = "build" diff --git a/src/pyvale/calib/__init__.py b/src/pyvale/calib/__init__.py index 7035d9ec6..3317d68cb 100644 --- a/src/pyvale/calib/__init__.py +++ b/src/pyvale/calib/__init__.py @@ -4,8 +4,21 @@ # Copyright (C) 2025 The Computer Aided Validation Team #=============================================================================== -from .calibdotdetect import dot_detection -from .calibstereo import stereo_calibration +"""Calibration tools for stereo DIC workflows. -__all__ = ["dot_detection", - "stereo_calibration"] +This package exposes the high-level calibration utilities used to detect dot +calibration targets and estimate stereo camera parameters. The main public +entry points are :func:`detect_dots`, :func:`calibrate_stereo`, and the +:class:`Calib` and :class:`CamIntrinsics` result containers. +""" + +from .calibdotdetect import detect_dots +from .calibstereo import calibrate_stereo +from .calibdataclass import Calib, CamIntrinsics, savetxt, loadtxt + +__all__ = ["detect_dots", + "Calib", + "CamIntrinsics", + "calibrate_stereo", + "savetxt", + "loadtxt"] diff --git a/src/pyvale/calib/calibdataclass.py b/src/pyvale/calib/calibdataclass.py new file mode 100644 index 000000000..331411498 --- /dev/null +++ b/src/pyvale/calib/calibdataclass.py @@ -0,0 +1,181 @@ +# ================================================================================ +# pyvale: the python validation engine +# License: MIT +# Copyright (C) 2025 The Computer Aided Validation Team +# ================================================================================ + +"""Dataclasses used to pass stereo calibration parameters through Pyvale.""" + +import csv +from dataclasses import dataclass +from pathlib import Path + +import numpy as np + + +@dataclass(slots=True) +class CamIntrinsics: + """Intrinsic camera parameters and distortion coefficients. + + The parameters follow the usual pinhole camera model with optional skew and + five OpenCV-style distortion coefficients. Focal lengths and the principal + point are expressed in pixels. + """ + + fx: float + """Focal length in x direction [pixels]""" + + fy: float + """Focal length in y direction [pixels]""" + + fs: float + """Skew coefficient [pixels]""" + + cx: float + """Principal point x-coordinate [pixels]""" + + cy: float + """Principal point y-coordinate [pixels]""" + + distortion: np.ndarray + """Distortion coefficients [kappa1, kappa2, p1, p2, kappa3]""" + + @property + def camera_matrix(self) -> np.ndarray: + """Return the 3x3 intrinsic camera matrix. + + Returns + ------- + np.ndarray + Matrix ``K`` with focal lengths, skew, and principal point arranged + as ``[[fx, fs, cx], [0, fy, cy], [0, 0, 1]]``. + """ + return np.array([ + [self.fx, self.fs, self.cx], + [0, self.fy, self.cy], + [0, 0, 1] + ], dtype=np.float64) + + +@dataclass(slots=True) +class Calib: + """Stereo camera calibration parameters. + + Attributes + ---------- + cam0, cam1 : CamIntrinsics + Intrinsic calibration for camera 0 and camera 1. + translation : np.ndarray + Translation vector from camera 0 to camera 1 in millimetres. + rotation : np.ndarray + Euler rotation angles from camera 0 to camera 1 in degrees, ordered as + ``[theta, phi, psi]``. + """ + + cam0: CamIntrinsics + """ Camera 0 intrinsic parameters""" + + cam1: CamIntrinsics + """ Camera 1 intrinsic parameters""" + + translation: np.ndarray + """Translation vector [x, y, z] in mm""" + + rotation: np.ndarray + """Euler angles [theta, phi, psi] in degrees""" + + +def savetxt(calib: Calib, path: str | Path, delimiter: str = ",") -> None: + """Save stereo calibration parameters to a two-column CSV file. + + Parameters + ---------- + calib : Calib + Stereo calibration parameters to save. + path : str or pathlib.Path + Output CSV file path. + delimiter : str, optional + Delimiter to use between CSV fields. Defaults to a comma. + """ + + path = Path(path) + distortion_names = ("k1", "k2", "p1", "p2", "k3") + + rows: list[tuple[str, float]] = [] + for cam_name, cam in (("cam0", calib.cam0), ("cam1", calib.cam1)): + rows.extend([ + (f"{cam_name}_fx_px", float(cam.fx)), + (f"{cam_name}_fy_px", float(cam.fy)), + (f"{cam_name}_fs_px", float(cam.fs)), + (f"{cam_name}_cx_px", float(cam.cx)), + (f"{cam_name}_cy_px", float(cam.cy)), + ]) + rows.extend( + (f"{cam_name}_{name}", float(value)) + for name, value in zip(distortion_names, cam.distortion) + ) + + rows.extend([ + ("tx_mm", float(calib.translation[0])), + ("ty_mm", float(calib.translation[1])), + ("tz_mm", float(calib.translation[2])), + ("theta_deg", float(calib.rotation[0])), + ("phi_deg", float(calib.rotation[1])), + ("psi_deg", float(calib.rotation[2])), + ]) + + with path.open("w", newline="") as csv_file: + writer = csv.writer(csv_file, delimiter=delimiter) + writer.writerows(rows) + +def loadtxt(path: str | Path, delimiter: str = ",") -> Calib: + """Load stereo calibration parameters from a two-column CSV file. + + Parameters + ---------- + path : str or pathlib.Path + Input CSV file path. + delimiter : str, optional + Delimiter used between CSV fields. Defaults to a comma. + + Returns + ------- + Calib + Stereo calibration parameters read from disk. + """ + path = Path(path) + with path.open("r", newline="") as csv_file: + reader = csv.reader(csv_file, delimiter=delimiter) + rows = [row for row in reader if row] + + values = {name: float(value) for name, value, *extra in rows} + + def get(name: str) -> float: + try: + return values[name] + except KeyError as exc: + raise ValueError(f"Missing calibration parameter: {name}") from exc + + def cam_from_values(cam_name: str) -> CamIntrinsics: + return CamIntrinsics( + fx=get(f"{cam_name}_fx_px"), + fy=get(f"{cam_name}_fy_px"), + fs=get(f"{cam_name}_fs_px"), + cx=get(f"{cam_name}_cx_px"), + cy=get(f"{cam_name}_cy_px"), + distortion=np.array([ + get(f"{cam_name}_k1"), + get(f"{cam_name}_k2"), + get(f"{cam_name}_p1"), + get(f"{cam_name}_p2"), + get(f"{cam_name}_k3"), + ], dtype=np.float64), + ) + + return Calib( + cam0=cam_from_values("cam0"), + cam1=cam_from_values("cam1"), + translation=np.array([get("tx_mm"), get("ty_mm"), get("tz_mm")], dtype=np.float64), + rotation=np.array([get("theta_deg"), get("phi_deg"), get("psi_deg")], dtype=np.float64), + ) + diff --git a/src/pyvale/calib/calibdotdetect.py b/src/pyvale/calib/calibdotdetect.py index 885bd3141..2bdb66d7c 100644 --- a/src/pyvale/calib/calibdotdetect.py +++ b/src/pyvale/calib/calibdotdetect.py @@ -4,6 +4,8 @@ # Copyright (C) 2025 The Computer Aided Validation Team # ================================================================================ +"""Dot target detection utilities for stereo camera calibration.""" + import cv2 import numpy as np import re @@ -12,13 +14,67 @@ from pathlib import Path import glob from scipy.optimize import least_squares - -def dot_detection(cam0: Path | list[Path] | np.ndarray | str, - cam1: Path | list[Path] | np.ndarray | str, - grid_height: int, grid_width: int, - grid_spacing: float, - visualisation: bool=False) -> tuple[list, list, list, np.ndarray]: - +import matplotlib.pyplot as plt + +import pyvale.common_py.util as common_py_util + +def detect_dots(cam0: Path | list[Path] | np.ndarray | str, + cam1: Path | list[Path] | np.ndarray | str, + grid_height: int, grid_width: int, + grid_spacing: float, + missing_dots: list[tuple[int, int]], + min_dot_fraction: float=0.5, + visualisationCV2: bool=False, + visualisationPLT: bool=False) -> tuple[list, list, list]: + """Detect and match calibration dots in synchronized stereo image pairs. + + The calibration target is assumed to contain a regular dark-dot grid with + three missing locations marked by light dots. The light-dot triangle is used + to orient each image relative to the known grid, then dark blobs are matched + to the nearest expected grid locations. Only points detected consistently in + both cameras are retained. + + Parameters + ---------- + cam0, cam1 : pathlib.Path, list[pathlib.Path], np.ndarray, or str + Camera 0 and camera 1 inputs. Paths and strings may include glob + patterns. Lists must contain corresponding image paths for each camera. + Array inputs are currently only shape-checked. + grid_height, grid_width : int + Number of dot rows and columns in the full calibration target. + grid_spacing : float + Physical spacing between neighbouring dots in the target coordinate + system. + missing_dots : list[tuple[int, int]] + The three missing-dot locations, expressed as ``(x, y)`` grid indices. + These define the orientation marker used for matching. + min_dot_fraction : float, optional + Minimum fraction of grid points that must be matched for an image pair + to be accepted. + visualisationCV2 : bool, optional + If ``True``, show an OpenCV overlay of detected and matched points for + each accepted pair. + visualisationPLT : bool, optional + If ``True``, show Matplotlib diagnostic plots of the detected points and + grid mapping. + + Returns + ------- + tuple[list, list, list, list, list] + Matched 2D points for camera 0, matched 2D points for camera 1, matched + 3D grid points, accepted camera 0 filenames, and accepted camera 1 + filenames. + + Raises + ------ + TypeError + If the input type is unsupported. + ValueError + If camera inputs are incompatible, image lists have different lengths, + or ``missing_dots`` is not three non-negative ``(x, y)`` tuples. + FileNotFoundError + If a path or glob pattern does not resolve to any images. + """ files_cam0 = [] files_cam1 = [] @@ -36,6 +92,16 @@ def dot_detection(cam0: Path | list[Path] | np.ndarray | str, f"cam0 and cam1 are different numpy shapes: cam0.shape={cam0.shape}, cam1.shape={cam1.shape}" ) + + if (len(missing_dots) != 3 + or not all(isinstance(dot, tuple) + and len(dot) == 2 + and all(isinstance(v, int) and v >= 0 for v in dot) + for dot in missing_dots)): + raise ValueError( + "missing_dots must contain exactly three (x, y) tuples of non-negative integers." + ) + # handle strings. convert to path for import elif isinstance(cam0, (str, Path)) and isinstance(cam1, (str, Path)): cam0 = Path(cam0) @@ -65,11 +131,9 @@ def dot_detection(cam0: Path | list[Path] | np.ndarray | str, fullgrid_3d[:, :2] *= grid_spacing - missing_idx = np.array([ - [2, grid_height-2-1], - [2, grid_height-7], - [9, grid_height-2-1], - ]) + # order by their internal triangle angles + missing_idx = order_triangle_points_by_angle(np.asarray(missing_dots)) + missing_idx = missing_idx.astype(np.intp) missing_grid = (missing_idx * grid_spacing - 2*grid_spacing) missing_grid = missing_grid.astype(np.float32) @@ -89,15 +153,20 @@ def dot_detection(cam0: Path | list[Path] | np.ndarray | str, gridpoints = [] dots_cam0 = [] dots_cam1 = [] + filenames_cam0 = [] + filenames_cam1 = [] num_file_pairs = len(files_cam1) img_dims = np.zeros((2)) for i in range(0, num_file_pairs): - print("Running Dot detection on image pair: " - f"{os.path.basename(files_cam0[i])}, " - f"{os.path.basename(files_cam1[i])}") + + + common_py_util.info(f"Dot detection: " + f"\033[1;4m{os.path.basename(files_cam0[i])}\033[0m + " + f"\033[1;4m{os.path.basename(files_cam1[i])}\033[0m") + # read images img0 = cv2.imread(str(files_cam0[i]), cv2.IMREAD_GRAYSCALE) @@ -105,7 +174,7 @@ def dot_detection(cam0: Path | list[Path] | np.ndarray | str, if img0 is None or img1 is None: - print(f"Skipping missing pair: {files_cam0[i]} {files_cam1[i]}") + common_py_util.info(f"Skipping missing pair: {files_cam0[i]} {files_cam1[i]}") continue img_dims0 = (img0.shape[1], img0.shape[0]) @@ -113,7 +182,7 @@ def dot_detection(cam0: Path | list[Path] | np.ndarray | str, # check image dimensions agree if (img_dims0[0] != img_dims1[0]) or (img_dims0[1] != img_dims1[1]): - print("image dimensions don't agree: " + common_py_util.info("image dimensions don't agree: " f" - dimensions of {files_cam0}: {img_dims0}" f" - dimensions of {files_cam1}: {img_dims1}" "Skipping image pair") @@ -127,9 +196,9 @@ def dot_detection(cam0: Path | list[Path] | np.ndarray | str, # there should always be 3 points in keypoints_lght_cam0 and keypoints_lght_cam1 if len(keypoints_lght_cam0) != 3 or len(keypoints_lght_cam1) != 3: - print(f"Skipping pair due to insufficient light blobs.") - print("left:", len(keypoints_lght_cam0)) - print("right:", len(keypoints_lght_cam1)) + common_py_util.info(f"Skipping image pair. Insufficient num of axis markers.") + common_py_util.info(f"left: {len(keypoints_lght_cam0)}") + common_py_util.info(f"right: {len(keypoints_lght_cam1)}") num_file_pairs = num_file_pairs-1 continue @@ -137,21 +206,10 @@ def dot_detection(cam0: Path | list[Path] | np.ndarray | str, keypoints_dark_cam0 = detector_dark.detect(img0) keypoints_dark_cam1 = detector_dark.detect(img1) - # there should always be 3 points in keypoints_lght_cam0 and keypoints_lght_cam1 - if len(keypoints_lght_cam0) != 3 or len(keypoints_lght_cam1) != 3: - print(f"WARNING: Skipping pair due to insufficient light blobs." - f"left: {len(keypoints_lght_cam0)}" - f"right: {len(keypoints_lght_cam1)}") - continue - # Convert KeyPoints to NumPy arrays light_pts_cam0 = np.array([kp.pt for kp in keypoints_lght_cam0], dtype=np.float32) light_pts_cam1 = np.array([kp.pt for kp in keypoints_lght_cam1], dtype=np.float32) - - # print(light_pts_cam0) - # print(light_pts_cam1) - # Order points consistently based on right angle light_pts_cam0_ordered = order_triangle_points_by_angle(light_pts_cam0) light_pts_cam1_ordered = order_triangle_points_by_angle(light_pts_cam1) @@ -159,7 +217,7 @@ def dot_detection(cam0: Path | list[Path] | np.ndarray | str, # get the translation matrix between the triangle that forms from the light blobs between the left and right images cam0togrid = cv2.getAffineTransform(light_pts_cam0_ordered, missing_grid[:,:]) cam1togrid = cv2.getAffineTransform(light_pts_cam1_ordered, missing_grid[:,:]) - + pts_cam0_raw = np.array([kp.pt for kp in keypoints_dark_cam0], dtype=np.float32) pts_cam1_raw = np.array([kp.pt for kp in keypoints_dark_cam1], dtype=np.float32) @@ -170,7 +228,7 @@ def dot_detection(cam0: Path | list[Path] | np.ndarray | str, # get the nearest neighbours tree = cKDTree(finalgrid_2d) - dist, indices = tree.query(transformed_cam0, distance_upper_bound=1.5) + dist, indices = tree.query(transformed_cam0,distance_upper_bound=0.33*grid_spacing) valid_mask = dist != np.inf valid_indices = indices[valid_mask] @@ -195,7 +253,7 @@ def dot_detection(cam0: Path | list[Path] | np.ndarray | str, ######################################################### transformed_cam1 = (pts_cam1_raw @ cam1togrid[:, :2].T) + cam1togrid[:, 2] tree = cKDTree(matched_grid) - dist, indices = tree.query(transformed_cam1, distance_upper_bound=1.5) + dist, indices = tree.query(transformed_cam1,distance_upper_bound=0.33*grid_spacing) valid_mask = dist != np.inf valid_indices = indices[valid_mask] @@ -222,23 +280,228 @@ def dot_detection(cam0: Path | list[Path] | np.ndarray | str, matched_cam0 = np.append(matched_cam0, light_pts_cam0_ordered, axis=0) matched_cam1 = np.append(matched_cam1, light_pts_cam1_ordered, axis=0) + + ############ TESTING USING HOMOGRAPHY TO INCREASE MATCHES + H0, mask0 = cv2.findHomography(matched_cam0, matched_grid,method=cv2.RANSAC) + H1, mask1 = cv2.findHomography(matched_cam1, matched_grid,method=cv2.RANSAC) + + def warp_with_H(pts, H): + pts_h = cv2.convertPointsToHomogeneous(pts)[:, 0, :] # Nx3 + warped = (H @ pts_h.T).T + return warped[:, :2] / warped[:, [2]] + + test0 = warp_with_H(pts_cam0_raw, H0) # Nx2 + test1 = warp_with_H(pts_cam1_raw, H1) # Nx2 + + + + ###################################### + # map cam0 to grid. keep mutual points + ###################################### + + # get the nearest neighbours + tree = cKDTree(finalgrid_2d) + dist, indices = tree.query(test0,distance_upper_bound=0.1*grid_spacing) + + valid_mask = dist != np.inf + valid_indices = indices[valid_mask] + valid_dist = dist[valid_mask] + valid_pts = pts_cam0_raw[valid_mask] + valid_kps = np.array(keypoints_dark_cam0)[valid_mask] + + best_for_grid = {} + best_kps = {} + for pt, idx, d, kp in zip(valid_pts, valid_indices, valid_dist, valid_kps): + if idx not in best_for_grid or d < best_for_grid[idx][1]: + best_for_grid[idx] = (pt, d) + best_kps[idx] = kp + + + matched_cam0 = np.array([v[0] for v in best_for_grid.values()]) + matched_kps0 = [best_kps[i] for i in best_for_grid.keys()] + matched_grid = finalgrid_2d[list(best_for_grid.keys())] + + ######################################################### + # map cam1 to grid. keep mutual points from prev matching + ######################################################### + tree = cKDTree(matched_grid) + dist, indices = tree.query(test1,distance_upper_bound=0.1*grid_spacing) + + valid_mask = dist != np.inf + valid_indices = indices[valid_mask] + valid_dist = dist[valid_mask] + valid_pts = pts_cam1_raw[valid_mask] + valid_kps = np.array(keypoints_dark_cam1)[valid_mask] + + best_for_grid = {} + best_kps = {} + for pt, idx, d, kp in zip(valid_pts, valid_indices, valid_dist, valid_kps): + if idx not in best_for_grid or d < best_for_grid[idx][1]: + best_for_grid[idx] = (pt, d) + best_kps[idx] = kp + + unique_indices = sorted(best_for_grid.keys()) + matched_cam1 = np.array([best_for_grid[i][0] for i in unique_indices]) + matched_kps1 = np.array([best_kps[i] for i in unique_indices]) + matched_cam0 = matched_cam0[unique_indices] + matched_grid = matched_grid[unique_indices] + matched_kps0 = [matched_kps0[i] for i in unique_indices] + + # Add back in the light points + matched_grid = np.append(matched_grid, missing_grid, axis=0) + matched_cam0 = np.append(matched_cam0, light_pts_cam0_ordered, axis=0) + matched_cam1 = np.append(matched_cam1, light_pts_cam1_ordered, axis=0) + + + + # if the number of matches is less than minimum requirement then skip + if (matched_grid.shape[0] < min_dot_fraction*grid_height*grid_width): + print(f"WARNING: Skipping pair due to insufficient number of matches." + f"Number of mutual points: {matched_grid.shape[0]}. Minimum fraction is {min_dot_fraction}") + continue + # Append for calibration matched_grid = np.hstack((matched_grid, np.zeros((matched_grid.shape[0], 1), dtype=matched_grid.dtype))) gridpoints.append(matched_grid) dots_cam0.append(matched_cam0) dots_cam1.append(matched_cam1) + filenames_cam0.append(Path(files_cam0[i]).name) + filenames_cam1.append(Path(files_cam1[i]).name) - print(f"Points found in cam0: {len(pts_cam0_raw)+len(light_pts_cam0)}, " - f"cam1: {len(pts_cam1_raw)+len(light_pts_cam1)}, " - f"mutual: {matched_grid.shape[0]}") - print() + common_py_util.info("Num dots: " + f"\033[1;4mcam0\033[0m={len(pts_cam0_raw)+len(light_pts_cam0)}, " + f"\033[1;4mcam1\033[0m={len(pts_cam1_raw)+len(light_pts_cam1)}, " + f"\033[1;4mmutual\033[0m={matched_grid.shape[0]}" + ) - return dots_cam0, dots_cam1, gridpoints, img_dims + if visualisationCV2: + + img0_color = cv2.cvtColor(img0, cv2.COLOR_GRAY2BGR) + img1_color = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR) + overlay_l = img0_color.copy() + overlay_r = img1_color.copy() + + alpha = 0.5 + r = 20 + + # Draw dark keypoints in red + for kp in keypoints_dark_cam0: + x, y = map(int, kp.pt) + cv2.circle(overlay_l, (x, y), r, (0, 0, 255), thickness=-1) + + # Draw dark cam1 keypoints in red + for kp in keypoints_dark_cam1: + x, y = map(int, kp.pt) + cv2.circle(overlay_r, (x, y), r, (0, 0, 255), thickness=-1) + + # Draw matched_cam0 keypoints in blue + for x, y in matched_cam0: + cv2.circle(overlay_l, (int(x), int(y)), r, (255, 0, 0), thickness=-1) + + # Draw matched_cam1 keypoints in blue + for x, y in matched_cam1: + cv2.circle(overlay_r, (int(x), int(y)), r, (255, 0, 0), thickness=-1) + + # Draw light cam0 keypoints in green + for kp in keypoints_lght_cam0: + x, y = map(int, kp.pt) + cv2.circle(overlay_l, (x, y), r, (0, 255, 0), thickness=-1) + + # Draw light cam1 keypoints in green + for kp in keypoints_lght_cam1: + x, y = map(int, kp.pt) + cv2.circle(overlay_r, (x, y), r, (0, 255, 0), thickness=-1) + + # Blend overlays with original color images + im_with_keypoints_l = cv2.addWeighted(overlay_l, alpha, img0_color, 1 - alpha, 0) + im_with_keypoints_r = cv2.addWeighted(overlay_r, alpha, img1_color, 1 - alpha, 0) + + # Combine side-by-side + side_by_side = np.hstack((im_with_keypoints_l, im_with_keypoints_r)) + + # Show result + cv2.namedWindow("Stereo Keypoints", cv2.WINDOW_NORMAL) + cv2.imshow("Stereo Keypoints", side_by_side) + cv2.resizeWindow("Stereo Keypoints", 1200, 600) + cv2.waitKey(0) + + if visualisationPLT: + + pts_cam0 = matched_cam0.reshape(-1, 2) + pts_cam1 = matched_cam1.reshape(-1, 2) + fig, axes = plt.subplots(1, 4, figsize=(20, 6)) + + # # Left image with detected circles + axes[0].imshow(img0, cmap='gray') + axes[0].plot(light_pts_cam0_ordered[0, 0], light_pts_cam0_ordered[0, 1], 'co', markersize=5) + axes[0].plot(light_pts_cam0_ordered[1, 0], light_pts_cam0_ordered[1, 1], 'yo', markersize=5) + axes[0].plot(light_pts_cam0_ordered[2, 0], light_pts_cam0_ordered[2, 1], 'mo', markersize=5) + axes[0].plot(pts_cam0[:, 0], pts_cam0[:, 1], 'ro', markersize=5) + # corners0 = corners0.reshape(-1,2) + # axes[0].plot(corners0[:, 0], corners0[:, 1], 'bo', markersize=2) + axes[0].set_title('Left Image with \n Detected Circles') + + # Right image with detected circles + axes[1].imshow(img1, cmap='gray') + axes[1].plot(light_pts_cam1_ordered[0, 0], light_pts_cam1_ordered[0, 1], 'co', markersize=5) + axes[1].plot(light_pts_cam1_ordered[1, 0], light_pts_cam1_ordered[1, 1], 'yo', markersize=5) + axes[1].plot(light_pts_cam1_ordered[2, 0], light_pts_cam1_ordered[2, 1], 'mo', markersize=5) + axes[1].plot(pts_cam1[:, 0], pts_cam1[:, 1], 'ro', markersize=5) + # corners1 = corners1.reshape(-1,2) + # axes[1].plot(corners1[:, 0], corners1[:, 1], 'bo', markersize=2) + axes[1].set_title('Right Image with \n Detected Circles') + + axes[2].plot(transformed_cam0[:, 0], transformed_cam0[:, 1], 'go', markersize=5) + axes[2].plot(test0[:, 0], test0[:, 1], 'ro', markersize=5) + axes[2].plot(missing_grid[0, 0], missing_grid[0, 1], 'gv', markersize=20) + axes[2].plot(missing_grid[1, 0], missing_grid[1, 1], 'g^', markersize=20) + axes[2].plot(missing_grid[2, 0], missing_grid[2, 1], 'g<', markersize=20) + axes[2].plot(finalgrid_2d[:, 0], finalgrid_2d[:, 1], 'x', markersize=5) + axes[2].invert_yaxis() + axes[2].set_title('left circles mapped to \n to grid reference frame ') + + axes[3].plot(transformed_cam1[:, 0], transformed_cam1[:, 1], 'go', markersize=5) + axes[3].plot(test1[:, 0], test1[:, 1], 'ro', markersize=5) + axes[3].plot(finalgrid_2d[:, 0], finalgrid_2d[:, 1], 'x', markersize=5) + axes[3].invert_yaxis() + axes[3].set_title('right cricles mapped to \n to grid reference frame ') + + # Save the figure to a temporary PNG file + # filename = f"output/frame_{i:03d}.png" + # plt.savefig(filename) + # plt.close(fig) + plt.show() + + return dots_cam0, dots_cam1, gridpoints, filenames_cam0, filenames_cam1 def initial_reconstruction(dots_cam0, dots_cam1, grid, img_dims, num_file_pairs: int) -> tuple[dict,dict]: + """Estimate per-image camera intrinsics and poses for an initial solution. + + This helper performs independent nonlinear intrinsics fits for each camera + and image pair, then estimates the target pose with OpenCV ``solvePnP``. It + is mainly useful for diagnostics and older calibration experiments; the main + stereo calibration path uses OpenCV calibration followed by C++ refinement. + + Parameters + ---------- + dots_cam0, dots_cam1 : sequence[np.ndarray] + Detected 2D calibration dot coordinates for each camera. + grid : sequence[np.ndarray] + Matching 3D calibration target coordinates for each image pair. + img_dims : sequence[int] + Image dimensions as ``[width, height]`` in pixels. + num_file_pairs : int + Number of image pairs to process. + + Returns + ------- + tuple[list[dict], list[dict]] + Per-image intrinsic matrices, distortion vectors, poses, mean errors, + and optimizer success flags for camera 0 and camera 1. + """ print(f"Running initial reconstruction with {len(grid)} valid image pairs...") @@ -390,6 +653,24 @@ def initial_reconstruction(dots_cam0, dots_cam1, grid, def reprojection_intrinsics_error(params, gridpoints, dots): + """Return point-wise reprojection residuals for intrinsics optimization. + + Parameters + ---------- + params : array-like + Intrinsic and distortion parameters ordered as + ``[fx, fy, cx, cy, k1, k2, p1, p2, k3]``. + gridpoints : np.ndarray + 3D calibration target coordinates. + dots : np.ndarray + Observed 2D dot coordinates for one image. + + Returns + ------- + np.ndarray + Flattened ``x`` and ``y`` reprojection residuals. If pose estimation + fails, a large residual vector is returned. + """ fx, fy, cx, cy = params[0:4] k1, k2, p1, p2, k3 = params[4:9] @@ -419,6 +700,20 @@ def reprojection_intrinsics_error(params, gridpoints, dots): def create_blob_detector(light: bool): + """Create an OpenCV blob detector for light or dark calibration dots. + + Parameters + ---------- + light : bool + If ``True``, detect bright dots. If ``False``, detect dark dots. + + Returns + ------- + cv2.SimpleBlobDetector + Configured blob detector with area, circularity, colour, and inertia + filters suitable for the calibration target. + """ + params = cv2.SimpleBlobDetector_Params() params.filterByArea = True params.minArea = 500 @@ -432,6 +727,27 @@ def create_blob_detector(light: bool): return cv2.SimpleBlobDetector_create(params) def get_file_list(path0: Path, path1: Path): + """Resolve and pair camera image paths from two glob patterns. + + If the two glob patterns produce different numbers of files, paths with + unmatched base names are reported and excluded. Matching is based on + :func:`get_base_name`. + + Parameters + ---------- + path0, path1 : pathlib.Path + Glob patterns or concrete paths for camera 0 and camera 1 images. + + Returns + ------- + tuple[list[pathlib.Path], list[pathlib.Path]] + Paired camera 0 and camera 1 paths. + + Raises + ------ + FileNotFoundError + If either pattern resolves to no files. + """ sorted_cam0 = sorted(glob.glob(str(path0))) sorted_cam1 = sorted(glob.glob(str(path1))) @@ -487,10 +803,41 @@ def get_file_list(path0: Path, path1: Path): def get_base_name(filename): + """Return the shared base name used to pair stereo calibration images. + + Parameters + ---------- + filename : str or pathlib.Path + Calibration image filename expected to end with ``_.tiff``. + + Returns + ------- + str or None + Filename prefix before the camera/image suffix, or ``None`` if the + filename does not match the expected pattern. + """ + match = re.match(r"(.*)_\d\.tiff$", filename) return match.group(1) if match else None def order_triangle_points_by_angle(pts): + """Order three triangle points by decreasing internal angle. + + The missing-dot marker is identified from a triangle of light blobs. Ordering + by internal angle gives a consistent point order before estimating the + affine mapping from image coordinates to target-grid coordinates. + + Parameters + ---------- + pts : np.ndarray + Three ``(x, y)`` points. + + Returns + ------- + np.ndarray + The same three points ordered from largest to smallest internal angle. + """ + angles = [] for i in range(3): ang = angle_between(pts[(i+1)%3], pts[i], pts[(i+2)%3]) @@ -502,6 +849,19 @@ def order_triangle_points_by_angle(pts): return ordered_pts def angle_between(p1, p2, p3): + """Calculate the angle at ``p2`` formed by points ``p1``, ``p2``, and ``p3``. + + Parameters + ---------- + p1, p2, p3 : np.ndarray + Two-dimensional points. + + Returns + ------- + float + Angle in degrees. + """ + v1 = p1 - p2 v2 = p3 - p2 cos_theta = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)) diff --git a/src/pyvale/calib/calibparams.py b/src/pyvale/calib/calibparams.py deleted file mode 100644 index a3a4dd0a9..000000000 --- a/src/pyvale/calib/calibparams.py +++ /dev/null @@ -1,47 +0,0 @@ -# ================================================================================ -# pyvale: the python validation engine -# License: MIT -# Copyright (C) 2025 The Computer Aided Validation Team -# ================================================================================ - - -from dataclasses import dataclass -import numpy as np - -@dataclass(slots=True) -class Results: - """ - Data container for Digital Image Correlation (DIC) analysis results. - - This dataclass stores the displacements, convergence info, and correlation data - associated with a DIC computation. - - Attributes - ---------- - ss_x : np.ndarray - The x-coordinates of the subset centers (in pixels). - ss_y : np.ndarray - The y-coordinates of the subset centers (in pixels). - u : np.ndarray - Horizontal displacements at each subset location. - v : np.ndarray - Vertical displacements at each subset location. - mag : np.ndarray - Displacement magnitude at each subset location, typically computed as sqrt(u^2 + v^2). - converged : np.ndarray - boolean value for whether the subset has converged or not. - cost : np.ndarray - Final cost or residual value from the correlation optimization (e.g., ZNSSD). - ftol : np.ndarray - Final `ftol` value from the optimization routine, indicating function tolerance. - xtol : np.ndarray - Final `xtol` value from the optimization routine, indicating solution tolerance. - niter : np.ndarray - Number of iterations taken to converge for each subset point. - shape_params : np.ndarray | None - Optional shape parameters if output during DIC calculation (e.g., affine, rigid). - filenames : list[str] - name of DIC result files that have been found - """ - - diff --git a/src/pyvale/calib/calibstereo.py b/src/pyvale/calib/calibstereo.py index c71974c03..7eb3fb0ef 100644 --- a/src/pyvale/calib/calibstereo.py +++ b/src/pyvale/calib/calibstereo.py @@ -4,438 +4,257 @@ # Copyright (C) 2025 The Computer Aided Validation Team # ================================================================================ +"""Stereo camera calibration routines for dot-target image pairs.""" + import matplotlib.pyplot as plt import numpy as np +from scipy.optimize._lsq import common import cv2 - +import math +from pathlib import Path +from typing import Literal +from enum import Enum import pyvale.calib.calibcpp as calibcpp - - -def stereo_calibration(dots_cam0, dots_cam1, grid, img_dims, method: str="bundle_adjustment") -> None: - - # check dots are the same length +from pyvale.calib.calibdataclass import Calib, CamIntrinsics +import pyvale.common_cpp.common_cpp as common_cpp +import pyvale.common_py.util as common_py_util + +class ReprojError(str, Enum): + """Available reprojection error formulations for calibration refinement.""" + + RMSE = "RMSE" + MEAN = "MEAN" + MSE = "MSE" + +def calibrate_stereo(dots_cam0: list[np.ndarray] | np.ndarray, + dots_cam1: list[np.ndarray] | np.ndarray, + grid: list[np.ndarray] | np.ndarray, + img_dims: list[int] | np.ndarray, + filenames: list[str] | list[Path] | None = None, + optimize_distortion: bool = True, + precision: float = 0.001, + max_iter: int = 40, + num_threads: int | None = None, + error_formulation: Literal["RMSE", "MEAN", "MSE"] = "RMSE" + ) -> tuple[Calib, np.ndarray, np.ndarray]: + """Estimate stereo camera calibration parameters from matched dot targets. + + The function starts with OpenCV single-camera and stereo calibration to get + an initial estimate, then passes the flattened parameters to the C++ bundle + adjustment routine for refinement. The returned calibration is stored in + Pyvale dataclasses and uses millimetres for translation and degrees for the + stereo rotation angles. + + Parameters + ---------- + dots_cam0, dots_cam1 : list[np.ndarray] or np.ndarray + Matched 2D image coordinates for camera 0 and camera 1. Each image pair + must contain the same number of points in the same order. + grid : list[np.ndarray] or np.ndarray + Corresponding 3D calibration target coordinates for each image pair. + The first dimension must match the number of image pairs. + img_dims : list[int] or np.ndarray + Image dimensions as ``[width, height]`` in pixels. + filenames : list[str] or list[pathlib.Path] or None, optional + Optional names for the calibration images. This is currently only + checked for length consistency when provided. + optimize_distortion : bool, optional + If ``True``, refine radial and tangential distortion coefficients. If + ``False``, distortion coefficients are set to zero before refinement. + precision : float, optional + Convergence tolerance passed to the C++ optimizer. + max_iter : int, optional + Maximum number of C++ refinement iterations. + num_threads : int or None, optional + Number of OpenMP threads to use in the C++ optimizer. If ``None``, the + current runtime default is used. + error_formulation : {"RMSE", "MEAN", "MSE"}, optional + Error metric used by the C++ calibration optimizer. + + Returns + ------- + tuple[Calib, np.ndarray, np.ndarray] + The refined stereo calibration, followed by per-point reprojection + errors for camera 0 and camera 1. + + Raises + ------ + TypeError + If the camera point arrays and grid are not provided using compatible + container types, or if ``optimize_distortion`` is not boolean. + ValueError + If image-pair counts, point counts, shapes, filenames, image dimensions, + or the error formulation are invalid. + """ + + # Type checks + if type(dots_cam0) != type(dots_cam1): + raise TypeError(f"dots_cam0 and dots_cam1 must be the same type, got {type(dots_cam0)} and {type(dots_cam1)}") + if type(dots_cam0) != type(grid): + raise TypeError(f"dots and grid must be the same type, got {type(dots_cam0)} and {type(grid)}") + + # shape check if np.ndarray + if isinstance(dots_cam0, np.ndarray): + if dots_cam0.shape != dots_cam1.shape: + raise ValueError(f"dots_cam0 and dots_cam1 ndarray shapes do not match: {dots_cam0.shape} vs {dots_cam1.shape}") + if dots_cam0.shape[0] != grid.shape[0]: + raise ValueError(f"dots and grid have mismatched first dimension: {dots_cam0.shape[0]} vs {grid.shape[0]}") + + # length check if list if len(dots_cam0) != len(dots_cam1): - ValueError(f"ERROR: dots_cam0 and dots_cam1 are different lengths:" - f" - length of dot_cam0: {len(dots_cam0)}" - f" - length of dot_cam1: {len(dots_cam1)}") - - # check dots and grid are the same length + raise ValueError(f"dots_cam0 and dots_cam1 are different lengths: {len(dots_cam0)} vs {len(dots_cam1)}") if len(dots_cam0) != len(grid): - ValueError(f"ERROR: dots_cam0 and grid are different lengths:" - f" - length of dot_cam0: {len(dots_cam0)}" - f" - length of grid: {len(grid)}") - - num_file_pairs = len(dots_cam0) + raise ValueError(f"dots_cam0 and grid are different lengths: {len(dots_cam0)} vs {len(grid)}") - if method=="bundle_adjustment": - bundle(dots_cam0, dots_cam1, grid, img_dims, num_file_pairs) - elif method=="zhang": - zhang(dots_cam0, dots_cam1, grid, img_dims, num_file_pairs) - elif method=="cpp": - cpp(dots_cam0, dots_cam1, grid, img_dims, num_file_pairs) - else: - raise ValueError(f"ERROR: Unknown calibration method: {method}. " - f"Allowed options: 'bundle', 'zhang', 'cpp'") + # check elements size of dots/grid + if isinstance(dots_cam0, list): + for i, (d0, d1, g) in enumerate(zip(dots_cam0, dots_cam1, grid)): + if d0.shape != d1.shape: + raise ValueError(f"Shape mismatch at index {i}: dots_cam0 {d0.shape} vs dots_cam1 {d1.shape}") + if d0.shape[0] != g.shape[0]: + raise ValueError(f"Point count mismatch at index {i}: dots {d0.shape[0]} vs grid {g.shape[0]}") + # --- filenames check (only if provided) --- + if filenames is not None and len(filenames) != len(dots_cam0): + raise ValueError(f"filenames length={len(filenames)} does not match with dots length={len(dots_cam0)}") + # image dimensions check + if len(img_dims) != 2: + raise ValueError(f"img_dims should have 2 elements (width, height), got {len(img_dims)}") + if any(d <= 0 for d in img_dims): + raise ValueError(f"img_dims must be positive, got {img_dims}") + if not isinstance(optimize_distortion, (bool, np.bool_)): + raise TypeError("optimize_distortion must be a boolean") -def cpp(dots_cam0, dots_cam1, grid, img_dims, num_file_pairs): + num_file_pairs = len(dots_cam0) + flat_dots_cam0 = np.concatenate(dots_cam0,axis=0).astype(np.float32).ravel().tolist() flat_dots_cam1 = np.concatenate(dots_cam1,axis=0).astype(np.float32).ravel().tolist() flat_grid = np.concatenate(grid, axis=0).astype(np.float32).ravel().tolist() lengths = np.array([arr.shape[0] for arr in dots_cam1],dtype=np.int32).tolist() - # initial parameter guess + + common_py_util.info(f"Performing Initial calibration guess...") + + # initial parameter guess with fixed distortion parameters flags = cv2.CALIB_FIX_K1 | cv2.CALIB_FIX_K2 | cv2.CALIB_FIX_K3 | cv2.CALIB_ZERO_TANGENT_DIST _, K0, D0, rvecs0, tvecs0 = cv2.calibrateCamera(grid, dots_cam0, img_dims, None, None, flags=flags) _, K1, D1, rvecs1, tvecs1 = cv2.calibrateCamera(grid, dots_cam1, img_dims, None, None, flags=flags) + # stereo calibration with variable distortion parameters. Zhang method. + criteria = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, 100, 1e-6) ret, K0_stereo, D0_stereo, K1_stereo, D1_stereo, R_stereo, T_stereo, E, F = cv2.stereoCalibrate( grid, dots_cam0, dots_cam1, K0, D0, K1, D1, img_dims, - flags=cv2.CALIB_FIX_INTRINSIC + flags=cv2.CALIB_USE_INTRINSIC_GUESS, + criteria=criteria ) - rvec_stereo, _ = cv2.Rodrigues(R_stereo) - - D0 = D0.flatten() - D1 = D1.flatten() - - fx0, fy0, cx0, cy0 = K0[0, 0], K0[1, 1], K0[0, 2], K0[1, 2] - fx1, fy1, cx1, cy1 = K1[0, 0], K1[1, 1], K1[0, 2], K1[1, 2] - - - # Initial poses from intrinsics_cam0 - initial_poses_cam0 = [] - for i in range(num_file_pairs): - initial_poses_cam0.extend(rvecs0[i].flatten()) - initial_poses_cam0.extend(tvecs0[i].flatten()) - - - # full list of initial parameters - initial_params = np.hstack([fx0, fy0, cx0, cy0, D0, - fx1, fy1, cx1, cy1, D1, - rvec_stereo.flatten(), T_stereo.flatten(), - initial_poses_cam0]) - - - flat_initial_params = initial_params.ravel().tolist() - - calibcpp.stereo_calibration(flat_initial_params,flat_dots_cam0, flat_dots_cam1, flat_grid, - lengths, img_dims[0], img_dims[1], num_file_pairs) - - - -def zhang(dots_cam0, dots_cam1, grid, img_dims, num_file_pairs): + common_py_util.info(f"Initial calibration guess completed.") - print(f"Running calibration with {len(grid)} valid image pairs...") + # Compute consistent cam0 poses using refined intrinsics + rvecs0_consistent = [] + tvecs0_consistent = [] + for i in range(len(grid)): + ret, rvec, tvec = cv2.solvePnP(grid[i], dots_cam0[i],K0_stereo, D0_stereo,flags=cv2.SOLVEPNP_ITERATIVE) + rvecs0_consistent.append(rvec) + tvecs0_consistent.append(tvec) - # Left and Right cam calib - _, K0, D0, rvec0, tvec0 = cv2.calibrateCamera(grid, dots_cam0, img_dims, None, None) - _, K1, D1, rvec1, tvec1 = cv2.calibrateCamera(grid, dots_cam1, img_dims, None, None) + # check reprojection following refinement + for i in range(len(rvecs0)): + R0, _ = cv2.Rodrigues(rvecs0_consistent[i]) + R1_expected = R_stereo @ R0 + T1_expected = R_stereo @ tvecs0_consistent[i] + T_stereo - error0 = [] - error1 = [] + proj, _ = cv2.projectPoints(grid[i], cv2.Rodrigues(R1_expected)[0], T1_expected, K1_stereo, D1_stereo) + err = np.mean(np.linalg.norm(proj.squeeze() - dots_cam1[i].squeeze(), axis=1)) + #print(f"Refinement: Image {i} cam1 reprojection error: {err:.4f} px") - for i, objp in enumerate(grid): + # visualize_initial_projection_no_images( + # grid, + # dots_cam0, dots_cam1, + # K0, D0, rvecs0, tvecs0, + # K1, D1, rvecs1, tvecs1, + # img_dims + # ) + # - # Projected points - projected_points_opt0, _ = cv2.projectPoints(objp, rvec0[i], tvec0[i], K0, D0) - projected_points_opt1, _ = cv2.projectPoints(objp, rvec1[i], tvec1[i], K1, D1) - - # Ensure points are Nx2 arrays - projected_points_opt0 = projected_points_opt0.reshape(-1, 2) - projected_points_opt1 = projected_points_opt1.reshape(-1, 2) - - dots_cam0_i = dots_cam0[i].reshape(-1, 2) - dots_cam1_i = dots_cam1[i].reshape(-1, 2) - - # Compute RMS reprojection error - diff0 = np.sqrt(np.sum((dots_cam0_i - projected_points_opt0)**2, axis=1)) - diff1 = np.sqrt(np.sum((dots_cam1_i - projected_points_opt1)**2, axis=1)) - - error0 = np.mean(diff0) - error1 = np.mean(diff1) - print("ERROR", error0, error1) - - print(f"Mean left RMS error: {np.mean(error0):.4f} px") - print(f"Mean right RMS error: {np.mean(error1):.4f} px") - - # stereo calib - ret, K0_opt, D0_opt, K1_opt, D1_opt, R, T, E, F = cv2.stereoCalibrate( - objectPoints=grid, - imagePoints1=dots_cam0, - imagePoints2=dots_cam1, - cameraMatrix1=K0, - distCoeffs1=D0, - cameraMatrix2=K1, - distCoeffs2=D1, - imageSize=img_dims, - flags=0 - ) - - print("\n--- Calibration Results ---") - print("Calibration RMS error:", ret) - print('\nLeft Camera Matrix:\n', K0_opt) - print('Left Distortion Coefficients:\n', D0_opt) - print('\nRight Camera Matrix:\n', K1_opt) - print('Right Distortion Coefficients:\n', D1_opt) - print('\nRotation Matrix (R):\n', R) - print('Translation Vector (T):\n', T) - - error0 = [] - error1 = [] - - - _, K0_test, D0_test, rvec0_opt, tvec0_opt = cv2.calibrateCamera( - objectPoints=grid, - imagePoints=dots_cam0, - imageSize=img_dims, - cameraMatrix=K0_opt, - distCoeffs=D0_opt, - flags=cv2.CALIB_FIX_INTRINSIC - ) - - _, K1_test, D1_test, rvec1_opt, tvec1_opt = cv2.calibrateCamera( - objectPoints=grid, - imagePoints=dots_cam1, - imageSize=img_dims, - cameraMatrix=K1_opt, - distCoeffs=D1_opt, - flags=cv2.CALIB_FIX_INTRINSIC - ) - - - for i, objp in enumerate(grid): - - # Projected points - projected_points_opt0, _ = cv2.projectPoints(objp, rvec0[i], tvec0[i], K0_opt, D0_opt) - projected_points_opt1, _ = cv2.projectPoints(objp, rvec1[i], tvec1[i], K1_opt, D1_opt) - - # Ensure points are Nx2 arrays - projected_points_opt0 = projected_points_opt0.reshape(-1, 2) - projected_points_opt1 = projected_points_opt1.reshape(-1, 2) - - dots_cam0_i = dots_cam0[i].reshape(-1, 2) - dots_cam1_i = dots_cam1[i].reshape(-1, 2) - - # Compute RMS reprojection error - diff0 = np.sqrt(np.sum((dots_cam0_i - projected_points_opt0)**2, axis=1)) - diff1 = np.sqrt(np.sum((dots_cam1_i - projected_points_opt1)**2, axis=1)) - - error0 = np.mean(diff0) - error1 = np.mean(diff1) - print("ERROR", error0, error1) - - fig, ax = plt.subplots(1, 2, figsize=(20, 6)) - ax[0].scatter(dots_cam0_i[:, 0], dots_cam0_i[:, 1], label='Observed', c='blue') - ax[0].scatter(projected_points_opt0[:, 0], projected_points_opt0[:, 1], label='Projected', c='red', marker='x') - ax[1].scatter(dots_cam1_i[:, 0], dots_cam1_i[:, 1], label='Observed', c='blue') - ax[1].scatter(projected_points_opt1[:, 0], projected_points_opt1[:, 1], label='Projected', c='red', marker='x') - plt.gca().invert_yaxis() # Optional: match image coordinates - plt.ticklabel_format(style='plain') - plt.grid(True) - plt.show() - - print(f"Mean left RMS error: {np.mean(error0):.4f} px") - print(f"Mean right RMS error: {np.mean(error1):.4f} px") - - # Save as .npy (NumPy binary) - # np.save('stereo_calibration.npy', { - # 'ret': ret, - # 'Kl': Kl, - # 'Dl': Dl, - # 'Kr': Kr, - # 'Dr': Dr, - # 'R': R, - # 'T': T, - # 'E': E, - # 'F': F - # }) - - # # Save as .yaml (human-readable) - # calib_data = { - # 'ret': float(ret), - # 'Kl': Kl.tolist(), - # 'Dl': Dl.tolist(), - # 'Kr': Kr.tolist(), - # 'Dr': Dr.tolist(), - # 'R': R.tolist(), - # 'T': T.tolist(), - # 'E': E.tolist(), - # 'F': F.tolist() - # } - - # with open('stereo_calibration.yaml', 'w') as f: - # yaml.dump(calib_data, f) - -def bundle(dots_cam0, dots_cam1, grid, img_dims, num_file_pairs): - - flags = cv2.CALIB_FIX_K1 | cv2.CALIB_FIX_K2 | cv2.CALIB_FIX_K3 | cv2.CALIB_ZERO_TANGENT_DIST - _, K0, D0, rvecs0, tvecs0 = cv2.calibrateCamera(grid, dots_cam0, img_dims, None, None, flags=flags) - _, K1, D1, rvecs1, tvecs1 = cv2.calibrateCamera(grid, dots_cam1, img_dims, None, None, flags=flags) - - ret, K0_stereo, D0_stereo, K1_stereo, D1_stereo, R_stereo, T_stereo, E, F = cv2.stereoCalibrate( - grid, dots_cam0, dots_cam1, - K0, D0, K1, D1, - img_dims, - flags=cv2.CALIB_FIX_INTRINSIC - ) + # get into correct format for C++ rvec_stereo, _ = cv2.Rodrigues(R_stereo) - D0 = D0.flatten() - D1 = D1.flatten() - - fx0, fy0, cx0, cy0 = K0[0, 0], K0[1, 1], K0[0, 2], K0[1, 2] - fx1, fy1, cx1, cy1 = K1[0, 0], K1[1, 1], K1[0, 2], K1[1, 2] + # distortion + D0 = D0_stereo.flatten() + D1 = D1_stereo.flatten() + if not optimize_distortion: + D0 = np.zeros_like(D0) + D1 = np.zeros_like(D1) + # intrinsic cam matrix + fx0, fy0, fs0, cx0, cy0 = K0_stereo[0, 0], K0_stereo[1, 1], K0_stereo[0,1], K0_stereo[0, 2], K0_stereo[1, 2] + fx1, fy1, fs1, cx1, cy1 = K1_stereo[0, 0], K1_stereo[1, 1], K1_stereo[0,1], K1_stereo[0, 2], K1_stereo[1, 2] # Initial poses from intrinsics_cam0 initial_poses_cam0 = [] for i in range(num_file_pairs): - initial_poses_cam0.extend(rvecs0[i].flatten()) - initial_poses_cam0.extend(tvecs0[i].flatten()) + initial_poses_cam0.extend(rvecs0_consistent[i].flatten()) + initial_poses_cam0.extend(tvecs0_consistent[i].flatten()) # full list of initial parameters - initial_params = np.hstack([fx0, fy0, cx0, cy0, D0, - fx1, fy1, cx1, cy1, D1, + initial_params = np.hstack([fx0, fy0, fs0, cx0, cy0, D0, + fx1, fy1, fs1, cx1, cy1, D1, rvec_stereo.flatten(), T_stereo.flatten(), initial_poses_cam0]) - result = least_squares( - bundle_adjustment_error, - initial_params, - args=(grid, dots_cam0, dots_cam1, num_file_pairs), - verbose=2, - max_nfev=500, # Increased iterations for complex optimization - # x_scale=scales, - # bounds=(lower_bounds, upper_bounds), - ftol=1e-10, # Tighter tolerance for better accuracy - xtol=None + flat_initial_params = initial_params.ravel().tolist() + + + error_formulation_enum = ReprojError(error_formulation) + error_formulation_cpp = getattr(calibcpp.ReprojError, error_formulation_enum.name) + + #set the number of OMP threads + if num_threads is not None: + common_cpp.set_num_threads(num_threads) + + + common_py_util.info("Starting stereo calibration bundle adjustment...") + result_cpp = calibcpp.calibrate_stereo(flat_initial_params, + flat_dots_cam0, + flat_dots_cam1, + flat_grid, + lengths, + img_dims[0], + img_dims[1], + num_file_pairs, + bool(optimize_distortion), + precision, + max_iter, + error_formulation_cpp) + + calib_cpp = result_cpp.calib + calib = Calib( + cam0=CamIntrinsics(calib_cpp.cam0.fx, calib_cpp.cam0.fy, calib_cpp.cam0.fs, + calib_cpp.cam0.cx, calib_cpp.cam0.cy, + np.asarray(calib_cpp.cam0.distortion, dtype=np.float64)), + cam1=CamIntrinsics(calib_cpp.cam1.fx, calib_cpp.cam1.fy, calib_cpp.cam1.fs, + calib_cpp.cam1.cx, calib_cpp.cam1.cy, + np.asarray(calib_cpp.cam1.distortion, dtype=np.float64)), + translation=np.asarray(calib_cpp.translation, dtype=np.float64), + rotation=np.asarray(np.rad2deg(calib_cpp.rotation), dtype=np.float64), ) - # --- Step 7: Extract results --- - opt = result.x - fx0, fy0, cx0, cy0 = opt[0:4] - D0_opt = opt[4:9] - fx1, fy1, cx1, cy1 = opt[9:13] - D1_opt = opt[13:18] - rvec_stereo = opt[18:21] - tvec_stereo = opt[21:24] - base = 24 + 0 * 6 - rvec0 = opt[base:base+3] - tvec0 = opt[base+3:base+6] - - K0_opt = np.array([[fx0, 0, cx0], - [0, fy0, cy0], - [0, 0, 1]]) - K1_opt = np.array([[fx1, 0, cx1], - [0, fy1, cy1], - [0, 0, 1]]) - - print("\n--- Optimized Left Camera Intrinsics ---") - print("K0:\n", K0_opt) - print("Distortion:", D0_opt) - - print("\n--- Optimized Right Camera Intrinsics ---") - print("K1:\n", K1_opt) - print("Distortion:", D1_opt) - - print("\n--- Stereo Transform (Right from Left) ---") - print("Rotation Vector:", rvec_stereo) - print("Translation Vector:", tvec_stereo) - - # ADD THIS: Calculate right camera pose from stereo transform - R_stereo, _ = cv2.Rodrigues(rvec_stereo) - R0, _ = cv2.Rodrigues(rvec0) - T0 = tvec0.reshape(3, 1) - - # Right camera pose - R1 = R_stereo @ R0 - T1 = R_stereo @ T0 + tvec_stereo.reshape(3, 1) - rvec1, _ = cv2.Rodrigues(R1) - tvec1 = T1.flatten() # Make sure it's 1D for cv2.projectPoints - - - # Compute right camera pose from stereo transform - R_stereo, _ = cv2.Rodrigues(rvec_stereo) - R0, _ = cv2.Rodrigues(rvec0) - T0 = tvec0.reshape(3, 1) - - R1 = R_stereo @ R0 - T1 = R_stereo @ T0 + tvec_stereo.reshape(3, 1) - rvec1, _ = cv2.Rodrigues(R1) - tvec1 = T1.flatten() - - # Loop over all image pairs - for i in range(num_file_pairs): - rvec_i = opt[base + i*6 : base + i*6 + 3] - tvec_i = opt[base + i*6 + 3 : base + i*6 + 6] - - # Project points to cam0 - proj0, _ = cv2.projectPoints(grid[i], rvec_i, tvec_i, K0_opt, D0_opt) - proj0 = proj0.reshape(-1, 2) - - # Compose pose for cam1 - R_i, _ = cv2.Rodrigues(rvec_i) - R1_i = R_stereo @ R_i - T1_i = R_stereo @ tvec_i.reshape(3, 1) + tvec_stereo.reshape(3, 1) - rvec1_i, _ = cv2.Rodrigues(R1_i) - tvec1_i = T1_i.flatten() - - # Project points to cam1 - proj1, _ = cv2.projectPoints(grid[i], rvec1_i, tvec1_i, K1_opt, D1_opt) - proj1 = proj1.reshape(-1, 2) - - # Observed points - obs0 = dots_cam0[i].reshape(-1, 2) - obs1 = dots_cam1[i].reshape(-1, 2) - - print(np.sqrt((obs0 - proj0)**2)) - print(np.sqrt((obs1 - proj1)**2)) - - # RMS error - err0 = np.sqrt(np.sum((obs0 - proj0)**2, axis=1)).mean() - err1 = np.sqrt(np.sum((obs1 - proj1)**2, axis=1)).mean() - print(f"Image {i}: RMS Error cam0 = {err0:.3f}, cam1 = {err1:.3f}") - - # Plot - fig, ax = plt.subplots(1, 2, figsize=(16, 6)) - ax[0].scatter(obs0[:, 0], obs0[:, 1], c='blue', label='Observed') - ax[0].scatter(proj0[:, 0], proj0[:, 1], c='red', marker='x', label='Projected') - ax[0].set_title(f'Camera 0 - Image {i}') - ax[0].invert_yaxis() - ax[0].legend() - ax[0].grid(True) - - ax[1].scatter(obs1[:, 0], obs1[:, 1], c='blue', label='Observed') - ax[1].scatter(proj1[:, 0], proj1[:, 1], c='red', marker='x', label='Projected') - ax[1].set_title(f'Camera 1 - Image {i}') - ax[1].invert_yaxis() - ax[1].legend() - ax[1].grid(True) - - plt.tight_layout() - plt.show() - -def bundle_adjustment_error(params, gridpoints, dots_cam0, dots_cam1, num_img): - - # --- Extract intrinsics --- - fx0, fy0, cx0, cy0 = params[0:4] - D0 = params[4:9] - fx1, fy1, cx1, cy1 = params[9:13] - D1 = params[13:18] - - # Stereo tranlation and rotation - rvec_stereo = params[18:21] - tvec_stereo = params[21:24] - R_stereo, _ = cv2.Rodrigues(rvec_stereo) - - # Camera matrices - K0 = np.array([[fx0, 0, cx0], - [0, fy0, cy0], - [0, 0, 1]]) - K1 = np.array([[fx1, 0, cx1], - [0, fy1, cy1], - [0, 0, 1]]) - - pose0_start = 24 - residuals = [] - - for i in range(num_img): - - # Cam0 Pose - rvec0 = params[pose0_start + i*6 : pose0_start + i*6 + 3] - tvec0 = params[pose0_start + i*6 + 3 : pose0_start + i*6 + 6] - R0, _ = cv2.Rodrigues(rvec0) - T0 = tvec0.reshape(3, 1) - - # Cam1 pose (derived from cam0 + stereo) - R1 = R_stereo @ R0 - T1 = R_stereo @ T0 + tvec_stereo.reshape(3, 1) - rvec1, _ = cv2.Rodrigues(R1) - tvec1 = T1 - - # Projected points - proj0, _ = cv2.projectPoints(gridpoints[i], rvec0, tvec0, K0, D0) - proj1, _ = cv2.projectPoints(gridpoints[i], rvec1, tvec1, K1, D1) - proj0 = proj0.reshape(-1, 2) - proj1 = proj1.reshape(-1, 2) - - # residual - res0 = (proj0 - dots_cam0[i]).flatten() - res1 = (proj1 - dots_cam1[i]).flatten() - residuals.extend(res0) - residuals.extend(res1) - - return np.array(residuals) + errors0 = np.asarray(result_cpp.errors_cam0, dtype=np.float64) + errors1 = np.asarray(result_cpp.errors_cam1, dtype=np.float64) + + return calib, errors0, errors1 + + diff --git a/src/pyvale/calib/cpp/bindings.cpp b/src/pyvale/calib/cpp/bindings.cpp index 175f97435..1946d6258 100644 --- a/src/pyvale/calib/cpp/bindings.cpp +++ b/src/pyvale/calib/cpp/bindings.cpp @@ -17,6 +17,35 @@ namespace py = pybind11; PYBIND11_MODULE(calibcpp, m) { - m.def("stereo_calibration", &stereo_calibration, "stereo_calibration"); + py::add_ostream_redirect(m, "ostream_redirect"); + + py::class_(m, "CamIntrinsics") + .def(py::init<>()) + .def_readwrite("fx", &CamIntrinsics::fx) + .def_readwrite("fy", &CamIntrinsics::fy) + .def_readwrite("fs", &CamIntrinsics::fs) + .def_readwrite("cx", &CamIntrinsics::cx) + .def_readwrite("cy", &CamIntrinsics::cy) + .def_readwrite("distortion", &CamIntrinsics::distortion); + + + py::class_(m, "Calib") + .def(py::init<>()) + .def_readwrite("cam0", &Calib::cam0) + .def_readwrite("cam1", &Calib::cam1) + .def_readwrite("rotation", &Calib::rotation) + .def_readwrite("translation", &Calib::translation); + + py::class_(m, "StereoCalibResult") + .def_readonly("calib", &StereoCalibResult::calib) + .def_readonly("errors_cam0", &StereoCalibResult::errors_cam0) + .def_readonly("errors_cam1", &StereoCalibResult::errors_cam1); + + py::enum_(m, "ReprojError") + .value("RMSE", ReprojError::RMSE) + .value("MEAN", ReprojError::MEAN) + .value("MSE", ReprojError::MSE); + + m.def("calibrate_stereo", &calibrate_stereo, "Run stereo bundle adjustment and return calibrated parameters and per-image errors."); } diff --git a/src/pyvale/calib/cpp/calibdotdetect.cpp b/src/pyvale/calib/cpp/calibdotdetect.cpp deleted file mode 100644 index 26f91b9a5..000000000 --- a/src/pyvale/calib/cpp/calibdotdetect.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// ================================================================================ -// pyvale: the python validation engine -// License: MIT -// Copyright (C) 2025 The Computer Aided Validation Team -// ================================================================================ - -// STD library Header files -#include -#include -#include -#include - -// eigen header filesfiles -//#include - - diff --git a/src/pyvale/calib/cpp/calibopt.cpp b/src/pyvale/calib/cpp/calibopt.cpp index 75dc5e705..79867dfcf 100644 --- a/src/pyvale/calib/cpp/calibopt.cpp +++ b/src/pyvale/calib/cpp/calibopt.cpp @@ -5,15 +5,18 @@ // ================================================================================ // STD library Header files +#define _USE_MATH_DEFINES +#include #include #include #include #include #include #include +#include // common_cpp Header files -#include "../../common_cpp/defines.hpp" +#include "../../common_cpp/util.hpp" // Eigen Header Files #include @@ -32,16 +35,22 @@ namespace optimization { double ftol = 0; double xtol = 0; bool converged = false; - opt.lambda = 0.001; + opt.lambda = 0.01; + const double eps = 1e-10; while (iter < opt.max_iter) { // calculate updated parameters - iterate_cost(opt, dots_cam0, dots_cam1, grid, num_img, lengths); + iterate_cost(opt, dots_cam0, dots_cam1, grid, num_img, lengths, iter); + + // relative change of all parameters + const double dp_norm = std::sqrt(std::inner_product(opt.dp.begin(), opt.dp.end(), opt.dp.begin(), 0.0)); + const double p_norm = std::sqrt(std::inner_product( opt.p.begin(), opt.p.end(), opt.p.begin(), 0.0)); + xtol = dp_norm / (p_norm+eps); ftol = std::abs(opt.costpdp - opt.costp); - std::cout << iter << " " << opt.costp << " " << opt.costpdp << " " << ftol << std::endl; + //std::cout << iter << " " << opt.costp << " " << opt.costpdp << " " << ftol << std::endl; if ((xtol < opt.precision) && (ftol < opt.precision)) { converged=true; @@ -51,22 +60,27 @@ namespace optimization { } //return the residuals from the final iteration - opt.p = opt.pdp; - optimization::Output final_results = calc_residuals(opt.p, dots_cam0, dots_cam1, grid, num_img, lengths, false); + optimization::Output final_results = calc_residuals(opt.p, dots_cam0, dots_cam1, grid, num_img, lengths, iter, false); + final_results.iter = iter; return final_results; } - void iterate_cost(Parameters &opt, const std::vector &dots_cam0, const std::vector &dots_cam1, - const std::vector &grid, const size_t num_img, const std::vector &lengths){ + void iterate_cost(Parameters &opt, + const std::vector &dots_cam0, + const std::vector &dots_cam1, + const std::vector &grid, + const size_t num_img, + const std::vector &lengths, + const int iter){ // Compute residuals at current point. p get updated in this - optimization::Output res = calc_residuals(opt.p, dots_cam0, dots_cam1, grid, num_img, lengths, false); + optimization::Output res = calc_residuals(opt.p, dots_cam0, dots_cam1, grid, num_img, lengths, iter, false); // calculate jacobian - Eigen::MatrixXd J = calc_jac(opt.p, res.residuals, dots_cam0, dots_cam1, grid, num_img, lengths); + Eigen::MatrixXd J = calc_jac(opt.p, res.residuals, dots_cam0, dots_cam1, grid, num_img, iter, lengths); // calc gradient @@ -75,20 +89,35 @@ namespace optimization { // Hessian Eigen::MatrixXd H = J.transpose() * J; - // (H + lambda*Identity) - Eigen::MatrixXd A = H + opt.lambda * Eigen::MatrixXd::Identity(H.rows(), H.cols()); + // Remove fixed parameters from the linear system while retaining the + // coupled solution for every parameter that is allowed to vary. + for (int i = 0; i < opt.num_params; ++i) { + if (!opt.vary[i]) { + H.row(i).setZero(); + H.col(i).setZero(); + H(i, i) = 1.0; + g(i) = 0.0; + } + } + + // (H + lambda*diag(H)) + Eigen::VectorXd diagH = H.diagonal(); + Eigen::MatrixXd D = diagH.asDiagonal(); + Eigen::MatrixXd A = H + opt.lambda*D; + //Eigen::MatrixXd A = H + opt.lambda * Eigen::MatrixXd::Identity(H.rows(), H.cols()); // get change in parameters Eigen::VectorXd dp = A.ldlt().solve(-g); // Updated parameters for (int i = 0; i < opt.p.size(); i++) { - opt.pdp[i] = opt.p[i] + dp(i); + opt.dp[i] = opt.vary[i] ? dp(i) : 0.0; + opt.pdp[i] = opt.p[i] + opt.dp[i]; } // Evaluate new cost - optimization::Output res_new = calc_residuals(opt.pdp, dots_cam0, dots_cam1, grid, num_img, lengths, false); + optimization::Output res_new = calc_residuals(opt.pdp, dots_cam0, dots_cam1, grid, num_img, lengths, iter, false); opt.costp = 0.0; opt.costpdp = 0.0; for (int i = 0; i < res_new.residuals.size(); i++){ @@ -96,17 +125,56 @@ namespace optimization { opt.costpdp += res_new.residuals(i) * res_new.residuals(i); } - //std::cout << opt.costp << " " << opt.costpdp << std::endl; - - // Accept or reject step - if (opt.costpdp < opt.costp) { + + + double cost_cam0_p = 0.0, cost_cam1_p = 0.0; + double cost_cam0_pdp = 0.0, cost_cam1_pdp = 0.0; + for (int k = 0; k < res_new.residuals.size() / 4; ++k) { + double r0x = res.residuals(4*k+0), r0y = res.residuals(4*k+1); + double r1x = res.residuals(4*k+2), r1y = res.residuals(4*k+3); + cost_cam0_p += r0x*r0x + r0y*r0y; + cost_cam1_p += r1x*r1x + r1y*r1y; + r0x = res_new.residuals(4*k+0); + r0y = res_new.residuals(4*k+1); + r1x = res_new.residuals(4*k+2); + r1y = res_new.residuals(4*k+3); + cost_cam0_pdp += r0x*r0x + r0y*r0y; + cost_cam1_pdp += r1x*r1x + r1y*r1y; + } + + double actual = opt.costp - opt.costpdp; + double predicted = -g.dot(dp) - 0.5 * dp.dot(H * dp); + double rho = actual / predicted; + const bool step_accepted = rho > 0.0; + + + // std::cout << std::scientific << std::setprecision(4 + // << "[iter " << std::setw(3) << iter << "] " + // << "cost=" << opt.costp + // << " -> " << opt.costpdp + // << " dcost=" << actual + // << " rho=" << rho + // << " lambda=" << opt.lambda + // << " |dp|=" << dp.norm() + // << " cam0=" << cost_cam0_p << "->" << cost_cam0_pdp + // << " cam1=" << cost_cam1_p << "->" << cost_cam1_pdp + // << " step=" << (step_accepted ? "accepted" : "rejected") + // << '\n'; + + common_util::info_out( + "Iter= " + std::to_string(iter), + "cost=" + std::to_string(opt.costp) + "->" + std::to_string(opt.costpdp) + + " cam0=" + std::to_string(cost_cam0_pdp) + + " cam1=" + std::to_string(cost_cam1_pdp)); + + if (rho > 0) { opt.p = opt.pdp; - opt.lambda *= 0.1; - } - else { - opt.lambda *= 10.0; + opt.lambda *= std::max(1.0/3.0, 1.0 - pow(2*rho - 1, 3)); + } else { + opt.lambda *= 2.0; } + } @@ -128,17 +196,60 @@ namespace optimization { return rotation.toRotationMatrix(); } + Eigen::Vector3d matrix_to_rodrigues(const Eigen::Matrix3d &R) { + Eigen::Vector3d rvec; + + double trace = R.trace(); + double cos_theta = (trace - 1.0) * 0.5; + + // Clamp for numerical stability + cos_theta = std::min(1.0, std::max(-1.0, cos_theta)); + + double theta = std::acos(cos_theta); + + // v small angle + if (theta < 1e-10) + { + return Eigen::Vector3d::Zero(); + } + + // small angle + if (M_PI - theta < 1e-6) + { + Eigen::Vector3d axis; + + axis(0) = std::sqrt(std::max(0.0, (R(0,0) + 1.0) * 0.5)); + axis(1) = std::sqrt(std::max(0.0, (R(1,1) + 1.0) * 0.5)); + axis(2) = std::sqrt(std::max(0.0, (R(2,2) + 1.0) * 0.5)); + + // Fix signs using off-diagonal elements + if (R(0,1) < 0) axis(1) = -axis(1); + if (R(0,2) < 0) axis(2) = -axis(2); + + return theta * axis.normalized(); + } + + // General + Eigen::Vector3d axis; + axis << R(2,1) - R(1,2), + R(0,2) - R(2,0), + R(1,0) - R(0,1); + + axis /= (2.0 * std::sin(theta)); + + rvec = theta * axis; + return rvec; + } + // Project 3D points to 2D with distortion (assuming radial + tangential distortion) std::vector project_points(const std::vector &gridpoints_3d, - const Eigen::Vector3d &rvec, + const Eigen::Matrix3d &R, const Eigen::Vector3d &tvec, const Eigen::Matrix3d &K, const Eigen::VectorXd &D) { - - Eigen::Matrix3d R = rodrigues_to_matrix(rvec); std::vector projected(gridpoints_3d.size()*2); int count = 0; @@ -167,8 +278,8 @@ namespace optimization { const double y_distorted = y * radial + dy; // Project to image coordinates - projected[2*count+0] = K(0,0) * x_distorted + K(0,2); - projected[2*count+1] = K(1,1) * y_distorted + K(1,2); + projected[2*count+0] = K(0,0) * x_distorted + K(0,1) * y_distorted + K(0,2); + projected[2*count+1] = K(1,0) * x_distorted + K(1,1) * y_distorted + K(1,2); count++; } return projected; @@ -179,7 +290,7 @@ namespace optimization { optimization::Output calc_residuals(std::vector &p, const std::vector &dots_cam0, const std::vector &dots_cam1, const std::vector &grid, - const size_t num_img, const std::vector &lengths, + const size_t num_img, const std::vector &lengths, const int iter, const bool print_flag){ // ------------------------------------------------------ @@ -189,36 +300,52 @@ namespace optimization { // Camera matrices Eigen::Matrix3d K0, K1; - K0 << p[0], 0, p[2], 0, p[1], p[3], 0, 0, 1; - K1 << p[9], 0, p[11], 0, p[10], p[12], 0, 0, 1; + K0 << p[0], p[2], p[3], + 0, p[1], p[4], + 0, 0, 1.0; + + K1 << p[10], p[12], p[13], + 0, p[11], p[14], + 0, 0, 1.0; // cam 0 distortion parameters Eigen::VectorXd D0(5); - for (int i = 0; i < 5; i++) D0(i) = p[4 + i]; + for (int i = 0; i < 5; i++) D0(i) = p[5 + i]; // cam1 distortion parameters Eigen::VectorXd D1(5); - for (int i = 0; i < 5; i++) D1(i) = p[13 + i]; + for (int i = 0; i < 5; i++) D1(i) = p[15 + i]; // Stereo translation and rotation - Eigen::Vector3d rvec_stereo(p[18], p[19], p[20]); - Eigen::Vector3d tvec_stereo(p[21], p[22], p[23]); + Eigen::Vector3d rvec_stereo(p[20], p[21], p[22]); + Eigen::Vector3d tvec_stereo(p[23], p[24], p[25]); Eigen::Matrix3d R_stereo = rodrigues_to_matrix(rvec_stereo); - //cam0 projections start at element 24 - int start_cam0 = 24; + //cam0 projections start at element 26 + int start_cam0 = 26; // init residuals Eigen::VectorXd residuals(2*dots_cam0.size()); - std::vector proj0(2*dots_cam0.size()); - std::vector proj1(2*dots_cam0.size()); + std::vector proj0(dots_cam0.size()); + std::vector proj1(dots_cam1.size()); + + std::vector point_offsets(num_img); + std::vector offset_2d(num_img); + std::vector offset_3d(num_img); + + int cumulative_points = 0; + int cumulative_2d = 0; + int cumulative_3d = 0; - std::vector img_offsets(num_img); - int count = 0; for (size_t i = 0; i < num_img; i++) { - img_offsets[i] = count; - count += lengths[i]; + point_offsets[i] = cumulative_points; + offset_2d[i] = cumulative_2d; + offset_3d[i] = cumulative_3d; + + cumulative_points += lengths[i]; + cumulative_2d += 2 * lengths[i]; + cumulative_3d += 3 * lengths[i]; } @@ -227,7 +354,7 @@ namespace optimization { for (int i = 0; i < num_img; i++){ // rotation vector - Eigen::Vector3d rvec0(p[start_cam0 + i*6], + Eigen::Vector3d rvec0(p[start_cam0 + i*6 + 0], p[start_cam0 + i*6 + 1], p[start_cam0 + i*6 + 2]); @@ -239,37 +366,16 @@ namespace optimization { // rotation matrix Eigen::Matrix3d R0 = rodrigues_to_matrix(rvec0); - // Cam1 pose (derived from cam0 + stereo) - Eigen::Matrix3d R1 = R_stereo * R0; - Eigen::Vector3d T1 = R_stereo * tvec0 + tvec_stereo; - - // Convert R1 back to rotation vector for projection - // Simple conversion (can be improved for numerical stability) - Eigen::Vector3d rvec1; - double trace = R1.trace(); - double angle = acos((trace - 1) / 2); - if (angle < 1e-8) { - rvec1.setZero(); - } - else { - Eigen::Vector3d axis; - axis(0) = R1(2,1) - R1(1,2); - axis(1) = R1(0,2) - R1(2,0); - axis(2) = R1(1,0) - R1(0,1); - axis.normalize(); - rvec1 = angle * axis; - } + // Cam1 pose. From cam0 + stereo) + Eigen::Matrix3d R1 = R_stereo*R0; + Eigen::Vector3d T1 = R_stereo*tvec0 + tvec_stereo; + //convert grid for this image to a vector of eigen 3d points std::vector grid_img_i(lengths[i]); - int idx_start_3d = 0; - int idx_start_2d = 0; - - //get start index of the grid for this image - for (int j = 0; j < i; j++){ - idx_start_3d+=3*lengths[j]; - idx_start_2d+=2*lengths[j]; - } + int idx_start_3d = offset_3d[i]; + int idx_start_2d = offset_2d[i]; + int local_offset = point_offsets[i]; for (int j = 0; j < lengths[i]; j++){ grid_img_i[j](0) = grid[idx_start_3d+j*3+0]; @@ -278,64 +384,60 @@ namespace optimization { } // Projection - std::vector proj0_i = project_points(grid_img_i, rvec0, tvec0, K0, D0); - std::vector proj1_i = project_points(grid_img_i, rvec1, T1, K1, D1); - - // offset in results arrays for local copy - int local_offset = img_offsets[i]; + std::vector proj0_i = project_points(grid_img_i, R0, tvec0, K0, D0); + std::vector proj1_i = project_points(grid_img_i, R1, T1, K1, D1); // residuals - for (size_t j = 0; j < lengths[i]; j++) { + for (size_t pt = 0; pt < lengths[i]; pt++) { //global index - int global_idx = local_offset+j; - - if (print_flag) { - std::cout << std::setprecision(10) << dots_cam0[idx_start_2d+2*j+0] << " " << dots_cam0[idx_start_2d+2*j+1] << " "; - std::cout << std::setprecision(10) << proj0_i[2*j+0] << " " << proj0_i[2*j+1] << std::endl; - } + int global_idx = local_offset+pt; // residuals - residuals[4*global_idx+0] = proj0_i[2*j+0] - dots_cam0[idx_start_2d+2*j+0]; - residuals[4*global_idx+1] = proj0_i[2*j+1] - dots_cam0[idx_start_2d+2*j+1]; - residuals[4*global_idx+2] = proj1_i[2*j+0] - dots_cam1[idx_start_2d+2*j+0]; - residuals[4*global_idx+3] = proj1_i[2*j+1] - dots_cam1[idx_start_2d+2*j+1]; + residuals[4*global_idx+0] = proj0_i[2*pt+0] - dots_cam0[idx_start_2d+2*pt+0]; + residuals[4*global_idx+1] = proj0_i[2*pt+1] - dots_cam0[idx_start_2d+2*pt+1]; + residuals[4*global_idx+2] = proj1_i[2*pt+0] - dots_cam1[idx_start_2d+2*pt+0]; + residuals[4*global_idx+3] = proj1_i[2*pt+1] - dots_cam1[idx_start_2d+2*pt+1]; // populate master reprojection array for every image - proj0[2*global_idx+0] = proj0_i[2*j+0]; - proj0[2*global_idx+1] = proj0_i[2*j+1]; - proj1[2*global_idx+0] = proj1_i[2*j+0]; - proj1[2*global_idx+1] = proj1_i[2*j+1]; + proj0[2*global_idx+0] = proj0_i[2*pt+0]; // cam0 x + proj0[2*global_idx+1] = proj0_i[2*pt+1]; // cam0 y + proj1[2*global_idx+0] = proj1_i[2*pt+0]; // cam1 x + proj1[2*global_idx+1] = proj1_i[2*pt+1]; // cam1 y - // increment count - count++; + if (print_flag) { + std::cout << std::setprecision(10) << dots_cam0[idx_start_2d+2*pt+0] << " " << dots_cam0[idx_start_2d+2*pt+1] << " "; + std::cout << std::setprecision(10) << proj0_i[2*pt+0] << " " << proj0_i[2*pt+1] << " "; + std::cout << std::setprecision(10) << dots_cam1[idx_start_2d+2*pt+0] << " " << dots_cam1[idx_start_2d+2*pt+1] << " "; + std::cout << std::setprecision(10) << proj1_i[2*pt+0] << " " << proj1_i[2*pt+1] << std::endl; + } } if (print_flag) std::cout << std::endl; } - return {residuals, proj0, proj1}; + return {residuals, proj0, proj1, iter}; } Eigen::MatrixXd calc_jac(std::vector &p, const Eigen::VectorXd &r, const std::vector &dots_cam0, const std::vector &dots_cam1, - const std::vector &grid, const size_t num_img, const std::vector &lengths){ + const std::vector &grid, const size_t num_img, const int iter, const std::vector &lengths){ const int m = r.size(); const int n = p.size(); - const double h = 1e-8; + const double eps_fd = 1e-6; Eigen::MatrixXd jac(m,n); // perturb one parameter at a time - #pragma omp parallel for for (int j = 0; j < n; j++) { std::vector p_prime = p; - p_prime[j] += h; + double h_j = eps_fd; // * std::max(1.0, std::abs(p[j])); + p_prime[j] += h_j; - auto [r_prime, proj0, proj1] = calc_residuals(p_prime, dots_cam0, dots_cam1, grid, num_img, lengths, false); + auto [r_prime, proj0, proj1, _] = calc_residuals(p_prime, dots_cam0, dots_cam1, grid, num_img, lengths, iter, false); for (int i = 0; i < m; i++) { - jac(i, j) = (r_prime[i] - r[i]) / h; + jac(i, j) = (r_prime[i] - r[i]) / h_j; } } return jac; diff --git a/src/pyvale/calib/cpp/calibopt.hpp b/src/pyvale/calib/cpp/calibopt.hpp index d9db81c1c..c57b9cc38 100644 --- a/src/pyvale/calib/cpp/calibopt.hpp +++ b/src/pyvale/calib/cpp/calibopt.hpp @@ -29,6 +29,7 @@ namespace optimization { std::vector p; // hard coded optimzation parameters std::vector dp; // deltaP std::vector pdp; // P + deltaP + std::vector vary; // whether each parameter is optimized int max_iter; double precision; int px_vert; @@ -39,7 +40,7 @@ namespace optimization { // Constructor to initialize vectors and other parameters Parameters(int num_params_, int max_iter_, - double precision_, int px_vert_, int px_hori_) + double precision_) : num_params(num_params_), lambda(0.01), @@ -48,10 +49,9 @@ namespace optimization { p(num_params, 0.0), dp(num_params, 0.0), pdp(num_params, 0.0), + vary(num_params, true), max_iter(max_iter_), - precision(precision_), - px_vert(px_vert_), - px_hori(px_hori_) {} + precision(precision_) {} }; @@ -59,26 +59,50 @@ namespace optimization { Eigen::VectorXd residuals; std::vector proj0; std::vector proj1; + int iter; }; // master optimization routine - optimization::Output bundle_adjustment(Parameters &opt, const std::vector &dots_cam0, const std::vector &dots_cam1, - const std::vector &grid, const size_t num_img, const std::vector &lengths); + optimization::Output bundle_adjustment(Parameters &opt, + const std::vector &dots_cam0, + const std::vector &dots_cam1, + const std::vector &grid, + const size_t num_img, + const std::vector &lengths); // single iteration of optimization - void iterate_cost(Parameters &opt, const std::vector &dots_cam0, const std::vector &dots_cam1, - const std::vector &grid, const size_t num_img, const std::vector &lengths); + void iterate_cost(Parameters &opt, + const std::vector &dots_cam0, + const std::vector &dots_cam1, + const std::vector &grid, + const size_t num_img, + const std::vector &lengths, + const int iter); // calculate jacobian - Eigen::MatrixXd calc_jac(std::vector &p, const Eigen::VectorXd &r, const std::vector &dots_cam0, const std::vector &dots_cam1, - const std::vector &grid, const size_t num_img, const std::vector &lengths); + Eigen::MatrixXd calc_jac(std::vector &p, + const Eigen::VectorXd &r, + const std::vector &dots_cam0, + const std::vector &dots_cam1, + const std::vector &grid, + const size_t num_img, + const int iter, + const std::vector &lengths); // calculate residuals - optimization::Output calc_residuals(std::vector &p, const std::vector &dots_cam0, - const std::vector &dots_cam1, const std::vector &grid, - const size_t num_img, const std::vector &lengths, const bool print_flag); - - + optimization::Output calc_residuals(std::vector &p, + const std::vector &dots_cam0, + const std::vector &dots_cam1, + const std::vector &grid, + const size_t num_img, + const std::vector &lengths, + const int iter, + const bool print_flag); + + + // Function to convert Rodrigues rotation vector to rotation matrix using Eigen + Eigen::Matrix3d rodrigues_to_matrix(const Eigen::Vector3d &rvec); + Eigen::Vector3d matrix_to_rodrigues(const Eigen::Matrix3d &R); } #endif // CALIBOPT_H diff --git a/src/pyvale/calib/cpp/calibstereo.cpp b/src/pyvale/calib/cpp/calibstereo.cpp index d4b6d90b8..fa8f89d76 100644 --- a/src/pyvale/calib/cpp/calibstereo.cpp +++ b/src/pyvale/calib/cpp/calibstereo.cpp @@ -1,14 +1,18 @@ -// ================================================================================ +// ================================================================================) // pyvale: the python validation engine // License: MIT // Copyright (C) 2025 The Computer Aided Validation Team // ================================================================================ // STD library Header files +#define _USE_MATH_DEFINES #include #include #include #include +#include +#include +#include // Eigen header files #include @@ -17,31 +21,61 @@ #include "./calibopt.hpp" #include "./calibstereo.hpp" +#include "../../common_cpp/util.hpp" + + +StereoCalibResult calibrate_stereo(const std::vector &init_params, + const std::vector &dots_cam0, // 2d + const std::vector &dots_cam1, // 2d + const std::vector &grid, // 3d + const std::vector &lengths, + const int px_hori, const int px_vert, + const int num_img, + const bool optimize_distortion, + const double precision, + const int max_iter, + const ReprojError reproj_error){ + + int num_params = 5*2 + 5*2 + 3 + 3 + 6*num_img; + + if (init_params.size() != num_params){ + throw std::invalid_argument("Unexpected number of calibration parameters"); + } -void stereo_calibration(const std::vector &init_params, - const std::vector &dots_cam0, - const std::vector &dots_cam1, - const std::vector &grid, - const std::vector &lengths, - const int px_hori, const int px_vert, const int num_img){ - - int num_params = 4*2 + 5*2 + 3 + 3 + 6*num_img; - optimization::Parameters opt(init_params.size(), 100, 0.001, px_hori, px_vert); + optimization::Parameters opt(init_params.size(), max_iter, precision); // assign initial guess for parameter values for (int i = 0; i < init_params.size(); i++){ opt.p[i] = init_params[i]; } + if (!optimize_distortion) { + for (int i = 5; i < 10; ++i) { + opt.p[i] = 0.0; + opt.vary[i] = false; + } + for (int i = 15; i < 20; ++i) { + opt.p[i] = 0.0; + opt.vary[i] = false; + } + } + // run optimization routine optimization::Output output = optimization::bundle_adjustment(opt, dots_cam0, dots_cam1, grid, num_img, lengths); + common_util::info_out("Optimization finished after " + std::to_string(output.iter+1) + " iterations.", ""); + + if ((output.iter+1) >= max_iter) { + common_util::info_out("WARN: Maximum number of iterations reached.", ""); + common_util::info_out("WARN: Optimization may not have converged.", ""); + } + + + common_util::info_out("Calculating final reprojection errors for each image...", ""); // calculate the error for each image based on the final residuals std::vector err0(num_img,0.0); std::vector err1(num_img,0.0); - std::string formulation = "MatchID"; - int img_start = 0; for (int img = 0; img < num_img; img++){ for (int d = 0; d < lengths[img]; d++){ @@ -58,38 +92,76 @@ void stereo_calibration(const std::vector &init_params, const double dx1 = output.proj1[idx_x] - dots_cam1[idx_x]; const double dy1 = output.proj1[idx_y] - dots_cam1[idx_y]; - if (formulation=="MatchID"){ + if (reproj_error==ReprojError::MSE){ err0[img] += (dx0*dx0 + dy0*dy0)/(2.0*lengths[img]); err1[img] += (dx1*dx1 + dy1*dy1)/(2.0*lengths[img]); } - else if (formulation=="RMS"){ + else if (reproj_error==ReprojError::RMSE){ err0[img] += (dx0*dx0 + dy0*dy0)/(lengths[img]); err1[img] += (dx1*dx1 + dy1*dy1)/(lengths[img]); } - else if (formulation=="mean"){ + else if (reproj_error==ReprojError::MEAN){ err0[img] += std::sqrt(dx0*dx0 + dy0*dy0)/(lengths[img]); err1[img] += std::sqrt(dx1*dx1 + dy1*dy1)/(lengths[img]); } else { - std::cout << "Unknown Reprojection Error formulation: '" << formulation << "'." << std::endl; - std::cout << "Allowed options: 'MatchID', 'RMS', 'mean'." << std::endl; + throw std::invalid_argument( + "Unknown reprojection error formulation: '" + "'. Allowed options are: 'MSE', 'RMS', 'mean'." + ); } } - if (formulation=="RMS"){ + if (reproj_error==ReprojError::RMSE){ err0[img] = std::sqrt(err0[img]); err1[img] = std::sqrt(err1[img]); } - img_start += lengths[img]; - std::cout << "error image " << img << ": " << err0[img] << " (L) " << err1[img] << " (R) " << std::endl; - } -} + img_start += 2*lengths[img]; + //std::cout << "error image " << img << ": " << err0[img] << " (L) " << err1[img] << " (R) " << std::endl; + + + + + int start_cam0 = 26; + // rotation vector + Eigen::Vector3d rvec0(opt.p[start_cam0 + img*6 + 0], + opt.p[start_cam0 + img*6 + 1], + opt.p[start_cam0 + img*6 + 2]); + // translation vector + Eigen::Vector3d tvec0(opt.p[start_cam0 + img*6 + 3], + opt.p[start_cam0 + img*6 + 4], + opt.p[start_cam0 + img*6 + 5]); + // rotation matrix + Eigen::Matrix3d R0 = optimization::rodrigues_to_matrix(rvec0); + Eigen::Vector3d rvec_stereo(opt.p[20], opt.p[21], opt.p[22]); + Eigen::Vector3d tvec_stereo(opt.p[23], opt.p[24], opt.p[25]); + Eigen::Matrix3d R_stereo = optimization::rodrigues_to_matrix(rvec_stereo); + // Cam1 pose. From cam0 + stereo + Eigen::Matrix3d R1 = R_stereo * R0; + Eigen::Vector3d rvec1 = optimization::matrix_to_rodrigues(R1); + Eigen::Vector3d tvec1 = R_stereo * tvec0 + tvec_stereo; + } + + Calib calib{ + .cam0 = {opt.p[0], opt.p[1], opt.p[2], opt.p[3], opt.p[4], + {opt.p[5], opt.p[6], opt.p[7], opt.p[8], opt.p[9]}}, + .cam1 = {opt.p[10], opt.p[11], opt.p[12], opt.p[13], opt.p[14], + {opt.p[15], opt.p[16], opt.p[17], opt.p[18], opt.p[19]}}, + .translation = {opt.p[23], opt.p[24], opt.p[25]}, + .rotation = {opt.p[20], opt.p[21], opt.p[22]}, + }; + + + common_util::info_out("Calibration Finished.", ""); + + return {std::move(calib), std::move(err0), std::move(err1)}; +} diff --git a/src/pyvale/calib/cpp/calibstereo.hpp b/src/pyvale/calib/cpp/calibstereo.hpp index 51a356b09..1bb71eca4 100644 --- a/src/pyvale/calib/cpp/calibstereo.hpp +++ b/src/pyvale/calib/cpp/calibstereo.hpp @@ -15,13 +15,50 @@ #include #include -// master func that gets called from python -void stereo_calibration(const std::vector &init_params, - const std::vector &dots_cam0, - const std::vector &dots_cam1, - const std::vector &grid, - const std::vector &lengths, - const int px_hori, const int px_vert, const int num_img_pairs); +enum class ReprojError { + MSE, + RMSE, + MEAN +}; +/** Camera intrinsic parameters */ +struct CamIntrinsics { + double fx; /**< Focal length in x direction [pixels] */ + double fy; /**< Focal length in y direction [pixels] */ + double fs; /**< Skew coefficient [pixels] */ + double cx; /**< Principal point x-coordinate [pixels] */ + double cy; /**< Principal point y-coordinate [pixels] */ + std::vector distortion; /**< Distortion coefficients [kappa1, kappa2, p1, p2, kappa3] */ +}; + +/** Stereo camera calibration parameters */ +struct Calib { + CamIntrinsics cam0; /**< Camera 0 intrinsic parameters */ + CamIntrinsics cam1; /**< Camera 1 intrinsic parameters */ + std::vector translation; /**< Translation vector [x, y, z] in mm */ + std::vector rotation; /**< Stereo Rodrigues rotation vector [radians] */ +}; + + +/** Result of stereo calibration. Errors are reported independently per image. */ +struct StereoCalibResult { + Calib calib; + std::vector errors_cam0; + std::vector errors_cam1; +}; + +/** Run stereo bundle adjustment and return calibrated parameters and reprojection errors. */ +StereoCalibResult calibrate_stereo(const std::vector &init_params, + const std::vector &dots_cam0, + const std::vector &dots_cam1, + const std::vector &grid, + const std::vector &lengths, + const int px_hori, + const int px_vert, + const int num_img_pairs, + const bool optimize_distortion, + const double precision, + const int max_iter, + const ReprojError reproj_error); #endif diff --git a/src/pyvale/common_cpp/bindings.cpp b/src/pyvale/common_cpp/bindings.cpp index 1c465eedb..7303bcc50 100644 --- a/src/pyvale/common_cpp/bindings.cpp +++ b/src/pyvale/common_cpp/bindings.cpp @@ -25,9 +25,9 @@ PYBIND11_MODULE(common_cpp, m) { .def_readwrite("binary", &common_util::SaveConfig::binary) .def_readwrite("prefix", &common_util::SaveConfig::prefix) .def_readwrite("delimiter", &common_util::SaveConfig::delimiter) - .def_readwrite("at_end", &common_util::SaveConfig::at_end) .def_readwrite("output_below_threshold", &common_util::SaveConfig::output_below_threshold) .def_readwrite("shape_params", &common_util::SaveConfig::shape_params); m.def("set_num_threads", &common_util::set_num_threads, "Set number of OMP threads"); + m.def("get_num_threads", &common_util::get_num_threads, "Get number of OMP threads"); } diff --git a/src/pyvale/common_cpp/defines.hpp b/src/pyvale/common_cpp/defines.hpp index 5240adf8a..debc5fd6e 100644 --- a/src/pyvale/common_cpp/defines.hpp +++ b/src/pyvale/common_cpp/defines.hpp @@ -8,10 +8,6 @@ #ifndef DEFINES_H #define DEFINES_H -#define TITLE(a) std::cout << std::string(80, '-') << std::endl; std::cout << std::string(38 - std::strlen(a)/2, '-') << " " << a << " " << std::string(38 - std::strlen(a)/2, '-') << std::endl; std::cout << std::string(80, '-') << std::endl; -#define INFO_OUT(a,b) std::cout << " - " << std::left << std::setw(50) << a << b << std::endl; -#define DEBUGGER std::cout << __FILE__ << " " << __LINE__ << std::endl; - #ifdef CUDA #define CUDA_CALL(x) do { if((x) != cudaSuccess) {\ printf("Error at %s:%d\n",__FILE__,__LINE__);\ diff --git a/src/pyvale/common_cpp/dicsignalhandler.cpp b/src/pyvale/common_cpp/dicsignalhandler.cpp index b12c8595c..d47799758 100644 --- a/src/pyvale/common_cpp/dicsignalhandler.cpp +++ b/src/pyvale/common_cpp/dicsignalhandler.cpp @@ -6,6 +6,7 @@ #include "./dicsignalhandler.hpp" #include +#include std::atomic stop_request = false; @@ -14,3 +15,11 @@ void signalHandler(int signal) { stop_request = true; } } + +void raise_on_interrupt() { + if (stop_request) { + stop_request = false; // Reset for next operation + PyErr_SetString(PyExc_KeyboardInterrupt, ""); + throw pybind11::error_already_set(); + } +} diff --git a/src/pyvale/common_cpp/dicsignalhandler.hpp b/src/pyvale/common_cpp/dicsignalhandler.hpp index d3647aebe..957282c37 100644 --- a/src/pyvale/common_cpp/dicsignalhandler.hpp +++ b/src/pyvale/common_cpp/dicsignalhandler.hpp @@ -6,6 +6,8 @@ #pragma once #include +#include extern std::atomic stop_request; void signalHandler(int signal); +void raise_on_interrupt(); diff --git a/src/pyvale/common_cpp/img_read.cpp b/src/pyvale/common_cpp/img_read.cpp new file mode 100644 index 000000000..238941612 --- /dev/null +++ b/src/pyvale/common_cpp/img_read.cpp @@ -0,0 +1,249 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "./util.hpp" +#include "./img_read.hpp" + +#define STB_IMAGE_IMPLEMENTATION +#include "../common_cpp/stb_image.h" + +Image read_img(const std::string& fullpath) { + + + common_util::Timer time("to read " +std::filesystem::path(fullpath).filename().string() + ":", 2); + + // Find extension + auto dotPos = fullpath.find_last_of('.'); + if (dotPos == std::string::npos) { + throw std::runtime_error("File has no extension: " + fullpath); + } + + std::string ext = fullpath.substr(dotPos); + + // Convert extension to lowercase + std::transform(ext.begin(), ext.end(), ext.begin(), + [](unsigned char c) { return std::tolower(c); }); + + if (ext == ".tif" || ext == ".tiff") { + return read_tiff(fullpath); + } + else if (ext == ".bmp") { + return read_bmp(fullpath); + } + else { + throw std::runtime_error("Unsupported image format: " + ext); + } +} + +Image read_tiff(const std::string &fullpath) { + + + TIFF* tif = TIFFOpen(fullpath.c_str(), "r"); + if (!tif) throw std::runtime_error("Failed to open: " + fullpath); + uint32_t width = 0, height = 0; + TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width); + TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height); + uint16_t bps = 8, spp = 1; + TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bps); + TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &spp); + + // check whether tiff is int, uint, or f32 + uint16_t format; + int found = TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLEFORMAT, &format); + + Image img; + img.filename = std::filesystem::path(fullpath).filename().string(); + img.width = width; + img.height = height; + std::vector scanline(TIFFScanlineSize(tif)); + if (format == SAMPLEFORMAT_UINT && bps == 8) { + img.type = PixelType::UINT8; + img.data8.resize(width * height); + for (uint32_t row = 0; row < height; ++row) { + if (TIFFReadScanline(tif, scanline.data(), row) < 0) + throw std::runtime_error("Failed to read row " + std::to_string(row)); + if (spp == 1) { + std::memcpy(img.data8.data() + static_cast(row) * width, + scanline.data(), + width * sizeof(uint8_t)); + } + else { + for (uint32_t x = 0; x < width; ++x) + img.data8[row * width + x] = scanline[x * spp]; + } + } + } + else if (format == SAMPLEFORMAT_UINT && bps == 16) { + img.type = PixelType::UINT16; + img.data16.resize((size_t)width * height); + for (uint32_t row = 0; row < height; ++row) { + if (TIFFReadScanline(tif, scanline.data(), row) < 0) + throw std::runtime_error("Failed to read row " + std::to_string(row)); + auto *p = reinterpret_cast(scanline.data()); + if (spp == 1) { + std::memcpy(img.data16.data() + static_cast(row) * width, + scanline.data(), + static_cast(width) * sizeof(uint16_t)); + } + else { + for (uint32_t x = 0; x < width; ++x) + img.data16[row * width + x] = p[x * spp]; + } + } + } + else if (format == SAMPLEFORMAT_UINT && bps == 32) { + img.type = PixelType::UINT32; + img.data32.resize((size_t)width * height); + for (uint32_t row = 0; row < height; ++row) { + if (TIFFReadScanline(tif, scanline.data(), row) < 0) + throw std::runtime_error("Failed to read row " + std::to_string(row)); + auto *p = reinterpret_cast(scanline.data()); + if (spp == 1) { + std::memcpy(img.data32.data() + static_cast(row) * width, + scanline.data(), + static_cast(width) * sizeof(uint32_t)); + } + else { + for (uint32_t x = 0; x < width; ++x) + img.data32[row * width + x] = p[x * spp]; + } + } + } + else if (format == SAMPLEFORMAT_IEEEFP && bps == 32) { + img.type = PixelType::UINT32F; + img.data32f.resize((size_t)width * height); + for (uint32_t row = 0; row < height; ++row) { + if (TIFFReadScanline(tif, scanline.data(), row) < 0) + throw std::runtime_error("Failed to read row " + std::to_string(row)); + auto *p = reinterpret_cast(scanline.data()); + if (spp == 1) { + std::memcpy(img.data32f.data() + static_cast(row) * width, + scanline.data(), + static_cast(width) * sizeof(uint32_t)); + } + else { + for (uint32_t x = 0; x < width; ++x) + img.data32f[row * width + x] = p[x * spp]; + } + } + } + else { + const char* format_name = "unknown"; + switch (format) { + case SAMPLEFORMAT_UINT: format_name = "unsigned integer"; break; + case SAMPLEFORMAT_INT: format_name = "signed integer"; break; + case SAMPLEFORMAT_IEEEFP: format_name = "IEEE float"; break; + case SAMPLEFORMAT_VOID: format_name = "undefined"; break; + } + + TIFFClose(tif); + throw std::runtime_error( + "Unsupported TIFF pixel type: " + + std::to_string(bps) + "-bit " + format_name + + " (SampleFormat=" + std::to_string(format) + + ", SamplesPerPixel=" + std::to_string(spp) + ")."); + } + + TIFFClose(tif); + return img; +} + +Image read_bmp(const std::string& fullpath) { + int width = 0, height = 0, channels = 0; + + // First, check if image is 16-bit + if (stbi_is_16_bit(fullpath.c_str())) { + uint16_t* raw = stbi_load_16( + fullpath.c_str(), + &width, + &height, + &channels, + 0 + ); + + if (!raw) { + throw std::runtime_error( + "Failed to open BMP: " + fullpath + " (" + stbi_failure_reason() + ")" + ); + } + + Image img; + img.filename = std::filesystem::path(fullpath).filename().string(); + img.width = static_cast(width); + img.height = static_cast(height); + img.type = PixelType::UINT16; + const size_t pixel_count = static_cast(width) * height; + img.data16.resize(pixel_count); + + if (channels == 1) { + std::memcpy(img.data16.data(), raw, pixel_count * sizeof(uint16_t)); + } + else { + // Use first channel only + #pragma omp parallel for schedule(static) + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + img.data16[y * width + x] = + raw[(y * width + x) * channels]; + } + } + } + + stbi_image_free(raw); + return img; + } + else { + // Standard 8-bit BMP + uint8_t* raw = stbi_load( + fullpath.c_str(), + &width, + &height, + &channels, + 0 + ); + + if (!raw) { + throw std::runtime_error( + "Failed to open BMP: " + fullpath + " (" + stbi_failure_reason() + ")" + ); + } + + Image img; + img.filename = std::filesystem::path(fullpath).filename().string(); + img.width = static_cast(width); + img.height = static_cast(height); + img.type = PixelType::UINT8; + const size_t pixel_count = static_cast(width) * height; + img.data8.resize(pixel_count); + + if (channels == 1) { + std::memcpy(img.data8.data(), raw, pixel_count * sizeof(uint8_t)); + } + else { + // Use first channel only + #pragma omp parallel for schedule(static) + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + img.data8[y * width + x] = + raw[(y * width + x) * channels]; + } + } + } + + stbi_image_free(raw); + return img; + } +} diff --git a/src/pyvale/calib/cpp/calibdotdetect.hpp b/src/pyvale/common_cpp/img_read.hpp similarity index 54% rename from src/pyvale/calib/cpp/calibdotdetect.hpp rename to src/pyvale/common_cpp/img_read.hpp index ab0ceab97..70f0c947e 100644 --- a/src/pyvale/calib/cpp/calibdotdetect.hpp +++ b/src/pyvale/common_cpp/img_read.hpp @@ -4,17 +4,17 @@ // Copyright (C) 2025 The Computer Aided Validation Team // ================================================================================ -#ifndef CALIBDOTDETECT_H -#define CALIBDOTDETECT_H +#pragma once -// STD library Header files -#include -#include -#include #include +#include +#include +#include -// eigen header filesfiles -#include "./Eigen/Dense" +// common_cpp header files +#include "./util.hpp" +Image read_img(const std::string& filename); +Image read_bmp(const std::string& filename); +Image read_tiff(const std::string& filename); -#endif // CALIBDOTDETECT_H diff --git a/src/pyvale/common_cpp/progressbar.hpp b/src/pyvale/common_cpp/progressbar.hpp index 9e1d33047..a2a8d7ed0 100644 --- a/src/pyvale/common_cpp/progressbar.hpp +++ b/src/pyvale/common_cpp/progressbar.hpp @@ -3,10 +3,22 @@ // STD header files #include +#include #include #include #include #include +#include +#include +#include + +#if defined(_WIN32) +#include +#else +#include +#endif + +#include "./util.hpp" class ProgressBar { private: @@ -14,63 +26,133 @@ class ProgressBar { int total_iterations; int current_iter; std::chrono::steady_clock::time_point start_time; + std::chrono::steady_clock::time_point last_update_time; + int last_iter; bool started; - static constexpr int CONSOLE_WIDTH = 100; + std::size_t last_progress_chars; + double next_update_percent; + + static constexpr int BAR_WIDTH = 36; + static constexpr double UPDATE_INTERVAL_PERCENT = 1.0; public: // Constructor - ProgressBar(const std::string& msg, int total_iters) - : message(msg), total_iterations(total_iters), current_iter(0), started(false) {} - - // Update progress (call this each iteration) + ProgressBar(const std::string& msg, int total_iters) + : message(msg), + total_iterations(total_iters), + current_iter(0), + last_iter(0), + started(false), + last_progress_chars(0), + next_update_percent(0.0) {} + + // Update progress void update(int iteration) { + current_iter = std::clamp(iteration, 0, total_iterations); + double percent = 100.0; + if (total_iterations > 0) { + percent = (static_cast(current_iter) / total_iterations) * 100.0; + percent = std::clamp(percent, 0.0, 100.0); + } + + const bool is_first_update = !started; + const bool is_final_update = current_iter >= total_iterations; + const bool should_update = + is_first_update || is_final_update || percent >= next_update_percent; + + if (!should_update) return; + if (!started) { start_time = std::chrono::steady_clock::now(); + last_update_time = start_time; started = true; hide_cursor(); - } - current_iter = iteration; + std::string time = "[" + common_util::current_datetime_ms() +"] "; + + // Print message line once; keep the progress line open for carriage-return updates. + std::cout << time << message << "\n"; + } - // Calculate percentage - float percent = (static_cast(current_iter) / static_cast(total_iterations)) * 100.0f; + while (next_update_percent <= percent) { + next_update_percent += UPDATE_INTERVAL_PERCENT; + } - // Calculate elapsed time + // Time auto now = std::chrono::steady_clock::now(); auto duration = std::chrono::duration_cast(now - start_time); - - // Format time as HH:MM:SS.mmm or MM:SS.mmm - std::string time_str = format_duration(duration); - - // Build the progress string - std::ostringstream oss; - oss << "\r" << std::left << std::setw(40) << message << std::right - << "[" << std::setw(6) << current_iter << "/" << std::setw(6) << total_iterations << "] " - << std::fixed << std::setprecision(1) << std::setw(5) << percent << "% " - << "Time: [" << std::setw(12) << time_str << "]\t"; - std::cout << "\r" << oss.str() << std::flush; - + std::string time_str = common_util::format_duration_ms(duration); + + last_update_time = now; + last_iter = current_iter; + + // Progress bar + std::string bar = make_bar(percent); + + // Build progress line + std::ostringstream progress; + progress << "[" + << bar << "] " + << std::fixed << std::setprecision(1) + << std::setw(5) << percent << "% " + << "(" << std::setw(6) << current_iter + << "/" << std::setw(6) << total_iterations << ") " + << std::setprecision(2) + << "Time: " << std::setw(10) << time_str; + + const std::string progress_line = progress.str(); + const std::size_t padding = + last_progress_chars > progress_line.size() + ? last_progress_chars - progress_line.size() + : 0; + + std::cout << "\r" << progress_line << std::string(padding, 32) + << std::flush; + last_progress_chars = progress_line.size(); } - // Increment and update (convenient for loops) + // Increment helper void tick() { update(current_iter + 1); } - // Complete the progress bar (prints newline) + // Finish — leaves both message and progress bar visible, cursor on new line void finish() { - std::cout << std::endl; + if (started && current_iter < total_iterations) { + update(total_iterations); + } + if (started) { + std::cout << "\n" << std::flush; + } show_cursor(); } - // Reset the progress bar + // Reset void reset() { current_iter = 0; + last_iter = 0; started = false; + last_progress_chars = 0; + next_update_percent = 0.0; } private: - std::string format_duration(const std::chrono::milliseconds& duration) { + // Build visual bar + std::string make_bar(double percent) const { + int filled = static_cast(std::round((percent / 100.0) * BAR_WIDTH)); + int empty = BAR_WIDTH - filled; + + std::string bar; + bar.reserve(BAR_WIDTH); + + bar.append(filled, '#'); + bar.append(empty, '-'); + + return bar; + } + + // Format duration + std::string format_duration(const std::chrono::milliseconds& duration) const { auto total_seconds = duration.count() / 1000; auto milliseconds = duration.count() % 1000; @@ -79,28 +161,40 @@ class ProgressBar { int seconds = total_seconds % 60; std::ostringstream oss; + oss << std::setfill('0'); if (hours > 0) { - oss << std::setfill('0') - << std::setw(2) << hours << ":" + oss << std::setw(2) << hours << ":" << std::setw(2) << minutes << ":" << std::setw(2) << seconds << "." << std::setw(3) << milliseconds; } else { - oss << std::setfill('0') - << std::setw(2) << minutes << ":" + oss << std::setw(2) << minutes << ":" << std::setw(2) << seconds << "." << std::setw(3) << milliseconds; } + return oss.str(); } static void hide_cursor() { - std::cout << "\033[?25l" << std::flush; // ANSI escape code + if (is_terminal()) { + std::cout << "\033[?25l" << std::flush; + } } - + static void show_cursor() { - std::cout << "\033[?25h" << std::flush; // ANSI escape code + if (is_terminal()) { + std::cout << "\033[?25h" << std::flush; + } + } + + static bool is_terminal() { +#if defined(_WIN32) + return _isatty(_fileno(stdout)) != 0; +#else + return isatty(fileno(stdout)) != 0; +#endif } }; diff --git a/src/pyvale/common_cpp/stb_image.h b/src/pyvale/common_cpp/stb_image.h new file mode 100644 index 000000000..9eedabedc --- /dev/null +++ b/src/pyvale/common_cpp/stb_image.h @@ -0,0 +1,7988 @@ +/* stb_image - v2.30 - public domain image loader - http://nothings.org/stb + no warranty implied; use at your own risk + + Do this: + #define STB_IMAGE_IMPLEMENTATION + before you include this file in *one* C or C++ file to create the implementation. + + // i.e. it should look like this: + #include ... + #include ... + #include ... + #define STB_IMAGE_IMPLEMENTATION + #include "stb_image.h" + + You can #define STBI_ASSERT(x) before the #include to avoid using assert.h. + And #define STBI_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free + + + QUICK NOTES: + Primarily of interest to game developers and other people who can + avoid problematic images and only need the trivial interface + + JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib) + PNG 1/2/4/8/16-bit-per-channel + + TGA (not sure what subset, if a subset) + BMP non-1bpp, non-RLE + PSD (composited view only, no extra channels, 8/16 bit-per-channel) + + GIF (*comp always reports as 4-channel) + HDR (radiance rgbE format) + PIC (Softimage PIC) + PNM (PPM and PGM binary only) + + Animated GIF still needs a proper API, but here's one way to do it: + http://gist.github.com/urraka/685d9a6340b26b830d49 + + - decode from memory or through FILE (define STBI_NO_STDIO to remove code) + - decode from arbitrary I/O callbacks + - SIMD acceleration on x86/x64 (SSE2) and ARM (NEON) + + Full documentation under "DOCUMENTATION" below. + + +LICENSE + + See end of file for license information. + +RECENT REVISION HISTORY: + + 2.30 (2024-05-31) avoid erroneous gcc warning + 2.29 (2023-05-xx) optimizations + 2.28 (2023-01-29) many error fixes, security errors, just tons of stuff + 2.27 (2021-07-11) document stbi_info better, 16-bit PNM support, bug fixes + 2.26 (2020-07-13) many minor fixes + 2.25 (2020-02-02) fix warnings + 2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically + 2.23 (2019-08-11) fix clang static analysis warning + 2.22 (2019-03-04) gif fixes, fix warnings + 2.21 (2019-02-25) fix typo in comment + 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs + 2.19 (2018-02-11) fix warning + 2.18 (2018-01-30) fix warnings + 2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings + 2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes + 2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC + 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs + 2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes + 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes + 2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64 + RGB-format JPEG; remove white matting in PSD; + allocate large structures on the stack; + correct channel count for PNG & BMP + 2.10 (2016-01-22) avoid warning introduced in 2.09 + 2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED + + See end of file for full revision history. + + + ============================ Contributors ========================= + + Image formats Extensions, features + Sean Barrett (jpeg, png, bmp) Jetro Lauha (stbi_info) + Nicolas Schulz (hdr, psd) Martin "SpartanJ" Golini (stbi_info) + Jonathan Dummer (tga) James "moose2000" Brown (iPhone PNG) + Jean-Marc Lienher (gif) Ben "Disch" Wenger (io callbacks) + Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG) + Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip) + Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD) + github:urraka (animated gif) Junggon Kim (PNM comments) + Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA) + socks-the-fox (16-bit PNG) + Jeremy Sawicki (handle all ImageNet JPGs) + Optimizations & bugfixes Mikhail Morozov (1-bit BMP) + Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query) + Arseny Kapoulkine Simon Breuss (16-bit PNM) + John-Mark Allen + Carmelo J Fdez-Aguera + + Bug & warning fixes + Marc LeBlanc David Woo Guillaume George Martins Mozeiko + Christpher Lloyd Jerry Jansson Joseph Thomson Blazej Dariusz Roszkowski + Phil Jordan Dave Moore Roy Eltham + Hayaki Saito Nathan Reed Won Chun + Luke Graham Johan Duparc Nick Verigakis the Horde3D community + Thomas Ruf Ronny Chevalier github:rlyeh + Janez Zemva John Bartholomew Michal Cichon github:romigrou + Jonathan Blow Ken Hamada Tero Hanninen github:svdijk + Eugene Golushkov Laurent Gomila Cort Stratton github:snagar + Aruelien Pocheville Sergio Gonzalez Thibault Reuille github:Zelex + Cass Everitt Ryamond Barbiero github:grim210 + Paul Du Bois Engin Manap Aldo Culquicondor github:sammyhw + Philipp Wiesemann Dale Weiler Oriol Ferrer Mesia github:phprus + Josh Tobin Neil Bickford Matthew Gregan github:poppolopoppo + Julian Raschke Gregory Mullen Christian Floisand github:darealshinji + Baldur Karlsson Kevin Schmidt JR Smith github:Michaelangel007 + Brad Weinberger Matvey Cherevko github:mosra + Luca Sas Alexander Veselov Zack Middleton [reserved] + Ryan C. Gordon [reserved] [reserved] + DO NOT ADD YOUR NAME HERE + + Jacko Dirks + + To add your name to the credits, pick a random blank space in the middle and fill it. + 80% of merge conflicts on stb PRs are due to people adding their name at the end + of the credits. +*/ + +#ifndef STBI_INCLUDE_STB_IMAGE_H +#define STBI_INCLUDE_STB_IMAGE_H + +// DOCUMENTATION +// +// Limitations: +// - no 12-bit-per-channel JPEG +// - no JPEGs with arithmetic coding +// - GIF always returns *comp=4 +// +// Basic usage (see HDR discussion below for HDR usage): +// int x,y,n; +// unsigned char *data = stbi_load(filename, &x, &y, &n, 0); +// // ... process data if not NULL ... +// // ... x = width, y = height, n = # 8-bit components per pixel ... +// // ... replace '0' with '1'..'4' to force that many components per pixel +// // ... but 'n' will always be the number that it would have been if you said 0 +// stbi_image_free(data); +// +// Standard parameters: +// int *x -- outputs image width in pixels +// int *y -- outputs image height in pixels +// int *channels_in_file -- outputs # of image components in image file +// int desired_channels -- if non-zero, # of image components requested in result +// +// The return value from an image loader is an 'unsigned char *' which points +// to the pixel data, or NULL on an allocation failure or if the image is +// corrupt or invalid. The pixel data consists of *y scanlines of *x pixels, +// with each pixel consisting of N interleaved 8-bit components; the first +// pixel pointed to is top-left-most in the image. There is no padding between +// image scanlines or between pixels, regardless of format. The number of +// components N is 'desired_channels' if desired_channels is non-zero, or +// *channels_in_file otherwise. If desired_channels is non-zero, +// *channels_in_file has the number of components that _would_ have been +// output otherwise. E.g. if you set desired_channels to 4, you will always +// get RGBA output, but you can check *channels_in_file to see if it's trivially +// opaque because e.g. there were only 3 channels in the source image. +// +// An output image with N components has the following components interleaved +// in this order in each pixel: +// +// N=#comp components +// 1 grey +// 2 grey, alpha +// 3 red, green, blue +// 4 red, green, blue, alpha +// +// If image loading fails for any reason, the return value will be NULL, +// and *x, *y, *channels_in_file will be unchanged. The function +// stbi_failure_reason() can be queried for an extremely brief, end-user +// unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS +// to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly +// more user-friendly ones. +// +// Paletted PNG, BMP, GIF, and PIC images are automatically depalettized. +// +// To query the width, height and component count of an image without having to +// decode the full file, you can use the stbi_info family of functions: +// +// int x,y,n,ok; +// ok = stbi_info(filename, &x, &y, &n); +// // returns ok=1 and sets x, y, n if image is a supported format, +// // 0 otherwise. +// +// Note that stb_image pervasively uses ints in its public API for sizes, +// including sizes of memory buffers. This is now part of the API and thus +// hard to change without causing breakage. As a result, the various image +// loaders all have certain limits on image size; these differ somewhat +// by format but generally boil down to either just under 2GB or just under +// 1GB. When the decoded image would be larger than this, stb_image decoding +// will fail. +// +// Additionally, stb_image will reject image files that have any of their +// dimensions set to a larger value than the configurable STBI_MAX_DIMENSIONS, +// which defaults to 2**24 = 16777216 pixels. Due to the above memory limit, +// the only way to have an image with such dimensions load correctly +// is for it to have a rather extreme aspect ratio. Either way, the +// assumption here is that such larger images are likely to be malformed +// or malicious. If you do need to load an image with individual dimensions +// larger than that, and it still fits in the overall size limit, you can +// #define STBI_MAX_DIMENSIONS on your own to be something larger. +// +// =========================================================================== +// +// UNICODE: +// +// If compiling for Windows and you wish to use Unicode filenames, compile +// with +// #define STBI_WINDOWS_UTF8 +// and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convert +// Windows wchar_t filenames to utf8. +// +// =========================================================================== +// +// Philosophy +// +// stb libraries are designed with the following priorities: +// +// 1. easy to use +// 2. easy to maintain +// 3. good performance +// +// Sometimes I let "good performance" creep up in priority over "easy to maintain", +// and for best performance I may provide less-easy-to-use APIs that give higher +// performance, in addition to the easy-to-use ones. Nevertheless, it's important +// to keep in mind that from the standpoint of you, a client of this library, +// all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all. +// +// Some secondary priorities arise directly from the first two, some of which +// provide more explicit reasons why performance can't be emphasized. +// +// - Portable ("ease of use") +// - Small source code footprint ("easy to maintain") +// - No dependencies ("ease of use") +// +// =========================================================================== +// +// I/O callbacks +// +// I/O callbacks allow you to read from arbitrary sources, like packaged +// files or some other source. Data read from callbacks are processed +// through a small internal buffer (currently 128 bytes) to try to reduce +// overhead. +// +// The three functions you must define are "read" (reads some bytes of data), +// "skip" (skips some bytes of data), "eof" (reports if the stream is at the end). +// +// =========================================================================== +// +// SIMD support +// +// The JPEG decoder will try to automatically use SIMD kernels on x86 when +// supported by the compiler. For ARM Neon support, you must explicitly +// request it. +// +// (The old do-it-yourself SIMD API is no longer supported in the current +// code.) +// +// On x86, SSE2 will automatically be used when available based on a run-time +// test; if not, the generic C versions are used as a fall-back. On ARM targets, +// the typical path is to have separate builds for NEON and non-NEON devices +// (at least this is true for iOS and Android). Therefore, the NEON support is +// toggled by a build flag: define STBI_NEON to get NEON loops. +// +// If for some reason you do not want to use any of SIMD code, or if +// you have issues compiling it, you can disable it entirely by +// defining STBI_NO_SIMD. +// +// =========================================================================== +// +// HDR image support (disable by defining STBI_NO_HDR) +// +// stb_image supports loading HDR images in general, and currently the Radiance +// .HDR file format specifically. You can still load any file through the existing +// interface; if you attempt to load an HDR file, it will be automatically remapped +// to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1; +// both of these constants can be reconfigured through this interface: +// +// stbi_hdr_to_ldr_gamma(2.2f); +// stbi_hdr_to_ldr_scale(1.0f); +// +// (note, do not use _inverse_ constants; stbi_image will invert them +// appropriately). +// +// Additionally, there is a new, parallel interface for loading files as +// (linear) floats to preserve the full dynamic range: +// +// float *data = stbi_loadf(filename, &x, &y, &n, 0); +// +// If you load LDR images through this interface, those images will +// be promoted to floating point values, run through the inverse of +// constants corresponding to the above: +// +// stbi_ldr_to_hdr_scale(1.0f); +// stbi_ldr_to_hdr_gamma(2.2f); +// +// Finally, given a filename (or an open file or memory block--see header +// file for details) containing image data, you can query for the "most +// appropriate" interface to use (that is, whether the image is HDR or +// not), using: +// +// stbi_is_hdr(char *filename); +// +// =========================================================================== +// +// iPhone PNG support: +// +// We optionally support converting iPhone-formatted PNGs (which store +// premultiplied BGRA) back to RGB, even though they're internally encoded +// differently. To enable this conversion, call +// stbi_convert_iphone_png_to_rgb(1). +// +// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per +// pixel to remove any premultiplied alpha *only* if the image file explicitly +// says there's premultiplied data (currently only happens in iPhone images, +// and only if iPhone convert-to-rgb processing is on). +// +// =========================================================================== +// +// ADDITIONAL CONFIGURATION +// +// - You can suppress implementation of any of the decoders to reduce +// your code footprint by #defining one or more of the following +// symbols before creating the implementation. +// +// STBI_NO_JPEG +// STBI_NO_PNG +// STBI_NO_BMP +// STBI_NO_PSD +// STBI_NO_TGA +// STBI_NO_GIF +// STBI_NO_HDR +// STBI_NO_PIC +// STBI_NO_PNM (.ppm and .pgm) +// +// - You can request *only* certain decoders and suppress all other ones +// (this will be more forward-compatible, as addition of new decoders +// doesn't require you to disable them explicitly): +// +// STBI_ONLY_JPEG +// STBI_ONLY_PNG +// STBI_ONLY_BMP +// STBI_ONLY_PSD +// STBI_ONLY_TGA +// STBI_ONLY_GIF +// STBI_ONLY_HDR +// STBI_ONLY_PIC +// STBI_ONLY_PNM (.ppm and .pgm) +// +// - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still +// want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB +// +// - If you define STBI_MAX_DIMENSIONS, stb_image will reject images greater +// than that size (in either width or height) without further processing. +// This is to let programs in the wild set an upper bound to prevent +// denial-of-service attacks on untrusted data, as one could generate a +// valid image of gigantic dimensions and force stb_image to allocate a +// huge block of memory and spend disproportionate time decoding it. By +// default this is set to (1 << 24), which is 16777216, but that's still +// very big. + +#ifndef STBI_NO_STDIO +#include +#endif // STBI_NO_STDIO + +#define STBI_VERSION 1 + +enum +{ + STBI_default = 0, // only used for desired_channels + + STBI_grey = 1, + STBI_grey_alpha = 2, + STBI_rgb = 3, + STBI_rgb_alpha = 4 +}; + +#include +typedef unsigned char stbi_uc; +typedef unsigned short stbi_us; + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef STBIDEF +#ifdef STB_IMAGE_STATIC +#define STBIDEF static +#else +#define STBIDEF extern +#endif +#endif + +////////////////////////////////////////////////////////////////////////////// +// +// PRIMARY API - works on images of any type +// + +// +// load image by filename, open file, or memory buffer +// + +typedef struct +{ + int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read + void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative + int (*eof) (void *user); // returns nonzero if we are at end of file/data +} stbi_io_callbacks; + +//////////////////////////////////// +// +// 8-bits-per-channel interface +// + +STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels); + +#ifndef STBI_NO_STDIO +STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); +// for stbi_load_from_file, file pointer is left pointing immediately after image +#endif + +#ifndef STBI_NO_GIF +STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp); +#endif + +#ifdef STBI_WINDOWS_UTF8 +STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input); +#endif + +//////////////////////////////////// +// +// 16-bits-per-channel interface +// + +STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels); + +#ifndef STBI_NO_STDIO +STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); +STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); +#endif + +//////////////////////////////////// +// +// float-per-channel interface +// +#ifndef STBI_NO_LINEAR + STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels); + STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels); + + #ifndef STBI_NO_STDIO + STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels); + STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels); + #endif +#endif + +#ifndef STBI_NO_HDR + STBIDEF void stbi_hdr_to_ldr_gamma(float gamma); + STBIDEF void stbi_hdr_to_ldr_scale(float scale); +#endif // STBI_NO_HDR + +#ifndef STBI_NO_LINEAR + STBIDEF void stbi_ldr_to_hdr_gamma(float gamma); + STBIDEF void stbi_ldr_to_hdr_scale(float scale); +#endif // STBI_NO_LINEAR + +// stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR +STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user); +STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len); +#ifndef STBI_NO_STDIO +STBIDEF int stbi_is_hdr (char const *filename); +STBIDEF int stbi_is_hdr_from_file(FILE *f); +#endif // STBI_NO_STDIO + + +// get a VERY brief reason for failure +// on most compilers (and ALL modern mainstream compilers) this is threadsafe +STBIDEF const char *stbi_failure_reason (void); + +// free the loaded image -- this is just free() +STBIDEF void stbi_image_free (void *retval_from_stbi_load); + +// get image dimensions & components without fully decoding +STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp); +STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp); +STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len); +STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user); + +#ifndef STBI_NO_STDIO +STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp); +STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp); +STBIDEF int stbi_is_16_bit (char const *filename); +STBIDEF int stbi_is_16_bit_from_file(FILE *f); +#endif + + + +// for image formats that explicitly notate that they have premultiplied alpha, +// we just return the colors as stored in the file. set this flag to force +// unpremultiplication. results are undefined if the unpremultiply overflow. +STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply); + +// indicate whether we should process iphone images back to canonical format, +// or just pass them through "as-is" +STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert); + +// flip the image vertically, so the first pixel in the output array is the bottom left +STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip); + +// as above, but only applies to images loaded on the thread that calls the function +// this function is only available if your compiler supports thread-local variables; +// calling it will fail to link if your compiler doesn't +STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply); +STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert); +STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip); + +// ZLIB client - used by PNG, available for other purposes + +STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen); +STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header); +STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen); +STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); + +STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen); +STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen); + + +#ifdef __cplusplus +} +#endif + +// +// +//// end header file ///////////////////////////////////////////////////// +#endif // STBI_INCLUDE_STB_IMAGE_H + +#ifdef STB_IMAGE_IMPLEMENTATION + +#if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \ + || defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \ + || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \ + || defined(STBI_ONLY_ZLIB) + #ifndef STBI_ONLY_JPEG + #define STBI_NO_JPEG + #endif + #ifndef STBI_ONLY_PNG + #define STBI_NO_PNG + #endif + #ifndef STBI_ONLY_BMP + #define STBI_NO_BMP + #endif + #ifndef STBI_ONLY_PSD + #define STBI_NO_PSD + #endif + #ifndef STBI_ONLY_TGA + #define STBI_NO_TGA + #endif + #ifndef STBI_ONLY_GIF + #define STBI_NO_GIF + #endif + #ifndef STBI_ONLY_HDR + #define STBI_NO_HDR + #endif + #ifndef STBI_ONLY_PIC + #define STBI_NO_PIC + #endif + #ifndef STBI_ONLY_PNM + #define STBI_NO_PNM + #endif +#endif + +#if defined(STBI_NO_PNG) && !defined(STBI_SUPPORT_ZLIB) && !defined(STBI_NO_ZLIB) +#define STBI_NO_ZLIB +#endif + + +#include +#include // ptrdiff_t on osx +#include +#include +#include + +#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) +#include // ldexp, pow +#endif + +#ifndef STBI_NO_STDIO +#include +#endif + +#ifndef STBI_ASSERT +#include +#define STBI_ASSERT(x) assert(x) +#endif + +#ifdef __cplusplus +#define STBI_EXTERN extern "C" +#else +#define STBI_EXTERN extern +#endif + + +#ifndef _MSC_VER + #ifdef __cplusplus + #define stbi_inline inline + #else + #define stbi_inline + #endif +#else + #define stbi_inline __forceinline +#endif + +#ifndef STBI_NO_THREAD_LOCALS + #if defined(__cplusplus) && __cplusplus >= 201103L + #define STBI_THREAD_LOCAL thread_local + #elif defined(__GNUC__) && __GNUC__ < 5 + #define STBI_THREAD_LOCAL __thread + #elif defined(_MSC_VER) + #define STBI_THREAD_LOCAL __declspec(thread) + #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__) + #define STBI_THREAD_LOCAL _Thread_local + #endif + + #ifndef STBI_THREAD_LOCAL + #if defined(__GNUC__) + #define STBI_THREAD_LOCAL __thread + #endif + #endif +#endif + +#if defined(_MSC_VER) || defined(__SYMBIAN32__) +typedef unsigned short stbi__uint16; +typedef signed short stbi__int16; +typedef unsigned int stbi__uint32; +typedef signed int stbi__int32; +#else +#include +typedef uint16_t stbi__uint16; +typedef int16_t stbi__int16; +typedef uint32_t stbi__uint32; +typedef int32_t stbi__int32; +#endif + +// should produce compiler error if size is wrong +typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1]; + +#ifdef _MSC_VER +#define STBI_NOTUSED(v) (void)(v) +#else +#define STBI_NOTUSED(v) (void)sizeof(v) +#endif + +#ifdef _MSC_VER +#define STBI_HAS_LROTL +#endif + +#ifdef STBI_HAS_LROTL + #define stbi_lrot(x,y) _lrotl(x,y) +#else + #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (-(y) & 31))) +#endif + +#if defined(STBI_MALLOC) && defined(STBI_FREE) && (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED)) +// ok +#elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC) && !defined(STBI_REALLOC_SIZED) +// ok +#else +#error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC (or STBI_REALLOC_SIZED)." +#endif + +#ifndef STBI_MALLOC +#define STBI_MALLOC(sz) malloc(sz) +#define STBI_REALLOC(p,newsz) realloc(p,newsz) +#define STBI_FREE(p) free(p) +#endif + +#ifndef STBI_REALLOC_SIZED +#define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz) +#endif + +// x86/x64 detection +#if defined(__x86_64__) || defined(_M_X64) +#define STBI__X64_TARGET +#elif defined(__i386) || defined(_M_IX86) +#define STBI__X86_TARGET +#endif + +#if defined(__GNUC__) && defined(STBI__X86_TARGET) && !defined(__SSE2__) && !defined(STBI_NO_SIMD) +// gcc doesn't support sse2 intrinsics unless you compile with -msse2, +// which in turn means it gets to use SSE2 everywhere. This is unfortunate, +// but previous attempts to provide the SSE2 functions with runtime +// detection caused numerous issues. The way architecture extensions are +// exposed in GCC/Clang is, sadly, not really suited for one-file libs. +// New behavior: if compiled with -msse2, we use SSE2 without any +// detection; if not, we don't use it at all. +#define STBI_NO_SIMD +#endif + +#if defined(__MINGW32__) && defined(STBI__X86_TARGET) && !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD) +// Note that __MINGW32__ doesn't actually mean 32-bit, so we have to avoid STBI__X64_TARGET +// +// 32-bit MinGW wants ESP to be 16-byte aligned, but this is not in the +// Windows ABI and VC++ as well as Windows DLLs don't maintain that invariant. +// As a result, enabling SSE2 on 32-bit MinGW is dangerous when not +// simultaneously enabling "-mstackrealign". +// +// See https://github.com/nothings/stb/issues/81 for more information. +// +// So default to no SSE2 on 32-bit MinGW. If you've read this far and added +// -mstackrealign to your build settings, feel free to #define STBI_MINGW_ENABLE_SSE2. +#define STBI_NO_SIMD +#endif + +#if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET)) +#define STBI_SSE2 +#include + +#ifdef _MSC_VER + +#if _MSC_VER >= 1400 // not VC6 +#include // __cpuid +static int stbi__cpuid3(void) +{ + int info[4]; + __cpuid(info,1); + return info[3]; +} +#else +static int stbi__cpuid3(void) +{ + int res; + __asm { + mov eax,1 + cpuid + mov res,edx + } + return res; +} +#endif + +#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name + +#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2) +static int stbi__sse2_available(void) +{ + int info3 = stbi__cpuid3(); + return ((info3 >> 26) & 1) != 0; +} +#endif + +#else // assume GCC-style if not VC++ +#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16))) + +#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2) +static int stbi__sse2_available(void) +{ + // If we're even attempting to compile this on GCC/Clang, that means + // -msse2 is on, which means the compiler is allowed to use SSE2 + // instructions at will, and so are we. + return 1; +} +#endif + +#endif +#endif + +// ARM NEON +#if defined(STBI_NO_SIMD) && defined(STBI_NEON) +#undef STBI_NEON +#endif + +#ifdef STBI_NEON +#include +#ifdef _MSC_VER +#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name +#else +#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16))) +#endif +#endif + +#ifndef STBI_SIMD_ALIGN +#define STBI_SIMD_ALIGN(type, name) type name +#endif + +#ifndef STBI_MAX_DIMENSIONS +#define STBI_MAX_DIMENSIONS (1 << 24) +#endif + +/////////////////////////////////////////////// +// +// stbi__context struct and start_xxx functions + +// stbi__context structure is our basic context used by all images, so it +// contains all the IO context, plus some basic image information +typedef struct +{ + stbi__uint32 img_x, img_y; + int img_n, img_out_n; + + stbi_io_callbacks io; + void *io_user_data; + + int read_from_callbacks; + int buflen; + stbi_uc buffer_start[128]; + int callback_already_read; + + stbi_uc *img_buffer, *img_buffer_end; + stbi_uc *img_buffer_original, *img_buffer_original_end; +} stbi__context; + + +static void stbi__refill_buffer(stbi__context *s); + +// initialize a memory-decode context +static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len) +{ + s->io.read = NULL; + s->read_from_callbacks = 0; + s->callback_already_read = 0; + s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer; + s->img_buffer_end = s->img_buffer_original_end = (stbi_uc *) buffer+len; +} + +// initialize a callback-based context +static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *user) +{ + s->io = *c; + s->io_user_data = user; + s->buflen = sizeof(s->buffer_start); + s->read_from_callbacks = 1; + s->callback_already_read = 0; + s->img_buffer = s->img_buffer_original = s->buffer_start; + stbi__refill_buffer(s); + s->img_buffer_original_end = s->img_buffer_end; +} + +#ifndef STBI_NO_STDIO + +static int stbi__stdio_read(void *user, char *data, int size) +{ + return (int) fread(data,1,size,(FILE*) user); +} + +static void stbi__stdio_skip(void *user, int n) +{ + int ch; + fseek((FILE*) user, n, SEEK_CUR); + ch = fgetc((FILE*) user); /* have to read a byte to reset feof()'s flag */ + if (ch != EOF) { + ungetc(ch, (FILE *) user); /* push byte back onto stream if valid. */ + } +} + +static int stbi__stdio_eof(void *user) +{ + return feof((FILE*) user) || ferror((FILE *) user); +} + +static stbi_io_callbacks stbi__stdio_callbacks = +{ + stbi__stdio_read, + stbi__stdio_skip, + stbi__stdio_eof, +}; + +static void stbi__start_file(stbi__context *s, FILE *f) +{ + stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f); +} + +//static void stop_file(stbi__context *s) { } + +#endif // !STBI_NO_STDIO + +static void stbi__rewind(stbi__context *s) +{ + // conceptually rewind SHOULD rewind to the beginning of the stream, + // but we just rewind to the beginning of the initial buffer, because + // we only use it after doing 'test', which only ever looks at at most 92 bytes + s->img_buffer = s->img_buffer_original; + s->img_buffer_end = s->img_buffer_original_end; +} + +enum +{ + STBI_ORDER_RGB, + STBI_ORDER_BGR +}; + +typedef struct +{ + int bits_per_channel; + int num_channels; + int channel_order; +} stbi__result_info; + +#ifndef STBI_NO_JPEG +static int stbi__jpeg_test(stbi__context *s); +static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_PNG +static int stbi__png_test(stbi__context *s); +static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp); +static int stbi__png_is16(stbi__context *s); +#endif + +#ifndef STBI_NO_BMP +static int stbi__bmp_test(stbi__context *s); +static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_TGA +static int stbi__tga_test(stbi__context *s); +static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_PSD +static int stbi__psd_test(stbi__context *s); +static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc); +static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp); +static int stbi__psd_is16(stbi__context *s); +#endif + +#ifndef STBI_NO_HDR +static int stbi__hdr_test(stbi__context *s); +static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_PIC +static int stbi__pic_test(stbi__context *s); +static void *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_GIF +static int stbi__gif_test(stbi__context *s); +static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp); +static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp); +#endif + +#ifndef STBI_NO_PNM +static int stbi__pnm_test(stbi__context *s); +static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri); +static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp); +static int stbi__pnm_is16(stbi__context *s); +#endif + +static +#ifdef STBI_THREAD_LOCAL +STBI_THREAD_LOCAL +#endif +const char *stbi__g_failure_reason; + +STBIDEF const char *stbi_failure_reason(void) +{ + return stbi__g_failure_reason; +} + +#ifndef STBI_NO_FAILURE_STRINGS +static int stbi__err(const char *str) +{ + stbi__g_failure_reason = str; + return 0; +} +#endif + +static void *stbi__malloc(size_t size) +{ + return STBI_MALLOC(size); +} + +// stb_image uses ints pervasively, including for offset calculations. +// therefore the largest decoded image size we can support with the +// current code, even on 64-bit targets, is INT_MAX. this is not a +// significant limitation for the intended use case. +// +// we do, however, need to make sure our size calculations don't +// overflow. hence a few helper functions for size calculations that +// multiply integers together, making sure that they're non-negative +// and no overflow occurs. + +// return 1 if the sum is valid, 0 on overflow. +// negative terms are considered invalid. +static int stbi__addsizes_valid(int a, int b) +{ + if (b < 0) return 0; + // now 0 <= b <= INT_MAX, hence also + // 0 <= INT_MAX - b <= INTMAX. + // And "a + b <= INT_MAX" (which might overflow) is the + // same as a <= INT_MAX - b (no overflow) + return a <= INT_MAX - b; +} + +// returns 1 if the product is valid, 0 on overflow. +// negative factors are considered invalid. +static int stbi__mul2sizes_valid(int a, int b) +{ + if (a < 0 || b < 0) return 0; + if (b == 0) return 1; // mul-by-0 is always safe + // portable way to check for no overflows in a*b + return a <= INT_MAX/b; +} + +#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) +// returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow +static int stbi__mad2sizes_valid(int a, int b, int add) +{ + return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add); +} +#endif + +// returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow +static int stbi__mad3sizes_valid(int a, int b, int c, int add) +{ + return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) && + stbi__addsizes_valid(a*b*c, add); +} + +// returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow +#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM) +static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add) +{ + return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) && + stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add); +} +#endif + +#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR) +// mallocs with size overflow checking +static void *stbi__malloc_mad2(int a, int b, int add) +{ + if (!stbi__mad2sizes_valid(a, b, add)) return NULL; + return stbi__malloc(a*b + add); +} +#endif + +static void *stbi__malloc_mad3(int a, int b, int c, int add) +{ + if (!stbi__mad3sizes_valid(a, b, c, add)) return NULL; + return stbi__malloc(a*b*c + add); +} + +#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR) || !defined(STBI_NO_PNM) +static void *stbi__malloc_mad4(int a, int b, int c, int d, int add) +{ + if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL; + return stbi__malloc(a*b*c*d + add); +} +#endif + +// returns 1 if the sum of two signed ints is valid (between -2^31 and 2^31-1 inclusive), 0 on overflow. +static int stbi__addints_valid(int a, int b) +{ + if ((a >= 0) != (b >= 0)) return 1; // a and b have different signs, so no overflow + if (a < 0 && b < 0) return a >= INT_MIN - b; // same as a + b >= INT_MIN; INT_MIN - b cannot overflow since b < 0. + return a <= INT_MAX - b; +} + +// returns 1 if the product of two ints fits in a signed short, 0 on overflow. +static int stbi__mul2shorts_valid(int a, int b) +{ + if (b == 0 || b == -1) return 1; // multiplication by 0 is always 0; check for -1 so SHRT_MIN/b doesn't overflow + if ((a >= 0) == (b >= 0)) return a <= SHRT_MAX/b; // product is positive, so similar to mul2sizes_valid + if (b < 0) return a <= SHRT_MIN / b; // same as a * b >= SHRT_MIN + return a >= SHRT_MIN / b; +} + +// stbi__err - error +// stbi__errpf - error returning pointer to float +// stbi__errpuc - error returning pointer to unsigned char + +#ifdef STBI_NO_FAILURE_STRINGS + #define stbi__err(x,y) 0 +#elif defined(STBI_FAILURE_USERMSG) + #define stbi__err(x,y) stbi__err(y) +#else + #define stbi__err(x,y) stbi__err(x) +#endif + +#define stbi__errpf(x,y) ((float *)(size_t) (stbi__err(x,y)?NULL:NULL)) +#define stbi__errpuc(x,y) ((unsigned char *)(size_t) (stbi__err(x,y)?NULL:NULL)) + +STBIDEF void stbi_image_free(void *retval_from_stbi_load) +{ + STBI_FREE(retval_from_stbi_load); +} + +#ifndef STBI_NO_LINEAR +static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp); +#endif + +#ifndef STBI_NO_HDR +static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp); +#endif + +static int stbi__vertically_flip_on_load_global = 0; + +STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip) +{ + stbi__vertically_flip_on_load_global = flag_true_if_should_flip; +} + +#ifndef STBI_THREAD_LOCAL +#define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global +#else +static STBI_THREAD_LOCAL int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set; + +STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip) +{ + stbi__vertically_flip_on_load_local = flag_true_if_should_flip; + stbi__vertically_flip_on_load_set = 1; +} + +#define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \ + ? stbi__vertically_flip_on_load_local \ + : stbi__vertically_flip_on_load_global) +#endif // STBI_THREAD_LOCAL + +static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc) +{ + memset(ri, 0, sizeof(*ri)); // make sure it's initialized if we add new fields + ri->bits_per_channel = 8; // default is 8 so most paths don't have to be changed + ri->channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order + ri->num_channels = 0; + + // test the formats with a very explicit header first (at least a FOURCC + // or distinctive magic number first) + #ifndef STBI_NO_PNG + if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp, ri); + #endif + #ifndef STBI_NO_BMP + if (stbi__bmp_test(s)) return stbi__bmp_load(s,x,y,comp,req_comp, ri); + #endif + #ifndef STBI_NO_GIF + if (stbi__gif_test(s)) return stbi__gif_load(s,x,y,comp,req_comp, ri); + #endif + #ifndef STBI_NO_PSD + if (stbi__psd_test(s)) return stbi__psd_load(s,x,y,comp,req_comp, ri, bpc); + #else + STBI_NOTUSED(bpc); + #endif + #ifndef STBI_NO_PIC + if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri); + #endif + + // then the formats that can end up attempting to load with just 1 or 2 + // bytes matching expectations; these are prone to false positives, so + // try them later + #ifndef STBI_NO_JPEG + if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp, ri); + #endif + #ifndef STBI_NO_PNM + if (stbi__pnm_test(s)) return stbi__pnm_load(s,x,y,comp,req_comp, ri); + #endif + + #ifndef STBI_NO_HDR + if (stbi__hdr_test(s)) { + float *hdr = stbi__hdr_load(s, x,y,comp,req_comp, ri); + return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp); + } + #endif + + #ifndef STBI_NO_TGA + // test tga last because it's a crappy test! + if (stbi__tga_test(s)) + return stbi__tga_load(s,x,y,comp,req_comp, ri); + #endif + + return stbi__errpuc("unknown image type", "Image not of any known type, or corrupt"); +} + +static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int channels) +{ + int i; + int img_len = w * h * channels; + stbi_uc *reduced; + + reduced = (stbi_uc *) stbi__malloc(img_len); + if (reduced == NULL) return stbi__errpuc("outofmem", "Out of memory"); + + for (i = 0; i < img_len; ++i) + reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is sufficient approx of 16->8 bit scaling + + STBI_FREE(orig); + return reduced; +} + +static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int channels) +{ + int i; + int img_len = w * h * channels; + stbi__uint16 *enlarged; + + enlarged = (stbi__uint16 *) stbi__malloc(img_len*2); + if (enlarged == NULL) return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory"); + + for (i = 0; i < img_len; ++i) + enlarged[i] = (stbi__uint16)((orig[i] << 8) + orig[i]); // replicate to high and low byte, maps 0->0, 255->0xffff + + STBI_FREE(orig); + return enlarged; +} + +static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel) +{ + int row; + size_t bytes_per_row = (size_t)w * bytes_per_pixel; + stbi_uc temp[2048]; + stbi_uc *bytes = (stbi_uc *)image; + + for (row = 0; row < (h>>1); row++) { + stbi_uc *row0 = bytes + row*bytes_per_row; + stbi_uc *row1 = bytes + (h - row - 1)*bytes_per_row; + // swap row0 with row1 + size_t bytes_left = bytes_per_row; + while (bytes_left) { + size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp); + memcpy(temp, row0, bytes_copy); + memcpy(row0, row1, bytes_copy); + memcpy(row1, temp, bytes_copy); + row0 += bytes_copy; + row1 += bytes_copy; + bytes_left -= bytes_copy; + } + } +} + +#ifndef STBI_NO_GIF +static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel) +{ + int slice; + int slice_size = w * h * bytes_per_pixel; + + stbi_uc *bytes = (stbi_uc *)image; + for (slice = 0; slice < z; ++slice) { + stbi__vertical_flip(bytes, w, h, bytes_per_pixel); + bytes += slice_size; + } +} +#endif + +static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp) +{ + stbi__result_info ri; + void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8); + + if (result == NULL) + return NULL; + + // it is the responsibility of the loaders to make sure we get either 8 or 16 bit. + STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16); + + if (ri.bits_per_channel != 8) { + result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp); + ri.bits_per_channel = 8; + } + + // @TODO: move stbi__convert_format to here + + if (stbi__vertically_flip_on_load) { + int channels = req_comp ? req_comp : *comp; + stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc)); + } + + return (unsigned char *) result; +} + +static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, int *y, int *comp, int req_comp) +{ + stbi__result_info ri; + void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16); + + if (result == NULL) + return NULL; + + // it is the responsibility of the loaders to make sure we get either 8 or 16 bit. + STBI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16); + + if (ri.bits_per_channel != 16) { + result = stbi__convert_8_to_16((stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp); + ri.bits_per_channel = 16; + } + + // @TODO: move stbi__convert_format16 to here + // @TODO: special case RGB-to-Y (and RGBA-to-YA) for 8-bit-to-16-bit case to keep more precision + + if (stbi__vertically_flip_on_load) { + int channels = req_comp ? req_comp : *comp; + stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi__uint16)); + } + + return (stbi__uint16 *) result; +} + +#if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR) +static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp) +{ + if (stbi__vertically_flip_on_load && result != NULL) { + int channels = req_comp ? req_comp : *comp; + stbi__vertical_flip(result, *x, *y, channels * sizeof(float)); + } +} +#endif + +#ifndef STBI_NO_STDIO + +#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8) +STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide); +STBI_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default); +#endif + +#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8) +STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input) +{ + return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL); +} +#endif + +static FILE *stbi__fopen(char const *filename, char const *mode) +{ + FILE *f; +#if defined(_WIN32) && defined(STBI_WINDOWS_UTF8) + wchar_t wMode[64]; + wchar_t wFilename[1024]; + if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)/sizeof(*wFilename))) + return 0; + + if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)/sizeof(*wMode))) + return 0; + +#if defined(_MSC_VER) && _MSC_VER >= 1400 + if (0 != _wfopen_s(&f, wFilename, wMode)) + f = 0; +#else + f = _wfopen(wFilename, wMode); +#endif + +#elif defined(_MSC_VER) && _MSC_VER >= 1400 + if (0 != fopen_s(&f, filename, mode)) + f=0; +#else + f = fopen(filename, mode); +#endif + return f; +} + + +STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp) +{ + FILE *f = stbi__fopen(filename, "rb"); + unsigned char *result; + if (!f) return stbi__errpuc("can't fopen", "Unable to open file"); + result = stbi_load_from_file(f,x,y,comp,req_comp); + fclose(f); + return result; +} + +STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) +{ + unsigned char *result; + stbi__context s; + stbi__start_file(&s,f); + result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); + if (result) { + // need to 'unget' all the characters in the IO buffer + fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR); + } + return result; +} + +STBIDEF stbi__uint16 *stbi_load_from_file_16(FILE *f, int *x, int *y, int *comp, int req_comp) +{ + stbi__uint16 *result; + stbi__context s; + stbi__start_file(&s,f); + result = stbi__load_and_postprocess_16bit(&s,x,y,comp,req_comp); + if (result) { + // need to 'unget' all the characters in the IO buffer + fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR); + } + return result; +} + +STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *comp, int req_comp) +{ + FILE *f = stbi__fopen(filename, "rb"); + stbi__uint16 *result; + if (!f) return (stbi_us *) stbi__errpuc("can't fopen", "Unable to open file"); + result = stbi_load_from_file_16(f,x,y,comp,req_comp); + fclose(f); + return result; +} + + +#endif //!STBI_NO_STDIO + +STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels); +} + +STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user); + return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels); +} + +STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); +} + +STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); + return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp); +} + +#ifndef STBI_NO_GIF +STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp) +{ + unsigned char *result; + stbi__context s; + stbi__start_mem(&s,buffer,len); + + result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp); + if (stbi__vertically_flip_on_load) { + stbi__vertical_flip_slices( result, *x, *y, *z, *comp ); + } + + return result; +} +#endif + +#ifndef STBI_NO_LINEAR +static float *stbi__loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp) +{ + unsigned char *data; + #ifndef STBI_NO_HDR + if (stbi__hdr_test(s)) { + stbi__result_info ri; + float *hdr_data = stbi__hdr_load(s,x,y,comp,req_comp, &ri); + if (hdr_data) + stbi__float_postprocess(hdr_data,x,y,comp,req_comp); + return hdr_data; + } + #endif + data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp); + if (data) + return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp); + return stbi__errpf("unknown image type", "Image not of any known type, or corrupt"); +} + +STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__loadf_main(&s,x,y,comp,req_comp); +} + +STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); + return stbi__loadf_main(&s,x,y,comp,req_comp); +} + +#ifndef STBI_NO_STDIO +STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp) +{ + float *result; + FILE *f = stbi__fopen(filename, "rb"); + if (!f) return stbi__errpf("can't fopen", "Unable to open file"); + result = stbi_loadf_from_file(f,x,y,comp,req_comp); + fclose(f); + return result; +} + +STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp) +{ + stbi__context s; + stbi__start_file(&s,f); + return stbi__loadf_main(&s,x,y,comp,req_comp); +} +#endif // !STBI_NO_STDIO + +#endif // !STBI_NO_LINEAR + +// these is-hdr-or-not is defined independent of whether STBI_NO_LINEAR is +// defined, for API simplicity; if STBI_NO_LINEAR is defined, it always +// reports false! + +STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len) +{ + #ifndef STBI_NO_HDR + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__hdr_test(&s); + #else + STBI_NOTUSED(buffer); + STBI_NOTUSED(len); + return 0; + #endif +} + +#ifndef STBI_NO_STDIO +STBIDEF int stbi_is_hdr (char const *filename) +{ + FILE *f = stbi__fopen(filename, "rb"); + int result=0; + if (f) { + result = stbi_is_hdr_from_file(f); + fclose(f); + } + return result; +} + +STBIDEF int stbi_is_hdr_from_file(FILE *f) +{ + #ifndef STBI_NO_HDR + long pos = ftell(f); + int res; + stbi__context s; + stbi__start_file(&s,f); + res = stbi__hdr_test(&s); + fseek(f, pos, SEEK_SET); + return res; + #else + STBI_NOTUSED(f); + return 0; + #endif +} +#endif // !STBI_NO_STDIO + +STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user) +{ + #ifndef STBI_NO_HDR + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user); + return stbi__hdr_test(&s); + #else + STBI_NOTUSED(clbk); + STBI_NOTUSED(user); + return 0; + #endif +} + +#ifndef STBI_NO_LINEAR +static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f; + +STBIDEF void stbi_ldr_to_hdr_gamma(float gamma) { stbi__l2h_gamma = gamma; } +STBIDEF void stbi_ldr_to_hdr_scale(float scale) { stbi__l2h_scale = scale; } +#endif + +static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f; + +STBIDEF void stbi_hdr_to_ldr_gamma(float gamma) { stbi__h2l_gamma_i = 1/gamma; } +STBIDEF void stbi_hdr_to_ldr_scale(float scale) { stbi__h2l_scale_i = 1/scale; } + + +////////////////////////////////////////////////////////////////////////////// +// +// Common code used by all image loaders +// + +enum +{ + STBI__SCAN_load=0, + STBI__SCAN_type, + STBI__SCAN_header +}; + +static void stbi__refill_buffer(stbi__context *s) +{ + int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen); + s->callback_already_read += (int) (s->img_buffer - s->img_buffer_original); + if (n == 0) { + // at end of file, treat same as if from memory, but need to handle case + // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file + s->read_from_callbacks = 0; + s->img_buffer = s->buffer_start; + s->img_buffer_end = s->buffer_start+1; + *s->img_buffer = 0; + } else { + s->img_buffer = s->buffer_start; + s->img_buffer_end = s->buffer_start + n; + } +} + +stbi_inline static stbi_uc stbi__get8(stbi__context *s) +{ + if (s->img_buffer < s->img_buffer_end) + return *s->img_buffer++; + if (s->read_from_callbacks) { + stbi__refill_buffer(s); + return *s->img_buffer++; + } + return 0; +} + +#if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) +// nothing +#else +stbi_inline static int stbi__at_eof(stbi__context *s) +{ + if (s->io.read) { + if (!(s->io.eof)(s->io_user_data)) return 0; + // if feof() is true, check if buffer = end + // special case: we've only got the special 0 character at the end + if (s->read_from_callbacks == 0) return 1; + } + + return s->img_buffer >= s->img_buffer_end; +} +#endif + +#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) +// nothing +#else +static void stbi__skip(stbi__context *s, int n) +{ + if (n == 0) return; // already there! + if (n < 0) { + s->img_buffer = s->img_buffer_end; + return; + } + if (s->io.read) { + int blen = (int) (s->img_buffer_end - s->img_buffer); + if (blen < n) { + s->img_buffer = s->img_buffer_end; + (s->io.skip)(s->io_user_data, n - blen); + return; + } + } + s->img_buffer += n; +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM) +// nothing +#else +static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n) +{ + if (s->io.read) { + int blen = (int) (s->img_buffer_end - s->img_buffer); + if (blen < n) { + int res, count; + + memcpy(buffer, s->img_buffer, blen); + + count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen); + res = (count == (n-blen)); + s->img_buffer = s->img_buffer_end; + return res; + } + } + + if (s->img_buffer+n <= s->img_buffer_end) { + memcpy(buffer, s->img_buffer, n); + s->img_buffer += n; + return 1; + } else + return 0; +} +#endif + +#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC) +// nothing +#else +static int stbi__get16be(stbi__context *s) +{ + int z = stbi__get8(s); + return (z << 8) + stbi__get8(s); +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC) +// nothing +#else +static stbi__uint32 stbi__get32be(stbi__context *s) +{ + stbi__uint32 z = stbi__get16be(s); + return (z << 16) + stbi__get16be(s); +} +#endif + +#if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) +// nothing +#else +static int stbi__get16le(stbi__context *s) +{ + int z = stbi__get8(s); + return z + (stbi__get8(s) << 8); +} +#endif + +#ifndef STBI_NO_BMP +static stbi__uint32 stbi__get32le(stbi__context *s) +{ + stbi__uint32 z = stbi__get16le(s); + z += (stbi__uint32)stbi__get16le(s) << 16; + return z; +} +#endif + +#define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings + +#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) +// nothing +#else +////////////////////////////////////////////////////////////////////////////// +// +// generic converter from built-in img_n to req_comp +// individual types do this automatically as much as possible (e.g. jpeg +// does all cases internally since it needs to colorspace convert anyway, +// and it never has alpha, so very few cases ). png can automatically +// interleave an alpha=255 channel, but falls back to this for other cases +// +// assume data buffer is malloced, so malloc a new one and free that one +// only failure mode is malloc failing + +static stbi_uc stbi__compute_y(int r, int g, int b) +{ + return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8); +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM) +// nothing +#else +static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y) +{ + int i,j; + unsigned char *good; + + if (req_comp == img_n) return data; + STBI_ASSERT(req_comp >= 1 && req_comp <= 4); + + good = (unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0); + if (good == NULL) { + STBI_FREE(data); + return stbi__errpuc("outofmem", "Out of memory"); + } + + for (j=0; j < (int) y; ++j) { + unsigned char *src = data + j * x * img_n ; + unsigned char *dest = good + j * x * req_comp; + + #define STBI__COMBO(a,b) ((a)*8+(b)) + #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b) + // convert source image with img_n components to one with req_comp components; + // avoid switch per pixel, so use switch per scanline and massive macros + switch (STBI__COMBO(img_n, req_comp)) { + STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=255; } break; + STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; + STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=255; } break; + STBI__CASE(2,1) { dest[0]=src[0]; } break; + STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; + STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break; + STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=255; } break; + STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break; + STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = 255; } break; + STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break; + STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = src[3]; } break; + STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break; + default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return stbi__errpuc("unsupported", "Unsupported format conversion"); + } + #undef STBI__CASE + } + + STBI_FREE(data); + return good; +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) +// nothing +#else +static stbi__uint16 stbi__compute_y_16(int r, int g, int b) +{ + return (stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8); +} +#endif + +#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) +// nothing +#else +static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y) +{ + int i,j; + stbi__uint16 *good; + + if (req_comp == img_n) return data; + STBI_ASSERT(req_comp >= 1 && req_comp <= 4); + + good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2); + if (good == NULL) { + STBI_FREE(data); + return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory"); + } + + for (j=0; j < (int) y; ++j) { + stbi__uint16 *src = data + j * x * img_n ; + stbi__uint16 *dest = good + j * x * req_comp; + + #define STBI__COMBO(a,b) ((a)*8+(b)) + #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b) + // convert source image with img_n components to one with req_comp components; + // avoid switch per pixel, so use switch per scanline and massive macros + switch (STBI__COMBO(img_n, req_comp)) { + STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=0xffff; } break; + STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; + STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=0xffff; } break; + STBI__CASE(2,1) { dest[0]=src[0]; } break; + STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break; + STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break; + STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=0xffff; } break; + STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break; + STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = 0xffff; } break; + STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break; + STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = src[3]; } break; + STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break; + default: STBI_ASSERT(0); STBI_FREE(data); STBI_FREE(good); return (stbi__uint16*) stbi__errpuc("unsupported", "Unsupported format conversion"); + } + #undef STBI__CASE + } + + STBI_FREE(data); + return good; +} +#endif + +#ifndef STBI_NO_LINEAR +static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp) +{ + int i,k,n; + float *output; + if (!data) return NULL; + output = (float *) stbi__malloc_mad4(x, y, comp, sizeof(float), 0); + if (output == NULL) { STBI_FREE(data); return stbi__errpf("outofmem", "Out of memory"); } + // compute number of non-alpha components + if (comp & 1) n = comp; else n = comp-1; + for (i=0; i < x*y; ++i) { + for (k=0; k < n; ++k) { + output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale); + } + } + if (n < comp) { + for (i=0; i < x*y; ++i) { + output[i*comp + n] = data[i*comp + n]/255.0f; + } + } + STBI_FREE(data); + return output; +} +#endif + +#ifndef STBI_NO_HDR +#define stbi__float2int(x) ((int) (x)) +static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp) +{ + int i,k,n; + stbi_uc *output; + if (!data) return NULL; + output = (stbi_uc *) stbi__malloc_mad3(x, y, comp, 0); + if (output == NULL) { STBI_FREE(data); return stbi__errpuc("outofmem", "Out of memory"); } + // compute number of non-alpha components + if (comp & 1) n = comp; else n = comp-1; + for (i=0; i < x*y; ++i) { + for (k=0; k < n; ++k) { + float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f; + if (z < 0) z = 0; + if (z > 255) z = 255; + output[i*comp + k] = (stbi_uc) stbi__float2int(z); + } + if (k < comp) { + float z = data[i*comp+k] * 255 + 0.5f; + if (z < 0) z = 0; + if (z > 255) z = 255; + output[i*comp + k] = (stbi_uc) stbi__float2int(z); + } + } + STBI_FREE(data); + return output; +} +#endif + +////////////////////////////////////////////////////////////////////////////// +// +// "baseline" JPEG/JFIF decoder +// +// simple implementation +// - doesn't support delayed output of y-dimension +// - simple interface (only one output format: 8-bit interleaved RGB) +// - doesn't try to recover corrupt jpegs +// - doesn't allow partial loading, loading multiple at once +// - still fast on x86 (copying globals into locals doesn't help x86) +// - allocates lots of intermediate memory (full size of all components) +// - non-interleaved case requires this anyway +// - allows good upsampling (see next) +// high-quality +// - upsampled channels are bilinearly interpolated, even across blocks +// - quality integer IDCT derived from IJG's 'slow' +// performance +// - fast huffman; reasonable integer IDCT +// - some SIMD kernels for common paths on targets with SSE2/NEON +// - uses a lot of intermediate memory, could cache poorly + +#ifndef STBI_NO_JPEG + +// huffman decoding acceleration +#define FAST_BITS 9 // larger handles more cases; smaller stomps less cache + +typedef struct +{ + stbi_uc fast[1 << FAST_BITS]; + // weirdly, repacking this into AoS is a 10% speed loss, instead of a win + stbi__uint16 code[256]; + stbi_uc values[256]; + stbi_uc size[257]; + unsigned int maxcode[18]; + int delta[17]; // old 'firstsymbol' - old 'firstcode' +} stbi__huffman; + +typedef struct +{ + stbi__context *s; + stbi__huffman huff_dc[4]; + stbi__huffman huff_ac[4]; + stbi__uint16 dequant[4][64]; + stbi__int16 fast_ac[4][1 << FAST_BITS]; + +// sizes for components, interleaved MCUs + int img_h_max, img_v_max; + int img_mcu_x, img_mcu_y; + int img_mcu_w, img_mcu_h; + +// definition of jpeg image component + struct + { + int id; + int h,v; + int tq; + int hd,ha; + int dc_pred; + + int x,y,w2,h2; + stbi_uc *data; + void *raw_data, *raw_coeff; + stbi_uc *linebuf; + short *coeff; // progressive only + int coeff_w, coeff_h; // number of 8x8 coefficient blocks + } img_comp[4]; + + stbi__uint32 code_buffer; // jpeg entropy-coded buffer + int code_bits; // number of valid bits + unsigned char marker; // marker seen while filling entropy buffer + int nomore; // flag if we saw a marker so must stop + + int progressive; + int spec_start; + int spec_end; + int succ_high; + int succ_low; + int eob_run; + int jfif; + int app14_color_transform; // Adobe APP14 tag + int rgb; + + int scan_n, order[4]; + int restart_interval, todo; + +// kernels + void (*idct_block_kernel)(stbi_uc *out, int out_stride, short data[64]); + void (*YCbCr_to_RGB_kernel)(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step); + stbi_uc *(*resample_row_hv_2_kernel)(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs); +} stbi__jpeg; + +static int stbi__build_huffman(stbi__huffman *h, int *count) +{ + int i,j,k=0; + unsigned int code; + // build size list for each symbol (from JPEG spec) + for (i=0; i < 16; ++i) { + for (j=0; j < count[i]; ++j) { + h->size[k++] = (stbi_uc) (i+1); + if(k >= 257) return stbi__err("bad size list","Corrupt JPEG"); + } + } + h->size[k] = 0; + + // compute actual symbols (from jpeg spec) + code = 0; + k = 0; + for(j=1; j <= 16; ++j) { + // compute delta to add to code to compute symbol id + h->delta[j] = k - code; + if (h->size[k] == j) { + while (h->size[k] == j) + h->code[k++] = (stbi__uint16) (code++); + if (code-1 >= (1u << j)) return stbi__err("bad code lengths","Corrupt JPEG"); + } + // compute largest code + 1 for this size, preshifted as needed later + h->maxcode[j] = code << (16-j); + code <<= 1; + } + h->maxcode[j] = 0xffffffff; + + // build non-spec acceleration table; 255 is flag for not-accelerated + memset(h->fast, 255, 1 << FAST_BITS); + for (i=0; i < k; ++i) { + int s = h->size[i]; + if (s <= FAST_BITS) { + int c = h->code[i] << (FAST_BITS-s); + int m = 1 << (FAST_BITS-s); + for (j=0; j < m; ++j) { + h->fast[c+j] = (stbi_uc) i; + } + } + } + return 1; +} + +// build a table that decodes both magnitude and value of small ACs in +// one go. +static void stbi__build_fast_ac(stbi__int16 *fast_ac, stbi__huffman *h) +{ + int i; + for (i=0; i < (1 << FAST_BITS); ++i) { + stbi_uc fast = h->fast[i]; + fast_ac[i] = 0; + if (fast < 255) { + int rs = h->values[fast]; + int run = (rs >> 4) & 15; + int magbits = rs & 15; + int len = h->size[fast]; + + if (magbits && len + magbits <= FAST_BITS) { + // magnitude code followed by receive_extend code + int k = ((i << len) & ((1 << FAST_BITS) - 1)) >> (FAST_BITS - magbits); + int m = 1 << (magbits - 1); + if (k < m) k += (~0U << magbits) + 1; + // if the result is small enough, we can fit it in fast_ac table + if (k >= -128 && k <= 127) + fast_ac[i] = (stbi__int16) ((k * 256) + (run * 16) + (len + magbits)); + } + } + } +} + +static void stbi__grow_buffer_unsafe(stbi__jpeg *j) +{ + do { + unsigned int b = j->nomore ? 0 : stbi__get8(j->s); + if (b == 0xff) { + int c = stbi__get8(j->s); + while (c == 0xff) c = stbi__get8(j->s); // consume fill bytes + if (c != 0) { + j->marker = (unsigned char) c; + j->nomore = 1; + return; + } + } + j->code_buffer |= b << (24 - j->code_bits); + j->code_bits += 8; + } while (j->code_bits <= 24); +} + +// (1 << n) - 1 +static const stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535}; + +// decode a jpeg huffman value from the bitstream +stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h) +{ + unsigned int temp; + int c,k; + + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + + // look at the top FAST_BITS and determine what symbol ID it is, + // if the code is <= FAST_BITS + c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); + k = h->fast[c]; + if (k < 255) { + int s = h->size[k]; + if (s > j->code_bits) + return -1; + j->code_buffer <<= s; + j->code_bits -= s; + return h->values[k]; + } + + // naive test is to shift the code_buffer down so k bits are + // valid, then test against maxcode. To speed this up, we've + // preshifted maxcode left so that it has (16-k) 0s at the + // end; in other words, regardless of the number of bits, it + // wants to be compared against something shifted to have 16; + // that way we don't need to shift inside the loop. + temp = j->code_buffer >> 16; + for (k=FAST_BITS+1 ; ; ++k) + if (temp < h->maxcode[k]) + break; + if (k == 17) { + // error! code not found + j->code_bits -= 16; + return -1; + } + + if (k > j->code_bits) + return -1; + + // convert the huffman code to the symbol id + c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k]; + if(c < 0 || c >= 256) // symbol id out of bounds! + return -1; + STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]); + + // convert the id to a symbol + j->code_bits -= k; + j->code_buffer <<= k; + return h->values[c]; +} + +// bias[n] = (-1<code_bits < n) stbi__grow_buffer_unsafe(j); + if (j->code_bits < n) return 0; // ran out of bits from stream, return 0s intead of continuing + + sgn = j->code_buffer >> 31; // sign bit always in MSB; 0 if MSB clear (positive), 1 if MSB set (negative) + k = stbi_lrot(j->code_buffer, n); + j->code_buffer = k & ~stbi__bmask[n]; + k &= stbi__bmask[n]; + j->code_bits -= n; + return k + (stbi__jbias[n] & (sgn - 1)); +} + +// get some unsigned bits +stbi_inline static int stbi__jpeg_get_bits(stbi__jpeg *j, int n) +{ + unsigned int k; + if (j->code_bits < n) stbi__grow_buffer_unsafe(j); + if (j->code_bits < n) return 0; // ran out of bits from stream, return 0s intead of continuing + k = stbi_lrot(j->code_buffer, n); + j->code_buffer = k & ~stbi__bmask[n]; + k &= stbi__bmask[n]; + j->code_bits -= n; + return k; +} + +stbi_inline static int stbi__jpeg_get_bit(stbi__jpeg *j) +{ + unsigned int k; + if (j->code_bits < 1) stbi__grow_buffer_unsafe(j); + if (j->code_bits < 1) return 0; // ran out of bits from stream, return 0s intead of continuing + k = j->code_buffer; + j->code_buffer <<= 1; + --j->code_bits; + return k & 0x80000000; +} + +// given a value that's at position X in the zigzag stream, +// where does it appear in the 8x8 matrix coded as row-major? +static const stbi_uc stbi__jpeg_dezigzag[64+15] = +{ + 0, 1, 8, 16, 9, 2, 3, 10, + 17, 24, 32, 25, 18, 11, 4, 5, + 12, 19, 26, 33, 40, 48, 41, 34, + 27, 20, 13, 6, 7, 14, 21, 28, + 35, 42, 49, 56, 57, 50, 43, 36, + 29, 22, 15, 23, 30, 37, 44, 51, + 58, 59, 52, 45, 38, 31, 39, 46, + 53, 60, 61, 54, 47, 55, 62, 63, + // let corrupt input sample past end + 63, 63, 63, 63, 63, 63, 63, 63, + 63, 63, 63, 63, 63, 63, 63 +}; + +// decode one 64-entry block-- +static int stbi__jpeg_decode_block(stbi__jpeg *j, short data[64], stbi__huffman *hdc, stbi__huffman *hac, stbi__int16 *fac, int b, stbi__uint16 *dequant) +{ + int diff,dc,k; + int t; + + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + t = stbi__jpeg_huff_decode(j, hdc); + if (t < 0 || t > 15) return stbi__err("bad huffman code","Corrupt JPEG"); + + // 0 all the ac values now so we can do it 32-bits at a time + memset(data,0,64*sizeof(data[0])); + + diff = t ? stbi__extend_receive(j, t) : 0; + if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta","Corrupt JPEG"); + dc = j->img_comp[b].dc_pred + diff; + j->img_comp[b].dc_pred = dc; + if (!stbi__mul2shorts_valid(dc, dequant[0])) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); + data[0] = (short) (dc * dequant[0]); + + // decode AC components, see JPEG spec + k = 1; + do { + unsigned int zig; + int c,r,s; + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); + r = fac[c]; + if (r) { // fast-AC path + k += (r >> 4) & 15; // run + s = r & 15; // combined length + if (s > j->code_bits) return stbi__err("bad huffman code", "Combined length longer than code bits available"); + j->code_buffer <<= s; + j->code_bits -= s; + // decode into unzigzag'd location + zig = stbi__jpeg_dezigzag[k++]; + data[zig] = (short) ((r >> 8) * dequant[zig]); + } else { + int rs = stbi__jpeg_huff_decode(j, hac); + if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG"); + s = rs & 15; + r = rs >> 4; + if (s == 0) { + if (rs != 0xf0) break; // end block + k += 16; + } else { + k += r; + // decode into unzigzag'd location + zig = stbi__jpeg_dezigzag[k++]; + data[zig] = (short) (stbi__extend_receive(j,s) * dequant[zig]); + } + } + } while (k < 64); + return 1; +} + +static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__huffman *hdc, int b) +{ + int diff,dc; + int t; + if (j->spec_end != 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); + + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + + if (j->succ_high == 0) { + // first scan for DC coefficient, must be first + memset(data,0,64*sizeof(data[0])); // 0 all the ac values now + t = stbi__jpeg_huff_decode(j, hdc); + if (t < 0 || t > 15) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); + diff = t ? stbi__extend_receive(j, t) : 0; + + if (!stbi__addints_valid(j->img_comp[b].dc_pred, diff)) return stbi__err("bad delta", "Corrupt JPEG"); + dc = j->img_comp[b].dc_pred + diff; + j->img_comp[b].dc_pred = dc; + if (!stbi__mul2shorts_valid(dc, 1 << j->succ_low)) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); + data[0] = (short) (dc * (1 << j->succ_low)); + } else { + // refinement scan for DC coefficient + if (stbi__jpeg_get_bit(j)) + data[0] += (short) (1 << j->succ_low); + } + return 1; +} + +// @OPTIMIZE: store non-zigzagged during the decode passes, +// and only de-zigzag when dequantizing +static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j, short data[64], stbi__huffman *hac, stbi__int16 *fac) +{ + int k; + if (j->spec_start == 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG"); + + if (j->succ_high == 0) { + int shift = j->succ_low; + + if (j->eob_run) { + --j->eob_run; + return 1; + } + + k = j->spec_start; + do { + unsigned int zig; + int c,r,s; + if (j->code_bits < 16) stbi__grow_buffer_unsafe(j); + c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1); + r = fac[c]; + if (r) { // fast-AC path + k += (r >> 4) & 15; // run + s = r & 15; // combined length + if (s > j->code_bits) return stbi__err("bad huffman code", "Combined length longer than code bits available"); + j->code_buffer <<= s; + j->code_bits -= s; + zig = stbi__jpeg_dezigzag[k++]; + data[zig] = (short) ((r >> 8) * (1 << shift)); + } else { + int rs = stbi__jpeg_huff_decode(j, hac); + if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG"); + s = rs & 15; + r = rs >> 4; + if (s == 0) { + if (r < 15) { + j->eob_run = (1 << r); + if (r) + j->eob_run += stbi__jpeg_get_bits(j, r); + --j->eob_run; + break; + } + k += 16; + } else { + k += r; + zig = stbi__jpeg_dezigzag[k++]; + data[zig] = (short) (stbi__extend_receive(j,s) * (1 << shift)); + } + } + } while (k <= j->spec_end); + } else { + // refinement scan for these AC coefficients + + short bit = (short) (1 << j->succ_low); + + if (j->eob_run) { + --j->eob_run; + for (k = j->spec_start; k <= j->spec_end; ++k) { + short *p = &data[stbi__jpeg_dezigzag[k]]; + if (*p != 0) + if (stbi__jpeg_get_bit(j)) + if ((*p & bit)==0) { + if (*p > 0) + *p += bit; + else + *p -= bit; + } + } + } else { + k = j->spec_start; + do { + int r,s; + int rs = stbi__jpeg_huff_decode(j, hac); // @OPTIMIZE see if we can use the fast path here, advance-by-r is so slow, eh + if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG"); + s = rs & 15; + r = rs >> 4; + if (s == 0) { + if (r < 15) { + j->eob_run = (1 << r) - 1; + if (r) + j->eob_run += stbi__jpeg_get_bits(j, r); + r = 64; // force end of block + } else { + // r=15 s=0 should write 16 0s, so we just do + // a run of 15 0s and then write s (which is 0), + // so we don't have to do anything special here + } + } else { + if (s != 1) return stbi__err("bad huffman code", "Corrupt JPEG"); + // sign bit + if (stbi__jpeg_get_bit(j)) + s = bit; + else + s = -bit; + } + + // advance by r + while (k <= j->spec_end) { + short *p = &data[stbi__jpeg_dezigzag[k++]]; + if (*p != 0) { + if (stbi__jpeg_get_bit(j)) + if ((*p & bit)==0) { + if (*p > 0) + *p += bit; + else + *p -= bit; + } + } else { + if (r == 0) { + *p = (short) s; + break; + } + --r; + } + } + } while (k <= j->spec_end); + } + } + return 1; +} + +// take a -128..127 value and stbi__clamp it and convert to 0..255 +stbi_inline static stbi_uc stbi__clamp(int x) +{ + // trick to use a single test to catch both cases + if ((unsigned int) x > 255) { + if (x < 0) return 0; + if (x > 255) return 255; + } + return (stbi_uc) x; +} + +#define stbi__f2f(x) ((int) (((x) * 4096 + 0.5))) +#define stbi__fsh(x) ((x) * 4096) + +// derived from jidctint -- DCT_ISLOW +#define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \ + int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \ + p2 = s2; \ + p3 = s6; \ + p1 = (p2+p3) * stbi__f2f(0.5411961f); \ + t2 = p1 + p3*stbi__f2f(-1.847759065f); \ + t3 = p1 + p2*stbi__f2f( 0.765366865f); \ + p2 = s0; \ + p3 = s4; \ + t0 = stbi__fsh(p2+p3); \ + t1 = stbi__fsh(p2-p3); \ + x0 = t0+t3; \ + x3 = t0-t3; \ + x1 = t1+t2; \ + x2 = t1-t2; \ + t0 = s7; \ + t1 = s5; \ + t2 = s3; \ + t3 = s1; \ + p3 = t0+t2; \ + p4 = t1+t3; \ + p1 = t0+t3; \ + p2 = t1+t2; \ + p5 = (p3+p4)*stbi__f2f( 1.175875602f); \ + t0 = t0*stbi__f2f( 0.298631336f); \ + t1 = t1*stbi__f2f( 2.053119869f); \ + t2 = t2*stbi__f2f( 3.072711026f); \ + t3 = t3*stbi__f2f( 1.501321110f); \ + p1 = p5 + p1*stbi__f2f(-0.899976223f); \ + p2 = p5 + p2*stbi__f2f(-2.562915447f); \ + p3 = p3*stbi__f2f(-1.961570560f); \ + p4 = p4*stbi__f2f(-0.390180644f); \ + t3 += p1+p4; \ + t2 += p2+p3; \ + t1 += p2+p4; \ + t0 += p1+p3; + +static void stbi__idct_block(stbi_uc *out, int out_stride, short data[64]) +{ + int i,val[64],*v=val; + stbi_uc *o; + short *d = data; + + // columns + for (i=0; i < 8; ++i,++d, ++v) { + // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing + if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0 + && d[40]==0 && d[48]==0 && d[56]==0) { + // no shortcut 0 seconds + // (1|2|3|4|5|6|7)==0 0 seconds + // all separate -0.047 seconds + // 1 && 2|3 && 4|5 && 6|7: -0.047 seconds + int dcterm = d[0]*4; + v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm; + } else { + STBI__IDCT_1D(d[ 0],d[ 8],d[16],d[24],d[32],d[40],d[48],d[56]) + // constants scaled things up by 1<<12; let's bring them back + // down, but keep 2 extra bits of precision + x0 += 512; x1 += 512; x2 += 512; x3 += 512; + v[ 0] = (x0+t3) >> 10; + v[56] = (x0-t3) >> 10; + v[ 8] = (x1+t2) >> 10; + v[48] = (x1-t2) >> 10; + v[16] = (x2+t1) >> 10; + v[40] = (x2-t1) >> 10; + v[24] = (x3+t0) >> 10; + v[32] = (x3-t0) >> 10; + } + } + + for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) { + // no fast case since the first 1D IDCT spread components out + STBI__IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7]) + // constants scaled things up by 1<<12, plus we had 1<<2 from first + // loop, plus horizontal and vertical each scale by sqrt(8) so together + // we've got an extra 1<<3, so 1<<17 total we need to remove. + // so we want to round that, which means adding 0.5 * 1<<17, + // aka 65536. Also, we'll end up with -128 to 127 that we want + // to encode as 0..255 by adding 128, so we'll add that before the shift + x0 += 65536 + (128<<17); + x1 += 65536 + (128<<17); + x2 += 65536 + (128<<17); + x3 += 65536 + (128<<17); + // tried computing the shifts into temps, or'ing the temps to see + // if any were out of range, but that was slower + o[0] = stbi__clamp((x0+t3) >> 17); + o[7] = stbi__clamp((x0-t3) >> 17); + o[1] = stbi__clamp((x1+t2) >> 17); + o[6] = stbi__clamp((x1-t2) >> 17); + o[2] = stbi__clamp((x2+t1) >> 17); + o[5] = stbi__clamp((x2-t1) >> 17); + o[3] = stbi__clamp((x3+t0) >> 17); + o[4] = stbi__clamp((x3-t0) >> 17); + } +} + +#ifdef STBI_SSE2 +// sse2 integer IDCT. not the fastest possible implementation but it +// produces bit-identical results to the generic C version so it's +// fully "transparent". +static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64]) +{ + // This is constructed to match our regular (generic) integer IDCT exactly. + __m128i row0, row1, row2, row3, row4, row5, row6, row7; + __m128i tmp; + + // dot product constant: even elems=x, odd elems=y + #define dct_const(x,y) _mm_setr_epi16((x),(y),(x),(y),(x),(y),(x),(y)) + + // out(0) = c0[even]*x + c0[odd]*y (c0, x, y 16-bit, out 32-bit) + // out(1) = c1[even]*x + c1[odd]*y + #define dct_rot(out0,out1, x,y,c0,c1) \ + __m128i c0##lo = _mm_unpacklo_epi16((x),(y)); \ + __m128i c0##hi = _mm_unpackhi_epi16((x),(y)); \ + __m128i out0##_l = _mm_madd_epi16(c0##lo, c0); \ + __m128i out0##_h = _mm_madd_epi16(c0##hi, c0); \ + __m128i out1##_l = _mm_madd_epi16(c0##lo, c1); \ + __m128i out1##_h = _mm_madd_epi16(c0##hi, c1) + + // out = in << 12 (in 16-bit, out 32-bit) + #define dct_widen(out, in) \ + __m128i out##_l = _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), (in)), 4); \ + __m128i out##_h = _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), (in)), 4) + + // wide add + #define dct_wadd(out, a, b) \ + __m128i out##_l = _mm_add_epi32(a##_l, b##_l); \ + __m128i out##_h = _mm_add_epi32(a##_h, b##_h) + + // wide sub + #define dct_wsub(out, a, b) \ + __m128i out##_l = _mm_sub_epi32(a##_l, b##_l); \ + __m128i out##_h = _mm_sub_epi32(a##_h, b##_h) + + // butterfly a/b, add bias, then shift by "s" and pack + #define dct_bfly32o(out0, out1, a,b,bias,s) \ + { \ + __m128i abiased_l = _mm_add_epi32(a##_l, bias); \ + __m128i abiased_h = _mm_add_epi32(a##_h, bias); \ + dct_wadd(sum, abiased, b); \ + dct_wsub(dif, abiased, b); \ + out0 = _mm_packs_epi32(_mm_srai_epi32(sum_l, s), _mm_srai_epi32(sum_h, s)); \ + out1 = _mm_packs_epi32(_mm_srai_epi32(dif_l, s), _mm_srai_epi32(dif_h, s)); \ + } + + // 8-bit interleave step (for transposes) + #define dct_interleave8(a, b) \ + tmp = a; \ + a = _mm_unpacklo_epi8(a, b); \ + b = _mm_unpackhi_epi8(tmp, b) + + // 16-bit interleave step (for transposes) + #define dct_interleave16(a, b) \ + tmp = a; \ + a = _mm_unpacklo_epi16(a, b); \ + b = _mm_unpackhi_epi16(tmp, b) + + #define dct_pass(bias,shift) \ + { \ + /* even part */ \ + dct_rot(t2e,t3e, row2,row6, rot0_0,rot0_1); \ + __m128i sum04 = _mm_add_epi16(row0, row4); \ + __m128i dif04 = _mm_sub_epi16(row0, row4); \ + dct_widen(t0e, sum04); \ + dct_widen(t1e, dif04); \ + dct_wadd(x0, t0e, t3e); \ + dct_wsub(x3, t0e, t3e); \ + dct_wadd(x1, t1e, t2e); \ + dct_wsub(x2, t1e, t2e); \ + /* odd part */ \ + dct_rot(y0o,y2o, row7,row3, rot2_0,rot2_1); \ + dct_rot(y1o,y3o, row5,row1, rot3_0,rot3_1); \ + __m128i sum17 = _mm_add_epi16(row1, row7); \ + __m128i sum35 = _mm_add_epi16(row3, row5); \ + dct_rot(y4o,y5o, sum17,sum35, rot1_0,rot1_1); \ + dct_wadd(x4, y0o, y4o); \ + dct_wadd(x5, y1o, y5o); \ + dct_wadd(x6, y2o, y5o); \ + dct_wadd(x7, y3o, y4o); \ + dct_bfly32o(row0,row7, x0,x7,bias,shift); \ + dct_bfly32o(row1,row6, x1,x6,bias,shift); \ + dct_bfly32o(row2,row5, x2,x5,bias,shift); \ + dct_bfly32o(row3,row4, x3,x4,bias,shift); \ + } + + __m128i rot0_0 = dct_const(stbi__f2f(0.5411961f), stbi__f2f(0.5411961f) + stbi__f2f(-1.847759065f)); + __m128i rot0_1 = dct_const(stbi__f2f(0.5411961f) + stbi__f2f( 0.765366865f), stbi__f2f(0.5411961f)); + __m128i rot1_0 = dct_const(stbi__f2f(1.175875602f) + stbi__f2f(-0.899976223f), stbi__f2f(1.175875602f)); + __m128i rot1_1 = dct_const(stbi__f2f(1.175875602f), stbi__f2f(1.175875602f) + stbi__f2f(-2.562915447f)); + __m128i rot2_0 = dct_const(stbi__f2f(-1.961570560f) + stbi__f2f( 0.298631336f), stbi__f2f(-1.961570560f)); + __m128i rot2_1 = dct_const(stbi__f2f(-1.961570560f), stbi__f2f(-1.961570560f) + stbi__f2f( 3.072711026f)); + __m128i rot3_0 = dct_const(stbi__f2f(-0.390180644f) + stbi__f2f( 2.053119869f), stbi__f2f(-0.390180644f)); + __m128i rot3_1 = dct_const(stbi__f2f(-0.390180644f), stbi__f2f(-0.390180644f) + stbi__f2f( 1.501321110f)); + + // rounding biases in column/row passes, see stbi__idct_block for explanation. + __m128i bias_0 = _mm_set1_epi32(512); + __m128i bias_1 = _mm_set1_epi32(65536 + (128<<17)); + + // load + row0 = _mm_load_si128((const __m128i *) (data + 0*8)); + row1 = _mm_load_si128((const __m128i *) (data + 1*8)); + row2 = _mm_load_si128((const __m128i *) (data + 2*8)); + row3 = _mm_load_si128((const __m128i *) (data + 3*8)); + row4 = _mm_load_si128((const __m128i *) (data + 4*8)); + row5 = _mm_load_si128((const __m128i *) (data + 5*8)); + row6 = _mm_load_si128((const __m128i *) (data + 6*8)); + row7 = _mm_load_si128((const __m128i *) (data + 7*8)); + + // column pass + dct_pass(bias_0, 10); + + { + // 16bit 8x8 transpose pass 1 + dct_interleave16(row0, row4); + dct_interleave16(row1, row5); + dct_interleave16(row2, row6); + dct_interleave16(row3, row7); + + // transpose pass 2 + dct_interleave16(row0, row2); + dct_interleave16(row1, row3); + dct_interleave16(row4, row6); + dct_interleave16(row5, row7); + + // transpose pass 3 + dct_interleave16(row0, row1); + dct_interleave16(row2, row3); + dct_interleave16(row4, row5); + dct_interleave16(row6, row7); + } + + // row pass + dct_pass(bias_1, 17); + + { + // pack + __m128i p0 = _mm_packus_epi16(row0, row1); // a0a1a2a3...a7b0b1b2b3...b7 + __m128i p1 = _mm_packus_epi16(row2, row3); + __m128i p2 = _mm_packus_epi16(row4, row5); + __m128i p3 = _mm_packus_epi16(row6, row7); + + // 8bit 8x8 transpose pass 1 + dct_interleave8(p0, p2); // a0e0a1e1... + dct_interleave8(p1, p3); // c0g0c1g1... + + // transpose pass 2 + dct_interleave8(p0, p1); // a0c0e0g0... + dct_interleave8(p2, p3); // b0d0f0h0... + + // transpose pass 3 + dct_interleave8(p0, p2); // a0b0c0d0... + dct_interleave8(p1, p3); // a4b4c4d4... + + // store + _mm_storel_epi64((__m128i *) out, p0); out += out_stride; + _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p0, 0x4e)); out += out_stride; + _mm_storel_epi64((__m128i *) out, p2); out += out_stride; + _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p2, 0x4e)); out += out_stride; + _mm_storel_epi64((__m128i *) out, p1); out += out_stride; + _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p1, 0x4e)); out += out_stride; + _mm_storel_epi64((__m128i *) out, p3); out += out_stride; + _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p3, 0x4e)); + } + +#undef dct_const +#undef dct_rot +#undef dct_widen +#undef dct_wadd +#undef dct_wsub +#undef dct_bfly32o +#undef dct_interleave8 +#undef dct_interleave16 +#undef dct_pass +} + +#endif // STBI_SSE2 + +#ifdef STBI_NEON + +// NEON integer IDCT. should produce bit-identical +// results to the generic C version. +static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64]) +{ + int16x8_t row0, row1, row2, row3, row4, row5, row6, row7; + + int16x4_t rot0_0 = vdup_n_s16(stbi__f2f(0.5411961f)); + int16x4_t rot0_1 = vdup_n_s16(stbi__f2f(-1.847759065f)); + int16x4_t rot0_2 = vdup_n_s16(stbi__f2f( 0.765366865f)); + int16x4_t rot1_0 = vdup_n_s16(stbi__f2f( 1.175875602f)); + int16x4_t rot1_1 = vdup_n_s16(stbi__f2f(-0.899976223f)); + int16x4_t rot1_2 = vdup_n_s16(stbi__f2f(-2.562915447f)); + int16x4_t rot2_0 = vdup_n_s16(stbi__f2f(-1.961570560f)); + int16x4_t rot2_1 = vdup_n_s16(stbi__f2f(-0.390180644f)); + int16x4_t rot3_0 = vdup_n_s16(stbi__f2f( 0.298631336f)); + int16x4_t rot3_1 = vdup_n_s16(stbi__f2f( 2.053119869f)); + int16x4_t rot3_2 = vdup_n_s16(stbi__f2f( 3.072711026f)); + int16x4_t rot3_3 = vdup_n_s16(stbi__f2f( 1.501321110f)); + +#define dct_long_mul(out, inq, coeff) \ + int32x4_t out##_l = vmull_s16(vget_low_s16(inq), coeff); \ + int32x4_t out##_h = vmull_s16(vget_high_s16(inq), coeff) + +#define dct_long_mac(out, acc, inq, coeff) \ + int32x4_t out##_l = vmlal_s16(acc##_l, vget_low_s16(inq), coeff); \ + int32x4_t out##_h = vmlal_s16(acc##_h, vget_high_s16(inq), coeff) + +#define dct_widen(out, inq) \ + int32x4_t out##_l = vshll_n_s16(vget_low_s16(inq), 12); \ + int32x4_t out##_h = vshll_n_s16(vget_high_s16(inq), 12) + +// wide add +#define dct_wadd(out, a, b) \ + int32x4_t out##_l = vaddq_s32(a##_l, b##_l); \ + int32x4_t out##_h = vaddq_s32(a##_h, b##_h) + +// wide sub +#define dct_wsub(out, a, b) \ + int32x4_t out##_l = vsubq_s32(a##_l, b##_l); \ + int32x4_t out##_h = vsubq_s32(a##_h, b##_h) + +// butterfly a/b, then shift using "shiftop" by "s" and pack +#define dct_bfly32o(out0,out1, a,b,shiftop,s) \ + { \ + dct_wadd(sum, a, b); \ + dct_wsub(dif, a, b); \ + out0 = vcombine_s16(shiftop(sum_l, s), shiftop(sum_h, s)); \ + out1 = vcombine_s16(shiftop(dif_l, s), shiftop(dif_h, s)); \ + } + +#define dct_pass(shiftop, shift) \ + { \ + /* even part */ \ + int16x8_t sum26 = vaddq_s16(row2, row6); \ + dct_long_mul(p1e, sum26, rot0_0); \ + dct_long_mac(t2e, p1e, row6, rot0_1); \ + dct_long_mac(t3e, p1e, row2, rot0_2); \ + int16x8_t sum04 = vaddq_s16(row0, row4); \ + int16x8_t dif04 = vsubq_s16(row0, row4); \ + dct_widen(t0e, sum04); \ + dct_widen(t1e, dif04); \ + dct_wadd(x0, t0e, t3e); \ + dct_wsub(x3, t0e, t3e); \ + dct_wadd(x1, t1e, t2e); \ + dct_wsub(x2, t1e, t2e); \ + /* odd part */ \ + int16x8_t sum15 = vaddq_s16(row1, row5); \ + int16x8_t sum17 = vaddq_s16(row1, row7); \ + int16x8_t sum35 = vaddq_s16(row3, row5); \ + int16x8_t sum37 = vaddq_s16(row3, row7); \ + int16x8_t sumodd = vaddq_s16(sum17, sum35); \ + dct_long_mul(p5o, sumodd, rot1_0); \ + dct_long_mac(p1o, p5o, sum17, rot1_1); \ + dct_long_mac(p2o, p5o, sum35, rot1_2); \ + dct_long_mul(p3o, sum37, rot2_0); \ + dct_long_mul(p4o, sum15, rot2_1); \ + dct_wadd(sump13o, p1o, p3o); \ + dct_wadd(sump24o, p2o, p4o); \ + dct_wadd(sump23o, p2o, p3o); \ + dct_wadd(sump14o, p1o, p4o); \ + dct_long_mac(x4, sump13o, row7, rot3_0); \ + dct_long_mac(x5, sump24o, row5, rot3_1); \ + dct_long_mac(x6, sump23o, row3, rot3_2); \ + dct_long_mac(x7, sump14o, row1, rot3_3); \ + dct_bfly32o(row0,row7, x0,x7,shiftop,shift); \ + dct_bfly32o(row1,row6, x1,x6,shiftop,shift); \ + dct_bfly32o(row2,row5, x2,x5,shiftop,shift); \ + dct_bfly32o(row3,row4, x3,x4,shiftop,shift); \ + } + + // load + row0 = vld1q_s16(data + 0*8); + row1 = vld1q_s16(data + 1*8); + row2 = vld1q_s16(data + 2*8); + row3 = vld1q_s16(data + 3*8); + row4 = vld1q_s16(data + 4*8); + row5 = vld1q_s16(data + 5*8); + row6 = vld1q_s16(data + 6*8); + row7 = vld1q_s16(data + 7*8); + + // add DC bias + row0 = vaddq_s16(row0, vsetq_lane_s16(1024, vdupq_n_s16(0), 0)); + + // column pass + dct_pass(vrshrn_n_s32, 10); + + // 16bit 8x8 transpose + { +// these three map to a single VTRN.16, VTRN.32, and VSWP, respectively. +// whether compilers actually get this is another story, sadly. +#define dct_trn16(x, y) { int16x8x2_t t = vtrnq_s16(x, y); x = t.val[0]; y = t.val[1]; } +#define dct_trn32(x, y) { int32x4x2_t t = vtrnq_s32(vreinterpretq_s32_s16(x), vreinterpretq_s32_s16(y)); x = vreinterpretq_s16_s32(t.val[0]); y = vreinterpretq_s16_s32(t.val[1]); } +#define dct_trn64(x, y) { int16x8_t x0 = x; int16x8_t y0 = y; x = vcombine_s16(vget_low_s16(x0), vget_low_s16(y0)); y = vcombine_s16(vget_high_s16(x0), vget_high_s16(y0)); } + + // pass 1 + dct_trn16(row0, row1); // a0b0a2b2a4b4a6b6 + dct_trn16(row2, row3); + dct_trn16(row4, row5); + dct_trn16(row6, row7); + + // pass 2 + dct_trn32(row0, row2); // a0b0c0d0a4b4c4d4 + dct_trn32(row1, row3); + dct_trn32(row4, row6); + dct_trn32(row5, row7); + + // pass 3 + dct_trn64(row0, row4); // a0b0c0d0e0f0g0h0 + dct_trn64(row1, row5); + dct_trn64(row2, row6); + dct_trn64(row3, row7); + +#undef dct_trn16 +#undef dct_trn32 +#undef dct_trn64 + } + + // row pass + // vrshrn_n_s32 only supports shifts up to 16, we need + // 17. so do a non-rounding shift of 16 first then follow + // up with a rounding shift by 1. + dct_pass(vshrn_n_s32, 16); + + { + // pack and round + uint8x8_t p0 = vqrshrun_n_s16(row0, 1); + uint8x8_t p1 = vqrshrun_n_s16(row1, 1); + uint8x8_t p2 = vqrshrun_n_s16(row2, 1); + uint8x8_t p3 = vqrshrun_n_s16(row3, 1); + uint8x8_t p4 = vqrshrun_n_s16(row4, 1); + uint8x8_t p5 = vqrshrun_n_s16(row5, 1); + uint8x8_t p6 = vqrshrun_n_s16(row6, 1); + uint8x8_t p7 = vqrshrun_n_s16(row7, 1); + + // again, these can translate into one instruction, but often don't. +#define dct_trn8_8(x, y) { uint8x8x2_t t = vtrn_u8(x, y); x = t.val[0]; y = t.val[1]; } +#define dct_trn8_16(x, y) { uint16x4x2_t t = vtrn_u16(vreinterpret_u16_u8(x), vreinterpret_u16_u8(y)); x = vreinterpret_u8_u16(t.val[0]); y = vreinterpret_u8_u16(t.val[1]); } +#define dct_trn8_32(x, y) { uint32x2x2_t t = vtrn_u32(vreinterpret_u32_u8(x), vreinterpret_u32_u8(y)); x = vreinterpret_u8_u32(t.val[0]); y = vreinterpret_u8_u32(t.val[1]); } + + // sadly can't use interleaved stores here since we only write + // 8 bytes to each scan line! + + // 8x8 8-bit transpose pass 1 + dct_trn8_8(p0, p1); + dct_trn8_8(p2, p3); + dct_trn8_8(p4, p5); + dct_trn8_8(p6, p7); + + // pass 2 + dct_trn8_16(p0, p2); + dct_trn8_16(p1, p3); + dct_trn8_16(p4, p6); + dct_trn8_16(p5, p7); + + // pass 3 + dct_trn8_32(p0, p4); + dct_trn8_32(p1, p5); + dct_trn8_32(p2, p6); + dct_trn8_32(p3, p7); + + // store + vst1_u8(out, p0); out += out_stride; + vst1_u8(out, p1); out += out_stride; + vst1_u8(out, p2); out += out_stride; + vst1_u8(out, p3); out += out_stride; + vst1_u8(out, p4); out += out_stride; + vst1_u8(out, p5); out += out_stride; + vst1_u8(out, p6); out += out_stride; + vst1_u8(out, p7); + +#undef dct_trn8_8 +#undef dct_trn8_16 +#undef dct_trn8_32 + } + +#undef dct_long_mul +#undef dct_long_mac +#undef dct_widen +#undef dct_wadd +#undef dct_wsub +#undef dct_bfly32o +#undef dct_pass +} + +#endif // STBI_NEON + +#define STBI__MARKER_none 0xff +// if there's a pending marker from the entropy stream, return that +// otherwise, fetch from the stream and get a marker. if there's no +// marker, return 0xff, which is never a valid marker value +static stbi_uc stbi__get_marker(stbi__jpeg *j) +{ + stbi_uc x; + if (j->marker != STBI__MARKER_none) { x = j->marker; j->marker = STBI__MARKER_none; return x; } + x = stbi__get8(j->s); + if (x != 0xff) return STBI__MARKER_none; + while (x == 0xff) + x = stbi__get8(j->s); // consume repeated 0xff fill bytes + return x; +} + +// in each scan, we'll have scan_n components, and the order +// of the components is specified by order[] +#define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7) + +// after a restart interval, stbi__jpeg_reset the entropy decoder and +// the dc prediction +static void stbi__jpeg_reset(stbi__jpeg *j) +{ + j->code_bits = 0; + j->code_buffer = 0; + j->nomore = 0; + j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = j->img_comp[3].dc_pred = 0; + j->marker = STBI__MARKER_none; + j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff; + j->eob_run = 0; + // no more than 1<<31 MCUs if no restart_interal? that's plenty safe, + // since we don't even allow 1<<30 pixels +} + +static int stbi__parse_entropy_coded_data(stbi__jpeg *z) +{ + stbi__jpeg_reset(z); + if (!z->progressive) { + if (z->scan_n == 1) { + int i,j; + STBI_SIMD_ALIGN(short, data[64]); + int n = z->order[0]; + // non-interleaved data, we just need to process one block at a time, + // in trivial scanline order + // number of blocks to do just depends on how many actual "pixels" this + // component has, independent of interleaved MCU blocking and such + int w = (z->img_comp[n].x+7) >> 3; + int h = (z->img_comp[n].y+7) >> 3; + for (j=0; j < h; ++j) { + for (i=0; i < w; ++i) { + int ha = z->img_comp[n].ha; + if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0; + z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data); + // every data block is an MCU, so countdown the restart interval + if (--z->todo <= 0) { + if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); + // if it's NOT a restart, then just bail, so we get corrupt data + // rather than no data + if (!STBI__RESTART(z->marker)) return 1; + stbi__jpeg_reset(z); + } + } + } + return 1; + } else { // interleaved + int i,j,k,x,y; + STBI_SIMD_ALIGN(short, data[64]); + for (j=0; j < z->img_mcu_y; ++j) { + for (i=0; i < z->img_mcu_x; ++i) { + // scan an interleaved mcu... process scan_n components in order + for (k=0; k < z->scan_n; ++k) { + int n = z->order[k]; + // scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (y=0; y < z->img_comp[n].v; ++y) { + for (x=0; x < z->img_comp[n].h; ++x) { + int x2 = (i*z->img_comp[n].h + x)*8; + int y2 = (j*z->img_comp[n].v + y)*8; + int ha = z->img_comp[n].ha; + if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0; + z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data); + } + } + } + // after all interleaved components, that's an interleaved MCU, + // so now count down the restart interval + if (--z->todo <= 0) { + if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); + if (!STBI__RESTART(z->marker)) return 1; + stbi__jpeg_reset(z); + } + } + } + return 1; + } + } else { + if (z->scan_n == 1) { + int i,j; + int n = z->order[0]; + // non-interleaved data, we just need to process one block at a time, + // in trivial scanline order + // number of blocks to do just depends on how many actual "pixels" this + // component has, independent of interleaved MCU blocking and such + int w = (z->img_comp[n].x+7) >> 3; + int h = (z->img_comp[n].y+7) >> 3; + for (j=0; j < h; ++j) { + for (i=0; i < w; ++i) { + short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w); + if (z->spec_start == 0) { + if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n)) + return 0; + } else { + int ha = z->img_comp[n].ha; + if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->huff_ac[ha], z->fast_ac[ha])) + return 0; + } + // every data block is an MCU, so countdown the restart interval + if (--z->todo <= 0) { + if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); + if (!STBI__RESTART(z->marker)) return 1; + stbi__jpeg_reset(z); + } + } + } + return 1; + } else { // interleaved + int i,j,k,x,y; + for (j=0; j < z->img_mcu_y; ++j) { + for (i=0; i < z->img_mcu_x; ++i) { + // scan an interleaved mcu... process scan_n components in order + for (k=0; k < z->scan_n; ++k) { + int n = z->order[k]; + // scan out an mcu's worth of this component; that's just determined + // by the basic H and V specified for the component + for (y=0; y < z->img_comp[n].v; ++y) { + for (x=0; x < z->img_comp[n].h; ++x) { + int x2 = (i*z->img_comp[n].h + x); + int y2 = (j*z->img_comp[n].v + y); + short *data = z->img_comp[n].coeff + 64 * (x2 + y2 * z->img_comp[n].coeff_w); + if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n)) + return 0; + } + } + } + // after all interleaved components, that's an interleaved MCU, + // so now count down the restart interval + if (--z->todo <= 0) { + if (z->code_bits < 24) stbi__grow_buffer_unsafe(z); + if (!STBI__RESTART(z->marker)) return 1; + stbi__jpeg_reset(z); + } + } + } + return 1; + } + } +} + +static void stbi__jpeg_dequantize(short *data, stbi__uint16 *dequant) +{ + int i; + for (i=0; i < 64; ++i) + data[i] *= dequant[i]; +} + +static void stbi__jpeg_finish(stbi__jpeg *z) +{ + if (z->progressive) { + // dequantize and idct the data + int i,j,n; + for (n=0; n < z->s->img_n; ++n) { + int w = (z->img_comp[n].x+7) >> 3; + int h = (z->img_comp[n].y+7) >> 3; + for (j=0; j < h; ++j) { + for (i=0; i < w; ++i) { + short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w); + stbi__jpeg_dequantize(data, z->dequant[z->img_comp[n].tq]); + z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data); + } + } + } + } +} + +static int stbi__process_marker(stbi__jpeg *z, int m) +{ + int L; + switch (m) { + case STBI__MARKER_none: // no marker found + return stbi__err("expected marker","Corrupt JPEG"); + + case 0xDD: // DRI - specify restart interval + if (stbi__get16be(z->s) != 4) return stbi__err("bad DRI len","Corrupt JPEG"); + z->restart_interval = stbi__get16be(z->s); + return 1; + + case 0xDB: // DQT - define quantization table + L = stbi__get16be(z->s)-2; + while (L > 0) { + int q = stbi__get8(z->s); + int p = q >> 4, sixteen = (p != 0); + int t = q & 15,i; + if (p != 0 && p != 1) return stbi__err("bad DQT type","Corrupt JPEG"); + if (t > 3) return stbi__err("bad DQT table","Corrupt JPEG"); + + for (i=0; i < 64; ++i) + z->dequant[t][stbi__jpeg_dezigzag[i]] = (stbi__uint16)(sixteen ? stbi__get16be(z->s) : stbi__get8(z->s)); + L -= (sixteen ? 129 : 65); + } + return L==0; + + case 0xC4: // DHT - define huffman table + L = stbi__get16be(z->s)-2; + while (L > 0) { + stbi_uc *v; + int sizes[16],i,n=0; + int q = stbi__get8(z->s); + int tc = q >> 4; + int th = q & 15; + if (tc > 1 || th > 3) return stbi__err("bad DHT header","Corrupt JPEG"); + for (i=0; i < 16; ++i) { + sizes[i] = stbi__get8(z->s); + n += sizes[i]; + } + if(n > 256) return stbi__err("bad DHT header","Corrupt JPEG"); // Loop over i < n would write past end of values! + L -= 17; + if (tc == 0) { + if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0; + v = z->huff_dc[th].values; + } else { + if (!stbi__build_huffman(z->huff_ac+th, sizes)) return 0; + v = z->huff_ac[th].values; + } + for (i=0; i < n; ++i) + v[i] = stbi__get8(z->s); + if (tc != 0) + stbi__build_fast_ac(z->fast_ac[th], z->huff_ac + th); + L -= n; + } + return L==0; + } + + // check for comment block or APP blocks + if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) { + L = stbi__get16be(z->s); + if (L < 2) { + if (m == 0xFE) + return stbi__err("bad COM len","Corrupt JPEG"); + else + return stbi__err("bad APP len","Corrupt JPEG"); + } + L -= 2; + + if (m == 0xE0 && L >= 5) { // JFIF APP0 segment + static const unsigned char tag[5] = {'J','F','I','F','\0'}; + int ok = 1; + int i; + for (i=0; i < 5; ++i) + if (stbi__get8(z->s) != tag[i]) + ok = 0; + L -= 5; + if (ok) + z->jfif = 1; + } else if (m == 0xEE && L >= 12) { // Adobe APP14 segment + static const unsigned char tag[6] = {'A','d','o','b','e','\0'}; + int ok = 1; + int i; + for (i=0; i < 6; ++i) + if (stbi__get8(z->s) != tag[i]) + ok = 0; + L -= 6; + if (ok) { + stbi__get8(z->s); // version + stbi__get16be(z->s); // flags0 + stbi__get16be(z->s); // flags1 + z->app14_color_transform = stbi__get8(z->s); // color transform + L -= 6; + } + } + + stbi__skip(z->s, L); + return 1; + } + + return stbi__err("unknown marker","Corrupt JPEG"); +} + +// after we see SOS +static int stbi__process_scan_header(stbi__jpeg *z) +{ + int i; + int Ls = stbi__get16be(z->s); + z->scan_n = stbi__get8(z->s); + if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s->img_n) return stbi__err("bad SOS component count","Corrupt JPEG"); + if (Ls != 6+2*z->scan_n) return stbi__err("bad SOS len","Corrupt JPEG"); + for (i=0; i < z->scan_n; ++i) { + int id = stbi__get8(z->s), which; + int q = stbi__get8(z->s); + for (which = 0; which < z->s->img_n; ++which) + if (z->img_comp[which].id == id) + break; + if (which == z->s->img_n) return 0; // no match + z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return stbi__err("bad DC huff","Corrupt JPEG"); + z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return stbi__err("bad AC huff","Corrupt JPEG"); + z->order[i] = which; + } + + { + int aa; + z->spec_start = stbi__get8(z->s); + z->spec_end = stbi__get8(z->s); // should be 63, but might be 0 + aa = stbi__get8(z->s); + z->succ_high = (aa >> 4); + z->succ_low = (aa & 15); + if (z->progressive) { + if (z->spec_start > 63 || z->spec_end > 63 || z->spec_start > z->spec_end || z->succ_high > 13 || z->succ_low > 13) + return stbi__err("bad SOS", "Corrupt JPEG"); + } else { + if (z->spec_start != 0) return stbi__err("bad SOS","Corrupt JPEG"); + if (z->succ_high != 0 || z->succ_low != 0) return stbi__err("bad SOS","Corrupt JPEG"); + z->spec_end = 63; + } + } + + return 1; +} + +static int stbi__free_jpeg_components(stbi__jpeg *z, int ncomp, int why) +{ + int i; + for (i=0; i < ncomp; ++i) { + if (z->img_comp[i].raw_data) { + STBI_FREE(z->img_comp[i].raw_data); + z->img_comp[i].raw_data = NULL; + z->img_comp[i].data = NULL; + } + if (z->img_comp[i].raw_coeff) { + STBI_FREE(z->img_comp[i].raw_coeff); + z->img_comp[i].raw_coeff = 0; + z->img_comp[i].coeff = 0; + } + if (z->img_comp[i].linebuf) { + STBI_FREE(z->img_comp[i].linebuf); + z->img_comp[i].linebuf = NULL; + } + } + return why; +} + +static int stbi__process_frame_header(stbi__jpeg *z, int scan) +{ + stbi__context *s = z->s; + int Lf,p,i,q, h_max=1,v_max=1,c; + Lf = stbi__get16be(s); if (Lf < 11) return stbi__err("bad SOF len","Corrupt JPEG"); // JPEG + p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline + s->img_y = stbi__get16be(s); if (s->img_y == 0) return stbi__err("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG + s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width","Corrupt JPEG"); // JPEG requires + if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + c = stbi__get8(s); + if (c != 3 && c != 1 && c != 4) return stbi__err("bad component count","Corrupt JPEG"); + s->img_n = c; + for (i=0; i < c; ++i) { + z->img_comp[i].data = NULL; + z->img_comp[i].linebuf = NULL; + } + + if (Lf != 8+3*s->img_n) return stbi__err("bad SOF len","Corrupt JPEG"); + + z->rgb = 0; + for (i=0; i < s->img_n; ++i) { + static const unsigned char rgb[3] = { 'R', 'G', 'B' }; + z->img_comp[i].id = stbi__get8(s); + if (s->img_n == 3 && z->img_comp[i].id == rgb[i]) + ++z->rgb; + q = stbi__get8(s); + z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return stbi__err("bad H","Corrupt JPEG"); + z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return stbi__err("bad V","Corrupt JPEG"); + z->img_comp[i].tq = stbi__get8(s); if (z->img_comp[i].tq > 3) return stbi__err("bad TQ","Corrupt JPEG"); + } + + if (scan != STBI__SCAN_load) return 1; + + if (!stbi__mad3sizes_valid(s->img_x, s->img_y, s->img_n, 0)) return stbi__err("too large", "Image too large to decode"); + + for (i=0; i < s->img_n; ++i) { + if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h; + if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v; + } + + // check that plane subsampling factors are integer ratios; our resamplers can't deal with fractional ratios + // and I've never seen a non-corrupted JPEG file actually use them + for (i=0; i < s->img_n; ++i) { + if (h_max % z->img_comp[i].h != 0) return stbi__err("bad H","Corrupt JPEG"); + if (v_max % z->img_comp[i].v != 0) return stbi__err("bad V","Corrupt JPEG"); + } + + // compute interleaved mcu info + z->img_h_max = h_max; + z->img_v_max = v_max; + z->img_mcu_w = h_max * 8; + z->img_mcu_h = v_max * 8; + // these sizes can't be more than 17 bits + z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w; + z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h; + + for (i=0; i < s->img_n; ++i) { + // number of effective pixels (e.g. for non-interleaved MCU) + z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max; + z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max; + // to simplify generation, we'll allocate enough memory to decode + // the bogus oversized data from using interleaved MCUs and their + // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't + // discard the extra data until colorspace conversion + // + // img_mcu_x, img_mcu_y: <=17 bits; comp[i].h and .v are <=4 (checked earlier) + // so these muls can't overflow with 32-bit ints (which we require) + z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8; + z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8; + z->img_comp[i].coeff = 0; + z->img_comp[i].raw_coeff = 0; + z->img_comp[i].linebuf = NULL; + z->img_comp[i].raw_data = stbi__malloc_mad2(z->img_comp[i].w2, z->img_comp[i].h2, 15); + if (z->img_comp[i].raw_data == NULL) + return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory")); + // align blocks for idct using mmx/sse + z->img_comp[i].data = (stbi_uc*) (((size_t) z->img_comp[i].raw_data + 15) & ~15); + if (z->progressive) { + // w2, h2 are multiples of 8 (see above) + z->img_comp[i].coeff_w = z->img_comp[i].w2 / 8; + z->img_comp[i].coeff_h = z->img_comp[i].h2 / 8; + z->img_comp[i].raw_coeff = stbi__malloc_mad3(z->img_comp[i].w2, z->img_comp[i].h2, sizeof(short), 15); + if (z->img_comp[i].raw_coeff == NULL) + return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory")); + z->img_comp[i].coeff = (short*) (((size_t) z->img_comp[i].raw_coeff + 15) & ~15); + } + } + + return 1; +} + +// use comparisons since in some cases we handle more than one case (e.g. SOF) +#define stbi__DNL(x) ((x) == 0xdc) +#define stbi__SOI(x) ((x) == 0xd8) +#define stbi__EOI(x) ((x) == 0xd9) +#define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1 || (x) == 0xc2) +#define stbi__SOS(x) ((x) == 0xda) + +#define stbi__SOF_progressive(x) ((x) == 0xc2) + +static int stbi__decode_jpeg_header(stbi__jpeg *z, int scan) +{ + int m; + z->jfif = 0; + z->app14_color_transform = -1; // valid values are 0,1,2 + z->marker = STBI__MARKER_none; // initialize cached marker to empty + m = stbi__get_marker(z); + if (!stbi__SOI(m)) return stbi__err("no SOI","Corrupt JPEG"); + if (scan == STBI__SCAN_type) return 1; + m = stbi__get_marker(z); + while (!stbi__SOF(m)) { + if (!stbi__process_marker(z,m)) return 0; + m = stbi__get_marker(z); + while (m == STBI__MARKER_none) { + // some files have extra padding after their blocks, so ok, we'll scan + if (stbi__at_eof(z->s)) return stbi__err("no SOF", "Corrupt JPEG"); + m = stbi__get_marker(z); + } + } + z->progressive = stbi__SOF_progressive(m); + if (!stbi__process_frame_header(z, scan)) return 0; + return 1; +} + +static stbi_uc stbi__skip_jpeg_junk_at_end(stbi__jpeg *j) +{ + // some JPEGs have junk at end, skip over it but if we find what looks + // like a valid marker, resume there + while (!stbi__at_eof(j->s)) { + stbi_uc x = stbi__get8(j->s); + while (x == 0xff) { // might be a marker + if (stbi__at_eof(j->s)) return STBI__MARKER_none; + x = stbi__get8(j->s); + if (x != 0x00 && x != 0xff) { + // not a stuffed zero or lead-in to another marker, looks + // like an actual marker, return it + return x; + } + // stuffed zero has x=0 now which ends the loop, meaning we go + // back to regular scan loop. + // repeated 0xff keeps trying to read the next byte of the marker. + } + } + return STBI__MARKER_none; +} + +// decode image to YCbCr format +static int stbi__decode_jpeg_image(stbi__jpeg *j) +{ + int m; + for (m = 0; m < 4; m++) { + j->img_comp[m].raw_data = NULL; + j->img_comp[m].raw_coeff = NULL; + } + j->restart_interval = 0; + if (!stbi__decode_jpeg_header(j, STBI__SCAN_load)) return 0; + m = stbi__get_marker(j); + while (!stbi__EOI(m)) { + if (stbi__SOS(m)) { + if (!stbi__process_scan_header(j)) return 0; + if (!stbi__parse_entropy_coded_data(j)) return 0; + if (j->marker == STBI__MARKER_none ) { + j->marker = stbi__skip_jpeg_junk_at_end(j); + // if we reach eof without hitting a marker, stbi__get_marker() below will fail and we'll eventually return 0 + } + m = stbi__get_marker(j); + if (STBI__RESTART(m)) + m = stbi__get_marker(j); + } else if (stbi__DNL(m)) { + int Ld = stbi__get16be(j->s); + stbi__uint32 NL = stbi__get16be(j->s); + if (Ld != 4) return stbi__err("bad DNL len", "Corrupt JPEG"); + if (NL != j->s->img_y) return stbi__err("bad DNL height", "Corrupt JPEG"); + m = stbi__get_marker(j); + } else { + if (!stbi__process_marker(j, m)) return 1; + m = stbi__get_marker(j); + } + } + if (j->progressive) + stbi__jpeg_finish(j); + return 1; +} + +// static jfif-centered resampling (across block boundaries) + +typedef stbi_uc *(*resample_row_func)(stbi_uc *out, stbi_uc *in0, stbi_uc *in1, + int w, int hs); + +#define stbi__div4(x) ((stbi_uc) ((x) >> 2)) + +static stbi_uc *resample_row_1(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + STBI_NOTUSED(out); + STBI_NOTUSED(in_far); + STBI_NOTUSED(w); + STBI_NOTUSED(hs); + return in_near; +} + +static stbi_uc* stbi__resample_row_v_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // need to generate two samples vertically for every one in input + int i; + STBI_NOTUSED(hs); + for (i=0; i < w; ++i) + out[i] = stbi__div4(3*in_near[i] + in_far[i] + 2); + return out; +} + +static stbi_uc* stbi__resample_row_h_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // need to generate two samples horizontally for every one in input + int i; + stbi_uc *input = in_near; + + if (w == 1) { + // if only one sample, can't do any interpolation + out[0] = out[1] = input[0]; + return out; + } + + out[0] = input[0]; + out[1] = stbi__div4(input[0]*3 + input[1] + 2); + for (i=1; i < w-1; ++i) { + int n = 3*input[i]+2; + out[i*2+0] = stbi__div4(n+input[i-1]); + out[i*2+1] = stbi__div4(n+input[i+1]); + } + out[i*2+0] = stbi__div4(input[w-2]*3 + input[w-1] + 2); + out[i*2+1] = input[w-1]; + + STBI_NOTUSED(in_far); + STBI_NOTUSED(hs); + + return out; +} + +#define stbi__div16(x) ((stbi_uc) ((x) >> 4)) + +static stbi_uc *stbi__resample_row_hv_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // need to generate 2x2 samples for every one in input + int i,t0,t1; + if (w == 1) { + out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2); + return out; + } + + t1 = 3*in_near[0] + in_far[0]; + out[0] = stbi__div4(t1+2); + for (i=1; i < w; ++i) { + t0 = t1; + t1 = 3*in_near[i]+in_far[i]; + out[i*2-1] = stbi__div16(3*t0 + t1 + 8); + out[i*2 ] = stbi__div16(3*t1 + t0 + 8); + } + out[w*2-1] = stbi__div4(t1+2); + + STBI_NOTUSED(hs); + + return out; +} + +#if defined(STBI_SSE2) || defined(STBI_NEON) +static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // need to generate 2x2 samples for every one in input + int i=0,t0,t1; + + if (w == 1) { + out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2); + return out; + } + + t1 = 3*in_near[0] + in_far[0]; + // process groups of 8 pixels for as long as we can. + // note we can't handle the last pixel in a row in this loop + // because we need to handle the filter boundary conditions. + for (; i < ((w-1) & ~7); i += 8) { +#if defined(STBI_SSE2) + // load and perform the vertical filtering pass + // this uses 3*x + y = 4*x + (y - x) + __m128i zero = _mm_setzero_si128(); + __m128i farb = _mm_loadl_epi64((__m128i *) (in_far + i)); + __m128i nearb = _mm_loadl_epi64((__m128i *) (in_near + i)); + __m128i farw = _mm_unpacklo_epi8(farb, zero); + __m128i nearw = _mm_unpacklo_epi8(nearb, zero); + __m128i diff = _mm_sub_epi16(farw, nearw); + __m128i nears = _mm_slli_epi16(nearw, 2); + __m128i curr = _mm_add_epi16(nears, diff); // current row + + // horizontal filter works the same based on shifted vers of current + // row. "prev" is current row shifted right by 1 pixel; we need to + // insert the previous pixel value (from t1). + // "next" is current row shifted left by 1 pixel, with first pixel + // of next block of 8 pixels added in. + __m128i prv0 = _mm_slli_si128(curr, 2); + __m128i nxt0 = _mm_srli_si128(curr, 2); + __m128i prev = _mm_insert_epi16(prv0, t1, 0); + __m128i next = _mm_insert_epi16(nxt0, 3*in_near[i+8] + in_far[i+8], 7); + + // horizontal filter, polyphase implementation since it's convenient: + // even pixels = 3*cur + prev = cur*4 + (prev - cur) + // odd pixels = 3*cur + next = cur*4 + (next - cur) + // note the shared term. + __m128i bias = _mm_set1_epi16(8); + __m128i curs = _mm_slli_epi16(curr, 2); + __m128i prvd = _mm_sub_epi16(prev, curr); + __m128i nxtd = _mm_sub_epi16(next, curr); + __m128i curb = _mm_add_epi16(curs, bias); + __m128i even = _mm_add_epi16(prvd, curb); + __m128i odd = _mm_add_epi16(nxtd, curb); + + // interleave even and odd pixels, then undo scaling. + __m128i int0 = _mm_unpacklo_epi16(even, odd); + __m128i int1 = _mm_unpackhi_epi16(even, odd); + __m128i de0 = _mm_srli_epi16(int0, 4); + __m128i de1 = _mm_srli_epi16(int1, 4); + + // pack and write output + __m128i outv = _mm_packus_epi16(de0, de1); + _mm_storeu_si128((__m128i *) (out + i*2), outv); +#elif defined(STBI_NEON) + // load and perform the vertical filtering pass + // this uses 3*x + y = 4*x + (y - x) + uint8x8_t farb = vld1_u8(in_far + i); + uint8x8_t nearb = vld1_u8(in_near + i); + int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(farb, nearb)); + int16x8_t nears = vreinterpretq_s16_u16(vshll_n_u8(nearb, 2)); + int16x8_t curr = vaddq_s16(nears, diff); // current row + + // horizontal filter works the same based on shifted vers of current + // row. "prev" is current row shifted right by 1 pixel; we need to + // insert the previous pixel value (from t1). + // "next" is current row shifted left by 1 pixel, with first pixel + // of next block of 8 pixels added in. + int16x8_t prv0 = vextq_s16(curr, curr, 7); + int16x8_t nxt0 = vextq_s16(curr, curr, 1); + int16x8_t prev = vsetq_lane_s16(t1, prv0, 0); + int16x8_t next = vsetq_lane_s16(3*in_near[i+8] + in_far[i+8], nxt0, 7); + + // horizontal filter, polyphase implementation since it's convenient: + // even pixels = 3*cur + prev = cur*4 + (prev - cur) + // odd pixels = 3*cur + next = cur*4 + (next - cur) + // note the shared term. + int16x8_t curs = vshlq_n_s16(curr, 2); + int16x8_t prvd = vsubq_s16(prev, curr); + int16x8_t nxtd = vsubq_s16(next, curr); + int16x8_t even = vaddq_s16(curs, prvd); + int16x8_t odd = vaddq_s16(curs, nxtd); + + // undo scaling and round, then store with even/odd phases interleaved + uint8x8x2_t o; + o.val[0] = vqrshrun_n_s16(even, 4); + o.val[1] = vqrshrun_n_s16(odd, 4); + vst2_u8(out + i*2, o); +#endif + + // "previous" value for next iter + t1 = 3*in_near[i+7] + in_far[i+7]; + } + + t0 = t1; + t1 = 3*in_near[i] + in_far[i]; + out[i*2] = stbi__div16(3*t1 + t0 + 8); + + for (++i; i < w; ++i) { + t0 = t1; + t1 = 3*in_near[i]+in_far[i]; + out[i*2-1] = stbi__div16(3*t0 + t1 + 8); + out[i*2 ] = stbi__div16(3*t1 + t0 + 8); + } + out[w*2-1] = stbi__div4(t1+2); + + STBI_NOTUSED(hs); + + return out; +} +#endif + +static stbi_uc *stbi__resample_row_generic(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs) +{ + // resample with nearest-neighbor + int i,j; + STBI_NOTUSED(in_far); + for (i=0; i < w; ++i) + for (j=0; j < hs; ++j) + out[i*hs+j] = in_near[i]; + return out; +} + +// this is a reduced-precision calculation of YCbCr-to-RGB introduced +// to make sure the code produces the same results in both SIMD and scalar +#define stbi__float2fixed(x) (((int) ((x) * 4096.0f + 0.5f)) << 8) +static void stbi__YCbCr_to_RGB_row(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step) +{ + int i; + for (i=0; i < count; ++i) { + int y_fixed = (y[i] << 20) + (1<<19); // rounding + int r,g,b; + int cr = pcr[i] - 128; + int cb = pcb[i] - 128; + r = y_fixed + cr* stbi__float2fixed(1.40200f); + g = y_fixed + (cr*-stbi__float2fixed(0.71414f)) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000); + b = y_fixed + cb* stbi__float2fixed(1.77200f); + r >>= 20; + g >>= 20; + b >>= 20; + if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; } + if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; } + if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; } + out[0] = (stbi_uc)r; + out[1] = (stbi_uc)g; + out[2] = (stbi_uc)b; + out[3] = 255; + out += step; + } +} + +#if defined(STBI_SSE2) || defined(STBI_NEON) +static void stbi__YCbCr_to_RGB_simd(stbi_uc *out, stbi_uc const *y, stbi_uc const *pcb, stbi_uc const *pcr, int count, int step) +{ + int i = 0; + +#ifdef STBI_SSE2 + // step == 3 is pretty ugly on the final interleave, and i'm not convinced + // it's useful in practice (you wouldn't use it for textures, for example). + // so just accelerate step == 4 case. + if (step == 4) { + // this is a fairly straightforward implementation and not super-optimized. + __m128i signflip = _mm_set1_epi8(-0x80); + __m128i cr_const0 = _mm_set1_epi16( (short) ( 1.40200f*4096.0f+0.5f)); + __m128i cr_const1 = _mm_set1_epi16( - (short) ( 0.71414f*4096.0f+0.5f)); + __m128i cb_const0 = _mm_set1_epi16( - (short) ( 0.34414f*4096.0f+0.5f)); + __m128i cb_const1 = _mm_set1_epi16( (short) ( 1.77200f*4096.0f+0.5f)); + __m128i y_bias = _mm_set1_epi8((char) (unsigned char) 128); + __m128i xw = _mm_set1_epi16(255); // alpha channel + + for (; i+7 < count; i += 8) { + // load + __m128i y_bytes = _mm_loadl_epi64((__m128i *) (y+i)); + __m128i cr_bytes = _mm_loadl_epi64((__m128i *) (pcr+i)); + __m128i cb_bytes = _mm_loadl_epi64((__m128i *) (pcb+i)); + __m128i cr_biased = _mm_xor_si128(cr_bytes, signflip); // -128 + __m128i cb_biased = _mm_xor_si128(cb_bytes, signflip); // -128 + + // unpack to short (and left-shift cr, cb by 8) + __m128i yw = _mm_unpacklo_epi8(y_bias, y_bytes); + __m128i crw = _mm_unpacklo_epi8(_mm_setzero_si128(), cr_biased); + __m128i cbw = _mm_unpacklo_epi8(_mm_setzero_si128(), cb_biased); + + // color transform + __m128i yws = _mm_srli_epi16(yw, 4); + __m128i cr0 = _mm_mulhi_epi16(cr_const0, crw); + __m128i cb0 = _mm_mulhi_epi16(cb_const0, cbw); + __m128i cb1 = _mm_mulhi_epi16(cbw, cb_const1); + __m128i cr1 = _mm_mulhi_epi16(crw, cr_const1); + __m128i rws = _mm_add_epi16(cr0, yws); + __m128i gwt = _mm_add_epi16(cb0, yws); + __m128i bws = _mm_add_epi16(yws, cb1); + __m128i gws = _mm_add_epi16(gwt, cr1); + + // descale + __m128i rw = _mm_srai_epi16(rws, 4); + __m128i bw = _mm_srai_epi16(bws, 4); + __m128i gw = _mm_srai_epi16(gws, 4); + + // back to byte, set up for transpose + __m128i brb = _mm_packus_epi16(rw, bw); + __m128i gxb = _mm_packus_epi16(gw, xw); + + // transpose to interleave channels + __m128i t0 = _mm_unpacklo_epi8(brb, gxb); + __m128i t1 = _mm_unpackhi_epi8(brb, gxb); + __m128i o0 = _mm_unpacklo_epi16(t0, t1); + __m128i o1 = _mm_unpackhi_epi16(t0, t1); + + // store + _mm_storeu_si128((__m128i *) (out + 0), o0); + _mm_storeu_si128((__m128i *) (out + 16), o1); + out += 32; + } + } +#endif + +#ifdef STBI_NEON + // in this version, step=3 support would be easy to add. but is there demand? + if (step == 4) { + // this is a fairly straightforward implementation and not super-optimized. + uint8x8_t signflip = vdup_n_u8(0x80); + int16x8_t cr_const0 = vdupq_n_s16( (short) ( 1.40200f*4096.0f+0.5f)); + int16x8_t cr_const1 = vdupq_n_s16( - (short) ( 0.71414f*4096.0f+0.5f)); + int16x8_t cb_const0 = vdupq_n_s16( - (short) ( 0.34414f*4096.0f+0.5f)); + int16x8_t cb_const1 = vdupq_n_s16( (short) ( 1.77200f*4096.0f+0.5f)); + + for (; i+7 < count; i += 8) { + // load + uint8x8_t y_bytes = vld1_u8(y + i); + uint8x8_t cr_bytes = vld1_u8(pcr + i); + uint8x8_t cb_bytes = vld1_u8(pcb + i); + int8x8_t cr_biased = vreinterpret_s8_u8(vsub_u8(cr_bytes, signflip)); + int8x8_t cb_biased = vreinterpret_s8_u8(vsub_u8(cb_bytes, signflip)); + + // expand to s16 + int16x8_t yws = vreinterpretq_s16_u16(vshll_n_u8(y_bytes, 4)); + int16x8_t crw = vshll_n_s8(cr_biased, 7); + int16x8_t cbw = vshll_n_s8(cb_biased, 7); + + // color transform + int16x8_t cr0 = vqdmulhq_s16(crw, cr_const0); + int16x8_t cb0 = vqdmulhq_s16(cbw, cb_const0); + int16x8_t cr1 = vqdmulhq_s16(crw, cr_const1); + int16x8_t cb1 = vqdmulhq_s16(cbw, cb_const1); + int16x8_t rws = vaddq_s16(yws, cr0); + int16x8_t gws = vaddq_s16(vaddq_s16(yws, cb0), cr1); + int16x8_t bws = vaddq_s16(yws, cb1); + + // undo scaling, round, convert to byte + uint8x8x4_t o; + o.val[0] = vqrshrun_n_s16(rws, 4); + o.val[1] = vqrshrun_n_s16(gws, 4); + o.val[2] = vqrshrun_n_s16(bws, 4); + o.val[3] = vdup_n_u8(255); + + // store, interleaving r/g/b/a + vst4_u8(out, o); + out += 8*4; + } + } +#endif + + for (; i < count; ++i) { + int y_fixed = (y[i] << 20) + (1<<19); // rounding + int r,g,b; + int cr = pcr[i] - 128; + int cb = pcb[i] - 128; + r = y_fixed + cr* stbi__float2fixed(1.40200f); + g = y_fixed + cr*-stbi__float2fixed(0.71414f) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000); + b = y_fixed + cb* stbi__float2fixed(1.77200f); + r >>= 20; + g >>= 20; + b >>= 20; + if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; } + if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; } + if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; } + out[0] = (stbi_uc)r; + out[1] = (stbi_uc)g; + out[2] = (stbi_uc)b; + out[3] = 255; + out += step; + } +} +#endif + +// set up the kernels +static void stbi__setup_jpeg(stbi__jpeg *j) +{ + j->idct_block_kernel = stbi__idct_block; + j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_row; + j->resample_row_hv_2_kernel = stbi__resample_row_hv_2; + +#ifdef STBI_SSE2 + if (stbi__sse2_available()) { + j->idct_block_kernel = stbi__idct_simd; + j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd; + j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd; + } +#endif + +#ifdef STBI_NEON + j->idct_block_kernel = stbi__idct_simd; + j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd; + j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd; +#endif +} + +// clean up the temporary component buffers +static void stbi__cleanup_jpeg(stbi__jpeg *j) +{ + stbi__free_jpeg_components(j, j->s->img_n, 0); +} + +typedef struct +{ + resample_row_func resample; + stbi_uc *line0,*line1; + int hs,vs; // expansion factor in each axis + int w_lores; // horizontal pixels pre-expansion + int ystep; // how far through vertical expansion we are + int ypos; // which pre-expansion row we're on +} stbi__resample; + +// fast 0..255 * 0..255 => 0..255 rounded multiplication +static stbi_uc stbi__blinn_8x8(stbi_uc x, stbi_uc y) +{ + unsigned int t = x*y + 128; + return (stbi_uc) ((t + (t >>8)) >> 8); +} + +static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp) +{ + int n, decode_n, is_rgb; + z->s->img_n = 0; // make stbi__cleanup_jpeg safe + + // validate req_comp + if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error"); + + // load a jpeg image from whichever source, but leave in YCbCr format + if (!stbi__decode_jpeg_image(z)) { stbi__cleanup_jpeg(z); return NULL; } + + // determine actual number of components to generate + n = req_comp ? req_comp : z->s->img_n >= 3 ? 3 : 1; + + is_rgb = z->s->img_n == 3 && (z->rgb == 3 || (z->app14_color_transform == 0 && !z->jfif)); + + if (z->s->img_n == 3 && n < 3 && !is_rgb) + decode_n = 1; + else + decode_n = z->s->img_n; + + // nothing to do if no components requested; check this now to avoid + // accessing uninitialized coutput[0] later + if (decode_n <= 0) { stbi__cleanup_jpeg(z); return NULL; } + + // resample and color-convert + { + int k; + unsigned int i,j; + stbi_uc *output; + stbi_uc *coutput[4] = { NULL, NULL, NULL, NULL }; + + stbi__resample res_comp[4]; + + for (k=0; k < decode_n; ++k) { + stbi__resample *r = &res_comp[k]; + + // allocate line buffer big enough for upsampling off the edges + // with upsample factor of 4 + z->img_comp[k].linebuf = (stbi_uc *) stbi__malloc(z->s->img_x + 3); + if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); } + + r->hs = z->img_h_max / z->img_comp[k].h; + r->vs = z->img_v_max / z->img_comp[k].v; + r->ystep = r->vs >> 1; + r->w_lores = (z->s->img_x + r->hs-1) / r->hs; + r->ypos = 0; + r->line0 = r->line1 = z->img_comp[k].data; + + if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1; + else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2; + else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2; + else if (r->hs == 2 && r->vs == 2) r->resample = z->resample_row_hv_2_kernel; + else r->resample = stbi__resample_row_generic; + } + + // can't error after this so, this is safe + output = (stbi_uc *) stbi__malloc_mad3(n, z->s->img_x, z->s->img_y, 1); + if (!output) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); } + + // now go ahead and resample + for (j=0; j < z->s->img_y; ++j) { + stbi_uc *out = output + n * z->s->img_x * j; + for (k=0; k < decode_n; ++k) { + stbi__resample *r = &res_comp[k]; + int y_bot = r->ystep >= (r->vs >> 1); + coutput[k] = r->resample(z->img_comp[k].linebuf, + y_bot ? r->line1 : r->line0, + y_bot ? r->line0 : r->line1, + r->w_lores, r->hs); + if (++r->ystep >= r->vs) { + r->ystep = 0; + r->line0 = r->line1; + if (++r->ypos < z->img_comp[k].y) + r->line1 += z->img_comp[k].w2; + } + } + if (n >= 3) { + stbi_uc *y = coutput[0]; + if (z->s->img_n == 3) { + if (is_rgb) { + for (i=0; i < z->s->img_x; ++i) { + out[0] = y[i]; + out[1] = coutput[1][i]; + out[2] = coutput[2][i]; + out[3] = 255; + out += n; + } + } else { + z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); + } + } else if (z->s->img_n == 4) { + if (z->app14_color_transform == 0) { // CMYK + for (i=0; i < z->s->img_x; ++i) { + stbi_uc m = coutput[3][i]; + out[0] = stbi__blinn_8x8(coutput[0][i], m); + out[1] = stbi__blinn_8x8(coutput[1][i], m); + out[2] = stbi__blinn_8x8(coutput[2][i], m); + out[3] = 255; + out += n; + } + } else if (z->app14_color_transform == 2) { // YCCK + z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); + for (i=0; i < z->s->img_x; ++i) { + stbi_uc m = coutput[3][i]; + out[0] = stbi__blinn_8x8(255 - out[0], m); + out[1] = stbi__blinn_8x8(255 - out[1], m); + out[2] = stbi__blinn_8x8(255 - out[2], m); + out += n; + } + } else { // YCbCr + alpha? Ignore the fourth channel for now + z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n); + } + } else + for (i=0; i < z->s->img_x; ++i) { + out[0] = out[1] = out[2] = y[i]; + out[3] = 255; // not used if n==3 + out += n; + } + } else { + if (is_rgb) { + if (n == 1) + for (i=0; i < z->s->img_x; ++i) + *out++ = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]); + else { + for (i=0; i < z->s->img_x; ++i, out += 2) { + out[0] = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]); + out[1] = 255; + } + } + } else if (z->s->img_n == 4 && z->app14_color_transform == 0) { + for (i=0; i < z->s->img_x; ++i) { + stbi_uc m = coutput[3][i]; + stbi_uc r = stbi__blinn_8x8(coutput[0][i], m); + stbi_uc g = stbi__blinn_8x8(coutput[1][i], m); + stbi_uc b = stbi__blinn_8x8(coutput[2][i], m); + out[0] = stbi__compute_y(r, g, b); + out[1] = 255; + out += n; + } + } else if (z->s->img_n == 4 && z->app14_color_transform == 2) { + for (i=0; i < z->s->img_x; ++i) { + out[0] = stbi__blinn_8x8(255 - coutput[0][i], coutput[3][i]); + out[1] = 255; + out += n; + } + } else { + stbi_uc *y = coutput[0]; + if (n == 1) + for (i=0; i < z->s->img_x; ++i) out[i] = y[i]; + else + for (i=0; i < z->s->img_x; ++i) { *out++ = y[i]; *out++ = 255; } + } + } + } + stbi__cleanup_jpeg(z); + *out_x = z->s->img_x; + *out_y = z->s->img_y; + if (comp) *comp = z->s->img_n >= 3 ? 3 : 1; // report original components, not output + return output; + } +} + +static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + unsigned char* result; + stbi__jpeg* j = (stbi__jpeg*) stbi__malloc(sizeof(stbi__jpeg)); + if (!j) return stbi__errpuc("outofmem", "Out of memory"); + memset(j, 0, sizeof(stbi__jpeg)); + STBI_NOTUSED(ri); + j->s = s; + stbi__setup_jpeg(j); + result = load_jpeg_image(j, x,y,comp,req_comp); + STBI_FREE(j); + return result; +} + +static int stbi__jpeg_test(stbi__context *s) +{ + int r; + stbi__jpeg* j = (stbi__jpeg*)stbi__malloc(sizeof(stbi__jpeg)); + if (!j) return stbi__err("outofmem", "Out of memory"); + memset(j, 0, sizeof(stbi__jpeg)); + j->s = s; + stbi__setup_jpeg(j); + r = stbi__decode_jpeg_header(j, STBI__SCAN_type); + stbi__rewind(s); + STBI_FREE(j); + return r; +} + +static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp) +{ + if (!stbi__decode_jpeg_header(j, STBI__SCAN_header)) { + stbi__rewind( j->s ); + return 0; + } + if (x) *x = j->s->img_x; + if (y) *y = j->s->img_y; + if (comp) *comp = j->s->img_n >= 3 ? 3 : 1; + return 1; +} + +static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp) +{ + int result; + stbi__jpeg* j = (stbi__jpeg*) (stbi__malloc(sizeof(stbi__jpeg))); + if (!j) return stbi__err("outofmem", "Out of memory"); + memset(j, 0, sizeof(stbi__jpeg)); + j->s = s; + result = stbi__jpeg_info_raw(j, x, y, comp); + STBI_FREE(j); + return result; +} +#endif + +// public domain zlib decode v0.2 Sean Barrett 2006-11-18 +// simple implementation +// - all input must be provided in an upfront buffer +// - all output is written to a single output buffer (can malloc/realloc) +// performance +// - fast huffman + +#ifndef STBI_NO_ZLIB + +// fast-way is faster to check than jpeg huffman, but slow way is slower +#define STBI__ZFAST_BITS 9 // accelerate all cases in default tables +#define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1) +#define STBI__ZNSYMS 288 // number of symbols in literal/length alphabet + +// zlib-style huffman encoding +// (jpegs packs from left, zlib from right, so can't share code) +typedef struct +{ + stbi__uint16 fast[1 << STBI__ZFAST_BITS]; + stbi__uint16 firstcode[16]; + int maxcode[17]; + stbi__uint16 firstsymbol[16]; + stbi_uc size[STBI__ZNSYMS]; + stbi__uint16 value[STBI__ZNSYMS]; +} stbi__zhuffman; + +stbi_inline static int stbi__bitreverse16(int n) +{ + n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1); + n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2); + n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4); + n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8); + return n; +} + +stbi_inline static int stbi__bit_reverse(int v, int bits) +{ + STBI_ASSERT(bits <= 16); + // to bit reverse n bits, reverse 16 and shift + // e.g. 11 bits, bit reverse and shift away 5 + return stbi__bitreverse16(v) >> (16-bits); +} + +static int stbi__zbuild_huffman(stbi__zhuffman *z, const stbi_uc *sizelist, int num) +{ + int i,k=0; + int code, next_code[16], sizes[17]; + + // DEFLATE spec for generating codes + memset(sizes, 0, sizeof(sizes)); + memset(z->fast, 0, sizeof(z->fast)); + for (i=0; i < num; ++i) + ++sizes[sizelist[i]]; + sizes[0] = 0; + for (i=1; i < 16; ++i) + if (sizes[i] > (1 << i)) + return stbi__err("bad sizes", "Corrupt PNG"); + code = 0; + for (i=1; i < 16; ++i) { + next_code[i] = code; + z->firstcode[i] = (stbi__uint16) code; + z->firstsymbol[i] = (stbi__uint16) k; + code = (code + sizes[i]); + if (sizes[i]) + if (code-1 >= (1 << i)) return stbi__err("bad codelengths","Corrupt PNG"); + z->maxcode[i] = code << (16-i); // preshift for inner loop + code <<= 1; + k += sizes[i]; + } + z->maxcode[16] = 0x10000; // sentinel + for (i=0; i < num; ++i) { + int s = sizelist[i]; + if (s) { + int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s]; + stbi__uint16 fastv = (stbi__uint16) ((s << 9) | i); + z->size [c] = (stbi_uc ) s; + z->value[c] = (stbi__uint16) i; + if (s <= STBI__ZFAST_BITS) { + int j = stbi__bit_reverse(next_code[s],s); + while (j < (1 << STBI__ZFAST_BITS)) { + z->fast[j] = fastv; + j += (1 << s); + } + } + ++next_code[s]; + } + } + return 1; +} + +// zlib-from-memory implementation for PNG reading +// because PNG allows splitting the zlib stream arbitrarily, +// and it's annoying structurally to have PNG call ZLIB call PNG, +// we require PNG read all the IDATs and combine them into a single +// memory buffer + +typedef struct +{ + stbi_uc *zbuffer, *zbuffer_end; + int num_bits; + int hit_zeof_once; + stbi__uint32 code_buffer; + + char *zout; + char *zout_start; + char *zout_end; + int z_expandable; + + stbi__zhuffman z_length, z_distance; +} stbi__zbuf; + +stbi_inline static int stbi__zeof(stbi__zbuf *z) +{ + return (z->zbuffer >= z->zbuffer_end); +} + +stbi_inline static stbi_uc stbi__zget8(stbi__zbuf *z) +{ + return stbi__zeof(z) ? 0 : *z->zbuffer++; +} + +static void stbi__fill_bits(stbi__zbuf *z) +{ + do { + if (z->code_buffer >= (1U << z->num_bits)) { + z->zbuffer = z->zbuffer_end; /* treat this as EOF so we fail. */ + return; + } + z->code_buffer |= (unsigned int) stbi__zget8(z) << z->num_bits; + z->num_bits += 8; + } while (z->num_bits <= 24); +} + +stbi_inline static unsigned int stbi__zreceive(stbi__zbuf *z, int n) +{ + unsigned int k; + if (z->num_bits < n) stbi__fill_bits(z); + k = z->code_buffer & ((1 << n) - 1); + z->code_buffer >>= n; + z->num_bits -= n; + return k; +} + +static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z) +{ + int b,s,k; + // not resolved by fast table, so compute it the slow way + // use jpeg approach, which requires MSbits at top + k = stbi__bit_reverse(a->code_buffer, 16); + for (s=STBI__ZFAST_BITS+1; ; ++s) + if (k < z->maxcode[s]) + break; + if (s >= 16) return -1; // invalid code! + // code size is s, so: + b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s]; + if (b >= STBI__ZNSYMS) return -1; // some data was corrupt somewhere! + if (z->size[b] != s) return -1; // was originally an assert, but report failure instead. + a->code_buffer >>= s; + a->num_bits -= s; + return z->value[b]; +} + +stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z) +{ + int b,s; + if (a->num_bits < 16) { + if (stbi__zeof(a)) { + if (!a->hit_zeof_once) { + // This is the first time we hit eof, insert 16 extra padding btis + // to allow us to keep going; if we actually consume any of them + // though, that is invalid data. This is caught later. + a->hit_zeof_once = 1; + a->num_bits += 16; // add 16 implicit zero bits + } else { + // We already inserted our extra 16 padding bits and are again + // out, this stream is actually prematurely terminated. + return -1; + } + } else { + stbi__fill_bits(a); + } + } + b = z->fast[a->code_buffer & STBI__ZFAST_MASK]; + if (b) { + s = b >> 9; + a->code_buffer >>= s; + a->num_bits -= s; + return b & 511; + } + return stbi__zhuffman_decode_slowpath(a, z); +} + +static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room for n bytes +{ + char *q; + unsigned int cur, limit, old_limit; + z->zout = zout; + if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG"); + cur = (unsigned int) (z->zout - z->zout_start); + limit = old_limit = (unsigned) (z->zout_end - z->zout_start); + if (UINT_MAX - cur < (unsigned) n) return stbi__err("outofmem", "Out of memory"); + while (cur + n > limit) { + if(limit > UINT_MAX / 2) return stbi__err("outofmem", "Out of memory"); + limit *= 2; + } + q = (char *) STBI_REALLOC_SIZED(z->zout_start, old_limit, limit); + STBI_NOTUSED(old_limit); + if (q == NULL) return stbi__err("outofmem", "Out of memory"); + z->zout_start = q; + z->zout = q + cur; + z->zout_end = q + limit; + return 1; +} + +static const int stbi__zlength_base[31] = { + 3,4,5,6,7,8,9,10,11,13, + 15,17,19,23,27,31,35,43,51,59, + 67,83,99,115,131,163,195,227,258,0,0 }; + +static const int stbi__zlength_extra[31]= +{ 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 }; + +static const int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, +257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0}; + +static const int stbi__zdist_extra[32] = +{ 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; + +static int stbi__parse_huffman_block(stbi__zbuf *a) +{ + char *zout = a->zout; + for(;;) { + int z = stbi__zhuffman_decode(a, &a->z_length); + if (z < 256) { + if (z < 0) return stbi__err("bad huffman code","Corrupt PNG"); // error in huffman codes + if (zout >= a->zout_end) { + if (!stbi__zexpand(a, zout, 1)) return 0; + zout = a->zout; + } + *zout++ = (char) z; + } else { + stbi_uc *p; + int len,dist; + if (z == 256) { + a->zout = zout; + if (a->hit_zeof_once && a->num_bits < 16) { + // The first time we hit zeof, we inserted 16 extra zero bits into our bit + // buffer so the decoder can just do its speculative decoding. But if we + // actually consumed any of those bits (which is the case when num_bits < 16), + // the stream actually read past the end so it is malformed. + return stbi__err("unexpected end","Corrupt PNG"); + } + return 1; + } + if (z >= 286) return stbi__err("bad huffman code","Corrupt PNG"); // per DEFLATE, length codes 286 and 287 must not appear in compressed data + z -= 257; + len = stbi__zlength_base[z]; + if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]); + z = stbi__zhuffman_decode(a, &a->z_distance); + if (z < 0 || z >= 30) return stbi__err("bad huffman code","Corrupt PNG"); // per DEFLATE, distance codes 30 and 31 must not appear in compressed data + dist = stbi__zdist_base[z]; + if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]); + if (zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG"); + if (len > a->zout_end - zout) { + if (!stbi__zexpand(a, zout, len)) return 0; + zout = a->zout; + } + p = (stbi_uc *) (zout - dist); + if (dist == 1) { // run of one byte; common in images. + stbi_uc v = *p; + if (len) { do *zout++ = v; while (--len); } + } else { + if (len) { do *zout++ = *p++; while (--len); } + } + } + } +} + +static int stbi__compute_huffman_codes(stbi__zbuf *a) +{ + static const stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 }; + stbi__zhuffman z_codelength; + stbi_uc lencodes[286+32+137];//padding for maximum single op + stbi_uc codelength_sizes[19]; + int i,n; + + int hlit = stbi__zreceive(a,5) + 257; + int hdist = stbi__zreceive(a,5) + 1; + int hclen = stbi__zreceive(a,4) + 4; + int ntot = hlit + hdist; + + memset(codelength_sizes, 0, sizeof(codelength_sizes)); + for (i=0; i < hclen; ++i) { + int s = stbi__zreceive(a,3); + codelength_sizes[length_dezigzag[i]] = (stbi_uc) s; + } + if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0; + + n = 0; + while (n < ntot) { + int c = stbi__zhuffman_decode(a, &z_codelength); + if (c < 0 || c >= 19) return stbi__err("bad codelengths", "Corrupt PNG"); + if (c < 16) + lencodes[n++] = (stbi_uc) c; + else { + stbi_uc fill = 0; + if (c == 16) { + c = stbi__zreceive(a,2)+3; + if (n == 0) return stbi__err("bad codelengths", "Corrupt PNG"); + fill = lencodes[n-1]; + } else if (c == 17) { + c = stbi__zreceive(a,3)+3; + } else if (c == 18) { + c = stbi__zreceive(a,7)+11; + } else { + return stbi__err("bad codelengths", "Corrupt PNG"); + } + if (ntot - n < c) return stbi__err("bad codelengths", "Corrupt PNG"); + memset(lencodes+n, fill, c); + n += c; + } + } + if (n != ntot) return stbi__err("bad codelengths","Corrupt PNG"); + if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0; + if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0; + return 1; +} + +static int stbi__parse_uncompressed_block(stbi__zbuf *a) +{ + stbi_uc header[4]; + int len,nlen,k; + if (a->num_bits & 7) + stbi__zreceive(a, a->num_bits & 7); // discard + // drain the bit-packed data into header + k = 0; + while (a->num_bits > 0) { + header[k++] = (stbi_uc) (a->code_buffer & 255); // suppress MSVC run-time check + a->code_buffer >>= 8; + a->num_bits -= 8; + } + if (a->num_bits < 0) return stbi__err("zlib corrupt","Corrupt PNG"); + // now fill header the normal way + while (k < 4) + header[k++] = stbi__zget8(a); + len = header[1] * 256 + header[0]; + nlen = header[3] * 256 + header[2]; + if (nlen != (len ^ 0xffff)) return stbi__err("zlib corrupt","Corrupt PNG"); + if (a->zbuffer + len > a->zbuffer_end) return stbi__err("read past buffer","Corrupt PNG"); + if (a->zout + len > a->zout_end) + if (!stbi__zexpand(a, a->zout, len)) return 0; + memcpy(a->zout, a->zbuffer, len); + a->zbuffer += len; + a->zout += len; + return 1; +} + +static int stbi__parse_zlib_header(stbi__zbuf *a) +{ + int cmf = stbi__zget8(a); + int cm = cmf & 15; + /* int cinfo = cmf >> 4; */ + int flg = stbi__zget8(a); + if (stbi__zeof(a)) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec + if ((cmf*256+flg) % 31 != 0) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec + if (flg & 32) return stbi__err("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png + if (cm != 8) return stbi__err("bad compression","Corrupt PNG"); // DEFLATE required for png + // window = 1 << (8 + cinfo)... but who cares, we fully buffer output + return 1; +} + +static const stbi_uc stbi__zdefault_length[STBI__ZNSYMS] = +{ + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8 +}; +static const stbi_uc stbi__zdefault_distance[32] = +{ + 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5 +}; +/* +Init algorithm: +{ + int i; // use <= to match clearly with spec + for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8; + for ( ; i <= 255; ++i) stbi__zdefault_length[i] = 9; + for ( ; i <= 279; ++i) stbi__zdefault_length[i] = 7; + for ( ; i <= 287; ++i) stbi__zdefault_length[i] = 8; + + for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5; +} +*/ + +static int stbi__parse_zlib(stbi__zbuf *a, int parse_header) +{ + int final, type; + if (parse_header) + if (!stbi__parse_zlib_header(a)) return 0; + a->num_bits = 0; + a->code_buffer = 0; + a->hit_zeof_once = 0; + do { + final = stbi__zreceive(a,1); + type = stbi__zreceive(a,2); + if (type == 0) { + if (!stbi__parse_uncompressed_block(a)) return 0; + } else if (type == 3) { + return 0; + } else { + if (type == 1) { + // use fixed code lengths + if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , STBI__ZNSYMS)) return 0; + if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0; + } else { + if (!stbi__compute_huffman_codes(a)) return 0; + } + if (!stbi__parse_huffman_block(a)) return 0; + } + } while (!final); + return 1; +} + +static int stbi__do_zlib(stbi__zbuf *a, char *obuf, int olen, int exp, int parse_header) +{ + a->zout_start = obuf; + a->zout = obuf; + a->zout_end = obuf + olen; + a->z_expandable = exp; + + return stbi__parse_zlib(a, parse_header); +} + +STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen) +{ + stbi__zbuf a; + char *p = (char *) stbi__malloc(initial_size); + if (p == NULL) return NULL; + a.zbuffer = (stbi_uc *) buffer; + a.zbuffer_end = (stbi_uc *) buffer + len; + if (stbi__do_zlib(&a, p, initial_size, 1, 1)) { + if (outlen) *outlen = (int) (a.zout - a.zout_start); + return a.zout_start; + } else { + STBI_FREE(a.zout_start); + return NULL; + } +} + +STBIDEF char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen) +{ + return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen); +} + +STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header) +{ + stbi__zbuf a; + char *p = (char *) stbi__malloc(initial_size); + if (p == NULL) return NULL; + a.zbuffer = (stbi_uc *) buffer; + a.zbuffer_end = (stbi_uc *) buffer + len; + if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) { + if (outlen) *outlen = (int) (a.zout - a.zout_start); + return a.zout_start; + } else { + STBI_FREE(a.zout_start); + return NULL; + } +} + +STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen) +{ + stbi__zbuf a; + a.zbuffer = (stbi_uc *) ibuffer; + a.zbuffer_end = (stbi_uc *) ibuffer + ilen; + if (stbi__do_zlib(&a, obuffer, olen, 0, 1)) + return (int) (a.zout - a.zout_start); + else + return -1; +} + +STBIDEF char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen) +{ + stbi__zbuf a; + char *p = (char *) stbi__malloc(16384); + if (p == NULL) return NULL; + a.zbuffer = (stbi_uc *) buffer; + a.zbuffer_end = (stbi_uc *) buffer+len; + if (stbi__do_zlib(&a, p, 16384, 1, 0)) { + if (outlen) *outlen = (int) (a.zout - a.zout_start); + return a.zout_start; + } else { + STBI_FREE(a.zout_start); + return NULL; + } +} + +STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen) +{ + stbi__zbuf a; + a.zbuffer = (stbi_uc *) ibuffer; + a.zbuffer_end = (stbi_uc *) ibuffer + ilen; + if (stbi__do_zlib(&a, obuffer, olen, 0, 0)) + return (int) (a.zout - a.zout_start); + else + return -1; +} +#endif + +// public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18 +// simple implementation +// - only 8-bit samples +// - no CRC checking +// - allocates lots of intermediate memory +// - avoids problem of streaming data between subsystems +// - avoids explicit window management +// performance +// - uses stb_zlib, a PD zlib implementation with fast huffman decoding + +#ifndef STBI_NO_PNG +typedef struct +{ + stbi__uint32 length; + stbi__uint32 type; +} stbi__pngchunk; + +static stbi__pngchunk stbi__get_chunk_header(stbi__context *s) +{ + stbi__pngchunk c; + c.length = stbi__get32be(s); + c.type = stbi__get32be(s); + return c; +} + +static int stbi__check_png_header(stbi__context *s) +{ + static const stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 }; + int i; + for (i=0; i < 8; ++i) + if (stbi__get8(s) != png_sig[i]) return stbi__err("bad png sig","Not a PNG"); + return 1; +} + +typedef struct +{ + stbi__context *s; + stbi_uc *idata, *expanded, *out; + int depth; +} stbi__png; + + +enum { + STBI__F_none=0, + STBI__F_sub=1, + STBI__F_up=2, + STBI__F_avg=3, + STBI__F_paeth=4, + // synthetic filter used for first scanline to avoid needing a dummy row of 0s + STBI__F_avg_first +}; + +static stbi_uc first_row_filter[5] = +{ + STBI__F_none, + STBI__F_sub, + STBI__F_none, + STBI__F_avg_first, + STBI__F_sub // Paeth with b=c=0 turns out to be equivalent to sub +}; + +static int stbi__paeth(int a, int b, int c) +{ + // This formulation looks very different from the reference in the PNG spec, but is + // actually equivalent and has favorable data dependencies and admits straightforward + // generation of branch-free code, which helps performance significantly. + int thresh = c*3 - (a + b); + int lo = a < b ? a : b; + int hi = a < b ? b : a; + int t0 = (hi <= thresh) ? lo : c; + int t1 = (thresh <= lo) ? hi : t0; + return t1; +} + +static const stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 }; + +// adds an extra all-255 alpha channel +// dest == src is legal +// img_n must be 1 or 3 +static void stbi__create_png_alpha_expand8(stbi_uc *dest, stbi_uc *src, stbi__uint32 x, int img_n) +{ + int i; + // must process data backwards since we allow dest==src + if (img_n == 1) { + for (i=x-1; i >= 0; --i) { + dest[i*2+1] = 255; + dest[i*2+0] = src[i]; + } + } else { + STBI_ASSERT(img_n == 3); + for (i=x-1; i >= 0; --i) { + dest[i*4+3] = 255; + dest[i*4+2] = src[i*3+2]; + dest[i*4+1] = src[i*3+1]; + dest[i*4+0] = src[i*3+0]; + } + } +} + +// create the png data from post-deflated data +static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y, int depth, int color) +{ + int bytes = (depth == 16 ? 2 : 1); + stbi__context *s = a->s; + stbi__uint32 i,j,stride = x*out_n*bytes; + stbi__uint32 img_len, img_width_bytes; + stbi_uc *filter_buf; + int all_ok = 1; + int k; + int img_n = s->img_n; // copy it into a local for later + + int output_bytes = out_n*bytes; + int filter_bytes = img_n*bytes; + int width = x; + + STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1); + a->out = (stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0); // extra bytes to write off the end into + if (!a->out) return stbi__err("outofmem", "Out of memory"); + + // note: error exits here don't need to clean up a->out individually, + // stbi__do_png always does on error. + if (!stbi__mad3sizes_valid(img_n, x, depth, 7)) return stbi__err("too large", "Corrupt PNG"); + img_width_bytes = (((img_n * x * depth) + 7) >> 3); + if (!stbi__mad2sizes_valid(img_width_bytes, y, img_width_bytes)) return stbi__err("too large", "Corrupt PNG"); + img_len = (img_width_bytes + 1) * y; + + // we used to check for exact match between raw_len and img_len on non-interlaced PNGs, + // but issue #276 reported a PNG in the wild that had extra data at the end (all zeros), + // so just check for raw_len < img_len always. + if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG"); + + // Allocate two scan lines worth of filter workspace buffer. + filter_buf = (stbi_uc *) stbi__malloc_mad2(img_width_bytes, 2, 0); + if (!filter_buf) return stbi__err("outofmem", "Out of memory"); + + // Filtering for low-bit-depth images + if (depth < 8) { + filter_bytes = 1; + width = img_width_bytes; + } + + for (j=0; j < y; ++j) { + // cur/prior filter buffers alternate + stbi_uc *cur = filter_buf + (j & 1)*img_width_bytes; + stbi_uc *prior = filter_buf + (~j & 1)*img_width_bytes; + stbi_uc *dest = a->out + stride*j; + int nk = width * filter_bytes; + int filter = *raw++; + + // check filter type + if (filter > 4) { + all_ok = stbi__err("invalid filter","Corrupt PNG"); + break; + } + + // if first row, use special filter that doesn't sample previous row + if (j == 0) filter = first_row_filter[filter]; + + // perform actual filtering + switch (filter) { + case STBI__F_none: + memcpy(cur, raw, nk); + break; + case STBI__F_sub: + memcpy(cur, raw, filter_bytes); + for (k = filter_bytes; k < nk; ++k) + cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); + break; + case STBI__F_up: + for (k = 0; k < nk; ++k) + cur[k] = STBI__BYTECAST(raw[k] + prior[k]); + break; + case STBI__F_avg: + for (k = 0; k < filter_bytes; ++k) + cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); + for (k = filter_bytes; k < nk; ++k) + cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); + break; + case STBI__F_paeth: + for (k = 0; k < filter_bytes; ++k) + cur[k] = STBI__BYTECAST(raw[k] + prior[k]); // prior[k] == stbi__paeth(0,prior[k],0) + for (k = filter_bytes; k < nk; ++k) + cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes], prior[k], prior[k-filter_bytes])); + break; + case STBI__F_avg_first: + memcpy(cur, raw, filter_bytes); + for (k = filter_bytes; k < nk; ++k) + cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); + break; + } + + raw += nk; + + // expand decoded bits in cur to dest, also adding an extra alpha channel if desired + if (depth < 8) { + stbi_uc scale = (color == 0) ? stbi__depth_scale_table[depth] : 1; // scale grayscale values to 0..255 range + stbi_uc *in = cur; + stbi_uc *out = dest; + stbi_uc inb = 0; + stbi__uint32 nsmp = x*img_n; + + // expand bits to bytes first + if (depth == 4) { + for (i=0; i < nsmp; ++i) { + if ((i & 1) == 0) inb = *in++; + *out++ = scale * (inb >> 4); + inb <<= 4; + } + } else if (depth == 2) { + for (i=0; i < nsmp; ++i) { + if ((i & 3) == 0) inb = *in++; + *out++ = scale * (inb >> 6); + inb <<= 2; + } + } else { + STBI_ASSERT(depth == 1); + for (i=0; i < nsmp; ++i) { + if ((i & 7) == 0) inb = *in++; + *out++ = scale * (inb >> 7); + inb <<= 1; + } + } + + // insert alpha=255 values if desired + if (img_n != out_n) + stbi__create_png_alpha_expand8(dest, dest, x, img_n); + } else if (depth == 8) { + if (img_n == out_n) + memcpy(dest, cur, x*img_n); + else + stbi__create_png_alpha_expand8(dest, cur, x, img_n); + } else if (depth == 16) { + // convert the image data from big-endian to platform-native + stbi__uint16 *dest16 = (stbi__uint16*)dest; + stbi__uint32 nsmp = x*img_n; + + if (img_n == out_n) { + for (i = 0; i < nsmp; ++i, ++dest16, cur += 2) + *dest16 = (cur[0] << 8) | cur[1]; + } else { + STBI_ASSERT(img_n+1 == out_n); + if (img_n == 1) { + for (i = 0; i < x; ++i, dest16 += 2, cur += 2) { + dest16[0] = (cur[0] << 8) | cur[1]; + dest16[1] = 0xffff; + } + } else { + STBI_ASSERT(img_n == 3); + for (i = 0; i < x; ++i, dest16 += 4, cur += 6) { + dest16[0] = (cur[0] << 8) | cur[1]; + dest16[1] = (cur[2] << 8) | cur[3]; + dest16[2] = (cur[4] << 8) | cur[5]; + dest16[3] = 0xffff; + } + } + } + } + } + + STBI_FREE(filter_buf); + if (!all_ok) return 0; + + return 1; +} + +static int stbi__create_png_image(stbi__png *a, stbi_uc *image_data, stbi__uint32 image_data_len, int out_n, int depth, int color, int interlaced) +{ + int bytes = (depth == 16 ? 2 : 1); + int out_bytes = out_n * bytes; + stbi_uc *final; + int p; + if (!interlaced) + return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->s->img_x, a->s->img_y, depth, color); + + // de-interlacing + final = (stbi_uc *) stbi__malloc_mad3(a->s->img_x, a->s->img_y, out_bytes, 0); + if (!final) return stbi__err("outofmem", "Out of memory"); + for (p=0; p < 7; ++p) { + int xorig[] = { 0,4,0,2,0,1,0 }; + int yorig[] = { 0,0,4,0,2,0,1 }; + int xspc[] = { 8,8,4,4,2,2,1 }; + int yspc[] = { 8,8,8,4,4,2,2 }; + int i,j,x,y; + // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1 + x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p]; + y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p]; + if (x && y) { + stbi__uint32 img_len = ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y; + if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) { + STBI_FREE(final); + return 0; + } + for (j=0; j < y; ++j) { + for (i=0; i < x; ++i) { + int out_y = j*yspc[p]+yorig[p]; + int out_x = i*xspc[p]+xorig[p]; + memcpy(final + out_y*a->s->img_x*out_bytes + out_x*out_bytes, + a->out + (j*x+i)*out_bytes, out_bytes); + } + } + STBI_FREE(a->out); + image_data += img_len; + image_data_len -= img_len; + } + } + a->out = final; + + return 1; +} + +static int stbi__compute_transparency(stbi__png *z, stbi_uc tc[3], int out_n) +{ + stbi__context *s = z->s; + stbi__uint32 i, pixel_count = s->img_x * s->img_y; + stbi_uc *p = z->out; + + // compute color-based transparency, assuming we've + // already got 255 as the alpha value in the output + STBI_ASSERT(out_n == 2 || out_n == 4); + + if (out_n == 2) { + for (i=0; i < pixel_count; ++i) { + p[1] = (p[0] == tc[0] ? 0 : 255); + p += 2; + } + } else { + for (i=0; i < pixel_count; ++i) { + if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) + p[3] = 0; + p += 4; + } + } + return 1; +} + +static int stbi__compute_transparency16(stbi__png *z, stbi__uint16 tc[3], int out_n) +{ + stbi__context *s = z->s; + stbi__uint32 i, pixel_count = s->img_x * s->img_y; + stbi__uint16 *p = (stbi__uint16*) z->out; + + // compute color-based transparency, assuming we've + // already got 65535 as the alpha value in the output + STBI_ASSERT(out_n == 2 || out_n == 4); + + if (out_n == 2) { + for (i = 0; i < pixel_count; ++i) { + p[1] = (p[0] == tc[0] ? 0 : 65535); + p += 2; + } + } else { + for (i = 0; i < pixel_count; ++i) { + if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2]) + p[3] = 0; + p += 4; + } + } + return 1; +} + +static int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int pal_img_n) +{ + stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y; + stbi_uc *p, *temp_out, *orig = a->out; + + p = (stbi_uc *) stbi__malloc_mad2(pixel_count, pal_img_n, 0); + if (p == NULL) return stbi__err("outofmem", "Out of memory"); + + // between here and free(out) below, exitting would leak + temp_out = p; + + if (pal_img_n == 3) { + for (i=0; i < pixel_count; ++i) { + int n = orig[i]*4; + p[0] = palette[n ]; + p[1] = palette[n+1]; + p[2] = palette[n+2]; + p += 3; + } + } else { + for (i=0; i < pixel_count; ++i) { + int n = orig[i]*4; + p[0] = palette[n ]; + p[1] = palette[n+1]; + p[2] = palette[n+2]; + p[3] = palette[n+3]; + p += 4; + } + } + STBI_FREE(a->out); + a->out = temp_out; + + STBI_NOTUSED(len); + + return 1; +} + +static int stbi__unpremultiply_on_load_global = 0; +static int stbi__de_iphone_flag_global = 0; + +STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply) +{ + stbi__unpremultiply_on_load_global = flag_true_if_should_unpremultiply; +} + +STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert) +{ + stbi__de_iphone_flag_global = flag_true_if_should_convert; +} + +#ifndef STBI_THREAD_LOCAL +#define stbi__unpremultiply_on_load stbi__unpremultiply_on_load_global +#define stbi__de_iphone_flag stbi__de_iphone_flag_global +#else +static STBI_THREAD_LOCAL int stbi__unpremultiply_on_load_local, stbi__unpremultiply_on_load_set; +static STBI_THREAD_LOCAL int stbi__de_iphone_flag_local, stbi__de_iphone_flag_set; + +STBIDEF void stbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply) +{ + stbi__unpremultiply_on_load_local = flag_true_if_should_unpremultiply; + stbi__unpremultiply_on_load_set = 1; +} + +STBIDEF void stbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert) +{ + stbi__de_iphone_flag_local = flag_true_if_should_convert; + stbi__de_iphone_flag_set = 1; +} + +#define stbi__unpremultiply_on_load (stbi__unpremultiply_on_load_set \ + ? stbi__unpremultiply_on_load_local \ + : stbi__unpremultiply_on_load_global) +#define stbi__de_iphone_flag (stbi__de_iphone_flag_set \ + ? stbi__de_iphone_flag_local \ + : stbi__de_iphone_flag_global) +#endif // STBI_THREAD_LOCAL + +static void stbi__de_iphone(stbi__png *z) +{ + stbi__context *s = z->s; + stbi__uint32 i, pixel_count = s->img_x * s->img_y; + stbi_uc *p = z->out; + + if (s->img_out_n == 3) { // convert bgr to rgb + for (i=0; i < pixel_count; ++i) { + stbi_uc t = p[0]; + p[0] = p[2]; + p[2] = t; + p += 3; + } + } else { + STBI_ASSERT(s->img_out_n == 4); + if (stbi__unpremultiply_on_load) { + // convert bgr to rgb and unpremultiply + for (i=0; i < pixel_count; ++i) { + stbi_uc a = p[3]; + stbi_uc t = p[0]; + if (a) { + stbi_uc half = a / 2; + p[0] = (p[2] * 255 + half) / a; + p[1] = (p[1] * 255 + half) / a; + p[2] = ( t * 255 + half) / a; + } else { + p[0] = p[2]; + p[2] = t; + } + p += 4; + } + } else { + // convert bgr to rgb + for (i=0; i < pixel_count; ++i) { + stbi_uc t = p[0]; + p[0] = p[2]; + p[2] = t; + p += 4; + } + } + } +} + +#define STBI__PNG_TYPE(a,b,c,d) (((unsigned) (a) << 24) + ((unsigned) (b) << 16) + ((unsigned) (c) << 8) + (unsigned) (d)) + +static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) +{ + stbi_uc palette[1024], pal_img_n=0; + stbi_uc has_trans=0, tc[3]={0}; + stbi__uint16 tc16[3]; + stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0; + int first=1,k,interlace=0, color=0, is_iphone=0; + stbi__context *s = z->s; + + z->expanded = NULL; + z->idata = NULL; + z->out = NULL; + + if (!stbi__check_png_header(s)) return 0; + + if (scan == STBI__SCAN_type) return 1; + + for (;;) { + stbi__pngchunk c = stbi__get_chunk_header(s); + switch (c.type) { + case STBI__PNG_TYPE('C','g','B','I'): + is_iphone = 1; + stbi__skip(s, c.length); + break; + case STBI__PNG_TYPE('I','H','D','R'): { + int comp,filter; + if (!first) return stbi__err("multiple IHDR","Corrupt PNG"); + first = 0; + if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG"); + s->img_x = stbi__get32be(s); + s->img_y = stbi__get32be(s); + if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + z->depth = stbi__get8(s); if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return stbi__err("1/2/4/8/16-bit only","PNG not supported: 1/2/4/8/16-bit only"); + color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG"); + if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG"); + if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG"); + comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG"); + filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG"); + interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG"); + if (!s->img_x || !s->img_y) return stbi__err("0-pixel image","Corrupt PNG"); + if (!pal_img_n) { + s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0); + if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode"); + } else { + // if paletted, then pal_n is our final components, and + // img_n is # components to decompress/filter. + s->img_n = 1; + if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large","Corrupt PNG"); + } + // even with SCAN_header, have to scan to see if we have a tRNS + break; + } + + case STBI__PNG_TYPE('P','L','T','E'): { + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if (c.length > 256*3) return stbi__err("invalid PLTE","Corrupt PNG"); + pal_len = c.length / 3; + if (pal_len * 3 != c.length) return stbi__err("invalid PLTE","Corrupt PNG"); + for (i=0; i < pal_len; ++i) { + palette[i*4+0] = stbi__get8(s); + palette[i*4+1] = stbi__get8(s); + palette[i*4+2] = stbi__get8(s); + palette[i*4+3] = 255; + } + break; + } + + case STBI__PNG_TYPE('t','R','N','S'): { + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if (z->idata) return stbi__err("tRNS after IDAT","Corrupt PNG"); + if (pal_img_n) { + if (scan == STBI__SCAN_header) { s->img_n = 4; return 1; } + if (pal_len == 0) return stbi__err("tRNS before PLTE","Corrupt PNG"); + if (c.length > pal_len) return stbi__err("bad tRNS len","Corrupt PNG"); + pal_img_n = 4; + for (i=0; i < c.length; ++i) + palette[i*4+3] = stbi__get8(s); + } else { + if (!(s->img_n & 1)) return stbi__err("tRNS with alpha","Corrupt PNG"); + if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len","Corrupt PNG"); + has_trans = 1; + // non-paletted with tRNS = constant alpha. if header-scanning, we can stop now. + if (scan == STBI__SCAN_header) { ++s->img_n; return 1; } + if (z->depth == 16) { + for (k = 0; k < s->img_n && k < 3; ++k) // extra loop test to suppress false GCC warning + tc16[k] = (stbi__uint16)stbi__get16be(s); // copy the values as-is + } else { + for (k = 0; k < s->img_n && k < 3; ++k) + tc[k] = (stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->depth]; // non 8-bit images will be larger + } + } + break; + } + + case STBI__PNG_TYPE('I','D','A','T'): { + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if (pal_img_n && !pal_len) return stbi__err("no PLTE","Corrupt PNG"); + if (scan == STBI__SCAN_header) { + // header scan definitely stops at first IDAT + if (pal_img_n) + s->img_n = pal_img_n; + return 1; + } + if (c.length > (1u << 30)) return stbi__err("IDAT size limit", "IDAT section larger than 2^30 bytes"); + if ((int)(ioff + c.length) < (int)ioff) return 0; + if (ioff + c.length > idata_limit) { + stbi__uint32 idata_limit_old = idata_limit; + stbi_uc *p; + if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096; + while (ioff + c.length > idata_limit) + idata_limit *= 2; + STBI_NOTUSED(idata_limit_old); + p = (stbi_uc *) STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory"); + z->idata = p; + } + if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata","Corrupt PNG"); + ioff += c.length; + break; + } + + case STBI__PNG_TYPE('I','E','N','D'): { + stbi__uint32 raw_len, bpl; + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if (scan != STBI__SCAN_load) return 1; + if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG"); + // initial guess for decoded data size to avoid unnecessary reallocs + bpl = (s->img_x * z->depth + 7) / 8; // bytes per line, per component + raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */; + z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone); + if (z->expanded == NULL) return 0; // zlib should set error + STBI_FREE(z->idata); z->idata = NULL; + if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans) + s->img_out_n = s->img_n+1; + else + s->img_out_n = s->img_n; + if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0; + if (has_trans) { + if (z->depth == 16) { + if (!stbi__compute_transparency16(z, tc16, s->img_out_n)) return 0; + } else { + if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0; + } + } + if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2) + stbi__de_iphone(z); + if (pal_img_n) { + // pal_img_n == 3 or 4 + s->img_n = pal_img_n; // record the actual colors we had + s->img_out_n = pal_img_n; + if (req_comp >= 3) s->img_out_n = req_comp; + if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n)) + return 0; + } else if (has_trans) { + // non-paletted image with tRNS -> source image has (constant) alpha + ++s->img_n; + } + STBI_FREE(z->expanded); z->expanded = NULL; + // end of PNG chunk, read and skip CRC + stbi__get32be(s); + return 1; + } + + default: + // if critical, fail + if (first) return stbi__err("first not IHDR", "Corrupt PNG"); + if ((c.type & (1 << 29)) == 0) { + #ifndef STBI_NO_FAILURE_STRINGS + // not threadsafe + static char invalid_chunk[] = "XXXX PNG chunk not known"; + invalid_chunk[0] = STBI__BYTECAST(c.type >> 24); + invalid_chunk[1] = STBI__BYTECAST(c.type >> 16); + invalid_chunk[2] = STBI__BYTECAST(c.type >> 8); + invalid_chunk[3] = STBI__BYTECAST(c.type >> 0); + #endif + return stbi__err(invalid_chunk, "PNG not supported: unknown PNG chunk type"); + } + stbi__skip(s, c.length); + break; + } + // end of PNG chunk, read and skip CRC + stbi__get32be(s); + } +} + +static void *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp, stbi__result_info *ri) +{ + void *result=NULL; + if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error"); + if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) { + if (p->depth <= 8) + ri->bits_per_channel = 8; + else if (p->depth == 16) + ri->bits_per_channel = 16; + else + return stbi__errpuc("bad bits_per_channel", "PNG not supported: unsupported color depth"); + result = p->out; + p->out = NULL; + if (req_comp && req_comp != p->s->img_out_n) { + if (ri->bits_per_channel == 8) + result = stbi__convert_format((unsigned char *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y); + else + result = stbi__convert_format16((stbi__uint16 *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y); + p->s->img_out_n = req_comp; + if (result == NULL) return result; + } + *x = p->s->img_x; + *y = p->s->img_y; + if (n) *n = p->s->img_n; + } + STBI_FREE(p->out); p->out = NULL; + STBI_FREE(p->expanded); p->expanded = NULL; + STBI_FREE(p->idata); p->idata = NULL; + + return result; +} + +static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + stbi__png p; + p.s = s; + return stbi__do_png(&p, x,y,comp,req_comp, ri); +} + +static int stbi__png_test(stbi__context *s) +{ + int r; + r = stbi__check_png_header(s); + stbi__rewind(s); + return r; +} + +static int stbi__png_info_raw(stbi__png *p, int *x, int *y, int *comp) +{ + if (!stbi__parse_png_file(p, STBI__SCAN_header, 0)) { + stbi__rewind( p->s ); + return 0; + } + if (x) *x = p->s->img_x; + if (y) *y = p->s->img_y; + if (comp) *comp = p->s->img_n; + return 1; +} + +static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp) +{ + stbi__png p; + p.s = s; + return stbi__png_info_raw(&p, x, y, comp); +} + +static int stbi__png_is16(stbi__context *s) +{ + stbi__png p; + p.s = s; + if (!stbi__png_info_raw(&p, NULL, NULL, NULL)) + return 0; + if (p.depth != 16) { + stbi__rewind(p.s); + return 0; + } + return 1; +} +#endif + +// Microsoft/Windows BMP image + +#ifndef STBI_NO_BMP +static int stbi__bmp_test_raw(stbi__context *s) +{ + int r; + int sz; + if (stbi__get8(s) != 'B') return 0; + if (stbi__get8(s) != 'M') return 0; + stbi__get32le(s); // discard filesize + stbi__get16le(s); // discard reserved + stbi__get16le(s); // discard reserved + stbi__get32le(s); // discard data offset + sz = stbi__get32le(s); + r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124); + return r; +} + +static int stbi__bmp_test(stbi__context *s) +{ + int r = stbi__bmp_test_raw(s); + stbi__rewind(s); + return r; +} + + +// returns 0..31 for the highest set bit +static int stbi__high_bit(unsigned int z) +{ + int n=0; + if (z == 0) return -1; + if (z >= 0x10000) { n += 16; z >>= 16; } + if (z >= 0x00100) { n += 8; z >>= 8; } + if (z >= 0x00010) { n += 4; z >>= 4; } + if (z >= 0x00004) { n += 2; z >>= 2; } + if (z >= 0x00002) { n += 1;/* >>= 1;*/ } + return n; +} + +static int stbi__bitcount(unsigned int a) +{ + a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2 + a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4 + a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits + a = (a + (a >> 8)); // max 16 per 8 bits + a = (a + (a >> 16)); // max 32 per 8 bits + return a & 0xff; +} + +// extract an arbitrarily-aligned N-bit value (N=bits) +// from v, and then make it 8-bits long and fractionally +// extend it to full full range. +static int stbi__shiftsigned(unsigned int v, int shift, int bits) +{ + static unsigned int mul_table[9] = { + 0, + 0xff/*0b11111111*/, 0x55/*0b01010101*/, 0x49/*0b01001001*/, 0x11/*0b00010001*/, + 0x21/*0b00100001*/, 0x41/*0b01000001*/, 0x81/*0b10000001*/, 0x01/*0b00000001*/, + }; + static unsigned int shift_table[9] = { + 0, 0,0,1,0,2,4,6,0, + }; + if (shift < 0) + v <<= -shift; + else + v >>= shift; + STBI_ASSERT(v < 256); + v >>= (8-bits); + STBI_ASSERT(bits >= 0 && bits <= 8); + return (int) ((unsigned) v * mul_table[bits]) >> shift_table[bits]; +} + +typedef struct +{ + int bpp, offset, hsz; + unsigned int mr,mg,mb,ma, all_a; + int extra_read; +} stbi__bmp_data; + +static int stbi__bmp_set_mask_defaults(stbi__bmp_data *info, int compress) +{ + // BI_BITFIELDS specifies masks explicitly, don't override + if (compress == 3) + return 1; + + if (compress == 0) { + if (info->bpp == 16) { + info->mr = 31u << 10; + info->mg = 31u << 5; + info->mb = 31u << 0; + } else if (info->bpp == 32) { + info->mr = 0xffu << 16; + info->mg = 0xffu << 8; + info->mb = 0xffu << 0; + info->ma = 0xffu << 24; + info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 0 + } else { + // otherwise, use defaults, which is all-0 + info->mr = info->mg = info->mb = info->ma = 0; + } + return 1; + } + return 0; // error +} + +static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info) +{ + int hsz; + if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') return stbi__errpuc("not BMP", "Corrupt BMP"); + stbi__get32le(s); // discard filesize + stbi__get16le(s); // discard reserved + stbi__get16le(s); // discard reserved + info->offset = stbi__get32le(s); + info->hsz = hsz = stbi__get32le(s); + info->mr = info->mg = info->mb = info->ma = 0; + info->extra_read = 14; + + if (info->offset < 0) return stbi__errpuc("bad BMP", "bad BMP"); + + if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown"); + if (hsz == 12) { + s->img_x = stbi__get16le(s); + s->img_y = stbi__get16le(s); + } else { + s->img_x = stbi__get32le(s); + s->img_y = stbi__get32le(s); + } + if (stbi__get16le(s) != 1) return stbi__errpuc("bad BMP", "bad BMP"); + info->bpp = stbi__get16le(s); + if (hsz != 12) { + int compress = stbi__get32le(s); + if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE"); + if (compress >= 4) return stbi__errpuc("BMP JPEG/PNG", "BMP type not supported: unsupported compression"); // this includes PNG/JPEG modes + if (compress == 3 && info->bpp != 16 && info->bpp != 32) return stbi__errpuc("bad BMP", "bad BMP"); // bitfields requires 16 or 32 bits/pixel + stbi__get32le(s); // discard sizeof + stbi__get32le(s); // discard hres + stbi__get32le(s); // discard vres + stbi__get32le(s); // discard colorsused + stbi__get32le(s); // discard max important + if (hsz == 40 || hsz == 56) { + if (hsz == 56) { + stbi__get32le(s); + stbi__get32le(s); + stbi__get32le(s); + stbi__get32le(s); + } + if (info->bpp == 16 || info->bpp == 32) { + if (compress == 0) { + stbi__bmp_set_mask_defaults(info, compress); + } else if (compress == 3) { + info->mr = stbi__get32le(s); + info->mg = stbi__get32le(s); + info->mb = stbi__get32le(s); + info->extra_read += 12; + // not documented, but generated by photoshop and handled by mspaint + if (info->mr == info->mg && info->mg == info->mb) { + // ?!?!? + return stbi__errpuc("bad BMP", "bad BMP"); + } + } else + return stbi__errpuc("bad BMP", "bad BMP"); + } + } else { + // V4/V5 header + int i; + if (hsz != 108 && hsz != 124) + return stbi__errpuc("bad BMP", "bad BMP"); + info->mr = stbi__get32le(s); + info->mg = stbi__get32le(s); + info->mb = stbi__get32le(s); + info->ma = stbi__get32le(s); + if (compress != 3) // override mr/mg/mb unless in BI_BITFIELDS mode, as per docs + stbi__bmp_set_mask_defaults(info, compress); + stbi__get32le(s); // discard color space + for (i=0; i < 12; ++i) + stbi__get32le(s); // discard color space parameters + if (hsz == 124) { + stbi__get32le(s); // discard rendering intent + stbi__get32le(s); // discard offset of profile data + stbi__get32le(s); // discard size of profile data + stbi__get32le(s); // discard reserved + } + } + } + return (void *) 1; +} + + +static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + stbi_uc *out; + unsigned int mr=0,mg=0,mb=0,ma=0, all_a; + stbi_uc pal[256][4]; + int psize=0,i,j,width; + int flip_vertically, pad, target; + stbi__bmp_data info; + STBI_NOTUSED(ri); + + info.all_a = 255; + if (stbi__bmp_parse_header(s, &info) == NULL) + return NULL; // error code already set + + flip_vertically = ((int) s->img_y) > 0; + s->img_y = abs((int) s->img_y); + + if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + + mr = info.mr; + mg = info.mg; + mb = info.mb; + ma = info.ma; + all_a = info.all_a; + + if (info.hsz == 12) { + if (info.bpp < 24) + psize = (info.offset - info.extra_read - 24) / 3; + } else { + if (info.bpp < 16) + psize = (info.offset - info.extra_read - info.hsz) >> 2; + } + if (psize == 0) { + // accept some number of extra bytes after the header, but if the offset points either to before + // the header ends or implies a large amount of extra data, reject the file as malformed + int bytes_read_so_far = s->callback_already_read + (int)(s->img_buffer - s->img_buffer_original); + int header_limit = 1024; // max we actually read is below 256 bytes currently. + int extra_data_limit = 256*4; // what ordinarily goes here is a palette; 256 entries*4 bytes is its max size. + if (bytes_read_so_far <= 0 || bytes_read_so_far > header_limit) { + return stbi__errpuc("bad header", "Corrupt BMP"); + } + // we established that bytes_read_so_far is positive and sensible. + // the first half of this test rejects offsets that are either too small positives, or + // negative, and guarantees that info.offset >= bytes_read_so_far > 0. this in turn + // ensures the number computed in the second half of the test can't overflow. + if (info.offset < bytes_read_so_far || info.offset - bytes_read_so_far > extra_data_limit) { + return stbi__errpuc("bad offset", "Corrupt BMP"); + } else { + stbi__skip(s, info.offset - bytes_read_so_far); + } + } + + if (info.bpp == 24 && ma == 0xff000000) + s->img_n = 3; + else + s->img_n = ma ? 4 : 3; + if (req_comp && req_comp >= 3) // we can directly decode 3 or 4 + target = req_comp; + else + target = s->img_n; // if they want monochrome, we'll post-convert + + // sanity-check size + if (!stbi__mad3sizes_valid(target, s->img_x, s->img_y, 0)) + return stbi__errpuc("too large", "Corrupt BMP"); + + out = (stbi_uc *) stbi__malloc_mad3(target, s->img_x, s->img_y, 0); + if (!out) return stbi__errpuc("outofmem", "Out of memory"); + if (info.bpp < 16) { + int z=0; + if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); } + for (i=0; i < psize; ++i) { + pal[i][2] = stbi__get8(s); + pal[i][1] = stbi__get8(s); + pal[i][0] = stbi__get8(s); + if (info.hsz != 12) stbi__get8(s); + pal[i][3] = 255; + } + stbi__skip(s, info.offset - info.extra_read - info.hsz - psize * (info.hsz == 12 ? 3 : 4)); + if (info.bpp == 1) width = (s->img_x + 7) >> 3; + else if (info.bpp == 4) width = (s->img_x + 1) >> 1; + else if (info.bpp == 8) width = s->img_x; + else { STBI_FREE(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); } + pad = (-width)&3; + if (info.bpp == 1) { + for (j=0; j < (int) s->img_y; ++j) { + int bit_offset = 7, v = stbi__get8(s); + for (i=0; i < (int) s->img_x; ++i) { + int color = (v>>bit_offset)&0x1; + out[z++] = pal[color][0]; + out[z++] = pal[color][1]; + out[z++] = pal[color][2]; + if (target == 4) out[z++] = 255; + if (i+1 == (int) s->img_x) break; + if((--bit_offset) < 0) { + bit_offset = 7; + v = stbi__get8(s); + } + } + stbi__skip(s, pad); + } + } else { + for (j=0; j < (int) s->img_y; ++j) { + for (i=0; i < (int) s->img_x; i += 2) { + int v=stbi__get8(s),v2=0; + if (info.bpp == 4) { + v2 = v & 15; + v >>= 4; + } + out[z++] = pal[v][0]; + out[z++] = pal[v][1]; + out[z++] = pal[v][2]; + if (target == 4) out[z++] = 255; + if (i+1 == (int) s->img_x) break; + v = (info.bpp == 8) ? stbi__get8(s) : v2; + out[z++] = pal[v][0]; + out[z++] = pal[v][1]; + out[z++] = pal[v][2]; + if (target == 4) out[z++] = 255; + } + stbi__skip(s, pad); + } + } + } else { + int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0; + int z = 0; + int easy=0; + stbi__skip(s, info.offset - info.extra_read - info.hsz); + if (info.bpp == 24) width = 3 * s->img_x; + else if (info.bpp == 16) width = 2*s->img_x; + else /* bpp = 32 and pad = 0 */ width=0; + pad = (-width) & 3; + if (info.bpp == 24) { + easy = 1; + } else if (info.bpp == 32) { + if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000) + easy = 2; + } + if (!easy) { + if (!mr || !mg || !mb) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); } + // right shift amt to put high bit in position #7 + rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr); + gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg); + bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb); + ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma); + if (rcount > 8 || gcount > 8 || bcount > 8 || acount > 8) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); } + } + for (j=0; j < (int) s->img_y; ++j) { + if (easy) { + for (i=0; i < (int) s->img_x; ++i) { + unsigned char a; + out[z+2] = stbi__get8(s); + out[z+1] = stbi__get8(s); + out[z+0] = stbi__get8(s); + z += 3; + a = (easy == 2 ? stbi__get8(s) : 255); + all_a |= a; + if (target == 4) out[z++] = a; + } + } else { + int bpp = info.bpp; + for (i=0; i < (int) s->img_x; ++i) { + stbi__uint32 v = (bpp == 16 ? (stbi__uint32) stbi__get16le(s) : stbi__get32le(s)); + unsigned int a; + out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount)); + out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount)); + out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount)); + a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255); + all_a |= a; + if (target == 4) out[z++] = STBI__BYTECAST(a); + } + } + stbi__skip(s, pad); + } + } + + // if alpha channel is all 0s, replace with all 255s + if (target == 4 && all_a == 0) + for (i=4*s->img_x*s->img_y-1; i >= 0; i -= 4) + out[i] = 255; + + if (flip_vertically) { + stbi_uc t; + for (j=0; j < (int) s->img_y>>1; ++j) { + stbi_uc *p1 = out + j *s->img_x*target; + stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target; + for (i=0; i < (int) s->img_x*target; ++i) { + t = p1[i]; p1[i] = p2[i]; p2[i] = t; + } + } + } + + if (req_comp && req_comp != target) { + out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y); + if (out == NULL) return out; // stbi__convert_format frees input on failure + } + + *x = s->img_x; + *y = s->img_y; + if (comp) *comp = s->img_n; + return out; +} +#endif + +// Targa Truevision - TGA +// by Jonathan Dummer +#ifndef STBI_NO_TGA +// returns STBI_rgb or whatever, 0 on error +static int stbi__tga_get_comp(int bits_per_pixel, int is_grey, int* is_rgb16) +{ + // only RGB or RGBA (incl. 16bit) or grey allowed + if (is_rgb16) *is_rgb16 = 0; + switch(bits_per_pixel) { + case 8: return STBI_grey; + case 16: if(is_grey) return STBI_grey_alpha; + // fallthrough + case 15: if(is_rgb16) *is_rgb16 = 1; + return STBI_rgb; + case 24: // fallthrough + case 32: return bits_per_pixel/8; + default: return 0; + } +} + +static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp) +{ + int tga_w, tga_h, tga_comp, tga_image_type, tga_bits_per_pixel, tga_colormap_bpp; + int sz, tga_colormap_type; + stbi__get8(s); // discard Offset + tga_colormap_type = stbi__get8(s); // colormap type + if( tga_colormap_type > 1 ) { + stbi__rewind(s); + return 0; // only RGB or indexed allowed + } + tga_image_type = stbi__get8(s); // image type + if ( tga_colormap_type == 1 ) { // colormapped (paletted) image + if (tga_image_type != 1 && tga_image_type != 9) { + stbi__rewind(s); + return 0; + } + stbi__skip(s,4); // skip index of first colormap entry and number of entries + sz = stbi__get8(s); // check bits per palette color entry + if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) { + stbi__rewind(s); + return 0; + } + stbi__skip(s,4); // skip image x and y origin + tga_colormap_bpp = sz; + } else { // "normal" image w/o colormap - only RGB or grey allowed, +/- RLE + if ( (tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11) ) { + stbi__rewind(s); + return 0; // only RGB or grey allowed, +/- RLE + } + stbi__skip(s,9); // skip colormap specification and image x/y origin + tga_colormap_bpp = 0; + } + tga_w = stbi__get16le(s); + if( tga_w < 1 ) { + stbi__rewind(s); + return 0; // test width + } + tga_h = stbi__get16le(s); + if( tga_h < 1 ) { + stbi__rewind(s); + return 0; // test height + } + tga_bits_per_pixel = stbi__get8(s); // bits per pixel + stbi__get8(s); // ignore alpha bits + if (tga_colormap_bpp != 0) { + if((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16)) { + // when using a colormap, tga_bits_per_pixel is the size of the indexes + // I don't think anything but 8 or 16bit indexes makes sense + stbi__rewind(s); + return 0; + } + tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL); + } else { + tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL); + } + if(!tga_comp) { + stbi__rewind(s); + return 0; + } + if (x) *x = tga_w; + if (y) *y = tga_h; + if (comp) *comp = tga_comp; + return 1; // seems to have passed everything +} + +static int stbi__tga_test(stbi__context *s) +{ + int res = 0; + int sz, tga_color_type; + stbi__get8(s); // discard Offset + tga_color_type = stbi__get8(s); // color type + if ( tga_color_type > 1 ) goto errorEnd; // only RGB or indexed allowed + sz = stbi__get8(s); // image type + if ( tga_color_type == 1 ) { // colormapped (paletted) image + if (sz != 1 && sz != 9) goto errorEnd; // colortype 1 demands image type 1 or 9 + stbi__skip(s,4); // skip index of first colormap entry and number of entries + sz = stbi__get8(s); // check bits per palette color entry + if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd; + stbi__skip(s,4); // skip image x and y origin + } else { // "normal" image w/o colormap + if ( (sz != 2) && (sz != 3) && (sz != 10) && (sz != 11) ) goto errorEnd; // only RGB or grey allowed, +/- RLE + stbi__skip(s,9); // skip colormap specification and image x/y origin + } + if ( stbi__get16le(s) < 1 ) goto errorEnd; // test width + if ( stbi__get16le(s) < 1 ) goto errorEnd; // test height + sz = stbi__get8(s); // bits per pixel + if ( (tga_color_type == 1) && (sz != 8) && (sz != 16) ) goto errorEnd; // for colormapped images, bpp is size of an index + if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd; + + res = 1; // if we got this far, everything's good and we can return 1 instead of 0 + +errorEnd: + stbi__rewind(s); + return res; +} + +// read 16bit value and convert to 24bit RGB +static void stbi__tga_read_rgb16(stbi__context *s, stbi_uc* out) +{ + stbi__uint16 px = (stbi__uint16)stbi__get16le(s); + stbi__uint16 fiveBitMask = 31; + // we have 3 channels with 5bits each + int r = (px >> 10) & fiveBitMask; + int g = (px >> 5) & fiveBitMask; + int b = px & fiveBitMask; + // Note that this saves the data in RGB(A) order, so it doesn't need to be swapped later + out[0] = (stbi_uc)((r * 255)/31); + out[1] = (stbi_uc)((g * 255)/31); + out[2] = (stbi_uc)((b * 255)/31); + + // some people claim that the most significant bit might be used for alpha + // (possibly if an alpha-bit is set in the "image descriptor byte") + // but that only made 16bit test images completely translucent.. + // so let's treat all 15 and 16bit TGAs as RGB with no alpha. +} + +static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + // read in the TGA header stuff + int tga_offset = stbi__get8(s); + int tga_indexed = stbi__get8(s); + int tga_image_type = stbi__get8(s); + int tga_is_RLE = 0; + int tga_palette_start = stbi__get16le(s); + int tga_palette_len = stbi__get16le(s); + int tga_palette_bits = stbi__get8(s); + int tga_x_origin = stbi__get16le(s); + int tga_y_origin = stbi__get16le(s); + int tga_width = stbi__get16le(s); + int tga_height = stbi__get16le(s); + int tga_bits_per_pixel = stbi__get8(s); + int tga_comp, tga_rgb16=0; + int tga_inverted = stbi__get8(s); + // int tga_alpha_bits = tga_inverted & 15; // the 4 lowest bits - unused (useless?) + // image data + unsigned char *tga_data; + unsigned char *tga_palette = NULL; + int i, j; + unsigned char raw_data[4] = {0}; + int RLE_count = 0; + int RLE_repeating = 0; + int read_next_pixel = 1; + STBI_NOTUSED(ri); + STBI_NOTUSED(tga_x_origin); // @TODO + STBI_NOTUSED(tga_y_origin); // @TODO + + if (tga_height > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (tga_width > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + + // do a tiny bit of precessing + if ( tga_image_type >= 8 ) + { + tga_image_type -= 8; + tga_is_RLE = 1; + } + tga_inverted = 1 - ((tga_inverted >> 5) & 1); + + // If I'm paletted, then I'll use the number of bits from the palette + if ( tga_indexed ) tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16); + else tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3), &tga_rgb16); + + if(!tga_comp) // shouldn't really happen, stbi__tga_test() should have ensured basic consistency + return stbi__errpuc("bad format", "Can't find out TGA pixelformat"); + + // tga info + *x = tga_width; + *y = tga_height; + if (comp) *comp = tga_comp; + + if (!stbi__mad3sizes_valid(tga_width, tga_height, tga_comp, 0)) + return stbi__errpuc("too large", "Corrupt TGA"); + + tga_data = (unsigned char*)stbi__malloc_mad3(tga_width, tga_height, tga_comp, 0); + if (!tga_data) return stbi__errpuc("outofmem", "Out of memory"); + + // skip to the data's starting position (offset usually = 0) + stbi__skip(s, tga_offset ); + + if ( !tga_indexed && !tga_is_RLE && !tga_rgb16 ) { + for (i=0; i < tga_height; ++i) { + int row = tga_inverted ? tga_height -i - 1 : i; + stbi_uc *tga_row = tga_data + row*tga_width*tga_comp; + stbi__getn(s, tga_row, tga_width * tga_comp); + } + } else { + // do I need to load a palette? + if ( tga_indexed) + { + if (tga_palette_len == 0) { /* you have to have at least one entry! */ + STBI_FREE(tga_data); + return stbi__errpuc("bad palette", "Corrupt TGA"); + } + + // any data to skip? (offset usually = 0) + stbi__skip(s, tga_palette_start ); + // load the palette + tga_palette = (unsigned char*)stbi__malloc_mad2(tga_palette_len, tga_comp, 0); + if (!tga_palette) { + STBI_FREE(tga_data); + return stbi__errpuc("outofmem", "Out of memory"); + } + if (tga_rgb16) { + stbi_uc *pal_entry = tga_palette; + STBI_ASSERT(tga_comp == STBI_rgb); + for (i=0; i < tga_palette_len; ++i) { + stbi__tga_read_rgb16(s, pal_entry); + pal_entry += tga_comp; + } + } else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) { + STBI_FREE(tga_data); + STBI_FREE(tga_palette); + return stbi__errpuc("bad palette", "Corrupt TGA"); + } + } + // load the data + for (i=0; i < tga_width * tga_height; ++i) + { + // if I'm in RLE mode, do I need to get a RLE stbi__pngchunk? + if ( tga_is_RLE ) + { + if ( RLE_count == 0 ) + { + // yep, get the next byte as a RLE command + int RLE_cmd = stbi__get8(s); + RLE_count = 1 + (RLE_cmd & 127); + RLE_repeating = RLE_cmd >> 7; + read_next_pixel = 1; + } else if ( !RLE_repeating ) + { + read_next_pixel = 1; + } + } else + { + read_next_pixel = 1; + } + // OK, if I need to read a pixel, do it now + if ( read_next_pixel ) + { + // load however much data we did have + if ( tga_indexed ) + { + // read in index, then perform the lookup + int pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s) : stbi__get16le(s); + if ( pal_idx >= tga_palette_len ) { + // invalid index + pal_idx = 0; + } + pal_idx *= tga_comp; + for (j = 0; j < tga_comp; ++j) { + raw_data[j] = tga_palette[pal_idx+j]; + } + } else if(tga_rgb16) { + STBI_ASSERT(tga_comp == STBI_rgb); + stbi__tga_read_rgb16(s, raw_data); + } else { + // read in the data raw + for (j = 0; j < tga_comp; ++j) { + raw_data[j] = stbi__get8(s); + } + } + // clear the reading flag for the next pixel + read_next_pixel = 0; + } // end of reading a pixel + + // copy data + for (j = 0; j < tga_comp; ++j) + tga_data[i*tga_comp+j] = raw_data[j]; + + // in case we're in RLE mode, keep counting down + --RLE_count; + } + // do I need to invert the image? + if ( tga_inverted ) + { + for (j = 0; j*2 < tga_height; ++j) + { + int index1 = j * tga_width * tga_comp; + int index2 = (tga_height - 1 - j) * tga_width * tga_comp; + for (i = tga_width * tga_comp; i > 0; --i) + { + unsigned char temp = tga_data[index1]; + tga_data[index1] = tga_data[index2]; + tga_data[index2] = temp; + ++index1; + ++index2; + } + } + } + // clear my palette, if I had one + if ( tga_palette != NULL ) + { + STBI_FREE( tga_palette ); + } + } + + // swap RGB - if the source data was RGB16, it already is in the right order + if (tga_comp >= 3 && !tga_rgb16) + { + unsigned char* tga_pixel = tga_data; + for (i=0; i < tga_width * tga_height; ++i) + { + unsigned char temp = tga_pixel[0]; + tga_pixel[0] = tga_pixel[2]; + tga_pixel[2] = temp; + tga_pixel += tga_comp; + } + } + + // convert to target component count + if (req_comp && req_comp != tga_comp) + tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height); + + // the things I do to get rid of an error message, and yet keep + // Microsoft's C compilers happy... [8^( + tga_palette_start = tga_palette_len = tga_palette_bits = + tga_x_origin = tga_y_origin = 0; + STBI_NOTUSED(tga_palette_start); + // OK, done + return tga_data; +} +#endif + +// ************************************************************************************************* +// Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB + +#ifndef STBI_NO_PSD +static int stbi__psd_test(stbi__context *s) +{ + int r = (stbi__get32be(s) == 0x38425053); + stbi__rewind(s); + return r; +} + +static int stbi__psd_decode_rle(stbi__context *s, stbi_uc *p, int pixelCount) +{ + int count, nleft, len; + + count = 0; + while ((nleft = pixelCount - count) > 0) { + len = stbi__get8(s); + if (len == 128) { + // No-op. + } else if (len < 128) { + // Copy next len+1 bytes literally. + len++; + if (len > nleft) return 0; // corrupt data + count += len; + while (len) { + *p = stbi__get8(s); + p += 4; + len--; + } + } else if (len > 128) { + stbi_uc val; + // Next -len+1 bytes in the dest are replicated from next source byte. + // (Interpret len as a negative 8-bit int.) + len = 257 - len; + if (len > nleft) return 0; // corrupt data + val = stbi__get8(s); + count += len; + while (len) { + *p = val; + p += 4; + len--; + } + } + } + + return 1; +} + +static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc) +{ + int pixelCount; + int channelCount, compression; + int channel, i; + int bitdepth; + int w,h; + stbi_uc *out; + STBI_NOTUSED(ri); + + // Check identifier + if (stbi__get32be(s) != 0x38425053) // "8BPS" + return stbi__errpuc("not PSD", "Corrupt PSD image"); + + // Check file type version. + if (stbi__get16be(s) != 1) + return stbi__errpuc("wrong version", "Unsupported version of PSD image"); + + // Skip 6 reserved bytes. + stbi__skip(s, 6 ); + + // Read the number of channels (R, G, B, A, etc). + channelCount = stbi__get16be(s); + if (channelCount < 0 || channelCount > 16) + return stbi__errpuc("wrong channel count", "Unsupported number of channels in PSD image"); + + // Read the rows and columns of the image. + h = stbi__get32be(s); + w = stbi__get32be(s); + + if (h > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (w > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + + // Make sure the depth is 8 bits. + bitdepth = stbi__get16be(s); + if (bitdepth != 8 && bitdepth != 16) + return stbi__errpuc("unsupported bit depth", "PSD bit depth is not 8 or 16 bit"); + + // Make sure the color mode is RGB. + // Valid options are: + // 0: Bitmap + // 1: Grayscale + // 2: Indexed color + // 3: RGB color + // 4: CMYK color + // 7: Multichannel + // 8: Duotone + // 9: Lab color + if (stbi__get16be(s) != 3) + return stbi__errpuc("wrong color format", "PSD is not in RGB color format"); + + // Skip the Mode Data. (It's the palette for indexed color; other info for other modes.) + stbi__skip(s,stbi__get32be(s) ); + + // Skip the image resources. (resolution, pen tool paths, etc) + stbi__skip(s, stbi__get32be(s) ); + + // Skip the reserved data. + stbi__skip(s, stbi__get32be(s) ); + + // Find out if the data is compressed. + // Known values: + // 0: no compression + // 1: RLE compressed + compression = stbi__get16be(s); + if (compression > 1) + return stbi__errpuc("bad compression", "PSD has an unknown compression format"); + + // Check size + if (!stbi__mad3sizes_valid(4, w, h, 0)) + return stbi__errpuc("too large", "Corrupt PSD"); + + // Create the destination image. + + if (!compression && bitdepth == 16 && bpc == 16) { + out = (stbi_uc *) stbi__malloc_mad3(8, w, h, 0); + ri->bits_per_channel = 16; + } else + out = (stbi_uc *) stbi__malloc(4 * w*h); + + if (!out) return stbi__errpuc("outofmem", "Out of memory"); + pixelCount = w*h; + + // Initialize the data to zero. + //memset( out, 0, pixelCount * 4 ); + + // Finally, the image data. + if (compression) { + // RLE as used by .PSD and .TIFF + // Loop until you get the number of unpacked bytes you are expecting: + // Read the next source byte into n. + // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally. + // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times. + // Else if n is 128, noop. + // Endloop + + // The RLE-compressed data is preceded by a 2-byte data count for each row in the data, + // which we're going to just skip. + stbi__skip(s, h * channelCount * 2 ); + + // Read the RLE data by channel. + for (channel = 0; channel < 4; channel++) { + stbi_uc *p; + + p = out+channel; + if (channel >= channelCount) { + // Fill this channel with default data. + for (i = 0; i < pixelCount; i++, p += 4) + *p = (channel == 3 ? 255 : 0); + } else { + // Read the RLE data. + if (!stbi__psd_decode_rle(s, p, pixelCount)) { + STBI_FREE(out); + return stbi__errpuc("corrupt", "bad RLE data"); + } + } + } + + } else { + // We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...) + // where each channel consists of an 8-bit (or 16-bit) value for each pixel in the image. + + // Read the data by channel. + for (channel = 0; channel < 4; channel++) { + if (channel >= channelCount) { + // Fill this channel with default data. + if (bitdepth == 16 && bpc == 16) { + stbi__uint16 *q = ((stbi__uint16 *) out) + channel; + stbi__uint16 val = channel == 3 ? 65535 : 0; + for (i = 0; i < pixelCount; i++, q += 4) + *q = val; + } else { + stbi_uc *p = out+channel; + stbi_uc val = channel == 3 ? 255 : 0; + for (i = 0; i < pixelCount; i++, p += 4) + *p = val; + } + } else { + if (ri->bits_per_channel == 16) { // output bpc + stbi__uint16 *q = ((stbi__uint16 *) out) + channel; + for (i = 0; i < pixelCount; i++, q += 4) + *q = (stbi__uint16) stbi__get16be(s); + } else { + stbi_uc *p = out+channel; + if (bitdepth == 16) { // input bpc + for (i = 0; i < pixelCount; i++, p += 4) + *p = (stbi_uc) (stbi__get16be(s) >> 8); + } else { + for (i = 0; i < pixelCount; i++, p += 4) + *p = stbi__get8(s); + } + } + } + } + } + + // remove weird white matte from PSD + if (channelCount >= 4) { + if (ri->bits_per_channel == 16) { + for (i=0; i < w*h; ++i) { + stbi__uint16 *pixel = (stbi__uint16 *) out + 4*i; + if (pixel[3] != 0 && pixel[3] != 65535) { + float a = pixel[3] / 65535.0f; + float ra = 1.0f / a; + float inv_a = 65535.0f * (1 - ra); + pixel[0] = (stbi__uint16) (pixel[0]*ra + inv_a); + pixel[1] = (stbi__uint16) (pixel[1]*ra + inv_a); + pixel[2] = (stbi__uint16) (pixel[2]*ra + inv_a); + } + } + } else { + for (i=0; i < w*h; ++i) { + unsigned char *pixel = out + 4*i; + if (pixel[3] != 0 && pixel[3] != 255) { + float a = pixel[3] / 255.0f; + float ra = 1.0f / a; + float inv_a = 255.0f * (1 - ra); + pixel[0] = (unsigned char) (pixel[0]*ra + inv_a); + pixel[1] = (unsigned char) (pixel[1]*ra + inv_a); + pixel[2] = (unsigned char) (pixel[2]*ra + inv_a); + } + } + } + } + + // convert to desired output format + if (req_comp && req_comp != 4) { + if (ri->bits_per_channel == 16) + out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, 4, req_comp, w, h); + else + out = stbi__convert_format(out, 4, req_comp, w, h); + if (out == NULL) return out; // stbi__convert_format frees input on failure + } + + if (comp) *comp = 4; + *y = h; + *x = w; + + return out; +} +#endif + +// ************************************************************************************************* +// Softimage PIC loader +// by Tom Seddon +// +// See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format +// See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/ + +#ifndef STBI_NO_PIC +static int stbi__pic_is4(stbi__context *s,const char *str) +{ + int i; + for (i=0; i<4; ++i) + if (stbi__get8(s) != (stbi_uc)str[i]) + return 0; + + return 1; +} + +static int stbi__pic_test_core(stbi__context *s) +{ + int i; + + if (!stbi__pic_is4(s,"\x53\x80\xF6\x34")) + return 0; + + for(i=0;i<84;++i) + stbi__get8(s); + + if (!stbi__pic_is4(s,"PICT")) + return 0; + + return 1; +} + +typedef struct +{ + stbi_uc size,type,channel; +} stbi__pic_packet; + +static stbi_uc *stbi__readval(stbi__context *s, int channel, stbi_uc *dest) +{ + int mask=0x80, i; + + for (i=0; i<4; ++i, mask>>=1) { + if (channel & mask) { + if (stbi__at_eof(s)) return stbi__errpuc("bad file","PIC file too short"); + dest[i]=stbi__get8(s); + } + } + + return dest; +} + +static void stbi__copyval(int channel,stbi_uc *dest,const stbi_uc *src) +{ + int mask=0x80,i; + + for (i=0;i<4; ++i, mask>>=1) + if (channel&mask) + dest[i]=src[i]; +} + +static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result) +{ + int act_comp=0,num_packets=0,y,chained; + stbi__pic_packet packets[10]; + + // this will (should...) cater for even some bizarre stuff like having data + // for the same channel in multiple packets. + do { + stbi__pic_packet *packet; + + if (num_packets==sizeof(packets)/sizeof(packets[0])) + return stbi__errpuc("bad format","too many packets"); + + packet = &packets[num_packets++]; + + chained = stbi__get8(s); + packet->size = stbi__get8(s); + packet->type = stbi__get8(s); + packet->channel = stbi__get8(s); + + act_comp |= packet->channel; + + if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (reading packets)"); + if (packet->size != 8) return stbi__errpuc("bad format","packet isn't 8bpp"); + } while (chained); + + *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel? + + for(y=0; ytype) { + default: + return stbi__errpuc("bad format","packet has bad compression type"); + + case 0: {//uncompressed + int x; + + for(x=0;xchannel,dest)) + return 0; + break; + } + + case 1://Pure RLE + { + int left=width, i; + + while (left>0) { + stbi_uc count,value[4]; + + count=stbi__get8(s); + if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pure read count)"); + + if (count > left) + count = (stbi_uc) left; + + if (!stbi__readval(s,packet->channel,value)) return 0; + + for(i=0; ichannel,dest,value); + left -= count; + } + } + break; + + case 2: {//Mixed RLE + int left=width; + while (left>0) { + int count = stbi__get8(s), i; + if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (mixed read count)"); + + if (count >= 128) { // Repeated + stbi_uc value[4]; + + if (count==128) + count = stbi__get16be(s); + else + count -= 127; + if (count > left) + return stbi__errpuc("bad file","scanline overrun"); + + if (!stbi__readval(s,packet->channel,value)) + return 0; + + for(i=0;ichannel,dest,value); + } else { // Raw + ++count; + if (count>left) return stbi__errpuc("bad file","scanline overrun"); + + for(i=0;ichannel,dest)) + return 0; + } + left-=count; + } + break; + } + } + } + } + + return result; +} + +static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_comp, stbi__result_info *ri) +{ + stbi_uc *result; + int i, x,y, internal_comp; + STBI_NOTUSED(ri); + + if (!comp) comp = &internal_comp; + + for (i=0; i<92; ++i) + stbi__get8(s); + + x = stbi__get16be(s); + y = stbi__get16be(s); + + if (y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + + if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pic header)"); + if (!stbi__mad3sizes_valid(x, y, 4, 0)) return stbi__errpuc("too large", "PIC image too large to decode"); + + stbi__get32be(s); //skip `ratio' + stbi__get16be(s); //skip `fields' + stbi__get16be(s); //skip `pad' + + // intermediate buffer is RGBA + result = (stbi_uc *) stbi__malloc_mad3(x, y, 4, 0); + if (!result) return stbi__errpuc("outofmem", "Out of memory"); + memset(result, 0xff, x*y*4); + + if (!stbi__pic_load_core(s,x,y,comp, result)) { + STBI_FREE(result); + result=0; + } + *px = x; + *py = y; + if (req_comp == 0) req_comp = *comp; + result=stbi__convert_format(result,4,req_comp,x,y); + + return result; +} + +static int stbi__pic_test(stbi__context *s) +{ + int r = stbi__pic_test_core(s); + stbi__rewind(s); + return r; +} +#endif + +// ************************************************************************************************* +// GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb + +#ifndef STBI_NO_GIF +typedef struct +{ + stbi__int16 prefix; + stbi_uc first; + stbi_uc suffix; +} stbi__gif_lzw; + +typedef struct +{ + int w,h; + stbi_uc *out; // output buffer (always 4 components) + stbi_uc *background; // The current "background" as far as a gif is concerned + stbi_uc *history; + int flags, bgindex, ratio, transparent, eflags; + stbi_uc pal[256][4]; + stbi_uc lpal[256][4]; + stbi__gif_lzw codes[8192]; + stbi_uc *color_table; + int parse, step; + int lflags; + int start_x, start_y; + int max_x, max_y; + int cur_x, cur_y; + int line_size; + int delay; +} stbi__gif; + +static int stbi__gif_test_raw(stbi__context *s) +{ + int sz; + if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') return 0; + sz = stbi__get8(s); + if (sz != '9' && sz != '7') return 0; + if (stbi__get8(s) != 'a') return 0; + return 1; +} + +static int stbi__gif_test(stbi__context *s) +{ + int r = stbi__gif_test_raw(s); + stbi__rewind(s); + return r; +} + +static void stbi__gif_parse_colortable(stbi__context *s, stbi_uc pal[256][4], int num_entries, int transp) +{ + int i; + for (i=0; i < num_entries; ++i) { + pal[i][2] = stbi__get8(s); + pal[i][1] = stbi__get8(s); + pal[i][0] = stbi__get8(s); + pal[i][3] = transp == i ? 0 : 255; + } +} + +static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_info) +{ + stbi_uc version; + if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') + return stbi__err("not GIF", "Corrupt GIF"); + + version = stbi__get8(s); + if (version != '7' && version != '9') return stbi__err("not GIF", "Corrupt GIF"); + if (stbi__get8(s) != 'a') return stbi__err("not GIF", "Corrupt GIF"); + + stbi__g_failure_reason = ""; + g->w = stbi__get16le(s); + g->h = stbi__get16le(s); + g->flags = stbi__get8(s); + g->bgindex = stbi__get8(s); + g->ratio = stbi__get8(s); + g->transparent = -1; + + if (g->w > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + if (g->h > STBI_MAX_DIMENSIONS) return stbi__err("too large","Very large image (corrupt?)"); + + if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments + + if (is_info) return 1; + + if (g->flags & 0x80) + stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1); + + return 1; +} + +static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp) +{ + stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif)); + if (!g) return stbi__err("outofmem", "Out of memory"); + if (!stbi__gif_header(s, g, comp, 1)) { + STBI_FREE(g); + stbi__rewind( s ); + return 0; + } + if (x) *x = g->w; + if (y) *y = g->h; + STBI_FREE(g); + return 1; +} + +static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code) +{ + stbi_uc *p, *c; + int idx; + + // recurse to decode the prefixes, since the linked-list is backwards, + // and working backwards through an interleaved image would be nasty + if (g->codes[code].prefix >= 0) + stbi__out_gif_code(g, g->codes[code].prefix); + + if (g->cur_y >= g->max_y) return; + + idx = g->cur_x + g->cur_y; + p = &g->out[idx]; + g->history[idx / 4] = 1; + + c = &g->color_table[g->codes[code].suffix * 4]; + if (c[3] > 128) { // don't render transparent pixels; + p[0] = c[2]; + p[1] = c[1]; + p[2] = c[0]; + p[3] = c[3]; + } + g->cur_x += 4; + + if (g->cur_x >= g->max_x) { + g->cur_x = g->start_x; + g->cur_y += g->step; + + while (g->cur_y >= g->max_y && g->parse > 0) { + g->step = (1 << g->parse) * g->line_size; + g->cur_y = g->start_y + (g->step >> 1); + --g->parse; + } + } +} + +static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g) +{ + stbi_uc lzw_cs; + stbi__int32 len, init_code; + stbi__uint32 first; + stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear; + stbi__gif_lzw *p; + + lzw_cs = stbi__get8(s); + if (lzw_cs > 12) return NULL; + clear = 1 << lzw_cs; + first = 1; + codesize = lzw_cs + 1; + codemask = (1 << codesize) - 1; + bits = 0; + valid_bits = 0; + for (init_code = 0; init_code < clear; init_code++) { + g->codes[init_code].prefix = -1; + g->codes[init_code].first = (stbi_uc) init_code; + g->codes[init_code].suffix = (stbi_uc) init_code; + } + + // support no starting clear code + avail = clear+2; + oldcode = -1; + + len = 0; + for(;;) { + if (valid_bits < codesize) { + if (len == 0) { + len = stbi__get8(s); // start new block + if (len == 0) + return g->out; + } + --len; + bits |= (stbi__int32) stbi__get8(s) << valid_bits; + valid_bits += 8; + } else { + stbi__int32 code = bits & codemask; + bits >>= codesize; + valid_bits -= codesize; + // @OPTIMIZE: is there some way we can accelerate the non-clear path? + if (code == clear) { // clear code + codesize = lzw_cs + 1; + codemask = (1 << codesize) - 1; + avail = clear + 2; + oldcode = -1; + first = 0; + } else if (code == clear + 1) { // end of stream code + stbi__skip(s, len); + while ((len = stbi__get8(s)) > 0) + stbi__skip(s,len); + return g->out; + } else if (code <= avail) { + if (first) { + return stbi__errpuc("no clear code", "Corrupt GIF"); + } + + if (oldcode >= 0) { + p = &g->codes[avail++]; + if (avail > 8192) { + return stbi__errpuc("too many codes", "Corrupt GIF"); + } + + p->prefix = (stbi__int16) oldcode; + p->first = g->codes[oldcode].first; + p->suffix = (code == avail) ? p->first : g->codes[code].first; + } else if (code == avail) + return stbi__errpuc("illegal code in raster", "Corrupt GIF"); + + stbi__out_gif_code(g, (stbi__uint16) code); + + if ((avail & codemask) == 0 && avail <= 0x0FFF) { + codesize++; + codemask = (1 << codesize) - 1; + } + + oldcode = code; + } else { + return stbi__errpuc("illegal code in raster", "Corrupt GIF"); + } + } + } +} + +// this function is designed to support animated gifs, although stb_image doesn't support it +// two back is the image from two frames ago, used for a very specific disposal format +static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back) +{ + int dispose; + int first_frame; + int pi; + int pcount; + STBI_NOTUSED(req_comp); + + // on first frame, any non-written pixels get the background colour (non-transparent) + first_frame = 0; + if (g->out == 0) { + if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header + if (!stbi__mad3sizes_valid(4, g->w, g->h, 0)) + return stbi__errpuc("too large", "GIF image is too large"); + pcount = g->w * g->h; + g->out = (stbi_uc *) stbi__malloc(4 * pcount); + g->background = (stbi_uc *) stbi__malloc(4 * pcount); + g->history = (stbi_uc *) stbi__malloc(pcount); + if (!g->out || !g->background || !g->history) + return stbi__errpuc("outofmem", "Out of memory"); + + // image is treated as "transparent" at the start - ie, nothing overwrites the current background; + // background colour is only used for pixels that are not rendered first frame, after that "background" + // color refers to the color that was there the previous frame. + memset(g->out, 0x00, 4 * pcount); + memset(g->background, 0x00, 4 * pcount); // state of the background (starts transparent) + memset(g->history, 0x00, pcount); // pixels that were affected previous frame + first_frame = 1; + } else { + // second frame - how do we dispose of the previous one? + dispose = (g->eflags & 0x1C) >> 2; + pcount = g->w * g->h; + + if ((dispose == 3) && (two_back == 0)) { + dispose = 2; // if I don't have an image to revert back to, default to the old background + } + + if (dispose == 3) { // use previous graphic + for (pi = 0; pi < pcount; ++pi) { + if (g->history[pi]) { + memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 ); + } + } + } else if (dispose == 2) { + // restore what was changed last frame to background before that frame; + for (pi = 0; pi < pcount; ++pi) { + if (g->history[pi]) { + memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 ); + } + } + } else { + // This is a non-disposal case eithe way, so just + // leave the pixels as is, and they will become the new background + // 1: do not dispose + // 0: not specified. + } + + // background is what out is after the undoing of the previou frame; + memcpy( g->background, g->out, 4 * g->w * g->h ); + } + + // clear my history; + memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame + + for (;;) { + int tag = stbi__get8(s); + switch (tag) { + case 0x2C: /* Image Descriptor */ + { + stbi__int32 x, y, w, h; + stbi_uc *o; + + x = stbi__get16le(s); + y = stbi__get16le(s); + w = stbi__get16le(s); + h = stbi__get16le(s); + if (((x + w) > (g->w)) || ((y + h) > (g->h))) + return stbi__errpuc("bad Image Descriptor", "Corrupt GIF"); + + g->line_size = g->w * 4; + g->start_x = x * 4; + g->start_y = y * g->line_size; + g->max_x = g->start_x + w * 4; + g->max_y = g->start_y + h * g->line_size; + g->cur_x = g->start_x; + g->cur_y = g->start_y; + + // if the width of the specified rectangle is 0, that means + // we may not see *any* pixels or the image is malformed; + // to make sure this is caught, move the current y down to + // max_y (which is what out_gif_code checks). + if (w == 0) + g->cur_y = g->max_y; + + g->lflags = stbi__get8(s); + + if (g->lflags & 0x40) { + g->step = 8 * g->line_size; // first interlaced spacing + g->parse = 3; + } else { + g->step = g->line_size; + g->parse = 0; + } + + if (g->lflags & 0x80) { + stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1); + g->color_table = (stbi_uc *) g->lpal; + } else if (g->flags & 0x80) { + g->color_table = (stbi_uc *) g->pal; + } else + return stbi__errpuc("missing color table", "Corrupt GIF"); + + o = stbi__process_gif_raster(s, g); + if (!o) return NULL; + + // if this was the first frame, + pcount = g->w * g->h; + if (first_frame && (g->bgindex > 0)) { + // if first frame, any pixel not drawn to gets the background color + for (pi = 0; pi < pcount; ++pi) { + if (g->history[pi] == 0) { + g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be; + memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 ); + } + } + } + + return o; + } + + case 0x21: // Comment Extension. + { + int len; + int ext = stbi__get8(s); + if (ext == 0xF9) { // Graphic Control Extension. + len = stbi__get8(s); + if (len == 4) { + g->eflags = stbi__get8(s); + g->delay = 10 * stbi__get16le(s); // delay - 1/100th of a second, saving as 1/1000ths. + + // unset old transparent + if (g->transparent >= 0) { + g->pal[g->transparent][3] = 255; + } + if (g->eflags & 0x01) { + g->transparent = stbi__get8(s); + if (g->transparent >= 0) { + g->pal[g->transparent][3] = 0; + } + } else { + // don't need transparent + stbi__skip(s, 1); + g->transparent = -1; + } + } else { + stbi__skip(s, len); + break; + } + } + while ((len = stbi__get8(s)) != 0) { + stbi__skip(s, len); + } + break; + } + + case 0x3B: // gif stream termination code + return (stbi_uc *) s; // using '1' causes warning on some compilers + + default: + return stbi__errpuc("unknown code", "Corrupt GIF"); + } + } +} + +static void *stbi__load_gif_main_outofmem(stbi__gif *g, stbi_uc *out, int **delays) +{ + STBI_FREE(g->out); + STBI_FREE(g->history); + STBI_FREE(g->background); + + if (out) STBI_FREE(out); + if (delays && *delays) STBI_FREE(*delays); + return stbi__errpuc("outofmem", "Out of memory"); +} + +static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp) +{ + if (stbi__gif_test(s)) { + int layers = 0; + stbi_uc *u = 0; + stbi_uc *out = 0; + stbi_uc *two_back = 0; + stbi__gif g; + int stride; + int out_size = 0; + int delays_size = 0; + + STBI_NOTUSED(out_size); + STBI_NOTUSED(delays_size); + + memset(&g, 0, sizeof(g)); + if (delays) { + *delays = 0; + } + + do { + u = stbi__gif_load_next(s, &g, comp, req_comp, two_back); + if (u == (stbi_uc *) s) u = 0; // end of animated gif marker + + if (u) { + *x = g.w; + *y = g.h; + ++layers; + stride = g.w * g.h * 4; + + if (out) { + void *tmp = (stbi_uc*) STBI_REALLOC_SIZED( out, out_size, layers * stride ); + if (!tmp) + return stbi__load_gif_main_outofmem(&g, out, delays); + else { + out = (stbi_uc*) tmp; + out_size = layers * stride; + } + + if (delays) { + int *new_delays = (int*) STBI_REALLOC_SIZED( *delays, delays_size, sizeof(int) * layers ); + if (!new_delays) + return stbi__load_gif_main_outofmem(&g, out, delays); + *delays = new_delays; + delays_size = layers * sizeof(int); + } + } else { + out = (stbi_uc*)stbi__malloc( layers * stride ); + if (!out) + return stbi__load_gif_main_outofmem(&g, out, delays); + out_size = layers * stride; + if (delays) { + *delays = (int*) stbi__malloc( layers * sizeof(int) ); + if (!*delays) + return stbi__load_gif_main_outofmem(&g, out, delays); + delays_size = layers * sizeof(int); + } + } + memcpy( out + ((layers - 1) * stride), u, stride ); + if (layers >= 2) { + two_back = out - 2 * stride; + } + + if (delays) { + (*delays)[layers - 1U] = g.delay; + } + } + } while (u != 0); + + // free temp buffer; + STBI_FREE(g.out); + STBI_FREE(g.history); + STBI_FREE(g.background); + + // do the final conversion after loading everything; + if (req_comp && req_comp != 4) + out = stbi__convert_format(out, 4, req_comp, layers * g.w, g.h); + + *z = layers; + return out; + } else { + return stbi__errpuc("not GIF", "Image was not as a gif type."); + } +} + +static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + stbi_uc *u = 0; + stbi__gif g; + memset(&g, 0, sizeof(g)); + STBI_NOTUSED(ri); + + u = stbi__gif_load_next(s, &g, comp, req_comp, 0); + if (u == (stbi_uc *) s) u = 0; // end of animated gif marker + if (u) { + *x = g.w; + *y = g.h; + + // moved conversion to after successful load so that the same + // can be done for multiple frames. + if (req_comp && req_comp != 4) + u = stbi__convert_format(u, 4, req_comp, g.w, g.h); + } else if (g.out) { + // if there was an error and we allocated an image buffer, free it! + STBI_FREE(g.out); + } + + // free buffers needed for multiple frame loading; + STBI_FREE(g.history); + STBI_FREE(g.background); + + return u; +} + +static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp) +{ + return stbi__gif_info_raw(s,x,y,comp); +} +#endif + +// ************************************************************************************************* +// Radiance RGBE HDR loader +// originally by Nicolas Schulz +#ifndef STBI_NO_HDR +static int stbi__hdr_test_core(stbi__context *s, const char *signature) +{ + int i; + for (i=0; signature[i]; ++i) + if (stbi__get8(s) != signature[i]) + return 0; + stbi__rewind(s); + return 1; +} + +static int stbi__hdr_test(stbi__context* s) +{ + int r = stbi__hdr_test_core(s, "#?RADIANCE\n"); + stbi__rewind(s); + if(!r) { + r = stbi__hdr_test_core(s, "#?RGBE\n"); + stbi__rewind(s); + } + return r; +} + +#define STBI__HDR_BUFLEN 1024 +static char *stbi__hdr_gettoken(stbi__context *z, char *buffer) +{ + int len=0; + char c = '\0'; + + c = (char) stbi__get8(z); + + while (!stbi__at_eof(z) && c != '\n') { + buffer[len++] = c; + if (len == STBI__HDR_BUFLEN-1) { + // flush to end of line + while (!stbi__at_eof(z) && stbi__get8(z) != '\n') + ; + break; + } + c = (char) stbi__get8(z); + } + + buffer[len] = 0; + return buffer; +} + +static void stbi__hdr_convert(float *output, stbi_uc *input, int req_comp) +{ + if ( input[3] != 0 ) { + float f1; + // Exponent + f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8)); + if (req_comp <= 2) + output[0] = (input[0] + input[1] + input[2]) * f1 / 3; + else { + output[0] = input[0] * f1; + output[1] = input[1] * f1; + output[2] = input[2] * f1; + } + if (req_comp == 2) output[1] = 1; + if (req_comp == 4) output[3] = 1; + } else { + switch (req_comp) { + case 4: output[3] = 1; /* fallthrough */ + case 3: output[0] = output[1] = output[2] = 0; + break; + case 2: output[1] = 1; /* fallthrough */ + case 1: output[0] = 0; + break; + } + } +} + +static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + char buffer[STBI__HDR_BUFLEN]; + char *token; + int valid = 0; + int width, height; + stbi_uc *scanline; + float *hdr_data; + int len; + unsigned char count, value; + int i, j, k, c1,c2, z; + const char *headerToken; + STBI_NOTUSED(ri); + + // Check identifier + headerToken = stbi__hdr_gettoken(s,buffer); + if (strcmp(headerToken, "#?RADIANCE") != 0 && strcmp(headerToken, "#?RGBE") != 0) + return stbi__errpf("not HDR", "Corrupt HDR image"); + + // Parse header + for(;;) { + token = stbi__hdr_gettoken(s,buffer); + if (token[0] == 0) break; + if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1; + } + + if (!valid) return stbi__errpf("unsupported format", "Unsupported HDR format"); + + // Parse width and height + // can't use sscanf() if we're not using stdio! + token = stbi__hdr_gettoken(s,buffer); + if (strncmp(token, "-Y ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format"); + token += 3; + height = (int) strtol(token, &token, 10); + while (*token == ' ') ++token; + if (strncmp(token, "+X ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format"); + token += 3; + width = (int) strtol(token, NULL, 10); + + if (height > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)"); + if (width > STBI_MAX_DIMENSIONS) return stbi__errpf("too large","Very large image (corrupt?)"); + + *x = width; + *y = height; + + if (comp) *comp = 3; + if (req_comp == 0) req_comp = 3; + + if (!stbi__mad4sizes_valid(width, height, req_comp, sizeof(float), 0)) + return stbi__errpf("too large", "HDR image is too large"); + + // Read data + hdr_data = (float *) stbi__malloc_mad4(width, height, req_comp, sizeof(float), 0); + if (!hdr_data) + return stbi__errpf("outofmem", "Out of memory"); + + // Load image data + // image data is stored as some number of sca + if ( width < 8 || width >= 32768) { + // Read flat data + for (j=0; j < height; ++j) { + for (i=0; i < width; ++i) { + stbi_uc rgbe[4]; + main_decode_loop: + stbi__getn(s, rgbe, 4); + stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp); + } + } + } else { + // Read RLE-encoded data + scanline = NULL; + + for (j = 0; j < height; ++j) { + c1 = stbi__get8(s); + c2 = stbi__get8(s); + len = stbi__get8(s); + if (c1 != 2 || c2 != 2 || (len & 0x80)) { + // not run-length encoded, so we have to actually use THIS data as a decoded + // pixel (note this can't be a valid pixel--one of RGB must be >= 128) + stbi_uc rgbe[4]; + rgbe[0] = (stbi_uc) c1; + rgbe[1] = (stbi_uc) c2; + rgbe[2] = (stbi_uc) len; + rgbe[3] = (stbi_uc) stbi__get8(s); + stbi__hdr_convert(hdr_data, rgbe, req_comp); + i = 1; + j = 0; + STBI_FREE(scanline); + goto main_decode_loop; // yes, this makes no sense + } + len <<= 8; + len |= stbi__get8(s); + if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); } + if (scanline == NULL) { + scanline = (stbi_uc *) stbi__malloc_mad2(width, 4, 0); + if (!scanline) { + STBI_FREE(hdr_data); + return stbi__errpf("outofmem", "Out of memory"); + } + } + + for (k = 0; k < 4; ++k) { + int nleft; + i = 0; + while ((nleft = width - i) > 0) { + count = stbi__get8(s); + if (count > 128) { + // Run + value = stbi__get8(s); + count -= 128; + if ((count == 0) || (count > nleft)) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); } + for (z = 0; z < count; ++z) + scanline[i++ * 4 + k] = value; + } else { + // Dump + if ((count == 0) || (count > nleft)) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); } + for (z = 0; z < count; ++z) + scanline[i++ * 4 + k] = stbi__get8(s); + } + } + } + for (i=0; i < width; ++i) + stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp); + } + if (scanline) + STBI_FREE(scanline); + } + + return hdr_data; +} + +static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp) +{ + char buffer[STBI__HDR_BUFLEN]; + char *token; + int valid = 0; + int dummy; + + if (!x) x = &dummy; + if (!y) y = &dummy; + if (!comp) comp = &dummy; + + if (stbi__hdr_test(s) == 0) { + stbi__rewind( s ); + return 0; + } + + for(;;) { + token = stbi__hdr_gettoken(s,buffer); + if (token[0] == 0) break; + if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1; + } + + if (!valid) { + stbi__rewind( s ); + return 0; + } + token = stbi__hdr_gettoken(s,buffer); + if (strncmp(token, "-Y ", 3)) { + stbi__rewind( s ); + return 0; + } + token += 3; + *y = (int) strtol(token, &token, 10); + while (*token == ' ') ++token; + if (strncmp(token, "+X ", 3)) { + stbi__rewind( s ); + return 0; + } + token += 3; + *x = (int) strtol(token, NULL, 10); + *comp = 3; + return 1; +} +#endif // STBI_NO_HDR + +#ifndef STBI_NO_BMP +static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp) +{ + void *p; + stbi__bmp_data info; + + info.all_a = 255; + p = stbi__bmp_parse_header(s, &info); + if (p == NULL) { + stbi__rewind( s ); + return 0; + } + if (x) *x = s->img_x; + if (y) *y = s->img_y; + if (comp) { + if (info.bpp == 24 && info.ma == 0xff000000) + *comp = 3; + else + *comp = info.ma ? 4 : 3; + } + return 1; +} +#endif + +#ifndef STBI_NO_PSD +static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp) +{ + int channelCount, dummy, depth; + if (!x) x = &dummy; + if (!y) y = &dummy; + if (!comp) comp = &dummy; + if (stbi__get32be(s) != 0x38425053) { + stbi__rewind( s ); + return 0; + } + if (stbi__get16be(s) != 1) { + stbi__rewind( s ); + return 0; + } + stbi__skip(s, 6); + channelCount = stbi__get16be(s); + if (channelCount < 0 || channelCount > 16) { + stbi__rewind( s ); + return 0; + } + *y = stbi__get32be(s); + *x = stbi__get32be(s); + depth = stbi__get16be(s); + if (depth != 8 && depth != 16) { + stbi__rewind( s ); + return 0; + } + if (stbi__get16be(s) != 3) { + stbi__rewind( s ); + return 0; + } + *comp = 4; + return 1; +} + +static int stbi__psd_is16(stbi__context *s) +{ + int channelCount, depth; + if (stbi__get32be(s) != 0x38425053) { + stbi__rewind( s ); + return 0; + } + if (stbi__get16be(s) != 1) { + stbi__rewind( s ); + return 0; + } + stbi__skip(s, 6); + channelCount = stbi__get16be(s); + if (channelCount < 0 || channelCount > 16) { + stbi__rewind( s ); + return 0; + } + STBI_NOTUSED(stbi__get32be(s)); + STBI_NOTUSED(stbi__get32be(s)); + depth = stbi__get16be(s); + if (depth != 16) { + stbi__rewind( s ); + return 0; + } + return 1; +} +#endif + +#ifndef STBI_NO_PIC +static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp) +{ + int act_comp=0,num_packets=0,chained,dummy; + stbi__pic_packet packets[10]; + + if (!x) x = &dummy; + if (!y) y = &dummy; + if (!comp) comp = &dummy; + + if (!stbi__pic_is4(s,"\x53\x80\xF6\x34")) { + stbi__rewind(s); + return 0; + } + + stbi__skip(s, 88); + + *x = stbi__get16be(s); + *y = stbi__get16be(s); + if (stbi__at_eof(s)) { + stbi__rewind( s); + return 0; + } + if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) { + stbi__rewind( s ); + return 0; + } + + stbi__skip(s, 8); + + do { + stbi__pic_packet *packet; + + if (num_packets==sizeof(packets)/sizeof(packets[0])) + return 0; + + packet = &packets[num_packets++]; + chained = stbi__get8(s); + packet->size = stbi__get8(s); + packet->type = stbi__get8(s); + packet->channel = stbi__get8(s); + act_comp |= packet->channel; + + if (stbi__at_eof(s)) { + stbi__rewind( s ); + return 0; + } + if (packet->size != 8) { + stbi__rewind( s ); + return 0; + } + } while (chained); + + *comp = (act_comp & 0x10 ? 4 : 3); + + return 1; +} +#endif + +// ************************************************************************************************* +// Portable Gray Map and Portable Pixel Map loader +// by Ken Miller +// +// PGM: http://netpbm.sourceforge.net/doc/pgm.html +// PPM: http://netpbm.sourceforge.net/doc/ppm.html +// +// Known limitations: +// Does not support comments in the header section +// Does not support ASCII image data (formats P2 and P3) + +#ifndef STBI_NO_PNM + +static int stbi__pnm_test(stbi__context *s) +{ + char p, t; + p = (char) stbi__get8(s); + t = (char) stbi__get8(s); + if (p != 'P' || (t != '5' && t != '6')) { + stbi__rewind( s ); + return 0; + } + return 1; +} + +static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri) +{ + stbi_uc *out; + STBI_NOTUSED(ri); + + ri->bits_per_channel = stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n); + if (ri->bits_per_channel == 0) + return 0; + + if (s->img_y > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + if (s->img_x > STBI_MAX_DIMENSIONS) return stbi__errpuc("too large","Very large image (corrupt?)"); + + *x = s->img_x; + *y = s->img_y; + if (comp) *comp = s->img_n; + + if (!stbi__mad4sizes_valid(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0)) + return stbi__errpuc("too large", "PNM too large"); + + out = (stbi_uc *) stbi__malloc_mad4(s->img_n, s->img_x, s->img_y, ri->bits_per_channel / 8, 0); + if (!out) return stbi__errpuc("outofmem", "Out of memory"); + if (!stbi__getn(s, out, s->img_n * s->img_x * s->img_y * (ri->bits_per_channel / 8))) { + STBI_FREE(out); + return stbi__errpuc("bad PNM", "PNM file truncated"); + } + + if (req_comp && req_comp != s->img_n) { + if (ri->bits_per_channel == 16) { + out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, s->img_n, req_comp, s->img_x, s->img_y); + } else { + out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y); + } + if (out == NULL) return out; // stbi__convert_format frees input on failure + } + return out; +} + +static int stbi__pnm_isspace(char c) +{ + return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r'; +} + +static void stbi__pnm_skip_whitespace(stbi__context *s, char *c) +{ + for (;;) { + while (!stbi__at_eof(s) && stbi__pnm_isspace(*c)) + *c = (char) stbi__get8(s); + + if (stbi__at_eof(s) || *c != '#') + break; + + while (!stbi__at_eof(s) && *c != '\n' && *c != '\r' ) + *c = (char) stbi__get8(s); + } +} + +static int stbi__pnm_isdigit(char c) +{ + return c >= '0' && c <= '9'; +} + +static int stbi__pnm_getinteger(stbi__context *s, char *c) +{ + int value = 0; + + while (!stbi__at_eof(s) && stbi__pnm_isdigit(*c)) { + value = value*10 + (*c - '0'); + *c = (char) stbi__get8(s); + if((value > 214748364) || (value == 214748364 && *c > '7')) + return stbi__err("integer parse overflow", "Parsing an integer in the PPM header overflowed a 32-bit int"); + } + + return value; +} + +static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp) +{ + int maxv, dummy; + char c, p, t; + + if (!x) x = &dummy; + if (!y) y = &dummy; + if (!comp) comp = &dummy; + + stbi__rewind(s); + + // Get identifier + p = (char) stbi__get8(s); + t = (char) stbi__get8(s); + if (p != 'P' || (t != '5' && t != '6')) { + stbi__rewind(s); + return 0; + } + + *comp = (t == '6') ? 3 : 1; // '5' is 1-component .pgm; '6' is 3-component .ppm + + c = (char) stbi__get8(s); + stbi__pnm_skip_whitespace(s, &c); + + *x = stbi__pnm_getinteger(s, &c); // read width + if(*x == 0) + return stbi__err("invalid width", "PPM image header had zero or overflowing width"); + stbi__pnm_skip_whitespace(s, &c); + + *y = stbi__pnm_getinteger(s, &c); // read height + if (*y == 0) + return stbi__err("invalid width", "PPM image header had zero or overflowing width"); + stbi__pnm_skip_whitespace(s, &c); + + maxv = stbi__pnm_getinteger(s, &c); // read max value + if (maxv > 65535) + return stbi__err("max value > 65535", "PPM image supports only 8-bit and 16-bit images"); + else if (maxv > 255) + return 16; + else + return 8; +} + +static int stbi__pnm_is16(stbi__context *s) +{ + if (stbi__pnm_info(s, NULL, NULL, NULL) == 16) + return 1; + return 0; +} +#endif + +static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp) +{ + #ifndef STBI_NO_JPEG + if (stbi__jpeg_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_PNG + if (stbi__png_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_GIF + if (stbi__gif_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_BMP + if (stbi__bmp_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_PSD + if (stbi__psd_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_PIC + if (stbi__pic_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_PNM + if (stbi__pnm_info(s, x, y, comp)) return 1; + #endif + + #ifndef STBI_NO_HDR + if (stbi__hdr_info(s, x, y, comp)) return 1; + #endif + + // test tga last because it's a crappy test! + #ifndef STBI_NO_TGA + if (stbi__tga_info(s, x, y, comp)) + return 1; + #endif + return stbi__err("unknown image type", "Image not of any known type, or corrupt"); +} + +static int stbi__is_16_main(stbi__context *s) +{ + #ifndef STBI_NO_PNG + if (stbi__png_is16(s)) return 1; + #endif + + #ifndef STBI_NO_PSD + if (stbi__psd_is16(s)) return 1; + #endif + + #ifndef STBI_NO_PNM + if (stbi__pnm_is16(s)) return 1; + #endif + return 0; +} + +#ifndef STBI_NO_STDIO +STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp) +{ + FILE *f = stbi__fopen(filename, "rb"); + int result; + if (!f) return stbi__err("can't fopen", "Unable to open file"); + result = stbi_info_from_file(f, x, y, comp); + fclose(f); + return result; +} + +STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp) +{ + int r; + stbi__context s; + long pos = ftell(f); + stbi__start_file(&s, f); + r = stbi__info_main(&s,x,y,comp); + fseek(f,pos,SEEK_SET); + return r; +} + +STBIDEF int stbi_is_16_bit(char const *filename) +{ + FILE *f = stbi__fopen(filename, "rb"); + int result; + if (!f) return stbi__err("can't fopen", "Unable to open file"); + result = stbi_is_16_bit_from_file(f); + fclose(f); + return result; +} + +STBIDEF int stbi_is_16_bit_from_file(FILE *f) +{ + int r; + stbi__context s; + long pos = ftell(f); + stbi__start_file(&s, f); + r = stbi__is_16_main(&s); + fseek(f,pos,SEEK_SET); + return r; +} +#endif // !STBI_NO_STDIO + +STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__info_main(&s,x,y,comp); +} + +STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user); + return stbi__info_main(&s,x,y,comp); +} + +STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len) +{ + stbi__context s; + stbi__start_mem(&s,buffer,len); + return stbi__is_16_main(&s); +} + +STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user) +{ + stbi__context s; + stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user); + return stbi__is_16_main(&s); +} + +#endif // STB_IMAGE_IMPLEMENTATION + +/* + revision history: + 2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs + 2.19 (2018-02-11) fix warning + 2.18 (2018-01-30) fix warnings + 2.17 (2018-01-29) change sbti__shiftsigned to avoid clang -O2 bug + 1-bit BMP + *_is_16_bit api + avoid warnings + 2.16 (2017-07-23) all functions have 16-bit variants; + STBI_NO_STDIO works again; + compilation fixes; + fix rounding in unpremultiply; + optimize vertical flip; + disable raw_len validation; + documentation fixes + 2.15 (2017-03-18) fix png-1,2,4 bug; now all Imagenet JPGs decode; + warning fixes; disable run-time SSE detection on gcc; + uniform handling of optional "return" values; + thread-safe initialization of zlib tables + 2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs + 2.13 (2016-11-29) add 16-bit API, only supported for PNG right now + 2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes + 2.11 (2016-04-02) allocate large structures on the stack + remove white matting for transparent PSD + fix reported channel count for PNG & BMP + re-enable SSE2 in non-gcc 64-bit + support RGB-formatted JPEG + read 16-bit PNGs (only as 8-bit) + 2.10 (2016-01-22) avoid warning introduced in 2.09 by STBI_REALLOC_SIZED + 2.09 (2016-01-16) allow comments in PNM files + 16-bit-per-pixel TGA (not bit-per-component) + info() for TGA could break due to .hdr handling + info() for BMP to shares code instead of sloppy parse + can use STBI_REALLOC_SIZED if allocator doesn't support realloc + code cleanup + 2.08 (2015-09-13) fix to 2.07 cleanup, reading RGB PSD as RGBA + 2.07 (2015-09-13) fix compiler warnings + partial animated GIF support + limited 16-bpc PSD support + #ifdef unused functions + bug with < 92 byte PIC,PNM,HDR,TGA + 2.06 (2015-04-19) fix bug where PSD returns wrong '*comp' value + 2.05 (2015-04-19) fix bug in progressive JPEG handling, fix warning + 2.04 (2015-04-15) try to re-enable SIMD on MinGW 64-bit + 2.03 (2015-04-12) extra corruption checking (mmozeiko) + stbi_set_flip_vertically_on_load (nguillemot) + fix NEON support; fix mingw support + 2.02 (2015-01-19) fix incorrect assert, fix warning + 2.01 (2015-01-17) fix various warnings; suppress SIMD on gcc 32-bit without -msse2 + 2.00b (2014-12-25) fix STBI_MALLOC in progressive JPEG + 2.00 (2014-12-25) optimize JPG, including x86 SSE2 & NEON SIMD (ryg) + progressive JPEG (stb) + PGM/PPM support (Ken Miller) + STBI_MALLOC,STBI_REALLOC,STBI_FREE + GIF bugfix -- seemingly never worked + STBI_NO_*, STBI_ONLY_* + 1.48 (2014-12-14) fix incorrectly-named assert() + 1.47 (2014-12-14) 1/2/4-bit PNG support, both direct and paletted (Omar Cornut & stb) + optimize PNG (ryg) + fix bug in interlaced PNG with user-specified channel count (stb) + 1.46 (2014-08-26) + fix broken tRNS chunk (colorkey-style transparency) in non-paletted PNG + 1.45 (2014-08-16) + fix MSVC-ARM internal compiler error by wrapping malloc + 1.44 (2014-08-07) + various warning fixes from Ronny Chevalier + 1.43 (2014-07-15) + fix MSVC-only compiler problem in code changed in 1.42 + 1.42 (2014-07-09) + don't define _CRT_SECURE_NO_WARNINGS (affects user code) + fixes to stbi__cleanup_jpeg path + added STBI_ASSERT to avoid requiring assert.h + 1.41 (2014-06-25) + fix search&replace from 1.36 that messed up comments/error messages + 1.40 (2014-06-22) + fix gcc struct-initialization warning + 1.39 (2014-06-15) + fix to TGA optimization when req_comp != number of components in TGA; + fix to GIF loading because BMP wasn't rewinding (whoops, no GIFs in my test suite) + add support for BMP version 5 (more ignored fields) + 1.38 (2014-06-06) + suppress MSVC warnings on integer casts truncating values + fix accidental rename of 'skip' field of I/O + 1.37 (2014-06-04) + remove duplicate typedef + 1.36 (2014-06-03) + convert to header file single-file library + if de-iphone isn't set, load iphone images color-swapped instead of returning NULL + 1.35 (2014-05-27) + various warnings + fix broken STBI_SIMD path + fix bug where stbi_load_from_file no longer left file pointer in correct place + fix broken non-easy path for 32-bit BMP (possibly never used) + TGA optimization by Arseny Kapoulkine + 1.34 (unknown) + use STBI_NOTUSED in stbi__resample_row_generic(), fix one more leak in tga failure case + 1.33 (2011-07-14) + make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements + 1.32 (2011-07-13) + support for "info" function for all supported filetypes (SpartanJ) + 1.31 (2011-06-20) + a few more leak fixes, bug in PNG handling (SpartanJ) + 1.30 (2011-06-11) + added ability to load files via callbacks to accomidate custom input streams (Ben Wenger) + removed deprecated format-specific test/load functions + removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway + error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha) + fix inefficiency in decoding 32-bit BMP (David Woo) + 1.29 (2010-08-16) + various warning fixes from Aurelien Pocheville + 1.28 (2010-08-01) + fix bug in GIF palette transparency (SpartanJ) + 1.27 (2010-08-01) + cast-to-stbi_uc to fix warnings + 1.26 (2010-07-24) + fix bug in file buffering for PNG reported by SpartanJ + 1.25 (2010-07-17) + refix trans_data warning (Won Chun) + 1.24 (2010-07-12) + perf improvements reading from files on platforms with lock-heavy fgetc() + minor perf improvements for jpeg + deprecated type-specific functions so we'll get feedback if they're needed + attempt to fix trans_data warning (Won Chun) + 1.23 fixed bug in iPhone support + 1.22 (2010-07-10) + removed image *writing* support + stbi_info support from Jetro Lauha + GIF support from Jean-Marc Lienher + iPhone PNG-extensions from James Brown + warning-fixes from Nicolas Schulz and Janez Zemva (i.stbi__err. Janez (U+017D)emva) + 1.21 fix use of 'stbi_uc' in header (reported by jon blow) + 1.20 added support for Softimage PIC, by Tom Seddon + 1.19 bug in interlaced PNG corruption check (found by ryg) + 1.18 (2008-08-02) + fix a threading bug (local mutable static) + 1.17 support interlaced PNG + 1.16 major bugfix - stbi__convert_format converted one too many pixels + 1.15 initialize some fields for thread safety + 1.14 fix threadsafe conversion bug + header-file-only version (#define STBI_HEADER_FILE_ONLY before including) + 1.13 threadsafe + 1.12 const qualifiers in the API + 1.11 Support installable IDCT, colorspace conversion routines + 1.10 Fixes for 64-bit (don't use "unsigned long") + optimized upsampling by Fabian "ryg" Giesen + 1.09 Fix format-conversion for PSD code (bad global variables!) + 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz + 1.07 attempt to fix C++ warning/errors again + 1.06 attempt to fix C++ warning/errors again + 1.05 fix TGA loading to return correct *comp and use good luminance calc + 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free + 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR + 1.02 support for (subset of) HDR files, float interface for preferred access to them + 1.01 fix bug: possible bug in handling right-side up bmps... not sure + fix bug: the stbi__bmp_load() and stbi__tga_load() functions didn't work at all + 1.00 interface to zlib that skips zlib header + 0.99 correct handling of alpha in palette + 0.98 TGA loader by lonesock; dynamically add loaders (untested) + 0.97 jpeg errors on too large a file; also catch another malloc failure + 0.96 fix detection of invalid v value - particleman@mollyrocket forum + 0.95 during header scan, seek to markers in case of padding + 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same + 0.93 handle jpegtran output; verbose errors + 0.92 read 4,8,16,24,32-bit BMP files of several formats + 0.91 output 24-bit Windows 3.0 BMP files + 0.90 fix a few more warnings; bump version number to approach 1.0 + 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd + 0.60 fix compiling as c++ + 0.59 fix warnings: merge Dave Moore's -Wall fixes + 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian + 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available + 0.56 fix bug: zlib uncompressed mode len vs. nlen + 0.55 fix bug: restart_interval not initialized to 0 + 0.54 allow NULL for 'int *comp' + 0.53 fix bug in png 3->4; speedup png decoding + 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments + 0.51 obey req_comp requests, 1-component jpegs return as 1-component, + on 'test' only check type, not whether we support this variant + 0.50 (2006-11-19) + first released version +*/ + + +/* +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +*/ diff --git a/src/pyvale/common_cpp/util.cpp b/src/pyvale/common_cpp/util.cpp index 6174eb3da..d039ad8b4 100644 --- a/src/pyvale/common_cpp/util.cpp +++ b/src/pyvale/common_cpp/util.cpp @@ -6,14 +6,44 @@ // STD library Header files +#include +#include +#include +#include #include +#include // common_cpp header files +#include "./util.hpp" namespace common_util { + std::string current_datetime_ms() { + const auto now = std::chrono::system_clock::now(); + const auto now_time = std::chrono::system_clock::to_time_t(now); + const auto ms = std::chrono::duration_cast( + now.time_since_epoch()) % 1000; + + std::tm local_time{}; + + #if defined(_WIN32) + localtime_s(&local_time, &now_time); + #else + localtime_r(&now_time, &local_time); + #endif + + std::ostringstream oss; + oss << std::put_time(&local_time, "%Y-%m-%d %H:%M:%S") + << "." << std::setfill('0') << std::setw(3) << ms.count(); + return oss.str(); + } + void set_num_threads(int n){ omp_set_num_threads(n); } + int get_num_threads(){ + return omp_get_max_threads(); + } + } diff --git a/src/pyvale/common_cpp/util.hpp b/src/pyvale/common_cpp/util.hpp index 6c5223104..3b337e8f3 100644 --- a/src/pyvale/common_cpp/util.hpp +++ b/src/pyvale/common_cpp/util.hpp @@ -9,18 +9,34 @@ #define UTIL_H // STD library Header files +#include #include #include #include #include #include #include +#include #include #include +#include // common_cpp header files #include "./defines.hpp" +enum class PixelType { UINT8, UINT16, UINT32, UINT32F}; + +struct Image { + std::string filename; + PixelType type; + uint32_t width; + uint32_t height; + std::vector data8; + std::vector data16; + std::vector data32; + std::vector data32f; +}; + namespace common_util { struct SaveConfig { @@ -28,27 +44,122 @@ namespace common_util { std::string prefix; std::string delimiter; bool binary; - bool at_end; bool output_below_threshold; bool shape_params; }; - class Timer { - public: - Timer(const std::string& label) - : label_(label), start_(std::chrono::high_resolution_clock::now()) {} + std::string current_datetime_ms(); + + + inline std::string format_duration_ms(const std::chrono::milliseconds& duration) + { + const auto total_seconds = duration.count() / 1000; + const auto milliseconds = duration.count() % 1000; + + const int hours = static_cast(total_seconds / 3600); + const int minutes = static_cast((total_seconds % 3600) / 60); + const int seconds = static_cast(total_seconds % 60); + + std::ostringstream oss; + oss << std::setfill('0'); + + if (hours > 0) + { + oss << std::setw(2) << hours << ":"; + } + + oss << std::setw(2) << minutes << ":" + << std::setw(2) << seconds << "." + << std::setw(3) << milliseconds; + + return oss.str(); + } + + + template + void info_out(const A& a, const B& b) + { + constexpr std::size_t total_width = 80; + + std::ostringstream lhs_stream; + lhs_stream << "[" << current_datetime_ms() << "] " << a; + + std::ostringstream rhs_stream; + rhs_stream << b; + + const std::string lhs = lhs_stream.str(); + const std::string rhs = rhs_stream.str(); + + std::size_t padding = 1; + + if (lhs.size() + rhs.size() < total_width) + { + padding = total_width - lhs.size() - rhs.size(); + } - ~Timer() { - auto end = std::chrono::high_resolution_clock::now(); - std::chrono::duration elapsed = end - start_; - INFO_OUT("Time taken for " + label_, elapsed.count() << " [s]"); - } + std::cout << lhs + << std::string(padding, ' ') + << rhs + << '\n'; + } + + + class Timer + { + public: + Timer(const std::string& label, int level = 2) + : label_(label), + enabled_(g_debug_level >= level), + start_(std::chrono::high_resolution_clock::now()) + {} + + ~Timer() + { + if (!enabled_) + return; + + auto end = std::chrono::high_resolution_clock::now(); + auto elapsed = std::chrono::duration_cast( + end - start_); - private: - std::string label_; - std::chrono::high_resolution_clock::time_point start_; + info_out("Time " + label_, format_duration_ms(elapsed)); + } + + private: + std::string label_; + bool enabled_; + std::chrono::high_resolution_clock::time_point start_; }; + inline void title(const std::string& text) + { + constexpr std::size_t width = 80; + + std::cout << std::string(width, '-') << '\n'; + + const std::size_t content_width = text.size() + 2; // spaces around title + + if (content_width >= width) + { + std::cout << text << '\n'; + } + else + { + const std::size_t total_pad = width - content_width; + const std::size_t left_pad = total_pad / 2; + const std::size_t right_pad = total_pad - left_pad; + + std::cout << std::string(left_pad, '-') + << ' ' + << text + << ' ' + << std::string(right_pad, '-') + << '\n'; + } + + std::cout << std::string(width, '-') << '\n'; + } + inline void write_int(std::ofstream& out, int val) { out.write(reinterpret_cast(&val), sizeof(int)); } @@ -67,6 +178,13 @@ namespace common_util { * @param n The number of threads to set for the DIC engine. */ void set_num_threads(int n); + + /** + * Gets the maximum number of OpenMP threads available to a parallel region. + * + * The maximum number of OpenMP threads. + */ + int get_num_threads(); } #endif //UTIL_H diff --git a/src/pyvale/common_py/util.py b/src/pyvale/common_py/util.py index c3213b2b8..595eef5b2 100644 --- a/src/pyvale/common_py/util.py +++ b/src/pyvale/common_py/util.py @@ -6,6 +6,55 @@ import os import sys +from datetime import datetime + +def print_pyvale_banner(): + print(" # ## #### ") + print(" #+# #-+# #--+ ") + print(" ##-## ##--# #--+ ") + print(" #.-+# #++.+# ### #### #--+ #### ") + print(" #+---# #.---# ##-----+--# #--+ ##------# ") + print(" #.---##.-.-+# +--+###+--# #--+ #+--####--# ") + print(" #++--+-...+# +--## #+--# #--+ #+--####### ") + print(" #+-+#--.-# #+--------# #--+ #----+---# ") + print(" #...+..+# ##+###### #### ##++## ") + print(" #+++---+-# ") + print(" #####+++## ##### ##### ##-.----.--++--.------------+-----.-## ") + print(" #---------# +--+##+--# #+-.--.-.--..-.-..+.--.+.-..--#..-.--.-+.-## ") + print(" #---# #---# #+--++--## #--..-.---.-..-------.-----.#+..----.-.#-+-# ") + print(" #---###+--+ +----# #+---#-+-.-..-.---.-++-+..-+....--.-.-..#-+## ") + print(" #--------+# #+--# ##+...-.----..-----------..-++---.--.----+-.--+# ") + print(" #---#### #+--# ###+---.-..-+.--.-.-+-..-----------++#..+.++##..-## ") + print(" #---# #+--# ##########+++#+#++########### #----# #+++# ") + print(" ##### ##### #+.-# ## ") + print(" ## ") + + +def print_title(title: str): + line_width = 80 + + print("-" * line_width) + print(f" {title} ".center(line_width, "-")) + print("-" * line_width) + + +def info(msg: str) -> None: + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + print(f"[{timestamp}] {msg}") + + +def info_out(label: object, value: object) -> None: + total_width = 80 + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + lhs = f"[{timestamp}] {label}" + rhs = str(value) + padding = 1 + + if len(lhs) + len(rhs) < total_width: + padding = total_width - len(lhs) - len(rhs) + + print(lhs + " " * padding + rhs) + def check_output_directory(output_basepath: str, output_prefix: str, debug_level: int) -> None: @@ -32,7 +81,7 @@ def check_output_directory(output_basepath: str, If the output directory does not exist or the user chooses not to proceed after being warned about existing files. """ - + # check if there's output files try: files = os.listdir(output_basepath) @@ -49,10 +98,7 @@ def check_output_directory(output_basepath: str, if conflicting_files: conflicting_files.sort() if (debug_level>0): - print("WARNING: The following output files already exist and may be overwritten:") - for f in conflicting_files: - print(f" - {os.path.join(output_basepath, f)}") - print("") + info("WARNING: files with matching prefix in output dir") ###### TURNING USER INPUT OFF FOR NOW ###### diff --git a/src/pyvale/data/Step01_00,00-sys1-0000_0.tif b/src/pyvale/data/Step01_00,00-sys1-0000_0.tif new file mode 100644 index 000000000..0a7a4f2f7 Binary files /dev/null and b/src/pyvale/data/Step01_00,00-sys1-0000_0.tif differ diff --git a/src/pyvale/data/Step01_00,00-sys1-0000_1.tif b/src/pyvale/data/Step01_00,00-sys1-0000_1.tif new file mode 100644 index 000000000..35ef37640 Binary files /dev/null and b/src/pyvale/data/Step01_00,00-sys1-0000_1.tif differ diff --git a/src/pyvale/data/hole_cam0_frame00.tiff b/src/pyvale/data/hole_cam0_frame00.tiff new file mode 100644 index 000000000..ca419faca Binary files /dev/null and b/src/pyvale/data/hole_cam0_frame00.tiff differ diff --git a/src/pyvale/data/hole_cam0_frame05.tiff b/src/pyvale/data/hole_cam0_frame05.tiff new file mode 100644 index 000000000..2b21aed61 Binary files /dev/null and b/src/pyvale/data/hole_cam0_frame05.tiff differ diff --git a/src/pyvale/data/hole_cam0_frame10.tiff b/src/pyvale/data/hole_cam0_frame10.tiff new file mode 100644 index 000000000..6274a3fb2 Binary files /dev/null and b/src/pyvale/data/hole_cam0_frame10.tiff differ diff --git a/src/pyvale/data/hole_cam1_frame00.tiff b/src/pyvale/data/hole_cam1_frame00.tiff new file mode 100644 index 000000000..4ef973ab0 Binary files /dev/null and b/src/pyvale/data/hole_cam1_frame00.tiff differ diff --git a/src/pyvale/data/hole_cam1_frame05.tiff b/src/pyvale/data/hole_cam1_frame05.tiff new file mode 100644 index 000000000..61f99e40a Binary files /dev/null and b/src/pyvale/data/hole_cam1_frame05.tiff differ diff --git a/src/pyvale/data/hole_cam1_frame10.tiff b/src/pyvale/data/hole_cam1_frame10.tiff new file mode 100644 index 000000000..bde6332e0 Binary files /dev/null and b/src/pyvale/data/hole_cam1_frame10.tiff differ diff --git a/src/pyvale/data/hydro_cam0_frame00.tiff b/src/pyvale/data/hydro_cam0_frame00.tiff new file mode 100644 index 000000000..31a026e58 Binary files /dev/null and b/src/pyvale/data/hydro_cam0_frame00.tiff differ diff --git a/src/pyvale/data/hydro_cam0_frame05.tiff b/src/pyvale/data/hydro_cam0_frame05.tiff new file mode 100644 index 000000000..f565c6b2d Binary files /dev/null and b/src/pyvale/data/hydro_cam0_frame05.tiff differ diff --git a/src/pyvale/data/hydro_cam0_frame10.tiff b/src/pyvale/data/hydro_cam0_frame10.tiff new file mode 100644 index 000000000..11d935243 Binary files /dev/null and b/src/pyvale/data/hydro_cam0_frame10.tiff differ diff --git a/src/pyvale/data/hydro_cam1_frame00.tiff b/src/pyvale/data/hydro_cam1_frame00.tiff new file mode 100644 index 000000000..5624508e7 Binary files /dev/null and b/src/pyvale/data/hydro_cam1_frame00.tiff differ diff --git a/src/pyvale/data/hydro_cam1_frame05.tiff b/src/pyvale/data/hydro_cam1_frame05.tiff new file mode 100644 index 000000000..6a9e190a7 Binary files /dev/null and b/src/pyvale/data/hydro_cam1_frame05.tiff differ diff --git a/src/pyvale/data/hydro_cam1_frame10.tiff b/src/pyvale/data/hydro_cam1_frame10.tiff new file mode 100644 index 000000000..d26a51557 Binary files /dev/null and b/src/pyvale/data/hydro_cam1_frame10.tiff differ diff --git a/src/pyvale/data/plate_hole_def0000.tiff b/src/pyvale/data/plate_hole_def0000.tiff deleted file mode 100644 index 3d43de86e..000000000 Binary files a/src/pyvale/data/plate_hole_def0000.tiff and /dev/null differ diff --git a/src/pyvale/data/plate_hole_def0001.tiff b/src/pyvale/data/plate_hole_def0001.tiff deleted file mode 100644 index 37e6e0a06..000000000 Binary files a/src/pyvale/data/plate_hole_def0001.tiff and /dev/null differ diff --git a/src/pyvale/data/plate_hole_ref0000.tiff b/src/pyvale/data/plate_hole_ref0000.tiff deleted file mode 100644 index 6e17de1f9..000000000 Binary files a/src/pyvale/data/plate_hole_ref0000.tiff and /dev/null differ diff --git a/src/pyvale/data/plate_rigid_def0000.tiff b/src/pyvale/data/plate_rigid_def0000.tiff deleted file mode 100644 index 823540b7c..000000000 Binary files a/src/pyvale/data/plate_rigid_def0000.tiff and /dev/null differ diff --git a/src/pyvale/data/plate_rigid_def0001.tiff b/src/pyvale/data/plate_rigid_def0001.tiff deleted file mode 100644 index 53beb7749..000000000 Binary files a/src/pyvale/data/plate_rigid_def0001.tiff and /dev/null differ diff --git a/src/pyvale/data/plate_rigid_def_25px.tiff b/src/pyvale/data/plate_rigid_def_25px.tiff deleted file mode 100644 index a0bdeb0db..000000000 Binary files a/src/pyvale/data/plate_rigid_def_25px.tiff and /dev/null differ diff --git a/src/pyvale/data/plate_rigid_def_50px.tiff b/src/pyvale/data/plate_rigid_def_50px.tiff deleted file mode 100644 index dea3dcfa1..000000000 Binary files a/src/pyvale/data/plate_rigid_def_50px.tiff and /dev/null differ diff --git a/src/pyvale/data/plate_rigid_ref0000.tiff b/src/pyvale/data/plate_rigid_ref0000.tiff deleted file mode 100644 index 72613a36a..000000000 Binary files a/src/pyvale/data/plate_rigid_ref0000.tiff and /dev/null differ diff --git a/src/pyvale/data/rigid_cam0_frame00.tiff b/src/pyvale/data/rigid_cam0_frame00.tiff new file mode 100644 index 000000000..31a026e58 Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame00.tiff differ diff --git a/src/pyvale/data/rigid_cam0_frame01.tiff b/src/pyvale/data/rigid_cam0_frame01.tiff new file mode 100644 index 000000000..7ab3355b3 Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame01.tiff differ diff --git a/src/pyvale/data/rigid_cam0_frame02.tiff b/src/pyvale/data/rigid_cam0_frame02.tiff new file mode 100644 index 000000000..35d1f5c1b Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame02.tiff differ diff --git a/src/pyvale/data/rigid_cam0_frame03.tiff b/src/pyvale/data/rigid_cam0_frame03.tiff new file mode 100644 index 000000000..f812f6460 Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame03.tiff differ diff --git a/src/pyvale/data/rigid_cam0_frame04.tiff b/src/pyvale/data/rigid_cam0_frame04.tiff new file mode 100644 index 000000000..4412cf9b7 Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame04.tiff differ diff --git a/src/pyvale/data/rigid_cam0_frame05.tiff b/src/pyvale/data/rigid_cam0_frame05.tiff new file mode 100644 index 000000000..609e8dd5d Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame05.tiff differ diff --git a/src/pyvale/data/rigid_cam0_frame06.tiff b/src/pyvale/data/rigid_cam0_frame06.tiff new file mode 100644 index 000000000..29874f00f Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame06.tiff differ diff --git a/src/pyvale/data/rigid_cam0_frame07.tiff b/src/pyvale/data/rigid_cam0_frame07.tiff new file mode 100644 index 000000000..287dc98af Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame07.tiff differ diff --git a/src/pyvale/data/rigid_cam0_frame08.tiff b/src/pyvale/data/rigid_cam0_frame08.tiff new file mode 100644 index 000000000..c3efeb7ab Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame08.tiff differ diff --git a/src/pyvale/data/rigid_cam0_frame09.tiff b/src/pyvale/data/rigid_cam0_frame09.tiff new file mode 100644 index 000000000..4f79ccec1 Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame09.tiff differ diff --git a/src/pyvale/data/rigid_cam0_frame10.tiff b/src/pyvale/data/rigid_cam0_frame10.tiff new file mode 100644 index 000000000..bda184f8b Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame10.tiff differ diff --git a/src/pyvale/data/rigid_cam0_frame11.tiff b/src/pyvale/data/rigid_cam0_frame11.tiff new file mode 100644 index 000000000..d7334fef0 Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame11.tiff differ diff --git a/src/pyvale/data/rigid_cam0_frame12.tiff b/src/pyvale/data/rigid_cam0_frame12.tiff new file mode 100644 index 000000000..46cd63fe5 Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame12.tiff differ diff --git a/src/pyvale/data/rigid_cam0_frame13.tiff b/src/pyvale/data/rigid_cam0_frame13.tiff new file mode 100644 index 000000000..a490bd7e5 Binary files /dev/null and b/src/pyvale/data/rigid_cam0_frame13.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame00.tiff b/src/pyvale/data/rigid_cam1_frame00.tiff new file mode 100644 index 000000000..5624508e7 Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame00.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame01.tiff b/src/pyvale/data/rigid_cam1_frame01.tiff new file mode 100644 index 000000000..76e00a031 Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame01.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame02.tiff b/src/pyvale/data/rigid_cam1_frame02.tiff new file mode 100644 index 000000000..3424c769d Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame02.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame03.tiff b/src/pyvale/data/rigid_cam1_frame03.tiff new file mode 100644 index 000000000..126b4cb27 Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame03.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame04.tiff b/src/pyvale/data/rigid_cam1_frame04.tiff new file mode 100644 index 000000000..c41dde4a1 Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame04.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame05.tiff b/src/pyvale/data/rigid_cam1_frame05.tiff new file mode 100644 index 000000000..b1e6db832 Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame05.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame06.tiff b/src/pyvale/data/rigid_cam1_frame06.tiff new file mode 100644 index 000000000..64a7baa6d Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame06.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame07.tiff b/src/pyvale/data/rigid_cam1_frame07.tiff new file mode 100644 index 000000000..e3d4b531e Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame07.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame08.tiff b/src/pyvale/data/rigid_cam1_frame08.tiff new file mode 100644 index 000000000..33df99e28 Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame08.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame09.tiff b/src/pyvale/data/rigid_cam1_frame09.tiff new file mode 100644 index 000000000..97c3921cf Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame09.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame10.tiff b/src/pyvale/data/rigid_cam1_frame10.tiff new file mode 100644 index 000000000..52bb86e61 Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame10.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame11.tiff b/src/pyvale/data/rigid_cam1_frame11.tiff new file mode 100644 index 000000000..3a14bef68 Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame11.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame12.tiff b/src/pyvale/data/rigid_cam1_frame12.tiff new file mode 100644 index 000000000..81e64be5f Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame12.tiff differ diff --git a/src/pyvale/data/rigid_cam1_frame13.tiff b/src/pyvale/data/rigid_cam1_frame13.tiff new file mode 100644 index 000000000..d39dbb3c4 Binary files /dev/null and b/src/pyvale/data/rigid_cam1_frame13.tiff differ diff --git a/src/pyvale/dataset/dataset.py b/src/pyvale/dataset/dataset.py index 1200d1687..0f648dd3e 100644 --- a/src/pyvale/dataset/dataset.py +++ b/src/pyvale/dataset/dataset.py @@ -354,7 +354,7 @@ def element_case_output_path(elem_type: EElemTest) -> Path: .joinpath(f"case00_{elem_type.value}_out.e")) -def dic_plate_with_hole_ref() -> Path: +def dic_plate_with_hole_cam0_ref() -> Path: """ Path to the reference image for the plate with hole example. 1040x1540 image in .tiff format. @@ -370,10 +370,28 @@ def dic_plate_with_hole_ref() -> Path: Path to the reference image (``.tiff``). """ return Path(files("pyvale.data") - .joinpath("plate_hole_ref0000.tiff")) + .joinpath("hole_cam0_frame00.tiff")) + +def dic_plate_with_hole_cam1_ref() -> Path: + """ + Path to the reference image for the plate with hole example. + 1040x1540 image in .tiff format. + + Parameters + ---------- + elem_type : EElemTest + Enumeration specifying the element type for this test case. + + Returns + ------- + Path + Path to the reference image (``.tiff``). + """ + return Path(files("pyvale.data") + .joinpath("hole_cam1_frame00.tiff")) -def dic_plate_with_hole_def() -> Path: +def dic_plate_with_hole_cam0_def() -> Path: """ Path to the deformed images for the plate with hole example. 1040x1540 image in .tiff format. @@ -389,12 +407,31 @@ def dic_plate_with_hole_def() -> Path: Path to the reference image (``.tiff``). """ return Path(files("pyvale.data") - .joinpath("plate_hole_def*.tiff")) + .joinpath("hole_cam0_frame*.tiff")) +def dic_plate_with_hole_cam1_def() -> Path: + """ + Path to the deformed images for the plate with hole example. + 1040x1540 image in .tiff format. -def dic_plate_rigid_ref() -> Path: + Parameters + ---------- + elem_type : EElemTest + Enumeration specifying the element type for this test case. + + Returns + ------- + Path + Path to the reference image (``.tiff``). + """ + return Path(files("pyvale.data") + .joinpath("hole_cam1_frame*.tiff")) + + + +def dic_plate_with_hydro_cam0_ref() -> Path: """ - Path to the reference image for the rigid deformation example. + Path to the reference image for the plate with hydro example. 1040x1540 image in .tiff format. Parameters @@ -408,12 +445,120 @@ def dic_plate_rigid_ref() -> Path: Path to the reference image (``.tiff``). """ return Path(files("pyvale.data") - .joinpath("plate_rigid_ref0000.tiff")) + .joinpath("hydro_cam0_frame00.tiff")) +def dic_plate_with_hydro_cam1_ref() -> Path: + """ + Path to the reference image for the plate with hydro example. + 1040x1540 image in .tiff format. + + Parameters + ---------- + elem_type : EElemTest + Enumeration specifying the element type for this test case. -def dic_plate_rigid_def() -> Path: + Returns + ------- + Path + Path to the reference image (``.tiff``). + """ + return Path(files("pyvale.data") + .joinpath("hydro_cam1_frame00.tiff")) + + +def dic_plate_with_hydro_cam0_def() -> Path: + """ + Path to the deformed images for the plate with hydro example. + 1040x1540 image in .tiff format. + + Parameters + ---------- + elem_type : EElemTest + Enumeration specifying the element type for this test case. + + Returns + ------- + Path + Path to the reference image (``.tiff``). + """ + return Path(files("pyvale.data") + .joinpath("hydro_cam0_frame*.tiff")) + +def dic_plate_with_hydro_cam1_def() -> Path: + """ + Path to the deformed images for the plate with hydro example. + 1040x1540 image in .tiff format. + + Parameters + ---------- + elem_type : EElemTest + Enumeration specifying the element type for this test case. + + Returns + ------- + Path + Path to the reference image (``.tiff``). """ - Path to the rigid deformation example images. + return Path(files("pyvale.data") + .joinpath("hydro_cam1_frame*.tiff")) + + + +def dic_plate_rigid_cam0_ref() -> Path: + """ + Path to the reference image for the rigid deformation example from camera 1 perspective. + 1040x1540 image in .tiff format. + + Parameters + ---------- + elem_type : EElemTest + Enumeration specifying the element type for this test case. + + Returns + ------- + Path + Path to the reference image (``.tiff``). + """ + return Path(files("pyvale.data") + .joinpath("rigid_cam0_frame00.tiff")) + + +def dic_plate_rigid_cam0_def() -> Path: + """ + Path to the rigid deformation example images from camera 0 perspective. + 1040x1540 image in .tiff format. + + Returns + ------- + Path + Path to the deformation images (``.tiff``). + """ + return Path(files("pyvale.data") + .joinpath("rigid_cam0_frame*.tiff")) + + +def dic_plate_rigid_cam1_ref() -> Path: + """ + Path to the reference image for the rigid deformation example from camera 1 perspective. + 1040x1540 image in .tiff format. + + Parameters + ---------- + elem_type : EElemTest + Enumeration specifying the element type for this test case. + + Returns + ------- + Path + Path to the reference image (``.tiff``). + """ + return Path(files("pyvale.data") + .joinpath("rigid_cam1_frame00.tiff")) + + +def dic_plate_rigid_cam1_def() -> Path: + """ + Path to the rigid deformation example images from camera 1 perspective. 1040x1540 image in .tiff format. Returns @@ -422,10 +567,31 @@ def dic_plate_rigid_def() -> Path: Path to the deformation images (``.tiff``). """ return Path(files("pyvale.data") - .joinpath("plate_rigid_def0*.tiff")) + .joinpath("rigid_cam1_frame*.tiff")) + +def dic_plate_rigid_cam0_def_small() -> list[Path]: + """ + Returns rigid_cam0_frame0000.tiff to rigid_cam1_frame0010.tiff. + """ + data_dir = files("pyvale.data") + + return [ + Path(data_dir.joinpath(f"rigid_cam0_frame{i:02d}.tiff")) + for i in range(11) + ] +def dic_plate_rigid_cam1_def_small() -> list[Path]: + """ + Returns rigid_cam1_frame0000.tiff to rigid_cam1_frame0010.tiff. + """ + data_dir = files("pyvale.data") + + return [ + Path(data_dir.joinpath(f"rigid_cam1_frame{i:02d}.tiff")) + for i in range(11) + ] -def dic_plate_rigid_def_25px() -> Path: +def dic_plate_rigid_cam0_def_10px() -> Path: """ Path to the 25px rigid deformation image. 1040x1540 image in .tiff format. @@ -435,10 +601,23 @@ def dic_plate_rigid_def_25px() -> Path: Path Path to the 25 px deformed image (``.tiff``). """ - return Path(files("pyvale.data").joinpath("plate_rigid_def_25px.tiff")) + return Path(files("pyvale.data").joinpath("rigid_cam0_frame11.tiff")) -def dic_plate_rigid_def_50px() -> Path: +def dic_plate_rigid_cam0_def_25px() -> Path: + """ + Path to the 25px rigid deformation image. + 1040x1540 image in .tiff format. + + Returns + ------- + Path + Path to the 25 px deformed image (``.tiff``). + """ + return Path(files("pyvale.data").joinpath("rigid_cam0_frame12.tiff")) + + +def dic_plate_rigid_cam0_def_50px() -> Path: """ Path to the 50px rigid deformation image. 1040x1540 image in .tiff format. @@ -448,12 +627,15 @@ def dic_plate_rigid_def_50px() -> Path: Path Path to the 50px deformed image (``.tiff``). """ - return Path(files("pyvale.data").joinpath("plate_rigid_def_50px.tiff")) + return Path(files("pyvale.data").joinpath("rigid_cam0_frame13.tiff")) -def dic_challenge_ref() -> Path: +def dic_chal_2d_ref() -> Path: """ - Path to the reference images for the 2D DIC challenge. + Path to the reference images for the 2D-DIC Challenge 2.0. + Images are openly available at: + + https://idics.org/challenge/ Returns ------- @@ -464,9 +646,12 @@ def dic_challenge_ref() -> Path: .joinpath("DIC_Challenge_Star_Noise_Ref.tiff")) -def dic_challenge_def() -> Path: +def dic_chal_2d_def() -> Path: """ - Path to the reference images for the 2D DIC challenge. + Path to the deformed images for the 2D-DIC Challenge 2.0. + Images are openly available at: + + https://idics.org/challenge/ Returns ------- @@ -476,6 +661,50 @@ def dic_challenge_def() -> Path: return Path(files("pyvale.data") .joinpath("DIC_Challenge_Star_Noise_Def.tiff")) + +def dic_chal_3d_cam0() -> Path: + """ + Path to the reference images for cam0 from the Stereo DIC challenge 1.0. + + Figures reproduced from: + + Ahmad, W., Helm, J., Bossuyt, S., et al. + "Stereo-DIC Challenge 1.0 – Rigid Body Motion of a Complex Shape", + Experimental Mechanics, 2024. + + Licensed under CC BY 4.0: + https://creativecommons.org/licenses/by/4.0/ + + Returns + ------- + Path + Path to the reference image (``.tiff``). + """ + return Path(files("pyvale.data") + .joinpath("Step01_00,00-sys1-0000_0.tif")) + + +def dic_chal_3d_cam1() -> Path: + """ + Path to the reference images for cam1 from the Stereo DIC challenge 1.0. + + Figures reproduced from: + + Ahmad, W., Helm, J., Bossuyt, S., et al. + "Stereo-DIC Challenge 1.0 – Rigid Body Motion of a Complex Shape", + Experimental Mechanics, 2024. + + Licensed under CC BY 4.0: + https://creativecommons.org/licenses/by/4.0/ + + Returns + ------- + Path + Path to the deformed image (``.tiff``). + """ + return Path(files("pyvale.data") + .joinpath("Step01_00,00-sys1-0000_1.tif")) + def cal_target() -> Path: """ Path to example calibration target. diff --git a/src/pyvale/dic/__init__.py b/src/pyvale/dic/__init__.py index 2b99608f3..00001f48a 100644 --- a/src/pyvale/dic/__init__.py +++ b/src/pyvale/dic/__init__.py @@ -5,11 +5,19 @@ #=============================================================================== from .dic2d import calculate_2d -from .dicdataimport import import_2d +from .dic3d import calculate_3d +from .dicimport2d import import_2d +from .dicimport3d import import_3d from .dicregionofinterest import RegionOfInterest from .dicresults import Results +from .diccpp import Bspline, Interpolator + __all__ = ["calculate_2d", + "calculate_3d", "RegionOfInterest", "import_2d", + "import_3d", + "Bspline", + "Interpolator", "Results"] diff --git a/src/pyvale/dic/cpp/bindings.cpp b/src/pyvale/dic/cpp/bindings.cpp index 1efafd3d8..0c8479c4a 100644 --- a/src/pyvale/dic/cpp/bindings.cpp +++ b/src/pyvale/dic/cpp/bindings.cpp @@ -4,25 +4,60 @@ // Copyright (C) 2025 The Computer Aided Validation Team // ================================================================================ -// pybind header files +// pybind11 header files #include #include #include #include +// Standard Library +#include + // common_cpp Header Files #include "../../common_cpp/util.hpp" // DIC Header files #include "./dicutil.hpp" #include "./dicmain.hpp" +#include "./dicinterp.hpp" +#include "./dicmultiwindow_util.hpp" +#include "./dicinterpBspline.hpp" namespace py = pybind11; -PYBIND11_MODULE(dic2dcpp, m) { +PYBIND11_MODULE(diccpp, m) { py::add_ostream_redirect(m, "ostream_redirect"); + py::enum_(m, "CorrCrit") + .value("SSD", util::CorrCrit::SSD) + .value("NSSD", util::CorrCrit::NSSD) + .value("ZNSSD", util::CorrCrit::ZNSSD); + + py::enum_(m, "ShapeFunc") + .value("RIGID", util::ShapeFunc::RIGID) + .value("AFFINE", util::ShapeFunc::AFFINE) + .value("QUAD", util::ShapeFunc::QUAD); + + py::enum_(m, "InterpRoutine") + .value("BSPLINE", util::InterpRoutine::BSPLINE) + .value("HERMITE", util::InterpRoutine::HERMITE); + + py::enum_(m, "ScanMethod") + .value("MULTIWINDOW_RG", util::ScanMethod::MULTIWINDOW_RG) + .value("SINGLEWINDOW_RG", util::ScanMethod::SINGLEWINDOW_RG) + .value("MULTIWINDOW", util::ScanMethod::MULTIWINDOW) + .value("RASTER", util::ScanMethod::RASTER); + + py::enum_(m, "IncrementalCond") + .value("IMAGE", util::IncrementalCond::IMAGE) + .value("ITER", util::IncrementalCond::ITER) + .value("COST", util::IncrementalCond::COST); + + py::enum_(m, "FFTPrecision") + .value("FLOAT32", util::FFTPrecision::FLOAT32) + .value("FLOAT64", util::FFTPrecision::FLOAT64); + py::class_(m, "Config") .def(py::init<>()) .def_readwrite("ss_step", &util::Config::ss_step) @@ -30,8 +65,8 @@ PYBIND11_MODULE(dic2dcpp, m) { .def_readwrite("max_iter", &util::Config::max_iter) .def_readwrite("precision", &util::Config::precision) .def_readwrite("threshold", &util::Config::threshold) - .def_readwrite("bf_threshold", &util::Config::bf_threshold) .def_readwrite("max_disp", &util::Config::max_disp) + .def_readwrite("epi_distance", &util::Config::epi_distance) .def_readwrite("corr_crit", &util::Config::corr_crit) .def_readwrite("shape_func", &util::Config::shape_func) .def_readwrite("interp_routine", &util::Config::interp_routine) @@ -39,14 +74,88 @@ PYBIND11_MODULE(dic2dcpp, m) { .def_readwrite("px_hori", &util::Config::px_hori) .def_readwrite("px_vert", &util::Config::px_vert) .def_readwrite("num_def_img", &util::Config::num_def_img) - .def_readwrite("rg_seed", &util::Config::rg_seed) + .def_readwrite("rg_seeds", &util::Config::rg_seeds) .def_readwrite("num_params", &util::Config::num_params) - .def_readwrite("fft_mad", &util::Config::fft_mad) - .def_readwrite("fft_mad_scale", &util::Config::fft_mad_scale) - .def_readwrite("filenames", &util::Config::filenames) - .def_readwrite("debug_level", &util::Config::debug_level); - + .def_readwrite("fft_filter", &util::Config::fft_filter) + .def_readwrite("fft_filter_threshold", &util::Config::fft_filter_threshold) + .def_readwrite("fft_filter_radius", &util::Config::fft_filter_radius) + .def_readwrite("fft_filter_corr_power", &util::Config::fft_filter_corr_power) + .def_readwrite("fft_save", &util::Config::fft_save) + .def_readwrite("fft_precision", &util::Config::fft_precision) + .def_readwrite("basenames", &util::Config::basenames) + .def_readwrite("fullpaths", &util::Config::fullpaths) + .def_readwrite("debug_level", &util::Config::debug_level) + .def_readwrite("stereo", &util::Config::stereo) + .def_readwrite("incremental", &util::Config::incremental) + .def_readwrite("incremental_update_cond", &util::Config::incremental_update_cond) + .def_readwrite("incremental_update_val", &util::Config::incremental_update_val); + + py::class_(m, "MultiwindowConfig") + .def(py::init<>()) + .def_readwrite("overlap", &MultiwindowConfig::overlap) + .def_readwrite("subset_size", &MultiwindowConfig::subset_size) + .def_readwrite("search_area", &MultiwindowConfig::search_area); + // Bind the engine function - m.def("dic_engine", &DICengine, "Run DIC analysis on input images with config"); -} + m.def("engine", &engine, "Run DIC analysis on input images with config"); + // interpolator bindings + py::class_(m, "InterpVals") + .def_readonly("f", &InterpVals::f) + .def_readonly("dfdx", &InterpVals::dfdx) + .def_readonly("dfdy", &InterpVals::dfdy); + + // ABC + py::class_(m, "Interpolator") + .def("eval", &Interpolator::eval) + .def("eval_dx", &Interpolator::eval_dx) + .def("eval_dy", &Interpolator::eval_dy) + .def("eval_and_derivs", &Interpolator::eval_and_derivs); + + // 2d b-spline interpolator + using U8Array = py::array_t; + using U16Array = py::array_t; + using U32Array = py::array_t; + + py::class_(m, "Bspline") + .def(py::init([](py::handle h) { + Image img; + + if (auto arr = U8Array::ensure(h)) { + if (arr.ndim() != 2) + throw std::runtime_error("Unsupported array: expected 2D array"); + + img.width = arr.shape(1); + img.height = arr.shape(0); + img.type = PixelType::UINT8; + img.data8.assign(arr.data(), arr.data() + img.width * img.height); + } else if (auto arr = U16Array::ensure(h)) { + if (arr.ndim() != 2) + throw std::runtime_error("Unsupported array: expected 2D array"); + + img.width = arr.shape(1); + img.height = arr.shape(0); + img.type = PixelType::UINT16; + img.data16.assign(arr.data(), arr.data() + img.width * img.height); + } else if (auto arr = U32Array::ensure(h)) { + if (arr.ndim() != 2) + throw std::runtime_error("Unsupported array: expected 2D array"); + + img.width = arr.shape(1); + img.height = arr.shape(0); + img.type = PixelType::UINT32; + img.data32.assign(arr.data(), arr.data() + img.width * img.height); + } else { + throw std::runtime_error( + "Unsupported array: expected 2D C-contiguous NumPy array of dtype uint8/uint16/uint32" + ); + } + + return Bspline(img); + })) + .def("eval", &Bspline::eval) + .def("eval_dx", &Bspline::eval_dx) + .def("eval_dy", &Bspline::eval_dy) + .def("eval_and_derivs", &Bspline::eval_and_derivs); + +} diff --git a/src/pyvale/dic/cpp/diccoarsefine.cpp b/src/pyvale/dic/cpp/diccoarsefine.cpp new file mode 100644 index 000000000..6b796b8af --- /dev/null +++ b/src/pyvale/dic/cpp/diccoarsefine.cpp @@ -0,0 +1,172 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + +#include +#include +#include +#include + +#include "./dicfourier.hpp" +#include "./diccoarsefine.hpp" + +namespace coarsefine { + + // Downsample an image by factor 2 using simple averaging + std::vector downsample(const double* img, int width, int height) { + int new_w = width / 2; + int new_h = height / 2; + std::vector out(new_w * new_h, 0.0); + for (int y = 0; y < new_h; ++y) { + for (int x = 0; x < new_w; ++x) { + out[y * new_w + x] = 0.25 * ( + img[(2*y) * width + (2*x) ] + + img[(2*y) * width + (2*x+1)] + + img[(2*y+1) * width + (2*x) ] + + img[(2*y+1) * width + (2*x+1)] + ); + } + } + return out; + } + + // Extract a subset patch centered at (cx, cy) from an image of given width + // Returns false if the patch would go out of bounds + bool extract_subset(const double* img, int img_w, int img_h, + int cx, int cy, int ss_size, + std::vector& patch) { + int half = ss_size / 2; + int x0 = cx - half, y0 = cy - half; + if (x0 < 0 || y0 < 0 || x0 + ss_size > img_w || y0 + ss_size > img_h) + return false; + patch.resize(ss_size * ss_size); + for (int y = 0; y < ss_size; ++y) + for (int x = 0; x < ss_size; ++x) + patch[y * ss_size + x] = img[(y0 + y) * img_w + (x0 + x)]; + return true; + } + + CoarseToFineResult coarse_to_fine_search( + const double* img_ref, + const double* img_def, + int img_w, + int img_h, + int center_x, // subset center in the reference image + int center_y, + int ss_size, // subset size at full resolution (e.g. 51) + int max_displacement, // max displacement at full resolution (e.g. 1000) + bool subpx, + const std::string& peak_method + ) { + // --- Build image pyramids --- + // Number of levels: enough to reduce max_displacement to ~1 pixel + int n_levels = static_cast(std::ceil(std::log2(max_displacement))) + 1; + + // Clamp so the image doesn't shrink below the subset size + while (n_levels > 1) { + int scale = 1 << (n_levels - 1); + if ((img_w / scale) < ss_size || (img_h / scale) < ss_size) + --n_levels; + else + break; + } + + // Build pyramid: level 0 = original, level n-1 = coarsest + std::vector> pyr_ref(n_levels); + std::vector> pyr_def(n_levels); + std::vector pyr_w(n_levels), pyr_h(n_levels); + + pyr_w[0] = img_w; pyr_h[0] = img_h; + // level 0 points into original data (we copy to vector for uniformity) + pyr_ref[0].assign(img_ref, img_ref + img_w * img_h); + pyr_def[0].assign(img_def, img_def + img_w * img_h); + + for (int lv = 1; lv < n_levels; ++lv) { + pyr_w[lv] = pyr_w[lv-1] / 2; + pyr_h[lv] = pyr_h[lv-1] / 2; + pyr_ref[lv] = downsample(pyr_ref[lv-1].data(), pyr_w[lv-1], pyr_h[lv-1]); + pyr_def[lv] = downsample(pyr_def[lv-1].data(), pyr_w[lv-1], pyr_h[lv-1]); + } + + // --- Coarse-to-fine refinement --- + double est_x = 0.0, est_y = 0.0; // displacement estimate (in full-res pixels) + CoarseToFineResult result; + + for (int lv = n_levels - 1; lv >= 0; --lv) { + double scale = static_cast(1 << lv); // full-res pixels per coarse pixel + + // Map full-res center and current estimate to this level + int lv_cx = static_cast(std::round(center_x / scale)); + int lv_cy = static_cast(std::round(center_y / scale)); + + // Predicted deformed center at this level + double pred_dx = est_x / scale; + double pred_dy = est_y / scale; + int lv_def_cx = static_cast(std::round(lv_cx + pred_dx)); + int lv_def_cy = static_cast(std::round(lv_cy + pred_dy)); + + // Clamp to image bounds (with half-subset margin) + int half = ss_size / 2; + lv_def_cx = std::clamp(lv_def_cx, half, pyr_w[lv] - half - 1); + lv_def_cy = std::clamp(lv_def_cy, half, pyr_h[lv] - half - 1); + lv_cx = std::clamp(lv_cx, half, pyr_w[lv] - half - 1); + lv_cy = std::clamp(lv_cy, half, pyr_h[lv] - half - 1); + + // Extract subsets + std::vector patch_ref, patch_def; + bool ok_ref = extract_subset(pyr_ref[lv].data(), pyr_w[lv], pyr_h[lv], + lv_cx, lv_cy, ss_size, patch_ref); + bool ok_def = extract_subset(pyr_def[lv].data(), pyr_w[lv], pyr_h[lv], + lv_def_cx, lv_def_cy, ss_size, patch_def); + if (!ok_ref || !ok_def) continue; + + // Run FFT cross-correlation + FFT fft(ss_size, ss_size, true); + fft.ss_ref.vals = patch_ref; + fft.ss_def.vals = patch_def; + // fft.zero_norm_subset(fft.ss_ref, ss_size, ss_size); + // fft.zero_norm_subset(fft.ss_def, ss_size, ss_size); + fft.correlate(); + + std::cout << std::endl; + for (int row = 0; row < ss_size; ++row) { + for (int col = 0; col < ss_size; ++col) { + int idx = row*ss_size+col; + std::cout << col << " " << row << " "; + std::cout << fft.ss_ref.x[idx] << " " << fft.ss_ref.y[idx] << " " << fft.ss_ref.vals[idx] << " "; + std::cout << fft.ss_def.x[idx] << " " << fft.ss_def.y[idx] << " " << fft.ss_def.vals[idx] << " "; + std::cout << fft.cross_corr[idx] << std::endl; + } + } + + // Get peak — use get_peak_offset which centers the result for you + double px, py, pval; + // Only use subpixel at the finest level to avoid over-refining coarse estimates + bool do_subpx = subpx && (lv == 0); + // fft.get_peak_offset(px, py, pval, do_subpx, peak_method); + // std::cout << px << " " << py << std::endl; + fft.get_peak(px, py, pval, do_subpx, peak_method); + + //std::cout << px << " " << py << std::endl; + + // Residual displacement at this level, scaled back to full-res pixels + double residual_x = px * scale; + double residual_y = py * scale; + + est_x += residual_x; + est_y += residual_y; + + if (lv == 0) { + result.disp_x = est_x; + result.disp_y = est_y; + result.peak_val = pval; + result.success = true; + } + } + + return result; + } + +} diff --git a/src/pyvale/dic/cpp/diccoarsefine.hpp b/src/pyvale/dic/cpp/diccoarsefine.hpp new file mode 100644 index 000000000..bf5e67f12 --- /dev/null +++ b/src/pyvale/dic/cpp/diccoarsefine.hpp @@ -0,0 +1,45 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + +#ifndef DICCOARSEFINE_H +#define DICCOARSEFINE_H + +#include +#include + +namespace coarsefine { + + // Downsample an image by factor 2 using simple averaging + std::vector downsample(const double* img, int width, int height); + + // Extract a subset patch centered at (cx, cy) from an image of given width + // Returns false if the patch would go out of bounds + bool extract_subset(const double* img, int img_w, int img_h, + int cx, int cy, int ss_size, + std::vector& patch); + + struct CoarseToFineResult { + double disp_x = 0.0; + double disp_y = 0.0; + double peak_val = 0.0; + bool success = false; + }; + + CoarseToFineResult coarse_to_fine_search( + const double* img_ref, + const double* img_def, + int img_w, + int img_h, + int center_x, // subset center in the reference image + int center_y, + int ss_size, // subset size at full resolution (e.g. 51) + int max_displacement, // max displacement at full resolution (e.g. 1000) + bool subpx = true, + const std::string& peak_method = "GAUSSIAN_2D" + ); + +} +#endif // DICCOARSEFINE_H diff --git a/src/pyvale/dic/cpp/diccoursefine.hpp b/src/pyvale/dic/cpp/diccoursefine.hpp new file mode 100644 index 000000000..bf5e67f12 --- /dev/null +++ b/src/pyvale/dic/cpp/diccoursefine.hpp @@ -0,0 +1,45 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + +#ifndef DICCOARSEFINE_H +#define DICCOARSEFINE_H + +#include +#include + +namespace coarsefine { + + // Downsample an image by factor 2 using simple averaging + std::vector downsample(const double* img, int width, int height); + + // Extract a subset patch centered at (cx, cy) from an image of given width + // Returns false if the patch would go out of bounds + bool extract_subset(const double* img, int img_w, int img_h, + int cx, int cy, int ss_size, + std::vector& patch); + + struct CoarseToFineResult { + double disp_x = 0.0; + double disp_y = 0.0; + double peak_val = 0.0; + bool success = false; + }; + + CoarseToFineResult coarse_to_fine_search( + const double* img_ref, + const double* img_def, + int img_w, + int img_h, + int center_x, // subset center in the reference image + int center_y, + int ss_size, // subset size at full resolution (e.g. 51) + int max_displacement, // max displacement at full resolution (e.g. 1000) + bool subpx = true, + const std::string& peak_method = "GAUSSIAN_2D" + ); + +} +#endif // DICCOARSEFINE_H diff --git a/src/pyvale/dic/cpp/dicfourier.cpp b/src/pyvale/dic/cpp/dicfourier.cpp index e481b5785..9cf6d958e 100644 --- a/src/pyvale/dic/cpp/dicfourier.cpp +++ b/src/pyvale/dic/cpp/dicfourier.cpp @@ -6,700 +6,456 @@ // STD library Header files +#define _USE_MATH_DEFINES +#include #include #include #include -#define _USE_MATH_DEFINES -#include -#include #include #include // Common Header files -#include "../../common_cpp/progressbar.hpp" -#include "../../common_cpp/defines.hpp" -#include "../../common_cpp/dicsignalhandler.hpp" // DIC Header files #include "dicfourier.hpp" #include "dicsubset.hpp" #include "dicinterp.hpp" -namespace fourier { - - std::vector shifts; - - void init(std::vector &window_data, - std::vector &ss_sizes, - std::vector &ss_steps, - const bool *img_roi, const util::Config &conf){ - - // timer for the initialisation - //Timer timer("entire FFT initislisation"); - - // loop over the window sizes - for (size_t i = 0; i < ss_sizes.size(); i++) { - - const int window_size = ss_sizes[i]; - const int window_step = ss_steps[i]; - - // generate subset information for each window size. - // for the last size all subsets need to sit within the ROI - if (i==ss_sizes.size()-1) - window_data.push_back(subset::create_grid(img_roi, window_step, - window_size, conf.px_hori, - conf.px_vert, false)); - else - window_data.push_back(subset::create_grid(img_roi, window_step, - window_size, conf.px_hori, - conf.px_vert, true)); - - // shifts for each subset size - Shift shift; - shift.max_num_neigh = 4; - - // resize vectors - shift.x.resize(window_data[i].num); - shift.y.resize(window_data[i].num); - shift.cost.resize(window_data[i].num); - shift.max_val.resize(window_data[i].num); - - // we need the neighbours in the previous window size for all sizes - // except the first - if (i > 0){ - shift.gen_neighlist(window_data[i], window_data[i-1]); - } - - // add the shifts for the current window to the vector - shifts.push_back(shift); - } - } - - void remove_outliers(std::vector& shift, - const subset::Grid &ss_grid, - const double mad_scale) { - - std::vector updated = shift; - - int radius = 2; - - for (int ss = 0; ss < ss_grid.num; ss++) { - - // subset coords - int ss_x = ss_grid.coords[2*ss]; - int ss_y = ss_grid.coords[2*ss+1]; - - // subset x and y index in 2d mask - int idx_x = ss_x / ss_grid.step; - int idx_y = ss_y / ss_grid.step; - - std::vector neigh_vals; - - int min_x = std::max(0, idx_x-radius); - int min_y = std::max(0, idx_y-radius); - int max_y = std::min(ss_grid.num_ss_y, idx_y+radius+1); - int max_x = std::min(ss_grid.num_ss_x, idx_x+radius+1); - - for (int y = min_y; y < max_y; ++y) { - for (int x = min_x; x < max_x; ++x) { - - // index of neighbour - int nss_idx = ss_grid.mask[y*ss_grid.num_ss_x+x]; - // check if invalid neigh - if (nss_idx == -1 || nss_idx == ss) continue; - - neigh_vals.push_back(shift[nss_idx]); - } - } - - if (neigh_vals.size() < 4) continue; +// Helper: returns the center offset (0-based) for a given size +// odd size N -> (N-1)/2 e.g. 31 -> 15 +// even size N -> (N-1)/2.0 e.g. 32 -> 15.5 +inline double half_offset(int size) { + return (size - 1) * 0.5; +} - // Median - std::sort(neigh_vals.begin(), neigh_vals.end()); - size_t sz = neigh_vals.size(); - double median = (sz % 2 == 0) ? 0.5 * (neigh_vals[sz/2 - 1] + neigh_vals[sz/2]) : neigh_vals[sz/2]; - // MAD - std::vector abs_devs; - abs_devs.reserve(sz); - for (double v : neigh_vals) abs_devs.push_back(std::abs(v - median)); +void smooth_field(std::vector& shift, + const subset::Grid& ss_grid, + double sigma = 1.0, + int radius = 2) { - std::sort(abs_devs.begin(), abs_devs.end()); - double mad = (sz % 2 == 0) ? 0.5 * (abs_devs[sz/2 - 1] + abs_devs[sz/2]) : abs_devs[sz/2]; + std::vector smoothed = shift; - if (mad < 1e-12) continue; + const int width = ss_grid.num_ss_x; + const int height = ss_grid.num_ss_y; - if (std::abs(shift[ss] - median) > mad_scale * mad) { - updated[ss] = median; - } + // Precompute Gaussian weights + std::vector> weights(2 * radius + 1, std::vector(2 * radius + 1)); + for (int dy = -radius; dy <= radius; ++dy) { + for (int dx = -radius; dx <= radius; ++dx) { + double dist2 = dx * dx + dy * dy; + weights[dy + radius][dx + radius] = std::exp(-dist2 / (2.0 * sigma * sigma)); } - shift = std::move(updated); } - void multiwindow(const std::vector &ss_grid, - const double *img_ref, - const double *img_def, - const Interpolator &interp_def, - const bool fft_mad, - const double fft_mad_scale){ - - const int px_hori = interp_def.px_hori; - const int px_vert = interp_def.px_vert; - - // TODO: Add a proper flag for this - bool subpx = true; - - // Loop over window size - for (size_t i = 0; i < ss_grid.size(); i++){ - - //Timer timer("FFT windowing for subset size: " + std::to_string(ss_grid[i].size)); - - const int ss_size = ss_grid[i].size; - const int num_ss = ss_grid[i].num; - - std::fill(shifts[i].x.begin(), shifts[i].x.end(), 0.0); - std::fill(shifts[i].y.begin(), shifts[i].y.end(), 0.0); - - std::string bar_title = "FFT windowing for size " + std::to_string(ss_size) + ":"; - ProgressBar pbar(bar_title, ss_grid[i].num); - std::atomic current_progress = 0; - - - #pragma omp parallel shared(stop_request, shifts, ss_grid, interp_def, ss_size) - { - - - // class for FFT - fourier::FFT fft(ss_size); + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { - // loop over subsets for each size/step - #pragma omp for schedule(dynamic,10) - for (int ss = 0; ss < ss_grid[i].num; ss++){ + int center_idx = ss_grid.mask[y * width + x]; + if (center_idx == -1) continue; - // exit when ctrl+C - if (stop_request) continue; + double sum = 0.0; + double weight_sum = 0.0; - const int ss_x = ss_grid[i].coords[2*ss]; - const int ss_y = ss_grid[i].coords[2*ss+1]; + for (int dy = -radius; dy <= radius; ++dy) { + int ny = y + dy; + if (ny < 0 || ny >= height) continue; - // get the seed for the new window size - auto [prev_x, prev_y] = get_prev_shift(i, ss, ss_x, ss_y, shifts, ss_grid); - double ss_x_shft = ss_x+prev_x; - double ss_y_shft = ss_y+prev_y; + for (int dx = -radius; dx <= radius; ++dx) { + int nx = x + dx; + if (nx < 0 || nx >= width) continue; - // populate fft.ss_ref with reference subset values - subset::get_px_from_img(fft.ss_ref,ss_x, ss_y, px_hori, px_vert, img_ref); + int n_idx = ss_grid.mask[ny * width + nx]; + if (n_idx == -1) continue; - // populate fft.ss_def with interpolator value - subset::get_subpx_from_img(fft.ss_def, ss_x_shft, ss_y_shft, interp_def); + double val = shift[n_idx]; + double weight = weights[dy + radius][dx + radius]; - // zero normalise the subsets - zero_norm_subsets(fft.ss_ref.vals, fft.ss_def.vals, ss_size); - - // get peaks from the cross correlation - double peak_x = 0, peak_y = 0, max_val = 0.0; - fft.correlate(); - fft.find_peak(peak_x, peak_y, max_val, subpx, "GAUSSIAN_2D"); - - - // update the shift arrays - if (i == 0){ - shifts[i].x[ss] = peak_x; - shifts[i].y[ss] = peak_y; - } - else { - shifts[i].x[ss] = prev_x+peak_x; - shifts[i].y[ss] = prev_y+peak_y; - } - - // this isn't essential. storing peak amplitude and cost value for shifts - //subset::get_subpx_from_img(fft.ss_def, ss_x+shifts[i].x[ss], ss_y+shifts[i].y[ss], interp_def); - //shifts[i].cost[ss] = debugcost(fft.ss_ref,fft.ss_def); - shifts[i].max_val[ss] = max_val; - - - if (g_debug_level>0){ - int progress = current_progress.fetch_add(1); - if (omp_get_thread_num()==0) pbar.update(progress+1); - } + sum += val * weight; + weight_sum += weight; } } - // remove outliers in fft - if (fft_mad){ - remove_outliers(shifts[i].x, ss_grid[i], fft_mad); - remove_outliers(shifts[i].y, ss_grid[i], fft_mad); - } - - //smooth_field(shifts[i].x, ss_grid[i], 7.0, 5); - //smooth_field(shifts[i].y, ss_grid[i], 7.0, 5); - - //for (int ss = 0; ss < ss_grid[i].num; ss++){ - // std::cout << ss_grid[i].coords[2*ss] << " " << ss_grid[i].coords[2*ss+1] << " "; - // std::cout << shifts[i].x[ss] << " " << shifts[i].y[ss] << " "; - // std::cout << shifts[i].max_val[ss] << " "; - // std::cout << shifts[i].cost[ss] << std::endl; - //} - //std::cout << std::endl; - - if (g_debug_level>0){ - pbar.update(current_progress); - pbar.finish(); + if (weight_sum > 0.0) { + smoothed[center_idx] = sum / weight_sum; } } + } + + shift = std::move(smoothed); +} +double debugcost(const subset::Pixels &ss_ref, const subset::Pixels &ss_def){ + const int num_px = ss_def.num_px; + double cost = 0.0; + double mean_ref = 0.0; + double mean_def = 0.0; + // loop over pixel values in reference image + for (int i = 0; i < num_px; i++){ + mean_ref += ss_ref.vals[i]; + mean_def += ss_def.vals[i]; } + mean_ref /= num_px; + mean_def /= num_px; + + // get cost function denominators + double sum_squared_ref = 0.0; + double sum_squared_def = 0.0; + for (int i = 0; i < num_px; ++i) { + sum_squared_ref += (ss_ref.vals[i] - mean_ref)* + (ss_ref.vals[i] - mean_ref); + sum_squared_def += (ss_def.vals[i] - mean_def)* + (ss_def.vals[i] - mean_def); + } + double inv_sum_squared_ref = 1.0 / std::sqrt(sum_squared_ref); + double inv_sum_squared_def = 1.0 / std::sqrt(sum_squared_def); - std::pair get_prev_shift(const int i, const int ss, - const double ss_x, const double ss_y, - const std::vector& shifts, - const std::vector& ss_grid) { - const double epsilon = 10.0; - double weight_sum_x = 0.0; - double weight_sum_y = 0.0; - double weight_tot = 0.0; - double prev_x = 0; - double prev_y = 0; - double sum_x = 0; - double sum_y = 0; - - // assign values for all subset sizes EXCEPT first - if (i > 0){ - - // weighted average of 4 nearest neighbours - for (size_t j = 0; j < shifts[i].num_neigh_list[ss]; ++j) { - - int nidx = shifts[i].neigh_list[ss*shifts[i].max_num_neigh+j]; - int neigh_x = ss_grid[i-1].coords[2*nidx]; - int neigh_y = ss_grid[i-1].coords[2*nidx+1]; - - double dx = ss_x - neigh_x; - double dy = ss_y - neigh_y; - double dist_sq = dx * dx + dy * dy; - - double weight = 1.0 / (dist_sq + epsilon); - - //sum_x += shifts[i-1].x[nidx]; - //sum_y += shifts[i-1].y[nidx]; - weight_sum_x += shifts[i-1].x[nidx] * weight; - weight_sum_y += shifts[i-1].y[nidx] * weight; - weight_tot += weight; - } - //prev_x = sum_x / shifts[i].num_neigh_list[ss]; - //prev_y = sum_y / shifts[i].num_neigh_list[ss]; - prev_x = weight_sum_x / weight_tot; - prev_y = weight_sum_y / weight_tot; - } - return {prev_x, prev_y}; + // calcualte cost + for (int i = 0; i < num_px; i++){ + double def_norm = ss_def.vals[i] * inv_sum_squared_def; + double ref_norm = ss_ref.vals[i] * inv_sum_squared_ref; + cost += (ref_norm - def_norm) * (ref_norm - def_norm); } + return cost; +} - void smooth_field(std::vector& shift, - const subset::Grid& ss_grid, - double sigma = 1.0, - int radius = 2) { - std::vector smoothed = shift; - const int width = ss_grid.num_ss_x; - const int height = ss_grid.num_ss_y; - // Precompute Gaussian weights - std::vector> weights(2 * radius + 1, std::vector(2 * radius + 1)); - for (int dy = -radius; dy <= radius; ++dy) { - for (int dx = -radius; dx <= radius; ++dx) { - double dist2 = dx * dx + dy * dy; - weights[dy + radius][dx + radius] = std::exp(-dist2 / (2.0 * sigma * sigma)); +template +void fill_fft_window_from_img_subpx(FFTPixels &ss_def, + const double subpx_x, + const double subpx_y, + const Interpolator &interp_def) { + int count = 0; + for (int y = 0; y < ss_def.size_y; y++) { + for (int x = 0; x < ss_def.size_x; x++) { + const double px_x = subpx_x + x; + const double px_y = subpx_y + y; + if (ss_def.has_coords()) { + ss_def.x[count] = px_x; + ss_def.y[count] = px_y; } + ss_def.vals[count] = static_cast(interp_def.eval(0, 0, px_x, px_y)); + count++; } + } +} - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - - int center_idx = ss_grid.mask[y * width + x]; - if (center_idx == -1) continue; - - double sum = 0.0; - double weight_sum = 0.0; - - for (int dy = -radius; dy <= radius; ++dy) { - int ny = y + dy; - if (ny < 0 || ny >= height) continue; - - for (int dx = -radius; dx <= radius; ++dx) { - int nx = x + dx; - if (nx < 0 || nx >= width) continue; - - int n_idx = ss_grid.mask[ny * width + nx]; - if (n_idx == -1) continue; - - double val = shift[n_idx]; - double weight = weights[dy + radius][dx + radius]; - - sum += val * weight; - weight_sum += weight; - } - } - - if (weight_sum > 0.0) { - smoothed[center_idx] = sum / weight_sum; - } +template +void get_single_window_fftcc_peak(FFTImpl &fft, + std::vector &p, + double &max_val, + const double cx, const double cy, + const int ss_size_x, + const int ss_size_y, + const int window_size_x, + const int window_size_y, + const Image &img_ref, const Image &img_def, + const Interpolator &interp_def, + const bool debug){ + + // some consts + const int px_hori = interp_def.px_hori; + const int px_vert = interp_def.px_vert; + + // reset p values + std::fill(p.begin(), p.end(),0.0); + + // TODO: Add a proper flag for this + bool subpx = true; + + // top left corner of the subset + int corner_x = (int)std::floor(cx - (ss_size_x - 1) * 0.5); + int corner_y = (int)std::floor(cy - (ss_size_y - 1) * 0.5); + + fill_fft_window_with_subset_at_corner(fft.ss_ref, img_ref, + corner_x, corner_y, px_hori, px_vert, + ss_size_x, ss_size_y, + window_size_x, window_size_y); + + // populate deformed subset + fill_fft_window_from_img_subpx(fft.ss_def, + cx - half_offset(window_size_x), + cy - half_offset(window_size_y), + interp_def); + + // zero norm the subsets + bool normed_ref = fft.zero_norm_subset(fft.ss_ref, ss_size_x,ss_size_y); + bool normed_def = fft.zero_norm_subset(fft.ss_def, window_size_x,window_size_y); + + // get peaks from the cross correlation + double peak_x = 0.0, peak_y = 0.0; + + if (normed_ref && normed_def){ + fft.correlate(); + fft.get_peak_nowrap(peak_x, peak_y, max_val, subpx, "GAUSSIAN_2D"); + } + + // coordinate transform + p[0] = peak_x - half_offset(window_size_x); + p[1] = peak_y - half_offset(window_size_y); + + // debugging + if (debug) { + for (int row = 0; row < window_size_y; ++row) { + for (int col = 0; col < window_size_x; ++col) { + int idx = row*window_size_x+col; + std::cout << col << " " << row << " "; + std::cout << fft.ss_ref.x[idx] << " " << fft.ss_ref.y[idx] << " " << fft.ss_ref.vals[idx] << " "; + std::cout << fft.ss_def.x[idx] << " " << fft.ss_def.y[idx] << " " << fft.ss_def.vals[idx] << " "; + std::cout << fft.cross_corr[idx] << std::endl; } } - - shift = std::move(smoothed); + std::cout << std::endl; } +} - double debugcost(const subset::Pixels &ss_ref, const subset::Pixels &ss_def){ - const int num_px = ss_def.num_px; - double cost = 0.0; - double mean_ref = 0.0; - double mean_def = 0.0; +template +void get_single_window_fftcc_peak_centre(FFTImpl &fft, + std::vector &p, + double &max_val, + const double cx, const double cy, + const double offset_x, const double offset_y, + const int ss_size_x, + const int ss_size_y, + const int window_size_x, + const int window_size_y, + const Interpolator &interp_ref, + const Interpolator &interp_def, + const bool debug){ - // loop over pixel values in reference image - for (int i = 0; i < num_px; i++){ - mean_ref += ss_ref.vals[i]; - mean_def += ss_def.vals[i]; - } + // some consts + const int px_hori = interp_def.px_hori; + const int px_vert = interp_def.px_vert; + const double window_half_x = half_offset(window_size_x); + const double window_half_y = half_offset(window_size_y); + const double ss_half_x = half_offset(ss_size_x); + const double ss_half_y = half_offset(ss_size_y); - mean_ref /= num_px; - mean_def /= num_px; - - // get cost function denominators - double sum_squared_ref = 0.0; - double sum_squared_def = 0.0; - for (int i = 0; i < num_px; ++i) { - sum_squared_ref += (ss_ref.vals[i] - mean_ref)* - (ss_ref.vals[i] - mean_ref); - sum_squared_def += (ss_def.vals[i] - mean_def)* - (ss_def.vals[i] - mean_def); - } - double inv_sum_squared_ref = 1.0 / std::sqrt(sum_squared_ref); - double inv_sum_squared_def = 1.0 / std::sqrt(sum_squared_def); + // reset p values + std::fill(p.begin(), p.end(),0.0); - // calcualte cost - for (int i = 0; i < num_px; i++){ - double def_norm = ss_def.vals[i] * inv_sum_squared_def; - double ref_norm = ss_ref.vals[i] * inv_sum_squared_ref; - cost += (ref_norm - def_norm) * (ref_norm - def_norm); - } - return cost; - } + // TODO: Add a proper flag for this + bool subpx = true; + fill_fft_window_with_subset_at_centre(fft.ss_ref, interp_ref, + cx, cy, px_hori, px_vert, + ss_size_x, ss_size_y, + window_size_x, window_size_y); + // populate deformed subset + fill_fft_window_from_img_subpx(fft.ss_def, + cx-window_half_x+offset_x, + cy-window_half_y+offset_y, + interp_def); - void zero_norm_subsets(std::vector& ref_vals, std::vector& def_vals, const int ss_size) { - const int total_px = ss_size * ss_size; + // zero norm the subsets + bool normed_ref = fft.zero_norm_subsets_centered(fft.ss_ref,ss_size_x,ss_size_y, window_size_x, window_size_y); + bool normed_def = fft.zero_norm_subset(fft.ss_def, window_size_x,window_size_y); - // Compute means - double mean_def = 0.0; - double mean_ref = 0.0; - for (int i = 0; i < total_px; ++i) { - mean_def += def_vals[i]; - mean_ref += ref_vals[i]; - } - mean_def /= total_px; - mean_ref /= total_px; - - // Compute standard deviations - double std_def = 0.0; - double std_ref = 0.0; - for (int i = 0; i < total_px; ++i) { - std_def += std::pow(def_vals[i] - mean_def, 2); - std_ref += std::pow(ref_vals[i] - mean_ref, 2); - } - std_def = std::sqrt(std_def / total_px); - std_ref = std::sqrt(std_ref / total_px); + // get peaks from the cross correlation + double peak_x = 0.0, peak_y = 0.0; - // Normalize - for (int i = 0; i < total_px; ++i) { - def_vals[i] = (def_vals[i] - mean_def) / std_def; - ref_vals[i] = (ref_vals[i] - mean_ref) / std_ref; - } + if (normed_ref && normed_def){ + fft.correlate(); + fft.get_peak(peak_x, peak_y, max_val, subpx, "GAUSSIAN_2D"); } - - - void zero_norm_subsets_single(std::vector& ref_vals, int ss_size) { - const int total_px = ss_size * ss_size; - - // Compute means - double mean_def = 0.0; - double mean_ref = 0.0; - for (int i = 0; i < total_px; ++i) { - mean_ref += ref_vals[i]; - } - mean_def /= total_px; - mean_ref /= total_px; - - // Compute standard deviations - double std_def = 0.0; - double std_ref = 0.0; - for (int i = 0; i < total_px; ++i) { - std_ref += std::pow(ref_vals[i] - mean_ref, 2); - } - std_def = std::sqrt(std_def / total_px); - std_ref = std::sqrt(std_ref / total_px); - - // Normalize - for (int i = 0; i < total_px; ++i) { - ref_vals[i] = (ref_vals[i] - mean_ref) / std_ref; + // coordinate transform + p[0] = peak_x; + p[1] = peak_y; + + //debugging + if (debug) { + for (int row = 0; row < window_size_y; ++row) { + for (int col = 0; col < window_size_x; ++col) { + int idx = row*window_size_x+col; + std::cout << col << " " << row << " "; + std::cout << fft.ss_ref.x[idx] << " " << fft.ss_ref.y[idx] << " " << fft.ss_ref.vals[idx] << " "; + std::cout << fft.ss_def.x[idx] << " " << fft.ss_def.y[idx] << " " << fft.ss_def.vals[idx] << " "; + std::cout << fft.cross_corr[idx] << std::endl; + } } + std::cout << std::endl; } +} +template +void fill_fft_window_with_subset_at_centre(FFTPixels &ss_ref, + const Interpolator &interp_ref, + const double cx, + const double cy, + const int px_hori, + const int px_vert, + const int ss_size_x, + const int ss_size_y, + const int window_size_x, + const int window_size_y) { - void single_grid(const subset::Grid &ss_grid, - const double *prev_img_u, - const double *prev_img_v, - const int window_size, - const double *img_ref, - const double *img_def, - const Interpolator &interp_def){ - - - const int px_hori = interp_def.px_hori; - const int px_vert = interp_def.px_vert; - - const int ss_size = ss_grid.size; - - const int window_half = window_size/2; - - // TODO: Add a proper flag for this - bool subpx = true; - - // Loop over window size - #pragma omp parallel shared(stop_request, shifts, ss_grid, interp_def, ss_size) - { - - // class for FFT - int tid = omp_get_thread_num(); - fourier::FFT fft(window_size); - - // loop over subsets for each size/step - #pragma omp for schedule(dynamic,10) - for (int ss = 0; ss < ss_grid.num; ss++){ - - int ss_x = ss_grid.coords[2*ss] + prev_img_u[ss]; - int ss_y = ss_grid.coords[2*ss+1] + prev_img_v[ss]; - - // Clamp the deformed window to image range - int ss_x_shft, ss_y_shft; - ss_x_shft = std::clamp(ss_x - window_size/2 + ss_size/2, 0, px_hori - window_size); - ss_y_shft = std::clamp(ss_y - window_size/2 + ss_size/2, 0, px_vert - window_size); - - - // Offsets to center the subset within the larger window - int offset = window_half - ss_size/2; - int offset_x, offset_y; - offset_x = std::min(offset, ss_x); - offset_y = std::min(offset, ss_y); - - - for (int row = 0; row < ss_size; ++row) { - for (int col = 0; col < ss_size; ++col) { + const double w_half_x = half_offset(window_size_x); // e.g. 15.5 for 32 + const double w_half_y = half_offset(window_size_y); + const double ss_half_x = half_offset(ss_size_x); + const double ss_half_y = half_offset(ss_size_y); - // Source coordinates in img_def - int px_y = ss_y + row; - int px_x = ss_x + col; + // col offset runs over ss pixels: 0, 1, ..., ss_size_x-1 + // pixel coord = cx - ss_half_x + col (exact, works for even & odd) + // window coord = col + (w_half_x - ss_half_x) (centred in window) - if (px_x >= px_hori || px_y >= px_vert) { - std::cout << "original subset coords: " << ss_grid.coords[2*ss] << ss_grid.coords[2*ss+1] << std::endl; - std::cout << "previous displacements: " << prev_img_u[ss] << prev_img_v[ss] << std::endl; - std::cout << "Image access out of bounds!" << std::endl; - exit(0); - continue; - } + const double win_origin_x = w_half_x - ss_half_x; // offset of ss[0] in window + const double win_origin_y = w_half_y - ss_half_y; - // Target coordinates in ss_ref - int target_y = offset_y + row; - int target_x = offset_x + col; + for (int row = 0; row < ss_size_y; ++row) { + for (int col = 0; col < ss_size_x; ++col) { - int idx_img = px_y * px_hori + px_x; - int idx_window = target_y * window_size + target_x; + // true (possibly fractional) image coordinate + double px_x = (cx - ss_half_x) + col; + double px_y = (cy - ss_half_y) + row; - if (idx_window >= window_size*window_size){ - std::cout << "idx_window out of bounds: " << idx_window << " ss_x: " << ss_x << " ss_y: " << ss_y << " target_x: " << target_x << " target_y: " << target_y << std::endl; - exit(0); - } + // nearest integer pixel for bounds check & x/y storage + int ipx_x = (int)std::round(px_x); + int ipx_y = (int)std::round(px_y); - // On-the-fly 2D Hann window - const double hann_row = 0.5 * (1.0 - cos(2.0 * M_PI * row / (ss_size - 1))); - const double hann_col = 0.5 * (1.0 - cos(2.0 * M_PI * col / (ss_size - 1))); - const double window_val = hann_row * hann_col; + // window index — round to nearest integer window cell + int target_x = (int)std::round(win_origin_x + col); + int target_y = (int)std::round(win_origin_y + row); - fft.ss_ref.vals[idx_window] = img_ref[idx_img] * window_val; - } - } + int idx_window = target_y * window_size_x + target_x; - // populate fft.ss_def with interpolator values - subset::get_subpx_from_img(fft.ss_def, ss_x_shft, ss_y_shft, interp_def); - - // TODO: Make hanning window calc part of the initialization process. - for (int row = 0; row < window_size; ++row) { - for (int col = 0; col < window_size; ++col) { - // On-the-fly 2D Hann window - const double hann_row = 0.5 * (1.0 - cos(2.0 * M_PI * row / (window_size - 1))); - const double hann_col = 0.5 * (1.0 - cos(2.0 * M_PI * col / (window_size - 1))); - const double window_val = hann_row * hann_col; - fft.ss_def.vals[row*window_size+col]*=window_val; - } - } + if (ss_ref.has_coords()) { + ss_ref.x[idx_window] = px_x; + ss_ref.y[idx_window] = px_y; + } - // get peaks from the cross correlation - double peak_x = 0, peak_y = 0, max_val = 0.0; - fft.correlate_phase(); - fft.find_peak_offset(peak_x, peak_y, max_val, subpx, "gaussian_2d"); - - // resetting values in fft_ref to 0.0. - for (int row = 0; row < ss_size; ++row) { - for (int col = 0; col < ss_size; ++col) { - - // Target coordinates in ss_ref - int target_y = offset_y + row; - int target_x = offset_x + col; - int idx_window = target_y * window_size + target_x; - - if (idx_window >= window_size*window_size){ - std::cout << "idx_window out of bounds: " << idx_window << " ss_x: " << ss_x << " ss_y: " << ss_y << " target_x: " << target_x << " target_y: " << target_y << std::endl; - exit(0); - } - fft.ss_ref.vals[idx_window] = 0.0; - } - } - // update the shift arrays - shifts.back().x[ss] = peak_x; - shifts.back().y[ss] = peak_y; + if (ipx_x < 0 || ipx_x >= px_hori || ipx_y < 0 || ipx_y >= px_vert) { + ss_ref.vals[idx_window] = Real(0); + } else { + ss_ref.vals[idx_window] = static_cast(interp_ref.eval(0, 0, px_x, px_y)); } } - // remove outliers in fft - //if (fft_mad){ - // remove_outliers(shifts[i].x, ss_grid[i], fft_mad); - // remove_outliers(shifts[i].y, ss_grid[i], fft_mad); - //} } +} - void get_single_window_fftcc_peak(double &peak_x, double &peak_y, - const int ss_x, const int ss_y, - const int ss_size, const int window_size, - const double *img_ref, const double *img_def, - const Interpolator &interp_def){ - - const int px_hori = interp_def.px_hori; - const int px_vert = interp_def.px_vert; - const int window_half = window_size/2; - const int ss_half = ss_size/2; - - // class for FFT - fourier::FFT fft(window_size); - - // TODO: Add a proper flag for this - bool subpx = true; - - - // Clamp the deformed window to image range - int ss_x_shft, ss_y_shft; - ss_x_shft = std::clamp(ss_x - window_half + ss_half, 0, px_hori - window_size); - ss_y_shft = std::clamp(ss_y - window_half + ss_half, 0, px_vert - window_size); - - // Offsets to center the subset within the larger window - int offset = window_half - ss_half; - int offset_x, offset_y; - offset_x = std::min(offset, ss_x); - offset_y = std::min(offset, ss_y); - - - for (int row = 0; row < ss_size; ++row) { - for (int col = 0; col < ss_size; ++col) { - - // Source coordinates in img_def - int px_y = ss_y + row; - int px_x = ss_x + col; - - // Target coordinates in ss_ref - int target_y = offset_y + row; - int target_x = offset_x + col; - - int idx_img = px_y * px_hori + px_x; - int idx_window = target_y * window_size + target_x; +template +void fill_fft_window_with_subset_at_corner_impl(FFTPixels &ss_ref, + const std::vector &img, + const int corner_x, + const int corner_y, + const int px_hori, + const int px_vert, + const int ss_size_x, + const int ss_size_y, + const int window_size_x, + const int window_size_y){ - if (idx_window >= window_size*window_size){ - std::cout << "idx_window out of bounds: " << idx_window << " ss_x: " << ss_x << " ss_y: " << ss_y << " target_x: " << target_x << " target_y: " << target_y << std::endl; - continue; - } - if (px_x >= px_hori || px_y >= px_vert) { - std::cout << "Subset coords: (" << ss_x << ", " << ss_y << "). " << std::endl; - std::cout << "Image access out of bounds!" << std::endl; - continue; - } + // Iterate over subset pixels using offsets relative to the subset center + for (int row = 0; row < ss_size_y; ++row) { + for (int col = 0; col < ss_size_x; ++col) { - // On-the-fly 2D Hann window - const double hann_row = 0.5 * (1.0 - cos(2.0 * M_PI * row / (ss_size - 1))); - const double hann_col = 0.5 * (1.0 - cos(2.0 * M_PI * col / (ss_size - 1))); - const double window_val = hann_row * hann_col; + int px_x = corner_x + col; + int px_y = corner_y + row; - fft.ss_ref.vals[idx_window] = img_ref[idx_img] * window_val; + int idx_img = px_y * px_hori + px_x; + int idx_window = row * window_size_x + col; + double coeff = 1.0; //fourier::hamming(row, col, ss_size_x, ss_size_y); + if (ss_ref.has_coords()) { + ss_ref.x[idx_window] = px_x; + ss_ref.y[idx_window] = px_y; } - } - // populate fft.ss_def with interpolator values - subset::get_subpx_from_img(fft.ss_def, ss_x_shft, ss_y_shft, interp_def); - - - // TODO: Make hanning window calc part of the initialization process. - for (int row = 0; row < window_size; ++row) { - for (int col = 0; col < window_size; ++col) { - // On-the-fly 2D Hann window - const double hann_row = 0.5 * (1.0 - cos(2.0 * M_PI * row / (window_size - 1))); - const double hann_col = 0.5 * (1.0 - cos(2.0 * M_PI * col / (window_size - 1))); - const double window_val = hann_row * hann_col; - fft.ss_def.vals[row*window_size+col]*=window_val; + if (px_x < 0 || px_x >= px_hori || px_y < 0 || px_y >= px_vert) { + ss_ref.vals[idx_window] = Real(0); } - } - - - // get peaks from the cross correlation - double max_val = 0.0; - fft.correlate_phase(); - fft.find_peak(peak_x, peak_y, max_val, subpx, "gaussian_2d"); - - - - // resetting values in fft_ref to 0.0. - for (int row = 0; row < ss_size; ++row) { - for (int col = 0; col < ss_size; ++col) { - - // Target coordinates in ss_ref - int target_y = offset_y + row; - int target_x = offset_x + col; - - //int idx_img = px_y * px_hori + px_x; - int idx_window = target_y * window_size + target_x; - - if (idx_window >= window_size*window_size){ - std::cout << "idx_window out of bounds: " << idx_window << " ss_x: " << ss_x << " ss_y: " << ss_y << " target_x: " << target_x << " target_y: " << target_y << std::endl; - exit(0); - } - - // IF DUBUGGING WITH THE BELOW COMMENT THIS - fft.ss_ref.vals[idx_window] = 0.0; + else { + ss_ref.vals[idx_window] = static_cast(coeff * img[idx_img]); } + } + } +} +template +void fill_fft_window_with_subset_at_corner(FFTPixels &ss_ref, + const Image &img_ref, + const int corner_x, + const int corner_y, + const int px_hori, + const int px_vert, + const int ss_size_x, + const int ss_size_y, + const int window_size_x, + const int window_size_y) { + switch (img_ref.type) { + case PixelType::UINT8: + fill_fft_window_with_subset_at_corner_impl( + ss_ref, img_ref.data8, + corner_x, corner_y, px_hori, px_vert, + ss_size_x, ss_size_y, + window_size_x, window_size_y); + break; + + case PixelType::UINT16: + fill_fft_window_with_subset_at_corner_impl( + ss_ref, img_ref.data16, + corner_x, corner_y, px_hori, px_vert, + ss_size_x, ss_size_y, + window_size_x, window_size_y); + break; + + case PixelType::UINT32: + fill_fft_window_with_subset_at_corner_impl( + ss_ref, img_ref.data32, + corner_x, corner_y, px_hori, px_vert, + ss_size_x, ss_size_y, + window_size_x, window_size_y); + break; + + case PixelType::UINT32F: + fill_fft_window_with_subset_at_corner_impl( + ss_ref, img_ref.data32f, + corner_x, corner_y, px_hori, px_vert, + ss_size_x, ss_size_y, + window_size_x, window_size_y); + break; + + default: + throw std::runtime_error("Unsupported pixel type"); } } +double hanning(const int row, const int col, const int size_x, const int size_y){ + const double hann_row = 0.5 * (1.0 - cos(2.0 * M_PI * row / (size_y - 1))); + const double hann_col = 0.5 * (1.0 - cos(2.0 * M_PI * col / (size_x - 1))); + return hann_row * hann_col; +} + + +double hamming(const int row, const int col, const int size_x, const int size_y){ + const double ham_row = 0.54 - 0.46 * cos(2.0 * M_PI * row / (size_y - 1)); + const double ham_col = 0.54 - 0.46 * cos(2.0 * M_PI * col / (size_x - 1)); + return ham_row * ham_col; +} +template void get_single_window_fftcc_peak(FFTf&, std::vector&, double&, double, double, int, int, int, int, const Image&, const Image&, const Interpolator&, bool); +template void get_single_window_fftcc_peak(FFT&, std::vector&, double&, double, double, int, int, int, int, const Image&, const Image&, const Interpolator&, bool); +template void get_single_window_fftcc_peak_centre(FFTf&, std::vector&, double&, double, double, double, double, int, int, int, int, const Interpolator&, const Interpolator&, bool); +template void get_single_window_fftcc_peak_centre(FFT&, std::vector&, double&, double, double, double, double, int, int, int, int, const Interpolator&, const Interpolator&, bool); +template void fill_fft_window_with_subset_at_centre(FFTPixels&, const Interpolator&, double, double, int, int, int, int, int, int); +template void fill_fft_window_with_subset_at_centre(FFTPixels&, const Interpolator&, double, double, int, int, int, int, int, int); +template void fill_fft_window_with_subset_at_corner(FFTPixels&, const Image&, int, int, int, int, int, int, int, int); +template void fill_fft_window_with_subset_at_corner(FFTPixels&, const Image&, int, int, int, int, int, int, int, int); diff --git a/src/pyvale/dic/cpp/dicfourier.hpp b/src/pyvale/dic/cpp/dicfourier.hpp index 2110e9544..729723bab 100644 --- a/src/pyvale/dic/cpp/dicfourier.hpp +++ b/src/pyvale/dic/cpp/dicfourier.hpp @@ -13,6 +13,7 @@ #include #include #include +#include // common header files #include "../../common_cpp/pocketfft_hdronly.h" @@ -23,388 +24,578 @@ #include "./dicsubset.hpp" #include "./dicutil.hpp" -namespace fourier { - struct Shift { - - // number of neighbours to use for removing outliers - size_t max_num_neigh; +template +struct FFTPixels { + std::vector vals; + std::vector x; + std::vector y; + int size_x; + int size_y; + int num_px; + Real sum; + + FFTPixels(int ss_size_x, int ss_size_y, bool store_coords = true) + : vals(ss_size_x * ss_size_y, Real(0)), + x(store_coords ? ss_size_x * ss_size_y : 0, 0.0), + y(store_coords ? ss_size_x * ss_size_y : 0, 0.0), + size_x(ss_size_x), + size_y(ss_size_y), + num_px(ss_size_x * ss_size_y), + sum(0) + {} + + bool has_coords() const { return !x.empty() && !y.empty(); } +}; + +template +struct FFTImpl { + int ss_size_x; + int ss_size_y; + int n_complex; + + FFTPixels ss_def; + FFTPixels ss_ref; + + std::vector> fft_def; + std::vector> fft_ref; + std::vector cross_corr; + + pocketfft::shape_t shape_in; + pocketfft::shape_t axes = {0,1}; + + pocketfft::stride_t stride_in; + pocketfft::stride_t stride_out; + + Eigen::MatrixXd A; + Eigen::VectorXd b; + + FFTImpl(int ss_size_x_, int ss_size_y_, bool store_coords = false) + : ss_size_x(ss_size_x_), + ss_size_y(ss_size_y_), + n_complex(ss_size_x_ / 2 + 1), + ss_def(ss_size_x_, ss_size_y_, store_coords), + ss_ref(ss_size_x_, ss_size_y_, store_coords), + fft_def(ss_size_y_ * n_complex), + fft_ref(ss_size_y_ * n_complex), + cross_corr(ss_size_x_ * ss_size_y_), + A(9, 6), + b(9) + { + shape_in = {static_cast(ss_size_y), static_cast(ss_size_x)}; + stride_in = {static_cast(ss_size_x * sizeof(Real)), sizeof(Real)}; + stride_out = {static_cast(n_complex * sizeof(std::complex)), sizeof(std::complex)}; + } - //integer shifts - std::vector x; - std::vector y; - std::vector cost; - std::vector max_val; + void correlate() { + pocketfft::r2c(shape_in, stride_in, stride_out, axes, pocketfft::FORWARD, ss_ref.vals.data(), fft_ref.data(), Real(1), 1); + pocketfft::r2c(shape_in, stride_in, stride_out, axes, pocketfft::FORWARD, ss_def.vals.data(), fft_def.data(), Real(1), 1); - // list of neighbours from prev window - std::vector neigh_list; - std::vector num_neigh_list; + for (int px = 0; px < ss_size_y * n_complex; px++) { + fft_def[px] = std::conj(fft_ref[px]) * fft_def[px]; + } - void gen_neighlist(const subset::Grid ss_grid, - const subset::Grid ss_grid_prev) { + pocketfft::c2r(shape_in, stride_out, stride_in, axes, pocketfft::BACKWARD, fft_def.data(), cross_corr.data(), Real(1), 1); + } - //Timer timer("nearest neighbour collection for :"); + void correlate_phase() { + pocketfft::r2c(shape_in, stride_in, stride_out, axes, pocketfft::FORWARD, ss_ref.vals.data(), fft_ref.data(), Real(1), 1); + pocketfft::r2c(shape_in, stride_in, stride_out, axes, pocketfft::FORWARD, ss_def.vals.data(), fft_def.data(), Real(1), 1); - const int prev_step = ss_grid_prev.step; + for (int px = 0; px < ss_size_y * n_complex; ++px) { + std::complex val = std::conj(fft_ref[px]) * fft_def[px]; + Real mag = std::abs(val); + fft_def[px] = (mag > Real(1e-12)) ? val / mag : std::complex(0); + } + pocketfft::c2r(shape_in, stride_out, stride_in, axes, pocketfft::BACKWARD, fft_def.data(), cross_corr.data(), Real(1), 1); + } - // a list containing the number of neighbours from the previous - // window size for each subset in the current window size - num_neigh_list.resize(ss_grid.num); + void fftshift(std::vector& data, int size_x, int size_y) { + std::vector temp(size_x * size_y); - // we know the neigh_list is going to be a max size of - // max_neigh*num_ss. we can resize this later once populated - neigh_list.resize(max_num_neigh*ss_grid.num); + int half_x = size_x / 2; + int half_y = size_y / 2; - // For each subset, find 4 nearest neighbours in ss_grid_prev - #pragma omp parallel for - for (int ss = 0; ss < ss_grid.num; ++ss) { + for (int y = 0; y < size_y; ++y) { + for (int x = 0; x < size_x; ++x) { + int new_x = (x + half_x) % size_x; + int new_y = (y + half_y) % size_y; + temp[new_y * size_x + new_x] = data[y * size_x + x]; + } + } + data = temp; + } - // corner of subset - const int ss_x = ss_grid.coords[2*ss]; - const int ss_y = ss_grid.coords[2*ss+1]; + inline double safe_log(double val, double eps = 1e-7) { + return std::log(std::max(val, eps)); + } - // Vector to store pairs of (distance, index) - std::vector> dist_index_list; + inline int wrap(int coord, int size) { + return (coord + size) % size; + } - // loop over a 10x10 section from the previous window - int idx_x = (ss_x / prev_step); - int idx_y = (ss_y / prev_step); + void get_peak(double &peak_x, double &peak_y, double &max_val, const bool subpx, const std::string &method) { + max_val = -std::numeric_limits::infinity(); + int x0 = 0, y0 = 0; + + for (int y = 0; y < ss_size_y; ++y) { + for (int x = 0; x < ss_size_x; ++x) { + double val = static_cast(cross_corr[y * ss_size_x + x]); + if (val > max_val) { + max_val = val; + x0 = x; + y0 = y; + } + } + } - // range of neighbour search - int min_x = std::max(0,idx_x-5); - int min_y = std::max(0,idx_y-5); - int max_x = std::min(ss_grid_prev.num_ss_x,idx_x+6); - int max_y = std::min(ss_grid_prev.num_ss_y,idx_y+6); + if (!subpx) { + peak_x = (x0 <= ss_size_x / 2.0) ? x0 : x0 - ss_size_x; + peak_y = (y0 <= ss_size_y / 2.0) ? y0 : y0 - ss_size_y; + return; + } - for (int y = min_y; y < max_y; y++){ - for (int x = min_x; x < max_x; x++){ + int i = 0; + for (int dy = -1; dy <= 1; ++dy) { + for (int dx = -1; dx <= 1; ++dx) { + int xw = wrap(x0 + dx, ss_size_x); + int yw = wrap(y0 + dy, ss_size_y); + double val = static_cast(cross_corr[yw * ss_size_x + xw]); + if (method == "GAUSSIAN_2D" && val <= 0) val = 1e-6; + double z = (method == "GAUSSIAN_2D") ? std::log(val) : val; + + A(i, 0) = dx * dx; + A(i, 1) = dy * dy; + A(i, 2) = dx * dy; + A(i, 3) = dx; + A(i, 4) = dy; + A(i, 5) = 1.0; + b(i) = z; + i++; + } + } - // check if point is a valid subset - int nss_idx = ss_grid_prev.mask[y*ss_grid_prev.num_ss_x+x]; - if (nss_idx == -1) continue; + Eigen::VectorXd coeffs = A.colPivHouseholderQr().solve(b); + double a = coeffs(0), b = coeffs(1), c = coeffs(2); + double d = coeffs(3), e = coeffs(4); - int nss_x = ss_grid_prev.coords[2*nss_idx]; - int nss_y = ss_grid_prev.coords[2*nss_idx+1]; + Eigen::Matrix2d H; + H << 2 * a, c, + c, 2 * b; + Eigen::Vector2d g(-d, -e); - double dx = (nss_x) - ss_x; - double dy = (nss_y) - ss_y; - double dist_sq = dx*dx + dy*dy; + Eigen::Vector2d offset = H.ldlt().solve(g); - dist_index_list.emplace_back(dist_sq, nss_idx); - } - } + peak_x = x0 + offset(0); + peak_y = y0 + offset(1); + peak_x = (peak_x <= ss_size_x / 2.0) ? peak_x : peak_x - ss_size_x; + peak_y = (peak_y <= ss_size_y / 2.0) ? peak_y : peak_y - ss_size_y; + } - // either use max_num_neigh or size of list if less than max_num_neigh - int num_neigh = std::min(max_num_neigh, dist_index_list.size()); - - // can't find any neighbours. - if (num_neigh == 0){ - std::cerr << "Could not find any neighbours from the previous FFT window size for point (" << ss_x << ", " << ss_y << ")." << std::endl; - std::cerr << "Number of neighbours: " << dist_index_list.size() << std::endl; - std::cerr << "Neighbours from previous window: " << std::endl; - for (size_t n = 0; n < dist_index_list.size(); n++){ - int nss_idx = dist_index_list[n].second; - int nss_x = ss_grid_prev.coords[2*nss_idx]; - int nss_y = ss_grid_prev.coords[2*nss_idx+1]; - std::cerr << "(" << nss_x << ", " << nss_y << "), "; - } - std::cerr << std::endl; - exit(EXIT_FAILURE); + void get_peak_nowrap(double &peak_x, double &peak_y, double &max_val, const bool subpx, const std::string &method) { + max_val = -std::numeric_limits::infinity(); + int x0 = 0, y0 = 0; + + for (int y = 0; y < ss_size_y; ++y) { + for (int x = 0; x < ss_size_x; ++x) { + double val = static_cast(cross_corr[y * ss_size_x + x]); + if (val > max_val) { + max_val = val; + x0 = x; + y0 = y; } + } + } - num_neigh_list[ss] = num_neigh; - std::nth_element(dist_index_list.begin(), dist_index_list.begin() + num_neigh, dist_index_list.end()); - dist_index_list.resize(num_neigh); - - // Store neighbours indices into neighlist - for (size_t i = 0; i < num_neigh; ++i) { - neigh_list[ss*max_num_neigh+i] = dist_index_list[i].second; - } + if (!subpx) { + peak_x = x0; + peak_y = y0; + return; + } + int i = 0; + for (int dy = -1; dy <= 1; ++dy) { + for (int dx = -1; dx <= 1; ++dx) { + int xw = wrap(x0 + dx, ss_size_x); + int yw = wrap(y0 + dy, ss_size_y); + double val = static_cast(cross_corr[yw * ss_size_x + xw]); + if (method == "GAUSSIAN_2D" && val <= 0) val = 1e-6; + double z = (method == "GAUSSIAN_2D") ? std::log(val) : val; + A(i, 0) = dx * dx; + A(i, 1) = dy * dy; + A(i, 2) = dx * dy; + A(i, 3) = dx; + A(i, 4) = dy; + A(i, 5) = 1.0; + b(i) = z; + i++; } } - }; - - extern std::vector shifts; - - struct FFT { - int ss_size; - int n_complex; - - // input data - subset::Pixels ss_def; - subset::Pixels ss_ref; - - // output data - std::vector> fft_def; - std::vector> fft_ref; - std::vector cross_corr; - - pocketfft::shape_t shape_in; - pocketfft::shape_t axes = {0,1}; - - pocketfft::stride_t stride_in; - pocketfft::stride_t stride_out; - - // for subpixel peak position in correlation map - Eigen::MatrixXd A; - Eigen::VectorXd b; - - FFT(int ss_size_) - : ss_size(ss_size_), n_complex(ss_size_/2+1), ss_def(ss_size_), ss_ref(ss_size_), - fft_def(ss_size_*n_complex), fft_ref(ss_size_*n_complex), - cross_corr(ss_size_ * ss_size_), A(9,6), b(9) - { - - shape_in = {static_cast(ss_size), static_cast(ss_size)}; - stride_in = {static_cast(ss_size * sizeof(double)), sizeof(double)}; - stride_out = {static_cast(n_complex * sizeof(std::complex)), sizeof(std::complex)}; - } + Eigen::VectorXd coeffs = A.colPivHouseholderQr().solve(b); + double a = coeffs(0), b = coeffs(1), c = coeffs(2); + double d = coeffs(3), e = coeffs(4); + Eigen::Matrix2d H; + H << 2 * a, c, + c, 2 * b; + Eigen::Vector2d g(-d, -e); + Eigen::Vector2d offset = H.ldlt().solve(g); + peak_x = x0 + offset(0); + peak_y = y0 + offset(1); + } + + void get_peak_offset(double &peak_x, double &peak_y, double &max_val, + const bool subpx, const std::string &method) { - void correlate() { + max_val = -std::numeric_limits::infinity(); + int x0 = 0, y0 = 0; - // forward fft of reference and deformed subsets - pocketfft::r2c(shape_in, stride_in, stride_out, axes, pocketfft::FORWARD, ss_ref.vals.data(), fft_ref.data(), 1.0, 1); - pocketfft::r2c(shape_in, stride_in, stride_out, axes, pocketfft::FORWARD, ss_def.vals.data(), fft_def.data(), 1.0, 1); - - // multiplication of complex fft data - for (int px = 0; px < ss_size * n_complex; px++) { - fft_def[px] = std::conj(fft_ref[px]) * fft_def[px]; + for (int y = 0; y < ss_size_y; ++y) { + for (int x = 0; x < ss_size_x; ++x) { + double val = static_cast(cross_corr[y * ss_size_x + x]); + if (val > max_val) { + max_val = val; + x0 = x; + y0 = y; + } } + } - // inverse FFT to get cross correlation - pocketfft::c2r(shape_in, stride_out, stride_in, axes, pocketfft::BACKWARD, fft_def.data(), cross_corr.data(), 1.0, 1); + const double center_x = static_cast(ss_size_x) / 2.0; + const double center_y = static_cast(ss_size_y) / 2.0; + + if (!subpx) { + peak_x = static_cast(x0) - center_x; + peak_y = static_cast(y0) - center_y; + + if (peak_x <= -center_x) peak_x += ss_size_x; + if (peak_x > center_x - 1e-12) peak_x -= ss_size_x; + if (peak_y <= -center_y) peak_y += ss_size_y; + if (peak_y > center_y - 1e-12) peak_y -= ss_size_y; + return; } - void correlate_phase() { - - // forward fft of reference and deformed subsets - pocketfft::r2c(shape_in, stride_in, stride_out, axes, pocketfft::FORWARD, ss_ref.vals.data(), fft_ref.data(), 1.0, 1); - pocketfft::r2c(shape_in, stride_in, stride_out, axes, pocketfft::FORWARD, ss_def.vals.data(), fft_def.data(), 1.0, 1); - - // multiplication of complex fft data - for (int px = 0; px < ss_size * n_complex; ++px) { - std::complex val = std::conj(fft_ref[px]) * fft_def[px]; - double mag = std::abs(val); - fft_def[px] = (mag > 1e-12) ? val / mag : 0.0; + int i = 0; + for (int dy = -1; dy <= 1; ++dy) { + for (int dx = -1; dx <= 1; ++dx) { + int xw = wrap(x0 + dx, ss_size_x); + int yw = wrap(y0 + dy, ss_size_y); + double val = static_cast(cross_corr[yw * ss_size_x + xw]); + if (method == "GAUSSIAN_2D" && val <= 0) val = 1e-6; + double z = (method == "GAUSSIAN_2D") ? std::log(val) : val; + + A(i, 0) = dx * dx; + A(i, 1) = dy * dy; + A(i, 2) = dx * dy; + A(i, 3) = dx; + A(i, 4) = dy; + A(i, 5) = 1.0; + b(i) = z; + i++; } - - // inverse FFT to get cross correlation - pocketfft::c2r(shape_in, stride_out, stride_in, axes, pocketfft::BACKWARD, fft_def.data(), cross_corr.data(), 1.0, 1); } - void fftshift(std::vector& data, int size) { - std::vector temp(size * size); + Eigen::VectorXd coeffs = A.colPivHouseholderQr().solve(b); + double a = coeffs(0), b = coeffs(1), c = coeffs(2); + double d = coeffs(3), e = coeffs(4); - int half = size / 2; + Eigen::Matrix2d H; + H << 2 * a, c, + c, 2 * b; + Eigen::Vector2d g(-d, -e); - for (int y = 0; y < size; ++y) { - for (int x = 0; x < size; ++x) { - int new_x = (x + half) % size; - int new_y = (y + half) % size; - temp[new_y * size + new_x] = data[y * size + x]; - } - } + Eigen::Vector2d offset = H.ldlt().solve(g); - data = temp; + double raw_x = static_cast(x0) + offset(0); + double raw_y = static_cast(y0) + offset(1); + + peak_x = raw_x - center_x; + peak_y = raw_y - center_y; + + if (peak_x <= -center_x) peak_x += ss_size_x; + if (peak_x > center_x - 1e-12) peak_x -= ss_size_x; + if (peak_y <= -center_y) peak_y += ss_size_y; + if (peak_y > center_y - 1e-12) peak_y -= ss_size_y; } - inline double safe_log(double val, double eps = 1e-7) { - return std::log(std::max(val, eps)); + + bool zero_norm_subsets(std::vector& ref_vals, + std::vector& def_vals, + const int ss_size_x, + const int ss_size_y) { + const int total_px = ss_size_x * ss_size_y; + + double mean_def = 0.0; + double mean_ref = 0.0; + for (int i = 0; i < total_px; ++i) { + mean_def += def_vals[i]; + mean_ref += ref_vals[i]; } - - inline int wrap(int coord, int size) { - return (coord + size) % size; + mean_def /= total_px; + mean_ref /= total_px; + + double std_def = 0.0; + double std_ref = 0.0; + for (int i = 0; i < total_px; ++i) { + std_def += std::pow(static_cast(def_vals[i]) - mean_def, 2); + std_ref += std::pow(static_cast(ref_vals[i]) - mean_ref, 2); } + std_def = std::sqrt(std_def / total_px); + std_ref = std::sqrt(std_ref / total_px); - - void find_peak(double &peak_x, double &peak_y, double &max_val, const bool subpx, const std::string &method) { - max_val = -std::numeric_limits::infinity(); - int x0 = 0, y0 = 0; - - // Step 1: Find the integer peak - for (int y = 0; y < ss_size; ++y) { - for (int x = 0; x < ss_size; ++x) { - double val = cross_corr[y * ss_size + x]; - if (val > max_val) { - max_val = val; - x0 = x; - y0 = y; - } - } - } - - // Step 2: No subpixel refinement requested - if (!subpx) { - peak_x = (x0 <= ss_size / 2.0) ? x0 : x0 - ss_size; - peak_y = (y0 <= ss_size / 2.0) ? y0 : y0 - ss_size; - return; - } + if (std_def < 1e-10 || std_ref < 1e-10) return false; - int i = 0; - for (int dy = -1; dy <= 1; ++dy) { - for (int dx = -1; dx <= 1; ++dx) { - int xw = wrap(x0 + dx, ss_size); - int yw = wrap(y0 + dy, ss_size); - double val = cross_corr[yw * ss_size + xw]; - - // Safe log for Gaussian - if (method == "GAUSSIAN_2D" && val <= 0) val = 1e-6; - double z = (method == "GAUSSIAN_2D") ? std::log(val) : val; - - A(i, 0) = dx * dx; - A(i, 1) = dy * dy; - A(i, 2) = dx * dy; - A(i, 3) = dx; - A(i, 4) = dy; - A(i, 5) = 1.0; - b(i) = z; - i++; - } - } + for (int i = 0; i < total_px; ++i) { + def_vals[i] = static_cast((def_vals[i] - mean_def) / std_def); + ref_vals[i] = static_cast((ref_vals[i] - mean_ref) / std_ref); + } - // Step 4: Solve least squares - Eigen::VectorXd coeffs = A.colPivHouseholderQr().solve(b); - double a = coeffs(0), b_ = coeffs(1), c = coeffs(2); - double d = coeffs(3), e = coeffs(4); + return true; + } - // Step 5: Find stationary point (gradient = 0) - Eigen::Matrix2d H; - H << 2 * a, c, - c, 2 * b_; - Eigen::Vector2d g(-d, -e); + bool zero_norm_subset(FFTPixels &ss, + const int ss_size_x, + const int ss_size_y) { - Eigen::Vector2d offset = H.ldlt().solve(g); + const int total_px = ss_size_x * ss_size_y; - peak_x = x0 + offset(0); - peak_y = y0 + offset(1); - peak_x = (peak_x <= ss_size / 2.0) ? peak_x : peak_x - ss_size; - peak_y = (peak_y <= ss_size / 2.0) ? peak_y : peak_y - ss_size; - //std::cout << peak_x << " " << peak_y << std::endl; + double mean_ref = 0.0; + int idx; + for (int y = 0; y < ss_size_y; ++y) { + for (int x = 0; x < ss_size_x; ++x) { + idx = y * ss.size_x + x; + mean_ref += ss.vals[idx]; + } } + mean_ref /= total_px; - void find_peak_offset(double &peak_x, double &peak_y, double &max_val, - const bool subpx, const std::string &method) { - - max_val = -std::numeric_limits::infinity(); - int x0 = 0, y0 = 0; - - // Step 1: integer peak - for (int y = 0; y < ss_size; ++y) { - for (int x = 0; x < ss_size; ++x) { - double val = cross_corr[y * ss_size + x]; - if (val > max_val) { - max_val = val; - x0 = x; - y0 = y; - } - } + double std_ref = 0.0; + for (int y = 0; y < ss_size_y; ++y) { + for (int x = 0; x < ss_size_x; ++x) { + idx = y * ss.size_x + x; + std_ref += std::pow(static_cast(ss.vals[idx]) - mean_ref, 2); } + } + std_ref = std::sqrt(std_ref / total_px); - const double center = static_cast(ss_size) / 2.0; - - // No subpixel refinement requested: map index -> displacement by subtracting center - if (!subpx) { - peak_x = static_cast(x0) - center; - peak_y = static_cast(y0) - center; - // Optional wrap correction (shouldn't be necessary if using center subtraction): - if (peak_x <= -center) peak_x += ss_size; // maps -128 -> 128 if needed - if (peak_x > center - 1e-12) peak_x -= ss_size; - if (peak_y <= -center) peak_y += ss_size; - if (peak_y > center - 1e-12) peak_y -= ss_size; - return; - } + if (std_ref < 1e-10) return false; - // Subpixel refinement: build 3x3 neighborhood and solve quadratic (your existing code) - int i = 0; - for (int dy = -1; dy <= 1; ++dy) { - for (int dx = -1; dx <= 1; ++dx) { - int xw = wrap(x0 + dx, ss_size); - int yw = wrap(y0 + dy, ss_size); - double val = cross_corr[yw * ss_size + xw]; - - if (method == "GAUSSIAN_2D" && val <= 0) val = 1e-6; - double z = (method == "GAUSSIAN_2D") ? std::log(val) : val; - - A(i, 0) = dx * dx; - A(i, 1) = dy * dy; - A(i, 2) = dx * dy; - A(i, 3) = dx; - A(i, 4) = dy; - A(i, 5) = 1.0; - b(i) = z; - i++; - } + for (int y = 0; y < ss_size_y; ++y) { + for (int x = 0; x < ss_size_x; ++x) { + idx = y * ss.size_x + x; + ss.vals[idx] = static_cast((ss.vals[idx] - mean_ref) / std_ref); } + } - Eigen::VectorXd coeffs = A.colPivHouseholderQr().solve(b); - double a = coeffs(0), b_ = coeffs(1), c = coeffs(2); - double d = coeffs(3), e = coeffs(4); + return true; + } - Eigen::Matrix2d H; - H << 2 * a, c, - c, 2 * b_; - Eigen::Vector2d g(-d, -e); + bool zero_norm_subsets_centered(FFTPixels &ss, + const int ss_size_x, + const int ss_size_y, + const int window_size_x, + const int window_size_y) { + const int window_half_x = window_size_x / 2; + const int window_half_y = window_size_y / 2; - Eigen::Vector2d offset = H.ldlt().solve(g); + const int ss_half_x = ss_size_x / 2; + const int ss_half_y = ss_size_y / 2; - // Map integer + fractional index -> displacement relative to center - double raw_x = static_cast(x0) + offset(0); - double raw_y = static_cast(y0) + offset(1); + double mean_ref = 0.0; + int count = 0; - peak_x = raw_x - center; - peak_y = raw_y - center; + for (int row = -ss_half_y; row < ss_size_y - ss_half_y; ++row) { + for (int col = -ss_half_x; col < ss_size_x - ss_half_x; ++col) { + int x = window_half_x + col; + int y = window_half_y + row; + int idx = y * window_size_x + x; - // Optional: ensure in [-center, center) - if (peak_x <= -center) peak_x += ss_size; - if (peak_x > center - 1e-12) peak_x -= ss_size; - if (peak_y <= -center) peak_y += ss_size; - if (peak_y > center - 1e-12) peak_y -= ss_size; + mean_ref += ss.vals[idx]; + ++count; + } } - }; - void init(std::vector &ss_grid, - std::vector &ss_sizes, - std::vector &ss_steps, - const bool *img_roi, const util::Config &conf); + mean_ref /= count; - void multiwindow(const std::vector &ss_grid, - const double *img_ref, - const double *img_def, - const Interpolator &interp_def, - const bool fft_mad, - const double fft_mad_scale); + double std_ref = 0.0; + for (int row = -ss_half_y; row < ss_size_y - ss_half_y; ++row) { + for (int col = -ss_half_x; col < ss_size_x - ss_half_x; ++col) { + int x = window_half_x + col; + int y = window_half_y + row; + int idx = y * window_size_x + x; + std_ref += std::pow(static_cast(ss.vals[idx]) - mean_ref, 2); + } + } + std_ref = std::sqrt(std_ref / count); - void single_grid(const subset::Grid &ss_grid, - const double *prev_img_u, - const double *prev_img_v, - const int window_size, - const double *img_ref, - const double *img_def, - const Interpolator &interp_def); + if (std_ref < 1e-10) return false; + for (int row = -ss_half_y; row < ss_size_y - ss_half_y; ++row) { + for (int col = -ss_half_x; col < ss_size_x - ss_half_x; ++col) { + int x = window_half_x + col; + int y = window_half_y + row; + int idx = y * window_size_x + x; - void get_single_window_fftcc_peak(double &peak_x, double &peak_y, - const int ss_x, const int ss_y, - const int ss_size, const int window_size, - const double *img_ref, const double *img_def, - const Interpolator &interp_def); + ss.vals[idx] = static_cast((ss.vals[idx] - mean_ref) / std_ref); + } + } + return true; + } +}; + +using FFT = FFTImpl; +using FFTf = FFTImpl; + template + void get_single_window_fftcc_peak(FFTImpl &fft, + std::vector &p, + double &max_val, + const double cx, const double cy, + const int ss_size_x, + const int ss_size_y, + const int window_size_x, + const int window_size_y, + const Image &img_ref, const Image &img_def, + const Interpolator &interp_def, + const bool debug=false); + + + + template + void get_single_window_fftcc_peak_centre(FFTImpl &fft, + std::vector &p, + double &max_val, + const double cx, const double cy, + const double offset_x, const double offset_y, + const int ss_size_x, + const int ss_size_y, + const int window_size_x, + const int window_size_y, + const Interpolator &interp_ref, + const Interpolator &interp_def, + const bool debug=false); + + template + void fill_fft_window_with_subset_at_centre(FFTPixels &ss_ref, + const Interpolator &interp_ref, + const double ss_x, + const double ss_y, + const int px_hori, + const int px_vert, + const int ss_size_x, + const int ss_size_y, + const int window_size_x, + const int window_size_y); + + + template + void fill_fft_window_with_subset_at_corner(FFTPixels &ss_ref, + const Image &img_ref, + const int ss_x, + const int ss_y, + const int px_hori, + const int px_vert, + const int ss_size_x, + const int ss_size_y, + const int window_size_x, + const int window_size_y); + + /** + * Clamp a subset top-left coordinate so that the window fits inside the image. + * + * @param ss_coord Subset top-left coordinate (x or y) + * @param window_half Half-size of the FFT window in this direction + * @param ss_half Half-size of the subset in this direction + * @param img_size Image size (width or height) + * @param window_size FFT window size in this direction + * @return Clamped coordinate + */ + inline int clamp_subset(int ss_coord, int window_half, int ss_half, int img_size, int window_size) { + return std::clamp(ss_coord - window_half + ss_half, 0, img_size - window_size); + } + + /** + * Compute offsets to center the subset within the FFT window. + * + * @param window_half Half-size of the FFT window in this direction + * @param ss_half Half-size of the subset in this direction + * @param ss_coord Subset top-left coordinate (x or y) + * @return Offset for placing subset in window + */ + inline int compute_subset_offset(int window_half, int ss_half, int ss_coord) { + return std::min(window_half - ss_half, ss_coord); + } + + /** + * Reset a rectangular region inside the FFT reference window to zero. + * + * @param vals window intensity values + * @param offset_x x offset of subset in FFT window + * @param offset_y y offset of subset in FFT window + * @param ss_size_x width of subset + * @param ss_size_y height of subset + * @param window_size_x total FFT window width + * @param window_size_y total FFT window height + */ + template + inline void reset_fft_ref_subset(std::vector &vals, + int offset_x, int offset_y, + int ss_size_x, int ss_size_y, + int window_size_x, int window_size_y){ + + for (int row = 0; row < ss_size_y; ++row) { + for (int col = 0; col < ss_size_x; ++col) { + int target_y = offset_y + row; + int target_x = offset_x + col; + int idx_window = target_y * window_size_x + target_x; + + if (idx_window >= window_size_x * window_size_y) { + std::cerr << "reset_fft_ref_subset: idx_window out of bounds: " + << idx_window << " target_x: " << target_x + << " target_y: " << target_y << std::endl; + exit(1); + } - std::pair get_prev_shift(const int i, const int ss, - const double ss_x, const double ss_y, - const std::vector& shifts, - const std::vector& ss_grid); + vals[idx_window] = 0.0; + } + } + } double debugcost(subset::Pixels &ss_ref, subset::Pixels &ss_def); + void smooth_field(std::vector& shift, const subset::Grid& ss_grid, double sigma, int radius); + + + /** + * @brief Computes the Hanning window value at a given location. + * + * w(n) = 0.5 * (1 - cos(2πn / (N - 1))) + * + * @param row Row index (0 <= row < size_y) + * @param col Column index (0 <= col < size_x) + * @param size_x Total number of columns (must be > 1) + * @param size_y Total number of rows (must be > 1) + * + * @return The 2D Hann window coefficient at (row, col). + */ + double hanning(const int row, const int col, const int size_x, const int size_y); + + + /** + * @brief Computes the 2D Hamming window value at a given position. + * + * w(n) = 0.54 - 0.46 * cos(2πn / (N - 1)) + * + * @param row Row index (0 <= row < size_y) + * @param col Column index (0 <= col < size_x) + * @param size_x Total number of columns (must be > 1) + * @param size_y Total number of rows (must be > 1) + * + * @return The 2D Hamming window coefficient at (row, col). + */ + double hamming(const int row, const int col, const int size_x, const int size_y); - void zero_norm_subsets(std::vector& def_vals, std::vector& ref_vals, int ss_size); - void smooth_field(std::vector& shift, const subset::Grid& ss_grid, double sigma, int radius); - void test(double &peak_x, double &peak_y, int ss_x, int ss_y, const int window_size, const double *img_ref, const double *img_def, const Interpolator &interp_def); -} #endif // DICFOURIER_H diff --git a/src/pyvale/dic/cpp/dicinterpBspline.cpp b/src/pyvale/dic/cpp/dicinterpBspline.cpp index 472fab6f4..cacdcc08f 100644 --- a/src/pyvale/dic/cpp/dicinterpBspline.cpp +++ b/src/pyvale/dic/cpp/dicinterpBspline.cpp @@ -12,17 +12,37 @@ #include "./dicinterpBspline.hpp" -Bspline::Bspline(double* img, int px_hori, int px_vert){ +#include "../../common_cpp/util.hpp" - // intitialise vars used globally within Interpolator. - this->image = img; - this->px_vert = px_vert; - this->px_hori = px_hori; - coeff.resize(px_vert*px_hori); - for (int i = 0; i < px_hori*px_vert; i++){ - coeff[i] = img[i]; - } + +Bspline::Bspline(const Image &img) { + + common_util::Timer time("to init " + img.filename + " interp:", 2); + + this->px_hori = img.width; + this->px_vert = img.height; + + padded_hori = px_hori + 4; + padded_vert = px_vert + 4; + coeff_padded.resize(padded_hori * padded_vert, 0.0); + + // Lambda to get clamped pixel value regardless of type + auto getpix = [&](int x, int y) -> double { + x = std::clamp(x, 0, px_hori - 1); + y = std::clamp(y, 0, px_vert - 1); + if (img.type == PixelType::UINT8) return img.data8 [y * px_hori + x]; + else if (img.type == PixelType::UINT16) return img.data16[y * px_hori + x]; + else if (img.type == PixelType::UINT32) return img.data32[y * px_hori + x]; + else if (img.type == PixelType::UINT32F) return img.data32f[y * px_hori + x]; + else throw std::runtime_error("Unsupported pixel type"); + }; + + // Fill entire padded array using clamped reads — handles interior, edges, and corners + #pragma omp parallel for collapse(2) schedule(static) + for (int y = 0; y < padded_vert; y++) + for (int x = 0; x < padded_hori; x++) + coeff_padded[y * padded_hori + x] = getpix(x - 2, y - 2); prefilter_x(); prefilter_y(); @@ -54,22 +74,25 @@ void Bspline::prefilter_x() { const double lambda = (1.0 - z)*(1.0 - 1.0/z); // Normalize - for (int y = 0; y < px_vert; y++) - for (int x = 0; x < px_hori; x++) - coeff[y*px_hori + x] *= lambda; + #pragma omp parallel for collapse(2) schedule(static) + for (int y = 0; y < padded_vert; y++) + for (int x = 0; x < padded_hori; x++) + coeff_padded[y*padded_hori + x] *= lambda; // Causal - for (int y = 0; y < px_vert; y++) { - double* row = &coeff[y*px_hori]; - for (int x = 1; x < px_hori; x++) + #pragma omp parallel for schedule(static) + for (int y = 0; y < padded_vert; y++) { + double* row = &coeff_padded[y*padded_hori]; + for (int x = 1; x < padded_hori; x++) row[x] += z * row[x-1]; } // Anticausal - for (int y = 0; y < px_vert; y++) { - double* row = &coeff[y*px_hori]; - row[px_hori-1] = z/(z*z - 1.0) * row[px_hori-1]; - for (int x = px_hori-2; x >= 0; x--) + #pragma omp parallel for schedule(static) + for (int y = 0; y < padded_vert; y++) { + double* row = &coeff_padded[y*padded_hori]; + row[padded_hori-1] = z/(z*z - 1.0) * row[padded_hori-1]; + for (int x = padded_hori-2; x >= 0; x--) row[x] = z*(row[x+1] - row[x]); } } @@ -82,47 +105,58 @@ void Bspline::prefilter_y() { const double lambda = (1.0 - z)*(1.0 - 1.0/z); // Normalize - for (int y = 0; y < px_vert; y++) - for (int x = 0; x < px_hori; x++) - coeff[y*px_hori + x] *= lambda; + #pragma omp parallel for collapse(2) schedule(static) + for (int y = 0; y < padded_vert; y++) + for (int x = 0; x < padded_hori; x++) + coeff_padded[y*padded_hori + x] *= lambda; // Causal - for (int x = 0; x < px_hori; x++) { - for (int y = 1; y < px_vert; y++) - coeff[y*px_hori + x] += z * coeff[(y-1)*px_hori + x]; + #pragma omp parallel for schedule(static) + for (int x = 0; x < padded_hori; x++) { + for (int y = 1; y < padded_vert; y++) + coeff_padded[y*padded_hori + x] += z * coeff_padded[(y-1)*padded_hori + x]; } // Anticausal - for (int x = 0; x < px_hori; x++) { - coeff[(px_vert-1)*px_hori + x] = z/(z*z - 1.0) * coeff[(px_vert-1)*px_hori + x]; - for (int y = px_vert-2; y >= 0; y--) - coeff[y*px_hori + x] = z*(coeff[(y+1)*px_hori + x] - coeff[y*px_hori + x]); + #pragma omp parallel for schedule(static) + for (int x = 0; x < padded_hori; x++) { + coeff_padded[(padded_vert-1)*padded_hori + x] = z/(z*z - 1.0) * coeff_padded[(padded_vert-1)*padded_hori + x]; + for (int y = padded_vert-2; y >= 0; y--) + coeff_padded[y*padded_hori + x] = z*(coeff_padded[(y+1)*padded_hori + x] - coeff_padded[y*padded_hori + x]); } } -double Bspline::eval(const int ss_x, const int ss_y, const double subpx_x, double subpx_y) const { - int ix = (int)floor(subpx_x); - int iy = (int)floor(subpx_y); +double Bspline::eval(const int ss_x, const int ss_y, const double subpx_x, const double subpx_y) const { - double tx = subpx_x - ix; - double ty = subpx_y - iy; + double x = std::clamp(subpx_x, 0.0, (double)(px_hori - 1)); + double y = std::clamp(subpx_y, 0.0, (double)(px_vert - 1)); + const int ix = (int)x + 2; + const int iy = (int)y + 2; + const double tx = x - (ix-2); + const double ty = y - (iy-2); double Bx[4], By[4]; basis(tx, Bx); basis(ty, By); - double f = 0.0; - for (int j = 0; j < 4; j++) { - int yy = std::clamp(iy + j - 1, 0, px_vert-1); + // Row pointers + const double* r0 = coeff_padded.data() + (iy-1)*padded_hori; + const double* r1 = coeff_padded.data() + (iy )*padded_hori; + const double* r2 = coeff_padded.data() + (iy+1)*padded_hori; + const double* r3 = coeff_padded.data() + (iy+2)*padded_hori; - for (int i = 0; i < 4; i++) { - int xx = std::clamp(ix + i - 1, 0, px_hori-1); - double c = coeff[yy*px_hori + xx]; - f += c * Bx[i] * By[j]; - } - } - return f; + // Column indices + int xx0 = ix-1, xx1 = ix, xx2 = ix+1, xx3 = ix+2; + + // Compute row sums + double sum0 = r0[xx0]*Bx[0] + r0[xx1]*Bx[1] + r0[xx2]*Bx[2] + r0[xx3]*Bx[3]; + double sum1 = r1[xx0]*Bx[0] + r1[xx1]*Bx[1] + r1[xx2]*Bx[2] + r1[xx3]*Bx[3]; + double sum2 = r2[xx0]*Bx[0] + r2[xx1]*Bx[1] + r2[xx2]*Bx[2] + r2[xx3]*Bx[3]; + double sum3 = r3[xx0]*Bx[0] + r3[xx1]*Bx[1] + r3[xx2]*Bx[2] + r3[xx3]*Bx[3]; + + // Final value + return sum0*By[0] + sum1*By[1] + sum2*By[2] + sum3*By[3]; } double Bspline::eval_dx(const int ss_x, const int ss_y, const double subpx_x, double subpx_y) const { @@ -172,12 +206,14 @@ double Bspline::eval_dy(const int ss_x, const int ss_y, const double subpx_x, do return dfdy; } -InterpVals Bspline::eval_and_derivs(const int ss_x, const int ss_y, const double subpx_x, double subpx_y) const { - int ix = (int)floor(subpx_x); - int iy = (int)floor(subpx_y); +InterpVals Bspline::eval_and_derivs(const int ss_x, const int ss_y, const double subpx_x, const double subpx_y) const { - double tx = subpx_x - ix; - double ty = subpx_y - iy; + double x = std::clamp(subpx_x, 0.0, (double)(px_hori - 1)); + double y = std::clamp(subpx_y, 0.0, (double)(px_vert - 1)); + const int ix = (int)x + 2; + const int iy = (int)y + 2; + const double tx = x - (ix-2); + const double ty = y - (iy-2); double Bx[4], By[4], dBx[4], dBy[4]; basis(tx, Bx); @@ -185,17 +221,32 @@ InterpVals Bspline::eval_and_derivs(const int ss_x, const int ss_y, const double basis_d(tx, dBx); basis_d(ty, dBy); - InterpVals out {0,0,0}; - - for (int j = 0; j < 4; j++) { - int yy = std::clamp(iy + j - 1, 0, px_vert-1); - for (int i = 0; i < 4; i++) { - int xx = std::clamp(ix + i - 1, 0, px_hori-1); - double c = coeff[yy*px_hori + xx]; - out.f += c * Bx[i] * By[j]; - out.dfdx += c * dBx[i] * By[j]; - out.dfdy += c * Bx[i] * dBy[j]; - } - } - return out; + // Precompute row pointers + const double* r0 = coeff_padded.data() + (iy-1)*padded_hori; + const double* r1 = coeff_padded.data() + (iy )*padded_hori; + const double* r2 = coeff_padded.data() + (iy+1)*padded_hori; + const double* r3 = coeff_padded.data() + (iy+2)*padded_hori; + + // Column indices + int xx0 = ix-1; + int xx1 = ix; + int xx2 = ix+1; + int xx3 = ix+2; + + // Row sums + double sum_f0 = r0[xx0]*Bx[0] + r0[xx1]*Bx[1] + r0[xx2]*Bx[2] + r0[xx3]*Bx[3]; + double sum_f1 = r1[xx0]*Bx[0] + r1[xx1]*Bx[1] + r1[xx2]*Bx[2] + r1[xx3]*Bx[3]; + double sum_f2 = r2[xx0]*Bx[0] + r2[xx1]*Bx[1] + r2[xx2]*Bx[2] + r2[xx3]*Bx[3]; + double sum_f3 = r3[xx0]*Bx[0] + r3[xx1]*Bx[1] + r3[xx2]*Bx[2] + r3[xx3]*Bx[3]; + + // Compute value and derivatives + double f = sum_f0*By[0] + sum_f1*By[1] + sum_f2*By[2] + sum_f3*By[3]; + double dfdx = (r0[xx0]*dBx[0] + r0[xx1]*dBx[1] + r0[xx2]*dBx[2] + r0[xx3]*dBx[3])*By[0] + + (r1[xx0]*dBx[0] + r1[xx1]*dBx[1] + r1[xx2]*dBx[2] + r1[xx3]*dBx[3])*By[1] + + (r2[xx0]*dBx[0] + r2[xx1]*dBx[1] + r2[xx2]*dBx[2] + r2[xx3]*dBx[3])*By[2] + + (r3[xx0]*dBx[0] + r3[xx1]*dBx[1] + r3[xx2]*dBx[2] + r3[xx3]*dBx[3])*By[3]; + + double dfdy = sum_f0*dBy[0] + sum_f1*dBy[1] + sum_f2*dBy[2] + sum_f3*dBy[3]; + + return {f, dfdx, dfdy}; } diff --git a/src/pyvale/dic/cpp/dicinterpBspline.hpp b/src/pyvale/dic/cpp/dicinterpBspline.hpp index 70a5fbebe..445381f0d 100644 --- a/src/pyvale/dic/cpp/dicinterpBspline.hpp +++ b/src/pyvale/dic/cpp/dicinterpBspline.hpp @@ -18,13 +18,15 @@ // Program Header files #include "./dicinterp.hpp" +// common_cpp header files +#include "../../common_cpp/util.hpp" + class Bspline : public Interpolator { private: std::vector coeff; - double *image; // Recursive spline prefilter void prefilter_x(); @@ -34,6 +36,10 @@ class Bspline : public Interpolator { static inline void basis(double t, double B[4]); static inline void basis_d(double t, double Bd[4]); + std::vector coeff_padded; + int padded_hori; // width of padded array (px_hori + 4) + int padded_vert; // height of padded array (px_vert + 4) + public: /** @@ -45,7 +51,7 @@ class Bspline : public Interpolator { * @param px_hori Width of the image in pixels * @param px_vert Height of the image in pixels */ - Bspline(double * img, int px_hori, int px_vert); + Bspline(const Image &img); /** * @brief Evaluates the bicubic interpolation at a specified point. diff --git a/src/pyvale/dic/cpp/dicinterpHermite.cpp b/src/pyvale/dic/cpp/dicinterpHermite.cpp index 2a1b56e2b..d2edd2b1a 100644 --- a/src/pyvale/dic/cpp/dicinterpHermite.cpp +++ b/src/pyvale/dic/cpp/dicinterpHermite.cpp @@ -21,14 +21,27 @@ -Hermite::Hermite(double *img, int px_hori, int px_vert){ +Hermite::Hermite(const Image &img) { - //Timer timer("interpolator initialisation"); + common_util::Timer time("to init " + img.filename + " interp:", 2); // intitialise vars used globally within Interpolator. - this->image = img; - this->px_vert = px_vert; - this->px_hori = px_hori; + image.resize(px_hori*px_vert); + if (img.type == PixelType::UINT8) { + for (size_t i = 0; i < img.data8.size(); i++) { + image[i] = static_cast(img.data8[i]); + } + } + else if (img.type == PixelType::UINT16) { + for (size_t i = 0; i < img.data16.size(); i++) { + image[i] = static_cast(img.data16[i]); + } + } + else if (img.type == PixelType::UINT32) { + for (size_t i = 0; i < img.data32.size(); i++) { + image[i] = static_cast(img.data32[i]); + } + } // allocate memory for pixel coordinate arrays px_y.resize(px_vert); @@ -57,8 +70,7 @@ Hermite::Hermite(double *img, int px_hori, int px_vert){ ProgressBar pbar("Interpolator Initialisation:", niters); #ifdef _MSC_VER - // Windows/MSVC - no explicit sharing of member variables - #pragma omp parallel for shared(px_hori, px_vert, stop_request) + #pragma omp parallel for shared(stop_request) #else // Linux/GCC - explicit sharing works #pragma omp parallel for shared(px_x, image, dx, px_hori, px_vert, stop_request) @@ -92,7 +104,7 @@ Hermite::Hermite(double *img, int px_hori, int px_vert){ #ifdef _MSC_VER // Windows/MSVC - no explicit sharing of member variables - #pragma omp parallel for shared(px_hori, px_vert, stop_request) + #pragma omp parallel for shared(stop_request) #else // Linux/GCC - explicit sharing works #pragma omp parallel for shared(px_x, image, dx, px_hori, px_vert, stop_request) @@ -128,7 +140,7 @@ Hermite::Hermite(double *img, int px_hori, int px_vert){ // #ifdef _MSC_VER // Windows/MSVC - no explicit sharing of member variables - #pragma omp parallel for shared(px_hori, px_vert, stop_request) + #pragma omp parallel for shared(stop_request) #else // Linux/GCC - explicit sharing works #pragma omp parallel for shared(px_x, image, dx, px_hori, px_vert, stop_request) @@ -161,7 +173,6 @@ Hermite::Hermite(double *img, int px_hori, int px_vert){ if (g_debug_level>1){ - pbar.update(current_progress); pbar.finish(); } } diff --git a/src/pyvale/dic/cpp/dicinterpHermite.hpp b/src/pyvale/dic/cpp/dicinterpHermite.hpp index fe208d02f..4169e1b72 100644 --- a/src/pyvale/dic/cpp/dicinterpHermite.hpp +++ b/src/pyvale/dic/cpp/dicinterpHermite.hpp @@ -18,6 +18,9 @@ // Program Header files #include "./dicinterp.hpp" +// common_cpp header files +#include "../../common_cpp/util.hpp" + class Hermite : public Interpolator { private: @@ -27,7 +30,7 @@ class Hermite : public Interpolator { std::vector dxy; std::vector px_y; std::vector px_x; - double *image; + std::vector image; /** @@ -95,7 +98,7 @@ class Hermite : public Interpolator { * @param px_hori Width of the image in pixels * @param px_vert Height of the image in pixels */ - Hermite(double * img, int px_hori, int px_vert); + Hermite(const Image &img); /** * @brief Evaluates the bicubic interpolation at a specified point. diff --git a/src/pyvale/dic/cpp/dicinterpfactory.hpp b/src/pyvale/dic/cpp/dicinterpfactory.hpp new file mode 100644 index 000000000..6b6ea9133 --- /dev/null +++ b/src/pyvale/dic/cpp/dicinterpfactory.hpp @@ -0,0 +1,45 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + + +// InterpolatorFactory.h +#pragma once + + +#include +#include + +// DIC header files +#include "dicinterpBspline.hpp" +#include "dicinterpHermite.hpp" +#include "dicutil.hpp" + +// common_cpp header files +#include "../../common_cpp/util.hpp" +#include "../../common_cpp/img_read.hpp" + +/** + * @brief Factory function for creating interpolator instances. + * + * @param routine Interpolation method to use. + * @param img reference to the image data in row-major order. + * + * @return A `unique_ptr` to the constructed @ref Interpolator. + * + * @throws std::invalid_argument if @p routine is not a recognised method. + */ +inline std::unique_ptr make_interp(util::InterpRoutine routine, const std::string &image_str) { + + Image img = read_img(image_str); + + switch (routine) { + case util::InterpRoutine::BSPLINE: + return std::make_unique(img); + case util::InterpRoutine::HERMITE: + return std::make_unique(img); + } + throw std::invalid_argument("Unknown interpolation routine"); +} diff --git a/src/pyvale/dic/cpp/dicmain.cpp b/src/pyvale/dic/cpp/dicmain.cpp index a730a2e64..08b9c4d16 100644 --- a/src/pyvale/dic/cpp/dicmain.cpp +++ b/src/pyvale/dic/cpp/dicmain.cpp @@ -6,13 +6,15 @@ // STD library Header files -#include #include #include #include -#include +#include #include #include +#include +#include +#include // pybind header files #include @@ -26,16 +28,19 @@ #include "../../common_cpp/util.hpp" // DIC Header files -#include "./dicinterp.hpp" -#include "./dicinterpBspline.hpp" -#include "./dicinterpHermite.hpp" -#include "./dicoptimizer.hpp" -#include "./dicscanmethod.hpp" +#include "./dicinterpfactory.hpp" #include "./dicutil.hpp" -#include "./dicfourier.hpp" -#include "./dicshapefunc.hpp" #include "./dicresults.hpp" -#include "dicsubset.hpp" +#include "./dicsubset.hpp" +#include "./dicroiupdate.hpp" +#include "./dicmain.hpp" +#include "./dicmultiwindow_rg.hpp" +#include "./dicmultiwindow_only.hpp" +#include "./dicsinglewindow_rg.hpp" +#include "./dicrasterscan.hpp" + +// stereo header files +#include "./stereoutil.hpp" // cuda Header files //#include "../cuda/malloc.hpp" @@ -43,168 +48,435 @@ namespace py = pybind11; -void DICengine(const py::array_t& img_stack_arr, - const py::array_t& img_roi_arr, - util::Config &conf, - common_util::SaveConfig &saveconf){ +void engine(const py::array_t& img_roi_arr, + const Calib &calib, + const util::Config &conf, + const MultiwindowConfig &mwconf, + const common_util::SaveConfig &saveconf){ // Register signal handler for Ctrl+C and set debug_level signal(SIGINT, signalHandler); g_debug_level = conf.debug_level; - // ------------------------------------------------------------------------ - // Initialisation - // ------------------------------------------------------------------------ + // ----------------------------------------------------------------------- + // loop over deformed images and perform DIC + // ----------------------------------------------------------------------- if (g_debug_level>0){ - TITLE("Config"); - INFO_OUT("Width of Images: ", conf.px_hori << " [px]"); - INFO_OUT("Height of Images: ", conf.px_vert << " [px]"); - INFO_OUT("Number of Deformed Images: ", conf.num_def_img); - INFO_OUT("Max number of solver iterations: ", conf.max_iter); - INFO_OUT("Correlation Criterion: ", conf.corr_crit); - INFO_OUT("Shape Function: ", conf.shape_func); - INFO_OUT("Interpolation Routine: ", conf.interp_routine); - INFO_OUT("FFT MAD outlier removal enabled: ", conf.fft_mad); - INFO_OUT("FFT MAD scale: ", conf.fft_mad_scale); - INFO_OUT("Image Scan Method: ", conf.scan_method); - INFO_OUT("Optimization Precision:", conf.precision); - INFO_OUT("Correlation Cutoff Threshold:", conf.threshold); - INFO_OUT("Estimate for Max Displacement:", conf.max_disp << " [px]"); - INFO_OUT("Subset Size:", conf.ss_size << " [px]"); - INFO_OUT("Subset Step:", conf.ss_step << " [px]" ); - INFO_OUT("Number of OMP threads:", omp_get_max_threads()); - INFO_OUT("Debug level: ", conf.debug_level); - if (conf.scan_method.find("RG") != std::string::npos)INFO_OUT("Reliability Guided Seed central px location: ", "(" - << conf.rg_seed.first+conf.ss_size/2 << ", " << conf.rg_seed.second+conf.ss_size/2 << ") [px] " ) + common_util::title("Starting Correlation"); } + + int num_px_in_image = conf.px_hori * conf.px_vert; + // get raw pointers - bool* img_roi = static_cast(img_roi_arr.request().ptr); - double* img_stack = static_cast(img_stack_arr.request().ptr); + const bool *img_roi = img_roi_arr.data(); // ------------------------------------------------------------------------ // get a list of ss coordinates within RIO; // ------------------------------------------------------------------------ - std::vector ss_grids; - std::vector ss_sizes, ss_steps; - util::gen_size_and_step_vector(ss_sizes, ss_steps, conf.ss_size, conf.ss_step, conf.max_disp); - fourier::init(ss_grids, ss_sizes, ss_steps, img_roi, conf); + std::vector multiwindow_l; + subset::Grid ss_grid_l; + subset::Grid ss_grid_l_0; + + if (conf.scan_method == util::ScanMethod::MULTIWINDOW_RG || conf.scan_method == util::ScanMethod::MULTIWINDOW) { + multiwindow_init(multiwindow_l, img_roi, conf, mwconf, saveconf); + ss_grid_l = multiwindow_l.back().layout; + ss_grid_l_0 = ss_grid_l; + } + else if (conf.scan_method == util::ScanMethod::SINGLEWINDOW_RG || + conf.scan_method == util::ScanMethod::RASTER) { + + common_util::Timer timer("to create subset grid:", 2); + ss_grid_l = subset::create_grid(img_roi, conf.ss_step, + conf.ss_size, conf.ss_size, + conf.px_hori, conf.px_vert, false); + ss_grid_l_0 = ss_grid_l; + } + else { + throw std::invalid_argument("Unsupported scan method"); + } // resize the results based on subset information - OptResultArrays result_arrays(conf.num_def_img, ss_grids.back().num, - conf.num_params, saveconf.at_end); + ResultArrays results_def_l(ss_grid_l.num, conf.num_params, false); - // set relevent shape function - shapefunc::set(conf.shape_func); + // pointer to hold the reference interpolators (will be created once) + std::unique_ptr interp_ref_l; + std::unique_ptr interp_ref_r; + std::unique_ptr interp_def_l; + std::unique_ptr interp_def_r; + interp_ref_l = make_interp(conf.interp_routine, conf.fullpaths[0]); - // set cost function to use in optimization - optimizer::set_cost_function(conf.corr_crit); + // objects only needed for stereo + std::vector multiwindow_r; + subset::Grid *ss_grid_r = nullptr; + ResultArrays stereo_matches; + ResultArrays results_ref_r; + ResultArrays results_def_r; + stereo::Geometry stereo_geom; + common_util::SaveConfig saveconf_stereo; + // split out filenames into two vectors + auto [basenames_l, basenames_r] = stereo::split_basenames(conf); - // ----------------------------------------------------------------------- - // loop over deformed images and perform DIC - // ----------------------------------------------------------------------- - if (g_debug_level>0){ - std::cout << std::endl; - TITLE("Starting Correlation") - } - common_util::Timer timer("DIC Engine:"); - - // pointer to reference image at start of stack - double *img_ref = img_stack; - - // pointer to hold the reference interpolator (will be created once) - Interpolator* interp_ref = nullptr; - Interpolator* interp_ref_inc = nullptr; - - // loop over deformed images. They start at index 1 in the stack - for (int img_num = 1; img_num < conf.num_def_img+1; img_num++){ + // main bulk of initialisation for stereo matching + int match_strat=3; - // pointer to starting location of deformed image in memory - int num_px_in_image = conf.px_hori * conf.px_vert; - double *img_def = img_stack + img_num*num_px_in_image; + ResultArrays results_ref_l(ss_grid_l.num, conf.num_params, false); - // define our interpolator for the reference image - Interpolator* interp_def = nullptr; - if (conf.interp_routine == "BSPLINE") interp_def = new Bspline(img_def, conf.px_hori, conf.px_vert); - else if (conf.interp_routine == "HERMITE") interp_def = new Hermite(img_def, conf.px_hori, conf.px_vert); + if (conf.stereo){ - // ------------------------------------------------------------------------------------------------------------------------------------------- - // raster scan - // ------------------------------------------------------------------------------------------------------------------------------------------- - if (conf.scan_method=="IMAGE_SCAN") - scanmethod::image(img_ref, *interp_def, ss_grids[0], conf, img_num, result_arrays); + // resize the results based on subset information + results_ref_r = ResultArrays(ss_grid_l.num, conf.num_params, true); + results_def_r = ResultArrays(ss_grid_l.num, conf.num_params, true); + // sort out intrinsic and extrinsic matrices into struct + stereo_geom = stereo::compute_stereo_geometry(calib); + std::unique_ptr interp_l = make_interp(conf.interp_routine, conf.fullpaths[0]); + std::unique_ptr interp_r = make_interp(conf.interp_routine, conf.fullpaths[conf.num_def_img+1]); + } - // ------------------------------------------------------------------------------------------------------------------------------------------- - // multiwindow FFTCC + reliability Guided - // ------------------------------------------------------------------------------------------------------------------------------------------- - else if (conf.scan_method=="MULTIWINDOW_RG") - scanmethod::multiwindow_reliability_guided(img_ref, img_def, *interp_def, ss_grids, conf, img_num, result_arrays); + int img_num_ref_l = 0; + // ----------------------------------------------------------------------- + // loop over deformed images and perform DIC + // ----------------------------------------------------------------------- + for (int img_num = 1; img_num < conf.num_def_img+1; img_num++){ + int img_num_def_l = img_num; + int img_num_def_r = conf.num_def_img+1+img_num; + // interpolator for the L image + interp_def_l = make_interp(conf.interp_routine, conf.fullpaths[img_num_def_l]); - // ------------------------------------------------------------------------------------------------------------------------------------------- - // singlewindow FFTCC + reliability Guided - // ------------------------------------------------------------------------------------------------------------------------------------------- - else if (conf.scan_method=="SINGLEWINDOW_RG"){ - if (!interp_ref && conf.interp_routine=="BSPLINE") interp_ref = new Bspline(img_ref, conf.px_hori, conf.px_vert); - if (!interp_ref && conf.interp_routine=="HERMITE") interp_ref = new Hermite(img_ref, conf.px_hori, conf.px_vert); - scanmethod::singlewindow_incremental_reliability_guided(img_ref, img_def, *interp_ref, *interp_def, ss_grids, conf, 0, img_num, result_arrays); + // interpolator for the R image + if (conf.stereo) { + interp_def_r = make_interp(conf.interp_routine, conf.fullpaths[img_num_def_r]); } + // ---------------------------------------------------------------------------------------- + // raster scan + // ---------------------------------------------------------------------------------------- + if (conf.scan_method == util::ScanMethod::RASTER) { + if (conf.stereo) + throw std::invalid_argument("Unsupported scan method"); + + if (conf.incremental) + throw std::invalid_argument("Raster scan does not support incremental DIC"); + + results_def_l.reset(); + results_def_r.reset(); + + raster(*interp_ref_l, + *interp_def_l, + ss_grid_l, + conf, 0, + img_num, + results_def_l); + } - // ------------------------------------------------------------------------------------------------------------------------------------------- - // multi window FFTCC ONLY - // ------------------------------------------------------------------------------------------------------------------------------------------- - else if (conf.scan_method=="MULTIWINDOW") - scanmethod::multiwindow(img_ref, img_def, *interp_def, ss_grids, conf, img_num, result_arrays); - + // ---------------------------------------------------------------------------------------- + // multiwindow FFTCC + // ---------------------------------------------------------------------------------------- + else if (conf.scan_method == util::ScanMethod::MULTIWINDOW) { + + bool update_ref = conf.incremental && should_update_ref(img_num_def_l, results_def_l, conf); + if (update_ref) { + img_num_ref_l = img_num_def_l - 1; + results_ref_l = results_def_l; + interp_ref_l = make_interp(conf.interp_routine, conf.fullpaths[img_num_ref_l]); + + bool* roi_updated = propagate_roi(img_roi, results_def_l, conf, ss_grid_l); + multiwindow_l.clear(); + multiwindow_init_partial(multiwindow_l, roi_updated, conf, mwconf, saveconf, + mwconf.overlap.size() - 1); + + WindowLevel last_level; + last_level.u.assign(ss_grid_l.num, 0.0); + last_level.v.assign(ss_grid_l.num, 0.0); + last_level.cost.assign(ss_grid_l.num, 0.0); + last_level.max_val.assign(ss_grid_l.num, 0.0); + last_level.level = mwconf.overlap.size() - 1; + last_level.fft_filter = conf.fft_filter; + last_level.fft_filter_threshold = conf.fft_filter_threshold; + last_level.fft_filter_radius = conf.fft_filter_radius; + last_level.fft_filter_corr_power = conf.fft_filter_corr_power; + last_level.fft_save = conf.fft_save; + last_level.saveconf = saveconf; + last_level.step = mwconf.overlap.back(); + last_level.template_size = mwconf.subset_size.back(); + last_level.search_area = mwconf.search_area.back(); + + + // Step 1: update active flags on ss_grid_l + if (img_num_def_l > 1){ + for (int i = 0; i < ss_grid_l.num; i++) { + if (!results_ref_l.above_thresh[i]) { + ss_grid_l.active_ss[i] = false; + } + } + ss_grid_l.active_total = std::count(ss_grid_l.active_ss.begin(), + ss_grid_l.active_ss.end(), true); + } + last_level.layout = ss_grid_l; + for (int i = 0; i < ss_grid_l.num; i++) { + if (ss_grid_l.active_ss[i]) { + last_level.layout.coords[2*i] += results_ref_l.u[i]; + last_level.layout.coords[2*i+1] += results_ref_l.v[i]; + } + } + + multiwindow_l.push_back(std::move(last_level)); + if (multiwindow_l.size() > 1) { + multiwindow_l.back().gen_neighlist(multiwindow_l[multiwindow_l.size()-2].layout); + } + + } + + + results_def_l.reset(); + results_def_r.reset(); + + multiwindow_only(*interp_ref_l, + *interp_def_l, + multiwindow_l, + conf, + img_num_ref_l, + img_num_def_l, + results_ref_l, + results_def_l); - // ------------------------------------------------------------------------------------------------------------------------------------------- - // singlewindow FFTCC + reliability Guided + Incremental Updating - // ------------------------------------------------------------------------------------------------------------------------------------------- - else if (conf.scan_method=="SINGLEWINDOW_RG_INCREMENTAL"){ - double *img_prev = nullptr; - int img_num_prev = img_num-1; - img_prev = img_stack + img_num_prev*num_px_in_image; - if (conf.interp_routine=="BSPLINE") interp_ref_inc = new Bspline(img_prev, conf.px_hori, conf.px_vert); - if (conf.interp_routine=="HERMITE") interp_ref_inc = new Hermite(img_prev, conf.px_hori, conf.px_vert); - scanmethod::singlewindow_incremental_reliability_guided(img_prev, img_def, *interp_ref_inc, *interp_def, ss_grids, conf, img_num_prev, img_num, result_arrays); } + // ---------------------------------------------------------------------------------------- + // singlewindow FFTCC + RG + // ---------------------------------------------------------------------------------------- + else if (conf.scan_method == util::ScanMethod::SINGLEWINDOW_RG) { + if (conf.incremental && should_update_ref(img_num_def_l, results_def_l, conf)) { + + // update left image vars + img_num_ref_l = img_num_def_l - 1; + results_ref_l = results_def_l; + interp_ref_l = make_interp(conf.interp_routine, conf.fullpaths[img_num_ref_l]); + + // update right image vars + if (conf.stereo) + results_ref_r = results_def_r; + + // Step 1: update active flags on ss_grid_l + if (img_num_def_l > 1){ + for (int i = 0; i < ss_grid_l.num; i++) { + if (!results_ref_l.above_thresh[i]) { + ss_grid_l.active_ss[i] = false; + } + } + ss_grid_l.active_total = std::count(ss_grid_l.active_ss.begin(), + ss_grid_l.active_ss.end(), true); + } + + for (int i = 0; i < ss_grid_l.num; i++) { + if (ss_grid_l.active_ss[i]) { + ss_grid_l.coords[2*i] += results_ref_l.u[i]; + ss_grid_l.coords[2*i+1] += results_ref_l.v[i]; + } + } + + + } + + results_def_l.reset(); + results_def_r.reset(); + + singlewindow_rg(*interp_ref_l, + *interp_def_l, + ss_grid_l, + conf, + img_num_ref_l, + img_num_def_l, + results_ref_l, + results_def_l); + + if (conf.stereo) { + + if (match_strat != 3) { + std::cerr << "UNKNOWN MATCH_STRAT\n"; + exit(0); + } + + singlewindow_rg(*interp_ref_l, + *interp_def_r, + ss_grid_l, + conf, + img_num_ref_l, + img_num_def_r, + results_ref_l, + results_def_r, + "stereo", + stereo_geom.F); + + stereo::pixel_to_world(ss_grid_l_0, + calib, + results_def_l, + results_ref_r, + results_def_r, + stereo_geom.K0, + stereo_geom.K1, + stereo_geom.R, + conf.ss_size, + (img_num_def_l==1)); + } + } + // ---------------------------------------------------------------------------------------- + // multiwindow FFTCC + reliability Guided + // ---------------------------------------------------------------------------------------- + else if (conf.scan_method == util::ScanMethod::MULTIWINDOW_RG) { + + bool update_ref = conf.incremental && should_update_ref(img_num_def_l, results_def_l, conf); + if (update_ref) { + + // update left image vars + img_num_ref_l = img_num_def_l - 1; + results_ref_l = results_def_l; + interp_ref_l = make_interp(conf.interp_routine, conf.fullpaths[img_num_ref_l]); + + // update right image vars + if (conf.stereo) + results_ref_r = results_def_r; + + + bool* roi_updated = propagate_roi(img_roi, results_def_l, conf, ss_grid_l); + multiwindow_l.clear(); + multiwindow_init_partial(multiwindow_l, roi_updated, conf, mwconf, saveconf, + mwconf.overlap.size() - 1); + + WindowLevel last_level; + last_level.u.assign(ss_grid_l.num, 0.0); + last_level.v.assign(ss_grid_l.num, 0.0); + last_level.cost.assign(ss_grid_l.num, 0.0); + last_level.max_val.assign(ss_grid_l.num, 0.0); + last_level.level = mwconf.overlap.size() - 1; + last_level.fft_filter = conf.fft_filter; + last_level.fft_filter_threshold = conf.fft_filter_threshold; + last_level.fft_filter_radius = conf.fft_filter_radius; + last_level.fft_filter_corr_power = conf.fft_filter_corr_power; + last_level.fft_save = conf.fft_save; + last_level.saveconf = saveconf; + last_level.step = mwconf.overlap.back(); + last_level.template_size = mwconf.subset_size.back(); + last_level.search_area = mwconf.search_area.back(); + + + // Step 1: update active flags on ss_grid_l + if (img_num_def_l > 1){ + for (int i = 0; i < ss_grid_l.num; i++) { + if (!results_ref_l.above_thresh[i]) { + ss_grid_l.active_ss[i] = false; + } + } + ss_grid_l.active_total = std::count(ss_grid_l.active_ss.begin(), + ss_grid_l.active_ss.end(), true); + } + last_level.layout = ss_grid_l; + for (int i = 0; i < ss_grid_l.num; i++) { + if (ss_grid_l.active_ss[i]) { + last_level.layout.coords[2*i] += results_ref_l.u[i]; + last_level.layout.coords[2*i+1] += results_ref_l.v[i]; + } + } + + multiwindow_l.push_back(std::move(last_level)); + if (multiwindow_l.size() > 1) { + multiwindow_l.back().gen_neighlist(multiwindow_l[multiwindow_l.size()-2].layout); + } + + } + + results_def_l.reset(); + results_def_r.reset(); + + multiwindow_rg(*interp_ref_l, + *interp_def_l, + multiwindow_l, + conf, + img_num_ref_l, + img_num_def_l, + results_ref_l, + results_def_l); + + // multiwindow_rg_stereo(*interp_ref_l, + // *interp_def_l, + // *interp_def_r, + // multiwindow_l, + // conf, + // img_num_ref_l, + // img_num_def_l, + // results_ref_l, + // results_ref_r, + // results_def_l, + // results_def_r, + // stereo_geom.F); + + + // ------------------------------------------------------------ + // stereo + // ------------------------------------------------------------ + if (conf.stereo) { + + if (match_strat != 3) { + std::cerr << "UNKNOWN MATCH_STRAT\n"; + exit(0); + } + + singlewindow_rg(*interp_ref_l, + *interp_def_r, + multiwindow_l.back().layout, + conf, + img_num_ref_l, + img_num_def_r, + results_ref_l, + results_def_r, + "stereo", + stereo_geom.F, + results_def_l); + + stereo::pixel_to_world(ss_grid_l_0, + calib, + results_def_l, + results_ref_r, + results_def_r, + stereo_geom.K0, + stereo_geom.K1, + stereo_geom.R, + conf.ss_size, + (img_num_def_l==1)); + } - if (!saveconf.at_end) - result_arrays.write_to_disk(img_num, saveconf, ss_grids.back(), conf.num_def_img, conf.filenames); + } + else { + throw std::invalid_argument("Unsupported scan method"); + } - if (interp_def) delete interp_def; + if (!conf.stereo){ + results_def_l.write_to_disk_2d(saveconf, ss_grid_l, basenames_l[img_num]); + } + else { + results_def_l.write_to_disk_stereo(results_def_r, saveconf, ss_grid_l, basenames_l[img_num]); + } if (stop_request) break; } - if (saveconf.at_end) - for (int img_num = 1; img_num < conf.num_def_img+1; img_num++) - result_arrays.write_to_disk(img_num, saveconf, ss_grids.back(), conf.num_def_img, conf.filenames); - - - if (interp_ref) delete interp_ref; - - // TODO: don't have shifts as a global var. Should probably make fourier - // stuff a class at some point in the future - fourier::shifts.clear(); - fourier::shifts.shrink_to_fit(); + raise_on_interrupt(); } @@ -220,5 +492,38 @@ void build_info(){ //std::cout << std::endl; } +bool should_update_ref(const int img_num_def_l, const ResultArrays& results, const util::Config& conf) { + // never update on the first deformed image + if (img_num_def_l == 1) { + return false; + } + + switch (conf.incremental_update_cond) { + + case util::IncrementalCond::IMAGE: { + int interval = static_cast(conf.incremental_update_val); + return (img_num_def_l - 1) % interval == 0; + } + + case util::IncrementalCond::ITER: { + if (results.niter.empty()) return false; + + double avg = std::accumulate(results.niter.begin(), results.niter.end(), 0.0) / results.niter.size(); + return avg > conf.incremental_update_val; + } + + case util::IncrementalCond::COST: { + if (results.cost.empty()) return false; + + double avg = std::accumulate(results.cost.begin(), results.cost.end(), 0.0) / results.cost.size(); + return avg > conf.incremental_update_val; + } + default: + throw std::runtime_error( + "Unknown incremental update condition: " + + std::to_string(static_cast(conf.incremental_update_cond)) + ); + } +} diff --git a/src/pyvale/dic/cpp/dicmain.hpp b/src/pyvale/dic/cpp/dicmain.hpp index 42d2a168d..724f97eea 100644 --- a/src/pyvale/dic/cpp/dicmain.hpp +++ b/src/pyvale/dic/cpp/dicmain.hpp @@ -6,6 +6,10 @@ #ifndef DICMAIN_H #define DICMAIN_H + +// STD Library header files +#include + // Pybind11 Header Files #include #include @@ -13,13 +17,18 @@ // common_cpp header files #include "../../common_cpp/util.hpp" +// calib header files +#include "../../calib/cpp/calibstereo.hpp" + // DIC Header Files #include "./dicutil.hpp" +#include "./dicmultiwindow_util.hpp" +#include "./dicresults.hpp" namespace py = pybind11; /** - * @brief Runs the 2D Digital Image Correlation (DIC) engine on input image data. + * @brief Runs Digital Image Correlation (DIC) engine on input image data. * * This function performs 2D DIC using a specified correlation criterion, * shape function, interpolation scheme, and scan method. @@ -42,20 +51,24 @@ namespace py = pybind11; * between the reference and deformed images over subsets defined by the ROI. * * Subset data is initialized and processed in parallel using OpenMP. - * Results are optionally saved after each image or at the end of processing, - * depending on `saveconf.at_end`. + * Results are saved after processing each image. * * @note This function is intended to be called via the Python interface using pybind11. * Image arrays are expected to be contiguous and C-style (row-major) in memory. */ -void DICengine(const py::array_t& img_stack_arr, - const py::array_t& img_roi_arr, - util::Config& conf, - common_util::SaveConfig& saveconf); +void engine(const py::array_t& img_roi_arr, + const Calib &calib, + const util::Config& conf, + const MultiwindowConfig &multiwindowconf, + const common_util::SaveConfig& saveconf); void build_info(); +bool should_update_ref(const int img_num_def_l, const ResultArrays& results, const util::Config& conf); + + + #endif // DICMAIN_H diff --git a/src/pyvale/dic/cpp/dicmultiwindow_only.cpp b/src/pyvale/dic/cpp/dicmultiwindow_only.cpp new file mode 100644 index 000000000..c11927884 --- /dev/null +++ b/src/pyvale/dic/cpp/dicmultiwindow_only.cpp @@ -0,0 +1,140 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + + +// STD library Header files +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// common_cpp headers +#include "../../common_cpp/defines.hpp" +#include "../../common_cpp/progressbar.hpp" +#include "../../common_cpp/dicsignalhandler.hpp" + + +// Program Header files +#include "./dicinterp.hpp" +#include "./dicoptimizer.hpp" +#include "./dicutil.hpp" +#include "./dicsubset.hpp" +#include "./dicresults.hpp" +#include "./dicmultiwindow_util.hpp" + + +void multiwindow_only(const Interpolator &interp_ref, + const Interpolator &interp_def, + std::vector &multiwindow, + const util::Config &conf, + const int img_num_ref, + const int img_num_def, + const ResultArrays &results_ref, + ResultArrays &results_def){ + + // loop over the window sizes and calculate estimates for rigid + // displacement using FFTCC + for (int lvl = 0; lvl < multiwindow.size(); lvl++){ + multiwindow[lvl].calc_rigid_displacements(multiwindow[std::max(0,lvl-1)], + interp_ref, + interp_def, + img_num_ref, img_num_def, + lvl, multiwindow.size(), + conf.basenames, + conf.fft_precision); + } + + const subset::Grid &ss_grid = multiwindow.back().layout; + const int nsizes = multiwindow.size(); + const int last_size = nsizes-1; + + #pragma omp parallel shared(stop_request, results_ref, results_def, multiwindow, ss_grid, conf, interp_ref, interp_def) + { + + subset::Pixels ss_ref(ss_grid.size_x, ss_grid.size_y); + subset::Pixels ss_def(ss_grid.size_x, ss_grid.size_y); + + // get number of subsets and the size for the smalllest window size + const int num_ss = multiwindow[last_size].layout.num; + + + + #pragma omp for + for (int ss = 0; ss < num_ss; ss++){ + + // exit the main DIC loop when ctrl+C is hit + if (stop_request){ + continue; + } + + // append fourier results to master result vectors + OptResult res(conf.num_params); + res.u = multiwindow.back().u[ss]; + res.p[0] = multiwindow.back().u[ss]; + res.v = multiwindow.back().v[ss]; + res.p[1] = multiwindow.back().v[ss]; + + // get the reference subset + const double cx_img0 = ss_grid.coords[ss*2]; + const double cy_img0 = ss_grid.coords[ss*2+1]; + + // get the reference subset + subset::fill_from_centre_coords(ss_ref, cx_img0, cy_img0, interp_ref); + subset::fill_from_shape_params(ss_def, cx_img0, cy_img0, res.p, interp_def, util::ShapeFunc::RIGID); + + // calculate zncc value + double zncc = 0.0; + double mean_def = 0.0; + double mean_ref = 0.0; + + for (int i = 0; i < ss_def.num_px; ++i) { + mean_ref += ss_ref.vals[i]; + mean_def += ss_def.vals[i]; + } + + mean_ref /= ss_ref.num_px; + mean_def /= ss_def.num_px; + + double sum_squared_ref = 0.0; + double sum_squared_def = 0.0; + for (int i = 0; i < ss_def.num_px; ++i) { + sum_squared_ref += (ss_ref.vals[i] - mean_ref) * (ss_ref.vals[i] - mean_ref); + sum_squared_def += (ss_def.vals[i] - mean_def) * (ss_def.vals[i] - mean_def); + } + + // Bail out if either subset is degenerate (uniform/zero intensity) + if (sum_squared_def < 1e-12 || sum_squared_ref < 1e-12) { + zncc = 0.0; + } + else { + const double inv_sum_squared = 1.0 / sqrt(sum_squared_ref*sum_squared_def); + + for (int i = 0; i < ss_def.num_px; ++i) { + const double def_norm = (ss_def.vals[i] - mean_def); + const double ref_norm = (ss_ref.vals[i] - mean_ref); + zncc += ref_norm*def_norm; + } + zncc *= inv_sum_squared; + } + + res.cost = zncc; + res.converged=true; + + if (zncc>=conf.threshold) + res.above_thresh=true; + + res.u += results_ref.u[ss]; + res.v += results_ref.v[ss]; + + results_def.append(res, ss); + } + } +} diff --git a/src/pyvale/dic/cpp/dicmultiwindow_only.hpp b/src/pyvale/dic/cpp/dicmultiwindow_only.hpp new file mode 100644 index 000000000..89a909754 --- /dev/null +++ b/src/pyvale/dic/cpp/dicmultiwindow_only.hpp @@ -0,0 +1,41 @@ +#ifndef DICMULTIWINDOW_ONLY_H +#define DICMULTIWINDOW_ONLY_H + +// STD library Header files +#include + +// common_cpp headers + +// Eigen Header files +#include + +// Program Header files +#include "./dicinterp.hpp" +#include "./dicutil.hpp" +#include "./dicresults.hpp" +#include "./dicmultiwindow_util.hpp" + +/** + * @brief Multi Window Fast Fourier Transform (FFT) DIC method. + * correlation is calculated for initial seed point and nearest neighbours.image + * Scan proceeds along path with better matching subsets. + * A full indepth outline of the method can be found here: + * https://opg.optica.org/ao/abstract.cfm?uri=ao-48-8-1535 + * + * @param img_ref pointer to reference image + * @param img_def pointer to deformed image + * @param ss_grid pointer to subset information + * @param conf pointer to DIC config struct + * @param img_num current image number + */ +void multiwindow_only(const Interpolator &interp_ref, + const Interpolator &interp_def, + std::vector &multiwindow, + const util::Config &conf, + const int img_num_ref, + const int img_num_def, + const ResultArrays &results_ref, + ResultArrays &results_def); + + +#endif // DICMULTIWINDOW_ONLY_H diff --git a/src/pyvale/dic/cpp/dicmultiwindow_rg.cpp b/src/pyvale/dic/cpp/dicmultiwindow_rg.cpp new file mode 100644 index 000000000..98fa238b5 --- /dev/null +++ b/src/pyvale/dic/cpp/dicmultiwindow_rg.cpp @@ -0,0 +1,311 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + + +// STD library Header files +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// common_cpp headers +#include "../../common_cpp/defines.hpp" +#include "../../common_cpp/progressbar.hpp" +#include "../../common_cpp/dicsignalhandler.hpp" + + +// Program Header files +#include "./dicinterp.hpp" +#include "./dicoptimizer.hpp" +#include "./dicutil.hpp" +#include "./dicrg.hpp" +#include "./dicsubset.hpp" +#include "./dicresults.hpp" +#include "./dicmultiwindow_util.hpp" +#include "./dicmultiwindow_rg.hpp" + +void multiwindow_rg(const Interpolator &interp_ref, + const Interpolator &interp_def, + std::vector &multiwindow, + const util::Config &conf, + const int img_num_ref, + const int img_num_def, + const ResultArrays &results_ref, + ResultArrays &results_def){ + + // assign some consts for readability + const int px_hori = conf.px_hori; + const int px_vert = conf.px_vert; + + // subset information + const subset::Grid &ss_grid = multiwindow.back().layout; + const int num_ss = ss_grid.num; + const int ss_size_x = ss_grid.size_x; + const int ss_size_y = ss_grid.size_y; + const int ss_step = ss_grid.step; + + // loop over the window sizes and calculate estimates for rigid + // displacement using FFTCC + for (int lvl = 0; lvl < multiwindow.size(); lvl++){ + multiwindow[lvl].calc_rigid_displacements(multiwindow[std::max(0,lvl-1)], + interp_ref, + interp_def, + img_num_ref, img_num_def, + lvl, multiwindow.size(), + conf.basenames, + conf.fft_precision); + } + + + // progress bar + std::string bar_title = "Temporal \033[1;4m" + conf.basenames[img_num_ref] + "\033[0m -> \033[1;4m" + conf.basenames[img_num_def] + "\033[0m:"; + ProgressBar pbar(bar_title, ss_grid.active_total); + std::atomic current_progress(0); + + const auto t0 = std::chrono::steady_clock::now(); + + // Initialize binary mask for computed points (initialized to 0) + std::vector> computed_mask(ss_grid.mask.size()); + for (auto& val : computed_mask) val.store(0); + + rg::QueueGlobal queue(omp_get_max_threads()); + + + std::atomic error_flag(false); + std::string error_message; + + # pragma omp parallel + { + + int tid = omp_get_thread_num(); + + // Initialize ref and def subsets + subset::Pixels ss_def(ss_size_x, ss_size_y); + subset::Pixels ss_ref(ss_size_x, ss_size_y); + + // Optimization parameters + Optimizer opt(conf.shape_func, conf.corr_crit, conf.max_iter, conf.precision, conf.threshold, ss_size_x*ss_size_y); + + // TODO: for the seed location I'm going to overwride the max + // number of iterations to make sure we get a good convergence. + // this is hardcoded for now. Could do with updating so that + // the seed location is checked ahead of the main correlation run. + + // TODO: opt.seed_iter exposed to user. + opt.max_iter = 200; + + // --------------------------------------------------------------------------------------------------------------------------- + // PROCESS THE SEED SUBSET + // --------------------------------------------------------------------------------------------------------------------------- + if (tid == 0) { + + // num seeds + int num_seeds = conf.rg_seeds.size() / 2; + + + for (int s = 0; s < num_seeds; s++){ + + int seed_x = conf.rg_seeds[2*s]; + int seed_y = conf.rg_seeds[2*s+1]; + + // seed coordinates + int grid_x = seed_x / ss_step; + int grid_y = seed_y / ss_step; + int idx = ss_grid.mask[grid_y * ss_grid.num_ss_x + grid_x]; + + // if a seed subset is no longer active then we've hit a problemo + if (!ss_grid.active_ss[idx]) { + error_message = "Seed subset (" + std::to_string(seed_x) + "," + + std::to_string(seed_y) + ") is no longer active. " + + "The seed subset failed to converge in a previous calculation"; + error_flag.store(true); + break; + } + + double cx = ss_grid.coords[2*idx]; + double cy = ss_grid.coords[2*idx+1]; + + + // if the first image. Take the optimization parameters from rigid fourier + opt.copy_params_from_fft(idx, multiwindow.back().u, multiwindow.back().v); + + // Extract reference subset and solve for starting seed point + subset::fill_from_centre_coords(ss_ref, cx, cy, interp_ref); + for (int px = 0; px < ss_ref.num_px; px++) { + ss_ref.x[px] -= cx; + ss_ref.y[px] -= cy; + } + + OptResult seed_res = opt.solve(cx, cy, ss_ref, ss_def, interp_def, true); + + if (!rg::check_convergence(seed_x, seed_y, seed_res, error_message)) { + error_flag.store(true); + break; + } + + seed_res.u += results_ref.u[idx]; + seed_res.v += results_ref.v[idx]; + + // append the results for the current subset to result vectors + results_def.append(seed_res, idx); + + computed_mask[idx].store(1); + + // loop over the neighbours for the initial seed point + for (size_t n = 0; n < ss_grid.neigh[idx].size(); n++) { + + if (stop_request) continue; + + // subset index of neighbour to the current point + int nidx = ss_grid.neigh[idx][n]; + + const double cx = ss_grid.coords[nidx*2]; + const double cy = ss_grid.coords[nidx*2+1]; + + if (!ss_grid.active_ss[nidx]) { + error_message = "Direct neighbour (" + std::to_string(cx) + "," + std::to_string(cy) + + ") of seed subset (" + std::to_string(seed_x) + "," + std::to_string(seed_y) + + ") is no longer active. The seed subset failed to converge in a previous calculation"; + error_flag.store(true); + break; + } + + // fill the reference subset + subset::fill_from_centre_coords(ss_ref, cx, cy, interp_ref); + for (int px = 0; px < ss_ref.num_px; px++) { + ss_ref.x[px] -= cx; + ss_ref.y[px] -= cy; + } + + // perform optimization for seed point neighbours + opt.copy_params_from_neigh(results_def.p, + results_def.cost, + results_def.above_thresh, + ss_grid.neigh[nidx], + idx); + + // perform optimization for seed point neighbours + OptResult nres = opt.solve(cx, cy, ss_ref, ss_def, interp_def, true); + + if (!rg::check_convergence(seed_x, seed_y, nres, error_message, true)) { + error_flag.store(true); + break; + } + + nres.u += results_ref.u[nidx]; + nres.v += results_ref.v[nidx]; + + // append the results for the current subset to result vectors + results_def.append(nres, nidx); + + // update mask + computed_mask[nidx].store(1); + + // Add points to queue + queue.push(0, {rg::Point(nidx,nres.cost)}); + + // update progress bar + if (g_debug_level>0){ + int progress = current_progress.fetch_add(1); + if (omp_get_thread_num()==0) pbar.update(progress); + } + } + } + } + + // --------------------------------------------------------------------------------------------------------------------------- + // PROCESS ALL OTHER SUBSETS + // --------------------------------------------------------------------------------------------------------------------------- + #pragma omp barrier + + // TODO: reset seed location using the last computed point + opt.max_iter = conf.max_iter; + + std::vector temp_neigh; + temp_neigh.reserve(4); + + rg::Point current(0, 0); + + while (!stop_request && !error_flag.load()) { + + if (!queue.pop(tid, current)) + break; + + temp_neigh.clear(); + + // loop over neighbouring points + for (size_t n = 0; n < ss_grid.neigh[current.idx].size(); n++) { + + // subset index of neighbour to the current point + int nidx = ss_grid.neigh[current.idx][n]; + + int expected = 0; + expected = computed_mask[nidx].exchange(1); + if (expected == 0) { + + // coords of neigh + double cx = ss_grid.coords[nidx*2]; + double cy = ss_grid.coords[nidx*2+1]; + + OptResult nres(opt.num_params); + + // if the subset is no longer active then skip + if (!ss_grid.active_ss[nidx]){ + results_def.append(nres, nidx); + continue; + } + + // fill the reference subset + subset::fill_from_centre_coords(ss_ref, cx, cy, interp_ref); + for (int px = 0; px < ss_ref.num_px; px++) { + ss_ref.x[px] -= cx; + ss_ref.y[px] -= cy; + } + + if (results_def.above_thresh[current.idx]) + opt.copy_params_from_neigh(results_def.p, + results_def.cost, + results_def.above_thresh, + ss_grid.neigh[nidx], + current.idx); + else + opt.copy_params_from_fft(nidx, multiwindow.back().u, multiwindow.back().v); + + // optimize + if (ss_ref.sum!=0) nres = opt.solve(cx, cy, ss_ref, ss_def, interp_def); + + nres.u += results_ref.u[nidx]; + nres.v += results_ref.v[nidx]; + + // append results + results_def.append(nres, nidx); + + // add results to temp neighbour results + temp_neigh.emplace_back(nidx, nres.cost); + + // update progress bar + if (g_debug_level>0){ + int progress = current_progress.fetch_add(1); + if (omp_get_thread_num()==0) pbar.update(progress); + } + } + } + queue.push(tid, temp_neigh); + } + } + if (g_debug_level>0){ + pbar.finish(); + } + + if (error_flag.load()) { + throw std::runtime_error(error_message); + } +} diff --git a/src/pyvale/dic/cpp/dicmultiwindow_rg.hpp b/src/pyvale/dic/cpp/dicmultiwindow_rg.hpp new file mode 100644 index 000000000..1903b8c66 --- /dev/null +++ b/src/pyvale/dic/cpp/dicmultiwindow_rg.hpp @@ -0,0 +1,40 @@ +#ifndef DICMULTIWINDOW_RG_H +#define DICMULTIWINDOW_RG_H + +// STD library Header files +#include + +// common_cpp headers + +// Eigen Header files +#include + +// Program Header files +#include "./dicinterp.hpp" +#include "./dicutil.hpp" +#include "./dicresults.hpp" +#include "./dicmultiwindow_util.hpp" + +/** + * @brief reliability guided scan method. + * correlation is calculated for initial seed point and nearest neighbours.image + * Scan proceeds along path with better matching subsets. + * A full indepth outline of the method can be found here: + * https://opg.optica.org/ao/abstract.cfm?uri=ao-48-8-1535 + * + * @param img_ref pointer to reference image + * @param img_def pointer to deformed image + * @param ss_grid pointer to subset information + * @param conf pointer to DIC config struct + * @param img_num current image number + */ +void multiwindow_rg(const Interpolator &interp_ref, + const Interpolator &interp_def, + std::vector &multiwindow, + const util::Config &conf, + const int img_num_ref, + const int img_num_def, + const ResultArrays &results_ref, + ResultArrays &results_def); + +#endif // DICMULTIWINDOW_RG_H diff --git a/src/pyvale/dic/cpp/dicmultiwindow_util.cpp b/src/pyvale/dic/cpp/dicmultiwindow_util.cpp new file mode 100644 index 000000000..20d91f866 --- /dev/null +++ b/src/pyvale/dic/cpp/dicmultiwindow_util.cpp @@ -0,0 +1,629 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + + +// STD library Header files +#include "dicutil.hpp" +#include +#include +#define _USE_MATH_DEFINES +#include +#include +#include +#include + +// Common Header files +#include "../../common_cpp/progressbar.hpp" +#include "../../common_cpp/defines.hpp" +#include "../../common_cpp/dicsignalhandler.hpp" + +// DIC Header files +#include "./dicmultiwindow_util.hpp" +#include "./dicfourier.hpp" +#include "./dicsubset.hpp" +#include "./dicinterp.hpp" + + +void multiwindow_init(std::vector &level, + const bool *img_roi, + const util::Config &conf, + const MultiwindowConfig &mwconf, + const common_util::SaveConfig &saveconf) { + + common_util::Timer timer("to init multiwindow levels:", 2); + + for (size_t lvl = 0; lvl < mwconf.overlap.size(); lvl++) { + + const bool is_last = (lvl == mwconf.overlap.size() - 1); + const subset::Grid *prev = (lvl > 0) ? &level[lvl-1].layout : nullptr; + + level.emplace_back(img_roi, + mwconf.overlap[lvl], + mwconf.subset_size[lvl], + mwconf.search_area[lvl], + conf.px_hori, conf.px_vert, + !is_last, lvl, + conf.fft_filter, conf.fft_filter_threshold, + conf.fft_filter_radius, conf.fft_filter_corr_power, + conf.fft_save, saveconf, + prev); + + } +} + +void multiwindow_init_partial(std::vector &level, + const bool *img_roi, + const util::Config &conf, + const MultiwindowConfig &mwconf, + const common_util::SaveConfig &saveconf, + const size_t num_levels) { + + for (size_t lvl = 0; lvl < num_levels; lvl++) { + + const bool is_last = (lvl == num_levels - 1); + const subset::Grid *prev = (lvl > 0) ? &level[lvl-1].layout : nullptr; + + level.emplace_back(img_roi, + mwconf.overlap[lvl], + mwconf.subset_size[lvl], + mwconf.search_area[lvl], + conf.px_hori, conf.px_vert, + !is_last, lvl, + conf.fft_filter, conf.fft_filter_threshold, + conf.fft_filter_radius, conf.fft_filter_corr_power, + conf.fft_save, saveconf, + prev); + } +} + +void WindowLevel::gen_neighlist(const subset::Grid &layout_prev) { + + //Timer timer("nearest neighbour collection for :"); + + const int prev_step = layout_prev.step; + + // a list containing the number of neighbours from the previous + // window size for each subset in the current window size + num_neigh_list.resize(layout.num); + + // we know the neigh_list is going to be a max size of + // max_neigh*num_ss. we can resize this later once populated + neigh_list.resize(max_num_neigh*layout.num); + + // shared error state + std::atomic failed(false); + + struct ErrorInfo { + int ss = -1; + int ss_x = 0; + int ss_y = 0; + size_t num_found = 0; + }; + + ErrorInfo error; + + // For each subset, find 4 nearest neighbours in layout_prev + #pragma omp parallel for + for (int ss = 0; ss < layout.num; ++ss) { + + // another thread already failed + if (failed.load()) continue; + + + if (!layout.active_ss[ss]) continue; + + // corner of subset + const int ss_x = layout.coords[2*ss]; + const int ss_y = layout.coords[2*ss+1]; + + // Vector to store pairs of (distance, index) + std::vector> dist_index_list; + + // loop over a 10x10 section from the previous window + int idx_x = (ss_x / prev_step); + int idx_y = (ss_y / prev_step); + + // range of neighbour search + int min_x = std::max(0,idx_x-5); + int min_y = std::max(0,idx_y-5); + int max_x = std::min(layout_prev.num_ss_x,idx_x+6); + int max_y = std::min(layout_prev.num_ss_y,idx_y+6); + + for (int y = min_y; y < max_y; y++){ + for (int x = min_x; x < max_x; x++){ + + // check if point is a valid subset + int nss_idx = layout_prev.mask[y*layout_prev.num_ss_x+x]; + if (nss_idx == -1) continue; + + int nss_x = layout_prev.coords[2*nss_idx]; + int nss_y = layout_prev.coords[2*nss_idx+1]; + + double dx = (nss_x) - ss_x; + double dy = (nss_y) - ss_y; + double dist_sq = dx*dx + dy*dy; + + dist_index_list.emplace_back(dist_sq, nss_idx); + } + } + + // either use max_num_neigh or size of list if less than max_num_neigh + size_t num_neigh = std::min(max_num_neigh, dist_index_list.size()); + + // can't find any neighbours. + if (num_neigh == 0){ + bool expected = false; + // only first thread records error + if (failed.compare_exchange_strong(expected, true)) { + error.ss = ss; + error.ss_x = ss_x; + error.ss_y = ss_y; + error.num_found = dist_index_list.size(); + } + continue; + } + + if (dist_index_list.size() > num_neigh) { + std::nth_element(dist_index_list.begin(), + dist_index_list.begin() + num_neigh, + dist_index_list.end()); + dist_index_list.resize(num_neigh); + } + + num_neigh_list[ss] = static_cast(num_neigh); + + // Store neighbours indices into neighlist + for (size_t i = 0; i < num_neigh; ++i) { + neigh_list[ss*max_num_neigh+i] = dist_index_list[i].second; + } + } + + // report outside OpenMP + if (failed.load()) { + + // char msg[512]; + // + // snprintf(msg, + // sizeof(msg), + // "Could not find any neighbours from the previous FFT " + // "window size for subset (%d, %d). " + // "Found %zu neighbours.", + // error.ss_x, + // error.ss_y, + // error.num_found); + // + // throw std::runtime_error(msg); + } +} + +void WindowLevel::calc_rigid_displacements(const WindowLevel &prev, + const Interpolator &interp_ref, + const Interpolator &interp_def, + const int img_num_ref, + const int img_num_def, + const int window_level, + const int num_levels, + const std::vector &filenames, + const util::FFTPrecision fft_precision){ + + const int px_hori = interp_def.px_hori; + const int px_vert = interp_def.px_vert; + + // TODO: Add a proper flag for this + bool subpx = true; + + // consts + const int num_ss = layout.num; + + if (layout.num == 0 || layout.active_total == 0) { + return; + } + + // set all displacements for multiwindow level to 0 + std::fill(u.begin(), u.end(), 0.0); + std::fill(v.begin(), v.end(), 0.0); + + // progress bar initialisation + std::string bar_title = "FFT " + std::to_string(search_area) + "x" + std::to_string(search_area) + " \033[1;4m" + filenames[img_num_ref] + "\033[0m -> \033[1;4m" + filenames[img_num_def] + "\033[0m:"; + ProgressBar pbar(bar_title, layout.active_total); + std::atomic current_progress = 0; + + + auto run_fft_loop = [&](auto &fft) { + #pragma omp for + for (int ss = 0; ss < layout.num; ss++){ + + // exit when ctrl+C + if (stop_request) continue; + + if (!layout.active_ss[ss]) continue; + + const double cx = layout.coords[2*ss]; + const double cy = layout.coords[2*ss+1]; + + // get the seed for the new window size + double prev_u = 0.0; + double prev_v = 0.0; + + if (level>0) + get_displacement_from_prev_window(prev_u, prev_v, prev, ss, cx, cy); + + std::vector p(6,0.0); + double maxv_local = 0.0; + get_single_window_fftcc_peak_centre(fft, p, maxv_local, + cx, cy, + prev_u, prev_v, + template_size, template_size, + search_area, search_area, + interp_ref, interp_def, false); + + max_val[ss] = maxv_local; + + u[ss] = prev_u+p[0]; + v[ss] = prev_v+p[1]; + + if (g_debug_level>1){ + int progress = current_progress.fetch_add(1); + if (omp_get_thread_num()==0) pbar.update(progress+1); + } + } + }; + + #ifdef _MSC_VER + #pragma omp parallel + #else + #pragma omp parallel shared(stop_request, level, prev, interp_def, img_num_ref, search_area, u, v, max_val) + #endif + { + if (fft_precision == util::FFTPrecision::FLOAT32) { + FFTf fft(search_area, search_area, false); + run_fft_loop(fft); + } else { + FFT fft(search_area, search_area, false); + run_fft_loop(fft); + } + } + + // remove outliers in fft + if (fft_filter && (window_level != num_levels-1)){ + remove_outliers_vector(u, v, max_val, fft_filter_threshold, + fft_filter_radius, fft_filter_corr_power); + } + + if (fft_save){ + + // derive base name from deformed filename (remove path and extension) + std::string base = filenames[img_num_def]; + size_t slash = base.find_last_of("/\\"); + if (slash != std::string::npos) base = base.substr(slash + 1); + size_t dot = base.find_last_of('.'); + if (dot != std::string::npos) base = base.substr(0, dot); + + std::ostringstream str_size_x; + std::ostringstream str_size_y; + + str_size_x << std::setw(4) << std::setfill('0') << search_area; + str_size_y << std::setw(4) << std::setfill('0') << search_area; + + std::string filename = saveconf.basepath + "/fft_displacements_" + base + "_" + + str_size_x.str() + "x" + + str_size_y.str() + ".csv"; + + std::ofstream fout(filename); + + fout << "x" << saveconf.delimiter; + fout << "y" << saveconf.delimiter; + fout << "u" << saveconf.delimiter; + fout << "v" << saveconf.delimiter; + fout << "max_val" << saveconf.delimiter; + fout << "\n"; + + for (int ss = 0; ss < layout.num; ss++){ + fout << layout.coords[2*ss] << saveconf.delimiter; + fout << layout.coords[2*ss+1] << saveconf.delimiter; + fout << u[ss] << saveconf.delimiter; + fout << v[ss] << saveconf.delimiter; + fout << max_val[ss] << "\n"; + } + fout.close(); + } + + if (g_debug_level>1){ + pbar.finish(); + } + } + + +void WindowLevel::get_displacement_from_prev_window(double &prev_x, + double &prev_y, + const WindowLevel &prev, + const int ss, + const double cx, + const double cy) { + + if (num_neigh_list[ss] == 0){ + return; + } + + const double epsilon = 10.0; + double weight_sum_x = 0.0; + double weight_sum_y = 0.0; + double weight_tot = 0.0; + double sum_x = 0; + double sum_y = 0; + + // weighted average of 4 nearest neighbours + for (size_t j = 0; j < num_neigh_list[ss]; ++j) { + + int nidx = neigh_list[ss*max_num_neigh+j]; + double cx_neigh = prev.layout.coords[2*nidx]; + double cy_neigh = prev.layout.coords[2*nidx+1]; + + double dx = cx - cx_neigh; + double dy = cy - cy_neigh; + double dist_sq = dx * dx + dy * dy; + double weight = 1.0 / (dist_sq + epsilon); + + //sum_x += level[i-1].x[nidx]; + //sum_y += level[i-1].y[nidx]; + weight_sum_x += prev.u[nidx] * weight; + weight_sum_y += prev.v[nidx] * weight; + weight_tot += weight; + } + + //prev_x = sum_x / level[i].num_neigh_list[ss]; + //prev_y = sum_y / level[i].num_neigh_list[ss]; + prev_x = weight_sum_x / weight_tot; + prev_y = weight_sum_y / weight_tot; + +} + + +static double weighted_median(std::vector>& vals) +{ + // pair = {value, weight} + + std::sort(vals.begin(), vals.end(), + [](const auto& a, const auto& b) + { + return a.first < b.first; + }); + + double total_w = 0.0; + for (const auto& v : vals) + total_w += v.second; + + double accum = 0.0; + for (const auto& v : vals) + { + accum += v.second; + + if (accum >= 0.5 * total_w) + return v.first; + } + + return vals.back().first; +} + + +void WindowLevel::remove_outliers_vector( + std::vector& u, + std::vector& v, + const std::vector& max_val, + double threshold, + int radius, + double corr_power, + double eps) +{ + if (max_val.empty() || u.empty() || v.empty()) { + return; + } + + std::vector u_new = u; + std::vector v_new = v; + + // --------------------------------------------------------- + // Global max correlation + // --------------------------------------------------------- + + double max_corr = + *std::max_element(max_val.begin(), + max_val.end()); + + if (max_corr <= eps) + return; + + double log_max_corr = std::log1p(max_corr); + + // --------------------------------------------------------- + // Main loop + // --------------------------------------------------------- + + for (int ss = 0; ss < layout.num; ++ss) + { + // subset coords + int ss_x = layout.coords[2 * ss]; + int ss_y = layout.coords[2 * ss + 1]; + + // grid coords + int idx_x = ss_x / layout.step; + int idx_y = ss_y / layout.step; + + int min_x = std::max(0, idx_x - radius); + int min_y = std::max(0, idx_y - radius); + + int max_x = + std::min(layout.num_ss_x, + idx_x + radius + 1); + + int max_y = + std::min(layout.num_ss_y, + idx_y + radius + 1); + + // ----------------------------------------------------- + // Gather weighted neighbours + // ----------------------------------------------------- + + std::vector> neigh_u; + std::vector> neigh_v; + + neigh_u.reserve((2*radius+1)*(2*radius+1)); + neigh_v.reserve((2*radius+1)*(2*radius+1)); + + for (int y = min_y; y < max_y; ++y) + { + for (int x = min_x; x < max_x; ++x) + { + int nss_idx = + layout.mask[y * layout.num_ss_x + x]; + + if (nss_idx == -1 || nss_idx == ss) + continue; + + // --------------------------------------------- + // Spatial weight + // --------------------------------------------- + + double dx = double(x - idx_x); + double dy = double(y - idx_y); + + double dist2 = dx*dx + dy*dy; + + double spatial_w = + std::exp( + -dist2 / + (2.0 * radius * radius)); + + // --------------------------------------------- + // Correlation confidence + // LOG compression for huge dynamic range + // --------------------------------------------- + + double confidence = + std::log1p(max_val[nss_idx]) / + log_max_corr; + + confidence = + std::clamp(confidence, + 0.0, + 1.0); + + double corr_w = + std::pow(confidence, + corr_power); + + // --------------------------------------------- + // Final neighbour weight + // --------------------------------------------- + + double w = spatial_w * corr_w; + + neigh_u.push_back( + {u[nss_idx], w}); + + neigh_v.push_back( + {v[nss_idx], w}); + } + } + + // too few neighbours + if (neigh_u.size() < 5) + continue; + + // ----------------------------------------------------- + // Weighted vector median + // ----------------------------------------------------- + + double med_u = + weighted_median(neigh_u); + + double med_v = + weighted_median(neigh_v); + + // ----------------------------------------------------- + // Compute neighbour residuals + // ----------------------------------------------------- + + std::vector> + residuals; + + residuals.reserve(neigh_u.size()); + + for (size_t i = 0; i < neigh_u.size(); ++i) + { + double du = + neigh_u[i].first - med_u; + + double dv = + neigh_v[i].first - med_v; + + double r = + std::sqrt(du*du + dv*dv); + + double w = + neigh_u[i].second; + + residuals.push_back({r, w}); + } + + // weighted median residual + double med_res = + weighted_median(residuals); + + med_res = + std::max(med_res, eps); + + // ----------------------------------------------------- + // Current vector residual + // ----------------------------------------------------- + + double du0 = u[ss] - med_u; + double dv0 = v[ss] - med_v; + + double r0 = + std::sqrt(du0*du0 + dv0*dv0); + + double normalized_residual = + r0 / med_res; + + // ----------------------------------------------------- + // Adaptive threshold + // high confidence vectors trusted more + // ----------------------------------------------------- + + double self_confidence = + std::log1p(max_val[ss]) / + log_max_corr; + + self_confidence = + std::clamp(self_confidence, + 0.0, + 1.0); + + // threshold range: + // low confidence -> 1.5 + // high confidence -> threshold + + double adaptive_threshold = + 1.5 + + self_confidence * + (threshold - 1.5); + + // ----------------------------------------------------- + // Reject outlier + // ----------------------------------------------------- + + if (normalized_residual > + adaptive_threshold) + { + u_new[ss] = med_u; + v_new[ss] = med_v; + } + } + + u = std::move(u_new); + v = std::move(v_new); +} diff --git a/src/pyvale/dic/cpp/dicmultiwindow_util.hpp b/src/pyvale/dic/cpp/dicmultiwindow_util.hpp new file mode 100644 index 000000000..1ba52d44c --- /dev/null +++ b/src/pyvale/dic/cpp/dicmultiwindow_util.hpp @@ -0,0 +1,291 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + +#ifndef DICMULTIWINDOW_UTIL_H +#define DICMULTIWINDOW_UTIL_H + +// STD library Header files +#include +#include +#include + +// common header files +#include + +// DIC Header files +#include "./dicinterp.hpp" +#include "./dicsubset.hpp" +#include "./dicutil.hpp" + + + + +/** + * @brief Configuration struct for multiwindow correlation parameters. + * + * Members: + * - @c overlap : percentage overlap between adjacent subsets at each level (e.g., 50 for 50% overlap). + * - @c subset_size : vector of subset sizes for each level, ordered from coarsest (largest window) to finest (smallest window). + * - @c search_area : vector of search area sizes (in pixels) for FFT + * correlation at each level, ordered from coarsest to finest. + */ +struct MultiwindowConfig +{ + std::vector overlap; + std::vector subset_size; + std::vector search_area; +}; + +/** + * @brief Represents one level in a multiwindow FFT-based correlation hierarchy. + * + * A WindowLevel defines: + * - the subset layout (grid) at a particular window size, + * - allocated storage for displacement fields (@c u, @c v), + * - peak metrics (@c max_val, @c cost), + * - neighbour relationships to the previous (coarser) level for seeding. + * + * Members: + * - @c u, @c v : displacement fields for each subset at this level. + * - @c cost : user-defined cost value per subset (e.g., ZNSSD or debug metric). + * - @c max_val : peak height from FFT cross-correlation for this subset. + * - @c neigh_list : flattened list of neighbouring indices (previous level). + * - @c num_neigh_list : number of neighbours used for each subset. + * - @c max_num_neigh : maximum neighbours assigned per subset (default: 4). + * - @c level : multi-window level index (0 = coarsest / largest window). + * - @c layout : subset grid definition (coords, mask, sizes, steps). + * + * @note The constructor builds the grid and optionally generates neighbour lists + * if a pointer to @p prev_layout is provided. + * + * @warning The pointer @p img_roi must remain valid during grid construction. + * + * @see WindowLevel::gen_neighlist() + * @see WindowLevel::calc_rigid_displacements() + */ +struct WindowLevel { + + std::vector u; + std::vector v; + std::vector cost; + std::vector max_val; + std::vector neigh_list; + std::vector num_neigh_list; + size_t max_num_neigh = 4; + size_t level; // 0 is largest window + subset::Grid layout; + bool fft_filter; + double fft_filter_threshold; + int fft_filter_radius; + double fft_filter_corr_power; + bool fft_save; + common_util::SaveConfig saveconf; + int step; + int template_size; + int search_area; + + + WindowLevel() = default; + + WindowLevel(const bool *img_roi, + const int step, + const int template_size, + const int search_area, + const int px_hori, + const int px_vert, + const bool allow_outside, + const size_t level, + const bool fft_filter, + const double fft_filter_threshold, + const int fft_filter_radius, + const double fft_filter_corr_power, + const bool fft_save, + const common_util::SaveConfig &saveconf, + const subset::Grid *prev_layout) { + + // create grid for the window + layout = subset::create_grid(img_roi, step, template_size, template_size, px_hori, px_vert, allow_outside); + u.resize(layout.num); + v.resize(layout.num); + cost.resize(layout.num); + max_val.resize(layout.num); + + this->level = level; + this->fft_filter = fft_filter; + this->fft_filter_threshold = fft_filter_threshold; + this->fft_filter_radius = fft_filter_radius; + this->fft_filter_corr_power = fft_filter_corr_power; + this->fft_save = fft_save; + this->saveconf = saveconf; + this->step = step; + this->template_size = template_size; + this->search_area = search_area; + + // create neighbourlist if there's a a previous window size + if (prev_layout) { + gen_neighlist(*prev_layout); + } + } + + + /** + * @brief Generate nearest-neighbour mappings from a previous level's grid. + * + * For each subset in the current level's layout, finds up to @c max_num_neigh + * nearest valid subsets from @p layout_prev within a fixed search window (roughly 10x10 cells + * in the previous level's grid index space). Stores the neighbour count in + * @c num_neigh_list[ss] and neighbour indices in the flattened @c neigh_list buffer + * at positions [ss*max_num_neigh + k]. + * + * This method uses OpenMP for parallelization over current subsets. + * + * @param[in] layout_prev Previous level grid layout used as the source of neighbours. + * + * @pre @c layout and @c layout_prev must be initialized. + * @pre @c max_num_neigh > 0. + * @post @c num_neigh_list is resized to @c layout.num and filled. + * @post @c neigh_list is resized to @c max_num_neigh * layout.num and filled for found neighbours. + * + * @note If no neighbours are found for a subset (rare with a sensible ROI and step), + * the function prints diagnostics to stderr and terminates the process with EXIT_FAILURE. + * @note Uses @c std::nth_element to select the closest K neighbours (by squared distance). + * @note Neighbour validity is determined via @c layout_prev.mask (value -1 indicates invalid). + * + * @par Threading + * Uses `#pragma omp parallel for` over subsets; writes to disjoint regions of + * `num_neigh_list` and `neigh_list`, making it thread-safe for the given buffers. + */ + void gen_neighlist(const subset::Grid &layout_prev); + + + + /** + * @brief Compute rigid (translation-only) displacements at this level using FFT-CC. + * + * For every valid subset in the current level's layout, this method: + * 1. Seeds an initial guess from the previous level via weighted neighbours (if available). + * 2. Extracts the reference subset from @p img_ref. + * 3. Samples the deformed subset from @p interp_def at subpixel-shifted coordinates. + * 4. Zero-normalizes both subsets (ZNSSD). + * 5. Performs FFT-based cross-correlation and 2D Gaussian subpixel peak estimation. + * 6. Stores @c u, @c v (accumulated with the seed) and the peak amplitude (@c max_val). + * + * OpenMP is used to parallelize over subsets. Each thread owns a local @c FFT instance. + * Optionally applies a MAD-based outlier removal at the end if the level is configured to do so. + * + * @param[in] prev Previous level (for seeding); may be ignored if this is the first level. + * @param[in] img_ref Pointer to reference image pixel buffer. + * @param[in] img_def Pointer to deformed image pixel buffer (not directly sampled if using @p interp_def). + * @param[in] interp_def Interpolator providing subpixel access to the deformed image. + * @param[in] img_num_ref Index of the reference image in @p filenames (for diagnostics/progress text). + * @param[in] img_num_def Index of the deformed image in @p filenames (for diagnostics/progress text). + * @param[in] filenames Filenames vector used for progress-bar labeling. + * + * @pre @c layout, @c u, @c v, and @c max_val must be sized to @c layout.num. + * @pre @p interp_def must be valid and thread-safe for concurrent sampling (or internally synchronized). + * @post @c u and @c v contain the estimated translations per subset for this level; @c max_val stores peak amplitudes. + * + * @note Currently forces subpixel refinement to true (TODO flag in code). + * @note Uses Gaussian 2D peak estimation in frequency-domain correlation surface. + * @note Progress reporting depends on @c g_debug_level and may incur minor overhead. + * + * @par Threading + * Uses `#pragma omp parallel` with a private @c FFT instance per thread and a dynamic schedule. + * Writes to disjoint indices of @c u, @c v, @c max_val, making it thread-safe. + */ + void calc_rigid_displacements(const WindowLevel &prev, + const Interpolator &interp_ref, + const Interpolator &interp_def, + const int img_num_ref, + const int img_num_def, + const int window_level, + const int num_levels, + const std::vector &filenames, + const util::FFTPrecision fft_precision); + + + + /** + * @brief Seed current-level displacement from previous level using inverse-distance weighting. + * + * Computes a weighted average of the @p prev level's displacements for the + * nearest neighbours associated with the current subset @p ss. The weight is + * defined as w = 1 / (dist^2 + epsilon), where dist is the Euclidean distance + * between current subset center (@p ss_x, @p ss_y) and the neighbour's center. + * + * @param[out] prev_x Output seed for x-displacement at the current subset. + * @param[out] prev_y Output seed for y-displacement at the current subset. + * @param[in] prev Previous window level providing neighbour mappings and displacements. + * @param[in] ss Index of current subset (into this level's layout). + * @param[in] ss_x X-coordinate (pixels) of the current subset center or top-left (consistent with layout). + * @param[in] ss_y Y-coordinate (pixels) of the current subset center or top-left (consistent with layout). + * + * @pre Neighbour lists for the current level were generated against @p prev (via gen_neighlist). + * @pre For @p ss, @c prev.num_neigh_list[ss] > 0 and the indices in @c prev.neigh_list are valid. + * @post @p prev_x and @p prev_y contain the weighted average seed displacement for the current subset. + * + * @note Uses a small epsilon (10.0) to avoid singularity and to bound weights. + * @note Assumes @c prev.u and @c prev.v contain already computed displacements at the previous level. + */ + void get_displacement_from_prev_window(double &prev_x, + double &prev_y, + const WindowLevel &prev, + const int ss, + const double ss_x, + const double ss_y); + + + + void remove_outliers_vector( + std::vector& u, + std::vector& v, + const std::vector& max_val, + double threshold = 3.0, + int radius = 3, + double corr_power = 2.0, + double eps = 1e-6); + +}; + + + +/** + * @brief Initialize a multi-resolution FFT-CC pyramid (window levels). + * + * Builds the sequence of FFT window levels from the configuration by creating + * progressively smaller subset sizes (powers of two) down to the final + * configured subset size, and associates each level with the previous one for + * seeding/coarse-to-fine propagation. + * + * The constructed levels are appended to @p level using `emplace_back(...)` with: + * - mask/ROI pointer (@p img_roi), + * - step size for this level (half of size for power-of-two levels, or conf.ss_step for the last), + * - subset size for this level, + * - pixel pitch (conf.px_hori, conf.px_vert), + * - a boolean indicating whether there is a finer (next) level, + * - the level index, + * - a pointer to the previous level's layout (or nullptr for the first level). + * + * @param[out] level Output container to which new levels are appended. + * @param[in] img_roi Pointer to the image ROI (mask) used by the layouts. Must remain valid during level construction. + * @param[in] conf Runtime configuration containing max_disp, ss_size, ss_step, px_hori, px_vert. + * @param[in] saveconf Configuration for saving intermediate FFT results . + */ +void multiwindow_init(std::vector &level, + const bool *img_roi, + const util::Config &conf, + const MultiwindowConfig &mwconf, + const common_util::SaveConfig &saveconf); + +void multiwindow_init_partial(std::vector &level, + const bool *img_roi, + const util::Config &conf, + const MultiwindowConfig &mwconf, + const common_util::SaveConfig &saveconf, + const size_t num_levels); + + +#endif // DICMULTIWINDOW_UTIL_H diff --git a/src/pyvale/dic/cpp/dicoptimizer.cpp b/src/pyvale/dic/cpp/dicoptimizer.cpp index 59a956e47..176bb8aa2 100644 --- a/src/pyvale/dic/cpp/dicoptimizer.cpp +++ b/src/pyvale/dic/cpp/dicoptimizer.cpp @@ -21,533 +21,609 @@ #include "./dicoptimizer.hpp" #include "./dicshapefunc.hpp" #include "./dicresults.hpp" +#include "./dicsubset.hpp" +#include "dicutil.hpp" + + +// Constructor +Optimizer::Optimizer(util::ShapeFunc shape_func, + util::CorrCrit cost_func, + int max_iter_, + double precision_, + double threshold_, + int num_px) + + : num_params(get_num_params(shape_func)), + lambda(0.01), + costp(0.0), + costpdp(0.0), + g(num_params, 0.0), + dfdp(num_params, 0.0), + dfdx(num_px), + dfdy(num_px), + H(num_params * num_params, 0.0), + invH(num_params * num_params, 0.0), + p(num_params, 0.0), + dp(num_params, 0.0), + pdp(num_params, 0.0), + augmented(num_params * num_params * 2, 0.0), + max_iter(max_iter_), + precision(precision_), + threshold(threshold_), + optimize_cost(nullptr), + get_pixel(nullptr), + get_dfdp(nullptr), + get_displacement(nullptr) { + + set_shape(shape_func); + set_cost_function(cost_func); +} +// Get number of parameters from shape function name +int Optimizer::get_num_params(util::ShapeFunc shape_func) { + switch (shape_func) { + case util::ShapeFunc::RIGID: + return Rigid::num_params; + case util::ShapeFunc::AFFINE: + return Affine::num_params; + case util::ShapeFunc::QUAD: + return Quad::num_params; + } + throw std::invalid_argument("Unknown shape function"); +} -namespace optimizer { - - - - - // function pointer for correlation criteria - void (*optimize_cost)(const subset::Pixels &ss_ref, subset::Pixels &ss_def, const Interpolator &Interp, optimizer::Parameters &opt, const int global_x, const int global_y); +// Set shape function +void Optimizer::set_shape(util::ShapeFunc shape_func) { + switch (shape_func) { + case util::ShapeFunc::AFFINE: + get_pixel = &Affine::get_pixel; + get_dfdp = &Affine::get_dshape_dp; + get_displacement = &Affine::get_displacement; + break; + case util::ShapeFunc::RIGID: + get_pixel = &Rigid::get_pixel; + get_dfdp = &Rigid::get_dshape_dp; + get_displacement = &Rigid::get_displacement; + break; + case util::ShapeFunc::QUAD: + get_pixel = &Quad::get_pixel; + get_dfdp = &Quad::get_dshape_dp; + get_displacement = &Quad::get_displacement; + break; + } +} +void Optimizer::set_rigid_displacement(double dx, double dy) { + std::fill(p.begin(), p.end(), 0.0); + p[0] = dx; + p[1] = dy; +} - OptResult solve(const double ss_x, const double ss_y, subset::Pixels &ss_ref, subset::Pixels &ss_def, const Interpolator &interp_def, optimizer::Parameters &opt, const std::string &corr_crit){ +OptResult Optimizer::solve(const double cx, + const double cy, + subset::Pixels &ss_ref, + subset::Pixels &ss_def, + const Interpolator &interp_def, + const bool check_on_thresh){ - int iter = 0; - double ftol = 0; - double xtol = 0; - opt.lambda = 0.001; - uint8_t converged = false; - const double eps = 1e-10; + int iter = 0; + double ftol = 0; + double xtol = 0; + lambda = 0.01; + uint8_t converged = false; + const double eps = 1e-10; - // trying relative instead of global coordinates for the optimization - int global_x = ss_ref.x[0]; - int global_y = ss_ref.y[0]; - for (int px = 0; px < ss_ref.num_px; px++){ - ss_ref.x[px] -= global_x; - ss_ref.y[px] -= global_y; - } + while (iter < max_iter) { - while (iter < opt.max_iter) { + // if (cx == 360 && cy ==300){ + // std::cout << " iter: " << iter; + // } - // perform the optimization - optimize_cost(ss_ref, ss_def, interp_def, opt, global_x, global_y); + // perform the optimization + (this->*optimize_cost)(ss_ref, ss_def, interp_def, cx, cy); - // set new damping value - update_lambda(opt.costp, opt.costpdp, opt.p, opt.pdp, opt.lambda, opt.num_params); + // set new damping value + update_lambda(costp, costpdp, p, pdp, lambda, num_params); - // relative change of all parameters - const double dp_norm = std::sqrt(std::inner_product(opt.dp.begin(), opt.dp.end(), opt.dp.begin(), 0.0)); - const double p_norm = std::sqrt(std::inner_product( opt.p.begin(), opt.p.end(), opt.p.begin(), 0.0)); - xtol = dp_norm / (p_norm+eps); + // relative change of all parameters + const double dp_norm = std::sqrt(std::inner_product(dp.begin(), dp.end(), dp.begin(), 0.0)); + const double p_norm = std::sqrt(std::inner_product( p.begin(), p.end(), p.begin(), 0.0)); + xtol = dp_norm / (p_norm+eps); - // variation on correlation coefficient - ftol = std::abs(opt.costpdp - opt.costp) / (std::abs(opt.costp) + eps); + // variation on correlation coefficient + ftol = std::abs(costpdp - costp) / (std::abs(costp) + eps); + converged = (xtol < precision) && (ftol < precision); + bool good_enough = 1.0-0.5*costp > threshold; - // Check converged - if ((xtol < opt.precision) && (ftol < opt.precision)) { - //debug_print(ss_x, ss_y, iter, opt.costp, ftol, xtol, opt.p); - converged=true; - break; - } - iter++; + // Check converged + if (converged) { + if (criteria == util::CorrCrit::SSD) break; + if (!check_on_thresh || good_enough) break; } + iter++; + } - // calculate zncc value - double mean_def = 0.0; - double mean_ref = 0.0; + // calculate zncc value + double zncc = 0.0; + double mean_def = 0.0; + double mean_ref = 0.0; - for (int i = 0; i < ss_def.num_px; ++i) { - mean_ref += ss_ref.vals[i]; - mean_def += ss_def.vals[i]; - } + for (int i = 0; i < ss_def.num_px; ++i) { + mean_ref += ss_ref.vals[i]; + mean_def += ss_def.vals[i]; + } - mean_ref /= ss_ref.num_px; - mean_def /= ss_def.num_px; + mean_ref /= ss_ref.num_px; + mean_def /= ss_def.num_px; - double sum_squared_ref = 0.0; - double sum_squared_def = 0.0; - for (int i = 0; i < ss_def.num_px; ++i) { - sum_squared_ref += (ss_ref.vals[i] - mean_ref) * (ss_ref.vals[i] - mean_ref); - sum_squared_def += (ss_def.vals[i] - mean_def) * (ss_def.vals[i] - mean_def); - } + double sum_squared_ref = 0.0; + double sum_squared_def = 0.0; + for (int i = 0; i < ss_def.num_px; ++i) { + sum_squared_ref += (ss_ref.vals[i] - mean_ref) * (ss_ref.vals[i] - mean_ref); + sum_squared_def += (ss_def.vals[i] - mean_def) * (ss_def.vals[i] - mean_def); + } + // Bail out if either subset is degenerate (uniform/zero intensity) + if (sum_squared_def < 1e-12 || sum_squared_ref < 1e-12) { + zncc = 0.0; + } + else { const double inv_sum_squared = 1.0 / sqrt(sum_squared_ref*sum_squared_def); - double zncc = 0.0; for (int i = 0; i < ss_def.num_px; ++i) { const double def_norm = (ss_def.vals[i] - mean_def); const double ref_norm = (ss_ref.vals[i] - mean_ref); zncc += ref_norm*def_norm; } zncc *= inv_sum_squared; - - - OptResult res(opt.num_params); - shapefunc::get_displacement(res, ss_x-global_x, ss_y-global_y, opt.p); - res.iter = iter; - res.ftol = ftol; - res.xtol = xtol; - res.p = opt.p; - res.cost = zncc; - res.converged = converged; - if (zncc >= opt.threshold) res.above_threshold = true; - - // debugging - //if (iter == opt.max_iter) { - // debug_print(ss_x, ss_y, iter, opt.costp, ftol, xtol, opt.p); - //} - - return res; } + OptResult res(num_params); + get_displacement(res.u, res.v, 0.0, 0.0, p); + res.iter = iter; + res.ftol = ftol; + res.xtol = xtol; + res.p = p; + res.cost = zncc; + res.converged = converged; + if (zncc >= threshold) res.above_thresh = true; + + // debugging + //if (iter == max_iter) { + // debug_print(ss_x, ss_y, iter, costp, ftol, xtol, p); + //} + + return res; +} +void Optimizer::ssd(const subset::Pixels &ss_ref, + subset::Pixels &ss_def, + const Interpolator &interp_def, + const double cx, + const double cy){ + const int num_px = ss_def.num_px; + + // dont need std::vector for ssd + double dfdx_ssd; + double dfdy_ssd; + // interpolation data struct + InterpVals interp_vals; - void ssd(const subset::Pixels &ss_ref, - subset::Pixels &ss_def, - const Interpolator &interp_def, - optimizer::Parameters &opt, - const int global_x, - const int global_y){ - - const int num_px = ss_def.num_px; - const int num_params = opt.num_params; - double dfdx; - double dfdy; - - // interpolation data struct - InterpVals interp_vals; - - // reset derivative and hessian values - std::fill(opt.g.begin(), opt.g.end(), 0.0); - std::fill(opt.H.begin(), opt.H.end(), 0.0); - - // loop over the subset values - for (int i = 0; i < num_px; i++){ - - // apply shape function parameters to deformed subset - shapefunc::get_pixel(ss_def.x[i], ss_def.y[i], ss_ref.x[i], ss_ref.y[i], opt.p); + // reset derivative and hessian values + std::fill(g.begin(), g.end(), 0.0); + std::fill(H.begin(), H.end(), 0.0); - // x and y coordinates of reference subset - double def_x = ss_def.x[i]; - double def_y = ss_def.y[i]; + // loop over the subset values + for (int i = 0; i < num_px; i++){ - // get the subset value and derivitives - interp_vals = interp_def.eval_and_derivs(global_x, global_y, def_x+global_x, def_y+global_y); - ss_def.vals[i] = interp_vals.f; - double def = ss_def.vals[i]; + // apply shape function parameters to deformed subset + get_pixel(ss_def.x[i], ss_def.y[i], ss_ref.x[i], ss_ref.y[i], p); - dfdx = interp_vals.dfdx; - dfdy = interp_vals.dfdy; + // x and y coordinates of reference subset + double def_x = ss_def.x[i]; + double def_y = ss_def.y[i]; - // derivative of shape function with repsect to parameters - shapefunc::get_dfdp(opt.dfdp, def_x, def_y, dfdx, dfdy); + // get the subset value and derivitives + interp_vals = interp_def.eval_and_derivs(cx, cy, def_x+cx, def_y+cy); + ss_def.vals[i] = interp_vals.f; + double def = ss_def.vals[i]; - // Upper triangle of Hessian Matrix - for (int row = 0; row < num_params; row++) { - double dfdp_row = opt.dfdp[row]; - for (int col = row; col < num_params; col++) { - opt.H[row * num_params + col] += dfdp_row * opt.dfdp[col]; - } - } + dfdx_ssd = interp_vals.dfdx; + dfdy_ssd = interp_vals.dfdy; - const double dCost_df = - (ss_ref.vals[i] - def); + // derivative of shape function with repsect to parameters + get_dfdp(dfdp, def_x, def_y, dfdx_ssd, dfdy_ssd); - for (int j = 0; j < num_params; j++) { - opt.g[j] += dCost_df * opt.dfdp[j]; + // Upper triangle of Hessian Matrix + for (int row = 0; row < num_params; row++) { + double dfdp_row = dfdp[row]; + for (int col = row; col < num_params; col++) { + H[row * num_params + col] += dfdp_row * dfdp[col]; } - } - populate_hessian_lower_tri(opt.H, opt.lambda, opt.num_params); - invertMatrix(opt.H, opt.invH, opt.augmented, opt.num_params); - update_shapefunc_parameters(opt.pdp, opt.p, opt.dp, opt.invH, opt.g, opt.num_params); + const double dCost_df = - (ss_ref.vals[i] - def); - // calculate cost function for current and updated parameter values - opt.costp = 0.0; - for (int i = 0; i < num_px; i++){ - opt.costp += (ss_ref.vals[i] - ss_def.vals[i]) * (ss_ref.vals[i] - ss_def.vals[i]); + for (int j = 0; j < num_params; j++) { + g[j] += dCost_df * dfdp[j]; } + } - // calculate cost function for updated parameter values - opt.costpdp = 0.0; - for (int i = 0; i < num_px; ++i) { - shapefunc::get_pixel(ss_def.x[i], ss_def.y[i], ss_ref.x[i], ss_ref.y[i], opt.pdp); - ss_def.vals[i] = interp_def.eval(global_x, global_y, ss_def.x[i]+global_x, ss_def.y[i]+global_y); - opt.costpdp += (ss_ref.vals[i] - ss_def.vals[i]) * (ss_ref.vals[i] - ss_def.vals[i]); - } + populate_hessian_lower_tri(H, lambda, num_params); + invertMatrix(H, invH, augmented, num_params); + update_shapefunc_parameters(pdp, p, dp, invH, g, num_params); + + // calculate cost function for current and updated parameter values + costp = 0.0; + for (int i = 0; i < num_px; i++){ + costp += (ss_ref.vals[i] - ss_def.vals[i]) * (ss_ref.vals[i] - ss_def.vals[i]); } - void nssd(const subset::Pixels &ss_ref, - subset::Pixels &ss_def, - const Interpolator &interp_def, - optimizer::Parameters &opt, - const int global_x, - const int global_y){ + // calculate cost function for updated parameter values + costpdp = 0.0; + for (int i = 0; i < num_px; ++i) { + get_pixel(ss_def.x[i], ss_def.y[i], ss_ref.x[i], ss_ref.y[i], pdp); + ss_def.vals[i] = interp_def.eval(cx, cy, ss_def.x[i]+cx, ss_def.y[i]+cy); + costpdp += (ss_ref.vals[i] - ss_def.vals[i]) * (ss_ref.vals[i] - ss_def.vals[i]); + } +} - // reset derivative and hessian values - std::fill(opt.g.begin(), opt.g.end(), 0.0); - std::fill(opt.H.begin(), opt.H.end(), 0.0); - const int num_px = ss_def.num_px; - const int num_params = opt.num_params; +void Optimizer::nssd(const subset::Pixels &ss_ref, + subset::Pixels &ss_def, + const Interpolator &interp_def, + const double cx, + const double cy){ - std::vector dfdx(num_px); - std::vector dfdy(num_px); + // reset derivative and hessian values + std::fill(g.begin(), g.end(), 0.0); + std::fill(H.begin(), H.end(), 0.0); - double sum_squared_def = 0.0; - double sum_squared_ref = 0.0; - double inv_sum_squared_def; - double inv_sum_squared_ref; + const int num_px = ss_def.num_px; - // interpolation data struct - InterpVals interp_vals; + double sum_squared_def = 0.0; + double sum_squared_ref = 0.0; + double inv_sum_squared_def; + double inv_sum_squared_ref; - // reset cost function - opt.costp = 0.0; - opt.costpdp = 0.0; + // interpolation data struct + InterpVals interp_vals; - // get the normalisation values for both reference and deformed subsets - for (int i = 0; i < num_px; ++i) { + // reset cost function + costp = 0.0; + costpdp = 0.0; - // apply shape function parameters to deformed subset - shapefunc::get_pixel(ss_def.x[i], ss_def.y[i], ss_ref.x[i], ss_ref.y[i], opt.p); + // get the normalisation values for both reference and deformed subsets + for (int i = 0; i < num_px; ++i) { - interp_vals = interp_def.eval_and_derivs(global_x, global_y, ss_def.x[i]+global_x, ss_def.y[i]+global_y); - ss_def.vals[i] = interp_vals.f; - dfdx[i] = interp_vals.dfdx; - dfdy[i] = interp_vals.dfdy; - sum_squared_def += ss_def.vals[i] * ss_def.vals[i]; - sum_squared_ref += ss_ref.vals[i] * ss_ref.vals[i]; - } + // apply shape function parameters to deformed subset + get_pixel(ss_def.x[i], ss_def.y[i], ss_ref.x[i], ss_ref.y[i], p); - inv_sum_squared_def = 1.0 / sqrt(sum_squared_def); - inv_sum_squared_ref = 1.0 / sqrt(sum_squared_ref); + interp_vals = interp_def.eval_and_derivs(cx, cy, ss_def.x[i]+cx, ss_def.y[i]+cy); + ss_def.vals[i] = interp_vals.f; + dfdx[i] = interp_vals.dfdx; + dfdy[i] = interp_vals.dfdy; + sum_squared_def += ss_def.vals[i] * ss_def.vals[i]; + sum_squared_ref += ss_ref.vals[i] * ss_ref.vals[i]; + } + + inv_sum_squared_def = 1.0 / sqrt(sum_squared_def); + inv_sum_squared_ref = 1.0 / sqrt(sum_squared_ref); - // loop over the subset values - for (int i = 0; i < num_px; i++){ + // loop over the subset values + for (int i = 0; i < num_px; i++){ - const double def_x_i = ss_def.x[i]; - const double def_y_i = ss_def.y[i]; - const double dfdx_i = dfdx[i]; - const double dfdy_i = dfdy[i]; + const double def_x_i = ss_def.x[i]; + const double def_y_i = ss_def.y[i]; + const double dfdx_i = dfdx[i]; + const double dfdy_i = dfdy[i]; - // derivative of shape function with repsect to parameters - shapefunc::get_dfdp(opt.dfdp, def_x_i, def_y_i, dfdx_i, dfdy_i); + // derivative of shape function with repsect to parameters + get_dfdp(dfdp, def_x_i, def_y_i, dfdx_i, dfdy_i); - const double dCostdf = - inv_sum_squared_def * (ss_ref.vals[i] * inv_sum_squared_ref - ss_def.vals[i] * inv_sum_squared_def); + const double dCostdf = - inv_sum_squared_def * (ss_ref.vals[i] * inv_sum_squared_ref - ss_def.vals[i] * inv_sum_squared_def); - // Upper triangle of Hessian Matrix - for (int row = 0; row < num_params; row++) { - opt.g[row] += dCostdf * opt.dfdp[row]; - double dfdp_row = opt.dfdp[row]; - for (int col = row; col < num_params; col++) { - opt.H[row * num_params + col] += inv_sum_squared_def * inv_sum_squared_def * dfdp_row * opt.dfdp[col]; - } + // Upper triangle of Hessian Matrix + for (int row = 0; row < num_params; row++) { + g[row] += dCostdf * dfdp[row]; + double dfdp_row = dfdp[row]; + for (int col = row; col < num_params; col++) { + H[row * num_params + col] += inv_sum_squared_def * inv_sum_squared_def * dfdp_row * dfdp[col]; } } + } - populate_hessian_lower_tri(opt.H, opt.lambda, opt.num_params); - invertMatrix(opt.H, opt.invH, opt.augmented, opt.num_params); - update_shapefunc_parameters(opt.pdp, opt.p, opt.dp, opt.invH, opt.g, opt.num_params); + populate_hessian_lower_tri(H, lambda, num_params); + invertMatrix(H, invH, augmented, num_params); + update_shapefunc_parameters(pdp, p, dp, invH, g, num_params); - // calculate cost function for current parameter values - for (int i = 0; i < num_px; i++){ - const double def_norm = ss_def.vals[i] * inv_sum_squared_def; - const double ref_norm = ss_ref.vals[i] * inv_sum_squared_ref; - opt.costp += (ref_norm - def_norm) * (ref_norm - def_norm); - } + // calculate cost function for current parameter values + for (int i = 0; i < num_px; i++){ + const double def_norm = ss_def.vals[i] * inv_sum_squared_def; + const double ref_norm = ss_ref.vals[i] * inv_sum_squared_ref; + costp += (ref_norm - def_norm) * (ref_norm - def_norm); + } - // calculate cost function for updated parameter values - sum_squared_def = 0.0; - for (int i = 0; i < num_px; ++i) { - shapefunc::get_pixel(ss_def.x[i], ss_def.y[i], ss_ref.x[i], ss_ref.y[i], opt.pdp); - ss_def.vals[i] = interp_def.eval(global_x, global_y, ss_def.x[i]+global_x, ss_def.y[i]+global_y); - sum_squared_def += ss_def.vals[i] * ss_def.vals[i]; - } - - inv_sum_squared_def = 1.0 / sqrt(sum_squared_def); + // calculate cost function for updated parameter values + sum_squared_def = 0.0; + for (int i = 0; i < num_px; ++i) { + get_pixel(ss_def.x[i], ss_def.y[i], ss_ref.x[i], ss_ref.y[i], pdp); + ss_def.vals[i] = interp_def.eval(cx, cy, ss_def.x[i]+cx, ss_def.y[i]+cy); + sum_squared_def += ss_def.vals[i] * ss_def.vals[i]; + } - for (int i = 0; i < num_px; ++i) { - const double def_norm = ss_def.vals[i] * inv_sum_squared_def; - const double ref_norm = ss_ref.vals[i] * inv_sum_squared_ref; - opt.costpdp += (ref_norm - def_norm) * (ref_norm - def_norm); - } + inv_sum_squared_def = 1.0 / sqrt(sum_squared_def); + for (int i = 0; i < num_px; ++i) { + const double def_norm = ss_def.vals[i] * inv_sum_squared_def; + const double ref_norm = ss_ref.vals[i] * inv_sum_squared_ref; + costpdp += (ref_norm - def_norm) * (ref_norm - def_norm); } +} - void znssd(const subset::Pixels &ss_ref, - subset::Pixels &ss_def, - const Interpolator &interp_def, - optimizer::Parameters &opt, - const int global_x, - const int global_y){ +void Optimizer::znssd(const subset::Pixels &ss_ref, + subset::Pixels &ss_def, + const Interpolator &interp_def, + const double cx, + const double cy){ - // reset derivative and hessian values - std::fill(opt.g.begin(), opt.g.end(), 0.0); - std::fill(opt.H.begin(), opt.H.end(), 0.0); - const int num_px = ss_def.num_px; - const int num_params = opt.num_params; + // reset derivative and hessian values + std::fill(g.begin(), g.end(), 0.0); + std::fill(H.begin(), H.end(), 0.0); - std::vector dfdx(num_px); - std::vector dfdy(num_px); + const int num_px = ss_def.num_px; - double mean_ref = 0.0; - double mean_def = 0.0; + double mean_ref = 0.0; + double mean_def = 0.0; - // interpolation data struct - InterpVals interp_vals; + // interpolation data struct + InterpVals interp_vals; - // reset cost function - opt.costp = 0.0; - opt.costpdp = 0.0; + // reset cost function + costp = 0.0; + costpdp = 0.0; - // get the normalisation values for both reference and deformed subsets - for (int i = 0; i < num_px; ++i) { + // get the normalisation values for both reference and deformed subsets + for (int i = 0; i < num_px; ++i) { - // apply shape function parameters to deformed subset - shapefunc::get_pixel(ss_def.x[i], ss_def.y[i], ss_ref.x[i], ss_ref.y[i], opt.p); + // apply shape function parameters to deformed subset + get_pixel(ss_def.x[i], ss_def.y[i], ss_ref.x[i], ss_ref.y[i], p); - interp_vals = interp_def.eval_and_derivs(global_x, global_y, ss_def.x[i]+global_x, ss_def.y[i]+global_y); - ss_def.vals[i] = interp_vals.f; - dfdx[i] = interp_vals.dfdx; - dfdy[i] = interp_vals.dfdy; + interp_vals = interp_def.eval_and_derivs(cx, cy, ss_def.x[i]+cx, ss_def.y[i]+cy); + ss_def.vals[i] = interp_vals.f; + dfdx[i] = interp_vals.dfdx; + dfdy[i] = interp_vals.dfdy; - mean_ref += ss_ref.vals[i]; - mean_def += ss_def.vals[i]; + mean_ref += ss_ref.vals[i]; + mean_def += ss_def.vals[i]; - } + // debugging + // std::cout << ss_ref.x[i] << " " << ss_ref.y[i] << " " << ss_ref.vals[i] << " "; + // std::cout << ss_def.x[i] << " " << ss_def.y[i] << " " << ss_def.vals[i] << " "; + // std::cout << std::endl; + } + //exit(0); - mean_def /= num_px; - mean_ref /= num_px; + mean_def /= num_px; + mean_ref /= num_px; - double sum_squared_ref = 0.0; - double sum_squared_def = 0.0; - for (int i = 0; i < num_px; ++i) { - sum_squared_def += (ss_def.vals[i] - mean_def) * (ss_def.vals[i] - mean_def); - sum_squared_ref += (ss_ref.vals[i] - mean_ref) * (ss_ref.vals[i] - mean_ref); - } + double sum_squared_ref = 0.0; + double sum_squared_def = 0.0; + for (int i = 0; i < num_px; ++i) { + sum_squared_def += (ss_def.vals[i] - mean_def) * (ss_def.vals[i] - mean_def); + sum_squared_ref += (ss_ref.vals[i] - mean_ref) * (ss_ref.vals[i] - mean_ref); + } + + // Bail out if either subset is degenerate (uniform/zero intensity) + if (sum_squared_def < 1e-12 || sum_squared_ref < 1e-12) { + costp = 2.0; // max possible ZNSSD value, signals failure + costpdp = 2.0; + return; + } - double inv_sum_squared_def = 1.0 / sqrt(sum_squared_def); - double inv_sum_squared_ref = 1.0 / sqrt(sum_squared_ref); + double inv_sum_squared_def = 1.0 / sqrt(sum_squared_def); + double inv_sum_squared_ref = 1.0 / sqrt(sum_squared_ref); - // loop over the subset values - for (int i = 0; i < num_px; i++){ + // loop over the subset values + for (int i = 0; i < num_px; i++){ - const double def_x_i = ss_def.x[i]; - const double def_y_i = ss_def.y[i]; - const double dfdx_i = dfdx[i]; - const double dfdy_i = dfdy[i]; + const double def_x_i = ss_def.x[i]; + const double def_y_i = ss_def.y[i]; + const double dfdx_i = dfdx[i]; + const double dfdy_i = dfdy[i]; - // derivative of shape function with repsect to parameters - shapefunc::get_dfdp(opt.dfdp, def_x_i, def_y_i, dfdx_i, dfdy_i); + // derivative of shape function with repsect to parameters + get_dfdp(dfdp, def_x_i, def_y_i, dfdx_i, dfdy_i); - const double dCost_df = - inv_sum_squared_def * ((ss_ref.vals[i] - mean_ref) * inv_sum_squared_ref - (ss_def.vals[i] - mean_def) * inv_sum_squared_def); + const double dCost_df = - inv_sum_squared_def * ((ss_ref.vals[i] - mean_ref) * inv_sum_squared_ref - (ss_def.vals[i] - mean_def) * inv_sum_squared_def); - // Upper triangle of Hessian Matrix - for (int row = 0; row < num_params; row++) { - opt.g[row] += dCost_df * opt.dfdp[row]; - double dfdp_row = opt.dfdp[row]; - for (int col = row; col < num_params; col++) { - opt.H[row * num_params + col] += inv_sum_squared_def * inv_sum_squared_def * dfdp_row * opt.dfdp[col]; - } + // Upper triangle of Hessian Matrix + for (int row = 0; row < num_params; row++) { + g[row] += dCost_df * dfdp[row]; + double dfdp_row = dfdp[row]; + for (int col = row; col < num_params; col++) { + H[row * num_params + col] += inv_sum_squared_def * inv_sum_squared_def * dfdp_row * dfdp[col]; } } + } - populate_hessian_lower_tri(opt.H, opt.lambda, opt.num_params); - invertMatrix(opt.H, opt.invH, opt.augmented, opt.num_params); - update_shapefunc_parameters(opt.pdp, opt.p, opt.dp, opt.invH, opt.g, opt.num_params); + populate_hessian_lower_tri(H, lambda, num_params); + invertMatrix(H, invH, augmented, num_params); + update_shapefunc_parameters(pdp, p, dp, invH, g, num_params); - // calculate cost function for current parameter values - for (int i = 0; i < num_px; i++){ - const double def_norm = (ss_def.vals[i] - mean_def) * inv_sum_squared_def; - const double ref_norm = (ss_ref.vals[i] - mean_ref) * inv_sum_squared_ref; - opt.costp += (ref_norm - def_norm) * (ref_norm - def_norm); - } + // calculate cost function for current parameter values + for (int i = 0; i < num_px; i++){ + const double def_norm = (ss_def.vals[i] - mean_def) * inv_sum_squared_def; + const double ref_norm = (ss_ref.vals[i] - mean_ref) * inv_sum_squared_ref; + costp += (ref_norm - def_norm) * (ref_norm - def_norm); + } - // calculate cost function for updated parameter values - mean_def = 0.0; - for (int i = 0; i < num_px; ++i) { - shapefunc::get_pixel(ss_def.x[i], ss_def.y[i], ss_ref.x[i], ss_ref.y[i], opt.pdp); - ss_def.vals[i] = interp_def.eval(global_x, global_y, ss_def.x[i]+global_x, ss_def.y[i]+global_y); - mean_def += ss_def.vals[i]; - } + // calculate cost function for updated parameter values + mean_def = 0.0; + for (int i = 0; i < num_px; ++i) { + get_pixel(ss_def.x[i], ss_def.y[i], ss_ref.x[i], ss_ref.y[i], pdp); + ss_def.vals[i] = interp_def.eval(cx, cy, ss_def.x[i]+cx, ss_def.y[i]+cy); + mean_def += ss_def.vals[i]; + } - mean_def /= num_px; + mean_def /= num_px; - sum_squared_def = 0.0; - for (int i = 0; i < num_px; ++i) { - sum_squared_def += (ss_def.vals[i] - mean_def) * (ss_def.vals[i] - mean_def); - } - - inv_sum_squared_def = 1.0 / sqrt(sum_squared_def); + sum_squared_def = 0.0; + for (int i = 0; i < num_px; ++i) { + sum_squared_def += (ss_def.vals[i] - mean_def) * (ss_def.vals[i] - mean_def); + } + inv_sum_squared_def = 1.0 / sqrt(sum_squared_def); - for (int i = 0; i < num_px; ++i) { - const double def_norm = (ss_def.vals[i] - mean_def) * inv_sum_squared_def; - const double ref_norm = (ss_ref.vals[i] - mean_ref) * inv_sum_squared_ref; - opt.costpdp += (ref_norm - def_norm) * (ref_norm - def_norm); - } + for (int i = 0; i < num_px; ++i) { + const double def_norm = (ss_def.vals[i] - mean_def) * inv_sum_squared_def; + const double ref_norm = (ss_ref.vals[i] - mean_ref) * inv_sum_squared_ref; + costpdp += (ref_norm - def_norm) * (ref_norm - def_norm); } - // Inv matrix using Gauss Elim. - bool invertMatrix(const std::vector& matrix, std::vector& inverse, std::vector& augmented, int num_params) { +} - const int n = num_params; - - // Initialize augmented matrix with input matrix and identity matrix - for (int i = 0; i < n; ++i) { - for (int j = 0; j < n; ++j) { - augmented[i * (2 * n) + j] = matrix[i * n + j]; - augmented[i * (2 * n) + (j + n)] = (i == j) ? 1.0 : 0.0; - } - } +// Inv matrix using Gauss Elim. +bool Optimizer::invertMatrix(const std::vector& matrix, std::vector& inverse, std::vector& augmented, int num_params) { - // Gauss-Jordan Elimination - for (int i = 0; i < n; ++i) { - // Search for max element in column - double maxEl = std::abs(augmented[i * (2 * n) + i]); - int maxRow = i; - for (int k = i + 1; k < n; ++k) { - if (std::abs(augmented[k * (2 * n) + i]) > maxEl) { - maxEl = std::abs(augmented[k * (2 * n) + i]); - maxRow = k; - } - } + const int n = num_params; + + // Initialize augmented matrix with input matrix and identity matrix + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + augmented[i * (2 * n) + j] = matrix[i * n + j]; + augmented[i * (2 * n) + (j + n)] = (i == j) ? 1.0 : 0.0; + } + } - // Swap maximum row with current row - if (i != maxRow) { - for (int j = 0; j < 2 * n; ++j) { - std::swap(augmented[i * (2 * n) + j], augmented[maxRow * (2 * n) + j]); - } + // Gauss-Jordan Elimination + for (int i = 0; i < n; ++i) { + // Search for max element in column + double maxEl = std::abs(augmented[i * (2 * n) + i]); + int maxRow = i; + for (int k = i + 1; k < n; ++k) { + if (std::abs(augmented[k * (2 * n) + i]) > maxEl) { + maxEl = std::abs(augmented[k * (2 * n) + i]); + maxRow = k; } + } - // Make the pivot element 1 - double pivot = augmented[i * (2 * n) + i]; - if (pivot == 0) { - return false; // Singular matrix, can't invert - } + // Swap maximum row with current row + if (i != maxRow) { for (int j = 0; j < 2 * n; ++j) { - augmented[i * (2 * n) + j] /= pivot; + std::swap(augmented[i * (2 * n) + j], augmented[maxRow * (2 * n) + j]); } + } - // Make the elements below the pivot 0 - for (int k = i + 1; k < n; ++k) { - double factor = augmented[k * (2 * n) + i]; - for (int j = 0; j < 2 * n; ++j) { - augmented[k * (2 * n) + j] -= augmented[i * (2 * n) + j] * factor; - } - } + // Make the pivot element 1 + double pivot = augmented[i * (2 * n) + i]; + if (pivot == 0) { + return false; // Singular matrix, can't invert + } + for (int j = 0; j < 2 * n; ++j) { + augmented[i * (2 * n) + j] /= pivot; } - // Perform back substitution to eliminate entries above the pivot - for (int i = n - 1; i >= 0; --i) { - for (int k = i - 1; k >= 0; --k) { - double factor = augmented[k * (2 * n) + i]; - for (int j = 0; j < 2 * n; ++j) { - augmented[k * (2 * n) + j] -= augmented[i * (2 * n) + j] * factor; - } + // Make the elements below the pivot 0 + for (int k = i + 1; k < n; ++k) { + double factor = augmented[k * (2 * n) + i]; + for (int j = 0; j < 2 * n; ++j) { + augmented[k * (2 * n) + j] -= augmented[i * (2 * n) + j] * factor; } } + } - // Extract the inverse matrix from the augmented matrix - for (int i = 0; i < n; ++i) { - for (int j = 0; j < n; ++j) { - inverse[i * n + j] = augmented[i * (2 * n) + (j + n)]; + // Perform back substitution to eliminate entries above the pivot + for (int i = n - 1; i >= 0; --i) { + for (int k = i - 1; k >= 0; --k) { + double factor = augmented[k * (2 * n) + i]; + for (int j = 0; j < 2 * n; ++j) { + augmented[k * (2 * n) + j] -= augmented[i * (2 * n) + j] * factor; } } - - return true; } - void populate_hessian_lower_tri(std::vector &H, double lambda, int num_params){ - for (int row = 0; row < num_params; row++) { - for (int col = row + 1; col < num_params; col++) { - H[col * num_params + row] = H[row * num_params + col]; - } - H[row * num_params + row] += lambda * H[row * num_params + row]; // diagonal + // Extract the inverse matrix from the augmented matrix + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + inverse[i * n + j] = augmented[i * (2 * n) + (j + n)]; } } - void update_lambda(double costp, double costpdp, std::vector &p, std::vector &pdp, double &lambda, int num_params){ + return true; +} - if (costp < costpdp){ - lambda *= 10.0; - } - else{ - lambda *= 0.1; - for (int i = 0; i < num_params; i++){ - p[i] = pdp[i]; - } +void Optimizer::populate_hessian_lower_tri(std::vector &H, double lambda, int num_params){ + for (int row = 0; row < num_params; row++) { + for (int col = row + 1; col < num_params; col++) { + H[col * num_params + row] = H[row * num_params + col]; } + H[row * num_params + row] += lambda * H[row * num_params + row]; // diagonal } +} - void update_shapefunc_parameters(std::vector &pdp, std::vector &p, std::vector &dp, std::vector &invH, std::vector &g, int num_params){ +void Optimizer::update_lambda(double costp, double costpdp, std::vector &p, std::vector &pdp, double &lambda, int num_params){ - // multiply inverse with gradient - for (int i = 0; i < num_params; ++i) { - dp[i] = 0.0; - for (int j = 0; j < num_params; ++j) { - dp[i] += 1.0 * invH[i*num_params + j] * g[j]; - } + if (costp < costpdp){ + lambda *= 2.0; + } + else{ + lambda *= 0.5; + for (int i = 0; i < num_params; i++){ + p[i] = pdp[i]; } + } +} - // add p to delta p - for (int i = 0; i < num_params; ++i) { - pdp[i] = p[i] - dp[i]; +void Optimizer::update_shapefunc_parameters(std::vector &pdp, std::vector &p, std::vector &dp, std::vector &invH, std::vector &g, int num_params){ + + // multiply inverse with gradient + for (int i = 0; i < num_params; ++i) { + dp[i] = 0.0; + for (int j = 0; j < num_params; ++j) { + dp[i] += 1.0 * invH[i*num_params + j] * g[j]; } } + // add p to delta p + for (int i = 0; i < num_params; ++i) { + pdp[i] = p[i] - dp[i]; + } +} - void set_cost_function(const std::string& corr_crit) { - if (corr_crit == "SSD") optimize_cost = ssd; - else if (corr_crit == "NSSD") optimize_cost = nssd; - else if (corr_crit == "ZNSSD") optimize_cost = znssd; - else { - std::cerr << "Unexpected Correlation Criteria: '" << corr_crit << "'" << std::endl; - std::cerr << "Allowed Values: 'SSD', 'NSSD', 'ZNSSD'." << std::endl; - exit(EXIT_FAILURE); - } + +void Optimizer::set_cost_function(util::CorrCrit corr_crit) { + switch (corr_crit) { + case util::CorrCrit::SSD: + optimize_cost = &Optimizer::ssd; + break; + case util::CorrCrit::NSSD: + optimize_cost = &Optimizer::nssd; + break; + case util::CorrCrit::ZNSSD: + optimize_cost = &Optimizer::znssd; + break; } + criteria = corr_crit; +} - void debug_print(int ss_x, int ss_y, int iter, double costp, double ftol, double xtol, const std::vector& p) { +void Optimizer::debug_print(const int ss_x, const int ss_y, int iter, double costp, double ftol, double xtol) { #pragma omp critical { std::cout << omp_get_thread_num() << " "; @@ -559,6 +635,58 @@ namespace optimizer { std::cout << std::endl; } } +void Optimizer::copy_params_from_fft(const int idx, + const std::vector &shift_x, + const std::vector &shift_y) { + std::fill(p.begin(), p.end(), 0.0); + p[0] = shift_x[idx]; + p[1] = shift_y[idx]; +} + +void Optimizer::copy_params_from_neigh(const std::vector &results_p, + const int idx) { + const int idx_p = idx*num_params; + for (int i = 0; i < p.size(); i++) + p[i] = results_p[idx_p+i]; +} + +void Optimizer::copy_params_from_neigh(const std::vector &results_p, + const std::vector &results_cost, + const std::vector &results_above_thresh, + const std::vector &neigh, + const int fallback_idx) { + std::fill(p.begin(), p.end(), 0.0); + + double weight_sum = 0.0; + int count = 0; + + for (int nidx : neigh) { + if (count == 4) break; + if (!results_above_thresh[nidx]) continue; + + const double weight = results_cost[nidx]; + if (weight <= 0.0) continue; + + const int idx_p = nidx * num_params; + for (int i = 0; i < static_cast(p.size()); i++) { + p[i] += weight * results_p[idx_p + i]; + } + + weight_sum += weight; + count++; + } + if (weight_sum <= 0.0) { + copy_params_from_neigh(results_p, fallback_idx); + return; + } + + for (int i = 0; i < static_cast(p.size()); i++) { + p[i] /= weight_sum; + } +} +// Reset parameters to zero +void Optimizer::reset_params() { + std::fill(p.begin(), p.end(), 0.0); } diff --git a/src/pyvale/dic/cpp/dicoptimizer.hpp b/src/pyvale/dic/cpp/dicoptimizer.hpp index 2908f6e04..1a48d8c57 100644 --- a/src/pyvale/dic/cpp/dicoptimizer.hpp +++ b/src/pyvale/dic/cpp/dicoptimizer.hpp @@ -10,270 +10,217 @@ // STD library Header files #include +#include +#include // Program Header files -#include "./dicresults.hpp" +#include "./dicsubset.hpp" #include "./dicinterp.hpp" - - -namespace optimizer { - - - /** - * @brief - * - */ - struct Parameters { - int num_params; - double lambda; // damping - double costp; // cost function for current P values - double costpdp; // cost function for P+deltaP values - std::vector g; // gradient - std::vector dfdp; // derivative of shape function with respect to parameters - std::vector H; // Hessian ( becomes (H + lambda * diag(H)) ) - std::vector invH; // Used for inverse of (H + lambda * diag(H)) - std::vector p; // hard coded affine parameters - std::vector dp; // deltaP - std::vector pdp; // P + deltaP - std::vector augmented; +#include "./dicutil.hpp" + + +struct OptResult { + std::vector p; + double u = 0.0; + double v = 0.0; + double mag = 0.0; + double ftol = 0.0; + double xtol = 0.0; + int iter = 0; + double cost = 0.0; + uint8_t converged = false; + uint8_t above_thresh = false; + OptResult(size_t num_params) : p(num_params, 0.0) {} +}; + +class Optimizer { + + public: + // Constructor + Optimizer(util::ShapeFunc shape_func, + util::CorrCrit cost_func, + int max_iter, + double precision, + double threshold, + int ss_size); + + // Main solve method + OptResult solve(const double cx, + const double cy, + subset::Pixels &ss_ref, + subset::Pixels &ss_def, + const Interpolator &interp_def, + const bool check_on_thresh=false); + + // Public access to parameters + const int num_params; // Number of parameters int max_iter; - double precision; - double threshold; + std::vector p; // Current parameters + std::vector dp; // Parameter updates + std::vector pdp; // p + dp + + + void set_rigid_displacement(double dx, double dy); + + void copy_params_from_fft(const int idx, + const std::vector &shift_x, + const std::vector &shift_y); + + void copy_params_from_neigh(const std::vector &results_p, + const int idx); + + void copy_params_from_neigh(const std::vector &results_p, + const std::vector &results_cost, + const std::vector &results_above_thresh, + const std::vector &neigh, + const int fallback_idx); + + void reset_params(); + + private: + + util::CorrCrit criteria; + double costp; + double costpdp; + std::vector g; // Gradient + std::vector dfdp; // Derivative of shape function wrt parameters + std::vector dfdx; // Derivative of shape function wrt parameters + std::vector dfdy; // Derivative of shape function wrt parameters + std::vector H; // Hessian + std::vector invH; // Inverse Hessian + std::vector augmented; // For matrix inversion + double lambda; // Damping parameter + + // Settings + const double precision; + const double threshold; int px_vert; int px_hori; - - - // Constructor to initialize vectors and other parameters - Parameters(int num_params_, int max_iter_, double precision_, - double threshold_, int px_vert_, int px_hori_, - const std::string& corr_crit) - : - num_params(num_params_), - lambda(0.01), - costp(0.0), - costpdp(0.0), - g(num_params, 0.0), - dfdp(num_params, 0.0), - H(num_params*num_params, 0.0), - invH(num_params*num_params, 0.0), - p(num_params, 0.0), - dp(num_params, 0.0), - pdp(num_params, 0.0), - augmented(num_params*num_params*2, 0.0), - max_iter(max_iter_), - precision(precision_), - threshold(threshold_), - px_vert(px_vert_), - px_hori(px_hori_) { - } - }; - - /** - * @brief This function gets called before the corrolation optimization starts. Sets the function pointer for the user specified shape function. - * - * @param[in] corr_crit string for the correlation criteria, e.g. "SSD", "NSSD", "ZNSSD". - */ - void set_cost_function(const std::string& corr_crit); - - /** - * @brief - * - * @param ss_x - * @param ss_y - * @param iter - * @param costp - * @param ftol - * @param xtol - * @param p - */ - void debug_print(int ss_x, int ss_y, int iter, double costp, double ftol, double xtol, const std::vector& p); - - - /** - * @brief - * - * @param ss_x - * @param ss_y - * @param ss_ref - * @param ss_def - * @param interp_ref - * @param opt - * @return OptResult - */ - OptResult solve(const double ss_x, const double ss_y, subset::Pixels &ss_ref, subset::Pixels &ss_def, const Interpolator &interp_ref, optimizer::Parameters &opt, const std::string &corr_crit); - - /** - * @brief calcutes the Sum of Squared Differences (SSD) between reference and deformed subsets. - * - * @param[in] ss_ref reference subset - * @param[in,out] ss_def deformed subset - * @param[in] interp_def interpolator for deformed image - * @param[in,out] opt Optimization parameters including gradient, Hessian, etc. - */ - void ssd(const subset::Pixels &ss_ref, subset::Pixels &ss_def, const Interpolator &interp_def, optimizer::Parameters &opt); - - /** - * @brief calcutes the Normalized Sum of Squared Differences (NSSD) between reference and deformed subsets. - * - * @param[in] ss_ref reference subset - * @param[in,out] ss_def deformed subset - * @param[in] interp_def interpolator for deformed image - * @param[in,out] opt Optimization parameters including gradient, Hessian, etc. - */ - void nssd(const subset::Pixels &ss_ref, subset::Pixels &ss_def, const Interpolator &interp_def, optimizer::Parameters &opt); - - /** - * @brief calcutes the Zero Normalized Sum of Squared Differences (ZNSSD) between reference and deformed subsets. - * - * @param[in] ss_ref reference subset - * @param[in,out] ss_def deformed subset - * @param[in] interp_def interpolator for deformed image - * @param[in,out] opt Optimization parameters including gradient, Hessian, etc. - */ - void znssd(const subset::Pixels &ss_ref, subset::Pixels &ss_def, const Interpolator &interp_def, optimizer::Parameters &opt); - - - /** - * @brief Inverts square matrix using Gauss-Jordan elimination. - * - * @param[in] matrix - * @param[out] inverse - * @param[in] augmented - * @param[in] num_params Number of shape function parameters (2 for rigid, 6 for affine, ...) - * @return true Matrix inversion was successful - * @return false Matrix inversion failed - */ - bool invertMatrix(const std::vector& matrix, std::vector& inverse, std::vector& augmented, int num_params); - - /** - * @brief Updates the shape function parameters based on the current and updated parameters. - * - * @param[out] pdp shape function parameters for P+deltaP - * @param[in] p current shape function parameters P - * @param[out] dp the change in shape function for based on the Hessian and gradient - * @param[in] invH inverse of the Hessian matrix - * @param[in] g gradient vector - * @param[in] num_params Number of shape function parameters (2 for rigid, 6 for affine, ...) - */ - void update_shapefunc_parameters(std::vector &pdp, std::vector &p, std::vector &dp, std::vector &invH, std::vector &g, int num_params); - - /** - * @brief - * - * @param[in] costp cost value for current shape function parameters P - * @param[in] costpdp cost value for updated shape function parameters P+deltaP - * @param[out] p shape function parameters for P - * @param[in] pdp shape function parameters for P+deltaP - * @param lambda Optimization damping factor - * @param[in] num_params Number of shape function parameters (2 for rigid, 6 for affine, ...) - */ - void update_lambda(double costp, double costpdp, std::vector &p, std::vector &pdp, double &lambda, int num_params); - - /** - * @brief Populates the lower triangular part of the Hessian matrix, H. - * - * @param[in,out] H Hessian matrix - * @param[out] lambda Optimization damping factor - * @param[in] num_params Number of shape function parameters (2 for rigid, 6 for affine, ...) - */ - void populate_hessian_lower_tri(std::vector &H, double lambda, int num_params); - - /** - * @brief - * - * @param[out] x_new - * @param[out] y_new - * @param[in] x - * @param[in] y - * @param[in] p - */ - inline void affine(double &x_new, double &y_new, double x, double y, std::vector &p); - - /** - * @brief - * - * @param[out] x_new - * @param[out] y_new - * @param[in] x - * @param[in] y - * @param[in] p - */ - inline void rigid(double &x_new, double &y_new, double x, double y, std::vector &p); - - /** - * @brief - * - * @param[out] x_new - * @param[out] y_new - * @param[in] x - * @param[in] y - * @param[in] p - */ - inline void quad(double &x_new, double &y_new, double x, double y, std::vector &p); - - /** - * @brief calculates the derivative of the affine function with respect to each parameter - * - * @param[out dfdp - * @param[in] x - * @param[in] y - * @param[in] dfdx - * @param[in] dfdy - */ - inline void daffine_dp(std::vector &dfdp, double x, double y, double dfdx, double dfdy); - - /** - * @brief calculates the derivative of the rigid shape function with respect to each parameter - * - * @param[out dfdp - * @param[in] x - * @param[in] y - * @param[in] dfdx - * @param[in] dfdy - */ - inline void drigid_dp(std::vector &dfdp, double x, double y, double dfdx, double dfdy); - - /** - * @brief calculates the derivative of the quadratic function with respect to each parameter - * - * @param[out] x_new - * @param[out] y_new - * @param[in] x - * @param[in] y - * @param[in] p - */ - inline void dquad_dp(std::vector &dfdp, double x, double y, double dfdy); - - /** - * @brief Funcion to convert affine shape function parameters to displacement values - * - * @param[out] displacements values (u,v, magnitude) are added to results - * @param[in] ss_x subset x coordinate - * @param[in] ss_y subset y coordinate - * @param[in] p shape function parameters - */ - void quad_parameters_to_displacement(OptResult &results, double ss_x, double ss_y, std::vector &p); - - /** - * @brief Funcion to convert affine shape function parameters to displacement values - * - * @param[out] displacements values (u,v, magnitude) are added to results - * @param[in] ss_x subset x coordinate - * @param[in] ss_y subset y coordinate - * @param[in] p shape function parameters - */ - void affine_parameters_to_displacement(OptResult &results, double ss_x, double ss_y, std::vector &p); - - /** - * @brief Funcion to convert affine shape function parameters to displacement values - * - * @param[out] displacements values (u,v, magnitude) are added to results - * @param[in] ss_x subset x coordinate - * @param[in] ss_y subset y coordinate - * @param[in] p shape function parameters - */ - void rigid_parameters_to_displacement(OptResult &results, double ss_x, double ss_y, std::vector &p); - -} + + // Points to cost function + void (Optimizer::*optimize_cost)(const subset::Pixels&, subset::Pixels&, const Interpolator&, const double, const double); + + // Shape function pointers + void (*get_pixel)(double&, double&, const double, const double, const std::vector&); + void (*get_dfdp)(std::vector&, const double, const double, const double, const double); + void (*get_displacement)(double&, double&, const double, const double, const std::vector&); + + // Helper functions + static int get_num_params(util::ShapeFunc shape_func); + + + + + void set_shape(util::ShapeFunc shape_func); + + /** + * @brief This function gets called before the corrolation optimization starts. Sets the function pointer for the user specified shape function. + * + * @param[in] corr_crit correlation criteria enum. + */ + void set_cost_function(util::CorrCrit corr_crit); + + /** + * @brief + * + * @param ss_x + * @param ss_y + */ + void debug_print(const int ss_x, const int ss_y, int iter, double costp, double ftol, double xtol); + + + /** + * @brief calcutes the Sum of Squared Differences (SSD) between reference and deformed subsets. + * + * @param[in] ss_ref reference subset + * @param[in,out] ss_def deformed subset + * @param[in] interp_def interpolator for deformed image + * @param[in] cx x coordinate at subset centre + * @param[in] cy y coordinate at subset centre + */ + void ssd(const subset::Pixels &ss_ref, + subset::Pixels &ss_def, + const Interpolator &interp_def, + const double cx, + const double cy); + + /** + * @brief calcutes the Normalized Sum of Squared Differences (NSSD) between reference and deformed subsets. + * + * @param[in] ss_ref reference subset + * @param[in,out] ss_def deformed subset + * @param[in] interp_def interpolator for deformed image + * @param[in] cx x coordinate at subset centre + * @param[in] cy y coordinate at subset centre + */ + void nssd(const subset::Pixels &ss_ref, + subset::Pixels &ss_def, + const Interpolator &interp_def, + const double cx, + const double cy); + + /** + * @brief calcutes the Zero Normalized Sum of Squared Differences (ZNSSD) between reference and deformed subsets. + * + * @param[in] ss_ref reference subset + * @param[in,out] ss_def deformed subset + * @param[in] interp_def interpolator for deformed image + * @param[in] cx x coordinate at subset centre + * @param[in] cy y coordinate at subset centre + */ + void znssd(const subset::Pixels &ss_ref, + subset::Pixels &ss_def, + const Interpolator &interp_def, + const double cx, + const double cy); + + + /** + * @brief Inverts square matrix using Gauss-Jordan elimination. + * + * @param[in] matrix + * @param[out] inverse + * @param[in] augmented + * @param[in] num_params Number of shape function parameters (2 for rigid, 6 for affine, ...) + * @return true Matrix inversion was successful + * @return false Matrix inversion failed + */ + bool invertMatrix(const std::vector& matrix, std::vector& inverse, std::vector& augmented, int num_params); + + /** + * @brief Updates the shape function parameters based on the current and updated parameters. + * + * @param[out] pdp shape function parameters for P+deltaP + * @param[in] p current shape function parameters P + * @param[out] dp the change in shape function for based on the Hessian and gradient + * @param[in] invH inverse of the Hessian matrix + * @param[in] g gradient vector + * @param[in] num_params Number of shape function parameters (2 for rigid, 6 for affine, ...) + */ + void update_shapefunc_parameters(std::vector &pdp, std::vector &p, std::vector &dp, std::vector &invH, std::vector &g, int num_params); + + /** + * @brief + * + * @param[in] costp cost value for current shape function parameters P + * @param[in] costpdp cost value for updated shape function parameters P+deltaP + * @param[out] p shape function parameters for P + * @param[in] pdp shape function parameters for P+deltaP + * @param lambda Optimization damping factor + * @param[in] num_params Number of shape function parameters (2 for rigid, 6 for affine, ...) + */ + void update_lambda(double costp, double costpdp, std::vector &p, std::vector &pdp, double &lambda, int num_params); + + /** + * @brief Populates the lower triangular part of the Hessian matrix, H. + * + * @param[in,out] H Hessian matrix + * @param[out] lambda Optimization damping factor + * @param[in] num_params Number of shape function parameters (2 for rigid, 6 for affine, ...) + */ + void populate_hessian_lower_tri(std::vector &H, double lambda, int num_params); + + +}; #endif //DICOPTIMIZER_H diff --git a/src/pyvale/dic/cpp/dicrasterscan.cpp b/src/pyvale/dic/cpp/dicrasterscan.cpp new file mode 100644 index 000000000..4d56af6af --- /dev/null +++ b/src/pyvale/dic/cpp/dicrasterscan.cpp @@ -0,0 +1,106 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + + +// STD library Header files +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// common_cpp headers +#include "../../common_cpp/defines.hpp" +#include "../../common_cpp/progressbar.hpp" +#include "../../common_cpp/dicsignalhandler.hpp" + + +// Program Header files +#include "./dicinterp.hpp" +#include "./dicoptimizer.hpp" +#include "./dicutil.hpp" +#include "./dicsubset.hpp" +#include "./dicresults.hpp" +#include "./dicrasterscan.hpp" + + +void raster(const Interpolator &interp_ref, + const Interpolator &interp_def, + const subset::Grid &ss_grid, + const util::Config &conf, + const int img_num_ref, + const int img_num_def, + ResultArrays &result_arrays){ + + const int num_ss = ss_grid.num; + const int ss_size_x = ss_grid.size_x; + const int ss_size_y = ss_grid.size_y; + const int results_num = img_num_def-1; + + // progress bar + std::string bar_title = "Temporal matching for \033[1;4m" + conf.basenames[img_num_ref] + "\033[0m and \033[1;4m" + conf.basenames[img_num_def] + "\033[0m:"; + ProgressBar pbar(bar_title, ss_grid.active_total); + std::atomic current_progress = 0; + int prev_pct = 0; + + // loop over subsets within the ROI + #pragma omp parallel shared(stop_request) + { + + // initialise subsets + subset::Pixels ss_def(ss_size_x, ss_size_y); + subset::Pixels ss_ref(ss_size_x, ss_size_y); + + // optimization parameters + Optimizer opt(conf.shape_func, conf.corr_crit, conf.max_iter, conf.precision, conf.threshold, ss_size_x*ss_size_y); + + + #pragma omp for + for (int ss = 0; ss < num_ss; ss++){ + + // exit the main DIC loop when ctrl+C is hit + if (stop_request) continue; + + // subset coordinate list takes central locations. + // Converting to top left corner for optimization routine + double cx = ss_grid.coords[ss*2]; + double cy = ss_grid.coords[ss*2+1]; + + // get the reference subset + subset::fill_from_centre_coords(ss_ref, cx, cy, interp_ref); + for (int px = 0; px < ss_ref.num_px; px++) { + ss_ref.x[px] -= cx; + ss_ref.y[px] -= cy; + } + + for (int i = 0; i < opt.num_params; i++){ + opt.p[i] = 0.0; + } + + // if the reference subset is empty, then skip the optimization and set results to nan + OptResult res(opt.num_params); + if (ss_ref.sum!=0) res = opt.solve(cx, cy, ss_ref, ss_def, interp_def); + + // append the results for the current subset to result vectors + result_arrays.append(res, ss); + + // update progress bar + if (g_debug_level>0){ + int progress = current_progress.fetch_add(1); + if (omp_get_thread_num()==0) pbar.update(progress); + } + + } + } + if (g_debug_level>0){ + int progress = current_progress; + pbar.finish(); + } +} diff --git a/src/pyvale/dic/cpp/dicrasterscan.hpp b/src/pyvale/dic/cpp/dicrasterscan.hpp new file mode 100644 index 000000000..73da60e6d --- /dev/null +++ b/src/pyvale/dic/cpp/dicrasterscan.hpp @@ -0,0 +1,19 @@ +/** + * @brief straightforward image scan method. + * Loops over the subsets as a raster across the image. + * initial subset locations are distrubuted evenly across the image + * + * @param result_arrays where to populate the results + * @param img_ref pointer to reference image + * @param img_def pointer to deformed image + * @param ss_grid pointer to subset information + * @param conf pointer to DIC config struct + * @param img_num current image number + */ +void raster(const Interpolator &interp_ref, + const Interpolator &interp_def, + const subset::Grid &ss_grid, + const util::Config &conf, + const int img_num_ref, + const int img_num_def, + ResultArrays &result_arrays); diff --git a/src/pyvale/dic/cpp/dicresults.cpp b/src/pyvale/dic/cpp/dicresults.cpp index a57d4a4e4..9cb467543 100644 --- a/src/pyvale/dic/cpp/dicresults.cpp +++ b/src/pyvale/dic/cpp/dicresults.cpp @@ -16,84 +16,175 @@ // DIC Header files #include "./dicresults.hpp" -#include "./dicutil.hpp" -OptResultArrays::OptResultArrays(int num_def_img, int num_ss, int num_params, bool at_end){ +ResultArrays::ResultArrays(int num_ss, + int num_params, + bool stereo){ + + common_util::Timer timer("to resize result arrays:", 2); - if (g_debug_level>0) common_util::Timer timer("resizing of result arrays:"); - this->at_end = at_end; this->num_ss = num_ss; this->num_params = num_params; - - if (at_end){ - niter.resize(num_def_img * num_ss); - u.resize(num_def_img * num_ss); - v.resize(num_def_img * num_ss); - p.resize(num_def_img * num_ss * num_params); - ftol.resize(num_def_img * num_ss); - xtol.resize(num_def_img * num_ss); - cost.resize(num_def_img * num_ss); - conv.resize(num_def_img * num_ss); - above_thresh.resize(num_def_img * num_ss); - } - else { - niter.resize(num_ss); - u.resize(num_ss); - v.resize(num_ss); - p.resize(num_ss * num_params); - ftol.resize(num_ss); - xtol.resize(num_ss); - cost.resize(num_ss); - conv.resize(num_ss); - above_thresh.resize(num_def_img * num_ss); + this->stereo = stereo; + + niter.resize(num_ss, 0.0); + u.resize(num_ss, 0.0); + v.resize(num_ss, 0.0); + p.resize(num_ss * num_params, 0.0); + ftol.resize(num_ss, 0.0); + xtol.resize(num_ss, 0.0); + cost.resize(num_ss, 0.0); + conv.resize(num_ss, 0.0); + above_thresh.resize(num_ss); + + + // incremental tracking + // u_last_good.resize(num_ss, 0.0); + // v_last_good.resize(num_ss, 0.0); + // du_dt.resize(num_ss, 0.0); + // dv_dt.resize(num_ss, 0.0); + // last_success_frame.resize(num_ss, -1); + // has_good_history.resize(num_ss, 0); + + if (stereo){ + x_world.resize(num_ss,0.0); + y_world.resize(num_ss,0.0); + z_world.resize(num_ss,0.0); + u_world.resize(num_ss,0.0); + v_world.resize(num_ss,0.0); + w_world.resize(num_ss,0.0); } } -void OptResultArrays::append(OptResult &res, int results_num, int ss) { - int idx = index(ss, results_num); - - int idx_p = num_params*idx; - niter[idx] = res.iter; - u[idx] = res.u; - v[idx] = res.v; - ftol[idx] = res.ftol; - xtol[idx] = res.xtol; - cost[idx] = res.cost; - conv[idx] = res.converged; - above_thresh[idx] = res.above_threshold; +void ResultArrays::append(OptResult &res, const int i) { + + int pp = num_params*i; + niter[i] = res.iter; + u[i] = res.u; + v[i] = res.v; + ftol[i] = res.ftol; + xtol[i] = res.xtol; + cost[i] = res.cost; + conv[i] = res.converged; + above_thresh[i] = res.above_thresh; for (size_t i = 0; i < num_params; i++){ - p[idx_p+i] = res.p[i]; + p[pp+i] = res.p[i]; } } -int OptResultArrays::index(const int subset_idx, const int results_num){ - int idx = at_end ? (results_num) * num_ss + subset_idx : subset_idx; - return idx; -} - -int OptResultArrays::index_parameters(const int subset_idx, const int results_num){ - int idx = index(subset_idx, results_num) * num_params; - return idx; -} - - -void OptResultArrays::write_to_disk(int img_num, const common_util::SaveConfig &saveconf, - const subset::Grid &ss_grid, const int num_def_img, - const std::vector &filenames){ + void ResultArrays::reset() { + std::fill(niter.begin(), niter.end(), 0); + std::fill(u.begin(), u.end(), 0.0); + std::fill(v.begin(), v.end(), 0.0); + std::fill(p.begin(), p.end(), 0.0); + std::fill(ftol.begin(), ftol.end(), 0.0); + std::fill(xtol.begin(), xtol.end(), 0.0); + std::fill(cost.begin(), cost.end(), 0.0); + std::fill(conv.begin(), conv.end(), false); + std::fill(above_thresh.begin(), above_thresh.end(), false); + + if (stereo) { + std::fill(x_world.begin(), x_world.end(), 0.0); + std::fill(y_world.begin(), y_world.end(), 0.0); + std::fill(z_world.begin(), z_world.end(), 0.0); + std::fill(u_world.begin(), u_world.end(), 0.0); + std::fill(v_world.begin(), v_world.end(), 0.0); + std::fill(w_world.begin(), w_world.end(), 0.0); + } + } + +// void ResultArrays::get_latest_matches(const ResultArrays &results_def, const int img_num_def) { +// +// // Naive check that that results_ref and results_def are the same size +// if (u.size() != results_def.u.size()){ +// niter.resize(num_ss, 0.0); +// u.resize(num_ss, 0.0); +// v.resize(num_ss, 0.0); +// p.resize(num_ss * num_params, 0.0); +// ftol.resize(num_ss, 0.0); +// xtol.resize(num_ss, 0.0); +// cost.resize(num_ss, 0.0); +// conv.resize(num_ss, 0.0); +// above_thresh.resize(num_ss); +// +// +// // incremental tracking +// u_last_good.resize(num_ss, 0.0); +// v_last_good.resize(num_ss, 0.0); +// du_dt.resize(num_ss, 0.0); +// dv_dt.resize(num_ss, 0.0); +// last_success_frame.resize(num_ss, -1); +// has_good_history.resize(num_ss, 0); +// +// if (stereo){ +// x_world.resize(num_ss,0.0); +// y_world.resize(num_ss,0.0); +// z_world.resize(num_ss,0.0); +// u_world.resize(num_ss,0.0); +// v_world.resize(num_ss,0.0); +// w_world.resize(num_ss,0.0); +// } +// } +// +// for (int i = 0; i < u.size(); i++){ +// +// if (results_def.above_thresh[i]){ +// +// if (has_good_history[i]){ +// +// int dt = img_num_def - last_success_frame[i]; +// +// if (dt > 0){ +// du_dt[i] = (results_def.u[i] - u_last_good[i]) / dt; +// dv_dt[i] = (results_def.v[i] - v_last_good[i]) / dt; +// } +// } +// +// // update last good state +// u_last_good[i] = results_def.u[i]; +// v_last_good[i] = results_def.v[i]; +// last_success_frame[i] = img_num_def; +// has_good_history[i] = 1; +// u[i] = results_def.u[i]; +// v[i] = results_def.v[i]; +// p[i] = results_def.p[i]; +// above_thresh[i] = 1; +// } +// } +// } +// + + +// int ResultArrays::index(const int subset_idx, const int results_num){ +// int idx = at_end ? (results_num) * num_ss + subset_idx : subset_idx; +// return idx; +// } +// +// int ResultArrays::index_parameters(const int subset_idx, const int results_num){ +// int idx = index(subset_idx, results_num) * num_params; +// return idx; +// } + + +void ResultArrays::write_to_disk_2d(const common_util::SaveConfig &saveconf, + const subset::Grid &ss_grid, + const std::string &filename){ + + + common_util::Timer timer("to write DIC results to disk:", 2); const std::string delimiter = saveconf.delimiter; std::stringstream outfile_str; std::ofstream outfile; - int results_num = img_num-1; std::string file_ext; if (saveconf.binary) file_ext=".dic2d"; else file_ext=".csv"; - std::string full_filename = filenames[img_num]; + std::string full_filename = filename; size_t dot_pos = full_filename.find("."); if (dot_pos != std::string::npos) { full_filename = full_filename.substr(0, dot_pos); @@ -104,8 +195,10 @@ void OptResultArrays::write_to_disk(int img_num, const common_util::SaveConfig & << full_filename << file_ext; + outfile << std::fixed << std::setprecision(8); + // set the img var to 0 after opening file if not saving at end - if (!saveconf.at_end) results_num = 0; + //if (!saveconf.at_end) results_num = 0; // save in binary format if (saveconf.binary){ @@ -113,43 +206,39 @@ void OptResultArrays::write_to_disk(int img_num, const common_util::SaveConfig & for (int i = 0; i < ss_grid.num; ++i) { - int idx = index(i, results_num); - //int idx_p = num_params*idx; - // if the subset has not met threshold, set values to nan - if (!saveconf.output_below_threshold && !above_thresh[idx]) { - u[idx] = NAN; - v[idx] = NAN; - for (int pidx = 0; pidx < num_params; pidx++){ - p[num_params*idx+pidx] = NAN; + if (!saveconf.output_below_threshold && !above_thresh[i]) { + u[i] = NAN; + v[i] = NAN; + for (int pp = 0; pp < num_params; pp++){ + p[num_params*i+pp] = NAN; } - cost[idx] = NAN; - ftol[idx] = NAN; - xtol[idx] = NAN; + cost[i] = NAN; + ftol[i] = NAN; + xtol[i] = NAN; } - - double mag = std::sqrt(u[idx]*u[idx]+ - v[idx]*v[idx]); + // displacement magnitude + double mag = std::sqrt(u[i]*u[i]+v[i]*v[i]); // convert from corner to centre subset coords - double ss_x = ss_grid.coords[2*i ] + static_cast(ss_grid.size)/2.0 - 0.5; - double ss_y = ss_grid.coords[2*i+1] + static_cast(ss_grid.size)/2.0 - 0.5; + double ss_x = ss_grid.coords[2*i ]; + double ss_y = ss_grid.coords[2*i+1]; common_util::write_int(outfile, ss_x); common_util::write_int(outfile, ss_y); - common_util::write_dbl(outfile, u[idx]); - common_util::write_dbl(outfile, v[idx]); + common_util::write_dbl(outfile, u[i]); + common_util::write_dbl(outfile, v[i]); common_util::write_dbl(outfile, mag); - common_util::write_uint8t(outfile, conv[idx]); - common_util::write_dbl(outfile, cost[idx]); - common_util::write_dbl(outfile, ftol[idx]); - common_util::write_dbl(outfile, xtol[idx]); - common_util::write_int(outfile, niter[idx]); + common_util::write_uint8t(outfile, conv[i]); + common_util::write_dbl(outfile, cost[i]); + common_util::write_dbl(outfile, ftol[i]); + common_util::write_dbl(outfile, xtol[i]); + common_util::write_int(outfile, niter[i]); if (saveconf.shape_params) { - for (int pidx = 0; pidx < num_params; pidx++){ - common_util::write_dbl(outfile, p[num_params*idx+pidx]); + for (int pp = 0; pp < num_params; pp++){ + common_util::write_dbl(outfile, p[num_params*i+pp]); } } @@ -162,70 +251,313 @@ void OptResultArrays::write_to_disk(int img_num, const common_util::SaveConfig & outfile.open(outfile_str.str()); // column headers - outfile << "subset_x" << delimiter; - outfile << "subset_y" << delimiter; - outfile << "displacement_u" << delimiter; - outfile << "displacement_v" << delimiter; - outfile << "displacement_mag" << delimiter; - outfile << "converged" << delimiter; - outfile << "cost" << delimiter; - outfile << "ftol" << delimiter; - outfile << "xtol" << delimiter; - outfile << "num_iterations"; + outfile << "\"subset_x\"" << delimiter; + outfile << "\"subset_y\"" << delimiter; + outfile << "\"disp_u\"" << delimiter; + outfile << "\"disp_v\"" << delimiter; + outfile << "\"disp_mag\"" << delimiter; + outfile << "\"converged\"" << delimiter; + outfile << "\"cost_zncc\"" << delimiter; + outfile << "\"ftol\"" << delimiter; + outfile << "\"xtol\"" << delimiter; + outfile << "\"num_iter\"" << delimiter; // column headers for shape parameters - if (saveconf.shape_params) { - for (int p = 0; p < num_params; p++){ - outfile << delimiter; - outfile << "shape_p" << p; - } - } + // if (saveconf.shape_params) { + // for (int p = 0; p < num_params; p++){ + // outfile << "\"shape_p\"" << p; + // outfile << delimiter; + // } + // } // newline after headers outfile << "\n"; for (int i = 0; i < ss_grid.num; i++) { - int idx = index(i, results_num); - //int idx_p = num_params*idx; - // convert from corner to centre subset coords - double ss_x = ss_grid.coords[2*i ] + static_cast(ss_grid.size)/2.0 - 0.5; - double ss_y = ss_grid.coords[2*i+1] + static_cast(ss_grid.size)/2.0 - 0.5; + double ss_x = ss_grid.coords[2*i ]; + double ss_y = ss_grid.coords[2*i+1]; // if the subset has not met threshold, set values to nan - if (!saveconf.output_below_threshold && !above_thresh[idx]) { - u[idx] = NAN; - v[idx] = NAN; - for (int pidx = 0; pidx < num_params; pidx++){ - p[num_params*idx+pidx] = NAN; + if (!saveconf.output_below_threshold && !above_thresh[i]) { + u[i] = NAN; + v[i] = NAN; + for (int pi = 0; pi < num_params; pi++){ + p[num_params*i+pi] = NAN; } - cost[idx] = NAN; - ftol[idx] = NAN; - xtol[idx] = NAN; + cost[i] = NAN; + ftol[i] = NAN; + xtol[i] = NAN; } + // displacement magnitude + double mag = std::sqrt(u[i]*u[i]+v[i]*v[i]); outfile << ss_x << delimiter; outfile << ss_y << delimiter; - outfile << u[idx] << delimiter; - outfile << v[idx] << delimiter; - outfile << sqrt(u[idx]*u[idx]+ - v[idx]*v[idx]) << delimiter; - outfile << static_cast(conv[idx]) << delimiter; - outfile << cost[idx] << delimiter; - outfile << ftol[idx] << delimiter; - outfile << xtol[idx] << delimiter; - outfile << niter[idx]; + outfile << u[i] << delimiter; + outfile << v[i] << delimiter; + outfile << mag << delimiter; + outfile << static_cast(conv[i]) << delimiter; + outfile << cost[i] << delimiter; + outfile << ftol[i] << delimiter; + outfile << xtol[i] << delimiter; + outfile << niter[i]; // write shape parameters if requested - if (saveconf.shape_params) { - for (int pidx = 0; pidx < num_params; pidx++){ - outfile << delimiter; - outfile << p[num_params*idx+pidx]; + // if (saveconf.shape_params) { + // for (int pp = 0; pp < num_params; pp++){ + // outfile << delimiter; + // outfile << p[num_params*i+pp]; + // } + // } + + // newline after each subset + outfile << "\n"; + + + } + outfile.close(); + } +} + + + +void ResultArrays::write_to_disk_stereo(ResultArrays &stereo, + const common_util::SaveConfig &saveconf, + const subset::Grid &ss_grid, + const std::string &filename){ + + const std::string delimiter = saveconf.delimiter; + + std::stringstream outfile_str; + std::ofstream outfile; + + std::string file_ext; + if (saveconf.binary) file_ext=".dic3d"; + else file_ext=".csv"; + + std::string full_filename = filename; + size_t dot_pos = full_filename.find("."); + if (dot_pos != std::string::npos) { + full_filename = full_filename.substr(0, dot_pos); + } + + outfile_str << saveconf.basepath << "/" + << saveconf.prefix + << full_filename + << file_ext; + + outfile << std::fixed << std::setprecision(8); + + // set the img var to 0 after opening file if not saving at end + //if (!saveconf.at_end) results_num = 0; + + // save in binary format + if (saveconf.binary){ + outfile.open(outfile_str.str(), std::ios::binary); + + for (int i = 0; i < ss_grid.num; ++i) { + + // if the subset has not met threshold, set values to nan + if (!saveconf.output_below_threshold && !above_thresh[i]) { + + u[i] = NAN; + v[i] = NAN; + stereo.u[i] = NAN; + stereo.v[i] = NAN; + + for (int pp = 0; pp < num_params; pp++){ + p[num_params*i+pp] = NAN; + stereo.p[ stereo.num_params*i+pp] = NAN; } + + cost[i] = NAN; + ftol[i] = NAN; + xtol[i] = NAN; + } + + if (!saveconf.output_below_threshold && !stereo.above_thresh[i]) { + stereo.cost[i] = NAN; + stereo.ftol[i] = NAN; + stereo.xtol[i] = NAN; + stereo.x_world[i] = NAN; + stereo.y_world[i] = NAN; + stereo.z_world[i] = NAN; + stereo.u_world[i] = NAN; + stereo.v_world[i] = NAN; + stereo.w_world[i] = NAN; } + + // convert from corner to centre subset coords + double ss_x = ss_grid.coords[2*i ]; + double ss_y = ss_grid.coords[2*i+1]; + + // displacement magnitude + double mag_temporal = std::sqrt(u[i]*u[i]+v[i]*v[i]); + double mag_stereo = std::sqrt(stereo.u[i]*stereo.u[i]+stereo.v[i]*stereo.v[i]); + + common_util::write_int(outfile, ss_x); + common_util::write_int(outfile, ss_y); + common_util::write_dbl(outfile, u[i]); + common_util::write_dbl(outfile, v[i]); + common_util::write_dbl(outfile, mag_temporal); + common_util::write_uint8t(outfile, conv[i]); + common_util::write_dbl(outfile, cost[i]); + common_util::write_dbl(outfile, ftol[i]); + common_util::write_dbl(outfile, xtol[i]); + common_util::write_int(outfile, niter[i]); + common_util::write_dbl(outfile, stereo.u[i]); + common_util::write_dbl(outfile, stereo.v[i]); + common_util::write_dbl(outfile, mag_stereo); + common_util::write_dbl(outfile, stereo.u_world[i]); + common_util::write_dbl(outfile, stereo.v_world[i]); + common_util::write_dbl(outfile, stereo.w_world[i]); + common_util::write_dbl(outfile, stereo.x_world[i]); + common_util::write_dbl(outfile, stereo.y_world[i]); + common_util::write_dbl(outfile, stereo.z_world[i]); + common_util::write_uint8t(outfile, stereo.conv[i]); + common_util::write_dbl(outfile, stereo.cost[i]); + common_util::write_dbl(outfile, stereo.ftol[i]); + common_util::write_dbl(outfile, stereo.xtol[i]); + common_util::write_int(outfile, stereo.niter[i]); + + // if (saveconf.shape_params) { + // for (int pp = 0; pp < num_params; pp++){ + // common_util::write_dbl(outfile, p[num_params*i+pp]); + // common_util::write_dbl(outfile, stereo.p[stereo.num_params*i+pp]); + // } + // } + + } + + outfile.close(); + } + else { + + outfile.open(outfile_str.str()); + + // column headers + outfile << "\"subset_x\"" << delimiter; + outfile << "\"subset_y\"" << delimiter; + outfile << "\"disp_u\"" << delimiter; + outfile << "\"disp_v\"" << delimiter; + outfile << "\"disp_mag\"" << delimiter; + outfile << "\"converged\"" << delimiter; + outfile << "\"cost_zncc\"" << delimiter; + outfile << "\"ftol\"" << delimiter; + outfile << "\"xtol\"" << delimiter; + outfile << "\"num_iter\"" << delimiter; + outfile << "\"stereo_disp_u_px\"" << delimiter; + outfile << "\"stereo_disp_v_px\"" << delimiter; + outfile << "\"stereo_disp_mag_px\"" << delimiter; + outfile << "\"stereo_disp_u_mm\"" << delimiter; + outfile << "\"stereo_disp_v_mm\"" << delimiter; + outfile << "\"stereo_disp_w_mm\"" << delimiter; + outfile << "\"stereo_x_mm\"" << delimiter; + outfile << "\"stereo_y_mm\"" << delimiter; + outfile << "\"stereo_z_mm\"" << delimiter; + outfile << "\"stereo_converged\"" << delimiter; + outfile << "\"stereo_cost_zncc\"" << delimiter; + outfile << "\"stereo_ftol\"" << delimiter; + outfile << "\"stereo_xtol\"" << delimiter; + outfile << "\"stereo_num_iter\"" << delimiter; + // column headers for shape parameters + // if (saveconf.shape_params) { + // for (int p = 0; p < num_params; p++){ + // outfile << "\"shape_p\"" << p; + // outfile << delimiter; + // } + // } + + // column headers for shape parameters + // if (saveconf.shape_params) { + // for (int p = 0; p < num_params; p++){ + // outfile << "\"stereo_shape_p\"" << p; + // outfile << delimiter; + // } + // } + + // newline after headers + outfile << "\n"; + + for (int i = 0; i < ss_grid.num; i++) { + + // convert from corner to centre subset coords + double ss_x = ss_grid.coords[2*i ]; + double ss_y = ss_grid.coords[2*i+1]; + + // if the subset has not met threshold, set values to nan + // if the subset has not met threshold, set values to nan + if (!saveconf.output_below_threshold && !above_thresh[i]) { + + u[i] = NAN; + v[i] = NAN; + stereo.u[i] = NAN; + stereo.v[i] = NAN; + + for (int pp = 0; pp < num_params; pp++){ + p[num_params*i+pp] = NAN; + stereo.p[ stereo.num_params*i+pp] = NAN; + } + + cost[i] = NAN; + ftol[i] = NAN; + xtol[i] = NAN; + } + + if (!saveconf.output_below_threshold && !stereo.above_thresh[i]) { + stereo.cost[i] = NAN; + stereo.ftol[i] = NAN; + stereo.xtol[i] = NAN; + stereo.x_world[i] = NAN; + stereo.y_world[i] = NAN; + stereo.z_world[i] = NAN; + stereo.u_world[i] = NAN; + stereo.v_world[i] = NAN; + stereo.w_world[i] = NAN; + } + + // displacement magnitude + double mag_= std::sqrt(u[i]*u[i]+v[i]*v[i]); + double mag_stereo = std::sqrt(stereo.u[i]*stereo.u[i]+stereo.v[i]*stereo.v[i]); + + + outfile << ss_x << delimiter; + outfile << ss_y << delimiter; + outfile << u[i] << delimiter; + outfile << v[i] << delimiter; + outfile << mag_<< delimiter; + outfile << static_cast(conv[i]) << delimiter; + outfile << cost[i] << delimiter; + outfile << ftol[i] << delimiter; + outfile << xtol[i] << delimiter; + outfile << niter[i] << delimiter; + outfile << stereo.u[i] << delimiter; + outfile << stereo.v[i] << delimiter; + outfile << mag_stereo << delimiter; + outfile << stereo.u_world[i] << delimiter; + outfile << stereo.v_world[i] << delimiter; + outfile << stereo.w_world[i] << delimiter; + outfile << stereo.x_world[i] << delimiter; + outfile << stereo.y_world[i] << delimiter; + outfile << stereo.z_world[i] << delimiter; + outfile << static_cast(stereo.conv[i]) << delimiter; + outfile << stereo.cost[i] << delimiter; + outfile << stereo.ftol[i] << delimiter; + outfile << stereo.xtol[i] << delimiter; + outfile << stereo.niter[i]; + + // write shape parameters if requested + // if (saveconf.shape_params) { + // for (int pp = 0; pp < num_params; pp++){ + // outfile << delimiter; + // outfile << p[num_params*i+pp]; + // } + // } + // newline after each subset outfile << "\n"; diff --git a/src/pyvale/dic/cpp/dicresults.hpp b/src/pyvale/dic/cpp/dicresults.hpp index 79cb00ab5..98909b5c4 100644 --- a/src/pyvale/dic/cpp/dicresults.hpp +++ b/src/pyvale/dic/cpp/dicresults.hpp @@ -16,30 +16,20 @@ // DIC Header files #include "./dicsubset.hpp" - -struct OptResult { - std::vector p; - double u = 0.0; - double v = 0.0; - double mag = 0.0; - double ftol = 0.0; - double xtol = 0.0; - int iter = 0; - double cost = 0.0; - uint8_t converged = false; - uint8_t above_threshold = false; - OptResult(size_t num_params) : p(num_params, 0.0) {} -}; +#include "./dicoptimizer.hpp" -class OptResultArrays { +class ResultArrays { private: + + + public: + int num_ss; int num_params; - bool at_end; + bool stereo; - public: // result arrays. std::vector niter; std::vector u; @@ -51,14 +41,50 @@ class OptResultArrays { std::vector conv; std::vector above_thresh; - OptResultArrays(int num_def_img, int num_ss, int num_params, bool conf_at_end); - void append(OptResult &res, int img_num, int ss); - int index(const int subset_idx, const int img_num); - int index_parameters(const int subset_idx, const int img_num); - void write_to_disk(int img, const common_util::SaveConfig &saveconf, - const subset::Grid &ss_grid, const int num_def_img, - const std::vector &filenames); + // incremental tracking; + // std::vector u_last_good; + // std::vector v_last_good; + // std::vector du_dt; + // std::vector dv_dt; + // std::vector last_success_frame; + // std::vector has_good_history; + + // world coordinates + std::vector x_world; + std::vector y_world; + std::vector z_world; + std::vector u_world; + std::vector v_world; + std::vector w_world; + + // constructors + ResultArrays() = default; + ResultArrays(int num_ss, + int num_params, + bool stereo); + + + void append(OptResult &res, const int ss); + + + void reset(); + + + //void get_latest_matches(const ResultArrays &results_def, const int img_num_def); + + void write_to_disk_2d(const common_util::SaveConfig &saveconf, + const subset::Grid &ss_grid, + const std::string &filename); + + + void write_to_disk_stereo(ResultArrays &stereo, + const common_util::SaveConfig &saveconf, + const subset::Grid &ss_grid, + const std::string &filename); + + }; + #endif // DICRESULTS_H diff --git a/src/pyvale/dic/cpp/dicrg.cpp b/src/pyvale/dic/cpp/dicrg.cpp index a7880bbb6..92f0966b0 100644 --- a/src/pyvale/dic/cpp/dicrg.cpp +++ b/src/pyvale/dic/cpp/dicrg.cpp @@ -10,15 +10,101 @@ #include #include #include - - +#include +#include // Program Header files #include "./dicsubset.hpp" +#include "./dicfourier.hpp" +#include "./dicoptimizer.hpp" +#include "./dicresults.hpp" +#include "./dicrg.hpp" +#include "../../common_cpp/dicsignalhandler.hpp" namespace rg { + bool QueueLocal::try_pop_own_q(const int tid, rg::Point& out) { + std::lock_guard lock(locks[tid]); + if (qs[tid].empty()) return false; + out = qs[tid].top(); + qs[tid].pop(); + return true; + } + + bool QueueLocal::try_steal_from_other_q(const int tid, rg::Point& out) { + std::lock_guard steal_guard(steal_lock); + for (size_t i = 0; i < qs.size(); ++i) { + if (i == tid) continue; + std::lock_guard lock(locks[i]); + if (!qs[i].empty()) { + out = qs[i].top(); + qs[i].pop(); + return true; + } + } + return false; + } + + bool QueueLocal::pop(const int tid, rg::Point& out) { + if (try_pop_own_q(tid, out)) + return true; + const int max_idle_iters = 100; + for (int idle = 0; idle < max_idle_iters; ++idle) { + if (try_steal_from_other_q(tid, out)) + return true; + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + return false; + } + + void QueueLocal::push(const int tid, const std::vector& points) { + for (const auto& neigh : points) { + std::lock_guard lock(locks[tid]); + qs[tid].push(neigh); + } + } + + bool QueueGlobal::pop(const int tid, rg::Point& current) { + bool found = false; + { + std::lock_guard lock(m); + if (!q.empty()) { + current = q.top(); + q.pop(); + found = true; + } + } + if (found) return true; + + active_threads.fetch_sub(1); + while (active_threads.load() > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + { + std::lock_guard lock(m); + if (!q.empty()) { + current = q.top(); + q.pop(); + found = true; + break; + } + } + } + if (found) { + active_threads.fetch_add(1); + return true; + } + return false; + } + + void QueueGlobal::push(const int tid, const std::vector& points) { // fixed: proper method + if (points.empty()) return; + std::lock_guard lock(m); // fixed: m + for (const auto& pt : points) { + q.push(pt); + } + } + bool is_valid_point(const int ss_x, const int ss_y, const subset::Grid &ss_grid) { int x = ss_x / ss_grid.step; @@ -39,16 +125,29 @@ namespace rg { exit(EXIT_FAILURE); } else return true; + } - //auto it = ss_grid.coords_to_idx.find({ss_x, ss_y}); - //// check if coordinates are in the coordinate list - //if (it == ss_grid.coords_to_idx.end()) { - // std::cerr << "Error: coordinates not found in the coordinate list." << std::endl; - // std::cerr << "Coordinates: " << ss_x << ", " << ss_y << std::endl; - // exit(EXIT_FAILURE); - //} - //else return true; + bool check_convergence(const int x, const int y, const OptResult &res, std::string &msg, bool direct_neigh) { + if (!res.above_thresh) { + std::ostringstream oss; + + oss << (direct_neigh + ? "Direct neighbour failed threshold" + : "Seed subset failed threshold") + << "\n" + << "subset location: " << x << ", " << y << "\n" + << "displacement: " << res.u << ", " << res.v << "\n" + << "cost: " << res.cost << "\n" + << "xtol: " << res.xtol << "\n" + << "ftol: " << res.ftol << "\n" + << "above_thresh: " << static_cast(res.above_thresh) << "\n" + << "converged: " << static_cast(res.converged) << "\n" + << "iterations: " << res.iter; + msg = oss.str(); + return false; + } + return true; } } diff --git a/src/pyvale/dic/cpp/dicrg.hpp b/src/pyvale/dic/cpp/dicrg.hpp index 87b0ffa33..e8bf64031 100644 --- a/src/pyvale/dic/cpp/dicrg.hpp +++ b/src/pyvale/dic/cpp/dicrg.hpp @@ -9,9 +9,13 @@ #define DICRG_H // STD library Header files +#include +#include +#include // Program Header files #include "./dicsubset.hpp" +#include "./dicoptimizer.hpp" namespace rg { @@ -33,6 +37,112 @@ namespace rg { } }; + + struct QueuePolicy { + virtual bool pop(const int tid, rg::Point& out) = 0; + virtual void push(const int tid, const std::vector& points) = 0; + virtual ~QueuePolicy() = default; + }; + + struct QueueGlobal : QueuePolicy { + std::priority_queue q; + std::mutex m; + std::atomic active_threads; + + + explicit QueueGlobal(const int num_threads) { + active_threads.store(num_threads); + } + + /** + * @brief Retrieves the next point from a global priority queue. + * + * Coordinates multiple threads to pop the highest-priority point. If the queue is + * empty, threads will wait as long as other threads are still active (processing + * points that might add new neighbours to the queue). + * + * @param tid Thread ID. + * @param out Populated with the next point if found. + * @return True if a point was retrieved, false if the queue is + * empty and all threads are idle. + */ + bool pop(const int tid, rg::Point& out); + + + /** + * @brief Pushes multiple points to the global priority queue in a thread-safe manner. + * + * @param tid Thread ID. + * @param points Vector of points to add. + */ + void push(const int tid, const std::vector& points); + + }; + + struct QueueLocal : QueuePolicy { + std::vector> qs; + std::vector locks; + std::mutex steal_lock; + + explicit QueueLocal(const int num_threads) + : qs(num_threads), locks(num_threads) {} + + /** + * @brief Retrieves the next point for a thread to process, blocking briefly if queues are empty. + * + * First tries the calling thread's own queue via try_pop_own_q(). If empty, + * repeatedly attempts to steal from other threads via try_steal_from_other_q() + * up to a fixed number of idle iterations, sleeping 1ms between attempts. + * + * @param tid Thread ID. + * @param current Populated with the next point to process if one is found. + * @return True if a point was retrieved, false if all queues remained + * empty after exhausting idle iterations. + */ + bool pop(const int tid, rg::Point& current); + + + /** + * @brief Pushes multiple points to a local threads priority queue in a thread-safe manner. + * + * @param tid Thread ID. + * @param points neighbours to add. + */ + void push(const int tid, const std::vector &points); + + private: + + /** + * @brief Attempts to steal the highest-priority point from any other thread's queue. + * + * Iterates over all queues and pops from the first non-empty one found. + * The global @p steal_mtx is held for the duration to prevent two threads + * from stealing simultaneously. + * + * @param tid Thread ID. + * @param out Populated with the stolen point if successful. + * @return True if a point was stolen, false if all queues were empty. + */ + bool try_steal_from_other_q(const int tid, rg::Point& out); + + + /** + * @brief Attempts to pop the highest-priority point from a thread's own queue. + * + * @param tid Thread ID. + * @param out Populated with the popped point if successful. + * @return True if a point was popped, false if the queue was empty. + */ + bool try_pop_own_q(const int tid, rg::Point& out); + }; + + + + + + + + /** * @brief * @@ -46,6 +156,10 @@ namespace rg { */ bool is_valid_point(const int ss_x, const int ss_y, const subset::Grid &ss_grid); + + + bool check_convergence(const int x, const int y, const OptResult &res, std::string &msg, bool direct_neigh=false); + } diff --git a/src/pyvale/dic/cpp/dicroiupdate.cpp b/src/pyvale/dic/cpp/dicroiupdate.cpp new file mode 100644 index 000000000..6c25c82d2 --- /dev/null +++ b/src/pyvale/dic/cpp/dicroiupdate.cpp @@ -0,0 +1,84 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + +// opencv header files +#include +//#include + +// std lib header files +#include + +// program header files +#include "./dicroiupdate.hpp" + + +bool* propagate_roi( + const bool* img_roi, + const ResultArrays results_def, + const util::Config conf, + const subset::Grid ss_grid, + const bool debug){ + + + const int half = conf.ss_size / 2; + + std::vector roi_vec(conf.px_hori * conf.px_vert, false); + + for (int i = 0; i < results_def.conv.size(); i++) + { + if (!results_def.conv[i] || !results_def.above_thresh[i]) + continue; + + int new_cx = static_cast(std::round(ss_grid.coords[2*i + 0] + results_def.u[i])); + int new_cy = static_cast(std::round(ss_grid.coords[2*i + 1] + results_def.v[i])); + + for (int dy = -half; dy <= half; dy++) + { + for (int dx = -half; dx <= half; dx++) + { + int px = new_cx + dx; + int py = new_cy + dy; + + if (px < 0 || px >= conf.px_hori || py < 0 || py >= conf.px_vert) + continue; + + roi_vec[py*conf.px_hori+px] = true; + } + } + } + + // bool do_morph = false; + // if (do_morph) + // { + // cv::Mat kernel = cv::getStructuringElement( + // cv::MORPH_ELLIPSE, + // cv::Size(conf.ss_step, conf.ss_step)); + // + // cv::morphologyEx(roi_mat, roi_mat, cv::MORPH_CLOSE, kernel); + // } + + bool* roi_out = new bool[conf.px_hori * conf.px_vert]; + + // Copy vector → raw array + for (int i = 0; i < conf.px_hori * conf.px_vert; i++) + { + roi_out[i] = roi_vec[i]; + } + + if (debug) + { + for (int y = 0; y < conf.px_vert; y++) + { + for (int x = 0; x < conf.px_hori; x++) + { + std::cout << x << " " << y << " " + << roi_out[y * conf.px_hori + x] << std::endl; + } + } + } + + return roi_out; +} diff --git a/src/pyvale/dic/cpp/dicroiupdate.hpp b/src/pyvale/dic/cpp/dicroiupdate.hpp new file mode 100644 index 000000000..53ec3c837 --- /dev/null +++ b/src/pyvale/dic/cpp/dicroiupdate.hpp @@ -0,0 +1,24 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + +#ifndef DICROIUPDATE_H +#define DICROIUPDATE_H + +#include "dicresults.hpp" +#include "dicutil.hpp" +#include + + + + +bool* propagate_roi( + const bool* img_roi, + const ResultArrays results_def, + const util::Config conf, + const subset::Grid ss_grid, + const bool debug=false); + +#endif // DICROIUPDATE_H diff --git a/src/pyvale/dic/cpp/dicscanmethod.cpp b/src/pyvale/dic/cpp/dicscanmethod.cpp deleted file mode 100644 index bc3b1e758..000000000 --- a/src/pyvale/dic/cpp/dicscanmethod.cpp +++ /dev/null @@ -1,874 +0,0 @@ -// ================================================================================ -// pyvale: the python validation engine -// License: MIT -// Copyright (C) 2025 The Computer Aided Validation Team -// ================================================================================ - - -// STD library Header files -#include "dicscanmethod.hpp" -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// pybind headers -#include - -// common_cpp headers -#include "../../common_cpp/defines.hpp" -#include "../../common_cpp/progressbar.hpp" -#include "../../common_cpp/dicsignalhandler.hpp" - - -// Program Header files -#include "./dicinterp.hpp" -#include "./dicoptimizer.hpp" -#include "./dicutil.hpp" -#include "./dicrg.hpp" -#include "./dicfourier.hpp" -#include "./dicsubset.hpp" -#include "./dicresults.hpp" - -namespace scanmethod { - - - void image(const double *img_ref, - const Interpolator &interp_def, - const subset::Grid &ss_grid, - const util::Config &conf, - const int img_num, - OptResultArrays &result_arrays){ - - const int num_ss = ss_grid.num; - const int ss_size = ss_grid.size; - const int results_num = img_num-1; - - // progress bar - std::string bar_title = "Correlation for " + conf.filenames[img_num] + ":"; - ProgressBar pbar(bar_title, num_ss); - std::atomic current_progress = 0; - int prev_pct = 0; - - // loop over subsets within the ROI - #pragma omp parallel shared(stop_request) - { - - // initialise subsets - subset::Pixels ss_def(ss_size); - subset::Pixels ss_ref(ss_size); - - // optimization parameters - optimizer::Parameters opt(conf.num_params, conf.max_iter, - conf.precision, conf.threshold, - conf.px_vert, conf.px_hori, - conf.corr_crit); - - #pragma omp for - for (int ss = 0; ss < num_ss; ss++){ - - // exit the main DIC loop when ctrl+C is hit - if (stop_request) continue; - - // subset coordinate list takes central locations. - // Converting to top left corner for optimization routine - int ss_x = ss_grid.coords[ss*2]; - int ss_y = ss_grid.coords[ss*2+1]; - - // get the reference subset - subset::get_px_from_img(ss_ref, ss_x, ss_y, conf.px_hori, conf.px_vert, img_ref); - - for (int i = 0; i < opt.num_params; i++){ - opt.p[i] = 0.0; - } - - // perform optimization on subset from deformed image - double centre_x = ss_x + static_cast(ss_grid.size)/2.0 - 0.5; - double centre_y = ss_y + static_cast(ss_grid.size)/2.0 - 0.5; - OptResult res = optimizer::solve(centre_x, centre_y, ss_ref, ss_def, interp_def, opt, conf.corr_crit); - - // append the results for the current subset to result vectors - result_arrays.append(res, results_num, ss); - - // update progress bar - int progress = current_progress.fetch_add(1); - if (omp_get_thread_num()==0) pbar.update(progress); - - } - } - int progress = current_progress; - pbar.finish(); - } - - void multiwindow_reliability_guided(const double *img_ref, - const double *img_def, - const Interpolator &interp_def, - const std::vector &ss_grid, - const util::Config &conf, - const int img_num, - OptResultArrays &result_arrays){ - - // assign some consts for readability - const int px_hori = conf.px_hori; - const int px_vert = conf.px_vert; - const int seed_x = conf.rg_seed.first; - const int seed_y = conf.rg_seed.second; - const int nsizes = ss_grid.size(); - const int last_size = nsizes-1; - const int num_ss = ss_grid[last_size].num; - const int ss_size = ss_grid[last_size].size; - const int ss_step = ss_grid[last_size].step; - const int results_num = img_num-1; - - fourier::multiwindow(ss_grid, img_ref, img_def, interp_def, conf.fft_mad, conf.fft_mad_scale); - - // progress bar - std::string bar_title = "Correlation for " + conf.filenames[img_num] + ":"; - ProgressBar pbar(bar_title, num_ss); - std::atomic current_progress(0); - - // quick check for the initial seed point - if (!rg::is_valid_point(seed_x, seed_y, ss_grid[last_size])) { - return; - } - - // Initialize binary mask for computed points (initialized to 0) - std::vector> computed_mask(ss_grid[last_size].mask.size()); - for (auto& val : computed_mask) val.store(0); - - // queue for each thread - std::vector> local_q(omp_get_max_threads()); - - // Mutex vector to protect each queue - std::vector queue_mutexes(omp_get_max_threads()); - std::mutex steal_mutex; - - # pragma omp parallel - { - - int tid = omp_get_thread_num(); - std::priority_queue& thread_q = local_q[tid]; - - // Initialize ref and def subsets - subset::Pixels ss_def(ss_size); - subset::Pixels ss_ref(ss_size); - - // Optimization parameters - optimizer::Parameters opt(conf.num_params, conf.max_iter, - conf.precision, conf.threshold, - px_vert, px_hori, - conf.corr_crit); - - std::vector> fft_windows; - - for (size_t t = 0; t < ss_grid.size(); ++t) { - fft_windows.push_back(std::make_unique(ss_grid[t].size)); - } - - // TODO: for the seed location I'm going to overwride the max - // number of iterations to make sure we get a good convergence. - // this is hardcoded for now. Could do with updating so that - // the seed location is checked ahead of the main correlation run. - - // TODO: opt.seed_iter exposed to user. - opt.max_iter = 200; - - // --------------------------------------------------------------------------------------------------------------------------- - // PROCESS THE SEED SUBSET - // --------------------------------------------------------------------------------------------------------------------------- - if (tid == 0) { - - // seed coordinates - int x = seed_x / ss_step; - int y = seed_y / ss_step; - int idx = ss_grid[last_size].mask[y * ss_grid[last_size].num_ss_x + x]; - - - // if the first image. Take the optimization parameters from rigid fourier - std::fill(opt.p.begin(), opt.p.end(), 0.0); - opt.p[0] = fourier::shifts[last_size].x[idx]; - opt.p[1] = fourier::shifts[last_size].y[idx]; - - // Extract reference subset and solve for starting seed point - subset::get_px_from_img(ss_ref, seed_x, seed_y, px_hori, px_vert, img_ref); - - - double centre_x = seed_x + static_cast(ss_size)/2.0 - 0.5; - double centre_y = seed_y + static_cast(ss_size)/2.0 - 0.5; - - OptResult seed_res = optimizer::solve(centre_x, centre_y, ss_ref, ss_def, interp_def, opt, conf.corr_crit); - - // append the results for the current subset to result vectors - result_arrays.append(seed_res, results_num, idx); - - computed_mask[idx].store(1); - - // loop over the neighbours for the initial seed point - for (size_t n = 0; n < ss_grid[last_size].neigh[idx].size(); n++) { - - // subset index of neighbour to the current point - int nidx = ss_grid[last_size].neigh[idx][n]; - - int nx = ss_grid[last_size].coords[nidx*2]; - int ny = ss_grid[last_size].coords[nidx*2+1]; - - subset::get_px_from_img(ss_ref, nx, ny, px_hori, px_vert, img_ref); - - // get parameter values from fft output or from previous image - std::fill(opt.p.begin(), opt.p.end(), 0.0); - opt.p[0] = fourier::shifts[last_size].x[nidx]; - opt.p[1] = fourier::shifts[last_size].y[nidx]; - - // perform optimization for seed point neighbours - double centre_x = nx + static_cast(ss_size)/2.0 - 0.5; - double centre_y = ny + static_cast(ss_size)/2.0 - 0.5; - OptResult nres = optimizer::solve(centre_x, centre_y, ss_ref, ss_def, interp_def, opt, conf.corr_crit); - - // append the results for the current subset to result vectors - result_arrays.append(nres, results_num, nidx); - - // update mask - computed_mask[nidx].store(1); - - // Add points to queue - local_q[0].push(rg::Point(nidx,nres.cost)); - - // update progress bar - if (g_debug_level>0){ - int progress = current_progress.fetch_add(1); - pbar.update(progress); - } - } - } - - - // --------------------------------------------------------------------------------------------------------------------------- - // PROCESS ALL OTHER SUBSETS - // --------------------------------------------------------------------------------------------------------------------------- - #pragma omp barrier - - // TODO: reset seed location using the last computed point - opt.max_iter = conf.max_iter; - - std::vector temp_neigh; - temp_neigh.reserve(4); - - const int max_idle_iters = 100; - rg::Point current(0, 0); - - while (!stop_request) { - bool got_point = false; - int idle_iters = 0; - - // Try own queue safely - { - std::lock_guard lock(queue_mutexes[tid]); - if (!thread_q.empty()) { - current = thread_q.top(); - thread_q.pop(); - got_point = true; - } - } - - // Steal if nothing in own queue - if (!got_point) { - while (!got_point && idle_iters < max_idle_iters) { - { - std::lock_guard lock(steal_mutex); - for (size_t i = 0; i < local_q.size(); ++i) { - std::lock_guard lock(queue_mutexes[i]); - if (!local_q[i].empty()) { - current = local_q[i].top(); - local_q[i].pop(); - got_point = true; - break; - } - } - } - if (!got_point) { - ++idle_iters; - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } - } - } - - if (!got_point) { - break; - } - - temp_neigh.clear(); - - - // index of current point in results arrays - int idx_results = result_arrays.index(current.idx, results_num); - int idx_results_p = result_arrays.index_parameters(current.idx, results_num); - - // loop over neighbouring points - for (size_t n = 0; n < ss_grid[last_size].neigh[current.idx].size(); n++) { - - // subset index of neighbour to the current point - int nidx = ss_grid[last_size].neigh[current.idx][n]; - - int expected = 0; - expected = computed_mask[nidx].exchange(1); - if (expected == 0) { - - // coords of neigh - int nx = ss_grid[last_size].coords[nidx*2]; - int ny = ss_grid[last_size].coords[nidx*2+1]; - - // extract subset - subset::get_px_from_img(ss_ref, nx, ny, px_hori, px_vert, img_ref); - - // if the neighbouring subset had not met correlation threshold then try values from fft windowing - if (result_arrays.cost[idx_results] < conf.threshold){ - std::fill(opt.p.begin(), opt.p.end(), 0.0); - opt.p[0] = fourier::shifts[last_size].x[nidx]; - opt.p[1] = fourier::shifts[last_size].y[nidx]; - } - else { - for (int i = 0; i < opt.num_params; i++){ - opt.p[i] = result_arrays.p[idx_results_p+i]; - } - } - - // optimize - double centre_x = nx + static_cast(ss_size)/2.0 - 0.5; - double centre_y = ny + static_cast(ss_size)/2.0 - 0.5; - OptResult nres = optimizer::solve(centre_x, centre_y, ss_ref, ss_def, interp_def, opt, conf.corr_crit); - - // append results - result_arrays.append(nres, results_num, nidx); - - // add results to temp neighbour results - temp_neigh.emplace_back(nidx, nres.cost); - - // update progress bar - if (g_debug_level>0){ - int progress = current_progress.fetch_add(1); - if (omp_get_thread_num()==0) pbar.update(progress); - } - } - } - - for (const auto& neigh : temp_neigh) { - std::lock_guard lock(queue_mutexes[tid]); - thread_q.push(neigh); - } - } - } - if (g_debug_level>0){ - pbar.update(current_progress+1); - pbar.finish(); - } - } - - void singlewindow_incremental_reliability_guided(const double *img_ref, - const double *img_def, - const Interpolator &interp_ref, - const Interpolator &interp_def, - const std::vector &ss_grid, - const util::Config &conf, - const int img_num_ref, - const int img_num_def, - OptResultArrays &result_arrays){ - - - // assign some consts for readability - const int px_hori = conf.px_hori; - const int px_vert = conf.px_vert; - int seed_x = conf.rg_seed.first; - int seed_y = conf.rg_seed.second; - const int nsizes = ss_grid.size(); - const int last_size = nsizes-1; - const int num_ss = ss_grid[last_size].num; - const int ss_size = ss_grid[last_size].size; - const int ss_step = ss_grid[last_size].step; - const int results_num = img_num_def-1; - - // get start location of displacements in previous image - double *prev_img_u = result_arrays.u.data() + result_arrays.index(0,std::max(0,img_num_ref-1)); - double *prev_img_v = result_arrays.v.data() + result_arrays.index(0,std::max(0,img_num_ref-1)); - - // get rigid shifts from fourier - // fourier::single_grid(ss_grid[last_size], prev_img_u, prev_img_v, - // conf.max_disp, img_ref, img_def, interp_def); - - std::string bar_title = "Correlation for " + conf.filenames[img_num_def] + ":"; - ProgressBar pbar(bar_title, num_ss); - std::atomic current_progress(0); - - // quick check for the initial seed point - // if (!rg::is_valid_point(seed_x, seed_y, ss_grid[last_size])) { - // return; - // } - - // Initialize binary mask for computed points (initialized to 0) - std::vector> computed_mask(ss_grid[last_size].mask.size()); - for (auto& val : computed_mask) val.store(0); - - // queue for each thread - std::vector> local_q(omp_get_max_threads()); - - // Mutex vector to protect each queue - std::vector queue_mutexes(omp_get_max_threads()); - std::mutex steal_mutex; - - # pragma omp parallel - { - - int tid = omp_get_thread_num(); - std::priority_queue& thread_q = local_q[tid]; - - // Initialize ref and def subsets - subset::Pixels ss_def(ss_size); - subset::Pixels ss_ref(ss_size); - - // Optimization parameters - optimizer::Parameters opt(conf.num_params, conf.max_iter, - conf.precision, conf.threshold, - px_vert, px_hori, - conf.corr_crit); - - std::vector> fft_windows; - - for (size_t t = 0; t < ss_grid.size(); ++t) { - fft_windows.push_back(std::make_unique(ss_grid[t].size)); - } - - // TODO: opt.seed_iter exposed to user. - opt.max_iter = 200; - - // --------------------------------------------------------------------------------------------------------------------------- - // PROCESS THE SEED SUBSET - // --------------------------------------------------------------------------------------------------------------------------- - if (tid == 0) { - - // seed coordinates - int x = seed_x / ss_step; - int y = seed_y / ss_step; - int idx = ss_grid[last_size].mask[y * ss_grid[last_size].num_ss_x + x]; - - - // need to add offset based on displacements from previous correlation - double seed_x_new, seed_y_new; - if (img_num_ref == 0) { - seed_x_new = seed_x; - seed_y_new = seed_y; - } else { - seed_x_new = seed_x + prev_img_u[idx]; - seed_y_new = seed_y + prev_img_v[idx]; - } - - // reference subset based on results from previous image - subset::get_subpx_from_img(ss_ref, seed_x_new, seed_y_new, interp_ref); - - - // if the first image. Take the optimization parameters from rigid fourier - std::fill(opt.p.begin(), opt.p.end(), 0.0); - fourier::get_single_window_fftcc_peak(opt.p[0], opt.p[1], - seed_x_new, seed_y_new, - ss_size, std::max(conf.max_disp, conf.ss_size), - img_ref, img_def, - interp_def); - - - //std::cout << "PEAK " << opt.p[0] << " " << opt.p[1] << std::endl; - double centre_x = seed_x_new + static_cast(ss_size)/2.0 - 0.5; - double centre_y = seed_y_new + static_cast(ss_size)/2.0 - 0.5; - - OptResult seed_res = optimizer::solve(centre_x, centre_y, ss_ref, ss_def, interp_def, opt, conf.corr_crit); - - if (!seed_res.converged || !seed_res.above_threshold){ - std::cout << "ERROR: unsuccesful convergence at seed location." << std::endl; - std::cout << "Please select a different seed location." << std::endl; - exit(EXIT_FAILURE); - } - - // add deformation from reference image to new results - if (img_num_ref > 0){ - seed_res.u += prev_img_u[idx]; - seed_res.v += prev_img_v[idx]; - } - - // append the results for the current subset to result vectors - result_arrays.append(seed_res, results_num, idx); - - // mark subset as computed - computed_mask[idx].store(1); - - // loop over the neighbours for the initial seed point - for (size_t n = 0; n < ss_grid[last_size].neigh[idx].size(); n++) { - - // subset index of neighbour to the current point - int nidx = ss_grid[last_size].neigh[idx][n]; - - double nx = ss_grid[last_size].coords[nidx*2]; - double ny = ss_grid[last_size].coords[nidx*2+1]; - - - // need to add displacements from previous image - if (img_num_ref > 0){ - nx += prev_img_u[nidx]; - ny += prev_img_v[nidx]; - } - - subset::get_subpx_from_img(ss_ref, nx, ny, interp_ref); - - // get initial guess at parameter values from seed point - int index_p = result_arrays.index_parameters(idx,results_num); - for (int i = 0; i < opt.num_params; i++){ - opt.p[i] = result_arrays.p[index_p+i]; - } - - // perform optimization for seed point neighbours - double centre_x = nx + static_cast(ss_size)/2.0 - 0.5; - double centre_y = ny + static_cast(ss_size)/2.0 - 0.5; - OptResult nres = optimizer::solve(centre_x, centre_y, ss_ref, ss_def, interp_def, opt, conf.corr_crit); - - // add deformation from reference image to new results - if (img_num_ref > 0){ - nres.u += prev_img_u[nidx]; - nres.v += prev_img_v[nidx]; - } - - if (!nres.converged || !nres.above_threshold){ - std::cout << "ERROR: unsuccesful convergence at neighbouring point to seed." << std::endl; - std::cout << "Please select a different seed location." << std::endl; - exit(EXIT_FAILURE); - } - - // append the results for the current subset to result vectors - result_arrays.append(nres, results_num, nidx); - - // update mask - computed_mask[nidx].store(1); - - // add this point to queue - local_q[0].push(rg::Point(nidx,nres.cost)); - - // update progress bar - int progress = current_progress.fetch_add(1); - pbar.update(progress+1); - } - } - - - // --------------------------------------------------------------------------------------------------------------------------- - // PROCESS ALL OTHER SUBSETS - // --------------------------------------------------------------------------------------------------------------------------- - #pragma omp barrier - - // TODO: reset seed location using the last computed point - opt.max_iter = conf.max_iter; - - std::vector temp_neigh; - temp_neigh.reserve(4); - - const int max_idle_iters = 100; - rg::Point current(0, 0); - - while (!stop_request) { - bool got_point = false; - int idle_iters = 0; - - // Try own queue safely - { - std::lock_guard lock(queue_mutexes[tid]); - if (!thread_q.empty()) { - current = thread_q.top(); - thread_q.pop(); - got_point = true; - } - } - - // Steal if nothing in own queue - if (!got_point) { - while (!got_point && idle_iters < max_idle_iters) { - { - std::lock_guard lock(steal_mutex); - for (size_t i = 0; i < local_q.size(); ++i) { - std::lock_guard lock(queue_mutexes[i]); - if (!local_q[i].empty()) { - current = local_q[i].top(); - local_q[i].pop(); - got_point = true; - break; - } - } - } - if (!got_point) { - ++idle_iters; - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } - } - } - - if (!got_point) { - break; - } - - temp_neigh.clear(); - - - // index of current point in results arrays - int idx_results_def = result_arrays.index(current.idx, results_num); - int idx_results_def_p = result_arrays.index_parameters(current.idx, results_num); - - - // loop over neighbouring points - for (size_t n = 0; n < ss_grid[last_size].neigh[current.idx].size(); n++) { - - // subset index of neighbour to the current point - int nidx = ss_grid[last_size].neigh[current.idx][n]; - - int expected = 0; - expected = computed_mask[nidx].exchange(1); - if (expected == 0) { - - // coords of neigh - double nx = ss_grid[last_size].coords[nidx*2]; - double ny = ss_grid[last_size].coords[nidx*2+1]; - - // add displacements from reference image - if (img_num_ref > 0){ - nx += prev_img_u[nidx]; - ny += prev_img_v[nidx]; - } - - // temporarily fill p with results from prev img to get - int idx_results_p_ref = result_arrays.index_parameters(nidx, img_num_ref); - for (int i = 0; i < opt.num_params; i++) opt.p[i] = result_arrays.p[idx_results_p_ref+i]; - - subset::get_subpx_from_shape_params(ss_ref, nx, ny, opt.p, interp_ref); - - - // if the neighbouring subset had not met correlation threshold then try values from fft windowing - if (result_arrays.cost[idx_results_def] < conf.threshold){ - std::fill(opt.p.begin(), opt.p.end(), 0.0); - fourier::get_single_window_fftcc_peak(opt.p[0], opt.p[1], - nx, ny, - ss_size, std::max(conf.max_disp, conf.ss_size), - img_ref, img_def, - interp_def); - } - else { - for (int i = 0; i < opt.num_params; i++){ - opt.p[i] = result_arrays.p[idx_results_def_p+i]; - } - } - - // optimize - double centre_x = nx + static_cast(ss_size)/2.0 - 0.5; - double centre_y = ny + static_cast(ss_size)/2.0 - 0.5; - - OptResult nres = optimizer::solve(centre_x, centre_y, ss_ref, ss_def, interp_def, opt, conf.corr_crit); - - // add deformation from reference image to new results - if ((nres.converged) && (nres.above_threshold) && (img_num_ref > 0)){ - nres.u += prev_img_u[nidx]; - nres.v += prev_img_v[nidx]; - } - else if (img_num_ref > 0){ - nres.u = prev_img_u[nidx]; - nres.v = prev_img_v[nidx]; - } - - // append results - result_arrays.append(nres, results_num, nidx); - - // add results to temp neighbour results - temp_neigh.emplace_back(nidx, nres.cost); - - // update progress bar - int progress = current_progress.fetch_add(1); - if (tid==0) pbar.update(progress+1); - } - } - - for (const auto& neigh : temp_neigh) { - std::lock_guard lock(queue_mutexes[tid]); - thread_q.push(neigh); - } - } - } - pbar.update(current_progress+1); - pbar.finish(); - } - - void multiwindow(const double *img_ref, - const double *img_def, - const Interpolator &interp_def, - const std::vector &ss_grid, - const util::Config &conf, - const int img_num, - OptResultArrays &result_arrays){ - - // for the first image perform the FFT windowing. later images will be - // seeded with previous images - fourier::multiwindow(ss_grid, img_ref, img_def, interp_def, conf.fft_mad, conf.fft_mad_scale); - - const int nsizes = ss_grid.size(); - const int last_size = nsizes-1; - const int results_num = img_num-1; - - #pragma omp parallel shared(stop_request, result_arrays, ss_grid, conf, interp_def) - { - - subset::Pixels ss_ref(ss_grid[last_size].size); - subset::Pixels ss_def(ss_grid[last_size].size); - - // get number of subsets and the size for the smalllest window size - const int num_ss = ss_grid[last_size].num; - - - - #pragma omp for - for (int ss = 0; ss < num_ss; ss++){ - - // exit the main DIC loop when ctrl+C is hit - if (stop_request){ - continue; - } - - // append fourier results to master result vectors - OptResult res(conf.num_params); - res.u = fourier::shifts[last_size].x[ss]; - res.p[0] = fourier::shifts[last_size].x[ss]; - res.v = fourier::shifts[last_size].y[ss]; - res.p[1] = fourier::shifts[last_size].y[ss]; - - // get the reference subset - const double ss_x = ss_grid[last_size].coords[ss*2]; - const double ss_y = ss_grid[last_size].coords[ss*2+1]; - - // get the reference subset - subset::get_px_from_img(ss_ref, ss_x, ss_y, conf.px_hori, conf.px_vert, img_ref); - subset::get_subpx_from_img(ss_def, ss_x+res.u, ss_y+res.v, interp_def); - - // calculate zncc value - double zncc = 0.0; - double mean_def = 0.0; - double mean_ref = 0.0; - - for (int i = 0; i < ss_def.num_px; ++i) { - mean_ref += ss_ref.vals[i]; - mean_def += ss_def.vals[i]; - } - - mean_ref /= ss_ref.num_px; - mean_def /= ss_def.num_px; - - double sum_squared_ref = 0.0; - double sum_squared_def = 0.0; - for (int i = 0; i < ss_def.num_px; ++i) { - sum_squared_ref += (ss_ref.vals[i] - mean_ref) * (ss_ref.vals[i] - mean_ref); - sum_squared_def += (ss_def.vals[i] - mean_def) * (ss_def.vals[i] - mean_def); - } - - // Bail out if either subset is degenerate (uniform/zero intensity) - if (sum_squared_def < 1e-12 || sum_squared_ref < 1e-12) { - zncc = 0.0; - } - else { - const double inv_sum_squared = 1.0 / sqrt(sum_squared_ref*sum_squared_def); - - for (int i = 0; i < ss_def.num_px; ++i) { - const double def_norm = (ss_def.vals[i] - mean_def); - const double ref_norm = (ss_ref.vals[i] - mean_ref); - zncc += ref_norm*def_norm; - } - zncc *= inv_sum_squared; - } - - res.cost = zncc; - res.converged=true; - res.above_threshold=true; - result_arrays.append(res, results_num, ss); - } - } - } - - - - void single_window_fourier(const double *img_ref, - const double *img_def, - const Interpolator &interp_def, - const subset::Grid &ss_grid, - const util::Config &conf, - const int img_num, - OptResultArrays &result_arrays){ - - // for the first image perform the FFT windowing. later images will be - // seeded with previous images - //fourier::single_grid(ss_grid, 256, img_ref, img_def, interp_def); - - // get number of subsets and the size for the smalllest window size - const int num_ss = ss_grid.num; - const int ss_size = ss_grid.size; - const int results_num = img_num-1; - - // progress bar - std::string bar_title = "Correlation for " + conf.filenames[img_num] + ":"; - ProgressBar pbar(bar_title, num_ss); - std::atomic current_progress = 0; - - // loop over subsets within the ROI - #pragma omp parallel shared(stop_request) - { - - // initialise subsets - subset::Pixels ss_def(ss_size); - subset::Pixels ss_ref(ss_size); - - // optimization parameters - optimizer::Parameters opt(conf.num_params, conf.max_iter, - conf.precision, conf.threshold, - conf.px_vert, conf.px_hori, - conf.corr_crit); - - - #pragma omp for - for (int ss = 0; ss < num_ss; ss++){ - - // exit the main DIC loop when ctrl+C is hit - if (stop_request){ - continue; - } - - // subset coordinate list takes central locations. - // Converting to top left corner for optimization routine - int ss_x = ss_grid.coords[ss*2]; - int ss_y = ss_grid.coords[ss*2+1]; - - // get the reference subset - subset::get_px_from_img(ss_ref, ss_x, ss_y, conf.px_hori, conf.px_vert, img_ref); - - std::fill(opt.p.begin(), opt.p.end(), 0.0); - opt.p[0] = fourier::shifts[0].x[ss]; - opt.p[1] = fourier::shifts[0].y[ss]; - - // perform optimization on subset from deformed image - double centre_x = ss_x + static_cast(ss_size)/2.0 - 0.5; - double centre_y = ss_y + static_cast(ss_size)/2.0 - 0.5; - OptResult res = optimizer::solve(centre_x, centre_y, ss_ref, ss_def, interp_def, opt, conf.corr_crit); - - // append optimization results to results vectors - result_arrays.append(res, img_num, ss); - - // update progress bar - int progress = current_progress.fetch_add(1); - if (omp_get_thread_num()==0) pbar.update(progress); - - } - } - pbar.update(current_progress+1); - pbar.finish(); - } - -} diff --git a/src/pyvale/dic/cpp/dicscanmethod.hpp b/src/pyvale/dic/cpp/dicscanmethod.hpp deleted file mode 100644 index d1501c59d..000000000 --- a/src/pyvale/dic/cpp/dicscanmethod.hpp +++ /dev/null @@ -1,119 +0,0 @@ -// ================================================================================ -// pyvale: the python validation engine -// License: MIT -// Copyright (C) 2025 The Computer Aided Validation Team -// ================================================================================ - -#ifndef DICSCANMETHOD_H -#define DICSCANMETHOD_H - - -// STD library Header files - -// Program Header files -#include "./dicutil.hpp" -#include "dicresults.hpp" - - - -namespace scanmethod { - - - -/** - * @brief straightforward image scan method. - * Loops over the subsets as a raster across the image. - * initial subset locations are distrubuted evenly across the image - * - * @param result_arrays where to populate the results - * @param img_ref pointer to reference image - * @param img_def pointer to deformed image - * @param ss_grid pointer to subset information - * @param conf pointer to DIC config struct - * @param img_num current image number - */ -void image(const double *img_ref, - const Interpolator &interp_def, - const subset::Grid &ss_grid, - const util::Config &conf, - const int img_num, - OptResultArrays &result_arrays); - - -/** - * @brief reliability guided scan method. - * correlation is calculated for initial seed point and nearest neighbours.image - * Scan proceeds along path with better matching subsets. - * A full indepth outline of the method can be found here: - * https://opg.optica.org/ao/abstract.cfm?uri=ao-48-8-1535 - * - * @param img_ref pointer to reference image - * @param img_def pointer to deformed image - * @param ss_grid pointer to subset information - * @param conf pointer to DIC config struct - * @param img_num current image number - */ -void multiwindow_reliability_guided(const double *img_ref, - const double *img_def, - const Interpolator &interp_def, - const std::vector &ss_grid, - const util::Config &conf, - const int img_num, - OptResultArrays &result_arrays); - -/** - * @brief reliability guided scan method using a single grid FFTCC to estimate rigid displacements. - */ -void singlewindow_reliability_guided(const double *img_ref, - const double *img_def, - const Interpolator &interp_def, - const std::vector &ss_grid, - const util::Config &conf, - const int img_num, - OptResultArrays &result_arrays); - - -/** - * @brief reliability guided scan method with incremental updating. - */ -void singlewindow_incremental_reliability_guided(const double *img_ref, - const double *img_def, - const Interpolator &interp_ref, - const Interpolator &interp_def, - const std::vector &ss_grid, - const util::Config &conf, - const int img_num_ref, - const int img_num_def, - OptResultArrays &result_arrays); - - - - -/** - * @brief Multi Window Fast Fourier Transform (FFT) DIC method. - * correlation is calculated for initial seed point and nearest neighbours.image - * Scan proceeds along path with better matching subsets. - * A full indepth outline of the method can be found here: - * https://opg.optica.org/ao/abstract.cfm?uri=ao-48-8-1535 - * - * @param img_ref pointer to reference image - * @param img_def pointer to deformed image - * @param ss_grid pointer to subset information - * @param conf pointer to DIC config struct - * @param img_num current image number - */ -void multiwindow(const double *img_ref, - const double *img_def, - const Interpolator &interp_def, - const std::vector &ss_grid, - const util::Config &conf, - const int img_num, - OptResultArrays &result_arrays); - - - - - -} - -#endif // DICSCANMETHOD_H diff --git a/src/pyvale/dic/cpp/dicshapefunc.cpp b/src/pyvale/dic/cpp/dicshapefunc.cpp index 95bde9937..de2ad325a 100644 --- a/src/pyvale/dic/cpp/dicshapefunc.cpp +++ b/src/pyvale/dic/cpp/dicshapefunc.cpp @@ -11,56 +11,45 @@ #include // Program Header files -#include "./dicresults.hpp" - -namespace shapefunc { - - // Function pointer - void (*get_pixel)(double &, double &, const double, const double, const std::vector &); - void (*get_dfdp)(std::vector&, double, double, double, double); - void (*get_displacement)(OptResult &results, double ss_x, double ss_y, std::vector &p); - +#include "./dicshapefunc.hpp" // Shape function declarations - inline void get_pixel_affine(double &x_new, double &y_new, const double x, const double y, const std::vector &p){ + void Affine::get_pixel(double &x_new, double &y_new, const double x, const double y, const std::vector &p){ x_new = p[0] + (1.0+p[2]) * x + p[3] * y; y_new = p[1] + (1.0+p[5]) * y + p[4] * x; } - inline void get_pixel_rigid(double &x_new, double &y_new, const double x, const double y, const std::vector &p){ + void Rigid::get_pixel(double &x_new, double &y_new, const double x, const double y, const std::vector &p){ x_new = p[0] + x; y_new = p[1] + y; } - inline void get_pixel_quad(double &x_new, double &y_new, const double x, const double y, const std::vector &p){ + void Quad::get_pixel(double &x_new, double &y_new, const double x, const double y, const std::vector &p){ x_new = p[0] + (1.0+p[2])*x + p[3]*y + p[6]*x*x + p[7]*x*y + p[8]*y*y; y_new = p[1] + (1.0+p[5])*y + p[4]*x + p[9]*x*x + p[10]*x*y + p[11]*y*y; } - inline void get_displacement_from_quad(OptResult &res, double ss_x, double ss_y, std::vector &p){ - double x_new = p[0] + (1.0+p[2])*ss_x + p[3]*ss_y + p[6]*ss_x*ss_x + p[7]*ss_x*ss_y + p[8]*ss_y*ss_y; - double y_new = p[1] + (1.0+p[5])*ss_y + p[4]*ss_x + p[9]*ss_x*ss_x + p[10]*ss_x*ss_y + p[11]*ss_y*ss_y; - res.u = x_new - ss_x; - res.v = y_new - ss_y; - res.mag = std::sqrt(res.u * res.u + res.v * res.v); + void Quad::get_displacement(double &u, double &v, const double x, const double y, const std::vector &p){ + double x_new = p[0] + (1.0+p[2])*x + p[3]*y + p[6]*x*x + p[7]*x*y + p[8]*y*y; + double y_new = p[1] + (1.0+p[5])*y + p[4]*x + p[9]*x*x + p[10]*x*y + p[11]*y*y; + u = x_new - x; + v = y_new - y; } - inline void get_displacement_from_affine(OptResult &res, double ss_x, double ss_y, std::vector &p){ - double x_new = p[0] + (1.0+p[2]) * ss_x + p[3] * ss_y; - double y_new = p[1] + (1.0+p[5]) * ss_y + p[4] * ss_x; - res.u = x_new - ss_x; - res.v = y_new - ss_y; - res.mag = std::sqrt(res.u * res.u + res.v * res.v); + void Affine::get_displacement(double &u, double &v, const double x, const double y, const std::vector &p){ + double x_new = p[0] + (1.0+p[2]) * x + p[3] * y; + double y_new = p[1] + (1.0+p[5]) * y + p[4] * x; + u = x_new - x; + v = y_new - y; } - inline void get_displacement_from_rigid(OptResult &res, double ss_x, double ss_y, std::vector &p){ - res.u = p[0]; - res.v = p[1]; - res.mag = std::sqrt(res.u*res.u + res.v*res.v); + void Rigid::get_displacement(double &u, double &v, const double x, const double y, const std::vector &p){ + u = p[0]; + v = p[1]; } - inline void get_daffine_dp(std::vector &dfdp, double x, double y, double dfdx, double dfdy){ + void Affine::get_dshape_dp(std::vector &dfdp, const double x, const double y, const double dfdx, const double dfdy){ dfdp[0] = dfdx; dfdp[1] = dfdy; dfdp[2] = dfdx * x; @@ -69,12 +58,12 @@ namespace shapefunc { dfdp[5] = dfdy * y; } - inline void get_drigid_dp(std::vector &dfdp, double x, double y, double dfdx, double dfdy){ + void Rigid::get_dshape_dp(std::vector &dfdp, const double x, const double y, const double dfdx, const double dfdy){ dfdp[0] = dfdx; dfdp[1] = dfdy; } - inline void get_dquad_dp(std::vector &dfdp, double x, double y, double dfdx, double dfdy){ + void Quad::get_dshape_dp(std::vector &dfdp, const double x, const double y, const double dfdx, const double dfdy){ dfdp[0] = dfdx; dfdp[1] = dfdy; dfdp[2] = dfdx * x; @@ -89,29 +78,134 @@ namespace shapefunc { dfdp[11] = dfdy * y*y; } + void Rigid::compose(std::vector &pC, const std::vector &pA, const std::vector &pB){ + pC[0] = pA[0] + pB[0]; + pC[1] = pA[1] + pB[1]; + } + + void Affine::compose(std::vector &pC, const std::vector &pA, const std::vector &pB){ + pC[0] = pB[0] + (1.0+pB[2])*pA[0] + pB[3]*pA[1]; + pC[1] = pB[1] + (1.0+pB[5])*pA[1] + pB[4]*pA[0]; + pC[2] = (1.0+pB[2])*(1.0+pA[2]) + pB[3]*pA[4] - 1.0; + pC[3] = (1.0+pB[2])*pA[3] + pB[3]*(1.0+pA[5]); + pC[4] = (1.0+pB[5])*pA[4] + pB[4]*(1.0+pA[2]); + pC[5] = (1.0+pB[5])*(1.0+pA[5]) + pB[4]*pA[3] - 1.0; + } - // Setter for the current function - void set(const std::string& shape_func) { - if (shape_func == "RIGID") { - get_pixel = get_pixel_rigid; - get_dfdp = get_drigid_dp; - get_displacement = get_displacement_from_rigid; - } - else if (shape_func == "AFFINE") { - get_pixel = get_pixel_affine; - get_dfdp = get_daffine_dp; - get_displacement = get_displacement_from_affine; - } - else if (shape_func == "QUAD") { - get_pixel = get_pixel_quad; - get_dfdp = get_dquad_dp; - get_displacement = get_displacement_from_quad; - } - else { - std::cerr << "Unexpected Shape Function: '" << shape_func << "'" << std::endl; - std::cerr << "Allowed Values: 'RIGID', 'AFFINE', 'QUAD'." << std::endl; - exit(EXIT_FAILURE); - } + void Quad::compose(std::vector &pC, const std::vector &pA, const std::vector &pB){ + pC[0] = pB[0] + (1.0+pB[2])*pA[0] + pB[3]*pA[1]; + pC[1] = pB[1] + (1.0+pB[5])*pA[1] + pB[4]*pA[0]; + pC[2] = (1.0+pB[2])*(1.0+pA[2]) + pB[3]*pA[4] - 1.0; + pC[3] = (1.0+pB[2])*pA[3] + pB[3]*(1.0+pA[5]); + pC[4] = (1.0+pB[5])*pA[4] + pB[4]*(1.0+pA[2]); + pC[5] = (1.0+pB[5])*(1.0+pA[5]) + pB[4]*pA[3] - 1.0; + pC[6] = (1.0+pB[2])*pA[6] + pB[3]*pA[9] + pB[6]*(1.0+pA[2])*(1.0+pA[2]) + pB[7]*(1.0+pA[2])*pA[4] + pB[9]*pA[4]*pA[4]; + pC[7] = (1.0+pB[2])*pA[7] + pB[3]*pA[10] + pB[6]*2.0*(1.0+pA[2])*pA[3] + pB[7]*((1.0+pA[2])*(1.0+pA[5]) + pA[3]*pA[4]) + pB[9]*2.0*pA[4]*(1.0+pA[5]); + pC[8] = (1.0+pB[2])*pA[8] + pB[3]*pA[11] + pB[6]*pA[3]*pA[3] + pB[7]*pA[3]*(1.0+pA[5]) + pB[9]*(1.0+pA[5])*(1.0+pA[5]); + pC[9] = pB[4]*pA[6] + (1.0+pB[5])*pA[9] + pB[9]*(1.0+pA[2])*(1.0+pA[2]) + pB[10]*(1.0+pA[2])*pA[4] + pB[11]*pA[4]*pA[4]; + pC[10] = pB[4]*pA[7] + (1.0+pB[5])*pA[10] + pB[9]*2.0*(1.0+pA[2])*pA[3] + pB[10]*((1.0+pA[2])*(1.0+pA[5]) + pA[3]*pA[4]) + pB[11]*2.0*pA[4]*(1.0+pA[5]); + pC[11] = pB[4]*pA[8] + (1.0+pB[5])*pA[11] + pB[9]*pA[3]*pA[3] + pB[10]*pA[3]*(1.0+pA[5]) + pB[11]*(1.0+pA[5])*(1.0+pA[5]); } +void Affine::compose_inverse(std::vector& p_new, + const std::vector& p, + const std::vector& dp) +{ + // Build 2×2 warp matrices A = I + Jp, B = I + Jdp + const double A[2][2] = { {1.0 + p[2], p[3] }, + { p[4], 1.0 + p[5] } }; + const double B[2][2] = { {1.0 + dp[2], dp[3] }, + { dp[4], 1.0 + dp[5] } }; + + // Invert B + const double detB = B[0][0]*B[1][1] - B[0][1]*B[1][0]; + const double invB[2][2] = { { B[1][1]/detB, -B[0][1]/detB }, + {-B[1][0]/detB, B[0][0]/detB } }; + + // C = A · invB + const double C[2][2] = { + { A[0][0]*invB[0][0] + A[0][1]*invB[1][0], + A[0][0]*invB[0][1] + A[0][1]*invB[1][1] }, + { A[1][0]*invB[0][0] + A[1][1]*invB[1][0], + A[1][0]*invB[0][1] + A[1][1]*invB[1][1] } + }; + + // Translation: t_new = t_p - C · t_dp + p_new[0] = p[0] - (C[0][0]*dp[0] + C[0][1]*dp[1]); + p_new[1] = p[1] - (C[1][0]*dp[0] + C[1][1]*dp[1]); + + // Deformation gradient + p_new[2] = C[0][0] - 1.0; + p_new[3] = C[0][1]; + p_new[4] = C[1][0]; + p_new[5] = C[1][1] - 1.0; +} + +void Rigid::compose_inverse(std::vector& p_new, + const std::vector& p, + const std::vector& dp) +{ + // Build 2×2 warp matrices A = I + Jp, B = I + Jdp + const double A[2][2] = { {1.0 + p[2], p[3] }, + { p[4], 1.0 + p[5] } }; + const double B[2][2] = { {1.0 + dp[2], dp[3] }, + { dp[4], 1.0 + dp[5] } }; + + // Invert B + const double detB = B[0][0]*B[1][1] - B[0][1]*B[1][0]; + const double invB[2][2] = { { B[1][1]/detB, -B[0][1]/detB }, + {-B[1][0]/detB, B[0][0]/detB } }; + + // C = A · invB + const double C[2][2] = { + { A[0][0]*invB[0][0] + A[0][1]*invB[1][0], + A[0][0]*invB[0][1] + A[0][1]*invB[1][1] }, + { A[1][0]*invB[0][0] + A[1][1]*invB[1][0], + A[1][0]*invB[0][1] + A[1][1]*invB[1][1] } + }; + + // Translation: t_new = t_p - C · t_dp + p_new[0] = p[0] - (C[0][0]*dp[0] + C[0][1]*dp[1]); + p_new[1] = p[1] - (C[1][0]*dp[0] + C[1][1]*dp[1]); + + // Deformation gradient + p_new[2] = C[0][0] - 1.0; + p_new[3] = C[0][1]; + p_new[4] = C[1][0]; + p_new[5] = C[1][1] - 1.0; +} + + +void Quad::compose_inverse(std::vector& p_new, + const std::vector& p, + const std::vector& dp) +{ + // Build 2×2 warp matrices A = I + Jp, B = I + Jdp + const double A[2][2] = { {1.0 + p[2], p[3] }, + { p[4], 1.0 + p[5] } }; + const double B[2][2] = { {1.0 + dp[2], dp[3] }, + { dp[4], 1.0 + dp[5] } }; + + // Invert B + const double detB = B[0][0]*B[1][1] - B[0][1]*B[1][0]; + const double invB[2][2] = { { B[1][1]/detB, -B[0][1]/detB }, + {-B[1][0]/detB, B[0][0]/detB } }; + + // C = A · invB + const double C[2][2] = { + { A[0][0]*invB[0][0] + A[0][1]*invB[1][0], + A[0][0]*invB[0][1] + A[0][1]*invB[1][1] }, + { A[1][0]*invB[0][0] + A[1][1]*invB[1][0], + A[1][0]*invB[0][1] + A[1][1]*invB[1][1] } + }; + + // Translation: t_new = t_p - C · t_dp + p_new[0] = p[0] - (C[0][0]*dp[0] + C[0][1]*dp[1]); + p_new[1] = p[1] - (C[1][0]*dp[0] + C[1][1]*dp[1]); + + // Deformation gradient + p_new[2] = C[0][0] - 1.0; + p_new[3] = C[0][1]; + p_new[4] = C[1][0]; + p_new[5] = C[1][1] - 1.0; } diff --git a/src/pyvale/dic/cpp/dicshapefunc.hpp b/src/pyvale/dic/cpp/dicshapefunc.hpp index 1ff53205c..53e26c625 100644 --- a/src/pyvale/dic/cpp/dicshapefunc.hpp +++ b/src/pyvale/dic/cpp/dicshapefunc.hpp @@ -14,27 +14,192 @@ #include // DIC Header files -#include "./dicresults.hpp" -namespace shapefunc { - // Function pointer - extern void (*get_pixel)(double &, double &, const double, const double, const std::vector &); - extern void (*get_dfdp)(std::vector&, double, double, double, double); - extern void (*get_displacement)(OptResult &result, double ss_x, double ss_y, std::vector &p); +/** + * @brief Affine shape function for DIC subset deformation. + * Models translation, rotation, shear and normal strain (6 parameters). + * + * Coordinate convention: (x, y) are in the local subset frame, relative + * to the subset's top-left corner. Shape function parameters describe + * deformation anchored at the subset centre, so get_displacement() should + * be evaluated at (cx - global_x, cy - global_y) to recover u and v at + * the centre. + * + * p = [u, v, du/dx, du/dy, dv/dx, dv/dy] + */ +struct Affine { - // Shape function declarations - inline void get_pixel_affine(double &x_new, double &y_new, const double x, const double y, const std::vector &p); - inline void get_pixel_rigid(double &x_new, double &y_new, const double x, const double y, const std::vector &p); - inline void get_pixel_quad(double &x_new, double &y_new, const double x, const double y, const std::vector &p); + /** + * @brief Maps a reference pixel to its deformed position. + * @param[out] x_new Deformed x-coordinate in local subset frame [pixels] + * @param[out] y_new Deformed y-coordinate in local subset frame [pixels] + * @param[in] x Reference x-coordinate in local subset frame [pixels] + * @param[in] y Reference y-coordinate in local subset frame [pixels] + * @param[in] p Shape function parameters [u, v, du/dx, du/dy, dv/dx, dv/dy] + */ + static void get_pixel(double &x_new, double &y_new, const double x, const double y, const std::vector &p); - int num_params(std::string &shape_func); + /** + * @brief Computes the Jacobian row df/dp for this pixel, used to build the + * Hessian and gradient in the Levenberg-Marquardt optimisation. + * @param[out] dfdp Jacobian entries (6 elements): [dfdx, dfdy, dfdx*x, dfdx*y, dfdy*x, dfdy*y] + * @param[in] x Reference x-coordinate in local subset frame [pixels] + * @param[in] y Reference y-coordinate in local subset frame [pixels] + * @param[in] dfdx Image gradient in x at this pixel + * @param[in] dfdy Image gradient in y at this pixel + */ + static void get_dshape_dp(std::vector &dfdp, const double x, const double y, const double dfdx, const double dfdy); + /** + * @brief Computes displacement (u, v) at a point in the local subset frame. + * To recover the displacement at the subset centre, pass + * x = cx - global_x, y = cy - global_y. + * @param[out] u Displacement in x-direction [pixels] + * @param[out] v Displacement in y-direction [pixels] + * @param[in] x x-coordinate in local subset frame [pixels] + * @param[in] y y-coordinate in local subset frame [pixels] + * @param[in] p Shape function parameters [u, v, du/dx, du/dy, dv/dx, dv/dy] + */ + static void get_displacement(double &u, double &v, const double x, const double y, const std::vector &p); - // Setter for the current function - void set(const std::string &func_name); + /** + * @brief Composes two Affine transforms to produce a single equivalent transform. + * If pA maps shape0->shape1 and pB maps shape1->shape2, + * then pC maps shape0->shape2. + * @param[out] pC Composed shape function parameters (6 elements) + * @param[in] pA Shape function parameters for shape0->shape1 (6 elements) + * @param[in] pB Shape function parameters for shape1->shape2 (6 elements) + */ + static void compose(std::vector &pC, const std::vector &pA, const std::vector &pB); -} -#endif // DICSMOOTH_H + static void compose_inverse(std::vector& p_new, const std::vector& p, const std::vector& dp); + + static constexpr int num_params = 6; /**< Number of shape function parameters */ +}; + +/** + * @brief Quadratic shape function for DIC subset deformation. + * Extends affine with second-order terms, capturing bending and + * non-uniform strain fields (12 parameters). + * + * Coordinate convention: same as Affine — (x, y) are in the local subset + * frame, relative to the subset's top-left corner. + * + * p = [u, v, du/dx, du/dy, dv/dx, dv/dy, + * d2u/dx2, d2u/dxdy, d2u/dy2, d2v/dx2, d2v/dxdy, d2v/dy2] + */ +struct Quad { + + /** + * @brief Maps a reference pixel to its deformed position. + * @param[out] x_new Deformed x-coordinate in local subset frame [pixels] + * @param[out] y_new Deformed y-coordinate in local subset frame [pixels] + * @param[in] x Reference x-coordinate in local subset frame [pixels] + * @param[in] y Reference y-coordinate in local subset frame [pixels] + * @param[in] p Shape function parameters (12 total, see struct description) + */ + static void get_pixel(double &x_new, double &y_new, const double x, const double y, const std::vector &p); + + /** + * @brief Computes the Jacobian row df/dp for this pixel. + * @param[out] dfdp Jacobian entries (12 elements): + * [dfdx, dfdy, dfdx*x, dfdx*y, dfdy*x, dfdy*y, + * dfdx*x^2, dfdx*x*y, dfdx*y^2, dfdy*x^2, dfdy*x*y, dfdy*y^2] + * @param[in] x Reference x-coordinate in local subset frame [pixels] + * @param[in] y Reference y-coordinate in local subset frame [pixels] + * @param[in] dfdx Image gradient in x at this pixel + * @param[in] dfdy Image gradient in y at this pixel + */ + static void get_dshape_dp(std::vector &dfdp, const double x, const double y, const double dfdx, const double dfdy); + + /** + * @brief Computes displacement (u, v) at a point in the local subset frame. + * To recover displacement at the subset centre, pass + * x = cx - global_x, y = cy - global_y. + * @param[out] u Displacement in x-direction [pixels] + * @param[out] v Displacement in y-direction [pixels] + * @param[in] x x-coordinate in local subset frame [pixels] + * @param[in] y y-coordinate in local subset frame [pixels] + * @param[in] p Shape function parameters (12 total) + */ + static void get_displacement(double &u, double &v, const double x, const double y, const std::vector &p); + + /** + * @brief Composes two Quadratic transforms to produce a single equivalent transform. + * If pA maps shape0->shape1 and pB maps shape1->shape2, + * then pC maps shape0->shape2. + * Note: composing two quadratic maps produces terms beyond second order; + * these are truncated so the result remains a valid Quad parameter set. + * @param[out] pC Composed shape function parameters (12 elements) + * @param[in] pA Shape function parameters for shape0->shape1 (12 elements) + * @param[in] pB Shape function parameters for shape1->shape2 (12 elements) + */ + static void compose(std::vector &pC, const std::vector &pA, const std::vector &pB); + + static void compose_inverse(std::vector& p_new, const std::vector& p, const std::vector& dp); + + static constexpr int num_params = 12; /**< Number of shape function parameters */ +}; + +/** + * @brief Rigid (translation-only) shape function for DIC subset deformation. + * Models pure translation with no rotation or strain (2 parameters). + * Displacement is constant across the subset — (x, y) do not affect the result. + * + * Coordinate convention: same as Affine — (x, y) are in the local subset + * frame, relative to the subset's top-left corner. + * + * p = [u, v] + */ +struct Rigid { + /** + * @brief Maps a reference pixel to its deformed position (pure translation). + * @param[out] x_new Deformed x-coordinate in local subset frame [pixels] + * @param[out] y_new Deformed y-coordinate in local subset frame [pixels] + * @param[in] x Reference x-coordinate in local subset frame [pixels] + * @param[in] y Reference y-coordinate in local subset frame [pixels] + * @param[in] p Shape function parameters [u, v] + */ + static void get_pixel(double &x_new, double &y_new, const double x, const double y, const std::vector &p); + + /** + * @brief Computes the Jacobian row df/dp for this pixel. + * For rigid motion x and y do not contribute, so dfdp has only 2 elements. + * @param[out] dfdp Jacobian entries (2 elements): [dfdx, dfdy] + * @param[in] x Reference x-coordinate in local subset frame [pixels] (unused) + * @param[in] y Reference y-coordinate in local subset frame [pixels] (unused) + * @param[in] dfdx Image gradient in x at this pixel + * @param[in] dfdy Image gradient in y at this pixel + */ + static void get_dshape_dp(std::vector &dfdp, const double x, const double y, const double dfdx, const double dfdy); + + /** + * @brief Computes displacement (u, v). For rigid motion displacement is constant + * across the subset, so (x, y) are unused. + * @param[out] u Displacement in x-direction [pixels], equal to p[0] + * @param[out] v Displacement in y-direction [pixels], equal to p[1] + * @param[in] x x-coordinate in local subset frame [pixels] (unused) + * @param[in] y y-coordinate in local subset frame [pixels] (unused) + * @param[in] p Shape function parameters [u, v] + */ + static void get_displacement(double &u, double &v, const double x, const double y, const std::vector &p); + + /** + * @brief Composes two Rigid transforms to produce a single equivalent transform. + * If pA maps shape0->shape1 and pB maps shape1->shape2, + * then pC maps shape0->shape2. + * @param[out] pC Composed shape function parameters (2 elements) + * @param[in] pA Shape function parameters for shape0->shape1 (2 elements) + * @param[in] pB Shape function parameters for shape1->shape2 (2 elements) + */ + static void compose(std::vector &pC, const std::vector &pA, const std::vector &pB); + + static void compose_inverse(std::vector& p_new, const std::vector& p, const std::vector& dp); + + static constexpr int num_params = 2; /**< Number of shape function parameters */ +}; + +#endif // DICSHAPEFUNC_HPP diff --git a/src/pyvale/dic/cpp/dicsinglewindow_rg.cpp b/src/pyvale/dic/cpp/dicsinglewindow_rg.cpp new file mode 100644 index 000000000..c26d46fa0 --- /dev/null +++ b/src/pyvale/dic/cpp/dicsinglewindow_rg.cpp @@ -0,0 +1,444 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + + +// STD library Header files +#include +#include +#include +#include +#include +#include +#include +#include + +// common_cpp headers +#include "../../common_cpp/defines.hpp" +#include "../../common_cpp/progressbar.hpp" +#include "../../common_cpp/dicsignalhandler.hpp" + + +// Program Header files +#include "./dicinterp.hpp" +#include "./dicoptimizer.hpp" +#include "./dicutil.hpp" +#include "./dicrg.hpp" +#include "./dicfourier.hpp" +#include "./dicsubset.hpp" +#include "./dicresults.hpp" +#include "./dicsinglewindow_rg.hpp" +#include "./stereoutil.hpp" + + +void singlewindow_rg(const Interpolator &interp_ref, + const Interpolator &interp_def, + const subset::Grid &ss_grid, + const util::Config &conf, + const int img_num_ref, + const int img_num_def, + const ResultArrays &results_ref, + ResultArrays &results_def, + const std::string &mode, + const std::optional &F, + const std::optional &results_def_l){ + + + + + // assign some consts for readability + const int px_hori = conf.px_hori; + const int px_vert = conf.px_vert; + const int num_ss = ss_grid.num; + const int ss_size_x = ss_grid.size_x; + const int ss_size_y = ss_grid.size_y; + const int ss_step = ss_grid.step; + + auto get_initial_guess_temporal = [&](auto &fft, std::vector &p, double &max_val, double cx, double cy, bool debug) { + get_single_window_fftcc_peak_centre(fft, p, max_val, + cx, cy, + 0, 0, + ss_size_x, ss_size_y, + std::max(2*conf.max_disp, ss_size_x), + std::max(2*conf.max_disp, ss_size_y), + interp_ref, interp_def, debug); + + }; + + + auto get_initial_guess_stereo = [&](std::vector &p, double cx, double cy, double offset_x, double offset_y, bool print) { + stereo::get_rigid_translation_from_rectified_fft(p, cx, cy, ss_size_x, ss_size_y, + 2*conf.epi_distance, ss_size_y, F.value(), + interp_ref, interp_def, offset_x, offset_y, print); + }; + + std::string bar_title = mode + " \033[1;4m" + conf.basenames[img_num_ref] + + "\033[0m -> \033[1;4m" + conf.basenames[img_num_def] + + "\033[0m:"; + + ProgressBar pbar(bar_title, ss_grid.active_total); + std::atomic current_progress(0); + + // Initialize binary mask for computed points (initialized to 0) + std::vector> computed_mask(ss_grid.mask.size()); + for (auto& val : computed_mask) val.store(0); + + // queue for each thread + rg::QueueGlobal queue(omp_get_max_threads()); + + std::atomic error_flag(false); + std::string error_message; + + int count = 0; + + # pragma omp parallel + { + + int tid = omp_get_thread_num(); + + // Initialize ref and def subsets + subset::Pixels ss_def(ss_size_x, ss_size_y); + subset::Pixels ss_ref(ss_size_x, ss_size_y); + + // initialize FFT stuff + std::optional fft_float; + std::optional fft_double; + if (conf.fft_precision == util::FFTPrecision::FLOAT32) { + fft_float.emplace(std::max(2*conf.max_disp, ss_size_x), std::max(2*conf.max_disp, ss_size_y), false); + } else { + fft_double.emplace(std::max(2*conf.max_disp, ss_size_x), std::max(2*conf.max_disp, ss_size_y), false); + } + + double max_val = 0.0; + + // Optimization parameters + Optimizer opt(conf.shape_func, conf.corr_crit, conf.max_iter, conf.precision, conf.threshold, ss_size_x*ss_size_y); + + // TODO: opt.seed_iter exposed to user. + opt.max_iter = 200; + + // --------------------------------------------------------------------------------------------------------------------------- + // PROCESS THE SEED SUBSET + // --------------------------------------------------------------------------------------------------------------------------- + if (tid == 0) { + + + // num seeds + int num_seeds = conf.rg_seeds.size() / 2; + + for (int s = 0; s < num_seeds; s++){ + + int seed_x = conf.rg_seeds[2*s]; + int seed_y = conf.rg_seeds[2*s+1]; + + // seed coordinates + int grid_x = seed_x / ss_step; + int grid_y = seed_y / ss_step; + int idx = ss_grid.mask[grid_y * ss_grid.num_ss_x + grid_x]; + + // if a seed subset is no longer active then we've hit a problemo + if (!ss_grid.active_ss[idx]) { + error_message = "Seed subset (" + std::to_string(seed_x) + "," + + std::to_string(seed_y) + ") is no longer active. " + + "The seed subset failed to converge in a previous calculation"; + error_flag.store(true); + break; + } + + // get the centre coordinates for the subset in img k0 + double cx = ss_grid.coords[2*idx]; + double cy = ss_grid.coords[2*idx+1]; + + // std::cout << count << " " << cx << " " << cy << std::endl; + // count++; + + // fill the reference subset + subset::fill_from_centre_coords(ss_ref, cx, cy, interp_ref); + for (int px = 0; px < ss_ref.num_px; px++) { + ss_ref.x[px] -= cx; + ss_ref.y[px] -= cy; + } + + // if the first image. Take the optimization parameters from rigid fourier + if (mode=="temporal") { + if (conf.fft_precision == util::FFTPrecision::FLOAT32) { + get_initial_guess_temporal(*fft_float, opt.p, max_val, cx, cy, false); + } else { + get_initial_guess_temporal(*fft_double, opt.p, max_val, cx, cy, false); + } + } + if (mode=="stereo") { + get_initial_guess_stereo(opt.p, cx, cy, results_def_l->u[idx], results_def_l->v[idx], false); + } + + + // run optimizer + OptResult seed_res = opt.solve(cx, cy, ss_ref, ss_def, interp_def, true); + + if (!rg::check_convergence(seed_x, seed_y, seed_res, error_message)) { + error_flag.store(true); + break; + } + + // add deformation from reference image to new results + seed_res.u += results_ref.u[idx]; + seed_res.v += results_ref.v[idx]; + + // append the results for the current subset to result vectors + results_def.append(seed_res, idx); + + // mark subset as computed + computed_mask[idx].store(1); + + // loop over the neighbours for the initial seed point + for (size_t n = 0; n < ss_grid.neigh[idx].size(); n++) { + + if (stop_request) continue; + + // subset index of neighbour to the current point + int nidx = ss_grid.neigh[idx][n]; + + double cx = ss_grid.coords[nidx*2]; + double cy = ss_grid.coords[nidx*2+1]; + + // std::cout << count << " " << cx << " " << cy << std::endl; + // count++; + + if (!ss_grid.active_ss[nidx]) { + error_message = "Direct neighbour (" + std::to_string(cx) + "," + std::to_string(cy) + + ") of seed subset (" + std::to_string(seed_x) + "," + std::to_string(seed_y) + + ") is no longer active. The seed subset failed to converge in a previous calculation"; + error_flag.store(true); + break; + } + + // fill the reference subset + subset::fill_from_centre_coords(ss_ref, cx, cy, interp_ref); + for (int px = 0; px < ss_ref.num_px; px++) { + ss_ref.x[px] -= cx; + ss_ref.y[px] -= cy; + } + + // perform optimization for seed point neighbours + opt.copy_params_from_neigh(results_def.p, + results_def.cost, + results_def.above_thresh, + ss_grid.neigh[nidx], + idx); + + OptResult nres = opt.solve(cx, cy, ss_ref, ss_def, interp_def, true); + + if (!rg::check_convergence(seed_x, seed_y, seed_res, error_message, true)) { + error_flag.store(true); + break; + } + + // add deformation from reference image to new results + nres.u += results_ref.u[nidx]; + nres.v += results_ref.v[nidx]; + + + // append the results for the current subset to result vectors + results_def.append(nres, nidx); + + // update mask + computed_mask[nidx].store(1); + + // add this point to queue + queue.push(tid, {rg::Point(nidx,nres.cost)}); + + // update progress bar + if (g_debug_level>0){ + int progress = current_progress.fetch_add(1); + if (omp_get_thread_num()==0) pbar.update(progress+1); + } + } + } + } + + // --------------------------------------------------------------------------------------------------------------------------- + // PROCESS ALL OTHER SUBSETS + // --------------------------------------------------------------------------------------------------------------------------- + #pragma omp barrier + + // reset seed location using the last computed point + opt.max_iter = conf.max_iter; + + std::vector temp_neigh; + temp_neigh.reserve(4); + + rg::Point current(0, 0); + + while (!stop_request && !error_flag.load()) { + + if (!queue.pop(tid, current)) + break; + + temp_neigh.clear(); + + + // loop over neighbouring points + for (size_t n = 0; n < ss_grid.neigh[current.idx].size(); n++) { + + // subset index of neighbour to the current point + int nidx = ss_grid.neigh[current.idx][n]; + + int expected = 0; + expected = computed_mask[nidx].exchange(1); + if (expected == 0) { + + // coords of neigh + double cx = ss_grid.coords[nidx*2]; + double cy = ss_grid.coords[nidx*2+1]; + + // std::cout << count << " " << cx << " " << cy << std::endl; + // count++; + + OptResult nres(opt.num_params); + + // if the subset is no longer active then skip + if (!ss_grid.active_ss[nidx]){ + results_def.append(nres, nidx); + continue; + } + + // fill the reference subset + subset::fill_from_centre_coords(ss_ref, cx, cy, interp_ref); + for (int px = 0; px < ss_ref.num_px; px++) { + ss_ref.x[px] -= cx; + ss_ref.y[px] -= cy; + } + + if (results_def.above_thresh[current.idx]){ + opt.copy_params_from_neigh(results_def.p, + results_def.cost, + results_def.above_thresh, + ss_grid.neigh[nidx], + current.idx); + if (ss_ref.sum!=0) nres = opt.solve(cx, cy, ss_ref, ss_def, interp_def, false); + } + + // add deformation from reference image to new results + nres.u += results_ref.u[nidx]; + nres.v += results_ref.v[nidx]; + + // append results + results_def.append(nres, nidx); + + // add results to temp neighbour results + temp_neigh.emplace_back(nidx, nres.cost); + + // update progress bar + if (g_debug_level>0){ + int progress = current_progress.fetch_add(1); + if (tid==0) pbar.update(progress+1); + } + } + } + + queue.push(tid, temp_neigh); + } + } + + // // TODO: build a list of subsets using openmp that have correlated poorly but have atleast + // // one immediate neighbour above_threshold + // std::vector retry_indices; + // #pragma omp parallel + // { + // std::vector local_retry; + // #pragma omp for nowait + // for (int i = 0; i < num_ss; ++i) { + // // If the point was reached but failed threshold + // if (computed_mask[i].load() == 1 && !results_def.above_thresh[i]) { + // // Check if any neighbor was successful + // for (int nidx : ss_grid.neigh[i]) { + // if (results_def.above_thresh[nidx]) { + // local_retry.push_back(i); + // break; + // } + // } + // } + // } + // #pragma omp critical + // retry_indices.insert(retry_indices.end(), local_retry.begin(), local_retry.end()); + // } + // + // // TODO: retry the correlation using the parameters for the neighbour that + // // have successfuly correlated. use openmp + // + // #pragma omp parallel + // { + // subset::Pixels ss_def(ss_size_x, ss_size_y); + // subset::Pixels ss_ref(ss_size_x, ss_size_y); + // Optimizer opt(conf.shape_func, conf.corr_crit, conf.max_iter, conf.precision, conf.threshold, ss_size_x*ss_size_y); + // + // #pragma omp for + // for (int i = 0; i < (int)retry_indices.size(); ++i) { + // int idx = retry_indices[i]; + // + // // Find the neighbor with the lowest cost (best match) to use as new guess + // int best_neigh = -1; + // double best_cost = -std::numeric_limits::max(); + // for (int nidx : ss_grid.neigh[idx]) { + // if (results_def.above_thresh[nidx] && results_def.cost[nidx] > best_cost) { + // best_cost = results_def.cost[nidx]; + // best_neigh = nidx; + // } + // } + // + // if (best_neigh != -1) { + // double cx_img0 = ss_grid.coords[idx*2]; + // double cy_img0 = ss_grid.coords[idx*2+1]; + // double cx = cx_img0 + (img_num_ref > 0 ? results_ref.u[idx] : 0); + // double cy = cy_img0 + (img_num_ref > 0 ? results_ref.v[idx] : 0); + // + // // Re-fill reference + // opt.copy_params_from_neigh(results_ref.p, idx * conf.num_params); + // subset::fill_from_shape_params(ss_ref, cx_img0, cy_img0, opt.p, interp_ref, conf.shape_func); + // + // // Use best neighbor's converged parameters as the new starting point + // opt.copy_params_from_neigh(results_def.p, best_neigh * conf.num_params); + // + // OptResult retry_res = opt.solve(cx, cy, ss_ref, ss_def, interp_def, true); + // + // // If retry is better than original attempt, update results + // if (retry_res.cost > results_def.cost[idx]) { + // std::vector pA(conf.num_params); + // std::vector pB = retry_res.p; + // std::vector pC(conf.num_params); + // + // // pA: k0 -> k_ref + // std::copy(results_ref.p.begin() + idx*conf.num_params, + // results_ref.p.begin() + idx*conf.num_params + conf.num_params, + // pA.begin()); + // + // if (conf.shape_func == "RIGID") { + // Rigid::compose(pC, pA, pB); + // Rigid::get_displacement(retry_res.u, retry_res.v, 0.0, 0.0, pC); + // } + // else if (conf.shape_func == "AFFINE"){ + // Affine::compose(pC, pA, pB); + // Affine::get_displacement(retry_res.u, retry_res.v, 0.0, 0.0, pC); + // } + // else if (conf.shape_func == "QUAD") { + // Quad::compose(pC, pA, pB); + // Quad::get_displacement(retry_res.u, retry_res.v, 0.0, 0.0, pC); + // } + // retry_res.p = pC; + // + // results_def.append(retry_res, idx); + // } + // } + // } + // } + + if (g_debug_level>0){ + pbar.finish(); + } + + if (error_flag.load()) { + throw std::runtime_error(error_message); + } +} diff --git a/src/pyvale/dic/cpp/dicsinglewindow_rg.hpp b/src/pyvale/dic/cpp/dicsinglewindow_rg.hpp new file mode 100644 index 000000000..fc6ed3ca4 --- /dev/null +++ b/src/pyvale/dic/cpp/dicsinglewindow_rg.hpp @@ -0,0 +1,41 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + +#ifndef DICSINGLEWINDOW_RG_H +#define DICSINGLEWINDOW_RG_H + +// STD library Header files +#include +#include + +// common_cpp headers + +// Eigen Header files +#include + +// Program Header files +#include "./dicinterp.hpp" +#include "./dicutil.hpp" +#include "./dicsubset.hpp" +#include "./dicresults.hpp" + +/** +* @brief reliability guided scan method with incremental updating. +*/ +void singlewindow_rg(const Interpolator &interp_ref, + const Interpolator &interp_def, + const subset::Grid &ss_grid, + const util::Config &conf, + const int img_num_ref, + const int img_num_def, + const ResultArrays &results_ref, + ResultArrays &results_def, + const std::string &mode="temporal", + const std::optional &F=std::nullopt, + const std::optional &results_def_l=std::nullopt); + + +#endif //DICSINGLEWINDOW_RG_H diff --git a/src/pyvale/dic/cpp/dicsubset.cpp b/src/pyvale/dic/cpp/dicsubset.cpp index 828cf40ce..9ba4b8239 100644 --- a/src/pyvale/dic/cpp/dicsubset.cpp +++ b/src/pyvale/dic/cpp/dicsubset.cpp @@ -12,61 +12,97 @@ #include "./dicsubset.hpp" #include "./dicshapefunc.hpp" - +// common_cpp header files +#include "../../common_cpp/util.hpp" namespace subset { - void get_px_from_img(subset::Pixels &ss_ref, + void fill_from_img(subset::Pixels &ss_ref, const int ss_x, const int ss_y, const int px_hori, const int px_vert, - const double *img_def){ + const Image &img){ + + switch (img.type) { + case PixelType::UINT8: fill_impl(ss_ref, img.data8, ss_x, ss_y, px_hori); break; + case PixelType::UINT16: fill_impl(ss_ref, img.data16, ss_x, ss_y, px_hori); break; + case PixelType::UINT32: fill_impl(ss_ref, img.data32, ss_x, ss_y, px_hori); break; + } + } + + template + void fill_impl(subset::Pixels &ss_ref, + const std::vector &data, + int ss_x, int ss_y, + int px_hori) { int count = 0; - int idx; + ss_ref.sum = 0.0; + + for (int y = ss_y; y < ss_y + ss_ref.size_y; ++y) { + for (int x = ss_x; x < ss_x + ss_ref.size_x; ++x) { + int idx = y * px_hori + x; + if (ss_ref.has_coords()) { + ss_ref.x[count] = x; + ss_ref.y[count] = y; + } + ss_ref.vals[count] = data[idx]; + ss_ref.sum += data[idx]; + count++; + } + } + } - for (int px_y = ss_y; px_y < ss_y+ss_ref.size; px_y++){ - for (int px_x = ss_x; px_x < ss_x+ss_ref.size; px_x++){ + double zncc(const subset::Pixels &ss_ref, const subset::Pixels &ss_def) { + double mean_ref = 0.0; + double mean_def = 0.0; - // get coordinate values - ss_ref.x[count] = px_x; - ss_ref.y[count] = px_y; + for (int i = 0; i < ss_ref.num_px; ++i) { + mean_ref += ss_ref.vals[i]; + mean_def += ss_def.vals[i]; + } - // get pixel values - idx = px_y * px_hori + px_x; - ss_ref.vals[count] = img_def[idx]; - count++; + mean_ref /= ss_ref.num_px; + mean_def /= ss_def.num_px; - // debugging - //std::cout << px_x << " " << px_y << " "; - //std::cout << img_def[idx] << std::endl; - } + double sum_squared_ref = 0.0; + double sum_squared_def = 0.0; + + for (int i = 0; i < ss_ref.num_px; ++i) { + sum_squared_ref += (ss_ref.vals[i] - mean_ref) * (ss_ref.vals[i] - mean_ref); + sum_squared_def += (ss_def.vals[i] - mean_def) * (ss_def.vals[i] - mean_def); } + + const double inv_sum_squared = 1.0 / std::sqrt(sum_squared_ref * sum_squared_def); + + double zncc = 0.0; + for (int i = 0; i < ss_ref.num_px; ++i) { + const double def_norm = (ss_def.vals[i] - mean_def); + const double ref_norm = (ss_ref.vals[i] - mean_ref); + zncc += ref_norm * def_norm; + } + + return zncc * inv_sum_squared; } - void get_subpx_from_img(subset::Pixels &ss_def, + void fill_from_img_subpx(subset::Pixels &ss_def, const double subpx_x, const double subpx_y, const Interpolator &interp_def){ int count = 0; - for (int y = 0; y < ss_def.size; y++){ - for (int x = 0; x < ss_def.size; x++){ - if (count >= ss_def.size*ss_def.size){ - std::cerr << "issue with count for subpixel subset population" << std::endl; - std::cerr << "count: " << count << std::endl; - std::cerr << "subset size: " << ss_def.size << std::endl; - std::cerr << "num px (size*size): " << ss_def.size*ss_def.size << std::endl; - std::cerr << "subpixel value: " << subpx_x+x << " " << subpx_y+y << std::endl; - std::cerr << "subset coordinates: " << " " << subpx_x << " " << subpx_y << " " << std::endl; - exit(EXIT_FAILURE); - } + for (int y = 0; y < ss_def.size_y; y++){ + for (int x = 0; x < ss_def.size_x; x++){ // get coordinate values - ss_def.x[count] = subpx_x+x; - ss_def.y[count] = subpx_y+y; + const double px_x = subpx_x + x; + const double px_y = subpx_y + y; + if (ss_def.has_coords()) { + ss_def.x[count] = px_x; + ss_def.y[count] = px_y; + } // get pixel values - ss_def.vals[count] = interp_def.eval(0, 0, ss_def.x[count], ss_def.y[count]); + ss_def.vals[count] = interp_def.eval(0, 0, px_x, px_y); // debugging //std::cout << ss_def.x[count] << " " << ss_def.y[count] << " " << ss_def.vals[count] << std::endl; @@ -74,53 +110,85 @@ namespace subset { count++; } } - if (count!=ss_def.size*ss_def.size){ - std::cerr << "count for subpixel population is not the same as the number of subset pixels."; - std::cout << "count: " << count << std::endl; - std::cerr << "number of pixels: " << ss_def.size*ss_def.size << std::endl; - exit(EXIT_FAILURE); - } } - void get_subpx_from_shape_params(subset::Pixels &ss_def, - const double subpx_x, const double subpx_y, + void fill_from_shape_params(subset::Pixels &ss, + const double cx, const double cy, const std::vector& p, - const Interpolator &interp_def){ + const Interpolator &interp, + util::ShapeFunc shape_func){ + + // Get the right shape function + void (*get_pixel)(double&, double&, const double, const double, const std::vector&); + switch (shape_func) { + case util::ShapeFunc::AFFINE: + get_pixel = &Affine::get_pixel; + break; + case util::ShapeFunc::RIGID: + get_pixel = &Rigid::get_pixel; + break; + case util::ShapeFunc::QUAD: + get_pixel = &Quad::get_pixel; + break; + } - int count = 0; - for (int y = 0; y < ss_def.size; y++){ - for (int x = 0; x < ss_def.size; x++){ - if (count >= ss_def.size*ss_def.size){ - std::cerr << "issue with count for subpixel subset population" << std::endl; - std::cerr << "count: " << count << std::endl; - std::cerr << "subset size: " << ss_def.size << std::endl; - std::cerr << "num px (size*size): " << ss_def.size*ss_def.size << std::endl; - std::cerr << "subpixel value: " << subpx_x+x << " " << subpx_y+y << std::endl; - std::cerr << "subset coordinates: " << " " << subpx_x << " " << subpx_y << " " << std::endl; - exit(EXIT_FAILURE); + // NOTE: Assuming an odd number subset size + const double half_x = (ss.size_x - 1) / 2.0; + const double half_y = (ss.size_y - 1) / 2.0; + + int count = 0; + ss.sum = 0.0; + for (int y = 0; y < ss.size_y; y++){ + const double rel_y = y - half_y; + for (int x = 0; x < ss.size_x; x++){ + double px_x = 0.0; + double px_y = 0.0; + get_pixel(px_x, px_y, x - half_x, rel_y, p); + px_x += cx; + px_y += cy; + if (ss.has_coords()) { + ss.x[count] = px_x; + ss.y[count] = px_y; } + ss.vals[count] = interp.eval(cx, cy, px_x, px_y); + ss.sum += ss.vals[count]; + count++; + } + } + } - // get coordinate values based on shape function parameters - shapefunc::get_pixel(ss_def.x[count], ss_def.y[count], subpx_x+x, subpx_y+y, p); - - // get pixel values from interpolator - ss_def.vals[count] = interp_def.eval(0, 0, ss_def.x[count], ss_def.y[count]); + void fill_from_centre_coords(subset::Pixels &ss_def, + const double cx, const double cy, + const Interpolator &interp_def) { + // NOTE: Assuming an odd number subset size + const double half_x = (ss_def.size_x - 1) / 2.0; + const double half_y = (ss_def.size_y - 1) / 2.0; + + int count = 0; + ss_def.sum = 0.0; + for (int y = 0; y < ss_def.size_y; y++) { + for (int x = 0; x < ss_def.size_x; x++) { + const double px_x = cx + x - half_x; + const double px_y = cy + y - half_y; + if (ss_def.has_coords()) { + ss_def.x[count] = px_x; + ss_def.y[count] = px_y; + } + ss_def.vals[count] = interp_def.eval(cx, cy, + px_x, + px_y); + ss_def.sum += ss_def.vals[count]; count++; } } - if (count!=ss_def.size*ss_def.size){ - std::cerr << "count for subpixel population is not the same as the number of subset pixels."; - std::cout << "count: " << count << std::endl; - std::cerr << "number of pixels: " << ss_def.size*ss_def.size << std::endl; - exit(EXIT_FAILURE); - } } - subset::Grid create_grid(const bool *img_roi, const int ss_step, - const int ss_size, const int px_hori, - const int px_vert, const bool partial) { + subset::Grid create_grid(const bool *img_roi, const int ss_step, + const int ss_size_x, const int ss_size_y, + const int px_hori, const int px_vert, + const bool partial) { //Timer timer("subset grid generation for subset size " + std::to_string(ss_size) + " [px] with step " + std::to_string(ss_step) + " [px]:" ); @@ -139,7 +207,8 @@ namespace subset { ss_grid.num_in_mask = num_ss_x * num_ss_y; ss_grid.num = 0; ss_grid.step = ss_step; - ss_grid.size = ss_size; + ss_grid.size_x = ss_size_x; + ss_grid.size_y = ss_size_y; ss_grid.mask.resize(ss_grid.num_in_mask, -1); ss_grid.coords.resize(2*ss_grid.num_in_mask, -1); @@ -159,8 +228,8 @@ namespace subset { // pixel range of subset const int xmin = ss_x; const int ymin = ss_y; - const int xmax = ss_x + ss_size-1; - const int ymax = ss_y + ss_size-1; + const int xmax = ss_x + ss_size_x-1; + const int ymax = ss_y + ss_size_y-1; bool valid = true; int valid_count = 0; @@ -186,7 +255,7 @@ namespace subset { } if (partial && valid) { - valid = (valid_count >= (ss_size * ss_size) * 0.70); + valid = (valid_count >= (ss_size_x * ss_size_y) * 0.70); } if (valid) { @@ -204,6 +273,8 @@ namespace subset { int total_valid = thread_offsets.back() + thread_counts.back(); ss_grid.coords.resize(2 * total_valid); ss_grid.num = total_valid; + ss_grid.active_ss.resize(total_valid, true); + ss_grid.active_total = total_valid; // Reset thread counts to use as writing indices std::fill(thread_counts.begin(), thread_counts.end(), 0); @@ -219,8 +290,8 @@ namespace subset { // pixel range of subset const int xmin = ss_x; const int ymin = ss_y; - const int xmax = ss_x + ss_size-1; - const int ymax = ss_y + ss_size-1; + const int xmax = ss_x + ss_size_x-1; + const int ymax = ss_y + ss_size_y-1; // check if subset is within image and ROI. bool valid = true; @@ -253,15 +324,15 @@ namespace subset { } if (partial && valid) { - valid = (valid_count >= (ss_size * ss_size) * 0.70); + valid = (valid_count >= (ss_size_x * ss_size_y) * 0.70); } // if its a valid subset. add it to a list of coordinates if (valid) { const int tid = omp_get_thread_num(); const int offset = thread_offsets[tid] + thread_counts[tid]; - ss_grid.coords[2*offset] = ss_x; - ss_grid.coords[2*offset + 1] = ss_y; + ss_grid.coords[2*offset] = ss_x + static_cast(ss_size_x)/2-0.5; + ss_grid.coords[2*offset + 1] = ss_y + static_cast(ss_size_y)/2-0.5; ss_grid.mask[j * num_ss_x + i] = offset; thread_counts[tid]++; } @@ -301,25 +372,4 @@ namespace subset { return ss_grid; } - - inline bool px_in_img_dims(const int px_x, const int px_y, const int px_hori, - const int px_vert) { - - if (px_x < 0 || px_y < 0 || - px_x >= px_hori || px_y >= px_vert) { - return false; - } - return true; - } - - inline bool px_in_roi(const int px_x, const int px_y, const int px_hori, - const int px_vert, const bool *img_roi) { - - int idx = px_y * px_hori + px_x; - if (!img_roi[idx]) { - return false; - } - return true; - } - } diff --git a/src/pyvale/dic/cpp/dicsubset.hpp b/src/pyvale/dic/cpp/dicsubset.hpp index dc792cc49..5f577de84 100644 --- a/src/pyvale/dic/cpp/dicsubset.hpp +++ b/src/pyvale/dic/cpp/dicsubset.hpp @@ -9,22 +9,31 @@ // STD library Header files #include +#include +#include // Program Header files #include "./dicinterp.hpp" +#include "./dicutil.hpp" + +// common_cpp header files +#include "../../common_cpp/util.hpp" namespace subset { struct Grid { int num; int step; - int size; + int size_x; + int size_y; int num_ss_x; int num_ss_y; int num_in_mask; - std::vector coords; + std::vector coords; std::vector mask; std::vector> neigh; + std::vector active_ss; + int active_total; }; /** @@ -36,17 +45,22 @@ namespace subset { std::vector vals; std::vector x; std::vector y; - int size; + int size_x; + int size_y; int num_px; + int sum; // Constructor to initialize the vectors with ss_size - Pixels(int ss_size) - : vals(ss_size * ss_size, 0.0), - x(ss_size * ss_size, 0.0), - y(ss_size * ss_size, 0.0), - size(ss_size), - num_px(ss_size * ss_size) + Pixels(int ss_size_x, int ss_size_y, bool store_coords = true) + : vals(ss_size_x * ss_size_y, 0.0), + x(store_coords ? ss_size_x * ss_size_y : 0, 0.0), + y(store_coords ? ss_size_x * ss_size_y : 0, 0.0), + size_x(ss_size_x), + size_y(ss_size_y), + num_px(ss_size_x * ss_size_y) {} + + bool has_coords() const { return !x.empty() && !y.empty(); } }; /** @@ -57,18 +71,39 @@ namespace subset { * subset is determined by `ss_def->size`. Both the pixel values and their corresponding * coordinates are stored in `ss_def`. * - * @param ss_x X-coordinate (column) of the top-left corner of the subset in the image. - * @param ss_y Y-coordinate (row) of the top-left corner of the subset in the image. + * @param ss_x X-coordinate (column) of the TOP-LEFT CORNER of the subset in the image. + * @param ss_y Y-coordinate (row) of the TOP-LEFT CORNER of the subset in the image. * @param img_def Pointer to the source image (`util::Image`) from which to extract pixel data. * @param ss_def Pointer to the destination subset (`subset::Pixels`) where extracted pixel * values and coordinates are stored. */ - void get_px_from_img(subset::Pixels &ss_ref, + void fill_from_img(subset::Pixels &ss_ref, const int ss_x, const int ss_y, const int px_hori, const int px_vert, - const double *img_def); + const Image &img); + + template + void fill_impl(subset::Pixels &ss_ref, + const std::vector &data, + int ss_x, int ss_y, + int px_hori); + /** + * @brief Fills a subset of pixels from an image using centre coordinates. + * + * Samples pixel values from the interpolator over a grid centred at (cx, cy). + * Equivalent to fill_from_img_subpx but takes centre coordinates rather than + * the top-left corner, and stores both pixel coordinates and values in ss_def. + * + * @param ss_def Destination subset where pixel coordinates and values are stored. + * @param cx X-coordinate of the CENTRE of the subset in the image. + * @param cy Y-coordinate of the CENTRE of the subset in the image. + * @param interp_def Interpolator for the image from which to sample pixel data. + */ + void fill_from_centre_coords(subset::Pixels &ss_def, + const double cx, const double cy, + const Interpolator &interp_def); /** * @brief Extracts a square subset of pixels from an image and stores the data in a Subset object. @@ -79,22 +114,32 @@ namespace subset { * coordinates are stored in `ss_def`. * * @param ss_ref Pointer to the destination subset (`subset::Pixels`) where extracted pixel info will be stored - * @param ss_x X-coordinate (column) of the top-left corner of the subset in the image. - * @param ss_y Y-coordinate (row) of the top-left corner of the subset in the image. + * @param ss_x X-coordinate (column) of the TOP-LEFT CORNER of the subset in the image. + * @param ss_y Y-coordinate (row) of the TOP-LEFT CORNER of the subset in the image. * @param interp_ref interpolator for the reference image from which to extract pixel data. */ - void get_subpx_from_img(subset::Pixels &ss_def, + void fill_from_img_subpx(subset::Pixels &ss_def, const double subpx_x, const double subpx_y, const Interpolator &interp_def); /** - * - */ - void get_subpx_from_shape_params(subset::Pixels &ss_def, - const double subpx_x, const double subpx_y, + * @brief Populates a deformed subset with interpolated image values using shape function parameters. + * + * Applies the shape function to map reference subset coordinates (centred at cx, cy) + * to deformed image coordinates, then interpolates the image intensity at each mapped location. + * + * @param ss_def Output subset to populate with deformed coordinates and interpolated values. + * @param cx Global x-coordinate of the SUBSET CENTRE in the reference image. + * @param cy Global y-coordinate of the SUBSET CENTRE in the reference image. + * @param p Shape function parameters (e.g. displacement, strain components). + * @param interp_def Interpolator for the deformed image. + * @param shape_func Shape function type enum. + */ + void fill_from_shape_params(subset::Pixels &ss_def, + const double cx, const double cy, const std::vector& p, - const Interpolator &interp_def); - + const Interpolator &interp_def, + util::ShapeFunc shape_func); /** * @brief Generates a list of subsets based on the provided image ROI and parameters. * @@ -109,14 +154,74 @@ namespace subset { * @param ss_step Step size for generating subsets. * @return A subset::Grid object containing the generated subsets and their neighbours. */ - subset::Grid create_grid(const bool *img_roi, const int ss_step, const int ss_size, - const int px_hori, const int px_vert, const bool partial=false); + subset::Grid create_grid(const bool *img_roi, const int ss_step, + const int ss_size_x, const int ss_size_y, + const int px_hori, const int px_vert, + const bool partial); - inline bool px_in_img_dims(const int px_x, const int px_y, const int px_hori, - const int px_vert); + static inline bool px_in_img_dims(const int px_x, const int px_y, const int px_hori, const int px_vert) { + + if (px_x < 0 || px_y < 0 || + px_x >= px_hori || + px_y >= px_vert) { + return false; + } + return true; + } + + static inline bool px_in_roi(const int px_x, const int px_y, const int px_hori, + const int px_vert, const bool *img_roi) { + + int idx = px_y * px_hori + px_x; + if (!img_roi[idx]) { + return false; + } + return true; + } + + + + /** + * @brief Returns central subset coordinates for given subset corner values + * and dimensions + * + * @param cx[out] x pixel coordinate of subset centre + * @param cy[out] y pixel coordinate of subset centre + * @param ss_x[in] x pixel coordinate of subset corner + * @param ss_y[in] y pixel coordinate of subset corner + * @param size_x[in] subset size in x + * @param size_y[in] subset size in y + */ + static inline void get_centre(double& cx, double& cy, + const double ss_x, const double ss_y, + const int size_x, const int size_y) { + + cx = ss_x + static_cast(size_x) * 0.5 - 0.5; + cy = ss_y + static_cast(size_y) * 0.5 - 0.5; + + } + + /** + * @brief Returns corner subset coordinates for given subset centre values + * and dimensions + * + * @param cx[out] x pixel coordinate of subset centre + * @param cy[out] y pixel coordinate of subset centre + * @param ss_x[in] x pixel coordinate of subset corner + * @param ss_y[in] y pixel coordinate of subset corner + * @param size_x[in] subset size in x + * @param size_y[in] subset size in y + */ + static inline void get_corner(double& corner_x, double& corner_y, + const double cx, const double cy, + const int size_x, const int size_y) { + corner_x = cx - static_cast(size_x) * 0.5 + 0.5; + corner_y = cy - static_cast(size_y) * 0.5 + 0.5; + } + - inline bool px_in_roi(const int px_x, const int px_y, const int px_hori, - const int px_vert, const bool *img_roi); + // Compute ZNCC between two subsets + double zncc(const subset::Pixels& ss_ref, const subset::Pixels& ss_def); } -#endif // DICSMOOTH_H +#endif // DICSUBSET_H diff --git a/src/pyvale/dic/cpp/dicutil.cpp b/src/pyvale/dic/cpp/dicutil.cpp index cbb42a4a7..f081efd99 100644 --- a/src/pyvale/dic/cpp/dicutil.cpp +++ b/src/pyvale/dic/cpp/dicutil.cpp @@ -25,17 +25,6 @@ namespace util { - std::vector niter_arr; - std::vector u_arr; - std::vector v_arr; - std::vector p_arr; - std::vector ftol_arr; - std::vector xtol_arr; - std::vector cost_arr; - std::vector conv_arr; - bool at_end; - - void extract_image(double *img_def_stack, int image_number, diff --git a/src/pyvale/dic/cpp/dicutil.hpp b/src/pyvale/dic/cpp/dicutil.hpp index e0ca2ac94..98ff69203 100644 --- a/src/pyvale/dic/cpp/dicutil.hpp +++ b/src/pyvale/dic/cpp/dicutil.hpp @@ -11,6 +11,7 @@ // STD library Header files #include #include +#include // common_cpp header files @@ -19,6 +20,41 @@ namespace util { + enum class CorrCrit { + SSD, + NSSD, + ZNSSD + }; + + enum class ShapeFunc { + RIGID, + AFFINE, + QUAD + }; + + enum class InterpRoutine { + BSPLINE, + HERMITE + }; + + enum class ScanMethod { + MULTIWINDOW_RG, + SINGLEWINDOW_RG, + MULTIWINDOW, + RASTER + }; + + + enum class IncrementalCond { + IMAGE, + ITER, + COST + }; + + enum class FFTPrecision { + FLOAT32, + FLOAT64 + }; // Custom hash from above struct PairHash { @@ -39,17 +75,29 @@ namespace util { int num_params; double precision; double threshold; - double bf_threshold; int max_disp; - std::pair rg_seed; - std::string corr_crit; - std::string shape_func; - std::string interp_routine; - std::string scan_method; - std::vector filenames; - bool fft_mad; - double fft_mad_scale; + int epi_distance; + std::vector rg_seeds; + CorrCrit corr_crit; + ShapeFunc shape_func; + InterpRoutine interp_routine; + ScanMethod scan_method; + std::vector basenames; + std::vector fullpaths; + bool fft_filter; + bool fft_save; + FFTPrecision fft_precision; + double fft_filter_threshold; + int fft_filter_radius; + double fft_filter_corr_power; unsigned int debug_level; + bool stereo; + bool incremental; + IncrementalCond incremental_update_cond; + double incremental_update_val; + int multiwindow_overlap; + std::vector multiwindow_subset_size; + std::vector multiwindow_search_area; }; diff --git a/src/pyvale/dic/cpp/stereomatching.cpp b/src/pyvale/dic/cpp/stereomatching.cpp new file mode 100644 index 000000000..841b8d18f --- /dev/null +++ b/src/pyvale/dic/cpp/stereomatching.cpp @@ -0,0 +1,445 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + +// STD library Header files +#include +#include +#include +#include + +// common_cpp header files +#include "../../common_cpp/progressbar.hpp" +#include "../../common_cpp/dicsignalhandler.hpp" + +// calibration header files + +// dic header files +#include "./stereomatching.hpp" +#include "./stereoutil.hpp" +#include "./dicsubset.hpp" +#include "./dicoptimizer.hpp" +#include "./dicinterp.hpp" +#include "./dicrg.hpp" +#include "./dicshapefunc.hpp" + + +// Eigen +#include + +namespace stereo { + + +void matching(const Image &img_l, + const Image &img_r, + const Interpolator &interp_l, + const Interpolator &interp_r, + const subset::Grid &ss_grid, + const util::Config &conf, + const int img_num_def_l, + const int img_num_def_r, + const Eigen::Matrix3d &F, + const ResultArrays &results_l, + ResultArrays &results_r){ + + + + + // assign some consts for readability + const int px_hori = conf.px_hori; + const int px_vert = conf.px_vert; + const int num_ss = ss_grid.num; + const int ss_size_x = ss_grid.size_x; + const int ss_size_y = ss_grid.size_y; + const int ss_step = ss_grid.step; + + + auto get_initial_guess = [&](std::vector &p, double cx, double cy, bool print) { + stereo::get_rigid_translation_from_rectified_fft(p, cx, cy, ss_size_x, ss_size_y, + 2*conf.max_disp, ss_size_y, F, + interp_l, interp_r, 0.0, 0.0, print); + }; + + std::string bar_title = "Stereo \033[1;4m" + conf.basenames[img_num_def_l] + + "\033[0m -> \033[1;4m" + conf.basenames[img_num_def_r] + + "\033[0m:"; + + ProgressBar pbar(bar_title, ss_grid.active_total); + std::atomic current_progress(0); + + // Initialize binary mask for computed points (initialized to 0) + std::vector> computed_mask(ss_grid.mask.size()); + for (auto& val : computed_mask) val.store(0); + + // queue for each thread + rg::QueueGlobal queue(omp_get_max_threads()); + + std::atomic error_flag(false); + std::string error_message; + + # pragma omp parallel + { + + int tid = omp_get_thread_num(); + + // Initialize ref and def subsets + subset::Pixels ss_r(ss_size_x, ss_size_y); + subset::Pixels ss_l(ss_size_x, ss_size_y); + + // Optimization parameters + Optimizer opt(conf.shape_func, conf.corr_crit, conf.max_iter, conf.precision, conf.threshold, ss_size_x*ss_size_y); + + // TODO: opt.seed_iter exposed to user. + opt.max_iter = 200; + + // --------------------------------------------------------------------------------------------------------------------------- + // PROCESS THE SEED SUBSET + // --------------------------------------------------------------------------------------------------------------------------- + if (tid == 0) { + + + // num seeds + int num_seeds = conf.rg_seeds.size() / 2; + + for (int s = 0; s < num_seeds; s++){ + + int seed_x = conf.rg_seeds[2*s]; + int seed_y = conf.rg_seeds[2*s+1]; + + // seed coordinates + int grid_x = seed_x / ss_step; + int grid_y = seed_y / ss_step; + int idx = ss_grid.mask[grid_y * ss_grid.num_ss_x + grid_x]; + + // get the centre coordinates for the subset in img k0 + double cx_ref_l = ss_grid.coords[2*idx]; + double cy_ref_l = ss_grid.coords[2*idx+1]; + + // get the centre coordinates for the subset in img k + double cx_def_l = cx_ref_l + results_l.u[idx]; + double cy_def_l = cy_ref_l + results_l.v[idx]; + + // populate the subset for img k using shape function parameters + // that map subset in img k0 to k. + opt.copy_params_from_neigh(results_l.p, idx); + subset::fill_from_shape_params(ss_l, cx_ref_l, cy_ref_l, opt.p, interp_l, conf.shape_func); + + // if the first image. Take the optimization parameters from rigid fourier + get_initial_guess(opt.p, cx_def_l, cy_def_l, false); + + // run optimizer + OptResult seed_res = opt.solve(cx_def_l, cy_def_l, ss_l, ss_r, interp_r, true); + + if (!rg::check_convergence(cx_ref_l, cy_ref_l, seed_res, error_message)) { + error_flag.store(true); + break; + } + + // add deformation from reference image to new results + if (img_num_def_l > 0){ + std::vector pA(conf.num_params); + std::vector pB = seed_res.p; + std::vector pC(conf.num_params); + + + // pA: k0 -> k_l + std::copy(results_l.p.begin() + idx*conf.num_params, + results_l.p.begin() + idx*conf.num_params + conf.num_params, + pA.begin()); + + if (conf.shape_func == util::ShapeFunc::RIGID) { + Rigid::compose(pC, pA, pB); + Rigid::get_displacement(seed_res.u, seed_res.v, 0.0, 0.0, pC); + } + else if (conf.shape_func == util::ShapeFunc::AFFINE){ + Affine::compose(pC, pA, pB); + Affine::get_displacement(seed_res.u, seed_res.v, 0.0, 0.0, pC); + } + else if (conf.shape_func == util::ShapeFunc::QUAD) { + Quad::compose(pC, pA, pB); + Quad::get_displacement(seed_res.u, seed_res.v, 0.0, 0.0, pC); + } + seed_res.p = pC; + } + + // append the results for the current subset to result vectors + results_r.append(seed_res, idx); + + // mark subset as computed + computed_mask[idx].store(1); + + // loop over the neighbours for the initial seed point + for (size_t n = 0; n < ss_grid.neigh[idx].size(); n++) { + + // subset index of neighbour to the current point + int nidx = ss_grid.neigh[idx][n]; + + double cx_ref_l = ss_grid.coords[nidx*2]; + double cy_ref_l = ss_grid.coords[nidx*2+1]; + + double cx_def_l = cx_ref_l + results_l.u[nidx]; + double cy_def_l = cy_ref_l + results_l.v[nidx]; + + // fill the reference subset using the updated cx,cy and + // the shape function parameters for the correlation of + // the reference image + opt.copy_params_from_neigh(results_l.p, nidx); + subset::fill_from_shape_params(ss_l, cx_ref_l, cy_ref_l, opt.p, interp_l, conf.shape_func); + + // perform optimization for seed point neighbours + opt.copy_params_from_neigh(results_r.p, idx); + + OptResult nres = opt.solve(cx_def_l, cy_def_l, ss_l, ss_r, interp_r, true); + + if (!rg::check_convergence(cx_def_l, cy_def_l, nres, error_message, true)) { + error_flag.store(true); + break; + } + + // add deformation from reference image to new results + std::vector pA(conf.num_params); + std::vector pB = nres.p; + std::vector pC(conf.num_params); + + + // pA: k0 -> k_l + std::copy(results_l.p.begin() + nidx*conf.num_params, + results_l.p.begin() + nidx*conf.num_params + conf.num_params, + pA.begin()); + + if (conf.shape_func == util::ShapeFunc::RIGID) { + Rigid::compose(pC, pA, pB); + Rigid::get_displacement(nres.u, nres.v, 0.0, 0.0, pC); + } + else if (conf.shape_func == util::ShapeFunc::AFFINE){ + Affine::compose(pC, pA, pB); + Affine::get_displacement(nres.u, nres.v, 0.0, 0.0, pC); + } + else if (conf.shape_func == util::ShapeFunc::QUAD) { + Quad::compose(pC, pA, pB); + Quad::get_displacement(nres.u, nres.v, 0.0, 0.0, pC); + } + nres.p = pC; + + + // append the results for the current subset to result vectors + results_r.append(nres, nidx); + + // update mask + computed_mask[nidx].store(1); + + // add this point to queue + queue.push(tid, {rg::Point(nidx,nres.cost)}); + + // update progress bar + if (g_debug_level>0){ + int progress = current_progress.fetch_add(1); + if (omp_get_thread_num()==0) pbar.update(progress+1); + } + } + } + } + + // --------------------------------------------------------------------------------------------------------------------------- + // PROCESS ALL OTHER SUBSETS + // --------------------------------------------------------------------------------------------------------------------------- + #pragma omp barrier + + // reset seed location using the last computed point + opt.max_iter = conf.max_iter; + + std::vector temp_neigh; + temp_neigh.reserve(4); + + rg::Point current(0, 0); + + while (!stop_request && !error_flag.load()) { + + if (!queue.pop(tid, current)) + break; + + temp_neigh.clear(); + + + // loop over neighbouring points + for (size_t n = 0; n < ss_grid.neigh[current.idx].size(); n++) { + + // subset index of neighbour to the current point + int nidx = ss_grid.neigh[current.idx][n]; + + int expected = 0; + expected = computed_mask[nidx].exchange(1); + if (expected == 0) { + + // coords of neigh + double cx_ref_l = ss_grid.coords[nidx*2]; + double cy_ref_l = ss_grid.coords[nidx*2+1]; + + // add displacements from base to subset coords in ref_l + double cx_def_l = cx_ref_l + results_l.u[nidx]; + double cy_def_l = cy_ref_l + results_l.v[nidx]; + + // fill the reference subset using the updated cx,cy and + // the shape function parameters for the correlation of + // the reference image + opt.copy_params_from_neigh(results_l.p, nidx); + subset::fill_from_shape_params(ss_l, cx_ref_l, cy_ref_l, opt.p, interp_l, conf.shape_func); + + // if the neighbouring subset had not met correlation threshold then try values from fft windowing + if (results_r.above_thresh[current.idx]) + opt.copy_params_from_neigh(results_r.p, current.idx); + else + get_initial_guess(opt.p, cx_def_l, cy_def_l, false); + + // optimize + OptResult nres = opt.solve(cx_def_l, cy_def_l, ss_l, ss_r, interp_r); + + // add deformation from reference image to new results + if (nres.above_thresh){ + std::vector pA(conf.num_params); + std::vector pB = nres.p; + std::vector pC(conf.num_params); + + // pA: k0 -> k_l + std::copy(results_l.p.begin() + nidx*conf.num_params, + results_l.p.begin() + nidx*conf.num_params + conf.num_params, + pA.begin()); + + if (conf.shape_func == util::ShapeFunc::RIGID) { + Rigid::compose(pC, pA, pB); + Rigid::get_displacement(nres.u, nres.v, 0.0, 0.0, pC); + } + else if (conf.shape_func == util::ShapeFunc::AFFINE){ + Affine::compose(pC, pA, pB); + Affine::get_displacement(nres.u, nres.v, 0.0, 0.0, pC); + } + else if (conf.shape_func == util::ShapeFunc::QUAD) { + Quad::compose(pC, pA, pB); + Quad::get_displacement(nres.u, nres.v, 0.0, 0.0, pC); + } + nres.p = pC; + } + + // append results + results_r.append(nres, nidx); + + // add results to temp neighbour results + temp_neigh.emplace_back(nidx, nres.cost); + + // update progress bar + if (g_debug_level>0){ + int progress = current_progress.fetch_add(1); + if (tid==0) pbar.update(progress+1); + } + } + } + + queue.push(tid, temp_neigh); + } + } + + // // TODO: build a list of subsets using openmp that have correlated poorly but have atleast + // // one immediate neighbour above_threshold + // std::vector retry_indices; + // #pragma omp parallel + // { + // std::vector local_retry; + // #pragma omp for nowait + // for (int i = 0; i < num_ss; ++i) { + // // If the point was reached but failed threshold + // if (computed_mask[i].load() == 1 && !results_r.above_thresh[i]) { + // // Check if any neighbor was successful + // for (int nidx : ss_grid.neigh[i]) { + // if (results_r.above_thresh[nidx]) { + // local_retry.push_back(i); + // break; + // } + // } + // } + // } + // #pragma omp critical + // retry_indices.insert(retry_indices.end(), local_retry.begin(), local_retry.end()); + // } + // + // // TODO: retry the correlation using the parameters for the neighbour that + // // have successfuly correlated. use openmp + // + // #pragma omp parallel + // { + // subset::Pixels ss_r(ss_size_x, ss_size_y); + // subset::Pixels ss_l(ss_size_x, ss_size_y); + // Optimizer opt(conf.shape_func, conf.corr_crit, conf.max_iter, conf.precision, conf.threshold, ss_size_x*ss_size_y); + // + // #pragma omp for + // for (int i = 0; i < (int)retry_indices.size(); ++i) { + // int idx = retry_indices[i]; + // + // // Find the neighbor with the lowest cost (best match) to use as new guess + // int best_neigh = -1; + // double best_cost = -std::numeric_limits::max(); + // for (int nidx : ss_grid.neigh[idx]) { + // if (results_r.above_thresh[nidx] && results_r.cost[nidx] > best_cost) { + // best_cost = results_r.cost[nidx]; + // best_neigh = nidx; + // } + // } + // + // if (best_neigh != -1) { + // double cx_ref_l = ss_grid.coords[idx*2]; + // double cy_ref_l = ss_grid.coords[idx*2+1]; + // double cx = cx_ref_l + (img_num_l > 0 ? results_l.u[idx] : 0); + // double cy = cy_ref_l + (img_num_l > 0 ? results_l.v[idx] : 0); + // + // // Re-fill reference + // opt.copy_params_from_neigh(results_l.p, idx * conf.num_params); + // subset::fill_from_shape_params(ss_l, cx_ref_l, cy_ref_l, opt.p, interp_l, conf.shape_func); + // + // // Use best neighbor's converged parameters as the new starting point + // opt.copy_params_from_neigh(results_r.p, best_neigh * conf.num_params); + // + // OptResult retry_res = opt.solve(cx, cy, ss_l, ss_r, interp_r, true); + // + // // If retry is better than original attempt, update results + // if (retry_res.cost > results_r.cost[idx]) { + // std::vector pA(conf.num_params); + // std::vector pB = retry_res.p; + // std::vector pC(conf.num_params); + // + // // pA: k0 -> k_l + // std::copy(results_l.p.begin() + idx*conf.num_params, + // results_l.p.begin() + idx*conf.num_params + conf.num_params, + // pA.begin()); + // + // if (conf.shape_func == util::ShapeFunc::RIGID) { + // Rigid::compose(pC, pA, pB); + // Rigid::get_displacement(retry_res.u, retry_res.v, 0.0, 0.0, pC); + // } + // else if (conf.shape_func == util::ShapeFunc::AFFINE){ + // Affine::compose(pC, pA, pB); + // Affine::get_displacement(retry_res.u, retry_res.v, 0.0, 0.0, pC); + // } + // else if (conf.shape_func == util::ShapeFunc::QUAD) { + // Quad::compose(pC, pA, pB); + // Quad::get_displacement(retry_res.u, retry_res.v, 0.0, 0.0, pC); + // } + // retry_res.p = pC; + // + // results_r.append(retry_res, idx); + // } + // } + // } + // } + + if (g_debug_level>0){ + pbar.finish(); + } + + if (error_flag.load()) { + throw std::runtime_error(error_message); + } + } + +} // namespace + diff --git a/src/pyvale/dic/cpp/stereomatching.hpp b/src/pyvale/dic/cpp/stereomatching.hpp new file mode 100644 index 000000000..1b13a9ce6 --- /dev/null +++ b/src/pyvale/dic/cpp/stereomatching.hpp @@ -0,0 +1,53 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + +#ifndef STEREOMATCHING_HPP +#define STEREOMATCHING_HPP + +// STD library Header files +#include +#include + + +// program header files +#include "../../calib/cpp/calibstereo.hpp" +#include "./dicsubset.hpp" +#include "./dicresults.hpp" +#include "./dicinterp.hpp" +#include "./dicutil.hpp" + +// Eigen +#include + +namespace stereo { + + void matching(const Image &img_l, + const Image &img_r, + const Interpolator &interp_l, + const Interpolator &interp_r, + const subset::Grid &ss_grid, + const util::Config &conf, + const int img_num_def_l, + const int img_num_def_r, + const Eigen::Matrix3d &F, + const ResultArrays &results_l, + ResultArrays &results_r); + + // void matching_strategy3(const Image &img_l, + // const Image &img_r, + // const Interpolator &interp_l, + // const Interpolator &interp_r, + // const subset::Grid &ss_grid, + // const util::Config &conf, + // const int img_num_l, + // const int img_num_r, + // const Eigen::Matrix3d &F, + // ResultArrays &temporal, + // ResultArrays &matches); + +} // stereo namespace + +#endif // STEREOMATCHING_HPP diff --git a/src/pyvale/dic/cpp/stereoutil.cpp b/src/pyvale/dic/cpp/stereoutil.cpp new file mode 100644 index 000000000..20e72d99e --- /dev/null +++ b/src/pyvale/dic/cpp/stereoutil.cpp @@ -0,0 +1,755 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + +#define _USE_MATH_DEFINES +#include + +// dic header files +#include "./stereoutil.hpp" +#include "./dicfourier.hpp" +#include "./dicresults.hpp" +#include "./dicinterp.hpp" +#include "./dicsubset.hpp" + +// calib header files +#include "../../calib/cpp/calibstereo.hpp" + +// Eigen Header files +#include + +namespace stereo { + + + + Geometry compute_stereo_geometry(const Calib &calib) { + + common_util::Timer timer("to compute stereo geometry:", 2); + + Geometry geom; + geom.K0 = camera_matrix(calib.cam0); + geom.K1 = camera_matrix(calib.cam1); + geom.t_x = skew_translation(calib.translation); + geom.R = rotation_from_euler(calib.rotation); + geom.F = fundamental(geom.K0, geom.K1, geom.t_x, geom.R); + return geom; + } + + void pixel_to_world(const subset::Grid &ss_grid, + const Calib &calib, + ResultArrays &temporal, + ResultArrays &stereo_ref, + ResultArrays &stereo_def, + const Eigen::Matrix3d &K0, + const Eigen::Matrix3d &K1, + const Eigen::Matrix3d &R, + const int ss_size, + const bool first_frame){ + + Eigen::Vector3d t(calib.translation[0],calib.translation[1],calib.translation[2]); + Eigen::Matrix P0, P1; + + P0 << Eigen::Matrix3d::Identity(), Eigen::Vector3d::Zero(); + P1 << R, t; // just gonna assume t is in mm for now + + + #pragma omp parallel for + for (int ss = 0; ss < ss_grid.num; ss++){ + + if ((!first_frame && !temporal.above_thresh[ss]) || !stereo_def.above_thresh[ss]) + continue; + + // centre coords left + double cx_l = ss_grid.coords[ss*2] + temporal.u[ss]; + double cy_l = ss_grid.coords[ss*2+1] + temporal.v[ss]; + + // centre coords right + double cx_r = ss_grid.coords[ss*2] + stereo_def.u[ss]; + double cy_r = ss_grid.coords[ss*2+1] + stereo_def.v[ss]; + + // undistorted pixel value + double u_cx_l, u_cx_r, u_cy_l, u_cy_r; + stereo::undistortPoint(u_cx_l, u_cy_l, cx_l, cy_l, K0, calib.cam0.distortion); + stereo::undistortPoint(u_cx_r, u_cy_r, cx_r, cy_r, K1, calib.cam1.distortion); + + // 3d pixel coords guess + Eigen::Vector3d xl(u_cx_l, u_cy_l, 1.0); + Eigen::Vector3d xr(u_cx_r, u_cy_r, 1.0); + + // Build DLT system + Eigen::Matrix4d A; + + A.row(0) = xl(0) * P0.row(2) - P0.row(0); + A.row(1) = xl(1) * P0.row(2) - P0.row(1); + A.row(2) = xr(0) * P1.row(2) - P1.row(0); + A.row(3) = xr(1) * P1.row(2) - P1.row(1); + + // Solve with SVD + Eigen::JacobiSVD svd(A, Eigen::ComputeFullV); + Eigen::Vector4d X = svd.matrixV().col(3); + + // Convert from homogeneous + X /= X(3); + + double X_mm = X(0); + double Y_mm = X(1); + double Z_mm = X(2); + + stereo_def.x_world[ss] = X_mm; + stereo_def.y_world[ss] = Y_mm; + stereo_def.z_world[ss] = Z_mm; + + if (first_frame) { + stereo_def.u_world[ss] = 0.0; + stereo_def.v_world[ss] = 0.0; + stereo_def.w_world[ss] = 0.0; + } + else { + // compute delta relative to the provided stereo_ref world coords + // and add any previously-accumulated world displacement stored in + // stereo_ref.*_world. This ensures cumulative displacements are + // preserved when the reference is updated incrementally. + stereo_def.u_world[ss] = (X_mm - stereo_ref.x_world[ss]) + stereo_ref.u_world[ss]; + stereo_def.v_world[ss] = (Y_mm - stereo_ref.y_world[ss]) + stereo_ref.v_world[ss]; + stereo_def.w_world[ss] = (Z_mm - stereo_ref.z_world[ss]) + stereo_ref.w_world[ss]; + } + } + if (first_frame) stereo_ref = stereo_def; + } + + void undistortPoint(double &x_undistorted, double &y_undistorted, + const double x_distorted, const double y_distorted, + const Eigen::Matrix3d &K, + const std::vector &d) { + + const double fx = K(0,0); + const double fy = K(1,1); + const double fs = K(0,1); + const double cx = K(0,2); + const double cy = K(1,2); + const double k1 = d[0]; + const double k2 = d[1]; + const double p1 = d[2]; + const double p2 = d[3]; + const double k3 = d[4]; + + // Normalize to camera coords + double y_n = (y_distorted - cy) / fy; + double x_n = (x_distorted - cx - fs * y_n) / fx; + + // Initial guess = normalized distorted point + double x_u = x_n; + double y_u = y_n; + + for (int i = 0; i < 100; i++) { + double r2 = x_u*x_u + y_u*y_u; + double r4 = r2*r2; + double r6 = r2*r4; + + double radial = 1.0 + k1*r2 + k2*r4 + k3*r6; + + // Tangential distortion terms + double dx = 2.0*p1*x_u*y_u + p2*(r2 + 2.0*x_u*x_u); + double dy = p1*(r2 + 2.0*y_u*y_u) + 2.0*p2*x_u*y_u; + + // Predicted distorted point + double x_pred = x_u*radial + dx; + double y_pred = y_u*radial + dy; + + double ex = x_n - x_pred; + double ey = y_n - y_pred; + + if (std::abs(ex) < 1e-12 && std::abs(ey) < 1e-12) + break; + + double drad_dr2 = k1 + 2.0*k2*r2 + 3.0*k3*r4; + double drad_dxu = 2.0*x_u*drad_dr2; + double drad_dyu = 2.0*y_u*drad_dr2; + + double J00 = radial + x_u*drad_dxu + 2.0*p1*y_u + 6.0*p2*x_u; + double J01 = x_u*drad_dyu + 2.0*p1*x_u + 2.0*p2*y_u; + double J10 = y_u*drad_dxu + 2.0*p1*x_u + 2.0*p2*y_u; + double J11 = radial + y_u*drad_dyu + 6.0*p1*y_u + 2.0*p2*x_u; + + // Solve 2x2 + double det = J00*J11 - J01*J10; + if (std::abs(det) < 1e-14) + break; // singular, shouldn't happen in practice + + x_u += (J11*ex - J01*ey) / det; + y_u += (J00*ey - J10*ex) / det; + } + + x_undistorted = x_u; + y_undistorted = y_u; + } + + void search_epi_line(double &best_zncc, + double &best_disp_x, + double &best_disp_y, + const double x, + const double y, + const subset::Pixels &ss_l, + subset::Pixels &ss_r, + const Eigen::Vector2d P, + const Eigen::Vector2d dir, + const Interpolator &interp_r, + const int range){ + + + + + best_zncc = -1.0; + best_disp_x = 0.0; + best_disp_y = 0.0; + + double corner_x, corner_y, zncc; + Eigen::Vector2d P_i; + + for (int i = -range; i < range; i++){ + + P_i = P + static_cast(i)*dir; + + // Convert to CORNER position for get_subpx_from_img + subset::get_corner(corner_x,corner_y, P_i(0),P_i(1),ss_l.size_x,ss_l.size_y); + + subset::fill_from_img_subpx(ss_r, corner_x, corner_y, interp_r); + + zncc = subset::zncc(ss_l, ss_r); + + if (zncc > best_zncc) { + best_zncc = zncc; + best_disp_x = corner_x - x; + best_disp_y = corner_y - y; + } + } + } + + + + void compute_epi(Eigen::Vector2d &nearest_point, + Eigen::Vector2d &direction, + const double x, + const double y, + const Eigen::Matrix3d &F){ + + + + // std::cout << "subset" << std::endl; + // std::cout << x << " " << y << std::endl; + Eigen::Vector3d epi_line = F * Eigen::Vector3d(x, y, 1.0); + + // std::cout << "epi_line" << std::endl; + // std::cout << epi_line << std::endl; + + double a = epi_line(0), b = epi_line(1), c = epi_line(2); + double denom = a*a + b*b; + double nrm = std::sqrt(denom); + double t = (a*x + b*y + c)/denom; + + // std::cout << "t" << std::endl; + // std::cout << t << std::endl; + + nearest_point << x - a*t, y - b*t; + direction << -b/nrm, a/nrm; + + // std::cout << "nearest_point" << std::endl; + // std::cout << nearest_point << std::endl; + // + if (direction(0) < 0) + direction = -direction; + } + + void get_rigid_translation_from_rectified_fft(std::vector &p, + const double cx, const double cy, + const int ss_size_x, const int ss_size_y, + const int window_size_x, const int window_size_y, + const Eigen::Matrix3d &F, + const Interpolator &interp_ref, + const Interpolator &interp_def, + const double offset_x, + const double offset_y, + const bool print){ + + const int px_hori = interp_def.px_hori; + const int px_vert = interp_def.px_vert; + const int window_half_x = window_size_x/2; + const int window_half_y = window_size_y/2; + const int ss_half_x = ss_size_x/2; + const int ss_half_y = ss_size_y/2; + + // class for FFT + FFT fft(window_size_x, window_size_y, print); + + // put the subset at the corner of the window. + // for the FFT I'm just using a square subset and not the shape function + // parameters. I've not found a case where this has been insufficient + fill_fft_window_with_subset_at_centre(fft.ss_ref, interp_ref, + cx, cy, px_hori, px_vert, + ss_size_x, ss_size_y, + window_size_x, window_size_y); + + // equation of epipolar line for the corner + Eigen::Vector2d closest_point, dir; + stereo::compute_epi(closest_point, dir, cx + offset_x, cy + offset_y, F); + + + + // TODO: Add a proper flag for this + bool subpx = true; + + Eigen::Vector2d perp(dir(1), -dir(0)); + + for(int y = 0; y < window_size_y; y++){ + for(int x = 0; x < window_size_x; x++) { + Eigen::Vector2d centre = closest_point + (x-window_half_x)*dir; + Eigen::Vector2d sample_pt = centre - (y-window_half_y)*perp; + double val = interp_def.eval(0,0,sample_pt(0),sample_pt(1)); + const int idx = y * window_size_x + x; + if (fft.ss_def.has_coords()) { + fft.ss_def.x[idx] = sample_pt(0); + fft.ss_def.y[idx] = sample_pt(1); + } + fft.ss_def.vals[idx] = val; + } + } + + // apply window to deformed subset + for (int row = 0; row < window_size_y; ++row) { + for (int col = 0; col < window_size_x; ++col) { + double coeff = 1.0; //fourier::hamming(row,col,window_size_x, window_size_y); + fft.ss_def.vals[row*window_size_x+col] *= coeff; + } + } + + // zero norm the subsets + bool normed_ref = fft.zero_norm_subsets_centered(fft.ss_ref, ss_size_x,ss_size_y, window_size_x, window_size_y); + bool normed_def = fft.zero_norm_subset(fft.ss_def, window_size_x,window_size_y); + + // get peaks from the cross correlation + double max_val = 0.0, peak_x = 0.0, peak_y = 0.0; + if (normed_ref && normed_def){ + fft.correlate(); + fft.get_peak(peak_x, peak_y, max_val, subpx, "GAUSSIAN_2D"); + } + //std::cout << "peak: " << peak_x << " " << peak_y << std::endl; + + // coordinate transform + // peak_x = peak_x - window_half_x; + // peak_y = peak_y - window_half_y; + + if (print) { + for (int row = 0; row < window_size_y; ++row) { + for (int col = 0; col < window_size_x; ++col) { + int idx = row*window_size_x+col; + std::cout << col << " " << row << " "; + std::cout << fft.ss_ref.x[idx] << " " << fft.ss_ref.y[idx] << " " << fft.ss_ref.vals[idx] << " "; + std::cout << fft.ss_def.x[idx] << " " << fft.ss_def.y[idx] << " " << fft.ss_def.vals[idx] << " "; + std::cout << fft.cross_corr[idx] << std::endl; + } + } + std::cout << std::endl; + } + + + // Compute the unrectified position in the right image + Eigen::Vector2d unrectified_pos = closest_point + peak_x * dir - peak_y * perp; + + //std::cout << "unrectified_pos: " << unrectified_pos(0) << " " << unrectified_pos(1) << std::endl; + p[0] = unrectified_pos(0) - cx; + p[1] = unrectified_pos(1) - cy; + p[2] = dir(0) - 1.0; + p[3] = -perp(0); + p[4] = dir(1); + p[5] = -perp(1) - 1.0; + + } + + void get_rigid_translation_from_rectified_search(std::vector &p, + const int ss_x, const int ss_y, + const int ss_size_x, const int ss_size_y, + const Eigen::Vector2d closest_point, + const Eigen::Vector2d dir, + subset::Pixels &ss_l, + const Interpolator &interp_ref, + const Interpolator &interp_def){ + + const int px_hori = interp_def.px_hori; + const int px_vert = interp_def.px_vert; + + int range = 100; + Eigen::Vector2d perp(dir(1), -dir(0)); + subset::Pixels ss_r(ss_size_x, ss_size_y); + subset::Pixels ss_final(ss_size_x, ss_size_y); + double best_zncc = -1.0; + + //Optimizer opt_affine("AFFINE", "ZNSSD", 40, 0.0001, 0.90); + + for(int pt = -range; pt < range; pt++){ + + + Eigen::Vector2d def_coord = closest_point + (double(pt))*dir; + + // fill the deformed subset + for(int y = 0; y < ss_size_y; y++){ + for(int x = 0; x < ss_size_x; x++) { + Eigen::Vector2d corner = def_coord + x*dir; + Eigen::Vector2d sample_pt = corner - y*perp; + double val = interp_def.eval(0,0,sample_pt(0),sample_pt(1)); + + int idx = y*ss_size_x+x; + ss_r.x[idx] = sample_pt(0); + ss_r.y[idx] = sample_pt(1); + ss_r.vals[idx] = val; + // std::cout << ss_l.x[idx] << " " << ss_l.y[idx] << " " << ss_l.vals[idx] << " "; + // std::cout << ss_r.x[idx] << " " << ss_r.y[idx] << " " << ss_r.vals[idx] << std::endl; + } + } + double zncc = subset::zncc(ss_l,ss_r); + + + // testing with optimizer here + // subset::get_subpx_from_img(ss_l, ss_x, ss_y, interp_ref); + // opt_affine.p[0] = def_coord(0) - ss_x; + // opt_affine.p[1] = def_coord(1) - ss_y; + // opt_affine.p[2] = dir(0) - 1.0; + // opt_affine.p[3] = -perp(0); + // opt_affine.p[4] = dir(1); + // opt_affine.p[5] = -perp(1) - 1.0; + // OptResult seed_res = opt_affine.solve(ss_x, ss_y, ss_l, ss_r, interp_def); + // std::cout << seed_res.cost << " " << int(seed_res.converged) << " " << seed_res.iter << std::endl; + + if (zncc > best_zncc) { + best_zncc = zncc; + p[0] = def_coord(0) - ss_x; + p[1] = def_coord(1) - ss_y; + p[2] = dir(0) - 1.0; + p[3] = -perp(0); + p[4] = dir(1); + p[5] = -perp(1) - 1.0; + // for (int px = 0; px < ss_r.num_px; px++){ + // ss_final.x[px] = ss_r.x[px]; + // ss_final.y[px] = ss_r.y[px]; + // ss_final.vals[px] = ss_r.vals[px]; + // } + } + + } + + // for (int px = 0; px < ss_r.num_px; px++){ + // std::cout << ss_final.x[px] << " " << ss_final.y[px] << " " << ss_final.vals[px] << std::endl; + // } + } + + + + + Eigen::Matrix3d rotation_from_euler(const std::vector &rot_deg) { + double theta = rot_deg[0] * (M_PI / 180.0); + double phi = rot_deg[1] * (M_PI / 180.0); + double psi = rot_deg[2] * (M_PI / 180.0); + + Eigen::Matrix3d Rx, Ry, Rz; + Rx << 1, 0, 0, + 0, cos(theta), -sin(theta), + 0, sin(theta), cos(theta); + + Ry << cos(phi), 0, sin(phi), + 0, 1, 0, + -sin(phi), 0, cos(phi); + + Rz << cos(psi), -sin(psi), 0, + sin(psi), cos(psi), 0, + 0, 0, 1; + + return Rz * Ry * Rx; + } + + Eigen::Matrix3d camera_matrix(const CamIntrinsics &cam) { + Eigen::Matrix3d K; + K << cam.fx, cam.fs, cam.cx, + 0.0, cam.fy, cam.cy, + 0.0, 0.0, 1.0; + return K; + } + + Eigen::Matrix3d skew_translation(const std::vector &t) { + Eigen::Matrix3d t_x; + t_x << 0, -t[2], t[1], + t[2], 0, -t[0], + -t[1], t[0], 0; + return t_x; + } + + Eigen::MatrixXd patch_corners(const double x, + const double y, + const double size_x, + const double size_y) { + Eigen::MatrixXd pts(3,4); + pts << x, x, x+size_x, x+size_x, + y, y+size_y, y+size_y, y, + 1.0, 1.0, 1.0, 1.0; + return pts; + } + + Eigen::Matrix3d fundamental(const Eigen::Matrix3d &K0, + const Eigen::Matrix3d &K1, + const Eigen::Matrix3d &t_x, + const Eigen::Matrix3d &R){ + + Eigen::Matrix3d K0_inv = K0.inverse(); + Eigen::Matrix3d K1_inv_T = K1.inverse().transpose(); + Eigen::Matrix3d F = K1_inv_T * t_x * R * K0_inv; + F /= F.norm(); + return F; + + } + + EpipolarStrip calc_epi_strip_points(const double cx, + const double cy, + const Eigen::Matrix &lines, + const Eigen::Matrix3d F, + const double range) { + + + // equation of epipolar line + Eigen::Vector3d F_centre = F * Eigen::Vector3d(cx, cy, 1.0); + double a = F_centre(0), b = F_centre(1), c = F_centre(2); + + double denom = a*a + b*b; + double nrm = std::sqrt(denom); + + // bounds + double cmin = std::numeric_limits::infinity(); + double cmax = -std::numeric_limits::infinity(); + + // get the range of the bounding box + for (int i = 0; i < 4; i++){ + double ai = lines(0,i); + double bi = lines(1,i); + double ci = lines(2,i); + double offset = (ai*cx + bi*cy + ci)/nrm; + cmin = std::min(cmin, offset); + cmax = std::max(cmax, offset); + } + + double half_width = 0.5*(cmax-cmin); + + double t = (a*cx + b*cy + c)/denom; + Eigen::Vector2d P(cx - a*t, cy - b*t); + Eigen::Vector2d dir(-b/nrm, a/nrm); + Eigen::Vector2d n(a/nrm, b/nrm); + + + // point on the epipolar line pixels either side + // of the midpoint P. + Eigen::Vector2d P0 = P - range*dir; + Eigen::Vector2d P1 = P + range*dir; + + return EpipolarStrip { + P, // patch centre + P0 + half_width*n, // bounding box Q0 + P0 - half_width*n, // bounding box Q1 + P1 - half_width*n, // bounding box Q2 + P1 + half_width*n // bounding box Q3 + }; + } + + + std::tuple bounding_box(const EpipolarStrip &strip, + const int px_hori, + const int px_vert){ + + double xmin = std::max(0.0, std::min({strip.Q0.x(), strip.Q1.x(), strip.Q2.x(), strip.Q3.x()})); + double xmax = std::min((double)px_hori-1.0, std::max({strip.Q0.x(), strip.Q1.x(), strip.Q2.x(), strip.Q3.x()})); + double ymin = std::max(0.0, std::min({strip.Q0.y(), strip.Q1.y(), strip.Q2.y(), strip.Q3.y()})); + double ymax = std::min((double)px_vert-1.0, std::max({strip.Q0.y(), strip.Q1.y(), strip.Q2.y(), strip.Q3.y()})); + + return {std::ceil(xmin), std::ceil(xmax), std::ceil(ymin), std::ceil(ymax)}; + } + + + + bool* compute_roi_r(const subset::Grid ss_grid_l, + const ResultArrays &stereo_matches, + const int px_hori, + const int px_vert, + const int ss_size_x, + const int ss_size_y) { + + + const int half_y = ss_size_y/2; + const int half_x = ss_size_x/2; + + bool* img_roi_r = new bool[px_hori * px_vert](); + + int ss_x_l, ss_y_l; + double ss_x_r, ss_y_r; + double cx, cy; + for (int i = 0; i < stereo_matches.u.size(); i++) { + + ss_x_l = ss_grid_l.coords[2*i]; + ss_y_l = ss_grid_l.coords[2*i+1]; + subset::get_centre(cx, cy, ss_x_l, ss_y_l, ss_size_x, ss_size_y); + + ss_x_r = ss_x_l+stereo_matches.u[i]; + ss_y_r = ss_y_l+stereo_matches.v[i]; + + for (int dy = -half_y; dy <= half_y; dy++) { + for (int dx = -half_x; dx <= half_x; dx++) { + int x = static_cast(std::round(ss_x_r)) + dx; + int y = static_cast(std::round(ss_y_r)) + dy; + if (x >= 0 && x < px_hori && y >= 0 && y < px_vert) { + img_roi_r[y * px_hori + x] = true; + } + } + } + } + + return img_roi_r; + } + + bool* compute_roi_r_test(const bool* img_roi_l, + const subset::Grid& ss_grid, + const ResultArrays& stereo_matches, + const int px_hori, + const int px_vert) { + + // Count valid matches first + std::vector valid; + for (int i = 0; i < stereo_matches.u.size(); i++) + if (stereo_matches.conv[i] && stereo_matches.above_thresh[i]) + valid.push_back(i); + + assert(valid.size() >= 4 && "Not enough valid matches to compute H"); + + Eigen::MatrixXd A(valid.size() * 2, 9); + + for (int i = 0; i < valid.size(); i++) { + const int idx = valid[i]; + + const double cx = ss_grid.coords[2*idx]; + const double cy = ss_grid.coords[2*idx+1]; + + const double xr = cx + stereo_matches.u[idx]; + const double yr = cy + stereo_matches.v[idx]; + + A.row(2*i) << cx, cy, 1, 0, 0, 0, -xr*cx, -xr*cy, -xr; + A.row(2*i+1) << 0, 0, 0, cx, cy, 1, -yr*cx, -yr*cy, -yr; + } + + // Solve via SVD + Eigen::JacobiSVD svd(A, Eigen::ComputeFullV); + Eigen::VectorXd h = svd.matrixV().col(8); + Eigen::Matrix3d H; + H << h(0), h(1), h(2), + h(3), h(4), h(5), + h(6), h(7), h(8); + + // Warp left mask to right using H + bool* img_roi_r = new bool[px_hori * px_vert](); + + for (int v = 0; v < px_vert; v++) { + for (int u = 0; u < px_hori; u++) { + if (!img_roi_l[v * px_hori + u]) continue; + + Eigen::Vector3d p = H * Eigen::Vector3d(u, v, 1.0); + int u_r = static_cast(std::round(p.x() / p.z())); + int v_r = static_cast(std::round(p.y() / p.z())); + + for (int dv = -1; dv <= 1; dv++) { + for (int du = -1; du <= 1; du++) { + int u_n = u_r + du, v_n = v_r + dv; + if (u_n >= 0 && u_n < px_hori && v_n >= 0 && v_n < px_vert) + img_roi_r[v_n * px_hori + u_n] = true; + } + } + } + } + return img_roi_r; +} + + void remove_unmatched_subsets(subset::Grid& ss_grid_l, + subset::Grid& ss_grid_r, + const ResultArrays stereo_matches) { + + // Build list of subsets to keep + std::vector keep; + for (int ss = 0; ss < ss_grid_l.num; ss++) { + if (stereo_matches.above_thresh[ss]) + keep.push_back(ss); + } + + // Build remap + std::vector remap(ss_grid_l.num, -1); + for (int i = 0; i < keep.size(); i++) + remap[keep[i]] = i; + + // Compact coords + std::vector new_coords_l, new_coords_r; + + for (int i = 0; i < keep.size(); i++) { + int ss = keep[i]; + new_coords_l.push_back(ss_grid_l.coords[2*ss]); + new_coords_l.push_back(ss_grid_l.coords[2*ss + 1]); + new_coords_r.push_back(ss_grid_r.coords[2*ss]); + new_coords_r.push_back(ss_grid_r.coords[2*ss + 1]); + } + + // Compact neigh + std::vector> new_neigh_l, new_neigh_r; + for (int i = 0; i < keep.size(); i++) { + int ss = keep[i]; + + std::vector nl, nr; + for (int j = 0; j < ss_grid_l.neigh[ss].size(); j++) { + int n = ss_grid_l.neigh[ss][j]; + if (remap[n] != -1) + nl.push_back(remap[n]); + } + for (int j = 0; j < ss_grid_r.neigh[ss].size(); j++) { + int n = ss_grid_r.neigh[ss][j]; + if (remap[n] != -1) + nr.push_back(remap[n]); + } + + new_neigh_l.push_back(nl); + new_neigh_r.push_back(nr); + } + + // Fix mask + for (int i = 0; i < (int)ss_grid_l.mask.size(); i++) { + if (ss_grid_l.mask[i] != -1) + ss_grid_l.mask[i] = remap[ss_grid_l.mask[i]]; + if (ss_grid_r.mask[i] != -1) + ss_grid_r.mask[i] = remap[ss_grid_r.mask[i]]; + } + + ss_grid_l.coords = new_coords_l; + ss_grid_l.neigh = new_neigh_l; + ss_grid_l.num = keep.size(); + + ss_grid_r.coords = new_coords_r; + ss_grid_r.neigh = new_neigh_r; + ss_grid_r.num = keep.size(); + } + + std::pair, std::vector> + split_basenames(const util::Config &conf) { + if (!conf.stereo) + return {conf.basenames, {}}; + + std::vector basenames_l, basenames_r; + for (int i = 0; i < conf.basenames.size() / 2; i++) { + basenames_l.push_back(conf.basenames[i]); + basenames_r.push_back(conf.basenames[conf.num_def_img + 1 + i]); + } + return {basenames_l, basenames_r}; + } + + +} + + diff --git a/src/pyvale/dic/cpp/stereoutil.hpp b/src/pyvale/dic/cpp/stereoutil.hpp new file mode 100644 index 000000000..4574ba974 --- /dev/null +++ b/src/pyvale/dic/cpp/stereoutil.hpp @@ -0,0 +1,322 @@ +// ================================================================================ +// pyvale: the python validation engine +// License: MIT +// Copyright (C) 2025 The Computer Aided Validation Team +// ================================================================================ + + +#ifndef _STEREOUTIL_HPP +#define _STEREOUTIL_HPP + + +// dic header files +#include "./dicutil.hpp" +#include "./dicsubset.hpp" +#include "./dicresults.hpp" + +// calibration header files +#include "../../calib/cpp/calibstereo.hpp" + +// Eigen Header files +#include + + +namespace stereo { + + + + /** + * @brief Represents the epipolar strip bounding geometry for a patch. + * + * Stores the projected patch centre on the epipolar line and the four + * corner points of the bounding quadrilateral that limits the valid + * search region around that line. + */ + struct EpipolarStrip { + Eigen::Vector2d P; ///< Patch centre projected onto the epipolar line. + Eigen::Vector2d Q0; ///< Bounding corner Q0. + Eigen::Vector2d Q1; ///< Bounding corner Q1. + Eigen::Vector2d Q2; ///< Bounding corner Q2. + Eigen::Vector2d Q3; ///< Bounding corner Q3. + }; + + /** + * @brief Container for stereo camera geometry derived from calibration. + * + * Holds intrinsic matrices for both cameras, the rotation and translation + * between them (in skew-symmetric form), and the resulting fundamental matrix. + */ + struct Geometry { + Eigen::Matrix3d K0; ///< Intrinsic matrix of camera 0. + Eigen::Matrix3d K1; ///< Intrinsic matrix of camera 1. + Eigen::Matrix3d R; ///< Rotation from camera 0 to camera 1. + Eigen::Matrix3d t_x; ///< Skew-symmetric translation matrix. + Eigen::Matrix3d F; ///< Fundamental matrix. + }; + + + + /** + * @brief Computes all stereo geometry matrices from calibration parameters. + * + * Builds the intrinsic matrices of both cameras, the rotation and translation + * between them (in skew-symmetric form), and the resulting fundamental matrix. + * + * @param calib Stereo calibration parameters (intrinsics, rotation, translation). + * @return Geometry struct containing K0, K1, R, t_x, and F. + */ + Geometry compute_stereo_geometry(const Calib &calib); + + void undistortPoint(double &x_undistorted, double &y_undistorted, + const double x_distorted, const double y_distorted, + const Eigen::Matrix3d &K, + const std::vector &d); + + + /** + * @brief Computes a rotation matrix from Euler angles (degrees, XYZ order). + * @param rot_deg Euler angles in degrees: [theta_x, phi_y, psi_z]. + * @return 3x3 rotation matrix Rz * Ry * Rx. + */ + Eigen::Matrix3d rotation_from_euler(const std::vector &rot_deg); + + + /** + * @brief Constructs a camera intrinsic matrix from calibration parameters. + * @param cam Camera intrinsics (fx, fy, fs, cx, cy). + * @return 3x3 intrinsic matrix K. + */ + Eigen::Matrix3d camera_matrix(const CamIntrinsics &cam); + + + /** + * @brief Forms the skew-symmetric matrix of a translation vector. + * @param t Translation vector [tx, ty, tz]. + * @return 3x3 skew-symmetric matrix t_x. + */ + Eigen::Matrix3d skew_translation(const std::vector &t); + + + + + /** + * @brief Computes the homogeneous corner coordinates of an image patch. + * @param x Top-left x coordinate. + * @param y Top-left y coordinate. + * @param size_x Patch width. + * @param size_y Patch height. + * @return 3x4 matrix of patch corner points. + */ + Eigen::MatrixXd patch_corners(const double x, + const double y, + const double size_x, + const double size_y); + /** + * @brief Computes the fundamental matrix from intrinsics, translation, and rotation. + * @param K0 Intrinsic matrix of camera 0. + * @param K1 Intrinsic matrix of camera 1. + * @param t_x Skew-symmetric translation matrix. + * @param R Rotation from camera 0 to camera 1. + * @return Normalized 3x3 fundamental matrix F. + */ + Eigen::Matrix3d fundamental(const Eigen::Matrix3d &K0, + const Eigen::Matrix3d &K1, + const Eigen::Matrix3d &t_x, + const Eigen::Matrix3d &R); + + + + /** + * @brief Calculates epipolar strip geometry around a patch centre. + * @param cx Patch centre x coordinate. + * @param cy Patch centre y coordinate. + * @param lines Bounding box lines in homogeneous form. + * @param F Fundamental matrix. + * @param range Half-length of the epipolar search segment. + * @return EpipolarStrip containing midpoint and bounding quad points. + */ + EpipolarStrip calc_epi_strip_points(const double cx, const double cy, + const Eigen::Matrix &lines, + const Eigen::Matrix3d F, + double range); + + + + /** + * @brief Computes the image-space bounding box of an epipolar strip. + * @param strip Epipolar strip quad points. + * @param px_hori Image width in pixels. + * @param px_vert Image height in pixels. + * @return (xmin, xmax, ymin, ymax) bounding coordinates. + */ + std::tuple bounding_box(const EpipolarStrip &strip, + const int px_hori, + const int px_vert); + + + + + + /** + * @brief Triangulates 3D world coordinates for valid stereo matches (linear DLT). + * + * For each subset center whose match passes the threshold, this function: + * 1) Computes the left/right patch center in pixels, + * 2) Undistorts both points using each camera's intrinsics/distortion, + * 3) Forms normalized homogeneous points (assumes K = I in projection), + * 4) Builds the DLT system with P0 = [I | 0], P1 = [R | t], + * 5) Solves via SVD and recovers X in homogeneous coordinates. + * + * @param ss_grid Grid of subset (patch) top-left coordinates (pixel space). + * @param calib Calibration containing intrinsics/distortion for cam0/cam1 and translation (t). + * @param stereo_matches Match results; uses u,v (pixel disparities) and above_thresh mask. + * @param K0 Intrinsic matrix of camera 0 (used only for undistortion here). + * @param K1 Intrinsic matrix of camera 1 (used only for undistortion here). + * @param R Rotation from camera 0 to camera 1. + * @param ss_size Subset (patch) size in pixels (assumed square). + * + * @note Points are undistorted and treated as normalized image coordinates when building + * the DLT system (i.e., P0 = [I|0], P1 = [R|t]). Ensure undistortion produces + * normalized coordinates consistent with this assumption. + * @note Translation vector @p calib.translation is assumed to be in millimeters, so the + * resulting (X, Y, Z) will also be in millimeters. + * @pre ss_grid.num == stereo_matches.u.size() == stereo_matches.v.size() + * == stereo_matches.above_thresh.size() + * @warning This implementation computes X but does not store it; add assignments to + * persist (X_mm, Y_mm, Z_mm) into your results container as needed. + * TODO: NEED TO FIX THIS + */ + void pixel_to_world(const subset::Grid &ss_grid, + const Calib &calib, + ResultArrays &temporal, + ResultArrays &stereo_ref, + ResultArrays &stereo_def, + const Eigen::Matrix3d &K0, + const Eigen::Matrix3d &K1, + const Eigen::Matrix3d &R, + const int ss_size, + const bool first_frame=false); + + + + /** + * @brief Searches along an epipolar line for the best ZNCC match. + * @param best_zncc Output: highest ZNCC score found. + * @param best_disp_x Output: best displacement in x. + * @param best_disp_y Output: best displacement in y. + * @param x Reference patch centre x. + * @param y Reference patch centre y. + * @param ss_l Reference subset (left image). + * @param ss_r Workspace subset (right image). + * @param P Midpoint on the epipolar line. + * @param dir Direction of the epipolar line (unit length). + * @param interp_r Interpolator for the right image. + * @param range Search extent in pixels along the line. + */ + void search_epi_line(double &best_zncc, + double &best_disp_x, + double &best_disp_y, + const double x, + const double y, + const subset::Pixels &ss_l, + subset::Pixels &ss_r, + const Eigen::Vector2d P, + const Eigen::Vector2d dir, + const Interpolator &interp_r, + const int range); + + + /** + * @brief Computes the closest point and direction of the epipolar line for a pixel. + * @param nearest_point Output: orthogonal projection of (x,y) onto the epipolar line. + * @param direction Output: unit direction vector of the epipolar line. + * @param x Pixel x coordinate. + * @param y Pixel y coordinate. + * @param F Fundamental matrix. + */ + void compute_epi(Eigen::Vector2d &nearest_point, + Eigen::Vector2d &direction, + const double x, + const double y, + const Eigen::Matrix3d &F); + + + + /** + * @brief Estimates rigid translation using FFT correlation on a rectified search grid. + * @param p Output: 6‑parameter rigid/affine displacement seed. + * @param cx Subset centre x coordinate. + * @param cy Subset centre y coordinate. + * @param ss_size_x Subset width. + * @param ss_size_y Subset height. + * @param closest_point Epipolar closest point to the reference pixel. + * @param dir Epipolar direction unit vector. + * @param window_size_x FFT window width. + * @param window_size_y FFT window height. + * @param img_ref Pointer to reference image buffer. + * @param interp_def Interpolator for the deformed image. + * @param offset_x Offset from reference left subset centre to current left centre. + * @param offset_y Offset from reference left subset centre to current left centre. + */ + void get_rigid_translation_from_rectified_fft(std::vector &p, + const double cx, const double cy, + const int ss_size_x, const int ss_size_y, + const int window_size_x, const int window_size_y, + const Eigen::Matrix3d &F, + const Interpolator &interp_ref, + const Interpolator &interp_def, + const double offset_x=0.0, + const double offset_y=0.0, + const bool print=false); + + /** + * @brief Estimates rigid translation by brute‑force ZNCC search along the epipolar line. + * @param p Output: 6‑parameter rigid/affine displacement seed. + * @param ss_x Subset top‑left x coordinate. + * @param ss_y Subset top‑left y coordinate. + * @param ss_size_x Subset width. + * @param ss_size_y Subset height. + * @param closest_point Epipolar closest point to the reference pixel. + * @param dir Epipolar direction unit vector. + * @param ss_l Reference subset (left image). + * @param interp_ref Interpolator for reference image. + * @param interp_def Interpolator for deformed image. + */ + void get_rigid_translation_from_rectified_search(std::vector &p, + const int ss_x, const int ss_y, + const int ss_size_x, const int ss_size_y, + const Eigen::Vector2d closest_point, + const Eigen::Vector2d dir, + subset::Pixels &ss_l, + const Interpolator &interp_ref, + const Interpolator &interp_def); + + + + bool* compute_roi_r(const subset::Grid ss_grid, + const ResultArrays &stereo_matches, + const int px_hori, + const int px_vert, + const int ss_size_x, + const int ss_size_y); + + + + bool* compute_roi_r_test(const bool* img_roi_l, + const subset::Grid& ss_grid, + const ResultArrays& stereo_matches, + const int px_hori, + const int px_vert); + + void remove_unmatched_subsets(subset::Grid& ss_grid_l, + subset::Grid& ss_grid_r, + const ResultArrays stereo_matches); + + + std::pair, std::vector> + split_basenames(const util::Config &conf); +} + + + +#endif // _STEREOUTIL_HPP diff --git a/src/pyvale/dic/dic2d.py b/src/pyvale/dic/dic2d.py index 02de61c3d..41efc004a 100644 --- a/src/pyvale/dic/dic2d.py +++ b/src/pyvale/dic/dic2d.py @@ -4,37 +4,47 @@ # Copyright (C) 2025 The Computer Aided Validation Team # ================================================================================ - - +import os from logging import debug import numpy as np from pathlib import Path +from typing import Literal # pyvale -import pyvale.dic.dic2dcpp as dic2dcpp +import pyvale.dic.diccpp as diccpp +import pyvale.calib.calibcpp as calibcpp import pyvale.dic.dicchecks as dicchecks import pyvale.common_py.util as common_py_util +from pyvale.calib.calibdataclass import Calib import pyvale.common_cpp.common_cpp as common_cpp def calculate_2d(reference: np.ndarray | str | Path, deformed: np.ndarray | str | Path | list[Path], roi_mask: np.ndarray, - seed: list[int] | list[np.int32] | np.ndarray, + seed: list[int] | list[np.int32] | list[tuple[int, int]] | np.ndarray, subset_size: int = 21, subset_step: int = 10, - correlation_criteria: str="ZNSSD", - shape_function: str="AFFINE", - interpolation_routine: str="BSPLINE", + correlation_criteria: Literal["ZNSSD","NSSD","SSD"]="ZNSSD", + shape_function: Literal["AFFINE","QUAD","RIGID"]="AFFINE", + interpolation_routine: Literal["BSPLINE","HERMITE"]="BSPLINE", max_iterations: int=40, precision: float=0.001, threshold: float=0.9, - bf_threshold: float=0.6, num_threads: int | None = None, - max_displacement: int=128, - method: str="MULTIWINDOW_RG", - fft_mad: bool=False, - fft_mad_scale: float=3.0, - output_at_end: bool=False, + max_displacement: int=64, + method: Literal["MULTIWINDOW_RG","SINGLEWINDOW_RG","MULTIWINDOW","RASTER"] = "MULTIWINDOW_RG", + incremental: bool=False, + incremental_update_condition: Literal["IMAGE","COST","ITER"]="IMAGE", + incremental_update_value: float=1, + multiwindow_overlap: float=0.0, + multiwindow_subset_sizes: list[int] = [], + multiwindow_search_areas: list[int] = [], + fft_filter: bool=True, + fft_filter_radius: int=3, + fft_filter_threshold: float=3.0, + fft_filter_corr_power: float=2.0, + fft_save: bool=False, + fft_precision: Literal["F64","F32"]="F32", output_basepath: Path | str = "./", output_binary: bool=False, output_prefix: str="dic_results_", @@ -54,69 +64,119 @@ def calculate_2d(reference: np.ndarray | str | Path, ---------- reference : np.ndarray, str or pathlib.Path The reference image (2D array) or path to the image file. + deformed : np.ndarray, str , pathlib.Path or list[pathlib.Path] The deformed image(s) (3D array for multiple images) or path/pattern to image files. + roi_mask : np.ndarray A binary mask indicating the Region of Interest (ROI) for analysis (same size as image). - seed : list[int], list[np.int32] or np.ndarray - Coordinates `[x, y]` of the seed point for Reliability-Guided (RG) scanning, default is empty. + + seed : list[int], list[np.int32], list[tuple[int, int]] or np.ndarray + Coordinates of the seed points for Reliability-Guided (RG) scanning. Accepts either + the existing flat format ``[x0, y0, x1, y1, ...]`` or tuple format + ``[(x0, y0), (x1, y1), ...]``. If the method is not RG, this will be ignored. + subset_size : int, optional Size of the square subset window in pixels (default: 21). + subset_step : int, optional Step size between subset centers in pixels (default: 10). + correlation_criteria : str, optional - Metric for matching subsets: "ZNSSD", "NSSD" or "SSD" (default: "ZNSSD"). + Metric for matching subsets: ``"ZNSSD"``, ``"NSSD"`` or ``"SSD"`` (default: ``"ZNSSD"``). + shape_function : str, optional - Deformation model: e.g., "AFFINE", "RIGID" (default: "AFFINE"). + Deformation model: e.g., ``"AFFINE"``, ``"QUAD"``, ``"RIGID"`` (default: ``"AFFINE"``). + interpolation_routine : str, optional - Interpolation method used on image intensity. "BICUBIC" is currently the - only supported option. + Interpolation method used on image intensity. Options are ``"BSPLINE"`` and + ``"HERMITE"``. Implementation details can be found in our DIC theory + documentation. (default: `"BSPLINE"``). + max_iterations : int, optional Maximum number of iterations allowed for subset optimization (default: 40). + precision : float, optional Precision threshold for iterative optimization convergence (default: 0.001). + threshold : float, optional Minimum correlation/cost coefficient value to be considered a matching subset (default: 0.9). num_threads : int, optional - Number of threads to use for parallel computation (default: None, uses all available). - bf_threshold : float, optional - Correlation threshold used in rigid bruteforce check for a subset to be considered a - good match(default: 0.6). + Number of threads to use for parallel computation (default: ``None``, uses all available). max_displacement : int, optional Estimate for the Maximum displacement in any direction (in pixels) (default: 128). method : str, optional - Subset scanning method: "RG" for Reliability-Guided (best overall approach), - "IMAGE_SCAN" for a standard scan across the image with no seeding - (best performance with for subpixel displacements with high quality images), - "FFT" for a multi-window FFT based approach (Good for large displacements) - fft_mad : bool, optional - The option to smooth FFT windowing data by identifying and replacing outliers using - a robust statistical method. For each subset, the function collects values from its - neighboring subsets (within a 5x5 window, i.e., radius = 2), computes the median and - Median Absolute Deviation (MAD), and determines whether the value at the current - subset is an outlier. If it is, the value is replaced with the median of - its neighbors. (default: False) - fft_mad_scale : bool, optional - An outlier is defined as a value whose deviation from the local median exceeds - `fft_mad_scale` times the MAD. This value choses the scaling factor that determines - the threshold for detecting outliers relative to the MAD. - output_at_end : bool, optional - If True, results will only be written at the end of processing (default: False). + The core algorithmic method used to perform the DIC. Options include: + + * ``"MULTIWINDOW_RG"``: Multi-window Reliability-Guided DIC (best overall approach). + + * ``"SINGLEWINDOW_RG"``: Uses a single window for the rigid estimate for each subset. + The size of the window is determined by the ``max_displacement`` parameter. + + * ``"MULTIWINDOW"``: Uses only the multi-window FFT strategy. + + * ``"RASTER"``: No FFT initialization. Performs a raster scan of the image. + + incremental : bool, optional + If True, then references images will be updated depending on the + condition set by argument ``incremental_update_condition``. This is useful + for large deformations where the original reference may no longer be + valid for tracking. If False, the original reference image(s) will be + used for tracking all deformed images. Displacements will still be given relative to the + first reference image. Note, cost values will be reported for relative to the deformed and + updated reference image. (default: False). + incremental_update_condition : str, optional + Condition for updating reference images when ``incremental`` is True. Options include: + ``"IMAGE"`` to update every ``N`` images, ``"COST"`` to update when the average ZNCC cost + value falls below a threshold, ``"ITER"`` to update when the average number + of subset optimizer iterations exceeds a threshold. (default: ``"PER_IMAGE"``). + incremental_update_value : float, optional + Value corresponding to the ``incremental_update_condition``. For example, + if the condition is ``"IMAGE"``, this would be the number of images after + which to update the reference. If the condition is ``"COST"``, this would be + the cost threshold for updating. If the condition is ``"ITER"``, this would + be the iteration threshold for updating. (default: 1). + multiwindow_overlap : float, optional + For multi-window methods, the percentage overlap between adjacent FFT windows + at each level (default: 0.5). + multiwindow_subset_sizes: list[int], optional + List of subset_sizes for the multi-window FFT approach. If + None, defaults to powers of 2 with the largest window size determined by + the next power of 2 above ``max_displacement`` (default: None). + multiwindow_search_areas: list[int], optional + List of search window sizes for the multi-window FFT approach. If None, + defaults to the corresponding template window size (default: None). + fft_filter : bool, optional + Enables outlier filtering for rigid FFT displacement estimates at each + FFTCC window size. (default: ``False``) + fft_filter_threshold : float, optional + Rejection threshold for the FFT displacement outlier filter. Larger + values are more tolerant, while smaller values reject more vectors. + (default: ``3.0``) + fft_filter_radius : int, optional + Neighbourhood radius, in subset-grid steps, used by the FFT displacement + outlier filter. (default: ``3``) + fft_filter_corr_power : float, optional + Exponent applied to correlation confidence when weighting neighbours in + the FFT displacement outlier filter. (default: ``2.0``) + fft_precision : str, optional + Floating-point precision for FFT-only windowing buffers. Options are ``"F32"`` + for single precision and ``"F64"`` for double precision. (default: ``"F32"``). output_basepath : str or pathlib.Path, optional - Directory path where output files will be written (default: "./"). + Directory path where output files will be written (default: ``"./"``). output_binary : bool, optional - Whether to write output in binary format (default: False). + Whether to write output in binary format (default: ``False``). output_prefix : str, optional - Prefix for all output files (default: :code:`dic_results_`). results will be + Prefix for all output files (default: ``dic_results_``). results will be named with output_prefix + original filename. THe extension will be - changed to ".csv" or ".dic2d" depending on whether outputting as a binary. + changed to ``".csv"`` or ``".dic2d"`` depending on whether outputting as a binary. output_delimiter : str, optional - Delimiter used in text output files (default: ","). + Delimiter used in text output files (default: ``","``). output_below_threshold : bool, optional If True, subset results with cost values that did not exceed the cost threshold - will still be present in output (default: False). + will still be present in output (default: ``False``). output_shape_params : bool, optional - If True, all shape parameters will be saved in the output files (default: False). + If True, all shape parameters will be saved in the output files (default: ``False``). debug_level: Returns @@ -133,42 +193,82 @@ def calculate_2d(reference: np.ndarray | str | Path, """ if (debug_level>0): - dicchecks.print_title("Initial Checks") - - # do checks on vars in python land - image_stack, roi_c, filenames = dicchecks.check_and_get_images(reference,deformed,roi_mask, debug_level) - dicchecks.check_correlation_criteria(correlation_criteria) - dicchecks.check_interpolation(interpolation_routine) - dicchecks.check_method(method) - dicchecks.check_thresholds(threshold, bf_threshold, precision) + common_py_util.print_pyvale_banner() + common_py_util.print_title("Initial Checks") + + # make sure ROI is in the correct format + roi_c = np.ascontiguousarray(roi_mask) + + basenames, fullpaths, w, h, temp_dir = dicchecks.check_images(reference,deformed,roi_mask, debug_level) + + + # string to enum + method_enum = dicchecks.ScanMethod(method) + shape_function_enum = dicchecks.Shape(shape_function) + correlation_criteria_enum = dicchecks.CorrCrit(correlation_criteria) + interpolation_routine_enum = dicchecks.Interp(interpolation_routine) + incremental_update_condition_enum = dicchecks.IncrementalMethod(incremental_update_condition) + + # checks on the config + mw_overlap, mw_subset_size, mw_search_area = dicchecks.multiwindow_init(subset_size, + subset_step, + max_displacement, + multiwindow_overlap, + multiwindow_subset_sizes, + multiwindow_search_areas) + + dicchecks.check_thresholds(threshold, precision) common_py_util.check_output_directory(str(output_basepath), output_prefix, debug_level) dicchecks.check_subsets(subset_size, subset_step) - updated_seed = dicchecks.check_and_update_rg_seed(seed, roi_mask, method, image_stack.shape[2], image_stack.shape[1], subset_size, subset_step) - num_params = dicchecks.check_shape_function(shape_function) + updated_seeds = dicchecks.check_and_update_rg_seed(seed, roi_mask, method, w, h, subset_size, subset_step) + num_params = dicchecks.check_shape_function(shape_function_enum) # Assign values to config struct for c++ land - config = dic2dcpp.Config() + config = diccpp.Config() config.ss_step = subset_step config.ss_size = subset_size config.max_iter = max_iterations config.precision = precision config.threshold = threshold - config.bf_threshold = bf_threshold config.max_disp = max_displacement - config.corr_crit = correlation_criteria - config.shape_func = shape_function - config.interp_routine = interpolation_routine - config.scan_method = method - config.px_hori = image_stack.shape[2] - config.px_vert = image_stack.shape[1] - config.num_def_img = image_stack.shape[0]-1 # subtract ref image + config.corr_crit = getattr(diccpp.CorrCrit, correlation_criteria_enum.name) + config.shape_func = getattr(diccpp.ShapeFunc, shape_function_enum.name) + config.interp_routine = getattr(diccpp.InterpRoutine, interpolation_routine_enum.name) + config.shape_func = getattr(diccpp.ShapeFunc, shape_function_enum.name) + config.scan_method = getattr(diccpp.ScanMethod, method_enum.name) + config.incremental = incremental + config.incremental_update_cond = getattr(diccpp.IncrementalCond, incremental_update_condition_enum.name) + config.incremental_update_val = incremental_update_value + config.num_params = num_params - config.rg_seed = updated_seed - config.filenames = filenames - config.fft_mad = fft_mad - config.fft_mad_scale = fft_mad_scale + config.px_hori = w + config.px_vert = h + config.num_def_img = len(basenames)-1 # subtract ref image + config.num_params = num_params + config.rg_seeds = updated_seeds + config.basenames = basenames + config.fullpaths = fullpaths + config.fft_filter = fft_filter + config.fft_filter_threshold = fft_filter_threshold + config.fft_filter_radius = fft_filter_radius + config.fft_filter_corr_power = fft_filter_corr_power + config.fft_save = fft_save config.debug_level = debug_level + config.epi_distance = 0 + + # sort precision to use for FFT windowing + if fft_precision=="F32": + config.fft_precision = diccpp.FFTPrecision.FLOAT32 + elif fft_precision=="F64": + config.fft_precision = diccpp.FFTPrecision.FLOAT64 + else: + raise ValueError("fft_precision must be one of: F64, F32") + + multiwindowconf = diccpp.MultiwindowConfig() + multiwindowconf.overlap = mw_overlap + multiwindowconf.subset_size = mw_subset_size + multiwindowconf.search_area = mw_search_area # assigning c++ struct vals for save config saveconf = common_cpp.SaveConfig() @@ -176,15 +276,66 @@ def calculate_2d(reference: np.ndarray | str | Path, saveconf.binary = output_binary saveconf.prefix = output_prefix saveconf.delimiter = output_delimiter - saveconf.at_end = output_at_end saveconf.output_below_threshold = output_below_threshold saveconf.shape_params = output_shape_params + #TODO: sort this out so you can actually read in intrinsic parameters for + # single camera DIC + # Convert cam0 + cpp_cam0 = calibcpp.CamIntrinsics() + cpp_cam0.fx = 0.0 + cpp_cam0.fy = 0.0 + cpp_cam0.fs = 0.0 + cpp_cam0.cx = 0.0 + cpp_cam0.cy = 0.0 + cpp_cam0.distortion = [0.0,0.0,0.0,0.0,0.0] + + # Convert cam1 + cpp_cam1 = calibcpp.CamIntrinsics() + cpp_cam1.fx = 0.0 + cpp_cam1.fy = 0.0 + cpp_cam1.fs = 0.0 + cpp_cam1.cx = 0.0 + cpp_cam1.cy = 0.0 + cpp_cam1.distortion = [0.0,0.0,0.0,0.0,0.0] + + # Create C++ Calib object + calib = calibcpp.Calib() + calib.cam0 = cpp_cam0 + calib.cam1 = cpp_cam1 + calib.rotation = [0.0,0.0,0.0] + calib.translation = [0.0,0.0,0.0] + + + config.stereo = False + #set the number of OMP threads if num_threads is not None: common_cpp.set_num_threads(num_threads) + dicchecks.print_config_summary( + w, h, config.num_def_img, max_iterations, correlation_criteria, + shape_function, interpolation_routine, fft_filter, + fft_filter_threshold, fft_filter_radius, fft_filter_corr_power, method, + precision, threshold, max_displacement, subset_size, subset_step, + num_threads, debug_level, updated_seeds, None + ) + # calling the c++ dic engine - with dic2dcpp.ostream_redirect(stdout=True, stderr=True): - dic2dcpp.dic_engine(image_stack, roi_c, config, saveconf) + with diccpp.ostream_redirect(stdout=True, stderr=True): + diccpp.engine(roi_c, calib, config, multiwindowconf, saveconf) + + + # if there's a temp dir and the reference and deformed are np.ndarray + if temp_dir is not None and isinstance(reference, np.ndarray) and isinstance(deformed, np.ndarray): + + # delete each file in filename + for filename in os.listdir(temp_dir): + file_path = os.path.join(temp_dir, filename) + if os.path.isfile(file_path): + os.remove(file_path) + + os.rmdir(temp_dir) + + diff --git a/src/pyvale/dic/dic3d.py b/src/pyvale/dic/dic3d.py new file mode 100644 index 000000000..acc5b51c1 --- /dev/null +++ b/src/pyvale/dic/dic3d.py @@ -0,0 +1,345 @@ +# ================================================================================ +# pyvale: the python validation engine +# License: MIT +# Copyright (C) 2025 The Computer Aided Validation Team +# ================================================================================ + +import os +from logging import debug +import numpy as np +from pathlib import Path +from typing import Literal + +# pyvale +import pyvale.dic.diccpp as diccpp +import pyvale.calib.calibcpp as calibcpp +import pyvale.dic.dicchecks as dicchecks +import pyvale.common_py.util as common_py_util +from pyvale.calib.calibdataclass import Calib +import pyvale.common_cpp.common_cpp as common_cpp + + +def calculate_3d(reference: list[np.ndarray] | list[str] | list[Path], + deformed: list[np.ndarray] | list[str] | list[Path], + roi_mask: np.ndarray, + calibration: Calib, + seed: list[int] | list[np.int32] | list[tuple[int, int]] | np.ndarray, + subset_size: int = 21, + subset_step: int = 10, + correlation_criteria: Literal["ZNSSD","NSSD","SSD"]="ZNSSD", + shape_function: Literal["AFFINE","QUAD","RIGID"]="AFFINE", + interpolation_routine: Literal["BSPLINE","HERMITE"]="BSPLINE", + max_iterations: int=40, + precision: float=0.001, + threshold: float=0.9, + num_threads: int | None = None, + max_displacement: int=128, + epi_distance: int=300, + method: Literal["MULTIWINDOW_RG","SINGLEWINDOW_RG","MULTIWINDOW","RASTER"] = "MULTIWINDOW_RG", + incremental: bool=False, + incremental_update_condition: Literal["IMAGE","COST","ITER"]="IMAGE", + incremental_update_value: float | int=1, + multiwindow_overlap: float=0.0, + multiwindow_subset_sizes: list[int] = [], + multiwindow_search_areas: list[int] = [], + fft_filter: bool=True, + fft_filter_threshold: float=3.0, + fft_filter_radius: int=3, + fft_filter_corr_power: float=2.0, + fft_save: bool=False, + fft_precision: Literal["F64","F32"]="F32", + output_basepath: Path | str = "./", + output_binary: bool=False, + output_prefix: str="dic_results_", + output_delimiter: str=",", + output_below_threshold: bool=False, + output_shape_params: bool=False, + debug_level: int=1) -> None: + + """ + Perform Stereo Digital Image Correlation (DIC) between a reference image and one or more deformed images. + + This function wraps a C++ DIC engine by preparing configuration parameters, + performing input validation, and dispatching image data and settings. It supports + pixel-level displacement and strain measurement over a defined region of interest (ROI). + + Parameters + ---------- + reference : np.ndarray, str or pathlib.Path + The reference image (2D array) or path to the image file. + deformed : np.ndarray, str , pathlib.Path or list[pathlib.Path] + The deformed image(s) (3D array for multiple images) or path/pattern to image files. + roi_mask : np.ndarray + A binary mask indicating the Region of Interest (ROI) for analysis (same size as image). + seed : list[int], list[np.int32], list[tuple[int, int]] or np.ndarray + Coordinates of the seed points for Reliability-Guided (RG) scanning. Accepts either + the existing flat format ``[x0, y0, x1, y1, ...]`` or tuple format + ``[(x0, y0), (x1, y1), ...]``. If the method is not RG, this will be ignored. + subset_size : int, optional + Size of the square subset window in pixels (default: 21). + subset_step : int, optional + Step size between subset centers in pixels (default: 10). + correlation_criteria : str, optional + Metric for matching subsets: ``"ZNSSD"``, ``"NSSD"`` or ``"SSD"`` (default: ``"ZNSSD"``). + shape_function : str, optional + Deformation model: e.g., "AFFINE", "RIGID" (default: "AFFINE"). + interpolation_routine : str, optional + Interpolation method used on image intensity. Options are ``"BSPLINE"`` and ``"HERMITE"``. + Implementation details can be found in our DIC theory documentation. (default: ``“BSPLINE”``). + max_iterations : int, optional + Maximum number of iterations allowed for subset optimization (default: 40). + precision : float, optional + Precision threshold for iterative optimization convergence (default: 0.001). + threshold : float, optional + Minimum correlation/cost coefficient value to be considered a matching subset (default: 0.9). + num_threads : int, optional + Number of threads to use for parallel computation (default: None, uses all available). + max_displacement : int, optional + Estimate for the Maximum displacement for images from the same camera in any + direction (in pixels) (default: 128). + epi_distance : int, optional + Estimate for the maximum distance along the epipolar line (in pixels) between a identical point in + the left and right image (default: 300). + method : str, optional + The core algorithmic method used to perform the DIC. + + Options include: + + * ``"MULTIWINDOW_RG"``: Multi-window Reliability-Guided DIC + (best overall approach). + + * ``"SINGLEWINDOW_RG"``: Uses a single window for the rigid estimate + for each subset. The size of the window is determined by the + ``max_displacement`` parameter. + + * ``"MULTIWINDOW"``: Uses only the multi-window FFT strategy. + + * ``"RASTER"``: No FFT initialization. Performs a raster scan of + the image. + + incremental : bool, optional + If True, then references images will be updated depending on the + condition set by argument `incremental_update_condition`. This is useful + for large deformations where the original reference may no longer be + valid for tracking. If False, the original reference image(s) will be + used for tracking all deformed images (default: False). + incremental_update_condition : str, optional + Condition for updating reference images when ``incremental`` is True. Options include: + ``"IMAGE"`` to update every ``N`` images, ``"COST"`` to update when the average ZNCC cost + value falls below a threshold, ``"ITER"`` to update when the average number + of subset optimizer iterations exceeds a threshold. (default: `"PER_IMAGE"`). + incremental_update_value : float, optional + Value corresponding to the ``incremental_update_condition``. For example, + if the condition is "IMAGE", this would be the number of images after + which to update the reference. If the condition is ``"COST"``, this would be + the cost threshold for updating. If the condition is ``"ITER"``, this would + be the iteration threshold for updating. (default: 1). + multiwindow_overlap : int, optional + For multi-window methods, the percentage overlap between adjacent FFT windows + at each level (default: 50). + multiwindow_template : list[int], optional + List of template window sizes for the multi-window FFT approach. If + None, defaults to powers of 2 with the largest window size determined by + the next power of 2 above ``max_displacement`` (default: ``None``). + multiwindow_search : list[int], optional + List of search window sizes for the multi-window FFT approach. If None, + defaults to the corresponding template window size (default: ``None``). + fft_filter : bool, optional + Enables outlier filtering for rigid FFT displacement estimates at each + FFTCC window size. (default: ``False``) + fft_filter_threshold : float, optional + Rejection threshold for the FFT displacement outlier filter. Larger + values are more tolerant, while smaller values reject more vectors. + (default: ``3.0``) + fft_filter_radius : int, optional + Neighbourhood radius, in subset-grid steps, used by the FFT displacement + outlier filter. (default: ``3``) + fft_filter_corr_power : float, optional + Exponent applied to correlation confidence when weighting neighbours in + the FFT displacement outlier filter. (default: ``2.0``) + fft_precision : str, optional + Floating-point precision for FFT-only windowing buffers. Options are ``"F32"`` + for single precision and ``"F64"`` for double precision. (default: ``"F32"``). + output_basepath : str or pathlib.Path, optional + Directory path where output files will be written (default: ``"./"``). + output_binary : bool, optional + Whether to write output in binary format (default: False). + output_prefix : str, optional + Prefix for all output files (default: ``"dic_results_"``). results will be + named with output_prefix + original filename. THe extension will be + changed to ``".csv"`` or ``".dic2d"`` depending on whether outputting as a binary. + output_delimiter : str, optional + Delimiter used in text output files (default: ``","``). + output_below_threshold : bool, optional + If ``True``, subset results with cost values that did not exceed the cost threshold + will still be present in output (default: ``False``). + output_shape_params : bool, optional + If True, all shape parameters will be saved in the output files (default: ``False``). + debug_level: + + Returns + ------- + None + All outputs are written to files; no values are returned. + + Raises + ------ + ValueError + If input checks fail (e.g., invalid image sizes, unsupported parameters). + FileNotFoundError + If provided file paths do not exist. + """ + + + + if (debug_level>0): + common_py_util.print_pyvale_banner() + common_py_util.print_title("Initial Checks") + + # make sure ROI is in the correct format + roi_c = np.ascontiguousarray(roi_mask) + + # do checks on vars in python land + basenames0, fullpaths0, w0, h0, temp_dir = dicchecks.check_images(reference[0],deformed[0],roi_mask,debug_level) + basenames1, fullpaths1, w1, h1, temp_dir = dicchecks.check_images(reference[1],deformed[1],roi_mask,debug_level) + + assert(w0 == w1) + assert(h0 == h1) + assert(len(basenames0) == len(basenames1)) + assert(len(basenames0) == len(basenames1)) + assert(len(fullpaths0) == len(fullpaths1)) + basenames = basenames0 + basenames1 + fullpaths = fullpaths0 + fullpaths1 + + # string to enum + method_enum = dicchecks.ScanMethod(method) + shape_function_enum = dicchecks.Shape(shape_function) + correlation_criteria_enum = dicchecks.CorrCrit(correlation_criteria) + interpolation_routine_enum = dicchecks.Interp(interpolation_routine) + incremental_update_condition_enum = dicchecks.IncrementalMethod(incremental_update_condition) + + # checks on the config + mw_overlap, mw_subset_size, mw_search_area = dicchecks.multiwindow_init(subset_size, + subset_step, + max_displacement, + multiwindow_overlap, + multiwindow_subset_sizes, + multiwindow_search_areas) + + + + # checks on the config + dicchecks.check_thresholds(threshold, precision) + common_py_util.check_output_directory(str(output_basepath), output_prefix, debug_level) + dicchecks.check_subsets(subset_size, subset_step) + updated_seeds = dicchecks.check_and_update_rg_seed(seed, roi_mask, method, w0, h0, subset_size, subset_step) + num_params = dicchecks.check_shape_function(shape_function_enum) + + # Assign values to config struct for c++ land + config = diccpp.Config() + config.ss_step = subset_step + config.ss_size = subset_size + config.max_iter = max_iterations + config.precision = precision + config.threshold = threshold + config.corr_crit = getattr(diccpp.CorrCrit, correlation_criteria_enum.name) + config.shape_func = getattr(diccpp.ShapeFunc, shape_function_enum.name) + config.interp_routine = getattr(diccpp.InterpRoutine, interpolation_routine_enum.name) + config.shape_func = getattr(diccpp.ShapeFunc, shape_function_enum.name) + config.scan_method = getattr(diccpp.ScanMethod, method_enum.name) + config.incremental = incremental + config.incremental_update_cond = getattr(diccpp.IncrementalCond, incremental_update_condition_enum.value) + config.incremental_update_val = incremental_update_value + config.px_hori = w0 + config.px_vert = h0 + config.num_def_img = len(basenames0)-1 # subtract ref image + config.num_params = num_params + config.rg_seeds = updated_seeds + config.basenames = basenames + config.fullpaths = fullpaths + config.fft_filter = fft_filter + config.fft_filter_threshold = fft_filter_threshold + config.fft_filter_radius = fft_filter_radius + config.fft_filter_corr_power = fft_filter_corr_power + config.fft_save = fft_save + config.debug_level = debug_level + config.epi_distance = epi_distance + config.max_disp = max_displacement + + # sort precision to use for FFT windowing + if fft_precision=="F32": + config.fft_precision = diccpp.FFTPrecision.FLOAT32 + elif fft_precision=="F64": + config.fft_precision = diccpp.FFTPrecision.FLOAT64 + else: + raise ValueError("fft_precision must be one of: F64, F32") + + multiwindowconf = diccpp.MultiwindowConfig() + multiwindowconf.overlap = mw_overlap + multiwindowconf.subset_size = mw_subset_size + multiwindowconf.search_area = mw_search_area + + # assigning c++ struct vals for save config + saveconf = common_cpp.SaveConfig() + saveconf.basepath = str(output_basepath) + saveconf.binary = output_binary + saveconf.prefix = output_prefix + saveconf.delimiter = output_delimiter + saveconf.output_below_threshold = output_below_threshold + saveconf.shape_params = output_shape_params + + + # Convert cam0 + cpp_cam0 = calibcpp.CamIntrinsics() + cpp_cam0.fx = calibration.cam0.fx + cpp_cam0.fy = calibration.cam0.fy + cpp_cam0.fs = calibration.cam0.fs + cpp_cam0.cx = calibration.cam0.cx + cpp_cam0.cy = calibration.cam0.cy + cpp_cam0.distortion = calibration.cam0.distortion.tolist() + + # Convert cam1 + cpp_cam1 = calibcpp.CamIntrinsics() + cpp_cam1.fx = calibration.cam1.fx + cpp_cam1.fy = calibration.cam1.fy + cpp_cam1.fs = calibration.cam1.fs + cpp_cam1.cx = calibration.cam1.cx + cpp_cam1.cy = calibration.cam1.cy + cpp_cam1.distortion = calibration.cam1.distortion.tolist() + + # Create C++ Calib object + calib = calibcpp.Calib() + calib.cam0 = cpp_cam0 + calib.cam1 = cpp_cam1 + calib.rotation = calibration.rotation + calib.translation = calibration.translation + + + config.stereo = True + + #set the number of OMP threads + if num_threads is not None: + common_cpp.set_num_threads(num_threads) + + dicchecks.print_config_summary( + w0, h0, config.num_def_img, max_iterations, correlation_criteria, + shape_function, interpolation_routine, fft_filter, + fft_filter_threshold, fft_filter_radius, fft_filter_corr_power, method, + precision, threshold, max_displacement, subset_size, subset_step, + num_threads, debug_level, updated_seeds, epi_distance + ) + + # calling the c++ dic engine + with diccpp.ostream_redirect(stdout=True, stderr=True): + diccpp.engine(roi_c, calib, config, multiwindowconf, saveconf) + + + if temp_dir is not None: + + # delete each file in filename + for filename in os.listdir(temp_dir): + file_path = os.path.join(temp_dir, filename) + if os.path.isfile(file_path): + os.remove(file_path) + + os.rmdir(temp_dir) diff --git a/src/pyvale/dic/dicchecks.py b/src/pyvale/dic/dicchecks.py index 0b402a12f..802baebab 100644 --- a/src/pyvale/dic/dicchecks.py +++ b/src/pyvale/dic/dicchecks.py @@ -10,29 +10,124 @@ import sys from PIL import Image from pathlib import Path +from enum import Enum + +import pyvale.common_py.util as common_py_util """ This module contains functions for checking arguments passed to the 2D DIC Engine. """ +class ScanMethod(str, Enum): + MULTIWINDOW_RG = "MULTIWINDOW_RG" + SINGLEWINDOW_RG = "SINGLEWINDOW_RG" + MULTIWINDOW = "MULTIWINDOW" + RASTER = "RASTER" + +class Shape(str, Enum): + RIGID = "RIGID" + AFFINE = "AFFINE" + QUAD = "QUAD" + +class CorrCrit(str, Enum): + SSD = "SSD" + NSSD = "NSSD" + ZNSSD = "ZNSSD" + +class Interp(str, Enum): + BSPLINE = "BSPLINE" + HERMITE = "HERMITE" + +class IncrementalMethod(str, Enum): + IMAGE = "IMAGE" + COST = "COST" + ITER = "ITER" + + +def multiwindow_init(subset_size: int, + subset_step: int, + max_displacement: int, + multiwindow_overlap: float, + multiwindow_subset_size: list[int], + multiwindow_search_area: list[int]) -> tuple[list[int], list[int], list[int]]: + + + # check multiwindow_subset_size and multiwindow_search_area are same length + if len(multiwindow_subset_size) != len(multiwindow_search_area): + raise ValueError(f"multiwindow_subset_size and multiwindow_search_area must be the same length. " + f"Got lengths {len(multiwindow_subset_size)} and {len(multiwindow_search_area)}") + + # check if multiwindow_subset_size and multiwindow_search_area are descending + if any(multiwindow_subset_size[i] < multiwindow_subset_size[i+1] for i in range(len(multiwindow_subset_size)-1)): + raise ValueError(f"multiwindow_subset_size must be in descending order. " + f"Got {multiwindow_subset_size}") + + if any(multiwindow_search_area[i] < multiwindow_search_area[i+1] for i in range(len(multiwindow_search_area)-1)): + raise ValueError(f"multiwindow_search_area must be in descending order. " + f"Got {multiwindow_search_area}") + + # check if the overlap is a value between 0 and 100 + if multiwindow_overlap < 0 or multiwindow_overlap > 1: + raise ValueError(f"multiwindow_overlap must be a fractional value between 0 and 1." + f"Got {multiwindow_overlap}") + + # if they are both empty then use max_displacement as the largest subset_size and multiwindow_search_area + if len(multiwindow_subset_size) == 0 and len(multiwindow_search_area) == 0: + + # get descending powers from max_displacement down + powers_of_two = [2**i for i in range(int(np.floor(np.log2(max(2*max_displacement, subset_size)))), -1, -1)] + + # if elements of power_of_two are less than subset_size then remove them + powers_of_two = [p for p in powers_of_two if p >= subset_size] + + # only append max_displacement if it is greater than or equal to subset_size + if 2*max_displacement >= subset_size: + multiwindow_subset_size = [2*max_displacement] + powers_of_two + multiwindow_search_area = [2*max_displacement] + powers_of_two + else: + multiwindow_subset_size = powers_of_two + multiwindow_search_area = powers_of_two + + # check that all multiwindow_subset_sizes are less than or equal to the + # multiwindow_search_area elements + for i in range(len(multiwindow_subset_size)): + if multiwindow_subset_size[i] > multiwindow_search_area[i]: + raise ValueError(f"multiwindow_subset_size elements must be less than or equal to the corresponding " + f"multiwindow_search_area elements. Got {multiwindow_subset_size[i]} and " + f"{multiwindow_search_area[i]} at index {i}") + + overlap = [x * (1.0-multiwindow_overlap) for x in multiwindow_subset_size] + overlap.append(subset_step) + overlap = list(map(int,overlap)) + + multiwindow_subset_size.append(subset_size) + multiwindow_search_area.append(subset_size) + + return overlap, multiwindow_subset_size, multiwindow_search_area + + + + + def check_correlation_criteria(correlation_criteria: str) -> None: """ Validate that the correlation criteria is one of the allowed values. - Checks whether input `correlation_criteria` is among the - accepted options: "SSD", "NSSD", or "ZNSSD". If not, raises a `ValueError`. + Checks whether input ``correlation_criteria`` is among the + accepted options: ``"SSD"``, ``"NSSD"``, or ``"ZNSSD"``. If not, raises a + ``ValueError``. Parameters ---------- correlation_criteria : str - The correlation type. Must be one of: "SSD", "NSSD", or "ZNSSD". + The correlation type. Must be one of: ``"SSD"``, ``"NSSD"``, or ``"ZNSSD"``. Raises ------ ValueError - If `correlation_criteria` is not one of the allowed values. + If ``correlation_criteria`` is not one of the allowed values. """ allowed_values = {"SSD", "NSSD", "ZNSSD"} @@ -44,41 +139,31 @@ def check_correlation_criteria(correlation_criteria: str) -> None: -def check_shape_function(shape_function: str) -> int: +def check_shape_function(shape: Shape) -> int: """ - Checks whether input `shape_function` is one of the allowed - values ("RIGID", "AFFINE" or "QUAD"). If valid, it returns the number of transformation - parameters associated with that shape function. + Returns the number of parameters associated with that shape function. Parameters ---------- shape_function : str - The shape function type. Must be either "RIGID", "AFFINE" or "QUAD". + The shape function type. Must be either ``RIGID``, ``AFFINE`` or ``QUAD``. Returns ------- int The number of parameters for the specified shape function: - - 2 for "RIGID" - - 6 for "AFFINE" - - 12 for "QUAD" - - Raises - ------ - ValueError - If `shape_function` is not one of the allowed values. + - 2 for ``RIGID`` + - 6 for ``AFFINE`` + - 12 for ``QUAD`` """ - if (shape_function=="RIGID"): + if (shape==Shape.RIGID): num_params = 2 - elif (shape_function=="AFFINE"): + elif (shape==Shape.AFFINE): num_params = 6 - elif (shape_function=="QUAD"): + elif (shape==Shape.QUAD): num_params = 12 - else: - raise ValueError(f"Invalid shape_function: {shape_function}. " - f"Allowed values are: 'AFFINE', 'RIGID', 'QUAD'.") - + return num_params @@ -88,18 +173,18 @@ def check_interpolation(interpolation_routine: str) -> None: Validate that the interpolation routine is one of the allowed methods. Checks whether interpolation_routine is a supported - interpolation method. Allowed values are "BSPLINE" and "HERMITE". If the input - is not one of these, a `ValueError` is raised. + interpolation method. Allowed values are ``"BSPLINE"`` and ``"HERMITE"``. If the input + is not one of these, a ``ValueError`` is raised. Parameters ---------- interpolation_routine : str - The interpolation method to validate. Must be either "BSPLINE" or "HERMITE". + The interpolation method to validate. Must be either ``"BSPLINE"`` or ``"HERMITE"``. Raises ------ ValueError - If `interpolation_routine` is not a supported value. + If ``interpolation_routine`` is not a supported value. """ @@ -115,21 +200,20 @@ def check_interpolation(interpolation_routine: str) -> None: def check_method(method: str) -> None: """ Validate that the scan type one of the allowed methods. - Allowed values are "MULTIWINDOW_RG", "MULTIWINDOW", "SINGLEWINDOW_RG", "SINGLEWINDOW_RG_INCREMENTAL", "IMAGE_SCAN". Parameters ---------- - interpolation_routine : str - The interpolation method to validate. Must be either "BILINEAR" or "BICUBIC". + method : str + Allowed values are ``"MULTIWINDOW_RG"``, ``"MULTIWINDOW"``, ``"SINGLEWINDOW_RG"``, ``"RASTER"``. Raises ------ ValueError - If `interpolation_routine` is not a supported value. + If ``method`` is not a supported value. """ - allowed_values = {"MULTIWINDOW_RG", "MULTIWINDOW", "SINGLEWINDOW_RG", "SINGLEWINDOW_RG_INCREMENTAL", "IMAGE_SCAN"} + allowed_values = {"MULTIWINDOW_RG", "MULTIWINDOW", "SINGLEWINDOW_RG", "SINGLEWINDOW_RG", "RASTER"} if method not in allowed_values: raise ValueError(f"Invalid method: {method}. " @@ -138,18 +222,15 @@ def check_method(method: str) -> None: def check_thresholds(threshold: float, - bf_threshold: float, precision: float) -> None: """ - Ensures that `threshold`, `bf_threshold`, and `precision` - are all floats strictly between 0 and 1. Raises a `ValueError` if any condition fails. + Ensures that ``threshold``, and ``precision`` + are all floats strictly between 0 and 1. Raises a ``ValueError`` if any condition fails. Parameters ---------- threshold : float correlation/cost coeff minumum value to be considered matching subset. - bf_threshold : float - Threshold for the brute-force optimization method. precision : float Desired precision for the optimizer. @@ -163,10 +244,6 @@ def check_thresholds(threshold: float, raise ValueError("threshold must be a float " "strictly between 0 and 1.") - if not (0 < bf_threshold < 1): - raise ValueError("bf_threshold must be a float " - "strictly between 0 and 1.") - if not (0 < precision < 1): raise ValueError("Optimizer precision must be a float strictly " "between 0 and 1.") @@ -198,7 +275,13 @@ def check_subsets(subset_size: int, subset_step: int) -> None: -def check_and_update_rg_seed(seed: list[int] | list[np.int32] | np.ndarray, roi_mask: np.ndarray, method: str, px_hori: int, px_vert: int, subset_size: int, subset_step: int) -> list[int]: +def check_and_update_rg_seed(seed: list[int] | list[np.int32] | list[tuple[int, int]] | np.ndarray, + roi_mask: np.ndarray, + method: str, + px_hori: int, + px_vert: int, + subset_size: int, + subset_step: int) -> list[int]: """ Validate and update the region-growing seed location to align with image bounds and subset spacing. @@ -206,13 +289,14 @@ def check_and_update_rg_seed(seed: list[int] | list[np.int32] | np.ndarray, roi_ scanning method. It adjusts the seed to the nearest valid grid point based on the subset step size, clamps it to the image dimensions, and ensures it lies within the region of interest (ROI) mask. - If the scanning method is not "RG", the function returns a default seed of [0, 0]. + If the scanning method is not reliability guided, the function returns a default seed of [0, 0]. This seed is not used any other scan method methods. Parameters ---------- - seed : list[int], list[np.int32] or np.ndarray - The initial seed coordinates as a list of two integers: [x, y]. + seed : list[int], list[np.int32], list[tuple[int, int]] or np.ndarray + Initial seed coordinates as either a flat list ``[x0, y0, x1, y1, ...]`` or a list of + coordinate tuples ``[(x0, y0), (x1, y1), ...]``. roi_mask : np.ndarray A 2D binary mask (same size as the image) indicating the region of interest. method : str @@ -227,63 +311,115 @@ def check_and_update_rg_seed(seed: list[int] | list[np.int32] | np.ndarray, roi_ Returns ------- list of int - The adjusted seed coordinates [x, y] aligned to the subset grid and within bounds. + The adjusted seed coordinates flattened as ``[x0, y0, x1, y1, ...]`` for the C++ engine. Raises ------ ValueError - If the seed is improperly formatted, out of image bounds, or not a list of two integers. + If the seed is improperly formatted or out of image/ROI bounds. """ if "RG" not in method: return [0,0] - if (len(seed) != 2): - raise ValueError(f"Reliability Guided seed does not have two elements: " \ - f"seed={seed}. Seed " \ - f" must be a list of two integers: seed=[x, y]") + valid_int_types = (int, np.integer) + + if isinstance(seed, np.ndarray): + seed_values = seed.tolist() + else: + seed_values = seed + + if not isinstance(seed_values, list) or len(seed_values) == 0: + raise ValueError( + "Reliability Guided seed must contain one or more seed points in either " + "[x0, y0, x1, y1, ...] or [(x0, y0), (x1, y1), ...] format." + ) + + if all(isinstance(seed_point, tuple) for seed_point in seed_values): + if not all( + len(seed_point) == 2 + and all(isinstance(coord, valid_int_types) for coord in seed_point) + for seed_point in seed_values + ): + raise ValueError( + "Reliability Guided seed tuples must each contain two integers: " + "seed=[(x0, y0), (x1, y1), ...]" + ) + seed_points = seed_values + elif all(isinstance(seed_point, list) for seed_point in seed_values): + if not all( + len(seed_point) == 2 + and all(isinstance(coord, valid_int_types) for coord in seed_point) + for seed_point in seed_values + ): + raise ValueError( + "Reliability Guided seed coordinate lists must each contain two integers: " + "seed=[[x0, y0], [x1, y1], ...]" + ) + seed_points = [tuple(seed_point) for seed_point in seed_values] + else: + if len(seed_values) < 2 or len(seed_values) % 2 != 0: + raise ValueError( + "Reliability Guided seed must contain one or more seed points in either " + "[x0, y0, x1, y1, ...] or [(x0, y0), (x1, y1), ...] format." + ) + if not all(isinstance(coord, valid_int_types) for coord in seed_values): + raise ValueError( + "Reliability Guided seed must contain integer coordinates in either " + "[x0, y0, x1, y1, ...] or [(x0, y0), (x1, y1), ...] format." + ) + seed_points = list(zip(seed_values[::2], seed_values[1::2])) + + updated_seeds = [] - if not isinstance(seed, (list, np.ndarray)) or not all(isinstance(coord, (int, np.int32)) for coord in seed): - raise ValueError("Reliability Guided seed must be a list of two integers: seed=[x, y]") + for idx, seed_point in enumerate(seed_points): - x, y = seed + x, y = seed_point + if x < 0 or x >= px_hori or y < 0 or y >= px_vert: + raise ValueError(f"Seed {idx} ({x}, {y}) goes outside the image bounds: ({px_hori}, {px_vert})") - if x < 0 or x >= px_hori or y < 0 or y >= px_vert: - raise ValueError(f"Seed ({x}, {y}) goes outside the image bounds: ({px_hori}, {px_vert})") + corner_x = x - subset_size//2 + corner_y = y - subset_size//2 - corner_x = x - subset_size//2 - corner_y = y - subset_size//2 + def round_to_step(value: int, step: int) -> int: + return round(value / step) * step - def round_to_step(value: int, step: int) -> int: - return round(value / step) * step + # snap to grid + new_x = round_to_step(corner_x, subset_step) + new_y = round_to_step(corner_y, subset_step) - # snap to grid - new_x = round_to_step(corner_x, subset_step) - new_y = round_to_step(corner_y, subset_step) + # check if all pixel values within the seed location are within the ROI + # seed coordinates are the central pixel to the subset + max_x = new_x + subset_size//2+1 + max_y = new_y + subset_size//2+1 - # check if all pixel values within the seed location are within the ROI - # seed coordinates are the central pixel to the subset - max_x = new_x + subset_size//2+1 - max_y = new_y + subset_size//2+1 - # Check if all pixel values in the ROI are valid - for i in range(new_x, max_x): - for j in range(new_y, max_y): + # check whether all values in the roi_mask are 0 + all_zeros = not np.any(roi_mask) + if (all_zeros): + raise ValueError("All values in the ROI mask are 0. Please check the " + "ROI mask and try again.") - if i < 0 or i >= px_hori or j < 0 or j >= px_vert: - raise ValueError(f"Seed ({x}, {y}) goes outside the image bounds at pixel ({i}, {j})") + # Check if all pixel values in the ROI are valid + for i in range(new_x, max_x): + for j in range(new_y, max_y): - if not roi_mask[j, i]: - raise ValueError(f"Seed ({x}, {y}) goes outside the ROI at pixel ({i}, {j})") + if i < 0 or i >= px_hori or j < 0 or j >= px_vert: + raise ValueError(f"Seed {idx} ({x}, {y}) goes outside the image bounds at pixel ({i}, {j})") - return [new_x, new_y] + if not roi_mask[j, i]: + raise ValueError(f"Seed {idx} ({x}, {y}) goes outside the ROI at pixel ({i}, {j})") + updated_seeds.append(new_x) + updated_seeds.append(new_y) -def check_and_get_images(reference: np.ndarray | str | Path, - deformed: np.ndarray | str | Path | list[Path], - roi: np.ndarray, debug_level: int) -> tuple[np.ndarray, np.ndarray, list[str]]: + return updated_seeds + +def check_images(reference: np.ndarray | str | Path, + deformed: np.ndarray | str | Path | list[Path], + roi: np.ndarray, debug_level: int) -> tuple[list[str], list[str], int, int, Path | None]: """ - Load and validate reference and deformed images, checks consistency in shape/format. + Validate reference and deformed images, checks consistency in shape/format. This function accepts either: - A file path to a reference image and a glob pattern for a sequence of deformed image files, or @@ -305,28 +441,38 @@ def check_and_get_images(reference: np.ndarray | str | Path, or a glob pattern string pointing to multiple image files. roi : np.ndarray A 2D NumPy array defining the region of interest. Must match the reference image shape - if `reference` is an array. + if ``reference`` is an array. debug_level: int Determines how much information to provide in console output. Returns ------- - image_stack: np.ndarray - A 3D NumPy array containing all deformed images with shape (N, H, W). - filenames : list of str + basename : list of str List of base filenames of all images (empty if images are passed as arrays). + fullpath : list of str + List of full paths of all images (empty if images are passed as arrays). + w : int + Width of the images in pixels. + h : int + Height of the images in pixels. + temp_dir : pathlib.Path or None + Path to the temporary directory created to store array-based images on disk. + ``None`` if file-based input was used. Caller is responsible for cleanup + (e.g. ``shutil.rmtree(temp_dir)``). Raises ------ ValueError - If there is a type mismatch between `reference` and `deformed`, + If there is a type mismatch between ``reference`` and ``deformed``, if image files are not found or unreadable, or if image shapes do not match. FileNotFoundError If no files are found matching the deformed image pattern. """ - filenames = [] + basename = [] + fullpath = [] + temp_dir = None # Normalize Path or str to Path if isinstance(reference, (str, Path)): @@ -334,7 +480,7 @@ def check_and_get_images(reference: np.ndarray | str | Path, if isinstance(deformed, (str, Path)): deformed = Path(deformed) - # check matching filetypes + # check matching filetypes if isinstance(reference, np.ndarray): # both must be arrays if not isinstance(deformed, np.ndarray): @@ -350,106 +496,163 @@ def check_and_get_images(reference: np.ndarray | str | Path, # File-based input if isinstance(reference, Path): - assert isinstance(reference, Path) - if not reference.is_file(): raise ValueError(f"Reference image does not exist: {reference}") + if debug_level > 0: + common_py_util.info("Ref img: " + str(reference)) - if (debug_level>0): - print("Using reference image: ") - print(f" - {reference}\n") - - # Load reference image - ref_arr = np.array(Image.open(reference)) - - if ref_arr.ndim == 3: - if (debug_level>0): - print(f"Reference image appears to have {ref_arr.shape[2]} channels. Using channel 0.") - ref_arr = ref_arr[:, :, 0] + ref_img = Image.open(reference) - if (debug_level>0): - print(f"Reference image shape: {ref_arr.shape}") - print("") + if debug_level > 0: + common_py_util.info(f"Ref img shape: {ref_img.size}") - filenames.append(os.path.basename(reference)) + basename.append(os.path.basename(reference)) + fullpath.append(str(reference)) if isinstance(deformed, Path): files = sorted(glob.glob(str(deformed))) else: - files = [str(p) for p in deformed] + files = sorted(deformed, key=lambda p: os.path.basename(p)) if not files: raise FileNotFoundError(f"No deformation images found: {deformed}") + if debug_level > 1: + common_py_util.info(f"Found {len(files)} deformation images in dir: {os.path.dirname(files[0])}") - - if debug_level > 0: - print(f"Found {len(files)} deformation images:") - for file in files: - print(f" - {file}") - print("") - - # populate filenames list. Stars with ref image. - filenames.extend(os.path.basename(f) for f in files) - - def_arr = np.zeros((len(files), *ref_arr.shape), dtype=ref_arr.dtype) + basename.extend(os.path.basename(f) for f in files) + fullpath.extend(str(f) for f in files) for i, file in enumerate(files): - img = np.array(Image.open(file)) - if img.ndim == 3: - print(f"Deformed image {file} appears to have {img.shape[2]} channels. Using channel 0.") - img = img[:, :, 0] - if img.shape != ref_arr.shape: - raise ValueError(f"Shape mismatch: '{file}' has shape {img.shape}, expected {ref_arr.shape}") - def_arr[i] = img + def_img = Image.open(file) + if def_img.size != ref_img.size: + raise ValueError(f"Shape mismatch: '{file}' has shape {def_img.size}, expected {ref_img.size}") # Array-based input else: assert isinstance(reference, np.ndarray) assert isinstance(deformed, np.ndarray) + ref_arr = reference def_arr = deformed - # user might only pass a single deformed image. need to convert to 'stack' - if (reference.shape == deformed.shape): - def_arr = def_arr.reshape((1,def_arr.shape[0],def_arr.shape[1])) - - elif (reference.shape != deformed[0].shape or reference.shape != roi.shape): - raise ValueError(f"Shape mismatch: reference={reference.shape}, " - f"deformed[0]={deformed[0].shape}, roi={roi.shape}") - - # check ROI dimensions agrees with reference image - if (reference.shape != roi.shape): - raise ValueError(f"Shape mismatch: reference={reference.shape}, " - f"roi={roi.shape}") - - # need to set some dummy filenames in the case that the user passes numpy arrays - filenames = ["ref_img"] - for f in range(0,def_arr.shape[0]): - filenames.append(f"def_img_{f:04d}") - - # it might be the case that the roi has been manipulated prior to DIC run - # and therefore we need to to prevent the roi mask from being a 'view' - roi_c = np.ascontiguousarray(roi) + # Promote a single deformed image to a stack [1, H, W] + if ref_arr.shape == def_arr.shape: + def_arr = def_arr.reshape((1, def_arr.shape[0], def_arr.shape[1])) - # Build image stack: reference first, then deformed images - image_stack = np.concatenate(([ref_arr], def_arr), axis=0) + # Validate shapes + if ref_arr.shape != def_arr[0].shape: + raise ValueError( + f"Shape mismatch: reference={ref_arr.shape}, deformed[0]={def_arr[0].shape}" + ) - return image_stack, roi_c, filenames + if ref_arr.shape != roi.shape: + raise ValueError( + f"Shape mismatch: reference={ref_arr.shape}, roi={roi.shape}" + ) + # Drop channel dim if multi-channel + if ref_arr.ndim == 3: + if debug_level > 0: + print(f"Reference array has {ref_arr.shape[2]} channels. Using channel 0.") + ref_arr = ref_arr[:, :, 0] + # Create a tmp directory under cwd + temp_dir = Path.cwd() / "tmp_dic" + temp_dir.mkdir(parents=True, exist_ok=True) + if debug_level > 0: + print(f"Saving array images to temporary directory: {temp_dir}\n") + + # Save reference image + ref_filename = "ref_img.tiff" + ref_path = temp_dir / ref_filename + Image.fromarray(ref_arr).save(ref_path) + basename.append(ref_filename) + fullpath.append(str(ref_path)) + + # Save deformed images + for i in range(def_arr.shape[0]): + frame = def_arr[i] + if frame.ndim == 3: + if debug_level > 0: + print(f"Deformed array [{i}] has {frame.shape[2]} channels. Using channel 0.") + frame = frame[:, :, 0] + + def_filename = f"def_img_{i:04d}.tiff" + def_path = temp_dir / def_filename + Image.fromarray(frame).convert("L").save(def_path) + basename.append(def_filename) + fullpath.append(str(def_path)) + + if debug_level > 1: + print(f"Saved {def_arr.shape[0]} deformed images to {temp_dir}") + for name in basename[1:]: + print(f" - {name}") + print("") -def print_title(a: str): - line_width = 80 - half_width = 39 + ref_img = Image.open(ref_path) + + w, h = ref_img.size + + return basename, fullpath, w, h, temp_dir + + + +def print_config_summary(image_width: int, + image_height: int, + num_def_img: int, + max_iterations: int, + correlation_criteria: str, + shape_function: str, + interpolation_routine: str, + fft_filter: bool, + fft_filter_threshold: float, + fft_filter_radius: int, + fft_filter_corr_power: float, + method: str, + precision: float, + threshold: float, + max_displacement: int, + subset_size: int, + subset_step: int, + num_threads: int | None, + debug_level: int, + updated_seeds: list[int] | None = None, + epi_distance: int | None = None) -> None: + if debug_level <= 0: + return + + common_py_util.print_title("Config") + common_py_util.info_out("Width of Images: ", f"{image_width} [px]") + common_py_util.info_out("Height of Images: ", f"{image_height} [px]") + common_py_util.info_out("Number of Deformed Images: ", num_def_img) + common_py_util.info_out("Max number of solver iterations: ", max_iterations) + common_py_util.info_out("Correlation Criterion: ", correlation_criteria) + common_py_util.info_out("Shape Function: ", shape_function) + common_py_util.info_out("Interpolation Routine: ", interpolation_routine) + common_py_util.info_out("FFT displacement filter enabled: ", fft_filter) + common_py_util.info_out("FFT displacement filter threshold: ", fft_filter_threshold) + common_py_util.info_out("FFT displacement filter radius: ", fft_filter_radius) + common_py_util.info_out("FFT displacement filter correlation power: ", fft_filter_corr_power) + common_py_util.info_out("Image Scan Method: ", method) + common_py_util.info_out("Optimization Precision:", precision) + common_py_util.info_out("Correlation Cutoff Threshold:", threshold) + common_py_util.info_out("Estimate for Max Displacement:", f"{max_displacement} [px]") + if epi_distance is not None: + common_py_util.info_out("Estimate for Epipolar Distance:", f"{epi_distance} [px]") + common_py_util.info_out("Subset Size:", f"{subset_size} [px]") + common_py_util.info_out("Subset Step:", f"{subset_step} [px]") + if num_threads is None: + import pyvale.common_cpp.common_cpp as common_cpp + num_threads = common_cpp.get_num_threads() + common_py_util.info_out("Number of OMP threads:", num_threads) + common_py_util.info_out("Debug level: ", debug_level) + if updated_seeds is not None and "RG" in method: + for i in range(0, len(updated_seeds), 2): + x, y = updated_seeds[i], updated_seeds[i + 1] + common_py_util.info_out(f"Reliability Guided Seed {i//2}:", f"({x}, {y})") - print('-' * line_width) - # Center the title between dashes - left_dashes = '-' * (half_width - len(a) // 2) - right_dashes = '-' * (half_width - len(a) // 2) - print(f"{left_dashes} {a} {right_dashes}") - print('-' * line_width) diff --git a/src/pyvale/dic/dicdataimport.py b/src/pyvale/dic/dicdataimport.py deleted file mode 100644 index f3a965d86..000000000 --- a/src/pyvale/dic/dicdataimport.py +++ /dev/null @@ -1,403 +0,0 @@ -# ================================================================================ -# pyvale: the python validation engine -# License: MIT -# Copyright (C) 2025 The Computer Aided Validation Team -# ================================================================================ - - - -import numpy as np -import glob -import os -from pathlib import Path - -# Pyvale modules -from pyvale.dic.dicresults import Results - -""" -Module responsible for handling importing of DIC results from completed -calculations. -""" - - -def import_2d(data: str | Path | list[Path], - binary: bool = False, - layout: str = "matrix", - delimiter: str = ",") -> Results: - """ - Import DIC result data from human readable text or binary files. - - Parameters - ---------- - - data : str or pathlib.Path or list[pathlib.Path] - Path pattern to the data files (can include wildcards). Default is "./". - - layout : str, optional - Format of the output data layout: "column" (flat array per frame) or "matrix" - (reshaped grid per frame). Default is "column". - - binary : bool, optional - If True, expects files in a specific binary format. If False, expects text data. - Default is False. - - delimiter : str, optional - Delimiter used in text data files. Ignored if binary=True. Default is a single space. - - Returns - ------- - Results - A named container with the following fields: - - ss_x, ss_y (grid arrays if layout=="matrix"; otherwise, 1D integer arrays) - - u, v, m, converged, cost, ftol, xtol, niter (arrays with shape depending on layout) - - filenames (python list) - - Raises - ------ - ValueError: - If `layout` is not "column" or "matrix", or text data has insufficient columns, - or binary rows are malformed. - import cython module - FileNotFoundError: - If no matching data files are found. - """ - - - print("") - print("Attempting DIC Data import...") - print("") - - # convert to str - if isinstance(data, Path): - data = str(data) - - if isinstance(data, list): - files = list(map(str, data)) - else: - files = sorted(glob.glob(data)) - if not files: - raise FileNotFoundError(f"No results found in: {data}") - - print(f"Found {len(files)} files containing DIC results:") - for file in files: - print(f" - {file}") - print("") - - - # Read first file to define reference coordinates - read_data = read_binary if binary else read_text - ss_x_ref, ss_y_ref, *fields = read_data(files[0], delimiter=delimiter) - frames = [list(fields)] - - for file in files[1:]: - ss_x, ss_y, *f = read_data(file, delimiter) - if not (np.array_equal(ss_x_ref, ss_x) and np.array_equal(ss_y_ref, ss_y)): - raise ValueError("Mismatch in coordinates across frames.") - frames.append(f) - - # Stack results (except ss_x and ss_y) into arrays - arrays = [np.stack([frame[i] for frame in frames]) for i in range(len(fields))] - - if layout == "matrix": - - # convert x and y data to meshgrid - x_unique = np.unique(ss_x_ref) - y_unique = np.unique(ss_y_ref) - X, Y = np.meshgrid(x_unique, y_unique) - shape = (len(files), len(y_unique), len(x_unique)) - - - arrays = [to_grid(a,shape,ss_x_ref, ss_y_ref, x_unique,y_unique) for a in arrays] - - - # sorting out shape function parameters if they are present in the files - current_shape = arrays[0].shape # (file,x,y) - shape_params = np.zeros(()) - - # rigid - if len(fields) == 10: - shape_params = np.zeros(current_shape+(2,)) - shape_params[:,:,:,0] = arrays[8] - shape_params[:,:,:,1] = arrays[9] - if len(fields) == 14: - shape_params = np.zeros(current_shape+(6,)) - shape_params[:,:,:,0] = arrays[8] - shape_params[:,:,:,1] = arrays[9] - shape_params[:,:,:,2] = arrays[10] - shape_params[:,:,:,3] = arrays[11] - shape_params[:,:,:,4] = arrays[12] - shape_params[:,:,:,5] = arrays[13] - - - - - return Results(X, Y, arrays[0], arrays[1], arrays[2], arrays[3], - arrays[4], arrays[5], arrays[6], arrays[7], - shape_params, files) - # column layout - else: - - shape_params = np.zeros(()) - current_shape = arrays[0].shape # (file,(x,y)) - # rigid - if len(fields) == 10: - shape_params = np.zeros(current_shape+(2,)) - shape_params[:,:,0] = arrays[8] - shape_params[:,:,1] = arrays[9] - if len(fields) == 14: - shape_params = np.zeros(current_shape+(6,)) - shape_params[:,:,0] = arrays[8] - shape_params[:,:,1] = arrays[9] - shape_params[:,:,2] = arrays[10] - shape_params[:,:,3] = arrays[11] - shape_params[:,:,4] = arrays[12] - shape_params[:,:,5] = arrays[13] - - return Results(ss_x_ref, ss_y_ref, arrays[0], arrays[1], arrays[2], arrays[3], - arrays[4], arrays[5], arrays[6], arrays[7], - shape_params, files) - - - - - -def read_binary(file: str, delimiter: str): - """ - Read a binary DIC result file and extract DIC fields. - - Assumes a fixed binary structure with each row containing: - - 2 × int32 (subset coordinates) - - 6 × float64 (u, v, match quality, cost, ftol, xtol) - - 1 × int32 (number of iterations) - - 1 × uint8 (convergence flag) - - 2 or 6 × float64 (shape parameters) - - Parameters - ---------- - file : str - Path to the binary result file. - - delimiter : str - Ignored for binary data (included for API consistency). - - Returns - ------- - tuple of np.ndarray - Arrays corresponding to: - (ss_x, ss_y, u, v, m, cost, ftol, xtol, niter) - - Raises - ------ - ValueError - If the binary file size does not align with expected row size. - """ - - # row size can either be 3×4 + 6×8 + 1 = 61 bytes (without shape params) - # or 3×4 + 6×8 + 1 + 6×8 = 109 bytes (with shape params) - with open(file, "rb") as f: - raw = f.read() - - has_shape_params = False - has_rigid_params = False - has_affine_params = False - has_quad_params = False - - row_size_basic = 3 * 4 + 6 * 8 + 1 # 61 bytes - row_size_with_rigid = row_size_basic + 2 * 8 # 77 bytes - row_size_with_affine = row_size_basic + 6 * 8 # 109 bytes - row_size_with_quad = row_size_basic + 12 * 8 # 157 bytes - - if len(raw) % row_size_basic == 0: - row_size = row_size_basic - has_shape_params = False - elif len(raw) % row_size_with_rigid == 0: - has_shape_params = True - row_size = row_size_with_rigid - has_rigid_params = True - elif len(raw) % row_size_with_affine == 0: - has_shape_params = True - row_size = row_size_with_affine - has_affine_params = True - elif len(raw) % row_size_with_quad == 0: - has_shape_params = True - row_size = row_size_with_quad - has_quad_params = True - else: - raise ValueError( - f"Binary file has incomplete rows: {file}. " - f"Expected row size: 65 ((without shape params), " - f"81 (with rigid shape params) bytes, " - f"109 (with affine shape params). " - f"157 (with quad shape params). " - f"Actual size: {len(raw)} bytes." - ) - - rows = len(raw) // row_size - arr = np.frombuffer(raw, dtype=np.uint8).reshape(rows, row_size) - - def extract(col, dtype, start): - return np.frombuffer(arr[:, start:start+col].copy(), dtype=dtype) - - ss_x = extract(4, np.int32, 0) - ss_y = extract(4, np.int32, 4) - u = extract(8, np.float64, 8) - v = extract(8, np.float64, 16) - m = extract(8, np.float64, 24) - conv = extract(1, np.uint8, 32).astype(bool) - cost = extract(8, np.float64, 33) - ftol = extract(8, np.float64, 41) - xtol = extract(8, np.float64, 49) - niter = extract(4, np.int32, 57) - - if has_shape_params: - if has_rigid_params: - p0 = extract(8, np.float64, 61) - p1 = extract(8, np.float64, 69) - return ss_x, ss_y, u, v, m, conv, cost, ftol, xtol, niter, p0,p1 - if has_affine_params: - p0 = extract(8, np.float64, 61) - p1 = extract(8, np.float64, 69) - p2 = extract(8, np.float64, 77) - p3 = extract(8, np.float64, 85) - p4 = extract(8, np.float64, 93) - p5 = extract(8, np.float64, 101) - return ss_x, ss_y, u, v, m, conv, cost, ftol, xtol, niter,p0,p1,p2,p3,p4,p5 - if has_quad_params: - p0 = extract(8, np.float64, 61) - p1 = extract(8, np.float64, 69) - p2 = extract(8, np.float64, 77) - p3 = extract(8, np.float64, 85) - p4 = extract(8, np.float64, 93) - p5 = extract(8, np.float64, 101) - p6 = extract(8, np.float64, 109) - p7 = extract(8, np.float64, 117) - p8 = extract(8, np.float64, 125) - p9 = extract(8, np.float64, 133) - p10 = extract(8, np.float64, 141) - p11 = extract(8, np.float64, 149) - return ss_x, ss_y, u, v, m, conv, cost, ftol, xtol, niter,p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11 - else: - return ss_x, ss_y, u, v, m, conv, cost, ftol, xtol, niter - - - - -def read_text(file: str, delimiter: str): - """ - Read a human-readable text DIC result file and extract DIC fields. - - Expects at least 9 columns: - [ss_x, ss_y, u, v, m, conv, cost, ftol, xtol, niter] - Could also include shape parameters if present. - - Parameters - ---------- - file : str - Path to the text result file. - - delimiter : str - Delimiter used in the text file (e.g., space, tab, comma). - - Returns - ------- - tuple of np.ndarray - Arrays corresponding to: - (ss_x, ss_y, u, v, m, conv, cost, ftol, xtol, niter, shape_params) - - Raises - ------ - ValueError - If the text file has fewer than 9 columns. - """ - - data = np.loadtxt(file, delimiter=delimiter, skiprows=1) - - if data.shape[1] < 9: - raise ValueError("Text data must have at least 9 columns.") - - if data.shape[1] == 10: - return ( - data[:, 0].astype(np.int32), # ss_x - data[:, 1].astype(np.int32), # ss_y - data[:, 2], data[:, 3], data[:, 4], # u, v, mag - data[:, 5].astype(np.bool_), # convergence - data[:, 6], data[:, 7], data[:,8], # cost, ftol, xtol - data[:, 9].astype(np.int32) #niter - ) - #rigid - elif data.shape[1]==12: - return ( - data[:, 0].astype(np.int32), # ss_x - data[:, 1].astype(np.int32), # ss_y - data[:, 2], data[:, 3], data[:, 4], # u, v, mag - data[:, 5].astype(np.bool_), # convergence - data[:, 6], data[:, 7], data[:,8], # cost, ftol, xtol - data[:, 9].astype(np.int32), #niter - data[:,10], data[:,11] # shape params (rigid) - ) - #affine - elif data.shape[1]==16: - return ( - data[:, 0].astype(np.int32), # ss_x - data[:, 1].astype(np.int32), # ss_y - data[:, 2], data[:, 3], data[:, 4], # u, v, mag - data[:, 5].astype(np.bool_), # convergence - data[:, 6], data[:, 7], data[:,8], # cost, ftol, xtol - data[:, 9].astype(np.int32), #niter - data[:,10], data[:,11], data[:,12], data[:,13], data[:,14], data[:,15] # shape params (affine) - ) - #quad - elif data.shape[1]==22: - return ( - data[:, 0].astype(np.int32), # ss_x - data[:, 1].astype(np.int32), # ss_y - data[:, 2], data[:, 3], data[:, 4], # u, v, mag - data[:, 5].astype(np.bool_), # convergence - data[:, 6], data[:, 7], data[:,8], # cost, ftol, xtol - data[:, 9].astype(np.int32), #niter - data[:,10], data[:,11], data[:,12], data[:,13], data[:,14], data[:,15], - data[:,16], data[:,17], data[:,18], data[:,19], data[:,20], data[:,21] # shape params (quad) - ) - - - - -def to_grid(data, shape, ss_x_ref, ss_y_ref, x_unique, y_unique): - """ - Reshape a 2D DIC field from flat (column) format into grid (matrix) format. - - This is used when output layout is specified as "matrix". - Maps values using reference subset coordinates (ss_x_ref, ss_y_ref). - - Parameters - ---------- - data : np.ndarray - Array of shape (n_frames, n_points) to be reshaped into (n_frames, height, width). - - shape : tuple - Target shape of output array: (n_frames, height, width). - - ss_x_ref : np.ndarray - X coordinates of subset centers. - - ss_y_ref : np.ndarray - Y coordinates of subset centers. - - x_unique : np.ndarray - Sorted unique X coordinates in the grid. - - y_unique : np.ndarray - Sorted unique Y coordinates in the grid. - - Returns - ------- - np.ndarray - Reshaped array with shape `shape`, filled with NaNs where no data exists. - """ - - grid = np.full(shape, np.nan) - for i, (x, y) in enumerate(zip(ss_x_ref, ss_y_ref)): - x_idx = np.where(x_unique == x)[0][0] - y_idx = np.where(y_unique == y)[0][0] - grid[:, y_idx, x_idx] = data[:, i] - return grid diff --git a/src/pyvale/dic/dicimport2d.py b/src/pyvale/dic/dicimport2d.py new file mode 100644 index 000000000..29499b42e --- /dev/null +++ b/src/pyvale/dic/dicimport2d.py @@ -0,0 +1,302 @@ +# ================================================================================ +# pyvale: the python validation engine +# License: MIT +# Copyright (C) 2025 The Computer Aided Validation Team +# ================================================================================ + + + +import numpy as np +import glob +import os +from pathlib import Path +from typing import Literal + +# Pyvale modules +from pyvale.dic.dicresults import Results +import pyvale.common_py.util as common_py_util + +""" +Module responsible for handling importing of DIC results from completed +calculations. +""" + + +def import_2d(data: str | Path | list[Path], + delimiter: str, + binary: bool = False, + layout: Literal["column", "matrix"] = "matrix", + debug_level: int=1) -> Results: + + """ + Import DIC result data from human readable text or binary files. + + Parameters + ---------- + + data : str or pathlib.Path or list[pathlib.Path] + Path pattern to the data files (can include wildcards). Default is "./". + + layout : str, optional + Format of the output data layout: "column" (flat array per frame) or "matrix" + (reshaped grid per frame). Default is "column". + + binary : bool, optional + If True, expects files in a specific binary format. If False, expects text data. + Default is False. + + delimiter : str, optional + Delimiter used in text data files. Ignored if binary=True. Default is a single space. + + Returns + ------- + Results + A named container with the following fields: + - ss_x, ss_y (grid arrays if layout=="matrix"; otherwise, 1D integer arrays) + - u, v, m, converged, cost, ftol, xtol, niter (arrays with shape depending on layout) + - filenames (python list) + + Raises + ------ + ValueError: + If `layout` is not "column" or "matrix", or text data has insufficient columns, + or binary rows are malformed. + import cython module + FileNotFoundError: + If no matching data files are found. + """ + + if (debug_level>0): + common_py_util.print_title("Importing DIC Results") + + # convert to str + if isinstance(data, Path): + data = str(data) + + if isinstance(data, list): + files = list(map(str, data)) + else: + files = sorted(glob.glob(data)) + if not files: + raise FileNotFoundError(f"No results found in: {data}") + + if debug_level>0: + common_py_util.info_out(f"Found {len(files)} files containing DIC results:", "") + for file in files: + common_py_util.info_out(f"{file}", "") + + + # Read first file to define reference coordinates + read_data = read_binary if binary else read_text + ss_x_ref, ss_y_ref, *fields = read_data(files[0], delimiter=delimiter, debug_level=debug_level) + frames = [list(fields)] + + for file in files[1:]: + ss_x, ss_y, *f = read_data(file, delimiter) + if not (np.array_equal(ss_x_ref, ss_x) and np.array_equal(ss_y_ref, ss_y)): + raise ValueError("Mismatch in coordinates across frames.") + frames.append(f) + + # Stack results (except ss_x and ss_y) into arrays + arrays = [np.stack([frame[i] for frame in frames]) for i in range(len(fields))] + + if debug_level>0: + common_py_util.info_out(f"Imported {len(files)} frames of DIC data.", "") + + if layout == "matrix": + + # convert x and y data to meshgrid + x_unique = np.unique(ss_x_ref) + y_unique = np.unique(ss_y_ref) + X, Y = np.meshgrid(x_unique, y_unique) + shape = (len(files), len(y_unique), len(x_unique)) + + arrays = [to_grid(a,shape,ss_x_ref, ss_y_ref, x_unique,y_unique) for a in arrays] + + return Results(ss_x=X, + ss_y=Y, + u_px=arrays[0], + v_px=arrays[1], + mag_px=arrays[2], + converged=arrays[3], + cost=arrays[4], + ftol=arrays[5], + xtol=arrays[6], + niter=arrays[7], + filenames=files) + + # column layout + else: + + return Results(ss_x=ss_x_ref, + ss_y=ss_y_ref, + u_px=arrays[0], + v_px=arrays[1], + mag_px=arrays[2], + converged=arrays[3], + cost=arrays[4], + ftol=arrays[5], + xtol=arrays[6], + niter=arrays[7], + filenames=files) + + +def read_binary(file: str, delimiter: str, debug_level: int=1): + """ + Read a binary 2D DIC result file and extract DIC fields. + + Supports rows written by ``ResultArrays::write_to_disk_2d`` with or without + optional shape parameters. Shape parameters are currently ignored by the + public ``Results`` dataclass. + """ + + del delimiter + + if debug_level>0: + common_py_util.info(f"Reading binary DIC result file: {file}") + + with open(file, "rb") as f: + raw = f.read() + + row_size_basic = 2 * 4 + 3 * 8 + 1 + 3 * 8 + 4 + row_sizes = [row_size_basic + nparams * 8 for nparams in (12, 6, 2, 0)] + + row_size = next((size for size in row_sizes if len(raw) % size == 0), None) + if row_size is None: + raise ValueError( + f"Binary file has incomplete rows: {file}. " + f"Expected row size one of {row_sizes}, actual size: {len(raw)} bytes." + ) + + rows = len(raw) // row_size + arr = np.frombuffer(raw, dtype=np.uint8).reshape(rows, row_size) + + def extract(width, dtype, start): + return np.frombuffer(arr[:, start:start + width].copy(), dtype=dtype) + + offset = 0 + ss_x = extract(4, np.int32, offset); offset += 4 + ss_y = extract(4, np.int32, offset); offset += 4 + u = extract(8, np.float64, offset); offset += 8 + v = extract(8, np.float64, offset); offset += 8 + mag = extract(8, np.float64, offset); offset += 8 + conv = extract(1, np.uint8, offset).astype(bool); offset += 1 + cost = extract(8, np.float64, offset); offset += 8 + ftol = extract(8, np.float64, offset); offset += 8 + xtol = extract(8, np.float64, offset); offset += 8 + niter = extract(4, np.int32, offset) + + return ss_x, ss_y, u, v, mag, conv, cost, ftol, xtol, niter + + + + +def read_text(file: str, delimiter: str, debug_level: int=1): + """ + Read a human-readable text DIC result file and extract DIC fields. + + Expects at least 9 columns: + [ss_x, ss_y, u, v, m, conv, cost, ftol, xtol, niter] + Could also include shape parameters if present. + + Parameters + ---------- + file : str + Path to the text result file. + + delimiter : str + Delimiter used in the text file (e.g., space, tab, comma). + + Returns + ------- + tuple of np.ndarray + Arrays corresponding to: + (ss_x, ss_y, u, v, m, conv, cost, ftol, xtol, niter) + + Raises + ------ + ValueError + If the text file has fewer than 9 columns. + """ + if debug_level>0: + common_py_util.info(f"Reading text DIC result file: {file}") + + check_delimiter(file, delimiter) + data = np.loadtxt(file, delimiter=delimiter, skiprows=1) + + if data.shape[1] != 10: + raise ValueError(f"Input DIC data must have exactly 10 columns. Num cols = {data.shape[1]}") + + return ( + data[:, 0].astype(np.int32), # ss_x + data[:, 1].astype(np.int32), # ss_y + data[:, 2], data[:, 3], data[:, 4], # u, v, mag + data[:, 5].astype(np.bool_), # convergence + data[:, 6], data[:, 7], data[:,8], # cost, ftol, xtol + data[:, 9].astype(np.int32) #niter + ) + + + + +def to_grid(data, shape, ss_x_ref, ss_y_ref, x_unique, y_unique): + """ + Reshape a 2D DIC field from flat (column) format into grid (matrix) format. + + This is used when output layout is specified as "matrix". + Maps values using reference subset coordinates (ss_x_ref, ss_y_ref). + + Parameters + ---------- + data : np.ndarray + Array of shape (n_frames, n_points) to be reshaped into (n_frames, height, width). + + shape : tuple + Target shape of output array: (n_frames, height, width). + + ss_x_ref : np.ndarray + X coordinates of subset centers. + + ss_y_ref : np.ndarray + Y coordinates of subset centers. + + x_unique : np.ndarray + Sorted unique X coordinates in the grid. + + y_unique : np.ndarray + Sorted unique Y coordinates in the grid. + + Returns + ------- + np.ndarray + Reshaped array with shape `shape`, filled with NaNs where no data exists. + """ + + grid = np.full(shape, np.nan) + for i, (x, y) in enumerate(zip(ss_x_ref, ss_y_ref)): + x_idx = np.where(x_unique == x)[0][0] + y_idx = np.where(y_unique == y)[0][0] + grid[:, y_idx, x_idx] = data[:, i] + return grid + +def check_delimiter(fname: str, delimiter: str | None) -> None: + with open(fname, "r") as f: + for line in f: + line = line.strip() + if line and not line.startswith("#"): + first_line = line + break + else: + return + + if delimiter == "," and "," not in first_line: + raise ValueError( + f"Expected comma-separated data but first data row contains no commas:\n" + f" {first_line[:120]}" + ) + + if delimiter == "\t" and "\t" not in first_line: + raise ValueError( + f"Expected tab-separated data but first data row contains no tabs:\n" + f" {first_line[:120]}" + ) diff --git a/src/pyvale/dic/dicimport3d.py b/src/pyvale/dic/dicimport3d.py new file mode 100644 index 000000000..12258d891 --- /dev/null +++ b/src/pyvale/dic/dicimport3d.py @@ -0,0 +1,386 @@ +# ================================================================================ +# pyvale: the python validation engine +# License: MIT +# Copyright (C) 2025 The Computer Aided Validation Team +# ================================================================================ + +import numpy as np +import glob +from pathlib import Path +from typing import Literal + +# Pyvale modules +from pyvale.dic.dicresults import Results, StereoResults +from pyvale.dic.dicimport2d import to_grid, check_delimiter +import pyvale.common_py.util as common_py_util + + +def import_3d(data: str | Path | list[Path], + delimiter: str, + binary: bool = False, + layout: Literal["column", "matrix"] = "matrix", + debug_level: int=1) -> Results: + """ + Import stereo DIC result data from human readable text or binary files. + + Parameters + ---------- + data : str or pathlib.Path or list[pathlib.Path] + Path pattern to the data files (can include wildcards). + + layout : str, optional + Format of the output data layout: + "column" or "matrix". + + binary : bool, optional + If True, expects binary files. + + delimiter : str, optional + Delimiter used in text files. + + Returns + ------- + Results + Results object containing both 2D DIC and stereo DIC data. + """ + + + if debug_level > 0: + common_py_util.print_title("Importing Stereo DIC Results") + + # convert to str + if isinstance(data, Path): + data = str(data) + + if isinstance(data, list): + files = list(map(str, data)) + else: + files = sorted(glob.glob(data)) + if not files: + raise FileNotFoundError(f"No results found in: {data}") + + if debug_level>0: + common_py_util.info_out(f"Found {len(files)} files containing DIC results:", "") + for file in files: + common_py_util.info_out(f"{file}", "") + + read_data = read_binary_3d if binary else read_text_3d + + ss_x_ref, ss_y_ref, *fields = read_data( + files[0], + delimiter=delimiter, + debug_level=debug_level + ) + + frames = [list(fields)] + + for file in files[1:]: + + ss_x, ss_y, *f = read_data( + file, + delimiter=delimiter, + debug_level=debug_level + ) + + if not ( + np.array_equal(ss_x_ref, ss_x) + and np.array_equal(ss_y_ref, ss_y) + ): + raise ValueError( + "Mismatch in coordinates across frames." + ) + + frames.append(f) + + arrays = [ + np.stack([frame[i] for frame in frames]) + for i in range(len(fields)) + ] + + if debug_level>0: + common_py_util.info_out(f"Imported {len(files)} frames of stereo DIC data.", "") + + if layout == "matrix": + + + if debug_level>0: + common_py_util.info_out(f"converting DIC data to matrix layout...", "") + + + x_unique = np.unique(ss_x_ref) + y_unique = np.unique(ss_y_ref) + + X, Y = np.meshgrid( + x_unique, + y_unique, + ) + + shape = ( + len(files), + len(y_unique), + len(x_unique), + ) + + arrays = [ + to_grid( + a, + shape, + ss_x_ref, + ss_y_ref, + x_unique, + y_unique, + ) + for a in arrays + ] + + ss_x_out = X + ss_y_out = Y + + if debug_level>0: + common_py_util.info_out(f"Layout conversion finished.", "") + + else: + + ss_x_out = ss_x_ref + ss_y_out = ss_y_ref + + return Results( + ss_x=ss_x_out, + ss_y=ss_y_out, + + u_px=arrays[0], + v_px=arrays[1], + mag_px=arrays[2], + + converged=arrays[3], + cost=arrays[4], + ftol=arrays[5], + xtol=arrays[6], + niter=arrays[7], + + stereo=StereoResults( + u_px=arrays[8], + v_px=arrays[9], + mag_px=arrays[10], + + u_mm=arrays[11], + v_mm=arrays[12], + w_mm=arrays[13], + + x_mm=arrays[14], + y_mm=arrays[15], + z_mm=arrays[16], + + converged=arrays[17], + cost=arrays[18], + ftol=arrays[19], + xtol=arrays[20], + niter=arrays[21], + ), + + filenames=files, + ) + + +def read_text_3d( + file: str, + delimiter: str, + debug_level: int = 1 +): + """ + Read a human-readable stereo DIC result file. + + Expected columns + ---------------- + subset_x + subset_y + disp_u + disp_v + disp_mag + converged + cost_zncc + ftol + xtol + num_iter + stereo_disp_u_px + stereo_disp_v_px + stereo_disp_mag_px + stereo_disp_u_mm + stereo_disp_v_mm + stereo_disp_w_mm + stereo_x_mm + stereo_y_mm + stereo_z_mm + stereo_converged + stereo_cost_zncc + stereo_ftol + stereo_xtol + stereo_num_iter + """ + + if debug_level>0: + common_py_util.info(f"Reading text DIC result file: {file}") + + check_delimiter(file, delimiter) + data = np.loadtxt( + file, + delimiter=delimiter, + skiprows=1, + ) + + if data.shape[1] != 24: + raise ValueError( + "Input stereo DIC data must have exactly 24 columns." + ) + + return ( + data[:, 0].astype(np.int32), # subset_x + data[:, 1].astype(np.int32), # subset_y + + data[:, 2], # disp_u + data[:, 3], # disp_v + data[:, 4], # disp_mag + + data[:, 5].astype(np.bool_), # converged + data[:, 6], # cost + data[:, 7], # ftol + data[:, 8], # xtol + data[:, 9].astype(np.int32), # num_iter + + data[:, 10], # stereo_disp_u_px + data[:, 11], # stereo_disp_v_px + data[:, 12], # stereo_disp_mag_px + + data[:, 13], # stereo_disp_u_mm + data[:, 14], # stereo_disp_v_mm + data[:, 15], # stereo_disp_w_mm + + data[:, 16], # stereo_x_mm + data[:, 17], # stereo_y_mm + data[:, 18], # stereo_z_mm + + data[:, 19].astype(np.bool_), # stereo_converged + data[:, 20], # stereo_cost + data[:, 21], # stereo_ftol + data[:, 22], # stereo_xtol + data[:, 23].astype(np.int32), # stereo_num_iter + ) + +def read_binary_3d(file: str, delimiter: str, debug_level: int = 1): + """ + Read a binary stereo DIC result file and extract all fields. + + Must match ResultArrays::write_to_disk_stereo exactly. + """ + + if debug_level > 0: + common_py_util.info(f"Reading binary DIC result file: {file}") + + with open(file, "rb") as f: + raw = f.read() + + row_size = ( + 4 * 4 + # int32: ss_x, ss_y, niter, stereo_niter + 2 * 1 + # uint8: conv, stereo_conv + 18 * 8 # double fields written by ResultArrays::write_to_disk_stereo + ) + + file_size = len(raw) + + if file_size % row_size != 0: + raise ValueError( + f"Binary file has incomplete rows: {file}. " + f"Expected row size: {row_size}, " + f"Actual size: {file_size} bytes." + ) + + rows = file_size // row_size + + arr = np.frombuffer(raw, dtype=np.uint8).reshape(rows, row_size) + + def extract(col, dtype, start): + return np.frombuffer( + arr[:, start:start + col].copy(), + dtype=dtype, + ) + + offset = 0 + + # ----------------------- + # 2D base fields + # ----------------------- + ss_x = extract(4, np.int32, offset); offset += 4 + ss_y = extract(4, np.int32, offset); offset += 4 + + u = extract(8, np.float64, offset); offset += 8 + v = extract(8, np.float64, offset); offset += 8 + mag = extract(8, np.float64, offset); offset += 8 + + conv = extract(1, np.uint8, offset).astype(bool); offset += 1 + + cost = extract(8, np.float64, offset); offset += 8 + ftol = extract(8, np.float64, offset); offset += 8 + xtol = extract(8, np.float64, offset); offset += 8 + + niter = extract(4, np.int32, offset); offset += 4 + + # ----------------------- + # stereo pixel fields + # ----------------------- + stereo_u_px = extract(8, np.float64, offset); offset += 8 + stereo_v_px = extract(8, np.float64, offset); offset += 8 + stereo_mag_px = extract(8, np.float64, offset); offset += 8 + + # ----------------------- + # stereo world fields + # ----------------------- + stereo_u_mm = extract(8, np.float64, offset); offset += 8 + stereo_v_mm = extract(8, np.float64, offset); offset += 8 + stereo_w_mm = extract(8, np.float64, offset); offset += 8 + + stereo_x_mm = extract(8, np.float64, offset); offset += 8 + stereo_y_mm = extract(8, np.float64, offset); offset += 8 + stereo_z_mm = extract(8, np.float64, offset); offset += 8 + + # ----------------------- + # stereo convergence + # ----------------------- + stereo_conv = extract(1, np.uint8, offset).astype(bool); offset += 1 + + stereo_cost = extract(8, np.float64, offset); offset += 8 + stereo_ftol = extract(8, np.float64, offset); offset += 8 + stereo_xtol = extract(8, np.float64, offset); offset += 8 + + stereo_niter = extract(4, np.int32, offset); offset += 4 + + return ( + ss_x, + ss_y, + + u, + v, + mag, + conv, + cost, + ftol, + xtol, + niter, + + stereo_u_px, + stereo_v_px, + stereo_mag_px, + + stereo_u_mm, + stereo_v_mm, + stereo_w_mm, + + stereo_x_mm, + stereo_y_mm, + stereo_z_mm, + + stereo_conv, + stereo_cost, + stereo_ftol, + stereo_xtol, + stereo_niter, + ) diff --git a/src/pyvale/dic/dicregionofinterest.py b/src/pyvale/dic/dicregionofinterest.py index 9382945a5..71392860d 100644 --- a/src/pyvale/dic/dicregionofinterest.py +++ b/src/pyvale/dic/dicregionofinterest.py @@ -4,8 +4,14 @@ # Copyright (C) 2025 The Computer Aided Validation Team # ================================================================================ -from pyqtgraph.Qt import QtWidgets, QtCore, QtGui -import pyqtgraph as pg +try: + from pyqtgraph.Qt import QtWidgets, QtCore, QtGui + import pyqtgraph as pg +except (ImportError, OSError): + QtWidgets = None + QtCore = None + QtGui = None + pg = None import cv2 import numpy as np import matplotlib.pyplot as plt @@ -16,6 +22,17 @@ from pathlib import Path from functools import partial + +def _require_gui_dependencies() -> None: + if pg is None or QtWidgets is None or QtCore is None or QtGui is None: + raise ImportError( + "RegionOfInterest interactive GUI features require pyqtgraph " + "and a working Qt runtime. Install the missing Qt system " + "libraries or use read_yaml, read_array, rect_boundary, or " + "rect_region for non-interactive ROI setup." + ) + + class RegionOfInterest: """ A class for interactively selecting and manipulating ROI of an image before passing to the DIC engine. @@ -57,6 +74,8 @@ def __init__(self, ref_image: str | np.ndarray | Path): self.mask = np.zeros(self.ref_image.shape[:2], dtype=bool) self.seed = [] + self.seed_rois = [] + self.seed_roi = None self.__roi_selected = False self.roi_list = [] self.add_list = [] @@ -83,14 +102,14 @@ def __init__(self, ref_image: str | np.ndarray | Path): self.height = None self.width = None - self.subset_size = None + self.subset_size = 21 self.coord_label = None - def interactive_selection(self, subset_size): + def interactive_selection(self): """ Interactive GUI to select a region of interest (ROI) in the image using openCV. """ - self.subset_size = subset_size + _require_gui_dependencies() self.__roi_selected = True # Initialize GUI @@ -110,6 +129,7 @@ def interactive_selection(self, subset_size): def _setup_gui(self) -> tuple[np.ndarray, np.ndarray]: """Setup the main GUI window and sidebar.""" + _require_gui_dependencies() app = pg.mkQApp("ROI GUI") self.main_window = CustomMainWindow(dic_obj=self) main_layout = QtWidgets.QHBoxLayout() @@ -145,10 +165,21 @@ def _setup_gui(self) -> tuple[np.ndarray, np.ndarray]: # Create sidebar sidebar = self._create_sidebar(fill_array, temp_mask) + sidebar_widget = QtWidgets.QWidget() + sidebar_widget.setLayout(sidebar) + sidebar_widget.setFixedWidth(370) + sidebar_widget.setSizePolicy( + QtWidgets.QSizePolicy.Policy.Fixed, + QtWidgets.QSizePolicy.Policy.Expanding + ) + self.graphics_widget.setSizePolicy( + QtWidgets.QSizePolicy.Policy.Expanding, + QtWidgets.QSizePolicy.Policy.Expanding + ) # Create graphics widget - main_layout.addLayout(sidebar) - main_layout.addWidget(self.graphics_widget) + main_layout.addWidget(sidebar_widget) + main_layout.addWidget(self.graphics_widget, stretch=1) return fill_array, temp_mask @@ -198,7 +229,7 @@ def make_title(text): seed_input.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter) # Default subset size - seed_input.setText("21") + seed_input.setText(str(self.subset_size)) # Restrict to integer input validator = QtGui.QIntValidator(1, 999) @@ -210,15 +241,26 @@ def enforce_odd(): if text.isdigit(): val = int(text) if val % 2 == 0: # Convert even → odd - seed_input.setText(str(val + 1)) + val += 1 + seed_input.setText(str(val)) self.subset_size = val - if hasattr(self, 'seed_roi') and self.seed_roi is not None: - # Compute current center of existing ROI - old_rect = self.seed_roi.pos() - center = QtCore.QPointF(old_rect.x() + self.subset_size / 2, - old_rect.y() + self.subset_size / 2) - self._draw_seed_roi(center, fill_array, temp_mask) + if self.seed_rois: + centers = [] + for seed_roi in self.seed_rois: + try: + old_rect = seed_roi.pos() + old_size = seed_roi.size() + except RuntimeError: + continue + centers.append(QtCore.QPointF(old_rect.x() + old_size.x() / 2, + old_rect.y() + old_size.y() / 2)) + self._remove_graphics_item(seed_roi) + + self.seed_rois = [] + self.seed_roi = None + for center in centers: + self._draw_seed_roi(center, fill_array, temp_mask) seed_input.editingFinished.connect(enforce_odd) @@ -363,6 +405,8 @@ def _finish_mode(self): """Finish current drawing/removing mode.""" self._reset_all_modes() self.main_view.setCursor(QtCore.Qt.CursorShape.PointingHandCursor) + if 'add_seed' in self.buttons: + self.buttons['add_seed'].setEnabled(True) self._clear_redo_stack() def _mouse_clicked(self, event, fill_array: np.ndarray, temp_mask: np.ndarray) -> None: @@ -394,20 +438,8 @@ def _handle_polygon_click(self, event, fill_array: np.ndarray, temp_mask: np.nda def _draw_seed_roi(self, center_pos, fill_array, temp_mask): - """ - Draw or redraw the seed ROI with current subset_size. - center_pos: QPointF in scene coordinates. If None, uses previous ROI center. - """ - if hasattr(self, 'seed_roi') and self.seed_roi is not None: - # Save old center - old_rect = self.seed_roi.pos() - self.main_view.removeItem(self.seed_roi) - if center_pos is None: - center_pos = QtCore.QPointF(old_rect.x() + self.subset_size / 2, - old_rect.y() + self.subset_size / 2) - + """Draw a seed ROI with current subset_size.""" if center_pos is None: - # No previous position; nothing to draw return x = math.floor(center_pos.x() - self.subset_size / 2) @@ -426,8 +458,9 @@ def _draw_seed_roi(self, center_pos, fill_array, temp_mask): seed_roi.removeHandle(handle) self.main_view.addItem(seed_roi) - self.seed_roi = seed_roi# - self.seed_roi.sigRegionChanged.connect(lambda: self._redraw_fill_layer(fill_array, temp_mask)) + self.seed_rois.append(seed_roi) + self.seed_roi = seed_roi + seed_roi.sigRegionChanged.connect(lambda: self._redraw_fill_layer(fill_array, temp_mask)) self._redraw_fill_layer(fill_array, temp_mask) def _handle_seed_click(self, event, fill_array, temp_mask): @@ -544,7 +577,7 @@ def _finish_polygon_drawing(self, is_adding, fill_array: np.ndarray, temp_mask: def _redraw_fill_layer(self, fill_array: np.ndarray, temp_mask: np.ndarray) -> None: """Redraw the fill layer based on current ROIs.""" - has_seed = hasattr(self, 'seed_roi') and self.seed_roi is not None + has_seed = bool(self.seed_rois) if not self.roi_list and not has_seed: fill_array.fill(0) @@ -570,10 +603,10 @@ def _redraw_fill_layer(self, fill_array: np.ndarray, temp_mask: np.ndarray) -> N #fill_array[:, :, 3] = np.flip(temp_mask,axis=0) * 80 fill_array[:, :, 3] = temp_mask * 80 - # Yellow fill for seed ROI - if has_seed: - pos = self.seed_roi.pos() - size = self.seed_roi.size() + # Yellow fill for seed ROIs + for seed_roi in self.seed_rois: + pos = seed_roi.pos() + size = seed_roi.size() x, y = int(pos[0]), int(pos[1]) w, h = int(size[0]), int(size[1]) @@ -649,6 +682,50 @@ def _apply_poly_mask(self, roi, is_adding, temp_mask: np.ndarray): else: temp_mask &= ~mask.astype(bool) + def _apply_rect_data_mask(self, x, y, w, h, is_adding: bool, temp_mask: np.ndarray) -> None: + """Apply rectangle data loaded from YAML to temp_mask.""" + x = int(np.floor(x)) + y = int(np.floor(y)) + w = int(np.floor(w)) + h = int(np.floor(h)) + + x1 = max(0, x) + y1 = max(0, y) + x2 = min(temp_mask.shape[1], x + w) + y2 = min(temp_mask.shape[0], y + h) + + if x2 > x1 and y2 > y1: + temp_mask[y1:y2, x1:x2] = is_adding + + def _apply_circle_data_mask(self, cx, cy, w, h, is_adding: bool, temp_mask: np.ndarray) -> None: + """Apply circular or elliptical ROI data loaded from YAML to temp_mask.""" + rx = float(w) / 2.0 + ry = float(h) / 2.0 + if rx <= 0 or ry <= 0: + return + + y_coords, x_coords = np.ogrid[:temp_mask.shape[0], :temp_mask.shape[1]] + circle_mask = ((x_coords - float(cx)) / rx) ** 2 + ((y_coords - float(cy)) / ry) ** 2 <= 1 + + if is_adding: + temp_mask |= circle_mask + else: + temp_mask &= ~circle_mask + + def _apply_poly_data_mask(self, points, is_adding: bool, temp_mask: np.ndarray) -> None: + """Apply polygon ROI data loaded from YAML to temp_mask.""" + if len(points) < 3: + return + + vertices = np.array(points, dtype=np.int32) + mask = np.zeros_like(temp_mask, dtype=np.uint8) + cv2.fillPoly(mask, [vertices], 1) + + if is_adding: + temp_mask |= mask.astype(bool) + else: + temp_mask &= ~mask.astype(bool) + def _update_button_states(self): """Update the enabled state of undo and redo buttons.""" self.buttons['undo_prev'].setEnabled(len(self.roi_list) > 0) @@ -659,6 +736,13 @@ def _clear_redo_stack(self): self.undo_list = [] self._update_button_states() + def _remove_graphics_item(self, item) -> None: + """Remove a pyqtgraph item, tolerating stale Qt wrappers.""" + try: + self.main_view.removeItem(item) + except RuntimeError: + pass + def _undo_last(self, fill_array: np.ndarray, temp_mask: np.ndarray): """Undo the last ROI operation.""" if self.roi_list: @@ -682,6 +766,7 @@ def _redo_last(self, fill_array: np.ndarray, temp_mask: np.ndarray): def _save_interactive_roi(self) -> None: """Save the current ROI to a YAML file. This only works with the interactive GUI.""" + _require_gui_dependencies() filename, _ = QtWidgets.QFileDialog.getSaveFileName(self.main_window, 'Save ROI', 'roi_interactive.yaml', filter='YAML Files (*.yaml)') if filename: @@ -696,13 +781,14 @@ def _save_interactive_roi(self) -> None: for roi, add in zip(self.roi_list, self.add_list) ] - # add ROI to serialized data - if hasattr(self, 'seed_roi'): - self._finalize_seed_selection() + # add seed ROIs to serialized data + for seed_roi in self.seed_rois: + pos = seed_roi.pos() + size = seed_roi.size() seed_data = { 'type': 'SeedROI', - 'pos': [self.seed[0], self.seed[1]], - 'size': [self.subset_size, self.subset_size], + 'pos': [int(np.floor(pos.x())), int(np.floor(pos.y()))], + 'size': [int(size.x()), int(size.y())], 'add': True } serialized.append(seed_data) @@ -712,6 +798,7 @@ def _save_interactive_roi(self) -> None: def _open_interactive_roi(self, fill_layer: np.ndarray, temp_mask: np.ndarray): """Open ROI from a YAML file. This only works with the interactive GUI.""" + _require_gui_dependencies() filename, _ = QtWidgets.QFileDialog.getOpenFileName( self.main_window, 'Open ROI', filter='YAML Files (*.yaml)' ) @@ -725,7 +812,10 @@ def _open_interactive_roi(self, fill_layer: np.ndarray, temp_mask: np.ndarray): self.roi_list = [] self.add_list = [] - self.seed_roi = None # Clear existing seed + for seed_roi in self.seed_rois: + self._remove_graphics_item(seed_roi) + self.seed_rois = [] + self.seed_roi = None for entry in data: if entry.get('type') == 'SeedROI': @@ -735,14 +825,19 @@ def _open_interactive_roi(self, fill_layer: np.ndarray, temp_mask: np.ndarray): w,h = entry.get('size', [21, 21]) # fallback default #y = self.width-y print(x,y,w,h) - self.seed_roi = pg.RectROI( + seed_roi = pg.RectROI( [x, y], [w, h], pen=pg.mkPen('b', width=3), hoverPen=pg.mkPen('y', width=3), handlePen='#0000', handleHoverPen='#0000' ) - self.main_view.addItem(self.seed_roi) + for handle in seed_roi.getHandles(): + seed_roi.removeHandle(handle) + self.seed_rois.append(seed_roi) + self.seed_roi = seed_roi + self.main_view.addItem(seed_roi) + seed_roi.sigRegionChanged.connect(lambda: self._redraw_fill_layer(fill_layer, temp_mask)) else: # Restore standard ROI @@ -829,11 +924,12 @@ def _finalize_seed_selection(self) -> None: """Process the final mask and seed location.""" #mask = np.flipud(temp_mask.T) - if hasattr(self, 'seed_roi'): - pos = self.seed_roi.pos() + self.seed = [] + for seed_roi in self.seed_rois: + pos = seed_roi.pos() x = int(np.floor(pos.x())) y = int(np.floor(pos.y())) - self.seed = [x, y] + self.seed.extend([x, y]) #if not mask[y, x]: # raise ValueError(f"Seed location [{x}, {y}] is not within the mask") @@ -1041,13 +1137,14 @@ def save_yaml(self, filename: str | Path) -> None: for roi, add in zip(self.roi_list, self.add_list) ] - # add ROI to serialized data - if hasattr(self, 'seed_roi'): - self._finalize_seed_selection() + # add seed ROIs to serialized data + for seed_roi in self.seed_rois: + pos = seed_roi.pos() + size = seed_roi.size() seed_data = { 'type': 'SeedROI', - 'pos': [self.seed[0], self.seed[1]], - 'size': [self.subset_size, self.subset_size], + 'pos': [int(np.floor(pos.x())), int(np.floor(pos.y()))], + 'size': [int(size.x()), int(size.y())], 'add': True } serialized.append(seed_data) @@ -1057,69 +1154,57 @@ def save_yaml(self, filename: str | Path) -> None: def read_yaml(self, filename: str | Path) -> None: """ - Load the ROI from a YAML file and restore the state of the GUI. - This method will clear existing ROIs and restore the state from the YAML file. + Load ROI geometry from a YAML file without initialising the GUI. - Parameters - ---------- - filename : str or pathlib.Path - Path to the YAML file containing the ROI data. - - Raises - ------ - FileNotFoundError - If the specified file does not exist. - ValueError - If the loaded data is not a valid ROI format. + The YAML format is the same one written by the interactive ROI tool. + Rectangular, circular, and polygonal entries are applied directly to + ``self.mask`` in file order, while ``SeedROI`` entries populate + ``self.seed``. """ + if not os.path.exists(filename): + raise FileNotFoundError(f"File {filename!r} does not exist.") - # need to create a temp qapplication so I can import the ROI. - self.__roi_selected = True - - # Initialize GUI - fill_array, temp_mask = self._setup_gui() - self._connect_signals(fill_array, temp_mask) - - if filename: - with open(filename, 'r') as f: - data = yaml.safe_load(f) - - self.roi_list = [] - self.add_list = [] + with open(filename, "r") as f: + data = yaml.safe_load(f) - self.seed_roi = None # Clear existing seed + if data is None: + data = [] + if not isinstance(data, list): + raise ValueError("Loaded ROI YAML must contain a list of ROI entries.") - for entry in data: - if entry.get('type') == 'SeedROI': - # Restore the seed ROI - x, y = entry['pos'] - #y = self.width-y - size = entry.get('size', [10, 10]) # fallback default - self.seed_roi = pg.RectROI( - [x, y], size, - pen=pg.mkPen('y', width=3), - hoverPen=pg.mkPen('b', width=3), - handlePen='#0000', - handleHoverPen='#0000' - ) - self.main_view.addItem(self.seed_roi) + temp_mask = np.zeros(self.ref_image.shape[:2], dtype=bool) + self.roi_list = [] + self.add_list = [] + self.seed_rois = [] + self.seed_roi = None + self.seed = [] - else: - # Restore standard ROI - roi = self._create_roi_from_data(entry) - self.roi_list.append(roi) - self.add_list.append(entry['add']) - self.main_view.addItem(roi) - roi.sigRegionChanged.connect(lambda: self._redraw_fill_layer(fill_array, temp_mask)) + for entry in data: + if not isinstance(entry, dict) or "type" not in entry: + raise ValueError("Loaded ROI YAML contains an invalid ROI entry.") - self._redraw_fill_layer(fill_array, temp_mask) - self._update_button_states() - self._finalize_seed_selection() + roi_type = entry["type"] + if roi_type == "SeedROI": + x, y = entry["pos"] + self.seed.extend([int(np.floor(x)), int(np.floor(y))]) + continue - #finalize mask - self.mask = temp_mask - + is_adding = bool(entry.get("add", True)) + if roi_type == "RectROI": + x, y = entry["pos"] + w, h = entry["size"] + self._apply_rect_data_mask(x, y, w, h, is_adding, temp_mask) + elif roi_type == "CircleROI": + cx, cy = entry["pos"] + w, h = entry["size"] + self._apply_circle_data_mask(cx, cy, w, h, is_adding, temp_mask) + elif roi_type == "PolyLineROI": + self._apply_poly_data_mask(entry["points"], is_adding, temp_mask) + else: + raise TypeError(f"Unsupported ROI type: {roi_type}") + self.mask = temp_mask + self.__roi_selected = True def show_image(self) -> None: @@ -1161,7 +1246,7 @@ def show_image(self) -> None: plt.show() -class CustomMainWindow(QtWidgets.QWidget): +class CustomMainWindow(QtWidgets.QWidget if QtWidgets is not None else object): def __init__(self, dic_obj=None, *args, **kwargs): super().__init__(*args, **kwargs) diff --git a/src/pyvale/dic/dicresults.py b/src/pyvale/dic/dicresults.py index 912423a29..d0a92da9a 100644 --- a/src/pyvale/dic/dicresults.py +++ b/src/pyvale/dic/dicresults.py @@ -8,10 +8,59 @@ from dataclasses import dataclass import numpy as np + +@dataclass(slots=True) +class StereoResults: + """ + Data container for stereo DIC results + """ + + u_px: np.ndarray + """Horizontal displacement in pixels to the right image. shape=(img_num,y,x)""" + + v_px: np.ndarray + """Vertical displacement in pixels to the right image. shape=(img_num,y,x)""" + + u_mm: np.ndarray + """Horizontal displacement in physical units of mm relative in cam0 world coordinate system. shape=(img_num,y,x)""" + + v_mm: np.ndarray + """Vertical displacement in physical units of mm relative in cam0 world coordinate system. shape=(img_num,y,x)""" + + w_mm: np.ndarray + """Axial displacement in physical units of mm relative in cam0 world coordinate system. shape=(img_num,y,x)""" + + x_mm: np.ndarray + """X-coordinate in physical units of mm relative in cam0 world coordinate system. shape=(img_num,y,x)""" + + y_mm: np.ndarray + """Y-coordinate in physical units of mm relative in cam0 world coordinate system. shape=(img_num,y,x)""" + + z_mm: np.ndarray + """Z-coordinate in physical units of mm relative in cam0 world coordinate system. shape=(img_num,y,x)""" + + mag_px: np.ndarray | None = None + """Displacement magnitude to the right image, typically computed as sqrt(disp_u_px^2 + disp_v_px^2). shape=(img_num,y,x)""" + + converged: np.ndarray | None = None + """boolean value for whether the subset has converged or not. shape=(img_num,y,x)""" + + cost: np.ndarray | None = None + """Final cost or residual value from the correlation between subset in left and right image as calculated using ZNCC. shape=(img_num,y,x)""" + + ftol: np.ndarray | None = None + """Final `ftol` value from the optimization routine, indicating function tolerance. shape=(img_num,y,x)""" + + xtol: np.ndarray | None = None + """Final `xtol` value from the optimization routine, indicating solution tolerance. shape=(img_num,y,x)""" + + niter: np.ndarray | None = None + """Number of iterations taken to converge for each subset point. shape=(img_num,y,x)""" + @dataclass(slots=True) class Results: """ - Data container for Digital Image Correlation (DIC) analysis results. + Data container for DIC analysis results. This dataclass stores the displacements, convergence info, and correlation data associated with a DIC computation. @@ -22,20 +71,32 @@ class Results: ss_y: np.ndarray """The y-coordinates of the subset centers (in pixels). shape=(img_num,y,x)""" - u: np.ndarray - """Horizontal displacements at each subset location. shape=(img_num,y,x)""" + u_px: np.ndarray + """Horizontal displacements in pixels at each subset location. shape=(img_num,y,x)""" + + v_px: np.ndarray + """Vertical displacements in pixels at each subset location. shape=(img_num,y,x)""" + + u_mm: np.ndarray | None = None + """Horizontal displacement in physical units of mm relative in cam0 world coordinate system. shape=(img_num,y,x)""" + + v_mm: np.ndarray | None = None + """Vertical displacement in physical units of mm relative in cam0 world coordinate system. shape=(img_num,y,x)""" + + x_mm: np.ndarray | None = None + """X-coordinate in physical units of mm relative in cam0 world coordinate system. shape=(img_num,y,x)""" - v: np.ndarray - """Vertical displacements at each subset location. shape=(img_num,y,x)""" + y_mm: np.ndarray | None = None + """Y-coordinate in physical units of mm relative in cam0 world coordinate system. shape=(img_num,y,x)""" - mag: np.ndarray | None = None - """Displacement magnitude at each subset location, typically computed as sqrt(u^2 + v^2). shape=(img_num,y,x)""" + mag_px: np.ndarray | None = None + """Displacement magnitude in mm at each subset location, typically computed as sqrt(u^2 + v^2). shape=(img_num,y,x)""" converged: np.ndarray | None = None """boolean value for whether the subset has converged or not. shape=(img_num,y,x)""" cost: np.ndarray | None = None - """Final cost or residual value from the correlation optimization (e.g., ZNSSD). shape=(img_num,y,x)""" + """Final cost or residual value from the correlation optimization as calculated using ZNCC. shape=(img_num,y,x)""" ftol: np.ndarray | None = None """Final `ftol` value from the optimization routine, indicating function tolerance. shape=(img_num,y,x)""" @@ -46,8 +107,9 @@ class Results: niter: np.ndarray | None = None """Number of iterations taken to converge for each subset point. shape=(img_num,y,x)""" - shape_params: np.ndarray | None = None - """Optional shape parameters if output during DIC calculation (e.g., affine, rigid). shape=(img_num,y,x)""" - filenames: list[str] | None = None - """name of DIC result files that have been found""" \ No newline at end of file + """name of DIC result files that have been found""" + + stereo: StereoResults | None = None + """Optional field to store stereo DIC results if available.""" + diff --git a/src/pyvale/examples/dic/ex1_region_of_interest.py b/src/pyvale/examples/dic/ex01_region_of_interest.py similarity index 98% rename from src/pyvale/examples/dic/ex1_region_of_interest.py rename to src/pyvale/examples/dic/ex01_region_of_interest.py index 3f450c446..f6a1933f9 100644 --- a/src/pyvale/examples/dic/ex1_region_of_interest.py +++ b/src/pyvale/examples/dic/ex01_region_of_interest.py @@ -26,7 +26,7 @@ # displayed as the underlay during ROI selection. ref_img = dataset.dic_plate_with_hole_ref() roi = dic.RegionOfInterest(ref_image=ref_img) -roi.interactive_selection(subset_size=31) +roi.interactive_selection() # create a directory for the the different outputs output_path = Path.cwd() / "pyvale-output" diff --git a/src/pyvale/examples/dic/ex2_plate_with_hole.py b/src/pyvale/examples/dic/ex02_plate_with_hole.py similarity index 90% rename from src/pyvale/examples/dic/ex2_plate_with_hole.py rename to src/pyvale/examples/dic/ex02_plate_with_hole.py index 090690123..d3d086190 100644 --- a/src/pyvale/examples/dic/ex2_plate_with_hole.py +++ b/src/pyvale/examples/dic/ex02_plate_with_hole.py @@ -32,11 +32,11 @@ # The images used here are included in the `data `_ folder. # We've provided helper functions to load them regardless of your installation path. subset_size = 31 -ref_img = dataset.dic_plate_with_hole_ref() -def_img = dataset.dic_plate_with_hole_def() +ref_img = dataset.dic_plate_with_hole_cam0_ref() +def_img = dataset.dic_plate_with_hole_cam0_def() # create a directory for the the different outputs -output_path = Path.cwd() / "pyvale-output" +output_path = Path.cwd() / "pyvale-output" / "ex02" if not output_path.is_dir(): output_path.mkdir(parents=True, exist_ok=True) @@ -46,7 +46,8 @@ # as input. This image will be shown as the underlay during any ROI selection or # visualization. roi = dic.RegionOfInterest(ref_img) -roi.interactive_selection(subset_size) +roi.read_yaml("./ex10_roi.yaml") +#roi.interactive_selection() # %% # Once you've closed the ROI interactive window, a mask and seed location coordinates @@ -117,14 +118,14 @@ axes = axes.flatten() # First deformation image -im1 = axes[0].pcolor(dicdata.ss_x, dicdata.ss_y, dicdata.u[0]) -im2 = axes[1].pcolor(dicdata.ss_x, dicdata.ss_y, dicdata.v[0]) -im3 = axes[2].pcolor(dicdata.ss_x, dicdata.ss_y, dicdata.cost[0]) +im1 = axes[0].pcolor(dicdata.ss_x, dicdata.ss_y, dicdata.u_px[1]) +im2 = axes[1].pcolor(dicdata.ss_x, dicdata.ss_y, dicdata.v_px[1]) +im3 = axes[2].pcolor(dicdata.ss_x, dicdata.ss_y, dicdata.cost[1]) # Second deformation image -im4 = axes[3].pcolor(dicdata.ss_x, dicdata.ss_y, dicdata.u[1]) -im5 = axes[4].pcolor(dicdata.ss_x, dicdata.ss_y, dicdata.v[1]) -im6 = axes[5].pcolor(dicdata.ss_x, dicdata.ss_y, dicdata.cost[1]) +im4 = axes[3].pcolor(dicdata.ss_x, dicdata.ss_y, dicdata.u_px[2]) +im5 = axes[4].pcolor(dicdata.ss_x, dicdata.ss_y, dicdata.v_px[2]) +im6 = axes[5].pcolor(dicdata.ss_x, dicdata.ss_y, dicdata.cost[2]) # Titles axes[0].set_title('u component (def0000.tiff)') diff --git a/src/pyvale/examples/dic/ex3_plate_with_hole_strain.py b/src/pyvale/examples/dic/ex03_plate_with_hole_strain.py similarity index 91% rename from src/pyvale/examples/dic/ex3_plate_with_hole_strain.py rename to src/pyvale/examples/dic/ex03_plate_with_hole_strain.py index 06d89fe6a..49f92c6bb 100644 --- a/src/pyvale/examples/dic/ex3_plate_with_hole_strain.py +++ b/src/pyvale/examples/dic/ex03_plate_with_hole_strain.py @@ -26,12 +26,12 @@ # We'll start by importing the DIC data from the previous example. # create a directory for the the different outputs -output_path = Path.cwd() / "pyvale-output" +output_path = Path.cwd() / "pyvale-output" / "ex03" if not output_path.is_dir(): output_path.mkdir(parents=True, exist_ok=True) # specify where our input data is -input_data = output_path / "dic_results_*.csv" +input_data = Path.cwd() / "pyvale-output" / "ex02" / "dic_results_*.csv" # %% # You can calculate strain directly from the DIC results. @@ -50,7 +50,7 @@ # gradient tensor. If you also specify a `strain_formulation`, the corresponding # 2D strain tensor will be included in the output. strain.calculate_2d(data=input_data, window_size=5, window_element=4, - output_basepath=output_path) + output_basepath=output_path, strain_formulation="ALMANSI") # %% # Once the strain calculation is complete, you can import the results using @@ -69,10 +69,10 @@ axes = axes.flatten() fig.suptitle('Deformation Gradient for ' + straindata.filenames[0]) -im1 = axes[0].pcolor(straindata.window_x, straindata.window_y, straindata.def_xx[0]) -im2 = axes[1].pcolor(straindata.window_x, straindata.window_y, straindata.def_xy[0]) -im3 = axes[2].pcolor(straindata.window_x, straindata.window_y, straindata.def_yx[0]) -im4 = axes[3].pcolor(straindata.window_x, straindata.window_y, straindata.def_yy[0]) +im1 = axes[0].pcolor(straindata.window_x, straindata.window_y, straindata.def_xx[1]) +im2 = axes[1].pcolor(straindata.window_x, straindata.window_y, straindata.def_xy[1]) +im3 = axes[2].pcolor(straindata.window_x, straindata.window_y, straindata.def_yx[1]) +im4 = axes[3].pcolor(straindata.window_x, straindata.window_y, straindata.def_yy[1]) # titles axes[0].set_title('deformation gradient xx') diff --git a/src/pyvale/examples/dic/ex4_dic_blender.py b/src/pyvale/examples/dic/ex04_dic_blender.py similarity index 98% rename from src/pyvale/examples/dic/ex4_dic_blender.py rename to src/pyvale/examples/dic/ex04_dic_blender.py index 2a8cf78ab..4dfecee67 100644 --- a/src/pyvale/examples/dic/ex4_dic_blender.py +++ b/src/pyvale/examples/dic/ex04_dic_blender.py @@ -43,7 +43,7 @@ # Interactive ROI selection roi = dic.RegionOfInterest(ref_img) -roi.interactive_selection(subset_size) +roi.interactive_selection() #output_path output_path = Path.cwd() / "pyvale-output" diff --git a/src/pyvale/examples/dic/ex5_dic_challenge.py b/src/pyvale/examples/dic/ex05_dic_challenge.py similarity index 97% rename from src/pyvale/examples/dic/ex5_dic_challenge.py rename to src/pyvale/examples/dic/ex05_dic_challenge.py index fd734553d..fb301a59c 100644 --- a/src/pyvale/examples/dic/ex5_dic_challenge.py +++ b/src/pyvale/examples/dic/ex05_dic_challenge.py @@ -35,8 +35,8 @@ # %% # There's a pair of DIC challenge images that come as part of the Pyvale install. # We can preload them with: -ref_pattern = dataset.dic_challenge_ref() -def_pattern = dataset.dic_challenge_def() +ref_pattern = dataset.dic_chal_2d_ref() +def_pattern = dataset.dic_chal_2d_def() subset_size = 17 @@ -86,7 +86,7 @@ dicdata = dic.import_2d(data=data_path, layout='column', binary=False, delimiter=",") -# && +# %% # Finally a simple plot of the calculated displacements at y=2500. This could be # extended and compared to other DIC engines used in the 2.0 DIC challenge. A # link to the dataset can be found under '2D-DIC Challenge 2.0' can be found diff --git a/src/pyvale/examples/dic/ex6_hrdic.py b/src/pyvale/examples/dic/ex06_hrdic.py similarity index 94% rename from src/pyvale/examples/dic/ex6_hrdic.py rename to src/pyvale/examples/dic/ex06_hrdic.py index a2e4013d7..193a59389 100644 --- a/src/pyvale/examples/dic/ex6_hrdic.py +++ b/src/pyvale/examples/dic/ex06_hrdic.py @@ -53,8 +53,10 @@ subset_size=31, subset_step=15, max_displacement=1000, - fft_mad=True, - fft_mad_scale=3.0, + fft_filter=True, + fft_filter_threshold=3.0, + fft_filter_radius=3, + fft_filter_corr_power=2.0, correlation_criteria="ZNSSD", shape_function="AFFINE", threshold=0.8, @@ -65,7 +67,7 @@ # # * :code:`max_displacement`: Sets the assumed maximum displacment. It is better # to overestimate rather than underestimate this value -# * :code:`fft_mad` and :code:`fft_mad_scale`: These remove outliers by +# * :code:`fft_filter` and related settings: These remove outliers by # calculating a Mean Absolute Deviation (MAD) at each stage of the FFT windowing # and prevent the propagation of incorrect rigid body estimates down through the window sizes. # The mad scale value determines how strict the outlier removal is - larger values of diff --git a/src/pyvale/examples/dic/ex07_incremental.py b/src/pyvale/examples/dic/ex07_incremental.py new file mode 100644 index 000000000..017cbd551 --- /dev/null +++ b/src/pyvale/examples/dic/ex07_incremental.py @@ -0,0 +1,91 @@ +#================================================================================ +#Example: thermocouples on a 2d plate +# +#pyvale: the python validation engine +#License: MIT +#Copyright (C) 2024 The Computer Aided Validation Team +#================================================================================ +""" +2D Incremental DIC +--------------------- + +This example walks through setting up an incremental DIC caluculation for the +simple case of rigid body motion of a plate. Incremental DIC works by updating +the reference image at an interval to . it is good in cases where +there is large deformation.""" + +# %% +# While incremental can usually withstand a greater level of subset warping, it is important to remember that +# by updating the reference images you are compounding errors from previous +# correlations. The incremental DIC process as implemented in pyvale is best +# highlighted in the image below +# +# .. image:: ../../../../_static/incremental_light.png +# :alt: Incremental DIC +# :width: 100% +# :class: only-light +# +# .. image:: ../../../../_static/incremental_dark.png +# :alt: Incremental DIC +# :width: 100% +# :class: only-dark +# +# We start in the usual way by selecting the images and building the ROI: + +import matplotlib.pyplot as plt +from pathlib import Path +import numpy as np + +# pyvale modules +import pyvale.dataset as dataset +import pyvale.dic as dic + +subset_size = 31 +ref_img = dataset.dic_plate_rigid_ref() +def_img = dataset.dic_plate_rigid_def() + +# create a directory for the the different outputs +output_path = Path.cwd() / "pyvale-output" / "incremental" +if not output_path.is_dir(): + output_path.mkdir(parents=True, exist_ok=True) + +roi = dic.RegionOfInterest(ref_img) +roi.rect_boundary(left=50,right=50,top=50,bottom=50) + + +# %% +# We can now proceed with the incremental DIC calculation. There are three key +# arguments to be aware of when enabling incremental DIC: +# +# - ``incremental`` (bool): Enables incremental DIC when set to True. Default is False. +# - ``incremental_update_condition`` (``str``): Specifies the condition under which the reference +# image is updated. Valid options are: +# +# - ``"IMAGE"``: Update the reference image every N images, where N is given by +# ``incremental_update_value``. +# - ``"COST"``: Update the reference image when the mean ZNCC cost across all subsets +# falls below the threshold specified by ``incremental_update_value``. +# - ``"ITER"``: Update the reference image when the mean number of iterations exceeds +# the value specified by ``incremental_update_value``. +# - ``incremental_update_value`` (``int`` or ``float``): The threshold or interval used alongside +# ``incremental_update_condition``. +# +# In this example we will proceed with the simple case of updating the reference +# image after every image correlation procedure. Note: While the displacements +# are reported as a cumulative value, the reported ZNCC value in the results is relative to the +# subset in the current updated reference image, NOT the original reference image. + +dic.calculate_2d(reference=ref_img, + deformed=def_img, + roi_mask=roi.mask, + seed=[500,500], + subset_size=subset_size, + subset_step=10, + incremental=True, + incremental_update_condition="IMAGE", # can also be "COST" or "ITER" + incremental_update_value=1, # update the reference every 1 image(s) + output_basepath=output_path, + output_delimiter=",", + output_prefix="results_inc_") + + diff --git a/src/pyvale/examples/dic/ex08_calibration.py b/src/pyvale/examples/dic/ex08_calibration.py new file mode 100644 index 000000000..228cf9a80 --- /dev/null +++ b/src/pyvale/examples/dic/ex08_calibration.py @@ -0,0 +1,138 @@ +#================================================================================ +# Example: stereo calibration +# +#pyvale: the python validation engine +#License: MIT +#Copyright (C) 2024 The Computer Aided Validation Team +#================================================================================ +""" +Stereo Calibration + DIC +--------------------- + +This example detects a calibration-dot target in synchronised stereo images +and uses the detected correspondences to calibrate the two cameras. + +**The required calibration images are not distributed with the package to keep things lightweight. +** +`here `_. + +**Make sure to download/clone the repository and unzip the calibration images. +For this example we'll assume you've cloned the images to your current working +directory.** + +Currently, the DIC module in pyvale only supports the below type of calibration targets (If there's +a specific type of calibration target you would like adding then please get in touch +and we'll see what we can do). +""" + +# %% +# .. image:: ../../../../images/cal_target_flipped.png +# :alt: Calibration Target +# :width: 100% +# :align: center + + +# pyvale modules +import numpy as np +from pathlib import Path + +import pyvale.dic as dic +import pyvale.calib as calib + +# %% +# Calibration has two stages: the dot detection, then a bundle adjustment +# to find the camera parameters that minimise +# the reprojection error across every image pair. +cam0 = "./calibration-example-data/cam0_*.bmp" +cam1 = "./calibration-example-data/cam1_*.bmp" + +# %% +# Dot detection needs the image pairs, the target dimensions and spacing, and +# the three deliberately missing circles. Coordinates use the target grid with +# ``(0, 0)`` at the bottom-left dot; the missing dots can be specified in any order. + +dots0, dots1, grid, filenames0, filenames1 = calib.detect_dots( + cam0=cam0, + cam1=cam1, + grid_height=9, + grid_width=12, + missing_dots=[(9, 6), (2, 2), (2, 6)], # order of white dots does not matter + min_dot_fraction=0.5, # minimum fraction of matching dots. + grid_spacing=1.25, # in mm. +) + +# %% +# ``dots0`` and ``dots1`` are lists with one ``(N, 2)`` array per accepted image +# pair. Each row is a dot position in pixel coordinates. ``grid`` is the matching list of +# ``(N, 3)`` target coordinates in millimetres; it is the common object-point +# reference frame used by both cameras. The filename lists contain the accepted +# filenames from the dot detection. +# +# For a high volume of calibration images that have large dimensions. It might be worth +# saving the detected points so the detection doesn't need to be rerun everything you need +# to reperform a stereo calibration. + +# make the output directory +output_dir = Path.cwd() / "pyvale-output" / "ex08" +output_dir.mkdir(parents=True, exist_ok=True) + +# save the detected points to plain text files +for points0, points1, filename0, filename1 in zip(dots0, dots1, filenames0, filenames1): + np.savetxt(output_dir / f"dots_{Path(filename0).stem}.txt", points0) + np.savetxt(output_dir / f"dots_{Path(filename1).stem}.txt", points1) + +# %% +# The initial estimates are computed with Zhang's calibration method. More details +# of the approach, which we use as an initial estimate, can be found in the +# paper titled "A Flexible New Technique for Camera Calibration". The parameters from +# this method are then further refined with stereo bundle adjustment. You can toggle +# distortion parameters in the calibration using the ``optimize_distortion=`` function +# argument. The argument ``img_dims`` is ``[width, height]`` in +# pixels and must match the input images. + +calib_params, reproj_err0, reproj_err1 = calib.calibrate_stereo( + dots_cam0=dots0, + dots_cam1=dots1, + grid=grid, + optimize_distortion=False, # all distortion parameters set to 0. + img_dims=[2464, 2056], + error_formulation="RMSE" +) + +calib.savetxt(calib=calib_params, path=output_dir / "stereo_calibration.txt", delimiter=",") + +# %% +# ``calib_params`` contains the intrinsics of each camera (focal lengths, skew, +# principal point and distortion), plus the translation in millimetres and +# rotation in degrees from camera 0 to camera 1. ``reproj_err0`` and +# ``reproj_err1`` contain one root-mean-square reprojection error, in pixels, +# for each accepted image pair. Large values identify target poses that should +# be checked for poor dot detection or unsuitable images. + +print("") +print("Cam 0 Intrinsic Parameters:") +print(calib_params.cam0) +print("") +print("Cam 1 Intrinsic Parameters:") +print(calib_params.cam1) +print("") +print("Translation from cam 0 to cam 1 (mm):") +print(calib_params.translation) +print("") +print("Rotation from cam 0 to cam 1 (degrees):") +print(calib_params.rotation) +print("") +print("Reprojection Errors for cam 0 (pixels):") +print(reproj_err0) +print("") +print("Reprojection Errors for cam 1 (pixels):") +print(reproj_err1) + + + + + + + + + diff --git a/src/pyvale/examples/dic/ex09_stereo.py b/src/pyvale/examples/dic/ex09_stereo.py new file mode 100644 index 000000000..326c2885b --- /dev/null +++ b/src/pyvale/examples/dic/ex09_stereo.py @@ -0,0 +1,114 @@ +#================================================================================ +# Example: stereo calibration +# +#pyvale: the python validation engine +#License: MIT +#Copyright (C) 2024 The Computer Aided Validation Team +#================================================================================ +""" +Stereo DIC rigid body motion of a plate +--------------------- + +This example demonstrates how to perform stereo DIC using pyvale. The example +uses synthetic images generated using `Riley `_ +from a known calibration and deformation field. +The calibration parameters are loaded from a text file, and the DIC calculation +is performed on the reference and deformed images. +""" + +# pyvale modules +import numpy as np +from pathlib import Path +from dataclasses import fields + +import pyvale.dic as dic +import pyvale.calib as calib +import pyvale.dataset as dataset + +# %% +# Load in the ground truth calibration parameters. These are the parameters +# that were used to generate the synthetic images: + +calib_params = calib.loadtxt("./ex09_stereo_calibration.txt", delimiter=",") + + +# %% +# select the images and build the ROI: + +# reference images +ref0 = dataset.dic_plate_rigid_cam0_ref() +ref1 = dataset.dic_plate_rigid_cam1_ref() + +# deformed images +def0 = dataset.dic_plate_rigid_cam0_def() +def1 = dataset.dic_plate_rigid_cam1_def() + + +# Build ROI using cam 0 reference image +roi = dic.RegionOfInterest(ref0) +roi.rect_boundary(50,50,50,50) + +# create an output directory +output_path = Path.cwd() / "pyvale-output" / "ex09" +if not output_path.is_dir(): + output_path.mkdir(parents=True, exist_ok=True) + +# %% +# To perform the DIC calculation all the arguments we pass in the reference and deformed +# images as a list. we also pass in the calibration parameters. The rest +# of the arguments remain the same as ``dic.calculate_2d``. + +dic.calculate_3d(reference=[ref0, ref1], + deformed=[def0, def1], + calibration=calib_params, + roi_mask=roi.mask, + seed=[500,500], + subset_size=31, + subset_step=10, + max_displacement=100, + output_basepath=output_path) + +# %% +# can now import the results using dic.import_3d +dic_files = output_path / "dic_results_*.csv" +dic_results = dic.import_3d(data=dic_files, delimiter=",", binary=False) + +# %% +# The stereo DIC results are stored in a nested dataclass. The fields can be +# seen using the below commands: + +print("") +print("DIC results fields:") +for field in fields(dic_results): + print(f"{field.name}") + +print("") +print("Stereo DIC results fields:") +for field in fields(dic_results.stereo): + print(f"{field.name}") + +# %% +# plot a 3d reconstruction using pyvista + +import pyvista as pv + +stereo = dic_results.stereo +frame = 12 + + +points = np.column_stack(( + np.ravel(stereo.x_mm[frame]), + np.ravel(stereo.y_mm[frame]), + np.ravel(stereo.z_mm[frame]), +)) + +cloud = pv.PolyData(points) +cloud['elevation'] = points[:, 1] +cloud.plot(eye_dome_lighting=True) + + + + + + + diff --git a/src/pyvale/examples/dic/ex09_stereo_calibration.txt b/src/pyvale/examples/dic/ex09_stereo_calibration.txt new file mode 100644 index 000000000..1bb2899ae --- /dev/null +++ b/src/pyvale/examples/dic/ex09_stereo_calibration.txt @@ -0,0 +1,26 @@ +cam0_fx_px,14492.753623188406 +cam0_fy_px,14492.753623188406 +cam0_fs_px,0.00000000000 +cam0_cx_px,520.000000000000 +cam0_cy_px,770.000000000000 +cam0_k1,0.0000000000 +cam0_k2,0.0000000000 +cam0_p1,0.0000000000 +cam0_p2,0.0000000000 +cam0_k3,0.0000000000 +cam1_fx_px,14492.753623188406 +cam1_fy_px,14492.753623188406 +cam1_fs_px,0.0000000000 +cam1_cx_px,520.000000000000 +cam1_cy_px,770.000000000000 +cam1_k1,0.0000000000 +cam1_k2,0.0000000000 +cam1_p1,0.0000000000 +cam1_p2,0.0000000000 +cam1_k3,0.0000000000 +tx_mm,49.5681367139 +ty_mm,0.000000000000 +tz_mm,-8.7401998861 +theta_deg,0.000000000000 +phi_deg,20.000000000000 +psi_deg,0.000000000000 diff --git a/src/pyvale/examples/dic/ex10_roi.yaml b/src/pyvale/examples/dic/ex10_roi.yaml new file mode 100644 index 000000000..467c5b9c5 --- /dev/null +++ b/src/pyvale/examples/dic/ex10_roi.yaml @@ -0,0 +1,24 @@ +- type: RectROI + pos: + - 30.837242290658914 + - 31.606027336089696 + size: + - 980.181163810901 + - 1482.7271790648015 + add: true +- type: CircleROI + pos: + - 519.9337896112705 + - 771.9337896112705 + size: + - 265.8675792225411 + - 265.8675792225411 + add: false +- type: SeedROI + pos: + - 297 + - 376 + size: + - 21 + - 21 + add: true diff --git a/src/pyvale/examples/dic/ex10_stereo_platehole.py b/src/pyvale/examples/dic/ex10_stereo_platehole.py new file mode 100644 index 000000000..2a061aa46 --- /dev/null +++ b/src/pyvale/examples/dic/ex10_stereo_platehole.py @@ -0,0 +1,112 @@ +# %% +#================================================================================ +# Example: stereo calibration +# +#pyvale: the python validation engine +#License: MIT +#Copyright (C) 2024 The Computer Aided Validation Team +#================================================================================ +""" +Stereo DIC and strain calculation of a plate with a hole +--------------------- + +This example demonstrates how to perform stereo DIC using pyvale. The example +uses synthetic images generated using `Riley `_ +from a known calibration and deformation field. +The calibration parameters are loaded from a text file, and the DIC calculation +is performed on the reference and deformed images. +""" + +# pyvale modules +import numpy as np +from pathlib import Path + +import pyvale.dic as dic +import pyvale.calib as calib +import pyvale.strain as strain +import pyvale.dataset as dataset + +# %% +# We can use the same calibration parameters as before: + +calib_params = calib.loadtxt("./ex09_stereo_calibration.txt", delimiter=",") + + +# %% +# select the images and build the ROI: + +# reference images +ref0 = dataset.dic_plate_with_hole_cam0_ref() +ref1 = dataset.dic_plate_with_hole_cam1_ref() + +# deformed images +def0 = dataset.dic_plate_with_hole_cam0_def() +def1 = dataset.dic_plate_with_hole_cam1_def() + +# Build ROI using cam 0 reference image +roi = dic.RegionOfInterest(ref0) +roi.read_yaml("./ex10_roi.yaml") +# roi.interactive_selection() + +# %% + +# create an output directory +output_path = Path.cwd() / "pyvale-output" / "ex10" +if not output_path.is_dir(): + output_path.mkdir(parents=True, exist_ok=True) + +# %% +# To perform the DIC calculation all the arguments we pass in the reference and deformed +# images as a list. we also pass in the calibration parameters. The rest +# of the arguments remain the same as ``dic.calculate_2d``. + +dic.calculate_3d(reference=[ref0, ref1], + deformed=[def0, def1], + calibration=calib_params, + roi_mask=roi.mask, + seed=roi.seed, + subset_size=31, + subset_step=10, + output_basepath=output_path) + + +# %% +# Perform a strain calculation on the stereo DIC results: + +strain.calculate_3d(data=output_path / "dic_results*", + window_size=5, + window_element = 9, + strain_formulation="ALMANSI", + output_basepath=output_path) + +# %% +# import the results: +strain_files = output_path / "strain_*.csv" +strain_results = strain.import_3d(data=strain_files, delimiter=",", binary=False) + +# %% +# plot a 3d reconstruction using pyvista + +import pyvista as pv + +frame = 2 + + +points = np.column_stack(( + np.ravel(strain_results.x_mm[frame]), + np.ravel(strain_results.y_mm[frame]), + np.ravel(strain_results.z_mm[frame]) +)) + +cloud = pv.PolyData(points) +cloud["eps_xx"] = np.ravel(strain_results.eps_xx[frame]) +cloud.plot( + scalars="eps_xx", + eye_dome_lighting=True, +) + +# %% +# .. image:: ../../../../_static/dic_ex10_3d.png +# :alt: ROI +# :width: 100% +# :align: center diff --git a/src/pyvale/examples/dic/ex11_dic_chal.py b/src/pyvale/examples/dic/ex11_dic_chal.py new file mode 100644 index 000000000..a55eed9e9 --- /dev/null +++ b/src/pyvale/examples/dic/ex11_dic_chal.py @@ -0,0 +1,107 @@ +# %% +#================================================================================ +# Example: stereo calibration +# +#pyvale: the python validation engine +#License: MIT +#Copyright (C) 2024 The Computer Aided Validation Team +#================================================================================ +""" +Stereo Reconstruction from the DIC Challenge 1.0 +--------------------- + +For this exampe we'll reconstruct the bespoke sample used in the first iteration +of the `Stereo DIC challenge `_. +To keep the amount of images/data distributed with the package to a minimum, +we've already run the calibration (but feel free to try running the calibration +yourself by downloading all the images from the Stereo DIC challenge archive) +and the intrinsic/extrinisic parameters can be found in +`ex11_dic_chal_calibration.txt`. In this example we'll just use the reference +images from the left and right camera to build a 3D reconstruction. +""" + +# pyvale modules +import numpy as np +from pathlib import Path + +import pyvale.dic as dic +import pyvale.calib as calib +import pyvale.strain as strain +import pyvale.dataset as dataset + +# %% +# Import the calibration parameters: + +calib_params = calib.loadtxt("./ex11_dic_chal_calibration.txt", delimiter=",") + + +# %% +# select the images and build the ROI: + +# reference images +ref0 = dataset.dic_chal_3d_cam0() +ref1 = dataset.dic_chal_3d_cam1() + +# Build ROI using cam 0 reference image. +roi = dic.RegionOfInterest(ref0) +# roi.interactive_selection() # <- you can use the intercatvie_selection to view the yaml +roi.read_yaml("./ex11_dic_chal_roi.yaml") + + +# %% +# .. image:: ../../../../_static/dic_ex11_roi.png +# :alt: ROI +# :width: 100% +# :align: center + +# %% + +# create an output directory +output_path = Path.cwd() / "pyvale-output" / "ex11" +if not output_path.is_dir(): + output_path.mkdir(parents=True, exist_ok=True) + +# %% +# Perform the DIC: + +dic.calculate_3d(reference=[ref0, ref1], + deformed=[ref0, ref1], + calibration=calib_params, + roi_mask=roi.mask, + seed=roi.seed, + subset_size=27, + subset_step=2, + output_basepath=output_path) + +# %% +# import the results: +dic_files = output_path / "dic_results_*.csv" +dic_results = dic.import_3d(data=dic_files, delimiter=",", binary=False) + +# %% +# plot a 3d reconstruction using pyvista, using the stereo cost value as the marker colour + +import pyvista as pv + +stereo = dic_results.stereo +frame = 0 + + +points = np.column_stack(( + np.ravel(stereo.x_mm[frame]), + np.ravel(stereo.y_mm[frame]), + np.ravel(stereo.z_mm[frame]) +)) + +cloud = pv.PolyData(points) +cloud["cost"] = np.ravel(stereo.cost[frame]) +cloud.plot( + scalars="cost", + eye_dome_lighting=True, +) + +# %% +# .. image:: ../../../../_static/dic_ex11_3d.png +# :alt: ROI +# :width: 100% +# :align: center diff --git a/src/pyvale/examples/dic/ex11_dic_chal_calibration.txt b/src/pyvale/examples/dic/ex11_dic_chal_calibration.txt new file mode 100644 index 000000000..d96f12d9f --- /dev/null +++ b/src/pyvale/examples/dic/ex11_dic_chal_calibration.txt @@ -0,0 +1,26 @@ +cam0_fx_px,10646.2635738781 +cam0_fy_px,10634.51868186054 +cam0_fs_px,6.0256153526190985 +cam0_cx_px,1277.295672555161 +cam0_cy_px,1042.045005632957 +cam0_k1,0.05945001004712857 +cam0_k2,1.5791177314518978 +cam0_p1,0.0006657638711079392 +cam0_p2,0.0020726942833998656 +cam0_k3,-125.55586762886281 +cam1_fx_px,10752.760690885829 +cam1_fy_px,10742.236251897699 +cam1_fs_px,3.5217413878639987 +cam1_cx_px,1346.8645745793767 +cam1_cy_px,1061.8914683804305 +cam1_k1,0.034064244757864716 +cam1_k2,1.9316394775807741 +cam1_p1,0.0017285205001798549 +cam1_p2,0.0033949265498711043 +cam1_k3,-109.95333886836004 +tx_mm,-295.173391339363 +ty_mm,-0.9711130451639047 +tz_mm,72.1527719292799 +theta_deg,0.08995318230570155 +phi_deg,25.620215067039297 +psi_deg,-0.0961434679647092 diff --git a/src/pyvale/examples/dic/ex11_dic_chal_roi.yaml b/src/pyvale/examples/dic/ex11_dic_chal_roi.yaml new file mode 100644 index 000000000..7d950030b --- /dev/null +++ b/src/pyvale/examples/dic/ex11_dic_chal_roi.yaml @@ -0,0 +1,43 @@ +- type: PolyLineROI + points: + - - 465.4554535914519 + - 187.05839153246757 + - - 484.525009227421 + - 1763.474990772579 + - - 2037.6343738013481 + - 1746.5242746517179 + - - 1988.9010649538716 + - 206.12794716843666 + add: true +- type: SeedROI + pos: + - 1554 + - 1204 + size: + - 25 + - 25 + add: true +- type: SeedROI + pos: + - 842 + - 1202 + size: + - 25 + - 25 + add: true +- type: SeedROI + pos: + - 770 + - 301 + size: + - 25 + - 25 + add: true +- type: SeedROI + pos: + - 1880 + - 551 + size: + - 25 + - 25 + add: true diff --git a/src/pyvale/strain/__init__.py b/src/pyvale/strain/__init__.py index 215390b8d..b5ad12ed7 100644 --- a/src/pyvale/strain/__init__.py +++ b/src/pyvale/strain/__init__.py @@ -4,10 +4,13 @@ # Copyright (C) 2025 The Computer Aided Validation Team #=============================================================================== -from .strain import calculate_2d -from .strainimport import import_2d +from .strain2d import calculate_2d +from .strain3d import calculate_3d +from .strainimport import import_2d, import_3d from .strainresults import StrainResults __all__ = ["calculate_2d", + "calculate_3d", "StrainResults", - "import_2d"] + "import_2d", + "import_3d"] diff --git a/src/pyvale/strain/cpp/bindings.cpp b/src/pyvale/strain/cpp/bindings.cpp index 1d1cd22c9..86e425a74 100644 --- a/src/pyvale/strain/cpp/bindings.cpp +++ b/src/pyvale/strain/cpp/bindings.cpp @@ -19,7 +19,9 @@ PYBIND11_MODULE(strain_cpp, m) { py::add_ostream_redirect(m, "ostream_redirect"); - // Bind the engine function - m.def("strain_engine", &strain::engine, "Run DIC analysis on input images with config"); + // Bind the engine functions + m.def("strain_engine", &strain::engine, "Run 2D strain analysis on input images with config"); + m.def("strain_engine_2d", &strain::engine_2d, "Run 2D strain analysis using subset-grid coordinates"); + m.def("strain_engine_3d", &strain::engine_3d, "Run 3D surface strain analysis using physical stereo coordinates"); } diff --git a/src/pyvale/strain/cpp/smooth.cpp b/src/pyvale/strain/cpp/smooth.cpp index 1c05a7b7a..a76d80d5d 100644 --- a/src/pyvale/strain/cpp/smooth.cpp +++ b/src/pyvale/strain/cpp/smooth.cpp @@ -17,15 +17,18 @@ namespace smooth { // bilinear lagrange polynomials - Eigen::VectorXd q4(std::vector &x, std::vector &y, - std::vector& disp_vals){ + Eigen::VectorXd q4(const std::vector &x, const std::vector &y, + const std::vector& disp_vals){ Eigen::MatrixXd A(disp_vals.size(), 4); Eigen::VectorXd b(disp_vals.size()); + const size_t centre_idx = x.size() / 2; + const double x0 = x[centre_idx]; + const double y0 = y[centre_idx]; for (size_t i = 0; i < x.size(); ++i) { - double ss_x = x[i]; - double ss_y = y[i]; + double ss_x = x[i] - x0; + double ss_y = y[i] - y0; A(i, 0) = 1.0; A(i, 1) = ss_x; A(i, 2) = ss_y; @@ -39,15 +42,18 @@ namespace smooth { } // biquadratic lagrange polynomials - Eigen::VectorXd q9(std::vector& x, std::vector& y, - std::vector& disp_vals) { + Eigen::VectorXd q9(const std::vector& x, const std::vector& y, + const std::vector& disp_vals) { Eigen::MatrixXd A(disp_vals.size(), 9); Eigen::VectorXd b(disp_vals.size()); + const size_t centre_idx = x.size() / 2; + const double x0 = x[centre_idx]; + const double y0 = y[centre_idx]; for (size_t i = 0; i < x.size(); ++i) { - double ss_x = x[i]; - double ss_y = y[i]; + double ss_x = x[i] - x0; + double ss_y = y[i] - y0; A(i, 0) = 1.0; A(i, 1) = ss_x; A(i, 2) = ss_y; diff --git a/src/pyvale/strain/cpp/smooth.hpp b/src/pyvale/strain/cpp/smooth.hpp index c3681b5fc..16e756f67 100644 --- a/src/pyvale/strain/cpp/smooth.hpp +++ b/src/pyvale/strain/cpp/smooth.hpp @@ -26,7 +26,7 @@ namespace smooth { * @param[in] disp_vals * @return Eigen::VectorXd A vector of coefficients for a bilinear fit inside strain window */ - Eigen::VectorXd q4(std::vector &x, std::vector &y, std::vector& disp_vals); + Eigen::VectorXd q4(const std::vector &x, const std::vector &y, const std::vector& disp_vals); /** * @brief @@ -36,7 +36,7 @@ namespace smooth { * @param[in] disp_vals * @return Eigen::VectorXd A vector of coefficients for a bilinear fit inside strain window */ - Eigen::VectorXd q9(std::vector &x, std::vector &y, std::vector& disp_vals); + Eigen::VectorXd q9(const std::vector &x, const std::vector &y, const std::vector& disp_vals); /** * @brief diff --git a/src/pyvale/strain/cpp/strain.cpp b/src/pyvale/strain/cpp/strain.cpp index fa5059c87..900abe611 100644 --- a/src/pyvale/strain/cpp/strain.cpp +++ b/src/pyvale/strain/cpp/strain.cpp @@ -11,6 +11,10 @@ #include #include #include +#include +#include +#include +#include // pybind header files #include @@ -20,6 +24,7 @@ #include // common_cpp header files +#include "../../common_cpp/dicsignalhandler.hpp" #include "../../common_cpp/progressbar.hpp" #include "../../common_cpp/defines.hpp" #include "../../common_cpp/util.hpp" @@ -28,114 +33,238 @@ #include "./smooth.hpp" #include "./strain.hpp" - - namespace py = pybind11; namespace strain { - Eigen::Matrix2d I = Eigen::Matrix2d::Identity(); - - void engine(const py::array_t &ss_x_arr, - const py::array_t &ss_y_arr, - const py::array_t &u_arr, - const py::array_t &v_arr, - const int nss_x, const int nss_y, - const int nimg, const int sw_size, - const int q, const std::string &form, - const std::vector &filenames, - const common_util::SaveConfig &strain_save_conf){ - - TITLE("Strain Config"); - INFO_OUT("Number of Images:", nimg) - INFO_OUT("Strain window size:", sw_size) - INFO_OUT("Strain element (4 = bilinear, 9 is biquadratic):", q); - INFO_OUT("Strain formulation:", form); - INFO_OUT("Saving data to folder:", strain_save_conf.basepath) - INFO_OUT("Saving data as binary ", strain_save_conf.binary) + Eigen::Matrix3d I = Eigen::Matrix3d::Identity(); + namespace { + using SmoothFn = std::function&, + const std::vector&, + const std::vector&)>; - const bool save_at_end = strain_save_conf.at_end; - const int nwindows = nss_x*nss_y; + Eigen::Vector2d eval_poly_gradient_at_centre(const int q, const Eigen::VectorXd &c, + const double x0, const double y0) { + Eigen::Vector2d F = Eigen::Vector2d::Zero(); - // get raw pointers for numpy arrays - int* ss_x = static_cast(ss_x_arr.request().ptr); - int* ss_y = static_cast(ss_y_arr.request().ptr); - double* u = static_cast(u_arr.request().ptr); - double* v = static_cast(v_arr.request().ptr); - - // function wrapper for bilinear or biquadratic element - std::function&, std::vector&, std::vector&)> smooth_window = (q == 4) ? smooth::q4 : smooth::q9; - - - strain::Window window(sw_size); - strain::Results results(save_at_end ? nimg * nwindows : nwindows); - - - TITLE("Deformation Gradient and Strain Calculation") - - // loop over the displacement images - for (int img_num = 0; img_num < nimg; img_num++) { + if (q == 4) { + F(0) = c[1] + c[3] * y0; + F(1) = c[2] + c[3] * x0; + } + else if (q == 9) { + F(0) = c[1] + c[3]*y0 + 2.0*c[4]*x0 + 2.0*c[6]*x0*y0 + + c[7]*y0*y0 + 2.0*c[8]*x0*y0*y0; + F(1) = c[2] + c[3]*x0 + 2.0*c[5]*y0 + c[6]*x0*x0 + + 2.0*c[7]*x0*y0 + 2.0*c[8]*x0*x0*y0; + } + else { + throw std::invalid_argument("Unsupported polynomial order"); + } - ProgressBar pbar(filenames[img_num], nwindows); + return F; + } - // loop over strain windows within the image - for (int sw = 0; sw < nwindows; sw++){ + Eigen::Matrix3d compute_tangent_fit_coordinates(Window &window, + const int centre_idx) { + const Eigen::Vector3d centre(window.x_mm[centre_idx], + window.y_mm[centre_idx], + window.z_mm[centre_idx]); + + Eigen::Matrix3d covariance = Eigen::Matrix3d::Zero(); + for (size_t i = 0; i < window.x_mm.size(); ++i) { + Eigen::Vector3d d(window.x_mm[i], window.y_mm[i], window.z_mm[i]); + d -= centre; + covariance += d * d.transpose(); + } - int x0 = ss_x[sw]; - int y0 = ss_y[sw]; - results.x[sw] = x0; - results.y[sw] = y0; + Eigen::SelfAdjointEigenSolver solver(covariance); + if (solver.info() != Eigen::Success) { + throw std::runtime_error("Tangent-basis eigen decomposition failed."); + } - // TODO: through a warning. NAN out the entire window. - // it should be up to the user whether they correct with - // outlier removal / smoothing. - results.valid_window[sw] = fill_window(ss_x, ss_y, u, v, img_num, - sw, window, nss_x, - nss_y, sw_size); + Eigen::Matrix3d basis; + basis.col(0) = solver.eigenvectors().col(2).normalized(); + basis.col(1) = solver.eigenvectors().col(1).normalized(); + basis.col(2) = basis.col(0).cross(basis.col(1)); - // element coefficients - Eigen::VectorXd uc; - Eigen::VectorXd vc; + for (size_t i = 0; i < window.x_mm.size(); ++i) { + Eigen::Vector3d d(window.x_mm[i], window.y_mm[i], window.z_mm[i]); + d -= centre; + window.x[i] = d.dot(basis.col(0)); + window.y[i] = d.dot(basis.col(1)); + } - // 2D deformation gradient matrix and identity matrix - Eigen::Matrix2d deform_grad = Eigen::Matrix2d::Zero(); - Eigen::Matrix2d eps = Eigen::Matrix2d::Zero(); + return basis; + } - if (results.valid_window[sw]){ - uc = smooth_window(window.x, window.y, window.u); - vc = smooth_window(window.x, window.y, window.v); - deform_grad = compute_def_grad(q, uc, vc, x0, y0); - eps = compute_strain(form, deform_grad); - append_results(sw, results, save_at_end, x0, y0, - deform_grad, eps, nwindows, img_num); + void engine_impl(const py::array_t &ss_x_arr, + const py::array_t &ss_y_arr, + const py::array_t &x_mm_arr, + const py::array_t &y_mm_arr, + const py::array_t &z_mm_arr, + const py::array_t &u_arr, + const py::array_t &v_arr, + const py::array_t &w_arr, + const int nss_x, const int nss_y, + const int nimg, const int sw_size, + const int q, const std::string &form, + const std::vector &filenames, + const common_util::SaveConfig &strain_save_conf, + const int debug_level, + const bool use_3d_coordinates) { + + signal(SIGINT, signalHandler); + g_debug_level = debug_level; + + const int nwindows = nss_x * nss_y; + + int* ss_x = static_cast(ss_x_arr.request().ptr); + int* ss_y = static_cast(ss_y_arr.request().ptr); + double* x_mm = static_cast(x_mm_arr.request().ptr); + double* y_mm = static_cast(y_mm_arr.request().ptr); + double* z_mm = static_cast(z_mm_arr.request().ptr); + double* u = static_cast(u_arr.request().ptr); + double* v = static_cast(v_arr.request().ptr); + double* w = static_cast(w_arr.request().ptr); + + SmoothFn smooth_window = (q == 4) ? SmoothFn(smooth::q4) : SmoothFn(smooth::q9); + strain::Results results(nwindows); + + for (int img_num = 0; img_num < nimg; img_num++) { + + ProgressBar pbar(filenames[img_num], nwindows); + std::atomic current_progress(0); + + #pragma omp parallel for schedule(static) + for (int sw = 0; sw < nwindows; sw++){ + + Window window(sw_size); + + const int x0 = ss_x[sw]; + const int y0 = ss_y[sw]; + const int idx_3d_centre = nss_x*nss_y*img_num + sw; + results.x[sw] = x0; + results.y[sw] = y0; + results.x_mm[sw] = x_mm[idx_3d_centre]; + results.y_mm[sw] = y_mm[idx_3d_centre]; + results.z_mm[sw] = z_mm[idx_3d_centre]; + + if (use_3d_coordinates) { + results.valid_window[sw] = fill_window_3d(ss_x, ss_y, x_mm, y_mm, z_mm, + u, v, w, img_num, sw, window, + nss_x, nss_y, sw_size); + } + else { + results.valid_window[sw] = fill_window_2d(ss_x, ss_y, u, v, w, img_num, + sw, window, nss_x, nss_y, sw_size); + } + + Eigen::Matrix3d F = Eigen::Matrix3d::Zero(); + Eigen::Matrix3d eps = Eigen::Matrix3d::Zero(); + + if (results.valid_window[sw]){ + if (use_3d_coordinates) { + const int centre_idx = (sw_size * sw_size) / 2; + Eigen::Matrix3d tangent_basis = compute_tangent_fit_coordinates(window, centre_idx); + + Eigen::VectorXd uc = smooth_window(window.x, window.y, window.u); + Eigen::VectorXd vc = smooth_window(window.x, window.y, window.v); + Eigen::VectorXd wc = smooth_window(window.x, window.y, window.w); + + F = compute_surface_F_3d(q, uc, vc, wc, tangent_basis); + } + else { + Eigen::VectorXd uc = smooth_window(window.x, window.y, window.u); + Eigen::VectorXd vc = smooth_window(window.x, window.y, window.v); + Eigen::VectorXd wc = smooth_window(window.x, window.y, window.w); + + F = compute_F_2d(q, uc, vc, wc, 0.0, 0.0); + } + + eps = compute_strain(form, F); + append_results(sw, results, x0, y0, F, eps, nwindows); + } + + if (g_debug_level>0){ + int progress = current_progress.fetch_add(1); + if (omp_get_thread_num() == 0) pbar.update(progress+1); + } } - if (omp_get_thread_num() == 0) pbar.update(sw+1); - - } - - // finish up progress bar - pbar.finish(); + if(g_debug_level>0){ + pbar.finish(); + } - if (!save_at_end){ - strain::save_to_disk(img_num, results, strain_save_conf, - nwindows, nimg, filenames); + strain::save_to_disk(img_num, results, strain_save_conf, nwindows, nimg, filenames); + + if (stop_request) break; } + + raise_on_interrupt(); } + } - if (save_at_end){ - for (int img_num = 0; img_num < nimg; img_num++) - strain::save_to_disk(img_num, results, strain_save_conf, - nwindows, nimg, filenames); - } + void engine_2d(const py::array_t &ss_x_arr, + const py::array_t &ss_y_arr, + const py::array_t &x_mm_arr, + const py::array_t &y_mm_arr, + const py::array_t &z_mm_arr, + const py::array_t &u_arr, + const py::array_t &v_arr, + const py::array_t &w_arr, + const int nss_x, const int nss_y, + const int nimg, const int sw_size, + const int q, const std::string &form, + const std::vector &filenames, + const common_util::SaveConfig &strain_save_conf, + const int debug_level) { + engine_impl(ss_x_arr, ss_y_arr, x_mm_arr, y_mm_arr, z_mm_arr, u_arr, v_arr, w_arr, + nss_x, nss_y, nimg, sw_size, q, form, filenames, strain_save_conf, + debug_level, false); } + void engine_3d(const py::array_t &ss_x_arr, + const py::array_t &ss_y_arr, + const py::array_t &x_mm_arr, + const py::array_t &y_mm_arr, + const py::array_t &z_mm_arr, + const py::array_t &u_arr, + const py::array_t &v_arr, + const py::array_t &w_arr, + const int nss_x, const int nss_y, + const int nimg, const int sw_size, + const int q, const std::string &form, + const std::vector &filenames, + const common_util::SaveConfig &strain_save_conf, + const int debug_level) { + engine_impl(ss_x_arr, ss_y_arr, x_mm_arr, y_mm_arr, z_mm_arr, u_arr, v_arr, w_arr, + nss_x, nss_y, nimg, sw_size, q, form, filenames, strain_save_conf, + debug_level, true); + } + void engine(const py::array_t &ss_x_arr, + const py::array_t &ss_y_arr, + const py::array_t &x_mm_arr, + const py::array_t &y_mm_arr, + const py::array_t &z_mm_arr, + const py::array_t &u_arr, + const py::array_t &v_arr, + const py::array_t &w_arr, + const int nss_x, const int nss_y, + const int nimg, const int sw_size, + const int q, const std::string &form, + const std::vector &filenames, + const common_util::SaveConfig &strain_save_conf, + const int debug_level) { + engine_2d(ss_x_arr, ss_y_arr, x_mm_arr, y_mm_arr, z_mm_arr, u_arr, v_arr, w_arr, + nss_x, nss_y, nimg, sw_size, q, form, filenames, strain_save_conf, + debug_level); + } - bool fill_window(int *ss_x, int *ss_y, double *u, double *v, - int img, int sw, Window &window, - int nss_x, int nss_y, int sw_size){ + bool fill_window_2d(int *ss_x, int *ss_y, double *u, double *v, double *w, + int img, int sw, Window &window, + int nss_x, int nss_y, int sw_size){ const int swr = sw_size / 2; const int x0_idx = sw % nss_x; @@ -157,75 +286,120 @@ namespace strain { int idx_3d = nss_x*nss_y*img + idx_2d; // check if all subsets in the strain window are not nan - if (std::isnan(u[idx_3d]) || std::isnan(v[idx_3d])) return false; + if (std::isnan(u[idx_3d]) || std::isnan(v[idx_3d]) || std::isnan(w[idx_3d])) return false; - // populate subset window - window.x[widx] = ss_x[idx_2d]; - window.y[widx] = ss_y[idx_2d]; + window.x[widx] = static_cast(ss_x[idx_2d]); + window.y[widx] = static_cast(ss_y[idx_2d]); window.u[widx] = u[idx_3d]; window.v[widx] = v[idx_3d]; - - //std::cout << window.x[idx] << " " << window.y[idx] << " "; - //std::cout << window.u[idx] << " " << window.v[idx] << std::endl; + window.w[widx] = w[idx_3d]; widx++; } } return true; } + bool fill_window_3d(int *ss_x, int *ss_y, double *x_mm, double *y_mm, double *z_mm, + double *u, double *v, double *w, + int img, int sw, Window &window, + int nss_x, int nss_y, int sw_size){ + + const int swr = sw_size / 2; + const int x0_idx = sw % nss_x; + const int y0_idx = sw / nss_x; + const int xmin = x0_idx - swr; + const int xmax = x0_idx + swr; + const int ymin = y0_idx - swr; + const int ymax = y0_idx + swr; - Eigen::Matrix2d compute_def_grad(const int q, const Eigen::VectorXd &uc, - const Eigen::VectorXd& vc, const double x0, - const double y0) { + if ((xmin < 0) || (xmax >= nss_x) || (ymin < 0) || (ymax >= nss_y)) return false; + + int widx = 0; + for (int j = ymin; j <= ymax; j++){ + for (int i = xmin; i <= xmax; i++){ + int idx_2d = nss_x*j + i; + int idx_3d = nss_x*nss_y*img + idx_2d; - Eigen::Matrix2d grad; + if (std::isnan(x_mm[idx_3d]) || std::isnan(y_mm[idx_3d]) || std::isnan(z_mm[idx_3d]) || + std::isnan(u[idx_3d]) || std::isnan(v[idx_3d]) || std::isnan(w[idx_3d])) return false; - if (q == 4) { - grad(0,0) = 1.0 + uc[1] + uc[3]*y0; - grad(0,1) = uc[2] + uc[3]*x0; - grad(1,0) = vc[1] + vc[3]*y0; - grad(1,1) = 1.0 + vc[2] + vc[3]*x0; - } - else if (q == 9) { - grad(0,0) = 1.0 + uc[1] + uc[3]*y0 + 2.0*uc[4]*x0 + 2.0*uc[6]*x0*y0 + uc[7]*y0*y0 + 2.0*uc[8]*x0*y0*y0; - grad(0,1) = uc[2] + uc[3]*x0 + 2.0*uc[5]*y0 + uc[6]*x0*x0 + 2.0*uc[7]*x0*y0 + 2.0*uc[8]*x0*x0*y0; - grad(1,0) = vc[1] + vc[3]*y0 + 2.0*vc[4]*x0 + 2.0*vc[6]*x0*y0 + vc[7]*y0*y0 + 2.0*vc[8]*x0*y0*y0; - grad(1,1) = 1.0 + vc[2] + vc[3]*x0 + 2.0*vc[5]*y0 + vc[6]*x0*x0 + 2.0*vc[7]*x0*y0 + 2.0*vc[8]*x0*x0*y0; + window.x_mm[widx] = x_mm[idx_3d]; + window.y_mm[widx] = y_mm[idx_3d]; + window.z_mm[widx] = z_mm[idx_3d]; + window.u[widx] = u[idx_3d]; + window.v[widx] = v[idx_3d]; + window.w[widx] = w[idx_3d]; + widx++; + } } + return true; + } - return grad; + Eigen::Matrix3d compute_F_2d(const int q, + const Eigen::VectorXd &uc, + const Eigen::VectorXd &vc, + const Eigen::VectorXd &wc, + const double x0, + const double y0) { + + Eigen::Matrix3d F = Eigen::Matrix3d::Zero(); + Eigen::Vector2d gu = eval_poly_gradient_at_centre(q, uc, x0, y0); + Eigen::Vector2d gv = eval_poly_gradient_at_centre(q, vc, x0, y0); + Eigen::Vector2d gw = eval_poly_gradient_at_centre(q, wc, x0, y0); + + F(0,0) = 1.0 + gu(0); + F(0,1) = gu(1); + F(1,0) = gv(0); + F(1,1) = 1.0 + gv(1); + F(2,0) = gw(0); + F(2,1) = gw(1); + F(2,2) = 1.0; + + return F; } - Eigen::Matrix2d compute_strain(const std::string& form, const Eigen::Matrix2d& deform_grad) { - if (form == "GREEN") return green(deform_grad); - else if (form == "ALMANSI") return almansi(deform_grad); - else if (form == "HENCKY") return hencky(deform_grad); - else if (form == "BIOT_EULER") return biot_euler(deform_grad); - else if (form == "BIOT_LAGRANGE") return biot_lagrange(deform_grad); + Eigen::Matrix3d compute_surface_F_3d(const int q, + const Eigen::VectorXd &uc, + const Eigen::VectorXd &vc, + const Eigen::VectorXd &wc, + const Eigen::Matrix3d &tangent_basis) { - std::cerr << "Unknown Strain formulation: '" << form << "'." << std::endl; - return Eigen::Matrix2d::Zero(); - } + Eigen::Matrix3d F = Eigen::Matrix3d::Zero(); + F.block<1,2>(0,0) = eval_poly_gradient_at_centre(q, uc, 0.0, 0.0).transpose(); + F.block<1,2>(1,0) = eval_poly_gradient_at_centre(q, vc, 0.0, 0.0).transpose(); + F.block<1,2>(2,0) = eval_poly_gradient_at_centre(q, wc, 0.0, 0.0).transpose(); - inline Eigen::Matrix2d green(Eigen::Matrix2d F){ - return 0.5 * (F.transpose() * F - I); + return I + F * tangent_basis.transpose(); } + Eigen::Matrix3d compute_strain(const std::string& form, const Eigen::Matrix3d& F) { + if (form == "GREEN") return green(F); + else if (form == "ALMANSI") return almansi(F); + else if (form == "HENCKY") return hencky(F); + else if (form == "BIOT_EULER") return biot_euler(F); + else if (form == "BIOT_LAGRANGE") return biot_lagrange(F); + + std::cerr << "Unknown Strain formulation: '" << form << "'." << std::endl; + return Eigen::Matrix3d::Zero(); + } + inline Eigen::Matrix3d green(const Eigen::Matrix3d &F){ + return 0.5 * (F.transpose() * F - I); + } - inline Eigen::Matrix2d hencky(Eigen::Matrix2d F){ - Eigen::Matrix2d C = F.transpose() * F; + inline Eigen::Matrix3d hencky(const Eigen::Matrix3d &F){ + Eigen::Matrix3d C = F.transpose() * F; - Eigen::SelfAdjointEigenSolver solver(C); + Eigen::SelfAdjointEigenSolver solver(C); if (solver.info() != Eigen::Success) throw std::runtime_error("Eigen decomposition failed."); // Get eigenvectors and sqrt-eigenvalues - const Eigen::Matrix2d Q = solver.eigenvectors(); - const Eigen::Vector2d eigvals = solver.eigenvalues(); + const Eigen::Matrix3d Q = solver.eigenvectors(); + const Eigen::Vector3d eigvals = solver.eigenvalues(); return Q * (0.5 * eigvals.array().log().matrix().asDiagonal()) * Q.transpose(); } @@ -233,9 +407,9 @@ namespace strain { - inline Eigen::Matrix2d almansi(Eigen::Matrix2d F){ - Eigen::Matrix2d B = F * F.transpose(); - Eigen::Matrix2d B_inv = B.inverse(); + inline Eigen::Matrix3d almansi(const Eigen::Matrix3d &F){ + Eigen::Matrix3d B = F * F.transpose(); + Eigen::Matrix3d B_inv = B.inverse(); return 0.5 * (I - B_inv); } @@ -243,17 +417,17 @@ namespace strain { - inline Eigen::Matrix2d biot_euler(Eigen::Matrix2d F){ + inline Eigen::Matrix3d biot_euler(const Eigen::Matrix3d &F){ - Eigen::Matrix2d C = F * F.transpose(); + Eigen::Matrix3d C = F * F.transpose(); - Eigen::SelfAdjointEigenSolver solver(C); + Eigen::SelfAdjointEigenSolver solver(C); if (solver.info() != Eigen::Success) throw std::runtime_error("Eigen decomposition failed."); // U = sqrt(C) = Q * sqrt(D) * Q^T - Eigen::Matrix2d D_sqrt = solver.eigenvalues().cwiseSqrt().asDiagonal(); - Eigen::Matrix2d U = solver.eigenvectors() * D_sqrt * solver.eigenvectors().transpose(); + Eigen::Matrix3d D_sqrt = solver.eigenvalues().cwiseSqrt().asDiagonal(); + Eigen::Matrix3d U = solver.eigenvectors() * D_sqrt * solver.eigenvectors().transpose(); return U - I; @@ -262,129 +436,143 @@ namespace strain { - inline Eigen::Matrix2d biot_lagrange(Eigen::Matrix2d F){ + inline Eigen::Matrix3d biot_lagrange(const Eigen::Matrix3d &F){ - Eigen::Matrix2d C = F.transpose() * F; + Eigen::Matrix3d C = F.transpose() * F; - Eigen::SelfAdjointEigenSolver solver(C); + Eigen::SelfAdjointEigenSolver solver(C); if (solver.info() != Eigen::Success) throw std::runtime_error("Eigen decomposition failed."); // U = sqrt(C) = Q * sqrt(D) * Q^T - Eigen::Matrix2d D_sqrt = solver.eigenvalues().cwiseSqrt().asDiagonal(); - Eigen::Matrix2d U = solver.eigenvectors() * D_sqrt * solver.eigenvectors().transpose(); + Eigen::Matrix3d D_sqrt = solver.eigenvalues().cwiseSqrt().asDiagonal(); + Eigen::Matrix3d U = solver.eigenvectors() * D_sqrt * solver.eigenvectors().transpose(); return U - I; } - void append_results(int sw, strain::Results &results, - const bool save_at_end, const int x0, const int y0, - const Eigen::Matrix2d &deform_grad, - const Eigen::Matrix2d &eps, - const int nwindows, const int img){ - - // index in results arrays depending on whether saving at end or on a per image basis - int idx; - if (save_at_end) idx = nwindows * img + sw; - else idx = sw; - - results.def_grad[4*idx+0] = deform_grad(0,0); - results.def_grad[4*idx+1] = deform_grad(0,1); - results.def_grad[4*idx+2] = deform_grad(1,0); - results.def_grad[4*idx+3] = deform_grad(1,1); - results.strain[4*idx+0] = eps(0,0); - results.strain[4*idx+1] = eps(0,1); - results.strain[4*idx+2] = eps(1,0); - results.strain[4*idx+3] = eps(1,1); - } + void append_results(int sw, strain::Results &results, + const int x0, const int y0, + const Eigen::Matrix3d &F, + const Eigen::Matrix3d &eps, + const int nwindows){ - void save_to_disk(int img_num, const strain::Results &results, - const common_util::SaveConfig &strain_save_conf, - const int nwindows, const int nimg, - const std::vector filenames){ + results.F[6*sw+0] = F(0,0); + results.F[6*sw+1] = F(0,1); + results.F[6*sw+2] = F(1,0); + results.F[6*sw+3] = F(1,1); + results.F[6*sw+4] = F(2,0); + results.F[6*sw+5] = F(2,1); + results.strain[4*sw+0] = eps(0,0); + results.strain[4*sw+1] = eps(0,1); + results.strain[4*sw+2] = eps(1,0); + results.strain[4*sw+3] = eps(1,1); + } + + void save_to_disk(int img_num, + const strain::Results &results, + const common_util::SaveConfig &strain_save_conf, + const int nwindows, + const int nimg, + const std::vector filenames) + { const std::string delimiter = strain_save_conf.delimiter; - // open the file std::stringstream outfile_str; std::ofstream outfile; - - // file extension std::string file_ext; - if (strain_save_conf.binary) file_ext=".dic2d"; - else file_ext=".csv"; + if (strain_save_conf.binary) file_ext = ".dic3d"; + else file_ext = ".csv"; - // Extract the base filename without extension std::string full_filename = filenames[img_num]; size_t dot_pos = full_filename.find("."); if (dot_pos != std::string::npos) { full_filename = full_filename.substr(0, dot_pos); } - // output filename - outfile_str << strain_save_conf.basepath << "/" << - strain_save_conf.prefix << full_filename << file_ext; + outfile_str << strain_save_conf.basepath << "/" + << strain_save_conf.prefix + << full_filename + << file_ext; + + + outfile << std::fixed << std::setprecision(8); - // set the img var to 0 after opening file if not saving at end - if (!strain_save_conf.at_end) img_num = 0; + const int def_size = 6; + const int tensor_size = 4; - // save in binary format - if (strain_save_conf.binary){ + if (strain_save_conf.binary) + { outfile.open(outfile_str.str(), std::ios::binary); - for (int i = 0; i < nwindows; ++i) { - int idx = img_num * nwindows + i; - common_util::write_int(outfile, results.x[idx]); - common_util::write_int(outfile, results.y[idx]); - common_util::write_dbl(outfile, results.def_grad[4*idx+0]); - common_util::write_dbl(outfile, results.def_grad[4*idx+1]); - common_util::write_dbl(outfile, results.def_grad[4*idx+2]); - common_util::write_dbl(outfile, results.def_grad[4*idx+3]); - common_util::write_dbl(outfile, results.strain[4*idx+0]); - common_util::write_dbl(outfile, results.strain[4*idx+1]); - common_util::write_dbl(outfile, results.strain[4*idx+2]); - common_util::write_dbl(outfile, results.strain[4*idx+3]); + for (int i = 0; i < nwindows; ++i) + { + common_util::write_int(outfile, results.x[i]); + common_util::write_int(outfile, results.y[i]); + common_util::write_dbl(outfile, results.x_mm[i]); + common_util::write_dbl(outfile, results.y_mm[i]); + common_util::write_dbl(outfile, results.z_mm[i]); + + for (int k = 0; k < def_size; ++k) + common_util::write_dbl(outfile, results.F[def_size * i + k]); + + for (int k = 0; k < tensor_size; ++k) + common_util::write_dbl(outfile, results.strain[tensor_size * i + k]); } outfile.close(); } - else { + else + { outfile.open(outfile_str.str()); - // column headers - outfile << "window_x" << delimiter; - outfile << "window_y" << delimiter; - outfile << "def_grad_00" << delimiter; - outfile << "def_grad_01" << delimiter; - outfile << "def_grad_10" << delimiter; - outfile << "def_grad_11" << delimiter; - outfile << "eps_00" << delimiter; - outfile << "eps_01" << delimiter; - outfile << "eps_10" << delimiter; - outfile << "eps_11\n"; - - for (int i = 0; i < nwindows; i++) { - int idx = img_num * nwindows + i; - if (results.valid_window[idx]) { - outfile << results.x[idx] << delimiter; - outfile << results.y[idx] << delimiter; - outfile << results.def_grad[4*idx+0] << delimiter; - outfile << results.def_grad[4*idx+1] << delimiter; - outfile << results.def_grad[4*idx+2] << delimiter; - outfile << results.def_grad[4*idx+3] << delimiter; - outfile << results.strain[4*idx+0] << delimiter; - outfile << results.strain[4*idx+1] << delimiter; - outfile << results.strain[4*idx+2] << delimiter; - outfile << results.strain[4*idx+3] << "\n"; + outfile << "\"window_x\"" << delimiter + << "\"window_y\"" << delimiter + << "\"x_mm\"" << delimiter + << "\"y_mm\"" << delimiter + << "\"z_mm\"" << delimiter + << "\"def_grad_00\"" << delimiter + << "\"def_grad_01\"" << delimiter + << "\"def_grad_10\"" << delimiter + << "\"def_grad_11\"" << delimiter + << "\"def_grad_20\"" << delimiter + << "\"def_grad_21\"" << delimiter + << "\"eps_00\"" << delimiter + << "\"eps_01\"" << delimiter + << "\"eps_10\"" << delimiter + << "\"eps_11\"\n"; + + for (int i = 0; i < nwindows; i++) + { + if (results.valid_window[i]) + { + outfile << results.x[i] << delimiter; + outfile << results.y[i] << delimiter; + outfile << results.x_mm[i] << delimiter; + outfile << results.y_mm[i] << delimiter; + outfile << results.z_mm[i] << delimiter; + + for (int k = 0; k < def_size; ++k) + { + outfile << results.F[def_size * i + k] << delimiter; + } + + for (int k = 0; k < tensor_size; ++k) + { + outfile << results.strain[tensor_size * i + k]; + if (k != tensor_size - 1) outfile << delimiter; + } + + outfile << "\n"; } } + outfile.close(); } } - - } // namespace strain diff --git a/src/pyvale/strain/cpp/strain.hpp b/src/pyvale/strain/cpp/strain.hpp index ddee8ae9f..f00b0bba4 100644 --- a/src/pyvale/strain/cpp/strain.hpp +++ b/src/pyvale/strain/cpp/strain.hpp @@ -33,16 +33,24 @@ namespace strain { * */ struct Window { - std::vector x; - std::vector y; + std::vector x; + std::vector y; + std::vector x_mm; + std::vector y_mm; + std::vector z_mm; std::vector u; std::vector v; + std::vector w; - Window(int sw_size) + Window(int sw_size) : x(sw_size * sw_size, 0.0), y(sw_size * sw_size, 0.0), + x_mm(sw_size * sw_size, 0.0), + y_mm(sw_size * sw_size, 0.0), + z_mm(sw_size * sw_size, 0.0), u(sw_size * sw_size, 0.0), - v(sw_size * sw_size, 0.0) + v(sw_size * sw_size, 0.0), + w(sw_size * sw_size, 0.0) {} }; @@ -50,14 +58,20 @@ namespace strain { struct Results { std::vector x; std::vector y; - std::vector def_grad; + std::vector x_mm; + std::vector y_mm; + std::vector z_mm; + std::vector F; std::vector strain; std::vector valid_window; Results(int nwindows) : x(nwindows, 0), y(nwindows, 0), - def_grad(nwindows*4, std::nan("")), + x_mm(nwindows, std::nan("")), + y_mm(nwindows, std::nan("")), + z_mm(nwindows, std::nan("")), + F(nwindows*6, std::nan("")), strain(nwindows*4, std::nan("")), valid_window(nwindows, false) {} @@ -65,15 +79,50 @@ namespace strain { + void engine_2d(const py::array_t &ss_x_arr, + const py::array_t &ss_y_arr, + const py::array_t &x_mm_arr, + const py::array_t &y_mm_arr, + const py::array_t &z_mm_arr, + const py::array_t &u_arr, + const py::array_t &v_arr, + const py::array_t &w_arr, + const int nss_x, const int nss_y, + const int nimg, const int sw_size, + const int q, const std::string &form, + const std::vector &filenames, + const common_util::SaveConfig &strain_save_conf, + const int debug_level); + + void engine_3d(const py::array_t &ss_x_arr, + const py::array_t &ss_y_arr, + const py::array_t &x_mm_arr, + const py::array_t &y_mm_arr, + const py::array_t &z_mm_arr, + const py::array_t &u_arr, + const py::array_t &v_arr, + const py::array_t &w_arr, + const int nss_x, const int nss_y, + const int nimg, const int sw_size, + const int q, const std::string &form, + const std::vector &filenames, + const common_util::SaveConfig &strain_save_conf, + const int debug_level); + void engine(const py::array_t &ss_x_arr, const py::array_t &ss_y_arr, + const py::array_t &x_mm_arr, + const py::array_t &y_mm_arr, + const py::array_t &z_mm_arr, const py::array_t &u_arr, const py::array_t &v_arr, - const int nss_x, const int nss_y, - const int nimg, const int sw_size, + const py::array_t &w_arr, + const int nss_x, const int nss_y, + const int nimg, const int sw_size, const int q, const std::string &form, const std::vector &filenames, - const common_util::SaveConfig &strain_save_conf); + const common_util::SaveConfig &strain_save_conf, + const int debug_level); /** * @brief Fills the strain window with the subset coordinates @@ -83,6 +132,7 @@ namespace strain { * @param ss_y subset y-coordinates * @param u horizontal displacement * @param v vertical displacement + * @param w out of plane displacement * @param window strain window struct. * @param num_ss_x number of subsets along the x-axis * @param num_ss_y number of subsets along the y-axis @@ -92,28 +142,42 @@ namespace strain { * @return true if the strain window is filled successfully * @return false if the strain window is out of bounds */ - bool fill_window(int *ss_x, int *ss_y, double *u, double *v, - int img, int sw, Window &window, - int nss_x, int nss_y, int sw_size); + bool fill_window_2d(int *ss_x, int *ss_y, double *u, double *v, double *w, + int img, int sw, Window &window, + int nss_x, int nss_y, int sw_size); + + bool fill_window_3d(int *ss_x, int *ss_y, double *x_mm, double *y_mm, double *z_mm, + double *u, double *v, double *w, + int img, int sw, Window &window, + int nss_x, int nss_y, int sw_size); + + Eigen::Matrix3d compute_F_2d(const int q, + const Eigen::VectorXd &uc, + const Eigen::VectorXd &vc, + const Eigen::VectorXd &wc, + const double x0, + const double y0); - Eigen::Matrix2d compute_def_grad(const int q, const Eigen::VectorXd &uc, - const Eigen::VectorXd& vc, const double x0, - const double y0); + Eigen::Matrix3d compute_surface_F_3d(const int q, + const Eigen::VectorXd &uc, + const Eigen::VectorXd &vc, + const Eigen::VectorXd &wc, + const Eigen::Matrix3d &tangent_basis); - Eigen::Matrix2d compute_strain(const std::string& form, - const Eigen::Matrix2d& deform_grad); + Eigen::Matrix3d compute_strain(const std::string& form, + const Eigen::Matrix3d& deform_grad); /** */ void append_results(int sw, strain::Results &results, - const bool save_at_end, const int x0, const int y0, - const Eigen::Matrix2d &deform_grad, - const Eigen::Matrix2d &eps, - const int nwindows, const int img); + const int x0, const int y0, + const Eigen::Matrix3d &deform_grad, + const Eigen::Matrix3d &eps, + const int nwindows); /** @@ -131,45 +195,45 @@ namespace strain { * * @param F deformation gradient * @param I Identity Matrix - * @return Eigen::Matrix2d Green Strain + * @return Eigen::Matrix3d Green Strain */ - inline Eigen::Matrix2d green(Eigen::Matrix2d F); + inline Eigen::Matrix3d green(const Eigen::Matrix3d &F); /** * @brief Calculates Hencky strain for a given deformation gradient F and identity matrix I. * * @param F deformation gradient * @param I Identity Matrix - * @return Eigen::Matrix2d Hencky Strain + * @return Eigen::Matrix3d Hencky Strain */ - inline Eigen::Matrix2d hencky(Eigen::Matrix2d F); + inline Eigen::Matrix3d hencky(const Eigen::Matrix3d &F); /** * @brief Calculates Almansi strain for a given deformation gradient F and identity matrix I. * * @param F deformation gradient * @param I Identity Matrix - * @return Eigen::Matrix2d Almansi Strain + * @return Eigen::Matrix3d Almansi Strain */ - inline Eigen::Matrix2d almansi(Eigen::Matrix2d F); + inline Eigen::Matrix3d almansi(const Eigen::Matrix3d &F); /** * @brief Calculates Biot strain in the euler coordiate system for a given deformation gradient F and identity matrix I. * * @param F deformation gradient * @param I Identity Matrix - * @return Eigen::Matrix2d Almansi Strain + * @return Eigen::Matrix3d Almansi Strain */ - inline Eigen::Matrix2d biot_euler(Eigen::Matrix2d F); + inline Eigen::Matrix3d biot_euler(const Eigen::Matrix3d &F); /** * @brief Calculates Biot strain in the lagrange coordiate system for a given deformation gradient F and identity matrix I. * * @param F deformation gradient * @param I Identity Matrix - * @return Eigen::Matrix2d Almansi Strain + * @return Eigen::Matrix3d Almansi Strain */ - inline Eigen::Matrix2d biot_lagrange(Eigen::Matrix2d F); + inline Eigen::Matrix3d biot_lagrange(const Eigen::Matrix3d &F); } diff --git a/src/pyvale/strain/strain2d.py b/src/pyvale/strain/strain2d.py new file mode 100644 index 000000000..8101da2bd --- /dev/null +++ b/src/pyvale/strain/strain2d.py @@ -0,0 +1,171 @@ +# ================================================================================ +# pyvale: the python validation engine +# License: MIT +# Copyright (C) 2025 The Computer Aided Validation Team +# ================================================================================ + +from pathlib import Path +from typing import Literal +import numpy as np + +# pyvale +from pyvale.strain.strainresults import StrainResults +from pyvale.strain.strainchecks import check_strain_files +from pyvale.dic.dicimport2d import import_2d +from pyvale.dic.dicresults import Results as dicResults +from pyvale.common_py.util import check_output_directory +import pyvale.strain.strain_cpp as strain_cpp +import pyvale.common_cpp.common_cpp as common_cpp +import pyvale.common_py.util as common_py_util + +def calculate_2d(data: dicResults | str | Path | list[Path], + window_size: int=5, + window_element: int=9, + input_binary: bool=False, + input_delimiter: str=",", + output_basepath: Path | str="./", + output_binary: bool=False, + output_prefix: str="strain_", + output_delimiter: str=",", + num_threads: int | None = None, + strain_formulation: Literal["GREEN", "ALMANSI", "HENCKY", "BIOT_EULER", "BIOT_LAGRANGE"] = "HENCKY", + debug_level: int=1): + """ + Compute strain fields from DIC displacement data using a finite element smoothing approach. + + This function validates the input data and parameters, optionally loads DIC results from file, + and passes the data to a C++-accelerated backend for strain computation. + + Parameters + ---------- + data : dic.Results, pathlib.Path, list[pathlib.Path] str + input data can either be a dic.Results object or pathlib.Path / str if importing data + straight from a file + input_delimiter: str + delimiter used for the input dic results files if using + pathlib.Path or str for data import (default: ","). + input_binary bool: + whether input data is in human-readable or binary format if using + pathlib.Path or str for data import (default: False). + window_size : int, optional + The size of the local window over which to compute strain (must be odd, + default: 5). + window_element : int, optional + The type of finite element shape function used in the strain window: 4 (bilinear) or 9 (biquadratic), + by default 4. + strain_formulation : str, optional + The strain definition to use: one of 'GREEN', 'ALMANSI', 'HENCKY', 'BIOT_EULER', 'BIOT_LAGRANGE'. + Defaults to 'HENCKY'. + output_basepath : str or pathlib.Path, optional + Directory path where output files will be written (default: "./"). + output_binary : bool, optional + Whether to write output in binary format (default: False). + output_prefix : str, optional + Prefix for all output files (default: :code:`strain_`). results will be + named with output_prefix + original filename. THe extension will be + changed to ".csv" or ".dic2d" depending on whether outputting as a binary. + output_delimiter : str, optional + Delimiter used in text output files (default: ","). + + Raises + ------ + ValueError + If any of the input parameters are invalid (e.g., unsupported strain formulation, + even window size, or invalid element type). + """ + + allowed_formulations = ["GREEN", "ALMANSI", "HENCKY", "BIOT_EULER", "BIOT_LAGRANGE"] + if strain_formulation not in allowed_formulations: + raise ValueError(f"Invalid strain formulation: '{strain_formulation}'. " + f"Allowed values are: {', '.join(allowed_formulations)}.") + + allowed_elements = [4, 9] + if window_element not in allowed_elements: + raise ValueError(f"Invalid strain window element type: Q{window_element}. " + f"Allowed values are: {', '.join(map(str, allowed_elements))}.") + + if window_size % 2 == 0: + raise ValueError(f"Invalid strain window size: '{window_size}'. Must be an odd number.") + + + if isinstance(data, (str, Path, list)): + filenames = check_strain_files(strain_files=data) + + # Load data if a file path is given + dicresults = import_2d(layout="matrix", data=data, + binary=input_binary, delimiter=input_delimiter, debug_level=debug_level) + + elif isinstance(data, dicResults): + dicresults = data + assert dicresults.ss_x.ndim == 2 and dicresults.ss_y.ndim == 2, "ss_x and ss_y must be 2D" + assert dicresults.ss_x.shape == dicresults.ss_y.shape, "ss_x and ss_y must have the same shape" + assert dicresults.u_px.ndim == 3 and dicresults.v_px.ndim == 3, "u_px and v_px must be 3D" + assert dicresults.u_px.shape == dicresults.v_px.shape, "u_px and v_px must have the same shape" + assert dicresults.u_px.shape[1:] == dicresults.ss_x.shape, "Spatial dimensions of u_px must match ss_x" + + # need to make dummy filenames + filenames = [] + for f in range(0,dicresults.u_px.shape[0]): + filenames.append(f"strain_data_{f:04d}") + + else: + raise TypeError(f"Unexpected displacement data type: {type(data)}") + + # Extract dimensions from the validated object + nss_x = dicresults.ss_x.shape[1] + nss_y = dicresults.ss_x.shape[0] + nimg = dicresults.u_px.shape[0] + + + check_output_directory(str(output_basepath), output_prefix, 0) + + # assigning c++ struct vals for save config + strain_save_conf = common_cpp.SaveConfig() + strain_save_conf.basepath = str(output_basepath) + strain_save_conf.binary = output_binary + strain_save_conf.prefix = output_prefix + strain_save_conf.delimiter = output_delimiter + + # make empty arrays for optional 3D fields + w_dummy = np.zeros_like(dicresults.u_px) + x_mm = np.full_like(dicresults.u_px, np.nan, dtype=np.float64) + y_mm = np.full_like(dicresults.u_px, np.nan, dtype=np.float64) + z_mm = np.full_like(dicresults.u_px, np.nan, dtype=np.float64) + + if dicresults.stereo is not None: + x_mm = dicresults.stereo.x_mm + y_mm = dicresults.stereo.y_mm + z_mm = dicresults.stereo.z_mm + + #set the number of OMP threads + if num_threads is not None: + common_cpp.set_num_threads(num_threads) + else: + num_threads = common_cpp.get_num_threads() + + # print the config + if debug_level>0: + common_py_util.print_title("Starting Strain Calculation") + common_py_util.info_out("Number of images: ", nimg) + common_py_util.info_out("Number of spatial points in x: ", nss_x) + common_py_util.info_out("Number of spatial points in y: ", nss_y) + common_py_util.info_out("Window size: ", window_size) + common_py_util.info_out("Window element type: ", window_element) + common_py_util.info_out("Strain formulation: ", strain_formulation) + common_py_util.info_out("Number of Threads: ", num_threads) + + # Call to C++ backend + with strain_cpp.ostream_redirect(stdout=True, stderr=True): + strain_cpp.strain_engine_2d(dicresults.ss_x, dicresults.ss_y, + x_mm, y_mm, z_mm, + dicresults.u_px, dicresults.v_px, w_dummy, + nss_x, nss_y, nimg, + window_size, window_element, + strain_formulation, filenames, + strain_save_conf, + debug_level) + + + + + diff --git a/src/pyvale/strain/strain.py b/src/pyvale/strain/strain3d.py similarity index 65% rename from src/pyvale/strain/strain.py rename to src/pyvale/strain/strain3d.py index 0216476ce..1330d5851 100644 --- a/src/pyvale/strain/strain.py +++ b/src/pyvale/strain/strain3d.py @@ -5,17 +5,19 @@ # ================================================================================ from pathlib import Path +from typing import Literal # pyvale from pyvale.strain.strainresults import StrainResults from pyvale.strain.strainchecks import check_strain_files -from pyvale.dic.dicdataimport import import_2d +from pyvale.dic.dicimport3d import import_3d from pyvale.dic.dicresults import Results as dicResults from pyvale.common_py.util import check_output_directory import pyvale.strain.strain_cpp as strain_cpp import pyvale.common_cpp.common_cpp as common_cpp +import pyvale.common_py.util as common_py_util -def calculate_2d(data: dicResults | str | Path, +def calculate_3d(data: dicResults | str | Path | list[Path], window_size: int=5, window_element: int=9, input_binary: bool=False, @@ -24,8 +26,9 @@ def calculate_2d(data: dicResults | str | Path, output_binary: bool=False, output_prefix: str="strain_", output_delimiter: str=",", - output_at_end: bool=False, - strain_formulation: str="HENCKY"): + num_threads: int | None = None, + strain_formulation: Literal["GREEN", "ALMANSI", "HENCKY", "BIOT_EULER", "BIOT_LAGRANGE"] = "HENCKY", + debug_level: int=1): """ Compute strain fields from DIC displacement data using a finite element smoothing approach. @@ -34,7 +37,7 @@ def calculate_2d(data: dicResults | str | Path, Parameters ---------- - data : dic.Results, pathlib.Path or str + data : dic.Results, pathlib.Path, list[pathlib.Path] or str input data can either be a dic.Results object or pathlib.Path / str if importing data straight from a file input_delimiter: str @@ -84,25 +87,24 @@ def calculate_2d(data: dicResults | str | Path, raise ValueError(f"Invalid strain window size: '{window_size}'. Must be an odd number.") - if isinstance(data, (str, Path)): + if isinstance(data, (str, Path, list)): filenames = check_strain_files(strain_files=data) # Load data if a file path is given - dicresults = import_2d(layout="matrix", data=str(data), - binary=input_binary, delimiter=input_delimiter) + dicresults = import_3d(layout="matrix", data=data, + binary=input_binary, delimiter=input_delimiter, debug_level=debug_level) elif isinstance(data, dicResults): dicresults = data - print(dicresults.ss_x.shape, dicresults.ss_y.shape, dicresults.u.shape,dicresults.v.shape) assert dicresults.ss_x.ndim == 2 and dicresults.ss_y.ndim == 2, "ss_x and ss_y must be 2D" assert dicresults.ss_x.shape == dicresults.ss_y.shape, "ss_x and ss_y must have the same shape" - assert dicresults.u.ndim == 3 and dicresults.v.ndim == 3, "u and v must be 3D" - assert dicresults.u.shape == dicresults.v.shape, "u and v must have the same shape" - assert dicresults.u.shape[1:] == dicresults.ss_x.shape, "Spatial dimensions of u must match ss_x" + assert dicresults.u_px.ndim == 3 and dicresults.v_px.ndim == 3, "u and v must be 3D" + assert dicresults.u_px.shape == dicresults.v_px.shape, "u and v must have the same shape" + assert dicresults.u_px.shape[1:] == dicresults.ss_x.shape, "Spatial dimensions of u must match ss_x" # need to make dummy filenames filenames = [] - for f in range(0,dicresults.u.shape[0]): + for f in range(0,dicresults.u_px.shape[0]): filenames.append(f"strain_data_{f:04d}") else: @@ -111,7 +113,7 @@ def calculate_2d(data: dicResults | str | Path, # Extract dimensions from the validated object nss_x = dicresults.ss_x.shape[1] nss_y = dicresults.ss_x.shape[0] - nimg = dicresults.u.shape[0] + nimg = dicresults.u_px.shape[0] check_output_directory(str(output_basepath), output_prefix, 0) @@ -122,17 +124,41 @@ def calculate_2d(data: dicResults | str | Path, strain_save_conf.binary = output_binary strain_save_conf.prefix = output_prefix strain_save_conf.delimiter = output_delimiter - strain_save_conf.at_end = output_at_end - print(type(filenames)) + if dicresults.stereo is None: + raise ValueError("3D strain calculation requires DIC Results with stereo data.") + + x_mm = dicresults.stereo.x_mm + y_mm = dicresults.stereo.y_mm + z_mm = dicresults.stereo.z_mm + + #set the number of OMP threads + if num_threads is not None: + common_cpp.set_num_threads(num_threads) + else: + num_threads = common_cpp.get_num_threads() + + # print the config + if debug_level>0: + common_py_util.print_title("Starting Strain Calculation") + common_py_util.info_out("Number of images: ", nimg) + common_py_util.info_out("Number of spatial points in x: ", nss_x) + common_py_util.info_out("Number of spatial points in y: ", nss_y) + common_py_util.info_out("Window size: ", window_size) + common_py_util.info_out("Window element type: ", window_element) + common_py_util.info_out("Strain formulation: ", strain_formulation) + common_py_util.info_out("Number of Threads: ", num_threads) # Call to C++ backend - strain_cpp.strain_engine(dicresults.ss_x, dicresults.ss_y, - dicresults.u, dicresults.v, - nss_x, nss_y, nimg, - window_size, window_element, - strain_formulation, filenames, - strain_save_conf) + with strain_cpp.ostream_redirect(stdout=True, stderr=True): + strain_cpp.strain_engine_3d(dicresults.ss_x, dicresults.ss_y, + x_mm, y_mm, z_mm, + dicresults.stereo.u_mm, dicresults.stereo.v_mm, dicresults.stereo.w_mm, + nss_x, nss_y, nimg, + window_size, window_element, + strain_formulation, filenames, + strain_save_conf, + debug_level) diff --git a/src/pyvale/strain/strainchecks.py b/src/pyvale/strain/strainchecks.py index b0a74288d..666d2b60b 100644 --- a/src/pyvale/strain/strainchecks.py +++ b/src/pyvale/strain/strainchecks.py @@ -9,39 +9,42 @@ from pathlib import Path -def check_strain_files(strain_files: str | Path) -> list[str]: +def check_strain_files(strain_files: str | Path | list[Path] | list[str]) -> list[str]: """ - Check for strain/deformation files in the given path and return their filenames. + Check for strain/deformation files in the given path(s) and return their filenames. Parameters ---------- - strain_files : str or pathlib.Path - Path or glob pattern pointing to the strain/deformation files. + strain_files : str, pathlib.Path, list[str], or list[Path] + Path(s) or glob pattern(s) pointing to the strain/deformation files. Returns ------- list[str] - A sorted list of filenames (not full paths) matching the input path/pattern. + A sorted list of filenames (not full paths) matching the input path(s)/pattern(s). Raises ------ FileNotFoundError - If no files matching the given path or pattern are found. + If no files matching the given path(s) or pattern(s) are found. Examples -------- >>> check_strain_files("data/strain_*.tif") ['strain_001.tif', 'strain_002.tif', 'strain_003.tif'] """ + if isinstance(strain_files, (str, Path)): + patterns = [strain_files] + else: + patterns = strain_files - filenames = [] + files = [] + for pattern in patterns: + files.extend(glob.glob(str(pattern))) + + files = sorted(files) - # Find deformation image files - files = sorted(glob.glob(str(strain_files))) if not files: raise FileNotFoundError(f"No DIC data found: {strain_files}") - for file in files: - filenames.append(os.path.basename(file)) - - return filenames + return [os.path.basename(file) for file in files] diff --git a/src/pyvale/strain/strainimport.py b/src/pyvale/strain/strainimport.py index 84a6fdb84..ebafbb51f 100644 --- a/src/pyvale/strain/strainimport.py +++ b/src/pyvale/strain/strainimport.py @@ -4,73 +4,61 @@ # Copyright (C) 2025 The Computer Aided Validation Team # ================================================================================ - -import numpy as np import glob from pathlib import Path +from typing import Literal -# pyvale -from pyvale.strain.strainresults import StrainResults +import numpy as np +from pyvale.strain.strainresults import StrainResults +from pyvale.dic.dicimport2d import check_delimiter, to_grid +def import_2d(data: str | Path | list[Path], + binary: bool = False, + layout: Literal["column", "matrix"] = "matrix", + delimiter: str = ",") -> StrainResults: + """Import 2D strain result data from text or binary files. -def import_2d(data: str | Path, - binary: bool = False, - layout: str = "matrix", - delimiter: str = ",") -> StrainResults: + The importer accepts the legacy 20-column strain format and the 3D-aware + 23-column format that appends ``x_mm``, ``y_mm`` and ``z_mm`` after the + strain-window pixel coordinates. """ - Import strain result data from human readable text or binary files. - - Parameters - ---------- - - data : str or pathlib.Path - Path pattern to the data files (can include wildcards). Default is "./". - - layout : str, optional - Format of the output data layout: "column" (flat array per frame) or "matrix" - (reshaped grid per frame). Default is "column". - - binary : bool, optional - If True, expects files in a specific binary format. If False, expects text data. - Default is False. - - delimiter : str, optional - Delimiter used in text data files. Ignored if binary=True. Default is a single space. - - Returns - ------- - StrainResults - A named container with the following fields: - - window_x, window_y (grid arrays if layout=="matrix"; otherwise, 1D integer arrays) - - def_grad, eps (deformation gradient and strain arrays with shape depending on layout) - - filenames (python list) - - Raises - ------ - ValueError: - If `layout` is not "column" or "matrix", or text data has insufficient columns, - or binary rows are malformed. - - FileNotFoundError: - If no matching data files are found. + + return _import(data, binary=binary, layout=layout, delimiter=delimiter, require_coords=False) + + +def import_3d(data: str | Path | list[Path], + binary: bool = False, + layout: Literal["column", "matrix"] = "matrix", + delimiter: str = ",") -> StrainResults: + """Import 3D-aware strain result data from text or binary files. + + Returned ``StrainResults`` include ``x_mm``, ``y_mm`` and ``z_mm`` arrays for + the strain-window centre in the cam0 coordinate system. """ + return _import(data, binary=binary, layout=layout, delimiter=delimiter, require_coords=True) +def _import(data: str | Path | list[Path], + binary: bool, + layout: Literal["column", "matrix"], + delimiter: str, + require_coords: bool) -> StrainResults: + if layout not in {"column", "matrix"}: + raise ValueError("layout must be 'column' or 'matrix'.") - print("") print("Attempting Strain Data import...") - print("") - - # convert to str + if isinstance(data, Path): data = str(data) + if isinstance(data, list): + files = list(map(str, data)) + else: + files = sorted(glob.glob(data)) - files = sorted(glob.glob(data)) - filenames = files if not files: raise FileNotFoundError(f"No results found in: {data}") @@ -79,225 +67,169 @@ def import_2d(data: str | Path, print(f" - {file}") print("") + def read_file(file: str): + if binary: + return read_binary(file, delimiter=delimiter, require_coords=require_coords) + return read_text(file, delimiter=delimiter) - # Read first file to define reference coordinates - read_data = read_binary if binary else read_text - - window_x_ref, window_y_ref, *fields = read_data(files[0], delimiter=delimiter) - frames = [list(fields)] + first = read_file(files[0]) + window_x_ref, window_y_ref = first[:2] + frames = [first[2:]] for file in files[1:]: - window_x, window_y, *f = read_data(file, delimiter) - if not (np.array_equal(window_x_ref, window_x) and - np.array_equal(window_y_ref, window_y)): + current = read_file(file) + window_x, window_y = current[:2] + if not (np.array_equal(window_x_ref, window_x) and np.array_equal(window_y_ref, window_y)): raise ValueError("Mismatch in coordinates across frames.") - frames.append(f) + frames.append(current[2:]) - # Stack fields into arrays - arrays = [np.stack([frame[i] for frame in frames]) for i in range(8)] + arrays = [np.stack([frame[i] for frame in frames]) for i in range(len(frames[0]))] - # if reading into a matrix layout we need to convert to meshgrids - if layout == "matrix": - - # create meshgrid - x_unique = np.unique(window_x_ref) - y_unique = np.unique(window_y_ref) - X, Y = np.meshgrid(x_unique, y_unique) - - # convert results to a grid based on meshgrid dims - shape = (len(files), len(y_unique), len(x_unique)) - arrays = [to_grid(a,shape,window_x_ref, window_y_ref, x_unique,y_unique) for a in arrays] - - - # combine strain results into single np.ndarray. current dimeensions of - # each array are (file,x,y). The results will become - # (file,x,y,matrix_x,def_matrix_y) - current_shape = arrays[0].shape # (file,x,y) - def_xx = np.zeros(current_shape) - def_xy = np.zeros(current_shape) - def_yx = np.zeros(current_shape) - def_yy = np.zeros(current_shape) - eps_xx = np.zeros(current_shape) - eps_xy = np.zeros(current_shape) - eps_yx = np.zeros(current_shape) - eps_yy = np.zeros(current_shape) - - - def_xx[:,:,:] = arrays[0] - def_xy[:,:,:] = arrays[1] - def_yx[:,:,:] = arrays[2] - def_yy[:,:,:] = arrays[3] - eps_xx[:,:,:] = arrays[4] - eps_xy[:,:,:] = arrays[5] - eps_yx[:,:,:] = arrays[6] - eps_yy[:,:,:] = arrays[7] - - - return StrainResults(X, Y, - def_xx, def_xy, def_yx, def_yy, - eps_xx, eps_xy, eps_yx, eps_yy, - filenames) + has_coords = len(arrays) == 13 + if require_coords and not has_coords: + raise ValueError("3D strain data must include x_mm, y_mm and z_mm columns.") + if has_coords: + x_mm, y_mm, z_mm = arrays[:3] + tensor_arrays = arrays[3:] else: - current_shape = arrays[0].shape - def_xx = np.zeros(current_shape) - def_xy = np.zeros(current_shape) - def_yx = np.zeros(current_shape) - def_yy = np.zeros(current_shape) - eps_xx = np.zeros(current_shape) - eps_xy = np.zeros(current_shape) - eps_yx = np.zeros(current_shape) - eps_yy = np.zeros(current_shape) - def_xx[:,:,:] = arrays[0] - def_xy[:,:,:] = arrays[1] - def_yx[:,:,:] = arrays[2] - def_yy[:,:,:] = arrays[3] - eps_xx[:,:,:] = arrays[4] - eps_xy[:,:,:] = arrays[5] - eps_yx[:,:,:] = arrays[6] - eps_yy[:,:,:] = arrays[7] - return StrainResults(window_x_ref, window_y_ref, - def_xx, def_xy, def_yx, def_yy, - eps_xx, eps_xy, eps_yx, eps_yy, - filenames) - - - -def read_binary(file: str, delimiter: str): - """ - Read a binary Strain result file and extract DIC fields. - - Assumes a fixed binary structure with each row containing: - - 2x int32 (subset coordinates) - - 8x float64 (deformation matrix, strain matrix) - - Parameters - ---------- - file : str - Path to the binary result file. - - delimiter : str - Ignored for binary data (included for API consistency). - - Returns - ------- - tuple of np.ndarray - Arrays corresponding to: - (window_x, window_y, def_grad, eps) - - Raises - ------ - ValueError - If the binary file size does not align with expected row size. - """ - - row_size = (3 * 4 + 6 * 8) - with open(file, "rb") as f: - raw = f.read() - if len(raw) % row_size != 0: - raise ValueError("Binary file has incomplete rows.") + x_mm = y_mm = z_mm = None + tensor_arrays = arrays - rows = len(raw) // row_size - arr = np.frombuffer(raw, dtype=np.uint8).reshape(rows, row_size) - - # lil function to make extracting a bit easier - def extract(col, dtype, start): - return np.frombuffer(arr[:, start:start+col], dtype=dtype) + if len(tensor_arrays) != 10: + raise ValueError(f"Strain data must contain 10 deformation-gradient and strain tensor columns. Number of cols = {len(tensor_arrays)}") - window_x = extract(4, np.int32, 0) - window_y = extract(4, np.int32, 4) - def_grad00 = extract(8, np.float64, 8) - def_grad01 = extract(8, np.float64, 16) - def_grad10 = extract(8, np.float64, 24) - def_grad11 = extract(8, np.float64, 32) - eps00 = extract(8, np.float64, 40) - eps01 = extract(8, np.float64, 48) - eps10 = extract(8, np.float64, 56) - eps11 = extract(8, np.float64, 72) + if layout == "matrix": + x_unique = np.unique(window_x_ref) + y_unique = np.unique(window_y_ref) + window_x_out, window_y_out = np.meshgrid(x_unique, y_unique) + shape = (len(files), len(y_unique), len(x_unique)) - return window_x, window_y, def_grad00, def_grad01, def_grad10, def_grad11, eps00, eps01, eps10, eps11 + tensor_arrays = [ + to_grid(a, shape, window_x_ref, window_y_ref, x_unique, y_unique) + for a in tensor_arrays + ] + if has_coords: + x_mm = to_grid(x_mm, shape, window_x_ref, window_y_ref, x_unique, y_unique) + y_mm = to_grid(y_mm, shape, window_x_ref, window_y_ref, x_unique, y_unique) + z_mm = to_grid(z_mm, shape, window_x_ref, window_y_ref, x_unique, y_unique) + else: + window_x_out = window_x_ref + window_y_out = window_y_ref + + return StrainResults( + window_x=window_x_out, + window_y=window_y_out, + def_00=tensor_arrays[0], + def_01=tensor_arrays[1], + def_10=tensor_arrays[2], + def_11=tensor_arrays[3], + def_20=tensor_arrays[4], + def_21=tensor_arrays[5], + eps_xx=tensor_arrays[6], + eps_xy=tensor_arrays[7], + eps_yx=tensor_arrays[8], + eps_yy=tensor_arrays[9], + filenames=files, + x_mm=x_mm, + y_mm=y_mm, + z_mm=z_mm, + ) +def read_binary(file: str, delimiter: str, require_coords: bool | None = None): + """Read a binary strain result file. -def read_text(file: str, delimiter: str): + Supports legacy rows with ``window_x``, ``window_y`` and 18 doubles, plus + new 3D-aware rows with ``x_mm``, ``y_mm`` and ``z_mm`` before the 18 tensor + values. """ - Read a human-readable text DIC result file and extract DIC fields. - Expects at least 9 columns: - [ss_x, ss_y, u, v, m, cost, ftol, xtol, niter] + del delimiter - Parameters - ---------- - file : str - Path to the text result file. + row_size_2d = 2 * 4 + 18 * 8 + row_size_3d = 2 * 4 + 3 * 8 + 18 * 8 - delimiter : str - Delimiter used in the text file (e.g., space, tab, comma). - - Returns - ------- - tuple of np.ndarray - Arrays corresponding to: - (ss_x, ss_y, u, v, m, cost, ftol, xtol, niter) - - Raises - ------ - ValueError - If the text file has fewer than 9 columns. - """ - - data = np.loadtxt(file, delimiter=delimiter, skiprows=1) - if data.shape[1] < 9: - raise ValueError("Text data must have at least 9 columns.") - return ( - data[:, 0].astype(np.int32), # window_x - data[:, 1].astype(np.int32), # window_y - data[:, 2], data[:, 3], data[:, 4], data[:, 5], #def_grad - data[:, 6], data[:, 7], data[:, 8], data[:, 9] #eps - ) + with open(file, "rb") as f: + raw = f.read() + if require_coords is True: + if len(raw) % row_size_3d != 0: + raise ValueError( + f"Binary 3D strain file has incomplete rows: {file}. " + f"Expected row size {row_size_3d}, got {len(raw)} bytes." + ) + row_size = row_size_3d + has_coords = True + elif require_coords is False and Path(file).suffix != ".dic3d": + if len(raw) % row_size_2d != 0: + raise ValueError( + f"Binary 2D strain file has incomplete rows: {file}. " + f"Expected row size {row_size_2d}, got {len(raw)} bytes." + ) + row_size = row_size_2d + has_coords = False + elif len(raw) % row_size_3d == 0: + row_size = row_size_3d + has_coords = True + elif len(raw) % row_size_2d == 0: + row_size = row_size_2d + has_coords = False + else: + raise ValueError( + f"Binary file has incomplete rows: {file}. " + f"Expected row size {row_size_2d} or {row_size_3d}, got {len(raw)} bytes." + ) + rows = len(raw) // row_size + arr = np.frombuffer(raw, dtype=np.uint8).reshape(rows, row_size) + def extract(width, dtype, start): + return np.frombuffer(arr[:, start:start + width].copy(), dtype=dtype) + offset = 0 + window_x = extract(4, np.int32, offset); offset += 4 + window_y = extract(4, np.int32, offset); offset += 4 + coord_arrays = [] + if has_coords: + coord_arrays = [ + extract(8, np.float64, offset), + extract(8, np.float64, offset + 8), + extract(8, np.float64, offset + 16), + ] + offset += 24 -def to_grid(data, shape, ss_x_ref, ss_y_ref, x_unique, y_unique): - """ - Reshape a 2D DIC field from flat (column) format into grid (matrix) format. + tensor_arrays = [] + for _ in range(18): + tensor_arrays.append(extract(8, np.float64, offset)) + offset += 8 - This is used when output layout is specified as "matrix". - Maps values using reference subset coordinates (ss_x_ref, ss_y_ref). + return (window_x, window_y, *coord_arrays, *tensor_arrays) - Parameters - ---------- - data : np.ndarray - Array of shape (n_frames, n_points) to be reshaped into (n_frames, height, width). - shape : tuple - Target shape of output array: (n_frames, height, width). +def read_text(file: str, delimiter: str): + """Read a text strain result file. - ss_x_ref : np.ndarray - X coordinates of subset centers. + Expected formats are either 20 columns:: - ss_y_ref : np.ndarray - Y coordinates of subset centers. + window_x, window_y, def_grad_00..def_grad_22, eps_00..eps_22 - x_unique : np.ndarray - Sorted unique X coordinates in the grid. + or 23 columns with ``x_mm``, ``y_mm`` and ``z_mm`` inserted after + ``window_y``. + """ - y_unique : np.ndarray - Sorted unique Y coordinates in the grid. + check_delimiter(file, delimiter) + data = np.loadtxt(file, delimiter=delimiter, skiprows=1) + if data.ndim == 1: + data = data.reshape(1, -1) - Returns - ------- - np.ndarray - Reshaped array with shape `shape`, filled with NaNs where no data exists. - """ + if data.shape[1] not in {15, 18}: + raise ValueError(f"Text strain data must have exactly 15 or 18 columns. Number of cols = {data.shape[1]}") - grid = np.full(shape, np.nan) - for i, (x, y) in enumerate(zip(ss_x_ref, ss_y_ref)): - x_idx = np.where(x_unique == x)[0][0] - y_idx = np.where(y_unique == y)[0][0] - grid[:, y_idx, x_idx] = data[:, i] - return grid + return ( + data[:, 0].astype(np.int32), + data[:, 1].astype(np.int32), + *[data[:, i] for i in range(2, data.shape[1])], + ) diff --git a/src/pyvale/strain/strainresults.py b/src/pyvale/strain/strainresults.py index e9993b44c..58431221f 100644 --- a/src/pyvale/strain/strainresults.py +++ b/src/pyvale/strain/strainresults.py @@ -21,34 +21,49 @@ class StrainResults: The x-coordinates of the strain window centre. shape=(img_num,y,x) window_y : np.ndarray The y-coordinates of the strain window centre. shape=(img_num,y,x) - def_xx : np.ndarray - The xx component of the 2D deformation gradient. shape=(img_num,y,x) - def_xy : np.ndarray - The xy component of the 2D deformation gradient. shape=(img_num,y,x) - def_yx : np.ndarray - The yx component of the 2D deformation gradient. shape=(img_num,y,x) - def_yy : np.ndarray - The yy component of the 2D deformation gradient. shape=(img_num,y,x) + x_mm : np.ndarray | None + X-coordinate of the strain window centre in physical units relative to cam0. + y_mm : np.ndarray | None + Y-coordinate of the strain window centre in physical units relative to cam0. + z_mm : np.ndarray | None + Z-coordinate of the strain window centre in physical units relative to cam0. + def_00 : np.ndarray + The xx component (1 + ∂u/∂x) of the deformation gradient matrix. shape=(img_num, y, x) + def_01 : np.ndarray + The xy component (∂u/∂y) of the deformation gradient matrix. shape=(img_num, y, x) + def_10 : np.ndarray + The yx component (∂v/∂x) of the deformation gradient matrix. shape=(img_num, y, x) + def_11 : np.ndarray + The yy component (1 + ∂v/∂y) of the deformation gradient matrix. shape=(img_num, y, x) + def_20 : np.ndarray + The zx component (∂w/∂x) of the deformation gradient matrix. shape=(img_num, y, x) + def_21 : np.ndarray + The zy component (∂w/∂y) of the deformation gradient matrix. shape=(img_num, y, x) eps_xx : np.ndarray - The xx component of the 2D strain tensor. shape=(img_num,y,x) + The xx component of the surface strain tensor. shape=(img_num, y, x) eps_xy : np.ndarray - The xy component of the 2D strain tensor. shape=(img_num,y,x) + The xy component of the surface strain tensor. shape=(img_num, y, x) eps_yx : np.ndarray - The yx component of the 2D strain tensor. shape=(img_num,y,x) + The yx component of the surface strain tensor. shape=(img_num, y, x) eps_yy : np.ndarray - The yy component of the 2D strain tensor. shape=(img_num,y,x) + The yy component of the surface strain tensor. shape=(img_num, y, x) filenames : list[str] name of Strain result files that have been found """ window_x: np.ndarray window_y: np.ndarray - def_xx: np.ndarray - def_xy: np.ndarray - def_yx: np.ndarray - def_yy: np.ndarray + def_00: np.ndarray + def_01: np.ndarray + def_10: np.ndarray + def_11: np.ndarray + def_20: np.ndarray + def_21: np.ndarray eps_xx: np.ndarray eps_xy: np.ndarray eps_yx: np.ndarray eps_yy: np.ndarray filenames: list[str] + x_mm: np.ndarray | None = None + y_mm: np.ndarray | None = None + z_mm: np.ndarray | None = None diff --git a/src/pyvale/verif/pointsens.py b/src/pyvale/verif/pointsens.py index 7a9d61e3a..0abc629f4 100644 --- a/src/pyvale/verif/pointsens.py +++ b/src/pyvale/verif/pointsens.py @@ -15,6 +15,7 @@ """ import numpy as np +import platform import pyvale.mooseherder as mh import pyvale.sensorsim as sens import pyvale.dataio as io @@ -103,11 +104,25 @@ def check_gold_measurements(sens_dict: dict[str,sens.SensorsPoint], atol: float = 1e-5) -> list[str]: fails = [] + MACOS_GOLD_CASES = { + "scal2d_analytic_nomesh", + "scal3d_nomesh", + "vec2d_analytic_nomesh", + "tens2d_analytic_nomesh", + } + for ss in sens_dict: measurements = sens_dict[ss].sim_measurements() gold_path = pointsensconst.GOLD_PATH / f"{ss}.npy" - load_path = pointsensconst.GOLD_PATH / f"{ss.lower()}.npy" + print(ss.lower()) + + if (any(case in ss.lower() for case in MACOS_GOLD_CASES) and (platform.system()=='Darwin')): + load_path = (pointsensconst.GOLD_PATH / "macos" / f"{ss.lower()}.npy") + else: + load_path = (pointsensconst.GOLD_PATH / f"{ss.lower()}.npy") + + if load_path.is_file(): gold = np.load(load_path) diff --git a/tests/dic/calib.txt b/tests/dic/calib.txt new file mode 100644 index 000000000..bed44fd1e --- /dev/null +++ b/tests/dic/calib.txt @@ -0,0 +1,26 @@ +cam0_fx_px,14492.753623188406 +cam0_fy_px,14492.753623188406 +cam0_fs_px,0.00000000000 +cam0_cx_px,520.000000000000 +cam0_cy_px,770.000000000000 +cam0_k1,0.0000000000 +cam0_k2,0.0000000000 +cam0_p1,0.0000000000 +cam0_p2,0.0000000000 +cam0_k3,0.0000000000 +cam1_fx_px,14492.753623188406 +cam1_fy_px,14492.753623188406 +cam1_fs_px,0.0000000000 +cam1_cx_px,520.000000000000 +cam1_cy_px,770.000000000000 +cam1_k1,0.0000000000 +cam1_k2,0.0000000000 +cam1_p1,0.0000000000 +cam1_p2,0.0000000000 +cam1_k3,0.0000000000 +tx_mm,49.5681367139 +ty_mm,0.000000000000 +tz_mm,-8.7401998861 +theta_deg,0.000000000000 +phi_deg,20.000000000000 +psi_deg,0.000000000000 diff --git a/tests/dic/gen_results.py b/tests/dic/gen_results.py deleted file mode 100644 index 8199f3a1d..000000000 --- a/tests/dic/gen_results.py +++ /dev/null @@ -1,83 +0,0 @@ -""" -================================================================================ -Example: thermocouples on a 2d plate - -pyvale: the python validation engine -License: MIT -Copyright (C) 2024 The Computer Aided Validation Team -================================================================================ -""" -import os -os.environ['OMP_NUM_THREADS'] = '1' - - -import pyvale -ref_pattern = "../../src/pyvale/data/plate_rigid_ref0000.tiff" -def_pattern = "../../src/pyvale/data/plate_rigid_def0000.tiff" -roi = pyvale.dic.RegionOfInterest(ref_image=ref_pattern) -roi.rect_region(x=200,y=200,size_x=100,size_y=100) - -pyvale.dic.calculate_2d(reference=ref_pattern, - deformed=def_pattern, - roi_mask=roi.mask, - seed=[250,250], - subset_size=31, - subset_step=15, - max_displacement=2, - correlation_criteria="ZNSSD", - interpolation_routine="BICUBIC", - shape_function="AFFINE", - method="IMAGE_SCAN", - output_prefix="test_image_scan_znssd_affine_") - -pyvale.dic.calculate_2d(reference=ref_pattern, - deformed=def_pattern, - roi_mask=roi.mask, - seed=[250,250], - subset_size=31, - subset_step=15, - max_displacement=2, - correlation_criteria="ZNSSD", - interpolation_routine="BICUBIC", - shape_function="RIGID", - method="IMAGE_SCAN", - output_prefix="test_image_scan_znssd_rigid_") - -pyvale.dic.calculate_2d(reference=ref_pattern, - deformed=def_pattern, - roi_mask=roi.mask, - seed=[250,250], - subset_size=31, - subset_step=15, - max_displacement=2, - correlation_criteria="NSSD", - interpolation_routine="BICUBIC", - shape_function="AFFINE", - method="IMAGE_SCAN", - output_prefix="test_image_scan_nssd_affine_") - -pyvale.dic.calculate_2d(reference=ref_pattern, - deformed=def_pattern, - roi_mask=roi.mask, - seed=[250,250], - subset_size=31, - subset_step=15, - max_displacement=2, - correlation_criteria="ZNSSD", - interpolation_routine="BICUBIC", - shape_function="AFFINE", - method="MULTIWINDOW_RG", - output_prefix="test_rg_znssd_affine_") - -pyvale.dic.calculate_2d(reference=ref_pattern, - deformed=def_pattern, - roi_mask=roi.mask, - seed=[250,250], - subset_size=31, - subset_step=15, - max_displacement=2, - correlation_criteria="ZNSSD", - interpolation_routine="BICUBIC", - shape_function="AFFINE", - method="MULTIWINDOW", - output_prefix="test_fft_znssd_affine_") diff --git a/tests/dic/reference/ref_00_50.csv b/tests/dic/reference/ref_00_50.csv deleted file mode 100644 index 647259f6d..000000000 --- a/tests/dic/reference/ref_00_50.csv +++ /dev/null @@ -1,122 +0,0 @@ -subset_x,subset_y,displacement_u,displacement_v,displacement_mag,converged,cost -225,225,0.50,-0.50,0.707106,1,1.0,0,0,0 -240,225,0.50,-0.50,0.707106,1,1.0,0,0,0 -255,225,0.50,-0.50,0.707106,1,1.0,0,0,0 -270,225,0.50,-0.50,0.707106,1,1.0,0,0,0 -285,225,0.50,-0.50,0.707106,1,1.0,0,0,0 -300,225,0.50,-0.50,0.707106,1,1.0,0,0,0 -315,225,0.50,-0.50,0.707106,1,1.0,0,0,0 -330,225,0.50,-0.50,0.707106,1,1.0,0,0,0 -345,225,0.50,-0.50,0.707106,1,1.0,0,0,0 -360,225,0.50,-0.50,0.707106,1,1.0,0,0,0 -375,225,0.50,-0.50,0.707106,1,1.0,0,0,0 -225,240,0.50,-0.50,0.707106,1,1.0,0,0,0 -240,240,0.50,-0.50,0.707106,1,1.0,0,0,0 -255,240,0.50,-0.50,0.707106,1,1.0,0,0,0 -270,240,0.50,-0.50,0.707106,1,1.0,0,0,0 -285,240,0.50,-0.50,0.707106,1,1.0,0,0,0 -300,240,0.50,-0.50,0.707106,1,1.0,0,0,0 -315,240,0.50,-0.50,0.707106,1,1.0,0,0,0 -330,240,0.50,-0.50,0.707106,1,1.0,0,0,0 -345,240,0.50,-0.50,0.707106,1,1.0,0,0,0 -360,240,0.50,-0.50,0.707106,1,1.0,0,0,0 -375,240,0.50,-0.50,0.707106,1,1.0,0,0,0 -225,255,0.50,-0.50,0.707106,1,1.0,0,0,0 -240,255,0.50,-0.50,0.707106,1,1.0,0,0,0 -255,255,0.50,-0.50,0.707106,1,1.0,0,0,0 -270,255,0.50,-0.50,0.707106,1,1.0,0,0,0 -285,255,0.50,-0.50,0.707106,1,1.0,0,0,0 -300,255,0.50,-0.50,0.707106,1,1.0,0,0,0 -315,255,0.50,-0.50,0.707106,1,1.0,0,0,0 -330,255,0.50,-0.50,0.707106,1,1.0,0,0,0 -345,255,0.50,-0.50,0.707106,1,1.0,0,0,0 -360,255,0.50,-0.50,0.707106,1,1.0,0,0,0 -375,255,0.50,-0.50,0.707106,1,1.0,0,0,0 -225,270,0.50,-0.50,0.707106,1,1.0,0,0,0 -240,270,0.50,-0.50,0.707106,1,1.0,0,0,0 -255,270,0.50,-0.50,0.707106,1,1.0,0,0,0 -270,270,0.50,-0.50,0.707106,1,1.0,0,0,0 -285,270,0.50,-0.50,0.707106,1,1.0,0,0,0 -300,270,0.50,-0.50,0.707106,1,1.0,0,0,0 -315,270,0.50,-0.50,0.707106,1,1.0,0,0,0 -330,270,0.50,-0.50,0.707106,1,1.0,0,0,0 -345,270,0.50,-0.50,0.707106,1,1.0,0,0,0 -360,270,0.50,-0.50,0.707106,1,1.0,0,0,0 -375,270,0.50,-0.50,0.707106,1,1.0,0,0,0 -225,285,0.50,-0.50,0.707106,1,1.0,0,0,0 -240,285,0.50,-0.50,0.707106,1,1.0,0,0,0 -255,285,0.50,-0.50,0.707106,1,1.0,0,0,0 -270,285,0.50,-0.50,0.707106,1,1.0,0,0,0 -285,285,0.50,-0.50,0.707106,1,1.0,0,0,0 -300,285,0.50,-0.50,0.707106,1,1.0,0,0,0 -315,285,0.50,-0.50,0.707106,1,1.0,0,0,0 -330,285,0.50,-0.50,0.707106,1,1.0,0,0,0 -345,285,0.50,-0.50,0.707106,1,1.0,0,0,0 -360,285,0.50,-0.50,0.707106,1,1.0,0,0,0 -375,285,0.50,-0.50,0.707106,1,1.0,0,0,0 -225,300,0.50,-0.50,0.707106,1,1.0,0,0,0 -240,300,0.50,-0.50,0.707106,1,1.0,0,0,0 -255,300,0.50,-0.50,0.707106,1,1.0,0,0,0 -270,300,0.50,-0.50,0.707106,1,1.0,0,0,0 -285,300,0.50,-0.50,0.707106,1,1.0,0,0,0 -300,300,0.50,-0.50,0.707106,1,1.0,0,0,0 -315,300,0.50,-0.50,0.707106,1,1.0,0,0,0 -330,300,0.50,-0.50,0.707106,1,1.0,0,0,0 -345,300,0.50,-0.50,0.707106,1,1.0,0,0,0 -360,300,0.50,-0.50,0.707106,1,1.0,0,0,0 -375,300,0.50,-0.50,0.707106,1,1.0,0,0,0 -225,315,0.50,-0.50,0.707106,1,1.0,0,0,0 -240,315,0.50,-0.50,0.707106,1,1.0,0,0,0 -255,315,0.50,-0.50,0.707106,1,1.0,0,0,0 -270,315,0.50,-0.50,0.707106,1,1.0,0,0,0 -285,315,0.50,-0.50,0.707106,1,1.0,0,0,0 -300,315,0.50,-0.50,0.707106,1,1.0,0,0,0 -315,315,0.50,-0.50,0.707106,1,1.0,0,0,0 -330,315,0.50,-0.50,0.707106,1,1.0,0,0,0 -345,315,0.50,-0.50,0.707106,1,1.0,0,0,0 -360,315,0.50,-0.50,0.707106,1,1.0,0,0,0 -375,315,0.50,-0.50,0.707106,1,1.0,0,0,0 -225,330,0.50,-0.50,0.707106,1,1.0,0,0,0 -240,330,0.50,-0.50,0.707106,1,1.0,0,0,0 -255,330,0.50,-0.50,0.707106,1,1.0,0,0,0 -270,330,0.50,-0.50,0.707106,1,1.0,0,0,0 -285,330,0.50,-0.50,0.707106,1,1.0,0,0,0 -300,330,0.50,-0.50,0.707106,1,1.0,0,0,0 -315,330,0.50,-0.50,0.707106,1,1.0,0,0,0 -330,330,0.50,-0.50,0.707106,1,1.0,0,0,0 -345,330,0.50,-0.50,0.707106,1,1.0,0,0,0 -360,330,0.50,-0.50,0.707106,1,1.0,0,0,0 -375,330,0.50,-0.50,0.707106,1,1.0,0,0,0 -225,345,0.50,-0.50,0.707106,1,1.0,0,0,0 -240,345,0.50,-0.50,0.707106,1,1.0,0,0,0 -255,345,0.50,-0.50,0.707106,1,1.0,0,0,0 -270,345,0.50,-0.50,0.707106,1,1.0,0,0,0 -285,345,0.50,-0.50,0.707106,1,1.0,0,0,0 -300,345,0.50,-0.50,0.707106,1,1.0,0,0,0 -315,345,0.50,-0.50,0.707106,1,1.0,0,0,0 -330,345,0.50,-0.50,0.707106,1,1.0,0,0,0 -345,345,0.50,-0.50,0.707106,1,1.0,0,0,0 -360,345,0.50,-0.50,0.707106,1,1.0,0,0,0 -375,345,0.50,-0.50,0.707106,1,1.0,0,0,0 -225,360,0.50,-0.50,0.707106,1,1.0,0,0,0 -240,360,0.50,-0.50,0.707106,1,1.0,0,0,0 -255,360,0.50,-0.50,0.707106,1,1.0,0,0,0 -270,360,0.50,-0.50,0.707106,1,1.0,0,0,0 -285,360,0.50,-0.50,0.707106,1,1.0,0,0,0 -300,360,0.50,-0.50,0.707106,1,1.0,0,0,0 -315,360,0.50,-0.50,0.707106,1,1.0,0,0,0 -330,360,0.50,-0.50,0.707106,1,1.0,0,0,0 -345,360,0.50,-0.50,0.707106,1,1.0,0,0,0 -360,360,0.50,-0.50,0.707106,1,1.0,0,0,0 -375,360,0.50,-0.50,0.707106,1,1.0,0,0,0 -225,375,0.50,-0.50,0.707106,1,1.0,0,0,0 -240,375,0.50,-0.50,0.707106,1,1.0,0,0,0 -255,375,0.50,-0.50,0.707106,1,1.0,0,0,0 -270,375,0.50,-0.50,0.707106,1,1.0,0,0,0 -285,375,0.50,-0.50,0.707106,1,1.0,0,0,0 -300,375,0.50,-0.50,0.707106,1,1.0,0,0,0 -315,375,0.50,-0.50,0.707106,1,1.0,0,0,0 -330,375,0.50,-0.50,0.707106,1,1.0,0,0,0 -345,375,0.50,-0.50,0.707106,1,1.0,0,0,0 -360,375,0.50,-0.50,0.707106,1,1.0,0,0,0 -375,375,0.50,-0.50,0.707106,1,1.0,0,0,0 \ No newline at end of file diff --git a/tests/dic/reference/ref_01_00.csv b/tests/dic/reference/ref_01_00.csv deleted file mode 100644 index 15686216f..000000000 --- a/tests/dic/reference/ref_01_00.csv +++ /dev/null @@ -1,122 +0,0 @@ -subset_x,subset_y,displacement_u,displacement_v,displacement_mag,converged,cost -225,225,1.00,-1.00,1.414213562,1,1.0,0,0,0 -240,225,1.00,-1.00,1.414213562,1,1.0,0,0,0 -255,225,1.00,-1.00,1.414213562,1,1.0,0,0,0 -270,225,1.00,-1.00,1.414213562,1,1.0,0,0,0 -285,225,1.00,-1.00,1.414213562,1,1.0,0,0,0 -300,225,1.00,-1.00,1.414213562,1,1.0,0,0,0 -315,225,1.00,-1.00,1.414213562,1,1.0,0,0,0 -330,225,1.00,-1.00,1.414213562,1,1.0,0,0,0 -345,225,1.00,-1.00,1.414213562,1,1.0,0,0,0 -360,225,1.00,-1.00,1.414213562,1,1.0,0,0,0 -375,225,1.00,-1.00,1.414213562,1,1.0,0,0,0 -225,240,1.00,-1.00,1.414213562,1,1.0,0,0,0 -240,240,1.00,-1.00,1.414213562,1,1.0,0,0,0 -255,240,1.00,-1.00,1.414213562,1,1.0,0,0,0 -270,240,1.00,-1.00,1.414213562,1,1.0,0,0,0 -285,240,1.00,-1.00,1.414213562,1,1.0,0,0,0 -300,240,1.00,-1.00,1.414213562,1,1.0,0,0,0 -315,240,1.00,-1.00,1.414213562,1,1.0,0,0,0 -330,240,1.00,-1.00,1.414213562,1,1.0,0,0,0 -345,240,1.00,-1.00,1.414213562,1,1.0,0,0,0 -360,240,1.00,-1.00,1.414213562,1,1.0,0,0,0 -375,240,1.00,-1.00,1.414213562,1,1.0,0,0,0 -225,255,1.00,-1.00,1.414213562,1,1.0,0,0,0 -240,255,1.00,-1.00,1.414213562,1,1.0,0,0,0 -255,255,1.00,-1.00,1.414213562,1,1.0,0,0,0 -270,255,1.00,-1.00,1.414213562,1,1.0,0,0,0 -285,255,1.00,-1.00,1.414213562,1,1.0,0,0,0 -300,255,1.00,-1.00,1.414213562,1,1.0,0,0,0 -315,255,1.00,-1.00,1.414213562,1,1.0,0,0,0 -330,255,1.00,-1.00,1.414213562,1,1.0,0,0,0 -345,255,1.00,-1.00,1.414213562,1,1.0,0,0,0 -360,255,1.00,-1.00,1.414213562,1,1.0,0,0,0 -375,255,1.00,-1.00,1.414213562,1,1.0,0,0,0 -225,270,1.00,-1.00,1.414213562,1,1.0,0,0,0 -240,270,1.00,-1.00,1.414213562,1,1.0,0,0,0 -255,270,1.00,-1.00,1.414213562,1,1.0,0,0,0 -270,270,1.00,-1.00,1.414213562,1,1.0,0,0,0 -285,270,1.00,-1.00,1.414213562,1,1.0,0,0,0 -300,270,1.00,-1.00,1.414213562,1,1.0,0,0,0 -315,270,1.00,-1.00,1.414213562,1,1.0,0,0,0 -330,270,1.00,-1.00,1.414213562,1,1.0,0,0,0 -345,270,1.00,-1.00,1.414213562,1,1.0,0,0,0 -360,270,1.00,-1.00,1.414213562,1,1.0,0,0,0 -375,270,1.00,-1.00,1.414213562,1,1.0,0,0,0 -225,285,1.00,-1.00,1.414213562,1,1.0,0,0,0 -240,285,1.00,-1.00,1.414213562,1,1.0,0,0,0 -255,285,1.00,-1.00,1.414213562,1,1.0,0,0,0 -270,285,1.00,-1.00,1.414213562,1,1.0,0,0,0 -285,285,1.00,-1.00,1.414213562,1,1.0,0,0,0 -300,285,1.00,-1.00,1.414213562,1,1.0,0,0,0 -315,285,1.00,-1.00,1.414213562,1,1.0,0,0,0 -330,285,1.00,-1.00,1.414213562,1,1.0,0,0,0 -345,285,1.00,-1.00,1.414213562,1,1.0,0,0,0 -360,285,1.00,-1.00,1.414213562,1,1.0,0,0,0 -375,285,1.00,-1.00,1.414213562,1,1.0,0,0,0 -225,300,1.00,-1.00,1.414213562,1,1.0,0,0,0 -240,300,1.00,-1.00,1.414213562,1,1.0,0,0,0 -255,300,1.00,-1.00,1.414213562,1,1.0,0,0,0 -270,300,1.00,-1.00,1.414213562,1,1.0,0,0,0 -285,300,1.00,-1.00,1.414213562,1,1.0,0,0,0 -300,300,1.00,-1.00,1.414213562,1,1.0,0,0,0 -315,300,1.00,-1.00,1.414213562,1,1.0,0,0,0 -330,300,1.00,-1.00,1.414213562,1,1.0,0,0,0 -345,300,1.00,-1.00,1.414213562,1,1.0,0,0,0 -360,300,1.00,-1.00,1.414213562,1,1.0,0,0,0 -375,300,1.00,-1.00,1.414213562,1,1.0,0,0,0 -225,315,1.00,-1.00,1.414213562,1,1.0,0,0,0 -240,315,1.00,-1.00,1.414213562,1,1.0,0,0,0 -255,315,1.00,-1.00,1.414213562,1,1.0,0,0,0 -270,315,1.00,-1.00,1.414213562,1,1.0,0,0,0 -285,315,1.00,-1.00,1.414213562,1,1.0,0,0,0 -300,315,1.00,-1.00,1.414213562,1,1.0,0,0,0 -315,315,1.00,-1.00,1.414213562,1,1.0,0,0,0 -330,315,1.00,-1.00,1.414213562,1,1.0,0,0,0 -345,315,1.00,-1.00,1.414213562,1,1.0,0,0,0 -360,315,1.00,-1.00,1.414213562,1,1.0,0,0,0 -375,315,1.00,-1.00,1.414213562,1,1.0,0,0,0 -225,330,1.00,-1.00,1.414213562,1,1.0,0,0,0 -240,330,1.00,-1.00,1.414213562,1,1.0,0,0,0 -255,330,1.00,-1.00,1.414213562,1,1.0,0,0,0 -270,330,1.00,-1.00,1.414213562,1,1.0,0,0,0 -285,330,1.00,-1.00,1.414213562,1,1.0,0,0,0 -300,330,1.00,-1.00,1.414213562,1,1.0,0,0,0 -315,330,1.00,-1.00,1.414213562,1,1.0,0,0,0 -330,330,1.00,-1.00,1.414213562,1,1.0,0,0,0 -345,330,1.00,-1.00,1.414213562,1,1.0,0,0,0 -360,330,1.00,-1.00,1.414213562,1,1.0,0,0,0 -375,330,1.00,-1.00,1.414213562,1,1.0,0,0,0 -225,345,1.00,-1.00,1.414213562,1,1.0,0,0,0 -240,345,1.00,-1.00,1.414213562,1,1.0,0,0,0 -255,345,1.00,-1.00,1.414213562,1,1.0,0,0,0 -270,345,1.00,-1.00,1.414213562,1,1.0,0,0,0 -285,345,1.00,-1.00,1.414213562,1,1.0,0,0,0 -300,345,1.00,-1.00,1.414213562,1,1.0,0,0,0 -315,345,1.00,-1.00,1.414213562,1,1.0,0,0,0 -330,345,1.00,-1.00,1.414213562,1,1.0,0,0,0 -345,345,1.00,-1.00,1.414213562,1,1.0,0,0,0 -360,345,1.00,-1.00,1.414213562,1,1.0,0,0,0 -375,345,1.00,-1.00,1.414213562,1,1.0,0,0,0 -225,360,1.00,-1.00,1.414213562,1,1.0,0,0,0 -240,360,1.00,-1.00,1.414213562,1,1.0,0,0,0 -255,360,1.00,-1.00,1.414213562,1,1.0,0,0,0 -270,360,1.00,-1.00,1.414213562,1,1.0,0,0,0 -285,360,1.00,-1.00,1.414213562,1,1.0,0,0,0 -300,360,1.00,-1.00,1.414213562,1,1.0,0,0,0 -315,360,1.00,-1.00,1.414213562,1,1.0,0,0,0 -330,360,1.00,-1.00,1.414213562,1,1.0,0,0,0 -345,360,1.00,-1.00,1.414213562,1,1.0,0,0,0 -360,360,1.00,-1.00,1.414213562,1,1.0,0,0,0 -375,360,1.00,-1.00,1.414213562,1,1.0,0,0,0 -225,375,1.00,-1.00,1.414213562,1,1.0,0,0,0 -240,375,1.00,-1.00,1.414213562,1,1.0,0,0,0 -255,375,1.00,-1.00,1.414213562,1,1.0,0,0,0 -270,375,1.00,-1.00,1.414213562,1,1.0,0,0,0 -285,375,1.00,-1.00,1.414213562,1,1.0,0,0,0 -300,375,1.00,-1.00,1.414213562,1,1.0,0,0,0 -315,375,1.00,-1.00,1.414213562,1,1.0,0,0,0 -330,375,1.00,-1.00,1.414213562,1,1.0,0,0,0 -345,375,1.00,-1.00,1.414213562,1,1.0,0,0,0 -360,375,1.00,-1.00,1.414213562,1,1.0,0,0,0 -375,375,1.00,-1.00,1.414213562,1,1.0,0,0,0 \ No newline at end of file diff --git a/tests/dic/reference/ref_25_00.csv b/tests/dic/reference/ref_25_00.csv deleted file mode 100644 index 1ae95444e..000000000 --- a/tests/dic/reference/ref_25_00.csv +++ /dev/null @@ -1,122 +0,0 @@ -subset_x,subset_y,displacement_u,displacement_v,displacement_mag,converged,cost -225,225,25,-25,35.35533906,1,1.0,0,0,0 -240,225,25,-25,35.35533906,1,1.0,0,0,0 -255,225,25,-25,35.35533906,1,1.0,0,0,0 -270,225,25,-25,35.35533906,1,1.0,0,0,0 -285,225,25,-25,35.35533906,1,1.0,0,0,0 -300,225,25,-25,35.35533906,1,1.0,0,0,0 -315,225,25,-25,35.35533906,1,1.0,0,0,0 -330,225,25,-25,35.35533906,1,1.0,0,0,0 -345,225,25,-25,35.35533906,1,1.0,0,0,0 -360,225,25,-25,35.35533906,1,1.0,0,0,0 -375,225,25,-25,35.35533906,1,1.0,0,0,0 -225,240,25,-25,35.35533906,1,1.0,0,0,0 -240,240,25,-25,35.35533906,1,1.0,0,0,0 -255,240,25,-25,35.35533906,1,1.0,0,0,0 -270,240,25,-25,35.35533906,1,1.0,0,0,0 -285,240,25,-25,35.35533906,1,1.0,0,0,0 -300,240,25,-25,35.35533906,1,1.0,0,0,0 -315,240,25,-25,35.35533906,1,1.0,0,0,0 -330,240,25,-25,35.35533906,1,1.0,0,0,0 -345,240,25,-25,35.35533906,1,1.0,0,0,0 -360,240,25,-25,35.35533906,1,1.0,0,0,0 -375,240,25,-25,35.35533906,1,1.0,0,0,0 -225,255,25,-25,35.35533906,1,1.0,0,0,0 -240,255,25,-25,35.35533906,1,1.0,0,0,0 -255,255,25,-25,35.35533906,1,1.0,0,0,0 -270,255,25,-25,35.35533906,1,1.0,0,0,0 -285,255,25,-25,35.35533906,1,1.0,0,0,0 -300,255,25,-25,35.35533906,1,1.0,0,0,0 -315,255,25,-25,35.35533906,1,1.0,0,0,0 -330,255,25,-25,35.35533906,1,1.0,0,0,0 -345,255,25,-25,35.35533906,1,1.0,0,0,0 -360,255,25,-25,35.35533906,1,1.0,0,0,0 -375,255,25,-25,35.35533906,1,1.0,0,0,0 -225,270,25,-25,35.35533906,1,1.0,0,0,0 -240,270,25,-25,35.35533906,1,1.0,0,0,0 -255,270,25,-25,35.35533906,1,1.0,0,0,0 -270,270,25,-25,35.35533906,1,1.0,0,0,0 -285,270,25,-25,35.35533906,1,1.0,0,0,0 -300,270,25,-25,35.35533906,1,1.0,0,0,0 -315,270,25,-25,35.35533906,1,1.0,0,0,0 -330,270,25,-25,35.35533906,1,1.0,0,0,0 -345,270,25,-25,35.35533906,1,1.0,0,0,0 -360,270,25,-25,35.35533906,1,1.0,0,0,0 -375,270,25,-25,35.35533906,1,1.0,0,0,0 -225,285,25,-25,35.35533906,1,1.0,0,0,0 -240,285,25,-25,35.35533906,1,1.0,0,0,0 -255,285,25,-25,35.35533906,1,1.0,0,0,0 -270,285,25,-25,35.35533906,1,1.0,0,0,0 -285,285,25,-25,35.35533906,1,1.0,0,0,0 -300,285,25,-25,35.35533906,1,1.0,0,0,0 -315,285,25,-25,35.35533906,1,1.0,0,0,0 -330,285,25,-25,35.35533906,1,1.0,0,0,0 -345,285,25,-25,35.35533906,1,1.0,0,0,0 -360,285,25,-25,35.35533906,1,1.0,0,0,0 -375,285,25,-25,35.35533906,1,1.0,0,0,0 -225,300,25,-25,35.35533906,1,1.0,0,0,0 -240,300,25,-25,35.35533906,1,1.0,0,0,0 -255,300,25,-25,35.35533906,1,1.0,0,0,0 -270,300,25,-25,35.35533906,1,1.0,0,0,0 -285,300,25,-25,35.35533906,1,1.0,0,0,0 -300,300,25,-25,35.35533906,1,1.0,0,0,0 -315,300,25,-25,35.35533906,1,1.0,0,0,0 -330,300,25,-25,35.35533906,1,1.0,0,0,0 -345,300,25,-25,35.35533906,1,1.0,0,0,0 -360,300,25,-25,35.35533906,1,1.0,0,0,0 -375,300,25,-25,35.35533906,1,1.0,0,0,0 -225,315,25,-25,35.35533906,1,1.0,0,0,0 -240,315,25,-25,35.35533906,1,1.0,0,0,0 -255,315,25,-25,35.35533906,1,1.0,0,0,0 -270,315,25,-25,35.35533906,1,1.0,0,0,0 -285,315,25,-25,35.35533906,1,1.0,0,0,0 -300,315,25,-25,35.35533906,1,1.0,0,0,0 -315,315,25,-25,35.35533906,1,1.0,0,0,0 -330,315,25,-25,35.35533906,1,1.0,0,0,0 -345,315,25,-25,35.35533906,1,1.0,0,0,0 -360,315,25,-25,35.35533906,1,1.0,0,0,0 -375,315,25,-25,35.35533906,1,1.0,0,0,0 -225,330,25,-25,35.35533906,1,1.0,0,0,0 -240,330,25,-25,35.35533906,1,1.0,0,0,0 -255,330,25,-25,35.35533906,1,1.0,0,0,0 -270,330,25,-25,35.35533906,1,1.0,0,0,0 -285,330,25,-25,35.35533906,1,1.0,0,0,0 -300,330,25,-25,35.35533906,1,1.0,0,0,0 -315,330,25,-25,35.35533906,1,1.0,0,0,0 -330,330,25,-25,35.35533906,1,1.0,0,0,0 -345,330,25,-25,35.35533906,1,1.0,0,0,0 -360,330,25,-25,35.35533906,1,1.0,0,0,0 -375,330,25,-25,35.35533906,1,1.0,0,0,0 -225,345,25,-25,35.35533906,1,1.0,0,0,0 -240,345,25,-25,35.35533906,1,1.0,0,0,0 -255,345,25,-25,35.35533906,1,1.0,0,0,0 -270,345,25,-25,35.35533906,1,1.0,0,0,0 -285,345,25,-25,35.35533906,1,1.0,0,0,0 -300,345,25,-25,35.35533906,1,1.0,0,0,0 -315,345,25,-25,35.35533906,1,1.0,0,0,0 -330,345,25,-25,35.35533906,1,1.0,0,0,0 -345,345,25,-25,35.35533906,1,1.0,0,0,0 -360,345,25,-25,35.35533906,1,1.0,0,0,0 -375,345,25,-25,35.35533906,1,1.0,0,0,0 -225,360,25,-25,35.35533906,1,1.0,0,0,0 -240,360,25,-25,35.35533906,1,1.0,0,0,0 -255,360,25,-25,35.35533906,1,1.0,0,0,0 -270,360,25,-25,35.35533906,1,1.0,0,0,0 -285,360,25,-25,35.35533906,1,1.0,0,0,0 -300,360,25,-25,35.35533906,1,1.0,0,0,0 -315,360,25,-25,35.35533906,1,1.0,0,0,0 -330,360,25,-25,35.35533906,1,1.0,0,0,0 -345,360,25,-25,35.35533906,1,1.0,0,0,0 -360,360,25,-25,35.35533906,1,1.0,0,0,0 -375,360,25,-25,35.35533906,1,1.0,0,0,0 -225,375,25,-25,35.35533906,1,1.0,0,0,0 -240,375,25,-25,35.35533906,1,1.0,0,0,0 -255,375,25,-25,35.35533906,1,1.0,0,0,0 -270,375,25,-25,35.35533906,1,1.0,0,0,0 -285,375,25,-25,35.35533906,1,1.0,0,0,0 -300,375,25,-25,35.35533906,1,1.0,0,0,0 -315,375,25,-25,35.35533906,1,1.0,0,0,0 -330,375,25,-25,35.35533906,1,1.0,0,0,0 -345,375,25,-25,35.35533906,1,1.0,0,0,0 -360,375,25,-25,35.35533906,1,1.0,0,0,0 -375,375,25,-25,35.35533906,1,1.0,0,0,0 \ No newline at end of file diff --git a/tests/dic/reference/ref_50_00.csv b/tests/dic/reference/ref_50_00.csv deleted file mode 100644 index e82860c85..000000000 --- a/tests/dic/reference/ref_50_00.csv +++ /dev/null @@ -1,122 +0,0 @@ -subset_x,subset_y,displacement_u,displacement_v,displacement_mag,converged,cost -225,225,50,-50,70.71067,1,1.0,0,0,0 -240,225,50,-50,70.71067,1,1.0,0,0,0 -255,225,50,-50,70.71067,1,1.0,0,0,0 -270,225,50,-50,70.71067,1,1.0,0,0,0 -285,225,50,-50,70.71067,1,1.0,0,0,0 -300,225,50,-50,70.71067,1,1.0,0,0,0 -315,225,50,-50,70.71067,1,1.0,0,0,0 -330,225,50,-50,70.71067,1,1.0,0,0,0 -345,225,50,-50,70.71067,1,1.0,0,0,0 -360,225,50,-50,70.71067,1,1.0,0,0,0 -375,225,50,-50,70.71067,1,1.0,0,0,0 -225,240,50,-50,70.71067,1,1.0,0,0,0 -240,240,50,-50,70.71067,1,1.0,0,0,0 -255,240,50,-50,70.71067,1,1.0,0,0,0 -270,240,50,-50,70.71067,1,1.0,0,0,0 -285,240,50,-50,70.71067,1,1.0,0,0,0 -300,240,50,-50,70.71067,1,1.0,0,0,0 -315,240,50,-50,70.71067,1,1.0,0,0,0 -330,240,50,-50,70.71067,1,1.0,0,0,0 -345,240,50,-50,70.71067,1,1.0,0,0,0 -360,240,50,-50,70.71067,1,1.0,0,0,0 -375,240,50,-50,70.71067,1,1.0,0,0,0 -225,255,50,-50,70.71067,1,1.0,0,0,0 -240,255,50,-50,70.71067,1,1.0,0,0,0 -255,255,50,-50,70.71067,1,1.0,0,0,0 -270,255,50,-50,70.71067,1,1.0,0,0,0 -285,255,50,-50,70.71067,1,1.0,0,0,0 -300,255,50,-50,70.71067,1,1.0,0,0,0 -315,255,50,-50,70.71067,1,1.0,0,0,0 -330,255,50,-50,70.71067,1,1.0,0,0,0 -345,255,50,-50,70.71067,1,1.0,0,0,0 -360,255,50,-50,70.71067,1,1.0,0,0,0 -375,255,50,-50,70.71067,1,1.0,0,0,0 -225,270,50,-50,70.71067,1,1.0,0,0,0 -240,270,50,-50,70.71067,1,1.0,0,0,0 -255,270,50,-50,70.71067,1,1.0,0,0,0 -270,270,50,-50,70.71067,1,1.0,0,0,0 -285,270,50,-50,70.71067,1,1.0,0,0,0 -300,270,50,-50,70.71067,1,1.0,0,0,0 -315,270,50,-50,70.71067,1,1.0,0,0,0 -330,270,50,-50,70.71067,1,1.0,0,0,0 -345,270,50,-50,70.71067,1,1.0,0,0,0 -360,270,50,-50,70.71067,1,1.0,0,0,0 -375,270,50,-50,70.71067,1,1.0,0,0,0 -225,285,50,-50,70.71067,1,1.0,0,0,0 -240,285,50,-50,70.71067,1,1.0,0,0,0 -255,285,50,-50,70.71067,1,1.0,0,0,0 -270,285,50,-50,70.71067,1,1.0,0,0,0 -285,285,50,-50,70.71067,1,1.0,0,0,0 -300,285,50,-50,70.71067,1,1.0,0,0,0 -315,285,50,-50,70.71067,1,1.0,0,0,0 -330,285,50,-50,70.71067,1,1.0,0,0,0 -345,285,50,-50,70.71067,1,1.0,0,0,0 -360,285,50,-50,70.71067,1,1.0,0,0,0 -375,285,50,-50,70.71067,1,1.0,0,0,0 -225,300,50,-50,70.71067,1,1.0,0,0,0 -240,300,50,-50,70.71067,1,1.0,0,0,0 -255,300,50,-50,70.71067,1,1.0,0,0,0 -270,300,50,-50,70.71067,1,1.0,0,0,0 -285,300,50,-50,70.71067,1,1.0,0,0,0 -300,300,50,-50,70.71067,1,1.0,0,0,0 -315,300,50,-50,70.71067,1,1.0,0,0,0 -330,300,50,-50,70.71067,1,1.0,0,0,0 -345,300,50,-50,70.71067,1,1.0,0,0,0 -360,300,50,-50,70.71067,1,1.0,0,0,0 -375,300,50,-50,70.71067,1,1.0,0,0,0 -225,315,50,-50,70.71067,1,1.0,0,0,0 -240,315,50,-50,70.71067,1,1.0,0,0,0 -255,315,50,-50,70.71067,1,1.0,0,0,0 -270,315,50,-50,70.71067,1,1.0,0,0,0 -285,315,50,-50,70.71067,1,1.0,0,0,0 -300,315,50,-50,70.71067,1,1.0,0,0,0 -315,315,50,-50,70.71067,1,1.0,0,0,0 -330,315,50,-50,70.71067,1,1.0,0,0,0 -345,315,50,-50,70.71067,1,1.0,0,0,0 -360,315,50,-50,70.71067,1,1.0,0,0,0 -375,315,50,-50,70.71067,1,1.0,0,0,0 -225,330,50,-50,70.71067,1,1.0,0,0,0 -240,330,50,-50,70.71067,1,1.0,0,0,0 -255,330,50,-50,70.71067,1,1.0,0,0,0 -270,330,50,-50,70.71067,1,1.0,0,0,0 -285,330,50,-50,70.71067,1,1.0,0,0,0 -300,330,50,-50,70.71067,1,1.0,0,0,0 -315,330,50,-50,70.71067,1,1.0,0,0,0 -330,330,50,-50,70.71067,1,1.0,0,0,0 -345,330,50,-50,70.71067,1,1.0,0,0,0 -360,330,50,-50,70.71067,1,1.0,0,0,0 -375,330,50,-50,70.71067,1,1.0,0,0,0 -225,345,50,-50,70.71067,1,1.0,0,0,0 -240,345,50,-50,70.71067,1,1.0,0,0,0 -255,345,50,-50,70.71067,1,1.0,0,0,0 -270,345,50,-50,70.71067,1,1.0,0,0,0 -285,345,50,-50,70.71067,1,1.0,0,0,0 -300,345,50,-50,70.71067,1,1.0,0,0,0 -315,345,50,-50,70.71067,1,1.0,0,0,0 -330,345,50,-50,70.71067,1,1.0,0,0,0 -345,345,50,-50,70.71067,1,1.0,0,0,0 -360,345,50,-50,70.71067,1,1.0,0,0,0 -375,345,50,-50,70.71067,1,1.0,0,0,0 -225,360,50,-50,70.71067,1,1.0,0,0,0 -240,360,50,-50,70.71067,1,1.0,0,0,0 -255,360,50,-50,70.71067,1,1.0,0,0,0 -270,360,50,-50,70.71067,1,1.0,0,0,0 -285,360,50,-50,70.71067,1,1.0,0,0,0 -300,360,50,-50,70.71067,1,1.0,0,0,0 -315,360,50,-50,70.71067,1,1.0,0,0,0 -330,360,50,-50,70.71067,1,1.0,0,0,0 -345,360,50,-50,70.71067,1,1.0,0,0,0 -360,360,50,-50,70.71067,1,1.0,0,0,0 -375,360,50,-50,70.71067,1,1.0,0,0,0 -225,375,50,-50,70.71067,1,1.0,0,0,0 -240,375,50,-50,70.71067,1,1.0,0,0,0 -255,375,50,-50,70.71067,1,1.0,0,0,0 -270,375,50,-50,70.71067,1,1.0,0,0,0 -285,375,50,-50,70.71067,1,1.0,0,0,0 -300,375,50,-50,70.71067,1,1.0,0,0,0 -315,375,50,-50,70.71067,1,1.0,0,0,0 -330,375,50,-50,70.71067,1,1.0,0,0,0 -345,375,50,-50,70.71067,1,1.0,0,0,0 -360,375,50,-50,70.71067,1,1.0,0,0,0 -375,375,50,-50,70.71067,1,1.0,0,0,0 \ No newline at end of file diff --git a/tests/dic/test_dic.py b/tests/dic/test_dic.py index 698a300ac..3df3be6bc 100644 --- a/tests/dic/test_dic.py +++ b/tests/dic/test_dic.py @@ -8,257 +8,791 @@ ================================================================================ """ import os -os.environ['OMP_NUM_THREADS'] = '1' +import glob + +os.environ["OMP_NUM_THREADS"] = "1" from PIL import Image import numpy as np +import matplotlib.pyplot as plt import pyvale.dic as dic -import pyvale.dataset as pyv_data -from pathlib import Path +import pyvale.dataset as dataset +import pyvale.calib as calib + test_dir = os.path.dirname(__file__) -ref_pattern = pyv_data.dic_plate_rigid_ref() -def_pattern = pyv_data.dic_plate_rigid_def() -def_pattern_25px = pyv_data.dic_plate_rigid_def_25px() -def_pattern_50px = pyv_data.dic_plate_rigid_def_50px() -def_large = [def_pattern_25px, def_pattern_50px] +ref0 = dataset.dic_plate_rigid_cam0_ref() +ref1 = dataset.dic_plate_rigid_cam1_ref() +def0 = dataset.dic_plate_rigid_cam0_def_small() +def1 = dataset.dic_plate_rigid_cam1_def_small() + +ref0_hydro = dataset.dic_plate_with_hydro_cam0_ref() +ref1_hydro = dataset.dic_plate_with_hydro_cam1_ref() +def0_hydro = dataset.dic_plate_with_hydro_cam0_def() +def1_hydro = dataset.dic_plate_with_hydro_cam1_def() -roi = dic.RegionOfInterest(ref_image=ref_pattern) -roi.rect_region(x=200, y=200, size_x=200, size_y=200) +def0_10px = dataset.dic_plate_rigid_cam0_def_10px() +def0_25px = dataset.dic_plate_rigid_cam0_def_25px() +def0_50px = dataset.dic_plate_rigid_cam0_def_50px() +calib_file = test_dir + "/calib.txt" +calib_data = calib.loadtxt(calib_file) -true_file_00_5 = os.path.abspath(os.path.join(test_dir, "./reference/ref_00_50.csv")) -true_file_01_0 = os.path.abspath(os.path.join(test_dir, "./reference/ref_01_00.csv")) -true_file_25_0 = os.path.abspath(os.path.join(test_dir, "./reference/ref_25_00.csv")) -true_file_50_0 = os.path.abspath(os.path.join(test_dir, "./reference/ref_50_00.csv")) +def_large = [def0_10px, def0_25px, def0_50px] -true_00_5 = np.loadtxt(true_file_00_5, skiprows=1, delimiter=',') -true_01_0 = np.loadtxt(true_file_01_0, skiprows=1, delimiter=',') -true_25_0 = np.loadtxt(true_file_25_0, skiprows=1, delimiter=',') -true_50_0 = np.loadtxt(true_file_50_0, skiprows=1, delimiter=',') +roi = dic.RegionOfInterest(ref_image=ref0) +roi.rect_region(x=100, y=100, size_x=200, size_y=200) +# ------------------------------------------------------------------------------ +# Images +# ------------------------------------------------------------------------------ +ref_image = Image.open(ref0) +if isinstance(def0, list): + files = sorted(def0) +else: + files = sorted(def0.parent.glob(def0.name)) -# Create a deformed image where intensities are scaled/offset -ref_image = Image.open(ref_pattern) -files = list(def_pattern.parent.glob(def_pattern.name)) -files = sorted(files) -def_image = Image.open(files[0]) ref_arr = np.array(ref_image) + +# First deformed image used for intensity scaling tests +def_image = Image.open(files[7]) def_arr = np.array(def_image) + original_dtype = def_arr.dtype + scale = 0.5 offset = 50 -def_arr = def_arr.astype(np.float32) -def_arr_scaled = def_arr * scale -def_arr_scaled_offset = def_arr * scale + offset -def_arr = def_arr.astype(original_dtype) -def_arr_scaled = def_arr_scaled.astype(original_dtype) -def_arr_scaled_offset = def_arr_scaled_offset.astype(original_dtype) - - -def test_ssd_rigid(): - dic.calculate_2d(reference=ref_arr, - deformed=def_arr, - roi_mask=roi.mask, - seed=[250,250], - subset_size=31, - subset_step=15, - max_displacement=2, - correlation_criteria="SSD", - shape_function="AFFINE", - method="MULTIWINDOW_RG", - output_basepath=test_dir, - output_prefix="test_ssd_rigid_") - - output_file_00_5 = os.path.abspath(os.path.join(test_dir, "./test_ssd_rigid_def_img_0000.csv")) - output_data_00_5 = np.loadtxt(output_file_00_5, skiprows=1, delimiter=',') - np.testing.assert_allclose(true_00_5[:, :6], output_data_00_5[:, :6], rtol=0.005, atol=1e-6, - err_msg="Results from test Do not match ground truth displacement of 0.5px") - - os.remove(output_file_00_5) - -def test_nssd_scaled_image_rigid(): - dic.calculate_2d(reference=ref_arr, - deformed=def_arr_scaled, - roi_mask=roi.mask, - seed=[250,250], - subset_size=31, - subset_step=15, - max_displacement=2, - correlation_criteria="NSSD", - shape_function="AFFINE", - method="MULTIWINDOW_RG", - output_basepath=test_dir, - output_prefix="test_nssd_scaled_image_rigid_") - - output_file_00_5 = os.path.abspath(os.path.join(test_dir, "./test_nssd_scaled_image_rigid_def_img_0000.csv")) - output_data_00_5 = np.loadtxt(output_file_00_5, skiprows=1, delimiter=',') - np.testing.assert_allclose(true_00_5[:, :7], output_data_00_5[:, :7], rtol=0.005, atol=1e-6, - err_msg="Results from test Do not match ground truth displacement of 0.5px") - os.remove(output_file_00_5) - -def test_znssd_scaled_offset_image_rigid(): - dic.calculate_2d(reference=ref_arr, - deformed=def_arr_scaled_offset, - roi_mask=roi.mask, - seed=[250,250], - subset_size=31, - subset_step=15, - max_displacement=2, - correlation_criteria="ZNSSD", - shape_function="AFFINE", - method="MULTIWINDOW_RG", - output_basepath=test_dir, - output_prefix="test_znssd_scaled_offset_image_rigid_") - - output_file_00_5 = os.path.abspath(os.path.join(test_dir, "./test_znssd_scaled_offset_image_rigid_def_img_0000.csv")) - output_data_00_5 = np.loadtxt(output_file_00_5, skiprows=1, delimiter=',') - np.testing.assert_allclose(true_00_5[:, :7], output_data_00_5[:, :7], rtol=0.005, atol=1e-6, - err_msg="Results from test Do not match ground truth displacement of 0.5px") - os.remove(output_file_00_5) - -def test_image_scan_znssd_affine(): - dic.calculate_2d(reference=ref_pattern, - deformed=def_pattern, - roi_mask=roi.mask, - seed=[250,250], - subset_size=31, - subset_step=15, - max_displacement=2, - correlation_criteria="ZNSSD", - shape_function="AFFINE", - method="IMAGE_SCAN", - output_basepath=test_dir, - output_prefix="test_image_scan_znssd_affine_") - - output_file_00_5 = os.path.abspath(os.path.join(test_dir, "./test_image_scan_znssd_affine_plate_rigid_def0000.csv")) - output_file_01_0 = os.path.abspath(os.path.join(test_dir, "./test_image_scan_znssd_affine_plate_rigid_def0001.csv")) - output_data_00_5 = np.loadtxt(output_file_00_5, skiprows=1, delimiter=',') - output_data_01_0 = np.loadtxt(output_file_01_0, skiprows=1, delimiter=',') - - np.testing.assert_allclose(true_00_5[:, :7], output_data_00_5[:, :7], rtol=0.005, atol=1e-6, - err_msg="Results from test Do not match ground truth displacement of 0.5px") - - np.testing.assert_allclose(true_01_0[:, :7], output_data_01_0[:, :7], rtol=0.005, atol=1e-6, - err_msg="Results from test Do not match ground truth displacement of 1.0px") - - os.remove(output_file_00_5) - os.remove(output_file_01_0) - -def test_image_scan_znssd_rigid(): - dic.calculate_2d(reference=ref_pattern, - deformed=def_pattern, - roi_mask=roi.mask, - seed=[250,250], - subset_size=31, - subset_step=15, - max_displacement=2, - correlation_criteria="ZNSSD", - shape_function="RIGID", - method="IMAGE_SCAN", - output_basepath=test_dir, - output_prefix="test_image_scan_znssd_rigid_") - - output_file_00_5 = os.path.abspath(os.path.join(test_dir, "./test_image_scan_znssd_rigid_plate_rigid_def0000.csv")) - output_file_01_0 = os.path.abspath(os.path.join(test_dir, "./test_image_scan_znssd_rigid_plate_rigid_def0001.csv")) - output_data_00_5 = np.loadtxt(output_file_00_5, skiprows=1, delimiter=',') - output_data_01_0 = np.loadtxt(output_file_01_0, skiprows=1, delimiter=',') - - np.testing.assert_allclose(true_00_5[:, :7], output_data_00_5[:, :7], rtol=0.005, atol=1e-6, - err_msg="Results from test Do not match ground truth displacement of 0.5px") - - np.testing.assert_allclose(true_01_0[:, :7], output_data_01_0[:, :7], rtol=0.005, atol=1e-6, - err_msg="Results from test Do not match ground truth displacement of 1.0px") - - os.remove(output_file_00_5) - os.remove(output_file_01_0) - -def test_image_scan_nssd_affine(): - dic.calculate_2d(reference=ref_pattern, - deformed=def_pattern, - roi_mask=roi.mask, - seed=[250,250], - subset_size=31, - subset_step=15, - max_displacement=2, - correlation_criteria="NSSD", - shape_function="AFFINE", - method="IMAGE_SCAN", - output_basepath=test_dir, - output_prefix="test_image_scan_nssd_affine_") - - output_file_00_5 = os.path.abspath(os.path.join(test_dir, "./test_image_scan_nssd_affine_plate_rigid_def0000.csv")) - output_file_01_0 = os.path.abspath(os.path.join(test_dir, "./test_image_scan_nssd_affine_plate_rigid_def0001.csv")) - output_data_00_5 = np.loadtxt(output_file_00_5, skiprows=1, delimiter=',') - output_data_01_0 = np.loadtxt(output_file_01_0, skiprows=1, delimiter=',') - - np.testing.assert_allclose(true_00_5[:, :7], output_data_00_5[:, :7], rtol=0.005, atol=1e-6, - err_msg="Results from test Do not match ground truth displacement of 0.5px") - - np.testing.assert_allclose(true_01_0[:, :7], output_data_01_0[:, :7], rtol=0.005, atol=1e-6, - err_msg="Results from test Do not match ground truth displacement of 1.0px") - - os.remove(output_file_00_5) - os.remove(output_file_01_0) - -def test_rg_znssd_affine(): - dic.calculate_2d(reference=ref_pattern, - deformed=def_pattern, - roi_mask=roi.mask, - seed=[250,250], - subset_size=31, - subset_step=15, - max_displacement=2, - correlation_criteria="ZNSSD", - shape_function="AFFINE", - method="MULTIWINDOW_RG", - output_basepath=test_dir, - output_prefix="test_rg_znssd_affine_") - - output_file_00_5 = os.path.abspath(os.path.join(test_dir, "./test_rg_znssd_affine_plate_rigid_def0000.csv")) - output_file_01_0 = os.path.abspath(os.path.join(test_dir, "./test_rg_znssd_affine_plate_rigid_def0001.csv")) - output_data_00_5 = np.loadtxt(output_file_00_5, skiprows=1, delimiter=',') - output_data_01_0 = np.loadtxt(output_file_01_0, skiprows=1, delimiter=',') - - np.testing.assert_allclose(true_00_5[:, :7], output_data_00_5[:, :7], rtol=0.005, atol=1e-6, - err_msg="Results from test Do not match ground truth displacement of 0.5px") - - np.testing.assert_allclose(true_01_0[:, :7], output_data_01_0[:, :7], rtol=0.005, atol=1e-6, - err_msg="Results from test Do not match ground truth displacement of 1.0px") - - os.remove(output_file_00_5) - os.remove(output_file_01_0) - -def test_fft_large(): - dic.calculate_2d(reference=ref_pattern, - deformed=def_large, - roi_mask=roi.mask, - seed=[250,250], - subset_size=31, - subset_step=15, - max_displacement=100, - correlation_criteria="ZNSSD", - shape_function="RIGID", - method="MULTIWINDOW", - output_basepath=test_dir, - output_prefix="test_fft_") - - output_file_25_0 = os.path.abspath(os.path.join(test_dir, "./test_fft_plate_rigid_def_25px.csv")) - output_file_50_0 = os.path.abspath(os.path.join(test_dir, "./test_fft_plate_rigid_def_50px.csv")) - output_data_25_0 = np.loadtxt(output_file_25_0, skiprows=1, delimiter=',') - output_data_50_0 = np.loadtxt(output_file_50_0, skiprows=1, delimiter=',') - - np.testing.assert_allclose(true_25_0[:, :6], output_data_25_0[:, :6], rtol=0.01, atol=1e-6, - err_msg="Results from test Do not match ground truth displacement of 25px") - - np.testing.assert_allclose(true_50_0[:, :6], output_data_50_0[:, :6], rtol=0.01, atol=1e-6, - err_msg="Results from test Do not match ground truth displacement of 50px") - - os.remove(output_file_25_0) - os.remove(output_file_50_0) +def_arr_float = def_arr.astype(np.float32) + +def_arr_scaled = (def_arr_float * scale).astype(original_dtype) +def_arr_scaled_offset = (def_arr_float * scale + offset).astype(original_dtype) + +# ------------------------------------------------------------------------------ +# Ground truth displacements +# ------------------------------------------------------------------------------ + +u = [0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0] +u_short = [0.0,0.5,1.0] + + +# ------------------------------------------------------------------------------ +# Helpers +# ------------------------------------------------------------------------------ + +def validate_col(csv_file, gt, col, rtol,atol): + dic_data = np.loadtxt(csv_file, skiprows=1, delimiter=",") + + gt_u = np.full(dic_data.shape[0], gt) + + np.testing.assert_allclose( + gt_u, + dic_data[:, col], + rtol=rtol, + atol=atol, + err_msg=f"Horizontal displacement mismatch for {gt} px", + ) + + +def validate(output_pattern, gt, atol, rtol=0.0, atol_stereo=0.001,stereo=False): + output_files = sorted(glob.glob(output_pattern)) + + assert len(output_files) == len(gt), ( + f"Expected {len(gt)} output files but found " + f"{len(output_files)}" + ) + + for gt_i, output_file in zip(gt, output_files): + + # check horizontal displacement PIXELS + validate_col(output_file, gt_i, 2, rtol,atol) + + # check horizontal displacement PIXELS + validate_col(output_file, -1.0*gt_i, 3, rtol, atol) + + + if (stereo): + + # check horizontal displacement MM + validate_col(output_file, -0.01*gt_i, 13, rtol, atol_stereo) + + # check vertical displacement MM + validate_col(output_file, 0.01*gt_i, 14, rtol, atol_stereo) + + for files in (output_files): + os.remove(files) + + +def validate_hydro(output_pattern, gt, atol, rtol=0.0): + + output_files = sorted(glob.glob(output_pattern)) + + assert len(output_files) == len(gt), ( + f"Expected {len(gt)} output files but found {len(output_files)}" + ) + + for edge_disp, output_file in zip(gt, output_files): + + dic_data = np.loadtxt(output_file, skiprows=1, delimiter=",") + + x = dic_data[:, 0] + y = dic_data[:, 1] + + u = dic_data[:, 2] + v = dic_data[:, 3] + + cx = (20 + 1019) / 2.0 + cy = (20 + 1519) / 2.0 + + width = 999.0 + height = 1499.0 + + u_gt = 2.0 * edge_disp * (x - cx) / width + v_gt = 2.0 * edge_disp * (y - cy) / height + + try: + np.testing.assert_allclose( + u, + u_gt, + rtol=rtol, + atol=atol, + err_msg=f"Horizontal displacement mismatch ({edge_disp} px)" + ) + + np.testing.assert_allclose( + v, + v_gt, + rtol=rtol, + atol=atol, + err_msg=f"Vertical displacement mismatch ({edge_disp} px)" + ) + + except AssertionError: + + # xs = np.unique(x) + # ys = np.unique(y) + # nx = len(xs) + # ny = len(ys) + # + # err_u = (u - u_gt).reshape(ny, nx) + # err_v = (v - v_gt).reshape(ny, nx) + # err_mag = np.sqrt(err_u**2 + err_v**2) + # + # fig, ax = plt.subplots(1, 3, figsize=(15, 4)) + # + # im = ax[0].imshow( + # err_u, + # origin="lower", + # extent=[xs.min(), xs.max(), ys.min(), ys.max()], + # ) + # ax[0].set_title("u error") + # plt.colorbar(im, ax=ax[0]) + # + # im = ax[1].imshow( + # err_v, + # origin="lower", + # extent=[xs.min(), xs.max(), ys.min(), ys.max()], + # ) + # ax[1].set_title("v error") + # plt.colorbar(im, ax=ax[1]) + # + # im = ax[2].imshow( + # err_mag, + # origin="lower", + # extent=[xs.min(), xs.max(), ys.min(), ys.max()], + # ) + # ax[2].set_title("Error magnitude") + # plt.colorbar(im, ax=ax[2]) + # + # plt.tight_layout() + # plt.savefig(f"hydro_error_{edge_disp:.1f}.png") + # plt.close(fig) + + raise + + for output_file in output_files: + os.remove(output_file) + +# ------------------------------------------------------------------------------ +# SSD +# ------------------------------------------------------------------------------ + +def test_2d_ssd_rigid(): + + dic.calculate_2d( + reference=ref0, + deformed=def0, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=60, + correlation_criteria="SSD", + shape_function="AFFINE", + method="MULTIWINDOW_RG", + output_basepath=test_dir, + output_prefix="test_ssd_rigid_", + ) + + + output_files = os.path.abspath( os.path.join(test_dir, "./test_ssd_rigid_*.csv")) + validate(output_pattern=output_files,gt=u,atol=0.01) + + +# ------------------------------------------------------------------------------ +# NSSD +# ------------------------------------------------------------------------------ + +def test_2d_nssd_scaled_image_rigid(): + + dic.calculate_2d( + reference=ref_arr, + deformed=def_arr_scaled, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=60, + correlation_criteria="NSSD", + shape_function="AFFINE", + method="MULTIWINDOW_RG", + output_basepath=test_dir, + output_prefix="test_2d_nssd_scaled_image_rigid_", + ) + + output_file = os.path.abspath( + os.path.join( + test_dir, + "./test_2d_nssd_scaled_image_rigid_def_img_0000.csv", + ) + ) + + validate(output_pattern=output_file,gt=[0.7],atol=0.01,stereo=False) + + +# ------------------------------------------------------------------------------ +# ZNSSD +# ------------------------------------------------------------------------------ + +def test_2d_znssd_scaled_offset_image_rigid(): + + dic.calculate_2d( + reference=ref_arr, + deformed=def_arr_scaled_offset, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=60, + correlation_criteria="ZNSSD", + shape_function="AFFINE", + method="MULTIWINDOW_RG", + output_basepath=test_dir, + output_prefix="test_2d_znssd_scaled_offset_image_rigid_", + ) + + output_file = os.path.abspath( + os.path.join( + test_dir, + "./test_2d_znssd_scaled_offset_image_rigid_def_img_0000.csv", + ) + ) + + validate(output_pattern=output_file,gt=[0.7],atol=0.01,stereo=False) + + +# ------------------------------------------------------------------------------ +# Raster ZNSSD Affine +# ------------------------------------------------------------------------------ + +def test_2d_image_scan_znssd_affine(): + + dic.calculate_2d( + reference=ref0, + deformed=def0, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=60, + correlation_criteria="ZNSSD", + shape_function="AFFINE", + method="RASTER", + output_basepath=test_dir, + output_prefix="test_2d_image_scan_znssd_affine_", + ) + + validate( + os.path.abspath( + os.path.join( + test_dir, + "./test_2d_image_scan_znssd_affine_*.csv", + ) + ), + u, + atol=0.01 + ) + + +# ------------------------------------------------------------------------------ +# Raster ZNSSD Rigid +# ------------------------------------------------------------------------------ + +def test_2d_image_scan_znssd_rigid(): + + dic.calculate_2d( + reference=ref0, + deformed=def0, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=60, + correlation_criteria="ZNSSD", + shape_function="RIGID", + method="RASTER", + output_basepath=test_dir, + output_prefix="test_2d_image_scan_znssd_rigid_", + ) + + validate( + os.path.abspath( + os.path.join( + test_dir, + "./test_2d_image_scan_znssd_rigid_*.csv", + ) + ), + u, + atol=0.01 + ) + + +# ------------------------------------------------------------------------------ +# Raster NSSD +# ------------------------------------------------------------------------------ + +def test_2d_image_scan_nssd_affine(): + + dic.calculate_2d( + reference=ref0, + deformed=def0, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=60, + correlation_criteria="NSSD", + shape_function="AFFINE", + method="RASTER", + output_basepath=test_dir, + output_prefix="test_2d_image_scan_nssd_affine_", + ) + + validate( + os.path.abspath( + os.path.join( + test_dir, + "./test_2d_image_scan_nssd_affine_*.csv", + ) + ), + u, + atol=0.01 + ) + + +# ------------------------------------------------------------------------------ +# Multiwindow RG +# ------------------------------------------------------------------------------ + +def test_2d_rg_znssd_affine(): + + dic.calculate_2d( + reference=ref0, + deformed=def0, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=60, + correlation_criteria="ZNSSD", + shape_function="AFFINE", + method="MULTIWINDOW_RG", + output_basepath=test_dir, + output_prefix="test_2d_rg_znssd_affine_", + ) + + validate( + os.path.abspath( + os.path.join( + test_dir, + "./test_2d_rg_znssd_affine_*.csv", + ) + ), + u, + atol=0.01 + ) + +# ------------------------------------------------------------------------------ +# Multiwindow RG +# ------------------------------------------------------------------------------ + +def test_2d_rg_znssd_quad(): + + dic.calculate_2d( + reference=ref0, + deformed=def0, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=60, + correlation_criteria="ZNSSD", + shape_function="QUAD", + method="MULTIWINDOW_RG", + output_basepath=test_dir, + output_prefix="test_2d_rg_znssd_quad_", + ) + + validate( + os.path.abspath( + os.path.join( + test_dir, + "./test_2d_rg_znssd_quad_*.csv", + ) + ), + u, + atol=0.01 + ) + +# ------------------------------------------------------------------------------ +# singlewindow RG +# ------------------------------------------------------------------------------ + +def test_2d_singlewindow_znssd_affine(): + + dic.calculate_2d( + reference=ref0, + deformed=def0, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=60, + correlation_criteria="ZNSSD", + shape_function="AFFINE", + method="MULTIWINDOW_RG", + output_basepath=test_dir, + output_prefix="test_2d_rg_znssd_affine_", + ) + + validate( + os.path.abspath( + os.path.join( + test_dir, + "./test_2d_rg_znssd_affine_*.csv", + ) + ), + u, + atol=0.01 + ) + +# ------------------------------------------------------------------------------ +# Large displacement FFT with mutlwindow +# ------------------------------------------------------------------------------ + +def test_2d_multiwindow_fft_large(): + + dic.calculate_2d( + reference=ref0, + deformed=def_large, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=100, + correlation_criteria="ZNSSD", + shape_function="RIGID", + method="MULTIWINDOW", + output_basepath=test_dir, + output_prefix="test_fft_", + ) + + outputs = [ + ("./test_fft_rigid_cam0_frame11.csv", 10.0), + ("./test_fft_rigid_cam0_frame12.csv", 25.0), + ("./test_fft_rigid_cam0_frame13.csv", 50.0), + ] + + for filename, u in outputs: + + output_file = os.path.abspath( + os.path.join(test_dir, filename) + ) + + validate( + output_file, + [u], + atol=0.01, + ) + +# ------------------------------------------------------------------------------ +# Large displacement FFT with mutlwindow +# ------------------------------------------------------------------------------ + +def test_2d_multiwindow_rg_fft_large(): + + dic.calculate_2d( + reference=ref0, + deformed=def_large, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=100, + correlation_criteria="ZNSSD", + shape_function="RIGID", + method="MULTIWINDOW_RG", + output_basepath=test_dir, + output_prefix="test_fft_", + ) + + outputs = [ + ("./test_fft_rigid_cam0_frame11.csv", 10.0), + ("./test_fft_rigid_cam0_frame12.csv", 25.0), + ("./test_fft_rigid_cam0_frame13.csv", 50.0), + ] + + for filename, u in outputs: + + output_file = os.path.abspath( + os.path.join(test_dir, filename) + ) + + validate( + output_file, + [u], + atol=0.01, + ) +# ------------------------------------------------------------------------------ +# Large displacement FFT with singlewindow +# ------------------------------------------------------------------------------ + +def test_2d_singlewindow_fft_large(): + + dic.calculate_2d( + reference=ref0, + deformed=def_large, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=100, + correlation_criteria="ZNSSD", + shape_function="RIGID", + method="SINGLEWINDOW_RG", + output_basepath=test_dir, + output_prefix="test_fft_", + ) + + outputs = [ + ("./test_fft_rigid_cam0_frame11.csv", 10.0), + ("./test_fft_rigid_cam0_frame12.csv", 25.0), + ("./test_fft_rigid_cam0_frame13.csv", 50.0), + ] + + for filename, u in outputs: + + output_file = os.path.abspath( + os.path.join(test_dir, filename) + ) + + validate( + output_file, + [u], + atol=0.01, + ) + + +# ------------------------------------------------------------------------------ +# Multiwindow RG +# ------------------------------------------------------------------------------ + +def test_2d_hydro_rg_znssd_affine(): + + dic.calculate_2d( + reference=ref0_hydro, + deformed=def0_hydro, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=21, + subset_step=10, + max_displacement=2, + correlation_criteria="ZNSSD", + shape_function="AFFINE", + method="MULTIWINDOW_RG", + output_basepath=test_dir, + output_prefix="test_hydro_", + ) + + validate_hydro( + os.path.join( + test_dir, + "test_hydro_*.csv", + ), + u_short, + atol=0.005, + ) + +# ------------------------------------------------------------------------------ +# Multiwindow RG +# ------------------------------------------------------------------------------ + +def test_2d_hydro_rg_znssd_quad(): + + dic.calculate_2d( + reference=ref0_hydro, + deformed=def0_hydro, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=21, + subset_step=10, + max_displacement=2, + correlation_criteria="ZNSSD", + shape_function="QUAD", + method="MULTIWINDOW_RG", + output_basepath=test_dir, + output_prefix="test_hydro_", + ) + + validate_hydro( + os.path.join( + test_dir, + "test_hydro_*.csv", + ), + u_short, + atol=0.008, # more noise for quad + ) + +# ------------------------------------------------------------------------------ +# STEREO +# ------------------------------------------------------------------------------ + +def test_3d_rg_znssd_affine(): + + dic.calculate_3d( + reference=[ref0, ref1], + deformed=[def0, def1], + roi_mask=roi.mask, + calibration=calib_data, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=60, + correlation_criteria="ZNSSD", + shape_function="AFFINE", + method="MULTIWINDOW_RG", + output_basepath=test_dir, + output_prefix="test_3d_rg_znssd_affine_", + ) + + validate( + os.path.abspath( + os.path.join( + test_dir, + "./test_3d_rg_znssd_affine_*.csv", + ) + ), + u, + atol=0.01, + atol_stereo=0.0001, + stereo=True + ) + +def test_3d_rg_znssd_affine_incremental(): + + dic.calculate_3d( + reference=[ref0, ref1], + deformed=[def0, def1], + roi_mask=roi.mask, + calibration=calib_data, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=60, + correlation_criteria="ZNSSD", + shape_function="AFFINE", + incremental=True, + incremental_update_condition="IMAGE", + incremental_update_value=1, + method="MULTIWINDOW_RG", + output_basepath=test_dir, + output_prefix="test_3d_rg_znssd_incremental_affine_", + ) + + validate( + os.path.abspath(os.path.join(test_dir, "./test_3d_rg_znssd_incremental_affine*.csv",)), + gt=u, + stereo=True, + atol=0.01, + atol_stereo=0.0001 + ) + +def test_3d_rg_znssd_quad(): + + dic.calculate_3d( + reference=[ref0, ref1], + deformed=[def0, def1], + roi_mask=roi.mask, + calibration=calib_data, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=60, + correlation_criteria="ZNSSD", + shape_function="QUAD", + method="MULTIWINDOW_RG", + output_basepath=test_dir, + output_prefix="test_3d_rg_znssd_quad_", + ) + + validate( + os.path.abspath(os.path.join(test_dir, "./test_3d_rg_znssd_quad_*.csv",)), + gt=u, + stereo=True, + atol=0.01, + atol_stereo=0.0005) + + +def test_f32_support(): + + np.random.seed(100) + ref_arr = np.random.uniform(0, 200, size=(400,400)) + def_arr = np.roll(ref_arr, 1, axis=1) + def_arr = np.roll(def_arr, -1, axis=0) + + roi = dic.RegionOfInterest(ref_arr) + roi.rect_boundary(10,10,10,10) + + dic.calculate_2d( + reference=ref_arr, + deformed=def_arr, + roi_mask=roi.mask, + seed=[250, 250], + subset_size=31, + subset_step=15, + max_displacement=10, + output_basepath=test_dir, + output_prefix="test_f32_support_", + ) + + validate( + os.path.abspath(os.path.join(test_dir, "./test_f32_support*.csv",)), + gt=[1.0], + stereo=False, + atol=0.001, + atol_stereo=0.00001) diff --git a/tests/dic/test_import.py b/tests/dic/test_import.py deleted file mode 100644 index 008215f12..000000000 --- a/tests/dic/test_import.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -================================================================================ -example: thermocouples on a 2d plate - -pyvale: the python validation engine -license: mit -copyright (c) 2024 the computer aided validation team -================================================================================ -""" -import os -os.environ['OMP_NUM_THREADS'] = '1' - -import numpy as np -import pyvale.dic as dic - -test_dir = os.path.dirname(__file__) - - -def test_dic_data_column_import(): - - filename = "./reference/ref_00_50.csv" - datafile = os.path.abspath(os.path.join(test_dir,filename)) - dicdata = dic.import_2d(data=datafile, binary=False, layout='column', delimiter=",") - - raw_data = np.loadtxt(datafile, delimiter=",", skiprows=1) - assert np.allclose(dicdata.ss_x, raw_data[:, 0]), "Mismatch in ss_x data column" - assert np.allclose(dicdata.ss_y, raw_data[:, 1]), "Mismatch in ss_y data column" - assert np.allclose(dicdata.u, raw_data[:, 2]), "Mismatch in u data column" - assert np.allclose(dicdata.v, raw_data[:, 3]), "Mismatch in v data column" - assert np.allclose(dicdata.mag, raw_data[:, 4]), "Mismatch in mag data column" - assert np.allclose(dicdata.converged, raw_data[:, 5]), "Mismatch in cost data column" - assert np.allclose(dicdata.cost, raw_data[:, 6]), "Mismatch in cost data column" - assert np.allclose(dicdata.ftol, raw_data[:, 7]), "Mismatch in ftol data column" - assert np.allclose(dicdata.xtol, raw_data[:, 8]), "Mismatch in xtol data column" - assert np.allclose(dicdata.niter, raw_data[:, 9]), "Mismatch in niter data column" - - -def test_dic_data_matrix_import(): - - filename = "./reference/ref_00_50.csv" - datafile = os.path.abspath(os.path.join(test_dir,filename)) - dicdata = dic.import_2d(data=datafile, binary=False, layout='matrix', delimiter=",") - - raw_data = np.loadtxt(datafile, delimiter=",", skiprows=1) - - # loop over all x and y values in raw_data and check if they exist in the - # matrix format of the dicdata - for i in range(raw_data.shape[0]): - x = raw_data[i, 0] - y = raw_data[i, 1] - u_val = raw_data[i, 2] - try: - row_idx, col_idx = np.where((dicdata.ss_x == x) & (dicdata.ss_y == y)) - except IndexError: - raise ValueError(f"x={x}, y={y} not found in grid") - - u_matrix_val = dicdata.u[0, row_idx, col_idx] - assert np.isclose(u_matrix_val, u_val), f"Mismatch at (x={x}, y={y}): {u_matrix_val} != {u_val}" diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-all.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-all.npy new file mode 100644 index 000000000..0df46b60d Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-basic-dep.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-basic-dep.npy new file mode 100644 index 000000000..f79759d11 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-basic-gen.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-basic-gen.npy new file mode 100644 index 000000000..c673be89b Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-basic.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-basic.npy new file mode 100644 index 000000000..c673be89b Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-calib.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-calib.npy new file mode 100644 index 000000000..02668467e Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-calib.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-field-dep.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-field-dep.npy new file mode 100644 index 000000000..343e283e1 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-field.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-field.npy new file mode 100644 index 000000000..239b38ef8 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-none.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-none.npy new file mode 100644 index 000000000..8f00ca869 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-sim_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-all.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-all.npy new file mode 100644 index 000000000..ffac55db8 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-basic-dep.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-basic-dep.npy new file mode 100644 index 000000000..bf523e18c Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-basic-gen.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-basic-gen.npy new file mode 100644 index 000000000..40751af19 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-basic.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-basic.npy new file mode 100644 index 000000000..40751af19 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-calib.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-calib.npy new file mode 100644 index 000000000..b080ad22d Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-calib.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-field-dep.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-field-dep.npy new file mode 100644 index 000000000..d1e475c35 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-field.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-field.npy new file mode 100644 index 000000000..1e12de1a9 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-none.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-none.npy new file mode 100644 index 000000000..0161d8d2e Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-grid-22_time-user_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-all.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-all.npy new file mode 100644 index 000000000..8d4cf188b Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-basic-dep.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-basic-dep.npy new file mode 100644 index 000000000..0738de53f Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-basic-gen.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-basic-gen.npy new file mode 100644 index 000000000..adeeebc1b Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-basic.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-basic.npy new file mode 100644 index 000000000..adeeebc1b Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-calib.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-calib.npy new file mode 100644 index 000000000..e5f386f12 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-calib.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-field-dep.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-field-dep.npy new file mode 100644 index 000000000..cf9f1b169 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-field.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-field.npy new file mode 100644 index 000000000..289265d99 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-none.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-none.npy new file mode 100644 index 000000000..c219b1964 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-sim_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-all.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-all.npy new file mode 100644 index 000000000..68167626b Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-basic-dep.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-basic-dep.npy new file mode 100644 index 000000000..40424cf28 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-basic-gen.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-basic-gen.npy new file mode 100644 index 000000000..12b2980be Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-basic.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-basic.npy new file mode 100644 index 000000000..12b2980be Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-calib.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-calib.npy new file mode 100644 index 000000000..b2d4d7123 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-calib.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-field-dep.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-field-dep.npy new file mode 100644 index 000000000..97942ee0c Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-field.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-field.npy new file mode 100644 index 000000000..fb57d973b Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-none.npy b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-none.npy new file mode 100644 index 000000000..968dc94e1 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal2d_analytic_nomesh_pos-line-4_time-user_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-all.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-all.npy new file mode 100644 index 000000000..4c8f63b05 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-basic-dep.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-basic-dep.npy new file mode 100644 index 000000000..c36a8b7b2 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-basic-gen.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-basic-gen.npy new file mode 100644 index 000000000..6bdd664ca Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-basic.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-basic.npy new file mode 100644 index 000000000..6bdd664ca Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-calib.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-calib.npy new file mode 100644 index 000000000..ffb5215f8 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-calib.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-field-dep.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-field-dep.npy new file mode 100644 index 000000000..6fd2606a1 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-field.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-field.npy new file mode 100644 index 000000000..226647615 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-none.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-none.npy new file mode 100644 index 000000000..1aa25729c Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-sim_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-all.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-all.npy new file mode 100644 index 000000000..ac3cf2350 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-basic-dep.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-basic-dep.npy new file mode 100644 index 000000000..1e64ad37a Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-basic-gen.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-basic-gen.npy new file mode 100644 index 000000000..30af2f0f0 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-basic.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-basic.npy new file mode 100644 index 000000000..30af2f0f0 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-calib.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-calib.npy new file mode 100644 index 000000000..8a0fd1034 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-calib.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-field-dep.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-field-dep.npy new file mode 100644 index 000000000..0290c8f5c Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-field.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-field.npy new file mode 100644 index 000000000..460d31878 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-none.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-none.npy new file mode 100644 index 000000000..f0cde73d9 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-xy_time-user_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-all.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-all.npy new file mode 100644 index 000000000..c5dceffd0 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-basic-dep.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-basic-dep.npy new file mode 100644 index 000000000..c36a8b7b2 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-basic-gen.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-basic-gen.npy new file mode 100644 index 000000000..f523bd104 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-basic.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-basic.npy new file mode 100644 index 000000000..f523bd104 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-calib.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-calib.npy new file mode 100644 index 000000000..50215b0de Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-calib.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-field-dep.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-field-dep.npy new file mode 100644 index 000000000..1ddd61576 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-field.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-field.npy new file mode 100644 index 000000000..013b74e59 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-none.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-none.npy new file mode 100644 index 000000000..acc76848b Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-sim_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-all.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-all.npy new file mode 100644 index 000000000..e6aa970c2 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-basic-dep.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-basic-dep.npy new file mode 100644 index 000000000..1e64ad37a Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-basic-gen.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-basic-gen.npy new file mode 100644 index 000000000..2e12760df Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-basic.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-basic.npy new file mode 100644 index 000000000..2e12760df Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-calib.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-calib.npy new file mode 100644 index 000000000..635c60ab4 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-calib.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-field-dep.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-field-dep.npy new file mode 100644 index 000000000..f7b92dede Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-field.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-field.npy new file mode 100644 index 000000000..d5ecc959c Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-none.npy b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-none.npy new file mode 100644 index 000000000..a250dc564 Binary files /dev/null and b/tests/sensorsim/gold/macos/scal3d_nomesh_pos-line-y-yz_time-user_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-all.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-all.npy new file mode 100644 index 000000000..b90411dd5 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-basic-dep.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-basic-dep.npy new file mode 100644 index 000000000..3e7cd944d Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-basic-gen.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-basic-gen.npy new file mode 100644 index 000000000..74aaad50f Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-basic.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-basic.npy new file mode 100644 index 000000000..74aaad50f Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-field-dep.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-field-dep.npy new file mode 100644 index 000000000..b6591fa0e Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-field.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-field.npy new file mode 100644 index 000000000..efb691318 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-none.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-none.npy new file mode 100644 index 000000000..f187a306f Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-sim_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-all.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-all.npy new file mode 100644 index 000000000..bee980224 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-basic-dep.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-basic-dep.npy new file mode 100644 index 000000000..e8d7c8302 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-basic-gen.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-basic-gen.npy new file mode 100644 index 000000000..b37907829 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-basic.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-basic.npy new file mode 100644 index 000000000..b37907829 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-field-dep.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-field-dep.npy new file mode 100644 index 000000000..3cc3963ef Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-field.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-field.npy new file mode 100644 index 000000000..6466e5a94 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-none.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-none.npy new file mode 100644 index 000000000..7d5351a7b Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-grid-23_time-user_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-all.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-all.npy new file mode 100644 index 000000000..b012bc295 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-basic-dep.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-basic-dep.npy new file mode 100644 index 000000000..9f8cdb7aa Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-basic-gen.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-basic-gen.npy new file mode 100644 index 000000000..9b923c70e Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-basic.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-basic.npy new file mode 100644 index 000000000..9b923c70e Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-field-dep.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-field-dep.npy new file mode 100644 index 000000000..2c693ccfa Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-field.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-field.npy new file mode 100644 index 000000000..6a1452874 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-none.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-none.npy new file mode 100644 index 000000000..ed722def1 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-sim_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-all.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-all.npy new file mode 100644 index 000000000..32ddd3838 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-basic-dep.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-basic-dep.npy new file mode 100644 index 000000000..d395c7a5d Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-basic-gen.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-basic-gen.npy new file mode 100644 index 000000000..77f7017c7 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-basic.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-basic.npy new file mode 100644 index 000000000..77f7017c7 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-field-dep.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-field-dep.npy new file mode 100644 index 000000000..879f5ebf9 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-field.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-field.npy new file mode 100644 index 000000000..c81fa55fd Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-none.npy b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-none.npy new file mode 100644 index 000000000..d95804cb6 Binary files /dev/null and b/tests/sensorsim/gold/macos/tens2d_analytic_nomesh_pos-line-4_time-user_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-all.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-all.npy new file mode 100644 index 000000000..7b86ba304 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-basic-dep.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-basic-dep.npy new file mode 100644 index 000000000..9bad92931 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-basic-gen.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-basic-gen.npy new file mode 100644 index 000000000..68345a9fd Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-basic.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-basic.npy new file mode 100644 index 000000000..68345a9fd Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-field-dep.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-field-dep.npy new file mode 100644 index 000000000..63e620b04 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-field.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-field.npy new file mode 100644 index 000000000..db5c16259 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-none.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-none.npy new file mode 100644 index 000000000..46eb99ad3 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-sim_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-all.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-all.npy new file mode 100644 index 000000000..7335801d8 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-basic-dep.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-basic-dep.npy new file mode 100644 index 000000000..b0c2b52ce Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-basic-gen.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-basic-gen.npy new file mode 100644 index 000000000..d53e31787 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-basic.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-basic.npy new file mode 100644 index 000000000..d53e31787 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-field-dep.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-field-dep.npy new file mode 100644 index 000000000..403d76723 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-field.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-field.npy new file mode 100644 index 000000000..e67349d18 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-none.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-none.npy new file mode 100644 index 000000000..01ef222e7 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-grid-23_time-user_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-all.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-all.npy new file mode 100644 index 000000000..4d0cb6929 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-basic-dep.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-basic-dep.npy new file mode 100644 index 000000000..9d74043b5 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-basic-gen.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-basic-gen.npy new file mode 100644 index 000000000..9c3a7dc66 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-basic.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-basic.npy new file mode 100644 index 000000000..9c3a7dc66 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-field-dep.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-field-dep.npy new file mode 100644 index 000000000..2e0a5c9c2 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-field.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-field.npy new file mode 100644 index 000000000..df26c628d Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-none.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-none.npy new file mode 100644 index 000000000..1fa6466da Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-sim_err-none.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-all.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-all.npy new file mode 100644 index 000000000..570fcf79c Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-all.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-basic-dep.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-basic-dep.npy new file mode 100644 index 000000000..8a7a73ee5 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-basic-dep.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-basic-gen.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-basic-gen.npy new file mode 100644 index 000000000..5df5d1d42 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-basic-gen.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-basic.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-basic.npy new file mode 100644 index 000000000..5df5d1d42 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-basic.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-field-dep.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-field-dep.npy new file mode 100644 index 000000000..3452ff048 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-field-dep.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-field.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-field.npy new file mode 100644 index 000000000..c0a389129 Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-field.npy differ diff --git a/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-none.npy b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-none.npy new file mode 100644 index 000000000..3930cfa7a Binary files /dev/null and b/tests/sensorsim/gold/macos/vec2d_analytic_nomesh_pos-line-4_time-user_err-none.npy differ diff --git a/tests/strain/calib.txt b/tests/strain/calib.txt new file mode 100644 index 000000000..1bb2899ae --- /dev/null +++ b/tests/strain/calib.txt @@ -0,0 +1,26 @@ +cam0_fx_px,14492.753623188406 +cam0_fy_px,14492.753623188406 +cam0_fs_px,0.00000000000 +cam0_cx_px,520.000000000000 +cam0_cy_px,770.000000000000 +cam0_k1,0.0000000000 +cam0_k2,0.0000000000 +cam0_p1,0.0000000000 +cam0_p2,0.0000000000 +cam0_k3,0.0000000000 +cam1_fx_px,14492.753623188406 +cam1_fy_px,14492.753623188406 +cam1_fs_px,0.0000000000 +cam1_cx_px,520.000000000000 +cam1_cy_px,770.000000000000 +cam1_k1,0.0000000000 +cam1_k2,0.0000000000 +cam1_p1,0.0000000000 +cam1_p2,0.0000000000 +cam1_k3,0.0000000000 +tx_mm,49.5681367139 +ty_mm,0.000000000000 +tz_mm,-8.7401998861 +theta_deg,0.000000000000 +phi_deg,20.000000000000 +psi_deg,0.000000000000 diff --git a/tests/strain/roi.yaml b/tests/strain/roi.yaml new file mode 100644 index 000000000..467c5b9c5 --- /dev/null +++ b/tests/strain/roi.yaml @@ -0,0 +1,24 @@ +- type: RectROI + pos: + - 30.837242290658914 + - 31.606027336089696 + size: + - 980.181163810901 + - 1482.7271790648015 + add: true +- type: CircleROI + pos: + - 519.9337896112705 + - 771.9337896112705 + size: + - 265.8675792225411 + - 265.8675792225411 + add: false +- type: SeedROI + pos: + - 297 + - 376 + size: + - 21 + - 21 + add: true diff --git a/tests/strain/test_strain.py b/tests/strain/test_strain.py index ba7bb243e..771e40464 100644 --- a/tests/strain/test_strain.py +++ b/tests/strain/test_strain.py @@ -5,10 +5,16 @@ import scipy.linalg import os import glob +from pathlib import Path #pyvale stuff import pyvale.dic as dic +import pyvale.calib as calib import pyvale.strain as strain +import pyvale.dataset as dataset + + +TEST_DATA_DIR = Path(__file__).parent / "test" def generate_affine_displacement_grid(F, nx=100, ny=100): @@ -76,28 +82,36 @@ def test_strain_deformations(strain_formulation, deformation_type, F): # Generate displacement field X, Y, Ux, Uy = generate_affine_displacement_grid(F) - input_data = dic.Results(X, Y, Ux, Uy) + TEST_DATA_DIR.mkdir(exist_ok=True) + np.savetxt(TEST_DATA_DIR / f"u_{strain_formulation}_{deformation_type}.txt", Ux[0]) + np.savetxt(TEST_DATA_DIR / f"v_{strain_formulation}_{deformation_type}.txt", Uy[0]) + + input_data = dic.Results(ss_x=X, ss_y=Y, u_px=Ux, v_px=Uy) # Compute strain strain_results = strain.calculate_2d( data=input_data, window_size=5, - window_element=4, + window_element=9, strain_formulation=strain_formulation, - output_prefix=f"strain_{strain_formulation}_{deformation_type}_" + output_prefix=f"strain_{strain_formulation}_{deformation_type}_", + debug_level=2 ) # Analytic reference strain expected = reference_strain(F, strain_formulation) + print(F.shape) + print(expected.shape) + strainresults = strain.import_2d(f"./strain_{strain_formulation}_{deformation_type}_*.csv") # Map of deformation gradient and strain components checks = { - "def_xx": F[0,0], - "def_xy": F[0,1], - "def_yx": F[1,0], - "def_yy": F[1,1], + "def_00": F[0,0], + "def_01": F[0,1], + "def_10": F[1,0], + "def_11": F[1,1], "eps_xx": expected[0,0], "eps_xy": expected[0,1], "eps_yx": expected[1,0], @@ -117,3 +131,86 @@ def test_strain_deformations(strain_formulation, deformation_type, F): for file_path in glob.glob(f"./strain_{strain_formulation}_{deformation_type}_*.csv"): os.remove(file_path) + + +def run_strain_test(window_element): + ref0 = dataset.dic_plate_with_hole_cam0_ref() + ref1 = dataset.dic_plate_with_hole_cam1_ref() + def0 = dataset.dic_plate_with_hole_cam0_def() + def1 = dataset.dic_plate_with_hole_cam1_def() + + roi = dic.RegionOfInterest(ref0) + roi.read_yaml(Path(__file__).parent / "roi.yaml") + + calibration = calib.loadtxt(Path(__file__).parent / "calib.txt") + + common = dict( + roi_mask=roi.mask, + seed=roi.seed, + subset_size=21, + subset_step=10, + max_displacement=10, + output_basepath="./", + output_delimiter=",", + ) + + dic.calculate_2d( + reference=ref0, + deformed=def0, + output_prefix="dic_2d_", + **common, + ) + + strain.calculate_2d( + data="dic_2d_*csv", + window_size=5, + window_element=window_element, + output_basepath="./", + output_prefix="strain_2d_", + strain_formulation="ALMANSI", + ) + + dic.calculate_3d( + reference=[ref0, ref1], + deformed=[def0, def1], + calibration=calibration, + output_prefix="dic_3d_", + **common, + ) + + strain.calculate_3d( + data="dic_3d_*csv", + window_size=5, + window_element=window_element, + output_basepath="./", + output_prefix="strain_3d_", + strain_formulation="ALMANSI", + ) + + return ( + strain.import_2d("strain_2d_*"), + strain.import_3d("strain_3d_*"), + ) + + +@pytest.mark.parametrize("window_element", [4, 9]) +def test_strain_3d(window_element): + strain_2d, strain_3d = run_strain_test(window_element) + + for field, atol in [ + ("eps_xx", 4e-5), + ("eps_xy", 8e-5), + ("eps_yy", 2e-4), + ]: + np.testing.assert_allclose( + getattr(strain_2d, field)[1], + getattr(strain_3d, field)[1], + rtol=0.0, + atol=atol, + err_msg=f"mismatch for {field}", + ) + + + output_files = sorted(glob.glob("*dic*.csv")) + for files in output_files: + os.remove(files)