00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "shape.h"
00026
00027 Shape::Shape(const Transform &o2w, bool ro)
00028 : ObjectToWorld(o2w), WorldToObject(o2w.GetInverse()),
00029 reverseOrientation(ro),
00030 transformSwapsHandedness(o2w.SwapsHandedness()) {
00031
00032 static StatsCounter nShapesMade("Geometry",
00033 "Total shapes created");
00034 ++nShapesMade;
00035 }
00036
00037 DifferentialGeometry::DifferentialGeometry(const Point &P,
00038 const Vector &DPDU, const Vector &DPDV,
00039 const Vector &DNDU, const Vector &DNDV,
00040 float uu, float vv, const Shape *sh)
00041 : p(P), dpdu(DPDU), dpdv(DPDV), dndu((Normal)DNDU), dndv((Normal)DNDV) {
00042
00043 nn = Normal(Normalize(Cross(dpdu, dpdv)));
00044 u = uu;
00045 v = vv;
00046 shape = sh;
00047 dudx = dvdx = dudy = dvdy = 0;
00048
00049 if (shape->reverseOrientation ^ shape->transformSwapsHandedness)
00050 nn *= -1.f;
00051 }
00052 void DifferentialGeometry::ComputeDifferentials(
00053 const RayDifferential &ray) const {
00054 if (ray.hasDifferentials) {
00055
00056
00057 float d = -Dot(nn, Vector(p.x, p.y, p.z));
00058 Vector rxv(ray.rx.o.x, ray.rx.o.y, ray.rx.o.z);
00059 float tx = -(Dot(nn, rxv) + d) / Dot(nn, ray.rx.d);
00060 Point px = ray.rx.o + tx * ray.rx.d;
00061 Vector ryv(ray.ry.o.x, ray.ry.o.y, ray.ry.o.z);
00062 float ty = -(Dot(nn, ryv) + d) / Dot(nn, ray.ry.d);
00063 Point py = ray.ry.o + ty * ray.ry.d;
00064 dpdx = px - p;
00065 dpdy = py - p;
00066
00067
00068 float A[2][2], Bx[2], By[2], x[2];
00069 int axes[2];
00070 if (fabsf(nn.x) > fabsf(nn.y) && fabsf(nn.x) > fabsf(nn.z)) {
00071 axes[0] = 1; axes[1] = 2;
00072 }
00073 else if (fabsf(nn.y) > fabsf(nn.z)) {
00074 axes[0] = 0; axes[1] = 2;
00075 }
00076 else {
00077 axes[0] = 0; axes[1] = 1;
00078 }
00079
00080 A[0][0] = dpdu[axes[0]];
00081 A[0][1] = dpdv[axes[0]];
00082 A[1][0] = dpdu[axes[1]];
00083 A[1][1] = dpdv[axes[1]];
00084 Bx[0] = px[axes[0]] - p[axes[0]];
00085 Bx[1] = px[axes[1]] - p[axes[1]];
00086 By[0] = py[axes[0]] - p[axes[0]];
00087 By[1] = py[axes[1]] - p[axes[1]];
00088 if (SolveLinearSystem2x2(A, Bx, x)) {
00089 dudx = x[0]; dvdx = x[1];
00090 }
00091 else {
00092 dudx = 1.; dvdx = 0.;
00093 }
00094 if (SolveLinearSystem2x2(A, By, x)) {
00095 dudy = x[0]; dvdy = x[1];
00096 }
00097 else {
00098 dudy = 0.; dvdy = 1.;
00099 }
00100 }
00101 else {
00102 dudx = dvdx = 0.;
00103 dudy = dvdy = 0.;
00104 dpdx = dpdy = Vector(0,0,0);
00105 }
00106 }