public override double GenerateRay(CameraSample sample, ref Ray ray) { ++NumberOfRays; // Generate raster and camera samples Point pRas = new Point (sample.ImageX, sample.ImageY, 0); Point pCam = new Point (); RasterToCamera.Apply (pRas, ref pCam); ray = new Ray (new Point (), new Vector (pCam), 0.0, double.PositiveInfinity); if (LensRadius > 0.0) { double lensU = 0.0, lensV = 0.0; MonteCarlo.ConcentricSampleDisk (sample.LensU, sample.LensV, ref lensU, ref lensV); lensU *= LensRadius; lensV *= LensRadius; double ft = FocalDistance / ray.Direction.z; Point pFocus = ray.Apply (ft); ray.Origin = new Point (lensU, lensV, 0.0); ray.Direction = (pFocus - ray.Origin).Normalized; } ray.Time = Util.Lerp (sample.Time, ShutterOpen, ShutterClose); CameraToWorld.Apply (ray, ref ray); return 1.0; }
public override bool Intersect(Ray r, ref double tHit, ref double rayEpsilon, ref DifferentialGeometry dg) { // Transform _Ray_ to object space Ray ray = new Ray (); WorldToObject.Apply (r, ref ray); // Compute plane intersection for disk if (Math.Abs (ray.Direction.z) < 1E-07) return false; double thit = (Height - ray.Origin.z) / ray.Direction.z; if (thit < ray.MinT || thit > ray.MaxT) return false; // See if hit point is inside disk radii and $\phimax$ Point phit = ray.Apply (thit); double dist2 = phit.x * phit.x + phit.y * phit.y; if (dist2 > Radius * Radius || dist2 < InnerRadius * InnerRadius) return false; // Test disk $\phi$ value against $\phimax$ double phi = Math.Atan2 (phit.y, phit.x); if (phi < 0) phi += 2.0 * Util.Pi; if (phi > PhiMax) return false; // Find parametric representation of disk hit double u = phi / PhiMax; double v = 1.0 - ((Math.Sqrt (dist2) - InnerRadius) / (Radius - InnerRadius)); Vector dpdu = new Vector (-PhiMax * phit.y, PhiMax * phit.x, 0.0); Vector dpdv = new Vector (-phit.x / (1 - v), -phit.y / (1 - v), 0.0); dpdu *= PhiMax * Util.InvTwoPi; dpdv *= (Radius - InnerRadius) / Radius; Normal dndu = new Normal (0, 0, 0), dndv = new Normal (0, 0, 0); // Initialize _DifferentialGeometry_ from parametric information dg = new DifferentialGeometry (ObjectToWorld.Apply (phit), ObjectToWorld.Apply (dpdu), ObjectToWorld.Apply (dpdv), ObjectToWorld.Apply (dndu), ObjectToWorld.Apply (dndv), u, v, this); // Update _tHit_ for quadric intersection tHit = thit; // Compute _rayEpsilon_ for quadric intersection rayEpsilon = 0.0005 * tHit; return true; }
public override bool IntersectP(Ray ray) { Point p1 = Mesh.Points[Vertices[0]]; Point p2 = Mesh.Points[Vertices[1]]; Point p3 = Mesh.Points[Vertices[2]]; Vector e1 = p2 - p1; Vector e2 = p3 - p1; Vector s1 = ray.Direction % e2; double divisor = s1 ^ e1; if (divisor == 0.0) return false; double invDivisor = 1.0 / divisor; Vector d = ray.Origin - p1; double b1 = (d ^ s1) * invDivisor; if (b1 < 0.0 || b1 > 1.0) return false; Vector s2 = d % e1; double b2 = (ray.Direction ^ s2) * invDivisor; if (b2 < 0.0 || b1 + b2 > 1.0) return false; double t = (e2 ^ s2) * invDivisor; if (t < ray.MinT || t > ray.MaxT) return false; if (ray.Depth != -1) { Vector dpdu = new Vector (), dpdv = new Vector (); double[] uvs = new double[6]; GetUVs (uvs); double du1 = uvs[0] - uvs[4]; double du2 = uvs[2] - uvs[4]; double dv1 = uvs[1] - uvs[5]; double dv2 = uvs[3] - uvs[5]; Vector dp1 = p1 - p3, dp2 = p2 - p3; double determinant = du1 * dv2 - dv1 * du2; if (determinant == 0.0) Util.CoordinateSystem ((e2 % e1).Normalized, out dpdu, out dpdv); else { double invdet = 1.0 / determinant; dpdu = (dv2 * dp1 - dv1 * dp2) * invdet; dpdv = (-du2 * dp1 - du1 * dp2) * invdet; } double b0 = 1 - b1 - b2; double tu = b0 * uvs[0] + b1 * uvs[2] + b2 * uvs[4]; double tv = b0 * uvs[1] + b1 * uvs[3] + b2 * uvs[5]; DifferentialGeometry dgLocal = new DifferentialGeometry (ray.Apply (t), dpdu, dpdv, new Normal (), new Normal (), tu, tv, this); if (Mesh.AlphaTexture != null && Mesh.AlphaTexture.Evaluate (dgLocal) == 0.0) return false; } return true; }
public override bool IntersectP(Ray ray) { double rayT = 0.0, t = 0.0; if (Bounds.Inside (ray.Apply (ray.MinT))) rayT = ray.MinT; else if (!Bounds.IntersectP (ray, out rayT, out t)) return false; Point gridIntersect = ray.Apply (rayT); double[] nextCrossing = new double[3]; double[] delta = new double[3]; int[] Step = new int[3]; int[] Out = new int[3]; int[] Pos = new int[3]; for (int axis = 0; axis < 3; ++axis) { Pos[axis] = PositionToVoxel (gridIntersect, axis); if (ray.Direction[axis] >= 0) { nextCrossing[axis] = rayT + (VoxelToPos (Pos[axis] + 1, axis) - gridIntersect[axis]) / ray.Direction[axis]; delta[axis] = Width[axis] / ray.Direction[axis]; Step[axis] = 1; Out[axis] = nVoxels[axis]; } else { nextCrossing[axis] = rayT + (VoxelToPos (Pos[axis], axis) - gridIntersect[axis]) / ray.Direction[axis]; delta[axis] = -Width[axis] / ray.Direction[axis]; Step[axis] = -1; Out[axis] = -1; } } while (true) { Voxel voxel = Voxels[Offset (Pos[0], Pos[1], Pos[2])]; if (voxel != null) { if (voxel.IntersectP (ray)) return true; } int bits = (((nextCrossing[0] < nextCrossing[1]) ? 1 : 0) << 2) + (((nextCrossing[0] < nextCrossing[2]) ? 1 : 0) << 1) + (((nextCrossing[1] < nextCrossing[2]) ? 1 : 0)); int[] cmpTpAxis = new int[] { 2, 1, 2, 1, 2, 2, 0, 0 }; int stepAxis = cmpTpAxis[bits]; if (ray.MaxT < nextCrossing[stepAxis]) { break; } Pos[stepAxis] += Step[stepAxis]; if (Pos[stepAxis] == Out[stepAxis]) break; nextCrossing[stepAxis] += delta[stepAxis]; } return false; }
public Point Sample(Point p, LightSample lightSample, ref Normal Ns) { double temp = 0.0; int sn = AreaDistribution.SampleDiscrete (lightSample.uComponent, ref temp); Point pt = Shapes[sn].Sample (p, lightSample.uPos[0], lightSample.uPos[1], ref Ns); // Find closest intersection of ray with shapes in _ShapeSet_ Ray r = new Ray (p, pt - p, 0.001, double.PositiveInfinity); double rayEps = 0.0, thit = 1.0; bool anyHit = false; DifferentialGeometry dg = new DifferentialGeometry (); foreach (IShape shape in Shapes) anyHit |= shape.Intersect (r, ref thit, ref rayEps, ref dg); if (anyHit) Ns = new Normal (dg.n); return r.Apply (thit); }
public override bool Intersect(Ray r, ref double tHit, ref double rayEpsilon, ref DifferentialGeometry dg) { double phi; Point phit; // Transform _Ray_ to object space Ray ray = new Ray (); WorldToObject.Apply (r, ref ray); // Compute quadratic sphere coefficients double A = ray.Direction.x * ray.Direction.x + ray.Direction.y * ray.Direction.y + ray.Direction.z * ray.Direction.z; double B = 2 * (ray.Direction.x * ray.Origin.x + ray.Direction.y * ray.Origin.y + ray.Direction.z * ray.Origin.z); double C = ray.Origin.x * ray.Origin.x + ray.Origin.y * ray.Origin.y + ray.Origin.z * ray.Origin.z - Radius * Radius; // Solve quadratic equation for _t_ values double t0 = 0.0, t1 = 0.0; if (!Util.Quadratic (A, B, C, ref t0, ref t1)) return false; // Compute intersection distance along ray if (t0 > ray.MaxT || t1 < ray.MinT) return false; double thit = t0; if (t0 < ray.MinT) { thit = t1; if (thit > ray.MaxT) return false; } // Compute sphere hit position and $\phi$ phit = ray.Apply (thit); if (phit.x == 0.0 && phit.y == 0.0) phit.x = 1E-5 * Radius; phi = Math.Atan2 (phit.y, phit.x); if (phi < 0.0) phi += 2.0 * Util.Pi; // Test sphere intersection against clipping parameters if ((zMin > -Radius && phit.z < zMin) || (zMax < Radius && phit.z > zMax) || phi > PhiMax) { if (thit == t1) return false; if (t1 > ray.MaxT) return false; thit = t1; // Compute sphere hit position and $\phi$ phit = ray.Apply (thit); if (phit.x == 0.0 && phit.y == 0.0) phit.x = 1E-5f * Radius; phi = Math.Atan2 (phit.y, phit.x); if (phi < 0.0) phi += 2.0 * Util.Pi; if ((zMin > -Radius && phit.z < zMin) || (zMax < Radius && phit.z > zMax) || phi > PhiMax) return false; } // Find parametric representation of sphere hit double u = phi / PhiMax; double theta = Math.Acos (Util.Clamp (phit.z / Radius, -1.0, 1.0)); double v = (theta - ThetaMin) / (ThetaMax - ThetaMin); // Compute sphere $\dpdu$ and $\dpdv$ double zRadius = Math.Sqrt (phit.x * phit.x + phit.y * phit.y); double invzRadius = 1.0 / zRadius; double cosphi = phit.x * invzRadius; double sinphi = phit.y * invzRadius; Vector dpdu = new Vector (-PhiMax * phit.y, PhiMax * phit.x, 0); Vector dpdv = (ThetaMax - ThetaMin) * new Vector (phit.z * cosphi, phit.z * sinphi, -Radius * Math.Sin (theta)); // Compute sphere $\dndu$ and $\dndv$ Vector d2Pduu = -PhiMax * PhiMax * new Vector (phit.x, phit.y, 0); Vector d2Pduv = (ThetaMax - ThetaMin) * phit.z * PhiMax * new Vector (-sinphi, cosphi, 0.0); Vector d2Pdvv = -(ThetaMax - ThetaMin) * (ThetaMax - ThetaMin) * new Vector (phit.x, phit.y, phit.z); // Compute coefficients for fundamental forms double E = (dpdu ^ dpdu); double F = (dpdu ^ dpdv); double G = (dpdv ^ dpdv); Vector N = (dpdu % dpdv).Normalized; double e = (N ^ d2Pduu); double f = (N ^ d2Pduv); double g = (N ^ d2Pdvv); // Compute $\dndu$ and $\dndv$ from fundamental form coefficients double invEGF2 = 1.0 / (E * G - F * F); Normal dndu = new Normal ((f * F - e * G) * invEGF2 * dpdu + (e * F - f * E) * invEGF2 * dpdv); Normal dndv = new Normal ((g * F - f * G) * invEGF2 * dpdu + (f * F - g * E) * invEGF2 * dpdv); // Initialize _DifferentialGeometry_ from parametric information dg = new DifferentialGeometry (ObjectToWorld.Apply (phit), ObjectToWorld.Apply (dpdu), ObjectToWorld.Apply (dpdv), ObjectToWorld.Apply (dndu), ObjectToWorld.Apply (dndv), u, v, this); // Update _tHit_ for quadric intersection tHit = thit; // Compute _rayEpsilon_ for quadric intersection rayEpsilon = 0.0005 * tHit; return true; }
public override Point Sample(Point p, double u1, double u2, ref Normal Ns) { // Compute coordinate system for sphere sampling Point Pcenter = ObjectToWorld.Apply (new Point ()); Vector wc = (Pcenter - p).Normalized; Vector wcX, wcY; Util.CoordinateSystem (wc, out wcX, out wcY); // Sample uniformly on sphere if $\pt{}$ is inside it if (Util.DistanceSquared (p, Pcenter) - Radius * Radius < 0.0001) return Sample (u1, u2, ref Ns); // Sample sphere uniformly inside subtended cone double sinThetaMax2 = Radius * Radius / Util.DistanceSquared (p, Pcenter); double cosThetaMax = Math.Sqrt (Math.Max (0.0, 1.0 - sinThetaMax2)); DifferentialGeometry dgSphere = new DifferentialGeometry (); double thit = 0.0, rayEpsilon = 0.0; Point ps; Ray r = new Ray (p, MonteCarlo.UniformSampleCone (u1, u2, cosThetaMax, wcX, wcY, wc), 0.001); if (!Intersect (r, ref thit, ref rayEpsilon, ref dgSphere)) thit = ((Pcenter - p) ^ r.Direction.Normalized); ps = r.Apply (thit); Ns = new Normal ((ps - Pcenter).Normalized); if (ReverseOrientation) Ns *= -1.0; return ps; }
public override bool IntersectP(Ray r) { double phi; Point phit; // Transform _Ray_ to object space Ray ray = new Ray (); WorldToObject.Apply (r, ref ray); // Compute quadratic sphere coefficients double A = ray.Direction.x * ray.Direction.x + ray.Direction.y * ray.Direction.y + ray.Direction.z * ray.Direction.z; double B = 2 * (ray.Direction.x * ray.Origin.x + ray.Direction.y * ray.Origin.y + ray.Direction.z * ray.Origin.z); double C = ray.Origin.x * ray.Origin.x + ray.Origin.y * ray.Origin.y + ray.Origin.z * ray.Origin.z - Radius * Radius; // Solve quadratic equation for _t_ values double t0 = 0.0, t1 = 0.0; if (!Util.Quadratic (A, B, C, ref t0, ref t1)) return false; // Compute intersection distance along ray if (t0 > ray.MaxT || t1 < ray.MinT) return false; double thit = t0; if (t0 < ray.MinT) { thit = t1; if (thit > ray.MaxT) return false; } // Compute sphere hit position and $\phi$ phit = ray.Apply (thit); if (phit.x == 0.0 && phit.y == 0.0) phit.x = 1E-5 * Radius; phi = Math.Atan2 (phit.y, phit.x); if (phi < 0.0) phi += 2.0 * Util.Pi; // Test sphere intersection against clipping parameters if ((zMin > -Radius && phit.z < zMin) || (zMax < Radius && phit.z > zMax) || phi > PhiMax) { if (thit == t1) return false; if (t1 > ray.MaxT) return false; thit = t1; // Compute sphere hit position and $\phi$ phit = ray.Apply (thit); if (phit.x == 0.0 && phit.y == 0.0) phit.x = 1E-5 * Radius; phi = Math.Atan2 (phit.y, phit.x); if (phi < 0.0) phi += 2.0 * Util.Pi; if ((zMin > -Radius && phit.z < zMin) || (zMax < Radius && phit.z > zMax) || phi > PhiMax) return false; } return true; }
public virtual double Pdf(Point p, Vector wi) { DifferentialGeometry dgLight = new DifferentialGeometry (); Ray ray = new Ray (p, wi, 1e-3); ray.Depth = - 1; double thit = 0.0, rayEpsilon = 0.0; if (!Intersect (ray, ref thit, ref rayEpsilon, ref dgLight)) return 0.0; double pdf = Util.DistanceSquared (p, ray.Apply (thit)) / Util.AbsDot (dgLight.n, -wi) * Area; if (double.IsInfinity (pdf)) pdf = 0.0; return pdf; }
public override bool IntersectP(Ray r) { // Transform _Ray_ to object space Ray ray = new Ray (); WorldToObject.Apply (r, ref ray); // Compute plane intersection for disk if (Math.Abs (ray.Direction.z) < 1E-07) return false; double thit = (Height - ray.Origin.z) / ray.Direction.z; if (thit < ray.MinT || thit > ray.MaxT) return false; // See if hit point is inside disk radii and $\phimax$ Point phit = ray.Apply (thit); double dist2 = phit.x * phit.x + phit.y * phit.y; if (dist2 > Radius * Radius || dist2 < InnerRadius * InnerRadius) return false; // Test disk $\phi$ value against $\phimax$ double phi = Math.Atan2 (phit.y, phit.x); if (phi < 0) phi += 2.0 * Util.Pi; if (phi > PhiMax) return false; return true; }