-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstlsphere.cpp
519 lines (462 loc) · 14.5 KB
/
stlsphere.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Copyright (c) 2014 Andranik Abrahamyan
#include <math.h>
#include <algorithm>
#include "stlsphere.h"
#define M_PI 3.14159265358979323846
#define M_PI_2 1.57079632679489661923
bool sortByRadial (Facet a,Facet b)
{
float ar, br;
float ar1 = a.vector[0].x * a.vector[0].x + a.vector[0].y * a.vector[0].y +
a.vector[0].z * a.vector[0].z;
float ar2 = a.vector[1].x * a.vector[1].x + a.vector[1].y * a.vector[1].y +
a.vector[1].z * a.vector[1].z;
float ar3 = a.vector[2].x * a.vector[2].x + a.vector[2].y * a.vector[2].y +
a.vector[2].z * a.vector[2].z;
float br1 = b.vector[0].x * b.vector[0].x + b.vector[0].y * b.vector[0].y +
b.vector[0].z * b.vector[0].z;
float br2 = b.vector[1].x * b.vector[1].x + b.vector[1].y * b.vector[1].y +
b.vector[1].z * b.vector[1].z;
float br3 = b.vector[2].x * b.vector[2].x + b.vector[2].y * b.vector[2].y +
b.vector[2].z * b.vector[2].z;
if(ar1 < ar2)
ar = ar1;
else
ar = ar2;
if(ar3 < ar)
ar = ar3;
if(br1 < br2)
br = br1;
else
br = br2;
if(br3 < br)
br = br3;
if(ar < br)
return true;
else
return false;
}
StlSphere::StlSphere(int devideSphere)
{
m_devideSphere = devideSphere;
}
StlSphere::~StlSphere()
{
m_vSpherical.clear();
}
void StlSphere::allocate(int numFacets)
{
// Allocate memory
m_vSpherical.resize (numFacets);
}
int StlSphere::getMinRadial(int ind)
{
if( m_vSpherical[ind].radial[0] < m_vSpherical[ind].radial[1])
{
if( m_vSpherical[ind].radial[0] < m_vSpherical[ind].radial[2])
return 0;
} else {
if( m_vSpherical[ind].radial[1] < m_vSpherical[ind].radial[2])
return 1;
}
return 2;
}
void StlSphere::setUnitSphere(std::vector<Facet>* pfacet, Stl_Stats * stats)
{
// get the center of mass
facetMassCalc(pfacet, stats);
// change mesh center to the mass center.
m_maxRadial = 0;
m_UnitStats.minRadial = 3.40282e+038;
for (int i = 0; i < stats->numFacets ; i++)
{
pfacet->at(i).vector[0].x -= m_UnitStats.centerOfMass.x;
pfacet->at(i).vector[0].y -= m_UnitStats.centerOfMass.y;
pfacet->at(i).vector[0].z -= m_UnitStats.centerOfMass.z;
pfacet->at(i).vector[1].x -= m_UnitStats.centerOfMass.x;
pfacet->at(i).vector[1].y -= m_UnitStats.centerOfMass.y;
pfacet->at(i).vector[1].z -= m_UnitStats.centerOfMass.z;
pfacet->at(i).vector[2].x -= m_UnitStats.centerOfMass.x;
pfacet->at(i).vector[2].y -= m_UnitStats.centerOfMass.y;
pfacet->at(i).vector[2].z -= m_UnitStats.centerOfMass.z;
// get minRadial and maxRadial without sqrt
setMinMaxRadial(pfacet->at(i).vector);
}
m_maxRadial = sqrt(m_maxRadial);
m_UnitStats.minRadial = sqrt(m_UnitStats.minRadial) / m_maxRadial ;
// sort the facets by ascending
std::sort (pfacet->begin(), pfacet->end (), sortByRadial);
// we have maxRadial, transform to unit mesh
m_UnitStats.surface = 0;
m_UnitStats.volume = 0;
m_UnitStats.averageRadial = 0;
for (int i = 0; i < stats->numFacets ; i++)
{
pfacet->at(i).vector[0].x /= m_maxRadial;
pfacet->at(i).vector[0].y /= m_maxRadial;
pfacet->at(i).vector[0].z /= m_maxRadial;
pfacet->at(i).vector[1].x /= m_maxRadial;
pfacet->at(i).vector[1].y /= m_maxRadial;
pfacet->at(i).vector[1].z /= m_maxRadial;
pfacet->at(i).vector[2].x /= m_maxRadial;
pfacet->at(i).vector[2].y /= m_maxRadial;
pfacet->at(i).vector[2].z /= m_maxRadial;
// set Spherical data
setSphericalItem(pfacet, i);
// calculate the average_radial
m_UnitStats.averageRadial += m_vSpherical[i].radial[0] +
m_vSpherical[i].radial[1] +
m_vSpherical[i].radial[2];
// get volume of the unit mesh
Facet fac = pfacet->at(i);
float currentVolume;
currentVolume = (fac.vector[0].x * fac.vector[1].y * fac.vector[2].z -
fac.vector[0].x * fac.vector[2].y * fac.vector[1].z -
fac.vector[1].x * fac.vector[0].y * fac.vector[2].z +
fac.vector[1].x * fac.vector[2].y * fac.vector[0].z +
fac.vector[2].x * fac.vector[0].y * fac.vector[1].z -
fac.vector[2].x * fac.vector[1].y * fac.vector[0].z) / 6;
m_UnitStats.volume += currentVolume;
// get surface of the unit mesh
float area = getArea(&pfacet->at(i));
m_UnitStats.surface += area;
setNormalFrequency(pfacet->at(i).normal.x ,pfacet->at(i).normal.y,
pfacet->at(i).normal.z);
}
m_UnitStats.averageRadial /= (3*stats->numFacets);
if (m_UnitStats.surface < 0.0)
m_UnitStats.surface = -m_UnitStats.surface;
// set min,max,size, boundingDiameter values
m_UnitStats.min = stats->min /m_maxRadial;
m_UnitStats.max = stats->max /m_maxRadial;
m_UnitStats.size = stats->size /m_maxRadial;
//m_UnitStats.boundingDiameter = sqrt(m_UnitStats.size.x * m_UnitStats.size.x +
// m_UnitStats.size.y * m_UnitStats.size.y +
// m_UnitStats.size.z * m_UnitStats.size.z);
//implement this logic: box_length < box_width < box_height
if(m_UnitStats.size.x < m_UnitStats.size.y){
if(m_UnitStats.size.x < m_UnitStats.size.z){
m_UnitStats.box_length = m_UnitStats.size.x;
if(m_UnitStats.size.y < m_UnitStats.size.z) {
m_UnitStats.box_width = m_UnitStats.size.y;
m_UnitStats.box_height = m_UnitStats.size.z;
}else {
m_UnitStats.box_width = m_UnitStats.size.z;
m_UnitStats.box_height = m_UnitStats.size.y;
}
}else {
m_UnitStats.box_length = m_UnitStats.size.z;
m_UnitStats.box_width = m_UnitStats.size.x;
m_UnitStats.box_height = m_UnitStats.size.y;
}
} else {
if(m_UnitStats.size.y < m_UnitStats.size.z){
m_UnitStats.box_length = m_UnitStats.size.y;
if(m_UnitStats.size.x < m_UnitStats.size.z){
m_UnitStats.box_width = m_UnitStats.size.x;
m_UnitStats.box_height = m_UnitStats.size.z;
} else {
m_UnitStats.box_width = m_UnitStats.size.z;
m_UnitStats.box_height = m_UnitStats.size.x;
}
}else{
m_UnitStats.box_length = m_UnitStats.size.z;
m_UnitStats.box_width = m_UnitStats.size.y;
m_UnitStats.box_height = m_UnitStats.size.x;
}
}
}
// facets total mass and center of mass calculation
void StlSphere::facetMassCalc(std::vector<Facet>* pFacets, Stl_Stats * stats)
{
float totalVolume = 0, currentVolume;
float xCenter = 0, yCenter = 0, zCenter = 0;
Vector facetCenter;
Vector totalCenter;
for (int i = 0; i < stats->numFacets ; i++)
{
Facet fac = pFacets->at(i);
currentVolume = (fac.vector[0].x * fac.vector[1].y * fac.vector[2].z -
fac.vector[0].x * fac.vector[2].y * fac.vector[1].z -
fac.vector[1].x * fac.vector[0].y * fac.vector[2].z +
fac.vector[1].x * fac.vector[2].y * fac.vector[0].z +
fac.vector[2].x * fac.vector[0].y * fac.vector[1].z -
fac.vector[2].x * fac.vector[1].y * fac.vector[0].z) / 6;
totalVolume += currentVolume;
xCenter += ((fac.vector[0].x + fac.vector[1].x + fac.vector[2].x) / 4) * currentVolume;
yCenter += ((fac.vector[0].y + fac.vector[1].y + fac.vector[2].y) / 4) * currentVolume;
zCenter += ((fac.vector[0].z + fac.vector[1].z + fac.vector[2].z) / 4) * currentVolume;
}
m_UnitStats.volume = totalVolume;
m_UnitStats.centerOfMass.x = xCenter/totalVolume;
m_UnitStats.centerOfMass.y = yCenter/totalVolume;
m_UnitStats.centerOfMass.z = zCenter/totalVolume;
}
void StlSphere::setMinMaxRadial(Vector* vector)
{
for (int i = 0; i < 3; i++) {
double s = vector[i].x * vector[i].x + vector[i].y * vector[i].y + vector[i].z * vector[i].z;
if (s > m_maxRadial)
m_maxRadial = s;
if (s < m_UnitStats.minRadial)
m_UnitStats.minRadial = s;
}
}
void StlSphere::setNormalFrequency(float x, float y, float z)
{
bool finded = false;
NormalFrequency nf;
int devideSphere = m_devideSphere/2;
nf.x_pos = (short)(x*devideSphere);
nf.y_pos = (short)(y*devideSphere);
nf.z_pos = (short)(z*devideSphere);
nf.frequency = 1;
for( int i= 0; i < m_vNormalFreq.size (); i++)
{
if(m_vNormalFreq[i].x_pos == nf.x_pos &&
m_vNormalFreq[i].y_pos == nf.y_pos &&
m_vNormalFreq[i].z_pos == nf.z_pos)
{
m_vNormalFreq[i].frequency++;
finded = true;
break;
}
}
if(!finded)
m_vNormalFreq.push_back (nf);
}
void StlSphere::writeAsNormalFrequency(const ::std::string& fileName)
{
// Open the file, file have .nfr extention
::std::ofstream file(fileName.c_str(), ::std::ios::out);
file.setf(::std::ios::scientific);
file.precision(8);
if (file.is_open()) {
file << "NormalFrequency" << ::std::endl;
for (int i = 0; i < m_vNormalFreq.size() ; i++) {
file << "x_pos =" << m_vNormalFreq[i].x_pos << "\t"
<< "y_pos =" << m_vNormalFreq[i].y_pos << "\t"
<< "z_pos =" << m_vNormalFreq[i].z_pos << "\t"
<< "frequency =" << m_vNormalFreq[i].frequency << ::std::endl;
}
file << "endNormalFrequency" << ::std::endl;
file.close();
} else {
::std::cerr << "The file " << file << " could not be found." << ::std::endl;
}
}
void StlSphere::setSphericalItem(std::vector<Facet>* fac , int ind)
{
Vector* vector = fac->at(ind).vector;
for (int i = 0; i < 3; i++) {
double s = vector[i].x * vector[i].x + vector[i].y * vector[i].y + vector[i].z * vector[i].z;
m_vSpherical[ind].radial[i] = sqrt(s);
m_vSpherical[ind].polar[i] = acos(vector[i].z / m_vSpherical[ind].radial[i]);
m_vSpherical[ind].azimuth[i] = calcAtan(vector[i].x,vector[i].y);
}
/*
// set normal value
float x = fac->at(ind).normal.x ;
float y = fac->at(ind).normal.y ;
float z = fac->at(ind).normal.z ;
if (x > 0.95 || y > 0.95 || z > 0.95)
int f = 4;
//m_vSpherical[ind].normal.polar = acos(z / m_vSpherical[ind].radial[i]);
// I will use atan for geting 0 - 2*Pi radians
m_vSpherical[ind].normal.polar = calcAtan (sqrt(x*x + y*y), z);
m_vSpherical[ind].normal.azimuth = calcAtan(x, y);
*/
}
/*
theta^ = atan2(abs(y/x));
For theta in QI: theta = theta^
For theta in QII: theta= pi - theta^
For theta in QIII: theta = pi + theta^
For theta in QIV: theta = 2*pi - theta^
*/
float StlSphere::calcAtan(float x, float y)
{
float value;
value = atan(abs(y /x));//For theta in QI:
if (x == 0) {
if (y > 0)
value = M_PI_2;
else
value = -M_PI_2;
} else if (x > 0) {
if (y < 0) //For theta in QIV:
value = 2*M_PI - value;
} else {
if (y > 0)//For theta in QII:
value = M_PI - value;
else // For theta in QIII
value = M_PI + value;
}
return value;
}
float StlSphere::getArea(Facet *facet)
{
float cross[3][3];
float sum[3];
//.
float n[3];
for (int i = 0; i < 3; i++) {
cross[i][0] = ((facet->vector[i].y * facet->vector[(i + 1) % 3].z) -
(facet->vector[i].z * facet->vector[(i + 1) % 3].y));
cross[i][1] = ((facet->vector[i].z * facet->vector[(i + 1) % 3].x) -
(facet->vector[i].x * facet->vector[(i + 1) % 3].z));
cross[i][2] = ((facet->vector[i].x * facet->vector[(i + 1) % 3].y) -
(facet->vector[i].y * facet->vector[(i + 1) % 3].x));
}
sum[0] = cross[0][0] + cross[1][0] + cross[2][0];
sum[1] = cross[0][1] + cross[1][1] + cross[2][1];
sum[2] = cross[0][2] + cross[1][2] + cross[2][2];
// This should already be done. But just in case, let's do it again
//calculateNormal(n, facet);
//normalizeVector(n);
//float area = 0.5 * (n[0] * sum[0] + n[1] * sum[1] + n[2] * sum[2]);
calculateNormal( facet);
normalizeVector(&facet->normal);
float area = 0.5 * (facet->normal.x * sum[0] + facet->normal.y * sum[1] + facet->normal.z * sum[2]);
return area;
}
void StlSphere::calculateNormal( Facet *facet)
{
float v1[3];
float v2[3];
v1[0] = facet->vector[1].x - facet->vector[0].x;
v1[1] = facet->vector[1].y - facet->vector[0].y;
v1[2] = facet->vector[1].z - facet->vector[0].z;
v2[0] = facet->vector[2].x - facet->vector[0].x;
v2[1] = facet->vector[2].y - facet->vector[0].y;
v2[2] = facet->vector[2].z - facet->vector[0].z;
facet->normal.x = (float)((double)v1[1] * (double)v2[2])
- ((double)v1[2] * (double)v2[1]);
facet->normal.y = (float)((double)v1[2] * (double)v2[0])
- ((double)v1[0] * (double)v2[2]);
facet->normal.z = (float)((double)v1[0] * (double)v2[1])
- ((double)v1[1] * (double)v2[0]);
}
void StlSphere::normalizeVector(Normal* v)
{
double length = sqrt((double)v->x * (double)v->x
+ (double)v->y * (double)v->y
+ (double)v->z * (double)v->z);
float minNormalLength = 0.000000000001f;
if (length < minNormalLength) {
v->x = 1.0;
v->y = 0.0;
v->z = 0.0;
return;
}
double factor = 1.0 / length;
v->x *= factor;
v->y *= factor;
v->z *= factor;
}
// Purpose:
// Cartesian to sphere coordinate transform.
// Discussion:
// We allow the trivial case M = 1; in that case alone, the value R must be assumed to have a sign.
// Parameters:
// Input, int M, the spatial dimension.
// 1 <= M.
// Input, int N, the number of points to transform.
// Input, double C[M], the center of the sphere.
// Input, double X[M*N], the Cartesian coordinates of the points.
// Output, double R[N], the radius of the points on the sphere.
// Except for the trivial case M = 1, R is assumed nonnegative.
// Output, double THETA[(M-1)*N], the coordinate angles of the points, measured in radians.
void StlSphere::cartesianToSphere(int m, int n, double c[], double x[],
double r[], double theta[] )
{
int i;
int i1;
int j;
double t;
double *top;
double *x2;
//
// Handle special case of M = 1.
//
if ( m == 1 )
{
for ( j = 0; j < n; j++ )
{
r[j] = x[0+j*m] - c[0];
}
return;
}
//
// Subtract the center.
//
x2 = new double[ m * n ];
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < m; i++ )
{
x2[i+j*m] = x[i+j*m] - c[i];
}
}
//
// Compute R.
//
for ( j = 0; j < n; j++ )
{
t = 0.0;
for ( i = 0; i < m; i++ )
{
t = t + pow ( x2[i+m*j], 2 );
}
r[j] = sqrt ( t );
}
//
// Compute M-2 components of THETA.
//
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < m - 1; i++ )
{
theta[i+j*(m-1)] = 0.0;
}
}
for ( i = 1; i < m - 1; i++ )
{
for ( i1 = 0; i1 <= i - 1; i1++ )
{
for ( j = 0; j < n; j++ )
{
theta[i1+j*(m-1)] = theta[i1+j*(m-1)] + pow ( x2[i+j*m], 2 );
}
}
}
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < m - 2; i++ )
{
theta[i+j*(m-1)] = theta[i+j*(m-1)] + pow ( x2[m-1+j*m], 2 );
}
}
for ( i = 0; i < m - 2; i++ )
{
for ( j = 0; j < n; j++ )
{
theta[i+j*(m-1)] = atan2 ( sqrt ( theta[i+j*(m-1)] ), x2[i+j*m] );
}
}
//
// Compute last component of THETA.
//
top = new double[n];
for ( j = 0; j < n; j++ )
{
top[j] = sqrt ( pow ( x2[m-1+j*m], 2 ) + pow ( x2[m-2+j*m], 2 ) ) + x2[m-2+j*m];
}
for ( j = 0; j < n; j++ )
{
theta[m-2+j*(m-1)] = 2.0 * atan2 ( x2[m-1+j*m], top[j] );
}
delete [] top;
delete [] x2;
return;
}