Project

General

Profile

Download (8.42 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / bandaged / BandagedObject.java @ 789769bd

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.bandaged;
11

    
12
import org.distorted.library.main.DistortedNode;
13
import org.distorted.library.main.DistortedScreen;
14
import org.distorted.library.mesh.MeshBase;
15
import org.distorted.library.type.Static3D;
16
import org.distorted.library.type.Static4D;
17
import org.distorted.objectlib.main.TwistyObject;
18

    
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
public abstract class BandagedObject
22
{
23
    private final DistortedScreen mScreen;
24
    private final float[][] mFaceAxis;
25
    private BandagedCubit[] mCubits;
26

    
27
    final int[] mSize;
28
    final float mDist2D;
29

    
30
    int mMax;
31
    int mNumCubits;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
   BandagedObject(DistortedScreen screen)
36
     {
37
     mScreen = screen;
38
     mSize = new int[3];
39
     mDist2D = getDist2D();
40
     Static3D[] axis = getFaceAxis();
41
     int numAxis = axis.length;
42
     mFaceAxis = new float[numAxis][];
43
     for(int i=0; i<numAxis; i++) mFaceAxis[i] = new float[] { axis[i].get0(), axis[i].get1(), axis[1].get2() };
44
     }
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
   abstract float getDist2D();
49
   abstract float[] getDist3D();
50
   abstract int[] getColors();
51
   abstract Static3D[] getFaceAxis();
52
   abstract float[][][] getPositions();
53
   abstract boolean isAdjacent(float dx, float dy, float dz);
54
   abstract int computeProjectionAngle();
55
   abstract boolean tryChangeObject(int x, int y, int z);
56
   abstract boolean isInsideFace(int face, float[] p);
57
   abstract TwistyObject createObject(int mode, float scale );
58
   abstract MeshBase createMesh(int variant, float[] pos, boolean round);
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
   void createCubits(Static4D quatT, Static4D quatA, Static3D scale)
63
     {
64
     mCubits = new BandagedCubit[mNumCubits];
65
     float[][][] pos = getPositions();
66
     int c=0,numVariants = pos.length;
67

    
68
     for(int v=0; v<numVariants; v++)
69
       {
70
       int numCubits = pos[v].length;
71

    
72
       for(int vi=0; vi<numCubits; vi++)
73
         mCubits[c++] = new BandagedCubit(this,pos[v][vi],v,quatT,quatA,scale,false);
74
       }
75
     }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
   int computeMapsIndex(float[] faceAx)
80
      {
81
      int min=-1, numAxis = mFaceAxis.length;
82
      float error = Float.MAX_VALUE;
83
      float x = faceAx[0];
84
      float y = faceAx[1];
85
      float z = faceAx[2];
86

    
87
      for(int i=0; i<numAxis; i++)
88
        {
89
        float[] ax = mFaceAxis[i];
90
        float dx = x-ax[0];
91
        float dy = y-ax[1];
92
        float dz = z-ax[2];
93

    
94
        float diff = dx*dx + dy*dy + dz*dz;
95

    
96
        if( error>diff )
97
          {
98
          error = diff;
99
          min = i;
100
          }
101
        }
102

    
103
      return min;
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
   void resetObject(float scale)
109
     {
110
     for(int c=0; c<mNumCubits; c++)
111
       {
112
       if( !mCubits[c].isAttached() )
113
         {
114
         mCubits[c].attach();
115
         mScreen.attach(mCubits[c].getNode());
116
         }
117
       if( mCubits[c].getPosition().length>3 )
118
         {
119
         mCubits[c].reset(scale);
120
         }
121
       mCubits[c].setUnmarked();
122
       }
123
     }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
   float getMaxSize()
128
     {
129
     return mMax;
130
     }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
   float[][] getCubitPositions()
135
     {
136
     int numAttached = 0;
137

    
138
     for(int i=0; i<mNumCubits; i++)
139
       if( mCubits[i].isAttached() ) numAttached++;
140

    
141
     float[][] pos = new float[numAttached][];
142
     int attached=0;
143

    
144
     for(int i=0; i<mNumCubits; i++)
145
       if( mCubits[i].isAttached() )
146
         {
147
         pos[attached++] = mCubits[i].getPosition();
148
         }
149

    
150
     return pos;
151
     }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
   void tryConnectingCubits(int index1, int index2, float scale)
156
     {
157
     if( index1!=index2 )
158
       {
159
       float[] pos1 = mCubits[index1].getPosition();
160
       float[] pos2 = mCubits[index2].getPosition();
161

    
162
       if( isAdjacent(pos1,pos2) )
163
         {
164
         mCubits[index2].join(pos1,scale);
165
         mCubits[index1].detach();
166
         mScreen.detach(mCubits[index1].getNode());
167
         }
168
       }
169
     }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
   void attachCubits(float scale)
174
     {
175
     for(int i=0; i<mNumCubits; i++)
176
       {
177
       mCubits[i].scaleMove(scale);
178
       DistortedNode node = mCubits[i].getNode();
179
       mScreen.attach(node);
180
       }
181
     }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
   void attachAndMarkCubits(float scale, int touched)
186
     {
187
     for(int i=0; i<mNumCubits; i++)
188
       {
189
       if( mCubits[i].isAttached() )
190
         {
191
         mCubits[i].scaleMove(scale);
192
         if( touched==i ) mCubits[i].setMarked();
193
         else             mCubits[i].setUnmarked();
194
         DistortedNode node = mCubits[i].getNode();
195
         mScreen.attach(node);
196
         }
197
       }
198
     }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
   void scaleCubits(float scale)
203
     {
204
     for(int i=0; i<mNumCubits; i++) mCubits[i].scaleMove(scale);
205
     }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
   void recreateCubits(Static4D quatT, Static4D quatA, Static3D scale)
210
     {
211
     if( mCubits==null )
212
        {
213
        createCubits(quatT,quatA,scale);
214
        }
215
      else
216
        {
217
        for(int i=0; i<mNumCubits; i++) mCubits[i].recreateBitmap();
218
        }
219
     }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
  void touchCubit(int index)
224
    {
225
    if( index>=0 && index<mNumCubits && mCubits[index]!=null ) mCubits[index].setMarked();
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  void untouchCubit(int index)
231
    {
232
    if( index>=0 && index<mNumCubits && mCubits[index]!=null ) mCubits[index].setUnmarked();
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  int whichCubitTouched(float[] point3D)
238
    {
239
    float x = point3D[0]*mMax;
240
    float y = point3D[1]*mMax;
241
    float z = point3D[2]*mMax;
242

    
243
    int minIndex = -1;
244
    float minDist = Float.MAX_VALUE;
245

    
246
    for(int c=0; c<mNumCubits; c++)
247
      if( mCubits[c].isAttached() )
248
        {
249
        float[] pos = mCubits[c].getPosition();
250
        int len = pos.length/3;
251

    
252
        for(int p=0; p<len; p++)
253
          {
254
          float dx = pos[3*p  ]-x;
255
          float dy = pos[3*p+1]-y;
256
          float dz = pos[3*p+2]-z;
257
          float dist = dx*dx + dy*dy + dz*dz;
258

    
259
          if( dist<minDist )
260
            {
261
            minDist = dist;
262
            minIndex = c;
263
            }
264
          }
265
        }
266

    
267
    return minIndex;
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
  boolean isAdjacent(float[] pos1, float[] pos2)
273
     {
274
     int len1 = pos1.length/3;
275
     int len2 = pos2.length/3;
276

    
277
     for(int i=0; i<len1; i++)
278
       for(int j=0; j<len2; j++)
279
         {
280
         float dx = pos1[3*i  ] - pos2[3*j  ];
281
         float dy = pos1[3*i+1] - pos2[3*j+1];
282
         float dz = pos1[3*i+2] - pos2[3*j+2];
283

    
284
         if( isAdjacent(dx,dy,dz) ) return true;
285
         }
286

    
287
     return false;
288
     }
289
}
(9-9/16)