-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
239 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
Scallion.Tests/UnitTests/DomainModels/InterpolationTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using NUnit.Framework; | ||
using Scallion.DomainModels.Components; | ||
|
||
namespace Scallion.Tests.UnitTests.DomainModels | ||
{ | ||
[TestFixture] | ||
internal class InterpolationTest | ||
{ | ||
private object[] interpolationParams = new[] | ||
{ | ||
new object[] { new InterpolationParameter(20, 20), new InterpolationParameter(107, 107), 0.2f, 0.2f }, | ||
new object[] { new InterpolationParameter(20, 20), new InterpolationParameter(107, 107), 0.7f, 0.7f }, | ||
new object[] { new InterpolationParameter(84, 84), new InterpolationParameter(10, 108), 0.3f, 0.402165f }, | ||
new object[] { new InterpolationParameter(84, 84), new InterpolationParameter(10, 108), 0.4f, 0.684518f }, | ||
}; | ||
|
||
[Test] | ||
[TestCaseSource("interpolationParams")] | ||
public void CalculateInterpolatedValueTest(InterpolationParameter first, InterpolationParameter second, float t, float y) | ||
{ | ||
var interpolation = new Interpolation(first, second); | ||
Assert.AreEqual(y, interpolation.GetInterpolatedValue(t), 1e-5); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using Scallion.Core; | ||
using Scallion.DomainModels.Components; | ||
|
||
namespace Scallion.DomainModels | ||
{ | ||
/// <summary> | ||
/// Represents MMD Pose File(.vpd). | ||
/// </summary> | ||
public class Pose : IMMDFile<Pose> | ||
{ | ||
/// <summary> | ||
/// Gets or sets the name of a model which the pose made for. | ||
/// </summary> | ||
public string ModelName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a value of <see cref="BoneState"/> associated with the bone name. | ||
/// <see cref="BoneState.Interpolation"/> and <see cref="BoneState.ExternalParent"/> in these values will be ignored. | ||
/// </summary> | ||
public Dictionary<string, BoneState> Bones { get; set; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Pose"/> class. | ||
/// </summary> | ||
public Pose() | ||
{ | ||
Bones = new Dictionary<string, BoneState>(); | ||
} | ||
|
||
/// <summary> | ||
/// Loads a pose from the specified file. | ||
/// </summary> | ||
/// <param name="path">The file path to load a pose</param> | ||
/// <returns>The self instance assigned values from the file</returns> | ||
/// <exception cref="ArgumentException"> | ||
/// Thrown when the specified file is invalid or unsupported file. | ||
/// </exception> | ||
public Pose Load(string path) | ||
{ | ||
var raw = new Raw.Pose().Load(path); | ||
ModelName = raw.ModelName; | ||
Bones = raw.Bones.ToDictionary(p => p.Name, p => new BoneState() | ||
{ | ||
Position = p.Position, | ||
Quaternion = p.Quaternion | ||
}); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Saves this pose to the specified file. | ||
/// </summary> | ||
/// <param name="path">The file path to save this pose</param> | ||
public void Save(string path) | ||
{ | ||
new Raw.Pose() | ||
{ | ||
ModelName = this.ModelName, | ||
Bones = Bones.Select(p => new Raw.Components.Pose.Bone() | ||
{ | ||
Name = p.Key, | ||
Position = p.Value.Position, | ||
Quaternion = p.Value.Quaternion | ||
}).ToList() | ||
}.Save(path); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using System.Numerics; | ||
|
||
namespace Scallion.Raw.Components.Pose | ||
{ | ||
internal class Bone | ||
{ | ||
public string Name { get; set; } | ||
public Vector3 Position { get; set; } | ||
public Quaternion Quaternion { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.IO; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
using System.Numerics; | ||
using Scallion.Core; | ||
using Scallion.Raw.Components.Pose; | ||
|
||
namespace Scallion.Raw | ||
{ | ||
internal class Pose : IMMDFile<Pose> | ||
{ | ||
public static readonly string Signature = "Vocaloid Pose Data file"; | ||
|
||
public string ModelName { get; set; } | ||
public List<Bone> Bones { get; set; } | ||
|
||
public Pose Load(string path) | ||
{ | ||
using (var reader = new StreamReader(path, Encoding.GetEncoding("shift_jis"))) | ||
{ | ||
if (Signature != reader.ReadLine()) | ||
throw new ArgumentException("Unsupported or invalid .vpd file"); | ||
reader.ReadLine(); | ||
ModelName = Regex.Match(reader.ReadLine(), @".*(?=\.osm)").Value; | ||
int bonesCount = int.Parse(Regex.Match(reader.ReadLine(), @"\d+").Value); | ||
reader.ReadLine(); | ||
|
||
Bones = new List<Bone>(bonesCount); | ||
for (int i = 0; i < bonesCount; i++) | ||
{ | ||
string name = Regex.Match(reader.ReadLine(), @"(?<={).*").Value; | ||
float[] pos = Array.ConvertAll(Regex.Match(reader.ReadLine(), @"(?<=\s*).+(?=;)").Value.Split(','), float.Parse); | ||
float[] q = Array.ConvertAll(Regex.Match(reader.ReadLine(), @"(?<=\s*).+(?=;)").Value.Split(','), float.Parse); | ||
Bones.Add(new Bone() | ||
{ | ||
Name = name, | ||
Position = new Vector3(pos[0], pos[1], pos[2]), | ||
Quaternion = new Quaternion(q[0], q[1], q[2], q[3]) | ||
}); | ||
reader.ReadLine(); | ||
reader.ReadLine(); | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
public void Save(string path) | ||
{ | ||
using (var writer = new StreamWriter(path, false, Encoding.GetEncoding("shift_jis"))) | ||
{ | ||
writer.WriteLine(Signature); | ||
writer.WriteLine(); | ||
writer.WriteLine("{0}.osm;", ModelName); | ||
writer.WriteLine("{0};", Bones.Count); | ||
writer.WriteLine(); | ||
for (int i = 0; i < Bones.Count; i++) | ||
{ | ||
writer.WriteLine(@"Bone{0}{{{1}", i, Bones[i].Name); | ||
writer.WriteLine(" {0},{1},{2};", Bones[i].Position.X, Bones[i].Position.Y, Bones[i].Position.Z); | ||
writer.WriteLine(" {0},{1},{2},{3};", Bones[i].Quaternion.X, Bones[i].Quaternion.Y, Bones[i].Quaternion.Z, Bones[i].Quaternion.W); | ||
writer.WriteLine("}"); | ||
writer.WriteLine(); | ||
} | ||
writer.WriteLine(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters