Initial Commit

This commit is contained in:
2025-07-04 20:33:06 +03:00
commit 8f09347ae0
9219 changed files with 2447903 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
/*=========================================================================
Library: iMSTK-Unity
Copyright (c) Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
using UnityEngine;
namespace ImstkUnity
{
public class Capsule : Geometry
{
public Vector3 center = Vector3.zero;
public float radius = 0.5f;
public Quaternion orientation = Quaternion.identity;
public float length = 1.0f;
public Capsule()
{
geomType = GeometryType.Capsule;
}
public Vector3 GetTransformedCenter(Transform transform)
{
return transform.TransformPoint(center);
}
public Quaternion GetTransformedOrientation(Transform transform)
{
return orientation * transform.rotation;
}
public Mesh GetMesh()
{
Imstk.Capsule geom = this.ToImstkGeometry() as Imstk.Capsule;
Imstk.SurfaceMesh surfMesh = Imstk.Utils.toSurfaceMesh(geom);
return surfMesh.ToMesh();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f81a111151cf2074bb509ea399de47b8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 936dbb32eb653d84a9bcbe9caad48547, type: 3}
userData:
assetBundleName:
assetBundleVariant:
+54
View File
@@ -0,0 +1,54 @@
/*=========================================================================
Library: iMSTK-Unity
Copyright (c) Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
using UnityEngine;
namespace ImstkUnity
{
public class Cylinder : Geometry
{
public Vector3 center = Vector3.zero;
public float radius = 0.5f;
public Quaternion orientation = Quaternion.identity;
public float length = 1.0f;
public Cylinder()
{
geomType = GeometryType.Cylinder;
}
public Vector3 GetTransformedCenter(Transform transform)
{
return transform.TransformPoint(center);
}
public Quaternion GetTransformedOrientation(Transform transform)
{
return orientation * transform.rotation;
}
public Mesh GetMesh()
{
Imstk.Cylinder geom = this.ToImstkGeometry() as Imstk.Cylinder;
Imstk.SurfaceMesh surfMesh = Imstk.Utils.toSurfaceMesh(geom);
return surfMesh.ToMesh();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f2f6dc9b9f9eb8c4cba5968e2b98bed2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 936dbb32eb653d84a9bcbe9caad48547, type: 3}
userData:
assetBundleName:
assetBundleVariant:
+488
View File
@@ -0,0 +1,488 @@
/*=========================================================================
Library: iMSTK-Unity
Copyright (c) Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
using Imstk;
using System.Collections.Generic;
using UnityEngine;
namespace ImstkUnity
{
/// <summary>
/// Various geometryic utility functions
/// </summary>
public static class GeomUtil
{
/// <summary>
/// Map of GeometryType to MeshTopology
/// </summary>
public static Dictionary<GeometryType, MeshTopology> geomTypeToMeshTopology = new Dictionary<GeometryType, MeshTopology>()
{
{ GeometryType.PointSet, MeshTopology.Points },
{ GeometryType.LineMesh, MeshTopology.Lines },
{ GeometryType.SurfaceMesh, MeshTopology.Triangles }
};
/// <summary>
/// Map of MeshTopology to GeometryType
/// </summary>
public static Dictionary<MeshTopology, GeometryType> meshTopologyToGeomType = new Dictionary<MeshTopology, GeometryType>()
{
{ MeshTopology.Points, GeometryType.PointSet },
{ MeshTopology.Lines, GeometryType.LineMesh },
{ MeshTopology.Triangles, GeometryType.SurfaceMesh }
};
/// <summary>
/// Preforms a simple mesh copy (only works with triangles)
/// </summary>
public static void CopyMesh(Mesh srcMesh, Mesh destMesh)
{
destMesh.triangles = null;
destMesh.vertices = srcMesh.vertices;
destMesh.uv = srcMesh.uv;
destMesh.normals = srcMesh.normals;
destMesh.colors = srcMesh.colors;
destMesh.tangents = srcMesh.tangents;
for (int i = 0; i < srcMesh.subMeshCount; i++)
{
destMesh.SetIndices(srcMesh.GetIndices(i),
srcMesh.GetTopology(i), i);
}
}
public static void CopyMesh(ImstkMesh srcMesh, ImstkMesh destMesh)
{
destMesh.vertices = srcMesh.vertices;
destMesh.indices = srcMesh.indices;
destMesh.texCoords = srcMesh.texCoords;
destMesh.geomType = srcMesh.geomType;
}
/// <summary>
/// Converts Imstk.PointSet to Unity Mesh
/// SurfaceMesh
/// </summary>
public static Mesh ToMesh(this Imstk.PointSet geom)
{
string typeName = geom.getTypeName();
if (typeName != "LineMesh" &&
typeName != "SurfaceMesh" &&
typeName != "PointSet")
{
return null;
}
Imstk.PointSet pointSet = Imstk.Utils.CastTo<Imstk.PointSet>(geom);
Mesh results = new Mesh();
Vector3[] vertices = MathUtil.ToVector3Array(pointSet.getVertexPositions());
results.SetVertices(vertices);
if (typeName == "PointSet")
{
int[] indices = new int[vertices.Length];
for (int i = 0; i < indices.Length; i++)
{
indices[i] = i;
}
results.SetIndices(indices, MeshTopology.Points, 0);
}
else if (typeName == "LineMesh")
{
Imstk.LineMesh lineMesh = Imstk.Utils.CastTo<Imstk.LineMesh>(geom);
int[] indices = MathUtil.ToIntArray(lineMesh.getLinesIndices());
results.SetIndices(indices, MeshTopology.Lines, 0);
}
else if (typeName == "SurfaceMesh")
{
Imstk.SurfaceMesh surfMesh = Imstk.Utils.CastTo<Imstk.SurfaceMesh>(geom);
int[] indices = MathUtil.ToIntArray(surfMesh.getTriangleIndices());
results.SetIndices(indices, MeshTopology.Triangles, 0);
results.RecalculateNormals();
}
if (pointSet.getVertexTCoords() != null)
{
Vector2[] tCoords = MathUtil.ToVector2Array(pointSet.getVertexTCoords());
results.SetUVs(0, tCoords);
results.RecalculateNormals();
}
results.RecalculateBounds();
return results;
}
/// <summary>
/// Converts ImstkMesh to Unity Mesh
/// </summary>
public static Mesh ToMesh(this ImstkMesh geom)
{
if (geom.IsVolume)
return null;
Mesh results = new Mesh();
results.name = geom.name;
results.SetVertices(geom.vertices);
results.SetIndices(geom.indices, geomTypeToMeshTopology[geom.geomType], 0);
if (geom.texCoords.Length > 0)
{
results.SetUVs(0, geom.texCoords);
}
results.RecalculateBounds();
if (results.GetTopology(0) == MeshTopology.Triangles ||
results.GetTopology(0) == MeshTopology.Quads)
{
results.RecalculateNormals();
}
return results;
}
/// <summary>
/// Converts Unity Mesh to ImstkUnity Geometry
/// </summary>
public static ImstkMesh ToImstkMesh(this Mesh mesh)
{
// A Unity mesh can be made up multiple submeshes we only support 1
ImstkMesh geom = ScriptableObject.CreateInstance<ImstkMesh>();
geom.name = mesh.name;
geom.SetVertices(mesh.vertices);
geom.SetIndices(mesh.GetIndices(0));
geom.texCoords = new Vector2[mesh.uv.Length];
mesh.uv.CopyTo(geom.texCoords, 0);
geom.geomType = meshTopologyToGeomType[mesh.GetTopology(0)];
return geom;
}
/// <summary>
/// Converts Unity Mesh to ImstkUnity Geometry with transform applied
/// </summary>
public static ImstkMesh ToImstkMesh(this Mesh mesh, Matrix4x4 transform)
{
ImstkMesh geometry = ScriptableObject.CreateInstance<ImstkMesh>();
geometry.name = mesh.name;
geometry.geomType = meshTopologyToGeomType[mesh.GetTopology(0)];
geometry.SetVertices(mesh.vertices, transform);
int[] indices = mesh.GetIndices(0);
geometry.indices = new int[indices.Length];
mesh.triangles.CopyTo(geometry.indices, 0);
geometry.texCoords = new Vector2[mesh.uv.Length];
mesh.uv.CopyTo(geometry.texCoords, 0);
return geometry;
}
/// <summary>
/// Converts ImstkMesh to Imstk PointSet
/// </summary>
/// <param name="geometry"></param>
/// <returns></returns>
public static Imstk.PointSet ToPointSet(this ImstkMesh geometry)
{
Imstk.VecDataArray3d vertices = MathUtil.ToVecDataArray3d(geometry.vertices);
if (geometry.geomType == GeometryType.PointSet)
{
Imstk.PointSet pointSet = new Imstk.PointSet();
pointSet.initialize(vertices);
return pointSet;
}
else if (geometry.geomType == GeometryType.LineMesh)
{
Imstk.LineMesh mesh = new Imstk.LineMesh();
Imstk.VecDataArray2i indices = MathUtil.ToVecDataArray2i(geometry.indices);
mesh.initialize(vertices, indices);
return mesh;
}
else if (geometry.geomType == GeometryType.SurfaceMesh)
{
Imstk.SurfaceMesh mesh = new Imstk.SurfaceMesh();
Imstk.VecDataArray3i indices = MathUtil.ToVecDataArray3i(geometry.indices);
mesh.initialize(vertices, indices);
return mesh;
}
else if (geometry.geomType == GeometryType.TetrahedralMesh)
{
Imstk.TetrahedralMesh mesh = new Imstk.TetrahedralMesh();
Imstk.VecDataArray4i indices = MathUtil.ToVecDataArray4i(geometry.indices);
mesh.initialize(vertices, indices);
return mesh;
}
return null;
}
/// <summary>
/// Converts Geometry to Imstk Geometry
/// </summary>
public static Imstk.Geometry ToImstkGeometry(this Geometry geom)
{
if (geom == null)
{
Debug.LogError("ToImstkgeometry called with null");
}
if (geom.IsMesh)
{
return (geom as ImstkMesh).ToPointSet();
}
else if (geom.geomType == GeometryType.Capsule)
{
Capsule capsule = geom as Capsule;
Imstk.Capsule imstkCapsule = new Imstk.Capsule();
imstkCapsule.setPosition(capsule.center.ToImstkVec());
imstkCapsule.setRadius(capsule.radius);
imstkCapsule.setLength(capsule.length);
imstkCapsule.setOrientation(capsule.orientation.ToImstkQuat());
imstkCapsule.updatePostTransformData();
return imstkCapsule;
}
else if (geom.geomType == GeometryType.Cylinder)
{
Cylinder cylinder = geom as Cylinder;
Imstk.Cylinder imstkCylinder = new Imstk.Cylinder();
imstkCylinder.setPosition(cylinder.center.ToImstkVec());
imstkCylinder.setRadius(cylinder.radius);
imstkCylinder.setLength(cylinder.length);
imstkCylinder.setOrientation(cylinder.orientation.ToImstkQuat());
imstkCylinder.updatePostTransformData();
return imstkCylinder;
}
else if (geom.geomType == GeometryType.OrientedBox)
{
OrientedBox orientedBox = geom as OrientedBox;
Imstk.OrientedBox imstkOrientedBox = new Imstk.OrientedBox();
imstkOrientedBox.setPosition(orientedBox.center.ToImstkVec());
imstkOrientedBox.setOrientation(orientedBox.orientation.ToImstkQuat());
imstkOrientedBox.setExtents(orientedBox.extents.ToImstkVec());
imstkOrientedBox.updatePostTransformData();
return imstkOrientedBox;
}
else if (geom.geomType == GeometryType.Plane)
{
Plane plane = geom as Plane;
Imstk.Plane imstkPlane = new Imstk.Plane();
imstkPlane.setPosition(plane.center.ToImstkVec());
imstkPlane.setNormal(plane.normal.ToImstkVec());
imstkPlane.updatePostTransformData();
return imstkPlane;
}
else if (geom.geomType == GeometryType.Sphere)
{
Sphere sphere = geom as Sphere;
Imstk.Sphere imstkSphere = new Imstk.Sphere();
imstkSphere.setPosition(sphere.center.ToImstkVec());
imstkSphere.setRadius(sphere.radius);
imstkSphere.updatePostTransformData();
return imstkSphere;
}
return null;
}
/// <summary>
/// Lossy conversion, doesn't support mixed topologies
/// </summary>
public static Imstk.Geometry ToImstkGeometry(this Mesh mesh)
{
if (mesh.GetTopology(0) == MeshTopology.Points)
{
Imstk.PointSet geom = new Imstk.PointSet();
geom.initialize(MathUtil.ToVecDataArray3d(mesh.vertices));
return geom;
}
else if (mesh.GetTopology(0) == MeshTopology.Lines)
{
Imstk.LineMesh geom = new Imstk.LineMesh();
geom.initialize(
MathUtil.ToVecDataArray3d(mesh.vertices),
MathUtil.ToVecDataArray2i(mesh.GetIndices(0)));
return geom;
}
else if (mesh.GetTopology(0) == MeshTopology.Triangles)
{
Imstk.SurfaceMesh geom = new Imstk.SurfaceMesh();
geom.initialize(
MathUtil.ToVecDataArray3d(mesh.vertices),
MathUtil.ToVecDataArray3i(mesh.GetIndices(0)));
return geom;
}
return null;
}
/// <summary>
/// Converts Imstk PointSet to ImstkMesh
/// </summary>
public static ImstkMesh ToImstkMesh(this Imstk.PointSet pointSet)
{
ImstkMesh geom = ScriptableObject.CreateInstance<ImstkMesh>();
geom.vertices = MathUtil.ToVector3Array(pointSet.getVertexPositions());
string pointSetName = pointSet.getTypeName();
if (pointSetName == "PointSet")
{
geom.geomType = GeometryType.PointSet;
geom.indices = new int[geom.vertices.Length];
for (int i = 0; i < geom.indices.Length; i++)
{
geom.indices[i] = i;
}
}
else if (pointSetName == "LineMesh")
{
geom.geomType = GeometryType.LineMesh;
Imstk.LineMesh mesh = Imstk.Utils.CastTo<Imstk.LineMesh>(pointSet);
geom.indices = MathUtil.ToIntArray(mesh.getLinesIndices());
}
else if (pointSetName == "SurfaceMesh")
{
geom.geomType = GeometryType.SurfaceMesh;
Imstk.SurfaceMesh mesh = Imstk.Utils.CastTo<Imstk.SurfaceMesh>(pointSet);
geom.indices = MathUtil.ToIntArray(mesh.getTriangleIndices());
}
else if (pointSetName == "TetrahedralMesh")
{
geom.geomType = GeometryType.TetrahedralMesh;
Imstk.TetrahedralMesh mesh = Imstk.Utils.CastTo<Imstk.TetrahedralMesh>(pointSet);
geom.indices = MathUtil.ToIntArray(mesh.getTetrahedraIndices());
}
return geom;
}
public static Geometry ToGeometry(this Mesh mesh)
{
return mesh.ToImstkMesh();
}
/// <summary>
/// Converts Imstk Geometry to Geometry
/// </summary>
public static Geometry ToGeometry(Imstk.Geometry geom)
{
string name = geom.getTypeName();
if (name == "PointSet" || name == "LineMesh" ||
name == "SurfaceMesh" || name == "TetrahedralMesh")
{
Imstk.PointSet pointSet = Imstk.Utils.CastTo<Imstk.PointSet>(geom);
return pointSet.ToImstkMesh();
}
else if (name == "Capsule")
{
Imstk.Capsule imstkCapsule = Imstk.Utils.CastTo<Imstk.Capsule>(geom);
Imstk.Vec3d center = imstkCapsule.getCenter();
Imstk.Quatd quat = imstkCapsule.getOrientation();
Capsule capsule = ScriptableObject.CreateInstance<Capsule>();
capsule.center = center.ToUnityVec();
capsule.radius = (float)imstkCapsule.getRadius();
capsule.length = (float)imstkCapsule.getLength();
capsule.orientation = quat.ToUnityQuat();
return capsule;
}
else if (name == "Cylinder")
{
Imstk.Cylinder imstkCapsule = Imstk.Utils.CastTo<Imstk.Cylinder>(geom);
Imstk.Vec3d center = imstkCapsule.getCenter();
Imstk.Quatd quat = imstkCapsule.getOrientation();
Cylinder cylinder = ScriptableObject.CreateInstance<Cylinder>();
cylinder.center = center.ToUnityVec();
cylinder.radius = (float)imstkCapsule.getRadius();
cylinder.length = (float)imstkCapsule.getLength();
cylinder.orientation = quat.ToUnityQuat();
return cylinder;
}
else if (name == "OrientedBox")
{
Imstk.OrientedBox imstkOrientedBox = Imstk.Utils.CastTo<Imstk.OrientedBox>(geom);
Imstk.Vec3d center = imstkOrientedBox.getCenter();
Imstk.Vec3d extents = imstkOrientedBox.getExtents();
Imstk.Quatd orientation = imstkOrientedBox.getOrientation();
OrientedBox orientedBox = ScriptableObject.CreateInstance<OrientedBox>();
orientedBox.center = center.ToUnityVec();
orientedBox.extents = extents.ToUnityVec();
orientedBox.orientation = orientation.ToUnityQuat();
return orientedBox;
}
else if (name == "Plane")
{
Imstk.Plane imstkPlane = Imstk.Utils.CastTo<Imstk.Plane>(geom);
Imstk.Vec3d center = imstkPlane.getCenter();
Imstk.Vec3d normal = imstkPlane.getNormal();
Plane plane = ScriptableObject.CreateInstance<Plane>();
plane.center = center.ToUnityVec();
plane.normal = normal.ToUnityVec();
return plane;
}
else if (name == "Sphere")
{
Imstk.Sphere imstkSphere = Imstk.Utils.CastTo<Imstk.Sphere>(geom);
Imstk.Vec3d center = imstkSphere.getCenter();
Sphere sphere = ScriptableObject.CreateInstance<Sphere>();
sphere.center = center.ToUnityVec();
sphere.radius = (float)imstkSphere.getRadius();
return sphere;
}
return null;
}
/// <summary>Calculate list of vertices inside of a given mesh contained by unity meshfilter.
/// The points sampled need to be in world space as the enclosing mesh will also be transfered
/// into world space.</summary>
/// <param name="enclosingMesh">Unity.MeshFilter for enclosing mesh</param>
/// <param name="sampleMesh">Imstk.PointSet for sample mesh</param>
/// <returns>Return list of indices of of the sample mesh that are contained within the enclosing mesh</returns>
public static List<uint> PointsInside(MeshFilter enclosingMesh, PointSet sampleMesh)
{
Debug.Assert(enclosingMesh != null && sampleMesh != null);
ImstkMesh imstkEnclosingMesh = enclosingMesh.sharedMesh.ToImstkMesh(enclosingMesh.transform.localToWorldMatrix);
return PointsInside(imstkEnclosingMesh.ToImstkGeometry() as Imstk.SurfaceMesh, sampleMesh);
}
public static List<uint> PointsInside(SurfaceMesh enclosingMesh, PointSet sampleMesh)
{
var result = new List<uint>();
// Compute mask of enclosed points
Imstk.SelectEnclosedPoints selectEnclosed = new Imstk.SelectEnclosedPoints();
selectEnclosed.setInputMesh(enclosingMesh);
selectEnclosed.setInputPoints(sampleMesh);
selectEnclosed.setUsePruning(false);
selectEnclosed.update();
Imstk.DataArrayuc isInside = selectEnclosed.getIsInsideMask();
byte[] isInsideBytes = new byte[isInside.size()];
isInside.getValues(isInsideBytes);
for (int i = 0; i < isInsideBytes.Length; i++)
{
if (isInsideBytes[i] == 1)
result.Add((uint)i);
}
return result;
}
};
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 20a65b11fe8bd5a499fd46809b237b1c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 936dbb32eb653d84a9bcbe9caad48547, type: 3}
userData:
assetBundleName:
assetBundleVariant:
+60
View File
@@ -0,0 +1,60 @@
/*=========================================================================
Library: iMSTK-Unity
Copyright (c) Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
using UnityEngine;
namespace ImstkUnity
{
public enum GeometryType
{
Capsule,
Cylinder,
HexahedralMesh,
ImageData,
LineMesh,
OrientedBox,
Plane,
PointSet,
Sphere,
SurfaceMesh,
TetrahedralMesh,
UnityMesh
};
/// <summary>
/// We need to reimplement storage for all geometry classes in iMSTK as Unity
/// import and transfer from editor to runtime works via serialization
/// afaik it would be quite significant undertaking, and very possibly impossible
/// to serialize non C# classes, not to mention sensitive to changes. Thus the
/// base iMSTK geometry cannot be used for geometry storage
/// </summary>
public class Geometry : ScriptableObject
{
public GeometryType geomType = GeometryType.Plane;
public bool IsMesh { get { return
(geomType == GeometryType.PointSet ||
geomType == GeometryType.LineMesh ||
geomType == GeometryType.SurfaceMesh ||
geomType == GeometryType.TetrahedralMesh ||
geomType == GeometryType.HexahedralMesh); } }
public bool IsVolume { get { return (geomType == GeometryType.TetrahedralMesh || geomType == GeometryType.HexahedralMesh); } }
};
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3ea518f5190165c408289bf14f91f808
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 936dbb32eb653d84a9bcbe9caad48547, type: 3}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,132 @@
/*=========================================================================
Library: iMSTK-Unity
Copyright (c) Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
using UnityEngine;
namespace ImstkUnity
{
/// <summary>
/// Similar to a MeshFilter in Unity.It provides an input and output geometry.
/// It may take in any iMSTK geometry, as well as a Unity Mesh
/// (one can also drag/drop a MeshFilter to it). These are instances of geometries
/// used in all of iMSTK unity scripts.Instances of this class fit into
/// the ``Visual Geometry``, ``Physics Geometry``, and ``Collision Geometry``
/// slots on the model components.
/// </summary>
[AddComponentMenu("iMSTK/GeometryFilter")]
public class GeometryFilter : ImstkBehaviour
{
// GeometryFilter supports either type
public Geometry inputImstkGeom = null;
public Mesh inputUnityGeom = null;
// It can output this type
// Note: Imstk.Geometry cannot transition over editor and runtime
public Imstk.Geometry outputImstkGeom = null;
// Used to denote which should be seated, not what is currently
public GeometryType type = GeometryType.UnityMesh;
// Used to toggle drawing of it in the editor, ideally this would not
// exist in runtime code, but unity's architecture doesn't allow
public bool showHandles = true;
// Use to write the mesh as seen by iMSTK on creation,
// this can help with debugging mesh issues
public bool writeMesh = false;
private bool inGlobalSpace = false;
/// <summary>
/// Moves the geometry to a global transform
/// </summary>
public void MoveToGlobalSpace()
{
// Models in Unity are provided in Mesh pre transformed.
// We move the entire model to world space for simulation. Meaning the transform
// is applied before giving the simulation geometry. Here we store that initial
// transform and reset the local transform
if (inGlobalSpace) return;
var localToWorld = gameObject.transform.localToWorldMatrix.ToMat4d();
var filters = gameObject.GetComponents<GeometryFilter>();
foreach (var filter in filters)
{
if (filter.inGlobalSpace) continue;
// World coordinates
var geometry = filter.GetOutputGeometry();
geometry.transform(localToWorld, Imstk.Geometry.TransformType.ApplyToData);
geometry.setTransform(Imstk.Mat4d.Identity());
filter.inGlobalSpace = true;
}
gameObject.transform.localPosition = Vector3.zero;
gameObject.transform.localScale = Vector3.one;
gameObject.transform.localRotation = Quaternion.identity;
// Re-parent to itself for the period of the simulation run, this avoids having
// to recalculate the local transforms when the object is in a hierarchy
gameObject.transform.SetParent(null, false);
}
public void SetGeometry(Geometry geom)
{
inputImstkGeom = geom;
type = geom.geomType;
}
public void SetGeometry(Mesh geom)
{
inputUnityGeom = geom;
type = GeometryType.UnityMesh;
}
/// <summary>
/// This will convert the input to output, allocating in native code
/// </summary>
public Imstk.Geometry GetOutputGeometry()
{
if (outputImstkGeom == null)
{
if (type == GeometryType.UnityMesh)
outputImstkGeom = inputUnityGeom.ToImstkGeometry();
else
outputImstkGeom = inputImstkGeom.ToImstkGeometry();
}
return outputImstkGeom;
}
/// <summary>
/// Write mesh in this filter to file, useful for debugging
/// </summary>
public void WriteMesh(string filename)
{
var mesh = Imstk.Utils.CastTo<Imstk.SurfaceMesh>(GetOutputGeometry());
if (mesh != null)
{
//mesh.flipNormals();
mesh.correctWindingOrder();
mesh.computeVertexNormals();
mesh.computeTrianglesNormals();
Imstk.MeshIO.write(mesh, filename);
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fb7fd5ef0ac0aca4aaccabd7990fcfbf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 936dbb32eb653d84a9bcbe9caad48547, type: 3}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,81 @@
/*=========================================================================
Library: iMSTK-Unity
Copyright (c) Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
using System.Collections.Generic;
using UnityEngine;
namespace ImstkUnity
{
/// <summary>
/// Roles all meshes into one class. ie: PointSet, LineMesh, SurfaceMesh,
/// TetMesh, HexMesh
/// </summary>
public class ImstkMesh : Geometry
{
/// <summary>
/// Map of some geometry types to number of cell points
/// </summary>
public static Dictionary<GeometryType, int> typeToNumPts = new Dictionary<GeometryType, int>()
{
{ GeometryType.PointSet, 1 },
{ GeometryType.LineMesh, 2 },
{ GeometryType.SurfaceMesh, 3 },
{ GeometryType.TetrahedralMesh, 4 },
{ GeometryType.HexahedralMesh, 8 }
};
public int[] indices = new int[0];
public Vector3[] vertices = new Vector3[0];
public Vector2[] texCoords = new Vector2[0];
//public void CopyTo(ImstkMesh geometry)
//{
// geometry.type = type;
// geometry.vertices = new Vector3[vertices.Length];
// vertices.CopyTo(geometry.vertices, 0);
// geometry.texCoords = new Vector2[texCoords.Length];
// texCoords.CopyTo(geometry.texCoords, 0);
// geometry.indices = new int[indices.Length];
// indices.CopyTo(geometry.indices, 0);
//}
public void SetVertices(Vector3[] vertices, Matrix4x4 transform)
{
this.vertices = new Vector3[vertices.Length];
for (int i = 0; i < vertices.Length; i++)
{
this.vertices[i] = transform.MultiplyPoint(vertices[i]);
}
}
public void SetVertices(Vector3[] vertices) { this.vertices = vertices; }
public void SetTexCoords(Vector2[] texCoords) { this.texCoords = texCoords; }
public void SetIndices(int[] indices) { this.indices = indices; }
public void Transform(Matrix4x4 transform)
{
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = transform.MultiplyPoint(vertices[i]);
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9ff06caabe5106e489465ca1ca18add7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 936dbb32eb653d84a9bcbe9caad48547, type: 3}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,58 @@
/*=========================================================================
Library: iMSTK-Unity
Copyright (c) Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
using UnityEngine;
namespace ImstkUnity
{
public class OrientedBox : Geometry
{
public Vector3 center = Vector3.zero;
public Vector3 extents = new Vector3(0.5f, 0.5f, 0.5f);
public Quaternion orientation = Quaternion.identity;
public OrientedBox()
{
geomType = GeometryType.OrientedBox;
}
public Vector3 GetTransformedCenter(Transform transform)
{
return transform.TransformPoint(center);
}
public Vector3 GetTransformedExtent(Transform transform)
{
return Vector3.Scale(extents, transform.localScale);
}
public Quaternion GetTransformedOrientation(Transform transform)
{
return orientation * transform.rotation;
}
public Mesh GetMesh()
{
Imstk.OrientedBox geom = this.ToImstkGeometry() as Imstk.OrientedBox;
Imstk.SurfaceMesh surfMesh = Imstk.Utils.toSurfaceMesh(geom);
return surfMesh.ToMesh();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fd37a9e519de4c94e97f0ab4af6c0720
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 936dbb32eb653d84a9bcbe9caad48547, type: 3}
userData:
assetBundleName:
assetBundleVariant:
+46
View File
@@ -0,0 +1,46 @@
/*=========================================================================
Library: iMSTK-Unity
Copyright (c) Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
using UnityEngine;
namespace ImstkUnity
{
public class Plane : Geometry
{
public Plane()
{
geomType = GeometryType.Plane;
}
public Vector3 GetTransformedCenter(Transform transform)
{
return transform.TransformPoint(center);
}
public Vector3 GetTransformedNormal(Transform transform)
{
return transform.TransformDirection(normal).normalized;
}
public Vector3 center = Vector3.zero;
public Vector3 normal = Vector3.up;
public float visualWidth = 1.0f; ///> Purely used for visualization
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 11ff5901a1e43f54d931c16a488e306e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 936dbb32eb653d84a9bcbe9caad48547, type: 3}
userData:
assetBundleName:
assetBundleVariant:
+54
View File
@@ -0,0 +1,54 @@
/*=========================================================================
Library: iMSTK-Unity
Copyright (c) Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
using UnityEngine;
namespace ImstkUnity
{
public class Sphere : Geometry
{
public Vector3 center = Vector3.zero;
public float radius = 0.5f;
public Sphere()
{
geomType = GeometryType.Sphere;
}
public Vector3 GetTransformedCenter(Transform transform)
{
return transform.TransformPoint(center);
}
public float GetTransformedRadius(Transform transform)
{
Vector3 localScale = transform.localScale;
float max = Mathf.Max(Mathf.Max(localScale.x, localScale.y), localScale.z);
return radius * max;
}
public Mesh GetMesh()
{
Imstk.Sphere geom = this.ToImstkGeometry() as Imstk.Sphere;
Imstk.SurfaceMesh surfMesh = Imstk.Utils.toUVSphereSurfaceMesh(geom, 7, 7);
return surfMesh.ToMesh();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bb08b7d90a322dc4a85b33bd8924b96c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 936dbb32eb653d84a9bcbe9caad48547, type: 3}
userData:
assetBundleName:
assetBundleVariant: