Project

General

Profile

Download (17.2 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / json / JsonWriter.java @ e26eb4e7

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted 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                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objectlib.json;
21

    
22
import org.distorted.library.type.Static3D;
23
import org.distorted.library.type.Static4D;
24
import org.distorted.objectlib.helpers.ObjectShape;
25
import org.distorted.objectlib.helpers.ObjectSticker;
26
import org.distorted.objectlib.helpers.ScrambleState;
27
import org.json.JSONArray;
28
import org.json.JSONException;
29
import org.json.JSONObject;
30

    
31
import org.distorted.objectlib.main.TwistyObject;
32
import org.distorted.objectlib.main.ObjectType;
33

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

    
36
public class JsonWriter
37
{
38
  private static final int VERSION_MAJOR = 1;
39
  private static final int VERSION_MINOR = 0;
40

    
41
  private static JsonWriter mThis;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
  private JsonWriter()
46
    {
47

    
48
    }
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  private JSONArray generateCubits(TwistyObject object) throws JSONException
53
    {
54
    JSONArray array = new JSONArray();
55

    
56
    int[] numLayers     = object.getNumLayers();
57
    float[][] positions = object.getCubitPositions(numLayers);
58
    int numCubits = positions.length;
59
    int numFaces = object.getNumCubitFaces();
60

    
61
    for(int i=0; i<numCubits; i++)
62
      {
63
      JSONObject cubit = new JSONObject();
64
      Static4D rotQuat  = object.getQuat(i,numLayers);
65
      int variant       = object.getCubitVariant(i,numLayers);
66
      int[] solvedQuats = object.getSolvedQuats(i,numLayers);
67

    
68
      JSONArray pos = new JSONArray();
69
      int numPos = positions[i].length;
70
      for(int j=0; j<numPos; j++) pos.put(positions[i][j]);
71
      cubit.put("centers", pos);
72
      cubit.put("qx", rotQuat.get0() );
73
      cubit.put("qy", rotQuat.get1() );
74
      cubit.put("qz", rotQuat.get2() );
75
      cubit.put("qw", rotQuat.get3() );
76
      cubit.put("variant", variant );
77

    
78
      if( solvedQuats!=null )
79
        {
80
        JSONArray solved = new JSONArray();
81
        for (int solvedQuat : solvedQuats) solved.put(solvedQuat);
82
        cubit.put("solvedQuats",solved);
83
        }
84

    
85
      JSONArray colors = new JSONArray();
86

    
87
      for(int j=0; j<numFaces; j++)
88
        {
89
        int cubColor = object.getCubitFaceColor(i,j,numLayers);
90
        colors.put(cubColor);
91
        }
92
      cubit.put("colors",colors);
93

    
94
      array.put(cubit);
95
      }
96

    
97
    return array;
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  private JSONArray generateCorners(float[][] corners) throws JSONException
103
    {
104
    JSONArray array = new JSONArray();
105

    
106
    for(float[] c : corners)
107
      {
108
      JSONObject corner = new JSONObject();
109
      corner.put("strength", c[0]);
110
      corner.put("radius"  , c[1]);
111
      array.put(corner);
112
      }
113

    
114
    return array;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  private JSONArray generateCenters(float[][] centers) throws JSONException
120
    {
121
    JSONArray array = new JSONArray();
122

    
123
    for(float[] c : centers)
124
      {
125
      JSONObject center = new JSONObject();
126
      center.put("x", c[0]);
127
      center.put("y", c[1]);
128
      center.put("z", c[2]);
129
      array.put(center);
130
      }
131

    
132
    return array;
133
    }
134

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

    
137
  private JSONArray generateBands(float[][] bands) throws JSONException
138
    {
139
    JSONArray array = new JSONArray();
140

    
141
    for (float[] b : bands)
142
      {
143
      JSONObject band = new JSONObject();
144
      band.put("height"          , b[0]);
145
      band.put("angle"           , b[1]);
146
      band.put("distanceToCenter", b[2]);
147
      band.put("distanceToFlat"  , b[3]);
148
      band.put("numOfBands"      , b[4]);
149
      band.put("extraI"          , b[5]);
150
      band.put("extraJ"          , b[6]);
151
      array.put(band);
152
      }
153

    
154
    return array;
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  private JSONArray generateFaces(TwistyObject object, int[][] vertIndices, int[] bandIndices, int cubit, int[] numLayers) throws JSONException
160
    {
161
    JSONArray array = new JSONArray();
162
    int numFaces = vertIndices.length;
163
    int variant = object.getCubitVariant(cubit,numLayers);
164

    
165
    for(int i=0; i<numFaces; i++)
166
      {
167
      JSONObject face = new JSONObject();
168
      face.put("bandIndex", bandIndices[i]);
169

    
170
      int sticker = object.getVariantFaceColor(variant,i,numLayers);
171
      face.put("sticker",sticker);
172

    
173
      JSONArray vertArr = new JSONArray();
174
      int numV = vertIndices[i].length;
175
      for(int j=0; j<numV; j++) vertArr.put(vertIndices[i][j]);
176
      face.put("vertexIndices",vertArr);
177

    
178
      array.put(face);
179
      }
180

    
181
    return array;
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  private JSONArray generateVertices(double[][] vertices, int[] cornerIndices, int[] centerIndices) throws JSONException
187
    {
188
    JSONArray array = new JSONArray();
189
    int numVertices = vertices.length;
190

    
191
    for(int j=0; j<numVertices; j++)
192
      {
193
      JSONObject vert = new JSONObject();
194
      vert.put("x", vertices[j][0]);
195
      vert.put("y", vertices[j][1]);
196
      vert.put("z", vertices[j][2]);
197
      vert.put("cornerIndex", cornerIndices[j]);
198
      vert.put("centerIndex", centerIndices[j]);
199
      array.put(vert);
200
      }
201

    
202
    return array;
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  private JSONObject generateConvexity(float[] convexity) throws JSONException
208
    {
209
    JSONObject object = new JSONObject();
210
    object.put("x", convexity[0]);
211
    object.put("y", convexity[1]);
212
    object.put("z", convexity[2]);
213
    return object;
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  private int findCubitWithVariant(TwistyObject object, int variant, int numCubits, int[] numLayers)
219
    {
220
    for(int i=0; i<numCubits; i++)
221
      {
222
      if( object.getCubitVariant(i,numLayers)==variant ) return i;
223
      }
224

    
225
    return -1;
226
    }
227

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

    
230
  private JSONArray generateShapes(TwistyObject object) throws JSONException
231
    {
232
    JSONArray shapes = new JSONArray();
233

    
234
    int[] numLayers = object.getNumLayers();
235
    int numVariants = object.getNumCubitVariants(numLayers);
236
    float[][] positions = object.getCubitPositions(numLayers);
237
    int numCubits = positions.length;
238

    
239
    for(int i=0; i<numVariants; i++)
240
      {
241
      JSONObject shapeObj = new JSONObject();
242

    
243
      ObjectShape shape = object.getObjectShape(i);
244

    
245
      float[] convexity  = shape.getConvexityCenter();
246
      double[][] vertices= shape.getVertices();
247
      int[][] vertIndices= shape.getVertIndices();
248
      float[][] bands    = shape.getBands();
249
      int[] bandIndices  = shape.getBandIndices();
250
      float[][] corners  = shape.getCorners();
251
      int[] cornerIndices= shape.getCornerIndices();
252
      float[][] centers  = shape.getCenters();
253
      int[] centerIndices= shape.getCenterIndices();
254

    
255
      int cubit = findCubitWithVariant(object,i,numCubits,numLayers);
256

    
257
      if( convexity!=null )
258
        {
259
        JSONObject convObj = generateConvexity(convexity);
260
        shapeObj.put("convexity", convObj);
261
        }
262

    
263
      JSONArray verticesArr = generateVertices(vertices,cornerIndices,centerIndices);
264
      shapeObj.put("vertices", verticesArr);
265
      JSONArray facesArr = generateFaces(object,vertIndices,bandIndices,cubit,numLayers);
266
      shapeObj.put("faces", facesArr);
267
      JSONArray bandsArr = generateBands(bands);
268
      shapeObj.put("bands", bandsArr);
269
      JSONArray cornerArr = generateCorners(corners);
270
      shapeObj.put("cornerPush", cornerArr);
271
      JSONArray centerArr = generateCenters(centers);
272
      shapeObj.put("centerPush", centerArr);
273

    
274
      shapes.put(shapeObj);
275
      }
276

    
277
    return shapes;
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  private JSONArray generateStickers(TwistyObject object) throws JSONException
283
    {
284
    JSONArray stickers = new JSONArray();
285

    
286
    int[] numLayers = object.getNumLayers();
287
    int numStickers = object.getNumStickerTypes(numLayers);
288

    
289
    for(int i=0; i<numStickers; i++)
290
      {
291
      JSONObject stickerObj = new JSONObject();
292
      JSONArray  vertexArray= new JSONArray();
293

    
294
      ObjectSticker sticker = object.retSticker(i);
295

    
296
      float[] coords     = sticker.getCoords();
297
      float[] curvatures = sticker.getCurvature();
298
      float[] radii      = sticker.getRadii();
299
      float   stroke     = sticker.getStroke();
300

    
301
      stickerObj.put("stroke", stroke);
302
      int numVertices = radii.length;
303

    
304
      for(int j=0; j<numVertices; j++)
305
        {
306
        JSONObject vertex = new JSONObject();
307
        vertex.put("x", coords[2*j  ]);
308
        vertex.put("y", coords[2*j+1]);
309
        vertex.put("angle", curvatures==null ? 0 : curvatures[j]);
310
        vertex.put("radius",radii[j]);
311
        vertexArray.put(vertex);
312
        }
313
      stickerObj.put("vertices", vertexArray);
314
      stickers.put(stickerObj);
315
      }
316

    
317
    return stickers;
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
  private JSONObject generateMesh(TwistyObject object) throws JSONException
323
    {
324
    JSONObject mesh = new JSONObject();
325

    
326
    JSONArray cubits   = generateCubits(object);
327
    JSONArray shapes   = generateShapes(object);
328
    JSONArray stickers = generateStickers(object);
329

    
330
    mesh.put("cubits"  , cubits);
331
    mesh.put("shapes"  , shapes);
332
    mesh.put("stickers", stickers);
333

    
334
    return mesh;
335
    }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

    
339
  private JSONObject generateMetadata(TwistyObject object) throws JSONException
340
    {
341
    JSONObject metadata = new JSONObject();
342

    
343
    ObjectType type  = object.getObjectType();
344
    String inventor  = object.getInventor();
345
    String shortname = type.name();
346
    String lowerShort= shortname.toLowerCase();
347
    String longname  = object.getObjectName();
348

    
349
    metadata.put("longname"     , longname );
350
    metadata.put("shortname"    , shortname );
351
    metadata.put("inventor"     , inventor);
352
    metadata.put("year"         , object.getYearOfInvention());
353
    metadata.put("complexity"   , object.getComplexity());
354
    metadata.put("icon_huge"    , "h_"+lowerShort+".png");
355
    metadata.put("icon_big"     , "b_"+lowerShort+".png");
356
    metadata.put("icon_medium"  , "m_"+lowerShort+".png");
357
    metadata.put("icon_small"   , "s_"+lowerShort+".png");
358
    metadata.put("mesh"         , lowerShort+".dmesh");
359
    metadata.put("num_scrambles", type.getNumScramble() );
360
    metadata.put("size"         , object.getSize() );
361
    metadata.put("solved_func"  , object.getSolvedFunctionIndex() );
362

    
363
    return metadata;
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  private JSONArray generateQuats(TwistyObject object) throws JSONException
369
    {
370
    JSONArray quatsArray = new JSONArray();
371
    Static4D[] quats = object.getQuats();
372

    
373
    for(Static4D quat : quats)
374
      {
375
      JSONObject q = new JSONObject();
376
      q.put("x",quat.get0());
377
      q.put("y",quat.get1());
378
      q.put("z",quat.get2());
379
      q.put("w",quat.get3());
380
      quatsArray.put(q);
381
      }
382

    
383
    return quatsArray;
384
    }
385

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

    
388
  private JSONArray generateAxis(TwistyObject object) throws JSONException
389
    {
390
    JSONArray axis = new JSONArray();
391

    
392
    Static3D[] rotAxis = object.getRotationAxis();
393
    int numAxis = rotAxis.length;
394
    int[] basicAngle = object.getBasicAngle();
395
    int[] numLayers = object.getNumLayers();
396
    float[][] cuts = object.getCuts(numLayers);
397
    boolean[][] rotatable = object.getLayerRotatable(numLayers);
398

    
399
    for( int i=0; i<numAxis; i++ )
400
      {
401
      JSONObject axObject = new JSONObject();
402
      Static3D ax = rotAxis[i];
403

    
404
      axObject.put("x", ax.get0() );
405
      axObject.put("y", ax.get1() );
406
      axObject.put("z", ax.get2() );
407
      axObject.put("basicAngle", basicAngle[i] );
408

    
409
      JSONArray cutsArray = new JSONArray();
410
      for(float cut : cuts[i]) cutsArray.put(cut);
411
      axObject.put("cuts", cutsArray);
412
      JSONArray rotaArray = new JSONArray();
413
      for(boolean rot : rotatable[i]) rotaArray.put(rot);
414
      axObject.put("rotatable", rotaArray );
415

    
416
      axis.put(axObject);
417
      }
418

    
419
    return axis;
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
  private JSONObject generateScrambling(TwistyObject object) throws JSONException
425
    {
426
    JSONObject scrambling = new JSONObject();
427

    
428
    ScrambleState[] states = object.getScrambleStates();
429
    int scrambleType = object.getScrambleType();
430
    scrambling.put("scrambleType",scrambleType );
431

    
432
    if( states!=null )
433
      {
434
      JSONArray scrambleStates = new JSONArray();
435

    
436
      for(ScrambleState state : states)
437
        {
438
        JSONArray axisArray = new JSONArray();
439
        int numAxis = state.getNumAxis();
440

    
441
        for(int ax=0; ax<numAxis; ax++)
442
          {
443
          JSONArray axArray = new JSONArray();
444
          int[] axData = state.getAx(ax);
445

    
446
          if( axData!=null )
447
            for(int data : axData) axArray.put(data);
448

    
449
          axisArray.put(axArray);
450
          }
451

    
452
        scrambleStates.put(axisArray);
453
        }
454

    
455
      scrambling.put("scrambleStates", scrambleStates);
456
      }
457

    
458
    return scrambling;
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

    
463
  private JSONObject generateTouchControl(TwistyObject object) throws JSONException
464
    {
465
    JSONObject touchControl = new JSONObject();
466

    
467
    touchControl.put("movementType" , object.getMovementType() );
468
    touchControl.put("movementSplit", object.getMovementSplit() );
469

    
470
    int[][][] enabled = object.getEnabled();
471

    
472
    JSONArray enabledArray = new JSONArray();
473

    
474
    for(int[][] faceEnabled : enabled)
475
      {
476
      JSONArray faceArray = new JSONArray();
477

    
478
      for(int[] sectionEnabled : faceEnabled)
479
        {
480
        JSONArray sectionArray = new JSONArray();
481
        for(int ax : sectionEnabled) sectionArray.put(ax);
482
        faceArray.put(sectionArray);
483
        }
484
      enabledArray.put(faceArray);
485
      }
486

    
487
    touchControl.put("enabledAxis", enabledArray);
488

    
489
    return touchControl;
490
    }
491

    
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493

    
494
  public String createJsonString(TwistyObject object) throws JSONException
495
    {
496
    JSONObject json = new JSONObject();
497

    
498
    JSONObject metadata    = generateMetadata(object);
499
    JSONObject mesh        = generateMesh(object);
500
    JSONArray  axis        = generateAxis(object);
501
    JSONArray  quats       = generateQuats(object);
502
    JSONObject scrambling  = generateScrambling(object);
503
    JSONObject touchControl= generateTouchControl(object);
504

    
505
    json.put("major"       , VERSION_MAJOR);
506
    json.put("minor"       , VERSION_MINOR);
507
    json.put("metadata"    , metadata);
508
    json.put("mesh"        , mesh);
509
    json.put("axis"        , axis);
510
    json.put("quats"       , quats);
511
    json.put("scrambling"  , scrambling);
512
    json.put("touchcontrol", touchControl);
513

    
514
    String ret = json.toString();
515
    android.util.Log.e("D", ret);
516

    
517
    return ret;
518
    }
519

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521

    
522
  public static JsonWriter getInstance()
523
    {
524
    if( mThis==null ) mThis = new JsonWriter();
525
    return mThis;
526
    }
527
}
    (1-1/1)