Skip to content

Commit fa36c96

Browse files
authored
Merge pull request #13 from sandrofigo/develop
Release v3.1.0
2 parents 7640774 + 5965fad commit fa36c96

File tree

9 files changed

+76
-19
lines changed

9 files changed

+76
-19
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
77
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88
-->
99

10+
## [3.1.0] - 2023-01-03
11+
12+
### Added
13+
14+
- Added `GlobalPosition` property to `Voxel`
15+
1016
## [3.0.0] - 2023-01-03
1117

1218
### Added

VoxReader.UnitTests/UnitTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,30 @@ public class UnitTests
1616
private const string TestFile_3x3x3_at_center_with_corner = "data/3x3x3_at_center_with_corner.zip";
1717
private const string TestFile_groups = "data/groups.zip";
1818

19+
[Fact]
20+
public void VoxReader_Read_GlobalVoxelPositionIsCorrect()
21+
{
22+
string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_groups).First();
23+
24+
IVoxFile voxFile = VoxReader.Read(file);
25+
26+
voxFile.Models.Single(m => m.Name == "obj1").Voxels[0].GlobalPosition.Should().Be(new Vector3(0, 0, 0));
27+
voxFile.Models.Single(m => m.Name == "obj2").Voxels[0].GlobalPosition.Should().Be(new Vector3(0, 0, 2));
28+
29+
voxFile.Models.Single(m => m.Name == "obj3").Voxels.Single(v => v.Color == Color.Blue).GlobalPosition.Should().Be(new Vector3(-3, 0, 3));
30+
voxFile.Models.Single(m => m.Name == "obj3").Voxels.Single(v => v.Color == Color.Red).GlobalPosition.Should().Be(new Vector3(-1, 2, 5));
31+
32+
voxFile.Models.Single(m => m.Name == "obj4").Voxels.Single(v => v.Color == Color.Blue).GlobalPosition.Should().Be(new Vector3(-3, 0, 7));
33+
voxFile.Models.Single(m => m.Name == "obj4").Voxels.Single(v => v.Color == Color.Red).GlobalPosition.Should().Be(new Vector3(-1, 2, 9));
34+
}
35+
1936
[Fact]
2037
public void VoxReader_Read_ModelNamesAreParsedCorrectly()
2138
{
2239
string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_groups).First();
2340

2441
IVoxFile voxFile = VoxReader.Read(file);
42+
2543
voxFile.Models.Should().ContainSingle(m => m.Name == "obj1");
2644
voxFile.Models.Should().ContainSingle(m => m.Name == "obj2");
2745
voxFile.Models.Should().ContainSingle(m => m.Name == "obj3");
@@ -35,6 +53,7 @@ public void VoxReader_Read_ModelPositionsAreCorrectInGroups()
3553
string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_groups).First();
3654

3755
IVoxFile voxFile = VoxReader.Read(file);
56+
3857
voxFile.Models.Single(m => m.Name == "obj1").Position.Should().Be(new Vector3(0, 0, 0));
3958
voxFile.Models.Single(m => m.Name == "obj2").Position.Should().Be(new Vector3(0, 0, 2));
4059
voxFile.Models.Single(m => m.Name == "obj3").Position.Should().Be(new Vector3(-2, 1, 4));
@@ -48,6 +67,7 @@ public void VoxReader_Read_ModelPositionsAreCorrect()
4867
string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_MultipleModels).First();
4968

5069
IVoxFile voxFile = VoxReader.Read(file);
70+
5171
voxFile.Models.Single(m => m.Name == "black").Position.Should().Be(new Vector3(0, 0, 0));
5272
voxFile.Models.Single(m => m.Name == "red").Position.Should().Be(new Vector3(2, 0, 0));
5373
voxFile.Models.Single(m => m.Name == "green").Position.Should().Be(new Vector3(0, 2, 0));
@@ -63,6 +83,7 @@ public void VoxReader_Read_ModelPositionIsCorrectFor3x3x3Model()
6383
string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_3x3x3_at_center_with_corner).First();
6484

6585
IVoxFile voxFile = VoxReader.Read(file);
86+
6687
voxFile.Models.Single(m => m.Name == "obj1").Position.Should().Be(new Vector3(1, 1, 1));
6788
}
6889

VoxReader.UnitTests/VoxReader.UnitTests.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="FluentAssertions" Version="6.8.0"/>
11-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1"/>
12-
<PackageReference Include="SharpCompress" Version="0.32.2"/>
13-
<PackageReference Include="xunit" Version="2.4.2"/>
10+
<PackageReference Include="FluentAssertions" Version="6.8.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
12+
<PackageReference Include="SharpCompress" Version="0.32.2" />
13+
<PackageReference Include="xunit" Version="2.4.2" />
1414
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
<PrivateAssets>all</PrivateAssets>
@@ -22,11 +22,11 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25-
<Folder Include="data"/>
25+
<Folder Include="data" />
2626
</ItemGroup>
2727

