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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
#ifndef VISUAL_VECTOR_H
#define VISUAL_VECTOR_H
// Copyright (c) 2000, 2001, 2002, 2003 by David Scherer and others.
// See the file license.txt for complete license terms.
// See the file authors.txt for a complete list of contributors.
#include "cvisual.h"
#include "vcache.h"
#include <iosfwd>
#include <sstream>
#include <stdexcept>
#include <cmath>
namespace visual {
class vector
{
public:
double x;
double y;
double z;
public:
explicit vector( double a = 0.0, double b = 0.0, double c = 0.0) throw()
:x(a), y(b), z(c) {}
// We use this in gldevice to construct an rView, using an
// implicit conversion. It is also used for converting double* iterators
// that point into a Numeric.array into a vector represenation.
inline vector( const double xyz[3])
: x( xyz[0]), y(xyz[1]), z(xyz[2]) {}
inline vector( const float xyz[3])
: x( static_cast<double>(xyz[0])), y( static_cast<double>(xyz[1])), z(static_cast<double>(xyz[2])) {}
// See the source for a detailed discussion of this potentially dangerous
// function.
explicit vector( const boost::python::object& t);
vector( const vector& v) throw() : x(v.x), y(v.y), z(v.z) {}
// Convert to a python tuple
boost::python::tuple
as_tuple() const;
// Overloaded binary +, -, *, and /
inline vector
operator+( const vector& v) const throw()
{ return vector( x+v.x, y+v.y, z+v.z); }
inline vector
operator-( const vector& v) const throw()
{ return vector( x-v.x, y-v.y, z-v.z); }
inline vector
operator*( const double s) const throw()
{ return vector( s*x, y*s, z*s); }
inline vector
operator*( const int s) const throw()
{ return vector( s*x, y*s, z*s); }
inline vector
operator/( const double s) const throw()
{ return vector( x/s, y/s, z/s); }
inline vector
operator/( const int s) const throw()
{ return vector( x/s, y/s, z/s); }
// This operator describes a strict weak ordering as defined by the STL.
bool
operator<( const vector& v) const throw();
inline bool
operator==( const vector& v) const throw()
{ return (v.x == this->x && v.y == this->y && v.z == this->z); }
inline bool
operator!=( const vector& v) const throw()
{ return !(v == *this); }
// Overloaded uniary !, probably bad coding practice.
inline bool
operator!( void) const throw()
{ return !x && !y && !z; }
// Overloaded assignment: =, +=, -=, *=, /=
inline const vector&
operator=( const vector& v) throw()
{ x=v.x; y=v.y; z=v.z; return *this; }
inline const vector&
operator+=( const vector& v) throw()
{ x=x+v.x; y=y+v.y; z=z+v.z; return *this; }
inline const vector&
operator-=( const vector& v) throw()
{ x=x-v.x; y=y-v.y; z=z-v.z; return *this; }
inline const vector&
operator*=( const int s) throw()
{ x=x*s; y=y*s; z=z*s; return *this; }
inline const vector&
operator*=( const double s) throw()
{ x=x*s; y=y*s; z=z*s; return *this; }
inline const vector&
operator/=( const int s) throw()
{ x=x/s; y=y/s; z=z/s; return *this; }
inline const vector&
operator/=( const double s) throw()
{ x=x/s; y=y/s; z=z/s; return *this; }
inline vector
operator-() const throw()
{ return vector( -x, -y, -z); }
// return the magnitude of this vector
inline double
mag( void) const throw()
{ return std::sqrt( x*x + y*y + z*z); }
// return the square of the this vector's magnitude
inline double
mag2( void) const throw()
{ return (x*x + y*y + z*z); }
// return the unit vector of this vector
vector
norm( void) const throw();
// Pythonic function to provide a "representation" of this object.
// object.__repr__() should return a string that, were it executed as python
// code, should regenerate the object.
std::string
repr() const;
// return the dot product of this vector and another
inline double
dot( const vector& v) const throw()
{ return ( v.x * this->x + v.y * this->y + v.z * this->z); }
// Return the cross product of this vector and another.
vector
cross( const vector& v) const throw();
// Scalar projection of this to v
double
comp( const vector& v) const throw();
// Vector projection of this to v
vector
proj( const vector& v) const throw();
// Returns true iff this->dot( v) == 0. Probably always return false in floating point.
bool
orthogonal( const vector& v) const throw();
// Returns the angular difference between two vectors, in radians, between 0 and pi.
double
diff_angle( const vector& v) const throw();
// Scale this vector to another, by elementwise multiplication
inline vector
scale( const vector& v) const throw()
{ return vector( this->x*v.x, this->y*v.y, this->z*v.z); }
void
py_scale( double);
void
py_scale2( double);
vector
rotate( double angle, vector axis = vector(0,0,1)) const throw();
// Last ditch direct read/write access to the private variables
inline double
get_x( void) const throw() { return x; }
inline void
set_x( double s) throw() { this->x = s; }
inline double
get_y( void) const throw() { return y; }
inline void
set_y( double s) throw() { this->y = s; }
inline double
get_z( void) const throw() { return z; }
inline void
set_z( double s) throw() { this->z = s; }
// zero the state of the vector. Potentially useful for reusing a temporary.
inline void
clear( void) { x=0.0; y=0.0; z=0.0; }
inline int
py_len() { return 3; }
double py_getitem( int i) const;
void py_setitem(int i, double value);
inline double&
operator[]( int ref)
{
switch (ref) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
default:
return x;
}
}
inline const double&
operator[]( int ref) const
{
switch (ref) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
default:
return x;
}
}
inline vector
fabs() const
{ return vector( std::fabs(x), std::fabs(y), std::fabs(z)); }
// Returns true iff this is a linear multiple of other. This is equivalent
// to this->norm().fabs() == other.norm().fabs(), but faster.
bool
linear_multiple_of( const vector& other) const;
};
// Free functions for mag, mag2, dot, unit, cross, and tripleproducts.
// All of these functions merely call their class-member variants to save code.
inline double
mag( const vector& v)
{ return v.mag(); }
inline double
mag2( const vector& v)
{ return v.mag2(); }
inline vector
norm( const vector& v)
{ return v.norm(); }
inline double
dot( const vector& v1, const vector& v2)
{ return v1.dot( v2); }
inline vector
cross( const vector& v1, const vector& v2)
{ return v1.cross( v2); }
// Scalar projection of v1 -> v2
inline double
comp( const vector& v1, const vector& v2)
{ return v1.comp( v2); }
// Vector projection of v1 to v2
inline vector
proj( const vector& v1, const vector& v2)
{ return v1.proj( v2); }
// Returns true iff this->dot( v) == 0. Probably always return false.
inline bool
orthogonal( const vector& v1, const vector& v2)
{ return v1.orthogonal( v2); }
// Returns the angular difference between two vectors, in radians, from 0 - pi.
inline double
diff_angle( const vector& v1, const vector& v2)
{ return v1.diff_angle( v2); }
inline vector
rotate( const vector& v, double angle, const vector& axis = vector(0, 0, 1))
{ return v.rotate( angle, axis); }
// Initialize the vector class interface to python.
void
vector_init_type();
} // !nampespace visual
// Definitions of the global functions for operators *, and /,
// with a vector on the RHS, and scalar on the LHS.
inline visual::vector
operator*( const double& s, const visual::vector& v)
{
return visual::vector( s*v.x, s*v.y, s*v.z);
}
inline visual::vector
operator*( const int& s, const visual::vector& v)
{
return visual::vector( s*v.x, s*v.y, s*v.z);
}
// We should not need to place this in namespace std, but GCC's L/U fails
// if we don't.
namespace std {
// Insertion operator. Example output: <xxxx, yyyy, zzzz>
// Based on "The C++ Standard Library", N. M. Josuttis, section 13.12.1
template<typename char_T, typename traits>
basic_ostream<char_T, traits>&
operator<<( basic_ostream<char_T, traits>& stream, const visual::vector& v)
{
basic_ostringstream<char_T, traits> s;
s.copyfmt( stream);
s.width( 0);
s << "<" << v.x << ", " << v.y << ", " << v.z << ">";
stream << s.str();
return stream;
}
} // ! namespace std
namespace visual {
// This is a utility class to help provide wrapping around a shared vector object.
// Locking is only provided for writes, not reads; the reading object must lock
// it (ie, the rendering thread).
// shared_vector provides simmillar functionallity to LockedVectorPtr under the
// Py::CXX-based interface.
class shared_vector : public vector
{
private:
typedef Cache::write_lock write_lock;
mutex* owner; //< The owner of this mutex must be a Cache
// object. Since return_internal_reference<>() binds the lifetime of this
// vector to its owner, we do not need any additional lifetime management for
// the pointer. Just the same, all of the assignment checks verify that the
// mutex is not NULL. In the event that owner is NULL, shared_vector
// behaves indistinguishably from vector.
public:
shared_vector( mutex* _owner, const vector& v )
: vector(v), owner(_owner){}
shared_vector( mutex* _owner, double x, double y, double z)
: vector( x, y, z), owner( _owner){}
const shared_vector&
operator=( boost::python::tuple t);
void
set_x( const double& x);
void
set_y( const double& y);
void
set_z( const double& z);
// Thread safely assign to this vector.
const shared_vector&
operator=( const vector& v);
const shared_vector&
operator+=( const vector& v);
const shared_vector&
operator-=( const vector& v);
const shared_vector&
operator*=( const double& s);
const shared_vector&
operator/=( const double& s);
const shared_vector&
operator*=( const int& s);
const shared_vector&
operator/=( const int& s);
void py_setitem(int i, double value);
void py_scale( double);
void py_scale2( double);
}; // !class shared_vector
// Numeric doens't support the Sequence protocol, so I have to use this hack
// instead.
inline int
length(const boost::python::object& seq)
{
int ret = PySequence_Size( seq.ptr());
if (ret == -1) {
boost::python::throw_error_already_set();
}
return ret;
}
} // !namespace visual
#endif // !VECTOR_H
|