Files
GR-raytracing/src/observer.c
T
2026-08-25 22:45:43 -04:00

68 lines
2.8 KiB
C

#include "observer.h"
#include <math.h>
#include <stddef.h>
static const double pi = 3.14159265358979323846;
ObserverState observer_fixed_at_origin(void) {
return (ObserverState){.coordinate_time = 0.0,
.coordinate_position = {0.0, 0.0, 0.0},
.tetrad = {{1.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, -1.0},
{0.0, 0.0, 1.0, 0.0},
{0.0, 1.0, 0.0, 0.0}}};
}
ObserverState observer_fixed_at_origin_look_at(double ra_deg, double dec_deg) {
const double ra = ra_deg * pi / 180.0;
const double dec = dec_deg * pi / 180.0;
const double cos_ra = cos(ra), sin_ra = sin(ra);
const double cos_dec = cos(dec), sin_dec = sin(dec);
const double forward[3] = {cos_dec * cos_ra, sin_dec, cos_dec * sin_ra};
const double up[3] = {-sin_dec * cos_ra, cos_dec, -sin_dec * sin_ra};
const double right[3] = {-sin_ra, 0.0, cos_ra};
return (ObserverState){.coordinate_time = 0.0,
.coordinate_position = {0.0, 0.0, 0.0},
.tetrad = {{1.0, 0.0, 0.0, 0.0},
{0.0, forward[0], forward[1], forward[2]},
{0.0, up[0], up[1], up[2]},
{0.0, right[0], right[1], right[2]}}};
}
int observer_static_schwarzschild_ks(double mass, double radius,
ObserverState *out) {
if (out == NULL || mass <= 0.0 || radius <= 2.0 * mass)
return -1;
const double f = 2.0 * mass / radius;
const double normalization = sqrt(1.0 - f);
*out = (ObserverState){
.coordinate_time = 0.0,
.coordinate_position = {radius, 0.0, 0.0},
/* e_(0) is the static four-velocity. e_(1) points inward; its time
* component makes the tetrad orthonormal in the KS metric. */
.tetrad = {{1.0 / normalization, 0.0, 0.0, 0.0},
{-f / normalization, -normalization, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0},
{0.0, 0.0, 0.0, 1.0}}};
return 0;
}
int observer_inward_schwarzschild_ks(double mass, double radius,
double inward_speed, ObserverState *out) {
ObserverState static_observer;
if (inward_speed < 0.0 || inward_speed >= 1.0 ||
observer_static_schwarzschild_ks(mass, radius, &static_observer))
return -1;
const double gamma = 1.0 / sqrt(1.0 - inward_speed * inward_speed);
*out = static_observer;
for (int mu = 0; mu < 4; ++mu) {
const double e0 = static_observer.tetrad[0][mu];
const double forward = static_observer.tetrad[1][mu];
out->tetrad[0][mu] = gamma * (e0 + inward_speed * forward);
out->tetrad[1][mu] = gamma * (inward_speed * e0 + forward);
}
return 0;
}