<commit_before>import * as hittest from "core/hittest"
import {RBush} from "core/util/spatial"
import {Glyph, GlyphView} from "./glyph"

export class SegmentView extends GlyphView

  _index_data: () ->
    points = []
    for i in [0...@_x0.length]
      if not isNaN(@_x0[i] + @_x1[i] + @_y0[i] + @_y1[i])
        points.push({
          minX: Math.min(@_x0[i], @_x1[i]),
          minY: Math.min(@_y0[i], @_y1[i]),
          maxX: Math.max(@_x0[i], @_x1[i]),
          maxY: Math.max(@_y0[i], @_y1[i]),
          i: i
        })

    return new RBush(points)

  _render: (ctx, indices, {sx0, sy0, sx1, sy1}) ->
    if @visuals.line.doit
      for i in indices
        if isNaN(sx0[i]+sy0[i]+sx1[i]+sy1[i])
          continue

        ctx.beginPath()
        ctx.moveTo(sx0[i], sy0[i])
        ctx.lineTo(sx1[i], sy1[i])

        @visuals.line.set_vectorize(ctx, i)
        ctx.stroke()

  _hit_point: (geometry) ->
    [vx, vy] = [geometry.vx, geometry.vy]
    x = @renderer.xmapper.map_from_target(vx, true)
    y = @renderer.ymapper.map_from_target(vy, true)

    threshold = Math.max(2, @visuals.line.line_width.value() / 2)

    point =
      x: this.renderer.plot_view.canvas.vx_to_sx(vx)
      y: this.renderer.plot_view.canvas.vy_to_sy(vy)

    hits = []

    candidates = @index.indices({minX: x, minY: y, maxX: x, maxY: y})
    for i in candidates
      [p0, p1] = [{x: @sx0[i], y: @sy0[i]}, {x: @sx1[i], y: @sy1[i]}]
      dist = hittest.dist_to_segment(point, p0, p1)
      if dist < threshold
        hits.push(i)

    result = hittest.create_hit_test_result()
    result['1d'].indices = hits
    return result

  _hit_span: (geometry) ->
    hr = @renderer.plot_view.frame.h_range
    vr = @renderer.plot_view.frame.v_range

    [vx, vy] = [geometry.vx, geometry.vy]

    if geometry.direction == 'v'
      val = @renderer.ymapper.map_from_target(vy)
      [v0, v1] = [@_y0, @_y1]
    else
      val = @renderer.xmapper.map_from_target(vx)
      [v0, v1] = [@_x0, @_x1]

    hits = []

    candidates = @index.indices({
      minX: @renderer.xmapper.map_from_target(hr.min),
      minY: @renderer.ymapper.map_from_target(vr.min),<commit_msg>correctly handle scalar or vectorized line widths<commit_after>import * as hittest from "core/hittest"
import {RBush} from "core/util/spatial"
import {Glyph, GlyphView} from "./glyph"

export class SegmentView extends GlyphView

  _index_data: () ->
    points = []
    for i in [0...@_x0.length]
      if not isNaN(@_x0[i] + @_x1[i] + @_y0[i] + @_y1[i])
        points.push({
          minX: Math.min(@_x0[i], @_x1[i]),
          minY: Math.min(@_y0[i], @_y1[i]),
          maxX: Math.max(@_x0[i], @_x1[i]),
          maxY: Math.max(@_y0[i], @_y1[i]),
          i: i
        })

    return new RBush(points)

  _render: (ctx, indices, {sx0, sy0, sx1, sy1}) ->
    if @visuals.line.doit
      for i in indices
        if isNaN(sx0[i]+sy0[i]+sx1[i]+sy1[i])
          continue

        ctx.beginPath()
        ctx.moveTo(sx0[i], sy0[i])
        ctx.lineTo(sx1[i], sy1[i])

        @visuals.line.set_vectorize(ctx, i)
        ctx.stroke()

  _hit_point: (geometry) ->
    [vx, vy] = [geometry.vx, geometry.vy]
    x = @renderer.xmapper.map_from_target(vx, true)
    y = @renderer.ymapper.map_from_target(vy, true)

    point =
      x: this.renderer.plot_view.canvas.vx_to_sx(vx)
      y: this.renderer.plot_view.canvas.vy_to_sy(vy)

    hits = []

    candidates = @index.indices({minX: x, minY: y, maxX: x, maxY: y})
    for i in candidates
      threshold = Math.max(2, @visuals.line.cache_select('line_width', i) / 2)
      [p0, p1] = [{x: @sx0[i], y: @sy0[i]}, {x: @sx1[i], y: @sy1[i]}]
      dist = hittest.dist_to_segment(point, p0, p1)
      if dist < threshold
        hits.push(i)

    result = hittest.create_hit_test_result()
    result['1d'].indices = hits
    return result

  _hit_span: (geometry) ->
    hr = @renderer.plot_view.frame.h_range
    vr = @renderer.plot_view.frame.v_range

    [vx, vy] = [geometry.vx, geometry.vy]

    if geometry.direction == 'v'
      val = @renderer.ymapper.map_from_target(vy)
      [v0, v1] = [@_y0, @_y1]
    else
      val = @renderer.xmapper.map_from_target(vx)
      [v0, v1] = [@_x0, @_x1]

    hits = []

    candidates = @index.indices({
      minX: @renderer.xmapper.map_from_target(hr.min),
      minY: @renderer.ymapper.map_from_target(vr.min),
{'fraction_non_alphanumeric': 0.16926605504587156, 'fraction_numerical': 0.020642201834862386, 'mean_word_length': 2.6463210702341136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1077344', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1784, 'n_words': 336}