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 "TMarker.h"
19#include "TVirtualX.h"
20#include "TMath.h"
21#include "TPoint.h"
22#include "TText.h"
23#include "snprintf.h"
24
26
27
28/** \class TMarker
29\ingroup BasicGraphics
30
31Manages Markers.
32
33Use the TMarker constructor to create a marker.
34
35~~~ {.cpp}
36 TMarker(Double_t x,Double_t y,Int_t marker)
37~~~
38
39The parameters `x` and `y` are the marker coordinates and `marker` is the marker type.
40
41Use the TPolyMarker to create an array on N points in a 2D space.
42At each point `x[i]`, `y[i]` a marker is drawn.
43
44Use the TAttMarker class to change the attributes color, style and size of a marker.
45
46_**Example**_
47
48- Use the `TAttMarker::SetMarkerSize(size)` method to set the `size` of a marker.
49
50*/
51
52////////////////////////////////////////////////////////////////////////////////
53/// Marker default constructor.
54
56{
57 fX = 0;
58 fY = 0;
59}
60
61////////////////////////////////////////////////////////////////////////////////
62/// Marker normal constructor.
63
66{
67 fX = x;
68 fY = y;
69 fMarkerStyle = marker;
70}
71
72////////////////////////////////////////////////////////////////////////////////
73/// Marker default destructor.
74
76{
77}
78
79////////////////////////////////////////////////////////////////////////////////
80/// Marker copy constructor.
81
82TMarker::TMarker(const TMarker &marker) : TObject(marker), TAttMarker(marker), TAttBBox2D(marker)
83{
84 fX = 0;
85 fY = 0;
86 marker.TMarker::Copy(*this);
87}
88
89////////////////////////////////////////////////////////////////////////////////
90/// Copy this marker to marker.
91
92void TMarker::Copy(TObject &obj) const
93{
94 TObject::Copy(obj);
95 TAttMarker::Copy(((TMarker &)obj));
96 ((TMarker &)obj).fX = fX;
97 ((TMarker &)obj).fY = fY;
98}
99
100////////////////////////////////////////////////////////////////////////////////
101/// Display the table of markers with their numbers.
102
104{
105 TMarker marker;
106 TText text;
107 marker.SetMarkerSize(3);
108 text.SetTextFont(62);
109 text.SetTextAlign(22);
110 text.SetTextSize(0.1);
111 TString atext;
112 Double_t x = 0;
113 Double_t dx = 1/16.0;
114 for (Int_t i=1;i<16;i++) {
115 x += dx;
116 atext.Form("%d",i);
117 marker.SetMarkerStyle(i);
118 marker.DrawMarker(x,.25);
119 text.DrawText(x,.12,atext.Data());
120 atext.Form("%d",i+19);
121 marker.SetMarkerStyle(i+19);
122 marker.DrawMarker(x,.55);
123 text.DrawText(x,.42,atext.Data());
124 atext.Form("%d",i+34);
125 marker.SetMarkerStyle(i+34);
126 marker.DrawMarker(x,.85);
127 text.DrawText(x,.72,atext.Data());
128 }
129}
130
131////////////////////////////////////////////////////////////////////////////////
132/// Display the table of markers with different line widths and their numbers.
133
135{
136 TMarker marker;
137 TText text;
138 marker.SetMarkerSize(3);
139 text.SetTextFont(62);
140 text.SetTextAlign(22);
141 text.SetTextSize(0.075);
142 TString atext;
143 Double_t x = 0;
144 Double_t dx = 1/19.0;
145 for (Int_t i=1;i<19;i++) {
146 x += dx;
147 atext.Form("%d",i+49);
148 marker.SetMarkerStyle(i+49);
149 marker.DrawMarker(x,0.19);
150 text.DrawText(x,0.08,atext.Data());
151 atext.Form("%d",i+67);
152 marker.SetMarkerStyle(i+67);
153 marker.DrawMarker(x,0.42);
154 text.DrawText(x,0.31,atext.Data());
155 atext.Form("%d",i+85);
156 marker.SetMarkerStyle(i+85);
157 marker.DrawMarker(x,0.65);
158 text.DrawText(x,0.54,atext.Data());
159 atext.Form("%d",i+103);
160 marker.SetMarkerStyle(i+103);
161 marker.DrawMarker(x,0.88);
162 text.DrawText(x,0.77,atext.Data());
163 }
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// Compute distance from point px,py to a marker.
168///
169/// Compute the closest distance of approach from point px,py to this marker.
170/// The distance is computed in pixels units.
171/// \return 0 if the distance <= markerRadius
172/// exact computed distance if dist in [markerRadius+1, markerRadius+2]
173/// 999 if larger than markerRadius + 3
174/// 9999 if no pad
175
177{
178 if (!gPad) return 9999;
179 Int_t pxm, pym;
180 if (TestBit(kMarkerNDC)) {
181 pxm = gPad->UtoPixel(fX);
182 pym = gPad->VtoPixel(fY);
183 } else {
184 pxm = gPad->XtoAbsPixel(gPad->XtoPad(fX));
185 pym = gPad->YtoAbsPixel(gPad->YtoPad(fY));
186 }
187 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))));
188
189 //marker size = 1 is about 8 pixels
190 Int_t markerRadius = Int_t(4*fMarkerSize);
191 if (dist <= markerRadius) return 0;
192 if (dist > markerRadius+3) return 999;
193 return dist;
194}
195
196////////////////////////////////////////////////////////////////////////////////
197/// Draw this marker with its current attributes.
198
200{
202
203}
204
205////////////////////////////////////////////////////////////////////////////////
206/// Draw this marker with new coordinates.
207
209{
210 TMarker *newmarker = new TMarker(x, y, 1);
211 TAttMarker::Copy(*newmarker);
212 newmarker->SetBit(kCanDelete);
213 newmarker->AppendPad();
214 return newmarker;
215}
216
217////////////////////////////////////////////////////////////////////////////////
218/// Execute action corresponding to one event.
219///
220/// This member function is called when a marker is clicked with the locator
221///
222/// If Left button is clicked on a marker, the marker is moved to
223/// a new position when the mouse button is released.
224
226{
227 if (!gPad) return;
228
229 TPoint p;
230 static Int_t pxold, pyold;
231 static Bool_t ndcsav;
232 Double_t dpx, dpy, xp1,yp1;
233 Bool_t opaque = gPad->OpaqueMoving();
234
235 if (!gPad->IsEditable()) return;
236
237 switch (event) {
238
239 case kButton1Down:
240 ndcsav = TestBit(kMarkerNDC);
241 if (!opaque) {
242 gVirtualX->SetTextColor(-1); // invalidate current text color (use xor mode)
243 TAttMarker::Modify(); //Change marker attributes only if necessary
244 }
245 // No break !!!
246
247 case kMouseMotion:
248 pxold = px; pyold = py;
249 gPad->SetCursor(kMove);
250 break;
251
252 case kButton1Motion:
253 p.fX = pxold; p.fY = pyold;
254 if (!opaque) gVirtualX->DrawPolyMarker(1, &p);
255 p.fX = px; p.fY = py;
256 if (!opaque) gVirtualX->DrawPolyMarker(1, &p);
257 pxold = px; pyold = py;
258 if (opaque) {
259 if (ndcsav) this->SetNDC(kFALSE);
260 this->SetX(gPad->PadtoX(gPad->AbsPixeltoX(px)));
261 this->SetY(gPad->PadtoY(gPad->AbsPixeltoY(py)));
262 gPad->ShowGuidelines(this, event, 'i', true);
263 gPad->Modified(kTRUE);
264 gPad->Update();
265 }
266 break;
267
268 case kButton1Up:
269 if (opaque) {
270 if (ndcsav && !this->TestBit(kMarkerNDC)) {
271 this->SetX((fX - gPad->GetX1())/(gPad->GetX2()-gPad->GetX1()));
272 this->SetY((fY - gPad->GetY1())/(gPad->GetY2()-gPad->GetY1()));
273 this->SetNDC();
274 }
275 gPad->ShowGuidelines(this, event);
276 } else {
277 if (TestBit(kMarkerNDC)) {
278 dpx = gPad->GetX2() - gPad->GetX1();
279 dpy = gPad->GetY2() - gPad->GetY1();
280 xp1 = gPad->GetX1();
281 yp1 = gPad->GetY1();
282 fX = (gPad->AbsPixeltoX(pxold)-xp1)/dpx;
283 fY = (gPad->AbsPixeltoY(pyold)-yp1)/dpy;
284 } else {
285 fX = gPad->PadtoX(gPad->AbsPixeltoX(px));
286 fY = gPad->PadtoY(gPad->AbsPixeltoY(py));
287 }
288 gPad->Modified(kTRUE);
289 gPad->Update();
290 gVirtualX->SetTextColor(-1);
291 }
292 break;
293 }
294}
295
296////////////////////////////////////////////////////////////////////////////////
297/// List this marker with its attributes.
298
300{
302 printf("Marker X=%f Y=%f marker type=%d\n",fX,fY,fMarkerStyle);
303}
304
305////////////////////////////////////////////////////////////////////////////////
306/// Paint this marker with its current attributes.
307
309{
310 if (!gPad) return;
311 if (TestBit(kMarkerNDC)) {
312 Double_t u = gPad->GetX1() + fX*(gPad->GetX2()-gPad->GetX1());
313 Double_t v = gPad->GetY1() + fY*(gPad->GetY2()-gPad->GetY1());
314 PaintMarker(u,v);
315 } else {
316 PaintMarker(gPad->XtoPad(fX),gPad->YtoPad(fY));
317 }
318}
319
320////////////////////////////////////////////////////////////////////////////////
321/// Draw this marker with new coordinates.
322
324{
325 TAttMarker::Modify(); //Change line attributes only if necessary
326 if (gPad) gPad->PaintPolyMarker(-1,&x,&y,"");
327}
328
329////////////////////////////////////////////////////////////////////////////////
330/// Draw this marker with new coordinates in NDC.
331
333{
334}
335
336////////////////////////////////////////////////////////////////////////////////
337/// Dump this marker with its attributes.
338
340{
341 printf("Marker X=%f Y=%f",fX,fY);
342 if (GetMarkerColor() != 1) printf(" Color=%d",GetMarkerColor());
343 if (GetMarkerStyle() != 1) printf(" MarkerStyle=%d",GetMarkerStyle());
344 if (GetMarkerSize() != 1) printf(" MarkerSize=%f",GetMarkerSize());
345 printf("\n");
346}
347
348////////////////////////////////////////////////////////////////////////////////
349/// Save primitive as a C++ statement(s) on output stream out
350
351void TMarker::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
352{
353 if (gROOT->ClassSaved(TMarker::Class())) {
354 out<<" ";
355 } else {
356 out<<" TMarker *";
357 }
358 out<<"marker = new TMarker("<<fX<<","<<fY<<","<<fMarkerStyle<<");"<<std::endl;
359
360 SaveMarkerAttributes(out,"marker",1,1,1);
361
362 out<<" marker->Draw();"<<std::endl;
363}
364
365////////////////////////////////////////////////////////////////////////////////
366/// Set NDC mode on if isNDC = kTRUE, off otherwise
367
369{
371 if (isNDC) SetBit(kMarkerNDC);
372}
373
374////////////////////////////////////////////////////////////////////////////////
375/// Stream an object of class TMarker.
376
378{
379 if (R__b.IsReading()) {
380 UInt_t R__s, R__c;
381 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
382 if (R__v > 1) {
383 R__b.ReadClassBuffer(TMarker::Class(), this, R__v, R__s, R__c);
384 return;
385 }
386 //====process old versions before automatic schema evolution
387 TObject::Streamer(R__b);
389 Float_t x,y;
390 R__b >> x; fX = x;
391 R__b >> y; fY = y;
392 //====end of old versions
393
394 } else {
395 R__b.WriteClassBuffer(TMarker::Class(),this);
396 }
397}
398
399////////////////////////////////////////////////////////////////////////////////
400/// Return the bounding Box of the Line
401
403{
404 Double_t size = this->GetMarkerSize();
405
406 Rectangle_t BBox{0,0,0,0};
407 if (!gPad) return BBox;
408 BBox.fX = gPad->XtoPixel(fX)+(Int_t)(2*size);
409 BBox.fY = gPad->YtoPixel(fY)-(Int_t)(2*size);
410 BBox.fWidth = 2*size;
411 BBox.fHeight = 2*size;
412 return (BBox);
413}
414
415////////////////////////////////////////////////////////////////////////////////
416/// Return the center of the BoundingBox as TPoint in pixels
417
419{
420 TPoint p(0,0);
421 if (!gPad) return (p);
422 p.SetX(gPad->XtoPixel(fX));
423 p.SetY(gPad->YtoPixel(fY));
424 return(p);
425}
426
427////////////////////////////////////////////////////////////////////////////////
428/// Set center of the BoundingBox
429
431{
432 if (!gPad) return;
433 fX = gPad->PixeltoX(p.GetX());
434 fY = gPad->PixeltoY(p.GetY() - gPad->VtoPixel(0));
435}
436
437////////////////////////////////////////////////////////////////////////////////
438/// Set X coordinate of the center of the BoundingBox
439
441{
442 if (!gPad) return;
443 fX = gPad->PixeltoX(x);
444}
445
446////////////////////////////////////////////////////////////////////////////////
447/// Set Y coordinate of the center of the BoundingBox
448
450{
451 if (!gPad) return;
452 fY = gPad->PixeltoY(y - gPad->VtoPixel(0));
453}
454
455////////////////////////////////////////////////////////////////////////////////
456/// Set left hand side of BoundingBox to a value
457/// (resize in x direction on left)
458
460{
461 if (!gPad) return;
462 Double_t size = this->GetMarkerSize();
463 fX = gPad->PixeltoX(x + (Int_t)size);
464}
465
466////////////////////////////////////////////////////////////////////////////////
467/// Set right hand side of BoundingBox to a value
468/// (resize in x direction on right)
469
471{
472 if (!gPad) return;
473 Double_t size = this->GetMarkerSize();
474 fX = gPad->PixeltoX(x - (Int_t)size);
475}
476
477////////////////////////////////////////////////////////////////////////////////
478/// Set top of BoundingBox to a value (resize in y direction on top)
479
481{
482 if (!gPad) return;
483 Double_t size = this->GetMarkerSize();
484 fY = gPad->PixeltoY(y - (Int_t)size - gPad->VtoPixel(0));
485}
486
487////////////////////////////////////////////////////////////////////////////////
488/// Set bottom of BoundingBox to a value
489/// (resize in y direction on bottom)
490
492{
493 if (!gPad) return;
494 Double_t size = this->GetMarkerSize();
495 fY = gPad->PixeltoY(y + (Int_t)size - gPad->VtoPixel(0));
496}
@ kMouseMotion
Definition Buttons.h:23
@ kButton1Motion
Definition Buttons.h:20
@ kButton1Up
Definition Buttons.h:19
@ kButton1Down
Definition Buttons.h:17
@ kMove
Definition GuiTypes.h:374
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Definition RtypesCore.h:45
short Version_t
Definition RtypesCore.h:65
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
long long Long64_t
Definition RtypesCore.h:69
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:382
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void pxm
Option_t Option_t TPoint TPoint const char text
#define gROOT
Definition TROOT.h:406
#define gPad
#define gVirtualX
Definition TVirtualX.h:337
Abstract base class for elements drawn in the editor.
Definition TAttBBox2D.h:19
Marker Attributes class.
Definition TAttMarker.h:19
virtual void SaveMarkerAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t sizdef=1)
Save line attributes as C++ statement(s) on output stream out.
virtual void Modify()
Change current marker attributes if necessary.
virtual Style_t GetMarkerStyle() const
Return the marker style.
Definition TAttMarker.h:32
virtual Color_t GetMarkerColor() const
Return the marker color.
Definition TAttMarker.h:31
virtual Size_t GetMarkerSize() const
Return the marker size.
Definition TAttMarker.h:33
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:40
void Copy(TAttMarker &attmarker) const
Copy this marker attributes to a new TAttMarker.
virtual void Streamer(TBuffer &)
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:45
Size_t fMarkerSize
Marker size.
Definition TAttMarker.h:24
Style_t fMarkerStyle
Marker style.
Definition TAttMarker.h:23
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
Manages Markers.
Definition TMarker.h:22
void Streamer(TBuffer &) override
Stream an object of class TMarker.
Definition TMarker.cxx:377
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:225
void ls(Option_t *option="") const override
List this marker with its attributes.
Definition TMarker.cxx:299
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:449
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition TMarker.cxx:368
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TMarker.cxx:351
@ kMarkerNDC
Marker position is in NDC.
Definition TMarker.h:31
void Draw(Option_t *option="") override
Draw this marker with its current attributes.
Definition TMarker.cxx:199
~TMarker() override
Marker default destructor.
Definition TMarker.cxx:75
TMarker()
Marker default constructor.
Definition TMarker.cxx:55
Double_t fY
Y position of marker (left,center,etc..)
Definition TMarker.h:26
TPoint GetBBoxCenter() override
Return the center of the BoundingBox as TPoint in pixels.
Definition TMarker.cxx:418
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:440
void SetBBoxY1(const Int_t y) override
Set top of BoundingBox to a value (resize in y direction on top)
Definition TMarker.cxx:480
void SetBBoxY2(const Int_t y) override
Set bottom of BoundingBox to a value (resize in y direction on bottom)
Definition TMarker.cxx:491
void Copy(TObject &marker) const override
Copy this marker to marker.
Definition TMarker.cxx:92
void Paint(Option_t *option="") override
Paint this marker with its current attributes.
Definition TMarker.cxx:308
virtual TMarker * DrawMarker(Double_t x, Double_t y)
Draw this marker with new coordinates.
Definition TMarker.cxx:208
virtual void PaintMarker(Double_t x, Double_t y)
Draw this marker with new coordinates.
Definition TMarker.cxx:323
Rectangle_t GetBBox() override
Return the bounding Box of the Line.
Definition TMarker.cxx:402
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:459
static TClass * Class()
static void DisplayMarkerLineWidths()
Display the table of markers with different line widths and their numbers.
Definition TMarker.cxx:134
void Print(Option_t *option="") const override
Dump this marker with its attributes.
Definition TMarker.cxx:339
void SetBBoxCenter(const TPoint &p) override
Set center of the BoundingBox.
Definition TMarker.cxx:430
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a marker.
Definition TMarker.cxx:176
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:470
static void DisplayMarkerTypes()
Display the table of markers with their numbers.
Definition TMarker.cxx:103
virtual void PaintMarkerNDC(Double_t u, Double_t v)
Draw this marker with new coordinates in NDC.
Definition TMarker.cxx:332
Mother of all ROOT objects.
Definition TObject.h:41
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:199
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
Definition TObject.cxx:894
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:190
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:786
virtual void Copy(TObject &object) const
Copy this to obj.
Definition TObject.cxx:146
void ResetBit(UInt_t f)
Definition TObject.h:198
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:62
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:2895
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2356
Base class for several text objects.
Definition TText.h:22
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:662
Rectangle structure (maps to the X11 XRectangle structure)
Definition GuiTypes.h:361
Short_t fX
Definition GuiTypes.h:362