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

Last change on this file since 954 was 954, checked in by garnier, 15 years ago

remise a jour

File size: 7.2 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.3 2006/09/14 15:41:42 tinslay 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, const G4int&, 
65                                  const G4bool& visible) const
66{
67  // Return if attribute name has not been set. Just print one warning
68  if (fAttName.isNull()) {
69
70    if (!fWarnedMissingAttribute) {
71      std::ostringstream o;
72      o<<"Null attribute name";
73      G4Exception("G4TrajectoryDrawByAttribute::Draw", "NullAttributeName", JustWarning, o.str().c_str());
74     
75      fWarnedMissingAttribute = true;
76    }
77   
78    return;
79  }
80 
81  // Basically cache data loaded filter for efficiency
82  if (fFirst) {
83   
84    fFirst = false;
85   
86    // Get attribute definition
87    G4AttDef attDef;
88   
89    // Expect definition to exist   
90    if (!G4AttUtils::ExtractAttDef(object, fAttName, attDef)) {
91      std::ostringstream o;
92      o <<"Unable to extract attribute definition named "<<fAttName;
93      G4Exception
94        ("G4TrajectoryDrawByAttribute::Draw", "InvalidAttributeDefinition", FatalErrorInArgument, o.str().c_str());
95    }
96   
97    // Get new G4AttValue filter
98    filter = G4AttFilterUtils::GetNewFilter(attDef);
99    assert (0 != filter);
100   
101    // Load both interval and single valued data. Single valued data should
102    // override interval data.
103    ContextMap::const_iterator iter = fContextMap.begin();
104   
105    while (iter != fContextMap.end()) {
106      if (iter->first.second == G4TrajectoryDrawByAttribute::Interval) {
107        filter->LoadIntervalElement(iter->first.first);
108      }
109      else if (iter->first.second == G4TrajectoryDrawByAttribute::SingleValue) {
110        filter->LoadSingleValueElement(iter->first.first);
111      }
112      iter++;
113    }
114  }
115 
116  // Get attribute value
117  G4AttValue attVal;
118
119  // Expect value to exist
120  if (!G4AttUtils::ExtractAttValue(object, fAttName, attVal)) {
121      std::ostringstream o;
122      o <<"Unable to extract attribute value named "<<fAttName;
123      G4Exception
124        ("G4TrajectoryDrawByAttribute::Draw", "InvalidAttributeValue", FatalErrorInArgument, o.str().c_str());
125  }
126 
127  G4VisTrajContext myContext(GetContext());
128  G4String key;
129
130  // If attribute value passes filter, get corresponding interval/single value
131  // key loaded into G4AttValue filter.
132  if (filter->GetValidElement(attVal, key)) {
133
134    // Extract context corresponding to valid key.
135    // Single value match should have overriden interval match.
136    ContextMap::const_iterator iter = fContextMap.begin();
137
138    G4bool gotContext(false);
139
140    while (!gotContext && (iter != fContextMap.end())) {
141      if (iter->first.first == key) {
142        myContext = *(iter->second);
143        gotContext = true;
144      }
145      iter++;
146    }
147
148    assert (gotContext);
149  }
150
151  myContext.SetVisible(visible);
152 
153  if (GetVerbose()) {
154    G4cout<<"G4TrajectoryDrawByAttribute drawer named "<<Name();
155    G4cout<<", drawing style selected according to value of attribute "<<fAttName;
156    G4cout<<" : "<<attVal.GetValue()<<".  Selected context:"<<G4endl;
157    myContext.Print(G4cout);
158  }
159
160  // Draw the trajectory
161  G4TrajectoryDrawerUtils::DrawLineAndPoints(object, myContext);
162}
163
164void
165G4TrajectoryDrawByAttribute::Print(std::ostream& ostr) const
166{
167  ostr<<"G4TrajectoryDrawByAttribute, dumping configuration for model named "<< Name() <<":"<<std::endl;;
168
169  ostr<<"Default configuration:"<<G4endl;
170  GetContext().Print(ostr);
171
172  ostr<<"\nAttribute name "<<fAttName<<std::endl;
173  ostr<<"\nKey<->Context map dump:"<<std::endl;
174
175  ContextMap::const_iterator iter = fContextMap.begin();
176
177  while (iter != fContextMap.end()) {
178    ostr<<"Context for key "<<iter->first.first<<":"<<std::endl;
179    iter->second->Print(ostr);
180   
181    iter++;
182  }
183}
184
185void
186G4TrajectoryDrawByAttribute::Set(const G4String& name)
187{
188  fAttName = name;
189}
190
191void
192G4TrajectoryDrawByAttribute::AddIntervalContext(const G4String& name, G4VisTrajContext* context)
193{
194  // Takes ownership of context
195  std::pair<G4String, Config> myPair(name, G4TrajectoryDrawByAttribute::Interval);
196
197  ContextMap::iterator iter = fContextMap.find(myPair);
198 
199  if (iter != fContextMap.end()) {
200    std::ostringstream o;
201    o <<"Interval "<< name <<" already exists"<<std::endl;
202    G4Exception
203      ("G4TrajectoryDrawByAttribute::AddIntervalContext", "InvalidInterval", FatalErrorInArgument, o.str().c_str());
204  }
205
206  fContextMap[myPair] = context;
207}
208
209void
210G4TrajectoryDrawByAttribute::AddValueContext(const G4String& name, G4VisTrajContext* context)
211{
212  // Takes ownership of context
213  std::pair<G4String, Config> myPair(name, G4TrajectoryDrawByAttribute::SingleValue);
214
215  ContextMap::iterator iter = fContextMap.find(myPair);
216 
217  if (iter != fContextMap.end()) {
218    std::ostringstream o;
219    o <<"Single value "<< name <<" already exists"<<std::endl;
220    G4Exception
221      ("G4TrajectoryDrawByAttribute::AddSingleValueContext", "InvalidInterval", FatalErrorInArgument, o.str().c_str());
222  }
223
224  fContextMap[myPair] = context;
225}
Note: See TracBrowser for help on using the repository browser.