Project

General

Profile

« Previous | Next » 

Revision b1bfcdc7

Added by Leszek Koltunski over 1 year ago

Beginnings of MeshMultigon (does not work yet)

View differences:

src/main/java/org/distorted/library/mesh/MeshMultigon.java
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++) up[i][j] = isUp(v,i,j);
94
      }
95

  
96
    return up;
97
    }
98

  
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
// PUBLIC API
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
/**
103
 * Specify several lists of vertices. Each list defines one polygon. (@see MeshPolygon).
104
 * If two such polygons share an edge (or several edges) then the edge in both of them is
105
 * marked 'up'.
106
 *
107
 * @param vertices   an array of arrays, each specifying vertices of a single polygon.
108
 * @param band       see MeshPolygon. Shared among all polygons.
109
 * @param exIndex    see MeshPolygon. Shared among all polygons.
110
 * @param exVertices see MeshPolygon. Shared among all polygons.
111
 * @param centers    for each polygon, coordinates of its center.
112
 */
113
  public MeshMultigon(float[][] vertices, float[] band, int exIndex, int exVertices, float[][] centers)
114
    {
115
    super();
116

  
117
    int numPolygons = vertices.length;
118
    MeshPolygon[] meshes = new MeshPolygon[numPolygons];
119
    boolean[][] up = computeUp(vertices,centers);
120

  
121
    for(int i=0; i<numPolygons; i++)
122
      meshes[i] = new MeshPolygon(vertices[i],band,up[i],exIndex,exVertices,centers[i][0],centers[i][1]);
123

  
124
    join(meshes);
125
    }
126

  
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
/**
129
 * Copy constructor.
130
 */
131
  public MeshMultigon(MeshMultigon mesh, boolean deep)
132
    {
133
    super(mesh,deep);
134
    }
135

  
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
/**
138
 * Copy the Mesh.
139
 *
140
 * @param deep If to be a deep or shallow copy of mVertAttribs1, i.e. the array holding vertices,
141
 *             normals and inflates (the rest, in particular the mVertAttribs2 containing texture
142
 *             coordinates and effect associations, is always deep copied)
143
 */
144
  public MeshMultigon copy(boolean deep)
145
    {
146
    return new MeshMultigon(this,deep);
147
    }
148
 }

Also available in: Unified diff