| 1 |
/* |
| 2 |
* Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project |
| 3 |
* |
| 4 |
* Contact: oopse@oopse.org |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2.1 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* All we ask is that proper credit is given for our work, which includes |
| 11 |
* - but is not limited to - adding the above copyright notice to the beginning |
| 12 |
* of your source code files, and to any copyright notice that you may distribute |
| 13 |
* with programs based on this work. |
| 14 |
* |
| 15 |
* This program is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU Lesser General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU Lesser General Public License |
| 21 |
* along with this program; if not, write to the Free Software |
| 22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
/** |
| 27 |
* @file DataStorage.cpp |
| 28 |
* @author tlin |
| 29 |
* @date 10/26/2004 |
| 30 |
* @time 11:56am |
| 31 |
* @version 1.0 |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "brains/DataStorage.hpp" |
| 35 |
|
| 36 |
|
| 37 |
DataStorage::DataStorage() : size_(0), storageLayout_(0){ |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
DataStorage::DataStorage(int size, int storageLayout) : size_(size){ |
| 42 |
setStorageLayout(storageLayout); |
| 43 |
resize(size); |
| 44 |
} |
| 45 |
|
| 46 |
int DataStorage::getSize() { |
| 47 |
|
| 48 |
if (storageLayout_ & dslPosition && position.size() != size_) { |
| 49 |
//error |
| 50 |
std::cerr << "size does not match"<< std::endl; |
| 51 |
} |
| 52 |
|
| 53 |
if (storageLayout_ & dslVelocity && velocity.size() != size_) { |
| 54 |
//error |
| 55 |
std::cerr << "size does not match"<< std::endl; |
| 56 |
} |
| 57 |
|
| 58 |
if (storageLayout_ & dslAmat && aMat.size() != size_) { |
| 59 |
//error |
| 60 |
std::cerr << "size does not match"<< std::endl; |
| 61 |
} |
| 62 |
|
| 63 |
if (storageLayout_ & dslAngularMomentum && angularMomentum.size() != size_) { |
| 64 |
//error |
| 65 |
std::cerr << "size does not match"<< std::endl; |
| 66 |
} |
| 67 |
|
| 68 |
if (storageLayout_ & dslUnitVector && unitVector.size() != size_) { |
| 69 |
//error |
| 70 |
std::cerr << "size does not match"<< std::endl; |
| 71 |
} |
| 72 |
|
| 73 |
if (storageLayout_ & dslZAngle && zAngle.size() != size_) { |
| 74 |
//error |
| 75 |
std::cerr << "size does not match"<< std::endl; |
| 76 |
} |
| 77 |
|
| 78 |
if (storageLayout_ & dslForce && force.size() != size_) { |
| 79 |
//error |
| 80 |
std::cerr << "size does not match"<< std::endl; |
| 81 |
} |
| 82 |
|
| 83 |
if (storageLayout_ & dslTorque && torque.size() != size_) { |
| 84 |
//error |
| 85 |
std::cerr << "size does not match"<< std::endl; |
| 86 |
} |
| 87 |
|
| 88 |
return size_; |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
void DataStorage::resize(int newSize) { |
| 93 |
|
| 94 |
if (storageLayout_ & dslPosition) { |
| 95 |
internalResize(position, newSize); |
| 96 |
} |
| 97 |
|
| 98 |
if (storageLayout_ & dslVelocity) { |
| 99 |
internalResize(velocity, newSize); |
| 100 |
} |
| 101 |
|
| 102 |
if (storageLayout_ & dslAmat) { |
| 103 |
internalResize(aMat, newSize); |
| 104 |
} |
| 105 |
|
| 106 |
if (storageLayout_ & dslAngularMomentum) { |
| 107 |
internalResize(angularMomentum, newSize); |
| 108 |
} |
| 109 |
|
| 110 |
if (storageLayout_ & dslUnitVector) { |
| 111 |
internalResize(unitVector, newSize); |
| 112 |
} |
| 113 |
|
| 114 |
if (storageLayout_ & dslZAngle) { |
| 115 |
internalResize(zAngle, newSize); |
| 116 |
} |
| 117 |
|
| 118 |
if (storageLayout_ & dslForce) { |
| 119 |
internalResize(force, newSize); |
| 120 |
} |
| 121 |
|
| 122 |
if (storageLayout_ & dslTorque) { |
| 123 |
internalResize(torque, newSize); |
| 124 |
} |
| 125 |
|
| 126 |
size_ = newSize; |
| 127 |
} |
| 128 |
|
| 129 |
void DataStorage::reserve(int size) { |
| 130 |
if (storageLayout_ & dslPosition) { |
| 131 |
position.reserve(size); |
| 132 |
} |
| 133 |
|
| 134 |
if (storageLayout_ & dslVelocity) { |
| 135 |
velocity.reserve(size); |
| 136 |
} |
| 137 |
|
| 138 |
if (storageLayout_ & dslAmat) { |
| 139 |
aMat.reserve(size); |
| 140 |
} |
| 141 |
|
| 142 |
if (storageLayout_ & dslAngularMomentum) { |
| 143 |
angularMomentum.reserve(size); |
| 144 |
} |
| 145 |
|
| 146 |
if (storageLayout_ & dslUnitVector) { |
| 147 |
unitVector.reserve(size); |
| 148 |
} |
| 149 |
|
| 150 |
if (storageLayout_ & dslZAngle) { |
| 151 |
zAngle.reserve(size); |
| 152 |
} |
| 153 |
|
| 154 |
if (storageLayout_ & dslForce) { |
| 155 |
force.reserve(size); |
| 156 |
} |
| 157 |
|
| 158 |
if (storageLayout_ & dslTorque) { |
| 159 |
torque.reserve(size); |
| 160 |
} |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
void DataStorage::copy(int source, int num, int target) { |
| 165 |
if (num + target > size_ ) { |
| 166 |
//error |
| 167 |
} |
| 168 |
|
| 169 |
if (storageLayout_ & dslPosition) { |
| 170 |
interalCopy(position, source, num, target); |
| 171 |
} |
| 172 |
|
| 173 |
if (storageLayout_ & dslVelocity) { |
| 174 |
interalCopy(velocity, source, num, target); |
| 175 |
} |
| 176 |
|
| 177 |
if (storageLayout_ & dslAmat) { |
| 178 |
interalCopy(aMat, source, num, target); |
| 179 |
} |
| 180 |
|
| 181 |
if (storageLayout_ & dslAngularMomentum) { |
| 182 |
interalCopy(angularMomentum, source, num, target); |
| 183 |
} |
| 184 |
|
| 185 |
if (storageLayout_ & dslUnitVector) { |
| 186 |
interalCopy(unitVector, source, num, target); |
| 187 |
} |
| 188 |
|
| 189 |
if (storageLayout_ & dslZAngle) { |
| 190 |
interalCopy(zAngle, source, num, target); |
| 191 |
} |
| 192 |
|
| 193 |
if (storageLayout_ & dslForce) { |
| 194 |
interalCopy(force, source, num, target); |
| 195 |
} |
| 196 |
|
| 197 |
if (storageLayout_ & dslTorque) { |
| 198 |
interalCopy(torque, source, num, target); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
int DataStorage::getStorageLayout() { |
| 205 |
return storageLayout_; |
| 206 |
} |
| 207 |
|
| 208 |
void DataStorage::setStorageLayout(int layout) { |
| 209 |
storageLayout_ = layout; |
| 210 |
resize(size_); |
| 211 |
} |
| 212 |
|
| 213 |
double* DataStorage::getArrayPointer(int whichArray) { |
| 214 |
|
| 215 |
switch (whichArray) { |
| 216 |
case dslPosition: |
| 217 |
return internalGetArrayPointer(torque); |
| 218 |
break; |
| 219 |
|
| 220 |
case dslVelocity: |
| 221 |
return internalGetArrayPointer(velocity); |
| 222 |
break; |
| 223 |
|
| 224 |
case dslAmat: |
| 225 |
return internalGetArrayPointer(aMat); |
| 226 |
break; |
| 227 |
|
| 228 |
case dslAngularMomentum: |
| 229 |
return internalGetArrayPointer(angularMomentum); |
| 230 |
break; |
| 231 |
|
| 232 |
case dslUnitVector: |
| 233 |
return internalGetArrayPointer(unitVector); |
| 234 |
break; |
| 235 |
|
| 236 |
case dslZAngle: |
| 237 |
return internalGetArrayPointer(zAngle); |
| 238 |
break; |
| 239 |
|
| 240 |
case dslForce: |
| 241 |
return internalGetArrayPointer(force); |
| 242 |
break; |
| 243 |
|
| 244 |
case dslTorque: |
| 245 |
return internalGetArrayPointer(torque); |
| 246 |
break; |
| 247 |
|
| 248 |
default: |
| 249 |
//error message |
| 250 |
return NULL; |
| 251 |
|
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
double* DataStorage::internalGetArrayPointer(std::vector<Vector3d>& v) { |
| 256 |
if (v.size() == 0) { |
| 257 |
return NULL; |
| 258 |
} else { |
| 259 |
return v[0].getArrayPointer(); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
double* DataStorage::internalGetArrayPointer(std::vector<RotMat3x3d>& v) { |
| 264 |
if (v.size() == 0) { |
| 265 |
return NULL; |
| 266 |
} else { |
| 267 |
return v[0].getArrayPointer(); |
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
double* DataStorage::internalGetArrayPointer(std::vector<double>& v) { |
| 273 |
if (v.size() == 0) { |
| 274 |
return NULL; |
| 275 |
} else { |
| 276 |
return &(v[0]); |
| 277 |
} |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
template<typename T> |
| 282 |
void DataStorage::internalResize(std::vector<T>& v, int newSize){ |
| 283 |
int oldSize = v.size(); |
| 284 |
|
| 285 |
if (oldSize == newSize) { |
| 286 |
return; |
| 287 |
} else if (oldSize < newSize) { |
| 288 |
v.insert(v.end(), newSize-oldSize, T()); |
| 289 |
} else { |
| 290 |
typename std::vector<T>::iterator i; |
| 291 |
i = v.begin(); |
| 292 |
std::advance(i, newSize); |
| 293 |
v.erase(i, v.end()); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
template<typename T> |
| 298 |
void DataStorage::interalCopy(std::vector<T>& v, int source, int num, int target) { |
| 299 |
typename std::vector<T>::iterator first; |
| 300 |
typename std::vector<T>::iterator last; |
| 301 |
typename std::vector<T>::iterator result; |
| 302 |
|
| 303 |
first = v.begin(); |
| 304 |
last = v.begin(); |
| 305 |
result = v.begin(); |
| 306 |
|
| 307 |
std::advance(first, source); |
| 308 |
//STL algorithm use half opened range |
| 309 |
std::advance(last, num + 1); |
| 310 |
std::advance(result, target ); |
| 311 |
|
| 312 |
std::copy(first, last, result); |
| 313 |
} |