Skip to content

Commit

Permalink
implementaion of constructRayThroughPixel and javdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
nevcohen committed Apr 26, 2021
1 parent 7f173d7 commit 55cdc87
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 13 deletions.
92 changes: 88 additions & 4 deletions cameraproject/src/elements/Camera.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,45 @@
import primitives.Ray;
import primitives.Vector;

/**
* Class for the construction of the camera and the view plane of the project
*/
public class Camera {
/**
* Location of the camera in space
*/
private Point3D location;
private Vector vUp, vTo, vRight;
private double widthVP, heightVP, distanceVP;

/**
* Camera direction - Vector upward of the camera
*/
private Vector vUp;
/**
* Camera direction - vector to the front of the camera
*/
private Vector vTo;
/**
* Camera direction - vector to the right of the camera
*/
private Vector vRight;

/**
* The width of the view plane
*/
private double widthVP;
/**
* The height of the view plane
*/
private double heightVP;
/**
* The distance of the view plane from the Camera
*/
private double distanceVP;

/**
* A camera constructor, which gets location and two directions (and calculates
* the third direction), the constructor normalizes the direction vectors.
*
* @param location
* @param vUp
* @param vTo
Expand All @@ -24,22 +57,49 @@ public Camera(Point3D location, Vector vTo, Vector vUp) {
this.vRight = vTo.crossProduct(vUp).normalize();
}

/**
* Gets the location of the camera in space (x,y,z)
*
* @return Point3D of the location
*/
public Point3D getLocation() {
return location;
}

/**
* Gets the vector upward of the camera
*
* @return Vector vUp of the camera
*/
public Vector getvUp() {
return vUp;
}

/**
* Gets the vector to the front of the camera
*
* @return Vector vTo of the camera
*/
public Vector getvTo() {
return vTo;
}

/**
* Gets the vector to the right of the camera
*
* @return Vector vRight of the camera
*/
public Vector getvRight() {
return vRight;
}

/**
* Defining the length and width of the view plane (builder design template)
*
* @param width of the view plane (Greater than zero)
* @param height of the view plane (Greater than zero)
* @return the camera itself
*/
public Camera setViewPlaneSize(double width, double height) {
if (width < 0 || isZero(width) || height < 0 && isZero(height))
throw new IllegalArgumentException("The size of the view plane is invalid");
Expand All @@ -48,14 +108,38 @@ public Camera setViewPlaneSize(double width, double height) {
return this;
}

/**
* Defines the distance of the view plane from the camera
*
* @param distance of the view plane from the camera (Greater than zero)
* @return the camera itself
*/
public Camera setViewPlaneDistance(double distance) {
if (distance < 0 || isZero(distance))
throw new IllegalArgumentException("The distance of the view plane is invalid");
distanceVP = distance;
return this;
}

public Ray constructRayThroughPixel(int nX, int nY, int j, int i) {
return null;
/**
* Build a ray through a specific pixel in the view plane
*
* @param nX - Number of columns in the view plane
* @param nY - Number of rows in the view plane
* @param j - The column number of the pixel
* @param i - The row number of the pixel
* @return Ray from the camera through the desired pixel
*/
public Ray constructRayThroughPixel(int nX, int nY, int j, int i) {
double yI = alignZero((((nY - 1) / 2d) - i) * (heightVP / nY)),
xJ = alignZero((j - (nX - 1) / 2d) * (widthVP / nX));

Point3D pIJ = location.add(vTo.scale(distanceVP)); // center of the VP
if (xJ != 0)
pIJ = pIJ.add(vRight.scale(xJ));
if (yI != 0)
pIJ = pIJ.add(vUp.scale(yI));

return new Ray(location, pIJ.subtract(location));
}
}
17 changes: 8 additions & 9 deletions cameraproject/src/unittests/elements/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,16 @@ public void testPlane() {
camera.setViewPlaneSize(3, 3);
camera.setViewPlaneDistance(1);

// TC01: Plane is parralell to the view plane
Plane plane = new Plane(new Point3D(0, 0, -2), new Vector(0, 0, 1));
assertEquals("plane, paralell to the view plane, wrong number of intersection points", 9,
getSumIntersections(camera, plane));

// TC02: Plane is close by to the view plane (9 points within distance of 10)
// TC01: Plane is parallel to the view plane
Plane plane = new Plane(new Point3D(2, 0, -1), new Vector(0,0,-1));
assertEquals("plane, paralell to the view plane, wrong number of intersection points", 9, getSumIntersections(camera, plane));

// TC02: Plane has angles that allow for 9 points
plane = new Plane(new Point3D(0, 0, -3), new Vector(0, 0.5, -1));
assertEquals("plane, paralell to the view plane, wrong number of intersection points", 9,
getSumIntersections(camera, plane));

// TC03: Plane is far to the view plane (6 points within distance of 10)
getSumIntersections(camera, plane));
// TC03: Plane has a 90 degree angle with ray
plane = new Plane(new Point3D(0, 0, -3), new Vector(0, 1, -1));
assertEquals("plane, paralell to the view plane, wrong number of intersection points", 6,
getSumIntersections(camera, plane));
Expand Down

0 comments on commit 55cdc87

Please sign in to comment.