Skip to content

Add VFH Step 1: polar histogram construction from LiDAR data - #80

Merged
ShisatoYano merged 3 commits into
ShisatoYano:mainfrom
Khushi0512:feature/vfh-step1-polar-histogram
Sep 28, 2026
Merged

ShisatoYano merged 3 commits into
ShisatoYano:mainfrom
Khushi0512:feature/vfh-step1-polar-histogram

Conversation

@Khushi0512

Copy link
Copy Markdown
Contributor

Implements the first incremental step of the Vector Field Histogram (VFH) roadmap agreed with the maintainer in issue #52: build a 1D polar obstacle density histogram around the vehicle directly from the existing 2D LiDAR sensor simulation, with configurable sector resolution and smoothing.

  • PolarHistogram (src/components/mapping/polar_histogram/polar_histogram.py): pure-logic histogram construction. Bins LiDAR returns into angular sectors, weights each by the classic VFH linear magnitude falloff (Borenstein & Koren, 1991), and applies a triangular smoothing filter across neighboring sectors.
  • PolarHistogramMapper (polar_histogram_mapper.py): wires PolarHistogram to the existing OmniDirectionalLidar point cloud and renders it as a ring of colored wedges around the vehicle (denser sectors longer/redder), using the project's existing mapper slot on FourWheelsVehicle - mirrors PotentialFieldMapper's update(point_cloud, state) / draw(axes, elems) interface, so no changes were needed to shared vehicle/visualizer code.
  • vfh_polar_histogram_construction.py: simulation entry point, reusing the same obstacle scenario as the existing lidar_obstacle_sensing sample so the histogram's behavior can be compared directly against the raw point cloud it's built from.
  • Unit tests for histogram binning/smoothing math, the mapper, and the simulation entry point (15 tests total).
  • Regenerated pyrightconfig.json / devcontainer.json extraPaths via generate_pyrightconfig.py for the two new module directories.

Valley/direction selection is intentionally left for Step 2 per the agreed roadmap, to keep this PR small and reviewable.

