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