-
Notifications
You must be signed in to change notification settings - Fork 1
/
dis6.js
13978 lines (11086 loc) · 447 KB
/
dis6.js
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if (typeof dis === "undefined")
dis = {};
// Support for node.js style modules; ignore if not using node.js require
if (typeof exports === "undefined")
exports = {};
dis.CoordinateConversion = function()
{
this.RADIANS_TO_DEGREES = 180.0/Math.PI;
this.DEGREES_TO_RADIANS = Math.PI/180.0;
this.a = 6378137.0; //semi major axis (WGS 84)
this.b = 6356752.3142; //semi minor axis (WGS 84)
/**
* Converts DIS xyz world coordinates to latitude and longitude (IN DEGREES). This algorithm may not be 100% accurate
* near the poles. Uses WGS84 , though you can change the ellipsoid constants a and b if you want to use something
* else. These formulas were obtained from Military Handbook 600008. The code itself has been
* translated from C to Java to Javascript over the years, so hold onto your hats.
*
* @param position {x:, y:, z:}
* @return {latitude:, longitude: altitude:}
*/
dis.CoordinateConversion.prototype.convertDisToLatLongInDegrees = function(position)
{
var x = position.x;
var y = position.y;
var z = position.z;
var answer = [];
answer[0] = 0.0;
answer[1] = 0.0;
answer[2] = 0.0;
var eSquared; //first eccentricity squared
var rSubN; //radius of the curvature of the prime vertical
var ePrimeSquared; //second eccentricity squared
var W = Math.sqrt((x*x + y*y));
var a = 6378137.0; // shorter variable names
var b = 6356752.3142;
eSquared = (a*a - b*b) / (a*a);
ePrimeSquared = (a*a - b*b) / (b*b);
/**
* Get the longitude.
*/
if(x >= 0 )
{
answer[1] = Math.atan(y/x);
}
else if(x < 0 && y >= 0)
{
answer[1] = Math.atan(y/x) + Math.PI;
}
else
{
answer[1] = Math.atan(y/x) - Math.PI;
}
/**
* Longitude calculation done. Now calculate latitude.
* NOTE: The handbook mentions using the calculated phi (latitude) value to recalculate B
* using tan B = (1-f) tan phi and then performing the entire calculation again to get more accurate values.
* However, for terrestrial applications, one iteration is accurate to .1 millimeter on the surface of the
* earth (Rapp, 1984, p.124), so one iteration is enough for our purposes
*/
var tanBZero = (a*z) / (b * W);
var BZero = Math.atan((tanBZero));
var tanPhi = (z + (ePrimeSquared * b * (Math.pow(Math.sin(BZero), 3))) ) /(W - (a * eSquared * (Math.pow(Math.cos(BZero), 3))));
var phi = Math.atan(tanPhi);
answer[0] = phi;
/**
* Latitude done, now get the elevation. Note: The handbook states that near the poles, it is preferable to use
* h = (Z / sin phi ) - rSubN + (eSquared * rSubN). Our applications are never near the poles, so this formula
* was left unimplemented.
*/
rSubN = (a * a) / Math.sqrt(((a * a) * (Math.cos(phi)*Math.cos(phi)) + ((b*b) * (Math.sin(phi)*Math.sin(phi)))));
answer[2] = (W / Math.cos(phi)) - rSubN;
var result = {latitude:answer[0] * this.RADIANS_TO_DEGREES, longitude:answer[1] * this.RADIANS_TO_DEGREES, altitude:answer[2]};
return result;
};
/**
* Converts lat long and geodetic height (elevation) into DIS XYZ
* This algorithm also uses the WGS84 ellipsoid, though you can change the values
* of a and b for a different ellipsoid. Adapted from Military Handbook 600008
* @param latLonAlt {lat: lon: alt:} in degrees and meters
* @return {x: y: z:} in meters
*/
dis.CoordinateConversion.prototype.getXYZfromLatLonAltDegrees = function(latLonAlt)
{
var latitudeRadians = latLonAlt.lat * this.DEGREES_TO_RADIANS;
var longtitudeRadians = latLonAlt.lon * this.DEGREES_TO_RADIANS;
var cosLat = Math.cos(latitudeRadians);
var sinLat = Math.sin(latitudeRadians);
var rSubN = (this.a * this.a) / Math.sqrt(((this.a * this.a) * (cosLat * cosLat) + ((this.b * this.b) * (sinLat*sinLat))));
var X = (rSubN + latLonAlt.alt) * cosLat * Math.cos(longtitudeRadians);
var Y = (rSubN + latLonAlt.alt) * cosLat * Math.sin(longtitudeRadians);
var Z = ((((this.b * this.b) / (this.a * this.a)) * rSubN) + latLonAlt.alt) * sinLat;
return {x:X, y:Y, z:Z};
};
};
exports.CoordinateConversion = dis.CoordinateConversion;
/**
* Obsolete--the code generation now includes methods for accessing bit
* fields such as this. Remains only for backward compatiblity, and I doubt
* anyone is using it.
*
* Some code to extract the entity apperance bit fields.<p>
*
* The entityAppearance field in the espdu is a 32 bit integer. To save
* space, several different fields are contained within it.
* Specifically:
*
* Name bit position Purpose
* ---- ------------ --------
* Paint 0 0 = uniform color, 1=camo
* Mobility 1 0 = no mobility kill, 1 = mobility kill
* Fire Power 2 0 = no firepower kill, 1 = firepower kill
* Damage 3-4 0=no damange, 1=slight, 2=moderate, 3=destroyed
* Smoke 5-6 0=not smoking, 1=smoke plume, 2=emitting engine smoke, 3=engine smoke + smoke plume
* Trailing effects 7-8 dust cloud, 0=none, 1=small, 2=medium, 3=large
* hatch 9-11 0=NA, 1=hatch closed, 2=popped, 3=popped + person visible, 4=open, 5=open and visible
* head lights 12 0=off, 1=on
* tail light 13 0=off, 1=on
* brake lights 14 0=off, 1=on
* flaming 15 0=none, 1=flames present
* launcher 16 0=not raised, 1=raised
* camo type 17-18 0=desert, 1=winter, 2=forest
* concealed 19 0=not concealed, 1=prepared concealed position (netting, etc)
* frozen status 20 0=not frozen, 1=frozen (in simulation terms)
* power plant 22 0=power plant off 1=on
* state 23 0=active, 1=deactivated
* tent 24 0=not extended 1=extended
* ramp 25 0=not extended, 1=extended
* blackout lights 26 0=off, 1=on
* blackout brake 27 0=off, 1=on
* spot lights 28 0=off, 1=on
* interior lights 29 0=off, 1=on
* unused 30-31
*
* Typical use:
*
* var entityAppearance = new DisAppearance(espdu.entityAppearance);
* var damage = entityAppearance.getBitfield(3, 4);
*
* This returns the "damage" bitfield in bits 3-4.
*
* var mobility = entityAppearance.getBitfield(1, 1);
*
* Returns the mobility field, 0 = no mobo kill, 1 = mobility kill
*
* @author DMcG
**/
if (typeof dis === "undefined")
dis = {};
// Support for node.js style modules; ignore if not using node.js require
if (typeof exports === "undefined")
exports = {};
/** Constructor. Takes the integer value extracted from the DIS Entity State Field appearance
*
* @param {type} integerValue the entity appearance from the espdu
* @returns {undefined}
*/
dis.DisAppearance = function(integerValue)
{
this.entityAppearance = integerValue;
}
/**
* Test code for creating the correct bitmask
* @returns {undefined}
*/
dis.DisAppearance.prototype.getTestMask = function()
{
mask = 0;
for(var idx = 0; idx < 7; idx++)
{
mask = mask + this.bit_set(mask, idx);
}
return mask;
};
/**
*
* @param {integer} startPosition
* @param {integer} finishPosition
* @returns {integer}
*/
dis.DisAppearance.prototype.getBitField = function(startPosition, finishPosition)
{
// do some sanity checks
if(startPosition < 0 || startPosition > 31 || finishPosition < 0 || finishPosition > 31 || startPosition > finishPosition)
{
console.log("invalid start or finish for bitfield values: ", startPosition, " ", finishPosition);
return 0;
}
// Develop the mask. Addition is equivalent to setting multiple bits.
var mask = 0;
for(var idx = startPosition; idx <= finishPosition; idx++)
{
mask = mask + this.bit_set(0, idx);
}
// do the bitmask
var maskedValue = this.entityAppearance & mask;
// Shift bits to get the normalized value
var fieldValue = maskedValue >>> startPosition;
return fieldValue;
};
/** Set the "bit" position in a number to 1
*
* @param {integer} num the number whose bit we are setting. Typically zero.
* @param {integer} bit which bit to set
* @return {integer} the number passed in, with the "bit"th bit flipped on.
**/
dis.DisAppearance.prototype.bit_set = function(num, bit)
{
return num | 1<<bit;
}
exports.DisAppearance = dis.DisAppearance;
//var BigInteger = require('BigInteger');
if (typeof dis === "undefined")
dis = {};
// Support for node.js style modules; ignore if not using node.js require
if (typeof exports === "undefined")
exports = {};
dis.InputStream = function(binaryData)
{
this.dataView = new DataView(binaryData, 0); // data, byte offset
this.currentPosition = 0; // ptr to "current" position in array
dis.InputStream.prototype.readUByte = function()
{
var data = this.dataView.getUint8(this.currentPosition);
this.currentPosition = this.currentPosition + 1;
return data;
};
dis.InputStream.prototype.readByte = function()
{
var data = this.dataView.getInt8(this.currentPosition);
this.currentPosition = this.currentPosition + 1;
return data;
};
dis.InputStream.prototype.readUShort = function()
{
var data = this.dataView.getUint16(this.currentPosition);
this.currentPosition = this.currentPosition + 2;
return data;
};
dis.InputStream.prototype.readShort = function()
{
var data = this.dataView.getInt16(this.currentPosition);
this.currentPosition = this.currentPosition + 2;
return data;
};
dis.InputStream.prototype.readUInt = function()
{
var data = this.dataView.getUint32(this.currentPosition);
this.currentPosition = this.currentPosition + 4;
return data;
};
dis.InputStream.prototype.readInt = function()
{
var data = this.dataView.getInt32(this.currentPosition);
this.currentPosition = this.currentPosition + 4;
return data;
};
/** Read a long integer. Assumes big endian format. Uses the BigInteger package. */
dis.InputStream.prototype.readLongInt = function()
{
var data1 = this.dataView.getInt32(this.currentPosition);
var data2 = this.dataView.getInt32(this.currentPosition + 4);
this.currentPosition = this.currentPosition + 8;
};
dis.InputStream.prototype.readFloat32 = function()
{
var data = this.dataView.getFloat32(this.currentPosition);
this.currentPosition = this.currentPosition + 4;
return data;
};
dis.InputStream.prototype.readFloat64 = function()
{
var data = this.dataView.getFloat64(this.currentPosition);
this.currentPosition = this.currentPosition + 8;
return data;
};
dis.InputStream.prototype.readLong = function()
{
console.log("Problem in dis.InputStream. Javascript cannot natively handle 64 bit ints");
console.log("Returning 0 from read, which is almost certainly wrong");
this.currentPosition = this.currentPosition + 8;
return 0;
};
};
exports.InputStream = dis.InputStream;
if (typeof dis === "undefined")
dis = {};
// Support for node.js style modules; ignore if not using node.js require
if (typeof exports === "undefined")
exports = {};
/**
* @param binaryDataBuffer ArrayBuffer
*/
dis.OutputStream = function(binaryDataBuffer)
{
this.binaryData = binaryDataBuffer;
this.dataView = new DataView(this.binaryData); // data, byte offset
this.currentPosition = 0; // ptr to current position in array
/**
* Returns a byte array trimmed to the maximum number of bytes written
* to the stream. Eg, if we initialize with a 500 byte bufer, and we
* only write 10 bytes to the output stream, this will return the first
* ten bytes of the array.
*
* @returns {ArrayBuffer} Only the data written
*/
dis.OutputStream.prototype.toByteArray = function()
{
var trimmedData = this.binaryData.slice(0, this.currentPosition);
return trimmedData;
};
dis.OutputStream.prototype.writeUByte = function(userData)
{
this.dataView.setUint8(this.currentPosition, userData);
this.currentPosition = this.currentPosition + 1;
};
dis.OutputStream.prototype.writeByte = function(userData)
{
this.dataView.setInt8(this.currentPosition, userData);
this.currentPosition = this.currentPosition + 1;
};
dis.OutputStream.prototype.writeUShort = function(userData)
{
this.dataView.setUint16(this.currentPosition, userData);
this.currentPosition = this.currentPosition + 2;
};
dis.OutputStream.prototype.writeShort = function(userData)
{
this.dataView.setInt16(this.currentPosition, userData);
this.currentPosition = this.currentPosition + 2;
};
dis.OutputStream.prototype.writeUInt = function(userData)
{
this.dataView.setUint32(this.currentPosition, userData);
this.currentPosition = this.currentPosition + 4;
};
dis.OutputStream.prototype.writeInt = function(userData)
{
this.dataView.setInt32(this.currentPosition, userData);
this.currentPosition = this.currentPosition + 4;
};
dis.OutputStream.prototype.writeFloat32 = function(userData)
{
this.dataView.setFloat32(this.currentPosition, userData);
this.currentPosition = this.currentPosition + 4;
};
dis.OutputStream.prototype.writeFloat64 = function(userData)
{
this.dataView.setFloat64(this.currentPosition, userData);
this.currentPosition = this.currentPosition + 8;
};
dis.OutputStream.prototype.writeLong = function(userData)
{
console.log("Problem in dis.outputStream. Javascript cannot natively handle 64 bit ints");
console.log("writing 0, which is almost certainly wrong");
this.dataView.setInt32(this.currentPosition, 0);
this.dataView.setInt32(this.currentPosition + 4, 0);
this.currentPosition = this.currentPosition + 8;
};
};
exports.OutputStream = dis.OutputStream;
if (typeof dis === "undefined")
dis = {};
// Support for node.js style modules; ignore if not using node.js require
if (typeof exports === "undefined")
exports = {};
/**
* The PDU factory is responsible for decoding binary data and turning
* it into the appropriate type of PDU.
*
* The websocket will typically send the web page a IEEE 1278.1 binary
* array of data. It could be any one of dozens of PDUs. The start of
* all PDUs is the same--they have the same header. One of the fields in
* the header is the PduType, an 8 bit integer with a unqiue value for
* each type of PDU. We have to peak at that value, decide what type
* of PDU to create of the binary we have received, and then decode it.
*
* * @DMcG
*/
dis.PduFactory = function()
{
};
/**
* decode incoming binary data and
* return the correct type of PDU.
*
* @param {type} data the IEEE 1278.1 binary data
* @returns {Pdu} Returns an instance of some PDU, be it espdu, fire, detonation, etc. Exception if PduType not known.
*/
dis.PduFactory.prototype.createPdu = function(data)
{
var asUint8Array = new Uint8Array(data);
var pduType = asUint8Array[2];
var inputStream = new dis.InputStream(data);
var newPdu = null;
//try
//{
switch(pduType)
{
case 1: // entity state PDU
newPdu = new dis.EntityStatePdu();
newPdu.initFromBinary(inputStream);
break;
case 2: // Fire
newPdu = new dis.FirePdu();
newPdu.initFromBinary(inputStream);
break;
case 3: // detonation
newPdu = new dis.DetonationPdu();
newPdu.initFromBinary(inputStream);
break;
case 4: // Collision
newPdu = new dis.CollisionPdu();
newPdu.initFromBinary(inputStream);
break;
case 11: // Create entity
newPdu = new dis.CreateEntityPdu();
newPdu.initFromBinary(inputStream);
break;
case 12: // Remove entity
newPdu = new dis.RemoveEntityPdu();
newPdu.initFromBinary(inputStream);
break;
case 20: // data
newPdu = new dis.DataPdu();
newPdu.initFromBinary(inputStream);
break;
default:
throw "PduType: " + pduType + " Unrecognized PDUType. Add PDU in dis.PduFactory.";
}
//}
// This also picks up any errors decoding what we though was a "normal" PDU
//catch(error)
//{
// newPdu = null;
//}
return newPdu;
};
dis.PduFactory.prototype.getPdusFromBundle = function(data)
{
}
exports.PduFactory = dis.PduFactory;
/**
* Sets up a local tangent place (ENU) coordinate system at a given location
* and altitude, and handles conversions between geodetic, ECEF, and local
* tangent plane coordinate systems.
*
* For reference see "Conversion of Geodetic coordinates to the Local
* Tangent Plane", version 2.01,
* http://www.psas.pdx.edu/CoordinateSystem/Latitude_to_LocalTangent.pdf
*
* and "Geodetic Systems",
* http://wiki.gis.com/wiki/index.php/Geodetic_system#From_geodetic_coordinates_to_local_ENU_coordinates
*
* There's also a bunch of ancient code from older versions that someone, somewhere,
* lifted from a military handbook, originally written in C, translated to Java,
* and now translated to Javascript.
*
* Terminology:
*
* ECEF: earth centered, earth fixed coordinate system, same as DIS. Cartesian,
* origin at center of the earth, z through north pole, x out the equator and
* prime meridian, y out equator and 90 deg east. This coordinate system rotates
* with the earth, ie the x axis always points out the prime meridian and equator
* even as the earth rotates.
*
* Geodetic: latitude, longitude, altitude.
*
* WGS84: Shape of the earth, an ellipsoid roughly, with a and b the semimajor and semiminor axes
*
* ENU: East, North, Up: local coordinate system with a given geodetic origin. Tangent
* plane to the earth.
*
* All Errors mine
*
* @DMcG
*
* @param {float} lat latitude in degrees of the origin of the local tangent plane coordinate system
* @param {float} lon longitude, in degrees, of origin
* @param {float} alt altitude, in meters, of the origin of the local tangent plane coordinate system
*/
if (typeof dis === "undefined")
dis = {};
// Support for node.js style modules; ignore if not using node.js require
if (typeof exports === "undefined")
exports = {};
/** Constructor, creates an object that can do coordinate systems conversions.
* Takes a geodetic point that is the origin of a tangent plane to the surface
* of the earth. This is useful for doing local simulation work. The local
* coordinate system has postive x east, positive y north, and positive Z up,
* aka an ENU coordinate system. Methods for converting from that coordinate system
* to the DIS (ECEF) coordinate system or geotetic coordinate systems are provided.
*
* @param {type} lat latitude, in degrees, of where the local tangent plane is located
* @param {type} lon longitude, in degrees, of the origin of the local tangent plane
* @param {type} alt altitude, in meters, of the origin of the local tangent plane
* @returns {RangeCoordinates} An object that can do coordinate system conversions
*/
dis.RangeCoordinates = function(lat, lon, alt)
{
this.RADIANS_PER_DEGREE = 2 * Math.PI / 360.0;
this.DEGREES_PER_RADIAN = 360.0 / (2* Math.PI);
/** WGS84 semimajor axis (constant) */
this.a = 6378137.0;
/** WGS84 semiminor axis (constant) */
this.b = 6356752.3142;
/** Ellipsoidal Flatness (constant) */
this.f = (this.a - this.b) / this.a; // Should be 3.3528107 X 10^-3
/** Eccentricity (constant) */
this.e = Math.sqrt(this.f * (2 - this.f)); // Should be 8.1819191 X 10^-2
// The origin of the local, East-North-Up (ENU) coordinate system, in lat/lon degrees and meters.
this.ENUOrigin = {};
this.ENUOrigin.latitude = lat;
this.ENUOrigin.longitude = lon;
this.ENUOrigin.altitude = alt;
// Find the origin of the ENU in earth-centered, earth-fixed ECEF aka DIS coordinates
this.ENUOriginInECEF = {};
this.ENUOriginInECEF = this.latLonAltDegreesToECEF(lat, lon, alt);
};
/** Determines N, the distance from a normal plane at the given
* latitude to the Z-axis running through the center of the earth.
* This is NOT the same as the distance to the center of the earth.
*
* @param {float} lambda the latitude, in radians.
* @returns {float} distance in meters from the latitude to the axis of the earth
*/
dis.RangeCoordinates.prototype.N = function(lambda)
{
//N(lambda) = a / sqrt( 1 - e^2 * sin^2(lambda) )
var val = this.a / Math.sqrt(1- ( Math.pow(this.e, 2) * Math.pow( Math.sin(lambda), 2) ) );
return val;
};
/**
* Converts a latitude, longitude, and altitude object to DIS rectilinear
* coordinates, aka earth-centered, earth-fixed, rectilinear.
*
* @param {latitude:longitude:altitude:} latLonAlt The lat/lon/alt, in degrees and meters
* @returns {x, y, z} rectilienar coordinates in ECEF, aka DIS coordinates
*/
dis.RangeCoordinates.prototype.latLonAltDegreesObjectToECEF = function(latLonAlt)
{
return this.latLonAltDegreesToECEF(latLonAlt.latitude, latLonAlt.longitude, latLonAlt.altitude);
};
/**
* Converts a latitude, longitude, and altitude to DIS rectilinear
* coordinates, aka earth-centered, earth-fixed, rectilinear.
*
* @param {float} latitude (in radians)
* @param {float} longitude (in radians)
* @param {float} altitude (in meters)
* @returns {x, y, z} rectilienar coordinates in ECEF-r, aka DIS coordinates
*/
dis.RangeCoordinates.prototype.latLonAltRadiansToECEF = function(latitude, longitude, altitude)
{
/*
// altitude corresponds to h in the paper, lambda to latitude, phi to longitude
var x = (altitude + this.N(latitude)) * Math.cos(latitude) * Math.cos(longitude);
var y = (altitude + this.N(latitude)) * Math.cos(latitude) * Math.sin(longitude);
var z = (altitude + (1 - Math.pow(this.e, 2) ) * this.N(latitude)) * Math.sin(longitude);
var coordinates = {};
coordinates.x = x;
coordinates.y = y;
coordinates.z = z;
*/
var cosLat = Math.cos(latitude);
var sinLat = Math.sin(latitude);
var rSubN = (this.a*this.a) / Math.sqrt(((this.a*this.a) * (cosLat*cosLat) + ((this.b*this.b) * (sinLat*sinLat))));
var X = (rSubN + altitude) * cosLat * Math.cos(longitude);
var Y = (rSubN + altitude) * cosLat * Math.sin(longitude);
var Z = ((((this.b*this.b) / (this.a*this.a)) * rSubN) + altitude) * sinLat;
return {x:X, y:Y, z:Z};
};
/*
*
* @param {type} latitude in degrees
* @param {type} longitude in degrees
* @param {type} altitude in meters
* @returns {x,y,z} coordinates in ECEF, in meters aka DIS global coordinates
*/
dis.RangeCoordinates.prototype.latLonAltDegreesToECEF = function(latitude, longitude, altitude)
{
return this.latLonAltRadiansToECEF(latitude * this.RADIANS_PER_DEGREE, longitude * this.RADIANS_PER_DEGREE, altitude);
};
/**
* Converts DIS xyz world coordinates to latitude and longitude (IN DEGREES). This algorithm may not be 100% accurate
* near the poles. Uses WGS84 , though you can change the ellipsoid constants a and b if you want to use something
* else. These formulas were obtained from Military Handbook 600008. The code itself has been
* translated from C to Java to Javascript over the years, so hold onto your hats. (This is
* copied from other sources than those listed above. Seems to work, though.)
*
* @param position {x:, y:, z:}
* @return {latitude:, longitude: altitude:}
*/
dis.RangeCoordinates.prototype.ECEFObjectToLatLongAltInDegrees = function(position)
{
var x = position.x;
var y = position.y;
var z = position.z;
var answer = [];
answer[0] = 0.0;
answer[1] = 0.0;
answer[2] = 0.0;
var a = this.a; // semi major axis (WGS 84)
var b = this.b; //semi minor axis (WGS 84)
var eSquared; //first eccentricity squared
var rSubN; //radius of the curvature of the prime vertical
var ePrimeSquared;//second eccentricity squared
var W = Math.sqrt((x*x + y*y));
eSquared = (a*a - b*b) / (a*a);
ePrimeSquared = (a*a - b*b) / (b*b);
/**
* Get the longitude.
*/
if(x >= 0 )
{
answer[1] = Math.atan(y/x);
}
else if(x < 0 && y >= 0)
{
answer[1] = Math.atan(y/x) + Math.PI;
}
else
{
answer[1] = Math.atan(y/x) - Math.PI;
}
/**
* Longitude calculation done. Now calculate latitude.
* NOTE: The handbook mentions using the calculated phi (latitude) value to recalculate B
* using tan B = (1-f) tan phi and then performing the entire calculation again to get more accurate values.
* However, for terrestrial applications, one iteration is accurate to .1 millimeter on the surface of the
* earth (Rapp, 1984, p.124), so one iteration is enough for our purposes
*/
var tanBZero = (a*z) / (b * W);
var BZero = Math.atan((tanBZero));
var tanPhi = (z + (ePrimeSquared * b * (Math.pow(Math.sin(BZero), 3))) ) /(W - (a * eSquared * (Math.pow(Math.cos(BZero), 3))));
var phi = Math.atan(tanPhi);
answer[0] = phi;
/**
* Latitude done, now get the elevation. Note: The handbook states that near the poles, it is preferable to use
* h = (Z / sin phi ) - rSubN + (eSquared * rSubN). Our applications are never near the poles, so this formula
* was left unimplemented.
*/
rSubN = (a*a) / Math.sqrt(((a*a) * (Math.cos(phi)*Math.cos(phi)) + ((b*b) * (Math.sin(phi)*Math.sin(phi)))));
answer[2] = (W / Math.cos(phi)) - rSubN;
var ld = answer[0] * this.DEGREES_PER_RADIAN;
var lnd = answer[1] * this.DEGREES_PER_RADIAN;
var result = {latitude:ld, longitude:lnd, altitude:answer[2]};
return result;
};
/**
* Converts an ECEF position to the local ENU coordinate system. Units are meters,
* and the origin of the ENU coordinate system is set in the constructor.
*
* @param {x:y:z:} ecefPosition ecef position (in meters)
* @returns {x:y:z:} object with x, y, and z local coordinates, ENU
*/
dis.RangeCoordinates.prototype.ECEFObjectToENU = function(ecefPosition)
{
return this.ECEFtoENU(ecefPosition.x, ecefPosition.y, ecefPosition.z);
};
/**
* Converts an ECEF position to the local ENU coordinate system. Units are meters,
* and the origin of the ENU coordinate system is set in the constructor.
*
* @param {float} X the X coordinate of the ECEF position
* @param {float} Y the Y coordinate
* @param {float} Z the Z coordinate
* @returns {x:y:z:} object with x, y, and z local coordinates, ENU
*/
dis.RangeCoordinates.prototype.ECEFtoENU = function(X, Y, Z)
{
// Origin of ENU tangent plane coordinate system in ECEF coordinate system
var Xr = this.ENUOriginInECEF.x;
var Yr = this.ENUOriginInECEF.y;
var Zr = this.ENUOriginInECEF.z;
var originLonRadians = this.ENUOrigin.longitude * this.RADIANS_PER_DEGREE;
var originLatRadians = this.ENUOrigin.latitude * this.RADIANS_PER_DEGREE;
e = -(Math.sin(originLonRadians)) * (X-Xr) + Math.cos(originLonRadians) * (Y-Yr);
n = -(Math.sin(originLatRadians)) * Math.cos(originLonRadians) * (X-Xr) - Math.sin(originLatRadians) * Math.sin(originLonRadians) * (Y-Yr) + Math.cos(originLatRadians) * (Z-Zr);
u = Math.cos(originLatRadians) * Math.cos(originLonRadians) * (X-Xr) + Math.cos(originLatRadians) * Math.sin(originLonRadians) * (Y-Yr) + Math.sin(originLatRadians) * (Z-Zr);
// Local coordinate system x, y, z
return {x:e, y:n, z:u};
};
/**
* Converts a local coordinate system / ENU/ Local Tangent Plane object to ECEF, aka DIS coordinates.
*
* @param enuPosition {x:y:z:} local coordinate object
* @returns {x:y:z:} point in ECEF / DIS coordinate system
*/
dis.RangeCoordinates.prototype.ENUObjectToECEF = function(enuPosition)
{
return this.ENUtoECEF(enuPosition.x, enuPosition.y, enuPosition.z);
};
/**
* Converts a local coordinate system / ENU/ Local Tangent Plane point to ECEF, aka DIS coordinates.
*
* @param localX {float} local coordinate system X
* @param localY {float} local coordinate system Y
* @param localZ {float} local coordinate system Z
* @returns {x:y:z:} point in ECEF / DIS coordinate system
*/
dis.RangeCoordinates.prototype.ENUtoECEF = function(localX, localY, localZ)
{
// ENU local coordinate system origin, in ECEF
var Xr = this.ENUOriginInECEF.x;
var Yr = this.ENUOriginInECEF.y;
var Zr = this.ENUOriginInECEF.z;
var refLong = this.ENUOrigin.longitude;
var refLat = this.ENUOrigin.latitude;
/** original code this was copied from
function [X, Y, Z] = enu2xyz(refLat, refLong, refH, e, n, u)
% Convert east, north, up coordinates (labeled e, n, u) to ECEF
% coordinates. The reference point (phi, lambda, h) must be given. All distances are in metres
[Xr,Yr,Zr] = llh2xyz(refLat,refLong, refH); % location of reference point
X = -sin(refLong)*e - cos(refLong)*sin(refLat)*n + cos(refLong)*cos(refLat)*u + Xr;
Y = cos(refLong)*e - sin(refLong)*sin(refLat)*n + cos(refLat)*sin(refLong)*u + Yr;
Z = cos(refLat)*n + sin(refLat)*u + Zr;
*/
X = -(Math.sin(refLong)) * localX - Math.cos(refLong) * Math.sin(refLat) * localY + Math.cos(refLong) * Math.cos(refLat) * localZ + Xr;
Y = Math.cos(refLong) * localX - Math.sin(refLong) * Math.sin(refLat) * localY + Math.cos(refLat) * Math.sin(refLong) * localZ + Yr;
Z = Math.cos(refLat) * localY + Math.sin(refLat) * localZ + Zr;
return {x:X, y:Y, z:Z};
};
exports.RangeCoordinates = dis.RangeCoordinates;if (typeof dis === "undefined")
dis = {};
// Support for node.js style modules; ignore if not using node.js require
if (typeof exports === "undefined")
exports = {};
/**
* Utility class that converts between strings and the DIS ESPDU marking
* field. The marking field is 12 bytes long, with the first byte being
* the character set used, and the remaining 11 bytes character codes in
* that character set. This is often used for debugging or "billboard"
* displays in 3D; it's intended for humans. The string character values
* are clamped (or filled) to exactly 11 bytes, so "This is a long string"
* will be clamped to "This is a l" (in charachter codes) and "foo" will
* be filled to "foo\0\0\0\0\0\0\0\0".<p>
*
* It is recommended that only ASCII character set (character set = 1)
* be used.
*
* @returns {undefined}
*/
dis.StringConversion = function()
{
};
/**
* Given a string, returns a DIS marking field. The character set is set to
* 1, for ascii. The length is clamped to 11, and zero-filled if the string
* is shorter than 11.
*
* @returns {array} disMarking field, 12 bytes long, character set = 1 (ascii) in 0, zero-filled to 11 character codes
*/
dis.StringConversion.prototype.StringToDisMarking = function(markingString)
{
var byteMarking = [];
// character set 1 = ascii
byteMarking.push(1);
var markingLength = markingString.length;
// Clamp it to 11 bytes of character data
if(markingLength > 11)
markingLength = 11;
// If the string is shorter than 11 bytes, we zero-fill the array
var diff = 11 - markingLength;
for(var idx = 0; idx < markingLength; idx++)
{
byteMarking.push(markingString.charCodeAt(idx));
}
for(var idx = markingLength; idx < 11; idx++)
{
byteMarking.push(0);
}
return byteMarking;
};
/**
* Given a DIS marking field, returns a string. Assumes always ascii.
*
* @param {array} disMarking dis marking field, [0] = character set, the rest character codes
* @returns {string} string equivalent of the marking field
*/
dis.StringConversion.prototype.DisMarkingToString = function(disMarking)
{
var marking = "";
for(var idx = 1; idx < disMarking.length; idx++)
{
marking = marking + String.fromCharCode(disMarking[idx]);
}
return marking;
};
// This is a temporary placeholder until full require.js code
// support is present.
if (typeof exports === "undefined")
exports = {};
exports.RangeCoordinates = dis.RangeCoordinates;
exports.InputStream = dis.InputStream;
exports.OutputStream = dis.OutputStream;
/**
* Section 5.3.6.5. Acknowledge the receiptof a start/resume, stop/freeze, or RemoveEntityPDU. COMPLETE
*
* Copyright (c) 2008-2015, MOVES Institute, Naval Postgraduate School. All rights reserved.
* This work is licensed under the BSD open source license, available at https://www.movesinstitute.org/licenses/bsd.html
*
* @author DMcG
*/
// On the client side, support for a namespace.
if (typeof dis === "undefined")
dis = {};
// Support for node.js style modules. Ignored if used in a client context.
// See http://howtonode.org/creating-custom-modules
if (typeof exports === "undefined")
exports = {};
dis.AcknowledgePdu = function()
{
/** The version of the protocol. 5=DIS-1995, 6=DIS-1998. */
this.protocolVersion = 6;
/** Exercise ID */
this.exerciseID = 0;
/** Type of pdu, unique for each PDU class */
this.pduType = 15;
/** value that refers to the protocol family, eg SimulationManagement, et */
this.protocolFamily = 5;
/** Timestamp value */
this.timestamp = 0;
/** Length, in bytes, of the PDU. Changed name from length to avoid use of Hibernate QL reserved word */
this.pduLength = 0;
/** zero-filled array of padding */
this.padding = 0;
/** Entity that is sending message */
this.originatingEntityID = new dis.EntityID();
/** Entity that is intended to receive message */
this.receivingEntityID = new dis.EntityID();
/** type of message being acknowledged */
this.acknowledgeFlag = 0;
/** Whether or not the receiving entity was able to comply with the request */
this.responseFlag = 0;
/** Request ID that is unique */
this.requestID = 0;
dis.AcknowledgePdu.prototype.initFromBinary = function(inputStream)
{
this.protocolVersion = inputStream.readUByte();
this.exerciseID = inputStream.readUByte();
this.pduType = inputStream.readUByte();
this.protocolFamily = inputStream.readUByte();
this.timestamp = inputStream.readUInt();
this.pduLength = inputStream.readUShort();
this.padding = inputStream.readShort();
this.originatingEntityID.initFromBinary(inputStream);
this.receivingEntityID.initFromBinary(inputStream);