| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using CoreOSC; |
| | 5 | | using ReFlex.Core.Tuio.Interfaces; |
| | 6 | | using ReFlex.Core.Tuio.Util; |
| | 7 | |
|
| | 8 | | namespace 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 |
| 5 | 39 | | { |
| 5 | 40 | | var messages = new List<OscMessage>(); |
| | 41 | |
|
| 5 | 42 | | var messageFrame = new OscMessage( |
| 5 | 43 | | address: new Address("/tuio2/frm"), |
| 5 | 44 | | arguments: new object[] |
| 5 | 45 | | { |
| 5 | 46 | | parameters.FrameId, |
| 5 | 47 | | parameters.Time, |
| 5 | 48 | | parameters.Dimension, |
| 5 | 49 | | parameters.Source |
| 5 | 50 | | }); |
| | 51 | |
|
| 5 | 52 | | messages.Add(messageFrame); |
| | 53 | |
|
| 5 | 54 | | var posAddress = interpretation == TuioInterpretation.Point3D |
| 5 | 55 | | ? "/tuio2/p3d" |
| 5 | 56 | | : "/tuio2/ptr"; |
| | 57 | |
|
| 37 | 58 | | foreach (var pos in parameters.Positions) |
| 11 | 59 | | { |
| 11 | 60 | | var arguments = interpretation == TuioInterpretation.Point3D |
| 11 | 61 | | ? |
| 11 | 62 | | new object[] |
| 11 | 63 | | { |
| 11 | 64 | | parameters.SessionId, // session_id |
| 11 | 65 | | 1, // tu_id --> unknown finger == right index finger |
| 11 | 66 | | pos.Item1, // c_id = touch id |
| 11 | 67 | | pos.Item2, // x_pos |
| 11 | 68 | | pos.Item3, // y_pos |
| 11 | 69 | | pos.Item4, // z_pos |
| 11 | 70 | | 0.0f, // x_axis |
| 11 | 71 | | 0.0f, // y_axis |
| 11 | 72 | | 1.0f, // z_axis --> [0,0,-1] -> pointing towards sensor |
| 11 | 73 | | 1f // radius: 1 |
| 11 | 74 | | } |
| 11 | 75 | | : |
| 11 | 76 | | new object[] |
| 11 | 77 | | { |
| 11 | 78 | | parameters.SessionId, // sessionId |
| 11 | 79 | | 1, // tu_id --> unknown finger == right index finger |
| 11 | 80 | | pos.Item1, // c_id = touch id |
| 11 | 81 | | pos.Item2, // x_pos |
| 11 | 82 | | pos.Item3, // y_pos |
| 11 | 83 | | 0.0f, // angle |
| 11 | 84 | | 0.0f, // shear |
| 11 | 85 | | 1f, // radius: 1 |
| 11 | 86 | | pos.Item4, // pressure: [-1|1] |
| 11 | 87 | | }; |
| 11 | 88 | | var interactionMessage = new OscMessage( |
| 11 | 89 | | address: new Address(posAddress), |
| 11 | 90 | | arguments: arguments); |
| | 91 | |
|
| 11 | 92 | | messages.Add(interactionMessage); |
| 11 | 93 | | } |
| | 94 | |
|
| 5 | 95 | | var messageAlive = new OscMessage( |
| 5 | 96 | | address: new Address("/tuio2/alv"), |
| 5 | 97 | | arguments: new object[] |
| 5 | 98 | | { |
| 5 | 99 | | parameters.SessionId |
| 5 | 100 | | }); |
| | 101 | |
|
| 5 | 102 | | messages.Add(messageAlive); |
| | 103 | |
|
| 5 | 104 | | return messages; |
| 5 | 105 | | } |
| | 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 |
| 5 | 133 | | { |
| 5 | 134 | | var messages = new List<OscMessage>(); |
| | 135 | |
|
| 5 | 136 | | var address = interpretation == TuioInterpretation.Point3D |
| 5 | 137 | | ? "/tuio/3Dobj" |
| 5 | 138 | | : "/tuio/25Dobj"; |
| | 139 | |
|
| 5 | 140 | | var messageStart = new OscMessage( |
| 5 | 141 | | address: new Address(address), |
| 5 | 142 | | arguments: new object[] |
| 5 | 143 | | { |
| 5 | 144 | | "source", // command parameter |
| 5 | 145 | | parameters.Source // identifier |
| 5 | 146 | | }); |
| | 147 | |
|
| 5 | 148 | | messages.Add(messageStart); |
| | 149 | |
|
| 5 | 150 | | var messageAlive = new OscMessage( |
| 5 | 151 | | address: new Address(address), |
| 5 | 152 | | arguments: new object[] |
| 5 | 153 | | { |
| 5 | 154 | | "alive", // command parameter |
| 5 | 155 | | parameters.SessionId // session id |
| 5 | 156 | | }); |
| | 157 | |
|
| 5 | 158 | | messages.Add(messageAlive); |
| | 159 | |
|
| 33 | 160 | | foreach (var pos in parameters.Positions) |
| 9 | 161 | | { |
| 9 | 162 | | var arguments = interpretation == TuioInterpretation.Point3D |
| 9 | 163 | | ? |
| 9 | 164 | | new object[] |
| 9 | 165 | | { |
| 9 | 166 | | "set", // command parameter |
| 9 | 167 | | parameters.SessionId, // s: session_id |
| 9 | 168 | | pos.Item1, // i : class id = touch id |
| 9 | 169 | | pos.Item2, // x: x_pos |
| 9 | 170 | | pos.Item3, // y: y_pos |
| 9 | 171 | | pos.Item4, // z: z_pos |
| 9 | 172 | | 0.0f, // a |
| 9 | 173 | | 0.0f, // b |
| 9 | 174 | | 0.0f, // c |
| 9 | 175 | | 0f, // X (Velocity) |
| 9 | 176 | | 0f, // Y |
| 9 | 177 | | 0f, // Z |
| 9 | 178 | | 0f, // A (Rotation) |
| 9 | 179 | | 0f, // B |
| 9 | 180 | | 0f, // C |
| 9 | 181 | | 0f, // m (Motion acceleration) |
| 9 | 182 | | 0f, // r (rotation acceleration) |
| 9 | 183 | | } |
| 9 | 184 | | : |
| 9 | 185 | | new object[] |
| 9 | 186 | | { |
| 9 | 187 | | "set", // command parameter |
| 9 | 188 | | parameters.SessionId, // s: session_id |
| 9 | 189 | | pos.Item1, // i : class id = touch id |
| 9 | 190 | | pos.Item2, // x: x_pos |
| 9 | 191 | | pos.Item3, // y: y_pos |
| 9 | 192 | | pos.Item4, // z: z_pos |
| 9 | 193 | | 0.0f, // a |
| 9 | 194 | | 0f, // X (Velocity) |
| 9 | 195 | | 0f, // Y |
| 9 | 196 | | 0f, // Z |
| 9 | 197 | | 0f, // A (Rotation) |
| 9 | 198 | | 0f, // m (Motion acceleration) |
| 9 | 199 | | 0f, // r (rotation acceleration) |
| 9 | 200 | | }; |
| 9 | 201 | | var interactionMessage = new OscMessage( |
| 9 | 202 | | address: new Address(address), |
| 9 | 203 | | arguments: arguments); |
| | 204 | |
|
| 9 | 205 | | messages.Add(interactionMessage); |
| 9 | 206 | | } |
| | 207 | |
|
| 5 | 208 | | var messageEnd = new OscMessage( |
| 5 | 209 | | address: new Address(address), |
| 5 | 210 | | arguments: new object[] |
| 5 | 211 | | { |
| 5 | 212 | | "fseq", // command parameter |
| 5 | 213 | | parameters.FrameId // frame id |
| 5 | 214 | | }); |
| | 215 | |
|
| 5 | 216 | | messages.Add(messageEnd); |
| | 217 | |
|
| 5 | 218 | | return messages; |
| 5 | 219 | | } |
| | 220 | |
|
| | 221 | | public string GenerateStringRepresentation(OscBundle bundle) |
| 0 | 222 | | { |
| 0 | 223 | | return GenerateStringRepresentation(bundle.Messages); |
| 0 | 224 | | } |
| | 225 | |
|
| | 226 | | public string GenerateStringRepresentation(IEnumerable<OscMessage> messages) |
| 0 | 227 | | { |
| 0 | 228 | | if (messages == null) |
| 0 | 229 | | return ""; |
| | 230 | |
|
| 0 | 231 | | var msgList = messages.ToList(); |
| 0 | 232 | | var result = ""; |
| 0 | 233 | | for (var i = 0; i < msgList.Count(); i++) |
| 0 | 234 | | { |
| 0 | 235 | | result += $"{GenerateStringRepresentation(msgList[i], i + 1)}{Environment.NewLine}"; |
| 0 | 236 | | } |
| | 237 | |
|
| 0 | 238 | | return result; |
| 0 | 239 | | } |
| | 240 | |
|
| | 241 | | private string GenerateStringRepresentation(OscMessage msg, int index) |
| 0 | 242 | | { |
| 0 | 243 | | var args = msg.Arguments.ToList(); |
| 0 | 244 | | var result = args.Count > 0 ? $"{Environment.NewLine}" : ""; |
| 0 | 245 | | args.ForEach(arg => |
| 0 | 246 | | { |
| 0 | 247 | | var argStr = arg.ToString(); |
| 0 | 248 | | if (arg is Timetag) |
| 0 | 249 | | argStr = ((Timetag)arg).Tag.ToString(); |
| 0 | 250 | | result += $" {argStr} : ({arg.GetType().Name}) {Environment.NewLine}"; |
| 0 | 251 | | }); |
| | 252 | |
|
| 0 | 253 | | return $"[{index}] \"{msg.Address.Value}\"{result}"; |
| 0 | 254 | | } |
| | 255 | | } |
| | 256 | | } |