Skip to content

Commit

Permalink
misc utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rms80 committed Dec 16, 2017
1 parent 9e14897 commit 762cf93
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 8 deletions.
19 changes: 15 additions & 4 deletions math/Frame3f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,14 @@ public void ConstrainedAlignAxis(int nAxis, Vector3f vTo, Vector3f vAround)
Rotate(rot);
}

public Vector3f ProjectToPlane(Vector3f p, int nNormal)
{
Vector3f d = p - origin;
Vector3f n = GetAxis(nNormal);
return origin + (d - d.Dot(n) * n);
}

public Vector3f FromFrameP(Vector2f v, int nPlaneNormalAxis)
public Vector3f FromPlaneUV(Vector2f v, int nPlaneNormalAxis)
{
Vector3f dv = new Vector3f(v[0], v[1], 0);
if (nPlaneNormalAxis == 0) {
Expand All @@ -202,12 +208,17 @@ public Vector3f FromFrameP(Vector2f v, int nPlaneNormalAxis)
}
return this.rotation * dv + this.origin;
}
[System.Obsolete("replaced with FromPlaneUV")]
public Vector3f FromFrameP(Vector2f v, int nPlaneNormalAxis) {
return FromPlaneUV(v, nPlaneNormalAxis);
}

public Vector3f ProjectToPlane(Vector3f p, int nNormal)
public Vector2f ToPlaneUV(Vector3f p, int nNormal = 2, int nAxis0 = 0, int nAxis1 = 1)
{
Vector3f d = p - origin;
Vector3f n = GetAxis(nNormal);
return origin + (d - d.Dot(n) * n);
float fu = d.Dot(GetAxis(nAxis0));
float fv = d.Dot(GetAxis(nAxis1));
return new Vector2f(fu, fv);
}


Expand Down
6 changes: 4 additions & 2 deletions math/MathUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ public static double IsLeft(ref Vector2d P0, ref Vector2d P1, ref Vector2d P2)
/// If point is in triangle plane and inside triangle, coords will be positive and sum to 1.
/// ie if result is a, then vPoint = a.x*V0 + a.y*V1 + a.z*V2.
/// </summary>
public static Vector3d BarycentricCoords(Vector3d vPoint, Vector3d V0, Vector3d V1, Vector3d V2)
public static Vector3d BarycentricCoords(ref Vector3d vPoint, ref Vector3d V0, ref Vector3d V1, ref Vector3d V2)
{
Vector3d kV02 = V0 - V2;
Vector3d kV12 = V1 - V2;
Expand All @@ -536,7 +536,9 @@ public static Vector3d BarycentricCoords(Vector3d vPoint, Vector3d V0, Vector3d
double fBary3 = 1.0 - fBary1 - fBary2;
return new Vector3d(fBary1, fBary2, fBary3);
}

public static Vector3d BarycentricCoords(Vector3d vPoint, Vector3d V0, Vector3d V1, Vector3d V2) {
return BarycentricCoords(ref vPoint, ref V0, ref V1, ref V2);
}

/// <summary>
/// Compute barycentric coordinates/weights of vPoint inside triangle (V0,V1,V2).
Expand Down
33 changes: 33 additions & 0 deletions math/PrimalQuery2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,39 @@ public int ToTriangle(Vector2d test, int v0, int v1, int v2)
return ((sign0 != 0 && sign1 != 0 && sign2 != 0) ? -1 : 0);
}



