Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
texts_grid.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_eve_7
3/// SDF text on a fixed grid, for judging REveText rendering quality.
4///
5/// Unlike texts.C, which places random words at random sizes, this macro is
6/// deterministic, so two runs can be compared pixel by pixel. Each panel varies
7/// one parameter:
8///
9/// A screen mode, font-size ladder
10/// B screen mode, one line per font at the same size
11/// C world mode, size ladder in world units
12/// D world mode, text rotated about y and about x
13/// E mixed mode, text at increasing depth; all lines should be the same size
14///
15/// REveText::SetMode() takes 0 for world, 1 for screen and 2 for mixed. Two
16/// viewers, one perspective and one orthographic, show the same scenes.
17///
18/// Panels can be isolated from the ROOT prompt while the macro runs:
19/// tg_only("C") // show panel C alone
20/// tg_only("") // show all panels again
21///
22/// \macro_code
23///
24/// \author Matevz Tadel
25
26#include <ROOT/REveElement.hxx>
27#include <ROOT/REveManager.hxx>
28#include <ROOT/REveBox.hxx>
29#include <ROOT/REveScene.hxx>
30#include <ROOT/REveText.hxx>
31#include <ROOT/REveViewer.hxx>
32
33#include "TROOT.h"
34#include "TMath.h"
35
36#include <map>
37#include <string>
38
39namespace REX = ROOT::Experimental;
40using namespace ROOT::Experimental;
41
42// Short on purpose: at the top of the size ladder a long string runs off the
43// viewport. Chosen to expose the usual SDF failure modes --
44// Ill1 vertical stems a pixel apart OQ08 round counters
45// //\\ diagonals, the worst case for AA .. dots that vanish when the
46// smoothstep is too wide
47const char *kRuler = "Ill1 OQ08 //\\\\ ..";
48
49// These six faces ship with REve. The other fonts in TROOT::GetDataDir()/fonts,
50// such as verdana, georgia, comic and BlackChancery, can be used too:
51// REveText::AssertSdfFont() generates them from the TTF, which needs a display.
52// clang-format off
53const char *kFonts[] = {"LiberationSans-Regular", "LiberationSans-Bold",
54 "LiberationSerif-Regular", "LiberationSerif-Bold",
55 "LiberationMono-Regular", "LiberationMono-Bold"};
56// clang-format on
57const int kNFonts = sizeof(kFonts) / sizeof(char *);
58
59// Screen-mode font sizes, as fractions of viewport height.
60const float kSizes[] = {0.005f, 0.008f, 0.012f, 0.018f, 0.024f, 0.033f, 0.045f, 0.060f};
61const int kNSizes = sizeof(kSizes) / sizeof(float);
62
63const double kWorldLim = 100.0; // half-extent of the world-mode panels
64
65std::map<std::string, REveElement *> gPanels;
66
67//------------------------------------------------------------------------------
68
69static REveText *MakeText(REveElement *holder, const char *name, const std::string &txt, const char *font, int mode,
70 float size, Color_t col)
71{
72 auto t = new REveText(name);
73 t->SetText(txt);
74 t->SetFont(font);
75 t->SetMode(mode);
76 t->SetFontSize(size);
77 t->SetTextColor(col);
78 t->SetDrawFrame(false); // frames would dominate the picture at small sizes
79 holder->AddElement(t);
80 return t;
81}
82
83// REveText contributes nothing to the scene bounding box, so a scene of world text
84// gives the camera nothing to frame. Add a faint box of known extent.
85static void AddBBoxAnchor(REveElement *holder, double lim)
86{
87 auto b = new REveBox("bbox_anchor");
88 const double v[8][3] = {{-1, -1, -1}, {-1, 1, -1}, {1, 1, -1}, {1, -1, -1},
89 {-1, -1, 1}, {-1, 1, 1}, {1, 1, 1}, {1, -1, 1}};
90 for (int i = 0; i < 8; ++i)
91 b->SetVertex(i, lim * v[i][0], lim * v[i][1], lim * v[i][2]);
92 b->SetMainColor(kGray);
93 b->SetMainTransparency(90);
94 holder->AddElement(b);
95}
96
97// A -- SCREEN mode, font-size ladder, upper part of the left column.
98static void PanelA(REveElement *h)
99{
100 float y = 0.97;
101 for (int i = 0; i < kNSizes; ++i) {
102 float s = kSizes[i];
103 auto t = MakeText(h, Form("A_%02d_size_%.3f", i, s), Form("%.3f %s", s, kRuler), "LiberationSerif-Regular", 1, s,
104 kBlack);
105 t->SetPosition(REveVector(0.01, y, 0.0));
106 y -= 2.2 * s + 0.012;
107 }
108}
109
110// B -- SCREEN mode, one line per font, same size, below panel A.
111static void PanelB(REveElement *h)
112{
113 float y = 0.40;
114 for (int i = 0; i < kNFonts; ++i) {
115 auto t = MakeText(h, Form("B_%02d_%s", i, kFonts[i]), Form("%s %s", kRuler, kFonts[i]), kFonts[i], 1, 0.018f,
116 kBlue + 2);
117 t->SetPosition(REveVector(0.01, y, 0.0));
118 y -= 0.048;
119 }
120}
121
122// C -- WORLD mode, size ladder, stacked in y, all facing +z.
123static void PanelC(REveElement *h)
124{
125 AddBBoxAnchor(h, kWorldLim);
126 float y = -kWorldLim + 10.0;
127 for (int i = 0; i < kNSizes; ++i) {
128 float s = 200.0 * kSizes[i]; // world units: 1.0 .. 12.0
129 auto t = MakeText(h, Form("C_%02d_size_%.1f", i, s), Form("%.1f %s", s, kRuler), "LiberationSerif-Regular", 0, s,
130 kRed + 2);
131 t->RefMainTrans().SetPos(-kWorldLim, y, 0.0);
132 y += 2.4 * s + 3.0;
133 }
134}
135
136// D -- WORLD mode, constant size, rotated about y (top row) then x (bottom row).
137static void PanelD(REveElement *h)
138{
139 AddBBoxAnchor(h, kWorldLim);
140 const double d2r = TMath::Pi() / 180.0;
141 const int angles[] = {0, 20, 40, 60, 75};
142 for (int i = 0; i < 5; ++i) {
143 auto t = MakeText(h, Form("D_rotY_%02d", angles[i]), Form("Y%02d %s", angles[i], kRuler),
144 "LiberationSerif-Regular", 0, 8.0f, kGreen + 3);
145 auto &tr = t->RefMainTrans();
146 tr.SetPos(-kWorldLim, 20.0 * i + 10.0, 0.0);
147 tr.SetRotByAngles(0.0, angles[i] * d2r, 0.0);
148 }
149 for (int i = 0; i < 5; ++i) {
150 auto t = MakeText(h, Form("D_rotX_%02d", angles[i]), Form("X%02d %s", angles[i], kRuler),
151 "LiberationSerif-Regular", 0, 8.0f, kMagenta + 2);
152 auto &tr = t->RefMainTrans();
153 tr.SetPos(-kWorldLim, -20.0 * i - 10.0, 0.0);
154 tr.SetRotByAngles(angles[i] * d2r, 0.0, 0.0);
155 }
156}
157
158// E -- MIXED mode: world anchor, screen size, front facing. All five lines must
159// come out the same size on screen; if they shrink with depth the mode has
160// fallen back to world-space scaling.
161static void PanelE(REveElement *h)
162{
163 AddBBoxAnchor(h, kWorldLim);
164 for (int i = 0; i < 5; ++i) {
165 double z = -kWorldLim + i * (2.0 * kWorldLim / 4.0);
166 auto t = MakeText(h, Form("E_%02d_z_%+.0f", i, z), Form("z%+.0f %s", z, kRuler), "LiberationSerif-Regular", 2,
167 0.022f, kOrange + 7);
168 t->RefMainTrans().SetPos(-kWorldLim, -kWorldLim + i * 45.0, z);
169 }
170}
171
172//------------------------------------------------------------------------------
173/// Show only the named panel ("A".."E"); empty string shows all of them.
174/// Callable from the ROOT prompt while the macro is running.
175
176void tg_only(const char *panel = "")
177{
179 std::string want(panel);
180 for (auto &[name, el] : gPanels) {
181 bool on = want.empty() || name == want;
182 el->SetRnrSelf(on);
183 el->SetRnrChildren(on);
184 }
185}
186
187//------------------------------------------------------------------------------
188
189/// \param panels which panels to build, e.g. "ABCDE" (all) or "CDE" (world and
190/// mixed only). Leave out A and B to fit the camera to the world-mode
191/// panels, since screen-mode text in [0,1] is counted in the camera fit.
192void texts_grid(const char *panels = "ABCDE")
193{
194 std::string want(panels);
195 auto eveMng = REveManager::Create();
196 eveMng->AllowMultipleRemoteConnections(false, false);
197
198 // The shipped faces are found and nothing is generated. Another font is
199 // generated on first use, which needs a display; once its .png and .js.gz
200 // files exist it is found too.
201 std::string rf = std::string(TROOT::GetDataDir().Data()) + "/fonts/";
202 for (int i = 0; i < kNFonts; ++i)
203 REveText::AssertSdfFont(kFonts[i], rf + kFonts[i] + ".ttf");
204
205 // One scene per mode, so a mode can be switched off in the browser tree; one
206 // holder per panel, so tg_only() can isolate a panel from the prompt.
207 auto *scScreen = eveMng->SpawnNewScene("Screen text", "Panels A, B -- screen mode");
208 auto *scWorld = eveMng->SpawnNewScene("World text", "Panels C, D -- world mode");
209 auto *scMixed = eveMng->SpawnNewScene("Mixed text", "Panel E -- mixed mode");
210
211 // clang-format off
212 struct { const char *key; const char *name; REveScene *scene; void (*fill)(REveElement *); } defs[] = {
213 {"A", "A_screen_size_ladder", scScreen, PanelA},
214 {"B", "B_screen_fonts", scScreen, PanelB},
215 {"C", "C_world_size_ladder", scWorld, PanelC},
216 {"D", "D_world_rotation_fan", scWorld, PanelD},
217 {"E", "E_mixed_depth_ladder", scMixed, PanelE},
218 };
219 // clang-format on
220 for (auto &d : defs) {
221 if (want.find(d.key) == std::string::npos)
222 continue;
223 auto *h = new REveElement(d.name);
224 d.fill(h);
225 d.scene->AddElement(h);
226 gPanels[d.key] = h;
227 }
228
229 // The same scenes in a perspective and an orthographic viewer, to check that
230 // text renders the same under both cameras.
231 auto *v0 = (REveViewer *)eveMng->GetViewers()->FirstChild();
232 v0->SetName("Perspective");
233 v0->SetCameraType(REveViewer::kCameraPerspXOY); // XY plane face-on: panels C/D live there
234 auto *v1 = eveMng->SpawnNewViewer("Orthographic", "Same scenes, ortho camera");
235 v1->SetCameraType(REveViewer::kCameraOrthoXOY);
236
237 for (auto *v : {v0, v1}) {
238 v->AddScene(scScreen);
239 v->AddScene(scWorld);
240 v->AddScene(scMixed);
241 v->SetAxesType(REveViewer::kAxesEdge);
242 }
243
244 eveMng->Show();
245}
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define h(i)
Definition RSha256.hxx:106
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
short Color_t
Color number (short)
Definition RtypesCore.h:100
@ kGray
Definition Rtypes.h:65
@ kRed
Definition Rtypes.h:66
@ kOrange
Definition Rtypes.h:67
@ kBlack
Definition Rtypes.h:65
@ kGreen
Definition Rtypes.h:66
@ kMagenta
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
Option_t Option_t TPoint TPoint const char mode
char name[80]
Definition TGX11.cxx:142
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2571
3D box with arbitrary vertices (cuboid).
Base class for REveUtil visualization elements, providing hierarchy management, rendering control and...
RAII guard for locking Eve manager (ctor) and processing changes (dtor).
Eve representation of TGLScene.
Eve representation of a GL view.
static const TString & GetDataDir()
Get the data directory in the installation. Static utility function.
Definition TROOT.cxx:3406
Double_t y[n]
Definition legend1.C:17
Namespace for ROOT features in testing.
Definition TROOT.h:100
constexpr Double_t Pi()
Definition TMath.h:40