Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tpia_frame.cc
Go to the documentation of this file.
1 /*
2 # <<BEGIN-copyright>>
3 # Copyright (c) 2010, Lawrence Livermore National Security, LLC.
4 # Produced at the Lawrence Livermore National Laboratory
5 # Written by Bret R. Beck, beck6@llnl.gov.
6 # CODE-461393
7 # All rights reserved.
8 #
9 # This file is part of GIDI. For details, see nuclear.llnl.gov.
10 # Please also read the "Additional BSD Notice" at nuclear.llnl.gov.
11 #
12 # Redistribution and use in source and binary forms, with or without modification,
13 # are permitted provided that the following conditions are met:
14 #
15 # 1) Redistributions of source code must retain the above copyright notice,
16 # this list of conditions and the disclaimer below.
17 # 2) Redistributions in binary form must reproduce the above copyright notice,
18 # this list of conditions and the disclaimer (as noted below) in the
19 # documentation and/or other materials provided with the distribution.
20 # 3) Neither the name of the LLNS/LLNL nor the names of its contributors may be
21 # used to endorse or promote products derived from this software without
22 # specific prior written permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
25 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 # SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR
28 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 # <<END-copyright>>
35 */
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <tpia_target.h>
40 #include <tpia_misc.h>
41 
42 #if defined __cplusplus
43 namespace GIDI {
44 using namespace GIDI;
45 #endif
46 
47 static int nlab_Str = 0, nCOM_Str = 0;
48 static char lab_Str[] = "lab", COM_Str[] = "centerOfMass";
49 /*
50 ************************************************************
51 */
52 //int tpia_frame_clear( statusMessageReporting *smr, tpia_data_frame *frame ) {
54 
55  frame->frames = 0;
56  return( 0 );
57 }
58 /*
59 ************************************************************
60 */
61 int tpia_frame_setFromElement( statusMessageReporting *smr, xData_element *element, int dimension, tpia_data_frame *frame ) {
62 
63  char const *value = xData_getAttributesValueInElement( element, "frame" );
64 
65  if( value == NULL ) {
66  tpia_misc_setMessageError_Element( smr, NULL, element, __FILE__, __LINE__, 1, "element is missing frame attribute" );
67  return( 1 );
68  }
69  return( tpia_frame_setFromString( smr, element->fullName, value, dimension, frame ) );
70 }
71 /*
72 ************************************************************
73 */
74 int tpia_frame_setFromString( statusMessageReporting *smr, const char *forItem, const char *value, int dimension, tpia_data_frame *frame ) {
75 
76  char const *e;
77  int status = 1, i;
78 
79  for( i = 0; i < tpia_maxNumberOfFrames; i++ ) tpia_frame_setColumn( smr, frame, i, tpia_referenceFrame_None );
80  if( dimension > tpia_maxNumberOfFrames ) {
81  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "dimension argument = %d is greater than tpia_maxNumberOfFrames = %d", dimension,
82  tpia_maxNumberOfFrames );
83  return( status );
84  }
85  for( i = 0, e = value; ( i < dimension ) && ( *e != 0 ); i++ ) {
86  if( strstr( e, lab_Str ) == e ) {
88  e += 3; }
89  else if( strstr( e, COM_Str ) == e ) {
91  e += 12; }
92  else {
93  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad frame '%s' for %s", value, forItem );
94  break;
95  }
96  if( *e != 0 ) {
97  if( *e != ',' ) {
98  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad separater for frame '%s' for %s", value, forItem );
99  break;
100  }
101  e++;
102  }
103  }
104  if( smr_isOk( smr ) ) {
105  if( i == dimension ) {
106  if( *e == 0 ) {
107  status = 0; }
108  else {
109  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "extra values for frame '%s' for %s", value, forItem );
110  } }
111  else {
112  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "missing values for frame '%s' for %s", value, forItem );
113  }
114  }
115  return( status );
116 }
117 /*
118 ************************************************************
119 */
120 //int tpia_frame_getDimensions( statusMessageReporting *smr, tpia_data_frame *frame ) {
122 
123  int i, dimension = 0;
124  unsigned value = frame->frames;
125 
126  for( i = 0; i < tpia_maxNumberOfFrames; i++ ) {
127  if( ( value & 3 ) == 0 ) break;
128  dimension++;
129  value = value >> 2;
130  }
131  return( dimension );
132 }
133 /*
134 ************************************************************
135 */
136 //char *tpia_frame_toString( statusMessageReporting *smr, const char *fromItem, tpia_data_frame *frame ) {
137 char *tpia_frame_toString( statusMessageReporting *smr, const char *, tpia_data_frame *frame ) {
138 
139  int i, n = tpia_frame_getDimensions( smr, frame ), value, nStr = 0;
140  char *str = NULL, *p;
141 
142  if( nlab_Str == 0 ) {
143  nlab_Str = strlen( lab_Str );
144  nCOM_Str = strlen( COM_Str );
145  }
146  for( i = 0; i < n; i++ ) {
147  value = tpia_frame_getColumn( smr, frame, i );
148  if( value == tpia_referenceFrame_COM ) {
149  nStr += nCOM_Str + 1; }
150  else if( value == tpia_referenceFrame_lab ) {
151  nStr += nlab_Str + 1; }
152  else {
153  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad frame value = %d for column = %d", value, i );
154  return( NULL );
155  }
156  }
157  if( nStr == 0 ) nStr = 1;
158  //if( ( str = xData_malloc2( smr, nStr, 1, "str" ) ) == NULL ) return( NULL );
159  if( ( str = (char*) xData_malloc2( smr, nStr, 1, "str" ) ) == NULL ) return( NULL );
160  for( i = 0, p = str - 1; i < n; i++ ) {
161  p++;
162  value = tpia_frame_getColumn( smr, frame, i );
163  if( value == tpia_referenceFrame_COM ) {
164  strcpy( p, COM_Str );
165  p += nCOM_Str; }
166  else {
167  strcpy( p, lab_Str );
168  p += nlab_Str;
169  }
170  *p = ',';
171  }
172  *p = 0;
173  return( str );
174 }
175 /*
176 ************************************************************
177 */
178 int tpia_frame_setColumns( statusMessageReporting *smr, tpia_data_frame *frame, int nColumns, int *values ) {
179 
180  int i;
181 
182  tpia_frame_clear( smr, frame );
183  for( i = 0; i < nColumns; i++ ) if( tpia_frame_setColumn( smr, frame, i, values[i] ) != 0 ) return( 1 );
184 
185  return( 0 );
186 }
187 /*
188 ************************************************************
189 */
190 int tpia_frame_setColumn( statusMessageReporting *smr, tpia_data_frame *frame, int column, int value ) {
191 
192  int i;
193 
194  if( ( column < 0 ) || ( column >= tpia_maxNumberOfFrames ) ) {
195  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad column = %d value for setting frame (0 <= column < %d)", column, tpia_maxNumberOfFrames );
196  return( 1 ); }
197  else {
198  if( ( value < tpia_referenceFrame_None ) || ( value > tpia_referenceFrame_Max ) ) {
199  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad frame value = %d for column = %d", value, column );
200  return( 1 ); }
201  else {
202  i = 3 << ( 2 * column );
203  frame->frames = frame->frames & (~i);
204  value = value << ( 2 * column );
205  frame->frames = frame->frames | value;
206  }
207  }
208  return( 0 );
209 }
210 /*
211 ************************************************************
212 */
214 
215  unsigned int value;
216 
217  if( ( column < 0 ) || ( column >= tpia_maxNumberOfFrames ) ) {
218  smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad column = %d value for getting frame (0 <= column < %d)", column, tpia_maxNumberOfFrames );
219  return( -1 ); }
220  else {
221  value = frame->frames >> ( 2 * column );
222  }
223  return( (int) ( value & 3 ) );
224 }
225 
226 #if defined __cplusplus
227 }
228 #endif