Skip to content

Commit

Permalink
add TransformedMeshProjectionTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
rms80 committed May 23, 2018
1 parent a33e7bf commit d7912e9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion math/TransformSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public Vector3d TransformP(Vector3d p)


/// <summary>
/// Apply transforms to point
/// Apply transforms to vector. Includes scaling.
/// </summary>
public Vector3d TransformV(Vector3d v)
{
Expand Down
57 changes: 55 additions & 2 deletions spatial/BasicProjectionTargets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

namespace g3
{
/// <summary>
/// MeshProjectionTarget provides an IProjectionTarget interface to a mesh + spatial data structure.
/// Use to project points to mesh surface.
/// </summary>
public class MeshProjectionTarget : IOrientedProjectionTarget
{
public DMesh3 Mesh { get; set; }
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -69,11 +73,60 @@ public static MeshProjectionTarget Auto(DMesh3 mesh, IEnumerable<int> triangles,
DSubmesh3 submesh = new DSubmesh3(mesh, targetRegion);
return new MeshProjectionTarget(submesh.SubMesh);
}
}




/// <summary>
/// Extension of MeshProjectionTarget that allows the target to have a transformation
/// relative to it's internal space. Call SetTransform(), or initialize the transforms yourself
/// </summary>
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;
Expand Down

0 comments on commit d7912e9

Please sign in to comment.