2828
<ItemGroup>
29-
<ProjectReference Include="..\VoxReader\VoxReader.csproj"/>
29+
<ProjectReference Include="..\VoxReader\VoxReader.csproj" />
3030
</ItemGroup>
3131

3232
<ItemGroup>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:8ca59bcad5d9f276a76770b2e0e504067584e2a8ef36243cff5b2d5af6fa823a
3-
size 2009
2+
oid sha256:4d7b132c3ac61b208995ecaa8f0b95dafb220222e27fb9469e41d9d112b0faaf
3+
size 2024

VoxReader/Color.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,11 @@ public override string ToString()
7171
{
7272
return $"R: {R} G: {G} B: {B} A: {A}";
7373
}
74+
75+
internal static readonly Color Red = new(255, 0, 0, 255);
76+
internal static readonly Color Green = new(0, 255, 0, 255);
77+
internal static readonly Color Blue = new(0, 0, 255, 255);
78+
internal static readonly Color White = new(255, 255, 255, 255);
79+
internal static readonly Color Black = new(0, 0, 0, 255);
7480
}
7581
}

VoxReader/Helper.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ public static IEnumerable<IModel> ExtractModels(IChunk mainChunk, IPalette palet
6464
foreach (int id in ids)
6565
{
6666
string name = transformNodeChunk.Name;
67-
Vector3 position = GetGlobalTranslation(transformNodeChunk);
6867
Vector3 size = sizeChunks[id].Size;
69-
var voxels = voxelChunks[id].Voxels.Select(voxel => new Voxel(voxel.Position, palette.Colors[voxel.ColorIndex - 1])).ToArray();
68+
Vector3 position = GetGlobalTranslation(transformNodeChunk);
69+
70+
var voxels = voxelChunks[id].Voxels.Select(voxel => new Voxel(voxel.Position, position + voxel.Position - size / 2, palette.Colors[voxel.ColorIndex - 1])).ToArray();
7071

7172
// Create new model
7273
var model = new Model(id, name, position, size, voxels, !processedModelIds.Add(id));
@@ -77,11 +78,11 @@ public static IEnumerable<IModel> ExtractModels(IChunk mainChunk, IPalette palet
7778
Vector3 GetGlobalTranslation(ITransformNodeChunk target)
7879
{
7980
Vector3 position = target.Frames[0].Translation;
80-
81+
8182
while (TryGetParentTransformNodeChunk(target, out ITransformNodeChunk parent))
8283
{
8384
position += parent.Frames[0].Translation;
84-
85+
8586
target = parent;
8687
}
8788

VoxReader/Vector3.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ public struct Vector3 : IEquatable<Vector3>
88
/// The x-component of the vector (right).
99
/// </summary>
1010
public readonly int X;
11-
11+
1212
/// <summary>
1313
/// The y-component of the vector (forward).
1414
/// </summary>
1515
public readonly int Y;
16-
16+
1717
/// <summary>
1818
/// The z-component of the vector (up).
1919
/// </summary>
@@ -51,7 +51,9 @@ public override int GetHashCode()
5151
return hashCode;
5252
}
5353
}
54-
54+
55+
//TODO: add unit tests
56+
5557
public static bool operator ==(Vector3 a, Vector3 b)
5658
{
5759
return a.Equals(b);
@@ -61,10 +63,25 @@ public override int GetHashCode()
6163
{
6264
return !(a == b);
6365
}
64-
66+
6567
public static Vector3 operator +(Vector3 a, Vector3 b)
6668
{
6769
return new Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
6870
}
71+
72+
public static Vector3 operator -(Vector3 a, Vector3 b)
73+
{
74+
return new Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
75+
}
76+
77+
public static Vector3 operator *(Vector3 a, int i)
78+
{
79+
return new Vector3(a.X * i, a.Y * i, a.Z * i);
80+
}
81+
82+
public static Vector3 operator /(Vector3 a, int i)
83+
{
84+
return new Vector3(a.X / i, a.Y / i, a.Z / i);
85+
}
6986
}
7087
}

VoxReader/Voxel.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ namespace VoxReader
33
public readonly struct Voxel
44
{
55
/// <summary>
6-
/// The position of the voxel.
6+
/// The position of the voxel in the model.
77
/// </summary>
88
public Vector3 Position { get; }
9+
10+
/// <summary>
11+
/// The global position of the voxel in the scene.
12+
/// </summary>
13+
public Vector3 GlobalPosition { get; }
914

1015
/// <summary>
1116
/// The color of the voxel.
1217
/// </summary>
1318
public Color Color { get; }
1419

15-
internal Voxel(Vector3 position, Color color)
20+
internal Voxel(Vector3 position, Vector3 globalPosition, Color color)
1621
{
1722
Position = position;
23+
GlobalPosition = globalPosition;
1824
Color = color;
1925
}
2026

VoxReader/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.sandrofigo.voxreader",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"displayName": "VoxReader",
55
"description": "A C# library to read .vox files created with MagicaVoxel",
66
"documentationUrl": "https://github.com/sandrofigo/VoxReader",

0 commit comments

Comments
 (0)