ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
Visualization.hpp File Reference

Tree-analysis visitors and geometric utilities for rendering projection-pursuit tree decision boundaries and structure diagrams. More...

#include "models/TreeCondition.hpp"
#include "models/TreeResponse.hpp"
#include "utils/Types.hpp"
#include <vector>
#include <utility>
#include <string>

Go to the source code of this file.

Classes

struct  ppforest2::viz::BoundarySegment
 A clipped decision boundary line segment in 2D feature space. More...
 
struct  ppforest2::viz::BoundaryVisitor
 Visitor that collects and clips decision boundary line segments. More...
 
struct  ppforest2::viz::HalfSpace
 A half-space constraint derived from an ancestor split. More...
 
struct  ppforest2::viz::LayoutEdge
 An edge between two positioned nodes with a threshold label. More...
 
struct  ppforest2::viz::LayoutNode
 A positioned tree node in the computed layout. More...
 
struct  ppforest2::viz::LayoutParams
 Layout parameters for tree structure rendering. More...
 
struct  ppforest2::viz::NodeData
 Per-node data collected by routing observations through the tree. More...
 
struct  ppforest2::viz::NodeDataVisitor
 Visitor that routes observations through the tree and collects per-node projection data for histogram rendering. More...
 
struct  ppforest2::viz::RegionPolygon
 A convex decision region polygon in 2D with its predicted group. More...
 
struct  ppforest2::viz::RegionVisitor
 Visitor that collects convex decision region polygons. More...
 
struct  ppforest2::viz::TreeLayout
 Complete tree layout: positioned nodes and labelled edges. More...
 

Namespaces

namespace  ppforest2
 
namespace  ppforest2::viz
 

Typedefs

using ppforest2::viz::Polygon = std::vector<std::pair<types::Feature, types::Feature>>
 A polygon represented as an ordered list of (x, y) vertex pairs.
 

Functions

types::Feature ppforest2::viz::adjust_threshold (types::FeatureVector const &full_proj, types::Feature thr, std::vector< std::pair< int, types::Feature > > const &fixed_vars)
 Adjust a split threshold by subtracting contributions of fixed variables.
 
bool ppforest2::viz::clip_boundary_2d (types::FeatureVector const &a, types::Feature threshold, std::vector< HalfSpace > const &constraints, types::Feature x_min, types::Feature x_max, types::Feature y_min, types::Feature y_max, BoundarySegment &segment, int depth)
 Clip a 2D decision boundary line to the visible rectangle and all ancestor half-space constraints.
 
bool ppforest2::viz::clip_param_to_range (types::Feature origin, types::Feature direction, types::Feature range_min, types::Feature range_max, types::Feature &u_min, types::Feature &u_max)
 Clip a parametric interval [u_min, u_max] to a 1D range.
 
Polygon ppforest2::viz::clip_polygon_halfspace (Polygon const &polygon, types::FeatureVector const &normal, types::Feature threshold, bool is_lower)
 Clip a convex polygon against a single half-space.
 
TreeLayout ppforest2::viz::compute_tree_layout (TreeNode const &root, LayoutParams const &params=LayoutParams())
 Compute a left-aligned tree layout for rendering.
 
types::FeatureVector ppforest2::viz::project_2d (types::FeatureVector const &full_proj, int var_i, int var_j)
 Extract a 2D sub-projection from a full p-dimensional projector.
 

Detailed Description

Tree-analysis visitors and geometric utilities for rendering projection-pursuit tree decision boundaries and structure diagrams.

Architecture

C++ (this module) handles geometry and tree traversal:

  • NodeDataVisitor — Routes observations through the tree, collects per-node projected values and group labels for histogram rendering in the tree structure diagram.
  • BoundaryVisitor — Projects each split's decision boundary line into a 2D feature plane and clips it to the visible bounding box and all ancestor half-space constraints using parametric line clipping.
  • RegionVisitor — Computes convex decision region polygons for each leaf via Sutherland–Hodgman polygon clipping against ancestor half-spaces. Produces one polygon per reachable leaf.
  • compute_tree_layout — Positions tree nodes on a 2D canvas with a recursive left-aligned layout algorithm.

R (plot.R) handles rendering via ggplot2: Translates visitor output into ggplot2 layers (geom_polygon, geom_segment, geom_rect) and assembles composite layouts (mosaic, pairwise facets, tree diagram).

2D Projection

When the feature space has p > 2 variables, the visualization selects two variables (var_i, var_j) for the display axes and holds the remaining p−2 variables at fixed values (typically medians). Each split's p-dimensional projector and threshold are reduced to 2D via project_2d() and adjust_threshold(), preserving the boundary geometry in the chosen 2D slice.

Visitor Protocol

All visitors inherit from TreeNode::Visitor and implement visit() for TreeCondition (internal) and TreeResponse (leaf) nodes. Traversal is initiated by calling tree.root->accept(visitor). Results accumulate in public member vectors (nodes, segments, regions) that the R layer reads via Rcpp exports in main.cpp.

Thread Safety

Visitors are single-use, single-threaded. Create a fresh visitor for each traversal. Multiple visitors may run concurrently on the same (immutable) tree.