-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Diamond3iIterable.java
126 lines (106 loc) · 4.47 KB
/
Diamond3iIterable.java
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
/*
* Copyright 2020 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.terasology.math;
import com.google.common.base.Preconditions;
import org.joml.Vector3i;
import org.joml.Vector3ic;
import java.util.Iterator;
/**
* Diamond3Iterable iterates in a diamond shape hull where the manhattan distance is the same for that level.
* for each group of iterations the manhattan distance decreases. inner and outer radiuses can be set where
* the shape can be an hull with distance 1 to a full solid.
*/
public final class Diamond3iIterable implements Iterable<Vector3ic> {
private final Vector3i origin = new Vector3i();
private final int endDistance;
private int startDistance = 0;
private Diamond3iIterable(Vector3ic origin, int maxDistance) {
Preconditions.checkArgument(maxDistance >= 0, "maxDistance must be > 0");
this.origin.set(origin);
this.endDistance = maxDistance;
}
private Diamond3iIterable(Vector3ic origin, int startDistance, int maxDistance) {
Preconditions.checkArgument(startDistance < maxDistance, "startDistance must be < maxDistance");
Preconditions.checkArgument(maxDistance >= 0, "maxDistance must be >= 0");
Preconditions.checkArgument(startDistance >= 0, "maxDistance must be >= 0");
this.origin.set(origin);
this.endDistance = maxDistance;
this.startDistance = startDistance;
}
public static Diamond3iIterable.Builder region(Vector3ic origin, int radius) {
return new Diamond3iIterable.Builder(origin, radius);
}
public static Diamond3iIterable.Builder hollow(Vector3ic origin, int start, int end) {
return new Diamond3iIterable.Builder(origin, end).start(start);
}
public static Diamond3iIterable.Builder shell(Vector3ic origin, int radius) {
return new Diamond3iIterable.Builder(origin, radius).start(radius - 1);
}
@Override
public Iterator<Vector3ic> iterator() {
Vector3i pos = new Vector3i();
final int[] level = {this.startDistance};
final int[] offset = {-this.startDistance,0,0};
return new Iterator<Vector3ic>() {
@Override
public boolean hasNext() {
return level[0] < endDistance;
}
@Override
public Vector3ic next() {
pos.set(origin.x + offset[0], origin.y + offset[1], origin.z + offset[2]);
if (offset[2] < 0) {
offset[2] *= -1;
} else if (offset[1] < 0) {
offset[1] *= -1;
offset[2] = -(level[0] - TeraMath.fastAbs(offset[0]) - TeraMath.fastAbs(offset[1]));
} else {
offset[1] = -offset[1] + 1;
if (offset[1] > 0) {
if (++offset[0] <= level[0]) {
offset[1] = TeraMath.fastAbs(offset[0]) - level[0];
offset[2] = 0;
} else {
level[0]++;
offset[0] = -level[0];
offset[1] = 0;
offset[2] = 0;
}
} else {
offset[2] = -(level[0] - TeraMath.fastAbs(offset[0]) - TeraMath.fastAbs(offset[1]));
}
}
return pos;
}
};
}
public static final class Builder {
private final Vector3ic origin;
private final int endDistance;
private int startDistance = 0;
private Builder(Vector3ic origin, int endDistance){
this.origin = origin;
this.endDistance = endDistance + 1;
}
public Diamond3iIterable.Builder start(int start) {
this.startDistance = start + 1;
return this;
}
public Diamond3iIterable build() {
return new Diamond3iIterable(origin,startDistance,endDistance);
}
}
}