From fe588e54ae78f559183c17be569080c45c251c17 Mon Sep 17 00:00:00 2001 From: Walter Perdan Date: Tue, 16 Jun 2026 16:07:59 +0200 Subject: [PATCH] feat(tracker): optional centered marker origin (setOriginCentered) Add an opt-in option so the tracked pose origin is the marker centre instead of the reference image's top-left corner -- AR content at (0,0,0) then sits in the middle of the marker. Default OFF (matches ArtoolkitX, which never centres at any layer, and preserves existing behaviour). When enabled, the 3D object points are offset by (_pattern.size/2) just before solvePnP: this moves only the solved translation, leaves the rotation unchanged, and the 2D matching/template/homography paths untouched. Exposed as setOriginCentered(bool) via the tracker and manager. Refs webarkit/webarkit-testing#38 Co-Authored-By: Claude Opus 4.8 --- WebARKit/WebARKitManager.cpp | 4 ++++ .../WebARKitTracker.cpp | 24 ++++++++++++++++++- .../WebARKitOpticalTracking/WebARKitTracker.h | 3 +++ WebARKit/include/WebARKitManager.h | 3 +++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/WebARKit/WebARKitManager.cpp b/WebARKit/WebARKitManager.cpp index 099489f..448fedb 100644 --- a/WebARKit/WebARKitManager.cpp +++ b/WebARKit/WebARKitManager.cpp @@ -123,4 +123,8 @@ bool WebARKitManager::isValid() { return m_tracker->isValid(); } +void WebARKitManager::setOriginCentered(bool centered) { + m_tracker->setOriginCentered(centered); +} + } // namespace webarkit \ No newline at end of file diff --git a/WebARKit/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.cpp b/WebARKit/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.cpp index bfd8933..730f597 100644 --- a/WebARKit/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.cpp +++ b/WebARKit/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.cpp @@ -16,7 +16,7 @@ class WebARKitTracker::WebARKitTrackerImpl { : corners(4), initialized(false), output(17, 0.0), _valid(false), _maxNumberOfMarkersToTrack(1), _currentlyTrackedMarkers(0), _frameCount(0), _frameSizeX(0), _frameSizeY(0), - _isDetected(false), _isTracking(false), numMatches(0), + _isDetected(false), _isTracking(false), _centerOrigin(false), numMatches(0), minNumMatches(MIN_NUM_MATCHES), _nn_match_ratio(0.7f), _trackVizActive(false), _trackViz(TrackerVisualization()) { m_camMatrix = cv::Matx33d::zeros(); @@ -181,6 +181,10 @@ class WebARKitTracker::WebARKitTrackerImpl { bool isValid() { return _valid; }; + // WebARKitLib#38: when true, the pose origin is the marker centre instead of + // the reference image's top-left corner. Default false (ArtoolkitX parity). + void setOriginCentered(bool centered) { _centerOrigin = centered; }; + protected: bool RunTemplateMatching(cv::Mat frame, int trackableId) { // std::cout << "Starting template match" << std::endl; @@ -384,6 +388,19 @@ class WebARKitTracker::WebARKitTrackerImpl { cv::Mat _pose; std::vector imgPoints = _trackSelection.GetTrackedFeaturesWarped(); std::vector objPoints = _trackSelection.GetTrackedFeatures3d(); + // WebARKitLib#38 (webarkit-testing#38): optional centered origin. When + // enabled, shift the 3D object points so the pose origin is the marker + // centre instead of the reference image's top-left corner -- content at + // (0,0,0) then sits in the middle of the marker. Offsetting the object + // points moves only the solved translation; the rotation is unchanged. + // Applied to the solvePnP points ONLY -- the 2D imgPoints and the + // matching/template/homography paths stay in raw image space. Default + // off (ArtoolkitX parity). See docs/design-center-origin-option.md. + if (_centerOrigin) { + const float cx = _pattern.size.width * 0.5f; + const float cy = _pattern.size.height * 0.5f; + for (auto& p : objPoints) { p.x -= cx; p.y -= cy; } + } // Need at least 4 correspondences for solvePnP, and the two sets must // match in size. On the very first frame a marker can be detected // before the tracked-point selection is populated (the optical-flow @@ -703,6 +720,9 @@ class WebARKitTracker::WebARKitTrackerImpl { bool _isTracking; + // WebARKitLib#38: pose origin = marker centre when true (default false). + bool _centerOrigin; + std::vector corners; cv::Mat m_H; @@ -822,4 +842,6 @@ std::array WebARKitTracker::getCameraProjectionMatrix() { bool WebARKitTracker::isValid() { return _trackerImpl->isValid(); } +void WebARKitTracker::setOriginCentered(bool centered) { _trackerImpl->setOriginCentered(centered); } + } // namespace webarkit \ No newline at end of file diff --git a/WebARKit/WebARKitTrackers/WebARKitOpticalTracking/include/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.h b/WebARKit/WebARKitTrackers/WebARKitOpticalTracking/include/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.h index 6494877..491867c 100644 --- a/WebARKit/WebARKitTrackers/WebARKitOpticalTracking/include/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.h +++ b/WebARKit/WebARKitTrackers/WebARKitOpticalTracking/include/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.h @@ -43,6 +43,9 @@ class WebARKitTracker { bool isValid(); + // WebARKitLib#38: pose origin = marker centre when true (default false). + void setOriginCentered(bool centered); + private: class WebARKitTrackerImpl; diff --git a/WebARKit/include/WebARKitManager.h b/WebARKit/include/WebARKitManager.h index 6472aad..9b36b59 100644 --- a/WebARKit/include/WebARKitManager.h +++ b/WebARKit/include/WebARKitManager.h @@ -113,6 +113,9 @@ class WebARKitManager { std::array getCameraProjectionMatrix(); bool isValid(); + + /// WebARKitLib#38: pose origin = marker centre when true (default false). + void setOriginCentered(bool centered); }; } // namespace webarkit