Вы находитесь на странице: 1из 118

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

// THE PDF VERSION OF THIS FILE DOES NOT DISPLAY UNDERSCORE CHARACTERS CORRECTLY,
// EXCEPT WHEN MAGNIFICATION IS INCREASED TO 125% ... 150%. WHEN IN DOUBT ABOUT
// EMPTY SPACES BETWEEN ADJACENT LETTERS IN AN IDENTIFIER, INCREASE THE MAG VALUE.
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;

// R3dDLL.cs --- COPYRIGHT Glenn A. Merritt / CQvis, 2011


namespace R3dDLL
{
#region STATIC CLASS Ro - Rotator field r
public static class Ro
{
public static Rotator r = new Rotator(); public static bool valid=true;
}
#endregion
#region STATIC CLASS Rho
public static class Rho
{
public static IndexTriple[,] MI =
{{new IndexTriple(0,0,0),new IndexTriple(0,0,0),new IndexTriple(0,0,0),new
IndexTriple(0,0,0)},
{new IndexTriple(0,0,0),new IndexTriple(1,1,1),new IndexTriple(3,4,5),new
IndexTriple(2,4,3)},
{new IndexTriple(0,0,0),new IndexTriple(3,5,4),new IndexTriple(2,2,2),new
IndexTriple(1,3,4)},
{new IndexTriple(0,0,0),new IndexTriple(2,4,5),new IndexTriple(1,5,4),new
IndexTriple(1,4,2)},
{new IndexTriple(0,0,0),new IndexTriple(2,5,3),new IndexTriple(1,3,5),new
IndexTriple(1,2,3)},
{new IndexTriple(0,0,0),new IndexTriple(2,3,4),new IndexTriple(1,4,3),new
IndexTriple(5,5,5)}};
public static int II=0,JJ=0,KK=0,LL=0,MM=0;
public static void SetKL(int m, int i, int j)
{
SetJKL(m,i);
Rho3RotateTo(j,ref JJ,ref KK,ref LL);
}
public static void SetJKL(int m, int i)
{
MM = m; II = i; MM = ((MM-1)%5)+1; II = ((II-1)%5)+1;
JJ = MI[MM,II].J; KK = MI[MM,II].K; LL = MI[MM,II].L;
}
public static void SetIJKL(int m)
{
MM = m; II = m+1; MM = ((MM-1)%5)+1; II = ((II-1)%5)+1;
JJ = MI[MM,II].J; KK = MI[MM,II].K; LL = MI[MM,II].L;
}
public static void Rho3RotateTo(int U, ref int u, ref int v, ref int w)
{

IndexTriple(0,0,0),new IndexTriple(0,0,0),new
IndexTriple(2,5,4),new IndexTriple(2,3,5),new
IndexTriple(1,4,5),new IndexTriple(1,5,3),new
IndexTriple(3,3,3),new IndexTriple(1,2,5),new
IndexTriple(1,5,2),new IndexTriple(4,4,4),new
IndexTriple(1,2,4),new IndexTriple(1,3,2),new

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

int t=u,i=1,u1=u,v1=v,w1=w;
while ((i <= 3) & (u1 != U))
{
t = u1; u1 = v1; v1 = w1; w1 = t;
i++;
}
if (i > 1)
{
if (u1 == U) { u = u1; v = v1; w = w1; }
}
}
}
#endregion
#region IndexPair CLASS
/// <summary>
/// For use with the RhoJK table internal to the D3tetra class
/// </summary>
public struct IndexPair
{
int k,l;
public IndexPair(int i, int j)
{
k = i; l = j;
}
public int K { get { return k; } }
public int L { get { return l; } }
}
#endregion
#region IndexTriple CLASS
public struct IndexTriple
{
int j,k,l;
public IndexTriple(int jj, int kk, int ll)
{
j = jj; k = kk; l = ll;
}
public int J { get { return j; } }
public int K { get { return k; } }
public int L { get { return l; } }
}
#endregion
#region Numeric 3-tuple CLASSES
public class D3tuple
{
double xx; double yy; double zz;
public D3tuple() { xx = 0; yy = 0; zz = 0; }
public D3tuple(double x, double y, double z) { xx = x; yy = y; zz = z; }
public void Set(double x, double y, double z) { xx = x; yy = y; zz = z; }
public void Get(ref double x, ref double y, ref double z) { x = xx; y = yy; z = zz; }
/// <summary> X = a[0] = a4[1] = Numer
/// </summary>
public double X { get { return xx; } set { xx = (double)value; } }
/// <summary> Y = a[1] = a4[2] = Denom
/// </summary>
public double Y { get { return yy; } set { yy = (double)value; } }
/// <summary> Z = a[2] = a4[3] = Ratio
/// </summary>
public double Z { get { return zz; } set { zz = (double)value; } }

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

/// <summary> Numer = X = a[0] = a4[1]


/// </summary>
public double Numer { get { return xx; } set { xx = (double)value; } }
/// <summary> Denom = Y = a[1] = a4[2]
/// </summary>
public double Denom { get { return yy; } set { yy = (double)value; } }
/// <summary> Ratio = Z = a[2] = a4[3]
/// </summary>
public double Ratio { get { return zz; } set { zz = (double)value; } }
/// <summary>
/// This accessor returns a component of the D3tuple.
/// </summary>
/// <param name="i">Index of component (1..3)</param>
/// <returns>X, Y, or Z when i = 1, 2, or 3</returns>
public double this[int i]
{
get
{
double res = new double();
if ((i >= 1) & (i <= 3)) res = a4[i];
return res;
}
}
/// <summary> a = [X,Y,Z] = [Numer,Denom,Ratio] = [a4[1],a4[2],a4[3]]
/// </summary>
public double[] a
{
get { double[] v = new double[3]; v[0] = xx; v[1] = yy; v[2] = zz; return v; }
set
{
double[] v = value;
if (value.Length == 4) { xx = v[1]; yy = v[2]; zz = v[3]; }
else if (value.Length == 3) { xx = v[0]; yy = v[1]; zz = v[2]; }
}
}
/// <summary><para> a4 = [0,X,Y,Z] = [0,Numer,Denom,Ratio] = [0,a[0],a[1],a[2]]</para>
/// <para>
/// The a4 accessor is provided to allow an array of length 4 with elements
/// 1..3 containing the X,Y,Z values to be used to get and set 3tuples.</para>
/// </summary>
public double[] a4
{
get { double[] v = new double[4]; v[1] = xx; v[2] = yy; v[3] = zz; return v; }
set
{
double[] v = value;
if (value.Length == 4) { xx = v[1]; yy = v[2]; zz = v[3]; }
}
}
public D3tuple Square()
{
D3tuple res = new D3tuple();
res.X = X*X; res.Y = Y*Y; res.Z = Z*Z;
return res;
}
public double Sum()
{
double res = new double();

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

res = X+Y+Z;
return res;
}
/// <summary>
/// ZeroRef() sets the components X,Y,Z of a D3tuple to zero.
/// </summary>
public void Zero() { xx = 0; yy = 0; zz = 0; }
/// <summary>
/// IsZero() returns true if the current instance is the zero vector.
/// </summary>
/// <returns>True if this instance is the zero vector</returns>
public bool IsZero()
{
return ((X*X+Y*Y+Z*Z) == 0);
}
/// <summary> ~ Norm (Vector Length) Operator: ~(A) = |A|
/// </summary>
public static double operator~(D3tuple d1)
{
double d; d = Math.Sqrt(d1.X*d1.X+d1.Y*d1.Y+d1.Z*d1.Z);
return d;
}
/// <summary> ! Unit Vector Operator: !A = A/|A|
/// </summary>
public static D3tuple operator!(D3tuple d1)
{
D3tuple d = new D3tuple();
double L = ~d1; if (L != 0) { d.X = d1.X / L; d.Y = d1.Y / L; d.Z = d1.Z / L; }
return d;
}
/// <summary> - Pole Reversal Operator: A' = -A
/// </summary>
public static D3tuple operator-(D3tuple d1)
{
return new D3tuple(-d1.X, -d1.Y, -d1.Z);
}
/// <summary> + Addition Operator C = A + B
/// </summary>
public static D3tuple operator+(D3tuple d1, D3tuple d2)
{
return new D3tuple(d1.X+d2.X, d1.Y+d2.Y, d1.Z+d2.Z);
}
/// <summary> - Subtraction Operator C = B - A
/// </summary>
public static D3tuple operator-(D3tuple d1, D3tuple d2)
{
return new D3tuple(d1.X-d2.X, d1.Y-d2.Y, d1.Z-d2.Z);
}
/// <summary> * Scalar (Dot) Product Operator: A*B = a1*b1+a2*b2+a3*b3
/// </summary>
public static double operator*(D3tuple d1, D3tuple d2)
{
double d = new double();
d = d1.xx*d2.xx+d1.yy*d2.yy+d1.zz*d2.zz;
return d;
}
/// <summary> * Scalar Component Multiplication Operator: h*A = [h*a1,h*a2,h*a3]
/// </summary>

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

/// <param name="h">Scalar multiplier h</param>


/// <param name="d1">Vector A to be multiplied by h</param>
/// <returns>C = [h*d1.X,h*d1.Y,h*d1.Z]</returns>
public static D3tuple operator*(double h, D3tuple d1)
{
D3tuple dr = new D3tuple(h*d1.X,h*d1.Y,h*d1.Z);
return dr;
}
/// <summary> / Length Division Operator: A/B = A / |B| or [0,0,0]
/// </summary>
/// <param name="d1">Vector A whose components are to be divided</param>
/// <param name="d2">Vector B whose length provides the divisor L</param>
/// <returns>C = [d1.X/L,d1.Y/L,d1.Z/L]</returns>
public static D3tuple operator/(D3tuple d1, D3tuple d2)
{
D3tuple d = new D3tuple(); double L = Math.Sqrt(d2.X*d2.X+d2.Y*d2.Y+d2.Z*d2.Z);
if (L != 0) { d.X = d1.X / L; d.Y = d1.Y / L; d.Z = d1.Z / L; }
return d;
}
/// <summary> / Scalar Divide Components Operator: A/h = [a1/h,a2/h,a3/h]
/// </summary>
/// <param name="d1">Vector A whose components are to be divided</param>
/// <param name="v">Scalar divisor v</param>
/// <returns>C = [a1/v,a2/v,a3/v]</returns>
public static D3tuple operator/(D3tuple d1, double v)
{
D3tuple d = new D3tuple(d1.X,d1.Y,d1.Z);
if (v != 0) { d.X = d1.X / v; d.Y = d1.Y / v; d.Z = d1.Z / v; }
return d;
}
/// <summary> ^ Vector Product Operator: A^B = [a2b3-a2b3,a3b1-a3b1,a1b2-a1b2] <para>
/// Operator ^ is the vector (cross) product operator</para>
/// </summary>
/// <param name="d1">Vector A</param>
/// <param name="d2">Vector B</param>
/// <returns>CrossProd(A,B)</returns>
public static D3tuple operator^(D3tuple d1, D3tuple d2)
{
return new D3tuple(d1.yy*d2.zz-d2.yy*d1.zz, d1.zz*d2.xx-d2.zz*d1.xx, d1.xx*d2.yy-d2.xx*d1.yy);
}
/// <summary> &amp; Carrot Product Operator: A&amp;B = [a1*b1,a2*b2,a3*b3]
/// <para>Operator &amp; is the Carrot Product operator, which multiplies
/// the components of vector A with those of vector B</para>
/// </summary>
/// <param name="d1">Vector A = [a1,a2,a3]</param>
/// <param name="d2">Vector B = [b1,b2,b3]</param>
/// <returns>[a1*b1,a2*b2,a3*b3]</returns>
public static D3tuple operator&(D3tuple d1, D3tuple d2)
{
return new D3tuple(d1.X*d2.X, d1.Y*d2.Y, d1.Z*d2.Z);
}
/// <summary> &gt;&gt; Right-shift Indexes Operator <para>
/// The right shift operator shifts component indexing to the right
/// in the 3-component ring. A shift count of +1 will cause the Y
/// component to be the first component of the result vector, with
/// the Z and X components following it in cyclic order. A shift count
/// of 2 would place the Z component in the first component position
/// in the result vector, with the X and Y components following it.

285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342

/// Shift counts of 1 and -2 are equivalent, as are 2 and -1.</para>


/// </summary>
/// <param name="d1">The vector whose components are to be rotated.</param>
/// <param name="k">The rotation count</param>
/// <returns>The rotated vector</returns>
public static D3tuple operator>>(D3tuple d1, int k)
{
int c = k % 3; D3tuple d = new D3tuple(d1.X,d1.Y,d1.Z);
if ((c == 1)|(c == -2)) d.Set(d1.Y, d1.Z, d1.X);
else if ((c == 2)|(c == -1)) d.Set(d1.Z, d1.X, d1.Y);
return d;
}
/// <summary> &lt;&lt; Left-shift Indexes Operator <para>
/// The left shift operator shifts component indexing to the left
/// in the 3-component ring. A shift count of +1 would cause the Z
/// component to be the first component of the result vector, with
/// the X and Y components following it in cyclic order.</para>
/// </summary>
public static D3tuple operator<<(D3tuple d1, int k)
{
int c = k % 3; D3tuple d = new D3tuple(d1.X,d1.Y,d1.Z);
if ((c == -1)|(c == 2)) d.Set(d1.Y, d1.Z, d1.X);
else if ((c == -2)|(c == 1)) d.Set(d1.Z, d1.X, d1.Y);
return d;
}
bool WriteSquare = false;
string sf(double d)
{
string res = ""; if (WriteSquare) d = d * d;
res = string.Format("{0,18:F15}", d);
int p = res.IndexOf(' '),pm=res.IndexOf('-'),L=res.Length;
if (p >= 0)
{
while ((p < L) & (res[p] == ' ')) p++;
if ((p > 0) & (p == pm)) { res.Remove(p-1, 1); }
else if (pm < 0) { }
}
else if (pm < 0) { res = " " + res; }
return res;
}
/// <summary>
/// The ToString() override produces the form "[xxxxxx,yyyyyy,zzzzzz]" as a
/// string containing the numeric values of the components of the D3tuple.
/// </summary>
/// <returns></returns>
public override string ToString()
{
string s="";//,sf="{0,20:F16}";// s= "["+xx.ToString()+","+yy.ToString()+","+zz.ToString()+"]";
//s= "["+string.Format(sf,xx)+","+string.Format(sf,yy)+","+string.Format(sf,zz)+"]";
s= "["+sf(xx)+","+sf(yy)+","+sf(zz)+"]";
return s;
}
public string ToSqString()
{
WriteSquare = true; string s=this.ToString(); WriteSquare = false;
return s;
}
}

343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400

public class F3tuple


{
float xx; float yy; float zz;
public F3tuple() { xx = 0; yy = 0; zz = 0; }
public F3tuple(float x, float y, float z) { xx = x; yy = y; zz = z; }
public void Set(float x, float y, float z) { xx = x; yy = y; zz = z; }
public void Get(ref float x, ref float y, ref float z) { x = xx; y = yy; z = zz; }
public float X { get { return xx; } set { xx = (float)value; } }
public float Y { get { return yy; } set { yy = (float)value; } }
public float Z { get { return zz; } set { zz = (float)value; } }
public float Numer { get { return xx; } set { xx = (float)value; } }
public float Denom { get { return yy; } set { yy = (float)value; } }
public float Ratio { get { return zz; } set { zz = (float)value; } }
public float this[int i]
{
get
{
float res = new float();
if ((i >= 1) & (i <= 3)) res = a4[i];
return res;
}
}
public float[] a
{
get { float[] v = new float[3]; v[0] = xx; v[1] = yy; v[2] = zz; return v; }
set
{
float[] v = value;
if (value.Length == 4) { xx = v[1]; yy = v[2]; zz = v[3]; }
else if (value.Length == 3) { xx = v[0]; yy = v[1]; zz = v[2]; }
}
}
/// <summary>
/// The a4 accessor is designed to allow an array of length 4 with elements
/// 1..3 containing the X,Y,Z values to be used to get and set 3tuples.
/// </summary>
public float[] a4
{
get { float[] v = new float[4]; v[1] = xx; v[2] = yy; v[3] = zz; return v; }
set
{
float[] v = value;
if (value.Length == 4) { xx = v[1]; yy = v[2]; zz = v[3]; }
}
}
public void Zero() { xx = 0; yy = 0; zz = 0; }
public static float operator~(F3tuple f1)
{
float f; f = (float)Math.Sqrt(f1.X*f1.X+f1.Y*f1.Y+f1.Z*f1.Z);
return f;
}
public static F3tuple operator!(F3tuple f1)
{
F3tuple f = new F3tuple();
float L = ~f1; if (L != 0) { f.X = f1.X / L; f.Y = f1.Y / L; f.Z = f1.Z / L; }
return f;
}
public static F3tuple operator-(F3tuple f1)

401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458

{
return new F3tuple(-f1.X, -f1.Y, -f1.Z);
}
public static F3tuple operator +(F3tuple f1, F3tuple f2)
{
return new F3tuple(f1.X+f2.X, f1.Y+f2.Y, f1.Z+f2.Z);
}
public static F3tuple operator -(F3tuple f1, F3tuple f2)
{
return new F3tuple(f1.X-f2.X, f1.Y-f2.Y, f1.Z-f2.Z);
}
public static float operator*(F3tuple f1, F3tuple f2)
{
float f = new float();
f = f1.xx*f2.xx+f1.yy*f2.yy+f1.zz*f2.zz;
return f;
}
public static F3tuple operator*(float f, F3tuple f1)
{
F3tuple fr = new F3tuple(f*f1.X,f*f1.Y,f*f1.Z);
return fr;
}
public static F3tuple operator/(F3tuple f1, F3tuple f2)
{
F3tuple f = new F3tuple(); float L = (float)Math.Sqrt(f2.X*f2.X+f2.Y*f2.Y+f2.Z*f2.Z);
if (L != 0) { f.X = f1.X / L; f.Y = f1.Y / L; f.Z = f1.Z / L; }
return f;
}
public static F3tuple operator/(F3tuple f1, float v)
{
F3tuple f = new F3tuple(f1.X,f1.Y,f1.Z);
if (v != 0) { f.X = f1.X / v; f.Y = f1.Y / v; f.Z = f1.Z / v; }
return f;
}
public static F3tuple operator^(F3tuple f1, F3tuple f2)
{
return new F3tuple(f1.yy*f2.zz-f2.yy*f1.zz, f1.zz*f2.xx-f2.zz*f1.xx, f1.xx*f2.yy-f2.xx*f1.yy);
}
//public static bool operator==(F3tuple f1, F3tuple f2)
//{
// return ((f1.X==f2.X) & (f1.Y==f2.Y) & (f1.Z==f2.Z));
//}
//public static bool operator!=(F3tuple f1, F3tuple f2)
//{
// return ((f1.X!=f2.X) & (f1.Y!=f2.Y) & (f1.Z!=f2.Z));
//}
public static F3tuple operator>>(F3tuple f1, int k)
{
int c = k % 3; F3tuple f = new F3tuple(f1.X,f1.Y,f1.Z);
if ((c == 1)|(c == -2)) f.Set(f1.Y, f1.Z, f1.X);
else if ((c == 2)|(c == -1)) f.Set(f1.Z, f1.X, f1.Y);
return f;
}
public static F3tuple operator<<(F3tuple f1, int k)
{
int c = k % 3; F3tuple f = new F3tuple(f1.X,f1.Y,f1.Z);
if ((c == -1)|(c == 2)) f.Set(f1.Y, f1.Z, f1.X);
else if ((c == -2)|(c == 1)) f.Set(f1.Z, f1.X, f1.Y);

459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516

return f;
}
public override string ToString()
{
string s= "["+xx.ToString()+","+yy.ToString()+","+zz.ToString()+"]";
return s;
}
}
public class I3tuple
{
int xx; int yy; int zz;
public I3tuple() { xx = 0; yy = 0; zz = 0; }
public I3tuple(int x, int y, int z) { xx = x; yy = y; zz = z; }
public void Set(int x, int y, int z) { xx = x; yy = y; zz = z; }
public void Get(ref int x, ref int y, ref int z) { x = xx; y = yy; z = zz; }
public int X { get { return xx; } set { xx = (int)value; } }
public int Y { get { return yy; } set { yy = (int)value; } }
public int Z { get { return zz; } set { zz = (int)value; } }
public int this[int i]
{
get
{
int res = new int();
if ((i >= 1) & (i <= 3)) res = a4[i];
return res;
}
}
public int[] a
{
get { int[] v = new int[3]; v[0] = xx; v[1] = yy; v[2] = zz; return v; }
set
{
int[] v = value;
if (value.Length == 4) { xx = v[1]; yy = v[2]; zz = v[3]; }
else if (value.Length == 3) { xx = v[0]; yy = v[1]; zz = v[2]; }
}
}
/// <summary>
/// The a4 accessor is designed to allow an array of length 4 with elements
/// 1..3 containing the X,Y,Z values to be used to get and set 3tuples.
/// </summary>
public int[] a4
{
get { int[] v = new int[4]; v[1] = xx; v[2] = yy; v[3] = zz; return v; }
set
{
int[] v = value;
if (value.Length == 4) { xx = v[1]; yy = v[2]; zz = v[3]; }
}
}
public void Zero() { xx = 0; yy = 0; zz = 0; }
public static I3tuple operator-(I3tuple i1)
{
return new I3tuple(-i1.X, -i1.Y, -i1.Z);
}
public static I3tuple operator+(I3tuple i1, I3tuple i2)
{
return new I3tuple(i1.X+i2.X, i1.Y+i2.Y, i1.Z+i2.Z);

517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574

}
public static I3tuple operator-(I3tuple i1, I3tuple i2)
{
return new I3tuple(i1.X-i2.X, i1.Y-i2.Y, i1.Z-i2.Z);
}
public static double operator*(I3tuple i1, I3tuple i2)
{
int i = new int();
i = i1.xx*i2.xx+i1.yy*i2.yy+i1.zz*i2.zz;
return i;
}
public static I3tuple operator^(I3tuple i1, I3tuple i2)
{
return new I3tuple(i1.yy*i2.zz-i2.yy*i1.zz, i1.zz*i2.xx-i2.zz*i1.xx, i1.xx*i2.yy-i2.xx*i1.yy);
}
//public static bool operator==(I3tuple i1, I3tuple i2)
//{
// return ((i1.X==i2.X) & (i1.Y==i2.Y) & (i1.Z==i2.Z));
//}
//public static bool operator!=(I3tuple i1, I3tuple i2)
//{
// return ((i1.X!=i2.X) & (i1.Y!=i2.Y) & (i1.Z!=i2.Z));
//}
public static I3tuple operator>>(I3tuple i1, int k)
{
int c = k % 3; I3tuple i = new I3tuple(i1.X,i1.Y,i1.Z);
if ((c == 1)|(c == -2)) i.Set(i1.Y, i1.Z, i1.X);
else if ((c == 2)|(c == -1)) i.Set(i1.Z, i1.X, i1.Y);
return i;
}
public static I3tuple operator<<(I3tuple i1, int k)
{
int c = k % 3; I3tuple i = new I3tuple(i1.X,i1.Y,i1.Z);
if ((c == -1)|(c == 2)) i.Set(i1.Y, i1.Z, i1.X);
else if ((c == -2)|(c == 1)) i.Set(i1.Z, i1.X, i1.Y);
return i;
}
public override string ToString()
{
string s= "["+xx.ToString()+","+yy.ToString()+","+zz.ToString()+"]";
return s;
}
}
public class US3tuple
{
ushort xx; ushort yy; ushort zz;
public US3tuple() { xx = 0; yy = 0; zz = 0; }
public US3tuple(ushort x, ushort y, ushort z) { xx = x; yy = y; zz = z; }
public void Set(ushort x, ushort y, ushort z) { xx = x; yy = y; zz = z; }
public void Get(ref ushort x, ref ushort y, ref ushort z) { x = xx; y = yy; z = zz; }
public ushort X { get { return xx; } set { xx = value; } }
public ushort Y { get { return yy; } set { yy = value; } }
public ushort Z { get { return zz; } set { zz = value; } }
public ushort this[int i]
{
get
{
ushort res = new ushort();

575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631

if ((i >= 1) & (i <= 3)) res = a4[i];


return res;
}
}
public ushort[] a
{
get { ushort[] v = new ushort[3]; v[0] = xx; v[1] = yy; v[2] = zz; return v; }
set
{
ushort[] v = value;
if (value.Length == 4) { xx = v[1]; yy = v[2]; zz = v[3]; }
else if (value.Length == 3) { xx = v[0]; yy = v[1]; zz = v[2]; }
}
}
/// <summary>
/// The a4 accessor is designed to allow an array of length 4 with elements
/// 1..3 containing the X,Y,Z values to be used to get and set 3tuples.
/// </summary>
public ushort[] a4
{
get { ushort[] v = new ushort[4]; v[1] = xx; v[2] = yy; v[3] = zz; return v; }
set
{
ushort[] v = value;
if (value.Length == 4) { xx = v[1]; yy = v[2]; zz = v[3]; }
}
}
public void Zero() { xx = 0; yy = 0; zz = 0; }
public static US3tuple operator-(US3tuple u1)
{
return new US3tuple((ushort)(-u1.X), (ushort)(-u1.Y), (ushort)(-u1.Z));
}
public static US3tuple operator +(US3tuple u1, US3tuple u2)
{
return new US3tuple((ushort)(u1.X+u2.X), (ushort)(u1.Y+u2.Y), (ushort)(u1.Z+u2.Z));
}
public static US3tuple operator -(US3tuple u1, US3tuple u2)
{
return new US3tuple((ushort)(u1.X-u2.X), (ushort)(u1.Y-u2.Y), (ushort)(u1.Z-u2.Z));
}
public static ushort operator*(US3tuple u1, US3tuple u2)
{
ushort u = new ushort();
u = (ushort)(u1.xx*u2.xx+u1.yy*u2.yy+u1.zz*u2.zz);
return u;
}
/// <summary>
/// This operator override causes ^ to calculate the vector product of two vectors.
/// </summary>
/// <param name="u1"></param>
/// <param name="u2"></param>
/// <returns></returns>
public static US3tuple operator^(US3tuple u1, US3tuple u2)
{
return new US3tuple((ushort)(u1.yy*u2.zz-u2.yy*u1.zz), (ushort)(u1.zz*u2.xx-u2.zz*u1.xx), (ushort)(u1.xx*u2.yy-u2.xx*u1.yy))
;
}
//public static bool operator==(US3tuple u1, US3tuple u2)

632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689

//{
// return ((u1.X==u2.X) & (u1.Y==u2.Y) & (u1.Z==u2.Z));
//}
//public static bool operator!=(US3tuple u1, US3tuple u2)
//{
// return ((u1.X!=u2.X) & (u1.Y!=u2.Y) & (u1.Z!=u2.Z));
//}
public static US3tuple operator>>(US3tuple u1, int k)
{
int c = k % 3; US3tuple u = new US3tuple(u1.X,u1.Y,u1.Z);
if ((c == 1)|(c == -2)) u.Set(u1.Y, u1.Z, u1.X);
else if ((c == 2)|(c == -1)) u.Set(u1.Z, u1.X, u1.Y);
return u;
}
public static US3tuple operator<<(US3tuple u1, int k)
{
int c = k % 3; US3tuple u = new US3tuple(u1.X,u1.Y,u1.Z);
if ((c == -1)|(c == 2)) u.Set(u1.Y, u1.Z, u1.X);
else if ((c == -2)|(c == 1)) u.Set(u1.Z, u1.X, u1.Y);
return u;
}
public override string ToString()
{
string s= "["+xx.ToString()+","+yy.ToString()+","+zz.ToString()+"]";
return s;
}
}
#endregion
#region D3line CLASS
public class D3line
{
D3tuple[] x_ij = null; D3tuple xij = null; double len = 0; bool valid=false;
public D3line()
{
Init();
}
public D3line(D3tuple d1, D3tuple d2)
{
Init(); x_ij[1] = d1; x_ij[2] = d2; calcLen();
}
public D3line(D3tuple[] D)
{
Init();
if (D != null)
{
if (D.Length > 2)
{
if (D[1] != null) x_ij[1] = D[1];
if (D[2] != null) x_ij[2] = D[2];
calcLen();
}
}
}
/// <summary>
/// This constructor allows the use of arrays of length 2 to be
/// used to define the endpoints of a new D3line; and it also
/// provides for setting those points from two consecutive
/// elements located at offset Dofs in an arbitrarily long

690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747

/// D3tuple array


/// </summary>
/// <param name="res">Array of D3tuples of length at least 2</param>
/// <param name="Dofs">Offset of first of two elements in array D</param>
public D3line(D3tuple[] D, int Dofs)
{
Init();
if (D == null) return;
int L = D.Length;
if (L == 2) Dofs = 0;
if ((L >= 2) & (Dofs < L-1))
{
setXij(D[Dofs], D[Dofs+1]); calcLen();
}
}
void Init()
{
x_ij = new D3tuple[3]; x_ij[1] = new D3tuple(); x_ij[2] = new D3tuple();
xij = new D3tuple();
}
void calcLen()
{
xij = x_ij[2] - x_ij[1]; len = ~xij; valid = (len != 0);
}
void setXij(D3tuple d1, D3tuple d2)
{
if (d1 == null) x_ij[1].Zero(); else x_ij[1] = d1;
if (d2 == null) x_ij[2].Zero(); else x_ij[2] = d2;
calcLen();
}
/// Valid <summary>
/// This accessor returns true if the D3line is a valid instance of its class.
/// </summary>
public bool Valid { get { return valid; } }
/// Vec <summary>
/// This accessor returns the two defining points of the D3line
/// in a D3tuple array of length 3, with elements 1 and 2 containing
/// the endpoints Xij[1] and Xij[2].
/// </summary>
public D3tuple Vec { get { return xij; } }
/// <summary>
/// This accessor returns one of the endpoints of the D3line if i=1..2<para>
/// Otherwise, it returns the vector from the first endpoint to the second.</para>
/// </summary>
/// <param name="i">Endpoint number (1..2) or zero.</param>
/// <returns>An endpoint of the line segment if i=1..2, or the displacement vector if i=0</returns>
public D3tuple this[int i]
{
get
{
D3tuple res = new D3tuple();
if ((i >= 1) & (i <= 2)) { res = x_ij[i]; }
else res = xij;
return res;
}
set
{
D3tuple res = value;

748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805

if ((i >= 1) & (i <= 2)) { x_ij[i] = res; calcLen(); }


}
}
/// Xij <summary>
/// This accessor returns an array of three D3tuples, of which
/// elements at offsets 1 and 2 contain the defining points of
/// the D3line, and element 0 is normally not changed after the
/// D3line constructor is called, so it will contain any value
/// you have stored there. However, if you attempt to use an
/// array of length less than 3 to set these values, the D3line
/// will be zeroed and the values will not be stored.
/// </summary>
public D3tuple[] Xij
{
get { return x_ij; }
set
{
D3tuple[] v = value;
if (v == null) Zero();
else
{
if (v.Length < 3) Zero();
else { setXij(v[1], v[2]); }
}
}
}
/// Length
/// <summary>
/// This accessor returns the length of the D3line.
/// </summary>
public double Length { get { return len; } }
/// Zero()
/// <summary>
/// This routine sets both endpoints of the D3line to the zero vector.
/// </summary>
public void Zero() { x_ij[1].Zero(); x_ij[2].Zero(); calcLen(); }
/// IsZero()
/// <summary>
/// This function returns true if the length of the D3line is zero.
/// </summary>
/// <returns></returns>
public bool IsZero() { return (len == 0); }
/// SetAB()
/// <summary>
/// This routine sets the defining points of the D3line.
/// </summary>
/// <param name="D">D3tuple vector defining the first point</param>
/// <param name="D2">D3tuple vector defining the second point</param>
public void SetAB(D3tuple D1, D3tuple D2) { setXij(D1, D2); }
/// SignedAngle() <summary>
/// This function provides a signed angle measured counterclockwise
/// about an axis running from line DL1 to line DL2 (or about the
/// D3plane normal to their plane if they are coplanar). Note that
/// if the angle from DL1 to DL2 would be negative, the line from
/// DL1 to DL2 is opposite in direction to the normal of the D3plane
/// constructed by "new D3plane(DL1,DL2)". This condition is detected
/// by calculating both DL1|DL2, which finds the line-of-shortest/// distance, and DL1&DL2, which constructs a plane with DL1 defining

806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863

/// edge 12 and DL2 defining edge 13 and oriented such that N = 123
/// gives the direction of the plane normal, and then using the sign
/// of the scalar product of the vectors in these two directions to
/// determine the sign of the angle.
/// </summary>
/// <param name="DL1">Axis which angle is to be measured from</param>
/// <param name="DL2">Axis which angle is to be measured to</param>
/// <returns>Signed angle in degrees (-180..+180)</returns>
public double SignedAngle(D3line DL1, D3line DL2)
{
double d = new double();
D3plane DP = DL1|DL2; D3line DL = DL1&DL2;
if ((DP != null) & (DL != null))
{
double s = DP.N * DL.xij;
if (s < 0) s = -1; else s = 1;
d = DP.Angle(1) * 180.0 / Math.PI; d = s*d;
}
return d;
}
#region D3line OPERATORS
/// ~ Length Operator ~A = |A|
/// <summary>
/// The ~ operator produces the length of a D3line: ~A = |A|
/// </summary>
/// <param name="DL"></param>
/// <returns></returns>
public static double operator~(D3line DL)
{
return DL.Length;
}
/// ! Unit Vector Operator !A = A / |A|
/// <summary>
/// The ! operator produces a SignOnly vector from A: !A = A / |A|
/// </summary>
/// <param name="DL"></param>
/// <returns></returns>
public static D3tuple operator!(D3line DL)
{
D3tuple d = new D3tuple(); d = DL.x_ij[2] - DL.x_ij[1];
double L = ~d; if (L != 0) d = d / L;
return d;
}
/// - Pole Reversal Operator C = -A
/// <summary>
/// This produces a D3line C equal to and oriented in
/// the reverse direction to the D3line A. If A=ij
/// then C=ji.
/// </summary>
/// <param name="DL"></param>
/// <returns></returns>
public static D3line operator-(D3line DL)
{
D3line D = new D3line(DL.x_ij[2], DL.x_ij[1]);
return D;
}
/// + Addition Operator C = A+B
/// <summary>

864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921

/// The + operator produces the sum of two D3lines. If A=ij


/// and B=kl then C = j - i + l - k = ij + kl
/// </summary>
/// <param name="DL1"></param>
/// <param name="DL2"></param>
/// <returns>The vector sum of the two displacements formed by DL1 and DL2</returns>
public static D3line operator+(D3line DL1, D3line DL2)
{
D3tuple x0 = new D3tuple();
D3tuple x1 = DL1.x_ij[2] - DL1.x_ij[1] + DL2.x_ij[2] - DL2.x_ij[1];
return new D3line(x0, x1);
}
/// - Subtraction Operator C = A-B
/// <summary>
/// The - operator produces the difference of two D3lines.
/// If A=ij and B=kl then
/// C = j - i - l + k = ij - kl
/// </summary>
/// <param name="DL1"></param>
/// <param name="DL2"></param>
/// <returns>The vector sum of the displacements formed by DL1 and -DL2</returns>
public static D3line operator-(D3line DL1, D3line DL2)
{
D3tuple x0 = new D3tuple();
D3tuple x1 = DL1.x_ij[2] - DL1.x_ij[1] - DL2.x_ij[2] + DL2.x_ij[1];
return new D3line(x0, x1);
}
/// * Scalar (Dot) Product Operator D = A*B
/// <summary><para>
/// The * operator produces the scalar product of two D3lines.
/// If A=ij and B=kl then </para><para>
/// A*B = j*l + i*k - i*l - j*k = (j - i)*(l - k)</para>
/// </summary>
/// <param name="DL1"></param>
/// <param name="DL2"></param>
/// <returns></returns>
public static double operator*(D3line DL1, D3line DL2)
{
return DL1.xij*DL2.xij;
}
/// * Scalar Component Multiplication Operator C = h*A
/// <summary><para>
/// The * scalar multiplication operator produces a D3line
/// with length h times the length of A:</para><para>
/// |C| = |h*A| = |h(a2 - a1)| </para><para>
/// and with its endpoints such that c1 = a1 and</para>
/// c2 = a1 + h(a2 - a1)
/// </summary>
/// <param name="d"></param>
/// <param name="DL"></param>
/// <returns></returns>
public static D3line operator*(double d, D3line DL)
{
D3line D = new D3line();
D.x_ij[1] = DL.x_ij[1]; D.xij = d * DL.xij; D.x_ij[2] = D.x_ij[1] + D.xij;
return D;
}
/// / Scalar Divide Components Operator A' = A / D

922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979

/// <summary><para>
/// The / scalar multiplication operator produces a D3line
/// with length 1/D times the length of A:</para><para>
/// |C| = |(1/D)*A| = |(1/D)(a2 - a1)| </para><para>
/// and with its endpoints such that c1 = a1 and</para>
/// c2 = a1 + (1/D)(a2 - a1)
/// </summary>
/// <param name="DL"></param>
/// <param name="d"></param>
/// <returns></returns>
public static D3line operator/(D3line DL, double d)
{
D3line D = new D3line();
D.x_ij[1] = DL.x_ij[1]; D.xij = DL.xij / d; D.x_ij[2] = D.x_ij[1] + D.xij;
return D;
}
/// ^ Vector Product Operator C = A x B
/// <summary><para>
/// The ^ operator produces the vector product of two D3lines</para><para>
/// as a D3tuple C = A ^ B = (a2 - a1) x (b2 - b1)</para><para>
/// = a2xb2 + a1xb1 - a2xb1 - a1xb2</para><para>
/// = a2xb2 + b2xa1 + a1xb1 + b1xa2 </para><para>
/// =
(a2) (b2) (a1) (b1) </para><para>
/// =
(a1) (b1) (a2) (b2) </para>
/// </summary>
/// <param name="DL1"></param>
/// <param name="DL2"></param>
/// <returns></returns>
public static D3tuple operator^(D3line DL1, D3line DL2)
{
D3tuple D = new D3tuple();
D = DL1.xij ^ DL2.xij;
return D;
}
/// | Plane Constructor Operator for two D3lines <summary>
/// The plane constructor operator returns a D3plane defined by
/// placing the first endpoint of DL2 at the first endpoint of DL1
/// to provide the first point defining the plane, and then using
/// the second endpoints of DL1 and DL2 to, respectively, specify
/// the second and third points defining the D3plane.
/// </summary>
/// <param name="DL1"></param>
/// <param name="DL2"></param>
/// <returns></returns>
public static D3plane operator|(D3line DL1, D3line DL2)
{
D3plane DP = null;
if ((DL1 != null) & (DL2 != null))
{
DP = new D3plane(DL1.Xij[1], DL1.Xij[2], DL1.Xij[1]+DL2.Vec);
}
return DP;
}
///
Altitude Vector Product Operator - OmegaIKJL
/// <summary><para>
/// The &amp; operator produces a D3line using the Rotator
/// function OmegaIKJL() which returns the endpoint on each
/// line a2 - a1 and b2 - b1 of the vector defined by</para>

980
/// <para> (a1) (b1) (a2) (b2) </para>
981
/// </summary>
982
/// <param name="DL1"></param>
983
/// <param name="DL2"></param>
984
/// <returns></returns>
985
public static D3line operator&(D3line DL1, D3line DL2)
986
{
987
//Rotator r = new Rotator(); D3line D = null;
988
//return new D3line(r.OmegaIKJL(DL1.x_ij[1], DL1.x_ij[2], DL2.x_ij[1], DL2.x_ij[2]));
989
return new D3line(Ro.r.OmegaIKJL(DL1.x_ij[1], DL1.x_ij[2], DL2.x_ij[1], DL2.x_ij[2]));
990
}
991
#endregion End D3line OPERATORS
992
}
993
#endregion
994
#region D3plane CLASS
995
public class D3plane
996
{
997
D3tuple[] V_ijk = null; // LENGTH 4
998
D3tuple nn = null; double aa = 0; int ii=0,jj=0,kk=0; bool valid=false;
999
public D3plane()
1000
{
1001
Init();
1002
}
1003
public D3plane(D3tuple d1, D3tuple d2, D3tuple d3)
1004
{
1005
Init();
1006
if (d1 != null) V_ijk[1] = d1;
1007
if (d2 != null) V_ijk[2] = d2;
1008
if (d3 != null) V_ijk[3] = d3;
1009
calcNormal();
1010
}
1011
public D3plane(D3tuple locus, D3tuple anyNormal)
1012
{
1013
Init();
1014
setPoints(locus, anyNormal);
1015
}
1016
public D3plane(D3tuple[] D, int Dofs)
1017
{
1018
Init();
1019
if (D == null) return;
1020
int L = D.Length;
1021
if (L == 3) Dofs = 0;
1022
if ((L >= 3) & (Dofs < L-2))
1023
{
1024
if (D[Dofs] != null) V_ijk[1] = D[Dofs]; else valid = false;
1025
if (D[Dofs+1] != null) V_ijk[2] = D[Dofs+1]; else valid = false;
1026
if (D[Dofs+2] != null) V_ijk[3] = D[Dofs+2]; else valid = false;
1027
}
1028
calcNormal();
1029
}
1030
void Init()
1031
{
1032 //
Rp = new Rotator();
1033
V_ijk = new D3tuple[4]; nn = new D3tuple();
1034
for (int i = 1; (i<=3); i++) { V_ijk[i] = new D3tuple(); }
1035
}
1036
void modd3(ref int i) { i = Math.Abs(i); i = (((i-1)%3)+1); }
1037
void setJK(int i)

1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095

{
ii = i; jj = i+1; kk = i+2; modd3(ref ii); modd3(ref jj); modd3(ref kk);
}
/// <summary>
/// The operation performed by setPoints() is very similar to the Gramm-Schmidt
/// process of finding a normalized orthogonal set of coordinates. The inputs
/// are a point on the expected plane and the plane normal vector, and the output,
/// consisting of the input point and the two points at the endpoints of the axes,
/// is placed in the three D3plane[1..3] vectors.
/// </summary>
/// <param name="ptOn">Point on plane</param>
/// <param name="NormalVec">Plane normal vector</param>
void setPoints(D3tuple ptOn, D3tuple NormalVec)
{
D3tuple u = new D3tuple(),v = new D3tuple();
if (NormalVec == null) return;
bool ok = false; int i = 0;
if (NormalVec.IsZero()) NormalVec = Ro.r.Axes[0];//Rp.Axes[0];
nn = !NormalVec;
u = ptOn^NormalVec; if (u.IsZero()) u = NormalVec;
v = NormalVec^u;
if (v.IsZero())
{
while ((i <= 3) & (!ok))
{
u = !(Ro.r.ProjSin(Ro.r.Axes[i], NormalVec)); //u = !(Rp.ProjSin(Rp.Axes[j], NormalVec));
v = !(NormalVec^u);
if (~(u^v) != 0) ok = true;
}
}
if (ok)
{
V_ijk[1] = ptOn; V_ijk[2] = ptOn + u; V_ijk[3] = ptOn + v; valid = true;
}
nn = Ro.r.CVPS3(V_ijk[1], V_ijk[2], V_ijk[3]); //nn = Rp.CVPS3(V_ijk[1], V_ijk[2], V_ijk[3]);
aa = ~(nn) / 2;
}
void setVijk(D3tuple[] D)
{
D3tuple[] v = D; valid = false;
if (v != null)
{
if (v.Length >= 4)
{
valid = true;
if (v[1] != null) V_ijk[1] = v[1]; else valid = false;
if (v[2] != null) V_ijk[2] = v[2]; else valid = false;
if (v[3] != null) V_ijk[3] = v[3]; else valid = false;
calcNormal();
}
}
}
void calcNormal()
{
nn = Ro.r.CVPS3(V_ijk[1], V_ijk[2], V_ijk[3]); //nn = Rp.CVPS3(V_ijk[1], V_ijk[2], V_ijk[3]);
aa = ~nn / 2;
}
/// <summary>

1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153

/// This accessor gets or sets either the normal vector (if i = 0) or one of
/// the defining points of the D3plane (if i=1..3).
/// </summary>
/// <param name="i">Vector index</param>
/// <returns>The normal or one of the defining points</returns>
public D3tuple this[int i]
{
get
{
D3tuple res = new D3tuple();
if ((i >= 1) & (i <= 3))
{
res = V_ijk[i];
}
return res;
}
set
{
D3tuple d = value;
if (d != null)
{
if ((i >= 1) & (i <= 3)) { V_ijk[i] = d; }
else nn = d;
calcNormal();
}
}
}
public bool Valid { get { return valid; } }
/// A
/// <summary>
/// This accessor returns the area of the triangle defined by the
/// three vertex points V_ijk[1], V_ijk[2], and V_ijk[3].
/// </summary>
public double A { get { if (!valid) aa = 0; return aa; } }
/// Lvijk
/// <summary>
/// This function returns the length of the Cyclic Vector Product Sum
/// formed from the three defining vertex vectors of the D3plane.
/// Since the normal vector stored in the D3plane N field is calculated
/// from the CVPS3 of these points, this value is the length of the
/// normal vector N returned by the D3plane accessors N and Normal.
/// </summary>
public double LVijk { get { if (!valid) nn.Zero(); return ~nn; } }
/// STP
/// <summary>
/// This function returns the Scalar Triple Product [123] of the
/// vectors to the three vertices defining the D3plane triangle,
/// V_ijk[1], V_ijk[2], and V_ijk[3].
/// </summary>
public double STP { get { return V_ijk[1]*(V_ijk[2]^V_ijk[3]); } }
public double CosPhi(int i)
{
double d; D3tuple v1 = new D3tuple(),v2 = new D3tuple();
setJK(i); v1 = V_ijk[jj]-V_ijk[ii]; v2 = V_ijk[kk]-V_ijk[ii]; d = ~(v1)*~(v2);
if (d != 0) d = ((v1)*(v2)) / (d);
return d;
}

1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211

public double Angle(int i)


{
double d = new double(); modd3(ref i); d = Math.Acos(CosPhi(i)); return d;
}
/// UnitNormal
/// <summary>
/// This accessor returns the SignOnly normal to the plane represented
/// by the D3plane. The normal vector stored in the D3plane instance
/// is NOT normalized as a result of a call to this accessor.
/// </summary>
public D3tuple UnitNormal { get { if (!valid) nn.Zero(); return !nn; } }
/// Normal
/// <summary>
/// This accessor returns the D3plane's normal vector, which is NOT
/// normalized to be a SignOnly vector.
/// </summary>
public D3tuple Normal { get { if (!valid) nn.Zero(); return nn; } }
/// N
/// <summary>
/// This accessor returns the D3plane's normal vector, which is NOT
/// normalized to be a SignOnly vector.
/// </summary>
public D3tuple N { get { if (!valid) nn.Zero(); return nn; } }
/// Loci
/// <summary>
/// This accessor gets and sets the three defining points of the plane
/// represented by the D3plane instance. These points are the vertices
/// of a triangle in the general case. Loci and Vijk are equivalent.
/// </summary>
public D3tuple[] Loci { get { return V_ijk; } set { setVijk(value); } }
/// Vijk
/// <summary>
/// This accessor gets and sets the three defining points of the plane
/// represented by the D3plane instance. These points are the vertices
/// of a triangle in the general case. Loci and Vijk are equivalent.
/// </summary>
public D3tuple[] Vijk { get { return V_ijk; } set { setVijk(value); } }
/// SetD3plane()
/// <summary>
/// The routine sets the three defining points of the plane, given one
/// of those points and a plane normal vector. The second and third
/// points are calculated using the cross product of the locus vector
/// and the normal if possible; otherwise their directions are based
/// on the [1,1,1] direction or one or more of the axes.
/// </summary>
/// <param name="locus">A point lying on the plane being defined</param>
/// <param name="anyNormal">The normal vector to the plane</param>
public void SetD3plane(D3tuple locus, D3tuple anyNormal)
{
setPoints(locus, anyNormal);
}
/// Edge()
/// <summary><para>
/// The edges of the D3plane are indexed by the index of the
/// opposite vertex, and are returned by this function in cyclic
/// order; m.e.:</para><para> Edge(1)= 23 </para><para> Edge(2)= 31
/// </para><para>Edge(3)= 12 </para>
/// </summary>

1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269

/// <param name="i">Index of vertex opposite the edge to be returned</param>


/// <returns>A D3line containing the points defining the edge</returns>
public D3line Edge(int i)
{
D3line DL = new D3line(); i++;
i = ((i-1)%3)+1; int j = i + 1; j = ((j-1)%3)+1;
DL.SetAB(V_ijk[i], V_ijk[j]);
return DL;
}
/// Equation()
/// <summary><para>
/// This function returns a value of zero to 3 if the plane of
/// the D3plane is one of the default Rotator axes. The values
/// 1..3 represent the X,Y,Z axis planes and 0 the [1,1,1] plane
/// (although in this case 0 may signify any plane).</para>
/// <para>
/// The coefficients of a scalar plane equation in standard form are
/// returned in the ref parameters to this function so that the form
/// Ax + By + Cz + res = 0 can be constructed from any D3plane if required.
/// </para>
/// </summary>
/// <param name="A">Coefficient of x in the plane equation</param>
/// <param name="B">Coefficient of y in the plane equation</param>
/// <param name="C">Coefficient of z in the plane equation</param>
/// <param name="res">Origin distance factor in the plane equation</param>
/// <returns>The Rotator index of the plane, or zero</returns>
public int Equation(ref double A, ref double B, ref double C, ref double D)
{
int res = 0,i=3; double d=0;
A = nn.X; B = nn.Y; C = nn.Z; D = V_ijk[1] * (V_ijk[2]^V_ijk[3]);
while ((res==0) & (i > 0))
{
d = ~(nn^Ro.r.Axes[i]); //D = ~(nn^Rp.Axes[j]);
if (d == 0) res = i;
i--;
}
return res;
}
/// InterceptForm()
/// <summary><para>
/// This function returns a value of zero to 3 if the plane of
/// the D3plane is one of the default Rotator axes. The values
/// 1..3 represent the X,Y,Z axis planes and 0 the [1,1,1] plane.</para>
/// <para>
/// The axis intercepts are returned as ref parameters; and if
/// any axis intercept is greater than 1/ the value of 1/ is
/// returned for that parameter.</para>
/// </summary>
/// <param name="a">Returns first axis intercept or 1/</param>
/// <param name="b">Returns second axis intercept or 1/</param>
/// <param name="c">Returns third axis intercept or 1/</param>
/// <returns>The Rotator index of the plane, or zero</returns>
public int InterceptForm(ref double a, ref double b, ref double c)
{
int res = 0,i=3; double d=0; double D = V_ijk[1] * (V_ijk[3]^V_ijk[2]);
while ((res==0) & (i > 0))
{
d = ~(nn^Ro.r.Axes[i]); //D = ~(nn^Rp.Axes[j]);

1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327

if (d == 0) res = i;
i--;
}
if (nn.X != 0) a = D / nn.X; else a = 1 / Ro.r.Eta; //Rp.Eta;
if (nn.Y != 0) b = D / nn.Y; else b = 1 / Ro.r.Eta; //Rp.Eta;
if (nn.Z != 0) c = D / nn.Z; else c = 1 / Ro.r.Eta; //Rp.Eta;
return res;
}
#region D3plane OPERATORS
/// ~ Norm (Vector Length) Operator ~A = |A|
/// <summary>
/// This operator performs the same computation on the normal
/// vector of the D3plane as the D3tuple ~ operator does on
/// any D3tuple in general.
/// </summary>
/// <param name="DP">A D3plane</param>
/// <returns>Length of the D3plane normal vector</returns>
public static double operator~(D3plane DP) { return ~DP.nn; }
/// ! Unit Vector Operator A' = A / |A|
/// <summary>
/// This operator performs the same computation on the normal
/// vector of the D3plane as the D3tuple ! operator does on
/// any D3tuple in general.
/// </summary>
/// <param name="DP">A D3plane</param>
/// <returns>The unit normal vector to the D3plane</returns>
public static D3tuple operator!(D3plane DP)
{ D3tuple D = new D3tuple(); D = !DP.nn; return D; }
/// - Pole Reversal Operator A' = -A
/// <summary>
/// This operator performs the same computation on the normal
/// vector of the D3plane as the D3tuple - operator does on
/// any D3tuple in general.
/// </summary>
/// <param name="DP">A D3plane</param>
/// <returns>A vector equal and opposite to the D3plane normal</returns>
public static D3tuple operator-(D3plane DP)
{ D3tuple D = new D3tuple(); D = -DP.nn; return D; }
/// + Addition Operator C = A+B
/// <summary>
/// This operator performs the same computation on the normal
/// vector of the D3plane as the D3tuple + operator does on
/// any two D3tuples in general.
/// </summary>
/// <param name="DP1">First D3plane</param>
/// <param name="DP2">Second D3plane</param>
/// <returns>Sum of the two D3plane normal vectors</returns>
public static D3tuple operator+(D3plane DP1, D3plane DP2)
{
D3tuple D = new D3tuple(); D = DP1.nn+DP2.nn; return D;
}
/// - Subtraction Operator C = A-B
/// <summary>
/// This operator performs the same computation on the normal
/// vector of the D3plane as the D3tuple - operator does on
/// any two D3tuples in general.
/// </summary>
/// <param name="DP1">First D3plane</param>

1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385

/// <param name="DP2">Second D3plane</param>


/// <returns>Sum of the first D3plane's normal vector and the vector equal to and
/// opposite the second D3plane's normal vector.</returns>
public static D3tuple operator-(D3plane DP1, D3plane DP2)
{
D3tuple D = new D3tuple(); D = DP1.nn-DP2.nn; return D;
}
/// * Scalar (Dot) Product Operator D = A*B
/// <summary>
/// This operator performs the same computation on the normal
/// vector of the D3plane as the D3tuple * operator does on
/// any two D3tuples in general.
/// </summary>
/// <param name="DP1">A D3plane</param>
/// <param name="DP2">A D3plane</param>
/// <returns>The scalar product of the two D3plane normal vectors</returns>
public static double operator*(D3plane DP1, D3plane DP2)
{
double d = DP1.nn*DP2.nn; return d;
}
/// * Scalar Component Multiplication Operator A' = hA
/// <summary>
/// This operator performs the same computation on the normal
/// vector of the D3plane as the D3tuple * operator does on
/// any D3tuple in general.
/// </summary>
/// <param name="d">Scalar multiplier</param>
/// <param name="DP">A D3plane</param>
/// <returns>The D3plane normal vector multiplied by the given scalar</returns>
public static D3tuple operator*(double d, D3plane DP)
{
D3tuple D = new D3tuple(); D = d*DP.nn; return D;
}
/// / Length Division Operator A' = A / |B|
/// <summary>
/// This operator performs the same computation on the normal
/// vector of the D3plane as the D3tuple / operator does with
/// any two D3tuples in general.
/// </summary>
/// <param name="DP1">First D3plane</param>
/// <param name="DP2">Second D3plane</param>
/// <returns>The first D3plane normal vector divided by the length
/// of the second D3plane normal vector</returns>
public static D3tuple operator/(D3plane DP1, D3plane DP2)
{
D3tuple D = new D3tuple(); D = DP1.nn/~(DP2.nn); return D;
}
/// / Scalar Divide Components Operator A' = A / h
/// <summary>
/// This operator performs the same computation on the normal
/// vector of the D3plane as the D3tuple / operator does on
/// any D3tuple in general.
/// </summary>
/// <param name="DP">A D3plane</param>
/// <param name="d">A scalar divisor</param>
/// <returns></returns>
public static D3tuple operator/(D3plane DP, double d)
{

1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443

D3tuple D = new D3tuple(); D = DP.nn/d; return D;


}
/// ^ Vector Product Operator C = A x B
/// <summary>
/// This operator performs the numerical computation A x B which is equivalent to
/// 123 x 456
/// given that 123 and 456 are the defining points of two D3planes. The result
/// is not analytically equivalent in a symbolic algebraic sense in that the
/// individual points cannot be extracted from the result, although the value of
/// the result is the same analytically as it is computationally. For this
/// reason, the computation is performed on the normal vector fields of the
/// D3planes rather than on the defining points themselves, because it is more
/// efficient.
/// </summary>
/// <param name="DP1">First D3plane</param>
/// <param name="DP2">Second D3plane</param>
/// <returns>The vector product of the normal vectors of the two D3planes</returns>
public static D3tuple operator^(D3plane DP1, D3plane DP2)
{
return new D3tuple((DP1.nn^DP2.nn).X, (DP1.nn^DP2.nn).Y, (DP1.nn^DP2.nn).Z);
}
/// | Intersection Operator - Line Segment
/// <summary><para>
/// This operator projects the first point of each D3plane onto the line
/// of intersection and returns a D3line FROM the point projected from DP1
/// TO that projected from DP2. The location of these points on the line
/// will vary depending on how the points are enumerated in each plane.</para><para>
/// </para><para>
/// If the planes were each defined by a single point P and a normal N,
/// then P is the first point in each D3plane and the other two are
/// more or less arbitrary, having been determined by the D3plane
/// constructor; and therefore this situation is clearly nothing more
/// than determining the line of intersection of the two planes and
/// projecting the defining locus P of each plane onto that line such that
/// the direction of projection is perpendicular to the line of intersection.</para>
/// </summary>
/// <param name="DP1">First plane</param>
/// <param name="DP2">Second plane</param>
/// <returns>A directed segment on the line of intersection</returns>
public static D3line operator|(D3plane DP1, D3plane DP2)
{
D3tuple u12 = new D3tuple(), nxn = new D3tuple(),
v1 = new D3tuple(),v2 = new D3tuple();
double L = 0;
nxn = (DP1.UnitNormal^DP2.UnitNormal); L = nxn*nxn;
if (L != 0)
{
nxn = nxn/L;
u12 = DP1.V_ijk[1]-DP2.V_ijk[1];
v1 = DP1.V_ijk[1] + (u12*DP2.UnitNormal)*(nxn^DP1.UnitNormal);
v2 = DP2.V_ijk[1] + (u12*DP1.UnitNormal)*(nxn^DP2.UnitNormal);
}
return new D3line(v1, v2);
}
/// Right-shift Indexes Operator
/// <summary>
/// The operator provides the same functionality as the D3tuple
/// Index Right-Shift Operator, but it does not apply the operation

1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501

/// to the vertices of the defining triangle of the D3plane. The


/// shift is performed on the components of the normal vector to
/// the plane, and the result returned without affecting the state
/// of the D3plane normal or its defining points.
/// </summary>
/// <param name="DP">First operand of DPk</param>
/// <param name="k">Second operand of DPk (k = shift count)</param>
/// <returns></returns>
public static D3tuple operator>>(D3plane DP, int k)
{
int c = k % 3; D3tuple d = new D3tuple(DP.nn.X, DP.nn.Y, DP.nn.Z);
if ((c == 1)|(c == -2)) d.Set(DP.nn.Y, DP.nn.Z, DP.nn.X);
else if ((c == 2)|(c == -1)) d.Set(DP.nn.Z, DP.nn.X, DP.nn.Y);
return d;
}
/// Left-shift Indexes Operator
/// <summary>
/// The operator provides the same functionality as the D3tuple
/// Index Left-Shift Operator, but it does not apply the operation
/// to the vertices of the defining triangle of the D3plane. The
/// shift is performed on the components of the normal vector to
/// the plane, and the result returned without affecting the state
/// of the D3plane normal or its defining points.
/// </summary>
/// <param name="DP">First operand of DPk</param>
/// <param name="k">Second operand of DPk (k = shift count)</param>
/// <returns></returns>
public static D3tuple operator<<(D3plane DP, int k)
{
int c = k % 3; D3tuple d = new D3tuple(DP.nn.X, DP.nn.Y, DP.nn.Z);
if ((c == -1)|(c == 2)) d.Set(DP.nn.Y, DP.nn.Z, DP.nn.X);
else if ((c == -2)|(c == 1)) d.Set(DP.nn.Z, DP.nn.X, DP.nn.Y);
return d;
}
#endregion End D3plane OPERATORS
}
#endregion
#region D3tetra CLASS
public class D3tetra
{
D3tuple[] pts = null; int ii=0,jj=0,kk=0,ll=0; bool valid=false;
IndexPair[,] rhoIJ =
{{new IndexPair(0,0),new IndexPair(0,0),new IndexPair(0,0),new IndexPair(0,0),new
{new IndexPair(0,0),new IndexPair(1,1),new IndexPair(3,4),new IndexPair(4,2),new
{new IndexPair(0,0),new IndexPair(4,3),new IndexPair(2,2),new IndexPair(1,4),new
{new IndexPair(0,0),new IndexPair(4,2),new IndexPair(4,1),new IndexPair(3,3),new
{new IndexPair(0,0),new IndexPair(2,3),new IndexPair(3,1),new IndexPair(1,2),new
public D3tetra() { Init(); }
public D3tetra(D3tuple d1, D3tuple d2, D3tuple d3, D3tuple d4)
{
Init();
if (d1 != null) pts[1] = d1;
if (d2 != null) pts[2] = d2;
if (d3 != null) pts[3] = d3;
if (d4 != null) pts[4] = d4;
reCalc();
}
public D3tetra(D3tuple[] D, int Dofs)

IndexPair(0,0)},
IndexPair(2,3)},
IndexPair(3,1)},
IndexPair(1,2)},
IndexPair(4,4)}};

1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559

{
Init();
if (D == null) return;
int L = D.Length;
if (L == 4) Dofs = 0;
if ((L >= 4) & (Dofs < L-3))
{
if (D[Dofs] != null)
pts[1]
if (D[Dofs+1] != null) pts[2]
if (D[Dofs+2] != null) pts[3]
if (D[Dofs+3] != null) pts[4]
}
reCalc();

=
=
=
=

D[Dofs]; else valid =


D[Dofs+1]; else valid
D[Dofs+2]; else valid
D[Dofs+3]; else valid

false;
= false;
= false;
= false;

}
void Init()
{
pts = new D3tuple[6]; for (int i=1; (i<=5); i++) { pts[i] = new D3tuple(); }
valid=true;
}
void reCalc() { }
bool indexOK(int i) { return ((i >= 1) & (i <= 4)); }
D3tuple ensure(ref D3tuple d) { if (d == null) d = new D3tuple(); return d; }
void setKL(int i, int j)
{
ii=i; jj=j; ii = ((ii-1)%4)+1; jj = ((jj-1)%4)+1; kk = rhoIJ[ii,jj].K; ll = rhoIJ[ii,jj].L;
}
public void IJtoKL(int i, int j, ref int k, ref int l)
{
i = ((i-1)%4)+1; j = ((j-1)%4)+1; k = rhoIJ[i,j].K; l = rhoIJ[i,j].L;
}
void setJKL(int i)
{
ii = i; jj = ii + 1; ii = ((ii-1)%4)+1; jj = ((jj-1)%4)+1; kk = rhoIJ[ii,jj].K; ll = rhoIJ[ii,jj].L;
}
public void ItoJKL(int i, ref int j, ref int k,ref int l )
{
j = i + 1; i = ((i-1)%4)+1; j = ((j-1)%4)+1; k = rhoIJ[i,j].K; l = rhoIJ[i,j].L;
}
/// RhoIJ() <summary>
/// This function returns an IndexPair kl corresponding to
/// the indexes ij supplied as parameters to this function.<para>
/// </para>
/// <para>
/// Note that if k=j the function returns II rather than null.</para>
/// </summary>
/// <param name="i"></param>
/// <param name="j"></param>
/// <returns></returns>
public IndexPair RhoIJ(int i, int j)
{
IndexPair IP = new IndexPair();
if ((indexOK(i)) & (indexOK(j))) { IP = rhoIJ[i, j]; }
return IP;
}
/// Pt() <summary>
/// This function returns a D3tetra vertex point, given
/// its index (1..4).
/// </summary>

1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617

/// <param name="i">Index of vertex to return</param>


/// <returns></returns>
public D3tuple Pt(int i)
{
D3tuple D = new D3tuple(); if (indexOK(i)) D = pts[i];
return D;
}
/// SetPt() <summary>
/// This routine sets an individual point of the D3tetra,
/// given an index in the range 1..4
/// </summary>
/// <param name="res">D3tuple containing value to be stored</param>
/// <param name="i">Point index in D3tetra where value is to be stored</param>
public void SetPt(D3tuple D, int i)
{
if (D == null) return; if (indexOK(i)) { pts[i] = D; reCalc(); }
}
/// PtsIJ() <summary>
/// This function returns a D3line from Pts[i] to Pts[j], whereas
/// the Edge(i,j) method returns the edge from Pts[k] to Pts[l]
/// </summary>
/// <param name="i">Index of point to store as first point in the D3line</param>
/// <param name="j">Index of point to store as second point in the D3line</param>
/// <returns></returns>
public D3line PtsIJ(int i, int j)
{
D3line DL = new D3line();
if ((indexOK(i)) & (indexOK(j)))
{
DL.SetAB(pts[i], pts[j]);
}
return DL;
}
/// SetPtsIJ() <summary>
/// This routine sets the D3tetra points in pairs by the
/// actual array indexes of the points (1..4), given a D3line
/// provided as the first parameter.
/// </summary>
/// <param name="DL">A D3line instance containing two point values</param>
/// <param name="i">Destination index of first point of the D3line</param>
/// <param name="j">Destination index of second point of the D3line</param>
public void SetPtsIJ(D3line DL, int i, int j)
{
if ((DL != null) & (indexOK(i)) & (indexOK(j)))
{
pts[i] = DL.Xij[1]; pts[j] = DL.Xij[2]; reCalc();
}
}
/// <summary>
/// This accessor returns one of the four defining points of the D3tetra if i=1..4<para>
/// Otherwise, it returns the vector from edge 12 to edge 34</para>
/// </summary>
/// <param name="i">Vertex index (1..4), or zero</param>
/// <returns>A vertex (if i=1..4) or the diagonal from edge 12 to edge 34</returns>
public D3tuple this[int i]
{
get
{

1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675

D3tuple res = new D3tuple();


if (indexOK(i)) { res = pts[i]; }
else res = Ro.r.CVPS4(pts[1], pts[3], pts[2], pts[4]);
return res;
}
set
{
D3tuple d = value;
if (d != null)
{
if (indexOK(i))
{
pts[i] = d;
}
}
}
}
/// Pts <summary>
/// This accessor returns an array of length 6 containing the four
/// tetrahedron vertices in elements 1..4 and a possible fifth point
/// in element 5. The array returned can be used with Rotator class
/// methods for sphere and D5 calculations.
///
/// Used to set the vertices, this accessor accepts any array of
/// D3tuples, but only stores elements 1..4 (or elements 0..3 if the
/// array length is 4) in Pts[1..4].
/// </summary>
public D3tuple[] Pts
{
get { return pts; }
set
{
D3tuple[] d = value; int L = d.Length; int k0=0;
if (L > 4) k0 = 1;
if (k0+3 <= L)
{
int i = 1;
while (i <= 4)
{
pts[i] = d[k0];
i++; k0++;
}
reCalc();
}
}
}
/// SetPts() <summary>
/// This routine sets all four points from four D3tuples
/// provided as parameters.
/// </summary>
/// <param name="d1">Value to store in Pts[1]</param>
/// <param name="d2">Value to store in Pts[2]</param>
/// <param name="d3">Value to store in Pts[3]</param>
/// <param name="d4">Value to store in Pts[4]</param>
public void SetPts(D3tuple d1, D3tuple d2, D3tuple d3, D3tuple d4)
{
if (valid)
{

1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733

pts[1] = d1; pts[2] = d2; pts[3] = d3; pts[4] = d4; reCalc();


}
}
/// Datum <summary>
/// This accessor gets and sets the fifth point stored in the
/// D3tetra field Pts[5], which is a datum point used in those
/// D3tetra methods which calculate face distances, etc. Setting
/// this point to a valid value also allows the Pts[] array of
/// D3tuples returned by the Pts accessor to be used in Rotator
/// methods which require an array of at least five entries as input.
/// </summary>
public D3tuple Datum
{
get { return pts[5]; }
set { D3tuple D = value; if (D != null) { pts[5] = D; } }
}
/// GetAxisSet() <summary>
/// This function returns an AxisSet with origin Pts[i] and
/// axis points Pts[j], Pts[k], and Pts[l], where jkl = RhoJKL(i).
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public AxisSet GetAxisSet(int i)
{
setJKL(i);
AxisSet AS = new AxisSet(pts[ii], pts[jj], pts[kk], pts[ll]);
return AS;
}
/// Edge() <summary>
/// Edge(i,j) returns the edge from point j to point k
/// </summary>
/// <param name="i">First index (1..4)</param>
/// <param name="j">Second index (1..4)</param>
/// <returns></returns>
public D3line Edge(int i, int j)
{
D3line E = new D3line();
if ((indexOK(i)) & (indexOK(j)))
{
if (i != j)
{
E.SetAB(pts[i], pts[j]);
}
}
return E;
}
/// OppEdge() <summary>
/// OppEdge(i,j) returns the edge kl opposite vertex j in plane P(i) = jkl
/// </summary>
/// <param name="i">Plane index (1..4)</param>
/// <param name="j">Vertex index (1..4)</param>
/// <returns></returns>
public D3line OppEdge(int i, int j)
{
D3line E = new D3line();
if ((indexOK(i)) & (indexOK(j)))
{
if (i != j)

1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791

{
int J=j,K=j,L=j;
ItoJKL(i, ref J, ref K, ref L);
Rho.Rho3RotateTo(j, ref J, ref K, ref L);
E.SetAB(pts[K], pts[L]);
}
}
return E;
}
/// Face() <summary>
/// Face() returns the plane defined by face i of the tetrahedron.
/// Note that when the points are stored in dextrochiral order in
/// the D3tetra Pts array, the D3plane returned by this function
/// will contain an outward normal of the tetrahedron.
/// </summary>
/// <param name="i">Face index (1..4)</param>
/// <returns>The plane defined by face i of the tetrahedron</returns>
public D3plane Face(int i)
{
D3plane D = new D3plane(); D3tuple[] D3 = new D3tuple[4];
int c = 1;
setJKL(i);
while (c <= 3) { D3[c] = new D3tuple(); c++; }
D3[1] = pts[jj]; D3[2] = pts[kk]; D3[3] = pts[ll];
D.Vijk = D3;
return D;
}
/// FaceNormal() <summary>
/// This function returns the outward face normal of the
/// tetrahedron face with index i if the points stored in
/// the D3tetra are in dextrochiral order; otherwise, the
/// normal points into the interior of the tetrahedron.
/// </summary>
/// <param name="i">Face index (1..4)</param>
/// <returns>Outward face normal to plane i</returns>
public D3tuple FaceNormal(int i)
{
D3tuple D = new D3tuple();
setJKL(i); D = Ro.r.CVPS3(pts[jj], pts[kk], pts[ll]); //Tp.CVPS3(pts[JJ], pts[KK], pts[LL]);
return D;
}
#region REFERENCE COMMENTS
// Altitude vector definitions:
// http://cqvis.zxq.net/C4/VQC117.html
#endregion
/// FaceAltitude() <summary>
/// Each face of the tetrahedron being bounded by three edges,
/// the FaceAltitude function returns a vector lying in face i
/// directed toward vertex j from edge kl and which has the
/// length of the altitude of the triangle measured from the
/// base kl.
/// </summary>
/// <param name="i">Face index</param>
/// <param name="j">Altitude vector index</param>
/// <returns></returns>
public D3tuple FaceAltitude(int i, int j)
{
D3tuple D = new D3tuple(); D3tuple kl = new D3tuple();

1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849

if ((indexOK(i)) & (indexOK(j)))


{
double d = 0; setKL(i, j); kl = pts[ll] - pts[kk]; d = kl*kl;
if (d != 0)
{
D = (kl ^ Ro.r.CVPS3(pts[jj],pts[kk],pts[ll])) / d; //D = (kl ^ Tp.CVPS3(pts[JJ],pts[KK],pts[LL])) / D;
}
}
return D;
}
/// Diagonal() <summary>
/// This function returns a diagonal vector of the tetrahedron
/// which begins on edge ij and ends on edge kl. The
/// DiagonalSegment() function returns the actual endpoints of
/// this vector in a D3line, whereas this function returns only
/// the vector, whose magnitude equals the distance from one
/// edge of the tetrahedron to the opposite edge.
/// </summary>
/// <param name="i">First vertex on edge</param>
/// <param name="j">Second vertex on edge</param>
/// <returns>Edge connecting the remaining two vertexes</returns>
public D3tuple Diagonal(int i, int j)
{
D3tuple D = new D3tuple(); D3tuple ikjl = new D3tuple();
if ((indexOK(i)) & (indexOK(j)))
{
setKL(i, j); double d = 0,d5=0;
d5 = Ro.r.Dk(pts[ii], pts[jj], pts[kk], pts[ll]); //D5 = Tp.Dk(pts[II], pts[JJ], pts[KK], pts[LL]);
ikjl = (pts[jj]-pts[ii])^(pts[ll]-pts[kk]);
d = ikjl*ikjl;
if ((d != 0) & (d5 != 0))
{
D = -d5 * (ikjl / d);
}
}
return D;
}
/// DiagonalSegment() <summary>
/// This function returns a D3line containing the endpoints
/// of a diagonal running from one edge of the tetrahedron
/// to the opposite edge.
/// </summary>
/// <param name="i">First vertex on edge</param>
/// <param name="j">Second vertex on edge</param>
/// <returns>The diagonal connecting edge ij with the opposite edge</returns>
public D3line DiagonalSegment(int i, int j)
{
D3line D = new D3line();
if ((indexOK(i)) & (indexOK(j)))
{
setKL(i, j); D3line DL1 = new D3line(pts[ii],pts[jj]), Dl2 = new D3line(pts[kk],pts[ll]);
D = DL1 & Dl2; // This uses the D3line OmegaIKJL operator &
}
return D;
}
/// Parax() <summary>
/// This function returns the same value as that in the D3penta class,
/// but the calculations here use the equation written in terms of the

1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907

/// vertex points and a displacement point rather than the normal vectors
/// as in the D3penta Parax() function (q.v.).
/// <see>TranslateToPlane()</see>
/// </summary>
/// <param name="d1">First vertex</param>
/// <param name="d2">Second vertex</param>
/// <param name="d1">Third vertex</param>
/// <param name="d4">Fourth vertex</param>
/// <param name="d5">A point on a translation vector originating at the fourth vertex</param>
/// <returns>Zero if the four vertexes lie on a right circular cylinder with its axis parallel to
/// the translation direction</returns>
public double Parax(D3tuple d1, D3tuple d2, D3tuple d3, D3tuple d4, D3tuple d5)
{
double d = new double(); ensure(ref d1);ensure(ref d2);ensure(ref d3);ensure(ref d4);ensure(ref d5);
d = ((D3SQP(d5,d1,d2,d4)
) * (Ro.r.CVPS4(d4,d3,d5,d1)*Ro.r.CVPS4(d4,d3,d5,d2)))
- (D3SQP(d5,d1,d3,d2)+D3SQP(d4,d1,d2,d3)) * (Ro.r.CVPS3(d5,d4,d2)
*Ro.r.CVPS3(d5,d1,d4));
return d;
}
/// PointToPlane() <summary>
/// This function returns the locus of a point res after translation
/// along the normal to a D3plane DP onto that plane.
/// </summary>
/// <param name="res">Point to be 'shadowed' directly onto a plane</param>
/// <param name="DP">The D3plane onto which res is to be projected</param>
/// <returns></returns>
public D3tuple PointToPlane(D3tuple D, D3plane DP)
{
D3tuple res = new D3tuple();
if ((D != null) & (DP != null))
{
res = D + Ro.r.ProjCos(DP.Vijk[1]-D, DP.UnitNormal);
}
return res;
}
/// TranslateToPlane() <summary><para>
/// This function returns a scalar under certain conditions which indicates
/// whether the points involved in the computation lie on a cylinder. If the
/// length of the return array provided as the ref parameter D is at least 5,
/// then this scalar will be computed and returned.</para><para>
/// </para><para>
/// The calculation of this scalar is rather complex, involving four CVPs and
/// three SQPs:</para><para>
/// P = ( mijl )(
lkmi
*
lkmj
) - ( mikj + lijk )(
mlj
*
mil
)</para><para>
/// </para><para>
/// The points provided by the array D[] are projected onto the plane defined
/// by p[] during this routine; the number of values returned depending on the
/// length of the array D[]. The array p[] is interpreted differently by this
/// routine dpending on its length, as follows:</para><para>
/// L = 2</para><para>
/// p[1] is interpreted as a displacement vector, and is added to the fourth
/// point to provide the fifth required point.</para><para>
/// L = 3</para><para>
/// p[2]-p[1] is interpreted as a displacement, which is added to the fourth
/// point to provide the fifth required point.</para><para>
/// L = 4</para><para>
/// p[1],p[2], and p[3] are used in forming a CVPS3 which is then used as a
/// displacement vector, and added to point 4 to provide the fifth required point.</para><para>
/// </para><para>

1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964

/// The calculation of the Paraxial Equation value P requires four points ijkl
/// in conjunction with a fifth point m to form a translation bundle which is
/// translated to a plane through the origin along the direction lm. The fifth
/// point m is thus derived from the given displacement provided by p[] by
/// adding it to the vector l before the calculation of P is carried out. The
/// translation implied by this calculation is not actually carried out on the
/// vectors supplied by D[], although if the length of D[] is less than 5 the
/// points supplied in D[] will be translated to the origin.</para>
/// </summary>
/// <param name="res">Array to hold five result vectors; this may supply the points to be translated</param>
/// <param name="p">Array defining plane and/or normal vector for translation</param>
/// <returns>The scalar value of the paraxial equation; zero if the points are on a cylinder
/// with axis in the translation direction, or if they are invalid</returns>
public double TranslateToPlane(ref D3tuple[] D, D3tuple[] p)
{
double res = 0;
if (D != null)
{
int L = D.Length,pL = 0; D3tuple trD = new D3tuple();
D3tuple[] tmpD = new D3tuple[6];
for (int i = 1; (i <= 5); i++)
{ tmpD[i] = new D3tuple(); if (i < L) tmpD[i] = D[i]; else tmpD[i] = pts[i]; }
trD = tmpD[5] - tmpD[4];
if (p != null)
{
pL = p.Length;
if (pL == 2)
{
tmpD[5] = p[1] + tmpD[4];
}
else if (pL == 3) { tmpD[5] = (p[2]-p[1]) + tmpD[4]; }
else if (pL >= 4) { tmpD[5] = Ro.r.CVPS3(p[1], p[2], p[3]) + tmpD[4]; } //{ tmpD[5] = Tp.CVPS3(p[1], p[2], p[3]) + tmpD
[4]; }
}
D3plane tmpP = new D3plane(tmpD[4], tmpD[5]-tmpD[4]);
if (L >= 2) D[1] = PointToPlane(tmpD[1], tmpP);
if (L >= 3) D[2] = PointToPlane(tmpD[2], tmpP);
if (L >= 4) D[3] = PointToPlane(tmpD[3], tmpP);
if (L >= 5)
{ D[4] = PointToPlane(tmpD[4], tmpP);
// P = ( mijl )(
lkmi
*
lkmj
) - ( mikj + lijk )(
mlj
*
mil
)
res = ((D3SQP(tmpD[5], tmpD[1], tmpD[2], tmpD[4]))
* (Ro.r.CVPS4(tmpD[4], tmpD[3], tmpD[5], tmpD[1]) * Ro.r.CVPS4(tmpD[4], tmpD[3], tmpD[5], tmpD[2])))
(D3SQP(tmpD[5], tmpD[1], tmpD[3], tmpD[2]) + D3SQP(tmpD[4], tmpD[1], tmpD[2], tmpD[3])
* (Ro.r.CVPS3(tmpD[5], tmpD[4], tmpD[2]) * Ro.r.CVPS3(tmpD[5], tmpD[1], tmpD[4])));
}
}
return res;
}
/// D3SQP() <summary><para>
/// This function returns the Scalar Quadruple Product,
ijkl , defined as: </para><para>
///
ijkl
= j *
jkl
- [jkl]</para><para>
/// This value is the signed distance from the point j to the plane jkl
/// as measured along the direction of the plane normal,
jkl . The value
/// is calculated by subtracting the plane's origin distance from the
/// component of vector j in the direction of the normal.</para>
/// </summary>
/// <returns>The Scalar Quadruple Product of four vectors</returns>

