Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedObject.java @ 86308421

1 f404152d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 afd7e804 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
15 f404152d Leszek Koltunski
import org.distorted.library.type.Static3D;
16
import org.distorted.library.type.Static4D;
17
import org.distorted.objectlib.main.TwistyObject;
18
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21 86308421 Leszek Koltunski
public abstract class BandagedObject
22 f404152d Leszek Koltunski
{
23
    private final DistortedScreen mScreen;
24 86308421 Leszek Koltunski
    private final float[][] mFaceAxis;
25
    final int[] mSize;
26
    final float mDist2D;
27 f404152d Leszek Koltunski
28 86308421 Leszek Koltunski
    BandagedCubit[] mCubits;
29
    int mMax;
30
    int mNumCubits;
31 f404152d Leszek Koltunski
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
34
   BandagedObject(DistortedScreen screen)
35
     {
36
     mScreen = screen;
37 afd7e804 Leszek Koltunski
     mSize = new int[3];
38
     mDist2D = getDist2D();
39 86308421 Leszek Koltunski
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 afd7e804 Leszek Koltunski
     }
45
46 86308421 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48
   abstract float getDist2D();
49
   abstract float[] getDist3D();
50
   abstract int[] getColors();
51
   abstract Static3D[] getFaceAxis();
52
   abstract void createCubits(Static4D quatT, Static4D quatA, Static3D scale);
53
   abstract boolean isAdjacent(float[] pos1, float[] pos2);
54
   abstract int computeProjectionAngle();
55
   abstract boolean tryChangeObject(int x, int y, int z);
56
   abstract void stretchPoint(int face, float[] output);
57
   abstract int whichCubitTouched(int face, float pointX, float pointY);
58
   abstract boolean isInsideFace(int face, float[] p);
59
   abstract TwistyObject createObject(int mode, float scale );
60
   abstract MeshBase createMesh(float[] pos, boolean round);
61
62 afd7e804 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64
   int[] getSize()
65
     {
66
     return mSize;
67 f404152d Leszek Koltunski
     }
68
69 86308421 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71
   int computeMapsIndex(float[] faceAx)
72
      {
73
      int min=-1, numAxis = mFaceAxis.length;
74
      float error = Float.MAX_VALUE;
75
      float x = faceAx[0];
76
      float y = faceAx[1];
77
      float z = faceAx[2];
78
79
      for(int i=0; i<numAxis; i++)
80
        {
81
        float[] ax = mFaceAxis[i];
82
        float dx = x-ax[0];
83
        float dy = y-ax[1];
84
        float dz = z-ax[2];
85
86
        float diff = dx*dx + dy*dy + dz*dz;
87
88
        if( error>diff )
89
          {
90
          error = diff;
91
          min = i;
92
          }
93
        }
94
95
      return min;
96
      }
97
98 f404152d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99
100
   void resetObject(float scale)
101
     {
102
     for(int c=0; c<mNumCubits; c++)
103
       {
104
       if( !mCubits[c].isAttached() )
105
         {
106
         mCubits[c].attach();
107
         mScreen.attach(mCubits[c].getNode());
108
         }
109
       if( mCubits[c].getPosition().length>3 )
110
         {
111
         mCubits[c].reset(scale);
112
         }
113
       mCubits[c].setUnmarked();
114
       }
115
     }
116
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
119 afd7e804 Leszek Koltunski
   float getMaxSize()
120 f404152d Leszek Koltunski
     {
121 afd7e804 Leszek Koltunski
     return mMax;
122 f404152d Leszek Koltunski
     }
123
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126
   float[][] getCubitPositions()
127
     {
128
     int numAttached = 0;
129
130
     for(int i=0; i<mNumCubits; i++)
131
       if( mCubits[i].isAttached() ) numAttached++;
132
133
     float[][] pos = new float[numAttached][];
134
     int attached=0;
135
136
     for(int i=0; i<mNumCubits; i++)
137
       if( mCubits[i].isAttached() )
138
         {
139
         pos[attached++] = mCubits[i].getPosition();
140
         }
141
142
     return pos;
143
     }
144
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
147
   void tryConnectingCubits(int index1, int index2, float scale)
148
     {
149
     if( index1!=index2 )
150
       {
151
       float[] pos1 = mCubits[index1].getPosition();
152
       float[] pos2 = mCubits[index2].getPosition();
153
154
       if( isAdjacent(pos1,pos2) )
155
         {
156
         mCubits[index2].join(pos1,scale);
157
         mCubits[index1].detach();
158
         mScreen.detach(mCubits[index1].getNode());
159
         }
160
       }
161
     }
162
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
165
   void attachCubits(float scale)
166
     {
167
     for(int i=0; i<mNumCubits; i++)
168
       {
169
       mCubits[i].scaleMove(scale);
170
       DistortedNode node = mCubits[i].getNode();
171
       mScreen.attach(node);
172
       }
173
     }
174
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177
   void attachAndMarkCubits(float scale, int touched)
178
     {
179
     for(int i=0; i<mNumCubits; i++)
180
       {
181
       if( mCubits[i].isAttached() )
182
         {
183
         mCubits[i].scaleMove(scale);
184
         if( touched==i ) mCubits[i].setMarked();
185
         else             mCubits[i].setUnmarked();
186
         DistortedNode node = mCubits[i].getNode();
187
         mScreen.attach(node);
188
         }
189
       }
190
     }
191
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
194
   void scaleCubits(float scale)
195
     {
196
     for(int i=0; i<mNumCubits; i++) mCubits[i].scaleMove(scale);
197
     }
198
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201
   void recreateCubits(Static4D quatT, Static4D quatA, Static3D scale)
202
     {
203
     if( mCubits==null )
204
        {
205
        createCubits(quatT,quatA,scale);
206
        }
207
      else
208
        {
209
        for(int i=0; i<mNumCubits; i++) mCubits[i].recreateBitmap();
210
        }
211
     }
212
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
215
  void touchCubit(int index)
216
    {
217
    if( index>=0 && index<mNumCubits && mCubits[index]!=null ) mCubits[index].setMarked();
218
    }
219
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
222
  void untouchCubit(int index)
223
    {
224
    if( index>=0 && index<mNumCubits && mCubits[index]!=null ) mCubits[index].setUnmarked();
225
    }
226
}