Project

General

Profile

« Previous | Next » 

Revision f4ed769a

Added by Leszek Koltunski about 2 months ago

minor.

View differences:

src/main/java/org/distorted/objectlib/helpers/FactoryCubit.java
182 182
    mBuffer[0] /= len;
183 183
    mBuffer[1] /= len;
184 184
    mBuffer[2] /= len;
185

  
186
    // this theoretically should be uncommented to combat the same effect like in
187
    // TouchControlShapeChanging.FaceInfo, but when I try, the center sticker of
188
    // CoinTetrahedron gets inverted. Weird!
189
    // Uncommenting this does fix the fact that the convexity of the center cubits
190
    // of CoinT and CoinH goes concave
191

  
192
/*
193
    if( totalAngle(vertices,mBuffer)<0 )
194
      {
195
      mBuffer[0] *= -1;
196
      mBuffer[1] *= -1;
197
      mBuffer[2] *= -1;
198
      }
199
 */
200
    }
201

  
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
// this returns the total angle we get rotated about when we travel from the first vert to the last.
204
// It's always should be +2PI or -2PI, depending on if the verts are CW or CCW (when looking at the
205
// plane formed by the vertices from the direction the 'normal' vector points towards)
206
//
207
// The point: this way we can detect if the 'normal' vector is correct, i.e. if it points in the
208
// right direction.
209
// Sometimes it doesn't, when the first three vertices (from which the vector is computed) are 'concave'.
210

  
211
  public static float totalAngle(float[][] vert, float[] normal)
212
    {
213
    float ret = 0;
214
    int num = vert.length;
215

  
216
    for(int c=0; c<num; c++)
217
      {
218
      int n = (c==num-1 ? 0 : c+1);
219
      int m = (n==num-1 ? 0 : n+1);
220
      ret += angle( vert[c],vert[n],vert[m], normal);
221
      }
222

  
223
    return ret;
224
    }
225

  
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

  
228
  private static float angle(float[] v0, float[] v1, float[] v2, float[] normal)
229
    {
230
    float px = v1[0] - v0[0];
231
    float py = v1[1] - v0[1];
232
    float pz = v1[2] - v0[2];
233

  
234
    float rx = v2[0] - v1[0];
235
    float ry = v2[1] - v1[1];
236
    float rz = v2[2] - v1[2];
237

  
238
    float l1 = (float)Math.sqrt(px*px + py*py + pz*pz);
239
    float l2 = (float)Math.sqrt(rx*rx + ry*ry + rz*rz);
240

  
241
    px /= l1;
242
    py /= l1;
243
    pz /= l1;
244

  
245
    rx /= l2;
246
    ry /= l2;
247
    rz /= l2;
248

  
249
    float s = px*rx + py*ry + pz*rz;
250

  
251
    if( s> 1 ) s= 1;
252
    if( s<-1 ) s=-1;
253

  
254
    float a = (float) Math.acos(s);
255
    float[] cross = crossProduct(px,py,pz,rx,ry,rz);
256

  
257
    float ax = cross[0] + normal[0];
258
    float ay = cross[1] + normal[1];
259
    float az = cross[2] + normal[2];
260

  
261
    float bx = cross[0] - normal[0];
262
    float by = cross[1] - normal[1];
263
    float bz = cross[2] - normal[2];
264

  
265
    float f1 = ax*ax + ay*ay + az*az;
266
    float f2 = bx*bx + by*by + bz*bz;
267

  
268
    return f1>f2 ? a : -a;
269
    }
270

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

  
273
  private static float[] crossProduct(float a1, float a2, float a3, float b1, float b2, float b3)
274
    {
275
    float[] ret = new float[3];
276

  
277
    ret[0] = a2*b3 - a3*b2;
278
    ret[1] = a3*b1 - a1*b3;
279
    ret[2] = a1*b2 - a2*b1;
280

  
281
    return ret;
185 282
    }
186 283

  
187 284
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/objectlib/helpers/FactorySticker.java
258 258

  
259 259
    float dx = x-vx;
260 260
    float dy = y-vy;
261
    float d = (float)Math.sqrt(dx*dx+dy*dy);
262
    float f = (float)Math.sqrt(r*r-d*d);
261
    float d_squared = dx*dx+dy*dy;
262
    float f = (float)Math.sqrt(r*r-d_squared);
263 263
    float e = f / ((float)Math.sqrt(m*m+1));
264 264

  
265 265
    if( c==0 )
......
561 561
      startA *= 180/PI;
562 562
      sweepA *= 180/PI;
563 563

  
564
//android.util.Log.e("D", "drawing arc ox="+oX+" oy="+oY+" R="+R+" startA="+startA+" sweepA="+sweepA+" stopA="+(stopA*(180/PI)));
565
//android.util.Log.e("D", "drawing arc v1="+v1x+" , "+v1y+" v2="+v2x+" , "+v2y+" tex: "+mTexHeight);
564
// android.util.Log.e("D", "drawing arc ox="+oX+" oy="+oY+" R="+R+" startA="+startA+" sweepA="+sweepA+" stopA="+(stopA*(180/PI)));
565
// android.util.Log.e("D", "drawing arc v1="+v1x+" , "+v1y+" v2="+v2x+" , "+v2y+" tex: "+mTexHeight);
566 566

  
567 567
      canvas.drawArc( left+oX-R, bottom-oY-R, left+oX+R, bottom-oY+R, startA, sweepA, false, paint);