1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022

public double D3SQP(D3tuple d1, D3tuple d2, D3tuple d3, D3tuple d4)
{
return (d1*Ro.r.CVPS3(d2, d4, d3)) - (d2*(d4^d3)); //(D*Tp.CVPS3(D2, D4, D)) - (D2*(D4^D));
}
/// LineToSphere() <summary><para>
/// You must check the length of the array returned by this function before attempting to use it.</para>
/// <para>
/// LineToSphere(DL,DT) calculates the intersection points, if any exist, of the line given by
/// the segment endpoints in DL.Xij[1] and DL.Xij[2] with the circumsphere of the tetrahedron
/// whose vertices are contained in DT.</para>
/// <para>
/// RETURN VALUES:</para><para>
/// :::: Roots are not real:</para><para>
/// Length of returned array is 1; element 0 contains the coefficients of the quadratic,
/// which can be analyzed if need be.</para><para>
/// :::: Roots real and equal:</para><para>
/// Length of returned D3tuple array is 2. Element 1 contains the intersection point.</para><para>
/// :::: Roots real and unequal:</para><para>
/// Length of returned D3tuple array is 3. Elements 1 and 2 contain the intersection points.</para><para>
/// ARRAY ELEMENT 0:</para><para>
/// This will contain the quadratic coefficients A,B,C.</para><para>
/// If the D3line point Xij[1] coincides with the sphere center, the linear term
/// vanishes, and thus the coefficient B will be zero or vanishingly small.
/// If it lies on the sphere surface, the constant term vanishes, and thus the
/// coefficient C will be zero or vanishingly small.</para>
/// <see>
/// http://cqvis.zxq.net/C3/VQC100.html
/// </see><para>
/// DEFINITION OF THE QUADRATIC:</para><para>
/// Q: x1 or some point q along vector u</para><para>
/// U: x1 x2 or some vector u</para><para>
/// : Quadratic solution; gives point(s) Q + U on the sphere, if any exist.</para><para>
/// D5, F5, G5: These are calculated from the tetrahedron vertices by Rotator.GetDvalues()</para><para>
/// QUADRATIC COEFFICIENTS:</para><para>
/// A = D5*(U*U)</para><para>
/// B = U * ( F5 + 2*D5*Q )</para><para>
/// C = D5*(Q*Q) + (Q*F5) - G5</para>
/// </summary>
/// <param name="DL">A D3line specifying the location and direction of the line</param>
/// <param name="DT">A D3tetra containing the four points which define the spherical surface</param>
/// <returns>Coefficients of, or one or two points given by solutions to, the equation
/// for the intersection of a line with a sphere</returns>
public D3tuple[] LineToSphere(D3line DL, D3tetra DT)
{
D3tuple[] res = new D3tuple[3];
if ((DL != null) & (DT != null))
{
double A=0,B=0,C=0,D5=0,G5=0,d=0;
D3tuple Q = new D3tuple(DL.Xij[1][1], DL.Xij[1][2], DL.Xij[1][3]);
D3tuple U = new D3tuple(DL.Vec[1], DL.Vec[2], DL.Vec[3]);
Ro.r.GetDvalues(DT.pts, 1);
D5 = Ro.r.D5[5]; G5 = Ro.r.G5[5];
D3tuple F5 = new D3tuple(Ro.r.F5[5][1], Ro.r.F5[5][2], Ro.r.F5[5][3]);
A = D5 * (Q*Q); B = U * (F5 + 2*D5*Q); C = D5 * (Q*Q) + (Q*F5) - G5;
d = B*B - 4*A*C;
if (d >= 0)
{
d = Math.Sqrt(d); double d1 = (-B + d) / (2.0*A),d2 = (-B - d) / (2.0*A);

2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074

res[1] = new D3tuple(); res[1] = Q + d1*U;


if (d1 == d2) Array.Resize(ref res, 2);
else
{
res[2] = new D3tuple(); res[2] = Q + d2*U;
}
}
else Array.Resize(ref res, 1);
res[0] = new D3tuple(A, B, C);
}
return res;
}
}
#endregion
#region D3penta CLASS
public class D3penta
{
public D3tuple[] DQ = null; int ii=0,jj=0,kk=0,ll=0,mm=0; bool valid=false;
IndexTriple[,] rhoMI =
{{new IndexTriple(0,0,0),new IndexTriple(0,0,0),new IndexTriple(0,0,0),new IndexTriple(0,0,0),new
IndexTriple(0,0,0)},
{new IndexTriple(0,0,0),new IndexTriple(1,1,1),new IndexTriple(3,4,5),new IndexTriple(2,5,4),new
IndexTriple(2,4,3)},
{new IndexTriple(0,0,0),new IndexTriple(3,5,4),new IndexTriple(2,2,2),new IndexTriple(1,4,5),new
IndexTriple(1,3,4)},
{new IndexTriple(0,0,0),new IndexTriple(2,4,5),new IndexTriple(1,5,4),new IndexTriple(3,3,3),new
IndexTriple(1,4,2)},
{new IndexTriple(0,0,0),new IndexTriple(2,5,3),new IndexTriple(1,3,5),new IndexTriple(1,5,2),new
IndexTriple(1,2,3)},
{new IndexTriple(0,0,0),new IndexTriple(2,3,4),new IndexTriple(1,4,3),new IndexTriple(1,2,4),new
IndexTriple(5,5,5)}};
public D3penta()
{
Init();
}
public D3penta(D3tuple d1, D3tuple d2, D3tuple d3, D3tuple d4, D3tuple d5)
{
Init();
if (d1 != null) DQ[1] = d1;
if (d2 != null) DQ[2] = d2;
if (d3 != null) DQ[3] = d3;
if (d4 != null) DQ[4] = d4;
if (d5 != null) DQ[5] = d5;
reCalc();
}
public D3penta(D3tuple[] D, int Dofs)
{
Init();
if (D == null) return;
int L = D.Length;
if (L == 5) Dofs = 0;
if ((L >= 5) & (Dofs < L-4))
{
if (D[Dofs] != null)
DQ[1] = D[Dofs]; else valid = false;
if (D[Dofs+1] != null) DQ[2] = D[Dofs+1]; else valid = false;
if (D[Dofs+2] != null) DQ[3] = D[Dofs+2]; else valid = false;
if (D[Dofs+3] != null) DQ[4] = D[Dofs+3]; else valid = false;
if (D[Dofs+4] != null) DQ[5] = D[Dofs+4]; else valid = false;

IndexTriple(0,0,0),new
IndexTriple(2,3,5),new
IndexTriple(1,5,3),new
IndexTriple(1,2,5),new
IndexTriple(4,4,4),new
IndexTriple(1,3,2),new

2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132

reCalc();
}
}
void Init()
{
DQ = new D3tuple[6]; int i = 1;
while (i < 6) { DQ[i] = new D3tuple(); i++; }
valid=true;
}
void reCalc() { }
D3tuple ensure(ref D3tuple d) { if (d == null) d = new D3tuple(); return d; }
bool indexOK(int i) { return ((i >= 1) & (i <= 5)); }
public void modd5(ref int i) { if (i < 0) i = -i; i = (((i-1)%5)+1); }
public D3tuple this[int i]
{
get
{
D3tuple res = new D3tuple();
if (indexOK(i)) { res = DQ[i]; }
return res;
}
set
{
D3tuple d = value;
if (d != null)
{
if (indexOK(i))
{
DQ[i] = d;
}
}
}
}
/// Rho3RotateTo() <summary><para>
/// This routine rotates the index permutation uvw so that the
/// first element has the value U, provided that</para>
/// U
uvw
/// </summary>
/// <param name="U">One of the parameters u,v,w</param>
/// <param name="u">On input, the index u. Returns U</param>
/// <param name="v">On input, the index v. Returns the parameter cyclically following U</param>
/// <param name="w">On input, the index w. Returns the parameter cyclically preceding U</param>
public void Rho3RotateTo(int U, ref int u, ref int v, ref int w)
{
int t=u,i=1,u1=u,v1=v,w1=w;
while ((i <= 3) & (u1 != U))
{
t = u1; u1 = v1; v1 = w1; w1 = t;
i++;
}
if (i > 1)
{
if (u1 == U) { u = u1; v = v1; w = w1; }
}
}
void setKL(int m, int i, int j)
{
setJKL(m,i);

2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190

//
JJ = rhoMI[II,JJ].k; KK = rhoMI[II,JJ].K; LL = rhoMI[II,JJ].L;
Rho3RotateTo(j,ref jj,ref kk,ref ll);
}
void setJKL(int m, int i)
{
mm = m; ii = i; mm = ((mm-1)%5)+1; ii = ((ii-1)%5)+1;
jj = rhoMI[mm,ii].J; kk = rhoMI[mm,ii].K; ll = rhoMI[mm,ii].L;
}
void setIJKL(int m)
{
mm = m; ii = m+1; mm = ((mm-1)%5)+1; ii = ((ii-1)%5)+1;
jj = rhoMI[mm,ii].J; kk = rhoMI[mm,ii].K; ll = rhoMI[mm,ii].L;
}
public void SetPts(D3tuple d1, D3tuple d2, D3tuple d3, D3tuple d4, D3tuple d5)
{
ensure(ref d1); ensure(ref d2); ensure(ref d3); ensure(ref d4); ensure(ref d5);
DQ[1] = d1; DQ[2] = d2; DQ[3] = d3; DQ[4] = d4; DQ[5] = d5;
}
/// RhoMI() <summary>
/// This function returns an IndexTriple jkl corresponding to
/// the indexes mi supplied as parameters to this function.<para>
/// </para>
/// <para>
/// Note that if m=i the function returns iii rather than null.</para>
/// </summary>
/// <param name="m"></param>
/// <param name="i"></param>
/// <returns></returns>
public IndexTriple RhoMI(int m, int i)
{
IndexTriple IT = new IndexTriple();
if ((indexOK(m)) & (indexOK(i))) { IT = rhoMI[m, i]; }
return IT;
}
/// RhoMIJ() <summary>
/// This function returns an IndexPair kl corresponding to
/// the indexes mij supplied as parameters to this function.<para>
/// </para>
/// <para>
/// Note that if m=i the function returns JJ rather than null.</para>
/// </summary>
/// <param name="m">First index</param>
/// <param name="i">Second index</param>
/// <param name="j">Third index</param>
/// <returns>The remaining two indexes k' and l', in an order such that the
/// parity of mijk'l' is even</returns>
public IndexPair RhoMIJ(int m, int i, int j)
{
int JJ,KK,LL;
m = ((m-1)%5)+1; i = ((i-1)%5)+1; j = ((j-1)%5)+1;
JJ = rhoMI[mm,ii].J; KK = rhoMI[mm,ii].K; LL = rhoMI[mm,ii].L;
Rho3RotateTo(j, ref JJ, ref KK, ref LL);
IndexPair IP = new IndexPair(KK, LL);
return IP;
}
/// GetTetra() <summary>
/// This routine returns the data for a D3tetra in its ref parameter T.
/// T must be a valid D3tetra. The ordering of the four points in the

2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248

/// D3tetra follows the index order of SetIJKL().


/// </summary>
/// <param name="T">A valid D3tetra</param>
/// <param name="m">Sphere index (1..5)</param>
public void GetTetra(ref D3tetra T, int m)
{
if (T != null)
{
int L = T.Pts.Length;
if (L > 4)
{
setIJKL(m); T.SetPts(DQ[ii], DQ[jj], DQ[kk], DQ[ll]);
}
}
}
public int TetraIndex(int m, int t)
{
setIJKL(m); int res = t;
if (t==ii) res = 1; else if (t==jj) res = 2; else if (t==kk) res = 3; else if (t==ll) res = 4;
return res;
}
public int PlaneIndex(int m, int t, int u)
{
setJKL(m,t); int res = u;
if (u==jj) res = 1; else if (u==kk) res = 2; else if (u==ll) res = 3;
return res;
}
/// Vmi() <summary><para>
/// This function returns a plane normal vector indexed as</para>
/// <para>_</para>
/// <para>m</para>
/// <para>i</para>
/// <para>
/// which is discussed in Vector Quintets. The vMoI() function,
/// "m over i", also returns this vector.
/// </para>
/// </summary>
/// <param name="m">Upper index</param>
/// <param name="i">Lower index</param>
/// <returns></returns>
public D3tuple Vmi(int m, int i)
{
D3tuple D = new D3tuple();
setJKL(m, i); D = (DQ[kk]-DQ[jj])^(DQ[ll]-DQ[kk]);
return D;
}
/// Di() <summary>
/// This function returns D_k for N=5 as discussed in Vector Quintets.
/// </summary>
/// <param name="m">Index of the chirality matrix, m=1..5</param>
/// <returns>Determinant of the chirality matrix</returns>
public double Di(int m)
{
double d = new double();
if (valid) setIJKL(m); int I=ii,J=jj,K=kk,L=ll,M=mm;
d = DQ[I]*Vmi(I,M) - DQ[J]*(DQ[L]^DQ[K]);
return d;
}

2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306

/// Parax() <summary>


/// This function returns the same value as that in the D3tetra class,
/// but the calculations here use the equation written in terms of the
/// normal vectors rather than the vertex points and a displacement
/// point as in the D3tetra Parax() function (q.v.). The equation
/// is given in the comment for Parax2() in this class.
/// </summary>
/// <param name="res">An array of at least five D3tuple vectors</param>
/// <param name="Dofs">Offset into the array of the first vector of the five</param>
/// <returns>Scalar value of the paraxial equation</returns>
public double Parax(D3tuple[] D, int Dofs)
{
double d = new double(); int L = D.Length;
if (L >= Dofs+4)
{
SetPts(D[Dofs], D[Dofs+1], D[Dofs+2], D[Dofs+3], D[Dofs+4]);
double dk=Di(3),dlm=Di(4)+Di(5);
if (dk*dlm != 0)
{
dk = (Vmi(1, 3)*Vmi(2, 3)) / dk;
dlm = ((Vmi(1,4)+Vmi(1,5))*(Vmi(2,4)+Vmi(2,5))) / dlm;
d = dk + dlm;
}
}
return d;
}
// [ (lm)ikj ][ mli * mlj ] + [mijl][ lkmi * lkmj ] = 0
public double Parax2(D3tuple[] D, int Dofs)
{
double d = new double(); int L = D.Length;
if (L >= Dofs+4)
{
SetPts(D[Dofs], D[Dofs+1], D[Dofs+2], D[Dofs+3], D[Dofs+4]);
D3tuple LM = new D3tuple(); LM = DQ[5]-DQ[4];
double t1=0,t2=0,t3=0,t4=0;
t1 = LM*((DQ[1]^DQ[3])+(DQ[3]^DQ[2])+(DQ[2]^DQ[1]));
t2 = ((DQ[5]^DQ[4])+(DQ[4]^DQ[2])+(DQ[2]^DQ[5]))*((DQ[5]^DQ[4])+(DQ[4]^DQ[1])+(DQ[1]^DQ[5]));
t3 = DQ[5]*((DQ[1]^DQ[2])+(DQ[2]^DQ[4])+(DQ[4]^DQ[1])) - DQ[1]*(DQ[2]^DQ[4]);
t4 = ((DQ[4]^DQ[3])+(DQ[3]^DQ[5])+(DQ[5]^DQ[1])+(DQ[1]^DQ[4]))*((DQ[4]^DQ[3])+(DQ[3]^DQ[5])+(DQ[5]^DQ[2])+(DQ[2]^DQ[4]));
d = t1*t2 + t3*t4;
}
return d;
}
// (Di<li>+Dj<lj>+Dk<lk>)*(_Di*MoIsJK+_Dj*MoJsKI+_Dk*MoKsJI) = 0
public double Parax3(D3tuple[] D, int Dofs)
{
double d = new double(); int L = D.Length;
if (L >= Dofs+4)
{
Ro.r.GetDvalues(D, Dofs); SetPts(D[Dofs], D[Dofs+1], D[Dofs+2], D[Dofs+3], D[Dofs+4]);
double Di=Ro.r.D5[1],Dj=Ro.r.D5[2],Dk=Ro.r.D5[3],sgn = Ro.r.D5[5];
if ((Di*Dj*Dk) != 0)
{
if (sgn < 0) sgn = 1; else sgn = -1;
D3tuple V1=new D3tuple(),V2=new D3tuple();
V1 = Di*(DQ[1]-sgn*DQ[4])+Dj*(DQ[2]-sgn*DQ[4])+Dk*(DQ[3]-sgn*DQ[4]);
V2 = vMoIsJK(5, 1, 2, 3)/Di + vMoIsJK(5, 2, 3, 1)/Dj + vMoIsJK(5, 3, 1, 2)/Dk;
d = (!V1)*(!V2);

2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364

}
}
return d;
}
// The following table gives the notations for the scalar and vector
// quantities associated with the "M over I" indexing scheme, followed
// by the names of the functions which calculate these quantities:
//
_
_
_
//
m
m
m
m
m
m
//
jk
jk
j
j
//
i
i
i
i
i
i
//
vMoIsJK
vMoIsJ
vMoI
The 'v' means "vector",
// MoIsJK
MoIsJ
MoI
and 's' means "suffix"
//
/// MoI() - M over I <summary>
/// This function returns the Scalar Triple Product of the vectors
/// indexed by Rho3(m,i) = jkl, which is defined as [ X_j X_k X_l ].
/// </summary>
/// <param name="m">Sphere index</param>
/// <param name="i">Plane index</param>
/// <returns>Scalar Triple Product jkl</returns>
public double MoI(int m, int i)
{
double d = new double(); setJKL(m, i); d = DQ[jj]*(DQ[kk]^DQ[ll]); return d;
}
/// vMoI() - Vector M over I <summary>
/// This function returns the plane normal indexed by Rho3(m,i) = jkl
/// which is defined as the Cyclic Vector Product Sum V(m,i) = jkl
/// </summary>
/// <param name="m">Sphere index</param>
/// <param name="i">Plane index</param>
/// <returns>Normal to plane jkl</returns>
public D3tuple vMoI(int m, int i)
{
D3tuple D = new D3tuple(); setJKL(m, i); D = Ro.r.CVPS3(DQ[jj], DQ[kk], DQ[ll]); return D;
}
/// MoIsJ() - M over I, suffix J <summary>
/// This function returns the Scalar Product of the vector ij with
/// the vector indexed by Rho2(i,j) = kl from the set ijkl.
/// The product is calculated as ij * kl = (X_j-X_i)*(X_l-X_k).
/// </summary>
/// <param name="m">Sphere index</param>
/// <param name="i">First vector index</param>
/// <param name="j">Second vector index (the suffix to the operator)</param>
/// <returns></returns>
public double MoIsJ(int m, int i, int j)
{
double d = new double(); // m_i~j = m_l~kj - m_k~jl
setJKL(m, i); d = (DQ[jj]-DQ[ii])*(DQ[ll]-DQ[kk]);
return d;
}
/// vMoIsJ - Vector M over I, suffix J<summary>
/// This function returns the vector product of the vector with
/// index j and the normal vector to the plane indexed by Rho3(m,i).
/// This is defined as j X uvw where Rho3(m,i) = uvw.
/// </summary>
/// <param name="m">Sphere index</param>
/// <param name="i">Plane index</param>

2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422

/// <param name="j">First operand of vector product (the suffix to the operator)</param>
/// <returns></returns>
public D3tuple vMoIsJ(int m, int i, int j)
{
D3tuple D = new D3tuple(); setJKL(m, i); D = DQ[i]^Ro.r.CVPS3(DQ[jj], DQ[kk], DQ[ll]); return D;
}
/// MoIsJK() - M over I, suffix JK <summary><para>
/// This function returns the scalar product of two vectors indexed
/// from the set Rho3(m,i) = jkl by the suffixes on the operator.
/// Each suffix indexes the two vectors following it, cyclically,
/// in the permutation jkl, so that the terms in the product are,
/// for example, kl and lj, given the suffix jk.</para>
/// <para>
/// The value returned is proportional to the angle between two edges
/// of the triangle defined by jkl, so that, for instance, the sum</para><para>
/// MoIsJK(m,i,j,k)+MoIsJK(m,i,k,l)+MoIsJK(m,i,l,j) =</para><para>
/// MoIsJK(m,j,k,i)+MoIsJK(m,k,l,i)+MoIsJK(m,l,j,i) =</para><para>
/// MoIsJK(m,k,i,j)+MoIsJK(m,l,i,k)+MoIsJK(m,j,i,l) =</para><para>
/// -(1/2)(MoIsJK(m,i,j,j)+MoIsJK(m,i,k,k)+MoIsJK(m,i,l,l))</para>
/// </summary>
/// <param name="m">Sphere index</param>
/// <param name="i">Plane index</param>
/// <param name="j">First vertex indexer</param>
/// <param name="k">Second vertex indexer</param>
/// <returns></returns>
public double MoIsJK(int m, int i, int j, int k)
{
double d = new double(); D3tuple d1 = new D3tuple(),d2 = new D3tuple();
setJKL(m, i); int j1=jj,k1=kk,l1=ll;
Rho3RotateTo(j, ref j1, ref k1, ref l1); d1 = DQ[l1] - DQ[k1];
Rho3RotateTo(k, ref j1, ref k1, ref l1); d2 = DQ[l1] - DQ[k1];
d = d1*d2;
return d;
}
/// vMoIsJK() - Vector M over I suffix JK <summary><para>
/// This function returns the vector product of the vector from X[k] to X[k]
/// with the normal vector indexed by Rho3(m,i) = uvw. If uvw = JKL
/// then an outward normal to the triangle JKL will be returned if jk is
/// JK, KL, or LJ, or an inward normal will be returned if jk is JL,
/// LK, or KJ. </para>
/// <para>
/// For any vector V = uv the sum of vectors vMoIsJK() obtained by cyclic
/// permutation has the following identity: </para><para>
/// vMoIsJK(m,i,u,v) = (V*j)kl + (V*k)lj + (V*l)jk </para><para>
/// = - (V*jk)l - (V*kl)j - (V*lj)k </para><para>
/// Note here that m,i specify jkl but that there are no restrictions on u or v.</para>
/// </summary>
/// <param name="m">Sphere index</param>
/// <param name="i">Plane index</param>
/// <param name="j">First point defining vector jk</param>
/// <param name="k">Second point defining vector jk</param>
/// <returns>The outward normal to edge jk of triangle jkl</returns>
public D3tuple vMoIsJK(int m, int i, int j, int k)
{
D3tuple D = new D3tuple(); // jk X m_i
setJKL(m, i); D = (DQ[k]-DQ[j])^Ro.r.CVPS3(DQ[jj], DQ[kk], DQ[ll]);
return D;
}

2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480

/// MbarIJ() - M bar IJ <summary>


/// This function returns a vector kl indexed by one of the five 4x4 RhoIJ
/// tables indexed by m. Table 5 contains indexes 1234, and the pair ij
/// is referenced to kl by this table such that 12=34, 13=42, 14=23,
/// etc. MbarIJ(5,1,4) would return the vector 23 = DQ[3]-DQ[2], for instance.
/// </summary>
/// <param name="m">Sphere index</param>
/// <param name="j">Row index into table</param>
/// <param name="i">Column index into table</param>
/// <returns>A vector kl such that the concatenation of the indexes mji and
/// kl into the form mjikl has even parity</returns>
public D3tuple MbarIJ(int m, int j, int i)
{
D3tuple D = new D3tuple();
setJKL(m, j); D = DQ[ll]-DQ[kk];
return D;
}
// The following tables, with the table index parenthesizes, give Rho3:
//
//(1) 2 3 4 5 (2) 1 3 4 5 (3) 1 2 4 5 (4) 1 2 3 5 (5) 1 2 3 4
// 2 -- 45 53 34 1 -- 54 35 43 1 -- 45 52 24 1 -- 53 25 32 1 -- 34 42 23
// 3 54 -- 25 42 3 45 -- 51 14 2 54 -- 15 41 2 35 -- 51 13 2 43 -- 14 31
// 4 35 52 -- 23 4 53 15 -- 31 4 25 51 -- 12 3 52 15 -- 21 3 24 41 -- 12
// 5 43 24 32 -- 5 34 41 13 -- 5 42 14 21 -- 5 23 31 12 -- 4 32 13 21 -//
// If a permutation <Tbl><Row><Col><U><V> denotes a table entry having two
// digits <U> and <V>, the ordering is always such that the number of inversions
// in this indexing permutation is even. For instance, 41253 has four inversions
// and 42135 has four inversions. Thus, <Row><Col> and <Col><Row> would always
// produce <U><V> and <V><U>; and it is to be noted that any vector functions
// which are derived from this indexing method will also have sign inversion
// symmetries in their indexes.
/// vMcIJ - M comma IJ <summary><para>
/// This function returns the vector sum of two plane normals
/// indexed by m,k and m,l where m_ij = kl provides the same
/// indexing as used in MbarIJ(). Note that the sum of the
/// normals indexed by m,j and m,k is equal and opposite in sign
/// to this value.</para><para>
/// </para><para>
/// The following identity holds under cyclic permutation of the
/// indexes u,v,w:</para><para>
/// vMcIJ(u,v,w) + vMcIJ(v,w,u) + vMcIJ(w,u,v) = 0</para><para>
/// Note also that vMcIJ() also is equal to the CVPS4 of ikjl:</para><para>
/// vMcIJ(m,i,j) = CVPS4(Xi[i],Xi[k],Xi[j],Xi[l]) = ij X kl</para>
/// </summary>
/// <param name="m">Sphere index</param>
/// <param name="i">First point of ij</param>
/// <param name="j">Second point of ij</param>
/// <returns></returns>
public D3tuple vMcIJ(int m, int i, int j)
{
D3tuple D = new D3tuple();
setKL(m, i, j); // DA = (DQ[jj]-DQ[ii])^(DQ[ll]-DQ[kk]); // An alternate method of calculation
int K = kk,L = ll; D = vMoI(m, K) + vMoI(m, L);
return D;
}
}

