1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2023 Leszek Koltunski leszek@koltunski.pl //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// This library is free software; you can redistribute it and/or //
|
7
|
// modify it under the terms of the GNU Lesser General Public //
|
8
|
// License as published by the Free Software Foundation; either //
|
9
|
// version 2.1 of the License, or (at your option) any later version. //
|
10
|
// //
|
11
|
// This library 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 GNU //
|
14
|
// Lesser General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU Lesser General Public //
|
17
|
// License along with this library; if not, write to the Free Software //
|
18
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20
|
|
21
|
package org.distorted.library.mesh;
|
22
|
|
23
|
/**
|
24
|
* Create a 'multigon' mesh - a union of several polygons.
|
25
|
*
|
26
|
* <p>
|
27
|
* Specify several lists of vertices. Each list defines one polygon. (@see MeshPolygon).
|
28
|
* If two such polygons share an edge (or several edges) then the edge in both of them is
|
29
|
* marked 'up'.
|
30
|
*/
|
31
|
public class MeshMultigon extends MeshBase
|
32
|
{
|
33
|
private static final float MAX_ERROR = 0.0001f;
|
34
|
|
35
|
private boolean isSame(float[] v1, float[] v2)
|
36
|
{
|
37
|
float dx = v1[0]-v2[0];
|
38
|
float dy = v1[1]-v2[1];
|
39
|
return (dx*dx + dy*dy < MAX_ERROR);
|
40
|
}
|
41
|
|
42
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43
|
|
44
|
private boolean isUp(float[][][] vertices, int polygon, int edge)
|
45
|
{
|
46
|
float[][] p = vertices[polygon];
|
47
|
int lenP = p.length;
|
48
|
int len = vertices.length;
|
49
|
int next = (edge==lenP-1 ? 0 : edge+1);
|
50
|
|
51
|
float[] v1 = p[edge];
|
52
|
float[] v2 = p[next];
|
53
|
|
54
|
for(int i=0; i<len; i++)
|
55
|
if( i!=polygon )
|
56
|
{
|
57
|
int num = vertices[i].length;
|
58
|
|
59
|
for(int j=0; j<num; j++)
|
60
|
{
|
61
|
int n = (j==num-1 ? 0 : j+1);
|
62
|
if( isSame(v2,vertices[i][j]) && isSame(v1,vertices[i][n]) ) return true;
|
63
|
}
|
64
|
}
|
65
|
|
66
|
return false;
|
67
|
}
|
68
|
|
69
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
70
|
|
71
|
private boolean[][] computeUp(float[][] vertices, float[][] centers)
|
72
|
{
|
73
|
int num = vertices.length;
|
74
|
boolean[][] up = new boolean[num][];
|
75
|
float[][][] v = new float[num][][];
|
76
|
|
77
|
for(int i=0; i<num; i++)
|
78
|
{
|
79
|
int len = vertices[i].length/2;
|
80
|
v[i] = new float[len][2];
|
81
|
|
82
|
for(int j=0; j<len; j++)
|
83
|
{
|
84
|
v[i][j][0] = vertices[i][2*j ] + centers[i][0];
|
85
|
v[i][j][1] = vertices[i][2*j+1] + centers[i][1];
|
86
|
}
|
87
|
}
|
88
|
|
89
|
for(int i=0; i<num; i++)
|
90
|
{
|
91
|
int len = vertices[i].length/2;
|
92
|
up[i] = new boolean[len];
|
93
|
for(int j=0; j<len; j++)
|
94
|
{
|
95
|
up[i][j] = isUp(v,i,j);
|
96
|
|
97
|
android.util.Log.e("D", "polygon "+i+" edge "+j+" up: "+up[i][j]);
|
98
|
}
|
99
|
}
|
100
|
|
101
|
return up;
|
102
|
}
|
103
|
|
104
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
105
|
// PUBLIC API
|
106
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107
|
/**
|
108
|
* Specify several lists of vertices. Each list defines one polygon. (@see MeshPolygon).
|
109
|
* If two such polygons share an edge (or several edges) then the edge in both of them is
|
110
|
* marked 'up'.
|
111
|
*
|
112
|
* @param vertices an array of arrays, each specifying vertices of a single polygon.
|
113
|
* @param band see MeshPolygon. Shared among all polygons.
|
114
|
* @param exIndex see MeshPolygon. Shared among all polygons.
|
115
|
* @param exVertices see MeshPolygon. Shared among all polygons.
|
116
|
* @param centers for each polygon, coordinates of its center.
|
117
|
*/
|
118
|
public MeshMultigon(float[][] vertices, float[] band, int exIndex, int exVertices, float[][] centers)
|
119
|
{
|
120
|
super();
|
121
|
|
122
|
int numPolygons = vertices.length;
|
123
|
MeshPolygon[] meshes = new MeshPolygon[numPolygons];
|
124
|
boolean[][] up = computeUp(vertices,centers);
|
125
|
|
126
|
for(int i=0; i<numPolygons; i++)
|
127
|
meshes[i] = new MeshPolygon(vertices[i],band,up[i],exIndex,exVertices,centers[i][0],centers[i][1]);
|
128
|
|
129
|
join(meshes);
|
130
|
}
|
131
|
|
132
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
133
|
/**
|
134
|
* Copy constructor.
|
135
|
*/
|
136
|
public MeshMultigon(MeshMultigon mesh, boolean deep)
|
137
|
{
|
138
|
super(mesh,deep);
|
139
|
}
|
140
|
|
141
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142
|
/**
|
143
|
* Copy the Mesh.
|
144
|
*
|
145
|
* @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
|
146
|
* normals and inflates (the rest, in particular the mVertAttribs2 containing texture
|
147
|
* coordinates and effect associations, is always deep copied)
|
148
|
*/
|
149
|
public MeshMultigon copy(boolean deep)
|
150
|
{
|
151
|
return new MeshMultigon(this,deep);
|
152
|
}
|
153
|
}
|