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
|
#include <stdlib.h>
#include "quad.h"
Quad::Quad() :
V(new std::vector<double>)
{
quads[0][0] =
quads[0][1] =
quads[1][0] =
quads[1][1] = 0;
unsigned int V_old_size = V->size();
unsigned int V_new_size = V_old_size + 9;
V->resize(V_new_size, 0.);
v1_offset = V_new_size - 3*3;
v2_offset = V_new_size - 2*3;
v_mid_offset = V_new_size - 1*3;
set_range(0., 1., 0., 1.);
}
Quad::Quad( std::vector<double> * const V_) :
V(V_)
{
quads[0][0] =
quads[0][1] =
quads[1][0] =
quads[1][1] = 0;
unsigned int V_old_size = V->size();
unsigned int V_new_size = V_old_size + 9;
V->resize(V_new_size, 0.);
v1_offset = V_new_size - 3*3;
v2_offset = V_new_size - 2*3;
v_mid_offset = V_new_size - 1*3;
set_range(0., 1., 0., 1.);
}
Quad::Quad(Quad &q) :
V(new std::vector<double>)
{
operator= (q);
}
Quad::~Quad()
{
if(is_split())
{
delete quads[0][0];
delete quads[0][1];
delete quads[1][0];
delete quads[1][1];
}
}
Quad& Quad::operator=(Quad &q)
{
*x1() = *q.x1();
*x2() = *q.x2();
*y1() = *q.y1();
*y2() = *q.y2();
if( q.is_split() )
{
split();
quads[0][0] = q.quads[0][0];
quads[0][1] = q.quads[0][1];
quads[1][0] = q.quads[1][0];
quads[1][1] = q.quads[1][1];
}
return *this;
}
void Quad::split()
{
if(is_split())
return;
quads[0][0] = new Quad(V);
quads[0][1] = new Quad(V);
quads[1][0] = new Quad(V);
quads[1][1] = new Quad(V);
quads[0][0]->set_range( *x1(), *x_mid(), *y1(), *y_mid() );
quads[0][1]->set_range( *x_mid(), *x2(), *y1(), *y_mid() );
quads[1][0]->set_range( *x1(), *x_mid(), *y_mid(), *y2() );
quads[1][1]->set_range( *x_mid(), *x2(), *y_mid(), *y2() );
}
bool Quad::is_split()
{
return quads[0][0] &&
quads[0][1] &&
quads[1][0] &&
quads[1][1];
}
void Quad::set_range(double nx1, double nx2, double ny1, double ny2)
{
*x1() = nx1;
*x2() = nx2;
*y1() = ny1;
*y2() = ny2;
*x_mid() = ( *x1() + *x2() )*0.5f;
*y_mid() = ( *y1() + *y2() )*0.5f;
}
void Quad::track_down(double x, double y, int levels)
{
if(levels>0)
{
int a = ( x < *x_mid() ) ? 0 : 1;
int b = ( y < *y_mid() ) ? 0 : 1;
if( !is_split() )
split();
quads[b][a]->track_down(x, y, levels-1);
}
}
Quad *Quad::get_at(double x, double y, int max_level, int level)
{
if(!is_split())
return this;
/*if(max_level>-1 && level>=max_level)
return this;*/
int a = ( x < *x_mid() ) ? 0 : 1;
int b = ( y < *y_mid() ) ? 0 : 1;
return quads[b][a]->get_at(x, y, max_level, level+1);
}
|