summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/geom2d.cc
blob: eef902f14d23539368e58aee77fcc7c4e249d056 (plain) (blame)
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
/*
 * File:     geom2d.cc
 * Summary:  Plane geometry for shooting rays.
 */

#include "AppHdr.h"
#include "debug.h"
#include "geom2d.h"

#include <cmath>

namespace geom {

static bool double_is_zero(double d)
{
    return (std::abs(d) < 0.0000001);
}

// Is v parallel to the kernel of f?
static bool parallel(vector v, form f)
{
    return (double_is_zero(f(v)));
}

vector ray::shoot(double t) const
{
    return (start + dir * t);
}

void ray::advance(double t)
{
    start = shoot(t);
}

// Determine the value of t such that
//   r.start + t * r.dir lies on the line l.
// r and l must not be parallel.
double intersect(const ray &r, const line &l)
{
    ASSERT(!parallel(r.dir, l.f));
    double fp = l.f(r.start);
    double fd = l.f(r.dir);
    double t = (l.val - fp) / fd;
    return (t);
}

// Find the next intersection of r with a line in ls.
double nextintersect(const ray &r, const lineseq &ls)
{
    // ray must not be parallel to the lines
    ASSERT(!parallel(r.dir, ls.f));
    double fp = ls.f(r.start);
    double fd = ls.f(r.dir);
    // want smallest positive t with
    // fp + t*fd = ls.offset + k*ls.dist, k integral
    double a = (fp - ls.offset) / ls.dist;
    double k = (ls.dist*fd) > 0 ? ceil(a) : floor(a);
    double t = (k - a) * ls.dist / fd;
    return (t);
}

// Line distance in the ray parameter.
//   ls.f(r.shoot(tdist)) == ls.f(r.start) +- ls.dist
static double tdist(const ray &r, const lineseq &ls)
{
    ASSERT(!parallel(r.dir, ls.f));
    return (std::abs(ls.dist / ls.f(r.dir)));
}

// Shoot the ray inside the next cell.
// Returns true if succesfully advanced into a new cell,
// false if it hit a corner.
bool nextcell(ray &r, const grid &g)
{
    // ASSERT(!parallel(g.vert, g.horiz));
    lineseq ls;
    if (parallel(r.dir, g.ls1.f))
        ls = g.ls2;
    else if (parallel(r.dir, g.ls2.f))
        ls = g.ls1;
    else
    {
        double s = nextintersect(r, g.ls1);
        double t = nextintersect(r, g.ls2);
        double sd = tdist(r, g.ls1);
        double td = tdist(r, g.ls2);
        if (double_is_zero(s - t))
        {
            // Move onto the corner. The fact that we're on a corner
            // should be tracked externally.
            r.advance(s);
            return (false);
        }
        double dmin = std::min(s,t);
        double dnext = std::min(std::max(s,t), dmin + std::min(sd, td));
        r.advance((dmin + dnext) / 2.0);
        return (true);
    }
    // parallel: just advance an extra half
    double s = nextintersect(r, ls);
    r.advance(s + 0.5 * tdist(r, ls));
    return (true);
}

void movehalfcell(ray &r, const grid &g)
{
    // ASSERT(!parallel(g.vert, g.horiz));
    lineseq ls;
    double t;
    if (parallel(r.dir, g.ls1.f))
        t = tdist(r, g.ls2);
    else if (parallel(r.dir, g.ls2.f))
        t = tdist(r, g.ls1);
    else
        t = std::min(tdist(r, g.ls1), tdist(r, g.ls2));
    r.advance(0.5 * t);
}

// vector space implementation

const vector& vector::operator+=(const vector &v)
{
    x += v.x;
    y += v.y;
    return (*this);
}

vector vector::operator+(const vector &v) const
{
    vector copy = *this;
    copy += v;
    return (copy);
}

const vector& vector::operator*=(double t)
{
    x *= t;
    y *= t;
    return (*this);
}

vector vector::operator*(double t) const
{
    vector copy = *this;
    copy *= t;
    return (copy);
}

double form::operator()(const vector& v) const
{
    return (a*v.x + b*v.y);
}

}