Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from vimaec/private/mavimaec/viewer-import
Browse files Browse the repository at this point in the history
Viewer Import
  • Loading branch information
mavimaec authored Mar 26, 2019
2 parents caa00a0 + e745d32 commit add1c41
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 7 deletions.
8 changes: 8 additions & 0 deletions dotnet/UnityBridge/Ara3D.UnityBridge.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\revit-dev\RevitDataModel\Ara3D.Revit.DataModel.csproj">
<Project>{c1d61cf7-bcb8-4db6-a6d8-7a3de02275c9}</Project>
<Name>Ara3D.Revit.DataModel</Name>
</ProjectReference>
<ProjectReference Include="..\AraGeometry\Ara3D.Geometry.csproj">
<Project>{E89008FA-6D3F-48DE-900A-BE5A9D79F4AE}</Project>
<Name>Ara3D.Geometry</Name>
Expand All @@ -58,6 +62,10 @@
<Project>{81718218-f3e7-4d54-8552-2a65fbebee85}</Project>
<Name>Ara3D.LinqArray</Name>
</ProjectReference>
<ProjectReference Include="..\Utilities\Ara3D.DotNetUtilities.csproj">
<Project>{25330241-4c82-4c17-8027-ff882b91640e}</Project>
<Name>Ara3D.DotNetUtilities</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down
102 changes: 96 additions & 6 deletions dotnet/UnityBridge/UnityConverters.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System;
using System.IO;
using System.Collections.Generic;
using Ara3D;
using Ara3D.Revit.DataModel;
using UnityEngine;
using UnityEngine.Rendering;
using Vector2 = System.Numerics.Vector2;
using Vector3 = System.Numerics.Vector3;
using Vector4 = System.Numerics.Vector4;
using ILogger = Ara3D.ILogger;
using System.Linq;

namespace UnityBridge
{
Expand Down Expand Up @@ -152,15 +156,101 @@ public static void SetAra3DMatrix(this Transform transform, float[] mtx)

public static void SetFromNode(this Transform transform, ISceneNode node)
{
if (!System.Numerics.Matrix4x4.Decompose(node.Transform, out var scl, out var rot, out var pos))
// TODO: Strong assumption - The coordinate system of the ISceneNode's Transform matches that of Revit.
transform.SetFromRevitMatrix(node.Transform);
}

public static void SetFromRevitSceneNode(this Transform transform, RevitSceneNode node)
{
transform.SetFromRevitMatrix(node.Transform);
}

public static void SetFromRevitMatrix(this Transform transform, System.Numerics.Matrix4x4 matrix)
{
if (!matrix.UnityPRS(out var pos, out var rot, out var scl))
throw new Exception("Can't decompose matrix");


RevitToUnityCoords(pos, rot, scl, out var outPos, out var outRot, out var outScl);

transform.position = outPos;
transform.rotation = outRot;
transform.localScale = outScl;
}

/// <summary>
/// Converts the given Revit-based coordinates into Unity coordinates.
/// </summary>
public static void RevitToUnityCoords(
UnityEngine.Vector3 pos,
UnityEngine.Quaternion rot,
UnityEngine.Vector3 scale,
out UnityEngine.Vector3 outPos,
out UnityEngine.Quaternion outRot,
out UnityEngine.Vector3 outScale)
{
// Transform space is mirrored on X, and then rotated 90 degrees around X
transform.position = new UnityEngine.Vector3(-pos.X, pos.Z, -pos.Y);
// Quat is mirrored the same way, but then negated via W = -W because that's just easier to read
transform.rotation = new UnityEngine.Quaternion(rot.X, -rot.Z, rot.Y, rot.W);
outPos = new UnityEngine.Vector3(-pos.x, pos.z, -pos.y);

// Quaternion is mirrored the same way, but then negated via W = -W because that's just easier to read
outRot = new Quaternion(rot.x, -rot.z, rot.y, rot.w);

// TODO: test this, current scale is completely untested
transform.localScale = new UnityEngine.Vector3(scl.X, scl.Z, scl.Y);
outScale = new UnityEngine.Vector3(scale.x, scale.z, scale.y);
}

/// <summary>
/// Extracts the Unity-compatible types for position, rotation, and scale from the given matrix.
/// Returns false if the matrix cannot be decomposed.
/// </summary>
public static bool UnityPRS(
this System.Numerics.Matrix4x4 matrix,
out UnityEngine.Vector3 position,
out UnityEngine.Quaternion rotation,
out UnityEngine.Vector3 scale)
{
position = new UnityEngine.Vector3();
rotation = new UnityEngine.Quaternion();
scale = new UnityEngine.Vector3(1, 1, 1);

if (matrix.IsIdentity)
return true;

var decomposed = System.Numerics.Matrix4x4.Decompose(matrix, out var scl, out var rot, out var pos);
if (!decomposed)
return false;

position.Set(pos.X, pos.Y, pos.Z);
rotation.Set(rot.X, rot.Y, rot.Z, rot.W);
scale.Set(scl.X, scl.Y, scl.Z);

return true;
}
}

public class UnityLogger : ILogger
{
public List<LogEvent> Events = new List<LogEvent>();

public ILogger Log(string message = "", LogLevel level = LogLevel.None, int eventId = 0)
{

var e = new LogEvent
{
EventId = eventId,
Index = Events.Count,
Message = message,
When = DateTime.Now
};
Events.Add(e);

UnityEngine.Debug.Log(e);

return this;
}

public void ExportLog(string path)
{
File.WriteAllLines(path, Events.Select(e => e.ToString()));
}
}
}
10 changes: 9 additions & 1 deletion dotnet/Utilities/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public static void Log(this ILogger logger, string message, Action action, LogLe

public static T Log<T>(this ILogger logger, string message, Func<T> func, LogLevel level = LogLevel.Debug, LogLevel exceptionLevel = LogLevel.Critical, bool rethrow = false)
{
DateTime start = DateTime.Now;
try
{
logger.Log("begin: " + message, level);
Expand All @@ -149,10 +150,17 @@ public static T Log<T>(this ILogger logger, string message, Func<T> func, LogLe
}
finally
{
logger.Log("end: " + message, level);
logger.Log($"end: {message} | elapsed time (seconds): {(DateTime.Now - start).TotalSeconds}", level);
}

return default;
}

public static Action LogDuration(this ILogger logger, string message, LogLevel level = LogLevel.Debug)
{
DateTime start = DateTime.Now;
logger.Log($"begin: {message}", level);
return () => logger.Log($"end: {message} | elapsed time (seconds): {(DateTime.Now - start).TotalSeconds}");
}
}
}

0 comments on commit add1c41

Please sign in to comment.