ZLayout EDA Library v1.0.0
Advanced Electronic Design Automation Layout Library with Bilingual Documentation
Loading...
Searching...
No Matches
minimal_example.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""
3Minimal ZLayout example without external dependencies.
4
5This example demonstrates core functionality without requiring
6numpy or matplotlib.
7"""
8
9import sys
10import os
11
12# Add parent directory to path
13sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
14
15import zlayout
16
17
19 """Test basic geometry operations."""
20
21 print("=== Basic Geometry Tests ===")
22
23 # Create points
24 p1 = zlayout.Point(0, 0)
25 p2 = zlayout.Point(3, 4)
26 p3 = zlayout.Point(1, 1)
27
28 print(f"Point 1: {p1}")
29 print(f"Point 2: {p2}")
30 print(f"Distance p1 to p2: {p1.distance_to(p2):.2f}")
31
32 # Create rectangles
33 rect1 = zlayout.Rectangle(0, 0, 10, 5)
34 rect2 = zlayout.Rectangle(5, 2, 8, 6)
35
36 print(f"\nRectangle 1: {rect1}")
37 print(f"Rectangle 2: {rect2}")
38 print(f"Rectangles intersect: {rect1.intersects(rect2)}")
39 print(f"Point {p3} in rect1: {rect1.contains_point(p3)}")
40
41 # Create polygon
42 triangle = zlayout.Polygon([
43 zlayout.Point(0, 0),
44 zlayout.Point(4, 0),
45 zlayout.Point(2, 3)
46 ])
47
48 print(f"\nTriangle: {triangle}")
49 print(f"Triangle area: {triangle.area():.2f}")
50 print(f"Triangle is convex: {triangle.is_convex()}")
51
52 return rect1, rect2, triangle
53
54
56 """Test quadtree spatial indexing."""
57
58 print("\n=== Spatial Indexing Tests ===")
59
60 # Create quadtree
61 world_bounds = zlayout.Rectangle(0, 0, 100, 100)
62 quadtree = zlayout.QuadTree(world_bounds, capacity=2)
63
64 # Add objects
65 objects = [
66 zlayout.Rectangle(10, 10, 5, 5),
67 zlayout.Rectangle(20, 20, 8, 6),
68 zlayout.Rectangle(50, 50, 12, 8),
69 zlayout.Rectangle(75, 25, 6, 10),
70 ]
71
72 print(f"Adding {len(objects)} objects to quadtree...")
73 for i, obj in enumerate(objects):
74 success = quadtree.insert(obj)
75 print(f" Object {i+1}: {'✓' if success else '✗'}")
76
77 print(f"QuadTree size: {quadtree.size()}")
78
79 # Test queries
80 query_region = zlayout.Rectangle(0, 0, 30, 30)
81 found_objects = quadtree.query_range(query_region)
82 print(f"Objects in region (0,0,30,30): {len(found_objects)}")
83
84 query_point = zlayout.Point(12, 12)
85 containing_objects = quadtree.query_point(query_point)
86 print(f"Objects containing point (12,12): {len(containing_objects)}")
87
88 return quadtree
89
90
92 """Test layout analysis without visualization."""
93
94 print("\n=== Layout Analysis Tests ===")
95
96 # Create processor
97 world_bounds = zlayout.Rectangle(0, 0, 100, 100)
98 processor = zlayout.GeometryProcessor(world_bounds)
99
100 # Add components
101 components = [
102 # Regular rectangles
103 zlayout.Rectangle(10, 10, 15, 8),
104 zlayout.Rectangle(30, 15, 12, 6),
105 zlayout.Rectangle(50, 25, 20, 10),
106
107 # Triangles with potential sharp angles
109 zlayout.Point(70, 10),
110 zlayout.Point(85, 12),
111 zlayout.Point(72, 20) # Sharp angle
112 ]),
113
114 # Close components (narrow spacing)
115 zlayout.Rectangle(15, 35, 5, 3),
116 zlayout.Rectangle(21, 35, 5, 3), # Only 1 unit apart
117
118 # Potentially overlapping
120 zlayout.Point(40, 50),
121 zlayout.Point(55, 52),
122 zlayout.Point(53, 65),
123 zlayout.Point(38, 63)
124 ]),
126 zlayout.Point(50, 55),
127 zlayout.Point(65, 57),
128 zlayout.Point(63, 70),
129 zlayout.Point(48, 68)
130 ])
131 ]
132
133 for comp in components:
134 processor.add_component(comp)
135
136 # Run analysis
137 print("Running layout analysis...")
138 results = processor.analyze_layout(
139 sharp_angle_threshold=45.0,
140 narrow_distance_threshold=2.0
141 )
142
143 # Print results
144 print(f"Results structure: {results.keys()}")
145 analysis = results
146
147 print(f"\nAnalysis Results:")
148 print(f" Sharp angles found: {analysis['sharp_angles']['count']}")
149 if analysis['sharp_angles']['count'] > 0:
150 print(f" Sharpest: {analysis['sharp_angles']['sharpest']:.1f}°")
151
152 print(f" Narrow regions found: {analysis['narrow_distances']['count']}")
153 if analysis['narrow_distances']['count'] > 0:
154 print(f" Minimum distance: {analysis['narrow_distances']['minimum']:.3f}")
155
156 print(f" Intersecting polygon pairs: {analysis['intersections']['polygon_pairs']}")
157
158 # Get optimization suggestions
159 optimization = processor.optimize_layout()
160 print(f"\nOptimization Score: {optimization['optimization_score']:.1f}/100")
161
162 if optimization['suggestions']:
163 print("Suggestions:")
164 for suggestion in optimization['suggestions']:
165 print(f" • {suggestion}")
166 else:
167 print("✅ No issues found!")
168
169 return processor, optimization
170
171
172def _print_design_rule_results(process_name, violations, analysis):
173 """Helper function to print design rule checking results without conditionals in main test."""
174 # Pass case
175 if violations == 0:
176 print(f" ✅ Passes {process_name} constraints")
177 return
178
179 # Violation case
180 print(f" ❌ {violations} violations for {process_name}")
181 print(f" Sharp angles: {analysis['sharp_angles']['count']}")
182 print(f" Spacing issues: {analysis['narrow_distances']['count']}")
183
184
186 """Test prototype manufacturing process constraints."""
187 print("\n--- Prototype Process ---")
188 analysis = processor.analyze_layout(sharp_angle_threshold=20, narrow_distance_threshold=0.5)
189 violations = (analysis['sharp_angles']['count'] + analysis['narrow_distances']['count'] +
190 analysis['intersections']['polygon_pairs'])
191 _print_design_rule_results("Prototype", violations, analysis)
192
193
195 """Test standard manufacturing process constraints."""
196 print("\n--- Standard Process ---")
197 analysis = processor.analyze_layout(sharp_angle_threshold=30, narrow_distance_threshold=1.5)
198 violations = (analysis['sharp_angles']['count'] + analysis['narrow_distances']['count'] +
199 analysis['intersections']['polygon_pairs'])
200 _print_design_rule_results("Standard", violations, analysis)
201
202
204 """Test high-precision manufacturing process constraints."""
205 print("\n--- High-precision Process ---")
206 analysis = processor.analyze_layout(sharp_angle_threshold=45, narrow_distance_threshold=2.5)
207 violations = (analysis['sharp_angles']['count'] + analysis['narrow_distances']['count'] +
208 analysis['intersections']['polygon_pairs'])
209 _print_design_rule_results("High-precision", violations, analysis)
210
211
213 """Test design rule checking for different manufacturing processes."""
214
215 print("\n=== Design Rule Checking ===")
216
217 # Create test layout
218 world_bounds = zlayout.Rectangle(0, 0, 50, 50)
219 processor = zlayout.GeometryProcessor(world_bounds)
220
221 # Add components with known issues
222 processor.add_component(zlayout.Rectangle(5, 5, 10, 8)) # Normal component
223 processor.add_component(zlayout.Rectangle(16, 5, 10, 8)) # Close spacing (1 unit)
224
225 # Sharp angle component
226 sharp_poly = zlayout.Polygon([
227 zlayout.Point(10, 20),
228 zlayout.Point(25, 21),
229 zlayout.Point(12, 25) # Very sharp angle
230 ])
231 processor.add_component(sharp_poly)
232
233 # Test different manufacturing constraints
234 _test_prototype_process(processor)
235 _test_standard_process(processor)
237
238
239def main():
240 """Run all minimal tests."""
241
242 print("ZLayout - Minimal Functionality Test")
243 print("=" * 40)
244
245 try:
246 # Test basic geometry
247 rect1, rect2, triangle = test_basic_geometry()
248
249 # Test spatial indexing
250 quadtree = test_spatial_indexing()
251
252 # Test layout analysis
253 processor, optimization = test_layout_analysis()
254
255 # Test design rules
257
258 print("\n" + "=" * 40)
259 print("✅ All tests completed successfully!")
260 print("\nNote: For visualization features, install matplotlib:")
261 print(" pip install matplotlib")
262
263 except Exception as e:
264 print(f"\n❌ Test failed with error: {e}")
265 import traceback
266 traceback.print_exc()
267 return 1
268
269 return 0
270
271
272if __name__ == "__main__":
273 sys.exit(main())
2D point with high-precision coordinates and utility methods
Definition point.hpp:23
Polygon class supporting both convex and concave polygons.
Definition polygon.hpp:25
Axis-aligned rectangle for bounding boxes and simple EDA components.
Definition rectangle.hpp:26
Quadtree spatial index for efficient range and intersection queries.
Definition quadtree.hpp:120
_test_standard_process(processor)
_print_design_rule_results(process_name, violations, analysis)
_test_high_precision_process(processor)
_test_prototype_process(processor)