-
Notifications
You must be signed in to change notification settings - Fork 8
/
csg.cpp
145 lines (121 loc) · 3.61 KB
/
csg.cpp
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
#include "csg_private.hpp"
namespace csg {
std::vector<triangle_t> triangulate(const fragment_t& fragment) {
int vertex_count = fragment.vertices.size();
std::vector<int> indices;
std::vector<triangle_t> tris;
indices.resize(vertex_count);
for (int i=0; i<vertex_count; ++i)
indices[i] = i;
for (;;) {
int n = indices.size();
if (n < 3)
break;
if (n == 3) {
tris.push_back(triangle_t{indices[0], indices[1], indices[2]});
break;
}
tris.push_back(triangle_t{indices[0], indices[1], indices[2]});
tris.push_back(triangle_t{indices[2], indices[3], indices[4 % n]});
indices.erase(indices.begin() + 3);
indices.erase(indices.begin() + 1);
}
return tris;
}
volume_operation_t make_fill_operation(volume_t with) {
return [=](volume_t) { return with; };
}
volume_operation_t make_convert_operation(volume_t from, volume_t to) {
return [=](volume_t old) {
return (old == from)? to: old;
};
}
void brush_t::set_planes(const std::vector<plane_t>& planes) {
this->planes = planes;
world->need_face_and_box_rebuild.insert(this);
for (brush_t* intersecting: intersecting_brushes)
world->need_fragment_rebuild.insert(intersecting);
}
const std::vector<plane_t>& brush_t::get_planes() const {
return planes;
}
void brush_t::set_volume_operation(const volume_operation_t& volume_operation) {
this->volume_operation = volume_operation;
world->need_fragment_rebuild.insert(this);
for (brush_t* intersecting: intersecting_brushes)
world->need_fragment_rebuild.insert(intersecting);
}
const std::vector<face_t>& brush_t::get_faces() const {
return faces;
}
void brush_t::set_time(int time) {
this->time = time;
world->need_fragment_rebuild.insert(this);
for (brush_t* intersecting: intersecting_brushes)
world->need_fragment_rebuild.insert(intersecting);
}
int brush_t::get_time() const {
return time;
}
int brush_t::get_uid() const {
return uid;
}
box_t brush_t::get_box() const {
return box;
}
world_t::world_t() {
sentinel = new brush_t;
sentinel->next = sentinel;
sentinel->prev = sentinel;
void_volume = 0;
next_uid = 0;
}
world_t::~world_t() {
brush_t *b = first();
while (b) {
brush_t *n = next(b);
delete b;
b = n;
}
delete sentinel;
}
brush_t *world_t::first() {
return (sentinel->next == sentinel)? nullptr: sentinel->next;
}
brush_t *world_t::next(brush_t *brush) {
return (brush->next == sentinel)? nullptr: brush->next;
}
void world_t::remove(brush_t *brush) {
brush_t *prev = brush->prev;
brush_t *next = brush->next;
prev->next = next;
next->prev = prev;
delete brush;
}
brush_t *world_t::add() {
brush_t *brush = new brush_t;
brush->world = this;
brush->volume_operation = std::identity{};
brush->box = box_t{ glm::vec3(1,1,1), glm::vec3(-1,-1,-1) };
brush->uid = next_uid++;
brush->time = 0;//brush->uid;
brush_t *after = sentinel->prev;
brush_t *next = after->next;
after->next = brush;
next->prev = brush;
brush->next = next;
brush->prev = after;
return brush;
}
void world_t::set_void_volume(volume_t void_volume) {
this->void_volume = void_volume;
brush_t *b = first();
while (b) {
need_fragment_rebuild.insert(b);
b = next(b);
}
}
volume_t world_t::get_void_volume() const {
return void_volume;
}
}