source: trunk/source/visualization/modeling/src/G4TrajectoryDrawByAttribute.cc @ 1348

Last change on this file since 1348 was 1348, checked in by garnier, 13 years ago

update

File size: 7.7 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26// $Id: G4TrajectoryDrawByAttribute.cc,v 1.6 2010/12/11 16:41:11 allison Exp $
27// GEANT4 tag $Name:  $
28//
29// Jane Tinslay August 2006
30//
31#include "G4TrajectoryDrawByAttribute.hh"
32#include "G4AttDef.hh"
33#include "G4AttFilterUtils.hh"
34#include "G4AttUtils.hh"
35#include "G4AttValue.hh"
36#include "G4TrajectoryDrawerUtils.hh"
37#include "G4VAttValueFilter.hh"
38#include "G4VisTrajContext.hh"
39#include "G4VTrajectory.hh"
40#include <assert.h>
41#include <sstream>
42
43G4TrajectoryDrawByAttribute::G4TrajectoryDrawByAttribute(const G4String& name, G4VisTrajContext* context)
44  :G4VTrajectoryModel(name, context)
45  ,fAttName("")
46  ,fFirst(true)
47  ,fWarnedMissingAttribute(false)
48  ,filter(0)
49{}
50
51G4TrajectoryDrawByAttribute::~G4TrajectoryDrawByAttribute() 
52{
53  ContextMap::iterator iter = fContextMap.begin();
54 
55  while (iter != fContextMap.end()) {
56    delete iter->second;
57    iter++;
58  }
59 
60  delete filter;
61}
62
63void
64G4TrajectoryDrawByAttribute::Draw(const G4VTrajectory& object,
65                                  const G4int&,
66                                  const G4bool& visible) const
67{
68  Draw(object, visible);
69}
70
71void
72G4TrajectoryDrawByAttribute::Draw(const G4VTrajectory& object, 
73                                  const G4bool& visible) const
74{
75  // Return if attribute name has not been set. Just print one warning
76  if (fAttName.isNull()) {
77
78    if (!fWarnedMissingAttribute) {
79      std::ostringstream o;
80      o<<"Null attribute name";
81      G4Exception("G4TrajectoryDrawByAttribute::Draw", "NullAttributeName", JustWarning, o.str().c_str());
82     
83      fWarnedMissingAttribute = true;
84    }
85   
86    return;
87  }
88 
89  // Basically cache data loaded filter for efficiency
90  if (fFirst) {
91   
92    fFirst = false;
93   
94    // Get attribute definition
95    G4AttDef attDef;
96   
97    // Expect definition to exist   
98    if (!G4AttUtils::ExtractAttDef(object, fAttName, attDef)) {
99      static G4bool warnedUnableToExtract = false;
100      if (!warnedUnableToExtract) {
101        std::ostringstream o;
102        o <<"Unable to extract attribute definition named "<<fAttName;
103        G4Exception
104          ("G4TrajectoryDrawByAttribute::Draw", "InvalidAttributeDefinition", JustWarning, o.str().c_str());
105        G4cout << "Available attributes:\n"
106               << object.GetAttDefs();
107        warnedUnableToExtract = true;
108      }
109      return;
110    }
111   
112    // Get new G4AttValue filter
113    filter = G4AttFilterUtils::GetNewFilter(attDef);
114    assert (0 != filter);
115   
116    // Load both interval and single valued data. Single valued data should
117    // override interval data.
118    ContextMap::const_iterator iter = fContextMap.begin();
119   
120    while (iter != fContextMap.end()) {
121      if (iter->first.second == G4TrajectoryDrawByAttribute::Interval) {
122        filter->LoadIntervalElement(iter->first.first);
123      }
124      else if (iter->first.second == G4TrajectoryDrawByAttribute::SingleValue) {
125        filter->LoadSingleValueElement(iter->first.first);
126      }
127      iter++;
128    }
129  }
130 
131  // Get attribute value
132  G4AttValue attVal;
133
134  // Expect value to exist
135  if (!G4AttUtils::ExtractAttValue(object, fAttName, attVal)) {
136    static G4bool warnedUnableToExtract = false;
137    if (!warnedUnableToExtract) {
138      std::ostringstream o;
139      o <<"Unable to extract attribute value named "<<fAttName;
140      G4Exception
141        ("G4TrajectoryDrawByAttribute::Draw", "InvalidAttributeValue", JustWarning, o.str().c_str());
142        G4cout << "Available attributes:\n"
143               << object.GetAttDefs();
144      warnedUnableToExtract = true;
145    }
146      return;
147  }
148 
149  G4VisTrajContext myContext(GetContext());
150  G4String key;
151
152  // If attribute value passes filter, get corresponding interval/single value
153  // key loaded into G4AttValue filter.
154  if (filter->GetValidElement(attVal, key)) {
155
156    // Extract context corresponding to valid key.
157    // Single value match should have overriden interval match.
158    ContextMap::const_iterator iter = fContextMap.begin();
159
160    G4bool gotContext(false);
161
162    while (!gotContext && (iter != fContextMap.end())) {
163      if (iter->first.first == key) {
164        myContext = *(iter->second);
165        gotContext = true;
166      }
167      iter++;
168    }
169
170    assert (gotContext);
171  }
172
173  myContext.SetVisible(visible);
174 
175  if (GetVerbose()) {
176    G4cout<<"G4TrajectoryDrawByAttribute drawer named "<<Name();
177    G4cout<<", drawing style selected according to value of attribute "<<fAttName;
178    G4cout<<" : "<<attVal.GetValue()<<".  Selected context:"<<G4endl;
179    myContext.Print(G4cout);
180  }
181
182  // Draw the trajectory
183  G4TrajectoryDrawerUtils::DrawLineAndPoints(object, myContext);
184}
185
186void
187G4TrajectoryDrawByAttribute::Print(std::ostream& ostr) const
188{
189  ostr<<"G4TrajectoryDrawByAttribute, dumping configuration for model named "<< Name() <<":"<<std::endl;;
190
191  ostr<<"Default configuration:"<<G4endl;
192  GetContext().Print(ostr);
193
194  ostr<<"\nAttribute name "<<fAttName<<std::endl;
195  ostr<<"\nKey<->Context map dump:"<<std::endl;
196
197  ContextMap::const_iterator iter = fContextMap.begin();
198
199  while (iter != fContextMap.end()) {
200    ostr<<"Context for key "<<iter->first.first<<":"<<std::endl;
201    iter->second->Print(ostr);
202   
203    iter++;
204  }
205}
206
207void
208G4TrajectoryDrawByAttribute::Set(const G4String& name)
209{
210  fAttName = name;
211}
212
213void
214G4TrajectoryDrawByAttribute::AddIntervalContext(const G4String& name, G4VisTrajContext* context)
215{
216  // Takes ownership of context
217  std::pair<G4String, Config> myPair(name, G4TrajectoryDrawByAttribute::Interval);
218
219  ContextMap::iterator iter = fContextMap.find(myPair);
220 
221  if (iter != fContextMap.end()) {
222    std::ostringstream o;
223    o <<"Interval "<< name <<" already exists"<<std::endl;
224    G4Exception
225      ("G4TrajectoryDrawByAttribute::AddIntervalContext", "InvalidInterval", FatalErrorInArgument, o.str().c_str());
226  }
227
228  fContextMap[myPair] = context;
229}
230
231void
232G4TrajectoryDrawByAttribute::AddValueContext(const G4String& name, G4VisTrajContext* context)
233{
234  // Takes ownership of context
235  std::pair<G4String, Config> myPair(name, G4TrajectoryDrawByAttribute::SingleValue);
236
237  ContextMap::iterator iter = fContextMap.find(myPair);
238 
239  if (iter != fContextMap.end()) {
240    std::ostringstream o;
241    o <<"Single value "<< name <<" already exists"<<std::endl;
242    G4Exception
243      ("G4TrajectoryDrawByAttribute::AddSingleValueContext", "InvalidInterval", FatalErrorInArgument, o.str().c_str());
244  }
245
246  fContextMap[myPair] = context;
247}
Note: See TracBrowser for help on using the repository browser.