TernaryPoint.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "TernaryPoint.h"
00024 #include "TernaryConstants.h"
00025
00026 #include <limits>
00027
00028 #include <QChar>
00029 #include <QTextStream>
00030
00031 TernaryPoint::TernaryPoint()
00032 : m_a( -1.0 )
00033 , m_b( -1.0 )
00034 {
00035 Q_ASSERT( !isValid() );
00036 }
00037
00038 TernaryPoint::TernaryPoint( double a, double b )
00039 : m_a( -1.0 )
00040 , m_b( -1.0 )
00041 {
00042 set( a, b );
00043 }
00044
00045 void TernaryPoint::set( double a, double b )
00046 {
00047 if ( a >= 0.0 && a <= 1.0
00048 && b >= 0.0 && b <= 1.0
00049 && 1.0 - a - b >= -2.0 * std::numeric_limits<double>::epsilon() ) {
00050 m_a = a;
00051 m_b = b;
00052 Q_ASSERT( isValid() );
00053 } else {
00054 m_a = -1.0;
00055 m_b = -1.0;
00056 Q_ASSERT( ! isValid() );
00057 }
00058 }
00059
00060 bool TernaryPoint::isValid() const
00061 {
00062 return
00063 m_a >= 0.0 && m_a <= 1.0
00064 && m_b >= 0.0 && m_b <= 1.0
00065 && 1.0 - m_a + m_b >= - std::numeric_limits<double>::epsilon();
00066 }
00067
00068 QDebug operator<<( QDebug stream, const TernaryPoint& point )
00069 {
00070 QString string;
00071 QTextStream text( &string );
00072 text << "[TernaryPoint: ";
00073 if ( point.isValid() ) {
00074 text.setFieldWidth( 2 );
00075 text.setPadChar( QLatin1Char( '0' ) );
00076 text << ( int ) ( point.a() * 100.0 ) << "%|"
00077 << ( int ) ( point.b() * 100.0 ) << "%|"
00078 << ( int ) ( point.c() * 100.0 ) << "%]";
00079 } else {
00080 text << "a=" << point.a() << " - b=" << point.b() << " - INVALID]";
00081 }
00082 stream << string;
00083 return stream;
00084 }
00085
00086 QPointF translate( const TernaryPoint& point )
00087 {
00088 if ( point.isValid() ) {
00089
00090
00091
00092
00093
00094
00095 QPointF bPosition( 1.0 - point.b(), 0.0 );
00096 QPointF aPosition( point.a() * AxisVector_C_A );
00097 QPointF result( bPosition + aPosition );
00098 return result;
00099 } else {
00100 qWarning() << "TernaryPoint::translate(TernaryPoint): cannot translate invalid ternary points:"
00101 << point;
00102 return QPointF();
00103 }
00104 }