Skip to content

Commit 15dc93f

Browse files
kalwaltclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 39183f0 commit 15dc93f

4 files changed

Lines changed: 33 additions & 1 deletion

File tree

WebARKit/WebARKitManager.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,8 @@ bool WebARKitManager::isValid() {
123123
return m_tracker->isValid();
124124
}
125125

126+
void WebARKitManager::setOriginCentered(bool centered) {
127+
m_tracker->setOriginCentered(centered);
128+
}
129+
126130
} // namespace webarkit

WebARKit/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class WebARKitTracker::WebARKitTrackerImpl {
1616
: corners(4), initialized(false), output(17, 0.0), _valid(false), _maxNumberOfMarkersToTrack(1),
1717
_currentlyTrackedMarkers(0), _frameCount(0), _frameSizeX(0),
1818
_frameSizeY(0),
19-
_isDetected(false), _isTracking(false), numMatches(0),
19+
_isDetected(false), _isTracking(false), _centerOrigin(false), numMatches(0),
2020
minNumMatches(MIN_NUM_MATCHES), _nn_match_ratio(0.7f), _trackVizActive(false),
2121
_trackViz(TrackerVisualization()) {
2222
m_camMatrix = cv::Matx33d::zeros();
@@ -181,6 +181,10 @@ class WebARKitTracker::WebARKitTrackerImpl {
181181

182182
bool isValid() { return _valid; };
183183

184+
// WebARKitLib#38: when true, the pose origin is the marker centre instead of
185+
// the reference image's top-left corner. Default false (ArtoolkitX parity).
186+
void setOriginCentered(bool centered) { _centerOrigin = centered; };
187+
184188
protected:
185189
bool RunTemplateMatching(cv::Mat frame, int trackableId) {
186190
// std::cout << "Starting template match" << std::endl;
@@ -384,6 +388,19 @@ class WebARKitTracker::WebARKitTrackerImpl {
384388
cv::Mat _pose;
385389
std::vector<cv::Point2f> imgPoints = _trackSelection.GetTrackedFeaturesWarped();
386390
std::vector<cv::Point3f> objPoints = _trackSelection.GetTrackedFeatures3d();
391+
// WebARKitLib#38 (webarkit-testing#38): optional centered origin. When
392+
// enabled, shift the 3D object points so the pose origin is the marker
393+
// centre instead of the reference image's top-left corner -- content at
394+
// (0,0,0) then sits in the middle of the marker. Offsetting the object
395+
// points moves only the solved translation; the rotation is unchanged.
396+
// Applied to the solvePnP points ONLY -- the 2D imgPoints and the
397+
// matching/template/homography paths stay in raw image space. Default
398+
// off (ArtoolkitX parity). See docs/design-center-origin-option.md.
399+
if (_centerOrigin) {
400+
const float cx = _pattern.size.width * 0.5f;
401+
const float cy = _pattern.size.height * 0.5f;
402+
for (auto& p : objPoints) { p.x -= cx; p.y -= cy; }
403+
}
387404
// Need at least 4 correspondences for solvePnP, and the two sets must
388405
// match in size. On the very first frame a marker can be detected
389406
// before the tracked-point selection is populated (the optical-flow
@@ -703,6 +720,9 @@ class WebARKitTracker::WebARKitTrackerImpl {
703720

704721
bool _isTracking;
705722

723+
// WebARKitLib#38: pose origin = marker centre when true (default false).
724+
bool _centerOrigin;
725+
706726
std::vector<cv::Point2f> corners;
707727

708728
cv::Mat m_H;
@@ -822,4 +842,6 @@ std::array<double, 16> WebARKitTracker::getCameraProjectionMatrix() {
822842

823843
bool WebARKitTracker::isValid() { return _trackerImpl->isValid(); }
824844

845+
void WebARKitTracker::setOriginCentered(bool centered) { _trackerImpl->setOriginCentered(centered); }
846+
825847
} // namespace webarkit

WebARKit/WebARKitTrackers/WebARKitOpticalTracking/include/WebARKitTrackers/WebARKitOpticalTracking/WebARKitTracker.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class WebARKitTracker {
4343

4444
bool isValid();
4545

46+
// WebARKitLib#38: pose origin = marker centre when true (default false).
47+
void setOriginCentered(bool centered);
48+
4649
private:
4750
class WebARKitTrackerImpl;
4851

WebARKit/include/WebARKitManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ class WebARKitManager {
113113
std::array<double, 16> getCameraProjectionMatrix();
114114

115115
bool isValid();
116+
117+
/// WebARKitLib#38: pose origin = marker centre when true (default false).
118+
void setOriginCentered(bool centered);
116119
};
117120

118121
} // namespace webarkit

0 commit comments

Comments
 (0)