-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphere.cpp
114 lines (110 loc) · 2.38 KB
/
sphere.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
// Bresenham variant of midpoint circle algorithm adapted for sphere
// from Graphics Gems "Spheres-to-voxels conversion" chapter by Claudio Montani
// and Roberto Scopigno
#include <stdio.h>
#include <stdlib.h>
#include <list>
using namespace std;
#include "sphere.h"
void Sphere(int R, list<LStruct> &voxList);
void Slice(int r, int y);
void Track(int x, int y, int z);
void Voxel(int x, int y, int z);
#if 0
int main(int argc, char **argv) {
int R = 3;
--argc, ++argv;
if(argc == 1)
R = atol(*argv);
Sphere(R);
}
#endif
static list<LStruct> *voxListPtr;
void Sphere(int R, list<LStruct> &voxListArg)
{
voxListPtr = &voxListArg;
int x = 0, y = R;
int delta=2*(1-R), limit = 0;
while (y >= limit) {
if (delta < 0 ) {
int deltaLC = 2*delta + 2*y - 1;
if(deltaLC > 0 ) {
Slice(x,y);
x = x + 1; y = y - 1;
delta = delta + 2*x - 2*y + 2;
} else {
x = x+1;
delta = delta + 2*x + 1;
}
} else if (delta > 0 ) {
int deltaLC = 2*delta - 2*x - 1;
if(deltaLC > 0 ) {
Slice(x,y);
y = y - 1;
delta = delta - 2*y + 1;
} else {
Slice(x,y);
x = x + 1; y = y - 1;
delta = delta + 2*x - 2*y + 2;
}
} else {
Slice(x,y);
x = x+1; y = y-1;
delta = delta + 2*x - 2*y + 2;
}
}
}
void Slice(int r, int y) {
int x = 0, z = r;
int delta = 2*(1-r), limit = 0;
Track( x, y, z );
while( z >= limit ) {
if( delta < 0 ) {
int deltaLC = 2*delta + 2*z - 1;
if( deltaLC > 0 ) {
x = x + 1; z = z - 1;
delta = delta + 2*x - 2*z +2;
Track( x, y, z );
Track( -x, y, z );
} else {
x = x + 1; delta = delta + 2*x +1;
Track( x, y, z );
Track( -x, y, z );
}
} else if( delta > 0 ) {
int deltaLC = 2*delta - 2*x - 1;
if( deltaLC > 0 ) {
z = z - 1;
delta = delta - 2*z + 1;
} else {
x = x + 1; z = z - 1;
delta = delta + 2*x -2*z +2;
Track( x, y, z );
Track( -x, y, z );
}
} else {
x = x + 1; z = z - 1;
delta = delta + 2*x - 2*z + 2;
Track( x, y, z );
Track( -x, y, z );
}
}
}
void Track(int x, int y, int z) {
for(int k = -z; k <= z; k= k+ 1 ){
Voxel(x, y, k);
}
if( y != 0 ) {
for(int k = -z; k <= z; k= k+ 1 ){
Voxel(x, -y, k);
}
}
}
void Voxel(int x, int y, int z) {
LStruct lStruct;
lStruct.i = x;
lStruct.j = y;
lStruct.k = z;
voxListPtr->push_back(lStruct);
//printf("%d %d %d\n", x, y, z);
}