forked from gcherchi/InteractiveAndRobustMeshBooleans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-rotation.cpp
175 lines (157 loc) · 7.72 KB
/
main-rotation.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*****************************************************************************************
* MIT License *
* *
* Copyright (c) 2022 G. Cherchi, F. Pellacini, M. Attene and M. Livesu *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION *
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
* Authors: *
* Gianmarco Cherchi ([email protected]) *
* https://www.gianmarcocherchi.com *
* *
* Fabio Pellacini ([email protected]) *
* https://pellacini.di.uniroma1.it *
* *
* Marco Attene ([email protected]) *
* https://www.cnr.it/en/people/marco.attene/ *
* *
* Marco Livesu ([email protected]) *
* http://pers.ge.imati.cnr.it/livesu/ *
* *
* ***************************************************************************************/
#ifdef _MSC_VER // Workaround for known bugs and issues on MSVC
#define _HAS_STD_BYTE 0 // https://developercommunity.visualstudio.com/t/error-c2872-byte-ambiguous-symbol/93889
#define NOMINMAX // https://stackoverflow.com/questions/1825904/error-c2589-on-stdnumeric-limitsdoublemin
#endif
#include <cinolib/gl/glcanvas.h>
#include <cinolib/drawable_triangle_soup.h>
#include <thread>
#include "booleans.h"
int main(int argc, char **argv)
{
BoolOp op = UNION;
int vert_offset = 0;
std::vector<std::string> files = {"../data/bunny25k.obj", "../data/cow25k.obj"};
std::vector<double> in_coords, bool_coords;
std::vector<uint> in_tris, bool_tris;
std::vector<uint> in_labels;
std::vector<std::bitset<NBIT>> bool_labels;
std::vector<double> back_coords;
std::vector<uint> back_tris;
std::vector<std::bitset<NBIT>> back_labels;
std::cout << "Commands:" << std::endl;
std::cout << "- press I to toggle Intersection" << std::endl;
std::cout << "- press U to toggle Union" << std::endl;
std::cout << "- press S to toggle Subtraction" << std::endl;
std::cout << "- press W to toggle wireframe" << std::endl;
std::cout << "- press Space to Pause/Resume" << std::endl;
loadMultipleFiles(files, in_coords, in_tris, in_labels, vert_offset);
const cinolib::Color & c0 = cinolib::Color::PASTEL_ORANGE();
const cinolib::Color & c1 = cinolib::Color::PASTEL_CYAN();
uint n_tri = bool_tris.size()/3;
std::vector<cinolib::Color> tri_colors(n_tri, c0);
for(uint id = 0; id < n_tri; ++id)
{
if(bool_labels[id][1]) tri_colors[id] = c1;
}
for(uint i = vert_offset; i < in_coords.size(); ++i)
{
in_coords[i] += 1e-5;
}
cinolib::DrawableTriangleSoup soup(bool_coords, bool_tris, tri_colors);
cinolib::GLcanvas gui;
gui.push(&soup);
bool wireframe = false;
std::mutex mutex;
std::atomic<bool> pause = false;
gui.callback_key_pressed = [&](int key, int mod) -> bool
{
if(key==GLFW_KEY_I) op = INTERSECTION; else
if(key==GLFW_KEY_U) op = UNION; else
if(key==GLFW_KEY_S) op = SUBTRACTION; else
if(key==GLFW_KEY_SPACE) pause = !pause; else
if(key==GLFW_KEY_W) wireframe = !wireframe;
return false;
};
// boolean thread
std::atomic<bool> done = false;
std::atomic<bool> exit = false;
std::atomic<int> count = 0;
std::thread boolean_thread([&]()
{
while(!exit)
{
back_coords.clear();
back_tris.clear();
booleanPipeline(in_coords, in_tris, in_labels, op, back_coords, back_tris, back_labels);
{
std::lock_guard<std::mutex> lock(mutex);
back_coords.swap(bool_coords);
back_tris.swap(bool_tris);
back_labels.swap(bool_labels);
}
done = true; // next frame is ready to render
++count;
if(exit) return;
if(!pause) // rotate
{
static cinolib::mat3d R = cinolib::mat3d::ROT_3D(cinolib::vec3d(0,1,0), cinolib::to_rad(3));
for(uint i=vert_offset; i<in_coords.size(); i+=3)
{
cinolib::vec3d p = R * cinolib::vec3d(in_coords[i], in_coords[i+1], in_coords[i+2]);
in_coords[i ] = p[0];
in_coords[i+1] = p[1];
in_coords[i+2] = p[2];
}
}
}
});
std::promise<void> fps_exit_signal;
std::thread fps_thread([&]()
{
std::future<void> exit_condition = fps_exit_signal.get_future();
while(exit_condition.wait_for(std::chrono::milliseconds(1000)) == std::future_status::timeout)
{
count = 0;
}
});
// ui thread
glfwMakeContextCurrent(gui.window);
while(!glfwWindowShouldClose(gui.window))
{
if(done)
{
{
std::lock_guard<std::mutex> lock(mutex);
n_tri = bool_tris.size()/3;
tri_colors.resize(n_tri, c0);
for(uint id=0; id<n_tri; ++id)
tri_colors[id] = (bool_labels[id][0]) ? c0 : c1;
soup = cinolib::DrawableTriangleSoup(bool_coords, bool_tris, tri_colors, cinolib::Color::BLACK(), wireframe);
}
done = false;
}
gui.draw();
glfwPollEvents();
}
exit = true;
fps_exit_signal.set_value();
if(boolean_thread.joinable()) boolean_thread.join();
fps_thread.join();
return EXIT_SUCCESS;
}