Revision 89b93576
Added by Leszek Koltunski about 6 years ago
src/main/java/org/distorted/library/mesh/MeshSphere.java | ||
---|---|---|
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 , 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 , 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 , 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 , 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 = 20*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 |
if( currentVert>0 ) |
|
124 |
{ |
|
125 |
attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB ] = attribs[VERT_ATTRIBS*(currentVert-1) + POS_ATTRIB ]; |
|
126 |
attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB+1] = attribs[VERT_ATTRIBS*(currentVert-1) + POS_ATTRIB+1]; |
|
127 |
attribs[VERT_ATTRIBS*currentVert + POS_ATTRIB+2] = attribs[VERT_ATTRIBS*(currentVert-1) + POS_ATTRIB+2]; |
|
128 |
|
|
129 |
attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB ] = attribs[VERT_ATTRIBS*(currentVert-1) + NOR_ATTRIB ]; |
|
130 |
attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB+1] = attribs[VERT_ATTRIBS*(currentVert-1) + NOR_ATTRIB+1]; |
|
131 |
attribs[VERT_ATTRIBS*currentVert + NOR_ATTRIB+2] = attribs[VERT_ATTRIBS*(currentVert-1) + NOR_ATTRIB+2]; |
|
132 |
|
|
133 |
attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB ] = attribs[VERT_ATTRIBS*(currentVert-1) + INF_ATTRIB ]; |
|
134 |
attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB+1] = attribs[VERT_ATTRIBS*(currentVert-1) + INF_ATTRIB+1]; |
|
135 |
attribs[VERT_ATTRIBS*currentVert + INF_ATTRIB+2] = attribs[VERT_ATTRIBS*(currentVert-1) + INF_ATTRIB+2]; |
|
136 |
|
|
137 |
attribs[VERT_ATTRIBS*currentVert + TEX_ATTRIB ] = attribs[VERT_ATTRIBS*(currentVert-1) + TEX_ATTRIB ]; |
|
138 |
attribs[VERT_ATTRIBS*currentVert + TEX_ATTRIB+1] = attribs[VERT_ATTRIBS*(currentVert-1) + TEX_ATTRIB+1]; |
|
139 |
|
|
140 |
currentVert++; |
|
141 |
} |
|
142 |
} |
|
143 |
|
|
144 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
145 |
// Supposed to return the latitude of the point between two points on the sphere with latitudes |
|
146 |
// lat1 and lat2, so if for example quot=0.2, then it will return the latitude of something 20% |
|
147 |
// along the way from lat1 to lat2. |
|
148 |
// |
|
149 |
// This is approximation only - in general it is of course not true that the midpoint of two points |
|
150 |
// on a unit sphere with spherical coords (A1,B1) and (A2,B2) is ( (A1+A2)/2, (B1+B2)/2 ) - take |
|
151 |
// (0,0) and (PI, epsilon) as a counterexample. |
|
152 |
// |
|
153 |
// Here however, the latitudes we are interested at are the latitudes of the vertices of a regular |
|
154 |
// icosahedron - i.e. +=A and +=PI/2, whose longitudes are close, and we don't really care if the |
|
155 |
// split into smaller triangles is exact. |
|
156 |
// |
|
157 |
// quot better be between 0.0 and 1.0. |
|
158 |
// this is 'directed' i.e. from lat1 to lat2. |
|
159 |
|
|
160 |
private double midLatitude(double lat1, double lat2, double quot) |
|
161 |
{ |
|
162 |
return lat1*(1.0-quot)+lat2*quot; |
|
163 |
} |
|
164 |
|
|
165 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
166 |
// Same in case of longitude. This is for our needs exact, because we are ever only calling this with |
|
167 |
// two longitudes of two vertices with the same latitude. Additional problem: things can wrap around |
|
168 |
// the circle. |
|
169 |
// this is 'undirected' i.e. we don't assume from lon1 to lon2 - just along the smaller arc joining |
|
170 |
// lon1 to lon2. |
|
171 |
|
|
172 |
private double midLongitude(double lon1, double lon2, double quot) |
|
173 |
{ |
|
174 |
double min, max; |
|
175 |
|
|
176 |
if( lon1<lon2 ) { min=lon1; max=lon2; } |
|
177 |
else { min=lon2; max=lon1; } |
|
178 |
|
|
179 |
double diff = max-min; |
|
180 |
if( diff>P ) { diff=2*P-diff; min=max; } |
|
181 |
|
|
182 |
double ret = min+quot*diff; |
|
183 |
if( ret>=2*P ) ret-=2*P; |
|
184 |
|
|
185 |
return ret; |
|
186 |
} |
|
187 |
|
|
188 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
189 |
// linear map (column,row, level): |
|
190 |
// |
|
191 |
// ( 0, 0, level) -> (lonV1,latV12) |
|
192 |
// ( 0, level-1, level) -> (lonV3,latV3 ) |
|
193 |
// (level-1, 0, level) -> (lonV2,latV12) |
|
194 |
|
|
195 |
private void newVertex(float[] attribs, int column, int row, int level, |
|
196 |
double lonV1, double lonV2, double latV12, double latV3) |
|
197 |
{ |
|
198 |
double quotX = (double)column/(level-1); |
|
199 |
double quotY = (double)row /(level-1); |
|
200 |
|
|
201 |
double lonPoint = midLongitude(lonV1,lonV2, (quotX+0.5*quotY) ); |
|
202 |
double latPoint = midLatitude(latV12,latV3, quotY); |
|
203 |
|
|
204 |
addVertex(lonPoint,latPoint,attribs); |
|
205 |
} |
|
206 |
|
|
207 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
208 |
|
|
209 |
private void buildFace(float[] attribs, int level, int face, double lonV1, double lonV2, double latV12, double latV3) |
|
210 |
{ |
|
211 |
for(int row=0; row<level; row++) |
|
212 |
{ |
|
213 |
for (int column=0; column<level-row; column++) |
|
214 |
{ |
|
215 |
newVertex(attribs, column, row , level, lonV1, lonV2, latV12, latV3); |
|
216 |
if (column==0 && !(face==0 && row==0 ) ) repeatLast(attribs); |
|
217 |
newVertex(attribs, column, row+1, level, lonV1, lonV2, latV12, latV3); |
|
218 |
} |
|
219 |
|
|
220 |
newVertex(attribs, level-row, row , level, lonV1, lonV2, latV12, latV3); |
|
221 |
if( row!=level-1 || face!=NUMFACES-1 ) repeatLast(attribs); |
|
222 |
} |
|
223 |
} |
|
224 |
|
|
225 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
226 |
// PUBLIC API |
|
227 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
228 |
/** |
|
229 |
* Creates the underlying grid of vertices with the usual attribs which approximates a sphere. |
|
230 |
* <p> |
|
231 |
* When level=1, it outputs the 12 vertices of a regular icosahedron. |
|
232 |
* When level=N, it divides each of the 20 icosaherdon's triangular faces into N^2 smaller triangles |
|
233 |
* (by dividing each side into N equal segments) and 'inflates' the internal vertices so that they |
|
234 |
* touch the sphere. |
|
235 |
* |
|
236 |
* @param level Specifies the approximation level. Valid values: level ≥ 1 |
|
237 |
*/ |
|
238 |
public MeshSphere(int level) |
|
239 |
{ |
|
240 |
super(1.0f); |
|
241 |
|
|
242 |
computeNumberOfVertices(level); |
|
243 |
float[] attribs= new float[VERT_ATTRIBS*numVertices]; |
|
244 |
|
|
245 |
for(int face=0; face<NUMFACES; face++ ) |
|
246 |
{ |
|
247 |
buildFace(attribs, level, face, FACES[face][0], FACES[face][1], FACES[face][2], FACES[face][3]); |
|
248 |
} |
|
249 |
|
|
250 |
if( currentVert!=numVertices ) |
|
251 |
android.util.Log.d("MeshSphere", "currentVert= " +currentVert+" numVertices="+numVertices ); |
|
252 |
|
|
253 |
setAttribs(attribs); |
|
254 |
} |
|
255 |
} |
Also available in: Unified diff
Add support for MeshSphere (add ability to display it in the 'Effects3D' and 'Inflate' apps).
Still a bit buggy!