/**
* Java Modular Image Synthesis Toolkit (JMIST)
* Copyright (C) 2008-2013 Bradley W. Kimmel
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package ca.eandb.jmist.framework.lens;
import ca.eandb.jmist.framework.ScatteredRay;
import ca.eandb.jmist.framework.color.Color;
import ca.eandb.jmist.framework.path.EyeNode;
import ca.eandb.jmist.framework.path.EyeTerminalNode;
import ca.eandb.jmist.framework.path.PathInfo;
import ca.eandb.jmist.math.HPoint3;
import ca.eandb.jmist.math.Interval;
import ca.eandb.jmist.math.Optics;
import ca.eandb.jmist.math.Point2;
import ca.eandb.jmist.math.Point3;
import ca.eandb.jmist.math.Ray3;
import ca.eandb.jmist.math.Sphere;
import ca.eandb.jmist.math.Vector3;
/**
* A <code>Lens</code> that simulates an orthogonal projection of a mirrored
* sphere.
* @author Brad Kimmel
*/
public final class OmnimaxLens extends AbstractLens {
/** Serialization version ID. */
private static final long serialVersionUID = -4366154660419656383L;
/** The <code>Sphere</code> to bounce the orthogonally generated rays from. */
private static final Sphere LENS_SPHERE = new Sphere(new Point3(0, 0, 2), 1);
/* (non-Javadoc)
* @see ca.eandb.jmist.framework.Lens#sample(ca.eandb.jmist.math.Point2, ca.eandb.jmist.framework.path.PathInfo, double, double, double)
*/
@Override
public EyeNode sample(Point2 p, PathInfo pathInfo, double ru, double rv, double rj) {
return new Node(p, pathInfo, ru, rv, rj);
}
/**
* An <code>EyeNode</code> generated by a <code>OmnimaxLens</code>.
*/
private final class Node extends EyeTerminalNode {
/** Projected point on the image plane. */
private final Point2 pointOnImagePlane;
/**
* Creates a <code>Node</code>.
* @param pointOnImagePlane The <code>Point2</code> on the image plane.
* @param pathInfo The <code>PathInfo</code> describing the context for
* this node.
*/
public Node(Point2 pointOnImagePlane, PathInfo pathInfo, double ru, double rv, double rj) {
super(pathInfo, ru, rv, rj);
this.pointOnImagePlane = pointOnImagePlane;
}
/* (non-Javadoc)
* @see ca.eandb.jmist.framework.path.EyeNode#project(ca.eandb.jmist.math.HPoint3)
*/
@Override
public Point2 project(HPoint3 q) {
Vector3 v = q.isPoint() ? q.toPoint3().unitVectorFromOrigin()
: q.toVector3().unit();
Vector3 half = new Vector3(0.5 * v.x(), 0.5 * v.y(),
0.5 * (v.z() - 1.0)).unit();
double x = 0.5 * (half.x() + 1.0);
double y = 0.5 * (1.0 - half.y());
return new Point2(x, y);
}
/* (non-Javadoc)
* @see ca.eandb.jmist.framework.path.PathNode#getCosine(ca.eandb.jmist.math.Vector3)
*/
@Override
public double getCosine(Vector3 v) {
return 1.0;
}
/* (non-Javadoc)
* @see ca.eandb.jmist.framework.path.PathNode#getPDF()
*/
@Override
public double getPDF() {
return 1.0;
}
/* (non-Javadoc)
* @see ca.eandb.jmist.framework.path.PathNode#getPDF(ca.eandb.jmist.math.Vector3)
*/
@Override
public double getPDF(Vector3 v) {
return 1.0 / 16.0;
}
/* (non-Javadoc)
* @see ca.eandb.jmist.framework.path.PathNode#getPosition()
*/
@Override
public HPoint3 getPosition() {
return Point3.ORIGIN;
}
/* (non-Javadoc)
* @see ca.eandb.jmist.framework.path.PathNode#isSpecular()
*/
@Override
public boolean isSpecular() {
return true;
}
/* (non-Javadoc)
* @see ca.eandb.jmist.framework.path.PathNode#sample(double, double, double)
*/
@Override
public ScatteredRay sample(double ru, double rv, double rj) {
Point2 p = pointOnImagePlane;
double nx = 2.0 * (p.x() - 0.5);
double ny = 2.0 * (0.5 - p.y());
Ray3 init = new Ray3(new Point3(nx, ny, 1.0), Vector3.K);
Interval I = LENS_SPHERE.intersect(init);
if (I.isEmpty()) {
return null;
}
Vector3 n = LENS_SPHERE.center().vectorTo(init.pointAt(I.minimum()));
Vector3 r = Optics.reflect(init.direction(), n);
Ray3 ray = new Ray3(Point3.ORIGIN, r);
Color color = getWhite();
double pdf = 1.0 / 16.0;
return ScatteredRay.diffuse(ray, color, pdf);
}
/* (non-Javadoc)
* @see ca.eandb.jmist.framework.path.PathNode#scatter(ca.eandb.jmist.math.Vector3)
*/
@Override
public Color scatter(Vector3 v) {
return getGray(getPDF(v));
}
}
}