How it was verified: all 15 new tests pass via pytest (matching the project's existing show_plot=False test convention), and the full simulation runs cleanly end to end.

Implements the first incremental step of the Vector Field Histogram (VFH)
roadmap agreed with the maintainer in issue ShisatoYano#52: build a 1D polar obstacle
density histogram around the vehicle directly from the existing 2D LiDAR
sensor simulation, with configurable sector resolution and smoothing.

- PolarHistogram (src/components/mapping/polar_histogram/polar_histogram.py):
  pure-logic histogram construction. Bins LiDAR returns into angular sectors,
  weights each by the classic VFH linear magnitude falloff
  (Borenstein & Koren, 1991), and applies a triangular smoothing filter
  across neighboring sectors.
- PolarHistogramMapper (polar_histogram_mapper.py): wires PolarHistogram to
  the existing OmniDirectionalLidar point cloud and renders it as a ring of
  colored wedges around the vehicle (denser sectors longer/redder), using
  the project's existing mapper slot on FourWheelsVehicle - mirrors
  PotentialFieldMapper's update(point_cloud, state) / draw(axes, elems)
  interface, so no changes were needed to shared vehicle/visualizer code.
- vfh_polar_histogram_construction.py: simulation entry point, reusing the
  same obstacle scenario as the existing lidar_obstacle_sensing sample so
  the histogram's behavior can be compared directly against the raw point
  cloud it's built from.
- Unit tests for histogram binning/smoothing math, the mapper, and the
  simulation entry point (15 tests total).
- Regenerated pyrightconfig.json / devcontainer.json extraPaths via
  generate_pyrightconfig.py for the two new module directories.

Valley/direction selection is intentionally left for Step 2 per the agreed
roadmap, to keep this PR small and reviewable.

How it was verified: all 15 new tests pass via pytest (matching the
project's existing show_plot=False test convention), and the full
simulation runs cleanly end to end.
@ShisatoYano

Copy link
Copy Markdown
Owner

@Khushi0512
Thank you for the clean first step! The structure (pure-logic PolarHistogram + PolarHistogramMapper reusing the
existing mapper slot) is easy to follow, and all 15 tests pass locally.

A few requests before merging:

  1. Drawing performance: The simulation takes ~76s vs ~32s for lidar_obstacle_sensing with the same scenario.
    Profiling shows about half of the time is in PolarHistogramMapper.draw(), because each wedge is added with
    axes.add_patch() (~42 calls per frame). Collecting the wedges into a single matplotlib.collections.PatchCollection
    and calling axes.add_collection() once per frame reduced it to ~37s in my local test. Since later steps extend this
    draw(), it would be great to fix it here.
  2. Demo GIF: Could you add a demo GIF under src/simulations/mapping/vfh_polar_histogram_construction/? It will be
    listed in doc/EXAMPLES.md automatically after merge.
  3. Histogram origin (suggestion): Distances/angles are measured from the LiDAR position (1.0 m ahead of the vehicle
    origin), but the wedges are drawn around the vehicle origin. For close obstacles this shifts the direction noticeably
    (e.g. ~19 deg for an obstacle 3 m to the side). Centering the drawing at the LiDAR's global position, or noting this in
    the docstring, would help since Steps 3/4 select directions from this histogram.
  4. Minor: The magnitude docstring says "same as the original VFH paper", but it assumes the certainty value c = 1
    (the paper uses c^2 * (a - b*d)). Mentioning that would help readers.

… accuracy)

1. Drawing performance: PolarHistogramMapper.draw() was calling
   axes.add_patch() once per sector wedge (~42 calls/frame in this
   scenario). Collects all of a frame's wedges into a single
   matplotlib.collections.PatchCollection and adds it with one
   axes.add_collection() call instead (match_original=True keeps each
   wedge's own color/alpha). Confirmed on this machine using the exact
   same test/measurement this repo already uses for the simulation
   (test_vfh_polar_histogram_construction.py, which runs draw() 1000
   times without opening a plot window): 98.79s before this fix, 51.72s
   after - a ~48% reduction, consistent with the ~51% reduction (76s to
   37s) reported in review. Later steps' own draw() additions
   (candidate valleys, direction selection) are untouched here since
   they add far fewer patches per frame and weren't part of this
   review.

2. Histogram origin: this is a drawing-only approximation, not a data
   or logic bug - each measurement's angle/distance is computed from
   the LiDAR's actual mounted position (OmniDirectionalLidar), so the
   histogram density itself, and everything later steps derive from it
   (valleys, selected direction), is already geometrically accurate.
   Only the ring of wedges is drawn centered on the vehicle's origin
   rather than the LiDAR's position, for simplicity. Took the
   documented-approximation option offered in review rather than
   changing where this (and, to stay visually consistent, later
   steps' valley/direction drawings) are centered, since that would be
   a larger change across all 4 stacked branches for a cosmetic-only
   difference. Added a docstring note on PolarHistogramMapper
   explaining this and roughly quantifying it (up to ~20deg for very
   close obstacles, matching the reviewer's own example).

3. Magnitude formula docstring: clarified that PolarHistogram's
   magnitude equation follows the original VFH paper with its
   certainty value fixed at 1, rather than implying an exact match to
   the paper's general certainty^2 * (a - b*d) form.

Demo GIF for doc/EXAMPLES.md (also requested in review) is a separate
follow-up commit, since it's a binary asset rather than a code change.

Verified: all 14 existing tests for this step (test_polar_histogram.py,
test_polar_histogram_mapper.py) still pass unmodified, confirming the
draw() refactor didn't change any observable behavior these tests
check. test_vfh_polar_histogram_construction.py also still passes.
@Khushi0512

Copy link
Copy Markdown
Contributor Author

Hi @ShisatoYano , thanks for the detailed review! I've addressed all four points on Step 1 and merged the fixes forward through Steps 2–4 (performance batching via PatchCollection, plus both docstring notes).

I've also added the demo GIF for all four steps (1, 2, 3, and 4) on their respective branches/PRs.

Separately, 3 more steps remain in this VFH roadmap (dynamic speed control, VFH+ trajectory verification, and performance benchmarking). I'll implement, test, and upload each one once it's ready locally.

@ShisatoYano

Copy link
Copy Markdown
Owner

Thank you for addressing all the points so quickly!

  • Performance: confirmed locally, the simulation now runs in ~29s (was ~76s), on par with lidar_obstacle_sensing.
  • Demo GIF: looks good, and it will be added to doc/EXAMPLES.md automatically after merge.
  • Histogram origin: agreed, since the density data is LiDAR-centered and later steps use only the direction angle, documenting it is the right trade-off.
  • Docstring: thanks for clarifying the certainty term.

I'll approve and merge this once CI passes. Then I'll continue with #81.

@ShisatoYano
ShisatoYano merged commit 97d0b89 into ShisatoYano:main Sep 28, 2026
3 checks passed
Khushi0512 added a commit to Khushi0512/AutonomousVehicleControlBeginnersGuide that referenced this pull request Oct 3, 2026
Per maintainer feedback on PR ShisatoYano#80: doc/EXAMPLES.md is now generated from
each simulation script's docstring, which must be exactly the filename
followed by one line each of Title/Description/Author (HOWTOCONTRIBUTE.md
Step 2, item 6). Updates this step's docstring to match.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants