From: Markus R. <rol...@us...> - 2005-12-05 20:56:08
|
Update of /cvsroot/simspark/simspark/spark/salt In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9485 Added Files: Makefile.am bounds.cpp bounds.h defines.h fileclasses.cpp fileclasses.h frustum.cpp frustum.h gmath.h matrix.cpp matrix.h path.cpp path.h plane.cpp plane.h random.h rect.h salt.h sharedlibrary.cpp sharedlibrary.h tvector.h vector.h Log Message: --- NEW FILE: path.cpp --- /* -*- mode: c++ -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2003 Koblenz University $Id: path.cpp,v 1.1 2005/12/05 20:56:00 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "path.h" #include <boost/tokenizer.hpp> using namespace std; using namespace salt; Path::Path(const std::string &path) { Set(path); } void Path::Set(const std::string &path) { if (path[0] == '/' || path[0] == '\\') { mIsAbsolute = true; } else { mIsAbsolute = false; } Tokenize(path); } std::string Path::GetCleanPath(const std::string &sep) const { string path; if (mIsAbsolute) { path += sep; } unsigned int count = 0; for (TStringList::const_iterator i = mPathComponents.begin(); i != mPathComponents.end(); ++i) { path += (*i); ++count; if ( count != mPathComponents.size() ) { path += sep; } } return path; } bool Path::IsAbsolute() const { return mIsAbsolute; } bool Path::IsEmpty() const { return mPathComponents.empty(); } const std::string& Path::Front() const { return mPathComponents.front(); } void Path::PopFront() { mIsAbsolute = false; mPathComponents.pop_front(); } const std::string& Path::Back() const { return mPathComponents.back(); } void Path::PopBack() { mPathComponents.pop_back(); } void Path::Tokenize(const std::string &path) { typedef boost::tokenizer<boost::char_separator<char> > TTokenizer; boost::char_separator<char> sep("/\\"); TTokenizer tokens(path, sep); for (TTokenizer::iterator i = tokens.begin(); i != tokens.end(); ++i) { mPathComponents.push_back(*i); } } --- NEW FILE: fileclasses.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: fileclasses.h,v 1.1 2005/12/05 20:56:00 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* this file defines two interfaces- RFile and WFile, that are used to * implement classes providing unified file semantics employing * different means. Two classes implementing these interfaces are * included: Memfile and StdFile. * Derived file classes providing access to .zip, .rar and other * archives have been implemented as plugins to the fileserver of the * zeitgeist library. */ #ifndef SALT_FILECLASSES_H #define SALT_FILECLASSES_H // #ifdef HAVE_CONFIG_H // #include <config.h> // #endif #include <cstdio> #include <cstdlib> namespace salt { /** RFile defines an interface and some basic support functions for * classes providing read only file services */ class RFile { public: virtual ~RFile() {}; // a bunch of pure virtual functions which a file must implement /** opens the file fn in the specified mode. The implementation of * the namespace and mode semantics ist up to derived classes */ virtual bool Open(const char*fn=NULL, char*mode="rb") = 0; /** closes a previously opened file */ virtual void Close() = 0; /** releases any occupied ressources used by the file. The * semantic is up to the derived classes */ virtual void Destroy() = 0; /** returns a non zero value if the file pointer is at the end of * the file */ virtual int Eof() = 0; /** returns the current file pointer position */ virtual long Tell() = 0; /** copies the current file pointer position to pos and returns * true on success */ virtual int GetPos(long *pos) = 0; /** sets the file pointer for the file. The new position, measured * in bytes, is obtained by adding offset bytes to the position * specified by origin. If origin is set to SEEK_SET, SEEK_CUR, or * SEEK_END, the offset is relative to the start of the file, the * current position indicator, or end-of-file, respectively. */ virtual int Seek(long offset, int origin) = 0; /** sets the file pointer to the beginning of the file */ virtual void Rewind() = 0; /** return the size of the file */ virtual long Size() = 0; /** reads in at most one less than n characters from the file and * stores them into buffer. A '\0' is stored after the last character in * the buffer. */ virtual char* Gets(char*buffer,int n) = 0; /** reads the next character from the file returns it as an int r * EOF on end of file or error. */ virtual int Getc() = 0; /** returns a handle identifying the file. The semantics of this * handle depends on the subclass, implementing this method */ virtual void* GetHandle() = 0; /** reads reads count elements of data, each size bytes long, * storing them in the specified buffer */ virtual size_t Read(void *buffer,size_t size,size_t count) = 0; /** reads count bytes of data, storing them in the specified buffer */ virtual size_t Read(void *buffer,size_t bytes) = 0; /** reads a 2 byte int from the file in Intel ordering */ int Igetw() { int b1, b2; if ((b1 = Getc()) != EOF) if ((b2 = Getc()) != EOF) return ((b2 << 8) | b1); return EOF; } /** reads a 4 byte int from the file in Intel ordering */ long Igetl() { int b1, b2, b3, b4; if ((b1 = Getc()) != EOF) if ((b2 = Getc()) != EOF) if ((b3 = Getc()) != EOF) if ((b4 = Getc()) != EOF) return (((long)b4<<24) | ((long)b3<<16) | ((long)b2<<8) | (long)b1); return EOF; } /** reads a 2 byte int from the file in Motorola ordering */ int Mgetw() { int b1, b2; if ((b1 = Getc()) != EOF) if ((b2 = Getc()) != EOF) return ((b1 << 8) | b2); return EOF; } /** reads a 4 byte int from the file in Motorola ordering */ long Mgetl() { int b1, b2, b3, b4; if ((b1 = Getc()) != EOF) if ((b2 = Getc()) != EOF) if ((b3 = Getc()) != EOF) if ((b4 = Getc()) != EOF) return (((long)b1<<24) | ((long)b2<<16) | ((long)b3<<8) | (long)b4); return EOF; } }; /** Memfile implements the RFile interface using an inmemory * buffer. On open() a file is completely read into the buffer and * from there on served from memory. */ class MemFile : public RFile { public: MemFile(const char*fn=NULL, char*mode="rb"); MemFile(FILE*f); MemFile(RFile *f); ~MemFile(); bool Open(const char*fn=NULL, char*mode="rb"); bool Open(void*buffer, long s); void Close(); void Destroy(); int Eof(); long Tell(); int GetPos(long *pos); int Seek(long offset, int origin); void Rewind(); long Size(); char* Gets(char*buffer,int n); int Getc(); void* GetHandle() { return mHandle; } size_t Read(void *buffer,size_t size,size_t count); size_t Read(void *buffer,size_t count) { return Read(buffer, 1,count); } private: /** the file handle */ void* mHandle; /** a pointer to the buffer holding the file */ unsigned char* mCharHandle; /** the size of the file in bytes */ long mSize; /** the current file pointer position */ long mPosition; }; /** WFile extends the RFile interface with methods for writing to a * file and related support funtions */ class WFile : public RFile { public: virtual ~WFile() {}; /** writes a string without the trailing '\0' */ virtual int Puts(const char*s) = 0; /** writes a single character */ virtual int Putc(int c) = 0; /** writes a 2 byte int in Intel ordering */ int Iputw(int w) { int b1, b2; b1 = (w & 0xFF00) >> 8; b2 = w & 0x00FF; if (Putc(b2)==b2) if (Putc(b1)==b1) return w; return EOF; } /** writes count elements of data from buffer, each size bytes long */ virtual size_t Write(void *buffer,size_t size,size_t count) = 0; /** writes count bytes of data from buffer */ virtual size_t Write(void *buffer,size_t count) = 0; /** writes a 4 byte int in Intel ordering */ long Iputl(long l) { int b1, b2, b3, b4; b1 = (int)((l & 0xFF000000L) >> 24); b2 = (int)((l & 0x00FF0000L) >> 16); b3 = (int)((l & 0x0000FF00L) >> 8); b4 = (int)l & 0x00FF; if (Putc(b4)==b4) if (Putc(b3)==b3) if (Putc(b2)==b2) if (Putc(b1)==b1) return l; return EOF; } /** writes a 2 byte int in Motorola ordering */ int Mputw(int w) { int b1, b2; b1 = (w & 0xFF00) >> 8; b2 = w & 0x00FF; if (Putc(b1)==b1) if (Putc(b2)==b2) return w; return EOF; } /** writes a 4 byte in in Motorola ordering */ long Mputl(long l) { int b1, b2, b3, b4; b1 = (int)((l & 0xFF000000L) >> 24); b2 = (int)((l & 0x00FF0000L) >> 16); b3 = (int)((l & 0x0000FF00L) >> 8); b4 = (int)l & 0x00FF; if (Putc(b1)==b1) if (Putc(b2)==b2) if (Putc(b3)==b3) if (Putc(b4)==b4) return l; return EOF; } }; /** StdFile implements the WFile interface using the standard file system */ class StdFile : public WFile { public: StdFile(FILE*f); StdFile(const char*fn=NULL, char*mode="rb"); virtual ~StdFile(); bool Open(const char*fn=NULL, char*mode="rb"); void Close(); void Destroy(); int Eof(); long Tell(); int GetPos(long *pos); int Seek(long offset, int origin); void Rewind(); long Size(); char* Gets(char*buffer,int n); int Getc(); int Puts(const char*s); int Putc(int c); size_t Read(void *buffer,size_t size,size_t count); size_t Read(void *buffer,size_t count) { return Read(buffer,1,count); } size_t Write(void *buffer,size_t size,size_t count); size_t Write(void *buffer,size_t count) { return Write(buffer,1,count); } void* GetHandle(); protected: /** the standard file handle */ FILE *mHandle; }; } //namespace salt #endif //FILECLASSES_H__ --- NEW FILE: matrix.cpp --- /* -*- mode: c++ -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2003 Koblenz University $Id: matrix.cpp,v 1.1 2005/12/05 20:56:00 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "matrix.h" #include <cstdio> using namespace salt; float Matrix::mIdentity[16]= { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 }; void Matrix::Dump() const { printf("%f %f %f %f\n", m[0], m[4], m[8], m[12]); printf("%f %f %f %f\n", m[1], m[5], m[9], m[13]); printf("%f %f %f %f\n", m[2], m[6], m[10], m[14]); printf("%f %f %f %f\n", m[3], m[7], m[11], m[15]); } void Matrix::LookAt(const Vector3f & inEye, const Vector3f & inDirection, const Vector3f & inUp) { Vector3f forward = inDirection.Normalized(); Vector3f up = inUp.Normalized(); Vector3f right = forward.Cross(up); right.Normalize(); up = right.Cross(forward); // Make inverse rotation matrix using right, forward, up vectors Set( right.x(), right.y(), right.z(), 0.0f, up.x(), up.y(), up.z(), 0.0f, forward.x(), forward.y(), forward.z(), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); Pos() = Rotate(-inEye); } /*! Calculate an attenuation matrix. pos = world space position of light radius = radius of light (used for distance attenuation) */ void Matrix::CalcAttenuationNoRotation(const Vector3f &pos, float radius) { Matrix tmp1, tmp2; // translate to 'light space' ... no rotation needed tmp1.Translation(-pos); // create proper scaling matrix float invrad = 0.5f / radius; tmp2.Identity(); tmp2(0,0) = invrad; tmp2(1,1) = invrad; tmp2(2,2) = invrad; Identity (); Translation (Vector3f(0.5f, 0.5f, 0.5f)); *this *= tmp2 * tmp1; } void Matrix::CalcAttenuationWithRotation(const Matrix &lightWorldMatrix, float radius) { Matrix tmp1, tmp2; // translate to 'light space' ... this time with rotation tmp1 = lightWorldMatrix; tmp1.InvertRotationMatrix(); // create proper scaling matrix float invrad = 0.5f / radius; tmp2.Identity(); tmp2(0,0) = invrad; tmp2(1,1) = invrad; tmp2(2,2) = invrad; Identity (); Translation (Vector3f(0.5f, 0.5f, 0.5f)); *this *= tmp2 * tmp1; } void Matrix::CalcInfiniteProjection(float width, float height, float fov, float zNear) { const float halfWidth = zNear * (float)tan(gDegToRad(fov*0.5f)); const float halfHeight = halfWidth * (height/width); CalcInfiniteFrustum(-halfWidth, halfWidth, -halfHeight, halfHeight, zNear); } void Matrix::CalcInfiniteFrustum(float left, float right, float bottom, float top, float zNear) { Identity(); El(0,0) = (2*zNear) / (right - left); El(0,2) = (right + left) / (right - left); El(1,1) = (2*zNear) / (top - bottom); El(1,2) = (top + bottom) / (top - bottom); // nudge infinity in just slightly for lsb slop const float nudge = 1 - 1.0 / (1<<23); El(2,2) = -1 * nudge; El(2,3) = -2*zNear * nudge; El(3,2) = -1; El(3,3) = 0; } void Matrix::CalcSpotLight(const Matrix &lightWorldTransform, float fov, float width, float height, float zNear) { Matrix scale, proj, space; Vector3f halfVector(0.5f, 0.5f, 0.5f); Identity (); Translation (halfVector); scale.Identity (); scale.Scale (halfVector); // create projection matrix proj.CalcInfiniteProjection(width, height, fov, zNear); space = lightWorldTransform; space.InvertRotationMatrix(); // so, we transform first into light space, then project, then scale and // translate (this) *this *= scale * proj * space; } bool Matrix::IsEqual(const Matrix& matrix) const { for (int i=0; i<16; ++i) if (matrix.m[i] != m[i]) return false; return true; } --- NEW FILE: rect.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: rect.h,v 1.1 2005/12/05 20:56:00 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef SALT_RECT_H #define SALT_RECT_H #ifdef HAVE_CONFIG_H #include <config.h> #endif #include "defines.h" namespace salt { /** this class provides rectangle in two dimensional space */ class Rect { public: /** constucts undefined Rect for performance reasons */ f_inline Rect() { } /** copy constructs a Rect from inOther */ f_inline Rect(const Rect &inOther) : mLeft(inOther.Left()), mTop(inOther.Top()), mRight(inOther.Right()), mBottom(inOther.Bottom()) { } /** constructs a Rect from the coordinates inLeft, inTop, inRight and inBottom */ f_inline Rect(int inLeft, int inTop, int inRight, int inBottom) : mLeft(inLeft), mTop(inTop), mRight(inRight), mBottom(inBottom) { } /** sets up a Rect from the coordinates inLeft, inTop, inRight and inBottom */ f_inline void Set(int inLeft, int inTop, int inRight, int inBottom) { mLeft=inLeft; mTop=inTop; mRight=inRight; mBottom=inBottom; } // member access /** returns the left boundary of the rectangle */ f_inline int Left() const { return mLeft; } /** return the right boundary of the rectangle */ f_inline int Right() const { return mRight; } /** return the top boundary of the rectangle */ f_inline int Top() const { return mTop; } /** return the bottom boundary of the rectangle */ f_inline int Bottom() const { return mBottom; } /** calculates the width of the rectangle */ f_inline int Width() const { return mRight-mLeft; } /** calculates the height of the rectangle */ f_inline int Height() const { return mBottom-mTop; } /// Actions /** normalizes the rectangle coordinates, i.e. assures that right>left and top>bottom. */ f_inline void Normalize() { if (mRight < mLeft) gSwap(mLeft, mRight); if (mBottom < mTop) gSwap(mTop, mBottom); } /** widens the rectangle about inDelta */ f_inline void Widen(int inDelta) { mLeft-=inDelta; mTop-=inDelta; mRight+=inDelta; mBottom+=inDelta; } /** widens the rectangle horizontally about inDeltaWidth and * vertically about inDeltaHeight */ f_inline void Widen(int inDeltaWidth, int inDeltaHeight) { mRight+=inDeltaWidth; mBottom+=inDeltaHeight; } /** widens the rectangles left boundary about inDeltaleft, the right * boundary about inDeltaRigt, the top boundary about inDeltaTop * and the bottom boundary about inDeltaBottom */ f_inline void Widen(int inDeltaLeft, int inDeltaTop, int inDeltaRight, int inDeltaBottom) { mLeft-=inDeltaLeft; mTop-=inDeltaTop; mRight+=inDeltaRight; mBottom+=inDeltaBottom; } /** shrinks the rectangle about inDelta */ f_inline void Shrink(int inDelta) { mLeft+=inDelta; mTop+=inDelta; mRight-=inDelta; mBottom-=inDelta; } /** shrinks the rectangle horizontally about inDeltaWidth and * vertically about inDeltaHeight */ f_inline void Shrink(int inDeltaWidth, int inDeltaHeight) { mRight-=inDeltaWidth; mBottom-=inDeltaHeight; } /** shrinks the rectangles left boundary about inDeltaleft, the right * boundary about inDeltaRigt, the top boundary about inDeltaTop * and the bottom boundary about inDeltaBottom */ f_inline void Shrink(int inDeltaLeft, int inDeltaTop, int inDeltaRight, int inDeltaBottom) { mLeft+=inDeltaLeft; mTop+=inDeltaTop; mRight-=inDeltaRight; mBottom-=inDeltaBottom; } /** moves the rectangle horizontally inDeltaX and vertically inDeltaY */ f_inline void Offset(int inDeltaX, int inDeltaY) { mLeft+=inDeltaX; mTop+=inDeltaY; mRight+=inDeltaX; mBottom+=inDeltaY; } /** returns true if this rectangle intersects with the rectangle b */ f_inline bool Intersects(const Rect &b) const { return !(mLeft > b.mRight || mRight < b.mLeft || mTop > b.mBottom || mBottom < b.mTop); } // assignment /** sets up the rectangle from inOther */ f_inline Rect& operator=(const Rect &inOther) { mLeft=inOther.Left(); mTop=inOther.Top(); mRight=inOther.Right(); mBottom=inOther.Bottom(); return *this; } // comparison /** returns true if inRhs is equal to this rectangle */ f_inline bool operator==(const Rect &inRHS) const { return (mLeft==inRHS.Left()) && (mTop==inRHS.Top()) && (mRight==inRHS.Right()) && (mBottom==inRHS.Bottom()); } /** returns true if inRhs differs from this rectangle */ f_inline bool operator!=(const Rect &inRHS) const { return (mLeft!=inRHS.Left()) || (mTop!=inRHS.Top()) || (mRight!=inRHS.Right()) || (mBottom!=inRHS.Bottom()); } private: /** the left boundary */ int mLeft; /** the top boundary */ int mTop; /** the right boundary */ int mRight; /** the bottom boundary */ int mBottom; }; } //namespace salt #endif //SALT_RECT_H --- NEW FILE: bounds.cpp --- /* -*- mode: c++ -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2003 Koblenz University $Id: bounds.cpp,v 1.1 2005/12/05 20:56:00 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "bounds.h" #include <cstdio> using namespace salt; void AABB3::TransformBy(Matrix& matrix) { AABB3 bb; Vector3f v(minVec); Vector3f w(maxVec); bb.Encapsulate(matrix.Transform(Vector3f(v.x(),v.y(),v.z()))); bb.Encapsulate(matrix.Transform(Vector3f(w.x(),v.y(),v.z()))); bb.Encapsulate(matrix.Transform(Vector3f(v.x(),w.y(),v.z()))); bb.Encapsulate(matrix.Transform(Vector3f(w.x(),w.y(),v.z()))); bb.Encapsulate(matrix.Transform(Vector3f(v.x(),v.y(),w.z()))); bb.Encapsulate(matrix.Transform(Vector3f(w.x(),v.y(),w.z()))); bb.Encapsulate(matrix.Transform(Vector3f(v.x(),w.y(),w.z()))); bb.Encapsulate(matrix.Transform(Vector3f(w.x(),w.y(),w.z()))); minVec.Set(bb.minVec); maxVec.Set(bb.maxVec); } void BoundingSphere::Encapsulate(const Vector3f &v) { // TODO : check if this is correct Vector3f diff = v - center; float dist = diff.Dot(diff); if (dist > radiusSq) { Vector3f diff2 = diff.Normalized() * radius; Vector3f delta = 0.5f * (diff - diff2); center += delta; radius += delta.Length(); radiusSq = radius*radius; } } bool BoundingSphere::Intersects(const AABB3 &b) const { float distance = 0.0f; for (int t=0; t<3; t++) { if (center[t] < b.minVec[t]) { distance += (center[t] - b.minVec[t]) * (center[t] - b.minVec[t]); if (distance>radiusSq) return false; } else if (center[t]>b.maxVec[t]) { distance+=(center[t] - b.maxVec[t]) * (center[t] - b.maxVec[t]); if (distance>radiusSq) return false; } } return true; } bool BoundingSphere::Contains(const AABB3 &b) const { float distance = 0.0f; for (int t=0; t<3; t++) { if (center[t]<b.maxVec[t]) distance+=(center[t] - b.maxVec[t]) * (center[t] - b.maxVec[t]); else if (center[t]>b.minVec[t]) distance+=(center[t] - b.minVec[t]) * (center[t] - b.minVec[t]); if (distance>radiusSq) return false; } return true; } --- NEW FILE: Makefile.am --- if DEBUG pkglib_LTLIBRARIES = libsalt_debug.la libsalt_debug_la_SOURCES = $(sources) libsalt_debug_la_CXXFLAGS = -O -g -W -Wall libsalt_debug_la_LIBADD = @SALT_LIBADD@ ${top_srcdir}/utility/libobj/liblibobject.la libsalt_debug_la_LDFLAGS = -version-info @salt_version_info@ else pkglib_LTLIBRARIES = libsalt.la libsalt_la_SOURCES = $(sources) libsalt_la_CXXFLAGS = -O2 libsalt_la_LIBADD = @SALT_LIBADD@ ${top_srcdir}/utility/libobj/liblibobject.la libsalt_la_LDFLAGS = -version-info @salt_version_info@ endif bin_SCRIPTS = salt-config ## define include directory local to the pkgincludedir libpkgincludedir = $(includedir)/@PACKAGE@/salt sources = \ bounds.cpp \ fileclasses.cpp \ frustum.cpp \ matrix.cpp \ path.cpp \ plane.cpp \ gcc/sharedlibrary.cpp libpkginclude_HEADERS = \ bounds.h \ fileclasses.h \ gmath.h \ path.h \ random.h \ salt.h \ tvector.h \ defines.h \ frustum.h \ matrix.h \ plane.h \ rect.h \ sharedlibrary.h \ vector.h --- NEW FILE: tvector.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: tvector.h,v 1.1 2005/12/05 20:56:00 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. TVector is a template class for vector implementations. It abstracts away the number of elements and their type. TVector2 is a 2D version TVector3 is a 3D version HISTORY: 12.08.2002 MK - initial version */ #ifndef SALT_TVECTOR_H #define SALT_TVECTOR_H // #ifdef HAVE_CONFIG_H // #include <config.h> // #endif #include "defines.h" #include "gmath.h" #include <iostream> namespace salt { /** TVector is a template class for vector implementations. It * abstracts away the number of elements and their type. */ template <typename DATATYPE, int ELEMENTS, typename TYPE> class TVector { // // functions // public: TVector() {} // accessors /** returns a reference to a row of the vector */ f_inline DATATYPE& operator[](int row) { return El(row); } /** returns a constant reference to a row of a vector */ f_inline const DATATYPE& operator[](int row) const { return El(row); } /** returns a reference to a row of a vector */ f_inline DATATYPE& Get(int row) { return El(row); } /** returns a constant reference to a row of a vector */ f_inline const DATATYPE& Get(int row) const { return El(row); } // Direct pointer access to the data member ... use with care!!! /** copies another vector 'copy' */ f_inline const TYPE& SetData(const DATATYPE *copy); /** return a pointer to the encapsulated vector */ f_inline DATATYPE* GetData() { return mData; } // Output /** prints the contents of vector to stdout */ void Dump() const; /** fills all components of the vector with value 'fill' */ f_inline const TYPE& Fill(const DATATYPE& fill); /** sets all components of the vector to 0 */ f_inline TYPE& Zero(); // operators /** calculates this VECTOR + VECTOR */ f_inline const TYPE operator+(const TYPE &v) const; /** calculates VECTOR - VECTOR*/ f_inline const TYPE operator-(const TYPE &v) const; /** calculates VECTOR * VECTOR */ f_inline const TYPE operator*(const DATATYPE &v) const; /** calculates VECTOR / SCALAR */ f_inline const TYPE operator/(const DATATYPE &v) const; /** add another vector */ f_inline TYPE& operator+=(const TYPE &v); /** substracts another vector */ f_inline TYPE& operator-=(const TYPE &v); /** multiplies another vector */ f_inline TYPE& operator*=(const DATATYPE &v); /** divides another vector */ f_inline TYPE& operator/=(const DATATYPE &v); /** returns the negate of this vector */ f_inline TYPE operator-() const; /** returns true if this vector and v are equal */ f_inline bool operator==(const TYPE& v)const; /** returns true if this vector and v are not equal */ f_inline bool operator!=(const TYPE& v)const; /** returns the dot product from this vector and v */ f_inline DATATYPE Dot(const TYPE& v) const; /** normalizes the vector */ f_inline const TYPE& Normalize(); /** calculates the normalized vector, not modifying the vector */ f_inline TYPE Normalized() const; /** calculates the squared length of the vector */ f_inline DATATYPE SquareLength() const; /** calculates the length of the vector */ f_inline DATATYPE Length() const { return gSqrt(SquareLength()); } /** returns the index of least significant axis */ f_inline int GetLeastSignificantAxis() const; /** returns the index of the most significant axis */ f_inline int GetMostSignificantAxis() const; /** lineary interpolates between this vector and to with an delta increment */ f_inline TYPE LinearInterpolate(const TYPE& to, float delta) const; /** lineary interpolates between this vector and to with an delta * increment, returning a normalized vector */ f_inline TYPE NormalizedLinearInterpolate(const TYPE& to, float delta) const { return LinearInterpolate(to, delta).Normalize(); } protected: // Copy constructor: TVector(const TYPE &v) { for (int i=0; i<ELEMENTS; i++) mData[i] = v[i]; } // Element accessor const DATATYPE& El(int index) const { return mData[index]; } DATATYPE& El(int index) { return mData[index]; } // // members // private: DATATYPE mData[ELEMENTS]; }; template <typename DATATYPE, int ELEMENTS, typename TYPE> std::ostream& operator <<(std::ostream& ost, const TVector<DATATYPE,ELEMENTS,TYPE>& v) { if (ELEMENTS < 1) return ost; ost << v[0]; for (int i=1; i<ELEMENTS; ++i) ost << " " << v[i]; return ost; } /** TVector2 is a two dimensional version of TVector */ template <typename DATATYPE, class TYPE> class TVector2 : public TVector<DATATYPE, 2, TYPE> { public: /** constructs an undefined TVector2 */ TVector2() : TVector<DATATYPE, 2, TYPE>() {}; /** constructs a TVector2 from x and y */ TVector2(DATATYPE x, DATATYPE y) : TVector<DATATYPE, 2, TYPE>() { Set(x, y); } // Element Access operators /** returns a reference to the first component */ f_inline DATATYPE& x() { return this->El(0); } /** returns a constant reference to the first component */ f_inline const DATATYPE& x() const { return this->El(0); } /** returns a reference to the second component */ f_inline DATATYPE& y() { return this->El(1); } /** returns a constant reference to the second component */ f_inline const DATATYPE& y() const { return this->El(1); } /** sets up the vector from x and y */ f_inline const TYPE& Set(const DATATYPE& x, const DATATYPE& y) { this->El(0) = x; this->El(1) = y; return *static_cast<TYPE*>(this); } }; /** TVector3 is a two dimensional version of TVector */ template <typename DATATYPE, class TYPE> class TVector3 : public TVector<DATATYPE, 3, TYPE> { public: /** constructs an undefined TVector3 */ TVector3() : TVector<DATATYPE, 3, TYPE>() {}; /** constructs a TVector3 from x,y and z */ TVector3(const DATATYPE& x, const DATATYPE& y, const DATATYPE& z) : TVector<DATATYPE, 3, TYPE>() { Set(x, y, z); } // Element Access operators /** returns a reference to the first component */ f_inline DATATYPE& x() { return this->El(0); } /** returns a constant reference to the first component */ f_inline const DATATYPE& x() const { return this->El(0); } /** returns a reference to the second component */ f_inline DATATYPE& y() { return this->El(1); } /** returns a constant reference to the second component */ f_inline const DATATYPE& y() const { return this->El(1); } /** returns a reference to the third component */ f_inline DATATYPE& z() { return this->El(2); } /** returns a constant reference to the third component */ f_inline const DATATYPE& z() const { return this->El(2); } /** calculates the cross product, returning a new TVector3 */ const TYPE Cross(const TVector<DATATYPE, 3, TYPE>& v) const { // Create a new one TYPE r; r[0] = this->El(1) * v[2] - this->El(2) * v[1]; r[1] = this->El(2) * v[0] - this->El(0) * v[2]; r[2] = this->El(0) * v[1] - this->El(1) * v[0]; return r; } // // Set operator // /** sets up the vector from x,y and z */ const TYPE& Set(const DATATYPE& x, const DATATYPE& y, const DATATYPE& z) { this->El(0) = x; this->El(1) = y; this->El(2) = z; return *static_cast<TYPE*>(this); } /** sets up the vector from another TVector3 v */ const TYPE& Set(const TYPE& v) { this->El(0) = v.x(); this->El(1) = v.y(); this->El(2) = v.z(); return *static_cast<TYPE*>(this); } }; // Set operator for any vector. Just with a pointer template <typename DATATYPE, int ELEMENTS, typename TYPE> inline const TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::SetData(const DATATYPE *copy) { for (int i=0; i<ELEMENTS; i++) mData[i] = copy[i]; return *static_cast<TYPE*>(this); } // Output template <typename DATATYPE, int ELEMENTS, typename TYPE> inline void TVector<DATATYPE, ELEMENTS, TYPE>::Dump() const { for (int i=0; i<ELEMENTS; i++) std::cout << mData[i] << " "; std::cout << "\n"; } // fill vector with value 'fill' template <typename DATATYPE, int ELEMENTS, class TYPE> f_inline const TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::Fill(const DATATYPE& fill) { for (int c=0; c < ELEMENTS; c++) mData[c] = fill; return *static_cast<TYPE*>(this); } // fill vector with zeros template <typename DATATYPE, int ELEMENTS, class TYPE> f_inline TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::Zero() { for (int c=0; c < ELEMENTS; c++) mData[c] = DATATYPE(0); return *static_cast<TYPE*>(this); } // DATATYPE * vector template <typename DATATYPE, int ELEMENTS, class TYPE> f_inline const TYPE operator*(const DATATYPE& f, const TVector<DATATYPE, ELEMENTS, TYPE>& vec) { return vec * f; } // vector addition // this + v return new Vector template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline const TYPE TVector<DATATYPE, ELEMENTS, TYPE>::operator+(const TYPE& v) const { TYPE r; for (int c=0; c < ELEMENTS; c++) r[c]=mData[c] + v[c]; return r; } // vector subtraction // this - v return new Vector template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline const TYPE TVector<DATATYPE, ELEMENTS, TYPE>::operator-(const TYPE& v) const { TYPE r; for (int c=0; c < ELEMENTS; c++) r[c]=mData[c] - v[c]; return r; } // scale // this * DATATYPE return new Vector template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline const TYPE TVector<DATATYPE, ELEMENTS, TYPE>::operator*(const DATATYPE& v) const { TYPE r; for (int c=0; c < ELEMENTS; c++) r[c]=mData[c] * v; return r; } // division // this / DATATYPE return new Vector template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline const TYPE TVector<DATATYPE, ELEMENTS, TYPE>::operator/(const DATATYPE& v) const { TYPE r; for (int c=0; c < ELEMENTS; c++) r[c]=mData[c] / v; return r; } // this += v returns reference to first vector template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::operator+=(const TYPE& v) { for (int c=0; c<ELEMENTS; c++) mData[c] += v[c]; return *static_cast<TYPE*>(this); } // this -= v returns reference to first vector template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::operator-=(const TYPE& v) { for (int c=0; c<ELEMENTS; c++) mData[c] -= v[c]; return *static_cast<TYPE*>(this); } // this *= DATATYPE returns reference to first vector template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::operator*=(const DATATYPE& v) { for (int c=0; c<ELEMENTS; c++) mData[c] *= v; return *static_cast<TYPE*>(this); } // this /= DATATYPE returns reference to first vector template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::operator/=(const DATATYPE& v) { for (int c=0; c<ELEMENTS; c++) mData[c] *= v; return *static_cast<TYPE*>(this); } // unary minus template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline TYPE TVector<DATATYPE, ELEMENTS, TYPE>::operator-() const { TYPE r; for (int c=0; c < ELEMENTS; c++) r[c] = -mData[c]; return r; } // equality template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline bool TVector<DATATYPE, ELEMENTS, TYPE>::operator==(const TYPE& v) const { return (0==memcmp(this,& v, sizeof(*this))); } // inequality template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline bool TVector<DATATYPE, ELEMENTS, TYPE>::operator!=(const TYPE& v) const { return (0!=memcmp(this,& v, sizeof(*this))); } // generic dot product template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline DATATYPE TVector<DATATYPE, ELEMENTS, TYPE>::Dot(const TYPE& v) const { DATATYPE r = mData[0] * v[0]; for (int c=1; c < ELEMENTS; c++) r += mData[c] * v[c]; return r; } // Normalize vector template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline const TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::Normalize() { DATATYPE length = Length(); DATATYPE lengthInv = DATATYPE(DATATYPE(1) / length); for (int c=0; c<ELEMENTS; c++) mData[c] *= lengthInv; return *static_cast<TYPE*>(this); } template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline TYPE TVector<DATATYPE, ELEMENTS, TYPE>::Normalized() const { TYPE r; DATATYPE length = Length(); DATATYPE lengthInv = DATATYPE(DATATYPE(1) / length); for (int c=0; c<ELEMENTS; c++) r[c] = mData[c] * lengthInv; return r; } // Squared length determination: template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline DATATYPE TVector<DATATYPE, ELEMENTS, TYPE>::SquareLength() const { DATATYPE r = mData[0] * mData[0]; for (int c=1; c<ELEMENTS; c++) r += mData[c] * mData[c]; return r; } // Linear Interpolate. Interpolated from one vector to the other template <typename DATATYPE, int ELEMENTS, typename TYPE> f_inline TYPE TVector<DATATYPE, ELEMENTS, TYPE>::LinearInterpolate(const TYPE& to, float t) const { float it = 1.0f - t; TYPE r; for (int c=0; c<ELEMENTS; c++) r[c] = mData[c] * it + to[c] * t; return r; } } //namespace salt #endif //SALT_TVECTOR_H --- NEW FILE: path.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: path.h,v 1.1 2005/12/05 20:56:00 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Path HISTORY: 13.06.2002 MK - initial version 31.08.2002 MK - moved to salt */ #ifndef SALT_PATH_H #define SALT_PATH_H #ifdef HAVE_CONFIG_H # ifdef PACKAGE_BUGREPORT # undef PACKAGE_BUGREPORT # endif # ifdef PACKAGE_NAME # undef PACKAGE_NAME # endif # ifdef PACKAGE_STRING # undef PACKAGE_STRING # endif # ifdef PACKAGE_TARNAME # undef PACKAGE_TARNAME # endif # ifdef PACKAGE_VERSION # undef PACKAGE_VERSION # endif #include "config.h" #endif #include <string> #include <list> namespace salt { /** This class serves as a helper for path strings within the object * hierarchy. It is capable of cleaning paths and separating a path * into path components. */ class Path { // // types // public: typedef std::list<std::string> TStringList; // // functions // public: /** constructs a path object from a string, using the Set * method */ Path(const std::string &path = ""); /** Sets the managed path expression. The path gets tokenized * and can be read element by element using Back(), Front(), * PopBack() and PopFront() */ void Set(const std::string &path); /** returns true if the managed path expression denotes an * absoulute path, i.e. has a leading slash */ bool IsAbsolute() const; /** returns the first path component */ const std::string& Front() const; /** returns and removes the first path component */ void PopFront(); /** returns the last path component */ const std::string& Back() const; /** returns and removes the last path component */ void PopBack(); /** returns true, if no path components remain, i.e. all path * components are popped */ bool IsEmpty() const; /** returns a cleaned path expression, removing superfluous * separators */ std::string GetCleanPath(const std::string &sep = "/") const; private: Path(const Path &obj); Path& operator=(const Path &obj); /** tokenizes the path components */ void Tokenize(const std::string &path); // // members // public: protected: /** the list of tokenized path components */ TStringList mPathComponents; /** indicates an absolute path expression */ bool mIsAbsolute; private: }; } //namespace salt #endif //SALT_PATH_H --- NEW FILE: bounds.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: bounds.h,v 1.1 2005/12/05 20:56:00 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef SALT_BOUNDS_H #define SALT_BOUNDS_H // #ifdef HAVE_CONFIG_H // #include <config.h> // #endif #include "defines.h" #include "matrix.h" #include "vector.h" #include <cfloat> namespace salt { /** AABB3 provides an axis aligned three dimensional bounding box */ class AABB3 { public: // constructors /** constructs an empty bounding box */ f_inline AABB3() { Init(); } /** constructs a bounding box encapsulating mn and mx */ f_inline AABB3(const Vector3f &mn, const Vector3f &mx) { Init(); Encapsulate(mn); Encapsulate(mx); } // inline functions /** sets minVec and maxVec to describe an empty bounding box */ f_inline void Init() { minVec.Set(FLT_MAX, FLT_MAX, FLT_MAX); maxVec.Set(-FLT_MAX, -FLT_MAX, -FLT_MAX); } /** encapsulates the Vector v, growing the box if necessary */ f_inline void Encapsulate(const Vector3f &v) { minVec.x() = gMin(minVec.x(), v.x()); minVec.y() = gMin(minVec.y(), v.y()); minVec.z() = gMin(minVec.z(), v.z()); maxVec.x() = gMax(maxVec.x(), v.x()); maxVec.y() = gMax(maxVec.y(), v.y()); maxVec.z() = gMax(maxVec.z(), v.z()); } /** encapsulates the Vector <x,y,z>, growing the box if necessary */ f_inline void Encapsulate(const float x, const float y, const float z) { minVec.x() = gMin(minVec.x(), x); minVec.y() = gMin(minVec.y(), y); minVec.z() = gMin(minVec.z(), z); maxVec.x() = gMax(maxVec.x(), x); maxVec.y() = gMax(maxVec.y(), y); maxVec.z() = gMax(maxVec.z(), z); } /** encapsulates another box, growing the box if necessary */ f_inline void Encapsulate(const AABB3 &box) { Encapsulate(box.minVec); Encapsulate(box.maxVec);} /** grows the box evenly with delta along all axis */ f_inline void Widen(float delta) { minVec.x()-=delta; minVec.y()-=delta; minVec.z()-=delta; maxVec.x()+=delta; maxVec.y()+=delta; maxVec.z()+=delta; } /** moves the box along the vector v */ f_inline void Translate(const Vector3f &v) { minVec+=v; maxVec+=v; } /** returns true if he box contains the vector v */ f_inline bool Contains(const Vector3f &v) const { return (gInRange(v.x(), minVec.x(), maxVec.x()) && gInRange(v.z(), minVec.z(), maxVec.z()) && gInRange(v.y(), minVec.y(), maxVec.y())); } /** returns true if the box contains the box b */ f_inline bool Contains(const AABB3 &b) const { return (Contains(b.minVec) && Contains(b.maxVec)); } /** returns true if this box and the box b have some space in common */ f_inline bool Intersects(const AABB3 &b) const { return !(minVec.x() > b.maxVec.x() || maxVec.x() < b.minVec.x() || minVec.y() > b.maxVec.y() || maxVec.y() < b.minVec.y() || minVec.z() > b.maxVec.z() || maxVec.z() < b.minVec.z()); } /** calculates the current width of the box */ f_inline float GetWidth() const { return gAbs(minVec.x()-maxVec.x()); } /** calculates the current height of the box */ f_inline float GetHeight() const { return gAbs(minVec.y()-maxVec.y()); } /** calculates the current depth of the box */ f_inline float GetDepth() const { return gAbs(minVec.z()-maxVec.z()); } /** calculates the center point of the box */ f_inline Vector3f GetMiddle() const { return Vector3f((minVec.x()+maxVec.x())*0.5f, (minVec.y()+maxVec.y())*0.5f, (minVec.z()+maxVec.z())*0.5f); } /** calculates the distance from the center point to one of the corners, * i.e the radius of the bounding sphere through the center. */ f_inline float GetRadius() const { return ((maxVec-minVec)*0.5).Length(); } // get distance from middle of bounding box to one of the corners // (i.e. radius of bounding sphere through Middle()). /* multiplies the box with the given matrix */ void TransformBy(Matrix& matrix); // attributes /** a vector describing the lower corner of the box */ Vector3f minVec; /** a vector describing the higher corner of the box */ Vector3f maxVec; }; /** AABB2 provides an axis aligned two dimensional bounding box */ class AABB2 { public: // constructors /** constructs an empty bounding box */ f_inline AABB2() { Init(); } /** constructs a bounding box encapsulating mn and mx */ f_inline AABB2(const Vector2f &mn, const Vector2f &mx) { Init(); Encapsulate(mn); Encapsulate(mx); } // inline functions /** sets minVec and maxVec to describe an empty bounding box */ f_inline void Init() { minVec.Set(FLT_MAX, FLT_MAX); maxVec.Set(-FLT_MAX, -FLT_MAX); } /** encapsulates the Vector v, growing the box if necessary */ f_inline void Encapsulate(const Vector2f &v) { minVec.x() = gMin(minVec.x(), v.x()); minVec.y() = gMin(minVec.y(), v.y()); maxVec.x() = gMax(maxVec.x(), v.x()); maxVec.y() = gMax(maxVec.y(), v.y()); } /** encapsulates another box, growing the box if necessary */ f_inline void Encapsulate(const AABB2 &box) { Encapsulate(box.minVec); Encapsulate(box.maxVec);} /** grows the box evenly with delta along both axis */ f_inline void Widen(float delta) { minVec.x()-=delta; minVec.y()-=delta; maxVec.x()+=delta; maxVec.y()+=delta; } /** moves the box along the vector v */ f_inline void Translate(const Vector2f &v) { minVec+=v; maxVec+=v; } /** returns true if he box contains the vector v */ f_inline bool Contains(const Vector2f &v) const { return (gInRange(v.x(), minVec.x(), maxVec.x()) && gInRange(v.y(), minVec.y(), maxVec.y())); } /** returns true if the box contains the box b */ f_inline bool Contains(const AABB2 &b) const { return (Contains(b.minVec) && Contains(b.maxVec)); } /** returns true if this box and the box b have some space in common */ f_inline bool Intersects(const AABB2 &b) const { return !(minVec.x() > b.maxVec.x() || maxVec.x() < b.minVec.x() || minVec.y() > b.maxVec.y() || maxVec.y() < b.minVec.y()); } /** calculates the current width of the box */ f_inline float GetWidth() const // get width of bounding box { return gAbs(minVec.x()-maxVec.x()); } /** calculates the current height of the box */ f_inline float GetHeight() const // get height of bounding box { return gAbs(minVec.y()-maxVec.y()); } /** calculates the center point of the box */ f_inline Vector2f GetMiddle() const { return Vector2f((minVec.x()+maxVec.x())*0.5f, (minVec.y()+maxVec.y())*0.5f); } /** calculates the distance from the center point to one of the corners, * i.e the radius of the bounding sphere through the center. */ f_inline float GetRadius() const { return ((maxVec-minVec)*0.5).Length(); } // attributes /** a vector describing the lower corner of the box */ Vector2f minVec; /** a vector describing the higher corner of the box */ Vector2f maxVec; }; /** BoundingSphere provides a three dimensional sphere */ class BoundingSphere { public: // constructors /** constructs an empty sphere */ f_inline BoundingSphere() : center(Vector3f(0,0,0)), radius(0.0f), radiusSq(0.0f) {} /** constructs a sphere around pos with the radius rad */ f_inline BoundingSphere(const Vector3f &pos, float rad) : center(pos), radius(rad), radiusSq(rad*rad) {} /** Constructs a bounding sphere * \param pos The position where the sphere is constructed * \param rad is the radius of the sphere * \param radSq is the user supplied square of rad */ f_inline BoundingSphere(const Vector3f &pos, float rad, float radSq) : center(pos), radius(rad), radiusSq(radSq) {} // inline functions /** encapsulates the vector v in the sphere, growing it if neccesary. * this method is fast but not accurate */ f_inline void EncapsulateFast(const Vector3f &v) { Vector3f diff=(center - v); float dist=diff.Dot(diff); // not accurate if (dist>radiusSq) { radiusSq=dist; radius=gSqrt(dist); }} /** returns true if the sphere contains the vector v */ f_inline bool Contains(const Vector3f &v) { return ((center - v).SquareLength() < radiusSq); } /** returns true if the sphere contains the sphere s */ f_inline bool Contains(const BoundingSphere &s) const { return (radius >= s.radius) && ((center - s.center).SquareLength() < (radius - s.radius) * (radius - s.radius)); } /** returns true if this sphere and the sphere s intersect */ f_inline bool Intersects(const BoundingSphere &s) const { return ((center - s.center).SquareLength() < (radius + s.radius) * (radius + s.radius)); } // non-inline functions /** encapsulates the vector v in the sphere, growing it if neccesary. * this method is accurate but slower than EncapsulateFast */ void Encapsulate(const Vector3f &v); /** returns true, if the sphere contains the axis aligned bounding box b */ bool Contains(const AABB3 &b) const; /** returns true, if the sphere and the axis aligned bounding box b intersect */ bool Intersects(const AABB3 &b) const; // attributes /** describes the center of the sphere */ Vector3f center; /** describes the radius of the sphere */ float radius; /** the square of the sphere radius. The value ist either accuratly calculated * in the constructor or user supplied and may be inaccurate. */ float radiusSq; }; } // namespace salt #endif // SALT_BOUNDS_H --- NEW FILE: vector.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: vector.h,v 1.1 2005/12/05 20:56:00 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Here we define/complete the vector definitions of TVector into specific vector types (2d float, 3d float, etc...). HISTORY: 12.08.2002 MK - initial version */ #ifndef SALT_VECTOR_H #define SALT_VECTOR_H // #ifdef HAVE_CONFIG_H // #include <config.h> // #endif #include "tvector.h" #include <cstdlib> #include <climits> namespace salt { class Vector2f : public TVector2<float, Vector2f> { // // Methods // public: /// Construction/Destruction/Assignment f_inline Vector2f() : TVector2<float, Vector2f>() { } f_inline Vector2f(float x, float y) : TVector2<float, Vector2f>(x, y) { } explicit Vector2f(const float *f) : TVector2<float, Vector2f>() { SetData(f); } f_inline Vector2f(const Vector2f &v) : TVector2<float, Vector2f>(v) { } explicit Vector2f(float f) : TVector2<float, Vector2f>() { Fill(f); } float GetAngleRad() const { const double length = Length(); if (length == 0) { return 0.0; } double rad = gArcCos(Get(0) / length); if (Get(1) < 0) { rad = 2*M_PI - rad; } return rad; } float GetAngleDeg() const { return gRadToDeg(GetAngleRad()); } }; class Vector3f : public TVector3<float, Vector3f> { // // Methods // public: /// Construction/Destruction/Assignment f_inline Vector3f() : TVector3<float, Vector3f>() { } f_inline Vector3f(float x, float y, float z) : TVector3<float, Vector3f>(x, y, z) { } explicit Vector3f(const float *f) : TVector3<float, Vector3f>() { SetData(f); } f_inline Vector3f(const Vector3f &v) : TVector3<float, Vector3f>(v) { } explicit Vector3f(float f) : TVector3<float, Vector3f>() { Fill(f); } f_inline Vector3f Reflect(const Vector3f &normal) { float fac = 2.0f * (normal.x() * x() + normal.y() * y() + normal.z() * z()); return Vector3f(fac * normal.x()-x(), fac * normal.y()-y(), fac * normal.z()-z()); } f_inline void RandomUnitVector() { x() = 1.0f - 2.0f*rand()/(float)RAND_MAX; y() = 1.0f - 2.0f*rand()/(float)RAND_MAX; z() = 1.0f - 2.0f*rand()/(float)RAND_MAX; Normalize(); } }; } //namespace salt #endif //SALT_VECTOR_H --- NEW FILE: matrix.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2002,2003 Koblenz University Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group $Id: matrix.h,v 1.1 2005/12/05 20:56:00 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef SALT_MATRIX_H #define SALT_MATRIX_H // #ifdef HAVE_CONFIG_H // #include <config.h> // #endif #include "defines.h" #include "vector.h" #include <memory.h> namespace salt { #if 0 } #endif /** Matrix ... [truncated message content] |