2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538

#endregion
#region Identities and scale factor proportionalities
// The G matrices transform from one basis to another; for instance,
// G_<st> transforms from basis (s) to basis (t). The double affix
// above G represents the two axis set basis vector types from which
// G is formed. The notation ~k maps the index k from a value in
// <123456> to the value in the same position in <216543>.
//
// http://cqvis.zxq.net/PRG/R3D/TRANSID.html
//
// If
[ s<^~k|i^>t ] =
//
//
// i|~k
3
3
_
_
// G
= K
K
O ( e
) * O ( e
)
//
<st>
L=1 M=1
i
L
~k
M
//
<-i> <j>
(t)
(s)
//
//
// then
//
//
// i
i|~k k
k
// P = G
P
= [ s<^~k|i^>t ] P
// (t)
(s)
(s)
//
//
#endregion
#region AxisTriple CLASS
public class AxisTriple
{
public double[,] e = null; public AxisSet.Otyp Stype=AxisSet.Otyp.eNon,Ttype=AxisSet.Otyp.eNon;
#region CONSTRUCTORS / INITIALIZATION
public AxisTriple()
{
Init();
}
//
/// <summary>
/// This constructor creates a Zero matrix in a new AxisTriple,
/// which can be used for storage of subsidiary or temporary data.
/// The input and output types can be specified so that a record
/// is kept of the type of components which are being stored.
/// Values eCo,eCon,eUCo,eUCon,eNCo,eNCon,UCo,UCon are valid for
/// the stype and ttype parameters.
/// </summary>
/// <param name="stype">First (input) axis set type</param>
/// <param name="ttype">Second (output) axis set type</param>
public AxisTriple(AxisSet.Otyp stype, AxisSet.Otyp ttype)
{
Init(); Stype = stype; Ttype = ttype;
}
/// <summary>
/// This constructor creates a matrix from a single set of
/// basis vector components in an AxisSet. The input component
/// type field is set to the value you specify.
/// </summary>
/// <param name="AX">AxisSet defining the matrix</param>

2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596

/// <param name="stype">The component type: eCo,eCon,UCo,UCon,NCo,NCon</param>


public AxisTriple(AxisSet AX, AxisSet.Otyp stype)
{
Init();
if (AX != null)
{
Stype = stype; int k = (int)stype;
e1i = AX.Oek[k, 1]; e2i = AX.Oek[k, 2]; e3i = AX.Oek[k, 3]; // Three vectors are assigned.
}
}
/// <summary>
/// This constructor creates a transformation matrix from the
/// basis vector components in two AxisSets. The input and output
/// component type fields are set by the values in AX1 and AX2 when
/// the current instance is initialized and the matrix is constructed.
/// </summary>
/// <param name="AX1">First AxisSet</param>
/// <param name="AX2">Second AxisSet</param>
public AxisTriple(AxisSet AX1, AxisSet AX2)
{
Init();
if ((AX1 != null) & (AX2 != null))
{
int j = 1,i = 1,k = 1; double ee = 0;
int k1 = (int)AX1.OpType,k2 = (int)AX2.OpType;
Stype = AX1.OpType; Ttype = AX2.OpType;
while (j <= 3)
{
i = 1;
while (i <= 3)
{
k = 1; ee = 0;
while (k <= 3)
{
ee = ee + AX2.Oek[k2, j].a4[k]*AX1.Oek[k1, i].a4[k];
k++;
}
e[j, i] = ee;
i++;
}
j++;
}
}
}
/// <summary>
/// This constructor creates a copy of an AxisTriple in the new
/// instance.
/// </summary>
/// <param name="T">A defining AxisTriple</param>
public AxisTriple(AxisTriple T)
{
Init();
if (T != null)
{
Stype = T.Stype; Ttype = T.Ttype;
for (int i = 1; (i <= 3); i++)
{
for (int j = 1; (j <= 3); j++)

2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654

{
e[i, j] = T.e[i, j];
}
}
}
}
public AxisTriple(D3tuple d1, D3tuple d2, D3tuple d3)
{
Init();
e1i = d1; e2i = d2; e3i = d3; Stype = AxisSet.Otyp.eCo;
}
void Init()
{
e = new double[4, 4];
int i=1,j=1;
while (i <= 3)
{
while (j <= 3)
{
e[j, i] = new double();
j++;
}
i++;
}
}
#endregion
bool iOK(int i) { return ((i >= 1) & (i <= 3)); }
#region ACCESSORS
/ INDEXERS
/// this[] <summary>
/// This indexer returns the jth row of the AxisTriple matrix.
/// </summary>
/// <param name="j">Row index (1..3)</param>
/// <returns>A D3tuple containing the row elements as a vector</returns>
public D3tuple this[int j]
{
get
{
D3tuple res = new D3tuple();
if (iOK(j)) res.Set(e[j, 1], e[j, 2], e[j, 3]);
return res;
}
}
/// this[,] <summary>
/// This indexer returns the element in the ith column of the jth row
/// of the AxisTriple matrix.
/// </summary>
/// <param name="j">Row index</param>
/// <param name="i">Column index</param>
/// <returns></returns>
public double this[int j, int i]
{
get
{
double res = new double();
if ((iOK(j)) & (iOK(i))) res = this[j][i];
return res;
}
}

2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712

public double e11 { get { return e[1, 1]; } set { e[1, 1] = value; } }
public double e12 { get { return e[1, 2]; } set { e[1, 2] = value; } }
public double e13 { get { return e[1, 3]; } set { e[1, 3] = value; } }
public double e21 { get { return e[2, 1]; } set { e[2, 1] = value; } }
public double e22 { get { return e[2, 2]; } set { e[2, 2] = value; } }
public double e23 { get { return e[2, 3]; } set { e[2, 3] = value; } }
public double e31 { get { return e[3, 1]; } set { e[3, 1] = value; } }
public double e32 { get { return e[3, 2]; } set { e[3, 2] = value; } }
public double e33 { get { return e[3, 3]; } set { e[3, 3] = value; } }
/// e1[] <summary>
/// This accessor gets and sets the first row of the AxisTriple matrix
/// using an array of doubles, of length 3 or 4. If the length is 4,
/// entries 1..3 contain the matrix elements; otherwise, entries 0..2 do.
/// The get accessor returns a double array of length 4.
/// </summary>
public double[] e1
{
get
{
return new double[] { 0, e[1, 1], e[1, 2], e[1, 3]};
}
set
{
double[] ev = value;
int L = ev.Length,i=1,k=1; if (L < 4) k = 0;
if (L >= 3)
{
while ((i <= 3) & (k < L))
{
e[1, i] = ev[k];
i++; k++;
}
}
}
}
/// e2[] <summary>
/// This accessor gets and sets the second row of the AxisTriple matrix
/// using an array of doubles, of length 3 or 4. If the length is 4,
/// entries 1..3 contain the matrix elements; otherwise, entries 0..2 do.
/// The get accessor returns a double array of length 4.
/// </summary>
public double[] e2
{
get
{
return new double[] { 0, e[1, 1], e[1, 2], e[1, 3]};
}
set
{
double[] ev = value;
int L = ev.Length,i=1,k=1; if (L < 4) k = 0;
if (L >= 3)
{
while ((i <= 3) & (k < L))
{
e[1, i] = ev[k];
i++; k++;
}

2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770

}
}
}
/// e3[] <summary>
/// This accessor gets and sets the third row of the AxisTriple matrix
/// using an array of doubles, of length 3 or 4. If the length is 4,
/// entries 1..3 contain the matrix elements; otherwise, entries 0..2 do.
/// The get accessor returns a double array of length 4.
/// </summary>
public double[] e3
{
get
{
return new double[] { 0, e[1, 1], e[1, 2], e[1, 3]};
}
set
{
double[] ev = value;
int L = ev.Length,i=1,k=1; if (L < 4) k = 0;
if (L >= 3)
{
while ((i <= 3) & (k < L))
{
e[1, i] = ev[k];
i++; k++;
}
}
}
}
/// <summary>
/// This accessor gets and sets the first row of the AxisTriple matrix
/// using a D3tuple which stores the values.
/// </summary>
public D3tuple ej1
{
get { return new D3tuple(e[1,1],e[2,1],e[3,1]); }
set
{
D3tuple ev = value;
if (ev != null)
{
e[1, 1] = ev.X; e[2, 1] = ev.Y; e[3, 1] = ev.Z;
}
}
}
/// <summary>
/// This accessor gets and sets the second row of the AxisTriple matrix
/// using a D3tuple which stores the values.
/// </summary>
public D3tuple ej2
{
get { return new D3tuple(e[1,2],e[2,2],e[3,2]); }
set
{
D3tuple ev = value;
if (ev != null)
{
e[1, 2] = ev.X; e[2, 2] = ev.Y; e[3, 2] = ev.Z;

2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828

}
}
}
/// <summary>
/// This accessor gets and sets the third row of the AxisTriple matrix
/// using a D3tuple which stores the values.
/// </summary>
public D3tuple ej3
{
get { return new D3tuple(e[1,3],e[2,3],e[3,3]); }
set
{
D3tuple ev = value;
if (ev != null)
{
e[1, 3] = ev.X; e[2, 3] = ev.Y; e[3, 3] = ev.Z;
}
}
}
/// e1i <summary>
/// This accessor gets and sets the first column of the AxisTriple matrix
/// using a D3tuple which stores the values.
/// </summary>
public D3tuple e1i
{
get { return new D3tuple(e[1,1],e[1,2],e[1,3]); }
set
{
D3tuple ev = value;
if (ev != null)
{
e[1, 1] = ev.X; e[1, 2] = ev.Y; e[1, 3] = ev.Z;
}
}
}
/// e2i <summary>
/// This accessor gets and sets the second column of the AxisTriple matrix
/// using a D3tuple which stores the values.
/// </summary>
public D3tuple e2i
{
get { return new D3tuple(e[2,1],e[2,2],e[2,3]); }
set
{
D3tuple ev = value;
if (ev != null)
{
e[2, 1] = ev.X; e[2, 2] = ev.Y; e[2, 3] = ev.Z;
}
}
}
/// e3i <summary>
/// This accessor gets and sets the third column of the AxisTriple matrix
/// using a D3tuple which stores the values.
/// </summary>
public D3tuple e3i
{
get { return new D3tuple(e[3,1],e[3,2],e[3,3]); }

2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886

set
{
D3tuple ev = value;
if (ev != null)
{
e[3, 1] = ev.X; e[3, 2] = ev.Y; e[3, 3] = ev.Z;
}
}
}
#endregion
#region ACCESS TO ROWS/COLS USING INDIVIDUAL D3tuples
public void SetRows(D3tuple d1, D3tuple d2, D3tuple d3)
{
e1i = d1; e2i = d2; e3i = d3;
}
public void GetRows(ref D3tuple d1, ref D3tuple d2, ref D3tuple d3)
{
d1 = e1i; d2 = e2i; d3 = e3i;
}
public void SetCols(D3tuple d1, D3tuple d2, D3tuple d3)
{
ej1 = d1; ej2 = d2; ej3 = d3;
}
public void GetCols(ref D3tuple d1, ref D3tuple d2, ref D3tuple d3)
{
d1 = ej1; d2 = ej2; d3 = ej3;
}
#endregion
/// Transpose() <summary>
/// This routine does not return the transposed matrix.
/// It actually performs the transposition on the current
/// AxisTriple instance. To find the transpose without
/// changing the current AxisTriple, use the Trans() function
/// to return the transposed matrix.
/// </summary>
public void Transpose()
{
double d;
for (int j = 1; (j <= 3); j++)
{
for (int i = j+1; (i <= 3); i++)
{
d = e[i, j]; e[i, j] = e[j, i]; e[j, i] = d;
}
}
}
/// Trans() <summary>
/// This function returns the transpose of the matrix contained
/// within the current AxisTriple without altering the current
/// instance.
/// </summary>
/// <returns>An AxisTriple containing the transposed matrix</returns>
public AxisTriple Trans()
{
AxisTriple TR = new AxisTriple(this);
TR.Transpose();
return TR;
}

2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944

/// Tr() <summary>


/// This function returns the trace of the matrix contained
/// within the current AxisTriple instance.
/// </summary>
/// <returns></returns>
public double Tr()
{
return e[1, 1] + e[2, 2] + e[3, 3];
}
/// Invert() <summary>
/// This routine does not return the inverse of the AxisTriple matrix.
/// It actually performs the matrix inversion on the current instance.
/// To find the inverse without changing the matrix, use the Inv()
/// function to return the inverted matrix.
/// </summary>
public void Invert()
{
double det = e1i*(e2i^e3i);
if (det != 0)
{
D3tuple[] D = new D3tuple[3];
int i = 0, j = 1;
while (i < 3)
{
D[i] = new D3tuple(e[1, j], e[2, j], e[3, j]);
i++; j++;
}
e1i = (D[1]^D[2]) / det; e2i = (D[2]^D[0]) / det; e3i = (D[0]^D[1]) / det;
}
AxisSet.Otyp to = Stype; Stype = Ttype; Ttype = to;
}
/// Inv() <summary>
/// This function returns the inverse of the AxisTriple matrix.
/// The current AxisTriple instance is unaffected.
/// </summary>
/// <returns>An AxisTriple containing the inverse matrix</returns>
public AxisTriple Inv()
{
AxisTriple TR = new AxisTriple(this);
TR.Invert();
return TR;
}
#region AxisTriple OPERATORS
/// * OPERATOR - Vector Pre-Multiplication by an AxisTriple matrix <summary>
/// This operator premultiplies a vector res by a matrix from AxisTriple AT.
/// </summary>
/// <param name="AT">Matrix premultiplier</param>
/// <param name="res">Column vector to be multiplied</param>
/// <returns>Column vector containing the matrix product</returns>
public static D3tuple operator*(AxisTriple AT, D3tuple D)
{
double[] res = new double[3];
if ((AT != null) & (D != null))
{
int j = 1,k = 1; double ee = 0;
while (j <= 3)
{
k = 1; ee = 0;

2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002

while (k <= 3)
{
ee = ee + AT.e[j, k]*D.a4[k];
k++;
}
res[j-1] = ee;
j++;
}
}
return new D3tuple(res[0],res[1],res[2]);
}
/// * OPERATOR - Vector Post-Multiplication by an AxisTriple matrix <summary>
/// This operator postmultiplies a vector res by a matrix from AxisTriple AT.
/// </summary>
/// <param name="res">Row vector to be multiplied</param>
/// <param name="AT">Matrix postmultiplier</param>
/// <returns>Row vector containing the matrix product</returns>
public static D3tuple operator*(D3tuple D, AxisTriple AT)
{
double[] res = new double[3];
int k = 1, i=1; double ee = 0;
if ((AT != null) & (D != null))
{
while (i <= 3)
{
k = 1;
while (k <= 3)
{
ee = ee + D.a4[k]*AT.e[k, i];
k++;
}
res[i-1] = ee;
i++;
}
}
return new D3tuple(res[0],res[1],res[2]);
}
/// * OPERATOR - Matrix multiplication for AxisTriples <summary>
/// This function performs matrix multiplication AT1.e*AT2.e such that
/// each element RQmatrix.e[j,i] is the sum of AT1.e[j,k]*AT2.e[k,i]
/// for k=1..3 where the rows are indexed on j and columns on i.
/// </summary>
/// <param name="AT1">First AxisTriple matrix operand</param>
/// <param name="AT2">Second AxisTriple matrix operand</param>
/// <returns>The matrix product of AT1.e[] and AT2.e[]</returns>
public static AxisTriple operator*(AxisTriple AT1, AxisTriple AT2)
{
AxisTriple AT = new AxisTriple();
if ((AT1 != null) & (AT2 != null))
{
double ee = 0;
for (int i = 1; (i <= 3); i++)
{
for (int j = 1; (j <= 3); j++)
{
ee = 0;
for (int k = 1; (k <= 3); k++)
{

3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060

ee = ee + AT1.e[j, k] * AT2.e[k, i];


}
AT.e[j, i] = ee;
}
}
}
return AT;
}
#endregion
}
#endregion
#region AxisSet CLASS
public class AxisSet
{
D3tuple[] X5 = null; // ARRAY [0..5]
public D3tuple[,] Oek = null; // ARRAY [0..6,0..3] - Six sets of three axes
double [] G = null; double[,] H = null;
static int[] kbar = { 0,2,1,6,5,4,3,6,5 };
/// <summary>
/// The Otyp enumeration contains the constants identifying vector component types.
/// The characters 'U' and 'N' represent "Unit" and "Normalized". 'Co' and 'Con'
/// represent "Covariant" and "Contravariant". 'e' is a prefix signifying that
/// the reference is to a basis vector type.
/// </summary>
public enum Otyp { eNon=0,eCo=1,eCon=2,eUCo=3,eUCon=4,eNCo=5,eNCon=6,UCo=7,UCon=8 };
Otyp opType=(Otyp)3;
#region CONSTRUCTORS / INITIALIZATION
public AxisSet() { Init(); }
public AxisSet(D3tuple[] D)
{
Init(); SetAxisPts(D);
}
public AxisSet(D3tuple d1, D3tuple d2, D3tuple d3)
{
Init(); D3tuple[] D = new D3tuple[4]; D[1] = d1; D[2] = d2; D[3] = d3; SetAxisPts(D);
}
public AxisSet(D3tuple d1, D3tuple d2, D3tuple d3, D3tuple d4)
{
Init(); D3tuple[] D = new D3tuple[5]; D[1] = d1; D[2] = d2; D[3] = d3; D[4] = d4; SetAxisPts(D);
}
void Init()
{
X5 = new D3tuple[6]; Oek = new D3tuple[7, 4]; G = new double[7]; H = new double[7, 4];
int i = 0,j=1;
while (i <= 5) { X5[i] = new D3tuple(); i++; }
i = 1;
while (i <= 6)
{
j=1;
while (j <= 3) { Oek[i, j] = new D3tuple(); H[i,j] = new double(); j++; }
G[i] = new double();
i++;
}
}
#endregion
bool iOK(int i) { return ((i >= 1) & (i <= 3)); }
public int kBar(int k) { return kbar[modd6(k)]; }
int modd3(int i) { if (i < 0) i = -i; return ((i-1) % 3)+1; }

3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118

int modd6(int i) { if (i < 0) i = -i; return ((i-1) % 6)+1; }


/// this[] <summary>
/// This accessor returns a covariant axis e_i as a D3tuple if i=1..3.<para>
/// Otherwise, it returns a D3tuple containing the lengths of these three axes.</para>
/// </summary>
/// <param name="i">Axis number (1..3) or zero</param>
/// <returns>The axis vector e_i = X[i+1]-X[1], or the lengths of the axes when i=0</returns>
public D3tuple this[int i]
{
get
{
D3tuple res = new D3tuple();
if (iOK(i)) res = X5[i] - X5[1];
else res = X5[0];
return res;
}
}
/// this[,] <summary>
/// This accessor returns the jth component of axis e_i if i=1..3.<para>
/// Otherwise, it returns the length of the jth axis.</para>
/// </summary>
/// <param name="j">Index of component (1..3), or of axis (if i=0)</param>
/// <param name="i">Axis number (1..3) or zero</param>
/// <returns>The jth component of the axis X[i+1]-X[1], or the length of X[j+1]-X[1] when i=0</returns>
public double this[int j, int i]
{
get
{
double res = new double();
if (iOK(j))
{
if (iOK(i)) res = this[i][j];
else res = X5[0][j];
}
return res;
}
}
/// AxisPts <summary>
/// This accessor returns the AxisSet's defining points as a D3tuple array of length 5.
/// The four axis points are in entries 1..4 of the returned array.
/// </summary>
public D3tuple[] AxisPts { get { return X5; } }
/// SetAxisPts() <summary><para>
/// This routine accepts four vectors specifying the locations of
/// the origin and three axis points, one for each axis. The scaling
/// of each axis depends on the distance between the origin and the
/// given axis point.</para><para>
/// </para><para>
/// If the array res[] has a length of 3 then the origin will be set to
/// [0,0,0] and the axis lengths will be the lengths of the vectors
/// supplied in array res[].</para><para>
/// </para><para>
/// If res[] has length 4 then elements 1..3 will be interpreted as
/// axis points; the origin will be zeroed, and res[0] will be ignored.
/// This interpretation is consistent with the D3plane functions
/// which return D3tuple arrays.</para><para>
/// </para><para>
/// If res[] has length 5 or greater, then elements 2..4 will be

3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176

/// interpreted as axis points, and element 1 will be interpreted


/// as the origin. This interpretation is consistend with the D3tetra
/// functions which return D3tuple arrays.</para>
/// </summary>
/// <param name="res">Array of D3tuples, of length at least 3</param>
public void SetAxisPts(D3tuple[] D)
{
if (D == null) return;
int L = D.Length,i=1,j=0;
if (L == 3) { i = 2; j = 1; X5[1].Zero(); }
else if (L == 4) { j=1; X5[1].Zero(); }
else if (L>= 5) { j=1; }
while ((i <= 4) & (j < L))
{
X5[i] = D[j];
i++; j++;
}
reCalc(false);
}
/// SetOe1() <summary><para>
/// This routine accepts an array of length at least 3 which
/// supplies three values for axis vectors. The origin is zeroed
/// and each axis point is set to be located at the head of its
/// direction vector. The distance between this point and the
/// origin is set to equal the length of the supplied vector.</para><para>
/// </para><para>
/// If the array res[] is of length 3, elements 0..2 are accepted
/// as axis vectors. If res[] has length greater than 3, elements
/// 1..3 are used as the axis vectors. Note that this does not
/// conform to the axis-point definition scheme, where elements
/// 2..4 usually represent axis points.</para><para>
/// </para><para>
/// This routine is named SetOe1() because the set of vectors e_i
/// for k=1..3 which are assigned values by this function are the
/// covariant set O_1(e) .</para><para>
/// This concept is discussed in detail in Vector Quintets.
/// </para>
/// </summary>
/// <param name="res">An array of D3tuples of length at least 3</param>
public void SetOe1(D3tuple[] D)
{
if (D == null) return;
X5[1].Zero(); int L = D.Length,i=1,j=2;
if (L <= 3) i = 0;
while ((j <= 4) & (i < L))
{
X5[j] = D[i];
i++; j++;
}
reCalc(false);
}
#region PRIVATE
void reCalc(bool FromEi)
{
int i = 1;
if (FromEi) { while (i <= 6) { calcOek(i); i++; } }
i = 2;
while (i <= 4)

3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234

{
if (FromEi) { X5[i] = Oek[1, i-1] + X5[1]; }
else { Oek[1, i-1] = X5[i] - X5[1]; }
i++;
}
if (!FromEi)
{
i = 1;
while (i <= 6) { calcOek(i); i++; }
}
X5[0].X = ~Oek[1, 1]; X5[0].Y = ~Oek[1, 2]; X5[0].Z = ~Oek[1, 3];
}
D3tuple ensure(ref D3tuple d) { if (d == null) d = new D3tuple(); return d; }
void calcOek(int k) // Calculates one of the six axis sets O_k(e_i) | j=1..3 & k=1..6
{
double stp = 0,gg=0; int i = 1;
if (k == 2)
{
stp = Oek[1, 1]*(Oek[1, 2]^Oek[1, 3]);
if (stp != 0)
{
Oek[2, 1] = (Oek[1, 2]^Oek[1, 3]) / stp;
Oek[2, 2] = (Oek[1, 3]^Oek[1, 1]) / stp;
Oek[2, 3] = (Oek[1, 1]^Oek[1, 2]) / stp;
}
}
else if (k == 3)
{
while (i <= 3){ Oek[3, i] = !Oek[1, i]; i++;}
}
else if (k == 4)
{
while (i <= 3){ Oek[4, i] = !Oek[2, i]; i++;}
}
else if (k == 5)
{
stp = Oek[4, 1]*(Oek[4, 2]^Oek[4, 3]); G[4] = stp;
if (stp != 0)
{
Oek[5, 1] = (Oek[4, 2]^Oek[4, 3]) / stp;
Oek[5, 2] = (Oek[4, 3]^Oek[4, 1]) / stp;
Oek[5, 3] = (Oek[4, 1]^Oek[4, 2]) / stp;
}
}
else if (k == 6)
{
stp = Oek[3, 1]*(Oek[3, 2]^Oek[3, 3]); G[3] = stp;
if (stp != 0)
{
Oek[6, 1] = (Oek[3, 2]^Oek[3, 3]) / stp;
Oek[6, 2] = (Oek[3, 3]^Oek[3, 1]) / stp;
Oek[6, 3] = (Oek[3, 1]^Oek[3, 2]) / stp;
}
}
gg = Oek[k, 1]*(Oek[k, 2]^Oek[k, 3]);
G[k] = gg; H[k, 1] = ~Oek[k, 1]; H[k, 2] = ~Oek[k, 2]; H[k, 3] = ~Oek[k, 3];
}
void calc21()

3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292

{
double stp = Oek[2, 1]*(Oek[2, 2]^Oek[2, 3]);
if (stp != 0)
{
Oek[1, 1] = (Oek[2, 2]^Oek[2, 3]) / stp;
Oek[1, 2] = (Oek[2, 3]^Oek[2, 1]) / stp;
Oek[1, 3] = (Oek[2, 1]^Oek[2, 2]) / stp;
}
}
void calc54()
{
Oek[4, 1] = !((Oek[5, 2]^Oek[5, 3]));
Oek[4, 2] = !((Oek[5, 3]^Oek[5, 1]));
Oek[4, 3] = !((Oek[5, 1]^Oek[5, 2]));
}
void calc63()
{
Oek[3, 1] = !((Oek[6, 2]^Oek[6, 3]));
Oek[3, 2] = !((Oek[6, 3]^Oek[6, 1]));
Oek[3, 3] = !((Oek[6, 1]^Oek[6, 2]));
}
#endregion
public void Rescale()
{
reCalc(true);
}
#region CANONICAL OPERATIONS - IDENTITY, UNITY, RECIPROCITY
public Otyp OpType { get { return opType; } set { opType = value; } }
/// OpI() (IDENTITY) <summary><para>
/// IDENTITY OPERATION I(k) = k</para><para>
/// This function performs the identity operation on each element of
/// a vector array of arbitrary length > 2, and returns the result in a
/// D3tuple array of length 4 of which element 0 is the zero vector.</para>
/// </summary>
/// <param name="res"></param>
/// <returns></returns>
public D3tuple[] OpI(D3tuple[] D)
{
D3tuple[] res = new D3tuple[4];
if (D != null)
{
int L = D.Length; int i=1,j=1;
if (L == 3) j = 0;
while ((i <= 3) & (j < L)) { res[i] = D[j]; i++; j++; }
}
return res;
}
/// OpU() (UNITY) <summary><para>
/// UNIT VECTOR OPERATION U(k) = k / |k|</para><para>
/// This function performs the unity operation on each element of a
/// vector array of length at least 3, and returns the result in a
/// D3tuple array of length 4 of which element 0 is the zero vector.</para>
/// </summary>
/// <param name="res"></param>
/// <returns></returns>
public D3tuple[] OpU(D3tuple[] D)
{
D3tuple[] res = new D3tuple[4]; int i = 1,j=1;

3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350

if (D != null)
{
int L = D.Length;
if (L >= 3)
{
if (L == 3) j = 0;
while ((i <= 3) & (j < L))
{
res[i] = !D[j];
i++; j++;
}
}
}
return res;
}
/// OpX() (RECIPROCAL) <summary><para>
/// RECIPROCAL VECTOR OPERATION X(k) = ( j X k ) / [ k j k ]</para><para>
/// This function performs the reciprocity operation on each element
/// of a vector array of length at least 3, and returns the result in
/// a D3tuple array of length 4 of which element 0 is the zero vector.</para>
/// </summary>
/// <param name="res"></param>
/// <returns></returns>
public D3tuple[] OpX(D3tuple[] D)
{
D3tuple[] res = new D3tuple[4]; int j=1;
if (D != null)
{
int L = D.Length;
if (L >= 3)
{
if (L == 3) j = 0;
double stp = D[j]*(D[j+1]^D[j+2]);
if (stp != 0)
{
res[1] = (D[j+1]^D[j+2]) / stp;
res[2] = (D[j+2]^D[j ]) / stp;
res[3] = (D[j ]^D[j+1]) / stp;
}
}
}
return res;
}
#endregion
// Vectors e_i or e^j
eCo,eCon,eUCo,eUCon,eNCo,eNCon
#region SINGLE INDEXED (k=1..3) VECTORS
/// Co() <summary>
/// Returns a covariant vector by its index (1..3)
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public D3tuple Co(int i) { return Oek[1, modd3(i)]; }
/// Con() <summary>
/// Returns a contravariant vector by its index (1..3)
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public D3tuple Con(int i) { return Oek[2, modd3(i)]; }

3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408

/// UCo() <summary>


/// Returns a covariant SignOnly vector by its index (1..3)
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public D3tuple UCo(int i) { return Oek[3, modd3(i)]; }
/// UCon() <summary>
/// Returns a contravariant SignOnly vector by its index (1..3)
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public D3tuple UCon(int i) { return Oek[4, modd3(i)]; }
/// NCo() <summary>
/// Returns a normalized covariant vector by its index (1..3)
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public D3tuple NCo(int i) { return Oek[5, modd3(i)]; }
/// NCon() <summary>
/// Returns a normalized contravariant vector by its index (1..3)
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public D3tuple NCon(int i) { return Oek[6, modd3(i)]; }
#endregion
#region INDIVIDUAL VECTOR ACCESSORS e1..E3 c1..C3 u1..U3 (GET/SET)
/// e1 <summary>
/// Gets and sets the covariant vector e_1
///
/// Setting a covariant vector causes all six sets to be
/// recalculated. Only the lengths of the three covariant
/// vectors and the SignOnly vectors will remain unchanged once
/// this vector is set. The lengths of the contravariant and
/// the normalized vectors are recalculated.
/// </summary>
public D3tuple e1
{
get { return Oek[1, 1]; }
set { D3tuple d = value; ensure(ref d); Oek[1, 1] = d; reCalc(true); }
}
/// e2 <summary>
/// Gets and sets the covariant vector e_2
///
/// Setting a covariant vector causes all six sets to be
/// recalculated. Only the lengths of the three covariant
/// vectors and the SignOnly vectors will remain unchanged once
/// this vector is set. The lengths of the contravariant and
/// the normalized vectors are recalculated.
/// </summary>
public D3tuple e2
{
get { return Oek[1, 2]; }
set { D3tuple d = value; ensure(ref d); Oek[1, 2] = d; reCalc(true); }
}
/// e3 <summary>
/// Gets and sets the covariant vector e_3
///
/// Setting a covariant vector causes all six sets to be

3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466

/// recalculated. Only the lengths of the three covariant


/// vectors and the SignOnly vectors will remain unchanged once
/// this vector is set. The lengths of the contravariant and
/// the normalized vectors are recalculated.
/// </summary>
public D3tuple e3
{
get { return Oek[1, 3]; }
set { D3tuple d = value; ensure(ref d); Oek[1, 3] = d; reCalc(true); }
}
/// E1 <summary>
/// Gets and sets the contravariant vector e^1
///
/// Setting a contravariant vector causes all six sets to be
/// recalculated. Only the lengths of the three contravariant
/// vectors and the SignOnly vectors will remain unchanged once
/// this vector is set. The lengths of the covariant and
/// the normalized vectors are recalculated.
/// </summary>
public D3tuple E1
{
get { return Oek[2, 1]; }
set { D3tuple d = value; ensure(ref d); Oek[2, 1] = d; calc21(); reCalc(true); }
}
/// E2 <summary>
/// Gets and sets the contravariant vector e^2
///
/// Setting a contravariant vector causes all six sets to be
/// recalculated. Only the lengths of the three contravariant
/// vectors and the SignOnly vectors will remain unchanged once
/// this vector is set. The lengths of the covariant and
/// the normalized vectors are recalculated.
/// </summary>
public D3tuple E2
{
get { return Oek[2, 2]; }
set { D3tuple d = value; ensure(ref d); Oek[2, 2] = d; calc21(); reCalc(true);}
}
/// E3 <summary>
/// Gets and sets the contravariant vector e^3
///
/// Setting a contravariant vector causes all six sets to be
/// recalculated. Only the lengths of the three contravariant
/// vectors and the SignOnly vectors will remain unchanged once
/// this vector is set. The lengths of the covariant and
/// the normalized vectors are recalculated.
/// </summary>
public D3tuple E3
{
get { return Oek[2, 3]; }
set { D3tuple d = value; ensure(ref d); Oek[2, 3] = d; calc21(); reCalc(true);}
}
/// c1 <summary>
/// Gets and sets the covariant SignOnly vector e_1
///
/// Setting a covariant SignOnly vector causes all six sets to be
/// recalculated. Only the lengths of the SignOnly vectors will
/// remain unchanged once this vector is set. The lengths of

3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524

/// all other vectors are recalculated.


/// </summary>
public D3tuple c1
{
get { return Oek[3, 1]; }
set { D3tuple d = value; ensure(ref d); Oek[1, 1] = H[1, 1]*d; reCalc(true);}
}
/// c2 <summary>
/// Gets and sets the covariant SignOnly vector e_2
///
/// Setting a covariant SignOnly vector causes all six sets to be
/// recalculated. Only the lengths of the SignOnly vectors will
/// remain unchanged once this vector is set. The lengths of
/// all other vectors are recalculated.
/// </summary>
public D3tuple c2
{
get { return Oek[3, 2]; }
set { D3tuple d = value; ensure(ref d); Oek[1, 2] = H[1, 2]*d; reCalc(true);}
}
/// c3 <summary>
/// Gets and sets the covariant SignOnly vector e_3
///
/// Setting a covariant SignOnly vector causes all six sets to be
/// recalculated. Only the lengths of the SignOnly vectors will
/// remain unchanged once this vector is set. The lengths of
/// all other vectors are recalculated.
/// </summary>
public D3tuple c3
{
get { return Oek[3, 3]; }
set { D3tuple d = value; ensure(ref d); Oek[1, 3] = H[1, 3]*d; reCalc(true);}
}
/// C1 <summary>
/// Gets and sets the contravariant vector e^1
///
/// Setting a covariant SignOnly vector causes all six sets to be
/// recalculated. Only the lengths of the SignOnly vectors will
/// remain unchanged once this vector is set. The lengths of
/// all other vectors are recalculated.
/// </summary>
public D3tuple C1
{
get { return Oek[4, 1]; }
set { D3tuple d = value; ensure(ref d); Oek[2, 1] = H[2, 1]*d; calc21(); reCalc(true); }
}
/// C2 <summary>
/// Gets and sets the contravariant SignOnly vector e^2
///
/// Setting a covariant SignOnly vector causes all six sets to be
/// recalculated. Only the lengths of the SignOnly vectors will
/// remain unchanged once this vector is set. The lengths of
/// all other vectors are recalculated.
/// </summary>
public D3tuple C2
{
get { return Oek[4, 2]; }
set { D3tuple d = value; ensure(ref d); Oek[2, 2] = H[2, 2]*d; calc21(); reCalc(true); }

3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582

}
/// C3 <summary>
/// Gets and sets the contravariant SignOnly vector e^3
///
/// Setting a covariant SignOnly vector causes all six sets to be
/// recalculated. Only the lengths of the SignOnly vectors will
/// remain unchanged once this vector is set. The lengths of
/// all other vectors are recalculated.
/// </summary>
public D3tuple C3
{
get { return Oek[4, 3]; }
set { D3tuple d = value; ensure(ref d); Oek[2, 3] = H[2, 3]*d; calc21(); reCalc(true); }
}
/// u1 <summary>
/// Gets and sets the normalized covariant vector u_1
///
/// Setting a normalized covariant vector causes all six sets to be
/// recalculated. Only the lengths of the SignOnly vectors will
/// remain unchanged once this vector is set. The lengths of
/// all other vectors are recalculated.
/// </summary>
public D3tuple u1
{
get { return Oek[5, 1]; }
set { D3tuple d = value; ensure(ref d); Oek[5, 1] = d; calc54(); Oek[2, 1] = H[2, 1]*Oek[4,1]; reCalc(true); }
}
/// u2 <summary>
/// Gets and sets the normalized covariant vector u_2
///
/// Setting a normalized covariant vector causes all six sets to be
/// recalculated. Only the lengths of the SignOnly vectors will
/// remain unchanged once this vector is set. The lengths of
/// all other vectors are recalculated.
/// </summary>
public D3tuple u2
{
get { return Oek[5, 2]; }
set { D3tuple d = value; ensure(ref d); Oek[5, 2] = d; calc54(); Oek[2, 1] = H[2, 1]*Oek[4,1]; reCalc(true); }
}
/// u3 <summary>
/// Gets and sets the normalized covariant vector u_3
///
/// Setting a normalized covariant vector causes all six sets to be
/// recalculated. Only the lengths of the SignOnly vectors will
/// remain unchanged once this vector is set. The lengths of
/// all other vectors are recalculated.
/// </summary>
public D3tuple u3
{
get { return Oek[5, 3]; }
set { D3tuple d = value; ensure(ref d); Oek[5, 3] = d; calc54(); Oek[2, 1] = H[2, 1]*Oek[4,1]; reCalc(true); }
}
/// U1 <summary>
/// Gets and sets the normalized contravariant vector u^1
///
/// Setting a normalized contravariant vector causes all six
/// sets to be recalculated. Only the lengths of the SignOnly

3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640

/// vectors will remain unchanged once this vector is set.


/// The lengths of all other vectors are recalculated.
/// </summary>
public D3tuple U1
{
get { return Oek[6, 1]; }
set { D3tuple d = value; ensure(ref d); Oek[6, 1] = d; calc63(); Oek[1, 1] = H[1, 1]*Oek[3,1]; reCalc(true); }
}
/// U2 <summary>
/// Gets and sets the normalized contravariant vector u^2
///
/// Setting a normalized contravariant vector causes all six
/// sets to be recalculated. Only the lengths of the SignOnly
/// vectors will remain unchanged once this vector is set.
/// The lengths of all other vectors are recalculated.
/// </summary>
public D3tuple U2
{
get { return Oek[6, 2]; }
set { D3tuple d = value; ensure(ref d); Oek[6, 2] = d; calc63(); Oek[1, 2] = H[1, 2]*Oek[3,2]; reCalc(true); }
}
/// U3 <summary>
/// Gets and sets the normalized contravariant vector u^3
///
/// Setting a normalized contravariant vector causes all six
/// sets to be recalculated. Only the lengths of the SignOnly
/// vectors will remain unchanged once this vector is set.
/// The lengths of all other vectors are recalculated.
/// </summary>
public D3tuple U3
{
get { return Oek[6, 3]; }
set { D3tuple d = value; ensure(ref d); Oek[6, 3] = d; calc63(); Oek[1, 3] = H[1, 3]*Oek[3,3]; reCalc(true); }
}
/// P <summary>
/// This accessor gets and sets the default data point vector for this axis set.
/// </summary>
public D3tuple P
{
get { return X5[5]; }
set { D3tuple d = value; if (d != null) X5[5] = d; }
}
#endregion
#region Scale factors h_i or h^j
hCo,hCon,hUCo,hUCon,hNCo,hNCon
/// hCo() <summary>
/// This function returns the length of an indexed covariant vector e_i
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public double hCo(int i)
{ double d = new double(); d = H[1,modd3(i)]; return d; }
/// hCon() <summary>
/// This function returns the length of an indexed contravariant vector e^k
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public double hCon(int i)
{ double d = new double(); d = H[2,modd3(i)]; return d; }

3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698

/// hNCo() <summary>


/// This function returns the length of an indexed normalized covariant vector u_i
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public double hNCo(int i)
{ double d = new double(); d = H[5,modd3(i)]; return d; }
/// hNCon() <summary>
/// This function returns the length of an indexed normalized contravariant vector u^k
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public double hNCon(int i)
{ double d = new double(); d = H[6,modd3(i)]; return d; }
#endregion
#region Components Fi = P * e_i or P * e^j
FCo,FCon,FUCo,FUCon,FNCo,FNCon
public D3tuple UnitComponents(D3tuple V, Otyp o)
{
V = !V; return Components(V, o);
}
/// FCo() <summary>
/// This function returns the scalar product of a vector V with an
/// indexed covariant vector. The value returned is used as the
/// component associated with the contravariant vector with that index
/// when substituted into a vector equation.
/// </summary>
/// <param name="V"></param>
/// <param name="i"></param>
/// <returns></returns>
public double FCo(D3tuple V, int i)
{ double d = new double(); d = V*Oek[1,modd3(i)]; return d; }
/// FCon() <summary>
/// This function returns the scalar product of a vector V with an
/// indexed contravariant vector. The value returned is used as the
/// component associated with the covariant vector with that index
/// when substituted into a vector equation.
/// </summary>
/// <param name="V"></param>
/// <param name="i"></param>
/// <returns></returns>
public double FCon(D3tuple V, int i)
{ double d = new double(); d = V*Oek[2,modd3(i)]; return d; }
/// FUCo() <summary>
/// This function returns the scalar product of a vector V with an
/// indexed covariant SignOnly vector. The value returned is used as the
/// component associated with the normalized contravariant vector with
/// that index when substituted into a vector equation.
/// </summary>
/// <param name="V"></param>
/// <param name="i"></param>
/// <returns></returns>
public double FUCo(D3tuple V, int i)
{ double d = new double(); d = V*Oek[3,modd3(i)]; return d; }
/// FUCon() <summary>
/// This function returns the scalar product of a vector V with an
/// indexed contravariant SignOnly vector. The value returned is used
/// as the component associated with the normalized covariant vector
/// with that index when substituted into a vector equation.

3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756

/// </summary>
/// <param name="V"></param>
/// <param name="i"></param>
/// <returns></returns>
public double FUCon(D3tuple V, int i)
{ double d = new double(); d = V*Oek[4,modd3(i)]; return d; }
/// FNCo() <summary>
/// This function returns the scalar product of a vector V with an
/// indexed normalized covariant vector. The value returned is used
/// as the component associated with the contravariant SignOnly vector with
/// that index when substituted into a vector equation.
/// </summary>
/// <param name="V"></param>
/// <param name="i"></param>
/// <returns></returns>
public double FNCo(D3tuple V, int i)
{ double d = new double(); d = V*Oek[5,modd3(i)]; return d; }
/// FNCon() <summary>
/// This function returns the scalar product of a vector V with an
/// indexed contravariant vector. The value returned is used as the
/// component associated with the covariant SignOnly vector with that index
/// when substituted into a vector equation.
/// </summary>
/// <param name="V"></param>
/// <param name="i"></param>
/// <returns></returns>
public double FNCon(D3tuple V, int i)
{ double d = new double(); d = V*Oek[6,modd3(i)]; return d; }
#endregion
/// TetraCoords() <summary>
/// This function returns the tetradhedral coordinates of a point P
/// with respect to the planes of the current AxisSet instance. Each plane
/// is identified by its plane index (see Rho.SetJKL(5,k) for the vertices).
/// </summary>
/// <param name="P">Point for which tetrahedral coordinates are to be calculated</param>
/// <returns>An array of doubles of length 6 containing four coordinate values</returns>
public double[] TetraCoords(D3tuple P)
{
double[] res = new double[6];
X5[5] = P; Ro.r.GetDvalues(X5, 1); double d = Ro.r.D5[5];
if (d != 0)
{
int i = 1;
while (i <= 4)
{
res[i] = Ro.r.D5[i] / d;
i++;
}
}
return res;
}
/// DotProduct() <summary>
/// This function returns the dot product of a vector V with an
/// indexed vector (i=1..3) from the set specified by the o
/// parameter. If o' = kBar(o) then the vector with index i in
/// the set indexed by o' is the vector written with the function
/// value as its coefficient when an equation for V is expressed
/// in terms of basis vectors from the o' set.

3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814

/// </summary>
/// <param name="V">Vector for which a component is to be calculated</param>
/// <param name="o">Basis vector set type from which the scalar product is formed</param>
/// <param name="i">Index of component (1..3)</param>
/// <returns>Component calculated for vector V</returns>
public double DotProduct(D3tuple V, Otyp o, int i)
{
double d = new double(); if (V != null) { int k = modd6((int)o); d = V*Oek[k, modd3(i)]; } return d;
}
/// DotProducts() <summary>
/// This function returns a vector containing the dot products of a
/// vector V with the vectors from the basis set specified by the o
/// parameter. If o' = kBar(o) then the basis vectors are written
/// with the components of the returned vector as their coefficients
/// when an equation for V is expressed in terms of basis vectors
/// from the o' set.
/// </summary>
/// <param name="V">Vector whose components are to be calculated</param>
/// <param name="o">Basis vector set type from which the scalar products are formed</param>
/// <returns>The components of vector V</returns>
public D3tuple DotProducts(D3tuple V, Otyp o)
{
D3tuple res = new D3tuple(); int i = 1,k=modd6((int)o); double[] d = new double[4];
while (i <= 3)
{
//D3tuple v1 = new D3tuple(Oek[k, j].X, Oek[k, j].Y, Oek[k, j].Z);
d[i] = V.X*Oek[k, i].X+V.Y*Oek[k, i].Y+V.Z*Oek[k, i].Z;
i++;
}
res.a4 = d;
return res;
}
/// Component() <summary>
/// This function returns the component associated with an indexed
/// vector (i=1..3) from the set specified by the o parameter when
/// an equation for V is written in terms of basis vectors from the
/// set indexed by o. The component value returned is calculated
/// by forming the dot product of V with the vector with the same
/// index i from the set indexed by o' = kBar(o).
/// </summary>
/// <param name="V"></param>
/// <param name="o"></param>
/// <param name="i"></param>
/// <returns></returns>
public double Component(D3tuple V, Otyp o, int i)
{
double d = new double(); if (V != null) { int k=kbar[modd6((int)o)]; d = V*Oek[k, modd3(i)]; } return d;
}
/// Components() <summary>
/// This function returns the three components of a vector V with respect
/// to the set of axes specified by the Otyp parameter. The results are
/// returned in a D3tuple variable.
/// </summary>
/// <param name="V">Vector whose components are to be calculated</param>
/// <param name="o">The type of axis set (eCo,eCon,eUCo,eUCon,eNCo,eNCon)</param>
/// <returns></returns>
public D3tuple Components(D3tuple V, Otyp o)
{

3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872

D3tuple res = new D3tuple(); int i = 1,k=kbar[modd6((int)o)]; double[] d = new double[4];


while (i <= 3)
{
d[i] = V.X*Oek[k, i].X+V.Y*Oek[k, i].Y+V.Z*Oek[k, i].Z;
i++;
}
res.a4 = d;
return res;
}
#region Metric Tensor values g, gK, and scalars g_ji, g_JI, g_Ji, g_jI
/// g() <summary>
/// This returns the square of the product of the Scalar Triple
/// Products of the three covariant vectors and the three normalized
/// contravariant vectors.
/// </summary>
/// <returns></returns>
public double g()
{ double d = new double(); d = G[1]*G[6]; return d*d; }
/// gK() <summary>
/// This function returns the Scalar Triple Product of the indexed
/// vectors (k=1..3) in the set indexed by k.
/// </summary>
/// <param name="k"></param>
/// <returns></returns>
public double gK(int k)
{ double d = new double(); d = G[modd6(k)]; return d; }
/// g_ji() <summary>
/// This function returns the dot product of two indexed covariant
/// vectors e_i and e_j.
/// </summary>
/// <param name="i"></param>
/// <param name="j"></param>
/// <returns></returns>
public double gij(int i,int j)
{ double d = new double(); d = Oek[1, modd3(i)]*Oek[1, modd3(j)]; return d; }
/// g_JI() <summary>
/// This function returns the dot product of two indexed contravariant
/// vectors, e^i and e^j.
/// </summary>
/// <param name="i"></param>
/// <param name="j"></param>
/// <returns></returns>
public double gIJ(int i,int j)
{ double d = new double(); d = Oek[2, modd3(i)]*Oek[2, modd3(j)]; return d; }
/// g_Ji() <summary>
/// This function returns the dot product of a contravariant vector e^i with
/// index i with a covariant vector e_j with index j. This is formally
/// equivalent to the Kronecker delta function.
/// </summary>
/// <param name="i"></param>
/// <param name="j"></param>
/// <returns></returns>
public double gIj(int i,int j)
{ double d = new double(); d = Oek[2, modd3(i)]*Oek[1, modd3(j)]; return d; }
/// g_jI() <summary>
/// This function returns the dot product of a covariant vector e_i with
/// index i with a contravariant vector e^j with index j. This is formally
/// equivalent to the Kronecker delta function.

3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930

/// </summary>
/// <param name="i"></param>
/// <param name="j"></param>
/// <returns></returns>
public double giJ(int i,int j)
{ double d = new double(); d = Oek[1, modd3(i)]*Oek[2, modd3(j)]; return d; }
/// h_i() <summary>
/// This returns the covariant scale factor for the ith vector. Equivalent to
/// the value returned by h(eCo,i).
/// </summary>
/// <param name="i">Index of vector (1..3)</param>
/// <returns>Covariant scaling factor</returns>
public double hi(int i) { return H[1, modd3(i)]; }
/// h_I() <summary>
/// This returns the contravariant scale factor for the ith vector. Equivalent to
/// the value returned by h(eCon,i).
/// </summary>
/// <param name="i">Index of vector (1..3)</param>
/// <returns>Covariant scaling factor</returns>
public double hI(int i) { return H[2, modd3(i)]; }
/// <summary>
/// This returns the scaling factor for the ith vector from the set
/// specified by the Otyp parameter.
/// </summary>
/// <param name="o">Axis set type: eCo,eCon,eUCo,eUCon,eNCo, or eNCon</param>
/// <param name="i">Vector index (1..3)</param>
/// <returns>Vector scaling factor, equivalent to its basis-specific norm</returns>
public double h(AxisSet.Otyp o, int i) { return H[(int)o, modd3(i)]; }
#endregion
#region AxisSet OPERATORS
public static AxisTriple operator*(AxisSet A1, AxisSet A2)
{
AxisTriple AT = new AxisTriple();
if ((A1 != null) & (A2 != null))
{
int j = 1,i = 1,k = 1; double ee = 0;
int k1 = (int)A1.opType,k2 = (int)A2.opType;
AT.Stype = A1.OpType; AT.Ttype = A2.OpType;
while (j <= 3)
{
i = 1;
while (i <= 3)
{
k = 1; ee = 0;
while (k <= 3)
{
ee = ee + A2.Oek[k2, j].a4[k]*A1.Oek[k1, i].a4[k];
k++;
}
AT.e[j, i] = ee;
i++;
}
j++;
}
}
return AT;
}
/// AND Transform Matrix Operator <summary><para>

3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988

/// The AND operator is designed to create a matrix which transforms components of
/// type Sj.OpType in basis (s) to components of type Ti.OpType in basis (t) when a
/// column vector of components in (s) is premultiplied by the AxisTriple returned
/// by this function.</para><para>
/// </para><para>
/// The OpType fields Stype and Ttype stored in the returned AxisTriple indicate
/// the type of component transformation which the matrix peforms. The actual
/// composition of the matrix is by dot products of Kbar[Stype] (s) basis vectors
/// indexed by j with Ttype (t) basis vectors indexed by i, where i,j is Row,Col
/// indexing.</para>
/// </summary>
/// <param name="S">Input basis AxisSet</param>
/// <param name="T">Output basis AxisSet</param>
/// <returns>Transform matrix for D5RQ.OpType to T.OpType from basis (s) to basis (t)</returns>
public static AxisTriple operator&(AxisSet S, AxisSet T)
{
AxisTriple AT = new AxisTriple();
if ((S != null) & (T != null))
{
int j = 1,i = 1,k = 1; double ee = 0;
int k1 = (int)S.opType,k2 = (int)T.opType;
AT.Stype = S.OpType; AT.Ttype = T.OpType;
k1 = kbar[k1];
while (j <= 3)
{
i = 1;
while (i <= 3)
{
k = 1; ee = 0;
while (k <= 3)
{
ee = ee + T.Oek[k2, j].a4[k]*S.Oek[k1, i].a4[k];
k++;
}
AT.e[j, i] = ee;
i++;
}
j++;
}
}
return AT;
}
#endregion
}
#endregion
#region AxTrans CLASS
public class AxTrans
{
AxisSet Sj = null; AxisSet Ti = null;
double[,] g_ji = null; double[,] g_JI = null; double[,] g_Ji = null; double[,] g_jI = null;
double[] h_i = null; double[] h_I = null; double[] h_j = null; double[] h_J = null;
static int[] kbar = { 0,2,1,6,5,4,3,6,5 };
/// <summary>
/// The AxTrans class has only one constructor, which requires that
/// both of the AxisSets from which the transformation matrices are
/// constructed are specified when the class is instantiated.
/// </summary>
/// <param name="A1">First AxisSet</param>

3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046

/// <param name="A2">Second AxisSet</param>


public AxTrans(AxisSet A1, AxisSet A2)
{
Sj = A1; Ti = A2; Init();
}
void Init()
{
g_ji = new double[4, 4]; g_JI = new double[4, 4]; g_Ji = new double[4, 4]; g_jI = new double[4, 4];
h_i = new double[4]; h_I = new double[4]; h_j = new double[4]; h_J = new double[4];
int i = 1, j = 1;
while (i <= 3)
{
j = 1;
while (j <= 3)
{
g_ji[j,i] = new double(); g_JI[j,i] = new double(); g_Ji[j,i] = new double(); g_jI[j,i] = new double();
j++;
}
h_i[i] = new double(); h_I[i] = new double(); h_j[i] = new double(); h_J[i] = new double();
i++;
}
ReCalc();
}
void ReCalc()
{
int i = 1, j = 1;
while (i <= 3)
{
j = 1;
while (j <= 3)
{
g_ji[j, i] = Sj.Oek[1, j]*Ti.Oek[1, i];
g_JI[j, i] = Sj.Oek[2, j]*Ti.Oek[2, i];
g_Ji[j, i] = Sj.Oek[2, j]*Ti.Oek[1, i];
g_jI[j, i] = Sj.Oek[1, j]*Ti.Oek[2, i];
j++;
}
i++;
}
j = 1;
while (j <= 3)
{
h_j[j] = ~Sj.Oek[1, j]; h_J[j] = ~Sj.Oek[2, j];
h_i[j] = ~Ti.Oek[1, j]; h_I[j] = ~Ti.Oek[2, j];
j++;
}
}
bool iOK(int i) { return ((i >= 1) & (i <= 3)); }
public int kBar(int k) { return kbar[modd6(k)]; }
int modd3(int i) { if (i < 0) i = -i; return ((i-1) % 3)+1; }
int modd6(int i) { if (i < 0) i = -i; return ((i-1) % 6)+1; }
/// Left <summary>
/// This accessor gets and sets the first AxisSet. The name Left indicates
/// that it should be visualized as lying to the left of the 6x6 array of 36
/// matrices and that the index j associated with it and with the rows of
/// the array runs vertically from 1 to 6 downward.
/// </summary>
public AxisSet Left

4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104

{
get { return Sj; }
set { AxisSet AX = value; if (AX != null) { Sj = AX; ReCalc(); } }
}
/// Top <summary>
/// This accessor gets and sets the second AxisSet. The name Top indicates
/// that it should be visualized as lying above the 6x6 array of 36 matrices
/// and that the index i associated with it and with the columns of the array
/// runs horizontally from 1 to 6 rightward.
/// </summary>
public AxisSet Top
{
get { return Ti; }
set { AxisSet AX = value; if (AX != null) { Ti = AX; ReCalc(); } }
}
/// gji[,] <summary>
/// This accessor returns a 3x3 array of scalar products between the
/// covariant vectors of the first axis set, and the covariant vectors
/// of the second axis set, involved in the transformation.
/// The j,i index case is lower for covariant, and upper for contravariant,
/// which by convention defines the case-sensitive name of the accessor.
/// </summary>
public double[,] gji { get { return g_ji; } }
/// gJI[,] <summary>
/// This accessor returns a 3x3 array of scalar products between the
/// contravariant vectors of the first axis set, and the contravariant
/// vectors of the second axis set, involved in the transformation.
/// The j,i index case is lower for covariant, and upper for contravariant,
/// which by convention defines the case-sensitive name of the accessor.
/// </summary>
public double[,] gJI { get { return g_JI; } }
/// gJi[,] <summary>
/// This accessor returns a 3x3 array of scalar products between the
/// contravariant vectors of the first axis set, and the covariant vectors
/// of the second axis set, involved in the transformation.
/// The j,i index case is lower for covariant, and upper for contravariant,
/// which by convention defines the case-sensitive name of the accessor.
/// </summary>
public double[,] gJi { get { return g_Ji; } }
/// gjI[,] <summary>
/// This accessor returns a 3x3 array of scalar products between the
/// covariant vectors of the first axis set, and the contravariant vectors
/// of the second axis set, involved in the transformation.
/// each vector involved in the product.
/// The j,i index case is lower for covariant, and upper for contravariant,
/// which by convention defines the case-sensitive name of the accessor.
/// </summary>
public double[,] gjI { get { return g_jI; } }
/// hi[] <summary>
/// This accessor returns an array of 3 scalar products, which are the
/// squared norms of the covariant basis vectors of the second axis set.
/// </summary>
public double[] hi { get { return h_i; } }
/// hI[] <summary>
/// This accessor returns an array of 3 scalar products, which are the
/// squared norms of the contravariant basis vectors of the second axis set.
/// </summary>
public double[] hI { get { return h_I; } }

4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162

/// hj[] <summary>


/// This accessor returns an array of 3 scalar products, which are the
/// squared norms of the covariant basis vectors of the first axis set.
/// </summary>
public double[] hj { get { return h_j; } }
/// hJ[] <summary>
/// This accessor returns an array of 3 scalar products, which are the
/// squared norms of the contravariant basis vectors of the first axis set.
/// </summary>
public double[] hJ { get { return h_J; } }
void MatCalc(ref AxisTriple T, AxisSet.Otyp sty, AxisSet.Otyp tty, bool trans)
{
int j = 1,i = 1,k = 1; double ee = 0;
int k1 = (int)sty,k2 = (int)tty;
T.Stype = sty; T.Ttype = tty;
if (trans) k1 = Sj.kBar(k1);
while (j <= 3)
{
i = 1;
while (i <= 3)
{
k = 1; ee = 0;
while (k <= 3)
{
if (trans)
{ ee = ee + Sj.Oek[k1, i].a4[k]*Ti.Oek[k2, j].a4[k]; }
else
{ ee = ee + Sj.Oek[k2, j].a4[k]*Ti.Oek[k1, i].a4[k]; }
k++;
}
T.e[j, i] = ee;
i++;
}
j++;
}
}
/// G[,] <summary>
/// This accessor returns 36 matrices with indexes j=1..6 and i=1..6<para>
/// </para><para>
/// These matrices are the Gst matrices composed by dot products between
/// two systems of basis vectors s and t, where s is indexed on j and
/// t is indexed on i, and where j is the row index and i the column index.</para>
/// </summary>
public AxisTriple[,] G
{
get
{
AxisTriple[,] res = new AxisTriple[7, 7];
int j = 1, i = 1;
while (j <= 6)
{
i = 1;
while (i <= 6)
{
res[j, i] = Gmatrix(j, i);
i++;
}
j++;

4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220

}
return res;
}
}
/// Gmatrix() <summary>
/// This function returns one of 36 matrices G[kl]st constructed from the
/// dot products of two basis vector sets s and t which are indexed by
/// rows on j and columns on i, respectively.<para>
/// </para><para>
/// The indexes k,l give the row and column (k=j, l=i) in the 6x6 array of
/// matrices where the desired matrix appears. These indexes correspond to
/// the enumeration eCo,eCon,UCo,UCon,NCo,NCon, and the vector set selected
/// from the 6 sets for each basis involved in constructing the matrix G[kl]
/// is of the type indicated by one of the function parameters.
/// </para>
/// </summary>
/// <param name="j"></param>
/// <param name="i"></param>
/// <returns></returns>
public AxisTriple Gmatrix(int j, int i)
{
AxisTriple res = new AxisTriple(); i = modd6(i); j = modd6(j);
MatCalc(ref res, (AxisSet.Otyp)j, (AxisSet.Otyp)i, false);
return res;
}
/// T[,] <summary>
/// This accessor returns 36 transformation matrices with indexes j=1..6 and i=1..6<para>
/// </para><para>
/// These matrices are the G[kl]st matrices composed by dot products between
/// two systems of basis vectors s and t, where s is indexed on i and
/// t is indexed on j, and where j is the row index and i the column index.</para><para>
/// </para><para>
/// The indexes [kl] refer to input and output component set types, where k,l=1..6</para>
/// <para>
/// The purpose of the index k is to specify the type of component vector to which the
/// AxisTriple matrix will be premultiplied. This causes the matrix returned by this
/// accessor to have been calculated so as to perform that specific conversion of
/// component types from the specified input type to the specified output type.
/// </para>
/// </summary>
public AxisTriple[,] T
{
get
{
AxisTriple[,] res = new AxisTriple[7, 7];
int j = 1, i = 1, k = 1;
while (j <= 6)
{
i = 1;
while (i <= 6)
{
k = kBar(j);
AxisTriple tmp = new AxisTriple();
MatCalc(ref tmp, (AxisSet.Otyp)k, (AxisSet.Otyp)i, true);
res[j, i] = tmp;
i++;
}
j++;

4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278

}
return res;
}
}
/// ST() <summary>
/// This function returns one of 36 transformation matrices, given the
/// input component and output component types. See the T accessor for
/// more information.
/// </summary>
/// <param name="stype">Input component type</param>
/// <param name="ttype">Output component type</param>
/// <returns>Transformation matrix which will premultiply an input vector</returns>
public AxisTriple ST(AxisSet.Otyp stype, AxisSet.Otyp ttype)
{
AxisTriple res = new AxisTriple();
MatCalc(ref res, stype, ttype, true);
return res;
}
/// <summary><para>
/// The DkMatrix function returns a Dmatrix type calculated from
/// the two defining AxisSets of the current AxTrans instance.</para>
/// <para>
/// The transformation matrix from basis S contravariant components to
/// basis T contravariant components is also calculated, and stored in
/// the RQmatrix field, which is of type AxisTriple.</para>
/// <para>
/// The tetrahedral coordinates of the S datum (Sj.P) are stored in the
/// D5T array of the Dmatrix, and the Dk ratios are stored in its D5RQ
/// array. See the following references for further information:
/// </para>
/// http://cqvis.zxq.net/C2/VQC055.html
/// http://cqvis.zxq.net/C2/VQC056.html
/// </summary>
/// <param name="res"></param>
/// <returns></returns>
public Dmatrix DkMatrix(D3tuple D)
{
Dmatrix DM = new Dmatrix(); double d=0;
Sj.P = D; Sj.AxisPts[5] = D;
int i=1,k=1; D3tuple savP = Ti.P;
while (i <= 5)
{
Ti.P = Sj.AxisPts[i];
Ro.r.GetDvalues(Ti.AxisPts, 1);
k = 1;
while (k <= 5)
{
DM.D5RQ[k, i] = Ro.r.D5[k];
k++;
}
i++;
}
int j = 1;
while (j <= 5)
{
k = 1; d = DM.D5RQ[j, j];
if (d != 0)
while (k <= 5)

4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336

{
DM.D5T[j, k] = DM.D5RQ[j, k] / d;
k++;
}
j++;
}
Ti.P = savP;
DM.D5RQcalc();
return DM;
}
}
#endregion
#region Dmatrix CLASS
public class Dmatrix
{
public AxisTriple RQmatrix = null;
public double[,] D5RQ = null; public double[,] D5T = null;
public Dmatrix()
{
Init();
}
void Init()
{
RQmatrix = new AxisTriple(); D5RQ = new double[6, 6]; D5T = new double[6, 6];
int i=1,j=1;
while (i <= 5)
{
j = 1;
while (j <= 5)
{
D5RQ[j, i] = new double(); D5T[j, i] = new double();
j++;
}
i++;
}
}
public void D5RQcalc()
{
int i=1,k=1; double d = D5RQ[5, 1];
if (d == 0) return;
while (i <= 3)
{
k = 1;
while (k <= 3)
{
double d2 = D5RQ[k+1, i+1] - D5RQ[k+1, i];
RQmatrix.e[k, i] = d2 / d;
k++;
}
i++;
}
}
}
#endregion
#region Rotator CLASS
/// <summary><para>
/// The Rotator class uses the double numeric type, which is an 8-byte floating-point format

