/** \file
 *
 *  Contains the Float3 class implementation.
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 *
 */

#include "Float3.h"
#include "io/OutStream.h"
#include <math.h>

/// Constructor for non-array storage
Float3::Float3( const Unit& unit, float value, bool scaleValue )
    : Float( unit, value, scaleValue )
{
    typeSize_ = 3;
    binaryType_ = FLOAT3;
}

/// Constructor from binary
Float3::Float3( const Unit& unit, const void *binary )
    : Float( unit, 0, false )
{
    typeSize_ = 3;
    binaryType_ = FLOAT3;
    fromBinary( binary );
}

/// Return a pointer to a new copy
Float3* Float3::copy() const
{
    return new Float3( *unit_, value_, false );
}

/// A quick equality check used to see if value has changed enough
/// to matter in logging (useful for reduced resolution data storage
bool Float3::equalsForStorage( const float& compareTo ) const
{
    return ( *( unsigned int* )&value_ & 0xFFFFFF00 ) == ( *( unsigned int* )&compareTo & 0xFFFFFF00 )
           || ( isnan( value_ ) && isnan( compareTo ) );
}

/// Send the data value to the indicated stream.
/// Return the number of bytes written.
/// For now, assume little-endian.
unsigned int Float3::toStream( OutStream& outStream ) const
{
    return outStream.writeFloat3( value_ ).bytesWritten();
}

void Float3::fromBinary( const void *buffer )
{
    value_ = 0;
    memcpy( ( ( unsigned char * ) & value_ ) + 1, ( const void * ) buffer, typeSize_ );
}

/// Protected Empty constructor for use bya DataEntry class
Float3::Float3()
    : Float()
{
    typeSize_ = 3;
    binaryType_ = FLOAT3;
}
