ZLayout EDA Library v1.0.0
Advanced Electronic Design Automation Layout Library with Bilingual Documentation
Loading...
Searching...
No Matches
bench_geometry.cpp
Go to the documentation of this file.
1
5
6#define _USE_MATH_DEFINES
7#include <benchmark/benchmark.h>
11#include <random>
12#include <vector>
13#include <cmath>
14
15// Fallback for M_PI if not defined (MSVC 2012 compatibility)
16#ifndef M_PI
17#define M_PI 3.14159265358979323846
18#endif
19
20using namespace zlayout::geometry;
21
22// Benchmark polygon area calculation
23static void BM_PolygonArea(benchmark::State& state) {
24 std::vector<Point> vertices;
25 for (int i = 0; i < state.range(0); ++i) {
26 double angle = 2.0 * M_PI * i / state.range(0);
27 vertices.emplace_back(100 * cos(angle), 100 * sin(angle));
28 }
29
30 Polygon poly(vertices);
31
32 for (auto _ : state) {
33 double area = poly.area();
34 benchmark::DoNotOptimize(area);
35 }
36
37 state.SetComplexityN(state.range(0));
38}
39BENCHMARK(BM_PolygonArea)->Range(8, 8<<8)->Complexity();
40
41// Benchmark point-in-polygon testing
42static void BM_PointInPolygon(benchmark::State& state) {
43 std::vector<Point> vertices;
44 for (int i = 0; i < 100; ++i) {
45 double angle = 2.0 * M_PI * i / 100;
46 vertices.emplace_back(100 * cos(angle), 100 * sin(angle));
47 }
48
49 Polygon poly(vertices);
50
51 std::random_device rd;
52 std::mt19937 gen(42);
53 std::uniform_real_distribution<> dis(-150.0, 150.0);
54
55 for (auto _ : state) {
56 Point test_point(dis(gen), dis(gen));
57 bool contains = poly.contains_point(test_point);
58 benchmark::DoNotOptimize(contains);
59 }
60}
62
63// Benchmark sharp angle detection
64static void BM_SharpAngleDetection(benchmark::State& state) {
65 std::vector<Point> vertices;
66 std::random_device rd;
67 std::mt19937 gen(42);
68 std::uniform_real_distribution<> dis(0.0, 1000.0);
69
70 for (int i = 0; i < state.range(0); ++i) {
71 vertices.emplace_back(dis(gen), dis(gen));
72 }
73
74 Polygon poly(vertices);
75
76 for (auto _ : state) {
77 auto sharp_angles = poly.get_sharp_angles(30.0);
78 benchmark::DoNotOptimize(sharp_angles);
79 }
80
81 state.SetComplexityN(state.range(0));
82}
83BENCHMARK(BM_SharpAngleDetection)->Range(8, 8<<6)->Complexity();
84
85// Benchmark polygon distance calculation
86static void BM_PolygonDistance(benchmark::State& state) {
87 std::vector<Point> vertices1 = {
88 Point(0, 0), Point(100, 0), Point(100, 100), Point(0, 100)
89 };
90 std::vector<Point> vertices2 = {
91 Point(150, 50), Point(250, 50), Point(250, 150), Point(150, 150)
92 };
93
94 Polygon poly1(vertices1);
95 Polygon poly2(vertices2);
96
97 for (auto _ : state) {
98 double distance = poly1.distance_to(poly2);
99 benchmark::DoNotOptimize(distance);
100 }
101}
103
BENCHMARK_MAIN()
static void BM_PointInPolygon(benchmark::State &state)
static void BM_PolygonDistance(benchmark::State &state)
BENCHMARK(BM_PolygonArea) -> Range(8, 8<< 8) ->Complexity()
static void BM_SharpAngleDetection(benchmark::State &state)
static void BM_PolygonArea(benchmark::State &state)
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
double distance_to(const Polygon &other) const
Calculate minimum distance to another polygon.
Definition polygon.cpp:219
std::vector< size_t > get_sharp_angles(double threshold_degrees=30.0) const
Find vertices with sharp angles.
Definition polygon.cpp:175
bool contains_point(const Point &point) const
Check if point is inside polygon (ray casting algorithm)
Definition polygon.cpp:146
double area() const
Calculate polygon area using shoelace formula.
Definition polygon.cpp:47
double distance(const Point &p1, const Point &p2)
Calculate distance between two points.
Definition point.cpp:161
2D Point class for geometric calculations
#define M_PI
Definition polygon.cpp:15
Polygon class for complex geometric shapes and EDA components.
Axis-aligned rectangle class for bounding boxes and simple components.