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