1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2018 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.library.mesh;
|
21
|
|
22
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
23
|
/**
|
24
|
* Create a Mesh which approximates a sphere.
|
25
|
* <p>
|
26
|
* Do so by dividing each of the 20 faces of the regular icosahedron into smaller triangles and inflating
|
27
|
* those to lay on the surface of the sphere.
|
28
|
*/
|
29
|
public class MeshSphere extends MeshBase
|
30
|
{
|
31
|
private static final int NUMFACES = 20;
|
32
|
private static final double sqrt2 = Math.sqrt(2.0);
|
33
|
private static final double P = Math.PI;
|
34
|
private static final double A = 0.463647609; // arctan(0.5), +-latitude of the 10 'middle' vertices
|
35
|
// https://en.wikipedia.org/wiki/Regular_icosahedron
|
36
|
|
37
|
// An array of 20 entries, each describing a single face of the regular icosahedron in an (admittedly)
|
38
|
// weird fashion.
|
39
|
// Each face of a regular icosahedron is a equilateral triangle, with 2 vertices on the same latitude.
|
40
|
// Single row is (longitude of V1, longitude of V2, (common) latitude of V1 and V2, latitude of V3)
|
41
|
// longitude of V3 is simply midpoint of V1 and V2 so we don't have to specify it here.
|
42
|
|
43
|
private static final double FACES[][] = {
|
44
|
{ 0.0*P, 0.4*P, A, 0.5*P },
|
45
|
{ 0.4*P, 0.8*P, A, 0.5*P },
|
46
|
{ 0.8*P, 1.2*P, A, 0.5*P }, // 5 'top' faces with
|
47
|
{ 1.2*P, 1.6*P, A, 0.5*P }, // the North Pole
|
48
|
{ 1.6*P, 2.0*P, A, 0.5*P },
|
49
|
|
50
|
{ 0.0*P, 0.4*P, A, -A },
|
51
|
{ 0.4*P, 0.8*P, A, -A },
|
52
|
{ 0.8*P, 1.2*P, A, -A }, // 5 faces mostly above
|
53
|
{ 1.2*P, 1.6*P, A, -A }, // the equator
|
54
|
{ 1.6*P, 2.0*P, A, -A },
|
55
|
|
56
|
{ 0.2*P, 0.6*P, -A, A },
|
57
|
{ 0.6*P, 1.0*P, -A, A },
|
58
|
{ 1.0*P, 1.4*P, -A, A }, // 5 faces mostly below
|
59
|
{ 1.4*P, 1.8*P, -A, A }, // the equator
|
60
|
{ 1.8*P, 0.2*P, -A, A },
|
61
|
|
62
|
{ 0.2*P, 0.6*P, -A,-0.5*P },
|
63
|
{ 0.6*P, 1.0*P, -A,-0.5*P },
|
64
|
{ 1.0*P, 1.4*P, -A,-0.5*P }, // 5 'bottom' faces with
|
65
|
{ 1.4*P, 1.8*P, -A,-0.5*P }, // the South Pole
|
66
|
{ 1.8*P, 0.2*P, -A,-0.5*P },
|
67
|
};
|
68
|
private int currentVert;
|
69
|
private int numVertices;
|
70
|
|
71
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
72
|
// Each of the 20 faces of the icosahedron requires (level*level + 4*level) vertices for the face
|
73
|
// itself and a join to the next face (which requires 2 vertices). We don't need the join in case
|
74
|
// of the last, 20th face, thus the -2.
|
75
|
// (level*level +4*level) because there are level*level little triangles, each requiring new vertex,
|
76
|
// plus 2 extra vertices to start off a row and 2 to move to the next row (or the next face in case
|
77
|
// of the last row) and there are 'level' rows.
|
78
|
|
79
|
private void computeNumberOfVertices(int level)
|
80
|
{
|
81
|
numVertices = NUMFACES*level*(level+4) -2;
|
82
|
currentVert = 0;
|
83
|
}
|
84
|
|
85
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86
|
// (longitude,latitude) - spherical coordinates of a point on a unit sphere.
|
87
|
// Cartesian (0,0,1) - i.e. the point of the sphere closest to the camera - is spherical (0,0).
|
88
|
|
89
|
private void addVertex( double longitude, double latitude, float[] attribs)
|
90
|
{
|
91
|
double sinLON = Math.sin(longitude);
|
92
|
double cosLON = Math.cos(longitude);
|
93
|
double sinLAT = Math.sin(latitude);
|
94
|
double cosLAT = Math.cos(latitude);
|
95
|
|
96
|
float x = (float)(cosLAT*sinLON / sqrt2);
|
97
|
float y = (float)(sinLAT / sqrt2);
|
98
|
float z = (float)(cosLAT*cosLON / sqrt2);
|
99
|
|
100
|
attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB ] = x; //
|
101
|
attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB+1] = y; //
|
102
|
attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB+2] = z; //
|
103
|
// In case of this Mesh so it happens that
|
104
|
attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB ] = x; // the vertex coords, normal vector, and
|
105
|
attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB+1] = y; // inflate vector have identical (x,y,z).
|
106
|
attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB+2] = z; //
|
107
|
// TODO: think about some more efficient
|
108
|
attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB ] = x; // representation.
|
109
|
attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB+1] = y; //
|
110
|
attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB+2] = z; //
|
111
|
|
112
|
attribs[VERT_ATTRIBS*currentVert + TEX_ATTRIB ] = (float)longitude;
|
113
|
attribs[VERT_ATTRIBS*currentVert + TEX_ATTRIB+1] = (float)latitude;
|
114
|
|
115
|
currentVert++;
|
116
|
}
|
117
|
|
118
|
|
119
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120
|
|
121
|
private void repeatLast(float[] attribs)
|
122
|
{
|
123
|
//android.util.Log.e("sphere", "repeat last!");
|
124
|
|
125
|
if( currentVert>0 )
|
126
|
{
|
127
|
attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB ] = attribs[VERT_ATTRIBS*(currentVert-1) + POS_ATTRIB ];
|
128
|
attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(currentVert-1) + POS_ATTRIB+1];
|
129
|
attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(currentVert-1) + POS_ATTRIB+2];
|
130
|
|
131
|
attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB ] = attribs[VERT_ATTRIBS*(currentVert-1) + NOR_ATTRIB ];
|
132
|
attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(currentVert-1) + NOR_ATTRIB+1];
|
133
|
attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(currentVert-1) + NOR_ATTRIB+2];
|
134
|
|
135
|
attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB ] = attribs[VERT_ATTRIBS*(currentVert-1) + INF_ATTRIB ];
|
136
|
attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(currentVert-1) + INF_ATTRIB+1];
|
137
|
attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(currentVert-1) + INF_ATTRIB+2];
|
138
|
|
139
|
attribs[VERT_ATTRIBS*currentVert + TEX_ATTRIB ] = attribs[VERT_ATTRIBS*(currentVert-1) + TEX_ATTRIB ];
|
140
|
attribs[VERT_ATTRIBS*currentVert + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(currentVert-1) + TEX_ATTRIB+1];
|
141
|
|
142
|
currentVert++;
|
143
|
}
|
144
|
}
|
145
|
|
146
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
147
|
// Supposed to return the latitude of the point between two points on the sphere with latitudes
|
148
|
// lat1 and lat2, so if for example quot=0.2, then it will return the latitude of something 20%
|
149
|
// along the way from lat1 to lat2.
|
150
|
//
|
151
|
// This is approximation only - in general it is of course not true that the midpoint of two points
|
152
|
// on a unit sphere with spherical coords (A1,B1) and (A2,B2) is ( (A1+A2)/2, (B1+B2)/2 ) - take
|
153
|
// (0,0) and (PI, epsilon) as a counterexample.
|
154
|
//
|
155
|
// Here however, the latitudes we are interested at are the latitudes of the vertices of a regular
|
156
|
// icosahedron - i.e. +=A and +=PI/2, whose longitudes are close, and we don't really care if the
|
157
|
// split into smaller triangles is exact.
|
158
|
//
|
159
|
// quot better be between 0.0 and 1.0.
|
160
|
// this is 'directed' i.e. from lat1 to lat2.
|
161
|
|
162
|
private double midLatitude(double lat1, double lat2, double quot)
|
163
|
{
|
164
|
return lat1*(1.0-quot)+lat2*quot;
|
165
|
}
|
166
|
|
167
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
168
|
// Same in case of longitude. This is for our needs exact, because we are ever only calling this with
|
169
|
// two longitudes of two vertices with the same latitude. Additional problem: things can wrap around
|
170
|
// the circle.
|
171
|
// this is 'undirected' i.e. we don't assume from lon1 to lon2 - just along the smaller arc joining
|
172
|
// lon1 to lon2.
|
173
|
|
174
|
private double midLongitude(double lon1, double lon2, double quot)
|
175
|
{
|
176
|
double min, max;
|
177
|
|
178
|
if( lon1<lon2 ) { min=lon1; max=lon2; }
|
179
|
else { min=lon2; max=lon1; }
|
180
|
|
181
|
double diff = max-min;
|
182
|
if( diff>P ) { diff=2*P-diff; min=max; }
|
183
|
|
184
|
double ret = min+quot*diff;
|
185
|
if( ret>=2*P ) ret-=2*P;
|
186
|
|
187
|
return ret;
|
188
|
}
|
189
|
|
190
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
191
|
// linear map (column,row, level):
|
192
|
//
|
193
|
// ( 0, 0, level) -> (lonV1,latV12)
|
194
|
// ( 0, level-1, level) -> (lonV3,latV3 )
|
195
|
// (level-1, 0, level) -> (lonV2,latV12)
|
196
|
|
197
|
private void newVertex(float[] attribs, int column, int row, int level,
|
198
|
double lonV1, double lonV2, double latV12, double latV3)
|
199
|
{
|
200
|
double quotX = (double)column/level;
|
201
|
double quotY = (double)row /level;
|
202
|
double quotZ;
|
203
|
|
204
|
if( latV12*latV3 < 0.0 ) // equatorial triangle
|
205
|
{
|
206
|
quotZ = quotX + 0.5*quotY;
|
207
|
}
|
208
|
else // polar triangle
|
209
|
{
|
210
|
quotZ = (quotY==1.0 ? 0.0 : quotX / (1.0-quotY));
|
211
|
}
|
212
|
|
213
|
double lonPoint = midLongitude(lonV1, lonV2, quotZ );
|
214
|
double latPoint = midLatitude(latV12, latV3, quotY );
|
215
|
|
216
|
//android.util.Log.e("sphere", "newVertex: long:"+lonPoint+" lat:"+latPoint+" column="+column+" row="+row);
|
217
|
|
218
|
addVertex(lonPoint,latPoint,attribs);
|
219
|
}
|
220
|
|
221
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222
|
|
223
|
private void buildFace(float[] attribs, int level, int face, double lonV1, double lonV2, double latV12, double latV3)
|
224
|
{
|
225
|
for(int row=0; row<level; row++)
|
226
|
{
|
227
|
for (int column=0; column<level-row; column++)
|
228
|
{
|
229
|
newVertex(attribs, column, row , level, lonV1, lonV2, latV12, latV3);
|
230
|
if (column==0 && !(face==0 && row==0 ) ) repeatLast(attribs);
|
231
|
newVertex(attribs, column, row+1, level, lonV1, lonV2, latV12, latV3);
|
232
|
}
|
233
|
|
234
|
newVertex(attribs, level-row, row , level, lonV1, lonV2, latV12, latV3);
|
235
|
if( row!=level-1 || face!=NUMFACES-1 ) repeatLast(attribs);
|
236
|
}
|
237
|
}
|
238
|
|
239
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
240
|
// PUBLIC API
|
241
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
242
|
/**
|
243
|
* Creates the underlying grid of vertices with the usual attribs which approximates a sphere.
|
244
|
* <p>
|
245
|
* When level=1, it outputs the 12 vertices of a regular icosahedron.
|
246
|
* When level=N, it divides each of the 20 icosaherdon's triangular faces into N^2 smaller triangles
|
247
|
* (by dividing each side into N equal segments) and 'inflates' the internal vertices so that they
|
248
|
* touch the sphere.
|
249
|
*
|
250
|
* @param level Specifies the approximation level. Valid values: level ≥ 1
|
251
|
*/
|
252
|
public MeshSphere(int level)
|
253
|
{
|
254
|
super(1.0f);
|
255
|
|
256
|
computeNumberOfVertices(level);
|
257
|
float[] attribs= new float[VERT_ATTRIBS*numVertices];
|
258
|
|
259
|
for(int face=0; face<NUMFACES; face++ )
|
260
|
{
|
261
|
buildFace(attribs, level, face, FACES[face][0], FACES[face][1], FACES[face][2], FACES[face][3]);
|
262
|
}
|
263
|
|
264
|
if( currentVert!=numVertices )
|
265
|
android.util.Log.d("MeshSphere", "currentVert= " +currentVert+" numVertices="+numVertices );
|
266
|
|
267
|
setAttribs(attribs);
|
268
|
}
|
269
|
}
|