-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlayer.h
156 lines (134 loc) · 3.71 KB
/
layer.h
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
/*
C-PCB
Copyright (C) 2015 Chris Hinsley
chris (dot) hinsley (at) gmail (dot) com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef LAYER_H
#define LAYER_H
#include "mymath.h"
#include <memory>
extern double length_2d(const point_2d &p);
extern double det_2d(const point_2d &p1, const point_2d &p2);
extern double dot_2d(const point_2d &p1, const point_2d &p2);
extern point_2d sub_2d(const point_2d &p1, const point_2d &p2);
extern point_2d perp_2d(const point_2d &p);
extern point_2d scale_2d(const point_2d &p, double s);
extern bool collide_thick_lines_2d(const point_2d &tl1_p1, const point_2d &tl1_p2,
const point_2d &tl2_p1, const point_2d &tl2_p2, double r);
//layer class
class layer
{
public:
struct dims
{
int m_width;
int m_height;
};
struct line
{
bool operator==(const line &l) const
{
return std::tie(m_p1, m_p2, m_radius, m_gap) == std::tie(l.m_p1, l.m_p2, l.m_radius, l.m_gap);
}
point_2d m_p1;
point_2d m_p2;
double m_radius;
double m_gap;
};
struct record
{
record(int id, const line &l)
: m_id(id)
, m_line(l)
{
auto pv = perp_2d(sub_2d(l.m_p2, l.m_p1));
auto len = length_2d(pv);
m_lv_norm = scale_2d(pv, 1.0 / len);
m_lv_dist = dot_2d(m_lv_norm, l.m_p1);
m_pv_norm = perp_2d(m_lv_norm);
m_pv_dist1 = dot_2d(m_pv_norm, l.m_p1);
m_pv_dist2 = m_pv_dist1 - len;
}
auto hit(const line &l, double d)
{
auto dp1 = dot_2d(m_lv_norm, l.m_p1) - m_lv_dist;
auto dp2 = dot_2d(m_lv_norm, l.m_p2) - m_lv_dist;
if (dp1 > d && dp2 > d) return false;
if (dp1 < -d && dp2 < -d) return false;
dp1 = dot_2d(m_pv_norm, l.m_p1);
dp2 = dot_2d(m_pv_norm, l.m_p2);
if (dp1 - m_pv_dist1 > d && dp2 - m_pv_dist1 > d) return false;
if (dp1 - m_pv_dist2 < -d && dp2 - m_pv_dist2 < -d) return false;
return collide_thick_lines_2d(l.m_p1, l.m_p2, m_line.m_p1, m_line.m_p2, d);
}
int m_id;
line m_line;
point_2d m_lv_norm;
double m_lv_dist;
point_2d m_pv_norm;
double m_pv_dist1;
double m_pv_dist2;
};
struct aabb
{
int m_minx;
int m_miny;
int m_maxx;
int m_maxy;
};
typedef std::vector<std::shared_ptr<record>> bucket;
typedef std::vector<bucket> buckets;
layer(const dims &dims, double s);
~layer();
aabb get_aabb(const line &l);
void add_line(const line &l);
void sub_line(const line &l);
bool hit_line(const line &l);
private:
int m_width;
int m_height;
double m_scale;
buckets m_buckets;
int m_test;
};
//layers class
class layers
{
public:
struct dims
{
int m_width;
int m_height;
int m_depth;
};
struct line
{
point_3d m_p1;
point_3d m_p2;
double m_radius;
double m_gap;
};
layers(const dims &dims, double s);
~layers();
void add_line(const point_3d &p1, const point_3d &p2, double r, double g);
void sub_line(const point_3d &p1, const point_3d &p2, double r, double g);
bool hit_line(const point_3d &p1, const point_3d &p2, double r, double g);
void add_line(const line &l);
void sub_line(const line &l);
bool hit_line(const line &l);
private:
int m_depth;
std::vector<std::shared_ptr<layer>> m_layers;
};
#endif