Files
BQP/srcs/RobotNet10/RobotApp/Communication/CeresSharp/IMPLEMENTATION_PROGRESS.md
2026-07-13 09:25:40 +07:00

22 KiB

CeresSharp Implementation Progress

📋 Tổng Quan

Mục tiêu: Implement C# wrapper library cho Ceres Solver 2.2.0 thông qua C API (libceres_wrapper.so)

Target Framework: .NET 10.0
Platform: Linux only
API Style: High-level API với classes/objects giống C++ API của Ceres

📊 Progress Tracking

Overall Progress: 100% (15/15 modules)

Module Status Progress Notes
Core Foundation Complete 100% Error handling , enums , constants
Constants Complete 100% Default values , mathematical constants
SafeHandles Complete 100% All SafeHandles implemented
Problem Complete 100% Core problem operations
SolverOptions Complete 100% Solver configuration
SolverSummary Complete 100% Solver results
Cost Functions Complete 100% AutoDiff, NumericDiff , memory management , validation
Loss Functions Complete 100% All loss functions
Manifolds Complete 100% All manifolds
Interpolators Complete 100% BiCubic, Cubic
Problem Options Complete 100% Advanced problem config
Parameter Bounds Complete 100% Lower/upper bounds
Covariance Complete 100% Covariance estimation
Gradient Checker Complete 100% Gradient validation
Context Complete 100% Performance optimization
Callbacks Complete 100% Iteration callback , evaluation callback , memory management

🏗️ Architecture

Design Principles

  1. High-level API: Classes/objects giống C++ API của Ceres
  2. Exception-based: Tất cả errors được wrap trong exceptions
  3. Safe Handles: IDisposable pattern với SafeHandle
  4. Delegates: Callbacks sử dụng delegates với GCHandle pinning
  5. Type Safety: Strong typing, nullable reference types

Project Structure

CeresSharp/
├── Core/
│   ├── Problem.cs
│   ├── SolverOptions.cs
│   ├── SolverSummary.cs
│   ├── CostFunction.cs
│   ├── LossFunction.cs
│   ├── Manifold.cs
│   ├── AutoDiffManifold.cs (⭐ NEW)
│   ├── BiCubicInterpolator.cs
│   └── CubicInterpolator.cs
├── Advanced/
│   ├── ProblemOptions.cs
│   ├── Covariance.cs
│   ├── GradientChecker.cs
│   └── Context.cs
├── Native/
│   ├── CeresNative.cs (P/Invoke declarations)
│   └── SafeHandles/
│       ├── ProblemHandle.cs
│       ├── SolverOptionsHandle.cs
│       └── ...
├── Exceptions/
│   └── CeresException.cs
├── Enums/
│   ├── LinearSolverType.cs
│   ├── MinimizerType.cs
│   └── ...
└── IMPLEMENTATION_PROGRESS.md (this file)

📝 Implementation Details

1. Core Foundation (100%)

Status: Complete

Tasks:

  • Error codes enum (CeresErrorCode)
  • Custom exceptions (CeresException)
  • Core enums (LinearSolverType, MinimizerType, TerminationType, PreconditionerType, NumericDiffMethod)
  • Additional enums (TrustRegionStrategyType, DoglegType, LoggingType, LineSearchType, LineSearchDirectionType, NonlinearConjugateGradientType, SparseLinearAlgebraLibraryType, DenseLinearAlgebraLibraryType, CovarianceAlgorithmType)
  • Error message mapping (via ceres_wrapper_get_error_message)

Files:

  • Exceptions/CeresException.cs - Custom exception với error codes
  • Enums/LinearSolverType.cs - 9 solver types
  • Enums/MinimizerType.cs - Trust Region và Line Search
  • Enums/TerminationType.cs - 4 termination types
  • Enums/PreconditionerType.cs - 7 preconditioner types
  • Enums/NumericDiffMethod.cs - Forward, Central, Ridders
  • Enums/TrustRegionStrategyType.cs - LevenbergMarquardt, Dogleg
  • Enums/DoglegType.cs - TraditionalDogleg, SubspaceDogleg
  • Enums/LoggingType.cs - Silent, PerMinimizerIteration
  • Enums/LineSearchType.cs - Armijo, Wolfe
  • Enums/LineSearchDirectionType.cs - 4 direction types
  • Enums/NonlinearConjugateGradientType.cs - 3 CG types
  • Enums/SparseLinearAlgebraLibraryType.cs - 5 library types
  • Enums/DenseLinearAlgebraLibraryType.cs - 3 library types
  • Enums/CovarianceAlgorithmType.cs - DenseSvd, SparseQr
  • Core/Constants.cs - Default values và mathematical constants