4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394

/// having more than 15 decimal digits of precision. The radial measurements of points rotated
/// by the Rotator/RAxis/RAngle methods indicate that variations from 1E-16 to 9E-16 are common
/// when the radius returned is different from the radius to the unrotated point; m.e., the
/// difference is not exactly zero. How quickly cumulative error increases during iterative
/// multi-increment rotations applied to a successively to a point has not been measured.
/// However, the Preview() function can be used to get the coordinates of the endpoint of an arc
/// before the iterations along that arc begin, and the result compared with the endpoint of the
/// iteration if the amount of error needs to be determined.</para>
/// <para>
/// Use of the Rotator class at its simplest requires only a call to the Rotator() default
/// constructor; m.e., Rotator R3 = new Rotator(); This provides both an RAxis instance (in
/// R3.Axis) and an RAngle instance (in R3.Angle), which is sufficient to support all of the
/// calls to Rotator methods. For handling multiple trajectories, concatenated rotations with
/// different directions, or rotations about different points or axes, an RAxis instance can
/// be allocated directly by RAxis RX3[c] = new RAxis() and then RX3[c].SetLocation() and
/// RX3[c].SetDirection() called to orient the rotation axis for each case; and an RAngle
/// instance can be allocated directly by RAngle RA3[j] = new RAngle(RX3[c],Theta[j]), which
/// would provide a complete series of rotations which could be carried out by calls to
/// RA3[j].Rotate(pts[u].X,pts[u].Y,pts[u].Z) for any collection of points u = n1..n2 from
/// an array.</para>
/// <para>
/// The F3xyz accessors get and set float values, which have 7 decimal digits of precision,
/// and the integer type get and set accessors I3xyz and U3xyz have 0 digits of precision
/// and dynamic ranges of roughly [+|-]2^31 and 2^16 respectively. The I3xyz and U3xyz
/// accessors are provided only for use in converting to and from graphics world coordinates
/// and care should be taken not to allow inaccuracies resulting from their use to propagate
/// into successive applications of the rotation methods.</para>
/// <para>
/// The Microsoft.WindowsMobile.DirectX namespace contains classes and methods implementing
/// a matrix-oriented interface for 3-dimensional rotations, the System.Windows.Media.Media3D
/// namespace contains sophisticated 3-D rendering and animation methods, System.Windows.Media.
/// Imaging contains methods for 2-D rotations of bitmaps by multiples of 90 degrees, the GDI
/// functions provide for transformation of world coordinates in 2-D which can include rotation
/// as well as translation, and System.Windows.Controls provides a 3-D graphics context for
/// visuals. But if you only want to rotate points and don't need these interfaces for object
/// manipulation there isn't any simple, straighforward way to do that using their methods.
/// The Rotator class rotates points only; it simplifies axis, angle, and point definition
/// and data access; and it carries out the operations quickly. So it's a no-frills direct
/// rotation interface which doesn't need any graphics support at all, and can run in console
/// text mode as well as in bare-bones Windows Forms, without support from any of the DLLs
/// beyond what your application would otherwise require. That's its modus operandi, and
/// its raison D'etre is combined simplicity and speed.</para>
/// </summary>
public class Rotator
{
public const int XAxis = 1, YAxis = 2, ZAxis = 3, YZplane = 1, ZXplane = 2, XYplane = 3;
public double[,] AxisPlanes = {{1.0,1.0,1.0},{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0}};
public D3tuple[] Axes = null;
bool valid=false,previewing=false; int planeNo=0;
double X,Y,Z,oldX,oldY,oldZ,newX,newY,newZ,prvX,prvY,prvZ;
public RAngle Angle=null,PVAngle=null;
public RAxis Axis=null;
public Rotator()
{
Axis = new RAxis();
if (Axis != null)
{
Angle = new RAngle(Axis); if (Angle != null)

4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452

{ valid = true; ZeroRef(false);


PVAngle = new RAngle(Axis);
}
}
Axes = new D3tuple[4];
for (int i=0; (i <=3); i++)
{
Axes[i] = new D3tuple(AxisPlanes[i, 0], AxisPlanes[i, 1], AxisPlanes[i, 2]);
}
}
#region POINT ROTATION METHODS
/// Spin() <summary>
/// For any given point, call SetXYZ() or another method to set the X,Y,Z fields before calling Spin().
/// Each subsequent call to Spin() will rotate the point in X,Y,Z through successive rotations, and
/// if the rotation axis is unchanged the series of points will describe a circle. After N rotations,
/// a call to GetXYZ() will return a point X,Y,Z which has been rotated through an angle N*Theta,
/// where Theta is the angle initially set before calling Spin().
/// </summary>
public void Spin()
{
if ((valid)&(Angle != null)) { Angle.Rotate(ref X, ref Y, ref Z); }
}
/// ResetAngle() <summary>
/// Calling ResetAngle() before Spin() causes the new angle to be used by Spin(). Thus, if you set
/// the rotation angle to Pi/6, call Spin() twice and then ResetAngle(-Pi/3), the next Spin() call
/// will undo the two preceding rotations.
/// </summary>
/// <param name="Theta"></param>
public void ResetAngle(double Theta)
{
if ((valid) & (Angle != null))
{
if (Angle.Valid)
{
ZeroRef(false); Angle.SetAngle(Theta);
}
}
}
/// SetAxis() <summary>
/// SetAxis() sets the location and direction of the axis for the default RAxis instance
/// associated with this Rotator instance. If both the Location and Direction arguments
/// supplied to SetAxis() are valid and the Direction vector is nonzero, the values needed
/// by this Rotator instance are calculated and stored in the Rotator.Axis instance.
/// </summary>
/// <param name="Location"></param>
/// <param name="Direction"></param>
public void SetAxis(D3tuple Location, D3tuple Direction)
{
if (Axis == null) return; Axis.LocationOK = false; Axis.DirectionOK = false;
if (Location != null) { Axis.SetLocation(Location.X, Location.Y, Location.Z); }
if (Direction != null) { Axis.SetDirection(Direction.X, Direction.Y, Direction.Z); }
}
/// SetAngle() <summary>
/// SetAngle() does little more than calculate the coefficients depending on the rotation angle,
/// storing them internally in the RAngle class instance associated with this Rotator. Multiple
/// angle coefficients can be precalculated and stored in separate RAngle class instances by passing
/// a reference to an RAxis instance to the RAngle constructors (optionally with an included angle).
/// It is also possible to assign an RAxis instance to RAngle.Axis and then call RAngle.SetAngle()

4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510

/// to calculate these values (and this could be immediately followed by a call to RAngle.Rotate()
/// to perform a rotation about the chosen axis of an arbitrary point).
/// </summary>
/// <param name="Theta"></param>
public void SetAngle(double Theta)
{
if (Angle == null) return;
Angle.SetAngle(Theta);
}
/// SetAxisPlane() <summary>
/// SetAxisPlane() is passed one of the values XAxis, YAxis, Xaxis, YZplane, ZXplane, or XYplane
/// to orient the rotation axis to one of the principal Cartesian axes and normal to its axis plane.
/// The axis affected is this Rotator class Axis instance.
/// </summary>
/// <param name="plane">Integer index of axis (0..3)</param>
public void SetAxisPlane(int plane)
{
if (Axis == null) return;
if ((plane <= 3) & (plane >= 0))
{
planeNo = plane;
Axis.AxDirX = AxisPlanes[plane, 0];
Axis.AxDirY = AxisPlanes[plane, 1];
Axis.AxDirZ = AxisPlanes[plane, 2];
}
}
/// Increment() <summary>
/// Successive calls to Increment() will perform successive rotations of the point X,Y,Z which
/// is defined before the first call. The rotated points are returned by GetXYZ() and the
/// other accessors (D3xyz, etc.).
/// </summary>
/// <param name="Theta"></param>
public void Increment(double Theta)
{
if ((valid) & (Angle != null))
{
if (Angle.Valid)
{
Angle.SetAngle(Theta);
Angle.Rotate(ref X, ref Y, ref Z);
}
}
}
/// ZeroRef() <summary><para>
/// The ZeroRef() function does not 'zero' any fields. What it does is to set both the old and new
/// points to the current point X,Y,Z in this Rotator instance. ZeroRef(true) sets X,Y,Z to the last
/// point calculated by Preview() before doing so.</para>
/// <para>
/// Calling ZeroRef(false) before Preview() does not change the current X,Y,Z values, so you can call
/// these two functions multiple times without changing the current point. Calling ZeroRef(true) will
/// cause Preview() to rotate starting at the current Preview point and to return a new Preview point
/// via GetXYZ(), and a second call to GetXYZ then will return the latest value stored by X,Y,Z which
/// is now the Preview point which the rotation started from.</para>
/// <para>
/// This allows you to utilize a preview point as the starting point for another rotation instead of
/// just reading the value for reference as you would when ZeroRef() is not called or ZeroRef(false)
/// is called. Sequences of ZeroRef(true); Preview(Theta); will then advance the rotation just as
/// Spin() does. The only differences are that the Old and New point fields are loaded with new values

4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568

/// and that you can read both the preview point and the start point (in that order) by two calls to
/// GetXYZ() after Preview() returns.</para>
/// </summary>
public void ZeroRef(bool Preview)
{
if (Preview) { X = prvX; Y = prvY; Z = prvZ; }
oldX = X; oldY = Y; oldZ = Z; newX = X; newY = Y; newZ = Z;
}
/// Preview() <summary><para>
/// Call ZeroRef() after calling Preview() to set the current X,Y,Z as a reference datum from which
/// the rotated point calculated by Preview() will be advanced by the angle Theta. If you call ZeroRef()
/// before each of several successive calls to Preview() the datum and the calculated point will both
/// advance by the angle of rotation. This is almost equivalent to calling Spin() multiple times,
/// except for the fact that the datum does not advance until ZeroRef() is called.</para>
/// <para>
/// If you need a lookahead for accuracy measurement when incrementing by small angles using Spin()
/// you can call ZeroRef() and then Preview() beforehand to calculate a point which you should reach
/// after a cumulative angle of displacement has been reached; and comparing the point coordinates
/// as previewed with those of the point actually reached by multiple Spin() calls will give you a
/// good measure of whether cumulative error is influencing the Spin() cycle. This is especially
/// important when using integer, rather than floating-point, arguments as input to these functions.</para>
/// <para>
/// Calling GetXYZ(), D3xyz, F3xyz, U3xyz, or I3xyz immediately after Preview() returns the preview
/// point rather than the normal rotated point X,Y,Z. As long as Previewing returns true you can
/// still read this value at least once; after that you need to set Previewing=true before calling
/// one of these functions in order to obtain that point, because calls to these functions reset
/// the Previewing flag to false;</para>
/// </summary>
/// <param name="Theta"></param>
public void Preview(double Theta)
{
if (Angle == null) return;
if ((valid) & (PVAngle != null))
{
if (Angle.Axis == null) return;
if (PVAngle.Valid)
{
PVAngle.Axis = Angle.Axis;
PVAngle.SetAngle(Theta); prvX = X; prvY = Y; prvZ = Z;
PVAngle.Rotate(ref prvX, ref prvY, ref prvZ); previewing = true;
}
}
}
public bool Previewing
{
get { return previewing; }
set { previewing = value; }
}
#endregion
#region GET/SET POINT METHODS
public double OldX { get { return oldX; } set { oldX = value; } }
public double OldY { get { return oldY; } set { oldY = value; } }
public double OldZ { get { return oldZ; } set { oldZ = value; } }
public double NewX { get { return newX; } set { newX = value; } }
public double NewY { get { return newY; } set { newY = value; } }
public double NewZ { get { return newZ; } set { newZ = value; } }
public void SetXYZ(double inX, double inY, double inZ) { X = inX; Y = inY; Z = inZ; }
public void GetXYZ(ref double inX, ref double inY, ref double inZ)

4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624

{
if (previewing) { inX = prvX; inY = prvY; inZ = prvZ; previewing = false; }
else { inX = X; inY = Y; inZ = Z; }
}
public D3tuple XYZ
{
get
{
D3tuple d3 = new D3tuple();
if (previewing) { d3.X = prvX; d3.Y = prvY; d3.Z = prvZ; previewing =false; }
else { d3.X = X; d3.Y = Y; d3.Z = Z; }
return d3;
}
set { D3tuple d3 = value; X = d3.X; Y = d3.Y; Z = d3.Z; }
}
public F3tuple F3xyz
{
get
{
F3tuple f3 = new F3tuple();
if (previewing) { f3.X = (float)prvX; f3.Y = (float)prvY; f3.Z = (float)prvZ; previewing = false; }
else { f3.X = (float)X; f3.Y = (float)Y; f3.Z = (float)Z; }
return f3;
}
set { F3tuple F3 = value; X = (double)F3.X; Y = (double)F3.Y; Z = (double)F3.Z; }
}
public I3tuple I3xyz
{
get
{
I3tuple i3 = new I3tuple();
if (previewing) { i3.X = (int)Math.Round(prvX); i3.Y = (int)Math.Round(prvY); i3.Z = (int)Math.Round(prvZ); previewing =
false; }
else { i3.X = (int)Math.Round(X); i3.Y = (int)Math.Round(Y); i3.Z = (int)Math.Round(Z); }
return i3;
}
set { I3tuple i3 = value; X = i3.X; Y = i3.Y; Z = i3.Z; }
}
public US3tuple U3xyz
{
get
{
US3tuple u3 = new US3tuple();
if (previewing) { u3.X = (ushort)Math.Round(prvX); u3.Y = (ushort)Math.Round(prvY); u3.Z = (ushort)Math.Round(prvZ);
previewing=false; }
else { u3.X = (ushort)Math.Round(X); u3.Y = (ushort)Math.Round(Y); u3.Z = (ushort)Math.Round(Z); }
return u3;
}
set { US3tuple u3 = value; X = u3.X; Y = u3.Y; Z = u3.Z; }
}
#endregion
public D3tuple[] OrthoAxes()
{
D3tuple[] res = new D3tuple[5];
res[1] = new D3tuple(0, 0, 0);
res[2] = new D3tuple(1, 0, 0); res[3] = new D3tuple(0, 1, 0); res[4] = new D3tuple(0, 0, 1);
return res;
}

4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682

/// NormalAxis <summary>


/// If you need to find an arbitrary rotation axis and know the coordinates of at least three points
/// in a plane or know two vectors lying in that plane or parallel to it, you can call NormalAxis()
/// to find the axis for rotation in that plane. For three points, call GetNormal() to return the
/// plane normal vector. If unit=true, the vector returned will be a SignOnly vector.
/// </summary>
/// <param name="D">First of two vectors defining the plane</param>
/// <param name="D2">Second of two vectors defining the plane</param>
/// <param name="res">Returns the plane normal vector</param>
/// <param name="unit"></param>
public void NormalAxis(D3tuple D1, D3tuple D2, ref D3tuple D, bool unit)
{
double d3x=0,d3y=0,d3z=0;
CrossProd(D1.X,D1.Y,D1.Z,D2.X,D2.Y,D2.Z, out d3x, out d3y, out d3z);
if (unit) ScaleVec(d3x,d3y,d3z,1, out d3x, out d3y, out d3z);
D.X = d3z; D.Y = d3y; D.Z = d3z;
}
/// GetNormal <summary>
/// Given three points in the D3tuple instances u1, u2, D, GetNormal() returns a
/// D3tuple containing the normal vector to the plane containing those three points.
/// If unit=true it returns a SignOnly normal vector.
/// </summary>
/// <param name="D">First point in plane</param>
/// <param name="D2">Second point in plane</param>
/// <param name="D3">Third point in plane</param>
/// <param name="unit">True if a unit normal is to be returned</param>
/// <returns>Normal vector of plane containing points D,D2,D3</returns>
public D3tuple GetNormal(D3tuple D1, D3tuple D2, D3tuple D3, bool unit)
{
D3tuple D = new D3tuple();
D=D3Sum(D3CrossProd(D1,D2),D3CrossProd(D2,D3),1); D=D3Sum(D3CrossProd(D3,D1),D,1);
if (unit) { D3Scale(ref D, 1); }
return D;
}
/// GetVv() <summary>
/// This returns both the Cyclic Vector Product Sum and Scalar Triple Product for three Double3tuples.
/// </summary>
/// <param name="D">First vector</param>
/// <param name="D2">Second vector</param>
/// <param name="D3">Third vector</param>
/// <param name="stp">Return value of STP</param>
/// <param name="res">Return value of CVPS</param>
public void GetVv(D3tuple D1, D3tuple D2, D3tuple D3, ref double stp, ref D3tuple D)
{
if ((D1 != null) & (D2 != null) & (D3 != null) & (D != null))
{
D=D3Sum(D3CrossProd(D1, D2), D3CrossProd(D2, D3), 1); D=D3Sum(D3CrossProd(D3, D1), D, 1);
stp = D3STP(D1, D2, D3);
}
}
/// D3CrossProd <summary>
/// This returns a D3tuple containing the vector (cross) product of u1 and u2.
/// </summary>
/// <param name="D"></param>
/// <param name="D2"></param>
/// <returns>Vector product of vectors D and D2</returns>
public D3tuple D3CrossProd(D3tuple D1, D3tuple D2)
{

4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740

D3tuple D = new D3tuple(); double d3x=0,d3y=0,d3z=0;


CrossProd(D1.X,D1.Y,D1.Z,D2.X,D2.Y,D2.Z, out d3x, out d3y, out d3z);
D.X = d3x; D.Y = d3y; D.Z = d3z;
return D;
}
/// D3DotProd <summary>
/// This returns a scalar which is the inner (dot) product of vectors D and D2.
/// </summary>
/// <param name="D"></param>
/// <param name="D2"></param>
/// <returns></returns>
public double D3DotProd(D3tuple D1, D3tuple D2)
{
double d = 0;
if ((D1 != null) & (D2 != null)) d = D1.X*D2.X+D1.Y*D2.Y+D1.Z*D2.Z;
return d;
}
/// D3STP <summary>
/// This returns the Scalar Triple Product (STP) of three vectors D, D2, and D3.
/// </summary>
/// <param name="D">First vector</param>
/// <param name="D2">Second vector</param>
/// <param name="D3">Third vector</param>
/// <returns>The scalar triple product of three vectors</returns>
public double D3STP(D3tuple D1, D3tuple D2, D3tuple D3)
{
double d = 0;
if ((D1 != null) & (D2 != null) & (D3 != null)) d = D3DotProd(D1, D3CrossProd(D2, D3));
return d;
}
// TABLE OF RhoJK INDEX PERMUTATIONS
//
1
2
3
4
5
// 1 --- 345 254 235 243
// 2 354 --- 145 153 134
// 3 245 154 --- 125 142
// 4 253 135 152 --- 123
// 5 234 143 124 132 --// http://cqvis.zxq.net/C1/VQC012T.html
//
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///
///

D3MISTP <summary><para>
D3MISTP() returns the Scalar Triple Product (STP) of three vectors from a set of 5 indexed by
the permutation selector ptOn(m,i)= jkl</para><para></para>
<para>
This quantity is formally known as the origin distance of the plane containing the three points
located by those vectors.</para>
<para>
Other more compact routines are present in this class. This one is included for debugging
purposes to allow stepping through individual applications of the STP to a set of vectors
while using debugger quick-tips to examine the vector values. For those unfamiliar with
the Rho3 indexing scheme, this might avoid confusion over which vectors from the set of 5
for each value of the indexes (m,i) are used in forming the product.
</para>
</summary>
<param name="D">Vector v1</param>
<param name="D2">Vector v2</param>
<param name="D3">Vector v3</param>
<param name="D4">Vector v4</param>

4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798

/// <param name="D5">Vector v5</param>


/// <param name="m">Sphere index</param>
/// <param name="i">Tetrahedron vertex index</param>
/// <returns></returns>
public double D3MISTP(D3tuple D1, D3tuple D2, D3tuple D3, D3tuple D4, D3tuple D5, int m, int i)
{
double res=0;
m = ((m-1)%5)+1; i = ((i-1)%5)+1;
if (m == 1)
{
if
(i==2) res = D3STP(D3, D4, D5); // Rho3(1,2) = 345
else if (i==3) res = D3STP(D2, D5, D4); // Rho3(1,3) = 254
else if (i==4) res = D3STP(D2, D3, D5); // Rho3(1,4) = 235
else if (i==5) res = D3STP(D2, D4, D3); // Rho3(1,5) = 243
}
else if (m==2)
{
if
(i==1) res = D3STP(D3, D5, D4); // Rho3(2,1) = 354
else if (i==3) res = D3STP(D1, D4, D5); // Rho3(2,3) = 145
else if (i==4) res = D3STP(D1, D5, D3); // Rho3(2,4) = 153
else if (i==5) res = D3STP(D1, D3, D4); // Rho3(2,5) = 134
}
else if (m==3)
{
if
(i==1) res = D3STP(D2, D4, D5); // Rho3(3,1) = 245
else if (i==2) res = D3STP(D1, D5, D4); // Rho3(3,2) = 154
else if (i==4) res = D3STP(D1, D2, D5); // Rho3(3,4) = 125
else if (i==5) res = D3STP(D1, D4, D2); // Rho3(3,5) = 142
}
else if (m==4)
{
if
(i==1) res = D3STP(D2, D5, D3); // Rho3(4,1) = 253
else if (i==2) res = D3STP(D1, D3, D5); // Rho3(4,2) = 135
else if (i==3) res = D3STP(D1, D5, D2); // Rho3(4,3) = 152
else if (i==5) res = D3STP(D1, D2, D3); // Rho3(4,5) = 123
}
else if (m==5)
{
if
(i==1) res = D3STP(D2, D3, D4); // Rho3(5,1) = 234
else if (i==2) res = D3STP(D1, D4, D3); // Rho3(5,2) = 143
else if (i==3) res = D3STP(D1, D2, D4); // Rho3(5,3) = 124
else if (i==4) res = D3STP(D1, D3, D2); // Rho3(5,4) = 132
}
return res;
}
/// D3MIvec <summary><para>
/// D3MIvec() returns the Cyclic Vector Product Sum (CVPS) of three vectors from a set of 5 indexed by
/// the permutation selector ptOn(m,i)= jkl</para>
/// <para>
/// This vector sum is formally known as the Vector Triple Product of a set of three vectors, but here
/// the concept of the cyclic sum is extended to four or more vectors (although only three vectors are
/// actually relevant to the D3MIvec() routine).</para>
/// </summary>
/// <param name="D"></param>
/// <param name="D2"></param>
/// <param name="D3"></param>
/// <param name="D4"></param>
/// <param name="D5"></param>

4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856

/// <param name="m"></param>


/// <param name="i"></param>
/// <returns></returns>
public D3tuple D3MIvec(D3tuple D1, D3tuple D2, D3tuple D3, D3tuple D4, D3tuple D5, int m, int i)
{
D3tuple Dres= new D3tuple(); m = ((m-1)%5)+1; i = ((i-1)%5)+1;
if (m == 1)
{
if
(i==2) Dres = GetNormal(D3, D4, D5, false); // Rho3(1,2) = 345
else if (i==3) Dres = GetNormal(D2, D5, D4, false); // Rho3(1,3) = 254
else if (i==4) Dres = GetNormal(D2, D3, D5, false); // Rho3(1,4) = 235
else if (i==5) Dres = GetNormal(D2, D4, D3, false); // Rho3(1,5) = 243
}
else if (m==2)
{
if
(i==1) Dres = GetNormal(D3, D5, D4, false); // Rho3(2,1) = 354
else if (i==3) Dres = GetNormal(D1, D4, D5, false); // Rho3(2,3) = 145
else if (i==4) Dres = GetNormal(D1, D5, D3, false); // Rho3(2,4) = 153
else if (i==5) Dres = GetNormal(D1, D3, D4, false); // Rho3(2,5) = 134
}
else if (m==3)
{
if
(i==1) Dres = GetNormal(D2, D4, D5, false); // Rho3(3,1) = 245
else if (i==2) Dres = GetNormal(D1, D5, D4, false); // Rho3(3,2) = 154
else if (i==4) Dres = GetNormal(D1, D2, D5, false); // Rho3(3,4) = 125
else if (i==5) Dres = GetNormal(D1, D4, D2, false); // Rho3(3,5) = 142
}
else if (m==4)
{
if
(i==1) Dres = GetNormal(D2, D5, D3, false); // Rho3(4,1) = 253
else if (i==2) Dres = GetNormal(D1, D3, D5, false); // Rho3(4,2) = 135
else if (i==3) Dres = GetNormal(D1, D5, D2, false); // Rho3(4,3) = 152
else if (i==5) Dres = GetNormal(D1, D2, D3, false); // Rho3(4,5) = 123
}
else if (m==5)
{
if
(i==1) Dres = GetNormal(D2, D3, D4, false); // Rho3(5,1) = 234
else if (i==2) Dres = GetNormal(D1, D4, D3, false); // Rho3(5,2) = 143
else if (i==3) Dres = GetNormal(D1, D2, D4, false); // Rho3(5,3) = 124
else if (i==4) Dres = GetNormal(D1, D3, D2, false); // Rho3(5,4) = 132
}
return Dres;
}
/// GetD3M5Ivec <summary>
/// For a D3tuple array res[] of points, this returns a vector M(k,K) calculated from three of
/// the five elements of res[] starting at the index offs, where k,K = 1..5 and offs is less than
/// res.Length+1 - 4.
/// </summary>
/// <param name="res">Array of at least 5 D3tuple vectors</param>
/// <param name="offs">Offset into the array res[] of the first vector of the five</param>
/// <param name="k">Row index of the MI indexer</param>
/// <param name="K">Column index of the MI indexer</param>
/// <param name="O">The output vector is returned in this D3tuple</param>
public void GetD3M5Ivec(D3tuple[] D, int offs, int J, int K, out D3tuple O)
{
int lim = offs + 4; O = null; if ((D == null) | (J == K)) return;
if ((lim < D.Length)&(lim >= 4))
{

4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914

O = new D3tuple(); byte[] B = RhoJK(5, J, K); int i = offs-1;


O = GetNormal(D[B[3]+i], D[B[4]+i], D[B[5]+i],false);
}
}
#region [*] METHODS REQUIRING D5/F5/G5/Vjk/vjk/LVjk/LsqXi/Radii/Spheres ARRAYS
// http://cqvis.zxq.net/C1/VQC011P2.html
// http://cqvis.zxq.net/C3/VQC100.html
//
/// [*] D5mIinit() <summary><para><para>[*]</para>
/// The methods in this section (marked with [*]) require use of the arrays
/// D5[], F5[], G5[], Spheres[], Radii[], Vjk[,], vjk[,], LVjk[,], or LsqXi[],
/// which store intermediate results for use in an extended series of calculations
/// involving the same set of five vectors and the plane normals they define.</para>
/// <para>
/// D5MIinit() is called by these routines if you do not explicitly do so.
/// Each of these arrays is of length 6 and the elements used are 1..5,
/// with element 0 being null in the case of F5, Spheres, and Vjk. You
/// are free to use any element with a zero index as you wish to store
/// intermediate or final results, because this class does not contain
/// any methods which will reference those entries.
/// </para>
/// </summary>
public void D5MIinit()
{
if (D5initialized) return;
int i = 1,k = 1; bool ok = true;
while ((ok) & (i <= 5))
{
F5[i] = new D3tuple(); if (F5[i] == null) ok = false;
if (ok)
{
Spheres[i] = new D3tuple(); if (Spheres[i] == null) ok = false;
if (ok) { Inspheres[i] = new D3tuple(); if (Inspheres[i] == null) ok = false; }
if (ok) { Xi[i] = new D3tuple(); if (Xi[i] == null) ok = false; }
}
k = 1;
if (Vjk != null)
while ((ok) & (k <= 5))
{
Vjk[i, k] = new D3tuple(); if (Vjk[i, k] == null) ok = false; else k++;
}
i++;
}
if (!FactTblValid) FactTblInit(5);
D5initialized = ok;
}
/// GetDvalues() <summary><para>
/// The call to GetD3MIvecs() calculates values for the following arrays of
/// vectors and scalars which are used in further tetrahedron, sphere, plane,
/// and vector calculations. Among them (array lengths are in brackets) are:</para><para>
/// D5[6] - (scalar) 4x4 matrix determinants (DA^5)_k proportional to tetrahedral volumes</para><para>
/// F5[6] - (vector) non-normalized direction vectors through curcumsphere centers</para><para>
/// G5[6] - (scalar) sums of lengths of face normals for each tetrahedron</para><para>
/// Spheres[6] - (vector) Tetrahedron circumsphere center locations</para><para>
/// Inspheres[6] - (vector) Tetrahedron insphere center locations</para><para>
/// Radii[6] - (scalar) Tetrahedron circumsphere radii</para><para>
/// Inradii[6] - (scalar) Tetrahedron insphere radii</para><para>
/// Vjk[6,6] - (vector) Indexed face normals; 10 directed inward, 10 outward</para><para>

4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972

/// vjk[6,6] - (scalar) Indexed Scalar Triple Products of vertices for all faces of all tetrahedra</para><para>
/// LVjk[6,6] - (scalar) Lengths of the indexed face normals</para><para>
/// LsqXi[6] - (scalar) Lengths of the vertex position vectors</para><para>
/// Xi[6] - (vector) Vertex position vectors</para>
/// </summary>
public void GetDvalues(D3tuple[] D, int offs)
{
if (D == null) return;
if (!D5initialized) D5MIinit();
int L = D.Length,i=offs,j=1,j2=1,k=1;
if (i+4 < L)
{
k = 1; D3penta P = new D3penta(D, i);
while ((k <= 5) & (i < L))
{
Degeneracies[k] = 0;
// Xi[k] LsqXi[k]
Xi[k] = D[i]; LsqXi[k] = Xi[k]*Xi[k];
// Vjk[k,j] vjk[k,j] LVjk[k,j]
j=1;
while (j <= 5)
{
if (j != k)
{
Vjk[k, j] = P.vMoI(k, j); LVjk[k, j] = Vjk[k, j]*Vjk[k, j];
vjk[k,j] = P.MoI(k, j);
}
j++;
}
k++; i++;
}
k = 1; i = offs; D3tuple[] CK = new D3tuple[6];
while ((k <= 5) & (i < L))
{
// D5[k] F5[k] G5[k]
j = 1; D5[k] = 0; G5[k] = 0; F5[k].Zero(); CK[k] = new D3tuple();
while (j <= 5)
{
if (j != k)
{
D5[k] = D5[k] + vjk[k, j]; F5[k] = F5[k] + LsqXi[j]*Vjk[k, j];
G5[k] = G5[k] + LsqXi[j]*vjk[k, j]; CK[k] = LVjk[k, j]*Xi[k];
}
j++;
}
i++; k++;
}
k = 1; i = offs; j = 1;
while ((k <= 5) & (i < L))
{
// Spheres[k] Inspheres[k] Radii[k] Inradii[k]
Spheres[k] = (-1/(2.0*D5[k]))*F5[k];
if (LVjk[k, j] != 0) Inspheres[k] = CK[k] / LVjk[k, j];
j = k+1; if (j > 5) j = 1;
Radii[k] = ~(Xi[j] - Spheres[k]);
if (D5[k] == 0) Degeneracies[k] = SphereIsPlane;
else if (Math.Abs(D5[k]) < Eta)
{

4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030

if (Degeneracies[k] == 0) Degeneracies[k] = SphereMayBeTooLarge;


}
else if (Radii[k] == 0)
{
if (Degeneracies[k] == 0) Degeneracies[k] = SphereIsPoint;
}
else if (Math.Abs(Radii[k]) < Eta)
{
if (Degeneracies[k] == 0) Degeneracies[k] = SphereMayBeTooSmall;
}
j2 = 1;
while ((j2 == j) | (j2 == k)) { j2++; }
Inradii[k] = ~ProjSin(Xi[j2]-Inspheres[k], Vjk[k, j]);
k++; i++;
}
}
}
public bool D5initialized = false;
public double[] D5 = new double[6]; //double[] D5STP = new double[6];
public D3tuple[] F5 = new D3tuple[6];
public double[] G5 = new double[6]; //double[] LV5i = new double[6];
public D3tuple[] Spheres = new D3tuple[6];
public D3tuple[] Inspheres = new D3tuple[6];
public double[] Radii = new double[6];
public double[] Inradii = new double[6];
public D3tuple[,] Vjk = new D3tuple[6, 6];
public double[,] vjk = new double[6, 6];
public double[,] LVjk = new double[6, 6];
public double[] LsqXi = new double[6];
public D3tuple[] Xi = new D3tuple[6];
/// <summary>
/// Eta is an adjustable semi-constant which you can use as a limit
/// on how small 'vanishingly small' quantities have to be before you
/// benignly consider them as effectively zero. Most of the calculations
/// involving rotations produce variances from the ideal which are smaller
/// than this, and the demonstrations that quantities are zero which will
/// hold as such in algebraic equations won't need to be spoiled if you
/// trap for these variations, recalculate if necessary when variances
/// become larger than Eta, and find ways of reducing accumulated error.
/// </summary>
public double Eta = 1.0E-12;
public const int SphereIsPlane=-1,SphereIsPoint=-2,SphereMayBeTooSmall=1,SphereMayBeTooLarge=2;
public int SphereDegeneracy = 0;
public int[] Degeneracies = new int[6];
/// [*] Dk <summary><para>[*] </para>
/// Dk() returns the row 1 cofactor in column c of the matrix [ m1 m2 m3 m4 m5 ]
/// where mi = { 1 (xi*xi) xi1 xi2 xi3 } given the input vectors u1..D4 which are
/// modulus-index-consecutive in the matrix. Thus, to return cofactor c=2 you
/// would call Dk(D,D4,D5,u1); for c=3, Dk(D4,D5,u1,u2); etc.
/// </summary>
/// <param name="D"></param>
/// <param name="D2"></param>
/// <param name="D3"></param>
/// <param name="D4></param>
/// <returns></returns>
public double Dk(D3tuple D1, D3tuple D2, D3tuple D3, D3tuple D4)
{
double res = 0;

5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088

if ((D1 != null) & (D2 != null) & (D3 != null) & (D4 != null))
{
res = -D3DotProd(D1,GetNormal( D2, D3, D4, false)) + D3STP(D2, D3, D4);
}
return res;
}
/// [*] GetD3M5Ivecs <summary><para>[*] </para>
/// For a D3tuple array res[] of points, this returns five vectors M(index,u) calculated from
/// five sets of three of the five elements of res[] starting at the index offs, where u = 1..5 and
/// offs is less than res.Length+1 - 4. The output array O[] contains vectors M(index,1), M(index,2),
/// ...,M(index,5) with the exception of M(index,index) which is undefined (and that element of O[]
/// will be the zero vector).<para>
/// Note that all of the Rotator storage arrays, F5[], G5[], Vjk[,], etc. will be given new values
/// by a call to this routine. The array O[] in which the face normal vectors are returned may
/// then be used in any fashion, and its values restored to those produced by GetD3MIvecs() by
/// retrieving the values for j=1..5 from the Vjk[k,j] array.
/// </para>
/// </summary>
/// <param name="res">Array of at least 5 D3tuple vectors</param>
/// <param name="offs">Offset into the array res[] of the first vector of the five</param>
/// <param name="index">Row index of the MI indexer</param>
/// <param name="O">The output vectors are returned in this D3tuple array</param>
public void GetD3M5Ivecs(D3tuple[] D, int offs, int index, out D3tuple[] O)
{
int lim = offs + 4; O = null; if (D == null) return;
if (index > 5) index = 5; if (index < 1) index = 1;
if (!D5initialized) D5MIinit();
if ((lim < D.Length)&(lim >= 4))
{
int i = offs-1; int k = 1;
D3tuple[] DO = new D3tuple[6];
while (k <= 5)
{
DO[k] = new D3tuple();
DO[k] = D[i];
k++; i++;
}
GetDvalues(DO, 1);
if (O == null) O = new D3tuple[6];
k = 1;
while (k <= 5)
{
if (O[k] == null) O[k] = new D3tuple();
O[k] = Vjk[index, k];
k++;
}
}
}
/// [*] D5calcDemo <summary><para>[*] </para>
/// D5calc performs the same calculation as Dk(), but the input is from
/// five vectors located at a specified offset into an array res of Double3tuples,
/// and all five Dk values are calculated and stored in the array D5[].
/// </summary>
/// <param name="res"></param>
/// <param name="offs"></param>
public void D5calcDemo(D3tuple[] D, int offs)
{
int lim = offs + 4; if (D == null) return;

5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146

if (!D5initialized) D5MIinit();
if ((lim < D.Length)&(offs >= 0))
{
int i = offs - 1; int j; int index = 1; byte[] B = new byte[6];
D5.Initialize(); D3tuple V = new D3tuple(); double v=0;
while (index <= 5)
{
j = (int)((index-1) * 24);
B = RhoI(5, j); int sgn=1,ict = InversionCount(B, 1, 5,ref sgn);
GetVv(D[B[3]+i], D[B[4]+i], D[B[5]+i], ref v, ref V);
D5[index] = sgn*(v - D3DotProd(D[B[2]+i], V));
Xi[index] = D[index-1+offs];
index++;
}
}
}
public void D5calc(D3tuple[] D, int offs, ref double[] dD5, ref D3tuple[] dX5)
{
int lim = offs + 4; if (D == null) return;
if ((lim < D.Length)&(offs >= 0))
{
int i = offs - 1; int index = 1;
D3tuple V = new D3tuple(); double v=0;
while (index <= 5)
{
dX5[index] = D[index-1+offs];
index++;
}
index = 1;
while (index <= 5)
{
Rho.SetIJKL(index);
v = dX5[Rho.II]*CVPS3(dX5[Rho.JJ], dX5[Rho.KK], dX5[Rho.LL]) - dX5[Rho.JJ]*(dX5[Rho.KK]^dX5[Rho.LL]);
dD5[index] = -v;
index++;
}
}
}
/// [*] M5ISphereCenter <summary><para>[*] </para>
/// M5ISphereCenter() uses the data stored in the F5[], D5[], and LV5i[] arrays when
/// GetD3M5Ivecs() is called with the MakeD5 argument true, finding the center and radius
/// of one of five possible spheres.
/// </summary>
/// <param name="k">Sphere index, k = 1..5</param>
private void M5ISphereCenter(int k)
{
if (!D5initialized) D5MIinit();
double d = D5[k];
if (d != 0)
{
int i=0,k1=k+1; double r; if (k1 > 5) k1 = 1;
try
{
Spheres[k] = F5[k]; D3Scale(ref Spheres[k], -D3Len(F5[k]) / (2*d));
}
catch { Degeneracies[k] = SphereMayBeTooLarge; i = -1; }
if (i < 0) { r = 1 / Eta; }
else

5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204

{
r = ~(Xi[k1]-Spheres[k]);
Radii[k] = 0;
if (r != 0)
{
if (r < Eta) Degeneracies[k] = SphereMayBeTooSmall;
else Radii[k] = r;
}
else Degeneracies[k] = SphereIsPoint;
}
i=1; bool ok = true; double denom=0,lVi=0; Inspheres[k].Zero();
while ((i <= 5) & (ok))
{
try
{
lVi = LVjk[k,i];
if (i != k)
{
denom+=lVi; Inspheres[k] += lVi*Xi[i];
}
}
catch { ok=false; }
i++;
}
if ((ok) & (denom != 0))
{
Inspheres[k] = Inspheres[k] / denom; Inradii[k] = Math.Abs(d / denom);
}
}
else Degeneracies[k] = SphereIsPlane;
SphereDegeneracy = Degeneracies[k];
}
/// [*] CircleCenter <summary><para>[*] </para>
/// This function returns the center and radius of a circle given three
/// points in space d1, d2, and d3 which lie on the circle.
/// </summary>
/// <param name="d1">First point</param>
/// <param name="d2">Second point</param>
/// <param name="d3">Third point</param>
/// <param name="Radius">Radius of circle</param>
/// <returns>Center point of circle</returns>
public D3tuple CircleCenter(D3tuple d1, D3tuple d2, D3tuple d3, ref double Radius)
{
D3tuple C = new D3tuple(); D3tuple d = D3Sum(d1, d2, 1);
D3tuple v = new D3tuple();
C = GetNormal(d1, d2, d3, false); double m,c = D3DotProd(C,C);
Radius = 0;
// .5(j+c) + .5 * L^-2 * ( kijk )(ij x ijk)
//
u
m
c
if (c != 0)
{
D3Scale(ref d, 0.5*D3Len(d));
v = D3CrossProd(D3Sum(d2, d1, -1), C);
m = D3DotProd(D3Sum(d1, d3, -1), D3Sum(d3, d2, -1))/(2*c);
D3Scale(ref v, m*D3Len(v));
C = D3Sum(v, d, 1); Radius = D3Len(D3Sum(d1, C, -1));
}
return C;

5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262

}
/// [*] SphereCenter <summary><para>[*] </para>
/// This function returns the center and radius of a sphere given four
/// points in space d1, d2, d3, and d4 which lie on the sphere.
/// </summary>
/// <param name="d1">First point</param>
/// <param name="d2">Second point</param>
/// <param name="d3">Third point</param>
/// <param name="d4">Fourth point</param>
/// <param name="Radius">Radius of sphere</param>
/// <returns>Center of sphere</returns>
public D3tuple SphereCenter(D3tuple d1, D3tuple d2, D3tuple d3, D3tuple d4, ref double Radius)
{
D3tuple[] S = new D3tuple[6];
S[1] = d1; S[2] = d2; S[3] = d3; S[4] = d4; S[5] = new D3tuple(); S[0] = S[5];
D3tuple[] S2 = new D3tuple[6]; bool ok = false;
GetD3M5Ivecs(S, 1, 5, out S2);
GetSphere(5,ref ok,ref S[5],ref Radius);
if (ok)
{
S[0].Set(S[5].X, S[5].Y, S[5].Z); Radius = D3Len(D3Sum(S[5], d1, -1));
}
else { S[0].Set(0, 0, 0); Radius = 0; }
return S[0];
}
/// [*] GetSpheres <summary><para>[*] </para>
/// GetSpheres() should be called only after GetD3M5Ivecs() is called with MakeD5=true.
/// This function calculates the centers and radii of the possible five spheres defined
/// by the five vectors passed to GetD3M5Ivecs() and stores the degeneracy flags in the
/// Degeneracies[] array. See also GetSphere().
/// </summary>
public void GetSpheres()
{
int k = 1;
while (k <= 5)
{
M5ISphereCenter(k);
k++;
}
}
/// [*] GetSphere <summary><para>[*] </para><para>
/// GetSphere() returns the center and radius of sphere k for k=1..5, as calculated from
/// a D3tuple array of points when GetD3M5Ivecs() is called with the MakeD5 argument true.</para>
/// <para>
/// If the radius is smaller than the value of Eta (default is 1E-12) this function sets the
/// SphereDegeneracy value to SphereMayBeTooSmall. If the radius is zero or infinite this value
/// is set to SphereIsPoint or SphereIsPlane. Otherwise, the valid flag will be true on return.</para>
/// <para>
/// In the case of infinite radius, D5[k]=0 and Degeneracies[k]=SphereIsPlane. A radius may not
/// be calculated if |F5[k]|/D5[k] is too small, and Degeneracies[k]=SphereMayBeTooLarge will
/// indicate that situation.</para>
/// <para>
/// If the default value of Eta is too large, you may find that setting it to as small a value
/// as 1E-15 will return good results, but be careful about making Eta any smaller than that.</para>
/// </summary>
/// <param name="k">Index (1..5) of sphere</param>
/// <param name="valid">Returns true if a valid sphere calculation was carried out</param>
/// <param name="Center">This D3tuple returns the center point of the sphere</param>

5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320

/// <param name="Radius">This double returns the radius of the sphere</param>


public void GetSphere(int k, ref bool valid, ref D3tuple Center, ref double Radius)
{
valid = false; Radius = 0; if ((k < 1) | (k > 5)) return;
if (D5[k] == 0) SphereDegeneracy = SphereIsPlane;
else
{
M5ISphereCenter(k); Radius = Radii[k];
if (Radius == 0) { SphereDegeneracy = SphereIsPoint; }
else if (Center != null)
{
if (Math.Abs(Radius) < Eta) { SphereDegeneracy = SphereMayBeTooSmall; }
valid = true; Center = Spheres[k];
}
}
}
#endregion END OF METHODS REQUIRING D5/F5/G5/Vjk/vjk/LVjk/LsqXi/Radii/Spheres ARRAYS
/// SphereCI() <summary><para>
/// Given four vectors, SphereCI returns the center and radius of both
/// the circumsphere and insphere of the tetrahedron defined by those
/// four points. A three-element array is returned, with result[1] containing
/// the circumcenter and result[2] the incenter. The ref double[] parameter
/// must be at least of length 3 to hold the radii of these two spheres
/// in elements 1 and 2.</para>
/// <para>
/// This function and those following do not affect the contents of the
/// arrays D5[], F5[], G5[], Spheres[], Radii[], Vjk[,], vjk[,], LVjk[,],
/// or LsqXi[] used by the GetD3M5IVecs(), D5calc(), M5ISphereCenter(),
/// GetSpheres(), GetSphere(), or D5MIinit() routines.
/// </para>
/// </summary>
/// <param name="D">First point</param>
/// <param name="D2">Second point</param>
/// <param name="D3">Third point</param>
/// <param name="D4">Fourth point</param>
/// <param name="Radius">Array [0..1] of double to contain radii</param>
/// <returns>Array[0..1] of D3tuples containing the two centers</returns>
public D3tuple[] SphereCI(D3tuple D1, D3tuple D2, D3tuple D3, D3tuple D4, ref double[] Radius)
{
if (Radius == null) return null; if (Radius.Length < 3) return null;
D3tuple[] Dres = new D3tuple[3]; Dres[1] = new D3tuple(); Dres[2] = new D3tuple();
D3tuple[] D = new D3tuple[6]; double[] d = new double[6];
int i = 1; while (i <= 5) { D[i] = new D3tuple(); i++; }
D[1] = CVPS3(D2, D3, D4); d[1] = ~D[1]; // V_5,1 and its length
D[2] = CVPS3(D1, D4, D3); d[2] = ~D[2]; // V_5,2 and its length
D[3] = CVPS3(D1, D2, D4); d[3] = ~D[3]; // V_5,3 and its length
D[4] = CVPS3(D1, D3, D2); d[4] = ~D[4]; // V_5,4 and its length
d[5] = d[1]+d[2]+d[3]+d[4];
double d5 = -(D1 * D[1]) - (D2 * (D4^D3)); // this is D_5
if (d[5] != 0)
{
try { d[0] = 1 / d[5]; }
catch { d[5] = 0; d[0] = 1; } // if overflow, no go!
}
if (d[5] != 0) // If nonzero, D[0] contains the sum of the CVPS3 lengths
{
D[5] = d[1]*D1+d[2]*D2+d[3]*D3+d[4]*D4;
// Insphere first - result goes to Dres[2]

5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378

Dres[2] = d[0] * D[5];


Radius[2] = d5 / d[0];
// Circumsphere next - result goes to D[0]
d[0] = -1 / (2*d5); // factor -1/2D_5 is required
D[5].Zero(); i = 1;
while (i <= 4)
{
D[5] = D[5] + ((D[i]*D[i]) * D[i]); // summing (L^2)_i * V_5,m
i++;
}
// D[5] now contains the vector quantity <F_5>
Dres[1] = d[0] * D[5]; // Mul by factor -1/2D_5 to get circumcenter
Radius[1] = ~(D1 - Dres[1]); // Just find radius by vector subtraction
}
return Dres;
}
/// GetDk() <summary>
/// GetDk() returns an array of length 6 containing five D_k values if
/// the input array res has length >= 6, or containing one value in
/// result[5] if res has length 5. These values are calculated directly
/// from the CVPS3 and STP of sets of four vectors as defined by the
/// equation for D_k | c = 1..5 given in Vector Quintets (q.v.).
/// </summary>
/// <param name="res">An array of D3tuples with length of at least 5</param>
/// <returns>An array of doubles of length 6 with entries 1..5 or 5 filled</returns>
public double[] GetDk(D3tuple[] D)
{
if (D == null) return null; if (D.Length < 5) return null;
double[] dk = new double[6];
if (D.Length >= 6)
{
dk[1] = (D[5] * CVPS3(D[2], D[3], D[4])) - (D[2] * (D[3]^D[4]));
dk[2] = (D[5] * CVPS3(D[1], D[4], D[3])) - (D[1] * (D[4]^D[3]));
dk[3] = (D[5] * CVPS3(D[1], D[2], D[4])) - (D[1] * (D[2]^D[4]));
dk[4] = (D[5] * CVPS3(D[1], D[3], D[2])) - (D[1] * (D[3]^D[2]));
}
if (D.Length >= 5)
{
dk[5] = (D[1] * CVPS3(D[2], D[4], D[3])) - (D[2] * (D[4]^D[3]));
}
return dk;
}
/// Intercepts() <summary><para>
/// This function returns the axis intercepts of planes containing
/// a point ptOn given a set of four vectors which define the origin
/// and points on three axes extending from that origin. The axes
/// are defined by (res[2]-res[1]), (res[3]-res[1]), and (res[4]-res[1]), and
/// may be oblique. The result array contains res[1]=[0,0,0],
/// res[2]..res[4]=Intercepts, and res[5]=Coords, where the X,Y,Z
/// components of res[5] contain the corrdinates of ptOn with respect
/// to the given origin and axis points. If res[1] is the zero vector
/// then res[2] to res[4] will be direction vectors for the three axes.</para>
/// <para>
/// Take care to note that the coordinates in res[5] of the point ptOn
/// are global coordinates calculated with respect to the same origin
/// and with the same basis as the expressed coordinates of the
/// origin and axis points supplied in res[1]..res[4]. The result which
/// is returned in res[5] is relative to the given origin res[1] and

5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436

/// basis determined by (res[2]-res[1]), (res[3]-res[1]), and (res[4]-res[1]).</para>


/// </summary>
/// <param name="res">Array of D3tuples containing the origin, axis points, and point ptOn</param>
/// <returns>ZeroRef origin and relative intercepts and coordinates of ptOn</returns>
public D3tuple[] Intercepts(D3tuple[] D)
{
if (D == null) return null; if (D.Length < 6) return null;
D3tuple[] Dres = new D3tuple[6]; int j = 0;
while (j <= 5) { Dres[j] = new D3tuple(); j++; }
j = 2; Dres[0] = D[5]-D[1];
while (j <= 4)
{
Dres[j] = ProjSec(Dres[0],D[j]-D[1]);
Dres[5].a[j-2] = (Dres[0])*(!(D[j]-D[1]));
j++;
}
return Dres;
}
#region TRIANGLE FUNCTIONS
/// eTriIJ <summary>
/// eTriIJ() returns one of three symmetry indicators for a triangle defined by points
/// Xi,Xj,Xk. eTri() returns all three. The returned values sum to zero (calculations
/// will show the sum to be vanishingly small).
/// <para>&#x220E;</para>
/// e_ij = - [ &#x22C9;ijk&#x22CA; &lt;ci&gt; &lt;ij&gt; ]
/// <para>where incenter=m, circumcenter=c, normal=&#x22C9;ijk&#x22CA;, and
/// opposite edge=&lt;ij&gt;.</para>
/// </summary>
/// <param name="Xi"></param>
/// <param name="Xj"></param>
/// <param name="Xk"></param>
/// <returns></returns>
public double eTriIJ( D3tuple Xi, D3tuple Xj, D3tuple Xk)
{
double Lij = ~(Xj - Xi), Ljk = ~(Xk - Xj), Lki = ~(Xi - Xk),
Kjk = (Xi - Xk)*(Xj - Xi), Kki = (Xj - Xi)*(Xk - Xj),
Kij = (Xk - Xj)*(Xi - Xk), Kkk = (Xj - Xi)*(Xj - Xi);
double P = Lij + Ljk + Lki, res = 0, Lsq = Kjk*Kki - Kij*Kkk;
if (P != 0)
{
double Q = Lsq*(P-2*Lij) - P*Kjk*Kki; res = Q / (2*P);
}
return res;
}
/// eTri() <summary>
/// eTri returns all three symmetry indicators e_jk, e_ki, and e_ij
/// in the three components of a D3tuple vector.
/// </summary>
/// <param name="res">A D3tuple array containing the triangle vertices</param>
/// <param name="offs">Offset (zero-based) into the array where the
/// three vertices are found</param>
/// <returns></returns>
public D3tuple eTri(D3tuple[] D, int offs)
{
D3tuple Dres = new D3tuple();
if (D != null)
{
int lim = offs + 2, i = offs;

5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494

if ((D.Length >= lim) & (i >= 0))


{
Dres.X = eTriIJ2(D[i], D[i+1], D[i+2]);
Dres.Y = eTriIJ2(D[i+2], D[i], D[i+1]);
Dres.Z = eTriIJ2(D[i+1], D[i+2], D[i]);
}
}
return Dres;
}
public double eTriIJ2(D3tuple D1, D3tuple D2, D3tuple D3)
{
double res=0,inR=0; D3tuple i = Incenter(D1, D2, D3, ref inR);
double R=0;
D3tuple c = CircleCenter(D1, D2, D3, ref R);
D3tuple cvps = CVPS3(D1, D2, D3);
D3tuple ij = D2 - D1;
res = cvps * D3CrossProd((c-i),ij);
return res;
}
#endregion
/// OmegaIKJL() <summary><para>
/// OmegaIKJL() returns the intersection point of two intersecting lines, or if
/// the lines do not intersect and are not (anti)parallel, it returns the point
/// on each line closest to the other line. If the lines are (anti)parallel, both
/// points returned will be (0,0,0).</para>
/// <para>
/// The first two points are on line 1, and the second two are on line two.</para>
/// http://cqvis.zxq.net/C4/VQC118.thml
/// </summary>
/// <param name="Di">First point on line 1</param>
/// <param name="Dj">Second point on line 1</param>
/// <param name="Dk">First point on line 2</param>
/// <param name="Dl">Second point on line 2</param>
/// <returns></returns>
public D3tuple[] OmegaIKJL(D3tuple Di, D3tuple Dj, D3tuple Dk, D3tuple Dl)
{
D3tuple[] D = new D3tuple[3]; D[1] = new D3tuple(); D[2] = new D3tuple();
if ((Di != null) & (Dk != null) & (Dj != null) & (Dl != null))
{
D3tuple d1 = (Dj-Di)^(Dl-Dk); double l1sq = d1*d1;
if (l1sq != 0)
{
double alpha1 = (d1*(GetNormal(Di, Dk, Dl,false))) / l1sq;
d1 = -d1;
double alpha2 = (d1*(GetNormal(Dk, Di, Dj,false))) / l1sq;
D[1] = Di + alpha1*(Dj-Di); D[2] = Dk + alpha2*(Dl-Dk);
}
}
return D;
}
/// Incenter() <summary>
/// Incenter() returns the center of the inscribed circle of a triangle.
/// http://cqvis.zxq.net/C5/VQC152.html
/// http://cqvis.zxq.net/C5/VQC153.html
/// </summary>
/// <param name="Xi">First triangle vertex</param>
/// <param name="Xj">Second triangle vertex</param>
/// <param name="Xk">Third triangle vertex</param>

5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552

/// <param name="inRadius">Returns radius of inscribed circle</param>


/// <returns>Location of center of inscribed circle</returns>
public D3tuple Incenter(D3tuple Xi, D3tuple Xj, D3tuple Xk, ref double inRadius)
{
D3tuple D = new D3tuple();
if ((Xi != null) & (Xj != null) & (Xk != null))
{
double Lij = ~(Xj - Xi), Ljk = ~(Xk - Xj), Lki = ~(Xi - Xk),
Kjk = (Xi - Xk)*(Xj - Xi), Kki = (Xj - Xi)*(Xk - Xj),Kij = (Xk - Xj)*(Xi - Xk),
Kii = ~(Xk-Xj),Kjj= ~(Xi - Xk),Kkk = ~(Xj - Xi);
double P = Lij + Ljk + Lki;
if (P != 0)
{
//
double Qi = Math.Sqrt(Kjk)/ptOn,Qj = Math.Sqrt(Kki)/ptOn,Qk = Math.Sqrt(Kij)/ptOn;
//
h = Qi*Xi + Qj*Xj + Qk*Xk;
D = (Kii*Xi + Kjj*Xj + Kkk*Xk) / P;
inRadius = ~(Xi-D);
}
}
return D;
}
/// Chirality() <summary>
/// <para>Chirality() returns the determinant of the chirality matrix of four points if the SignOnly
/// parameter is false; if true, it returns only its sign.</para>
/// <para></para><para>
/// The concept of chirality here is based upon the enumerational order X Y Z ptOn with the
/// progression X Y Z being right-handed and the sign associated therewith being positive.
/// In such an enumerational order a tetrahedron whose vertices are expressed in this set
/// of X, Y, Z coordinates will have a positive chirality if a right-handed enumeration
/// p1 p2 p3 p4 results in the matrix [ Cp1 Cp2 Cp3 Cp4 ] having a positive determinant,
/// where Cp_i is a column matrix { 1 px py pz } giving the coordinates of point p_i.
/// The results of choosing the order of enumeration arbitrarily may be difficult to
/// interpret when it comes to distinguishing between inward and outward normals to
/// the bounding planes, so you need to be able to refer to the sign of the chirality
/// matrix when adopting a geometric convention for the signs of those quantities.</para>
/// </summary>
/// <param name="D">1st point</param>
/// <param name="D2">2nd point</param>
/// <param name="D3">3rd point</param>
/// <param name="D4">4th point</param>
/// <param name="SignOnly">Set to true if only the sign of the determinant is to be returned</param>
/// <returns></returns>
public double Chirality(D3tuple D1, D3tuple D2, D3tuple D3, D3tuple D4, bool SignOnly)
{
double res = 1; // [2-1][3x4] + [4-3][1x2]
res = D3STP(D3Sum(D2, D1, -1), D3, D4) + D3STP(D3Sum(D4, D3, -1), D1, D2);
if (SignOnly) res = Math.Sign(res);
return res;
}
#region VECTOR FUNCTIONS
/// D3Sum <summary>
/// D3Sum() adds two vectors in D and D2 if sgn=1. If sgn=-1 it subtracts D2 from D.
/// The resulting D3tuple is returned in the function result;
/// </summary>
/// <param name="D">First term of sum/difference</param>
/// <param name="D2">Second term; this is added to or subtracted from the first term</param>
/// <param name="sgn">Set to 1 to form the sum, or -1 to form the difference, of the terms</param>
/// <returns>Vector sum or difference D=D+D2 or D=D-D2</returns>

5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610

public D3tuple D3Sum(D3tuple D1, D3tuple D2, double sgn)


{
D3tuple D3 = new D3tuple();
D3.X = D1.X + sgn*D2.X; D3.Y = D1.Y + sgn*D2.Y; D3.Z = D1.Z + sgn*D2.Z;
return D3;
}
/// D3Len() <summary>
/// This returns the length of a D3tuple vector.
/// </summary>
/// <param name="D">The vector for which the length is to be found</param>
/// <returns>Length of vector res</returns>
public double D3Len(D3tuple D) { double L= Math.Sqrt(D.X*D.X+D.Y*D.Y+D.Z*D.Z); return L; }
/// D3Mul() <summary>
/// D3Mul() multiples the components of a vector by a scalar, mulfact. In general the length
/// of the resultant vector will not equal mulfact except when D is a unit vector. To set the
/// magnitude of a vector of arbitrary length to a value specified by a scalar, use D3Scale(),
/// which returns a vector with length equal to the scale argument.
/// </summary>
/// <param name="D">Vector whose components are to be multiplied</param>
/// <param name="mulfact">Scalar multiplier</param>
/// <returns></returns>
public D3tuple D3Mul(D3tuple D, double mulfact)
{
D3tuple res = new D3tuple();
if (D != null) { res.X=D.X*mulfact; res.Y=D.Y*mulfact; res.Z=D.Z*mulfact; }
return res;
}
/// D3Scale() <summary>
/// D3Scale() scales a vector. If the scale parameter is 1, the vector will become a unit vector.
/// </summary>
/// <param name="D">The vector to be scaled</param>
/// <param name="scale">The desired final vector length</param>
public void D3Scale(ref D3tuple D, double scale)
{
double L = D3Len(D); if (L != 0) { D.X=D.X*scale/L; D.Y=D.Y*scale/L; D.Z=D.Z*scale/L; }
}
#endregion
#region ANGLE FUNCTIONS
/// D3Angle() <summary><para>
/// D3Angle() returns the angle, in radian measure, between two vectors whose
/// components are provided in the Double3tuples D1 and D2. This angle is known
/// as a direct angle.</para>
/// <para>
/// Aside from its use in calculation per se, it is recommended that this function
/// also be used in comparing points rotated using integer and floating-point input
/// values at critical stages where accumulated error in successive integer inputs
/// to the rotation routines may adversely affect the accuracy of the outputs, and
/// a calculated set of floating-point inputs injected into the stream to compensate
/// for the error introduced.</para>
/// </summary>
/// <param name="D1"></param>
/// <param name="D2"></param>
/// <returns>The direct angle between vectors D1 and D2</returns>
public double D3Angle(D3tuple D1, D3tuple D2)
{
double res = D3Len(D1)*D3Len(D2);
if (res != 0) res = Math.Acos(D3DotProd(D1, D2) / res);
return res;

5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668

}
/// D3Cos() <summary>
/// D3Cos() returns the cosine of an angle between two vectors.
/// </summary>
/// <param name="D1">1st vector</param>
/// <param name="D2">2nd vector</param>
/// <returns>COS() = (D1*D2)/(|D1||D2|)</returns>
public double D3Cos(D3tuple D1, D3tuple D2)
{
double res = D3Len(D1)*D3Len(D2);
if (res != 0) res = D3DotProd(D1, D2) / res;
return res;
}
/// D3Sin() <summary><para>
/// D3Sin() returns the sine of an angle between two vectors. However, this value is
/// not a signed quantity and should be supplemented with calculation of the cosine
/// of the angle whenever possible.</para>
/// <para>
/// In three dimensions it is possible to define a reference axis perpendicular to the
/// plane of the two vectors and which points in the direction chosen for which all
/// angles measured with a given chirality will be considered positive; and then to
/// use the scalar product of [RefAxis,D1,D2] as a chirality matrix to provide the sign of
/// the sine returned by this function. This consistently provides signed angles
/// from 0 to [+|-]PI based on the unsigned value of D3Sin() and the value of D3Cos().</para>
/// </summary>
/// <param name="D1">1st vector</param>
/// <param name="D2">2nd vector</param>
/// <returns>|SIN()| = |D1^D2|/(|D1||D2|)</returns>
public double D3Sin(D3tuple D1, D3tuple D2)
{
D3tuple D = D3CrossProd(D1, D2); double res = D3Len(D1)*D3Len(D2);
if (res != 0) { res = Math.Asin(D3Len(D) / res); }
return res;
}
/// D3Tan() <summary><para>
/// D3Tan() returns the tangent of an angle between two vectors provided
/// that an independent reference axis RefAxis is supplied as a parameter
/// to this function. RefAxis must not depend on the relation between D1
/// and D2 (that is, it must not be a linear combination of D1 and D2, and
/// supplying RefAxis as D1 X D2 is thus an unworkable approach).</para>
/// <para>
/// The quadrant signs being COS:PMMP SIN:PPMM TAN:PMPM it is necessary to
/// know the sign of the SIN() value returned by D3Sin() in order to have
/// the TAN() result positive in the 1st and 3rd quadrants and negative in
/// the 2nd and 4th quadrants. Since the cross product is used in the D3Sin()
/// function and cannot return a signed value, the direction of D1^D2 has to
/// be referred to an independent reference axis which must not lie in the
/// plane of D1 and D2. A value of RefAxis which coincidentally happens to
/// equal D1^D2 could of course be supplied, but if the value is calculated
/// from D1 and D2 it will fail to provide the correct sign for the tangent
/// because D1^D2 points in the opposite direction in cases where the tangent
/// would be negative AS VIEWED FROM THE SAME DIRECTION as when the tangent
/// would be positive.</para>
/// <para>
/// The RefAxis direction, then, should be arbitrary, and chosen to point
/// toward the VP (view point), as it is called in a graphical coordinate
/// scheme in 3 dimensions. With respect to this observation point, the
/// counterclockwise rotations will always progress from D1 toward some

5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726

/// angle at which the vector D2 is located, and if this angle is greater
/// than 180 degrees the vector D1^D2 will point away from the observer,
/// so that [ RefAxis D1 D2 ] returns a negative value and the sign can
/// be imputed to the cross product SIN function irrespective of the fact
/// that it does not return a sign. In this way, the signs of both COS
/// and SIN are available to provide the proper sign in the ratio
/// TAN=SIN/COS as long as degeneracy is not introduced by allowing
/// RefAxis to be calculated from D1 or D2. If RefAxis should fall in
/// the plane D1^D2 the sign calculation will fail to return any negative
/// values.</para>
/// </summary>
/// <param name="D1">1st vector</param>
/// <param name="D2">2nd vector</param>
/// <param name="RefAxis">Arbitrary viewpoint axis direction in which
/// dextrochiral counterclockwise rotation is positive</param>
/// <returns>TAN() = SIN()/COS()</returns>
public double D3Tan(D3tuple D1, D3tuple D2, D3tuple RefAxis)
{
double res = D3Cos(D1, D2);
if (res != 0)
{
double sgn = RefAxis * (D1^D2); if (sgn < 0) sgn = -1; else sgn = 1;
res = Math.Atan(sgn*D3Sin(D1, D2) / res);
}
return res;
}
/// ObliqueAngle() <summary>
/// ObliqueAngle() returns an angle formed by the projections onto a plane P with
/// normal PNormal which contains point ptOnPlane, of two vectors from the point
/// ptOnPlane to the two points PtA and PtB in arbitrary locations in space.
/// The angle is called oblique because it is measured, not between the vectors
/// to those points, but is measured in plane P between the perpendicular
/// projections onto P of those two vectors.
/// </summary>
/// <param name="PtA">A point, not necessarily on plane P</param>
/// <param name="PtB">A second point, not necessarily on plane P</param>
/// <param name="PtOnPlane">A point lying on plane P</param>
/// <param name="PNormal">Normal vector to plane P specifying its orientation</param>
/// <returns>The angle measured in plane P</returns>
public double ObliqueAngle(D3tuple PtA, D3tuple PtB, D3tuple PtOnPlane, D3tuple PNormal)
{
double res=0;
res = D3Angle(ProjSin(D3Sum(PtA, PtOnPlane, -1),PNormal), ProjSin(D3Sum(PtB, PtOnPlane, -1),PNormal));
return res;
}
/// FacetAngle() <summary><para>
/// FacetAngle() returns the angle between two plane normals, which is called
/// a facet angle. It is measured about the line of intersection of the two planes,
/// from the direction of the normal to the first to the direction of the normal to
/// the second. Since D1 x D2 defines the direction of the intersection vector,
/// the chirality matrix formed by enumerating four distinct points, of which two
/// lie in both planes and the other two in one plane only, determines the value to
/// use for a signed angle 0 to [+|-]PI or an unsigned angle 0 to 2PI.</para>
/// <para>
/// Chirality(p1,p2,p3,p4,true) generates the value of this chirality matrix as
/// [+|-]1. Note that the order of enumeration must be consistent in your scheme
/// of measurement for all points involved in order to be able to depend on knowing
/// the meaning of the chiralities supplied by this function.</para>

5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783

/// <para>
/// Note that for the same two planes, different angles may be returned, depending
/// on whether the plane normals are both given with directionality in the same
/// chiral sense of rotation.</para>
/// </summary>
/// <param name="D1">Normal of plane from which angle is measured</param>
/// <param name="D2">Normal of plane toward which angle is measured</param>
/// <returns>Signed angle between two planes</returns>
public double FacetAngle(D3tuple D1, D3tuple D2)
{
double res = D3Len(D1)*D3Len(D2);
if (res != 0) res = Math.Acos(D3DotProd(D1, D2) / res);
return res;
}
#endregion
#region COMPONENT-WISE VECTOR FUNCTIONS
/// CrossProd() <summary>
/// This function takes six vector components, computes the cross product of the first and
/// second set of three components, and returns the result as a third set of three components.
/// </summary>
/// <param name="x1a">X[1,1]</param>
/// <param name="x2a">X[1,2]</param>
/// <param name="x3a">X[1,3]</param>
/// <param name="x1b">X[2,1]</param>
/// <param name="x2b">X[2,2]</param>
/// <param name="x3b">X[2,3]</param>
/// <param name="x1c">X[1]</param>
/// <param name="x2c">X[2]</param>
/// <param name="x3c">X[3]</param>
public void CrossProd(double x1a, double x2a, double x3a, double x1b, double x2b, double x3b, out double x1c, out double
x2c, out double x3c)
{ x1c=(x2a*x3b)-(x3a*x2b); x2c=(x3a*x1b)-(x1a*x3b); x3c=(x1a*x2b)-(x2a*x1b); }
/// VecLen() <summary>
/// VecLen() returns the norm of a vector given its three components.
/// </summary>
/// <param name="x1a">X[1,1]</param>
/// <param name="x2a">X[1,2]</param>
/// <param name="x3a">X[1,3]</param>
/// <returns>Length of vector X[1]</returns>
public double VecLen(double x1a, double x2a, double x3a)
{ double L = Math.Sqrt((x1a*x1a)+(x2a*x2a)+(x3a*x3a)); return L; }
/// ScaleVec() <summary>
/// ScaleVec() scales a vector to the length of a vector X[1].
/// If the scale parameter is 1, the vector will be a unit vector.
/// The resultant vector is collinear with X[1].
/// </summary>
/// <param name="x1a">X[1,1]</param>
/// <param name="x2a">X[1,2]</param>
/// <param name="x3a">X[1,3]</param>
/// <param name="scale">The desired length of the output vector</param>
/// <param name="x1res">Resulting X-component</param>
/// <param name="x2res">Resulting Y-component</param>
/// <param name="x3res">Resulting Z-component</param>
public void ScaleVec(double x1a, double x2a, double x3a, double scale, out double x1res, out double x2res, out double x3res)
{
double L = VecLen(x1a, x2a, x3a); x1res=0; x2res=0; x3res=0;
if (L != 0) { x1res=x1a*scale/L; x2res=x2a*scale/L; x3res=x3a*scale/L; }
}

5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841

#endregion
#region IMPLEMENTATION OF PROJECTION OPERATORS
// http://cqvis.zxq.net/C2/VQC068.html
// http://cqvis.zxq.net/C2/VQC069.html
//
/// ProjCos() <summary>
/// ProjCos(u,v) returns the projection of vector u onto vector v, which has the
/// length of u and the direction of [+|-]v. Since AB = |A||B|cos() the length
/// of the projection is |u|cos() and it is always less than or equal to the
/// length of u.
/// </summary>
/// <param name="u">Vector to be projected</param>
/// <param name="v">Vector which u is projected onto</param>
/// <returns>The projection of u onto v</returns>
public D3tuple ProjCos(D3tuple u, D3tuple v)
{
D3tuple res = new D3tuple();
// ( uc / cc ) <c>
double V = D3Len(v);
if (V != 0) { V = D3DotProd(u, v) / V; res = V*!v; //D3Scale(ref res, V);
}
return res;
}
/// ProjSec() <summary>
/// ProjSec(u,v) returns the extension of vector u on vector v, which has the
/// length of u divided by the cosine of the angle between u and v, and the
/// direction of [+|-]v. The length of this vector is |u|sec() and is thus
/// always greater than or equal to the length of u.
/// </summary>
/// <param name="u">Vector to be extended</param>
/// <param name="v">Vector which u is extended onto</param>
/// <returns>The extension of u onto v</returns>
public D3tuple ProjSec(D3tuple u, D3tuple v)
{
D3tuple res = new D3tuple();
// ( uu / uc ) <c>
double V = D3DotProd(u,v);
if (V != 0) { V = D3Len(v)*D3DotProd(u, u)/V; res = V*!v; //D3Scale(ref res, V);
}
return res;
}
/// ProjTan() <summary>
/// ProjTan(u,v) returns a vector beginning at the head of u and ending on v,
/// and which is perpendicular to u. When u is extended onto v by ProjSec(u,v),
/// this vector extends from the head of u to the head of ProjSec(u,v) on v.
/// Its length is |u|tan() and its direction is from u to v. Note that in
/// unit circular measure this vector is tangent to the unit circle at u and
/// extends to the x-axis at the head of v', where v' = ProjSec(u,v). The
/// length of v' is |u|/cos().
/// </summary>
/// <param name="u">Vector u at the head of which a tangent vector is to be extended to v</param>
/// <param name="v">Vector to which tangent is to be extended</param>
/// <returns>Extended tangent vector t such that v' = u + t</returns>
public D3tuple ProjTan(D3tuple u, D3tuple v)
{
D3tuple res = new D3tuple();
// ( ProjSec(u,c) - <u> )
res = D3Sum(ProjSec(u,v),u,-1);

5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899

return res;
}
/// ProjSin() <summary>
/// ProjSin(u,v) returns a vector beginning on v and ending on u at its head,
/// and which is perpendicular to v. It is analogous to the altitude of the
/// right triangle in circular measure which has its base and hypoteneuse meeting
/// at the origin. Its length is |u|sin(). This vector is the perpendicular
/// component of u with respect to v, and v' = ProjCos(u,v) is the collinear component
/// of u with respect to v.
/// </summary>
/// <param name="u">Vector u from which a perpendicular to v is to be dropped</param>
/// <param name="v">Vector on which the perpendicular to the head of u originates</param>
/// <returns>Perpendicular component vector p such that v' = u - p</returns>
public D3tuple ProjSin(D3tuple u, D3tuple v)
{
D3tuple res = new D3tuple();
// <u> - ( ProjCos(u,c) )
res = D3Sum(u,ProjCos(u,v),-1);
return res;
}
#endregion End of IMPLEMENTATION OF PROJECTION OPERATORS
#region CVPS - Cyclic Vector Product Sums
/// CVPS3() <summary>
/// This function returns a D3tuple containing the Cyclic Vector Product Sum
/// of three vectors.
/// </summary>
public D3tuple CVPS3(D3tuple D1, D3tuple D2, D3tuple D3)
{
D3tuple D = new D3tuple(); D = (D1^D2) + (D2^D3) + (D3^D1); return D;
}
/// CVPS4() <summary>
/// This function returns a D3tuple containing the Cyclic Vector Product Sum
/// of four vectors.
/// </summary>
public D3tuple CVPS4(D3tuple D1, D3tuple D2, D3tuple D3, D3tuple D4)
{
D3tuple D = new D3tuple();
D = D3CrossProd(D1, D2) + D3CrossProd(D2, D3) + D3CrossProd(D3, D4) + D3CrossProd(D4, D1);
D = D3CrossProd(D3Sum(D3, D1, -1), D3Sum(D4, D2, -1));
D = GetNormal(D1, D2, D3, false) + GetNormal(D1, D3, D4, false);
D = GetNormal(D2, D3, D4, false) + GetNormal(D1, D2, D4, false);
return D;
}
/// CVPSn() <summary>
/// This function returns a D3tuple containing the Cyclic Vector Product Sum
/// of n vectors contained in the array D[] beginning at element Dofs.
/// </summary>
public D3tuple CVPSn(D3tuple[] D, int Dofs, int n)
{
D3tuple res = new D3tuple();
int L = D.Length,lim=Dofs+n-1,k=Dofs; if (n < 3) k = L; // Exit if D=0
while ((k < L) & (k <= lim))
{
if (k == lim) { res = res + (D[k]^D[Dofs]); }
else
{
res = res + (D[k]^D[k+1]);
}

5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957

k++;
}
return res;
}
#endregion
#region AUXILIARY SCALAR FUNCTIONS
/// TenX <summary>
/// This function returns the inverse function of the base ten logarithm function.
/// </summary>
/// <param name="n">The base 10 logarithm of a number, N</param>
/// <returns>The number, N</returns>
public double TenX(int n)
{
double res=0;
try
{
res = Math.Pow(10, n);
}
catch { }
return res;
}
public double[] FactTbl = null; public bool FactTblValid=false; public int FactTblLimit=0,FactTblAbsLimit=17;
/// FactTblInit <summary>
/// FactTblInit initializes a table of n! values in the array FactTbl[]. The maximum allowed
/// value of n will be in FactTblAbsLimit, and the highest value of n for which n! has been
/// calculated and stored in FactTbl so far will be given by FactTblLimit.
/// </summary>
/// <param name="n">The desired value of n</param>
/// <returns>The value of n-factorial</returns>
public int FactTblInit(int n)
{
int res = 0; bool initializing = false; int kM = (n / 16 + 1) * 16;
if (FactTblValid)
{
if (FactTbl.Length < kM) { FactTblValid = false; }
}
if (!FactTblValid) { FactTbl = new double[kM+1]; initializing = true; FactTblValid = true; FactTblLimit = 0; }
if ((n > FactTblLimit) & (FactTblValid)) initializing = true;
if (initializing)
{
int k = FactTblLimit+1; FactTbl[0] = 1;
bool ok = true, ints = true; double F = FactTbl[k-1]; int L = FactTbl.Length;
while ((ok) & (k <= n) & (k <= L))
{
try
{
F = F * k;
FactTbl[k] = F;
if (FactTblAbsLimit < k) FactTblAbsLimit = k;
if (ints)
{
if (Frac(F) == 0.0)
{
if (FactTblLimit < k) FactTblLimit = k;
}
else
{
ints = false; FactTblLimit = k - 1;

5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015

}
}
}
catch { ok = false; FactTblAbsLimit = k - 1; FactTblLimit = k-1; }
if (ok) k++;
}
}
res = FactTblLimit;
return res;
}
/// iFact <summary>
/// If you aren't sure whether FactTbl[] has been initialized by a call to FactTblInit() yet,
/// call iFact(k) to retrieve the factorial. If you're sure that FactTblLimit >= k you can
/// read the value of k! directly from the table; but you must call FactTblInit() at least once.
/// </summary>
/// <param name="k">The value of k</param>
/// <returns>The value of k!</returns>
public double iFact(int k)
{
double res = 1; int lim=FactTblLimit;
if (k > lim) lim = FactTblInit(k);
if (k <= lim) res = FactTbl[k];
return res;
}
/// iS1 <summary>
/// Returns 1 if v is non-negative; 0 otherwise.
/// </summary>
/// <param name="v"></param>
/// <returns></returns>
public int iS1(double v)
{
int res = (int)Math.Truncate(Math.Abs((v+1)/(Math.Abs(v)+1)));
return res;
}
/// iS0 <summary>
/// Returns 0 if v is non-negative; -1 otherwise.
/// </summary>
/// <param name="v"></param>
/// <returns></returns>
public int iS0(double v)
{
int res = (int)Math.Truncate(Math.Abs((v+1)/(Math.Abs(v)+1)))-1;
return res;
}
/// iSign <summary>
/// Returns 1 if v is non-negative; -1 otherwise.
/// </summary>
/// <param name="v"></param>
/// <returns></returns>
public int iSign(double v)
{
int res =0; double d=Math.Truncate(Math.Abs((v+1)/(Math.Abs(v)+1)))-1;
d = Math.Pow(-1, d); res = (int)d;
return res;
}
/// ieXinAB <summary>
/// Returns 1 if x lies in the interval [a,b], and 0 otherwise.
/// </summary>

6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073

/// <param name="a"></param>


/// <param name="b"></param>
/// <param name="x"></param>
/// <returns></returns>
public int ieXinAB(double x, double a, double b)
{
int res=0;
res = (1+iS0(x-a))*(1+iS0(b-x));
return res;
}
/// LabI <summary>
/// Returns b-a if i is even, and returns b+a if i is odd.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="i"></param>
/// <returns></returns>
public double LabI(double a, double b, double i)
{
double res=0;
res = a*Math.Pow(-1, 3*i)+b*Math.Pow(-1, -2*i);
return res;
}
/// L2i <summary>
/// Returns 2 if i is even; otherwise 0.
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public double L2i(double i)
{
double res = 0;
res = LabI(1, 1, i);
return res;
}
/// L1i <summary>
/// Returns 1 if i is even; otherwise 0.
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public double L1i(double i)
{
double res = 0;
res = LabI(1, 1, i)/2;
return res;
}
/// RoundXtoNearestIthPowerOfR <summary><para>
/// For r greater than 1 and I less than 0, this function rounds the fractional portion
/// of a value to the nearest multiple of the Ith power of r. Thus, the sequence in the
/// example here arises, where c = RoundXtoNearestIthPowerOfR(x,2,-2):</para>
/// <para>
///
x 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5</para> <para>
///
c 0.0 0.0 0.25 0.25 0.5 0.5 0.5 0.75 0.75 1.0 1.0 1.0 1.25 1.25 1.5 1.5</para>
/// <para>
/// The concept of exponentiation of non-integer negative r to negative fractional powers of I
/// is not supported. The absolute value of r is used as the base of exponentiation, although
/// any values of x, r, and I can be supplied as arguments to this function. ZeroRef is returned
/// if a value r=0 is supplied.</para>
/// <para>

6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131

/// Cases may arise in multiple rotations where you may want to round values consistently to
/// some decimal fraction or multiple thereof which is not a pure binary fraction; and this
/// function directly provides that capability. It is thus possible to partition error (with
/// the aid of Rotator.Preview() to provide checkpoint values) into small neighborhoods rather
/// than allowing it to directly propagate into successive rotations.</para>
/// </summary>
/// <param name="x">Value to be rounded</param>
/// <param name="r">Base of rounded value</param>
/// <param name="i">Exponent of base for rounding</param>
/// <returns></returns>
public double RoundXtoNearestIthPowerOfR(double x, double r, double i)
{
double res=0; r = Math.Abs(r);
if (r > 0)
{
double rr = x*Math.Pow(r, -i); double flrr = Math.Truncate(rr);
res = Math.Pow(r, i)*(flrr-Math.Truncate(2*flrr - 2*rr));
}
return res;
}
/// GetRational <summary>
/// When scaling to integer coordinates (especially pixel coordinates) or using rounding
/// to binary fractions or to powers of some approximate differential length, you can use
/// GetRational() to return an array of candidate rational values Q and R such that Q/R
/// offers the scaling you require. This function returns if Ndigits decimal digits
/// to the right of the decimal point in the check-value calculated at each iteration
/// are all zeros or all nines, which is normally the case when the ratio approaches
/// closely to integer Q and R. If that proves unsatisfactory, you can increase Ndigits.
/// </summary>
/// <param name="X">Real value to be given a rational approximation</param>
/// <param name="ArrayItems">Length of array to be returned</param>
/// <param name="Ndigits">Number of digits expected</param>
/// <returns></returns>
public D3tuple[] GetRational(double X, int ArrayItems, int Ndigits)
{
int d = ArrayItems+1;
D3tuple[] res = new D3tuple[d]; res[0] = new D3tuple();
int I; double A,E,F,G,W; double localEta = Math.Pow(10.0,-Math.Abs(Ndigits));
I = 0; F = 0; G = 1;
E = Math.Abs(X); A = E;
if (X != E) { G = -1; }
while ((I < d) & (F <= 23))
{
W = A - Math.Truncate(A);
if ((W <= localEta) | ((1.0-W) < localEta))
{
return res;
}
else
{
A = 1 / W;
if (A > Eta)
{
F = F + Math.Log(A);
if (F > 23) { return res; }
else if (I < d)
{
I++; res[0].Ratio = (double)I;

6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189

res[I] = new D3tuple();


res[I].Numer = G*Math.Exp(F + Math.Log(E)); res[I].Denom = Math.Exp(F);
if (F > 0) res[I].Ratio = res[I].Numer / res[I].Denom;
}
}
else { return res; }
}
}
return res;
}
public double Frac(double v)
{
double res = v; int sgn = 1; if (v < 0) sgn = -1; v = Math.Abs(v);
v = v - Math.Truncate(v); res = v*sgn; return res;
}
#endregion
#region PERMUTATION INDEX FUNCTIONS
// The permutation functions are used to index sets of D3tuple vectors.
// The motivation for this approach stems from the Laplace development
// of the determinant for matrices of rank 4 and 5.
internal void RemoveByte(ref byte[] b, int k)
{
if (b != null)
{
if (k < b.Length)
{
int j = k;
while (j < b.Length-1)
{
b[j] = b[j+1];
j++;
}
b[b.Length-1] = 0;
}
}
}
/// RhoJK() <summary><para>
/// RhoJK() returns a byte array containing a permutation ptOn = {f(i)}(0) where f(i) is the Ith
/// permutation rule from the set i=1..N! and 0 = {f(0)}(p) = p with (p)=N.</para><para>
/// A subpermutation ^{5}_{t=3..5}(p_t) is used to index the arrays V5[], F5[], etc. when calculating
/// MI vectors or related scalars.</para>
/// <para>
/// This function provides selected signed elementary product index permutations from the space
/// of N! such permutations. The selection is made based on an a priori matrix factorization
/// scheme which underlies the formulation of the equations used by GetSphere() and the other
/// M5I functions.</para>
/// </summary>
/// <param name="N">Cardinality of the permutation</param>
/// <param name="j">Row index</param>
/// <param name="k">Column index</param>
/// <returns></returns>
public byte[] RhoJK(int N, int j, int k)
{
byte[] d = new byte[N+1]; d.Initialize(); byte[] D = new byte[N+1];
if (!FactTblValid) FactTblInit(N);
double res=0; if (N < 2) return d;
double x = k-j; double y = iS1(x); double z = j+k-1-y; z = ((Math.Pow(-1, 3*z) - Math.Pow(-1, -2*z)) / 2);
res = (FactTbl[N-2])*(j*N-N+x-y) + z + 1; if (res < 0) res = 0;

6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247

int i = (int)res; double w,u; byte[] b = new byte[N+1];


for (byte ii = 0; (ii <= N); ii++) { b[ii] = (byte)(ii+1); }

//
//

u: 0 1 2 3 4 5 6 ...
b[u]: 1 2 3 4 5 6 ...

int L = N; y = 0;
while (L >= 1)
{
w = FactTbl[L];
u = Math.Truncate(i / w);
d[N-L] = (byte)u;
i = i - (int)(u * w);
L--;
}
//
u:
0 1 2 3 4 5 6 ...
// u[u]: 0 4 3 2 1
----->> 51243
i = 0; byte kk=1,jj=1;
while (i < N)
{
kk = d[i+1]; jj = b[kk]; RemoveByte(ref b, kk);
D[i+1] = jj;
i++;
}
D[0] = (byte)0;
return D;
}
byte[] RhoTbl = null; bool RhoTblValid = false;
private void RhoTblInit()
{
RhoTbl = new byte[842]; byte[] b = new byte[6];
int i = 1, k = 1, c = 0, p = 0;
while (i <= 120)
{
b = RhoI(5, i-1); c = InversionCount(b, 1, 5, ref p); p++;
RhoTbl[k] = (byte)p; RhoTbl[k+1] = (byte)c;
Buffer.BlockCopy(b, 1, RhoTbl, k+2, 5);
i++; k += 7;
}
RhoTblValid = true;
}
/// Rho5I() <summary>
/// Rho5I() returns a permutation of length N=5 with permutation index i.
/// The count of inversions and the sign are returned in the last two
/// arguments. The permutation is in bytes 1..5 of the returned array.
/// </summary>
/// <param name="i">Permutation index</param>
/// <param name="InvCt">Inversion count</param>
/// <param name="sgn">Sign of permutation</param>
/// <returns>A byte array of length 6</returns>
public byte[] Rho5I(int i, ref int InvCt, ref int sgn)
{
byte[] b = new byte[6];
if (!RhoTblValid) RhoTblInit();
if ((RhoTblValid) & (i >= 1) & (i <= 120))
{
int k = 7*(i-1) + 1; sgn = (int)RhoTbl[k] - 1; InvCt = RhoTbl[k+1];
Buffer.BlockCopy(RhoTbl, k+2, b, 1, 5);
b[0] = (byte)5;
}
return b;

6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305

}
/// RhoI() <summary>
/// RhoI() returns a byte array containing apermutation of length N
/// and with permutation index u, from a possible range of N! such
/// permutations. Byte 0 of the returned array contains the value N.
/// </summary>
/// <param name="N">Length of permutation</param>
/// <param name="index">Permutation index, u = 1..N!</param>
/// <returns>An array of N+1 bytes in length</returns>
public byte[] RhoI(int N, double index)
{
byte[] d = new byte[N+1]; d.Initialize(); byte[] D = new byte[N+1];
if (!FactTblValid) FactTblInit(N);
if (N < 2) return d; if (index < 0) index = 0; if (index > FactTbl[N]) index = (int)FactTbl[N];
double w,u; byte[] b = new byte[N+1];
for (byte ii = 0; (ii <= N); ii++) { b[ii] = (byte)(ii+1); }
int L = N;
while (L >= 1)
{
w = FactTbl[L];
u = Math.Truncate(index / w);
d[N-L] = (byte)u;
index = index - (int)(u * w);
L--;
}
int i = 0; byte kk=1,jj=1;
while (i < N)
{
kk = d[i+1]; jj = b[kk]; RemoveByte(ref b, kk);
D[i+1] = jj;
i++;
}
D[0] = (byte)N;
return D;
}
/// InversionCount() <summary>
/// This function counts the inversions in an array of bytes, starting at
/// a specified offset and including a specified number of bytes. The
/// sign associated with an elementary product is returned in sgn.
/// </summary>
/// <param name="b">A byte array</param>
/// <param name="offs">Position of first byte in the permutation</param>
/// <param name="len">Length of the permutation</param>
/// <param name="sgn">Returns (-1)^(InversionCount)</param>
/// <returns></returns>
public int InversionCount(byte[] b, int offs, int len, ref int sgn)
{
int c = 0; sgn = 1;
if (b != null)
{
int k = offs; int j = k; int lim = offs + len - 1; if (lim > b.Length) lim = b.Length;
while (k < lim)
{
j = k + 1;
while (j <= lim)
{
if (b[j] < b[k]) { c++; sgn = -sgn; }
j++;

6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363

}
k++;
}
}
return c;
}
/// PermString() <summary>
/// PermString() creates a string containing characters representing
/// the bytes of the input array. The values in b are limited to the
/// ranges 1 to 9 and 65 to 90. These values are converted to string
/// characters '1'..'9' and 'A'..'Z'.
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public string PermString(byte[] b)
{
string s="",s2=""; char[] c= { 'A' }; byte[] cb = new byte[2];
if (b != null)
{
int L = b.Length;
if (b[0] == (L-1))
{
int i = 1; L--;
while (i <= L)
{
s2 = b[i].ToString();
if (s2.Length > 1)
{
if (b[i] >= 10)
{
b[i] += 55;
if ((b[i] > 64) & (b[i] <= 90)) b[i] -= 64;
s2 = b[i].ToString();
c = s2.ToCharArray(0, 1); Buffer.BlockCopy(c, 0, cb, 0, 1);
if (cb[0] > 48)
{
if ((cb[0] > 48) & (cb[0] <= 74)) cb[0] = (byte)(cb[0] + 16);
Buffer.BlockCopy(cb, 0, c, 0, 1); s2 = c[0].ToString();
}
else s2 = cb[0].ToString();
}
}
s = s + s2;
i++;
}
}
}
return s;
}
/// PermBytes() <summary>
/// PermBytes() returns a byte array containing the values of the
/// digits in a string s, if there are any '0'..'9' characters in it.
/// Any other characters in s only survive as byte values, and if
/// they are less than 48 or greater than 57 they will be unchanged
/// in the output array.
/// </summary>
/// <param name="s">An input string containing a permutation</param>
/// <returns>A byte array of length = s.Length+1</returns>

6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421

public byte[] PermBytes(string s)


{
byte[] b = null;
if (s != null)
{
int lim = s.Length;
if ((lim > 0) & (lim <= 255))
{
b = new byte[lim+1]; char[] c = s.ToCharArray();
int i = 0; byte[] bch = new byte[1]; byte v = 0;
while (i < lim)
{
Buffer.BlockCopy(c, i<<1, bch, 0, 1); v = bch[0];
if ((v > 48) & (v <= 57)) b[i+1] = (byte)(v-48);
else { if (v == 48) v = 64; if ((v >= 64) & (v < 90)) b[i+1] = (byte)(v+1); }
i++;
}
b[0] = (byte)s.Length;
}
}
return b;
}
#endregion
// REFERENCE LINKS:
// http://cqvis.zxq.net/C1/VQC002.html
// http://cqvis.zxq.net/C1/VQC003.html
// http://cqvis.zxq.net/C1/VQC004.html
#region PERMUTATION SHIFT FUNCTIONS
/// PiShift() <summary>
/// This string function cyclically rotates the substring of
/// string s beginning at offet Dofs. The rotation count is k,
/// and all characters from Dofs to the end of s are affected.
/// </summary>
/// <param name="s">String to undergo Pi shift rotation</param>
/// <param name="Dofs">Offset of first character to be shifted</param>
/// <param name="k">Count of positions to shift</param>
/// <returns>The string s with some portion of it rotated</returns>
public string PiShift(string s, int Dofs, int k)
{
string res = s; int L=s.Length;
if (L > 0)
{
L--;
res = PiShiftAB(s, Dofs, L, k);
}
return res;
}
/// PiShiftAB() <summary>
/// PiShiftAB() cyclically rotates a substring of s beginning
/// with the character at A and ending with that at B. The
/// rotation count is ct, and other characters in s are not
/// affected. The entire string is returned after rotation.
/// </summary>
/// <param name="s">String within which a substring is to be rotated</param>
/// <param name="A">Start position of substring</param>
/// <param name="B">End position of substring</param>
/// <param name="ct">Rotation count</param>
/// <returns></returns>

6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479

public string PiShiftAB(string s, int A, int B, int ct)


{
string res = s; ct = Math.Abs(ct); int i = A,j = A+ct,L=s.Length;
if ((A < 0) | (B < A) | (B >= L)) return res;
j = mod(j-A, B-A+1)+A;
while ((i <= B) & (i < L))
{
if (j > B) j = A;
res = res.Remove(i, 1); res = res.Insert(i, s[j]+"");
i++; j++;
}
return res;
}
/// D3PiShift() <summary>
/// This array function cyclically rotates a sequence of elements
/// in array D[] beginning at offet Dofs. The rotation count is ct,
/// and all elements from Dofs to the end of D[] are affected.
/// </summary>
/// <param name="D">Array containing a sequence of elements to be rotated</param>
/// <param name="Dofs">Offset of first element in D[]</param>
/// <param name="ct">Rotation count</param>
/// <returns></returns>
public D3tuple[] D3PiShift(D3tuple[] D, int Dofs, int ct)
{
D3tuple[] res = new D3tuple[ct+1]; int L=D.Length;
if (L > 0)
{
L--;
res = D3PiShiftAB(D, Dofs, L, ct);
}
return res;
}
/// D3PiShiftAB() <summary>
/// D3PiShiftAB() cyclically rotates a sequence of elements
/// of array D[] beginning with the element at A and ending
/// with that at B. The rotation count is ct, and other
/// elements in D[] are not affected. The entire array is
/// returned after rotation.
/// </summary>
/// <param name="D">Array in which a sequence of elements is to be rotated</param>
/// <param name="A">Position of first element in sequence</param>
/// <param name="B">Position of last element in sequence</param>
/// <param name="ct">Rotation count</param>
/// <returns></returns>
public D3tuple[] D3PiShiftAB(D3tuple[] D, int A, int B, int ct)
{
ct = Math.Abs(ct); int i = 0,j = A+ct,L=D.Length;
D3tuple[] res = new D3tuple[L];
if ((A < 0) | (B < A) | (B >= L)) return res;
while (i <= ct) { res[i] = new D3tuple(); i++; }
// 0 1 2 3 4 5 6 7
// . . A - - - B L
//
m
//
j
i = 0;
while ((i < A) & (i < L)) { res[i] = D[i]; i++; } // Copy up to D[A] to D[]
j = mod(j-A, B-A+1)+A;
while ((i <= B) & (i < L)) // Copy shifted range

6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537

{
if (j > B) j = A;
res[i] = D[j];
i++; j++;
}
while (i < L) { res[i] = D[i]; i++; } // Copy remaining entries
return res;
}
#endregion
/// mod() <summary>
/// This integer function returns an 'adjusted' modulus which
/// differs from the % operator by producing a result in the
/// range 1..M instead of 0..(M-1) as the ordinary modulus
/// function does. This is of primary importance in some rather
/// unusual number-theoretical demonstrations which have never
/// been in vogue; and it is used here because of an array-indexing
/// method which is not considered to be in vogue, either.
/// </summary>
/// <param name="k">Value the residue of which modulo M is required</param>
/// <param name="M">Modulus to which k is to be resolved</param>
/// <returns></returns>
public int mod(int k, int M)
{
int res = Math.Abs(k);
res = ((res - 1) % M) + 1; if (k < 0) res = -res;
return res;
}
private Random rnd = new Random();
public void RandomizePoint(ref D3tuple d)
{
if (d == null) return;
double v1,v2,v3;
int sgn1=rnd.Next(2),i1=rnd.Next(1000),sgn2=rnd.Next(2),i2=rnd.Next(1000),sgn3=rnd.Next(2),i3=rnd.Next(1000);
v1 = (double)Math.Pow(-1, sgn1)*(double)i1 / 100.0;
v2 = (double)Math.Pow(-1, sgn2)*(double)i2 / 100.0;
v3 = (double)Math.Pow(-1, sgn3)*(double)i3 / 100.0;
d.Set(v1, v2, v3);
}
}
#endregion End Rotator CLASS
#region RAngle CLASS
/// <summary>
/// The RAngle class provides storage of coefficients and methods for their calculation
/// to efficiently implement stage 2 of the Rotator implementation, which involves only
/// the angular parameter as a variable quantity. The stage 3 method, Rotate(), is in
/// this class, and it involves only the coordinates of a point as variable quantities.
/// All other calculations are complete by the time the stage 3 method is called, so
/// the remaining work left to do is minimal.
/// </summary>
public class RAngle
{
bool valid=false;
public double UX,UY,UZ,US,VX,VY,VZ,VS,WX,WY,WZ,WS,M,N,M1,Theta;
public RAxis Axis=null;
#region CONSTRUCTORS
/// <summary>
/// Four RAngle class constructors are provided. The first two initialize the rotation
/// angle to zero. The second two require that an RAxis instance be provided to the

6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595

/// constructor, and the first allocates an RAxis instance and stores its reference in
/// the RAngle.Axis field where it can be accessed later to set the rotation axis and
/// initialize rotations about it. The fourth differs from the first only in that it
/// initializes the angle to a value supplied to the consructor. Note that the default
/// axis direction when no RAxis instance is supplied to an RAngle constructor is the
/// X-axis, and the axis location is at the origin.
/// </summary>
public RAngle()
{
Axis = new RAxis(); Init(Axis, 0);
}
public RAngle(RAxis RotationAxis)
{
Init(RotationAxis, 0);
}
public RAngle(RAxis RotationAxis, double AngleInRadians)
{
Init(RotationAxis, AngleInRadians);
}
public RAngle(double AngleInRadians)
{
Axis = new RAxis(); Init(Axis, AngleInRadians);
}
private void Init(RAxis RotationAxis, double AngleInRadians)
{
if (RotationAxis == null) return;
Axis = RotationAxis;
SetAngle(AngleInRadians); valid = true;
}
#endregion
/// SetAngle <summary><para>
/// The SetAngle() method calculates the coefficients required by the Rotate() method
/// by calling an RAxis.ReturnParameters() method instance to initialize a rotation
/// about the axis which it defines. The design of the classes and methods is such that
/// the number of multiplication and division operations during repeated rotations using
/// the same axis and angle is nearly minimal; and this is achieved by separating the
/// work of calculation into three distinct stages, with the relevant sets of coefficients
/// required by each stage being partitioned into the classes where they are generated
/// and used. The stages are determined by (1) axis definition, (2) angle specification,
/// and (3) data point input followed by rotation and then output of the result. Any
/// set of parameters at one stage can be temporarily or permanently changed by use of
/// methods in (1) the RAxis class, (2) the RAngle class, and (3) the Rotator class.</para>
/// <para>
/// For garden-variety rotations and the simplest approach, use the Rotator() class
/// constructor and its SetAngle() and SetAxis() or SetAxisPlane() methods to set up
/// all three stages and their class instances, and carry out rotations and data access
/// using the Rotator methods Spin() and GetXYZ() or SetXYZ().</para>
/// </summary>
/// <param name="AngleInPiRadians">Rotation angle in radian measure</param>
public void SetAngle(double AngleInPiRadians)
{
Theta = AngleInPiRadians; M = Math.Cos(Theta); N = Math.Sin(Theta); M1 = M - 1;
if (valid)
{
if (Axis != null)
{
if (Axis.LocationOK & Axis.DirectionOK)
{

6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653

Axis.ReturnParameters(M,N,ref UX,ref UY,ref UZ,ref US,


ref VX,ref VY,ref VZ,ref VS,
ref WX,ref WY,ref WZ,ref WS);
}
}
}
}
/// <summary><para>
/// The Rotate() method requires three multiplications and four additions for each
/// component of a general rotation, and only five multiplications and five additions
/// for a ZX-p rotation. By the time Rotate() is called, 15 (or 7 for ZX-p)
/// multiplications at the angle specification stage have already been performed and
/// do not need to be repeated unless the rotation axis or the magnitude of the angle
/// need to be changed (for an angle of 2 degrees, for instance, eighteen calls to
/// Rotate() or to Rotator.Spin() can rotate a point by 36 degrees total without any
/// axis or angle recalculation, and this would only require 180 multiplcations and
/// 180 additions).</para>
/// <para>
/// Passing a set of X,Y,Z coordinates to Rotate() each time a rotation is performed
/// may be too involved for some cases, so the use of the Rotator methods Increment(),
/// Preview(), and Spin() is recommended if a simplistic approach is desired.</para>
/// <para>
/// See the following web page for a detailed derivation of the equations upon which
/// these calculation methods are based:</para>
/// <para>
/// http://cqvis.zxq.net/PRG/R3D1.html </para>
/// <para> </para>
/// </summary>
/// <param name="inX">X-coord of point to be rotated</param>
/// <param name="inY">Y-coord of point to be rotated</param>
/// <param name="inZ">Z-coord of point to be rotated</param>
public void Rotate(ref double inX, ref double inY, ref double inZ)
{
if (!valid) return;
double X = inX, Y = inY, Z = inZ;
if (Axis.vNonZero)
{
inX = UX*X + UY*Y + UZ*Z + US + Axis.AxPtX;
inY = VX*X + VY*Y + VZ*Z + VS + Axis.AxPtY;
inZ = WX*X + WY*Y + WZ*Z + WS + Axis.AxPtZ;
}
else
{
inX = UX*X +
UZ*Z - US;
inY =
VY*Y + VZ;
inZ = WX*X +
WZ*Z + WS;
}
}
public bool Valid
{
get { return valid; }
}
}
#endregion End RAngle CLASS
#region RAxis CLASS
/// <summary>
/// The RAxis class provides storage of coefficients and methods for their calculation
/// for stage 1 of the Rotator implementation, which involves only the location and

