00001 // **************************************************************************** 00002 // This file is part of the Integrating Vision Toolkit (IVT). 00003 // 00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT) 00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de). 00006 // 00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT). 00008 // All rights reserved. 00009 // 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are met: 00012 // 00013 // 1. Redistributions of source code must retain the above copyright 00014 // notice, this list of conditions and the following disclaimer. 00015 // 00016 // 2. Redistributions in binary form must reproduce the above copyright 00017 // notice, this list of conditions and the following disclaimer in the 00018 // documentation and/or other materials provided with the distribution. 00019 // 00020 // 3. Neither the name of the KIT nor the names of its contributors may be 00021 // used to endorse or promote products derived from this software 00022 // without specific prior written permission. 00023 // 00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY 00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY 00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00031 // 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 00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 // **************************************************************************** 00035 // **************************************************************************** 00036 // Filename: Vecd.cpp 00037 // Author: Pedram Azad 00038 // Date: 2004 00039 // **************************************************************************** 00040 00041 00042 // **************************************************************************** 00043 // Includes 00044 // **************************************************************************** 00045 00046 #include <new> // for explicitly using correct new/delete operators on VC DSPs 00047 00048 #include "Vecd.h" 00049 #include <string.h> 00050 #include <math.h> 00051 00052 00053 00054 // **************************************************************************** 00055 // Constructors / Destructor 00056 // **************************************************************************** 00057 00058 CVecd::CVecd() 00059 { 00060 m_pElements = 0; 00061 m_nSize = 0; 00062 } 00063 00064 CVecd::CVecd(int nSize) 00065 { 00066 m_pElements = 0; 00067 00068 SetSize(nSize); 00069 } 00070 00071 CVecd::CVecd(double dX1, double dX2) 00072 { 00073 m_pElements = 0; 00074 00075 SetSize(2); 00076 00077 m_pElements[0] = dX1; 00078 m_pElements[1] = dX2; 00079 } 00080 00081 CVecd::CVecd(const CVecd &v) 00082 { 00083 m_pElements = 0; 00084 00085 SetSize(v.m_nSize); 00086 00087 memcpy(m_pElements, v.m_pElements, m_nSize * sizeof(double)); 00088 } 00089 00090 CVecd::~CVecd() 00091 { 00092 if (m_pElements) 00093 delete [] m_pElements; 00094 } 00095 00096 00097 // **************************************************************************** 00098 // Operators 00099 // **************************************************************************** 00100 00101 CVecd& CVecd::operator= (const CVecd &v) 00102 { 00103 SetSize(v.GetSize()); 00104 00105 memcpy(m_pElements, v.m_pElements, m_nSize * sizeof(double)); 00106 00107 return *this; 00108 } 00109 00110 CVecd CVecd::operator+ (const CVecd &v) 00111 { 00112 //_ASSERTE(m_nSize == v.m_nSize); 00113 00114 if (m_nSize != v.m_nSize) 00115 return CVecd(0); 00116 00117 00118 CVecd result(m_nSize); 00119 00120 for (int i = 0; i < m_nSize; i++) 00121 result.m_pElements[i] = m_pElements[i] + v.m_pElements[i]; 00122 00123 00124 return result; 00125 } 00126 00127 CVecd CVecd::operator- (const CVecd &v) 00128 { 00129 //_ASSERTE(m_nSize == v.m_nSize); 00130 00131 if (m_nSize != v.m_nSize) 00132 return CVecd(0); 00133 00134 00135 CVecd result(m_nSize); 00136 00137 for (int i = 0; i < m_nSize; i++) 00138 result.m_pElements[i] = m_pElements[i] - v.m_pElements[i]; 00139 00140 00141 return result; 00142 } 00143 00144 double CVecd::operator* (const CVecd &v) 00145 { 00146 //_ASSERTE(m_nSize == v.m_nSize); 00147 00148 if (m_nSize != v.m_nSize) 00149 return 0.0; 00150 00151 00152 double dResult = 0.0; 00153 00154 for (int i = 0; i < m_nSize; i++) 00155 dResult += m_pElements[i] * v.m_pElements[i]; 00156 00157 00158 return dResult; 00159 } 00160 00161 double& CVecd::operator[] (const int n) const 00162 { 00163 //_ASSERTE(n >= 0 && n < m_nSize); 00164 00165 return m_pElements[n]; 00166 } 00167 00168 00169 // **************************************************************************** 00170 // Methods 00171 // **************************************************************************** 00172 00173 void CVecd::SetSize(int nSize) 00174 { 00175 //_ASSERTE(nSize >= 0); 00176 00177 // first free memory 00178 if (m_pElements) 00179 delete [] m_pElements; 00180 00181 // allocate memory for vector 00182 m_pElements = new double[nSize]; 00183 00184 for (int i = 0; i < nSize; i++) 00185 m_pElements[i] = 0.0; 00186 00187 // save size of vector 00188 m_nSize = nSize; 00189 } 00190 00191 double CVecd::Length() 00192 { 00193 double sum = 0.0; 00194 00195 for (int i = 0; i < m_nSize; i++) 00196 sum += m_pElements[i] * m_pElements[i]; 00197 00198 return sqrt(sum); 00199 }