Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedObject.java @ 6c295be1

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 final float[] mTmp;
26
    private BandagedCubit[] mCubits;
27

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

    
31
    int mMax;
32
    int mNumCubits;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
   int[] getSize()
66
     {
67
     return mSize;
68
     }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
   void createCubits(Static4D quatT, Static4D quatA, Static3D scale)
73
     {
74
     mCubits = new BandagedCubit[mNumCubits];
75
     float[][][] pos = getPositions();
76
     int c=0,numVariants = pos.length;
77

    
78
     for(int v=0; v<numVariants; v++)
79
       {
80
       int numCubits = pos[v].length;
81

    
82
       for(int vi=0; vi<numCubits; vi++)
83
         mCubits[c++] = new BandagedCubit(this,pos[v][vi],v,quatT,quatA,scale,false);
84
       }
85
     }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
   int computeMapsIndex(float[] faceAx)
90
      {
91
      int min=-1, numAxis = mFaceAxis.length;
92
      float error = Float.MAX_VALUE;
93
      float x = faceAx[0];
94
      float y = faceAx[1];
95
      float z = faceAx[2];
96

    
97
      for(int i=0; i<numAxis; i++)
98
        {
99
        float[] ax = mFaceAxis[i];
100
        float dx = x-ax[0];
101
        float dy = y-ax[1];
102
        float dz = z-ax[2];
103

    
104
        float diff = dx*dx + dy*dy + dz*dz;
105

    
106
        if( error>diff )
107
          {
108
          error = diff;
109
          min = i;
110
          }
111
        }
112

    
113
      return min;
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
   void resetObject(float scale)
119
     {
120
     for(int c=0; c<mNumCubits; c++)
121
       {
122
       if( !mCubits[c].isAttached() )
123
         {
124
         mCubits[c].attach();
125
         mScreen.attach(mCubits[c].getNode());
126
         }
127
       if( mCubits[c].getPosition().length>3 )
128
         {
129
         mCubits[c].reset(scale);
130
         }
131
       mCubits[c].setUnmarked();
132
       }
133
     }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
   float getMaxSize()
138
     {
139
     return mMax;
140
     }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
   float[][] getCubitPositions()
145
     {
146
     int numAttached = 0;
147

    
148
     for(int i=0; i<mNumCubits; i++)
149
       if( mCubits[i].isAttached() ) numAttached++;
150

    
151
     float[][] pos = new float[numAttached][];
152
     int attached=0;
153

    
154
     for(int i=0; i<mNumCubits; i++)
155
       if( mCubits[i].isAttached() )
156
         {
157
         pos[attached++] = mCubits[i].getPosition();
158
         }
159

    
160
     return pos;
161
     }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
   void tryConnectingCubits(int index1, int index2, float scale)
166
     {
167
     if( index1!=index2 )
168
       {
169
       float[] pos1 = mCubits[index1].getPosition();
170
       float[] pos2 = mCubits[index2].getPosition();
171

    
172
       if( isAdjacent(pos1,pos2) )
173
         {
174
         mCubits[index2].join(pos1,scale);
175
         mCubits[index1].detach();
176
         mScreen.detach(mCubits[index1].getNode());
177
         }
178
       }
179
     }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
   void attachCubits(float scale)
184
     {
185
     for(int i=0; i<mNumCubits; i++)
186
       {
187
       mCubits[i].scaleMove(scale);
188
       DistortedNode node = mCubits[i].getNode();
189
       mScreen.attach(node);
190
       }
191
     }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
   void attachAndMarkCubits(float scale, int touched)
196
     {
197
     for(int i=0; i<mNumCubits; i++)
198
       {
199
       if( mCubits[i].isAttached() )
200
         {
201
         mCubits[i].scaleMove(scale);
202
         if( touched==i ) mCubits[i].setMarked();
203
         else             mCubits[i].setUnmarked();
204
         DistortedNode node = mCubits[i].getNode();
205
         mScreen.attach(node);
206
         }
207
       }
208
     }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
   void scaleCubits(float scale)
213
     {
214
     for(int i=0; i<mNumCubits; i++) mCubits[i].scaleMove(scale);
215
     }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
   void recreateCubits(Static4D quatT, Static4D quatA, Static3D scale)
220
     {
221
     if( mCubits==null )
222
        {
223
        createCubits(quatT,quatA,scale);
224
        }
225
      else
226
        {
227
        for(int i=0; i<mNumCubits; i++) mCubits[i].recreateBitmap();
228
        }
229
     }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  void touchCubit(int index)
234
    {
235
    if( index>=0 && index<mNumCubits && mCubits[index]!=null ) mCubits[index].setMarked();
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  void untouchCubit(int index)
241
    {
242
    if( index>=0 && index<mNumCubits && mCubits[index]!=null ) mCubits[index].setUnmarked();
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  int whichCubitTouched(int face, float pointX, float pointY)
248
    {
249
    getTouchedPosition(mTmp, face, pointX, pointY);
250

    
251
    for(int c=0; c<mNumCubits; c++)
252
      if( mCubits[c].isAttached() )
253
        {
254
        float[] pos = mCubits[c].getPosition();
255
        int len = pos.length/3;
256

    
257
        for(int p=0; p<len; p++)
258
          {
259
          float dx = pos[3*p  ]-mTmp[0];
260
          float dy = pos[3*p+1]-mTmp[1];
261
          float dz = pos[3*p+2]-mTmp[2];
262

    
263
          if( dx*dx + dy*dy + dz*dz < 0.01f ) return c;
264
          }
265
        }
266

    
267
    android.util.Log.e("D", "whichCubitTouched: IMPOSSIBLE!!");
268
    return -1;
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

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

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

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

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