Skip to content

Commit

Permalink
Merge pull request #13 from sandrofigo/develop
Browse files Browse the repository at this point in the history
Release v3.1.0
  • Loading branch information
sandrofigo authored Jan 3, 2023
2 parents 7640774 + 5965fad commit fa36c96
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
-->

## [3.1.0] - 2023-01-03

### Added

- Added `GlobalPosition` property to `Voxel`

## [3.0.0] - 2023-01-03

### Added
Expand Down
21 changes: 21 additions & 0 deletions VoxReader.UnitTests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,30 @@ public class UnitTests
private const string TestFile_3x3x3_at_center_with_corner = "data/3x3x3_at_center_with_corner.zip";
private const string TestFile_groups = "data/groups.zip";

[Fact]
public void VoxReader_Read_GlobalVoxelPositionIsCorrect()
{
string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_groups).First();

IVoxFile voxFile = VoxReader.Read(file);

voxFile.Models.Single(m => m.Name == "obj1").Voxels[0].GlobalPosition.Should().Be(new Vector3(0, 0, 0));
voxFile.Models.Single(m => m.Name == "obj2").Voxels[0].GlobalPosition.Should().Be(new Vector3(0, 0, 2));

voxFile.Models.Single(m => m.Name == "obj3").Voxels.Single(v => v.Color == Color.Blue).GlobalPosition.Should().Be(new Vector3(-3, 0, 3));
voxFile.Models.Single(m => m.Name == "obj3").Voxels.Single(v => v.Color == Color.Red).GlobalPosition.Should().Be(new Vector3(-1, 2, 5));

voxFile.Models.Single(m => m.Name == "obj4").Voxels.Single(v => v.Color == Color.Blue).GlobalPosition.Should().Be(new Vector3(-3, 0, 7));
voxFile.Models.Single(m => m.Name == "obj4").Voxels.Single(v => v.Color == Color.Red).GlobalPosition.Should().Be(new Vector3(-1, 2, 9));
}

[Fact]
public void VoxReader_Read_ModelNamesAreParsedCorrectly()
{
string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_groups).First();

IVoxFile voxFile = VoxReader.Read(file);

voxFile.Models.Should().ContainSingle(m => m.Name == "obj1");
voxFile.Models.Should().ContainSingle(m => m.Name == "obj2");
voxFile.Models.Should().ContainSingle(m => m.Name == "obj3");
Expand All @@ -35,6 +53,7 @@ public void VoxReader_Read_ModelPositionsAreCorrectInGroups()
string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_groups).First();

IVoxFile voxFile = VoxReader.Read(file);

voxFile.Models.Single(m => m.Name == "obj1").Position.Should().Be(new Vector3(0, 0, 0));
voxFile.Models.Single(m => m.Name == "obj2").Position.Should().Be(new Vector3(0, 0, 2));
voxFile.Models.Single(m => m.Name == "obj3").Position.Should().Be(new Vector3(-2, 1, 4));
Expand All @@ -48,6 +67,7 @@ public void VoxReader_Read_ModelPositionsAreCorrect()
string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_MultipleModels).First();

IVoxFile voxFile = VoxReader.Read(file);

voxFile.Models.Single(m => m.Name == "black").Position.Should().Be(new Vector3(0, 0, 0));
voxFile.Models.Single(m => m.Name == "red").Position.Should().Be(new Vector3(2, 0, 0));
voxFile.Models.Single(m => m.Name == "green").Position.Should().Be(new Vector3(0, 2, 0));
Expand All @@ -63,6 +83,7 @@ public void VoxReader_Read_ModelPositionIsCorrectFor3x3x3Model()
string file = Zip.UnzipFilesFromSevenZipArchive(TestFile_3x3x3_at_center_with_corner).First();

IVoxFile voxFile = VoxReader.Read(file);

voxFile.Models.Single(m => m.Name == "obj1").Position.Should().Be(new Vector3(1, 1, 1));
}

Expand Down
12 changes: 6 additions & 6 deletions VoxReader.UnitTests/VoxReader.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.8.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1"/>
<PackageReference Include="SharpCompress" Version="0.32.2"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="FluentAssertions" Version="6.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="SharpCompress" Version="0.32.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand All @@ -22,11 +22,11 @@
</ItemGroup>

