Start a new topic

Can we get the screen coordinates from target model and projection matrices ?

How get the screen cordinates (X,Y points) from targets modelview and projection matrix?


I tried following code but the values were very high


 GLKMatrix4  modelView;

 GLKMatrix4  projection;

 memcpy(modelView.m, target.modelView, 16*sizeof(float));

 memcpy(projection.m, target.projection, 16*sizeof(float));

 GLKMatrix4 modelviewprojectionMatrix = GLKMatrix4Multiply(projection, modelView);

 static GLfloat rectVerts[] = {-0.1f, -0.1f, 0.0f,-0.1f,

0.1f, 0.0f,0.1f, 0.1f,

0.0f,0.1f, -0.1f, 0.0f

,0.0f,0.0f,0.0f,1.0f};

 GLKVector4 p_clip = GLKMatrix4MultiplyVector4(modelviewprojectionMatrix, GLKVector4MakeWithArray(rectVerts));

 GLKVector4 p_ndc = GLKVector4DivideScalar(p_clip, p_clip.w);

 GLint view[4]={0};

 glGetIntegerv(GL_VIEWPORT,view);

 CGRect rect = CGRectMake(view[0], view[1], view[2], view[3]);

 rect = [self convertCGRect:rect];

 float x = (rect.origin.x + rect.size.width) * (p_ndc.x + 1) /2;

 NSLog(@"Test x = %f",x);


No offense, but I've noticed that whenever a developer has a complex questions Wikitude responds by providing a link to their documentation. Generally the documentation isn't that good. We're developers, we know how to read the documentation. The support is so bad that I am considering switching to AR core. It has better documentation and better customer support. When a developer writes in its because he's probably spent days trying to figure out a problem to no avail.


For example. Developer asks. How do I get the screen coordinates of a target?


A good answer would be something like:


ObjectWhatever can be accessed by writing ObjectWhatever.CurrentScreenPositionXY


A bad answer is:


Take a look at our documentation its self explanatory. It's not self explanatory. It's a mess.

As Instant Tracking tracks the whole scene you will not get any updates in the recognized targets as this is intended for 2D image targets.


To get the distance from the origin of the scene to the camera, you need to extract the translation part from the given modelview matrix, which you can get from the last column of that matrix. This gives you a Vector with the x,y,z translation. You then just need to calculate the length of that vector to get the distance from the origin of the scene to the camera.


Hope that helps,

Markus

 

// Code for instant tracking

self.instantTracker = [self.wikitudeSDK.trackerManager createInstantTracker:self configuration:nil];

self.trackingState = WTInstantTrackerInitializing;

self.renderableRectangle = [[StrokedRectangle alloc] init];

self.renderableRectangle.scale = 0.45;

 

 

 

 

/* 

      //Code for image tracking from resources

 

    __weak typeof(self) weakSelf = self;

NSURL *imageTrackerResourceURL = [[NSBundle mainBundle] URLForResource:@"magazine" withExtension:@"wtc" subdirectory:@"Assets"];

self.targetCollectionResource = [self.wikitudeSDK.trackerManager createTargetCollectionResourceFromURL:imageTrackerResourceURL completion:^(BOOL success, NSError * _Nullable error) {

if ( !success )

{

NSLog(@"Failed to load URL resource. Reason: %@", [error localizedDescription]);

}

else

{

weakSelf.imageTracker = [weakSelf.wikitudeSDK.trackerManager createImageTrackerFromTargetCollectionResource:weakSelf.targetCollectionResource delegate:weakSelf configuration:nil];

}

}]; */


in the above code if I uncomment  //Code for image tracking from resources   I get the NSLog(@"Calculate Size %lu",_currentlyRecognizedTargets.size());

  as one during the tracking and for  // Code for instant tracking  it is always zero even if it is in tracking state

HI...I am still having this issue...I have managed to calculate screen coordinates but now I want to calculate the distance 


I am geeting callback on update method on each frame but the size of _currentlyRecognizedTargets always zero for instant tracking and for other tracking I am getting non zero count

 

void SimpleYUVInputPlugin::update(const std::list<wikitude::sdk::RecognizedTarget>& recognizedTargets_) {

 std::lock_guard<std::mutex> lock(_currentlyRecognizedTargetsMutex);

_currentlyRecognizedTargets = std::list<wikitude::sdk::RecognizedTarget>(recognizedTargets_);

NSLog(@"Calculate Size %lu",_currentlyRecognizedTargets.size());

}

Ok the whole idea is the same for 2d tracking as well as instant tracking.

What could be an issue is the actual object coordinates you want to transform.

You can refer to our native samples application to see the object coordinates of the drawn rectangle in the 2D tracking example and the Instant tracking example. 

Specifically the scale-property of the drawn rectangle is different in this case..


Hope that helps,

Markus

Hi Thanks for your reply.....It is not working with instant tracker but works with other tracking

Hello!


If the goal is to get the coordinates of the target in screen coordinates, the easiest way is to just use the wikitude plugins API and retreive the values from the RecognizedTarget in the update(const std::list<wikitude::sdk::RecognizedTarget>& recognizedTargets_) - method of the plugin class. More detailed documentation can be found at: 


https://www.wikitude.com/external/doc/documentation/latest/iosnative/pluginsapi.html#plugins-api  

Login or Signup to post a comment