Skip to content

Fix MATLAB wrapper virtual class downcasting for namespaced types#187

Merged
ProfFan merged 1 commit into
borglab:masterfrom
thatdudegrantt:fix/matlab-noisemodel-virtual-downcast
Jun 30, 2026
Merged

Fix MATLAB wrapper virtual class downcasting for namespaced types#187
ProfFan merged 1 commit into
borglab:masterfrom
thatdudegrantt:fix/matlab-noisemodel-virtual-downcast

Conversation

@thatdudegrantt

Copy link
Copy Markdown
Contributor

Problem
gtsam.noiseModel.Gaussian.Information(Q) with a scalar/diagonal matrix correctly downcasts to Isotropic in C++, but the MATLAB wrapper always returned a gtsam.noiseModel.Gaussian object. This caused JacobianFactor and similar functions that require a diagonal noise model to fail.

Root cause (two bugs)
_collector_return and wrap_collector_function_return_types always passed isVirtual=false to wrap_shared_ptr, bypassing the RTTI registry entirely even for classes declared virtual in the .i file.
generate_preamble stored flat concatenated names (e.g. gtsamnoiseModelIsotropic) in the RTTI registry instead of dot-separated MATLAB package names (e.g. gtsam.noiseModel.Isotropic), causing mexCallMATLAB to fail for any namespaced virtual class.

Fix

Check cls.is_virtual against self.classes in both return codegen functions and pass true to wrap_shared_ptr for virtual classes.
Use _format_class_name(cls, separator='.') for RTTI registry entries.

Testing
Verified end-to-end on Windows x64, MSVC 19.51, MATLAB R2026a:

nmQ = gtsam.noiseModel.Gaussian.Information(2);
class(nmQ)  % returns gtsam.noiseModel.Isotropic
gtsam.JacobianFactor(0, 0, 0, nmQ)  % succeeds

All 10 existing wrap Python tests pass. Added virtual base/derived fixture to tests/fixtures/inheritance.i to cover the isVirtual=true codegen case.
Fixes borglab/gtsam#605

@thatdudegrantt thatdudegrantt force-pushed the fix/matlab-noisemodel-virtual-downcast branch from 7dccf51 to 29d4073 Compare June 29, 2026 22:52
@thatdudegrantt

Copy link
Copy Markdown
Contributor Author

CI is passing. Verified end-to-end on Windows x64, MSVC 19.51, MATLAB R2026a. noiseModel.Gaussian.Information(2) now returns gtsam.noiseModel.Isotropic and gtsam.JacobianFactor(0, 0, 0, nmQ) succeeds. Ready for review, thanks in advance!

@ProfFan

ProfFan commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Cool I'll review today

@ProfFan ProfFan merged commit aa4a5a6 into borglab:master Jun 30, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Matlab noiseModel inheritance downcasting "smart" parameter

2 participants