<ItemGroup>
<Folder Include="data"/>
<Folder Include="data" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\VoxReader\VoxReader.csproj"/>
<ProjectReference Include="..\VoxReader\VoxReader.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions VoxReader.UnitTests/data/groups.zip
Git LFS file not shown
6 changes: 6 additions & 0 deletions VoxReader/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,11 @@ public override string ToString()
{
return $"R: {R} G: {G} B: {B} A: {A}";
}

internal static readonly Color Red = new(255, 0, 0, 255);
internal static readonly Color Green = new(0, 255, 0, 255);
internal static readonly Color Blue = new(0, 0, 255, 255);
internal static readonly Color White = new(255, 255, 255, 255);
internal static readonly Color Black = new(0, 0, 0, 255);
}
}
9 changes: 5 additions & 4 deletions VoxReader/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ public static IEnumerable<IModel> ExtractModels(IChunk mainChunk, IPalette palet
foreach (int id in ids)
{
string name = transformNodeChunk.Name;
Vector3 position = GetGlobalTranslation(transformNodeChunk);
Vector3 size = sizeChunks[id].Size;
var voxels = voxelChunks[id].Voxels.Select(voxel => new Voxel(voxel.Position, palette.Colors[voxel.ColorIndex - 1])).ToArray();
Vector3 position = GetGlobalTranslation(transformNodeChunk);

var voxels = voxelChunks[id].Voxels.Select(voxel => new Voxel(voxel.Position, position + voxel.Position - size / 2, palette.Colors[voxel.ColorIndex - 1])).ToArray();

// Create new model
var model = new Model(id, name, position, size, voxels, !processedModelIds.Add(id));
Expand All @@ -77,11 +78,11 @@ public static IEnumerable<IModel> ExtractModels(IChunk mainChunk, IPalette palet
Vector3 GetGlobalTranslation(ITransformNodeChunk target)
{
Vector3 position = target.Frames[0].Translation;

while (TryGetParentTransformNodeChunk(target, out ITransformNodeChunk parent))
{
position += parent.Frames[0].Translation;

target = parent;
}

Expand Down
25 changes: 21 additions & 4 deletions VoxReader/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public struct Vector3 : IEquatable<Vector3>
/// The x-component of the vector (right).
/// </summary>
public readonly int X;

/// <summary>
/// The y-component of the vector (forward).
/// </summary>
public readonly int Y;

/// <summary>
/// The z-component of the vector (up).
/// </summary>
Expand Down Expand Up @@ -51,7 +51,9 @@ public override int GetHashCode()
return hashCode;
}
}


//TODO: add unit tests

public static bool operator ==(Vector3 a, Vector3 b)
{
return a.Equals(b);
Expand All @@ -61,10 +63,25 @@ public override int GetHashCode()
{
return !(a == b);
}

public static Vector3 operator +(Vector3 a, Vector3 b)
{
return new Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
}

public static Vector3 operator -(Vector3 a, Vector3 b)
{
return new Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
}

public static Vector3 operator *(Vector3 a, int i)
{
return new Vector3(a.X * i, a.Y * i, a.Z * i);
}

public static Vector3 operator /(Vector3 a, int i)
{
return new Vector3(a.X / i, a.Y / i, a.Z / i);
}
}
}
10 changes: 8 additions & 2 deletions VoxReader/Voxel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ namespace VoxReader
public readonly struct Voxel
{
/// <summary>
/// The position of the voxel.
/// The position of the voxel in the model.
/// </summary>
public Vector3 Position { get; }

/// <summary>
/// The global position of the voxel in the scene.
/// </summary>
public Vector3 GlobalPosition { get; }

/// <summary>
/// The color of the voxel.
/// </summary>
public Color Color { get; }

internal Voxel(Vector3 position, Color color)
internal Voxel(Vector3 position, Vector3 globalPosition, Color color)
{
Position = position;
GlobalPosition = globalPosition;
Color = color;
}

Expand Down
2 changes: 1 addition & 1 deletion VoxReader/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.sandrofigo.voxreader",
"version": "3.0.0",
"version": "3.1.0",
"displayName": "VoxReader",
"description": "A C# library to read .vox files created with MagicaVoxel",
"documentationUrl": "https://github.com/sandrofigo/VoxReader",
Expand Down

0 comments on commit fa36c96

Please sign in to comment.