00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "G4TrajectoryDrawByAttribute.hh"
00031 #include "G4AttDef.hh"
00032 #include "G4AttFilterUtils.hh"
00033 #include "G4AttUtils.hh"
00034 #include "G4AttValue.hh"
00035 #include "G4TrajectoryDrawerUtils.hh"
00036 #include "G4VAttValueFilter.hh"
00037 #include "G4VisTrajContext.hh"
00038 #include "G4VTrajectory.hh"
00039 #include <assert.h>
00040
00041 G4TrajectoryDrawByAttribute::G4TrajectoryDrawByAttribute(const G4String& name, G4VisTrajContext* context)
00042 :G4VTrajectoryModel(name, context)
00043 ,fAttName("")
00044 ,fFirst(true)
00045 ,fWarnedMissingAttribute(false)
00046 ,filter(0)
00047 {}
00048
00049 G4TrajectoryDrawByAttribute::~G4TrajectoryDrawByAttribute()
00050 {
00051 ContextMap::iterator iter = fContextMap.begin();
00052
00053 while (iter != fContextMap.end()) {
00054 delete iter->second;
00055 iter++;
00056 }
00057
00058 delete filter;
00059 }
00060
00061 void
00062 G4TrajectoryDrawByAttribute::Draw(const G4VTrajectory& object,
00063 const G4int&,
00064 const G4bool& visible) const
00065 {
00066 Draw(object, visible);
00067 }
00068
00069 void
00070 G4TrajectoryDrawByAttribute::Draw(const G4VTrajectory& object,
00071 const G4bool& visible) const
00072 {
00073
00074 if (fAttName.isNull()) {
00075
00076 if (!fWarnedMissingAttribute) {
00077 G4ExceptionDescription ed;
00078 ed<<"Null attribute name";
00079 G4Exception("G4TrajectoryDrawByAttribute::Draw",
00080 "modeling0116",
00081 JustWarning, ed);
00082 fWarnedMissingAttribute = true;
00083 }
00084
00085 return;
00086 }
00087
00088
00089 if (fFirst) {
00090
00091 fFirst = false;
00092
00093
00094 G4AttDef attDef;
00095
00096
00097 if (!G4AttUtils::ExtractAttDef(object, fAttName, attDef)) {
00098 static G4bool warnedUnableToExtract = false;
00099 if (!warnedUnableToExtract) {
00100 G4ExceptionDescription ed;
00101 ed <<"Unable to extract attribute definition named "<<fAttName;
00102 G4Exception
00103 ("G4TrajectoryDrawByAttribute::Draw",
00104 "modeling0117", JustWarning, ed, "Invalid attribute name");
00105 G4cout << "Available attributes:\n"
00106 << object.GetAttDefs();
00107 warnedUnableToExtract = true;
00108 }
00109 return;
00110 }
00111
00112
00113 filter = G4AttFilterUtils::GetNewFilter(attDef);
00114 assert (0 != filter);
00115
00116
00117
00118 ContextMap::const_iterator iter = fContextMap.begin();
00119
00120 while (iter != fContextMap.end()) {
00121 if (iter->first.second == G4TrajectoryDrawByAttribute::Interval) {
00122 filter->LoadIntervalElement(iter->first.first);
00123 }
00124 else if (iter->first.second == G4TrajectoryDrawByAttribute::SingleValue) {
00125 filter->LoadSingleValueElement(iter->first.first);
00126 }
00127 iter++;
00128 }
00129 }
00130
00131
00132 G4AttValue attVal;
00133
00134
00135 if (!G4AttUtils::ExtractAttValue(object, fAttName, attVal)) {
00136 static G4bool warnedUnableToExtract = false;
00137 if (!warnedUnableToExtract) {
00138 G4ExceptionDescription ed;
00139 ed <<"Unable to extract attribute value named "<<fAttName;
00140 G4Exception
00141 ("G4TrajectoryDrawByAttribute::Draw",
00142 "modeling0118", JustWarning, ed, "Invalid attribute name");
00143 G4cout << "Available attributes:\n"
00144 << object.GetAttDefs();
00145 warnedUnableToExtract = true;
00146 }
00147 return;
00148 }
00149
00150 G4VisTrajContext myContext(GetContext());
00151 G4String key;
00152
00153
00154
00155 if (filter->GetValidElement(attVal, key)) {
00156
00157
00158
00159 ContextMap::const_iterator iter = fContextMap.begin();
00160
00161 G4bool gotContext(false);
00162
00163 while (!gotContext && (iter != fContextMap.end())) {
00164 if (iter->first.first == key) {
00165 myContext = *(iter->second);
00166 gotContext = true;
00167 }
00168 iter++;
00169 }
00170
00171 assert (gotContext);
00172 }
00173
00174 myContext.SetVisible(visible);
00175
00176 if (GetVerbose()) {
00177 G4cout<<"G4TrajectoryDrawByAttribute drawer named "<<Name();
00178 G4cout<<", drawing style selected according to value of attribute "<<fAttName;
00179 G4cout<<" : "<<attVal.GetValue()<<". Selected context:"<<G4endl;
00180 myContext.Print(G4cout);
00181 }
00182
00183
00184 G4TrajectoryDrawerUtils::DrawLineAndPoints(object, myContext);
00185 }
00186
00187 void
00188 G4TrajectoryDrawByAttribute::Print(std::ostream& ostr) const
00189 {
00190 ostr<<"G4TrajectoryDrawByAttribute, dumping configuration for model named "<< Name() <<":"<<std::endl;;
00191
00192 ostr<<"Default configuration:"<<G4endl;
00193 GetContext().Print(ostr);
00194
00195 ostr<<"\nAttribute name "<<fAttName<<std::endl;
00196 ostr<<"\nKey<->Context map dump:"<<std::endl;
00197
00198 ContextMap::const_iterator iter = fContextMap.begin();
00199
00200 while (iter != fContextMap.end()) {
00201 ostr<<"Context for key "<<iter->first.first<<":"<<std::endl;
00202 iter->second->Print(ostr);
00203
00204 iter++;
00205 }
00206 }
00207
00208 void
00209 G4TrajectoryDrawByAttribute::Set(const G4String& name)
00210 {
00211 fAttName = name;
00212 }
00213
00214 void
00215 G4TrajectoryDrawByAttribute::AddIntervalContext(const G4String& name, G4VisTrajContext* context)
00216 {
00217
00218 std::pair<G4String, Config> myPair(name, G4TrajectoryDrawByAttribute::Interval);
00219
00220 ContextMap::iterator iter = fContextMap.find(myPair);
00221
00222 if (iter != fContextMap.end()) {
00223 G4ExceptionDescription ed;
00224 ed <<"Interval "<< name <<" already exists";
00225 G4Exception
00226 ("G4TrajectoryDrawByAttribute::AddIntervalContext",
00227 "modeling0119", FatalErrorInArgument, ed, "Invalid interval");
00228 }
00229
00230 fContextMap[myPair] = context;
00231 }
00232
00233 void
00234 G4TrajectoryDrawByAttribute::AddValueContext(const G4String& name, G4VisTrajContext* context)
00235 {
00236
00237 std::pair<G4String, Config> myPair(name, G4TrajectoryDrawByAttribute::SingleValue);
00238
00239 ContextMap::iterator iter = fContextMap.find(myPair);
00240
00241 if (iter != fContextMap.end()) {
00242 G4ExceptionDescription ed;
00243 ed <<"Single value "<< name <<" already exists";
00244 G4Exception
00245 ("G4TrajectoryDrawByAttribute::AddSingleValueContext",
00246 "modeling0120", FatalErrorInArgument, ed, "Invalid value");
00247 }
00248
00249 fContextMap[myPair] = context;
00250 }