Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tikzgeom_test.cc
Go to the documentation of this file.
1#include <gtest/gtest.h>
2
3#include <cctype>
4#include <sstream>
5#include <string>
6
7#include <tikzgeom.H>
8
9using namespace Aleph;
10
11namespace
12{
13
14bool has_nan_or_inf(std::string s)
15{
16 for (char & c : s)
18
19 return s.find("nan") != std::string::npos or
20 s.find("inf") != std::string::npos;
21}
22
23} // namespace
24
26{
27 Tikz_Plane plane(120.0, 80.0, 5.0, 10.0);
28
29 EXPECT_DOUBLE_EQ(plane.get_wide(), 120.0);
30 EXPECT_DOUBLE_EQ(plane.get_height(), 80.0);
31 EXPECT_DOUBLE_EQ(plane.get_xoffset(), 5.0);
32 EXPECT_DOUBLE_EQ(plane.get_yoffset(), 10.0);
33 EXPECT_EQ(plane.size(), 0U);
34}
35
37{
38 Tikz_Plane plane(100, 60);
39
40 std::ostringstream output;
41 plane.draw(output);
42 const std::string result = output.str();
43
44 EXPECT_NE(result.find("\\begin{tikzpicture}"), std::string::npos);
45 EXPECT_NE(result.find("\\clip (-1.000000,-1.000000) rectangle (101.000000,61.000000);"),
46 std::string::npos);
47 EXPECT_NE(result.find("\\end{tikzpicture}"), std::string::npos);
49}
50
52{
53 Tikz_Plane plane(100, 60);
54 put_in_plane(plane, Point(10, 20));
55
56 std::ostringstream output;
58
59 const std::string result = output.str();
60 EXPECT_NE(result.find("\\fill"), std::string::npos);
62}
63
65{
66 {
67 Tikz_Plane plane(100, 60);
68 put_in_plane(plane, Segment(Point(10, 0), Point(10, 100))); // x-range = 0
69
70 std::ostringstream output;
72 const std::string result = output.str();
73 EXPECT_NE(result.find("\\draw"), std::string::npos);
75 }
76
77 {
78 Tikz_Plane plane(100, 60);
79 put_in_plane(plane, Segment(Point(0, 20), Point(100, 20))); // y-range = 0
80
81 std::ostringstream output;
83 const std::string result = output.str();
84 EXPECT_NE(result.find("\\draw"), std::string::npos);
86 }
87}
88
90{
91 Tikz_Plane plane(140, 90);
92
93 put_in_plane(plane, Point(0, 0));
94 put_in_plane(plane, Segment(Point(0, 0), Point(100, 50)));
95 put_in_plane(plane, Triangle(Point(0, 0), Point(60, 0), Point(30, 45)));
96 put_in_plane(plane, Ellipse(Point(20, 10), 12, 6));
97
98 Polygon poly;
99 poly.add_vertex(Point(0, 0));
100 poly.add_vertex(Point(40, 0));
101 poly.add_vertex(Point(20, 20));
102 poly.close();
103 put_in_plane(plane, poly);
104
105 Regular_Polygon reg(Point(50, 30), 15.0, 5);
106 put_in_plane(plane, reg);
107
108 put_in_plane(plane, Text(Point(10, 10), "Hello"));
109
110 std::ostringstream output;
111 plane.draw(output);
112 const std::string result = output.str();
113
114 EXPECT_NE(result.find("ellipse [x radius="), std::string::npos);
115 EXPECT_NE(result.find("\\node"), std::string::npos);
116 EXPECT_NE(result.find("-- cycle;"), std::string::npos);
118}
119
121{
122 Tikz_Plane plane(140, 90);
123
124 const Geom_Number pi_over_four = 0.7853981633974483096;
126
127 RotatedEllipse rotated(Point(20, 20), 12, 5, 0.7071067811865475244, 0.7071067811865475244);
128 put_in_plane(plane, rotated);
129
130 std::ostringstream output;
131 plane.draw(output);
132 const std::string result = output.str();
133
134 EXPECT_NE(result.find("\\fill"), std::string::npos);
135 EXPECT_NE(result.find("-- cycle;"), std::string::npos);
137}
138
140{
141 Tikz_Plane plane(120, 80);
142 plane.put_cartesian_axis();
143 put_in_plane(plane, Segment(Point(-10, -10), Point(10, 10)));
144
145 std::ostringstream output;
146 plane.draw(output);
147 const std::string result = output.str();
148
149 EXPECT_NE(result.find("\\draw["), std::string::npos);
150 EXPECT_NE(result.find("draw=gray"), std::string::npos);
151 EXPECT_NE(result.find("->"), std::string::npos);
152}
153
155{
156 Tikz_Plane plane(120, 80);
157 put_in_plane(plane, Rectangle(0, 0, 30, 20));
158 put_in_plane(plane, LineEq(0, 1)); // y = x
159
160 std::ostringstream output;
161 plane.draw(output);
162 const std::string result = output.str();
163
164 EXPECT_NE(result.find("-- cycle;"), std::string::npos);
165 EXPECT_NE(result.find("\\draw"), std::string::npos);
167}
168
170{
171 Tikz_Plane plane(120, 80);
172 put_in_plane(plane, Text(Point(0, 0), "A_%$#&{}\\B"));
173
174 std::ostringstream output;
175 plane.draw(output);
176 const std::string result = output.str();
177
178 EXPECT_NE(result.find("A\\_\\%\\$\\#\\&\\{\\}\\textbackslash{}B"), std::string::npos);
179}
180
182{
183 Tikz_Plane plane(100, 60);
184 plane.set_clip_padding_mm(3.5);
185
186 std::ostringstream output;
187 plane.draw(output);
188 const std::string result = output.str();
189
190 EXPECT_NE(result.find("\\clip (-3.500000,-3.500000) rectangle (103.500000,63.500000);"),
191 std::string::npos);
192}
193
195{
196 Tikz_Plane plane(120, 80);
197 put_quadratic_bezier_in_plane(plane, Point(0, 0), Point(20, 40), Point(40, 0), 8);
198 put_cubic_bezier_in_plane(plane, Point(40, 0), Point(60, 40), Point(80, -20), Point(100, 10), 8);
199
200 std::ostringstream output;
201 plane.draw(output);
202 const std::string result = output.str();
203
204 EXPECT_NE(result.find("\\draw"), std::string::npos);
206}
207
209{
210 Tikz_Plane plane(120, 80);
212 plane, Point(0, 0), Point(20, 40), Point(40, 0));
214 plane, Point(40, 0), Point(60, 40), Point(80, -20), Point(100, 10));
215
216 std::ostringstream output;
217 plane.draw(output);
218 const std::string result = output.str();
219
220 EXPECT_NE(result.find(".. controls"), std::string::npos);
221 EXPECT_NE(result.find(" and "), std::string::npos);
223}
224
226{
227 Tikz_Plane plane(120, 80);
228 Tikz_Style style = make_tikz_draw_style("black");
229 style.text_anchor = "west";
230 style.text_placement = "above right";
231 put_in_plane(plane, Text(Point(0, 0), "anchor demo"), style);
232
233 std::ostringstream output;
234 plane.draw(output);
235 const std::string result = output.str();
236
237 EXPECT_NE(result.find("anchor=west"), std::string::npos);
238 EXPECT_NE(result.find("above right"), std::string::npos);
239}
240
242{
243 Tikz_Plane plane(120, 80);
244 Tikz_Style style = make_tikz_fill_style("black", "gray!20");
245 style.pattern = "north east lines";
246 style.pattern_color = "black";
247
248 Polygon p;
249 p.add_vertex(Point(0, 0));
250 p.add_vertex(Point(20, 0));
251 p.add_vertex(Point(10, 10));
252 p.close();
253 put_in_plane(plane, p, style);
254
255 std::ostringstream output;
256 plane.draw(output);
257 const std::string result = output.str();
258
259 EXPECT_NE(result.find("pattern=north east lines"), std::string::npos);
260 EXPECT_NE(result.find("pattern color=black"), std::string::npos);
261}
262
264{
265 Tikz_Plane plane(120, 80);
266 plane.put_coordinate_grid(5.0, 5.0, true);
267 plane.put_cartesian_axis();
268 plane.enable_native_tikz_layers(true);
269 plane.enable_auto_legend(true);
270
272 named.thick = true;
273 plane.register_tikz_style("edgeA", named);
274
276 use_named.tikz_style_name = "edgeA";
277 use_named.draw_color = "";
278 put_in_plane(plane, Segment(Point(-10, -10), Point(10, 10)),
281 put_in_plane(plane, Segment(Point(-10, 10), Point(10, -10)),
283
284 std::ostringstream output;
285 plane.draw(output);
286 const std::string result = output.str();
287
288 EXPECT_NE(result.find("\\pgfdeclarelayer{background}"), std::string::npos);
289 EXPECT_NE(result.find("\\begin{pgfonlayer}{foreground}"), std::string::npos);
290 EXPECT_NE(result.find("\\tikzset{edgeA/.style="), std::string::npos);
291 EXPECT_NE(result.find("edgeA"), std::string::npos);
292 EXPECT_NE(result.find("draw=gray!40"), std::string::npos);
293 EXPECT_NE(result.find("\\node[anchor=west]"), std::string::npos);
295}
An axis-aligned ellipse.
Definition point.H:2006
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:229
Polar representation of a 2D point.
Definition point.H:726
A general (irregular) 2D polygon defined by a sequence of vertices.
Definition polygon.H:246
void add_vertex(const Point &point)
Add a vertex to the polygon.
Definition polygon.H:677
void close()
Close the polygon.
Definition polygon.H:840
An axis-aligned rectangle.
Definition point.H:1737
A regular polygon defined by center, side length, and vertex count.
Definition polygon.H:1132
An ellipse with arbitrary rotation.
Definition point.H:2404
Represents a line segment between two points.
Definition point.H:827
Represents a text string positioned at a 2D point.
Definition point.H:2739
2D TikZ canvas storing geometry objects and emitting LaTeX output.
Definition tikzgeom.H:184
void draw(std::ostream &output, const bool squarize=true) const
Emit a complete tikzpicture with all inserted objects.
Definition tikzgeom.H:1383
void set_clip_padding_mm(const double &padding_mm)
Configure clip expansion around frame borders.
Definition tikzgeom.H:1285
void put_cartesian_axis()
Enable Cartesian axes drawing (only when 0 lies in range).
Definition tikzgeom.H:1291
const double & get_wide() const
Plane width in millimeters.
Definition tikzgeom.H:1224
const double & get_height() const
Plane height in millimeters.
Definition tikzgeom.H:1226
static constexpr int Layer_Foreground
Definition tikzgeom.H:188
void enable_auto_legend(const bool enabled=true)
Enable auto-legend generation from style colors.
Definition tikzgeom.H:1328
void enable_native_tikz_layers(const bool enabled=true)
Enable native PGF layers (\pgfdeclarelayer).
Definition tikzgeom.H:1322
size_t size() const
Number of currently inserted objects.
Definition tikzgeom.H:1236
void put_coordinate_grid(const double step_x=1.0, const double step_y=1.0, const bool draw_ticks=true)
Enable coordinate grid/ticks with user step sizes.
Definition tikzgeom.H:1303
const double & get_yoffset() const
Vertical scope offset in millimeters.
Definition tikzgeom.H:1230
static constexpr int Layer_Background
Definition tikzgeom.H:186
void register_tikz_style(const std::string &name, const Tikz_Style &style)
Register a reusable \tikzset style for the rendered picture.
Definition tikzgeom.H:1346
const double & get_xoffset() const
Horizontal scope offset in millimeters.
Definition tikzgeom.H:1228
A non-degenerate triangle defined by three points.
Definition point.H:1478
#define TEST(name)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::string tolower(const char *str)
Convert a C std::string to lower-case.
void put_in_plane(Tikz_Plane &plane, const Geom &geom_obj)
Insert any supported geometry type in a Tikz_Plane.
Definition tikzgeom.H:1456
void put_cubic_bezier_native_in_plane(Tikz_Plane &plane, const Point &p0, const Point &p1, const Point &p2, const Point &p3, const Tikz_Style &style={}, const int layer=Tikz_Plane::Layer_Default)
Insert a cubic Bézier using native TikZ controls syntax.
Definition tikzgeom.H:1548
void put_quadratic_bezier_native_in_plane(Tikz_Plane &plane, const Point &p0, const Point &p1, const Point &p2, const Tikz_Style &style={}, const int layer=Tikz_Plane::Layer_Default)
Insert a quadratic Bézier using native TikZ controls syntax.
Definition tikzgeom.H:1536
void put_cubic_bezier_in_plane(Tikz_Plane &plane, const Point &p0, const Point &p1, const Point &p2, const Point &p3, const size_t subdivisions=64, const Tikz_Style &style={}, const int layer=Tikz_Plane::Layer_Default)
Approximate and insert a cubic Bézier as an open polyline.
Definition tikzgeom.H:1502
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
void put_quadratic_bezier_in_plane(Tikz_Plane &plane, const Point &p0, const Point &p1, const Point &p2, const size_t subdivisions=64, const Tikz_Style &style={}, const int layer=Tikz_Plane::Layer_Default)
Approximate and insert a quadratic Bézier as an open polyline.
Definition tikzgeom.H:1473
Tikz_Style make_tikz_draw_style(const std::string &draw_color)
Create a basic draw style with a custom color.
Definition tikzgeom.H:156
Tikz_Style make_tikz_fill_style(const std::string &draw_color, const std::string &fill_color)
Create a basic fill style with custom draw/fill colors.
Definition tikzgeom.H:164
2D infinite line in slope-intercept form.
Definition line.H:126
Style descriptor for TikZ primitives.
Definition tikzgeom.H:86
std::string text_anchor
TikZ node anchor (anchor=<value>)
Definition tikzgeom.H:93
std::string pattern
TikZ pattern (e.g. north east lines)
Definition tikzgeom.H:90
std::string text_placement
TikZ placement (above, below left, ...)
Definition tikzgeom.H:94
std::string tikz_style_name
Optional named style (from \tikzset)
Definition tikzgeom.H:87
std::string pattern_color
TikZ pattern color (pattern color=<color>)
Definition tikzgeom.H:91
TikZ/LaTeX geometric drawing utilities.
ofstream output
Definition writeHeap.C:215