1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
//*
//* v.cpp
//* NearTree
//*
//* Copyright 2001, 2008 Larry Andrews. All rights reserved
//* Revised 12 Dec 2008 for sourceforge release -- H. J. Bernstein
//**********************************************************************
//* *
//* YOU MAY REDISTRIBUTE NearTree UNDER THE TERMS OF THE LGPL *
//* *
//**********************************************************************/
//************************* LGPL NOTICES *******************************
//* *
//* This library is free software; you can redistribute it and/or *
//* modify it under the terms of the GNU Lesser General Public *
//* License as published by the Free Software Foundation; either *
//* version 2.1 of the License, or (at your option) any later version. *
//* *
//* This library is distributed in the hope that it will be useful, *
//* but WITHOUT ANY WARRANTY; without even the implied warranty of *
//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
//* Lesser General Public License for more details. *
//* *
//* You should have received a copy of the GNU Lesser General Public *
//* License along with this library; if not, write to the Free *
//* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
//* MA 02110-1301 USA *
//* *
//**********************************************************************/
// v.cpp: implementation of the v class.
//
//////////////////////////////////////////////////////////////////////
#include <float.h>
#include <math.h>
#include <ostream>
#include "v.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
v::v( )
{
m_bIsDefined = false;
m_dvec[0] = DBL_MAX;
m_dvec[1] = DBL_MAX;
m_dvec[2] = DBL_MAX;
}
v::~v( )
{
m_bIsDefined = false;
m_dvec[0] = DBL_MAX;
m_dvec[1] = DBL_MAX;
m_dvec[2] = DBL_MAX;
}
v::v( const double& da )
{
m_bIsDefined = true;
m_dvec[0] = da;
m_dvec[1] = da;
m_dvec[2] = da;
}
v::v( const double& da, const double& db, const double& dc )
{
m_bIsDefined = true;
m_dvec[0] = da;
m_dvec[1] = db;
m_dvec[2] = dc;
}
//////////////////////////////////////////////////////////////////////
// Other Stuff
//////////////////////////////////////////////////////////////////////
std::ostream& operator<< ( std::ostream& os, const v& v )
{
// output a vector
os << '{' <<v.m_dvec[0] << ',' << v.m_dvec[1] << ',' << v.m_dvec[2] <<'}' ;
return os;
}
v v::operator -(const v &vv) const
{
// subtract two vectors
v vReturn(
m_dvec[0]-vv.m_dvec[0],
m_dvec[1]-vv.m_dvec[1],
m_dvec[2]-vv.m_dvec[2] );
return ( vReturn );
}
v::operator double( ) const
{
// return the Euclidean (L2) length of the vector
return ( sqrt( m_dvec[0]*m_dvec[0] + m_dvec[1]*m_dvec[1] + m_dvec[2]*m_dvec[2] ) );
// city block measure (L1) return ( fabs(m_dvec[0]) + fabs(m_dvec[1]) + fabs(m_dvec[2]) );
// max value (L-infinity) return the largest of fabs of any element
// return ( fabs(m_dvec[0])>=fabs(m_dvec[1]) && fabs(m_dvec[0])>=fabs(m_dvec[2]) ? fabs(m_dvec[0]) : fabs(m_dvec[1])>=fabs(m_dvec[2]) ? fabs(m_dvec[1]) : fabs(m_dvec[2]) );
// Hamming measure (if this is a difference vector) return ( (m_dvec[0]==0 ? 0 : 1) + (m_dvec[1]==0 ? 0 : 1) + (m_dvec[2]==0 ? 0 : 1) )
}
double v::Norm( void ) const
{
return( sqrt( m_dvec[0]*m_dvec[0] + m_dvec[1]*m_dvec[1] + m_dvec[2]*m_dvec[2] ) );
}
|