< 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
 539        {
 540            var messages = new List<OscMessage>();
 41
 542            var messageFrame = new OscMessage(
 543                address: new Address("/tuio2/frm"),
 544                arguments: new object[]
 545                {
 546                     parameters.FrameId,
 547                     parameters.Time,
 548                     parameters.Dimension,
 549                     parameters.Source
 550                });
 51
 552            messages.Add(messageFrame);
 53
 554            var posAddress = interpretation == TuioInterpretation.Point3D
 555                ? "/tuio2/p3d"
 556                : "/tuio2/ptr";
 57
 3758            foreach (var pos in parameters.Positions)
 1159            {
 1160                var arguments = interpretation == TuioInterpretation.Point3D
 1161                    ?
 1162                        new object[]
 1163                        {
 1164                            parameters.SessionId,       // session_id
 1165                            1,                          // tu_id --> unknown finger == right index finger
 1166                            pos.Item1,                  // c_id = touch id
 1167                            pos.Item2,                  // x_pos
 1168                            pos.Item3,                  // y_pos
 1169                            pos.Item4,                  // z_pos
 1170                            0.0f,                       // x_axis
 1171                            0.0f,                       // y_axis
 1172                            1.0f,                       // z_axis --> [0,0,-1] -> pointing towards sensor
 1173                            1f                          // radius: 1
 1174                        }
 1175                    :
 1176                        new object[]
 1177                         {
 1178                             parameters.SessionId,   // sessionId
 1179                             1,                      // tu_id --> unknown finger == right index finger
 1180                             pos.Item1,              // c_id = touch id
 1181                             pos.Item2,              // x_pos
 1182                             pos.Item3,              // y_pos
 1183                             0.0f,                   // angle
 1184                             0.0f,                   // shear
 1185                             1f,                     // radius: 1
 1186                             pos.Item4,              // pressure: [-1|1]
 1187                         };
 1188               var interactionMessage = new OscMessage(
 1189                    address: new Address(posAddress),
 1190                    arguments: arguments);
 91
 1192               messages.Add(interactionMessage);
 1193            }
 94
 595            var messageAlive = new OscMessage(
 596                address: new Address("/tuio2/alv"),
 597                arguments: new object[]
 598                {
 599                    parameters.SessionId
 5100                });
 101
 5102            messages.Add(messageAlive);
 103
 5104            return messages;
 5105        }
 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
 5133        {
 5134             var messages = new List<OscMessage>();
 135
 5136             var address = interpretation == TuioInterpretation.Point3D
 5137                 ? "/tuio/3Dobj"
 5138                 : "/tuio/25Dobj";
 139
 5140            var messageStart = new OscMessage(
 5141                address: new Address(address),
 5142                arguments: new object[]
 5143                {
 5144                    "source",                // command parameter
 5145                    parameters.Source        // identifier
 5146                });
 147
 5148            messages.Add(messageStart);
 149
 5150            var messageAlive = new OscMessage(
 5151                address: new Address(address),
 5152                arguments: new object[]
 5153                {
 5154                    "alive",                // command parameter
 5155                    parameters.SessionId    // session id
 5156                });
 157
 5158            messages.Add(messageAlive);
 159
 33160            foreach (var pos in parameters.Positions)
 9161            {
 9162                var arguments = interpretation == TuioInterpretation.Point3D
 9163                    ?
 9164                        new object[]
 9165                        {
 9166                            "set",                      // command parameter
 9167                            parameters.SessionId,       // s: session_id
 9168                            pos.Item1,                  // i : class id = touch id
 9169                            pos.Item2,                  // x: x_pos
 9170                            pos.Item3,                  // y: y_pos
 9171                            pos.Item4,                  // z: z_pos
 9172                            0.0f,                       // a
 9173                            0.0f,                       // b
 9174                            0.0f,                       // c
 9175                            0f,                         // X (Velocity)
 9176                            0f,                         // Y
 9177                            0f,                         // Z
 9178                            0f,                         // A (Rotation)
 9179                            0f,                         // B
 9180                            0f,                         // C
 9181                            0f,                         // m (Motion acceleration)
 9182                            0f,                         // r (rotation acceleration)
 9183                        }
 9184                    :
 9185                        new object[]
 9186                        {
 9187                            "set",                      // command parameter
 9188                            parameters.SessionId,       // s: session_id
 9189                            pos.Item1,                  // i : class id = touch id
 9190                            pos.Item2,                  // x: x_pos
 9191                            pos.Item3,                  // y: y_pos
 9192                            pos.Item4,                  // z: z_pos
 9193                            0.0f,                       // a
 9194                            0f,                         // X (Velocity)
 9195                            0f,                         // Y
 9196                            0f,                         // Z
 9197                            0f,                         // A (Rotation)
 9198                            0f,                         // m (Motion acceleration)
 9199                            0f,                         // r (rotation acceleration)
 9200                        };
 9201               var interactionMessage = new OscMessage(
 9202                    address: new Address(address),
 9203                    arguments: arguments);
 204
 9205               messages.Add(interactionMessage);
 9206            }
 207
 5208            var messageEnd = new OscMessage(
 5209                address: new Address(address),
 5210                arguments: new object[]
 5211                {
 5212                    "fseq",             // command parameter
 5213                    parameters.FrameId  // frame id
 5214                });
 215
 5216            messages.Add(messageEnd);
 217
 5218            return messages;
 5219        }
 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}