private void flipCorner int println will check for flip corner vertice

  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
private void flipCorner(int c) {
//println("will check for flip corner: " + vertices.get((Integer)corners.get(c)));
//no opposite vertex
if (c == BOUND) {
return;
}
//compute triplets
buildTriplets();
//no opposite vertex
if (o(c) == BOUND) {
return;
}
//contains opposite vertex and mesh isn't Delaunay
if (!isDelaunay(c)) {
//println("will flip cuz not Delaunay: " + vertices.get((Integer)corners.get(c)));
int opp = o(c);
//println("change in corners:" + n(c) + " was " + corners.get(n(c)) + " became " + corners.get(opp));
corners.set(n(c), corners.get(opp));
//println("change in corners:" + n(opp) + " was " + corners.get(n(opp)) + " became " + corners.get(c));
corners.set(n(opp), corners.get(c));
//check for neighbor corners
buildTriplets();
flipCorner(c);
buildTriplets();
flipCorner(n(opp));
}
}
//check Delaunay codition for dirty vertices
private void fixMesh(ArrayList l) {
buildTriplets();
while (!l.isEmpty()) {
final int c = (Integer)l.get(0);
flipCorner(c);
l.remove(0);
}
}
//check if point is in triangle
private boolean isInTriangle(int triangleIndex, PVectorExt P) {
final int c = triangleIndex * 3;
PVector A = g(c).v;
PVector B = g(n(c)).v;
PVector C = g(p(c)).v;
if (geometricOperations.isLeftTurn(A,B,P.v) == geometricOperations.isLeftTurn(B,C,P.v) &&
geometricOperations.isLeftTurn(A,B,P.v) == geometricOperations.isLeftTurn(C,A,P.v)) {
return true;
}
return false;
}
//add point to mesh
public void addPoint(final float x, final float y, final float h) {
//PVector newPoint = new PVector(x,y);
PVectorExt newPoint = new PVectorExt(x, y, h);
vertices.add(newPoint);
++numberOfVertices;
final int currentNumberOfTriangles = numberOfTriangles;
for (int triangleIndex = 0; triangleIndex < currentNumberOfTriangles; ++triangleIndex) {
//found triangle which contains newPoint
if (isInTriangle(triangleIndex, newPoint)) {
//println("point is in triangle number: " + triangleIndex + ", point: " + newPoint);
//determing indexes of triangle
final int A = triangleIndex*3;
final int B = A+1;
final int C = A+2;
//println("corners before add: " + corners);
//add new needed corners
corners.add(corners.get(B));
corners.add(corners.get(C));
corners.add(numberOfVertices-1);
corners.add(corners.get(C));
corners.add(corners.get(A));
corners.add(numberOfVertices-1);
corners.set(C, numberOfVertices-1);
//println("corners after add: " + corners);
//determing corners for fixing
ArrayList dirtyCorners = new ArrayList();
int dirtyCorner1 = C;
int dirtyCorner2 = numberOfTriangles*3+2;
int dirtyCorner3 = numberOfTriangles*3+5;
//println("A: " + A + ", B: " + B + ", C: " + C);
//println("dirty corners: " + dirtyCorner1 + ", " + dirtyCorner2 + ", " + dirtyCorner3);
dirtyCorners.add(dirtyCorner1);
dirtyCorners.add(dirtyCorner2);
dirtyCorners.add(dirtyCorner3);
numberOfTriangles += 2;
numberOfCorners += 6;
//fixing mesh
fixMesh(dirtyCorners);
//println("corners after fix: " + corners);
break;
}
}
}
public void drawTriangles() {
noFill();
strokeWeight(1.0);
stroke(0,255,0);
//println("float[][][] triangles = {");
println("void draw() {");
for (int i = 0; i < numberOfTriangles; ++i) {
println("beginShape();");
int c = i*3;
PVectorExt A = g(c);
PVectorExt B = g(n(c));
PVectorExt C = g(p(c));
triangle(A.v.x, A.v.y, B.v.x, B.v.y, C.v.x, C.v.y);
println("vertex(" + A.v.x + ", " + A.v.y + ", " + A.h + ");");
println("vertex(" + B.v.x + ", " + B.v.y + ", " + B.h + ");");
println("vertex(" + C.v.x + ", " + C.v.y + ", " + C.h + ");");
println("endShape(CLOSE);");
}
println("}");
print("Delaunay = [");
for (int i = 0; i < numberOfTriangles; ++i) {
//println("beginShape();");
int c = i*3;
PVectorExt A = g(c);
PVectorExt B = g(n(c));
PVectorExt C = g(p(c));
//triangle(A.v.x, A.v.y, B.v.x, B.v.y, C.v.x, C.v.y);
//println("vertex(" + A.v.x + ", " + A.v.y + ", " + A.h + ");");
//println("vertex(" + B.v.x + ", " + B.v.y + ", " + B.h + ");");
//println("vertex(" + C.v.x + ", " + C.v.y + ", " + C.h + ");");
//println("endShape(CLOSE);");
print("[");
print("[" + A.v.x + ", " + A.v.y + ", " + A.h + "], [" + B.v.x + ", " + B.v.y + ", " + B.h + "], [" + C.v.x + ", " + C.v.y + ", " + C.h + "]");
print("], ");
}
println("]");
}
private void computeCircumcenters() {
hasCircumcircles = false;
circumcenters = new ArrayList();
circumcircleRadius = new ArrayList();
for (int i = 0; i < numberOfTriangles; ++i) {
int c = i*3;
PVector circumcenter = geometricOperations.circumcenter(g(c).v,g(p(c)).v,g(n(c)).v);
circumcenters.add(circumcenter);
circumcircleRadius.add(PVector.dist(g(c).v, circumcenter));
}
hasCircumcircles = true;
}
public void drawCircumcircles() {
if (hasCircumcircles) {
stroke(255,0,0);
noFill();
strokeWeight(1.0);
for (int i = 3; i < numberOfTriangles; ++i) {
stroke(0,0,255);
fill(0,0,255);
PVector circumcenter = (PVector) circumcenters.get(i);
Float radius = (Float) circumcircleRadius.get(i)*2;
ellipse(circumcenter.x, circumcenter.y, 5,5);
stroke(255,0,0);
noFill();
ellipse(circumcenter.x, circumcenter.y, radius, radius);
}
stroke(0,0,0);
noFill();
}
}
}