Spaces:
Sleeping
Sleeping
File size: 6,594 Bytes
e0ef700 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
#!/usr/bin/env python3
"""
Test geometric realization from Rivin LP angles.
With the corrected triangle extraction bug fixed, we should be able to:
1. Check realizability and get angles from the LP
2. Reconstruct point positions from those angles
3. Verify the reconstructed triangulation matches the original
"""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
import numpy as np
from ideal_poly_volume_toolkit.plantri_interface import find_plantri_executable
from ideal_poly_volume_toolkit.planar_utils import extract_faces_from_planar_embedding
from ideal_poly_volume_toolkit.rivin_delaunay import (
check_delaunay_realizability,
realize_angles_as_points
)
import subprocess
def get_nth_triangulation(n_vertices: int, index: int, min_connectivity: int = 3):
"""Get the nth triangulation for given vertex count."""
plantri = find_plantri_executable()
args = [plantri, f'-pc{min_connectivity}', '-a', str(n_vertices)]
result = subprocess.run(args, capture_output=True, text=True)
triangulations = []
for line in result.stdout.split('\n'):
line = line.strip()
if not line or line.startswith('>'):
continue
parts = line.split(maxsplit=1)
if len(parts) != 2:
continue
n = int(parts[0])
adj_str = parts[1]
# Build adjacency dict
adj = {}
for v_idx, neighbor_str in enumerate(adj_str.split(',')):
neighbors = [ord(c) - ord('a') for c in neighbor_str]
adj[v_idx] = neighbors
# Extract faces using CORRECTED method
closed_tri = extract_faces_from_planar_embedding(n, adj)
# Remove vertex 0 to get planar
planar_tri = [tri for tri in closed_tri if 0 not in tri]
if planar_tri:
triangulations.append(planar_tri)
if index < len(triangulations):
return triangulations[index]
else:
return None
def test_octahedron():
"""Test on the octahedron (n=6, the unique strictly realizable case)."""
print("="*70)
print("TEST: Octahedron Geometric Realization")
print("="*70)
# Get n=6 triangulations
print("\nLoading n=6 triangulations...")
triangulations = []
for i in range(7): # We know there are 7 of them
tri = get_nth_triangulation(6, i, min_connectivity=3)
if tri:
triangulations.append((i, tri))
print(f"Found {len(triangulations)} triangulations")
# Test each one, looking for the octahedron
for idx, triangles in triangulations:
print(f"\n{'='*70}")
print(f"Testing triangulation #{idx}")
print(f"{'='*70}")
print(f"Triangles: {triangles}")
# Check strict realizability
result = check_delaunay_realizability(triangles, verbose=False, strict=True)
if not result['realizable']:
print(f" ✗ Not strictly realizable, skipping")
continue
print(f" ✓ Strictly realizable!")
print(f" Min angle: {result.get('min_angle', 0):.6f} rad")
print(f" Max dihedral: {result.get('max_dihedral', 0):.6f} rad (π/2 = {np.pi/2:.6f})")
# Extract angles from LP solution
angles = result.get('angles')
if angles is None:
print(f" ✗ No angles in result")
continue
# Reshape angles to (n_triangles, 3)
n_triangles = len(triangles)
target_angles = angles.reshape((n_triangles, 3))
print(f"\n Reconstructing geometry from LP angles...")
print(f" Target angles shape: {target_angles.shape}")
# Realize as points
realization = realize_angles_as_points(triangles, target_angles, verbose=True)
if realization['success']:
print(f"\n ✓ Geometric realization SUCCESS!")
print(f" Angle error (RMS): {realization.get('angle_error', 0):.6e} rad")
print(f" Angle error: {realization.get('angle_error_degrees', 0):.6f}°")
print(f" Triangulation preserved: {realization.get('triangulation_preserved', False)}")
points = realization['points']
print(f"\n Point coordinates:")
vertex_list = realization['vertex_list']
for i, v in enumerate(vertex_list):
print(f" v{v}: ({points[i, 0]:8.5f}, {points[i, 1]:8.5f})")
else:
print(f"\n ✗ Geometric realization FAILED")
print(f" Message: {realization.get('message', 'Unknown error')}")
def test_simple_case(n: int = 7, index: int = 0):
"""Test on a specific triangulation."""
print("\n" + "="*70)
print(f"TEST: n={n} triangulation #{index}")
print("="*70)
triangles = get_nth_triangulation(n, index, min_connectivity=3)
if triangles is None:
print(f"Could not load triangulation")
return
print(f"\nTriangles: {triangles}")
print(f"Number of triangles: {len(triangles)}")
# Check realizability
print("\nChecking realizability (standard mode)...")
result = check_delaunay_realizability(triangles, verbose=False, strict=False)
if not result['realizable']:
print(f"✗ Not realizable")
return
print(f"✓ Realizable!")
# Extract angles
angles = result.get('angles')
n_triangles = len(triangles)
target_angles = angles.reshape((n_triangles, 3))
print(f"\nReconstructing geometry from LP angles...")
realization = realize_angles_as_points(triangles, target_angles, verbose=True)
if realization['success']:
print(f"\n✓ Geometric realization SUCCESS!")
print(f"Angle error (RMS): {realization.get('angle_error', 0):.6e} rad")
print(f"Triangulation preserved: {realization.get('triangulation_preserved', False)}")
else:
print(f"\n✗ Geometric realization FAILED")
print(f"Message: {realization.get('message', 'Unknown error')}")
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Test geometric realization from LP angles')
parser.add_argument('--test', choices=['octahedron', 'simple'], default='octahedron',
help='Which test to run')
parser.add_argument('--n', type=int, default=7, help='Number of vertices (for simple test)')
parser.add_argument('--index', type=int, default=0, help='Triangulation index (for simple test)')
args = parser.parse_args()
if args.test == 'octahedron':
test_octahedron()
else:
test_simple_case(args.n, args.index)
print("\n" + "="*70)
|