/usr/src/castle-game-engine-6.4/3d/castlespheresampling.pas is in castle-game-engine-src 6.4+dfsg1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | {
Copyright 2003-2017 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Random sampling of points (directions) on a sphere and hemisphere.
Useful e.g. for ray-tracers.
Most of the implementation based on "Global Illumination Compendium" IV.B
[http://www.cs.kuleuven.ac.be/~phil/GI/].
See also images there, they help to illustrate the meaning of some
functions here.
We use two ways to represent points on hemisphere:
@orderedList(
@item(Functions @italic(without "XYZ" suffix) return vector
of 2 floats = two angles, called phi and theta (in this order).
Phi (in [0, 2*Pi]) is an angle from some chosen meridian.
Theta (in [0, Pi/2] for hemisphere and [0, Pi] for sphere)
is an angle from chosen vector pointing outward from the (hemi)sphere.
See images in "Global Illumination Compendium",
they are probably much easier to understand than this definition.)
@item(Functions @italic(with "XYZ" suffix) return
3D point x, y, z.
@unorderedList(
@item (0, 0, 0) is the center of (hemi)sphere,
@item (0, 0, 1) is the chosen outward vector (i.e. Theta = 0 there),
@item (1, 0, 0) is the direction where Phi = 0 and Theta = Pi/2,
@item (0, 1, 0) is the direction where Phi = Pi/2 and Theta = Pi/2.
)
This is matching conventions in "Global Illumination Compendium",
see there (point 21).)
)
Functions with Density <> Const return PdfValue for returned point,
i.e. for density p(Theta) it's PfdValue = p(Result[1]).
These functions try to calculate PdfValue smartly (often calculating
PfdValue and calculating Result uses the same intermediate calculation,
so we can save some computation). PdfValue is needed for importance sampling.
}
unit CastleSphereSampling;
{$I castleconf.inc}
interface
uses CastleVectors, CastleUtils;
{ Convert from PhiTheta representation of (hemi)sphere direction to
XYZ representation.
See the beginning of this unit's documentation, CastleSphereSampling,
for more precise description of XYZ representation. }
function PhiThetaToXYZ(const PhiTheta: TVector2; const SphereRadius: Single)
:TVector3; overload;
{ Convert from PhiTheta representation of (hemi)sphere direction to
XYZ representation.
This is the more advanced version where you can freely specify which
vector is the "main outside (hemi)sphere vector", SphereTheta0.
Points with Theta = 0 are exactly on SphereTheta0.
It is @italic(undefined) where point like (Phi = 0, Theta = Pi/2)
(or any other point with Theta <> 0) will be placed,
i.e. it's not defined where's the "chosen meridian" for Phi = 0.
However @italic(it's defined that this meridian will be determined only by
SphereTheta0), and this is usually sufficient (since this makes sure
that sampling and then converting to XYZ multiple points with the same
SphereTheta0 will preserve sampled density).
Note that the length of SphereTheta0 determines also the sphere radius. }
function PhiThetaToXYZ(const PhiTheta: TVector2;
const SphereTheta0: TVector3): TVector3; overload;
{ Convert from XYZ representation of (hemi)sphere direction to PhiTheta. }
function XYZToPhiTheta(const XYZ: TVector3): TVector2;
{ Random point (direction) on unit hemisphere, sampled with
constant density (p(Theta) = 1/2*Pi).
@groupBegin }
function RandomHemispherePointConst: TVector2;
function RandomHemispherePointConstXYZ: TVector3;
{ @groupEnd }
{ Random point (direction) on unit hemisphere, sampled with
density p(Theta) = cos(Theta)/Pi.
@groupBegin }
function RandomHemispherePointCosTheta(
out PdfValue: Single): TVector2;
function RandomHemispherePointCosThetaXYZ(
out PdfValue: Single): TVector3;
{ @groupEnd }
{ Random point (direction) on unit hemisphere, sampled with
density p(Theta) = (n+1) * (cos(Theta))^n / 2*Pi.
@groupBegin }
function RandomHemispherePointCosThetaExp(const n: Single;
out PdfValue: Single): TVector2;
function RandomHemispherePointCosThetaExpXYZ(const n: Single;
out PdfValue: Single): TVector3;
{ @groupEnd }
implementation
{ Note: Math unit operates of Float type. We try to minimize in some routines
below conversions Single <-> Float. }
uses Math;
function PhiThetaToXYZ(const PhiTheta: TVector2; const SphereRadius: Single): TVector3;
var
SinPhi, CosPhi, SinTheta, CosTheta: Float;
begin
SinCos(PhiTheta[0], SinPhi, CosPhi);
SinCos(PhiTheta[1], SinTheta, CosTheta);
result[0] := SphereRadius * CosPhi * SinTheta;
result[1] := SphereRadius * SinPhi * SinTheta;
result[2] := SphereRadius * CosTheta;
end;
function XYZToPhiTheta(const XYZ: TVector3): TVector2;
begin
Result[0] := ArcTan2(XYZ[1], XYZ[0]);
Result[1] := ArcTan2(Sqrt(Sqr(XYZ[0]) + Sqr(XYZ[1])), XYZ[2]);
end;
function PhiThetaToXYZ(const PhiTheta: TVector2; const SphereTheta0: TVector3): TVector3;
var
NewX, NewY: TVector3;
SphereRadius, NewXLen, NewYLen: Single;
begin
result := PhiThetaToXYZ(PhiTheta, 1);
{ make NewX anything orthogonal (but not zero) to SphereTheta0. }
if IsZero(SphereTheta0[0]) and IsZero(SphereTheta0[1]) then
begin
{ then we're sure that SphereTheta0[2] <> 0, so NewX will not be zero }
NewX[0] := 0;
NewX[1] := -SphereTheta0[2];
NewX[2] := SphereTheta0[1];
end else
begin
NewX[0] := -SphereTheta0[1];
NewX[1] := SphereTheta0[0];
NewX[2] := 0;
end;
NewY := TVector3.CrossProduct(SphereTheta0, NewX);
{ set correct lengths for NewX and NewY. We calculate NewYLen fast, without
any Sqrt (which would happen inside VectorLen), because we know that
NewY was calculated by TVector3.CrossProduct above and that NewX and SphereTheta0
are orthogonal. }
SphereRadius := SphereTheta0.Length;
NewXLen := NewX.Length;
NewYLen := NewXLen * SphereRadius;
NewX := NewX * SphereRadius/NewXLen;
NewY := NewY * SphereRadius/NewYLen;
{ TODO: create TMatrix4.MultPointVar to speed this a little bit? }
Result := TransformToCoordsMatrix(TVector3.Zero,
NewX,
NewY,
SphereTheta0).MultPoint(Result);
end;
function RandomHemispherePointConst: TVector2;
begin
result[0] := 2*Pi*Random;
result[1] := ArcCos(Random);
end;
function RandomHemispherePointConstXYZ: TVector3;
var
r1, r2, sqroot: Single;
cosinus, sinus: Float;
begin
r1 := Random;
r2 := Random;
SinCos(2*Pi*r1, sinus, cosinus);
sqroot := Sqrt(1-Sqr(r2));
result[0] := cosinus * sqroot;
result[1] := sinus * sqroot;
result[2] := r2;
end;
function RandomHemispherePointCosTheta(
out PdfValue: Single): TVector2;
var
SqrtR2: Float;
begin
SqrtR2 := Sqrt(Random);
result[0] := 2*Pi*Random;
result[1] := ArcCos(SqrtR2);
PdfValue := SqrtR2 / Pi;
end;
function RandomHemispherePointCosThetaXYZ(
out PdfValue: Single): TVector3;
var
SqRoot, r1, r2: Single;
SinR1, CosR1: Float;
begin
r1 := Random;
r2 := Random;
SinCos(2*Pi*r1, SinR1, CosR1);
SqRoot := Sqrt(1-r2);
result[0] := CosR1 * SqRoot;
result[1] := SinR1 * SqRoot;
result[2] := Sqrt(r2);
PdfValue := result[2];
end;
function RandomHemispherePointCosThetaExp(const n: Single;
out PdfValue: Single): TVector2;
var
r2: Float;
begin
r2 := Random;
result[0] := 2*Pi*Random;
result[1] := ArcCos(Power(r2, 1/(n+1)));
PdfValue := (n+1) * Power(r2, n/(n+1)) / 2*Pi;
end;
function RandomHemispherePointCosThetaExpXYZ(const n: Single;
out PdfValue: Single): TVector3;
var
r1, r2, r2Power, r2Root: Single;
SinR1, CosR1: Float;
begin
r1 := Random;
r2 := Random;
SinCos(2*Pi*r1, SinR1, CosR1);
r2Power := Power(r2, 1/(n+1));
r2Root := Sqrt(1-Sqr(r2Power));
result[0] := CosR1 * r2Root;
result[1] := SinR1 * r2Root;
result[2] := r2Power;
PdfValue := (n+1) * Power(r2, n/(n+1)) / 2*Pi;
end;
end.
|