OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
TBeamline.h
Go to the documentation of this file.
1 #ifndef CLASSIC_TBeamline_HH
2 #define CLASSIC_TBeamline_HH
3 
4 // ------------------------------------------------------------------------
5 // $RCSfile: TBeamline.h,v $
6 // ------------------------------------------------------------------------
7 // $Revision: 1.3.2.1 $
8 // ------------------------------------------------------------------------
9 // Copyright: see Copyright.readme
10 // ------------------------------------------------------------------------
11 //
12 // Template class: TBeamline<class T>
13 //
14 // ------------------------------------------------------------------------
15 // Class category: Beamlines
16 // ------------------------------------------------------------------------
17 //
18 // $Date: 2004/11/18 22:18:06 $
19 // $Author: jsberg $
20 //
21 // ------------------------------------------------------------------------
22 
23 #include "Beamlines/Beamline.h"
26 #include "Utilities/CLRangeError.h"
27 #include "Algorithms/Vektor.h"
28 #include "Algorithms/Quaternion.h"
29 #include <algorithm>
30 #include <list>
31 
32 
33 // Template class TBeamline
34 // ------------------------------------------------------------------------
36 // Instantiation with different T types allows attachment of additional
37 // data to each position in the line.
38 
39 template <class T>
40 class TBeamline: public Beamline, public std::list<T> {
41 
42 public:
43 
45  TBeamline();
46 
48  explicit TBeamline(const std::string &name);
49 
50  TBeamline(const TBeamline<T> &right);
51  virtual ~TBeamline();
52 
54  virtual void accept(BeamlineVisitor &) const;
55 
57  // If the parameter [b]r2l[/b] is true, the line is traversed from
58  // right (s=C) to left (s=0).
59  // If any error occurs, this method may throw an exception.
60  virtual void iterate(BeamlineVisitor &, bool r2l) const;
61 
63  virtual TBeamline<T> *clone() const;
64 
66  virtual TBeamline<T> *copyStructure();
67 
69  // The whole beamline and the elements depending on [b]this[/b] are
70  // marked as sharable. After this call a [b]copyStructure()[/b] call
71  // reuses the element.
72  virtual void makeSharable();
73 
75  // Version for non-constant object.
76  virtual BeamlineGeometry &getGeometry();
77 
79  // Version for constant object.
80  virtual const BeamlineGeometry &getGeometry() const;
81 
83  // Return the length of the geometry, measured along the design orbit.
84  virtual double getArcLength() const;
85 
87  // Return the length of the geometry, measured along the design polygone.
88  virtual double getElementLength() const;
89 
91  // Return the arc length from the entrance to the origin of the
92  // geometry (non-negative).
93  virtual double getOrigin() const;
94 
96  // Return the arc length from the origin to the entrance of the
97  // geometry (non-positive).
98  virtual double getEntrance() const;
99 
101  // Return the arc length from the origin to the exit of the
102  // geometry (non-negative).
103  virtual double getExit() const;
104 
106  // Return the transform of the local coordinate system from the
107  // position [b]fromS[/b] to the position [b]toS[/b].
108  virtual Euclid3D getTransform(double fromS, double toS) const;
109 
111  // Equivalent to getTransform(0.0, s).
112  // Return the transform of the local coordinate system from the
113  // origin and [b]s[/b].
114  virtual Euclid3D getTransform(double s) const;
115 
117  // Equivalent to getTransform(getEntrance(), getExit()).
118  // Return the transform of the local coordinate system from the
119  // entrance to the exit of the element.
120  virtual Euclid3D getTotalTransform() const;
121 
123  // Equivalent to getTransform(0.0, getEntrance()).
124  // Return the transform of the local coordinate system from the
125  // origin to the entrance of the element.
126  virtual Euclid3D getEntranceFrame() const;
127 
129  // Equivalent to getTransform(0.0, getExit()).
130  // Return the transform of the local coordinate system from the
131  // origin to the exit of the element.
132  virtual Euclid3D getExitFrame() const;
133 
135  // Return the image of the element, containing the name and type string
136  // of the element, and a copy of the user-defined attributes.
137  virtual ElementImage *getImage() const;
138 
140  virtual ElementBase::ElementType getType() const;
141 
143  virtual void append(const T &);
144 
146  virtual void prepend(const T &);
147 
148  void setOrigin3D(const Vector_t& ori);
149  Vector_t getOrigin3D() const;
150  void setInitialDirection(const Quaternion& rot);
152 
153  void setRelativeFlag(bool flag);
154  bool getRelativeFlag() const;
155 protected:
156 
158  // Exists to match the interface for ElementBase.
160 
164 };
165 
166 
167 // Implementation of template class TBeamline
168 // ------------------------------------------------------------------------
169 
170 template <class T>
172  Beamline(),
173  std::list<T>(),
174  itsGeometry(*this),
175  itsOrigin_m(0),
176  itsCoordTrafoTo_m(1.0, 0.0, 0.0, 0.0),
177  relativePositions_m(false)
178 {}
179 
180 
181 template <class T>
182 TBeamline<T>::TBeamline(const std::string &name):
183  Beamline(name),
184  std::list<T>(),
185  itsGeometry(*this),
186  itsOrigin_m(0),
187  itsCoordTrafoTo_m(1.0, 0.0, 0.0, 0.0),
188  relativePositions_m(false)
189 {}
190 
191 
192 template <class T>
194  Beamline(rhs),
195  std::list<T>(rhs),
196  itsGeometry(*this),
197  itsOrigin_m(rhs.itsOrigin_m),
198  itsCoordTrafoTo_m(rhs.itsCoordTrafoTo_m),
199  relativePositions_m(rhs.relativePositions_m)
200 {}
201 
202 
203 template <class T>
205 {}
206 
207 
208 template <class T>
209 void TBeamline<T>::accept(BeamlineVisitor &visitor) const {
210  visitor.visitBeamline(*this);
211 }
212 
213 
214 template <class T>
215 void TBeamline<T>::iterate(BeamlineVisitor &visitor, bool r2l) const {
216  if(r2l) {
217  for(typename std::list<T>::const_reverse_iterator op = this->rbegin();
218  op != this->rend(); ++op) {
219  op->accept(visitor);
220  }
221  } else {
222  for(typename std::list<T>::const_iterator op = this->begin();
223  op != this->end(); ++op) {
224  op->accept(visitor);
225  }
226  }
227 }
228 
229 
230 template <class T>
232  TBeamline<T> *line = new TBeamline(getName());
233 
234  for(typename std::list<T>::const_iterator op = this->begin();
235  op != this->end(); ++op) {
236  // Make copy of the T object containing a deep copy of its child.
237  T newObj(*op);
238  newObj.setElement(op->getElement()->clone());
239  line->append(newObj);
240  }
241 
242  line->itsOrigin_m = itsOrigin_m;
243  line->itsCoordTrafoTo_m = itsCoordTrafoTo_m;
244  line->relativePositions_m = relativePositions_m;
245 
246  return line;
247 }
248 
249 // 21-6-2000 ada change iterator to const_iterator
250 template <class T>
252  if(isSharable()) {
253  return this;
254  } else {
255  TBeamline<T> *line = new TBeamline(getName());
256 
257  for(typename std::list<T>::const_iterator iter = this->begin();
258  iter != this->end(); ++iter) {
259  // The copy constructor ensures proper transmission of data.
260  T newObj(*iter);
261  newObj.setElement(iter->getElement()->copyStructure());
262  line->append(newObj);
263  }
264 
265  line->itsOrigin_m = itsOrigin_m;
266  line->itsCoordTrafoTo_m = itsCoordTrafoTo_m;
267  line->relativePositions_m = relativePositions_m;
268 
269  return line;
270  }
271 }
272 
273 // 21-6-2000 ada change iterator to const_iterator
274 template <class T> inline
276  shareFlag = true;
277 
278  for(typename std::list<T>::const_iterator iter = this->begin();
279  iter != this->end(); ++iter) {
280  iter->getElement()->makeSharable();
281  }
282 }
283 
284 
285 template <class T> inline
287  return itsGeometry;
288 }
289 
290 
291 template <class T> inline
293  return itsGeometry;
294 }
295 
296 
297 template <class T>
299  double length = 0.0;
300 
301  for(typename std::list<T>::const_iterator iter = this->begin();
302  iter != this->end(); ++iter) {
303  length += iter->getElement()->getArcLength();
304  }
305 
306  return length;
307 }
308 
309 
310 template <class T>
312  double length = 0.0;
313 
314  for(typename std::list<T>::const_iterator iter = this->begin();
315  iter != this->end(); ++iter) {
316  length += iter->getElement()->getElementLength();
317  }
318 
319  return length;
320 }
321 
322 
323 template <class T>
324 double TBeamline<T>::getOrigin() const {
325  return (getArcLength() / 2.0);
326 }
327 
328 
329 template <class T>
331  return (- getOrigin());
332 }
333 
334 
335 template <class T>
336 double TBeamline<T>::getExit() const {
337  return (getArcLength() / 2.0);
338 }
339 
340 
341 template <class T>
342 Euclid3D TBeamline<T>::getTransform(double fromS, double toS) const {
343  Euclid3D transform;
344 
345  if(fromS < toS) {
346  double s1 = getEntrance();
347  typename std::list<T>::const_iterator iter = this->begin();
348 
349  while(iter != this->end() && s1 <= toS) {
350  const ElementBase &element = *iter->getElement();
351  double l = element.getArcLength();
352  double s2 = s1 + l;
353 
354  if(s2 > fromS) {
355  double s0 = (s1 + s2) / 2.0;
356  double arc1 = std::max(s1, fromS) - s0;
357  double arc2 = std::min(s2, toS) - s0;
358  transform *= element.getTransform(arc1, arc2);
359  }
360 
361  s1 = s2;
362  ++iter;
363  }
364  } else {
365  double s1 = getExit();
366  typename std::list<T>::const_reverse_iterator iter = this->rbegin();
367 
368  while(iter != this->rend() && s1 >= toS) {
369  const ElementBase &element = *iter->getElement();
370  double l = element.getArcLength();
371  double s2 = s1 - l;
372 
373  if(s2 < fromS) {
374  double s0 = (s1 + s2) / 2.0;
375  double arc1 = std::min(s1, fromS) - s0;
376  double arc2 = std::max(s2, toS) - s0;
377  transform *= element.getTransform(arc1, arc2);
378  }
379 
380  s1 = s2;
381  ++iter;
382  }
383  }
384 
385  return transform;
386 }
387 
388 
389 template <class T>
391  Euclid3D transform;
392 
393  for(typename std::list<T>::const_iterator iter = this->begin();
394  iter != this->end(); ++iter) {
395  transform.dotBy(iter->getElement()->getTotalTransform());
396  }
397 
398  return transform;
399 }
400 
401 
402 template <class T>
404  return getTransform(0.0, s);
405 }
406 
407 
408 template <class T>
410  return getTransform(0.0, getEntrance());
411 }
412 
413 
414 template <class T>
416  return getTransform(0.0, getExit());
417 }
418 
419 
420 template <class T> inline
422  // this code needs revision when the class ElementImage is defined
423  return NULL;
424 }
425 
426 
427 template <class T> inline
429  return BEAMLINE;
430 }
431 
432 
433 template <class T> inline
434 void TBeamline<T>::append(const T &obj) {
435  this->push_back(obj);
436 }
437 
438 
439 template <class T> inline
440 void TBeamline<T>::prepend(const T &obj) {
441  this->push_front(obj);
442 }
443 
444 template <class T> inline
446  itsOrigin_m = ori;
447 }
448 
449 template <class T> inline
451  return itsOrigin_m;
452 }
453 
454 template <class T> inline
456  itsCoordTrafoTo_m = trafoTo;
457 }
458 
459 template <class T> inline
461  return itsCoordTrafoTo_m;
462 }
463 
464 template <class T> inline
466  relativePositions_m = flag;
467 }
468 
469 template <class T> inline
471  return relativePositions_m;
472 }
473 #endif // CLASSIC_TBeamline_HH
virtual TBeamline< T > * clone() const
Make clone.
Definition: TBeamline.h:231
virtual double getArcLength() const
Get arc length.
Definition: ElementBase.h:507
Interface for basic beam line object.
Definition: ElementBase.h:128
bool getRelativeFlag() const
Definition: TBeamline.h:470
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
Definition: ElementBase.h:531
Definition: rbendmap.h:8
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:123
virtual double getOrigin() const
Get origin position.
Definition: TBeamline.h:324
void setRelativeFlag(bool flag)
Definition: TBeamline.h:465
virtual ElementBase::ElementType getType() const
Get beamline type.
Definition: TBeamline.h:428
virtual double getElementLength() const
Get design length.
Definition: TBeamline.h:311
Implements the composite geometry of a beam line.
virtual Euclid3D getEntranceFrame() const
Get transform.
Definition: TBeamline.h:409
virtual void makeSharable()
Set sharable flag.
Definition: TBeamline.h:275
Vector_t getOrigin3D() const
Definition: TBeamline.h:450
Template class for beam lines.
Definition: TBeamline.h:40
virtual double getEntrance() const
Get entrance position.
Definition: TBeamline.h:330
void setOrigin3D(const Vector_t &ori)
Definition: TBeamline.h:445
void setInitialDirection(const Quaternion &rot)
Definition: TBeamline.h:455
const Euclid3D & dotBy(const Euclid3D &rhs)
Dot product with assign.
Definition: Euclid3D.cpp:102
An image of an element.
Definition: ElementImage.h:35
virtual Euclid3D getExitFrame() const
Get transform.
Definition: TBeamline.h:415
virtual Euclid3D getTotalTransform() const
Get transform.
Definition: TBeamline.h:390
Displacement and rotation in space.
Definition: Euclid3D.h:68
virtual void iterate(BeamlineVisitor &, bool r2l) const
Apply visitor to all elements of the line.
Definition: TBeamline.h:215
virtual void accept(BeamlineVisitor &) const
Apply BeamlineVisitor to this line.
Definition: TBeamline.h:209
An abstract sequence of beam line components.
Definition: Beamline.h:37
virtual ~TBeamline()
Definition: TBeamline.h:204
virtual BeamlineGeometry & getGeometry()
Get geometry.
Definition: TBeamline.h:286
Vector_t itsOrigin_m
Definition: TBeamline.h:161
virtual double getArcLength() const
Get arc length.
Definition: TBeamline.h:298
virtual void visitBeamline(const Beamline &)=0
Apply the algorithm to a Beamline.
const std::string name
Quaternion itsCoordTrafoTo_m
Definition: TBeamline.h:162
virtual void append(const T &)
Append a T object.
Definition: TBeamline.h:434
Quaternion getInitialDirection() const
Definition: TBeamline.h:460
BeamlineGeometry itsGeometry
The beamline geometry.
Definition: TBeamline.h:159
bool relativePositions_m
Definition: TBeamline.h:163
virtual TBeamline< T > * copyStructure()
Make structure copy.
Definition: TBeamline.h:251
virtual void prepend(const T &)
Prepend a T object.
Definition: TBeamline.h:440
TBeamline()
Default constructor.
Definition: TBeamline.h:171
Abstract algorithm.
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:95
virtual Euclid3D getTransform(double fromS, double toS) const
Get transform.
Definition: TBeamline.h:342
virtual ElementImage * getImage() const
Construct an image.
Definition: TBeamline.h:421
virtual double getExit() const
Get exit position.
Definition: TBeamline.h:336