diff --git a/math/TransformSequence.cs b/math/TransformSequence.cs index 8880d8d8..a8197e69 100644 --- a/math/TransformSequence.cs +++ b/math/TransformSequence.cs @@ -190,7 +190,7 @@ public Vector3d TransformP(Vector3d p) /// - /// Apply transforms to point + /// Apply transforms to vector. Includes scaling. /// public Vector3d TransformV(Vector3d v) { diff --git a/spatial/BasicProjectionTargets.cs b/spatial/BasicProjectionTargets.cs index 71b7e7de..6d274c5c 100644 --- a/spatial/BasicProjectionTargets.cs +++ b/spatial/BasicProjectionTargets.cs @@ -5,6 +5,10 @@ namespace g3 { + /// + /// MeshProjectionTarget provides an IProjectionTarget interface to a mesh + spatial data structure. + /// Use to project points to mesh surface. + /// public class MeshProjectionTarget : IOrientedProjectionTarget { public DMesh3 Mesh { get; set; } @@ -25,7 +29,7 @@ public MeshProjectionTarget(DMesh3 mesh) Spatial = new DMeshAABBTree3(mesh, true); } - public Vector3d Project(Vector3d vPoint, int identifier = -1) + public virtual Vector3d Project(Vector3d vPoint, int identifier = -1) { int tNearestID = Spatial.FindNearestTriangle(vPoint); Triangle3d triangle = new Triangle3d(); @@ -35,7 +39,7 @@ public Vector3d Project(Vector3d vPoint, int identifier = -1) return nearPt; } - public Vector3d Project(Vector3d vPoint, out Vector3d vProjectNormal, int identifier = -1) + public virtual Vector3d Project(Vector3d vPoint, out Vector3d vProjectNormal, int identifier = -1) { int tNearestID = Spatial.FindNearestTriangle(vPoint); Triangle3d triangle = new Triangle3d(); @@ -69,11 +73,60 @@ public static MeshProjectionTarget Auto(DMesh3 mesh, IEnumerable triangles, DSubmesh3 submesh = new DSubmesh3(mesh, targetRegion); return new MeshProjectionTarget(submesh.SubMesh); } + } + + + + + /// + /// Extension of MeshProjectionTarget that allows the target to have a transformation + /// relative to it's internal space. Call SetTransform(), or initialize the transforms yourself + /// + public class TransformedMeshProjectionTarget : MeshProjectionTarget + { + public TransformSequence SourceToTargetXForm; + public TransformSequence TargetToSourceXForm; + + public TransformedMeshProjectionTarget() { } + public TransformedMeshProjectionTarget(DMesh3 mesh, ISpatial spatial) : base(mesh, spatial) + { + } + public TransformedMeshProjectionTarget(DMesh3 mesh) : base(mesh) + { + } + public void SetTransform(TransformSequence sourceToTargetX) + { + SourceToTargetXForm = sourceToTargetX; + TargetToSourceXForm = SourceToTargetXForm.MakeInverse(); + } + + public override Vector3d Project(Vector3d vPoint, int identifier = -1) + { + Vector3d vTargetPt = SourceToTargetXForm.TransformP(vPoint); + Vector3d vTargetProj = base.Project(vTargetPt, identifier); + return TargetToSourceXForm.TransformP(vTargetProj); + } + + + public override Vector3d Project(Vector3d vPoint, out Vector3d vProjectNormal, int identifier = -1) + { + Vector3d vTargetPt = SourceToTargetXForm.TransformP(vPoint); + Vector3d vTargetProjNormal; + Vector3d vTargetProj = base.Project(vTargetPt, out vTargetProjNormal, identifier); + vProjectNormal = TargetToSourceXForm.TransformV(vTargetProjNormal).Normalized; + return TargetToSourceXForm.TransformP(vTargetProj); + } } + + + + + + public class PlaneProjectionTarget : IProjectionTarget { public Vector3d Origin;