2. SafeHandles (100%)

Status: Complete

Tasks:

  • Base SafeHandle class (BaseSafeHandle)
  • ProblemHandle
  • SolverOptionsHandle
  • SolverSummaryHandle
  • CostFunctionHandle
  • LossFunctionHandle
  • ManifoldHandle
  • InterpolatorHandle (BiCubic và Cubic)
  • ProblemOptionsHandle
  • CovarianceOptionsHandle
  • CovarianceHandle
  • GradientCheckerOptionsHandle
  • GradientCheckerHandle
  • ContextHandle

Files:

  • Native/SafeHandles/BaseSafeHandle.cs - Abstract base class với ReleaseNativeHandle
  • Native/SafeHandles/ProblemHandle.cs - Problem resource management
  • Native/SafeHandles/SolverOptionsHandle.cs - SolverOptions resource management
  • Native/SafeHandles/SolverSummaryHandle.cs - SolverSummary resource management
  • Native/SafeHandles/CostFunctionHandle.cs - CostFunction resource management
  • Native/SafeHandles/LossFunctionHandle.cs - LossFunction resource management
  • Native/SafeHandles/ManifoldHandle.cs - Manifold resource management
  • Native/SafeHandles/InterpolatorHandle.cs - Interpolator resource management (supports both BiCubic và Cubic)
  • Native/SafeHandles/ProblemOptionsHandle.cs - ProblemOptions resource management
  • Native/SafeHandles/CovarianceOptionsHandle.cs - CovarianceOptions resource management
  • Native/SafeHandles/CovarianceHandle.cs - Covariance resource management
  • Native/SafeHandles/GradientCheckerOptionsHandle.cs - GradientCheckerOptions resource management
  • Native/SafeHandles/GradientCheckerHandle.cs - GradientChecker resource management
  • Native/SafeHandles/ContextHandle.cs - Context resource management
  • Native/CeresNative.cs - Complete P/Invoke declarations (216+ functions)

3. Problem (100%)

Status: Complete

Tasks:

  • Problem class với IDisposable
  • AddParameterBlock (với error handling)
  • SetParameterBlockConstant/Variable
  • RemoveParameterBlock
  • AddResidualBlock (với CostFunction và LossFunction objects)
  • RemoveResidualBlock
  • SetManifold (với Manifold object)
  • SetParameterBounds (lower/upper)
  • GetParameterBounds (lower/upper)
  • Query methods (NumParameterBlocks, NumResidualBlocks, NumParameters, NumResiduals)
  • HasParameterBlock
  • IsParameterBlockConstant
  • GetParameterBlockSize
  • GetParameterBlockTangentSize
  • HasManifold
  • GetManifoldHandle
  • Solve method (với SolverOptions và returns SolverSummary)

Files:

  • Core/Problem.cs - Complete implementation với 20+ methods

API Count: 20+ methods


4. SolverOptions (100%)

Status: Complete

Tasks:

  • SolverOptions class với IDisposable
  • Linear solver type (getter/setter)
  • Minimizer type (getter/setter)
  • Iterations (max_num_iterations)
  • Threads (num_threads)
  • Tolerances (function, gradient, parameter)
  • Trust region (initial, max, min radius, strategy type, dogleg type)
  • Preconditioner type
  • Line search options (type, direction type, NCG type, LBFGS rank, step contractions, etc.)
  • LBFGS options (max_lbfgs_rank)
  • Linear solver options (sparse/dense library types, max/min iterations, tolerance)
  • Inner iterations (use_inner_iterations, tolerance)
  • Timing (max_solver_time_in_seconds)
  • Validation (IsValid method)
  • Other options (use_nonmonotonic_steps, logging_type, etc.)
  • Memory management cho iteration callbacks

Files:

  • Core/SolverOptions.cs - Complete implementation với 50+ properties

