Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
REveLine.cxx
Go to the documentation of this file.
1 // @(#)root/eve7:$Id$
2 // Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007, 2018
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2019, 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 <ROOT/REveLine.hxx>
14 #include <ROOT/REveRenderData.hxx>
15 
16 #include "TClass.h"
17 
18 #include "json.hpp"
19 
20 using namespace ROOT::Experimental;
21 namespace REX = ROOT::Experimental;
22 
23 /** \class REveLine
24 \ingroup REve
25 An arbitrary polyline with fixed line and marker attributes.
26 */
27 
28 Bool_t REveLine::fgDefaultSmooth = kFALSE;
29 
30 
31 ////////////////////////////////////////////////////////////////////////////////
32 /// Constructor.
33 
34 REveLine::REveLine(const std::string &name, const std::string &title, Int_t n_points) :
35  REvePointSet(name, title, n_points),
36  fRnrLine (kTRUE),
37  fRnrPoints (kFALSE),
38  fSmooth (fgDefaultSmooth)
39 {
40  fMainColorPtr = &fLineColor;
41  fMarkerColor = kGreen;
42 }
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// Copy constructor.
46 
47 REveLine::REveLine(const REveLine &l) :
48  REvePointSet(l),
49  TAttLine (l),
50  fRnrLine (l.fRnrLine),
51  fRnrPoints (l.fRnrPoints),
52  fSmooth (l.fSmooth)
53 {
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Set marker color. Propagate to projected lines.
58 
59 void REveLine::SetMarkerColor(Color_t col)
60 {
61  for (auto &pi: fProjectedList)
62  {
63  REveLine* l = dynamic_cast<REveLine*>(pi);
64  if (l && fMarkerColor == l->GetMarkerColor())
65  {
66  l->SetMarkerColor(col);
67  l->StampObjProps();
68  }
69  }
70  TAttMarker::SetMarkerColor(col);
71 }
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 /// Set line-style of the line.
75 /// The style is propagated to projecteds.
76 
77 void REveLine::SetLineStyle(Style_t lstyle)
78 {
79  for (auto &pi: fProjectedList)
80  {
81  REveLine* pt = dynamic_cast<REveLine*>(pi);
82  if (pt)
83  {
84  pt->SetLineStyle(lstyle);
85  pt->StampObjProps();
86  }
87  }
88  TAttLine::SetLineStyle(lstyle);
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////
92 /// Set line-style of the line.
93 /// The style is propagated to projecteds.
94 
95 void REveLine::SetLineWidth(Width_t lwidth)
96 {
97  for (auto &pi: fProjectedList)
98  {
99  REveLine* pt = dynamic_cast<REveLine*>(pi);
100  if (pt)
101  {
102  pt->SetLineWidth(lwidth);
103  pt->StampObjProps();
104  }
105  }
106  StampObjProps();
107  TAttLine::SetLineWidth(lwidth);
108 }
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// Set rendering of line. Propagate to projected lines.
112 
113 void REveLine::SetRnrLine(Bool_t r)
114 {
115  fRnrLine = r;
116  for (auto &pi: fProjectedList)
117  {
118  REveLine* l = dynamic_cast<REveLine*>(pi);
119  if (l)
120  {
121  l->SetRnrLine(r);
122  l->StampObjProps();
123  }
124  }
125  StampObjProps();
126 }
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 /// Set rendering of points. Propagate to projected lines.
130 
131 void REveLine::SetRnrPoints(Bool_t r)
132 {
133  fRnrPoints = r;
134  for (auto &pi: fProjectedList)
135  {
136  REveLine *l = dynamic_cast<REveLine*>(pi);
137  if (l)
138  {
139  l->SetRnrPoints(r);
140  l->StampObjProps();
141  }
142  }
143  StampObjProps();
144 }
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// Set smooth rendering. Propagate to projected lines.
148 
149 void REveLine::SetSmooth(Bool_t r)
150 {
151  fSmooth = r;
152  for (auto &pi: fProjectedList)
153  {
154  REveLine* l = dynamic_cast<REveLine*>(pi);
155  if (l)
156  {
157  l->SetSmooth(r);
158  l->StampObjProps();
159  }
160  }
161  StampObjProps();
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// Make sure that no segment is longer than max.
166 /// Per point references and integer ids are lost.
167 
168 void REveLine::ReduceSegmentLengths(Float_t max)
169 {
170  // XXXX rewrite
171 
172  const Float_t max2 = max*max;
173 
174  Float_t *p = & fPoints[0].fX;
175  Int_t s = fSize;
176  REveVector a, b, d;
177 
178  std::vector<REveVector> q;
179 
180  b.Set(p);
181  q.push_back(b);
182  for (Int_t i = 1; i < s; ++i)
183  {
184  a = b; b.Set(&p[3*i]); d = b - a;
185  Float_t m2 = d.Mag2();
186  if (m2 > max2)
187  {
188  Float_t f = TMath::Sqrt(m2) / max;
189  Int_t n = TMath::FloorNint(f);
190  d *= 1.0f / (n + 1);
191  for (Int_t j = 0; j < n; ++j)
192  {
193  a += d;
194  q.push_back(a);
195  }
196  }
197  q.push_back(b);
198  }
199 
200  s = q.size();
201  Reset(s);
202  for (auto &i: q)
203  SetNextPoint(i.fX, i.fY, i.fZ);
204 }
205 
206 ////////////////////////////////////////////////////////////////////////////////
207 /// Sum-up lengths of individual segments.
208 
209 Float_t REveLine::CalculateLineLength() const
210 {
211  Float_t sum = 0;
212 
213  for (Int_t i = 1; i < fSize; ++i)
214  {
215  sum += fPoints[i - 1].Distance(fPoints[i]);
216  }
217 
218  return sum;
219 }
220 
221 ////////////////////////////////////////////////////////////////////////////////
222 /// Return the first point of the line.
223 /// If there are no points (0,0,0) is returned.
224 
225 REveVector REveLine::GetLineStart() const
226 {
227  REveVector v;
228  if (fSize > 0) v = RefPoint(0);
229  return v;
230 }
231 
232 ////////////////////////////////////////////////////////////////////////////////
233 /// Return the last point of the line.
234 /// If there are no points (0,0,0) is returned.
235 
236 REveVector REveLine::GetLineEnd() const
237 {
238  REveVector v;
239  if (fSize > 0) v = RefPoint(fSize - 1);
240  return v;
241 }
242 
243 ////////////////////////////////////////////////////////////////////////////////
244 /// Copy visualization parameters from element el.
245 
246 void REveLine::CopyVizParams(const REveElement* el)
247 {
248  const REveLine* m = dynamic_cast<const REveLine*>(el);
249  if (m)
250  {
251  TAttLine::operator=(*m);
252  fRnrLine = m->fRnrLine;
253  fRnrPoints = m->fRnrPoints;
254  fSmooth = m->fSmooth;
255  }
256 
257  REvePointSet::CopyVizParams(el);
258 }
259 
260 ////////////////////////////////////////////////////////////////////////////////
261 /// Write visualization parameters.
262 
263 void REveLine::WriteVizParams(std::ostream& out, const TString& var)
264 {
265  REvePointSet::WriteVizParams(out, var);
266 
267  TString t = " " + var + "->";
268  TAttLine::SaveLineAttributes(out, var);
269  out << t << "SetRnrLine(" << ToString(fRnrLine) << ");\n";
270  out << t << "SetRnrPoints(" << ToString(fRnrPoints) << ");\n";
271  out << t << "SetSmooth(" << ToString(fSmooth) << ");\n";
272 }
273 
274 ////////////////////////////////////////////////////////////////////////////////
275 /// Virtual from REveProjectable, returns REvePointSetProjected class.
276 
277 TClass* REveLine::ProjectedClass(const REveProjection*) const
278 {
279  return TClass::GetClass<REveLineProjected>();
280 }
281 
282 //------------------------------------------------------------------------------
283 
284 Int_t REveLine::WriteCoreJson(nlohmann::json &j, Int_t rnr_offset)
285 {
286  Int_t ret = REvePointSet::WriteCoreJson(j, rnr_offset);
287 
288  j["fLineWidth"] = GetLineWidth();
289  j["fLineStyle"] = GetLineStyle();
290  j["fLineColor"] = GetLineColor();
291 
292  return ret;
293 }
294 
295 ////////////////////////////////////////////////////////////////////////////////
296 /// Virtual from REveElement. Prepares render data for binary streaming to client
297 
298 void REveLine::BuildRenderData()
299 {
300  if (fSize > 0)
301  {
302  fRenderData = std::make_unique<REveRenderData>("makeTrack", 3*fSize);
303  fRenderData->PushV(&fPoints[0].fX, 3*fSize);
304  }
305 }
306 
307 ////////////////////////////////////////////////////////////////////////////////
308 /// Get default value for smooth-line drawing flag.
309 /// Static function.
310 
311 Bool_t REveLine::GetDefaultSmooth()
312 {
313  return fgDefaultSmooth;
314 }
315 
316 ////////////////////////////////////////////////////////////////////////////////
317 /// Set default value for smooth-line drawing flag (default kFALSE).
318 /// Static function.
319 
320 void REveLine::SetDefaultSmooth(Bool_t r)
321 {
322  fgDefaultSmooth = r;
323 }
324 
325 /** \class REveLineProjected
326 \ingroup REve
327 Projected copy of a REveLine.
328 */
329 
330 
331 ////////////////////////////////////////////////////////////////////////////////
332 /// Default constructor.
333 
334 REveLineProjected::REveLineProjected() :
335  REveLine (),
336  REveProjected ()
337 {
338 }
339 
340 ////////////////////////////////////////////////////////////////////////////////
341 /// Set projection manager and projection model.
342 /// Virtual from REveProjected.
343 
344 void REveLineProjected::SetProjection(REveProjectionManager* mng,
345  REveProjectable* model)
346 {
347  REveProjected::SetProjection(mng, model);
348  CopyVizParams(dynamic_cast<REveElement*>(model));
349 }
350 
351 ////////////////////////////////////////////////////////////////////////////////
352 /// Set depth (z-coordinate) of the projected points.
353 
354 void REveLineProjected::SetDepthLocal(Float_t d)
355 {
356  SetDepthCommon(d, this, fBBox);
357 
358  Int_t n = fSize;
359  Float_t *p = & fPoints[0].fZ;
360  for (Int_t i = 0; i < n; ++i, p+=3)
361  *p = fDepth;
362 }
363 
364 ////////////////////////////////////////////////////////////////////////////////
365 /// Re-apply the projection.
366 /// Virtual from REveProjected.
367 
368 void REveLineProjected::UpdateProjection()
369 {
370  REveProjection& proj = * fManager->GetProjection();
371  REveLine & als = * dynamic_cast<REveLine*>(fProjectable);
372  REveTrans *tr = als.PtrMainTrans(kFALSE);
373 
374  Int_t n = als.GetSize();
375  Reset(n);
376  fSize = n;
377  const Float_t *o = & als.RefPoint(0).fX;
378  Float_t *p = & fPoints[0].fX;
379  for (Int_t i = 0; i < n; ++i, o+=3, p+=3)
380  {
381  proj.ProjectPointfv(tr, o, p, fDepth);
382  }
383 }