fifth level
diff --git a/src/elements/AmbientLight.java b/src/elements/AmbientLight.java
new file mode 100644
index 0000000..c31e221
--- /dev/null
+++ b/src/elements/AmbientLight.java
@@ -0,0 +1,25 @@
+package elements;
+
+import primitives.Color;
+
+
+public class AmbientLight {
+final Color _intensity;
+
+/**
+ * @param _intensity
+ */
+public AmbientLight(Color iA,double kA) {
+
+ this._intensity = iA.scale(kA);
+}
+
+/**
+ * @return the _intensity
+ */
+public Color getIntensity() {
+ return _intensity;
+}
+
+
+}
diff --git a/src/elements/Camera.java b/src/elements/Camera.java
index bad0290..ddd2909 100644
--- a/src/elements/Camera.java
+++ b/src/elements/Camera.java
@@ -73,7 +73,7 @@
this.vUp = vUp.normalized();
this.vTo = vTo.normalized();
- this.vRight = this.vTo.crossProduct(this.vUp);
+ this.vRight = (this.vTo.crossProduct(this.vUp)).normalized();
}
/**
diff --git a/src/primitives/Color.java b/src/primitives/Color.java
new file mode 100644
index 0000000..1b6a647
--- /dev/null
+++ b/src/primitives/Color.java
@@ -0,0 +1,177 @@
+package primitives;
+
+/**
+ * Wrapper class for java.jwt.Color The constructors operate with any
+ * non-negative RGB values. The colors are maintained without upper limit of
+ * 255. Some additional operations are added that are useful for manipulating
+ * light's colors
+ *
+ * @author Dan Zilberstein
+ */
+public class Color {
+ /**
+ * The internal fields tx`o maintain RGB components as double numbers from 0 to
+ * whatever...
+ */
+ private double r = 0.0;
+ private double g = 0.0;
+ private double b = 0.0;
+
+ public static final Color BLACK = new Color();
+
+ /**
+ * Default constructor - to generate Black Color (privately)
+ */
+ private Color() {
+ }
+
+ /**
+ * Constructor to generate a color according to RGB components Each component in
+ * range 0..255 (for printed white color) or more [for lights]
+ *
+ * @param r Red component
+ * @param g Green component
+ * @param b Blue component
+ */
+ public Color(double r, double g, double b) {
+ if (r < 0 || g < 0 || b < 0)
+ throw new IllegalArgumentException("Negative color component is illegal");
+ this.r = r;
+ this.g = g;
+ this.b = b;
+ }
+
+ /**
+ * Copy constructor for Color
+ *
+ * @param other the source color
+ */
+ public Color(Color other) {
+ r = other.r;
+ g = other.g;
+ b = other.b;
+ }
+
+ /**
+ * Constructor on base of java.awt.Color object
+ *
+ * @param other java.awt.Color's source object
+ */
+ public Color(java.awt.Color other) {
+ r = other.getRed();
+ g = other.getGreen();
+ b = other.getBlue();
+ }
+
+ /**
+ * Color setter to reset the color to BLACK
+ *
+ * @return the Color object itself for chaining calls
+ */
+ public Color setColor() {
+ r = 0.0;
+ g = 0.0;
+ b = 0.0;
+ return this;
+ }
+
+ /**
+ * Color setter to generate a color according to RGB components Each component
+ * in range 0..255 (for printed white color) or more [for lights]
+ *
+ * @param r Red component
+ * @param g Green component
+ * @param b Blue component
+ * @return the Color object itself for chaining calls
+ */
+ public Color setColor(double r, double g, double b) {
+ if (r < 0 || g < 0 || b < 0)
+ throw new IllegalArgumentException("Negative color component is illegal");
+ this.r = r;
+ this.g = g;
+ this.b = b;
+ return this;
+ }
+
+ /**
+ * Color setter to copy RGB components from another color
+ *
+ * @param other source Color object
+ * @return the Color object itself for chaining calls
+ */
+ public Color setColor(Color other) {
+ r = other.r;
+ g = other.g;
+ b = other.b;
+ return this;
+ }
+
+ /**
+ * Color setter to take components from an base of java.awt.Color object
+ *
+ * @param other java.awt.Color's source object
+ * @return the Color object itself for chaining calls
+ */
+ public Color setColor(java.awt.Color other) {
+ r = other.getRed();
+ g = other.getGreen();
+ b = other.getBlue();
+ return this;
+ }
+
+ /**
+ * Color getter - returns the color after converting it into java.awt.Color
+ * object During the conversion any component bigger than 255 is set to 255
+ *
+ * @return java.awt.Color object based on this Color RGB components
+ */
+ public java.awt.Color getColor() {
+ int ir = (int) r;
+ int ig = (int) g;
+ int ib = (int) b;
+ return new java.awt.Color(ir > 255 ? 255 : ir, ig > 255 ? 255 : ig, ib > 255 ? 255 : ib);
+ }
+
+ /**
+ * Operation of adding this and one or more other colors (by component)
+ *
+ * @param colors one or more other colors to add
+ * @return new Color object which is a result of the operation
+ */
+ public Color add(Color... colors) {
+ double rr = r;
+ double rg = g;
+ double rb = b;
+ for (Color c : colors) {
+ rr += c.r;
+ rg += c.g;
+ rb += c.b;
+ }
+ return new Color(rr, rg, rb);
+ }
+
+ /**
+ * Scale the color by a scalar
+ *
+ * @param k scale factor
+ * @return new Color object which is the result of the operation
+ */
+ public Color scale(double k) {
+ if (k < 0)
+ throw new IllegalArgumentException("Can't scale a color by a negative number");
+ return new Color(r * k, g * k, b * k);
+ }
+
+ /**
+ * Scale the color by (1 / reduction factor)
+ *
+ * @param k reduction factor
+ * @return new Color object which is the result of the operation
+ */
+ public Color reduce(double k) {
+ if (k < 1)
+ throw new IllegalArgumentException("Can't scale a color by a by a number lower than 1");
+ return new Color(r / k, g / k, b / k);
+ }
+
+}
diff --git a/src/renderer/ImageWriter.java b/src/renderer/ImageWriter.java
new file mode 100644
index 0000000..7586bbb
--- /dev/null
+++ b/src/renderer/ImageWriter.java
@@ -0,0 +1,95 @@
+package renderer;
+
+import primitives.Color;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.imageio.*;
+
+/**
+ * Image writer class combines accumulation of pixel color matrix and finally
+ * producing a non-optimized jpeg image from this matrix. The class although is
+ * responsible of holding image related parameters of View Plane - pixel matrix
+ * size and resolution
+ *
+ * @author Dan
+ */
+public class ImageWriter {
+ private int nX;
+ private int nY;
+
+ private static final String FOLDER_PATH = System.getProperty("user.dir") + "/images";
+
+ private BufferedImage image;
+ private String imageName;
+
+ private Logger logger = Logger.getLogger("ImageWriter");
+
+ // ***************** Constructors ********************** //
+ /**
+ * Image Writer constructor accepting image name and View Plane parameters,
+ *
+ * @param imageName the name of jpeg file
+ * @param width View Plane width in size units
+ * @param height View Plane height in size units
+ * @param nX amount of pixels by Width
+ * @param nY amount of pixels by height
+ */
+ public ImageWriter(String imageName, int nX, int nY) {
+ this.imageName = imageName;
+ this.nX = nX;
+ this.nY = nY;
+
+ image = new BufferedImage(nX, nY, BufferedImage.TYPE_INT_RGB);
+ }
+
+ // ***************** Getters/Setters ********************** //
+ /**
+ * View Plane Y axis resolution
+ *
+ * @return the amount of vertical pixels
+ */
+ public int getNy() {
+ return nY;
+ }
+
+ /**
+ * View Plane X axis resolution
+ *
+ * @return the amount of horizontal pixels
+ */
+ public int getNx() {
+ return nX;
+ }
+
+ // ***************** Operations ******************** //
+
+ /**
+ * Function writeToImage produces unoptimized png file of the image according to
+ * pixel color matrix in the directory of the project
+ */
+ public void writeToImage() {
+ try {
+ File file = new File(FOLDER_PATH + '/' + imageName + ".png");
+ ImageIO.write(image, "png", file);
+ } catch (IOException e) {
+ logger.log(Level.SEVERE, "I/O error", e);
+ }
+ }
+
+ /**
+ * The function writePixel writes a color of a specific pixel into pixel color
+ * matrix
+ *
+ * @param xIndex X axis index of the pixel
+ * @param yIndex Y axis index of the pixel
+ * @param color final color of the pixel
+ */
+ public void writePixel(int xIndex, int yIndex, Color color) {
+ image.setRGB(xIndex, yIndex, color.getColor().getRGB());
+ }
+
+}
diff --git a/src/renderer/RayTracerBase.java b/src/renderer/RayTracerBase.java
new file mode 100644
index 0000000..a2c3f7f
--- /dev/null
+++ b/src/renderer/RayTracerBase.java
@@ -0,0 +1,5 @@
+package renderer;
+
+public abstract class RayTracerBase {
+
+}
diff --git a/src/renderer/RayTracerBasic.java b/src/renderer/RayTracerBasic.java
new file mode 100644
index 0000000..21638e6
--- /dev/null
+++ b/src/renderer/RayTracerBasic.java
@@ -0,0 +1,11 @@
+package renderer;
+
+import scene.Scene;
+
+public class RayTracerBasic extends RayTracerBase{
+
+ public RayTracerBasic(Scene scene) {
+ // TODO Auto-generated constructor stub
+ }
+
+}
diff --git a/src/renderer/Render.java b/src/renderer/Render.java
new file mode 100644
index 0000000..aa351a2
--- /dev/null
+++ b/src/renderer/Render.java
@@ -0,0 +1,66 @@
+package renderer;
+
+import elements.Camera;
+import primitives.Color;
+import scene.Scene;
+
+public class Render {
+
+ private Scene scene;
+ private Camera camera;
+ private ImageWriter imageWriter;
+ private RayTracerBase rayTracer;
+
+ public void renderImage() {}
+
+
+
+
+ public void printGrid(int i, Color color) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void writeToImage() {
+ // TODO Auto-generated method stub
+
+ }
+
+
+
+
+ public Render setImageWriter(ImageWriter imageWriter2) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+
+
+
+ public Render setScene(Scene scene2) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+
+
+
+ public Render setCamera(Camera camera2) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+
+
+
+ public Render setRayTracer(RayTracerBasic rayTracerBasic) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+
+
+
+
+
+}
diff --git a/src/scene/Scene.java b/src/scene/Scene.java
new file mode 100644
index 0000000..49923c9
--- /dev/null
+++ b/src/scene/Scene.java
@@ -0,0 +1,58 @@
+package scene;
+
+import elements.AmbientLight;
+import geometries.Geometries;
+import primitives.Color;
+
+public class Scene {
+
+ public String name;
+ public Color background;
+ public AmbientLight ambientLight;
+ public Geometries geometries;
+
+ /**
+ * @param name
+ */
+ public Scene(String name) {
+
+ this.name = name;
+ }
+
+
+
+ /**
+ * @param background the background to set
+ * @return
+ */
+ public Scene setBackground(Color background) {
+ this.background = background;
+ return this;
+ }
+
+
+ /**
+ * @param ambientLight the ambientLight to set
+ * @return
+ */
+ public Scene setAmbientLight(AmbientLight ambientLight) {
+ this.ambientLight = ambientLight;
+ return this;
+ }
+
+
+
+ /**
+ * @param geometries the geometries to set
+ * @return
+ */
+ public Scene setGeometries(Geometries geometries) {
+ this.geometries = geometries;
+ return this;
+ }
+
+
+
+
+
+}
diff --git a/src/unittests/ImageWriterTest.java b/src/unittests/ImageWriterTest.java
new file mode 100644
index 0000000..025f289
--- /dev/null
+++ b/src/unittests/ImageWriterTest.java
@@ -0,0 +1,32 @@
+/**
+ *
+ */
+package unittests;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+/**
+ * @author Adiel
+ *
+ */
+public class ImageWriterTest {
+
+ /**
+ * Test method for {@link renderer.ImageWriter#writeToImage()}.
+ */
+ @Test
+ public void testWriteToImage() {
+ fail("Not yet implemented");
+ }
+
+ /**
+ * Test method for {@link renderer.ImageWriter#writePixel(int, int, primitives.Color)}.
+ */
+ @Test
+ public void testWritePixel() {
+ fail("Not yet implemented");
+ }
+
+}
diff --git a/src/unittests/CameraTests.java b/src/unittests/elements/CameraTests.java
similarity index 98%
rename from src/unittests/CameraTests.java
rename to src/unittests/elements/CameraTests.java
index 13fb558..1cb7c21 100644
--- a/src/unittests/CameraTests.java
+++ b/src/unittests/elements/CameraTests.java
@@ -1,4 +1,4 @@
-package unittests;
+package unittests.elements;
import static org.junit.Assert.*;
import org.junit.Test;
diff --git a/src/unittests/IntegrationTests.java b/src/unittests/elements/IntegrationTests.java
similarity index 98%
rename from src/unittests/IntegrationTests.java
rename to src/unittests/elements/IntegrationTests.java
index e184f25..b95e6e2 100644
--- a/src/unittests/IntegrationTests.java
+++ b/src/unittests/elements/IntegrationTests.java
@@ -1,4 +1,4 @@
-package unittests;
+package unittests.elements;
import geometries.*;
import elements.Camera;
diff --git a/src/unittests/CylinderTests.java b/src/unittests/geometries/CylinderTests.java
similarity index 89%
rename from src/unittests/CylinderTests.java
rename to src/unittests/geometries/CylinderTests.java
index 93e3a92..6cb2ad9 100644
--- a/src/unittests/CylinderTests.java
+++ b/src/unittests/geometries/CylinderTests.java
@@ -1,7 +1,7 @@
/**
*
*/
-package unittests;
+package unittests.geometries;
//import static org.junit.Assert.*;
diff --git a/src/unittests/GeometriesTests.java b/src/unittests/geometries/GeometriesTests.java
similarity index 98%
rename from src/unittests/GeometriesTests.java
rename to src/unittests/geometries/GeometriesTests.java
index 7d81864..3415ba3 100644
--- a/src/unittests/GeometriesTests.java
+++ b/src/unittests/geometries/GeometriesTests.java
@@ -1,7 +1,7 @@
/**
*
*/
-package unittests;
+package unittests.geometries;
import static org.junit.Assert.*;
diff --git a/src/unittests/PlaneTests.java b/src/unittests/geometries/PlaneTests.java
similarity index 98%
rename from src/unittests/PlaneTests.java
rename to src/unittests/geometries/PlaneTests.java
index e50ee3e..1064d31 100644
--- a/src/unittests/PlaneTests.java
+++ b/src/unittests/geometries/PlaneTests.java
@@ -1,7 +1,7 @@
/**
*Tests for a Plane class
*/
-package unittests;
+package unittests.geometries;
import static org.junit.Assert.*;
diff --git a/src/unittests/PolygonTests.java b/src/unittests/geometries/PolygonTests.java
similarity index 99%
rename from src/unittests/PolygonTests.java
rename to src/unittests/geometries/PolygonTests.java
index 677c937..a1a4952 100644
--- a/src/unittests/PolygonTests.java
+++ b/src/unittests/geometries/PolygonTests.java
@@ -1,7 +1,7 @@
/**
*
*/
-package unittests;
+package unittests.geometries;
import static org.junit.Assert.*;
diff --git a/src/unittests/SphereTests.java b/src/unittests/geometries/SphereTests.java
similarity index 99%
rename from src/unittests/SphereTests.java
rename to src/unittests/geometries/SphereTests.java
index 4a2535c..c0502c8 100644
--- a/src/unittests/SphereTests.java
+++ b/src/unittests/geometries/SphereTests.java
@@ -1,7 +1,7 @@
/**
* Tests for a Sphere class
*/
-package unittests;
+package unittests.geometries;
import static org.junit.Assert.*;
diff --git a/src/unittests/TriangleTests.java b/src/unittests/geometries/TriangleTests.java
similarity index 98%
rename from src/unittests/TriangleTests.java
rename to src/unittests/geometries/TriangleTests.java
index d1ed1ad..e236428 100644
--- a/src/unittests/TriangleTests.java
+++ b/src/unittests/geometries/TriangleTests.java
@@ -1,7 +1,7 @@
/**
*Tests for a Triangle class
*/
-package unittests;
+package unittests.geometries;
import static org.junit.Assert.*;
diff --git a/src/unittests/TubeTests.java b/src/unittests/geometries/TubeTests.java
similarity index 96%
rename from src/unittests/TubeTests.java
rename to src/unittests/geometries/TubeTests.java
index e2569be..f1adf55 100644
--- a/src/unittests/TubeTests.java
+++ b/src/unittests/geometries/TubeTests.java
@@ -1,7 +1,7 @@
/**
* Tests for a Tube class
*/
-package unittests;
+package unittests.geometries;
import static org.junit.Assert.*;
import org.junit.Test;
diff --git a/src/unittests/Point3DTests.java b/src/unittests/primitives/Point3DTests.java
similarity index 97%
rename from src/unittests/Point3DTests.java
rename to src/unittests/primitives/Point3DTests.java
index f609c2e..0c8da58 100644
--- a/src/unittests/Point3DTests.java
+++ b/src/unittests/primitives/Point3DTests.java
@@ -1,7 +1,7 @@
/**
* Tests for a Point3D class
*/
-package unittests;
+package unittests.primitives;
import static org.junit.Assert.*;
diff --git a/src/unittests/VectorTests.java b/src/unittests/primitives/VectorTests.java
similarity index 99%
rename from src/unittests/VectorTests.java
rename to src/unittests/primitives/VectorTests.java
index 9cb474b..6169079 100644
--- a/src/unittests/VectorTests.java
+++ b/src/unittests/primitives/VectorTests.java
@@ -1,7 +1,7 @@
/**
*Tests for a Vector class
*/
-package unittests;
+package unittests.primitives;
import static org.junit.Assert.*;
diff --git a/src/unittests/renderer/RenderTests.java b/src/unittests/renderer/RenderTests.java
new file mode 100644
index 0000000..96fe361
--- /dev/null
+++ b/src/unittests/renderer/RenderTests.java
@@ -0,0 +1,72 @@
+package unittests.renderer;
+
+import org.junit.Test;
+
+import elements.*;
+import geometries.*;
+import primitives.*;
+import renderer.*;
+import scene.Scene;
+
+/**
+ * Test rendering a basic image
+ *
+ * @author Dan
+ */
+public class RenderTests {
+ private Camera camera = new Camera(Point3D.ZERO, new Vector(0, 0, -1), new Vector(0, -1, 0)) //
+ .setVpDistance(100) //
+ .setViewPlaneSize(500, 500);
+
+ /**
+ * Produce a scene with basic 3D model and render it into a jpeg image with a
+ * grid
+ */
+ @Test
+ public void basicRenderTwoColorTest() {
+
+ Scene scene = new Scene("Test scene")//
+ .setAmbientLight(new AmbientLight(new Color(255, 191, 191), 1)) //
+ .setBackground(new Color(75, 127, 90));
+
+ scene.geometries.add(new Sphere(50, new Point3D(0, 0, -100)),
+ new Triangle(new Point3D(-100, 0, -100), new Point3D(0, 100, -100), new Point3D(-100, 100, -100)), // up left
+ new Triangle(new Point3D(100, 0, -100), new Point3D(0, 100, -100), new Point3D(100, 100, -100)), // up right
+ new Triangle(new Point3D(-100, 0, -100), new Point3D(0, -100, -100), new Point3D(-100, -100, -100)), // down left
+ new Triangle(new Point3D(100, 0, -100), new Point3D(0, -100, -100), new Point3D(100, -100, -100))); // down right
+
+ ImageWriter imageWriter = new ImageWriter("base render test", 1000, 1000);
+ Render render = new Render() //
+ .setImageWriter(imageWriter) //
+ .setScene(scene) //
+ .setCamera(camera) //
+ .setRayTracer(new RayTracerBasic(scene));
+
+ render.renderImage();
+ render.printGrid(100, new Color(java.awt.Color.YELLOW));
+ render.writeToImage();
+ }
+
+ /**
+ * Test for XML based scene - for bonus
+ */
+ @Test
+ public void basicRenderXml() {
+ Scene scene = new Scene("XML Test scene");
+ // enter XML file name and parse from XML file into scene object
+ // ...
+
+ ImageWriter imageWriter = new ImageWriter("xml render test", 1000, 1000);
+ Render render = new Render() //
+ .setImageWriter(imageWriter) //
+ .setScene(scene) //
+ .setCamera(camera) //
+ .setRayTracer(new RayTracerBasic(scene));
+
+ render.renderImage();
+ render.printGrid(100, new Color(java.awt.Color.YELLOW));
+ render.writeToImage();
+ }
+
+
+}