API Count: 50+ properties/methods


5. SolverSummary (100%)

Status: Complete

Tasks:

  • SolverSummary class với IDisposable
  • Termination type (getter)
  • Message (getter với StringBuilder)
  • Cost values (initial, final, cost_change)
  • Iteration counts (iterations, num_successful_steps, num_unsuccessful_steps, num_inner_iteration_steps)
  • Timing information (total, preprocessor, minimizer, postprocessor, linear_solver time)
  • Statistics (num_parameter_blocks, num_parameters, num_effective_parameters, num_residual_blocks, num_residuals)
  • Full report (getter với StringBuilder)
  • Support cho callbacks (temporary handle creation)

Files:

  • Core/SolverSummary.cs - Complete implementation với 20+ properties

API Count: 20+ properties


6. Cost Functions (100%)

Status: Complete

Tasks:

  • CostFunction base class với IDisposable
  • AutoDiffCostFunction (với callback marshalling)
  • DynamicAutoDiffCostFunction (với callback marshalling)
  • NumericDiffCostFunction (với callback marshalling)
  • DynamicNumericDiffCostFunction (với callback marshalling)
  • Callback handling với GCHandle pinning
  • Memory management (wrapper handles và native callback handles)
  • Enhanced validation (parameter block sizes, residuals)
  • Error handling và resource cleanup on failure
  • Proper exception messages

Files:

  • Core/CostFunction.cs - Complete implementation với all 4 cost function types

API Count: 4 cost function types + delegates


7. Loss Functions (100%)

Status: Complete

Tasks:

  • LossFunction base class với IDisposable
  • TrivialLoss (no parameters)
  • HuberLoss (scaling parameter a)
  • CauchyLoss (scaling parameter a)
  • SoftLOneLoss (scaling parameter a)
  • ArctanLoss (scaling parameter a)
  • TolerantLoss (scaling parameters a, b)

Files:

  • Core/LossFunction.cs - Complete implementation với all 6 loss function types

API Count: 6 loss function types


8. Manifolds (100%)

Status: Complete

