Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using readonly struct in WPF Ink module #3908

Merged
merged 2 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace MS.Internal.Ink
/// inner area being on the right side.
/// Used in hit-testing a contour vs another contour.
/// </summary>
internal struct ContourSegment
internal readonly struct ContourSegment
{
/// <summary>
/// Constructor for linear segments
Expand Down Expand Up @@ -61,9 +61,9 @@ internal ContourSegment(Point begin, Point end, Point center)

#region Fields

private Point _begin;
private Vector _vector;
private Vector _radius;
private readonly Point _begin;
private readonly Vector _vector;
private readonly Vector _radius;

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal EllipticalNodeOperations(StylusShape nodeShape)
/// <param name="beginNode">a node to connect</param>
/// <param name="endNode">another node, next to beginNode</param>
/// <returns>connecting quadrangle</returns>
internal override Quad GetConnectingQuad(StrokeNodeData beginNode, StrokeNodeData endNode)
internal override Quad GetConnectingQuad(in StrokeNodeData beginNode,in StrokeNodeData endNode)
{
if (beginNode.IsEmpty || endNode.IsEmpty || DoubleUtil.AreClose(beginNode.Position, endNode.Position))
{
Expand Down Expand Up @@ -213,7 +213,7 @@ internal override IEnumerable<ContourSegment> GetNonBezierContourSegments(Stroke
/// <param name="hitEndPoint">an end point of the hitting linear segment</param>
/// <returns>true if the hitting segment intersect the contour comprised of the two stroke nodes</returns>
internal override bool HitTest(
StrokeNodeData beginNode, StrokeNodeData endNode, Quad quad, Point hitBeginPoint, Point hitEndPoint)
in StrokeNodeData beginNode, in StrokeNodeData endNode, Quad quad, Point hitBeginPoint, Point hitEndPoint)
{
StrokeNodeData bigNode, smallNode;
if (beginNode.IsEmpty || (quad.IsEmpty && (endNode.PressureFactor > beginNode.PressureFactor)))
Expand Down Expand Up @@ -278,7 +278,7 @@ internal override bool HitTest(
/// <param name="hitContour">a collection of basic segments outlining the hitting contour</param>
/// <returns>true if the contours intersect or overlap</returns>
internal override bool HitTest(
StrokeNodeData beginNode, StrokeNodeData endNode, Quad quad, IEnumerable<ContourSegment> hitContour)
in StrokeNodeData beginNode, in StrokeNodeData endNode, Quad quad, IEnumerable<ContourSegment> hitContour)
{
StrokeNodeData bigNode, smallNode;
double bigRadiusSquared, smallRadiusSquared = 0;
Expand Down Expand Up @@ -384,7 +384,7 @@ internal override bool HitTest(
/// <param name="hitEndPoint">End point of the hitting segment</param>
/// <returns>Exact location to cut at represented by StrokeFIndices</returns>
internal override StrokeFIndices CutTest(
StrokeNodeData beginNode, StrokeNodeData endNode, Quad quad, Point hitBeginPoint, Point hitEndPoint)
in StrokeNodeData beginNode, in StrokeNodeData endNode, Quad quad, Point hitBeginPoint, Point hitEndPoint)
{
// Compute the positions of the involved points relative to the endNode.
Vector spineVector = beginNode.IsEmpty ? new Vector(0, 0) : (beginNode.Position - endNode.Position);
Expand Down Expand Up @@ -462,7 +462,7 @@ internal override StrokeFIndices CutTest(
/// <param name="hitContour">The hitting ContourSegments</param>
/// <returns>StrokeFIndices representing the location for cutting</returns>
internal override StrokeFIndices CutTest(
StrokeNodeData beginNode, StrokeNodeData endNode, Quad quad, IEnumerable<ContourSegment> hitContour)
in StrokeNodeData beginNode, in StrokeNodeData endNode, Quad quad, IEnumerable<ContourSegment> hitContour)
{
// Compute the positions of the beginNode relative to the endNode.
Vector spineVector = beginNode.IsEmpty ? new Vector(0, 0) : (beginNode.Position - endNode.Position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal struct Quad
{
#region Statics

private static Quad s_empty = new Quad(new Point(0, 0), new Point(0, 0), new Point(0, 0), new Point(0, 0));
private static readonly Quad s_empty = new Quad(new Point(0, 0), new Point(0, 0), new Point(0, 0), new Point(0, 0));

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace MS.Internal.Ink
internal struct StrokeFIndices : IEquatable<StrokeFIndices>
{
#region Private statics
private static StrokeFIndices s_empty = new StrokeFIndices(AfterLast, BeforeFirst);
private static StrokeFIndices s_full = new StrokeFIndices(BeforeFirst, AfterLast);
private static readonly StrokeFIndices s_empty = new StrokeFIndices(AfterLast, BeforeFirst);
private static readonly StrokeFIndices s_full = new StrokeFIndices(BeforeFirst, AfterLast);
#endregion

#region Internal API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ internal struct StrokeNode
internal StrokeNode(
StrokeNodeOperations operations,
int index,
StrokeNodeData nodeData,
StrokeNodeData lastNodeData,
in StrokeNodeData nodeData,
in StrokeNodeData lastNodeData,
bool isLastNode)
{
System.Diagnostics.Debug.Assert(operations != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ namespace MS.Internal.Ink
/// <summary>
/// This structure represents a node on a stroke spine.
/// </summary>
internal struct StrokeNodeData
internal readonly struct StrokeNodeData
{
#region Statics

private static StrokeNodeData s_empty = new StrokeNodeData();
private static readonly StrokeNodeData s_empty = new StrokeNodeData();

#endregion

#region API (internal)

/// <summary> Returns static object representing an unitialized node </summary>
internal static StrokeNodeData Empty { get { return s_empty; } }
internal static ref readonly StrokeNodeData Empty { get { return ref s_empty; } }

/// <summary>
/// Constructor for nodes of a pressure insensitive stroke
Expand Down Expand Up @@ -76,8 +76,8 @@ internal Point Position

#region Privates

private Point _position;
private float _pressure;
private readonly Point _position;
private readonly float _pressure;

#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal StrokeNodeOperations(StylusShape nodeShape)
/// </summary>
/// <param name="node">node to compute bounds of</param>
/// <returns>bounds of the node</returns>
internal Rect GetNodeBounds(StrokeNodeData node)
internal Rect GetNodeBounds(in StrokeNodeData node)
{
if (_shapeBounds.IsEmpty)
{
Expand Down Expand Up @@ -97,7 +97,7 @@ internal Rect GetNodeBounds(StrokeNodeData node)
return boundingBox;
}

internal void GetNodeContourPoints(StrokeNodeData node, List<Point> pointBuffer)
internal void GetNodeContourPoints(in StrokeNodeData node, List<Point> pointBuffer)
{
double pressureFactor = node.PressureFactor;
if (DoubleUtil.AreClose(pressureFactor, 1d))
Expand Down Expand Up @@ -182,7 +182,7 @@ internal virtual IEnumerable<ContourSegment> GetNonBezierContourSegments(StrokeN
/// <param name="beginNode">a node to connect</param>
/// <param name="endNode">another node, next to beginNode</param>
/// <returns>connecting quadrangle, that can be empty if one node is inside the other</returns>
internal virtual Quad GetConnectingQuad(StrokeNodeData beginNode, StrokeNodeData endNode)
internal virtual Quad GetConnectingQuad(in StrokeNodeData beginNode, in StrokeNodeData endNode)
{
// Return an empty quad if either of the nodes is empty (not a node)
// or if both nodes are at the same position.
Expand Down Expand Up @@ -296,7 +296,7 @@ internal virtual Quad GetConnectingQuad(StrokeNodeData beginNode, StrokeNodeData
/// <param name="hitEndPoint">End point of the hitting segment</param>
/// <returns>true if there's intersection, false otherwise</returns>
internal virtual bool HitTest(
StrokeNodeData beginNode, StrokeNodeData endNode, Quad quad, Point hitBeginPoint, Point hitEndPoint)
in StrokeNodeData beginNode, in StrokeNodeData endNode, Quad quad, Point hitBeginPoint, Point hitEndPoint)
{
// Check for special cases when the endNode is the very first one (beginNode.IsEmpty)
// or one node is completely inside the other. In either case the connecting quad
Expand Down Expand Up @@ -441,7 +441,7 @@ internal virtual bool HitTest(
/// <param name="hitContour">a collection of basic segments outlining the hitting contour</param>
/// <returns>true if the contours intersect or overlap</returns>
internal virtual bool HitTest(
StrokeNodeData beginNode, StrokeNodeData endNode, Quad quad, IEnumerable<ContourSegment> hitContour)
in StrokeNodeData beginNode, in StrokeNodeData endNode, Quad quad, IEnumerable<ContourSegment> hitContour)
{
// Check for special cases when the endNode is the very first one (beginNode.IsEmpty)
// or one node is completely inside the other. In either case the connecting quad
Expand Down Expand Up @@ -469,7 +469,7 @@ internal virtual bool HitTest(
/// <param name="hitEndPoint">End point of the hitting segment</param>
/// <returns>Exact location to cut at represented by StrokeFIndices</returns>
internal virtual StrokeFIndices CutTest(
StrokeNodeData beginNode, StrokeNodeData endNode, Quad quad, Point hitBeginPoint, Point hitEndPoint)
in StrokeNodeData beginNode, in StrokeNodeData endNode, Quad quad, Point hitBeginPoint, Point hitEndPoint)
{
StrokeFIndices result = StrokeFIndices.Empty;

Expand Down Expand Up @@ -561,7 +561,7 @@ internal virtual StrokeFIndices CutTest(
/// <param name="hitContour">a collection of basic segments outlining the hitting contour</param>
/// <returns></returns>
internal virtual StrokeFIndices CutTest(
StrokeNodeData beginNode, StrokeNodeData endNode, Quad quad, IEnumerable<ContourSegment> hitContour)
in StrokeNodeData beginNode, in StrokeNodeData endNode, Quad quad, IEnumerable<ContourSegment> hitContour)
{
if (beginNode.IsEmpty)
{
Expand Down Expand Up @@ -930,7 +930,7 @@ internal Vector[] GetVertices()
/// <param name="endNode">End node of the stroke segment</param>
/// <returns>true if hit; false otherwise</returns>
private bool HitTestPolygonContourSegments(
IEnumerable<ContourSegment> hitContour, StrokeNodeData beginNode, StrokeNodeData endNode)
IEnumerable<ContourSegment> hitContour, in StrokeNodeData beginNode, in StrokeNodeData endNode)
{
bool isHit = false;

Expand Down Expand Up @@ -1019,7 +1019,7 @@ private bool HitTestPolygonContourSegments(
/// <param name="endNode">End node of the stroke segment</param>
/// <returns>true if hit; false otherwise</returns>
private bool HitTestInkContour(
IEnumerable<ContourSegment> hitContour, Quad quad, StrokeNodeData beginNode, StrokeNodeData endNode)
IEnumerable<ContourSegment> hitContour, Quad quad, in StrokeNodeData beginNode, in StrokeNodeData endNode)
{
System.Diagnostics.Debug.Assert(!quad.IsEmpty);
bool isHit = false;
Expand Down Expand Up @@ -1183,7 +1183,7 @@ private bool HitTestInkContour(
/// <param name="result"></param>
/// <returns></returns>
private bool HitTestStrokeNodes(
ContourSegment hitSegment, StrokeNodeData beginNode, StrokeNodeData endNode, ref StrokeFIndices result)
in ContourSegment hitSegment, in StrokeNodeData beginNode, in StrokeNodeData endNode, ref StrokeFIndices result)
{
// First, find out if hitSegment intersects with either of the ink nodes
bool isHit = false;
Expand Down Expand Up @@ -1271,7 +1271,7 @@ private bool HitTestStrokeNodes(
/// <param name="pressureDelta"></param>
/// <returns>the clip location. not-clip if return StrokeFIndices.BeforeFirst</returns>
private double CalculateClipLocation(
ContourSegment hitSegment, StrokeNodeData beginNode, Vector spineVector, double pressureDelta)
in ContourSegment hitSegment, in StrokeNodeData beginNode, Vector spineVector, double pressureDelta)
{
double findex = StrokeFIndices.BeforeFirst;
bool clipIt = hitSegment.IsArc ? true
Expand Down