Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
TMarker.cxx
Go to the documentation of this file.
1// @(#)root/graf:$Id$
2// Author: Rene Brun 12/05/95
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include <cstdlib>
13#include <iostream>
14
15#include "TROOT.h"
16#include "TBuffer.h"
17#include "TVirtualPad.h"
18#include "TVirtualPadPainter.h"
19#include "TMarker.h"
20#include "TMath.h"
21#include "TPoint.h"
22#include "TText.h"
23#include "snprintf.h"
24
25
26
27/** \class TMarker
28\ingroup BasicGraphics
29
30Manages Markers.
31
32Use the TMarker constructor to create a marker.
33
34~~~ {.cpp}
35 TMarker(Double_t x,Double_t y,Int_t marker)
36~~~
37
38The parameters `x` and `y` are the marker coordinates and `marker` is the marker type.
39
40Use the TPolyMarker to create an array on N points in a 2D space.
41At each point `x[i]`, `y[i]` a marker is drawn.
42
43Use the TAttMarker class to change the attributes color, style and size of a marker.
44
45_**Example**_
46
47- Use the `TAttMarker::SetMarkerSize(size)` method to set the `size` of a marker.
48
49*/
50
51////////////////////////////////////////////////////////////////////////////////
52/// Marker default constructor.
53
55{
56 fX = 0;
57 fY = 0;
58}
59
60////////////////////////////////////////////////////////////////////////////////
61/// Marker normal constructor.
62
65{
66 fX = x;
67 fY = y;
68 fMarkerStyle = marker;
69}
70
71////////////////////////////////////////////////////////////////////////////////
72/// Marker default destructor.
73
77
78////////////////////////////////////////////////////////////////////////////////
79/// Marker copy constructor.
80
81TMarker::TMarker(const TMarker &marker) : TObject(marker), TAttMarker(marker), TAttBBox2D(marker)
82{
83 fX = 0;
84 fY = 0;
85 marker.TMarker::Copy(*this);
86}
87
88////////////////////////////////////////////////////////////////////////////////
89/// Copy this marker to marker.
90
91void TMarker::Copy(TObject &obj) const
92{
93 TObject::Copy(obj);
94 TAttMarker::Copy(((TMarker &)obj));
95 ((TMarker &)obj).fX = fX;
96 ((TMarker &)obj).fY = fY;
97}
98
99////////////////////////////////////////////////////////////////////////////////
100/// Display the table of markers with their numbers.
101
103{
104 TMarker marker;
105 TText text;
106 marker.SetMarkerSize(3);
107 text.SetTextFont(62);
108 text.SetTextAlign(22);
109 text.SetTextSize(0.1);
110 TString atext;
111 Double_t x = 0;
112 Double_t dx = 1/16.0;
113 for (Int_t i=1;i<16;i++) {
114 x += dx;
115 atext.Form("%d",i);
116 marker.SetMarkerStyle(i);
117 marker.DrawMarker(x,.25);
118 text.DrawText(x,.12,atext.Data());
119 atext.Form("%d",i+19);
120 marker.SetMarkerStyle(i+19);
121 marker.DrawMarker(x,.55);
122 text.DrawText(x,.42,atext.Data());
123 atext.Form("%d",i+34);
124 marker.SetMarkerStyle(i+34);
125 marker.DrawMarker(x,.85);
126 text.DrawText(x,.72,atext.Data());
127 }
128}
129
130////////////////////////////////////////////////////////////////////////////////
131/// Display the table of markers with different line widths and their numbers.
132
134{
135 TMarker marker;
136 TText text;
137 marker.SetMarkerSize(3);
138 text.SetTextFont(62);
139 text.SetTextAlign(22);
140 text.SetTextSize(0.075);
141 TString atext;
142 Double_t x = 0;
143 Double_t dx = 1/19.0;
144 for (Int_t i=1;i<19;i++) {
145 x += dx;
146 atext.Form("%d",i+49);
147 marker.SetMarkerStyle(i+49);
148 marker.DrawMarker(x,0.19);
149 text.DrawText(x,0.08,atext.Data());
150 atext.Form("%d",i+67);
151 marker.SetMarkerStyle(i+67);
152 marker.DrawMarker(x,0.42);
153 text.DrawText(x,0.31,atext.Data());
154 atext.Form("%d",i+85);
155 marker.SetMarkerStyle(i+85);
156 marker.DrawMarker(x,0.65);
157 text.DrawText(x,0.54,atext.Data());
158 atext.Form("%d",i+103);
159 marker.SetMarkerStyle(i+103);
160 marker.DrawMarker(x,0.88);
161 text.DrawText(x,0.77,atext.Data());
162 }
163}
164
165////////////////////////////////////////////////////////////////////////////////
166/// Compute distance from point px,py to a marker.
167///
168/// Compute the closest distance of approach from point px,py to this marker.
169/// The distance is computed in pixels units.
170/// \return 0 if the distance <= markerRadius
171/// exact computed distance if dist in [markerRadius+1, markerRadius+2]
172/// 999 if larger than markerRadius + 3
173/// 9999 if no pad
174
176{
177 if (!gPad) return 9999;
178 Int_t pxm, pym;
179 if (TestBit(kMarkerNDC)) {
180 pxm = gPad->UtoAbsPixel(fX);
181 pym = gPad->VtoAbsPixel(fY);
182 } else {
183 pxm = gPad->XtoAbsPixel(gPad->XtoPad(fX));
184 pym = gPad->YtoAbsPixel(gPad->YtoPad(fY));
185 }
186 Int_t dist = static_cast<Int_t>(std::min<Long64_t>(INT_MAX, TMath::Sqrt(Long64_t(px-pxm)*(px-pxm) + Long64_t(py-pym)*(py-pym))));
187
188 //marker size = 1 is about 8 pixels
189 Int_t markerRadius = Int_t(4*fMarkerSize);
190 if (dist <= markerRadius) return 0;
191 if (dist > markerRadius+3) return 999;
192 return dist;
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// Draw this marker with its current attributes.
197
199{
200 AppendPad(option);
201}
202
203////////////////////////////////////////////////////////////////////////////////
204/// Draw this marker with new coordinates.
205
207{
208 TMarker *newmarker = new TMarker(x, y, 1);
209 TAttMarker::Copy(*newmarker);
210 newmarker->SetBit(kCanDelete);
211 newmarker->AppendPad();
212 return newmarker;
213}
214
215////////////////////////////////////////////////////////////////////////////////
216/// Execute action corresponding to one event.
217///
218/// This member function is called when a marker is clicked with the locator
219///
220/// If Left button is clicked on a marker, the marker is moved to
221/// a new position when the mouse button is released.
222
224{
225 if (!gPad || !gPad->IsEditable()) return;
226
227 auto &parent = *gPad;
228
229 static Int_t pxold, pyold;
230 Bool_t opaque = parent.OpaqueMoving();
231
232 auto action = [this, &parent](Bool_t paint, Int_t posx, Int_t posy) {
233 Double_t x, y;
234 if ((posx != -1111) || (posy != -1111) || !paint) {
235 x = parent.AbsPixeltoX(posx);
236 y = parent.AbsPixeltoY(posy);
237 } else if (TestBit(kMarkerNDC)) {
238 // first non-opaque paint will be performed at the original position
239 x = parent.GetX1() + GetX() * (parent.GetX2() - parent.GetX1());
240 y = parent.GetY1() + GetY() * (parent.GetY2() - parent.GetY1());
241 } else {
242 x = parent.XtoPad(GetX());
243 y = parent.YtoPad(GetY());
244 }
245 if (paint) {
246 auto pp = parent.GetPainter();
247 pp->SetAttMarker(*this);
248 pp->DrawPolyMarker(1, &x, &y);
249 } else if (TestBit(kMarkerNDC)) {
250 SetX((x - parent.GetX1()) / (parent.GetX2() - parent.GetX1()));
251 SetY((y - parent.GetY1()) / (parent.GetY2() - parent.GetY1()));
252 } else {
253 SetX(parent.PadtoX(x));
254 SetY(parent.PadtoY(y));
255 }
256 };
257
258 switch (event) {
259
260 case kButton1Down:
261 case kMouseMotion:
262 pxold = pyold = -1111;
263 parent.SetCursor(kMove);
264 break;
265
266 case kButton1Motion:
267 if (opaque) {
268 action(false, px, py);
269 parent.ShowGuidelines(this, event, 'i', true);
270 parent.ModifiedUpdate();
271 } else {
272 action(true, pxold, pyold);
273 action(true, px, py);
274 pxold = px; pyold = py;
275 }
276 break;
277
278 case kButton1Up:
279 if (opaque) {
280 parent.ShowGuidelines(this, event);
281 } else {
282 action(false, px, py);
283 parent.ModifiedUpdate();
284 }
285 break;
286 }
287}
288
289////////////////////////////////////////////////////////////////////////////////
290/// List this marker with its attributes.
291
293{
295 printf("Marker X=%f Y=%f marker type=%d\n",fX,fY,fMarkerStyle);
296}
297
298////////////////////////////////////////////////////////////////////////////////
299/// Paint this marker with its current attributes.
300
302{
303 if (!gPad) return;
304 if (TestBit(kMarkerNDC)) {
305 Double_t u = gPad->GetX1() + fX*(gPad->GetX2()-gPad->GetX1());
306 Double_t v = gPad->GetY1() + fY*(gPad->GetY2()-gPad->GetY1());
307 PaintMarker(u,v);
308 } else {
309 PaintMarker(gPad->XtoPad(fX),gPad->YtoPad(fY));
310 }
311}
312
313////////////////////////////////////////////////////////////////////////////////
314/// Draw this marker with new coordinates.
315
317{
318 TAttMarker::Modify(); //Change line attributes only if necessary
319 if (gPad) gPad->PaintPolyMarker(-1,&x,&y,"");
320}
321
322////////////////////////////////////////////////////////////////////////////////
323/// Draw this marker with new coordinates in NDC.
324
328
329////////////////////////////////////////////////////////////////////////////////
330/// Dump this marker with its attributes.
331
333{
334 printf("Marker X=%f Y=%f",fX,fY);
335 if (GetMarkerColor() != 1) printf(" Color=%d",GetMarkerColor());
336 if (GetMarkerStyle() != 1) printf(" MarkerStyle=%d",GetMarkerStyle());
337 if (GetMarkerSize() != 1) printf(" MarkerSize=%f",GetMarkerSize());
338 printf("\n");
339}
340
341////////////////////////////////////////////////////////////////////////////////
342/// Save primitive as a C++ statement(s) on output stream out
343
344void TMarker::SavePrimitive(std::ostream &out, Option_t *option)
345{
346 SavePrimitiveConstructor(out, Class(), "marker", TString::Format("%g, %g, %d", fX, fY, fMarkerStyle), kFALSE);
347
348 SaveMarkerAttributes(out, "marker", 1, 1, 1);
349
350 SavePrimitiveDraw(out, "marker", option);
351}
352
353////////////////////////////////////////////////////////////////////////////////
354/// Set NDC mode on if isNDC = kTRUE, off otherwise
355
357{
359 if (isNDC) SetBit(kMarkerNDC);
360}
361
362////////////////////////////////////////////////////////////////////////////////
363/// Stream an object of class TMarker.
364
366{
367 if (R__b.IsReading()) {
368 UInt_t R__s, R__c;
369 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
370 if (R__v > 1) {
371 R__b.ReadClassBuffer(TMarker::Class(), this, R__v, R__s, R__c);
372 return;
373 }
374 //====process old versions before automatic schema evolution
375 TObject::Streamer(R__b);
377 Float_t x,y;
378 R__b >> x; fX = x;
379 R__b >> y; fY = y;
380 //====end of old versions
381
382 } else {
383 R__b.WriteClassBuffer(TMarker::Class(),this);
384 }
385}
386
387////////////////////////////////////////////////////////////////////////////////
388/// Return the bounding Box of the marker
389
391{
392 auto size = GetMarkerSize();
393 Rectangle_t bbox{0, 0, (UShort_t) (2*size), (UShort_t) (2*size) };
394 if (gPad) {
395 if (TestBit(kMarkerNDC)) {
396 bbox.fX = gPad->UtoPixel(GetX()) - size;
397 bbox.fY = gPad->VtoPixel(GetY()) - size;
398 } else {
399 bbox.fX = gPad->XtoPixel(gPad->XtoPad(GetX())) - size;
400 bbox.fY = gPad->YtoPixel(gPad->YtoPad(GetY())) - size;
401 }
402 }
403 return bbox;
404}
405
406////////////////////////////////////////////////////////////////////////////////
407/// Set X coordinate of the center of the BoundingBox
408
413
414////////////////////////////////////////////////////////////////////////////////
415/// Set Y coordinate of the center of the BoundingBox
416
421
422////////////////////////////////////////////////////////////////////////////////
423/// Set left hand side of BoundingBox to a value
424/// (resize in x direction on left)
425
427{
429}
430
431////////////////////////////////////////////////////////////////////////////////
432/// Set right hand side of BoundingBox to a value
433/// (resize in x direction on right)
434
436{
438}
439
440////////////////////////////////////////////////////////////////////////////////
441/// Set top of BoundingBox to a value (resize in y direction on top)
442
444{
446}
447
448////////////////////////////////////////////////////////////////////////////////
449/// Set bottom of BoundingBox to a value
450/// (resize in y direction on bottom)
451
453{
455}
@ kMouseMotion
Definition Buttons.h:23
@ kButton1Motion
Definition Buttons.h:20
@ kButton1Up
Definition Buttons.h:19
@ kButton1Down
Definition Buttons.h:17
@ kMove
Definition GuiTypes.h:375
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
unsigned short UShort_t
Unsigned Short integer 2 bytes (unsigned short).
Definition RtypesCore.h:54
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
short Version_t
Class version identifier (short).
Definition RtypesCore.h:79
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
Definition RtypesCore.h:60
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
const char Option_t
Option string (const char).
Definition RtypesCore.h:80
#define gPad
Abstract base class for elements drawn in the editor.
Definition TAttBBox2D.h:19
Double_t GetYCoord(const Int_t y, Bool_t is_ndc=kFALSE)
Double_t GetXCoord(const Int_t x, Bool_t is_ndc=kFALSE)
virtual Style_t GetMarkerStyle() const
Return the marker style.
Definition TAttMarker.h:34
virtual Color_t GetMarkerColor() const
Return the marker color.
Definition TAttMarker.h:33
virtual void Modify()
virtual Size_t GetMarkerSize() const
Return the marker size.
Definition TAttMarker.h:35
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:43
void Copy(TAttMarker &attmarker) const
virtual void Streamer(TBuffer &)
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:48
Size_t fMarkerSize
Marker size.
Definition TAttMarker.h:26
Style_t fMarkerStyle
Marker style.
Definition TAttMarker.h:25
virtual void SaveMarkerAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t sizdef=1)
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual Version_t ReadVersion(UInt_t *start=nullptr, UInt_t *bcnt=nullptr, const TClass *cl=nullptr)=0
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=nullptr)=0
Bool_t IsReading() const
Definition TBuffer.h:86
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
void Streamer(TBuffer &) override
Stream an object of class TMarker.
Definition TMarker.cxx:365
Double_t fX
X position of marker (left,center,etc..).
Definition TMarker.h:25
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition TMarker.cxx:223
void ls(Option_t *option="") const override
List this marker with its attributes.
Definition TMarker.cxx:292
virtual void SetX(Double_t x)
Definition TMarker.h:53
void SetBBoxCenterY(const Int_t y) override
Set Y coordinate of the center of the BoundingBox.
Definition TMarker.cxx:417
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition TMarker.cxx:356
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TMarker.cxx:344
void Draw(Option_t *option="") override
Draw this marker with its current attributes.
Definition TMarker.cxx:198
~TMarker() override
Marker default destructor.
Definition TMarker.cxx:74
TMarker()
Marker default constructor.
Definition TMarker.cxx:54
Double_t fY
Y position of marker (left,center,etc..).
Definition TMarker.h:26
virtual void SetY(Double_t y)
Definition TMarker.h:54
void SetBBoxCenterX(const Int_t x) override
Set X coordinate of the center of the BoundingBox.
Definition TMarker.cxx:409
void SetBBoxY1(const Int_t y) override
Set top of BoundingBox to a value (resize in y direction on top).
Definition TMarker.cxx:443
void SetBBoxY2(const Int_t y) override
Set bottom of BoundingBox to a value (resize in y direction on bottom).
Definition TMarker.cxx:452
void Copy(TObject &marker) const override
Copy this marker to marker.
Definition TMarker.cxx:91
void Paint(Option_t *option="") override
Paint this marker with its current attributes.
Definition TMarker.cxx:301
virtual TMarker * DrawMarker(Double_t x, Double_t y)
Draw this marker with new coordinates.
Definition TMarker.cxx:206
virtual void PaintMarker(Double_t x, Double_t y)
Draw this marker with new coordinates.
Definition TMarker.cxx:316
Rectangle_t GetBBox() override
Return the bounding Box of the marker.
Definition TMarker.cxx:390
void SetBBoxX1(const Int_t x) override
Set left hand side of BoundingBox to a value (resize in x direction on left).
Definition TMarker.cxx:426
static TClass * Class()
static void DisplayMarkerLineWidths()
Display the table of markers with different line widths and their numbers.
Definition TMarker.cxx:133
void Print(Option_t *option="") const override
Dump this marker with its attributes.
Definition TMarker.cxx:332
@ kMarkerNDC
Marker position is in NDC.
Definition TMarker.h:31
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a marker.
Definition TMarker.cxx:175
Double_t GetX() const
Definition TMarker.h:44
void SetBBoxX2(const Int_t x) override
Set right hand side of BoundingBox to a value (resize in x direction on right).
Definition TMarker.cxx:435
static void DisplayMarkerTypes()
Display the table of markers with their numbers.
Definition TMarker.cxx:102
virtual void PaintMarkerNDC(Double_t u, Double_t v)
Draw this marker with new coordinates in NDC.
Definition TMarker.cxx:325
Double_t GetY() const
Definition TMarker.h:45
Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
Definition TObject.cxx:997
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:204
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:888
virtual void Copy(TObject &object) const
Copy this to obj.
Definition TObject.cxx:159
static void SavePrimitiveDraw(std::ostream &out, const char *variable_name, Option_t *option=nullptr)
Save invocation of primitive Draw() method Skipped if option contains "nodraw" string.
Definition TObject.cxx:845
TObject()
TObject constructor.
Definition TObject.h:259
static void SavePrimitiveConstructor(std::ostream &out, TClass *cl, const char *variable_name, const char *constructor_agrs="", Bool_t empty_line=kTRUE)
Save object constructor in the output stream "out".
Definition TObject.cxx:777
void ResetBit(UInt_t f)
Definition TObject.h:203
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:71
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:3052
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2385
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2363
Definition TText.h:22
TText * text
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:673
Rectangle structure (maps to the X11 XRectangle structure).
Definition GuiTypes.h:362