568 568
      }
569 569
    else
570 570
      {
571
//android.util.Log.e("D", "drawing line from "+v1x+" , "+v1y+" to "+v2x+" , "+v2y);
571
// android.util.Log.e("D", "drawing line from "+v1x+" , "+v1y+" to "+v2x+" , "+v2y);
572 572

  
573 573
      canvas.drawLine( left+v1x, bottom-v1y, left+v2x, bottom-v2y, paint);
574 574
      }
......
642 642
  public void drawRoundedPolygons(Canvas canvas, Paint paint, int left, int bottom, int color,
643 643
                                  int height, ObjectSticker sticker, float borders, float corners)
644 644
    {
645
    //android.util.Log.d("D", "DRAW ROUNDED POLYGONS");
646

  
647 645
    mTexHeight = height;
648 646

  
649 647
    float[][] strokes    = sticker.getStrokes();
src/main/java/org/distorted/objectlib/touchcontrol/TouchControlShapeChanging.java
12 12
import org.distorted.library.helpers.QuatHelper;
13 13
import org.distorted.library.type.Static3D;
14 14
import org.distorted.library.type.Static4D;
15
import org.distorted.objectlib.helpers.FactoryCubit;
15 16
import org.distorted.objectlib.helpers.ObjectShape;
16 17
import org.distorted.objectlib.main.TwistyObject;
17 18

  
......
76 77
      normal[2] = vz;
77 78
      normal[3] = 0.0f;
78 79

  
79
      if( totalAngle(vertices,normal)<0 )
80
      if( FactoryCubit.totalAngle(vertices,normal)<0 )
80 81
        {
81 82
        normal[0] *= -1;
82 83
        normal[1] *= -1;
......
86 87
      distance = normal[0]*v0[0] + normal[1]*v0[1] + normal[2]*v0[2];
87 88
      }
88 89

  
89
    //////////////////////////////////////////////////////////
90
    // this returns the total angle we get rotated about when
91
    // we travel from the first vert to the last.
92
    // It's always should be +2PI or -2PI, depending on if the
93
    // verts are CW or CCW (when looking at the plane formed by
94
    // the vertices from the direction the 'normal' vector
95
    // points towards)
96
    //
97
    // The point: this way we can detect if the 'normal' vector
98
    // is correct, i.e. if it points in the right direction.
99
    // Sometimes it does not, when the first three vertices
100
    // (from which the vector is computed) are 'concave'.
101

  
102
    private float totalAngle(float[][] vert, float[] normal)
103
      {
104
      float ret = 0;
105
      int num = vert.length;
106

  
107
      for(int c=0; c<num; c++)
108
        {
109
        int n = (c==num-1 ? 0 : c+1);
110
        int m = (n==num-1 ? 0 : n+1);
111
        ret += angle( vert[c],vert[n],vert[m], normal);
112
        }
113

  
114
      return ret;
115
      }
116

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

  
119
    private float angle(float[] v0, float[] v1, float[] v2, float[] normal)
120
      {
121
      float px = v1[0] - v0[0];
122
      float py = v1[1] - v0[1];
123
      float pz = v1[2] - v0[2];
124

  
125
      float rx = v2[0] - v1[0];
126
      float ry = v2[1] - v1[1];
127
      float rz = v2[2] - v1[2];
128

  
129
      float l1 = (float)Math.sqrt(px*px + py*py + pz*pz);
130
      float l2 = (float)Math.sqrt(rx*rx + ry*ry + rz*rz);
131

  
132
      px /= l1;
133
      py /= l1;
134
      pz /= l1;
135

  
136
      rx /= l2;
137
      ry /= l2;
138
      rz /= l2;
139

  
140
      float s = px*rx + py*ry + pz*rz;
141

  
142
      if( s> 1 ) s= 1;
143
      if( s<-1 ) s=-1;
144

  
145
      float a = (float) Math.acos(s);
146
      float[] cross = crossProduct(px,py,pz,rx,ry,rz);
147

  
148
      float ax = cross[0] + normal[0];
149
      float ay = cross[1] + normal[1];
150
      float az = cross[2] + normal[2];
151

  
152
      float bx = cross[0] - normal[0];
153
      float by = cross[1] - normal[1];
154
      float bz = cross[2] - normal[2];
155

  
156
      float f1 = ax*ax + ay*ay + az*az;
157
      float f2 = bx*bx + by*by + bz*bz;
158

  
159
      return f1>f2 ? a : -a;
160
      }
161

  
162
    //////////////////////////////////////////////////////////
163

  
164
    private float[] crossProduct(float a1, float a2, float a3, float b1, float b2, float b3)
165
      {
166
      float[] ret = new float[3];
167

  
168
      ret[0] = a2*b3 - a3*b2;
169
      ret[1] = a3*b1 - a1*b3;
170
      ret[2] = a1*b2 - a2*b1;
171

  
172
      return ret;
173
      }
174

  
175 90
    //////////////////////////////////////////////////////////
176 91
    public float[] getNormal()
177 92
      {

Also available in: Unified diff