using System; using System.Runtime.InteropServices; using System.Text; using CeresSharp.Exceptions; namespace CeresSharp.Native; /// /// P/Invoke declarations for Ceres Wrapper C API. /// This file contains all native function declarations for the Ceres Solver wrapper. /// internal static partial class CeresNative { private const string LibraryName = "libceres_wrapper.so"; // ============================================================================ // Error Handling // ============================================================================ [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_get_error_message(CeresErrorCode errorCode); // ============================================================================ // Library Information and Version // ============================================================================ [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_get_version_string(); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_get_version_major(); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_get_version_minor(); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_get_version_revision(); [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern CeresErrorCode ceres_wrapper_get_detailed_version_string( StringBuilder buffer, int bufferSize); [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern CeresErrorCode ceres_wrapper_get_library_info( StringBuilder buffer, int bufferSize); // ============================================================================ // Official C API (for Problem creation/destruction) // Note: These may need to be imported from libceres.so instead // ============================================================================ [LibraryImport(LibraryName)] internal static partial IntPtr ceres_create_problem(); [LibraryImport(LibraryName)] internal static partial void ceres_free_problem(IntPtr problem); // ============================================================================ // Solver Options // ============================================================================ [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_solver_options(); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_solver_options(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_linear_solver_type( IntPtr options, int linearSolverType); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_linear_solver_type(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_minimizer_type( IntPtr options, int minimizerType); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_minimizer_type(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_max_num_iterations( IntPtr options, int maxNumIterations); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_max_num_iterations(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_num_threads( IntPtr options, int numThreads); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_num_threads(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_function_tolerance( IntPtr options, double functionTolerance); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_function_tolerance(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_gradient_tolerance( IntPtr options, double gradientTolerance); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_gradient_tolerance(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_parameter_tolerance( IntPtr options, double parameterTolerance); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_parameter_tolerance(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_initial_trust_region_radius( IntPtr options, double radius); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_initial_trust_region_radius(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_max_trust_region_radius( IntPtr options, double radius); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_max_trust_region_radius(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_min_trust_region_radius( IntPtr options, double radius); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_min_trust_region_radius(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_preconditioner_type( IntPtr options, int preconditionerType); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_preconditioner_type(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_trust_region_strategy_type( IntPtr options, int trustRegionStrategyType); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_trust_region_strategy_type(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_dogleg_type( IntPtr options, int doglegType); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_dogleg_type(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_use_nonmonotonic_steps( IntPtr options, int useNonmonotonicSteps); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_use_nonmonotonic_steps(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_max_consecutive_nonmonotonic_steps( IntPtr options, int maxSteps); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_max_consecutive_nonmonotonic_steps(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_max_num_consecutive_invalid_steps( IntPtr options, int maxSteps); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_max_num_consecutive_invalid_steps(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_min_relative_decrease( IntPtr options, double minRelativeDecrease); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_min_relative_decrease(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_logging_type( IntPtr options, int loggingType); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_logging_type(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_minimizer_progress_to_stdout( IntPtr options, int minimizerProgressToStdout); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_minimizer_progress_to_stdout(IntPtr options); // Note: Using DllImport instead of LibraryImport because StringBuilder is not supported by source-generated P/Invokes [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern int ceres_wrapper_solver_options_is_valid( IntPtr options, StringBuilder errorMessage, int errorMessageSize); // Line search options [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_line_search_type( IntPtr options, int lineSearchType); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_line_search_type(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_line_search_direction_type( IntPtr options, int lineSearchDirectionType); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_line_search_direction_type(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_nonlinear_conjugate_gradient_type( IntPtr options, int ncgType); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_nonlinear_conjugate_gradient_type(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_max_lbfgs_rank( IntPtr options, int maxLbfgsRank); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_max_lbfgs_rank(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_max_line_search_step_contraction( IntPtr options, double maxStepContraction); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_max_line_search_step_contraction(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_min_line_search_step_contraction( IntPtr options, double minStepContraction); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_min_line_search_step_contraction(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_max_num_line_search_step_size_iterations( IntPtr options, int maxIterations); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_max_num_line_search_step_size_iterations(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_max_num_line_search_direction_restarts( IntPtr options, int maxRestarts); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_max_num_line_search_direction_restarts(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_line_search_sufficient_function_decrease( IntPtr options, double sufficientDecrease); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_line_search_sufficient_function_decrease(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_line_search_sufficient_curvature_decrease( IntPtr options, double sufficientCurvatureDecrease); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_line_search_sufficient_curvature_decrease(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_max_line_search_step_expansion( IntPtr options, double maxStepExpansion); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_max_line_search_step_expansion(IntPtr options); // Trust region parameters [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_max_lm_diagonal( IntPtr options, double maxLmDiagonal); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_max_lm_diagonal(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_min_lm_diagonal( IntPtr options, double minLmDiagonal); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_min_lm_diagonal(IntPtr options); // Linear solver options [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_sparse_linear_algebra_library_type( IntPtr options, int sparseLibraryType); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_sparse_linear_algebra_library_type(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_dense_linear_algebra_library_type( IntPtr options, int denseLibraryType); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_dense_linear_algebra_library_type(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_max_linear_solver_iterations( IntPtr options, int maxIterations); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_max_linear_solver_iterations(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_min_linear_solver_iterations( IntPtr options, int minIterations); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_min_linear_solver_iterations(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_linear_solver_tolerance( IntPtr options, double tolerance); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_linear_solver_tolerance(IntPtr options); // Inner iterations [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_use_inner_iterations( IntPtr options, int useInnerIterations); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_use_inner_iterations(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_inner_iteration_tolerance( IntPtr options, double tolerance); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_inner_iteration_tolerance(IntPtr options); // Timing [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_max_solver_time_in_seconds( IntPtr options, double maxTime); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_options_get_max_solver_time_in_seconds(IntPtr options); // Checkpointing [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_update_state_every_iteration( IntPtr options, int updateEveryIteration); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_options_get_update_state_every_iteration(IntPtr options); // ============================================================================ // Solver Summary // ============================================================================ [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_solver_summary(); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_solver_summary(IntPtr summary); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_summary_get_termination_type(IntPtr summary); // Note: Using DllImport instead of LibraryImport because StringBuilder is not supported by source-generated P/Invokes [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern void ceres_wrapper_solver_summary_get_message( IntPtr summary, StringBuilder message, int messageSize); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_summary_get_initial_cost(IntPtr summary); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_summary_get_final_cost(IntPtr summary); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_summary_get_iterations(IntPtr summary); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_summary_get_num_successful_steps(IntPtr summary); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_summary_get_num_unsuccessful_steps(IntPtr summary); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_summary_get_num_inner_iteration_steps(IntPtr summary); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_summary_get_total_time_in_seconds(IntPtr summary); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_summary_get_preprocessor_time_in_seconds(IntPtr summary); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_summary_get_minimizer_time_in_seconds(IntPtr summary); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_summary_get_postprocessor_time_in_seconds(IntPtr summary); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_summary_get_linear_solver_time_in_seconds(IntPtr summary); // Note: Using DllImport instead of LibraryImport because StringBuilder is not supported by source-generated P/Invokes [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern void ceres_wrapper_solver_summary_get_full_report( IntPtr summary, StringBuilder report, int reportSize); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_summary_get_num_parameter_blocks(IntPtr summary); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_summary_get_num_parameters(IntPtr summary); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_summary_get_num_effective_parameters(IntPtr summary); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_summary_get_num_residual_blocks(IntPtr summary); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_solver_summary_get_num_residuals(IntPtr summary); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_solver_summary_get_cost_change(IntPtr summary); // ============================================================================ // Solve // ============================================================================ // Note: Using DllImport instead of LibraryImport because StringBuilder is not supported by source-generated P/Invokes [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern CeresErrorCode ceres_wrapper_solve( IntPtr problem, IntPtr options, IntPtr summary, StringBuilder errorMessage, int errorMessageSize); // ============================================================================ // Problem Operations // ============================================================================ // Note: Using DllImport instead of LibraryImport because StringBuilder is not supported by source-generated P/Invokes [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern CeresErrorCode ceres_wrapper_problem_add_parameter_block( IntPtr problem, IntPtr parameters, int size, StringBuilder errorMessage, int errorMessageSize); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_problem_set_parameter_block_constant( IntPtr problem, IntPtr parameters); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_problem_set_parameter_block_variable( IntPtr problem, IntPtr parameters); // Note: Using DllImport instead of LibraryImport because StringBuilder is not supported by source-generated P/Invokes [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] internal static extern CeresErrorCode ceres_wrapper_problem_remove_parameter_block( IntPtr problem, IntPtr parameters, StringBuilder errorMessage, int errorMessageSize); // Note: Using DllImport instead of LibraryImport because StringBuilder is not supported by source-generated P/Invokes [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] internal static extern CeresErrorCode ceres_wrapper_problem_remove_residual_block( IntPtr problem, IntPtr residualBlockId, StringBuilder errorMessage, int errorMessageSize); // Note: Using DllImport instead of LibraryImport because StringBuilder is not supported by source-generated P/Invokes [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] internal static extern CeresErrorCode ceres_wrapper_problem_add_residual_block( IntPtr problem, IntPtr costFunction, IntPtr lossFunction, IntPtr parameterBlocks, int numParameterBlocks, out IntPtr residualBlockId, StringBuilder errorMessage, int errorMessageSize); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_num_parameter_blocks(IntPtr problem); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_num_residual_blocks(IntPtr problem); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_num_parameters(IntPtr problem); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_num_residuals(IntPtr problem); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_has_parameter_block( IntPtr problem, IntPtr parameters); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_is_parameter_block_constant( IntPtr problem, IntPtr parameters); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_get_parameter_block_size( IntPtr problem, IntPtr parameters); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_get_parameter_block_tangent_size( IntPtr problem, IntPtr parameters); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_has_manifold( IntPtr problem, IntPtr parameters); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_problem_get_manifold( IntPtr problem, IntPtr parameters); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_problem_set_manifold( IntPtr problem, IntPtr parameters, IntPtr manifold); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_problem_set_parameter_lower_bound( IntPtr problem, IntPtr parameters, int index, double lowerBound); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_problem_set_parameter_upper_bound( IntPtr problem, IntPtr parameters, int index, double upperBound); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_problem_get_parameter_lower_bound( IntPtr problem, IntPtr parameters, int index); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_problem_get_parameter_upper_bound( IntPtr problem, IntPtr parameters, int index); // ============================================================================ // Loss Functions // ============================================================================ [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_huber_loss(double a); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_trivial_loss(); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_cauchy_loss(double a); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_softl1_loss(double a); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_arctan_loss(double a); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_tolerant_loss(double a, double b); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_loss_function(IntPtr loss); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_composed_loss( IntPtr f, int ownershipF, IntPtr g, int ownershipG); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_scaled_loss( IntPtr rho, double a, int ownership); // ============================================================================ // Manifolds // ============================================================================ [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_quaternion_manifold(); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_sphere_manifold(int dimension); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_line_manifold(int dimension); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_euclidean_manifold(int dimension); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_subset_manifold( IntPtr constantSubset, int constantSubsetSize, int ambientSize); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_product_manifold( IntPtr manifolds, int numManifolds); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_manifold(IntPtr manifold); // AutoDiff Manifold callbacks [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int CeresAutoDiffManifoldPlus( IntPtr userData, IntPtr x, IntPtr delta, IntPtr xPlusDelta); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int CeresAutoDiffManifoldMinus( IntPtr userData, IntPtr y, IntPtr x, IntPtr yMinusX); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_autodiff_manifold( int ambientSize, int tangentSize, CeresAutoDiffManifoldPlus plus, CeresAutoDiffManifoldMinus minus, IntPtr userData); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_autodiff_manifold(IntPtr manifold); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_manifold_ambient_size(IntPtr manifold); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_manifold_tangent_size(IntPtr manifold); // ============================================================================ // Interpolators // ============================================================================ [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_bicubic_interpolator( IntPtr data, int rows, int cols); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_bicubic_interpolator(IntPtr interpolator); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_bicubic_interpolator_evaluate( IntPtr interpolator, double x, double y, IntPtr value, IntPtr gradientX, IntPtr gradientY); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_cubic_interpolator( IntPtr data, int numValues); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_cubic_interpolator(IntPtr interpolator); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_cubic_interpolator_evaluate( IntPtr interpolator, double x, IntPtr value, IntPtr gradient); // ============================================================================ // Cost Functions // ============================================================================ // Callback delegate for AutoDiff cost function [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int CeresAutoDiffCostFunctionCallback( IntPtr userData, IntPtr parameters, IntPtr residuals); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_autodiff_cost_function( CeresAutoDiffCostFunctionCallback callback, IntPtr userData, int numResiduals, int numParameterBlocks, IntPtr parameterBlockSizes); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_dynamic_autodiff_cost_function( CeresAutoDiffCostFunctionCallback callback, IntPtr userData, int numResiduals, int numParameterBlocks, IntPtr parameterBlockSizes); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_autodiff_cost_function(IntPtr costFunction); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_dynamic_autodiff_cost_function(IntPtr costFunction); // Callback delegate for NumericDiff cost function [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int CeresNumericDiffCostFunctionCallback( IntPtr parameters, IntPtr residuals, IntPtr userData); // Note: NumericDiff uses a struct, we'll need to marshal it manually [StructLayout(LayoutKind.Sequential)] internal struct CeresNumericDiffOptions { public int numResiduals; public int numParameterBlocks; public IntPtr parameterBlockSizes; public CeresNumericDiffCostFunctionCallback callback; public IntPtr userData; } // Note: Using DllImport instead of LibraryImport because CeresNumericDiffOptions contains a delegate // which is not supported by source-generated P/Invokes [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr ceres_wrapper_create_numeric_diff_cost_function( ref CeresNumericDiffOptions options, int method); [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern IntPtr ceres_wrapper_create_dynamic_numeric_diff_cost_function( ref CeresNumericDiffOptions options, int method); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_numeric_diff_cost_function(IntPtr costFunction); // ============================================================================ // Problem Options // ============================================================================ [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_problem_options(); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_problem_options(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_problem_options_set_cost_function_ownership( IntPtr options, int ownership); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_options_get_cost_function_ownership(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_problem_options_set_loss_function_ownership( IntPtr options, int ownership); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_options_get_loss_function_ownership(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_problem_options_set_manifold_ownership( IntPtr options, int ownership); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_options_get_manifold_ownership(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_problem_options_set_enable_fast_removal( IntPtr options, int enable); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_options_get_enable_fast_removal(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_problem_options_set_disable_all_safety_checks( IntPtr options, int disable); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_problem_options_get_disable_all_safety_checks(IntPtr options); // Evaluation callback delegate [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate void CeresEvaluationCallback( IntPtr userData, int numResiduals, int numParameterBlocks, IntPtr parameterBlockSizes); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_problem_options_set_evaluation_callback( IntPtr options, CeresEvaluationCallback callback, IntPtr userData); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_problem_with_options(IntPtr options); // ============================================================================ // Iteration Callback // ============================================================================ [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal delegate int CeresIterationCallback( IntPtr userData, IntPtr summary); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_solver_options_set_iteration_callback( IntPtr options, CeresIterationCallback callback, IntPtr userData); // ============================================================================ // Covariance // ============================================================================ [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_covariance_options(); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_covariance_options(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_covariance_options_set_num_threads( IntPtr options, int numThreads); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_covariance_options_get_num_threads(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_covariance_options_set_sparse_linear_algebra_library_type( IntPtr options, int libraryType); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_covariance_options_get_sparse_linear_algebra_library_type(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_covariance_options_set_algorithm_type( IntPtr options, int algorithmType); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_covariance_options_get_algorithm_type(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_covariance_options_set_min_reciprocal_condition_number( IntPtr options, double minReciprocalConditionNumber); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_covariance_options_get_min_reciprocal_condition_number(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_covariance_options_set_null_space_rank( IntPtr options, int nullSpaceRank); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_covariance_options_get_null_space_rank(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_covariance_options_set_apply_loss_function( IntPtr options, int applyLossFunction); [LibraryImport(LibraryName)] internal static partial int ceres_wrapper_covariance_options_get_apply_loss_function(IntPtr options); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_covariance(); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_covariance_with_options(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_covariance(IntPtr covariance); // Note: Using DllImport instead of LibraryImport because StringBuilder is not supported by source-generated P/Invokes [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern CeresErrorCode ceres_wrapper_covariance_compute( IntPtr covariance, IntPtr problem, IntPtr options, IntPtr parameterBlocks, int numParameterBlocks, StringBuilder errorMessage, int errorMessageSize); // Note: Using DllImport instead of LibraryImport because StringBuilder is not supported by source-generated P/Invokes [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern CeresErrorCode ceres_wrapper_covariance_get_covariance_block( IntPtr covariance, IntPtr parameterBlock1, IntPtr parameterBlock2, IntPtr covarianceBlock, StringBuilder errorMessage, int errorMessageSize); // Note: Using DllImport instead of LibraryImport because StringBuilder is not supported by source-generated P/Invokes [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern CeresErrorCode ceres_wrapper_covariance_get_covariance_matrix( IntPtr covariance, IntPtr parameterBlocks, int numParameterBlocks, IntPtr covarianceMatrix, StringBuilder errorMessage, int errorMessageSize); // ============================================================================ // Gradient Checker // ============================================================================ [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_gradient_checker_options(); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_gradient_checker_options(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_gradient_checker_options_set_gradient_check_relative_precision( IntPtr options, double precision); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_gradient_checker_options_get_gradient_check_relative_precision(IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_gradient_checker_options_set_gradient_check_numeric_derivative_relative_step_size( IntPtr options, double stepSize); [LibraryImport(LibraryName)] internal static partial double ceres_wrapper_gradient_checker_options_get_gradient_check_numeric_derivative_relative_step_size(IntPtr options); [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_gradient_checker( IntPtr costFunction, IntPtr manifolds, int numManifolds, IntPtr options); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_gradient_checker(IntPtr checker); // Note: Using DllImport instead of LibraryImport because StringBuilder is not supported by source-generated P/Invokes [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl)] internal static extern CeresErrorCode ceres_wrapper_gradient_checker_probe( IntPtr checker, IntPtr parameters, double relativePrecision, StringBuilder errorMessage, int errorMessageSize); // ============================================================================ // Context // ============================================================================ [LibraryImport(LibraryName)] internal static partial IntPtr ceres_wrapper_create_context(); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_free_context(IntPtr context); [LibraryImport(LibraryName)] internal static partial void ceres_wrapper_problem_options_set_context( IntPtr options, IntPtr context); // ============================================================================ // Helper Methods // ============================================================================ /// /// Checks error code and throws exception if not successful. /// internal static void CheckError(CeresErrorCode errorCode) { if (errorCode != CeresErrorCode.Success) { var errorMessagePtr = ceres_wrapper_get_error_message(errorCode); var errorMessage = errorMessagePtr != IntPtr.Zero ? Marshal.PtrToStringAnsi(errorMessagePtr) : $"Unknown error code: {errorCode}"; throw new CeresException(errorCode, errorMessage); } } }