Vecd.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: Vecd.cpp
37 // Author: Pedram Azad
38 // Date: 2004
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "Vecd.h"
49 #include <string.h>
50 #include <math.h>
51 
52 
53 
54 // ****************************************************************************
55 // Constructors / Destructor
56 // ****************************************************************************
57 
59 {
60  m_pElements = 0;
61  m_nSize = 0;
62 }
63 
64 CVecd::CVecd(int nSize)
65 {
66  m_pElements = 0;
67 
68  SetSize(nSize);
69 }
70 
71 CVecd::CVecd(double dX1, double dX2)
72 {
73  m_pElements = 0;
74 
75  SetSize(2);
76 
77  m_pElements[0] = dX1;
78  m_pElements[1] = dX2;
79 }
80 
82 {
83  m_pElements = 0;
84 
85  SetSize(v.m_nSize);
86 
87  memcpy(m_pElements, v.m_pElements, m_nSize * sizeof(double));
88 }
89 
91 {
92  if (m_pElements)
93  delete [] m_pElements;
94 }
95 
96 
97 // ****************************************************************************
98 // Operators
99 // ****************************************************************************
100 
102 {
103  SetSize(v.GetSize());
104 
105  memcpy(m_pElements, v.m_pElements, m_nSize * sizeof(double));
106 
107  return *this;
108 }
109 
111 {
112  //_ASSERTE(m_nSize == v.m_nSize);
113 
114  if (m_nSize != v.m_nSize)
115  return CVecd(0);
116 
117 
118  CVecd result(m_nSize);
119 
120  for (int i = 0; i < m_nSize; i++)
121  result.m_pElements[i] = m_pElements[i] + v.m_pElements[i];
122 
123 
124  return result;
125 }
126 
128 {
129  //_ASSERTE(m_nSize == v.m_nSize);
130 
131  if (m_nSize != v.m_nSize)
132  return CVecd(0);
133 
134 
135  CVecd result(m_nSize);
136 
137  for (int i = 0; i < m_nSize; i++)
138  result.m_pElements[i] = m_pElements[i] - v.m_pElements[i];
139 
140 
141  return result;
142 }
143 
144 double CVecd::operator* (const CVecd &v)
145 {
146  //_ASSERTE(m_nSize == v.m_nSize);
147 
148  if (m_nSize != v.m_nSize)
149  return 0.0;
150 
151 
152  double dResult = 0.0;
153 
154  for (int i = 0; i < m_nSize; i++)
155  dResult += m_pElements[i] * v.m_pElements[i];
156 
157 
158  return dResult;
159 }
160 
161 double& CVecd::operator[] (const int n) const
162 {
163  //_ASSERTE(n >= 0 && n < m_nSize);
164 
165  return m_pElements[n];
166 }
167 
168 
169 // ****************************************************************************
170 // Methods
171 // ****************************************************************************
172 
173 void CVecd::SetSize(int nSize)
174 {
175  //_ASSERTE(nSize >= 0);
176 
177  // first free memory
178  if (m_pElements)
179  delete [] m_pElements;
180 
181  // allocate memory for vector
182  m_pElements = new double[nSize];
183 
184  for (int i = 0; i < nSize; i++)
185  m_pElements[i] = 0.0;
186 
187  // save size of vector
188  m_nSize = nSize;
189 }
190 
192 {
193  double sum = 0.0;
194 
195  for (int i = 0; i < m_nSize; i++)
196  sum += m_pElements[i] * m_pElements[i];
197 
198  return sqrt(sum);
199 }
double operator*(const CVecd &v)
Definition: Vecd.cpp:144
GLenum GLsizei n
Definition: glext.h:4209
CVecd operator-(const CVecd &v)
Definition: Vecd.cpp:127
CVecd()
Definition: Vecd.cpp:58
int m_nSize
Definition: Vecd.h:85
double * m_pElements
Definition: Vecd.h:84
int GetSize() const
Definition: Vecd.h:79
double Length()
Definition: Vecd.cpp:191
Data structure and operations for calculating with vectors of arbitrary dimension.
Definition: Vecd.h:54
void SetSize(int nSize)
Definition: Vecd.cpp:173
~CVecd()
Definition: Vecd.cpp:90
CVecd operator+(const CVecd &v)
Definition: Vecd.cpp:110
const GLdouble * v
Definition: glext.h:3212
double & operator[](const int n) const
Definition: Vecd.cpp:161
CVecd & operator=(const CVecd &v)
Definition: Vecd.cpp:101


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28