Skip to content
Browse files

Camera distortion

  • Loading branch information...
1 parent abd400a commit 7707cf381e38cce1d2b2e4fe7ce9a875f7c7a3ff @elliotwoods committed Jun 20, 2012
Showing with 57 additions and 2 deletions.
  1. +13 −0 src/ofxRay/Camera.cpp
  2. +3 −0 src/ofxRay/Camera.h
  3. +30 −2 src/ofxRay/Projector.cpp
  4. +6 −0 src/ofxRay/Projector.h
  5. +4 −0 src/ofxRay/Ray.cpp
  6. +1 −0 src/ofxRay/Ray.h
View
13 src/ofxRay/Camera.cpp
@@ -13,4 +13,17 @@ namespace ofxRay {
Projector(1.0f, ofVec2f(0.0f, 0.0f), 1024, 768) {
}
+
+ ofVec2f Camera::undistortCoordinate(const ofVec2f & xy) const {
+ float theta = atan2(xy.y, xy.x);
+ float r = xy.length();
+
+ r = r +
+ distortion.x * r * r +
+ distortion.y * r * r * r +
+ distortion.z * r * r * r * r +
+ distortion.w * r * r * r * r * r;
+
+ return ofVec2f(r * cos(theta), r * sin(theta));
+ }
}
View
3 src/ofxRay/Camera.h
@@ -13,5 +13,8 @@ namespace ofxRay {
class Camera : public Projector {
public:
Camera();
+
+ ofVec2f undistortCoordinate(const ofVec2f & xy) const;
+ ofVec4f distortion;
};
}
View
32 src/ofxRay/Projector.cpp
@@ -75,11 +75,12 @@ namespace ofxRay {
}
Ray Projector::castCoordinate(const ofVec2f& xy) const {
+ ofVec2f xyUndistorted = this->undistortCoordinate(xy);
ofMatrix4x4 matrix = this->getProjectionMatrix();
matrix.preMult(this->getViewMatrix());
- ofVec4f PosW = ofVec4f(xy.x, xy.y, 1.0f, 1.0f) * matrix.getInverse();
+ ofVec4f PosW = ofVec4f(xyUndistorted.x, xyUndistorted.y, 1.0f, 1.0f) * matrix.getInverse();
ofVec3f t = ofVec3f(PosW / PosW.w) - this->getPosition();
- return Ray(this->getPosition(), t, ofColor(255.0f * (xy.x + 1.0f) / 2.0f, 255.0f * (xy.x + 1.0f) / 2.0f, 0.0f));
+ return Ray(this->getPosition(), t, ofColor(255.0f * (xyUndistorted.x + 1.0f) / 2.0f, 255.0f * (xyUndistorted.x + 1.0f) / 2.0f, 0.0f));
}
void Projector::castCoordinates(const vector<ofVec2f>& xy, vector<Ray>& rays) const {
@@ -162,6 +163,33 @@ namespace ofxRay {
return this->getProjectionMatrix();
}
+ void Projector::drawOnNearPlane(ofBaseHasTexture &image) const {
+ ofMesh plane;
+
+ ofMatrix4x4 inversed;
+ inversed.makeInvertOf(this->getViewMatrix() * this->getProjectionMatrix());
+
+ plane.addVertex(ofVec3f(-1.0f, +1.0f, -1.0f) * inversed);
+ plane.addTexCoord(ofVec2f(0,0));
+ plane.addVertex(ofVec3f(+1.0f, +1.0f, -1.0f) * inversed);
+ plane.addTexCoord(ofVec2f(this->getWidth(),0));
+ plane.addVertex(ofVec3f(-1.0f, -1.0f, -1.0f) * inversed);
+ plane.addTexCoord(ofVec2f(0,this->getHeight()));
+ plane.addVertex(ofVec3f(+1.0f, -1.0f, -1.0f) * inversed);
+ plane.addTexCoord(ofVec2f(this->getWidth(),this->getHeight()));
+
+ plane.addIndex(0);
+ plane.addIndex(1);
+ plane.addIndex(2);
+ plane.addIndex(2);
+ plane.addIndex(1);
+ plane.addIndex(3);
+
+ image.getTextureReference().bind();
+ plane.draw();
+ image.getTextureReference().unbind();
+ }
+
void Projector::makeBox() {
if (drawBox != 0)
return;
View
6 src/ofxRay/Projector.h
@@ -30,6 +30,9 @@ namespace ofxRay {
///Choose a random pose, for the projector with scale factor
void randomisePose(float scale=1.0f);
+ ///Undistort the given coordinate (pass through for Projector)
+ virtual ofVec2f undistortCoordinate(const ofVec2f & xy) const { return xy;}
+
///Generate a ray for the given pixel coordinates x,y within the projector's image
Ray castPixel(int x, int y) const;
Ray castPixel(const ofVec2f& xy) const;
@@ -54,6 +57,9 @@ namespace ofxRay {
bool isProjectionMatrixInfinite() const;
ofMatrix4x4 getClippedProjectionMatrix() const; ///<If projection matrix is infinite, we enforce a (0.01...10.0) clipping
+
+ void drawOnNearPlane(ofBaseHasTexture & image) const;
+
protected:
int width;
int height;
View
4 src/ofxRay/Ray.cpp
@@ -156,6 +156,10 @@ namespace ofxRay {
float Ray::getLength() const {
return t.length();
}
+
+ float Ray::getLengthSquared() const {
+ return t.lengthSquared();
+ }
Ray Ray::operator*(float other) const {
return Ray(s, t * other, color, infinite);
View
1 src/ofxRay/Ray.h
@@ -33,6 +33,7 @@ namespace ofxRay {
ofVec3f getMidpoint() const;
float getLength() const;
+ float getLengthSquared() const;
Ray operator*(float other) const;
ofVec3f operator()(float other) const;

0 comments on commit 7707cf3

Please sign in to comment.
Something went wrong with that request. Please try again.