Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions BIS.Core/Math/Matrix4P.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ public void Write(BinaryWriterEx output)
output.Write(matrix.M43);
}

public System.Numerics.Matrix4x4 Matrix
{
get { return matrix; }
set { matrix = value; }
}

public float Altitude
{
get { return matrix.M42; }
set { matrix.M42 = value; }
}

public float AltitudeScale
{
get { return matrix.M22; }
set { matrix.M22 = value; }
}

public override string ToString()
{
return matrix.ToString();
Expand Down
19 changes: 19 additions & 0 deletions BIS.Core/Stream/BinaryReaderEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Numerics;
using System.Text;
using BIS.Core.Compression;

Expand Down Expand Up @@ -253,5 +254,23 @@ public T[] ReadCompressed<T>(Func<BinaryReaderEx, T> readElement, int nElements,
var stream = new BinaryReaderEx(new MemoryStream(ReadCompressed(expectedDataSize)));
return stream.ReadArrayBase(readElement, nElements);
}

public Vector3 ReadVector3()
{
return new Vector3(ReadSingle(), ReadSingle(), ReadSingle());
}

public Vector3 ReadVector3Compressed()
{
var value = ReadUInt32();
double scaleFactor = -1.0 / 511;
uint x = value & 0x3FF;
uint y = (value >> 10) & 0x3FF;
uint z = (value >> 20) & 0x3FF;
if (x > 511) { x -= 1024; }
if (y > 511) { y -= 1024; }
if (z > 511) { z -= 1024; }
return new Vector3((float)(x * scaleFactor), (float)(y * scaleFactor), (float)(z * scaleFactor));
}
}
}
24 changes: 23 additions & 1 deletion BIS.Core/Stream/BinaryWriterEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Numerics;
using System.Text;

namespace BIS.Core.Streams
Expand Down Expand Up @@ -53,14 +54,29 @@ public void WriteArray<T>(T[] array, Action<BinaryWriterEx, T> write)
WriteArrayBase(array, write);
}

private void WriteArrayBase<T>(T[] array, Action<BinaryWriterEx, T> write)
public void WriteArray(float[] array)
{
WriteArray(array, (w, f) => w.Write(f));
}

public void WriteArrayBase<T>(T[] array, Action<BinaryWriterEx, T> write)
{
foreach (var item in array)
{
write(this, item);
}
}

public void WriteArrayBase(float[] array)
{
WriteArrayBase(array, (w, f) => w.Write(f));
}

public void WriteArrayBase(int[] array)
{
WriteArrayBase(array, (w, f) => w.Write(f));
}

public void WriteCompressedFloatArray(float[] array)
{
WriteCompressedArray(array, (w, v) => w.Write(v), 4);
Expand Down Expand Up @@ -152,5 +168,11 @@ public void WriteUshorts(ushort[] elements)
{
WriteArrayBase(elements, (r,e) => r.Write(e));
}
public void Write(Vector3 value)
{
Write(value.X);
Write(value.Y);
Write(value.Z);
}
}
}
26 changes: 26 additions & 0 deletions BIS.P3D.Test/BIS.P3D.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BIS.P3D\BIS.P3D.csproj" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions BIS.P3D.Test/ODOL/ODOLTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BIS.Core.Streams;
using Xunit;

namespace BIS.P3D.Test.ODOL
{
public class ODOLTest
{
[Fact]
public void Test()
{

}

}
}
20 changes: 20 additions & 0 deletions BIS.P3D/IModelInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
using BIS.Core.Math;

namespace BIS.P3D
{
public interface IModelInfo
{
/// <summary>
/// Minimum coordinates of bounding box
/// </summary>
Vector3P BboxMin { get; }

/// <summary>
/// Maximum coordinates of bounding box
/// </summary>
Vector3P BboxMax { get; }
}
}
30 changes: 30 additions & 0 deletions BIS.P3D/MLOD/ComputedModelInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BIS.Core.Math;

namespace BIS.P3D.MLOD
{
class ComputedModelInfo : IModelInfo
{
internal ComputedModelInfo(MLOD mLOD)
{
var points = mLOD.Lods.SelectMany(l => l.Points);

BboxMin = new Vector3P(
points.Min(p => p.X),
points.Min(p => p.Y),
points.Min(p => p.Z));

BboxMax = new Vector3P(
points.Max(p => p.X),
points.Max(p => p.Y),
points.Max(p => p.Z));
}

public Vector3P BboxMin { get; }

public Vector3P BboxMax { get; }
}
}
21 changes: 18 additions & 3 deletions BIS.P3D/MLOD/MLOD.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
using BIS.Core.Streams;
using BIS.P3D.ODOL;
using System;
using System.IO;

namespace BIS.P3D.MLOD
{
public class MLOD
public class MLOD : IReadObject
{
public int Version { get; private set; }

public P3DM_LOD[] Lods { get; private set; }

public MLOD(string fileName) : this(File.OpenRead(fileName)) {}
public IModelInfo ModelInfo => new ComputedModelInfo(this);

public MLOD(string fileName) : this(File.OpenRead(fileName))
{
}

public MLOD(Stream stream)
{
Expand All @@ -22,11 +28,20 @@ public MLOD(P3DM_LOD[] lods)
Lods = lods;
}

private void Read(BinaryReaderEx input)
internal MLOD()
{
}

public void Read(BinaryReaderEx input)
{
if (input.ReadAscii(4) != "MLOD")
throw new FormatException("MLOD signature expected");

ReadContent(input);
}

internal void ReadContent(BinaryReaderEx input)
{
Version = input.ReadInt32();
if (Version != 257)
throw new ArgumentException("Unknown MLOD version");
Expand Down
Loading