< Summary - ReFlex - Library

Information
Class: ReFlex.Core.Tuio.Components.TuioMessageBuilder
Assembly: ReFlex.Core.Tuio
File(s): D:\a\reflex\reflex\library\src\Core\Tuio\Components\TuioMessageBuilder.cs
Line coverage
83%
Covered lines: 136
Uncovered lines: 26
Coverable lines: 162
Total lines: 256
Line coverage: 83.9%
Branch coverage
54%
Covered branches: 12
Total branches: 22
Branch coverage: 54.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CreateTuio20Messages(...)100%66100%
CreateTuio11Messages(...)100%66100%
GenerateStringRepresentation(...)100%210%
GenerateStringRepresentation(...)0%2040%
GenerateStringRepresentation(...)0%4260%

File(s)

D:\a\reflex\reflex\library\src\Core\Tuio\Components\TuioMessageBuilder.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using CoreOSC;
 5using ReFlex.Core.Tuio.Interfaces;
 6using ReFlex.Core.Tuio.Util;
 7
 8namespace ReFlex.Core.Tuio.Components
 9{
 10    /// <summary>
 11    /// Constructs Messages according to either TUIO 1.1 or TUIO 2.0 specification.
 12    /// </summary>
 13    public class TuioMessageBuilder : ITuioMessageBuilder
 14    {
 15        /// <summary>
 16        /// creates the messages for the tuio bundle according to TUIO 2.0 specification <see cref="http://www.tuio.org/
 17        /// Bundle contains:
 18        ///
 19        /// in case of <see cref="interpretation"/> = <see cref="TuioInterpretation.Point3D"/>:
 20        /// <code>
 21        /// [1] /tuio2/frm frame_id time dimension source
 22        /// [2] /tuio2/p3d session_id 1 touch_id pos_x pos_y pos_z 0 0 1 1
 23        /// ...
 24        /// [n] /tuio2/alv session_id
 25        /// </code>
 26        ///
 27        /// in case of <see cref="interpretation"/> = <see cref="TuioInterpretation.TouchPoint2DwithPressure"/>:
 28        /// <code>
 29        /// [1] /tuio2/frm frame_id time dimension source
 30        /// [2] /tuio2/ptr session_id 1 touch_id pos_x pos_y 0 0 1 pos_z
 31        /// ...
 32        /// [n] /tuio2/alv session_id
 33        /// </code>
 34        /// </summary>
 35        /// <param name="parameters"><see cref="TuioParameters"/> containing interactions of current frame and other met
 36        /// <param name="interpretation"><see cref="TuioInterpretation"/> specifying the message format.</param>
 37        /// <returns>List of all <see cref="OscMessage"/> to create bundle for transfer to TUIO server</returns>
 38        public IEnumerable<OscMessage> CreateTuio20Messages(TuioParameters parameters, TuioInterpretation interpretation
 1039        {
 1040            var messages = new List<OscMessage>();
 41
 1042            var messageFrame = new OscMessage(
 1043                address: new Address("/tuio2/frm"),
 1044                arguments: new object[]
 1045                {
 1046                     parameters.FrameId,
 1047                     parameters.Time,
 1048                     parameters.Dimension,
 1049                     parameters.Source
 1050                });
 51
 1052            messages.Add(messageFrame);
 53
 1054            var posAddress = interpretation == TuioInterpretation.Point3D
 1055                ? "/tuio2/p3d"
 1056                : "/tuio2/ptr";
 57
 4258            foreach (var pos in parameters.Positions)
 659            {
 660                var arguments = interpretation == TuioInterpretation.Point3D
 661                    ?
 662                        new object[]
 663                        {
 664                            parameters.SessionId,       // session_id
 665                            1,                          // tu_id --> unknown finger == right index finger
 666                            pos.Item1,                  // c_id = touch id
 667                            pos.Item2,                  // x_pos
 668                            pos.Item3,                  // y_pos
 669                            pos.Item4,                  // z_pos
 670                            0.0f,                       // x_axis
 671                            0.0f,                       // y_axis
 672                            1.0f,                       // z_axis --> [0,0,-1] -> pointing towards sensor
 673                            1f                          // radius: 1
 674                        }
 675                    :
 676                        new object[]
 677                         {
 678                             parameters.SessionId,   // sessionId
 679                             1,                      // tu_id --> unknown finger == right index finger
 680                             pos.Item1,              // c_id = touch id
 681                             pos.Item2,              // x_pos
 682                             pos.Item3,              // y_pos
 683                             0.0f,                   // angle
 684                             0.0f,                   // shear
 685                             1f,                     // radius: 1
 686                             pos.Item4,              // pressure: [-1|1]
 687                         };
 688               var interactionMessage = new OscMessage(
 689                    address: new Address(posAddress),
 690                    arguments: arguments);
 91
 692               messages.Add(interactionMessage);
 693            }
 94
 1095            var messageAlive = new OscMessage(
 1096                address: new Address("/tuio2/alv"),
 1097                arguments: new object[]
 1098                {
 1099                    parameters.SessionId
 10100                });
 101
 10102            messages.Add(messageAlive);
 103
 10104            return messages;
 10105        }
 106
 107        /// <summary>
 108        /// creates the messages for the tuio bundle according to TUIO 1.1 specification <see cref="http://www.tuio.org/
 109        /// Bundle contains:
 110        ///
 111        /// in case of <see cref="interpretation"/> = <see cref="TuioInterpretation.Point3D"/>:
 112        /// <code>
 113        /// [1] /tuio2/3Dobj "source" source
 114        /// [2] /tuio2/3Dobj "alive" session_id
 115        /// [3] /tuio2/3Dobj "set" session_id touch_id pos_x pos_y pos_z 0 0 0 0 0 0 0 0 0 0 0
 116        /// ...
 117        /// [n] /tuio2/3Dobj "fseq" frame_id
 118        /// </code>
 119        ///
 120        /// in case of <see cref="interpretation"/> = <see cref="TuioInterpretation.TouchPoint2DwithPressure"/>
 121        /// <code>
 122        /// [1] /tuio2/25Dobj "source" source
 123        /// [2] /tuio2/25Dobj "alive" session_id
 124        /// [3] /tuio2/25Dobj "set" session_id touch_id pos_x pos_y pos_z 0 0 0 0 0 0 0
 125        /// ...
 126        /// [n] /tuio2/25Dobj "fseq" frame_id
 127        /// </code>
 128        /// </summary>
 129        /// <param name="parameters"><see cref="TuioParameters"/> containing interactions of current frame and other met
 130        /// <param name="interpretation"><see cref="TuioInterpretation"/> specifying the message format.</param>
 131        /// <returns>List of all <see cref="OscMessage"/> to create bundle for transfer to TUIO server</returns>
 132        public IEnumerable<OscMessage> CreateTuio11Messages(TuioParameters parameters, TuioInterpretation interpretation
 10133        {
 10134             var messages = new List<OscMessage>();
 135
 10136             var address = interpretation == TuioInterpretation.Point3D
 10137                 ? "/tuio/3Dobj"
 10138                 : "/tuio/25Dobj";
 139
 10140            var messageStart = new OscMessage(
 10141                address: new Address(address),
 10142                arguments: new object[]
 10143                {
 10144                    "source",                // command parameter
 10145                    parameters.Source        // identifier
 10146                });
 147
 10148            messages.Add(messageStart);
 149
 10150            var messageAlive = new OscMessage(
 10151                address: new Address(address),
 10152                arguments: new object[]
 10153                {
 10154                    "alive",                // command parameter
 10155                    parameters.SessionId    // session id
 10156                });
 157
 10158            messages.Add(messageAlive);
 159
 70160            foreach (var pos in parameters.Positions)
 20161            {
 20162                var arguments = interpretation == TuioInterpretation.Point3D
 20163                    ?
 20164                        new object[]
 20165                        {
 20166                            "set",                      // command parameter
 20167                            parameters.SessionId,       // s: session_id
 20168                            pos.Item1,                  // i : class id = touch id
 20169                            pos.Item2,                  // x: x_pos
 20170                            pos.Item3,                  // y: y_pos
 20171                            pos.Item4,                  // z: z_pos
 20172                            0.0f,                       // a
 20173                            0.0f,                       // b
 20174                            0.0f,                       // c
 20175                            0f,                         // X (Velocity)
 20176                            0f,                         // Y
 20177                            0f,                         // Z
 20178                            0f,                         // A (Rotation)
 20179                            0f,                         // B
 20180                            0f,                         // C
 20181                            0f,                         // m (Motion acceleration)
 20182                            0f,                         // r (rotation acceleration)
 20183                        }
 20184                    :
 20185                        new object[]
 20186                        {
 20187                            "set",                      // command parameter
 20188                            parameters.SessionId,       // s: session_id
 20189                            pos.Item1,                  // i : class id = touch id
 20190                            pos.Item2,                  // x: x_pos
 20191                            pos.Item3,                  // y: y_pos
 20192                            pos.Item4,                  // z: z_pos
 20193                            0.0f,                       // a
 20194                            0f,                         // X (Velocity)
 20195                            0f,                         // Y
 20196                            0f,                         // Z
 20197                            0f,                         // A (Rotation)
 20198                            0f,                         // m (Motion acceleration)
 20199                            0f,                         // r (rotation acceleration)
 20200                        };
 20201               var interactionMessage = new OscMessage(
 20202                    address: new Address(address),
 20203                    arguments: arguments);
 204
 20205               messages.Add(interactionMessage);
 20206            }
 207
 10208            var messageEnd = new OscMessage(
 10209                address: new Address(address),
 10210                arguments: new object[]
 10211                {
 10212                    "fseq",             // command parameter
 10213                    parameters.FrameId  // frame id
 10214                });
 215
 10216            messages.Add(messageEnd);
 217
 10218            return messages;
 10219        }
 220
 221        public string GenerateStringRepresentation(OscBundle bundle)
 0222        {
 0223            return GenerateStringRepresentation(bundle.Messages);
 0224        }
 225
 226        public string GenerateStringRepresentation(IEnumerable<OscMessage> messages)
 0227        {
 0228            if (messages == null)
 0229                return "";
 230
 0231            var msgList = messages.ToList();
 0232            var result = "";
 0233            for (var i = 0; i < msgList.Count(); i++)
 0234            {
 0235                result += $"{GenerateStringRepresentation(msgList[i], i + 1)}{Environment.NewLine}";
 0236            }
 237
 0238            return result;
 0239        }
 240
 241        private string GenerateStringRepresentation(OscMessage msg, int index)
 0242        {
 0243            var args = msg.Arguments.ToList();
 0244            var result = args.Count > 0 ? $"{Environment.NewLine}" : "";
 0245            args.ForEach(arg =>
 0246            {
 0247                var argStr = arg.ToString();
 0248                if (arg is Timetag)
 0249                    argStr = ((Timetag)arg).Tag.ToString();
 0250                result += $"          {argStr} : ({arg.GetType().Name}) {Environment.NewLine}";
 0251            });
 252
 0253            return $"[{index}] \"{msg.Address.Value}\"{result}";
 0254        }
 255    }
 256}