Skip to main content

Mouse coord to virtual world - Parallel projection

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
Keopz
Offline
Joined: 2011-08-05

Hi,

I've been able to doit in Perspective projection the code above. Why does'n it work the same with Parallel projection? The point where new geometry is added doesn't correspond to the mouse point on screen...

code:

public Point3d getCanvasPtToVworldPt(int x, int y) {

Point3d VworldPt = new Point3d();

Point3d centerEyePt = new Point3d();

cv.getPixelLocationInImagePlate(x, y, VworldPt);

Transform3D imagePlateToVworld = new Transform3D();

cv.getImagePlateToVworld(imagePlateToVworld);

imagePlateToVworld.transform(VworldPt);

cv.getCenterEyeInImagePlate(centerEyePt);

imagePlateToVworld.transform(centerEyePt);

Point3d planePt = new Point3d(

centerEyePt.x - alpha * (VworldPt.x - centerEyePt.x),

centerEyePt.y - alpha * (VworldPt.y - centerEyePt.y),

0.0);

return planePt;

}

thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Keopz
Offline
Joined: 2011-08-05

Once nobody answered, I had to break my head and solve it. Hadn't had the time to clean it but here it is, free of charge:

public Point3d getCanvasPtToVworldPt(int x, int y) {

if (view.getProjectionPolicy() == View.PERSPECTIVE_PROJECTION) {

Point3d VworldPt = new Point3d();

Point3d centerEyePt = new Point3d();

// convert the canvas point to ImagePlate coords

cv.getPixelLocationInImagePlate(x, y, VworldPt);

// transform the point from an imageplate coordinate to a Vworld coordinate

// via transform3d

Transform3D imagePlateToVworld = new Transform3D();

//picked point

cv.getImagePlateToVworld(imagePlateToVworld);

imagePlateToVworld.transform(VworldPt);

//eye point

cv.getCenterEyeInImagePlate(centerEyePt);

imagePlateToVworld.transform(centerEyePt);

//Logging.trace(10, "Center eye pt VW " + centerEyePt);

//now compute the z=0 value in the centereye to VworldPt pt

//centerEyePt_VworldPt = alpha *centerEyePt_planePt with planePt.z=0;

double alpha = 0.0;

if (VworldPt.z != centerEyePt.z) {

alpha = centerEyePt.z / (VworldPt.z - centerEyePt.z);

}

/*

// The following may work for Z, I haven't tested it

double beta = 0.0;

if ( VworldPt.y != centerEyePt.y )

{

beta = centerEyePt.y /(VworldPt.y - centerEyePt.y);

}

*/

Point3d planePt = new Point3d(

centerEyePt.x - alpha * (VworldPt.x - centerEyePt.x),

centerEyePt.y - alpha * (VworldPt.y - centerEyePt.y),

0.0); //centerEyePt.z - beta * (VworldPt.z - centerEyePt.z )

return planePt;

} else {

//MOUSE TRACKING: ORTHOGRAPHIC

//point in drawing canvas

Point3d imgPt = new Point3d(x, y, 0);

//System.out.println("imgPt:" + imgPt);

//point in image plate

Point3d platePt = new Point3d();

cv.getPixelLocationInImagePlate(x, y, platePt);

//System.out.println("platePt:" + platePt);

//center point in virtual world

Point3d centerWorld = new Point3d(0, 0, 0);

//center point from world to image plate

Point3d centerPlate = centerWorld;

Transform3D imagePlateToVworld = new Transform3D();

cv.getImagePlateToVworld(imagePlateToVworld);

imagePlateToVworld.invert();

imagePlateToVworld.transform(centerPlate);

//System.out.println("centerPlate:" + centerPlate);

//point in world

Point3d worldPt = new Point3d(platePt.x - centerPlate.x, platePt.y - centerPlate.y, 0);

//System.out.println("worldPt:" + worldPt);

//scale/zoom transform

Vector3d planeVec = new Vector3d(worldPt.x, worldPt.y, 0);

Transform3D scaleTG = new Transform3D();

viewZoomTG.getTransform(scaleTG);

scaleTG.transform(planeVec);

Transform3D scaleTG2 = new Transform3D();

scaleTG2.set(17.59);

//eye position transform

Transform3D eyeTG = new Transform3D();

viewEyeTG.getTransform(eyeTG);

//eyeTG.invert();

//eyeTG.transform(planeVec);

Transform3D imageToWorldTG = new Transform3D();

cv.getImagePlateToVworld(imageToWorldTG);

//System.out.println("imageToWorldTG:\n" + imageToWorldTG);

//scaleTG2.transform(planeVec);

imageToWorldTG.transform(planeVec);

//System.out.println("scaleTG:\n" + scaleTG);

//System.out.println("worldPt scaled:" + planeVec.x + "," + planeVec.y);

worldPt = new Point3d(planeVec.x,planeVec.y,0);

//Zoom Correction

Transform3D zoomTG = new Transform3D();

viewZoomTG.getTransform(zoomTG);

zoomTG.invert();

zoomTG.transform(worldPt);

return worldPt;

}