/// <summary> /// Extracts an integer from the current line at the specified location. /// </summary> /// <param name="iterator">The current line information.</param> /// <param name="start"> /// The start index of the integer relative to the start of the line. /// </param> /// <param name="length">The length of the integer.</param> /// <returns>An integer representation of the string.</returns> protected static int GetInt32(LineIterator iterator, int start, int length) { int value; StringUtils.TryParseInt32(iterator.Buffer, iterator.Offset + start, length, out value); return(value); }
private static void LineIterator() { var img = new Mat("data/lenna.png", ImreadModes.Color); var pt1 = new Point(100, 100); var pt2 = new Point(300, 300); var iterator = new LineIterator(img, pt1, pt2, PixelConnectivity.Connectivity8); // invert color foreach (var pixel in iterator) { Vec3b value = pixel.GetValue<Vec3b>(); value.Item0 = (byte)~value.Item0; value.Item1 = (byte)~value.Item1; value.Item2 = (byte)~value.Item2; pixel.SetValue(value); } // re-enumeration works fine foreach (var pixel in iterator) { Vec3b value = pixel.GetValue<Vec3b>(); Console.WriteLine("{0} = ({1},{2},{3})", pixel.Pos, value.Item0, value.Item1, value.Item2); } Window.ShowImages(img); }
/* Function: GetPossibleDocumentationComments * * Goes through the file looking for comments that could possibly contain documentation and returns them as a list. These * comments are not guaranteed to have documentation in them, just to be acceptable candidates for them. If there are no * comments it will return an empty list. * * All the comments in the returned list will have their comment symbols marked as <CommentParsingType.CommentSymbol> * in the tokenizer. This allows further operations to be done on them in a language independent manner. If you want to also * filter out text boxes and lines, use <Comments.LineFinder>. */ override public List <PossibleDocumentationComment> GetPossibleDocumentationComments(Tokenizer source) { List <PossibleDocumentationComment> possibleDocumentationComments = new List <PossibleDocumentationComment>(); LineIterator lineIterator = source.FirstLine; while (lineIterator.IsInBounds) { PossibleDocumentationComment possibleDocumentationComment = null; if (TryToGetBlockComment(ref lineIterator, out possibleDocumentationComment) || TryToGetLineComment(ref lineIterator, out possibleDocumentationComment)) { if (possibleDocumentationComment != null) { // XML can actually use the Javadoc comment format in addition to its own. if (possibleDocumentationComment.Javadoc == true) { possibleDocumentationComment.XML = true; } possibleDocumentationComments.Add(possibleDocumentationComment); } // lineIterator should already be moved } else { lineIterator.Next(); } } return(possibleDocumentationComments); }
/* Function: TryToGetLineComment * If the iterator is on a line that starts with a line comment symbol, this function moves the iterator past the entire * comment and returns true. If the comment is a candidate for documentation it will also return it as a * <PossibleDocumentationComment>. If the line does not start with a line comment symbol it will return false and * leave the iterator where it is. */ protected bool TryToGetLineComment(ref LineIterator lineIterator, string commentSymbol, out PossibleDocumentationComment comment) { TokenIterator firstToken = lineIterator.FirstToken(LineBoundsMode.ExcludeWhitespace); if (firstToken.MatchesAcrossTokens(commentSymbol) == false) { comment = null; return(false); } comment = new PossibleDocumentationComment(); comment.Start = lineIterator; lineIterator.Next(); // Since we're definitely returning a comment we can mark the comment symbols as we go rather than waiting until // the end. firstToken.SetCommentParsingTypeByCharacters(CommentParsingType.CommentSymbol, commentSymbol.Length); while (lineIterator.IsInBounds) { firstToken = lineIterator.FirstToken(LineBoundsMode.ExcludeWhitespace); if (firstToken.MatchesAcrossTokens(commentSymbol) == false) { break; } firstToken.SetCommentParsingTypeByCharacters(CommentParsingType.CommentSymbol, commentSymbol.Length); lineIterator.Next(); } comment.End = lineIterator; return(true); }
/* Function: TryToGetFirstBlockLine * If the iterator is on a line that starts with one of the <BlockTags>, extracts the components and returns true. * Use <GetBlockTag()> to get the complete block since it may span multiple lines. */ protected bool TryToGetFirstBlockLine(LineIterator lineIterator, out string tag, out TokenIterator startOfContent) { tag = null; startOfContent = default(TokenIterator); TokenIterator tokenIterator = lineIterator.FirstToken(LineBoundsMode.CommentContent); if (tokenIterator.Character != '@') { return(false); } tokenIterator.Next(); if (tokenIterator.FundamentalType != FundamentalType.Text) { return(false); } string possibleTag = tokenIterator.String; if (BlockTags.Contains(possibleTag) == false) { return(false); } tokenIterator.Next(); tokenIterator.NextPastWhitespace(); tag = possibleTag; startOfContent = tokenIterator; return(true); }
/* Function: IsHorizontalLine * Returns whether the passed <LineIterator> is at a horizontal line, not including any comment symbols or decoration. */ public static bool IsHorizontalLine(LineIterator line) { TokenIterator start, end; line.GetBounds(LineBoundsMode.CommentContent, out start, out end); char symbolA, symbolB, symbolC; int symbolACount, symbolBCount, symbolCCount; bool lineResult = CountSymbolLine(ref start, end, out symbolA, out symbolB, out symbolC, out symbolACount, out symbolBCount, out symbolCCount); // Return false if it's not the only thing on the line. if (lineResult == false || start != end) { return(false); } else if (symbolACount >= 4 && (symbolBCount == 0 || (symbolBCount <= 3 && symbolCCount == 0))) { return(true); } else if (symbolACount <= 3 && symbolBCount >= 4 && (symbolCCount == 0 || symbolCCount <= 3)) { return(true); } else { return(false); } }
private static void LineIterator() { var img = new Mat("data/lenna.png", ImreadModes.Color); var pt1 = new Point(100, 100); var pt2 = new Point(300, 300); var iterator = new LineIterator(img, pt1, pt2, PixelConnectivity.Connectivity8); // invert color foreach (var pixel in iterator) { Vec3b value = pixel.GetValue <Vec3b>(); value.Item0 = (byte)~value.Item0; value.Item1 = (byte)~value.Item1; value.Item2 = (byte)~value.Item2; pixel.SetValue(value); } // re-enumeration works fine foreach (var pixel in iterator) { Vec3b value = pixel.GetValue <Vec3b>(); Console.WriteLine("{0} = ({1},{2},{3})", pixel.Pos, value.Item0, value.Item1, value.Item2); } Window.ShowImages(img); }
/// <summary> /// Loads the specified file for calls to Read. /// </summary> /// <param name="fileInfo">The file to load.</param> internal void LoadFile(MainfileInfo fileInfo) { Check.IsNotNull(fileInfo, "fileInfo"); this.DisposeReaderStream(); this.readerStream = FileSystem.Instance.OpenRead(fileInfo.Path); this.readerIterator = new LineIterator(this.readerStream, fileInfo.RecordLength, fileInfo.RecordCount); }
/// <inheritdoc /> protected override void ParseLine(PafRepository repository, LineIterator iterator) { var record = new ThoroughfareDescriptorRecord(); record.Key = GetInt32(iterator, KeyStart, KeyLength); record.Descriptor = GetString(iterator, DescriptorStart, DescriptorLength); repository.AddThoroughfareDescriptor(record); }
/// <inheritdoc /> protected override object[] ParseLine(LineIterator iterator) { return(new object[] { GetInt32(iterator, KeyStart, KeyLength), GetString(iterator, NameStart, NameLength) }); }
/* Function: IsFirstBlockLine * Whether the iterator is on a line that starts with one of the <BlockTags>. */ protected bool IsFirstBlockLine(LineIterator lineIterator) { // For simplicity we'll just hand off to TryToGetFirstBlockLine() and ignore the extracted content. string ignoreTag; TokenIterator ignoreStartOfContent; return(TryToGetFirstBlockLine(lineIterator, out ignoreTag, out ignoreStartOfContent)); }
/// <inheritdoc /> protected override void ParseLine(PafRepository repository, LineIterator iterator) { var record = new SubBuildingNameRecord(); record.Key = GetInt32(iterator, KeyStart, KeyLength); record.Name = GetString(iterator, NameStart, NameLength); repository.AddSubBuildingName(record); }
/// <param name="hasSegMarkers">if true, input has segmentation markers</param> /// <param name="hasTags">if true, input has morphological analyses separated by tagDelimiter.</param> /// <param name="hasDomainLabels"> /// if true, input has a whitespace-terminated domain at the beginning /// of each line of text /// </param> /// <param name="stripRewrites"> /// if true, erase orthographical rewrites from the gold labels (for /// comparison purposes) /// </param> /// <param name="tokFactory">a TokenizerFactory for the input</param> public ArabicDocumentReaderAndWriter(bool hasSegMarkers, bool hasTags, bool hasDomainLabels, string domain, bool stripRewrites, ITokenizerFactory <CoreLabel> tokFactory) { tf = tokFactory; inputHasTags = hasTags; inputHasDomainLabels = hasDomainLabels; inputDomain = domain; shouldStripRewrites = stripRewrites; segMarker = hasSegMarkers ? DefaultSegMarker : null; factory = LineIterator.GetFactory(new _ISerializableFunction_131(this)); }
/// <inheritdoc /> protected override void ParseLine(PafRepository repository, LineIterator iterator) { var record = new OrganisationRecord(); record.Key = GetInt32(iterator, KeyStart, KeyLength); record.PostcodeType = ParsePostcodeType(iterator.Buffer[iterator.Offset + PostcodeTypeIndex]); record.Name = GetString(iterator, NameStart, NameLength); record.Department = GetString(iterator, DepartmentStart, DepartmentLength); repository.AddOrganisation(record); }
/// <inheritdoc /> protected override object[] ParseLine(LineIterator iterator) { return(new object[] { GetInt32(iterator, KeyStart, KeyLength), GetString(iterator, NameStart, NameLength) ?? string.Empty, // Not sure if this is correct but the sample PAF data has completely empty organisations!? GetString(iterator, DepartmentStart, DepartmentLength), (byte)ParsePostcodeType(iterator.Buffer[iterator.Offset + PostcodeTypeIndex]) }); }
private List <Line> GetLines(LineIterator iterator) { List <Line> lines = new List <Line>(); while (iterator.MoveNext()) { lines.Add(iterator.current); } return(lines); }
public void SumPixelsByte() { var p1 = new Point(0, 0); var p2 = new Point(9, 9); using (Mat mat = new Mat(10, 10, MatType.CV_8UC1, 2)) using (var lineIterator = new LineIterator(mat, p1, p2)) { var sum = lineIterator.Sum(pixel => pixel.GetValue <byte>()); Assert.Equal(10 * 2, sum); } }
public void CountPixels() { var p1 = new Point(0, 0); var p2 = new Point(9, 9); using (Mat mat = Mat.Zeros(10, 10, MatType.CV_8UC1)) using (var lineIterator = new LineIterator(mat, p1, p2)) { var count = lineIterator.Count(); Assert.Equal(10, count); } }
public void CountPixels() { var p1 = new Point(0, 0); var p2 = new Point(9, 9); using (Mat mat = Mat.Zeros(10, 10, MatType.CV_8UC1)) using (var lineIterator = new LineIterator(mat, p1, p2)) { #pragma warning disable CA1829 var count = lineIterator.Count(); #pragma warning restore CA1829 Assert.Equal(10, count); } }
public void SwapWithNext_LastNR() { Init(); lines.SetText("line0\nline1\nline2\nline3"); LineIterator iterator = lines.GetLineRange(0, lines.LinesCount); iterator.MoveNext(); iterator.MoveNext(); iterator.MoveNext(); Assert.AreEqual("line2\n", iterator.current.Text); iterator.SwapCurrent(false); AssertText("line0\nline1\nline3\nline2"); }
/// <summary> /// Parses the specified file, adding parsed records to the specified /// repository. /// </summary> /// <param name="info">Contains the information about the file.</param> /// <param name="repository">The repository to add records to.</param> public void Parse(MainfileInfo info, PafRepository repository) { Check.IsNotNull(info, "info"); Check.IsNotNull(repository, "repository"); using (Stream stream = FileSystem.Instance.OpenRead(info.Path)) { var iterator = new LineIterator(stream, info.RecordLength, info.RecordCount); while (iterator.MoveNext()) { this.ParseLine(repository, iterator); } } }
private bool IsLinePresent(Mat image, Point start, Point end, Scalar color) { LineIterator it = new LineIterator(image, start, end); Vec3b rc = color.ToVec3b(); // required color foreach (var p in it) { var cc = p.GetValue <Vec3b>(); if (cc.Item0 != rc.Item0 || cc.Item1 != rc.Item1 || cc.Item2 != rc.Item2) { return(false); } } return(true); }
/* Function: GetPossibleDocumentationComments * * Goes through the file looking for comments that could possibly contain documentation and returns them as a list. These * comments are not guaranteed to have documentation in them, just to be acceptable candidates for them. If there are no * comments it will return an empty list. */ protected List <PossibleDocumentationComment> GetPossibleDocumentationComments(Tokenizer source) { List <PossibleDocumentationComment> possibleDocumentationComments = new List <PossibleDocumentationComment>(); LineIterator lineIterator = source.FirstLine; while (lineIterator.IsInBounds) { bool foundComment = false; PossibleDocumentationComment possibleDocumentationComment = null; // Block comments if (blockCommentStringPairs != null) { for (int i = 0; foundComment == false && i < blockCommentStringPairs.Length; i += 2) { foundComment = TryToGetBlockComment(ref lineIterator, blockCommentStringPairs[i], blockCommentStringPairs[i + 1], out possibleDocumentationComment); } } // Plain line comments if (foundComment == false && lineCommentStrings != null) { for (int i = 0; foundComment == false && i < lineCommentStrings.Length; i++) { foundComment = TryToGetLineComment(ref lineIterator, lineCommentStrings[i], out possibleDocumentationComment); } } // Nada. if (foundComment == false) { lineIterator.Next(); } else { if (possibleDocumentationComment != null) { possibleDocumentationComments.Add(possibleDocumentationComment); } // lineIterator would have been moved already if foundComment is true } } return(possibleDocumentationComments); }
/* Function: TryToGetPDLineComment * * If the line iterator is on a line comment, return it and all connected line comments as a * <PossibleDocumentationComment> and mark the symbols as <CommentParsingType.CommentSymbol>. Returns * null otherwise. * * This function takes a separate comment symbol for the first line and all remaining lines, allowing you to detect * Javadoc line comments that start with ## and the remaining lines use #. Both symbols can be the same if this isn't * required. If openingMustBeAlone is set, no symbol can appear immediately after the first line symbol for this * function to succeed. This allows you to specifically detect something like ## without also matching #######. */ protected PossibleDocumentationComment TryToGetPDLineComment(LineIterator lineIterator, string firstSymbol, string remainderSymbol, bool openingMustBeAlone) { TokenIterator firstToken = lineIterator.FirstToken(LineBoundsMode.ExcludeWhitespace); if (firstToken.MatchesAcrossTokens(firstSymbol) == false) { return(null); } if (openingMustBeAlone) { TokenIterator nextToken = firstToken; nextToken.NextByCharacters(firstSymbol.Length); if (nextToken.FundamentalType == FundamentalType.Symbol) { return(null); } } PossibleDocumentationComment comment = new PossibleDocumentationComment(); comment.Start = lineIterator; lineIterator.Next(); // Since we're definitely returning a comment (barring the operation being cancelled) we can mark the comment // symbols as we go rather than waiting until the end. firstToken.SetCommentParsingTypeByCharacters(CommentParsingType.CommentSymbol, firstSymbol.Length); while (lineIterator.IsInBounds) { firstToken = lineIterator.FirstToken(LineBoundsMode.ExcludeWhitespace); if (firstToken.MatchesAcrossTokens(remainderSymbol) == false) { break; } firstToken.SetCommentParsingTypeByCharacters(CommentParsingType.CommentSymbol, remainderSymbol.Length); lineIterator.Next(); } comment.End = lineIterator; return(comment); }
public void LineIterator_GetNextRange() { Init(4); lines.SetText("0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14"); CollectionAssert.AreEqual(new Line[] { lines[0], lines[1] }, GetLines(lines.GetLineRange(0, 2))); CollectionAssert.AreEqual(new Line[] { lines[1], lines[2] }, GetLines(lines.GetLineRange(1, 2))); CollectionAssert.AreEqual(new Line[] { lines[11], lines[12], lines[13] }, GetLines(lines.GetLineRange(11, 3))); CollectionAssert.AreEqual(new Line[] { lines[12], lines[13], lines[14] }, GetLines(lines.GetLineRange(12, 3))); LineIterator iterator = lines.GetLineRange(2, 10); iterator.MoveNext(); Assert.AreEqual(lines[2], iterator.current); iterator.MoveNext(); Assert.AreEqual(lines[3], iterator.current); CollectionAssert.AreEqual( new Line[] { lines[4], lines[5], lines[6], lines[7], lines[8] }, GetLines(iterator.GetNextRange(5))); }
public void SwapLinesWithPrev_SingleBlock() { Init(); lines.SetText("line0\nline1\nline2\nline3"); LineIterator iterator = lines.GetLineRange(0, lines.LinesCount); iterator.MoveNext(); iterator.MoveNext(); Assert.AreEqual("line1\n", iterator.current.Text); iterator.SwapCurrent(true); AssertText("line1\nline0\nline2\nline3"); iterator.MoveNext(); Assert.AreEqual("line2\n", iterator.current.Text); iterator.SwapCurrent(true); AssertText("line1\nline2\nline0\nline3"); }
public void SumPixelsVec3b() { var p1 = new Point(0, 0); var p2 = new Point(9, 9); using (Mat mat = new Mat(10, 10, MatType.CV_8UC3, new Scalar(1, 2, 3))) using (var lineIterator = new LineIterator(mat, p1, p2)) { int b = 0, g = 0, r = 0; foreach (var pixel in lineIterator) { var value = pixel.GetValue <Vec3b>(); (b, g, r) = (b + value.Item0, g + value.Item1, r + value.Item2); } Assert.Equal(10, b); Assert.Equal(20, g); Assert.Equal(30, r); } }
/* Function: CountEdgeSymbols * * An internal function that detects whether the <LineIterator> has symbols on its left and/or right sides. The * symbols must be no longer than three characters and be separated by whitespace from any other content on * the line. If either edge has symbols it will return true along with what they are and how many. If neither do it * will return false. The variables will be set to the null character and a zero count for any edge that doesn't have * symbols. * * symbolIsAloneOnLine is set to true if there was only one symbol on the line and no other content. The symbol * and count will be returned as the left side, but it's possible it would be intended for the right. For example, * look at this comment box: * * > //////////// * > // text // * > // // * > // text // * > //////////// * * Marking the comment symbols will leave the resulting content like this when using * <LineBoundsMode.CommentContent>: * * > ////////// * > text // * > // * > text // * > ////////// * * The slashes on the middle line will be returned as the left symbol but is meant to be the right. Make sure you * handle this situation correctly. */ private static bool CountEdgeSymbols(LineIterator line, out char leftSymbol, out char rightSymbol, out int leftSymbolCount, out int rightSymbolCount, out bool symbolIsAloneOnLine) { symbolIsAloneOnLine = false; TokenIterator lineStart, lineEnd; line.GetBounds(LineBoundsMode.CommentContent, out lineStart, out lineEnd); if (CountSymbols(ref lineStart, lineEnd, out leftSymbol, out leftSymbolCount)) { if ((lineStart.FundamentalType != FundamentalType.Whitespace && lineStart != lineEnd) || leftSymbolCount > 3) { leftSymbol = '\0'; leftSymbolCount = 0; } while (lineStart.FundamentalType == FundamentalType.Whitespace && lineStart < lineEnd) { lineStart.Next(); } if (lineStart == lineEnd) { symbolIsAloneOnLine = true; } } if (ReverseCountSymbols(lineStart, ref lineEnd, out rightSymbol, out rightSymbolCount)) { lineEnd.Previous(); if ((lineEnd >= lineStart && lineEnd.FundamentalType != FundamentalType.Whitespace) || rightSymbolCount > 3) { rightSymbol = '\0'; rightSymbolCount = 0; } } return(leftSymbolCount != 0 || rightSymbolCount != 0); }
/* Function: Parse * * Attempts to parse the passed comment into <Topics>. Returns whether it was successful, and if so, adds them * to the list. These fields will be set: * * - CommentLineNumber * - Body, if present * - Summary, if available */ public bool Parse(PossibleDocumentationComment sourceComment, List <Topic> topics) { if (HasAnyTag(sourceComment.Start.FirstToken(LineBoundsMode.CommentContent), sourceComment.End.FirstToken(LineBoundsMode.Everything)) == false) { return(false); } LineIterator firstBlockLine = sourceComment.Start; while (firstBlockLine < sourceComment.End && IsFirstBlockLine(firstBlockLine) == false) { firstBlockLine.Next(); } JavadocComment parsedComment = new JavadocComment(); if (sourceComment.Start < firstBlockLine) { parsedComment.Description = GetText(sourceComment.Start, firstBlockLine); } while (firstBlockLine < sourceComment.End) { // There may be unrecognized blocks so we have to manually advance if GetBlockTag() fails. if (!TryToGetBlock(ref firstBlockLine, sourceComment.End, parsedComment)) { firstBlockLine.Next(); } } Topic topic = GenerateTopic(parsedComment); if (topic != null) { topic.CommentLineNumber = sourceComment.Start.LineNumber; topics.Add(topic); } return(true); }
private static void CalculateIntersections(SegmentBase segment1, SegmentBase segment2, bool skipInner, bool skipOuter) { if (!segment1.PolylineApproximation.BoundingBox.IntersectsWith(segment2.PolylineApproximation.BoundingBox)) { return; } ConvertTimer.ThrowIfOvertime(); var iterator1 = new LineIterator(segment1.PolylineApproximation); while (iterator1.MoveNextLine()) { var iterator2 = new LineIterator(segment2.PolylineApproximation); var skipCurrentPart1 = true; while (iterator2.MoveNextLine()) { if (!iterator1.CurrentPart.BoundingBox.IntersectsWith(iterator2.CurrentPart.BoundingBox)) { iterator2.SkipCurrentPart(); continue; } skipCurrentPart1 = false; if ((skipInner && iterator1.IsLastLine && iterator2.IsFirstLine) || (skipOuter && iterator1.IsFirstLine && iterator2.IsLastLine)) { continue; } var intersection = CalculateIntersection(iterator1.CurrentStartPoint, iterator1.CurrentEndPoint, iterator2.CurrentStartPoint, iterator2.CurrentEndPoint); if (intersection.HasValue) { segment1.Intersections.Add(intersection.Value); segment2.Intersections.Add(intersection.Value); } } if (skipCurrentPart1) { iterator1.SkipCurrentPart(); } } }
private static void CalculateSelfIntersections(SegmentBase segment) { if (!(segment is CubicBezierSegment)) { // only cubic bezier segment can self-intersect return; } ConvertTimer.ThrowIfOvertime(); var iterator1 = new LineIterator(segment.PolylineApproximation); while (iterator1.MoveNextLine()) { var iterator2 = iterator1.Clone(); if (!iterator2.MoveNextLine()) { return; } var skipCurrentPart1 = true; while (iterator2.MoveNextLine()) { if (!iterator1.CurrentPart.BoundingBox.IntersectsWith(iterator2.CurrentPart.BoundingBox)) { iterator2.SkipCurrentPart(); continue; } skipCurrentPart1 = false; var intersection = CalculateIntersection(iterator1.CurrentStartPoint, iterator1.CurrentEndPoint, iterator2.CurrentStartPoint, iterator2.CurrentEndPoint); if (intersection.HasValue) { segment.Intersections.Add(intersection.Value); } } if (skipCurrentPart1) { iterator1.SkipCurrentPart(); } } }