// [RMS] added to handle queries where mesh is not consistently oriented
// For a triangle with vertices V0, V1, and V2, oriented cw or ccw,
// ToTriangleUnsigned returns
// +1, P outside triangle
// -1, P inside triangle
// 0, P on triangle
// The query involves three calls to ToLine, so the numbers match those
// of ToLine.
public int ToTriangleUnsigned(int i, int v0, int v1, int v2)
{
return ToTriangleUnsigned(PointF(i), v0, v1, v2);

}
public int ToTriangleUnsigned(Vector2d test, int v0, int v1, int v2)
{
int sign0 = ToLine(test, v1, v2);
int sign1 = ToLine(test, v0, v2);
int sign2 = ToLine(test, v0, v1);

// valid sign patterns are -+- and +-+, but also we might
// have zeros...can't figure out a more clever test right now
if ( (sign0 <= 0 && sign1 >= 0 && sign2 <= 0) ||
(sign0 >= 0 && sign1 <= 0 && sign2 >= 0 ))
{
return ((sign0 != 0 && sign1 != 0 && sign2 != 0) ? -1 : 0);
}
return +1;
}



// For a triangle with counterclockwise vertices V0, V1, and V2,
// ToCircumcircle returns
// +1, P outside circumcircle of triangle
Expand Down
2 changes: 1 addition & 1 deletion mesh_generators/GenCylGenerators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ override public MeshGenerator Generate()

int k = nStartR + j;
Vector2d pv = Polygon.Vertices[j % Slices];
Vector3d v = fCur.FromFrameP((Vector2f)pv, 2);
Vector3d v = fCur.FromPlaneUV((Vector2f)pv, 2);
vertices[k] = v;
uv[k] = new Vector2f(uv_along, uv_around);
Vector3f n = (Vector3f)(v - fCur.Origin).Normalized;
Expand Down
16 changes: 15 additions & 1 deletion mesh_selection/MeshBoundaryLoops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,21 @@ public Index2i FindVertexIndex(int vID)


/// <summary>
/// find the loop that contains an edge, or return -1
/// find the loop index that contains a vertex, or return -1
/// </summary>
public int FindLoopContainingVertex(int vid)
{
int N = Loops.Count;
for (int li = 0; li < N; ++li) {
if (Loops[li].Vertices.Contains(vid))
return li;
}
return -1;
}


/// <summary>
/// find the loop index that contains an edge, or return -1
/// </summary>
public int FindLoopContainingEdge(int eid)
{
Expand Down
8 changes: 8 additions & 0 deletions mesh_selection/MeshEdgeSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ public void SelectVertexEdges(IEnumerable<int> vertices)
}
}

public void SelectTriangleEdges(IEnumerable<int> triangles)
{
foreach ( int tid in triangles ) {
Index3i et = Mesh.GetTriEdges(tid);
add(et.a); add(et.b); add(et.c);
}
}


public void Deselect(int tid) {
remove(tid);
Expand Down
32 changes: 32 additions & 0 deletions mesh_selection/MeshVertexSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,37 @@ public void ExpandToOneRingNeighbours(int nRings, Func<int, bool> FilterF = null
}





/// <summary>
/// Grow selection outwards from seed vertex, until it hits boundaries defined by vertex filter.
/// </summary>
public void FloodFill(int vSeed, Func<int, bool> VertIncludedF = null)
{
FloodFill(new int[] { vSeed }, VertIncludedF);
}
/// <summary>
/// Grow selection outwards from seed vertex, until it hits boundaries defined by vertex filter.
/// </summary>
public void FloodFill(int[] Seeds, Func<int, bool> VertIncludedF = null)
{
DVector<int> stack = new DVector<int>(Seeds);
for (int k = 0; k < Seeds.Length; ++k)
add(Seeds[k]);
while (stack.size > 0) {
int vID = stack.back;
stack.pop_back();

foreach ( int nbr_vid in Mesh.VtxVerticesItr(vID) ) {
if ( IsSelected(nbr_vid) == true || VertIncludedF(nbr_vid) == false)
continue;
add(nbr_vid);
stack.push_back(nbr_vid);
}
}
}


}
}
2 changes: 2 additions & 0 deletions queries/MeshValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public enum ValidationStatus
NotBoundaryVertex,
NotBoundaryEdge,

NotATriangle,

VerticesNotConnectedByEdge,
IncorrectLoopOrientation,

Expand Down

0 comments on commit 762cf93

Please sign in to comment.