00001 // BLElement.hh 00002 /* 00003 This source file is part of G4beamline, http://g4beamline.muonsinc.com 00004 Copyright (C) 2003,2004,2005,2006 by Tom Roberts, all rights reserved. 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 http://www.gnu.org/copyleft/gpl.html 00017 */ 00018 00019 #ifndef BLELEMENT_HH 00020 #define BLELEMENT_HH 00021 00022 #include <vector> 00023 #include "G4ThreeVector.hh" 00024 #include "G4LogicalVolume.hh" 00025 00026 #include "BLCoordinateTransform.hh" 00027 #include "BLCommand.hh" 00028 00029 /** class BLElement - interface class for all g4beamline Elements 00030 * 00031 * BLElement defines elements to be placed into the geometry of g4beamline. 00032 * 00033 * Normally an element implementation will derive a class from this one, 00034 * and if it has an EM field, also derives from BLElementField. Usually 00035 * all are in a single file named for the element command class. 00036 * The default constructor of the derived class is used to implement 00037 * the element command (see BLCommand), and the copy constructor 00038 * is used by the element command to create instances of the element. 00039 * 00040 * Note the constructor of the derived class must contain enough 00041 * information so the size of the element is determined (getLength(), 00042 * etc.). Or at least these must be known before the element command 00043 * returns. 00044 * 00045 * A non-default instance of this class represents a specific type 00046 * of the element, complete with all argument values; it is created 00047 * by the element command of the derived class. This instance can 00048 * be placed multiple times by the place command and by place commands 00049 * applied to groups in which the instance has been placed. After the 00050 * command file has been read (i.e. all elements have been created and 00051 * placed), then the World group is constructed, and that results in a 00052 * traversal of the placement tree and the construction of all elements. 00053 * Thus a single instance of this class can appear multiple times at 00054 * multiple locations within the overall geometry. 00055 * 00056 * When element argments are given on the place command, clone() is used 00057 * to copy the element, and then defineNamedArgs() is called for 00058 * each element argument. Then the cloned element is placed. 00059 * 00060 * NOTE: Derived classes MUST use the copy constructor, not the 00061 * default constructor! ONLY the default object should use the default 00062 * constructor. 00063 **/ 00064 class BLElement : public BLCommand { 00065 static std::map<G4String,BLElement*> mapElement; 00066 G4String name; 00067 bool placed; 00068 public: 00069 /// Default constructor. 00070 BLElement() : BLCommand(), name() { placed = false; } 00071 00072 /// Destructor. 00073 virtual ~BLElement() { } 00074 00075 /// Copy constructor. 00076 BLElement(const BLElement& r) : BLCommand(r), name(r.name) 00077 { placed = r.placed; } 00078 00079 /// clone() will clone a copy of an element. Used when element 00080 /// arguments are given in a place command. 00081 virtual BLElement *clone() = 0; 00082 00083 /// getName() returns the element's name; do not confuse this with 00084 /// commandName(). Not used by the default instance. 00085 virtual G4String getName() const { return name; } 00086 00087 /// setName() sets the element's name; 00088 /// Adds the BLElement to mapElement. Used by instances of the derived 00089 /// class representing real elements; not used by the default instance. 00090 /// virtual so derived base classes can keep a list of their elements. 00091 virtual void setName(G4String _name); 00092 00093 /// find() finds the BLElement with a given name. 00094 static BLElement *find(G4String name); 00095 00096 /// allOK() returns true iff all elements are ready to process beam. 00097 static G4bool allOK(); 00098 00099 // General functions used by elements: 00100 00101 // virtual functions to be defined for individual BLElement-s: 00102 00103 /// construct() will construct a physical implementation of this 00104 /// element at a specific location within the overall geometry. 00105 /// The name of the implementation should be parentName plus the name 00106 /// of the element; the physical volume should be linked into the 00107 /// parent in the usual way. relativeRotation is an active rotation 00108 /// of this element wrt the parent's coordinate axes, and 00109 /// relativePosition is in the parent's coords as usual (if the parent 00110 /// is the world, the centerline coordinate transform is included) -- 00111 /// the rotation for G4VPlacement is relativeRotation->inverse(). 00112 /// parentRotation and parentPosition are the rotation and position 00113 /// of the parent, expressed in global coordinates -- most elements 00114 /// can ignore them, as they are normally used to construct a 00115 /// BLCoordinateTransform from global coordinates to the element's 00116 /// local coordinates (e.g. for GlobalField). 00117 virtual void construct(G4RotationMatrix *relativeRotation, 00118 G4ThreeVector relativePosition, 00119 G4LogicalVolume *parent, 00120 G4String parentName, 00121 G4RotationMatrix *parentRotation, 00122 G4ThreeVector parentPosition) = 0; 00123 00124 /// getLength() returns this element's Length along the Z axis. 00125 virtual G4double getLength() = 0; 00126 00127 /// getWidth() returns this element's Width along the X axis. 00128 /// For asymmetric elements, the width is twice the element's 00129 /// futhest point from X=0. 00130 virtual G4double getWidth() = 0; 00131 00132 /// getHeight() returns this element's height along the Y axis. 00133 /// For asymmetric elements, the width is twice the element's 00134 /// futhest point from Y=0. 00135 virtual G4double getHeight() = 0; 00136 00137 /// isOK() returns true iff this element is ready to process beam. 00138 /// It is queried AFTER the reference particle is tracked, so it 00139 /// can reflect the status of tuning performed by the reference 00140 /// particle. 00141 virtual G4bool isOK() = 0; 00142 00143 /// isGroupElement() returns true if this is a BLGroupElement. 00144 /// That is, if this element can be the parent of other elements. 00145 virtual G4bool isGroupElement() { return false; } 00146 00147 /// getPlacedFlag() returns true if this element has been placed. 00148 bool getPlacedFlag() { return placed; } 00149 00150 /// setPlacedFlag() sets the flag for placing this element. 00151 void setPlacedFlag(bool flag=true) { placed = flag; } 00152 00153 // Geometry test functions. 00154 00155 /// generatePoints() will generate points for the testGeometry() 00156 /// function (of BLGroupElement) for this element. It generates 00157 /// however many points are necessary to test extremes of its surface, 00158 /// plus randomly distributed ones on the surface, up to npoints total. 00159 /// Each point is in the local coordinates of the element. 00160 virtual void generatePoints(int npoints, std::vector<G4ThreeVector> &v) 00161 = 0; 00162 00163 /// isOutside() returns true if the point is outside this element, 00164 /// or is within tolerance of being outside. 00165 /// local[] is the point in local coordinates of this element. 00166 virtual G4bool isOutside(G4ThreeVector &local, G4double tolerance) = 0; 00167 00168 /// generateBox() generates geometry test points for a Box 00169 /// (a convenience method for derived classes to use) 00170 void generateBox(unsigned int npoints, G4double width, G4double height, 00171 G4double length, std::vector<G4ThreeVector> &v); 00172 00173 /// generateTubs() generates geometry test points for a Tubs 00174 /// (a convenience method for derived classes to use) 00175 void generateTubs(unsigned int npoints, G4double innerRadius, 00176 G4double outerRadius, G4double initialPhi, G4double finalPhi, 00177 G4double length, std::vector<G4ThreeVector> &v); 00178 }; 00179 00180 #endif // BLELEMENT_HH