6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711

/// direction of the axis of rotation as variable quantities. Calculations in this


/// class do not have the value of any rotation angle to work with, and the work done
/// involves only making the angular calculations in stage 2 independent of the details
/// of axis location and orientation so that the point-transformation coefficients can
/// be generated for use in stage 3 without having to recalculate any quantities which
/// are constant for a given rotation axis.
///
/// If you should by some chance decide to manually rotate points, you can call SetLocation(),
/// SetDirection(), Calculate(), and ReturnParameters() and use the 12 coefficients returned
/// to construct a matrix or set of equations with which to perform the transformation.
/// If that isn't the case, use the Rotator or RAngle class methods, which will do all of
/// the work for you; and just ignore the Calculate() and ReturnParameters() methods, which
/// are used internally by the RAngle class methods.
///
/// If you allocate instances of the RAxis class, use SetLocation() and SetDirection() to
/// define (initialize) the rotation axis. It doesn't matter which is called first. As soon
/// as both have been called, RAxis.Calculate() will generate the coefficients needed by the
/// calls to the Rotator or RAngle classes during stage 2. If you don't define axes yourself
/// or redefine axis positions or directions, you won't need to call any methods in this class.
/// </summary>
public class RAxis
{
bool LocOK=false,DirOK=false,vnonzero=false;
public double AxPtX,AxPtY,AxPtZ,AxDirX,AxDirY,AxDirZ;
private double sgnE;
private double II,JJ,JL,GHx_JJm1,HHpGG_xJ,HHpGG_xJJ;
private double EG,EI,HK,GI,GK,EH,HI,IK,mJHI,mJGI,GKpEHJ,GHII,EGmHKJ,GGpHHJJ,HHpGGJJ,EHpGKJ,HKmEGJ;
private double GGII,HHII,LGI,LHI;
public RAxis()
{
SetLocation(0, 0, 0); SetDirection(1, 0, 0);
}
public void SetLocation(double LocationX, double LocationY, double LocationZ)
{
AxPtX = LocationX; AxPtY = LocationY; AxPtZ = LocationZ; LocOK = true;
if (DirOK) Calculate();
}
public void SetDirection(double DirectionX, double DirectionY, double DirectionZ)
{
AxDirX = DirectionX; AxDirY = DirectionY; AxDirZ = DirectionZ; DirOK = true;
if (LocOK) Calculate();
}
/// <summary>
/// The operations carried out (stage 1) by Calculate() only need to be performed when an axis
/// of rotation is defined or its position or direction is changed. Once these calculations
/// are completed, setting or changing a rotation angle (stage 2) does not require any
/// recalculation of the coefficients generated by this method.
/// </summary>
public void Calculate()
{
double s,v,E,F,G,H,I,J,K,L;
double GG,HH,GH,JH,IL,HHpGG;
v = Math.Sqrt(AxDirX*AxDirX+AxDirZ*AxDirZ); vnonzero = (v != 0);
s = Math.Sqrt(AxDirX*AxDirX+AxDirY*AxDirY+AxDirZ*AxDirZ);
if (s == 0) { DirOK = false; return; }
if (vnonzero) { G=AxDirZ/v; H=AxDirX/v; }
else { G=0; H=0; }
I=v/s; J=AxDirY/s; if (AxDirY<0) sgnE=-1; else sgnE=1;

6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769

E=AxPtZ*H-AxPtX*G; F=-AxPtX*H-AxPtZ*G;
K=-AxPtY*I-F*J;
GG=G*G; HH=H*H; II=I*I; JJ=J*J; HHpGG=HH+GG;
GH=G*H; GI=G*I; JH=J*H; EG=E*G; HK=H*K; HI=H*I;
GHx_JJm1=GH*(JJ-1); HHpGG_xJ=HHpGG*J; HHpGG_xJJ=HHpGG_xJ*J;
mJHI=-J*HI; mJGI=-J*GI; GHII=GI*HI; HHII=HI*HI;
EI=E*I; IK=I*K; GK=G*K; EH=E*H; GKpEHJ=GK+EH*J;
EGmHKJ=EG-HK*J; GGpHHJJ=GG+HH*JJ; HHpGGJJ=HH+GG*JJ; EHpGKJ=EH+GK*J;
HKmEGJ=HK-EG*J; GGII=GI*GI;
L=-AxPtY*J+F*I; IL=I*L; LGI=L*GI; LHI=L*HI; JL=L*J;
}
/// <summary>
/// ReturnParameters() is called by the RAngle class, and in most instances you won't
/// need to call it directly, so you might as well forget about it once you know what it
/// does.
///
/// In the general case this routine performs 15 multiplications and 18 additions (or 7
/// multiplications and 3 additions for ZX-p rotation) which constitute stage 2 of
/// a rotation. These calculations only need to be carried out when a new angle of
/// rotation is specified as long as the axis of rotation is unchanged.
/// </summary>
/// <param name="M">Cosine of the rotation angle</param>
/// <param name="N">Sine of the rotation angle</param>
public void ReturnParameters(double M, double N,
ref double UX,ref double UY,ref double UZ,ref double US,
ref double VX,ref double VY,ref double VZ,ref double VS,
ref double WX,ref double WY,ref double WZ,ref double WS)
{
if (vNonZero) // 15 multiplications and 18 additions here
{
double M1=M-1,HHpGG_xJxN=HHpGG_xJ*N, mJGIxM1=mJGI*M1, HIxN=HI*N, GIxN=GI*N;
double mJHIxM1=mJHI*M1, GHx_JJm1_xM=GHx_JJm1*M;
UX=GGpHHJJ*M+HHII; UY=mJHIxM1-GIxN; UZ=GHx_JJm1_xM+HHpGG_xJxN+GHII; US=EGmHKJ*M-GKpEHJ*N+LHI;
VX=mJHIxM1+GIxN; VY=II*M+JJ; VZ=mJGIxM1-HIxN; VS=IK*M+EI*N+JL;
WX=GHx_JJm1_xM-HHpGG_xJxN+GHII; WY=HIxN+mJGIxM1; WZ=HHpGGJJ*M+GGII; WS=HKmEGJ*N-EHpGKJ*M+LGI;
}
else // 7 multipications and 3 additions here
{
double MsgnE=M*sgnE,NsgnE=N*sgnE;
UX=M; UY=0.0; UZ=NsgnE; US=AxPtX*M+AxPtZ*NsgnE;
VX=0; VY=sgnE; VZ=-AxPtY*sgnE;
WX=-N; WY=0.0; WZ=MsgnE; WS=AxPtX*N-AxPtZ*MsgnE;
}
}
public bool LocationOK
{
get { return LocOK; }
set { LocOK = false; }
}
public bool DirectionOK
{
get { return DirOK; }
set { DirOK = false; }
}
public bool vNonZero
{
get { return vnonzero; }

6770
6771
6772
6773 }
6774
6775

}
}
#endregion

End RAxis class

Вам также может понравиться