tpia_frame.cc

Go to the documentation of this file.
00001 /*
00002 # <<BEGIN-copyright>>
00003 # Copyright (c) 2010, Lawrence Livermore National Security, LLC. 
00004 # Produced at the Lawrence Livermore National Laboratory 
00005 # Written by Bret R. Beck, beck6@llnl.gov. 
00006 # CODE-461393
00007 # All rights reserved. 
00008 #  
00009 # This file is part of GIDI. For details, see nuclear.llnl.gov. 
00010 # Please also read the "Additional BSD Notice" at nuclear.llnl.gov. 
00011 # 
00012 # Redistribution and use in source and binary forms, with or without modification, 
00013 # are permitted provided that the following conditions are met: 
00014 #
00015 #      1) Redistributions of source code must retain the above copyright notice, 
00016 #         this list of conditions and the disclaimer below.
00017 #      2) Redistributions in binary form must reproduce the above copyright notice, 
00018 #         this list of conditions and the disclaimer (as noted below) in the 
00019 #          documentation and/or other materials provided with the distribution.
00020 #      3) Neither the name of the LLNS/LLNL nor the names of its contributors may be 
00021 #         used to endorse or promote products derived from this software without 
00022 #         specific prior written permission. 
00023 #
00024 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
00025 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00026 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT 
00027 # SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR 
00028 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00029 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
00030 # OR SERVICES;  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
00031 # AND ON  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00032 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
00033 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00034 # <<END-copyright>>
00035 */
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <tpia_target.h>
00040 #include <tpia_misc.h>
00041 
00042 #if defined __cplusplus
00043 namespace GIDI {
00044 using namespace GIDI;
00045 #endif
00046 
00047 static int nlab_Str = 0, nCOM_Str = 0;
00048 static char lab_Str[] = "lab", COM_Str[] = "centerOfMass";
00049 /*
00050 ************************************************************
00051 */
00052 //int tpia_frame_clear( statusMessageReporting *smr, tpia_data_frame *frame ) {
00053 int tpia_frame_clear( statusMessageReporting *, tpia_data_frame *frame ) {
00054 
00055     frame->frames = 0;
00056     return( 0 );
00057 }
00058 /*
00059 ************************************************************
00060 */
00061 int tpia_frame_setFromElement( statusMessageReporting *smr, xData_element *element, int dimension, tpia_data_frame *frame ) {
00062 
00063     char const *value = xData_getAttributesValueInElement( element, "frame" );
00064 
00065     if( value == NULL ) {
00066         tpia_misc_setMessageError_Element( smr, NULL, element, __FILE__, __LINE__, 1, "element is missing frame attribute" );
00067         return( 1 );
00068     }
00069     return( tpia_frame_setFromString( smr, element->fullName, value, dimension, frame ) );
00070 }
00071 /*
00072 ************************************************************
00073 */
00074 int tpia_frame_setFromString( statusMessageReporting *smr, const char *forItem, const char *value, int dimension, tpia_data_frame *frame ) {
00075 
00076     char const *e;
00077     int status = 1, i;
00078 
00079     for( i = 0; i < tpia_maxNumberOfFrames; i++ ) tpia_frame_setColumn( smr, frame, i, tpia_referenceFrame_None );
00080     if( dimension > tpia_maxNumberOfFrames ) {
00081         smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "dimension argument = %d is greater than tpia_maxNumberOfFrames = %d", dimension,
00082             tpia_maxNumberOfFrames );
00083         return( status );
00084     }
00085     for( i = 0, e = value; ( i < dimension ) && ( *e != 0 ); i++ ) {
00086         if( strstr( e, lab_Str ) == e ) {
00087             tpia_frame_setColumn( smr, frame, i, tpia_referenceFrame_lab );
00088             e += 3; }
00089         else if( strstr( e, COM_Str ) == e ) {
00090             tpia_frame_setColumn( smr, frame, i, tpia_referenceFrame_COM );
00091             e += 12; }
00092         else {
00093             smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad frame '%s' for %s", value, forItem );
00094             break;
00095         }
00096         if( *e != 0 ) {
00097             if( *e != ',' ) {
00098                 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad separater for frame '%s' for %s", value, forItem );
00099                 break;
00100             }
00101             e++;
00102         }
00103     }
00104     if( smr_isOk( smr ) ) {
00105         if( i == dimension ) {
00106             if( *e == 0 ) {
00107                 status = 0; }
00108             else {
00109                 smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "extra values for frame '%s' for %s", value, forItem );
00110             } }
00111         else {
00112             smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "missing values for frame '%s' for %s", value, forItem );
00113         }
00114     }
00115     return( status );
00116 }
00117 /*
00118 ************************************************************
00119 */
00120 //int tpia_frame_getDimensions( statusMessageReporting *smr, tpia_data_frame *frame ) {
00121 int tpia_frame_getDimensions( statusMessageReporting *, tpia_data_frame *frame ) {
00122 
00123     int i, dimension = 0;
00124     unsigned value = frame->frames;
00125 
00126     for( i = 0; i < tpia_maxNumberOfFrames; i++ ) {
00127         if( ( value & 3 ) == 0 ) break;
00128         dimension++;
00129         value = value >> 2;
00130     }
00131     return( dimension );
00132 }
00133 /*
00134 ************************************************************
00135 */
00136 //char *tpia_frame_toString( statusMessageReporting *smr, const char *fromItem, tpia_data_frame *frame ) {
00137 char *tpia_frame_toString( statusMessageReporting *smr, const char *, tpia_data_frame *frame ) {
00138 
00139     int i, n = tpia_frame_getDimensions( smr, frame ), value, nStr = 0;
00140     char *str = NULL, *p;
00141 
00142     if( nlab_Str == 0 ) {
00143         nlab_Str = strlen( lab_Str );
00144         nCOM_Str = strlen( COM_Str );
00145     }
00146     for( i = 0; i < n; i++ ) {
00147         value = tpia_frame_getColumn( smr, frame, i );
00148         if( value == tpia_referenceFrame_COM ) {
00149             nStr += nCOM_Str + 1; }
00150         else if( value == tpia_referenceFrame_lab ) {
00151             nStr += nlab_Str + 1; }
00152         else {
00153             smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad frame value = %d for column = %d", value, i );
00154             return( NULL );
00155         }
00156     }
00157     if( nStr == 0 ) nStr = 1;
00158     //if( ( str = xData_malloc2( smr, nStr, 1, "str" ) ) == NULL ) return( NULL );
00159     if( ( str = (char*) xData_malloc2( smr, nStr, 1, "str" ) ) == NULL ) return( NULL );
00160     for( i = 0, p = str - 1; i < n; i++ ) {
00161         p++;
00162         value = tpia_frame_getColumn( smr, frame, i );
00163         if( value == tpia_referenceFrame_COM ) {
00164             strcpy( p, COM_Str );
00165             p += nCOM_Str; }
00166         else {
00167             strcpy( p, lab_Str );
00168             p += nlab_Str;
00169         }
00170         *p = ',';
00171     }
00172     *p = 0;
00173     return( str );
00174 }
00175 /*
00176 ************************************************************
00177 */
00178 int tpia_frame_setColumns( statusMessageReporting *smr, tpia_data_frame *frame, int nColumns, int *values ) {
00179 
00180     int i;
00181 
00182     tpia_frame_clear( smr, frame );
00183     for( i = 0; i < nColumns; i++ ) if( tpia_frame_setColumn( smr, frame, i, values[i] ) != 0 ) return( 1 );
00184 
00185     return( 0 );
00186 }
00187 /*
00188 ************************************************************
00189 */
00190 int tpia_frame_setColumn( statusMessageReporting *smr, tpia_data_frame *frame, int column, int value ) {
00191 
00192     int i;
00193 
00194     if( ( column < 0 ) || ( column >= tpia_maxNumberOfFrames ) ) {
00195         smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad column = %d value for setting frame (0 <= column < %d)", column, tpia_maxNumberOfFrames );
00196         return( 1 ); }
00197     else {
00198         if( ( value < tpia_referenceFrame_None ) || ( value > tpia_referenceFrame_Max ) ) {
00199             smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad frame value = %d for column = %d", value, column );
00200             return( 1 ); }
00201         else {
00202             i = 3 << ( 2 * column );
00203             frame->frames = frame->frames & (~i);
00204             value = value << ( 2 * column );
00205             frame->frames = frame->frames | value;
00206         }
00207     }
00208     return( 0 );
00209 }
00210 /*
00211 ************************************************************
00212 */
00213 int tpia_frame_getColumn( statusMessageReporting *smr, tpia_data_frame *frame, int column ) {
00214 
00215     unsigned int value;
00216 
00217     if( ( column < 0 ) || ( column >= tpia_maxNumberOfFrames ) ) {
00218         smr_setMessageError( smr, NULL, __FILE__, __LINE__, 1, "bad column = %d value for getting frame (0 <= column < %d)", column, tpia_maxNumberOfFrames );
00219         return( -1 ); }
00220     else {
00221         value = frame->frames >> ( 2 * column );
00222     }
00223     return( (int) ( value & 3 ) );
00224 }
00225 
00226 #if defined __cplusplus
00227 }
00228 #endif

Generated on Mon May 27 17:50:35 2013 for Geant4 by  doxygen 1.4.7