| # AnalyticSDF |
|
|
| **Automatic Diagram Generation for Analytic Geometry Problems using Signed Distance Fields** |
|
|
| <p align="center"> |
| <img src="results/test/ellipse/problem_2063.png" width="600" alt="Example Output"> |
| </p> |
|
|
| <p align="center"> |
| <img src="https://img.shields.io/badge/Python-3.9%2B-blue.svg" alt="Python"> |
| <img src="https://img.shields.io/badge/PyTorch-2.0%2B-ee4c2c.svg" alt="PyTorch"> |
| <img src="https://img.shields.io/badge/Accuracy-96.7%25-brightgreen.svg" alt="Accuracy"> |
| <img src="https://img.shields.io/badge/Problems-10,861-orange.svg" alt="Problems"> |
| <img src="https://img.shields.io/badge/License-MIT-green.svg" alt="License"> |
| </p> |
|
|
| --- |
|
|
| ## Introduction |
|
|
| **AnalyticSDF** is an automatic system that converts mathematical geometry problem text into accurate visualizations. By leveraging **Signed Distance Fields (SDF)** combined with differentiable optimization, the system can automatically generate precise conic section diagrams from symbolic geometric expressions. |
|
|
| Traditional geometry diagram generation often relies on manual drawing or rule-based methods. This system takes a different approach: it models geometric constraints as differentiable loss functions and uses gradient-based optimization to find curve parameters that satisfy all constraints, ultimately rendering high-quality geometric diagrams. |
|
|
| ### Key Features |
|
|
| - **Fully Automated**: Generate PNG diagrams directly from JSON-formatted geometry problems without manual intervention |
| - **High Accuracy**: Achieves **96.7%** validation pass rate on parseable problems |
| - **Large-Scale Validation**: Tested on **10,861** geometry problems across 3 datasets |
| - **Rigorous Verification**: Multi-dimensional geometric property validation including focus position, eccentricity, and asymptote slope |
|
|
| --- |
|
|
| ## Supported Curve Types |
|
|
| The system supports four classical conic sections, each with complete SDF representation and constraint validation: |
|
|
| | Curve Type | Standard Form | Validated Properties | |
| |------------|---------------|---------------------| |
| | **Ellipse** | x²/a² + y²/b² = 1 | Focus distance c²=a²-b², eccentricity, points on curve | |
| | **Hyperbola** | x²/a² - y²/b² = 1 | Focus distance c²=a²+b², asymptote slope, eccentricity | |
| | **Parabola** | y² = 4px | Focus position, directrix equation, points on curve | |
| | **Circle** | (x-h)² + (y-k)² = r² | Center position, radius, points on curve | |
|
|
| --- |
|
|
| ## How It Works |
|
|
| The system follows a four-stage pipeline: |
|
|
| ``` |
| Text Parsing → SDF Construction → Constraint Optimization → Diagram Rendering |
| ``` |
|
|
| ### 1. Text Parsing |
|
|
| Extract geometric parameters from mathematical expressions. For example: |
|
|
| ``` |
| Input: "ellipse x²/4 + y²/9 = 1" |
| Output: Ellipse(a=2, b=3, center=(0,0)) |
| ``` |
|
|
| The parser supports various expression formats including fractions, radicals, and LaTeX-formatted mathematical expressions. |
|
|
| ### 2. SDF Construction |
|
|
| Build a signed distance field function for each curve type. The SDF value represents the signed distance from any point in space to the curve boundary: |
| - Positive: point is outside the curve |
| - Negative: point is inside the curve |
| - Zero: point is exactly on the curve |
|
|
| ### 3. Constraint Optimization |
|
|
| Optimize curve parameters via gradient descent to satisfy all geometric constraints: |
|
|
| ```python |
| # Total loss = point constraint + focus constraint + eccentricity constraint + crowd penalty |
| L_total = λ₁·L_point + λ₂·L_focus + λ₃·L_ecc + λ₄·L_crowd |
| ``` |
|
|
| Using the AdamW optimizer, convergence typically occurs within 100-200 iterations. |
|
|
| ### 4. Diagram Rendering |
|
|
| Extract the zero-level set of the SDF (i.e., the curve itself) and render in Chinese Gaokao (高考) analytic geometry style: white background, black curves, arrow axes through origin with O/x/y labels, feature point annotations (foci, vertices), and auxiliary elements (asymptotes, directrix) as dashed lines. No problem text, grid, or legend is included in the output. |
|
|
| --- |
|
|
| ## Datasets and Results |
|
|
| The system has been fully tested on three datasets: |
|
|
| | Dataset | Total Problems | Parseable | Successful | Parseable Accuracy | |
| |---------|----------------|-----------|------------|-------------------| |
| | dev | 1,035 | 809 | 788 | **97.4%** | |
| | test | 2,069 | 1,649 | 1,596 | **96.8%** | |
| | train | 7,757 | 6,052 | 5,849 | **96.6%** | |
| | **Total** | **10,861** | **8,510** | **8,233** | **96.7%** | |
|
|
| > **Note**: "Parseable" refers to problems with explicit geometric expressions (e.g., x²/4 + y²/9 = 1) rather than implicit or parametric forms. Approximately 22% of problems cannot be parsed due to overly complex or incomplete expressions. |
|
|
| ### Curve Type Distribution |
|
|
| | Type | Dev | Test | Train | Total | |
| |------|-----|------|-------|-------| |
| | Ellipse | 220 | 467 | 1,734 | 2,421 | |
| | Hyperbola | 293 | 565 | 2,025 | 2,883 | |
| | Parabola | 262 | 532 | 2,000 | 2,794 | |
| | Circle | 13 | 32 | 90 | 135 | |
|
|
| --- |
|
|
| ## Installation and Usage |
|
|
| ### Requirements |
|
|
| - Python >= 3.9 |
| - PyTorch >= 2.0.0 |
| - NumPy >= 1.24.0 |
| - Matplotlib >= 3.7.0 |
| - tqdm >= 4.65.0 |
|
|
| ### Install Dependencies |
|
|
| ```bash |
| pip install -r requirements.txt |
| ``` |
|
|
| ### Running Examples |
|
|
| ```bash |
| # Process test dataset |
| python src/main.py --input data/test_en.json --output results/test/ |
| |
| # Process dev dataset |
| python src/main.py --input data/dev_en.json --output results/dev/ |
| |
| # Process train dataset (~30 minutes) |
| python src/main.py --input data/train_en.json --output results/train/ |
| |
| # Process only first 100 problems with verbose output |
| python src/main.py -i data/test_en.json -o results/test/ -m 100 -v |
| ``` |
|
|
| ### Command Line Arguments |
|
|
| | Argument | Description | Default | |
| |----------|-------------|---------| |
| | `-i, --input` | Input JSON file path | `data/test_en.json` | |
| | `-o, --output` | Output directory | `results/` | |
| | `-m, --max` | Maximum number of problems to process | All | |
| | `-v, --verbose` | Enable verbose output | False | |
|
|
| ### Python API |
|
|
| ```python |
| from src.sdf_geo import SDFBatchProcessor |
| |
| # Create processor |
| processor = SDFBatchProcessor(output_dir='results/') |
| |
| # Batch processing |
| results = processor.process_batch('data/test_en.json', max_problems=100) |
| |
| # Calculate success rate |
| success_rate = sum(r['success'] for r in results) / len(results) |
| print(f"Success rate: {success_rate:.1%}") |
| ``` |
|
|
| --- |
|
|
| ## Project Structure |
|
|
| ``` |
| Image_SDF/ |
| ├── data/ |
| │ ├── dev_en.json # Dev set (1,035 problems) |
| │ ├── test_en.json # Test set (2,069 problems) |
| │ └── train_en.json # Train set (7,757 problems) |
| ├── results/ |
| │ ├── dev/ # Dev set results |
| │ ├── test/ # Test set results |
| │ └── train/ # Train set results |
| ├── samples/ # Sample images (3 per curve type per dataset) |
| ├── src/ |
| │ ├── main.py # CLI entry point |
| │ └── sdf_geo/ # Core modules |
| │ ├── primitives.py # SDF geometric primitives |
| │ ├── constraints.py # Differentiable constraint functions |
| │ ├── parser.py # Expression parser |
| │ ├── optimizer.py # Gradient optimizer |
| │ ├── processor.py # Batch processing, validation & visualization |
| │ └── renderer.py # SDF field evaluation & curve rendering |
| ├── create_samples.py # Generate sample images from results |
| ├── requirements.txt |
| └── README.md |
| ``` |
|
|
| --- |
|
|
| ## Output Format |
|
|
| Each problem generates a PNG image in Chinese Gaokao (高考) analytic geometry style, containing: |
|
|
| - **Geometric Curve**: Black curve rendered from the SDF zero-level set on white background |
| - **Feature Point Annotations**: Foci (F₁, F₂), vertices, center, marked as small black dots with labels |
| - **Coordinate System**: Arrow axes through origin with O, x, y labels (no grid) |
| - **Auxiliary Elements**: Asymptotes and directrix rendered as dashed lines |
|
|
| Results are organized by curve type: |
|
|
| ``` |
| results/test/ |
| ├── summary.json # Statistics summary |
| ├── circle/ # Circle diagrams |
| ├── ellipse/ # Ellipse diagrams |
| ├── hyperbola/ # Hyperbola diagrams |
| └── parabola/ # Parabola diagrams |
| ``` |
|
|
| --- |
|
|
| ## Validation Standards |
|
|
| The system employs multi-dimensional geometric property validation to ensure mathematical correctness of generated diagrams: |
|
|
| | Validation Item | Tolerance | Description | |
| |-----------------|-----------|-------------| |
| | Point on curve | 0.03 | SDF(p) ≈ 0 | |
| | Eccentricity | 0.05 | \|e_calculated - e_target\| < tol | |
| | Focus position | 0.05 | \|c_calculated - c_given\| < tol | |
| | Asymptote slope | 0.05 | \|b/a - slope\| < tol | |
|
|
| --- |
|
|
| ## Methodology |
|
|
| This implementation is based on the methodology described in: |
|
|
| > **GeoSDF: Plane Geometry Diagram Synthesis via Signed Distance Field** |
|
|
| The core ideas of using signed distance fields for geometric representation and differentiable optimization for constraint satisfaction are derived from this work. |
|
|
| --- |
|
|
| ## License |
|
|
| MIT License |
|
|