-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobbStruct.cpp
273 lines (242 loc) · 8.06 KB
/
obbStruct.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
#include "calcUtils.h"
#include "obbStruct.h"
std::vector<CObbStruct*> CObbStruct::_obbStructs;
CObbStruct::CObbStruct()
{
}
CObbStruct::CObbStruct(const double* ver,int verSize,const int* ind,int indSize,double triSize,int triCnt)
{
_triCnt=triCnt;
if (triSize<=0.0)
triSize=0.0;
else
{
if (triSize<double(0.005))
triSize=double(0.005);
}
_triSize=triSize;
vertices.assign(ver,ver+verSize);
indices.assign(ind,ind+indSize);
_originalVerticesSize=verSize;
_originalIndicesSize=indSize;
_originalVerticesHash=CCalcUtils::getDjb2Hash(reinterpret_cast<const char*>(ver),size_t(verSize)*sizeof(double));
_originalIndicesHash=CCalcUtils::getDjb2Hash(reinterpret_cast<const char*>(ind),size_t(indSize)*sizeof(int));
reduceTriangleSizes(vertices,indices,_triSize);
std::vector<int> usedTraingles;
for (size_t i=0;i<indices.size()/3;i++)
usedTraingles.push_back(int(i));
obb=new CObbNode(vertices,indices,usedTraingles,size_t(_triCnt));
}
CObbStruct::~CObbStruct()
{
delete obb;
}
CObbStruct* CObbStruct::copyYourself() const
{
CObbStruct* newObbStruct=new CObbStruct();
newObbStruct->obb=obb->copyYourself();
newObbStruct->vertices.assign(vertices.begin(),vertices.end());
newObbStruct->indices.assign(indices.begin(),indices.end());
newObbStruct->_originalVerticesSize=_originalVerticesSize;
newObbStruct->_originalVerticesHash=_originalVerticesHash;
newObbStruct->_originalIndicesSize=_originalIndicesSize;
newObbStruct->_originalIndicesHash=_originalIndicesHash;
newObbStruct->_triCnt=_triCnt;
newObbStruct->_triSize=_triSize;
return(newObbStruct);
}
void CObbStruct::scaleYourself(double f)
{
obb->scaleYourself(f);
for (size_t i=0;i<vertices.size();i++)
vertices[i]*=f;
}
bool CObbStruct::isSame(const double* v,int vSize,const int* ind,int indSize,double triSize,int triCnt)
{
if (_originalVerticesSize!=vSize)
return(false);
if (_originalIndicesSize!=indSize)
return(false);
if (triSize!=_triSize)
return(false);
if (triCnt!=_triCnt)
return(false);
if (_originalVerticesHash==0)
return(false);
if (_originalIndicesHash==0)
return(false);
unsigned long hash=CCalcUtils::getDjb2Hash(reinterpret_cast<const char*>(v),size_t(vSize)*sizeof(double));
if (hash!=_originalVerticesHash)
return(false);
hash=CCalcUtils::getDjb2Hash(reinterpret_cast<const char*>(ind),size_t(indSize)*sizeof(int));
if (hash!=_originalIndicesHash)
return(false);
return(true);
}
unsigned char* CObbStruct::serialize(int& dataSize) const
{
std::vector<unsigned char> data;
data.push_back(3); // ser ver
pushData(data,&_triSize,sizeof(double));
pushData(data,&_triCnt,sizeof(int));
pushData(data,&_originalVerticesSize,sizeof(int));
pushData(data,&_originalVerticesHash,sizeof(unsigned long));
pushData(data,&_originalIndicesSize,sizeof(int));
pushData(data,&_originalIndicesHash,sizeof(unsigned long));
int s=int(vertices.size());
pushData(data,&s,sizeof(int));
for (size_t i=0;i<vertices.size();i++)
pushData(data,&vertices[i],sizeof(double));
s=int(indices.size());
pushData(data,&s,sizeof(int));
for (size_t i=0;i<indices.size();i++)
pushData(data,&indices[i],sizeof(int));
obb->serialize(data);
unsigned char* retVal=new unsigned char[data.size()];
for (size_t i=0;i<data.size();i++)
retVal[i]=data[i];
dataSize=int(data.size());
return(retVal);
}
bool CObbStruct::deserialize(const unsigned char* data)
{
int pos=0;
unsigned char ver=data[pos++];
if (ver<=3)
{
_triSize=(reinterpret_cast<const double*>(data+pos))[0];pos+=sizeof(double);
_triCnt=(reinterpret_cast<const int*>(data+pos))[0];pos+=sizeof(int);
_originalVerticesSize=(reinterpret_cast<const int*>(data+pos))[0];pos+=sizeof(int);
if (ver==2)
{
_originalVerticesHash=0;
pos+=sizeof(double);
}
else
{
_originalVerticesHash=(reinterpret_cast<const unsigned long*>(data+pos))[0];
pos+=sizeof(unsigned long);
}
_originalIndicesSize=(reinterpret_cast<const int*>(data+pos))[0];pos+=sizeof(int);
if (ver==2)
{
_originalIndicesHash=0;
pos+=sizeof(int);
}
else
{
_originalIndicesHash=(reinterpret_cast<const unsigned long*>(data+pos))[0];
pos+=sizeof(unsigned long);
}
int s;
s=(reinterpret_cast<const int*>(data+pos))[0];pos+=sizeof(int);
for (int i=0;i<s;i++)
{
vertices.push_back((reinterpret_cast<const double*>(data+pos))[0]);
pos+=sizeof(double);
}
s=(reinterpret_cast<const int*>(data+pos))[0];pos+=sizeof(int);
for (int i=0;i<s;i++)
{
indices.push_back((reinterpret_cast<const int*>(data+pos))[0]);
pos+=sizeof(int);
}
obb=new CObbNode();
obb->deserialize(data,pos);
return(true);
}
return(false);
}
CObbStruct* CObbStruct::copyObbStructFromExisting(const double* vert,int vertSize,const int* ind,int indSize,double triSize,int triCnt)
{
for (size_t i=0;i<_obbStructs.size();i++)
{
if (_obbStructs[i]->isSame(vert,vertSize,ind,indSize,triSize,triCnt))
return(_obbStructs[i]->copyYourself());
}
return(nullptr);
}
void CObbStruct::addObbStruct(CObbStruct* obbStruct)
{
_obbStructs.push_back(obbStruct);
}
void CObbStruct::removeObbStruct(CObbStruct* obbStruct)
{
for (size_t i=0;i<_obbStructs.size();i++)
{
if (_obbStructs[i]==obbStruct)
{
_obbStructs.erase(_obbStructs.begin()+i);
delete obbStruct;
break;
}
}
}
void CObbStruct::reduceTriangleSizes(std::vector<double>& vert,std::vector<int>& ind,double triSize)
{
if (triSize>0.0)
{
std::vector<int> t1(ind);
for (size_t loop=0;loop<8;loop++)
{
std::vector<int> t2;
for (size_t i=0;i<t1.size()/3;i++)
{
int indd[5];
indd[0]=t1[3*i+0];
indd[1]=t1[3*i+1];
indd[2]=t1[3*i+2];
indd[3]=indd[0];
indd[4]=indd[1];
C3Vector p1(&vert[3*size_t(indd[0])]);
C3Vector p2(&vert[3*size_t(indd[1])]);
C3Vector p3(&vert[3*size_t(indd[2])]);
C3Vector pts[5]={p1,p2,p3,p1,p2};
C3Vector edges[3];
double ll[3];
edges[0]=p2-p1;
edges[1]=p3-p2;
edges[2]=p1-p3;
ll[0]=edges[0].getLength();
ll[1]=edges[1].getLength();
ll[2]=edges[2].getLength();
int a=0;
if (ll[1]>ll[0])
{
a=1;
if (ll[2]>ll[1])
a=2;
}
else
{
if (ll[2]>ll[0])
a=2;
}
if (ll[a]>triSize)
{ // tri gets divided
C3Vector np(pts[a]+edges[a]*0.5);
t2.push_back(indd[a]+0);
t2.push_back(int(vert.size()/3));
t2.push_back(indd[a+2]);
t2.push_back(int(vert.size()/3));
t2.push_back(indd[a+1]);
t2.push_back(indd[a+2]);
vert.push_back(np(0));
vert.push_back(np(1));
vert.push_back(np(2));
}
else
{ // tri remains same
t2.push_back(t1[3*i+0]);
t2.push_back(t1[3*i+1]);
t2.push_back(t1[3*i+2]);
}
}
if (t2.size()==t1.size())
break;
else
t1.assign(t2.begin(),t2.end());
}
ind.assign(t1.begin(),t1.end());
}
}