-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh.cpp
278 lines (231 loc) · 5.38 KB
/
mesh.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "mesh.h"
Mesh::Mesh()
{
}
Mesh::Mesh(std::string name)
{
this->name_ = name;
}
Mesh::Mesh(const Mesh * mesh)
{
this->name_ = mesh->name_;
this->positions_ = mesh->positions_;
this->normals_ = mesh->normals_;
this->texcoords_ = mesh->texcoords_;
this->faces_ = mesh->faces_;
}
Mesh::~Mesh()
{
faces_.clear();
texcoords_.clear();
normals_.clear();
positions_.clear();
}
void Mesh::Update(const std::vector<Vec3>& p, const std::vector<Vec3>& n)
{
this->positions_ = p;
this->normals_ = n;
changed_ = true;
}
void Mesh::Update(const Transform& t)
{
transform_ = t;
}
Mesh Mesh::operator*(Transform t)
{
Mesh mesh(this->name_);
mesh.faces_ = this->faces_;
mesh.texcoords_ = this->texcoords_;
for (auto& p : positions_) {
mesh.positions_.push_back(t * p);
}
for (auto& n : normals_) {
mesh.normals_.push_back(t * n);
}
return mesh;
}
void Mesh::operator+=(const Mesh & mesh)
{
this->positions_.insert(positions_.end(), mesh.positions_.begin(), mesh.positions_.end());
this->normals_.insert(normals_.end(), mesh.normals_.begin(), mesh.normals_.end());
this->texcoords_.insert(texcoords_.end(), mesh.texcoords_.begin(), mesh.texcoords_.end());
size_t size = this->positions_.size();
for (auto& f : mesh.faces_) {
faces_.push_back({ static_cast<unsigned>(f.v0 + size), static_cast<unsigned>(f.v1 + size),
static_cast<unsigned>(f.v2 + size) });
}
}
void Mesh::SetName(const std::string& name)
{
this->name_ = name;
}
void Mesh::SetMtl(const std::string& mtl)
{
mtl_name_ = mtl;
}
void Mesh::SetMtlFile(const std::string& path)
{
mtl_file_path_ = path;
}
void Mesh::AddVertice(const Vec3& v)
{
positions_.push_back(v);
}
void Mesh::AddNormal(const Vec3& n)
{
normals_.push_back(n);
}
void Mesh::AddUV(const Vec2 & uv)
{
texcoords_.push_back(uv);
has_texture_ = true;
}
void Mesh::AddFace(const Face& f)
{
faces_.push_back(f);
}
void Mesh::SetTransform(const Transform& t)
{
transform_ = t;
use_transform = true;
changed_ = true;
}
void Mesh::SetVisible(bool visible)
{
visible_ = visible;
}
void Mesh::SetChanged(bool changed)
{
changed_ = changed;
}
const Transform& Mesh::GetTransform()
{
return transform_;
}
bool Mesh::IsVisible()
{
return visible_;
}
bool Mesh::IsChanged()
{
return changed_;
}
const std::string & Mesh::GetName()
{
return name_;
}
const std::string & Mesh::GetMtl()
{
return mtl_name_;
}
const std::string & Mesh::GetMtlFilePath()
{
return mtl_file_path_;
}
const std::vector<Vec3>& Mesh::GetVertices()
{
return positions_;
}
const std::vector<Vec3>& Mesh::GetNormals()
{
return normals_;
}
const std::vector<Vec2>& Mesh::GetUVs()
{
return texcoords_;
}
const std::vector<Face>& Mesh::GetFaces()
{
return faces_;
}
std::vector<int> Mesh::FindUselessVertices() {
std::vector<int> list(positions_.size(), 1);
std::vector<int> res;
for (auto& f : faces_) {
list[f.v0 - 1] = 0;
list[f.v1 - 1] = 0;
list[f.v2 - 1] = 0;
}
for (int i = 0; i < list.size(); i++) {
if (list[i] == 1) {
res.push_back(i);
}
}
return res;
}
std::vector<int> Mesh::FindLostIndices(Mesh mesh)
{
std::vector<int> list;
for(int i = 0; i < this->positions_.size();i++){
if(std::find(mesh.positions_.begin(), mesh.positions_.end(), positions_[i])==mesh.positions_.end()){
list.push_back(i+1);
}
}
return list;
}
float CalcAngleBetween(const Vec3 &dir_a, const Vec3 &dir_b) {
float length_dot = dir_a.norm() * dir_b.norm();
if (length_dot < 0.0001f) {
length_dot = 0.0001f;
}
float f = dir_a.dot(dir_b) / length_dot;
f = f < -1.0 ? -1.0 : (f > 1.0 ? 1.0 : f);
return acosf(f);
}
void Mesh::CalculateNormal()
{
normals_.resize(positions_.size());
for(auto & n : normals_){
n=Vec3(0,0,0);
}
Vec3 pos[3];
Vec3 dir10,dir20;
Vec3 norm;
for(int i = 0;i < faces_.size()/3;i++ ){
pos[0] = positions_[faces_[i].v0];
pos[1] = positions_[faces_[i].v1];
pos[2] = positions_[faces_[i].v2];
dir10 = pos[1] - pos[0];
dir20 = pos[2] - pos[0];
norm = dir20.cross(dir10);
// std::cout<<"norm:\t"<<norm.x<<"\t"<<norm.y<<"\t"<<norm.z<<"\n";
for (int j = 0; j < 3; j++) {
// weight by angle to fix the L-Shape problem
float weight = CalcAngleBetween(pos[(j + 1) % 3] - pos[j], pos[(j + 2) % 3] - pos[j]);
if (weight <= 0.0f) {
weight = 0.0001f;
}
// std::cout<<"weight_"<<faces_[3*i+j]<<":\t"<<weight<<"\t";
if(j == 0)
normals_[faces_[i].v0] += norm * weight;
else if(j == 1)
normals_[faces_[i].v1] += norm * weight;
else
normals_[faces_[i].v2] += norm * weight;
}
// std::cout<<"\n";
}
}
MeshUnion::MeshUnion()
{
clear();
}
MeshUnion::~MeshUnion()
{
clear();
}
void MeshUnion::operator+=(const Mesh & mesh)
{
Mesh new_mesh = mesh;
for (auto &i : new_mesh.faces_) {
i.v0 += this->maxFaceId;
i.v1 += this->maxFaceId;
i.v2 += this->maxFaceId;
}
maxFaceId += mesh.positions_.size();
meshes.push_back(new_mesh);
}
void MeshUnion::clear()
{
meshes.clear();
}