16 std::map<std::string, unsigned char> BHepRepWriter::tags;
17 std::map<std::string, unsigned char> BHepRepWriter::attributes;
18 std::map<std::string, unsigned char> BHepRepWriter::values;
23 singlePrecision(true) {
26 union {
long l;
char c[
sizeof (long)]; } u;
28 isBigEndian = (u.c[
sizeof (long) - 1] == 1);
32 if (tags.size() <= 0) {
34 tags[
"heprep"] = 0x05;
35 tags[
"attdef"] = 0x06;
36 tags[
"attvalue"] = 0x07;
37 tags[
"instance"] = 0x08;
38 tags[
"treeid"] = 0x09;
39 tags[
"action"] = 0x0a;
40 tags[
"instancetree"] = 0x0b;
42 tags[
"typetree"] = 0x0d;
47 if (attributes.size() <= 0) {
49 attributes[
"version"] = 0x05;
50 attributes[
"xmlns"] = 0x06;
51 attributes[
"xmlns:xsi"] = 0x07;
52 attributes[
"xsi:schemaLocation"] = 0x08;
54 attributes[
"valueString"] = 0x10;
55 attributes[
"valueColor"] = 0x11;
56 attributes[
"valueLong"] = 0x12;
57 attributes[
"valueInt"] = 0x13;
58 attributes[
"valueBoolean"] = 0x14;
59 attributes[
"valueDouble"] = 0x15;
61 attributes[
"name"] = 0x20;
62 attributes[
"type"] = 0x22;
63 attributes[
"showlabel"] = 0x23;
64 attributes[
"desc"] = 0x24;
65 attributes[
"category"] = 0x25;
66 attributes[
"extra"] = 0x26;
67 attributes[
"x"] = 0x27;
68 attributes[
"y"] = 0x28;
69 attributes[
"z"] = 0x29;
70 attributes[
"qualifier"] = 0x2a;
71 attributes[
"expression"] = 0x2b;
72 attributes[
"typetreename"] = 0x2c;
73 attributes[
"typetreeversion"] = 0x2d;
74 attributes[
"order"] = 0x2e;
77 attributes[
"eof"] = 0x7f;
80 if (values.size() <= 0) {
82 values[
"drawas"] = 0x85;
83 values[
"drawasoptions"] = 0x86;
84 values[
"visibility"] = 0x87;
86 values[
"label"] = 0x88;
88 values[
"fontname"] = 0x89;
89 values[
"fontstyle"] = 0x8a;
90 values[
"fontsize"] = 0x8b;
91 values[
"fontcolor"] = 0x8c;
92 values[
"fonthasframe"] = 0x8d;
93 values[
"fontframecolor"] = 0x8e;
94 values[
"fontframewidth"] = 0x8f;
95 values[
"fonthasbanner"] = 0x90;
96 values[
"fontbannercolor"] = 0x91;
98 values[
"color"] = 0x92;
99 values[
"framecolor"] = 0x93;
100 values[
"layer"] = 0x94;
101 values[
"markname"] = 0x95;
102 values[
"marksize"] = 0x96;
103 values[
"marksizemultiplier"] = 0x97;
104 values[
"marktype"] = 0x98;
105 values[
"hasframe"] = 0x99;
106 values[
"framecolor"] = 0x9a;
107 values[
"framewidth"] = 0x9b;
109 values[
"linestyle"] = 0x9c;
110 values[
"linewidth"] = 0x9d;
111 values[
"linewidthmultiplier"] = 0x9e;
112 values[
"linehasarrow"] = 0x9f;
114 values[
"fillcolor"] = 0xa0;
115 values[
"filltype"] = 0xa1;
116 values[
"fill"] = 0xa2;
118 values[
"radius"] = 0xa3;
119 values[
"phi"] = 0xa4;
120 values[
"theta"] = 0xa5;
121 values[
"omega"] = 0xa6;
122 values[
"radius1"] = 0xa7;
123 values[
"radius2"] = 0xa8;
124 values[
"radius3"] = 0xa9;
125 values[
"curvature"] = 0xaa;
126 values[
"flylength"] = 0xab;
127 values[
"faces"] = 0xac;
129 values[
"text"] = 0xad;
130 values[
"hpos"] = 0xae;
131 values[
"vpos"] = 0xaf;
132 values[
"halign"] = 0xb0;
133 values[
"valign"] = 0xb1;
135 values[
"ispickable"] = 0xb2;
136 values[
"showparentvalues"] = 0xb3;
137 values[
"pickparent"] = 0xb4;
140 values[
"false"] = 0xd0;
141 values[
"true"] = 0xd1;
143 values[
"point"] = 0xd2;
144 values[
"line"] = 0xd3;
145 values[
"helix"] = 0xd4;
146 values[
"polygon"] = 0xd5;
147 values[
"circle"] = 0xd6;
148 values[
"curve"] = 0xd7;
149 values[
"ellipse"] = 0xd8;
150 values[
"ellipsoid"] = 0xd9;
151 values[
"prism"] = 0xda;
152 values[
"cylinder"] = 0xdb;
153 values[
"ellipseprism"] = 0xdc;
154 values[
"text"] = 0xdd;
156 values[
"nonzero"] = 0xde;
157 values[
"evenodd"] = 0xdf;
159 values[
"circle"] = 0xe0;
160 values[
"box"] = 0xe1;
161 values[
"uptriangle"] = 0xe2;
162 values[
"dntriangle"] = 0xe3;
163 values[
"diamond"] = 0xe4;
164 values[
"cross"] = 0xe5;
165 values[
"star"] = 0xe6;
166 values[
"plus"] = 0xe7;
167 values[
"hline"] = 0xe8;
168 values[
"vline"] = 0xe9;
170 values[
"solid"] = 0xea;
171 values[
"dotted"] = 0xeb;
172 values[
"dashed"] = 0xec;
173 values[
"dotdash"] = 0xed;
175 values[
"none"] = 0xee;
176 values[
"start"] = 0xef;
177 values[
"end"] = 0xf0;
178 values[
"both"] = 0xf1;
180 values[
"serif"] = 0xf2;
181 values[
"sansserif"] = 0xf3;
182 values[
"monotype"] = 0xf4;
183 values[
"symbol"] = 0xf5;
185 values[
"plain"] = 0xf6;
186 values[
"bold"] = 0xf7;
187 values[
"italic"] = 0xf8;
189 values[
"top"] = 0xf9;
190 values[
"baseline"] = 0xfa;
191 values[
"center"] = 0xfb;
192 values[
"bottom"] = 0xfc;
194 values[
"left"] = 0xfd;
195 values[
"right"] = 0xfe;
197 values[
"default"] = 0xff;
208 stringValues.clear();
211 writeByte(WBXML_VERSION);
212 writeMultiByteInt(UNKNOWN_PID);
213 writeMultiByteInt(UTF8);
215 version =
"BinaryHepRep/1.0";
218 writeMultiByteInt(version.length()+1);
221 writeString(version);
227 writeByte(attributes[
"eof"]);
232 writeTag(name,
true);
244 void BHepRepWriter::writeTag(std::string tagName,
bool hasContent) {
245 std::string s = tagName;
246 std::transform(s.begin(), s.end(), s.begin(), (
int(*)(
int)) tolower);
249 if (tags.count(s) <= 0) {
250 std::cerr <<
"Cannot find tag '" << s <<
"' in tags table." << std::endl;
255 bool isPoint = (s ==
"point");
256 bool hasAttributes = (stringAttributes.size() > 0) || (doubleAttributes.size() > (
unsigned int)(isPoint ? 3 : 0));
258 if (!hasAttributes && isPoint) {
260 points.push_back(doubleAttributes[
"x"]);
261 points.push_back(doubleAttributes[
"y"]);
262 points.push_back(doubleAttributes[
"z"]);
267 writeByte(tags[s] | ((hasContent || isPoint) ? CONTENT : 0x00) | (hasAttributes ?
ATTRIBUTE : 0x00));
272 for (std::map<std::string,std::string>::iterator i = stringAttributes.begin(); i != stringAttributes.end(); i++) {
273 std::string
name = i->first;
274 std::string value = i->second;
277 writeByte(attributes[name]);
278 std::string v = value;
279 std::transform(v.begin(), v.end(), v.begin(), (
int(*)(
int)) tolower);
280 if (values.count(v) > 0) {
282 writeByte(values[v]);
284 if (stringValues.count(value) <= 0) {
286 writeStringDefine(value);
287 int index = stringValues.size();
288 stringValues[value] = index;
292 writeMultiByteInt(stringValues[value]);
296 stringAttributes.clear();
299 for (std::map<std::string,std::vector<double> >::iterator i = colorAttributes.begin(); i != colorAttributes.end(); i++) {
300 std::string
name = i->first;
301 std::vector<double> value = i->second;
303 writeByte(attributes[name]);
306 writeMultiByteInt(value.size());
307 writeByte((
int)(value[0] * 0xff) & 0xff);
308 writeByte((
int)(value[1] * 0xff) & 0xff);
309 writeByte((
int)(value[2] * 0xff) & 0xff);
310 if (value.size() > 3) writeByte((
int)(value[3] * 0xff) & 0xff);
312 colorAttributes.clear();
315 for (std::map<std::string,int64>::iterator i = longAttributes.begin(); i != longAttributes.end(); i++) {
316 std::string
name = i->first;
317 int64 value = i->second;
319 writeByte(attributes[name]);
322 writeMultiByteInt(8);
325 longAttributes.clear();
328 for (std::map<std::string,int>::iterator i = intAttributes.begin(); i != intAttributes.end(); i++) {
329 std::string
name = i->first;
330 int value = i->second;
332 writeByte(attributes[name]);
335 writeMultiByteInt(4);
338 intAttributes.clear();
341 for (std::map<std::string,bool>::iterator i = booleanAttributes.begin(); i != booleanAttributes.end(); i++) {
342 std::string
name = i->first;
343 bool value = i->second;
345 writeByte(attributes[name]);
347 writeByte(value ? values[
"true"] : values[
"false"]);
349 booleanAttributes.clear();
352 for (std::map<std::string,double>::iterator i = doubleAttributes.begin(); i != doubleAttributes.end(); i++) {
353 std::string
name = i->first;
354 double value = i->second;
355 if (!isPoint && (name !=
"x") && (name !=
"y") && (name !=
"z")) {
357 writeByte(attributes[name]);
360 writeMultiByteInt(singlePrecision ? 4 : 8);
364 doubleAttributes.clear();
372 writeMultiByteInt(singlePrecision ? 12 : 24);
373 writeReal(doubleAttributes[
"x"]);
374 writeReal(doubleAttributes[
"y"]);
375 writeReal(doubleAttributes[
"z"]);
378 if (isPoint && !hasContent) {
384 void BHepRepWriter::writePoints() {
385 if (points.size() <= 0)
return;
387 writeByte(tags[
"point"] | CONTENT);
389 writeMultiByteInt(points.size()*(singlePrecision ? 4 : 8));
390 for (std::vector<double>::iterator i = points.begin(); i != points.end(); ) {
405 if (name ==
"value") name = name.append(
"String");
408 if (attributes.count(name) <= 0) {
409 std::cerr <<
"Cannot find attribute name '" << name <<
"' in attributes table, skipped." << std::endl;
413 stringAttributes[name] = value;
417 if (name ==
"value") name = name.append(
"Color");
420 if (attributes.count(name) <= 0) {
421 std::cerr <<
"Cannot find attribute name '" << name <<
"' in attributes table, skipped." << std::endl;
425 colorAttributes[name] = value;
429 if (name ==
"value") name = name.append(
"Long");
432 if (attributes.count(name) <= 0) {
433 std::cerr <<
"Cannot find attribute name '" << name <<
"' in attributes table, skipped." << std::endl;
437 longAttributes[name] = value;
441 if (name ==
"value") name = name.append(
"Int");
444 if (attributes.count(name) <= 0) {
445 std::cerr <<
"Cannot find attribute name '" << name <<
"' in attributes table, skipped." << std::endl;
449 intAttributes[name] = value;
453 if (name ==
"value") name = name.append(
"Boolean");
456 if (attributes.count(name) <= 0) {
457 std::cerr <<
"Cannot find attribute name '" << name <<
"' in attributes table, skipped." << std::endl;
461 booleanAttributes[name] = value;
465 if (name ==
"value") name = name.append(
"Double");
468 if (attributes.count(name) <= 0) {
469 std::cerr <<
"Cannot find attribute name '" << name <<
"' in attributes table, skipped." << std::endl;
473 doubleAttributes[name] = value;
476 void BHepRepWriter::writeStringDefine(std::string s) {
481 void BHepRepWriter::writeMultiByteInt(
unsigned int ui) {
482 unsigned char buf[5];
486 buf[idx++] = (
unsigned char) (ui & 0x7f);
492 writeByte(buf[--idx] | 0x80);
497 void BHepRepWriter::writeReal(
double d) {
498 if (singlePrecision) {
516 void BHepRepWriter::writeLong(
int64 i) {
518 os.put((i >> 56) & 0xff);
519 os.put((i >> 48) & 0xff);
520 os.put((i >> 40) & 0xff);
521 os.put((i >> 32) & 0xff);
522 os.put((i >> 24) & 0xff);
523 os.put((i >> 16) & 0xff);
524 os.put((i >> 8) & 0xff);
528 void BHepRepWriter::writeInt(
int i) {
530 os.put((i >> 24) & 0xff);
531 os.put((i >> 16) & 0xff);
532 os.put((i >> 8) & 0xff);
536 void BHepRepWriter::writeByte(
unsigned char b) {
540 void BHepRepWriter::writeString(std::string s) {
void setAttribute(std::string name, char *value)
void openTag(std::string name)
void printTag(std::string name)
void openDoc(std::string version="BinaryHepRep/1.0", std::string encoding="UTF-8", bool standalone=false)
BHepRepWriter(std::ostream &os)
void closeDoc(bool force=false)
const char * name(G4int ptype)