Skip to content

Commit

Permalink
Add Matrix4.setFromIntrinsic()
Browse files Browse the repository at this point in the history
Sets the corresponding OpenGL projection matrix from the intrinsic
camera parameters as described in
https://en.wikipedia.org/wiki/Camera_resectioning#Intrinsic_parameters
  • Loading branch information
httpdigest committed Mar 7, 2017
1 parent 6d01be7 commit d6d6d9a
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/org/joml/Matrix4d.java
Original file line number Diff line number Diff line change
Expand Up @@ -13117,6 +13117,58 @@ public Matrix4d setFrustumLH(double left, double right, double bottom, double to
return setFrustumLH(left, right, bottom, top, zNear, zFar, false);
}

/**
* Set this matrix to represent a perspective projection equivalent to the given intrinsic camera calibration parameters.
* The resulting matrix will be suited for a right-handed coordinate system using OpenGL's NDC z range of <tt>[-1..+1]</tt>.
* <p>
* See: <a href="https://en.wikipedia.org/wiki/Camera_resectioning#Intrinsic_parameters">https://en.wikipedia.org/</a>
* <p>
* Reference: <a href="http://ksimek.github.io/2013/06/03/calibrated_cameras_in_opengl/">http://ksimek.github.io/</a>
*
* @param alphaX
* specifies the focal length and scale along the X axis
* @param alphaY
* specifies the focal length and scale along the Y axis
* @param gamma
* the skew coefficient between the X and Y axis (may be <tt>0</tt>)
* @param u0
* the X coordinate of the principal point in image/sensor units
* @param v0
* the Y coordinate of the principal point in image/sensor units
* @param imgWidth
* the width of the sensor/image image/sensor units
* @param imgHeight
* the height of the sensor/image image/sensor units
* @param near
* the distance to the near plane
* @param far
* the distance to the far plane
* @return this
*/
public Matrix4d setFromIntrinsic(double alphaX, double alphaY, double gamma, double u0, double v0, int imgWidth, int imgHeight, double near, double far) {
double l00 = 2.0 / imgWidth;
double l11 = 2.0 / imgHeight;
double l22 = 2.0 / (near - far);
this.m00 = l00 * alphaX;
this.m01 = 0.0;
this.m02 = 0.0;
this.m03 = 0.0;
this.m10 = l00 * gamma;
this.m11 = l11 * alphaY;
this.m12 = 0.0;
this.m13 = 0.0;
this.m20 = l00 * u0 - 1.0;
this.m21 = l11 * v0 - 1.0;
this.m22 = l22 * -(near + far) + (far + near) / (near - far);
this.m23 = -1.0;
this.m30 = 0.0;
this.m31 = 0.0;
this.m32 = l22 * -near * far;
this.m33 = 0.0;
this.properties = PROPERTY_PERSPECTIVE;
return this;
}

/* (non-Javadoc)
* @see org.joml.Matrix4dc#frustumPlane(int, org.joml.Vector4d)
*/
Expand Down
52 changes: 52 additions & 0 deletions src/org/joml/Matrix4f.java
Original file line number Diff line number Diff line change
Expand Up @@ -10531,6 +10531,58 @@ public Matrix4f setFrustumLH(float left, float right, float bottom, float top, f
return setFrustumLH(left, right, bottom, top, zNear, zFar, false);
}

/**
* Set this matrix to represent a perspective projection equivalent to the given intrinsic camera calibration parameters.
* The resulting matrix will be suited for a right-handed coordinate system using OpenGL's NDC z range of <tt>[-1..+1]</tt>.
* <p>
* See: <a href="https://en.wikipedia.org/wiki/Camera_resectioning#Intrinsic_parameters">https://en.wikipedia.org/</a>
* <p>
* Reference: <a href="http://ksimek.github.io/2013/06/03/calibrated_cameras_in_opengl/">http://ksimek.github.io/</a>
*
* @param alphaX
* specifies the focal length and scale along the X axis
* @param alphaY
* specifies the focal length and scale along the Y axis
* @param gamma
* the skew coefficient between the X and Y axis (may be <tt>0</tt>)
* @param u0
* the X coordinate of the principal point in image/sensor units
* @param v0
* the Y coordinate of the principal point in image/sensor units
* @param imgWidth
* the width of the sensor/image image/sensor units
* @param imgHeight
* the height of the sensor/image image/sensor units
* @param near
* the distance to the near plane
* @param far
* the distance to the far plane
* @return this
*/
public Matrix4f setFromIntrinsic(float alphaX, float alphaY, float gamma, float u0, float v0, int imgWidth, int imgHeight, float near, float far) {
float l00 = 2.0f / imgWidth;
float l11 = 2.0f / imgHeight;
float l22 = 2.0f / (near - far);
this.m00 = l00 * alphaX;
this.m01 = 0.0f;
this.m02 = 0.0f;
this.m03 = 0.0f;
this.m10 = l00 * gamma;
this.m11 = l11 * alphaY;
this.m12 = 0.0f;
this.m13 = 0.0f;
this.m20 = l00 * u0 - 1.0f;
this.m21 = l11 * v0 - 1.0f;
this.m22 = l22 * -(near + far) + (far + near) / (near - far);
this.m23 = -1.0f;
this.m30 = 0.0f;
this.m31 = 0.0f;
this.m32 = l22 * -near * far;
this.m33 = 0.0f;
this.properties = PROPERTY_PERSPECTIVE;
return this;
}

/**
* Apply the rotation transformation of the given {@link Quaternionfc} to this matrix and store
* the result in <code>dest</code>.
Expand Down

0 comments on commit d6d6d9a

Please sign in to comment.