using System.Text.RegularExpressions;
using UnityEngine;
///
/// ·Îº¿ ÇÁ·Î±×·¥ÀÇ °³º° À̵¿ ¸í·É(Step) ÇÑ ÁÙÀ» ³ªÅ¸³»´Â Ŭ·¡½º
/// ·Îº¿ Á¦¾î±â·ÎºÎÅÍ ¹ÞÀº ¹®ÀÚ¿(Raw String)À» ÆÄ½ÌÇÏ¿© ¼Ó¼ºº°·Î °ü¸®Çϰí,
/// ¼öÁ¤ »çÇ×ÀÌ »ý±æ °æ¿ì ´Ù½Ã ¹®ÀÚ¿·Î Á¶ÇÕÇÏ´Â ±â´ÉÀ» ´ã´ç
///
public class RobotMoveStep
{
// --- ±âº» ¼Ó¼º ---
///
/// ·Îº¿ Á¦¾î±â¿¡¼ »ç¿ëÇÏ´Â ¿øº» ¸í·ÉÁÙ Àüü
/// ¿¹: "S1 move P,spd=50%,accu=3,tool=1 [0,0,0,0,0,0,"robot"]"
///
public string FullLine { get; private set; }
///
/// ÇØ´ç ½ºÅÜÀÇ ¸ñÇ¥ À§Ä¡ ¹× ȸÀü Á¤º¸ (x, y, z, rx, ry, rz)
///
public RobotPose Pose { get; private set; }
public int StepNumber { get; private set; }
public int Speed { get; private set; }
public int Accu { get; private set; }
public int Tool { get; private set; }
///
/// ÁÂÇ¥¸¦ Á¦¿ÜÇÑ À̵¿ ¸í·É (¿¹: "move P,spd=50%,accu=3,tool=1")
///
public string MoveCommand { get; private set; }
// --- Á¤±ÔÇ¥Çö½Ä (Regex) Á¤ÀÇ ---
// ¼º´É ÃÖÀûȸ¦ À§ÇØ Compiled ¿É¼ÇÀ» »ç¿ëÇϸç static readonly·Î ¼±¾ð
// Àüü ¶óÀÎ ÆÄ½Ì¿ë Regex
// ±¸Á¶: [½ÃÀÛ(S¹øÈ£)] + [°ø¹é] + [¸í·É¾î(move...)] + [°ø¹é] + [ÁÂÇ¥([...])]
// ±×·ì 2: ½ºÅÜ ¹øÈ£ ¼ýÀÚ (¿¹: 1)
// ±×·ì 3: À̵¿ ¸í·É¾î ¹®ÀÚ¿ (¿¹: move P...)
private static readonly Regex StepRegex = new Regex(@"^(S([0-9]+))\s+(move.*?)(\s+\[.*\])", RegexOptions.Compiled);
// ¸í·É¾î ³»ºÎ ÆÄ¶ó¹ÌÅÍ ÃßÃâ¿ë Regex
private static readonly Regex SpeedRegex = new Regex(@"spd=([0-9]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex AccuRegex = new Regex(@"accu=([0-9]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex ToolRegex = new Regex(@"tool=([0-9]+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
///
/// »ý¼ºÀÚ: ¹®ÀÚ¿ ¶óÀÎÀ» ¹Þ¾Æ °´Ã¼¸¦ ÃʱâÈÇÕ´Ï´Ù.
///
/// ½ºÅÜ ¹øÈ£¿Í ¸í·É¾î°¡ Æ÷ÇÔµÈ ¾ÕºÎºÐ ¹®ÀÚ¿ (ÁÂÇ¥ Æ÷Ç﵃ ¼öµµ ÀÖÀ½)
/// ´ë°ýÈ£ ¾ÈÀÇ ÁÂÇ¥ µ¥ÀÌÅÍ ¹®ÀÚ¿ (¿¹: "0,0,0,0,0,0,"robot"")
public RobotMoveStep(string line, string coordString)
{
this.FullLine = line;
// 1. ÁÂÇ¥ µ¥ÀÌÅÍ ÆÄ½Ì
// ½°Ç¥·Î ºÐ¸®ÇÏ¿© x, y, z, rx, ry, rz °ªÀ» ÃßÃâ
string[] values = coordString.Split(',');
if (values.Length >= 6)
{
this.Pose = new RobotPose(
float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]),
float.Parse(values[3]), float.Parse(values[4]), float.Parse(values[5])
);
}
else
{
Debug.LogError($"ÁÂÇ¥ ÆÄ½Ì ½ÇÆÐ: {coordString}");
this.Pose = new RobotPose(0, 0, 0, 0, 0, 0);
}
// 2. ¸í·É¾î ¶óÀÎ ÆÄ½Ì (Regex »ç¿ë)
Match match = StepRegex.Match(line);
if (match.Success)
{
// ±×·ì 2: ½ºÅÜ ¹øÈ£ ÃßÃâ
this.StepNumber = int.Parse(match.Groups[2].Value);
// ±×·ì 3: À̵¿ ¸í·É¾î ÃßÃâ (¾ÕµÚ °ø¹é Á¦°Å)
this.MoveCommand = match.Groups[3].Value.Trim();
// 3. ¸í·É¾î ³»ºÎ ¼¼ºÎ ÆÄ¶ó¹ÌÅÍ ÆÄ½Ì
Match speedMatch = SpeedRegex.Match(this.MoveCommand);
Match accuMatch = AccuRegex.Match(this.MoveCommand);
Match toolMatch = ToolRegex.Match(this.MoveCommand);
if (speedMatch.Success)
this.Speed = int.Parse(speedMatch.Groups[1].Value);
else
Debug.LogWarning($"'spd='¸¦ ãÀ» ¼ö ¾øÀ½: {this.MoveCommand}");
if (accuMatch.Success)
this.Accu = int.Parse(accuMatch.Groups[1].Value);
else
Debug.LogWarning($"'accu='¸¦ ãÀ» ¼ö ¾øÀ½: {this.MoveCommand}");
if (toolMatch.Success)
this.Tool = int.Parse(toolMatch.Groups[1].Value);
else
Debug.LogWarning($"'tool='À» ãÀ» ¼ö ¾øÀ½: {this.MoveCommand}");
}
else
{
Debug.LogError($"½ºÅÜ ¶óÀÎ ÆÄ½Ì ½ÇÆÐ: {line}");
this.StepNumber = -1;
this.MoveCommand = "move P,spd=50%,accu=3,tool=0"; // ±âº»°ª
this.Speed = 50; // ±âº»°ª
}
}
///
/// ½ºÅÜ ¹øÈ£¸¦ ¾÷µ¥ÀÌÆ®ÇÒ ¶§ »ç¿ë (¿¹: Áß°£ ½ºÅÜ »èÁ¦ ÈÄ ¹øÈ£ ÀçÁ¤·Ä ½Ã)
/// ¹øÈ£°¡ ¹Ù²î¸é FullLine ¹®ÀÚ¿µµ »õ·Î Á¶ÇÕÇÏ¿© °»½Å
///
/// »õ·Î¿î ½ºÅÜ ¹øÈ£
/// ¸í·É¾î¸¦ º¯°æÇÒ °æ¿ì ÀÔ·Â (nullÀÌ¸é ±âÁ¸ ¸í·É À¯Áö)
public void UpdateStepNumber(int newStepNumber, string newMoveCommand = null)
{
this.StepNumber = newStepNumber;
if (newMoveCommand != null)
{
this.MoveCommand = newMoveCommand;
}
// Æ÷¸Ë: "S{¹øÈ£} {¸í·É¾î} {ÁÂÇ¥}" ÇüÅ·Π¹®ÀÚ¿ ÀçÁ¶¸³
this.FullLine = $"S{this.StepNumber} {this.MoveCommand} {this.Pose.ToString()}";
}
///
/// ·Îº¿ÀÇ À§Ä¡(Pose) Á¤º¸¸¦ ¾÷µ¥ÀÌÆ®ÇÒ ¶§ »ç¿ë
/// À§Ä¡°¡ ¹Ù²î¸é FullLine ¹®ÀÚ¿µµ »õ·Î Á¶ÇÕÇÏ¿© °»½Å
///
/// »õ·Î¿î À§Ä¡/ȸÀü Á¤º¸
public void UpdatePose(RobotPose newPose)
{
this.Pose = newPose;
// º¯°æµÈ PoseÀÇ ToString()À» »ç¿ëÇÏ¿© FullLine °»½Å
this.FullLine = $"S{this.StepNumber} {this.MoveCommand} {this.Pose.ToString()}";
}
}
///
/// ·Îº¿ÀÇ 6Ãà ÁÂÇ¥ µ¥ÀÌÅÍ(À§Ä¡ ¹× ȸÀü)¸¦ °ü¸®ÇÏ´Â µ¥ÀÌÅÍ Å¬·¡½º
///
public class RobotPose
{
public float x, y, z, rx, ry, rz;
public RobotPose(float x, float y, float z, float rx, float ry, float rz)
{
this.x = x; this.y = y; this.z = z;
this.rx = rx; this.ry = ry; this.rz = rz;
}
///
/// RobotData(Model)¸¦ RobotPose(Program)·Î º¯È¯
///
public RobotPose(RobotData data)
{
this.x = data.x; this.y = data.y; this.z = data.z;
this.rx = data.rx; this.ry = data.ry; this.rz = data.rz;
}
///
/// RobotPose(Program)¸¦ RobotData(Model)·Î º¯È¯
///
public RobotData ToRobotData()
{
return new RobotData
{
x = this.x,
y = this.y,
z = this.z,
rx = this.rx,
ry = this.ry,
rz = this.rz
};
}
///
/// API¿¡¼ ¿ä±¸ÇÏ´Â ÁÂÇ¥ ¹®ÀÚ¿ Çü½ÄÀ¸·Î º¯È¯
///
public override string ToString()
{
return $"[{x},{y},{z},{rx},{ry},{rz},\"robot\"]";
}
}