Tasks:

  • Manifold base class với IDisposable
  • QuaternionManifold (for 3D rotations)
  • SphereManifold (với dimension parameter)
  • LineManifold (với dimension parameter)
  • EuclideanManifold (với dimension parameter)
  • SubsetManifold (với constant subset indices và ambient size)
  • ProductManifold (combine multiple manifolds)
  • AutoDiffManifold (callback-based custom manifolds) - NEW
    • PlusOperation và MinusOperation delegates
    • Callback marshalling (C# delegates → C callbacks)
    • Memory management với GCHandle pinning
    • Proper validation (ambientSize > 0, tangentSize > 0, tangentSize <= ambientSize)
    • Error handling và resource cleanup
  • AmbientSize và TangentSize properties
  • Proper validation cho parameters

Files:

  • Core/Manifold.cs - Complete implementation với all 6 standard manifold types
  • Core/AutoDiffManifold.cs - NEW - AutoDiff manifold với callback-based API

API Count: 7 manifold types (6 standard + AutoDiffManifold) + 2 properties + 2 delegates


9. Interpolators (100%)

Status: Complete

Tasks:

  • BiCubicInterpolator (2D interpolation)
  • CubicInterpolator (1D interpolation)
  • Evaluate methods với gradients (out parameters)
  • Evaluate methods without gradients (overloads)
  • Proper data validation (array sizes, dimensions)

Files:

  • Core/BiCubicInterpolator.cs - 2D cubic interpolation
  • Core/CubicInterpolator.cs - 1D cubic interpolation

API Count: 2 interpolator types + evaluate methods


10. Problem Options (100%)

Status: Complete

Tasks:

  • ProblemOptions class với IDisposable
  • Ownership settings (cost_function, loss_function, manifold ownership)
  • Fast removal (enable_fast_removal)
  • Safety checks (disable_all_safety_checks)
  • Evaluation callback (SetEvaluationCallback với proper memory management)
  • Context (SetContext method)
  • Memory management cho evaluation callbacks

Files:

  • Advanced/ProblemOptions.cs - Complete implementation
  • Advanced/EvaluationCallback.cs - Evaluation callback delegate

API Count: 6 properties + 2 methods


11. Parameter Bounds (100%)

Status: Complete

Tasks:

  • SetParameterLowerBound (với index validation)
  • SetParameterUpperBound (với index validation)
  • GetParameterLowerBound (returns -infinity if not set)
  • GetParameterUpperBound (returns +infinity if not set)
  • Proper validation (null checks, index bounds)

Files:

  • Core/Problem.cs - Methods implemented trong Problem class

API Count: 4 methods


12. Covariance (100%)

Status: Complete

Tasks:

  • CovarianceOptions class với IDisposable (num_threads, sparse_library_type, algorithm_type, min_reciprocal_condition_number, null_space_rank, apply_loss_function)
  • Covariance class với IDisposable (create với/không options)
  • Compute method (với Problem, CovarianceOptions, và parameter blocks) - ENHANCED - Now uses error codes and throws exceptions
  • GetCovarianceBlock (between two parameter blocks) - ENHANCED - Now uses error codes and throws exceptions
  • GetCovarianceMatrix (for multiple parameter blocks) - ENHANCED - Now uses error codes and throws exceptions
  • Proper array marshalling với GCHandle pinning
  • Error handling với CeresException (error codes + error messages) - NEW

Files:

  • Advanced/CovarianceOptions.cs - Complete implementation
  • Advanced/Covariance.cs - Complete implementation với enhanced error handling

API Count: 2 classes + 8+ methods/properties

Recent Updates (2024):

  • Enhanced error handling: All methods now use CeresErrorCode instead of bool
  • Exception-based API: Methods throw CeresException on failure instead of returning false
  • Error messages: All methods now provide detailed error messages via StringBuilder

13. Gradient Checker (100%)

Status: Complete

Tasks:

  • GradientCheckerOptions class với IDisposable (gradient_check_relative_precision, numeric_derivative_relative_step_size)
  • GradientChecker class với IDisposable (create với CostFunction, Manifolds, và Options)
  • Probe method (với parameters, relative_precision, và error message output) - ENHANCED - Now uses error codes internally
  • Probe method overload (without error message) - NEW - Simplified API
  • Proper array marshalling với GCHandle pinning
  • Support cho null manifolds array
  • Error handling với CeresException (error codes + error messages) - NEW
  • Gradient mismatch handling (InvalidParameter = expected, other errors = exceptions) - NEW

Files:

  • Advanced/GradientCheckerOptions.cs - Complete implementation
  • Advanced/GradientChecker.cs - Complete implementation với enhanced error handling

API Count: 2 classes + 4+ methods/properties (added Probe overload)

Recent Updates (2024):

  • Enhanced error handling: Probe method now uses CeresErrorCode internally
  • Exception-based API: Non-gradient-mismatch errors throw CeresException
  • Backward compatibility: Maintained bool return type with overload for error message
  • Error messages: Detailed error messages available via overload

14. Context (100%)

Status: Complete

Tasks:

  • Context class với IDisposable (for performance optimization)
  • Integration với ProblemOptions (SetContext method)
  • Proper resource management

Files:

  • Advanced/Context.cs - Complete implementation

API Count: 1 class + 1 method (SetContext trong ProblemOptions)


15. Callbacks (100%)

Status: Complete

Tasks:

  • IterationCallback delegate (trong Callbacks class)
  • EvaluationCallback delegate (trong Advanced namespace)
  • GCHandle pinning với proper memory management
  • Integration với SolverOptions (SetIterationCallback extension method)
  • Integration với ProblemOptions (SetEvaluationCallback method)
  • Memory management (track và cleanup handles trong Dispose)
  • Proper callback marshalling (C# delegates → C callbacks)

Files:

  • Core/Callbacks.cs - IterationCallback delegate và SolverOptionsExtensions
  • Advanced/EvaluationCallback.cs - EvaluationCallback delegate
  • Advanced/ProblemOptions.cs - SetEvaluationCallback implementation

API Count: 2 delegates + 2 methods


16. Constants (100%)

Status: Complete

Tasks:

  • Mathematical constants (Pi)
  • SolverOptions default values (tolerances, iterations, trust region, etc.)
  • Line search default values
  • Trust region default values
  • Linear solver default values
  • Inner iterations default values
  • Covariance default values
  • Gradient checker default values
  • Numeric diff default values

Files:

  • Core/Constants.cs - Complete constants class với all default values

API Count: 30+ constants


Status: Complete

Tasks:

  • IterationCallback delegate (trong Callbacks class)
  • EvaluationCallback delegate (trong Advanced namespace)
  • GCHandle pinning với proper memory management
  • Integration với SolverOptions (SetIterationCallback extension method)
  • Integration với ProblemOptions (SetEvaluationCallback method)
  • Memory management (track và cleanup handles trong Dispose)
  • Proper callback marshalling (C# delegates → C callbacks)

Files:

  • Core/Callbacks.cs - IterationCallback delegate và SolverOptionsExtensions
  • Advanced/EvaluationCallback.cs - EvaluationCallback delegate
  • Advanced/ProblemOptions.cs - SetEvaluationCallback implementation

API Count: 2 delegates + 2 methods


🔧 Technical Decisions

Memory Management

  • SafeHandle pattern cho tất cả native resources (14 SafeHandle classes)
  • IDisposable pattern cho high-level classes
  • Automatic cleanup khi object bị dispose
  • GCHandle tracking và cleanup cho callbacks
  • Resource cleanup on creation failure
  • No memory leaks

Error Handling

  • Tất cả errors → CeresException
  • Error codes được map thành meaningful exceptions
  • Null checks với ArgumentNullException
  • Parameter validation với ArgumentException
  • Specific error messages cho từng failure case
  • Enhanced error handling for advanced features - NEW
    • Covariance methods: All use error codes and throw exceptions
    • GradientChecker.Probe: Uses error codes internally, throws exceptions for non-mismatch errors
    • Error messages: All methods provide detailed error messages via StringBuilder

Callbacks

  • Delegates cho C# callbacks (IterationCallback, EvaluationCallback, AutoDiffCostFunctionCallback, NumericDiffCostFunctionCallback, AutoDiffManifoldPlusOperation, AutoDiffManifoldMinusOperation)
  • GCHandle.Alloc() để pin delegates
  • GCHandle.Free() trong Dispose
  • Proper callback marshalling (C# → C)
  • Memory management cho callback handles (track trong parent objects)

Type Safety

  • Strong typing với enums (13 enum types)
  • Nullable reference types
  • Parameter validation (null checks, bounds checks, positive number checks)
  • Array size validation
  • Index bounds checking

📚 Reference

  • C API Header: ipc/CeresWrapper/ceres_wrapper.h
  • C API Implementation: ipc/CeresWrapper/ceres_wrapper.cc
  • C API Tests: ipc/CeresWrapper/ceres_wrapper_test.c
  • Evaluation: ipc/CeresWrapper/CSHARP_WRAPPER_FINAL_EVALUATION.md

🚀 Next Steps

  1. Create project structure
  2. Implement Core Foundation (Error handling, Enums)
  3. Implement SafeHandles
  4. Implement Problem class
  5. Implement SolverOptions và SolverSummary
  6. Implement Cost Functions
  7. Implement Loss Functions
  8. Implement Manifolds
  9. Implement Interpolators
  10. Implement Advanced features
  11. Testing và validation
  12. Performance optimization
  13. Documentation completion
  14. Example projects

📊 Final Statistics

  • Total Files Created: 50+ C# files
  • Total Lines of Code: ~5,000+ lines
  • APIs Implemented: 220+ functions (including AutoDiffManifold)
  • Enums: 13 types
  • SafeHandles: 14 classes
  • Core Classes: 9 classes (added AutoDiffManifold)
  • Advanced Classes: 6 classes
  • Manifold Types: 7 types (6 standard + AutoDiffManifold)
  • Build Status: SUCCESS

Last Updated: 2024-12-19 (Updated with Error Handling Enhancement)
Status: 100% Complete - Including AutoDiffManifold + Enhanced Error Handling (Ready for Cartographer Integration)

Recent Updates (2024):

  • Enhanced error handling for Covariance methods (Compute, GetCovarianceBlock, GetCovarianceMatrix)
  • Enhanced error handling for GradientChecker.Probe method
  • All advanced features now have comprehensive error reporting with error codes and messages
  • Exception-based API for better error handling in C# code
  • Backward compatibility maintained for GradientChecker.Probe