netgen/libsrc/gprim/geomobjects.hpp

398 lines
7.8 KiB
C++
Raw Normal View History

2009-01-13 04:40:13 +05:00
#ifndef FILE_OBJECTS
#define FILE_OBJECTS
/* *************************************************************************/
/* File: geomobjects.hpp */
/* Author: Joachim Schoeberl */
/* Date: 20. Jul. 02 */
/* *************************************************************************/
2011-02-28 19:17:25 +05:00
namespace netgen
2009-01-13 04:40:13 +05:00
{
template <int D, typename T = double> class Vec;
template <int D, typename T = double> class Point;
2009-01-13 04:40:13 +05:00
template <int D, typename T>
2016-10-30 19:01:52 +05:00
class Point : public AlignedAlloc<Point<D,T>>
2009-01-13 04:40:13 +05:00
{
2011-02-28 19:17:25 +05:00
protected:
T x[D];
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
public:
Point () { ; }
Point (T ax) { for (int i = 0; i < D; i++) x[i] = ax; }
Point (T ax, T ay)
2014-08-20 01:58:36 +06:00
{
// static_assert(D==2, "Point<D> constructor with 2 args called");
x[0] = ax; x[1] = ay;
}
Point (T ax, T ay, T az)
2014-08-20 01:58:36 +06:00
{
// static_assert(D==3, "Point<D> constructor with 3 args called");
x[0] = ax; x[1] = ay; x[2] = az;
}
Point (T ax, T ay, T az, T au)
2011-02-28 19:17:25 +05:00
{ x[0] = ax; x[1] = ay; x[2] = az; x[3] = au;}
2009-01-13 04:40:13 +05:00
2016-10-30 19:01:52 +05:00
template <typename T2>
Point (const Point<D,T2> & p2)
{ for (int i = 0; i < D; i++) x[i] = p2(i); }
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
explicit Point (const Vec<D> & v)
{ for (int i = 0; i < D; i++) x[i] = v(i); }
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
Point & operator= (const Point<D> & p2)
{
for (int i = 0; i < D; i++) x[i] = p2.x[i];
return *this;
}
2009-01-13 04:40:13 +05:00
Point & operator= (T val)
2011-02-28 19:17:25 +05:00
{
for (int i = 0; i < D; i++) x[i] = val;
return *this;
}
2009-01-13 04:40:13 +05:00
T & operator() (int i) { return x[i]; }
const T & operator() (int i) const { return x[i]; }
2009-01-13 04:40:13 +05:00
operator const T* () const { return x; }
2011-02-28 19:17:25 +05:00
};
2009-01-13 04:40:13 +05:00
template <int D, typename T>
2016-10-30 19:01:52 +05:00
class Vec : public AlignedAlloc<Vec<D,T>>
2009-01-13 04:40:13 +05:00
{
2011-02-28 19:17:25 +05:00
protected:
T x[D];
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
public:
Vec () { ; } // for (int i = 0; i < D; i++) x[i] = 0; }
Vec (T ax) { for (int i = 0; i < D; i++) x[i] = ax; }
Vec (T ax, T ay)
2014-08-20 01:58:36 +06:00
{
// static_assert(D==2, "Vec<D> constructor with 2 args called");
x[0] = ax; x[1] = ay;
}
Vec (T ax, T ay, T az)
2014-08-20 01:58:36 +06:00
{
// static_assert(D==3, "Vec<D> constructor with 3 args called");
x[0] = ax; x[1] = ay; x[2] = az;
}
Vec (T ax, T ay, T az, T au)
2011-02-28 19:17:25 +05:00
{ x[0] = ax; x[1] = ay; x[2] = az; x[3] = au; }
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
Vec (const Vec<D> & p2)
{ for (int i = 0; i < D; i++) x[i] = p2.x[i]; }
2009-01-13 04:40:13 +05:00
2016-10-30 19:01:52 +05:00
explicit Vec (const Point<D,T> & p)
2011-02-28 19:17:25 +05:00
{ for (int i = 0; i < D; i++) x[i] = p(i); }
2009-01-13 04:40:13 +05:00
2016-10-30 19:01:52 +05:00
Vec (const Vec & p1, const Vec & p2)
2011-02-28 19:17:25 +05:00
{ for(int i=0; i<D; i++) x[i] = p2(i)-p1(1); }
2009-01-13 04:40:13 +05:00
2016-10-30 19:01:52 +05:00
Vec & operator= (const Vec & p2)
2011-02-28 19:17:25 +05:00
{
for (int i = 0; i < D; i++) x[i] = p2.x[i];
return *this;
}
2009-01-13 04:40:13 +05:00
Vec & operator= (T s)
2011-02-28 19:17:25 +05:00
{
for (int i = 0; i < D; i++) x[i] = s;
return *this;
}
2009-01-13 04:40:13 +05:00
T & operator() (int i) { return x[i]; }
const T & operator() (int i) const { return x[i]; }
2009-01-13 04:40:13 +05:00
operator const T* () const { return x; }
2009-01-13 04:40:13 +05:00
T Length () const
2011-02-28 19:17:25 +05:00
{
T l = 0;
2011-02-28 19:17:25 +05:00
for (int i = 0; i < D; i++)
l += x[i] * x[i];
return sqrt (l);
}
2009-01-13 04:40:13 +05:00
T Length2 () const
2011-02-28 19:17:25 +05:00
{
T l = 0;
2011-02-28 19:17:25 +05:00
for (int i = 0; i < D; i++)
l += x[i] * x[i];
return l;
}
2016-10-30 19:01:52 +05:00
Vec & Normalize ()
2011-02-28 19:17:25 +05:00
{
T l = Length();
2016-10-30 19:01:52 +05:00
// if (l != 0)
for (int i = 0; i < D; i++)
x[i] /= (l+1e-40);
2011-02-28 19:17:25 +05:00
return *this;
}
Vec<D> GetNormal () const;
};
2009-01-13 04:40:13 +05:00
template <int H, int W=H, typename T = double>
2016-10-30 19:01:52 +05:00
class Mat : public AlignedAlloc<Mat<H,W,T>>
2009-01-13 04:40:13 +05:00
{
2011-02-28 19:17:25 +05:00
protected:
T x[H*W];
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
public:
Mat () { ; }
Mat (const Mat & b)
{ for (int i = 0; i < H*W; i++) x[i] = b.x[i]; }
Mat & operator= (T s)
2011-02-28 19:17:25 +05:00
{
for (int i = 0; i < H*W; i++) x[i] = s;
return *this;
}
Mat & operator= (const Mat & b)
{
for (int i = 0; i < H*W; i++) x[i] = b.x[i];
return *this;
}
T & operator() (int i, int j) { return x[i*W+j]; }
const T & operator() (int i, int j) const { return x[i*W+j]; }
T & operator() (int i) { return x[i]; }
const T & operator() (int i) const { return x[i]; }
2011-02-28 19:17:25 +05:00
Vec<H,T> Col (int i) const
2011-02-28 19:17:25 +05:00
{
Vec<H,T> hv;
2011-02-28 19:17:25 +05:00
for (int j = 0; j < H; j++)
hv(j) = x[j*W+i];
return hv;
}
Vec<W,T> Row (int i) const
2011-02-28 19:17:25 +05:00
{
Vec<W,T> hv;
2011-02-28 19:17:25 +05:00
for (int j = 0; j < W; j++)
hv(j) = x[i*W+j];
return hv;
}
void Solve (const Vec<H,T> & rhs, Vec<W,T> & sol) const
2011-02-28 19:17:25 +05:00
{
Mat<W,H,T> inv;
2011-02-28 19:17:25 +05:00
CalcInverse (*this, inv);
sol = inv * rhs;
}
};
2009-01-13 04:40:13 +05:00
2010-09-23 20:07:12 +06:00
2011-02-28 19:17:25 +05:00
template <int D>
class Box
2010-09-23 20:07:12 +06:00
{
2011-02-28 19:17:25 +05:00
protected:
Point<D> pmin, pmax;
public:
Box () { ; }
2010-09-23 20:07:12 +06:00
2011-02-28 19:17:25 +05:00
Box ( const Point<D> & p1)
{
for (int i = 0; i < D; i++)
pmin(i) = pmax(i) = p1(i);
}
2010-09-23 20:07:12 +06:00
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
Box ( const Point<D> & p1, const Point<D> & p2)
{
for (int i = 0; i < D; i++)
{
pmin(i) = min2(p1(i), p2(i));
pmax(i) = max2(p1(i), p2(i));
}
}
enum EB_TYPE { EMPTY_BOX = 1 };
Box ( EB_TYPE et )
{
pmin = Point<3> (1e99, 1e99, 1e99);
pmax = Point<3> (-1e99, -1e99, -1e99);
}
const Point<D> & PMin () const { return pmin; }
const Point<D> & PMax () const { return pmax; }
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
void Set (const Point<D> & p)
{ pmin = pmax = p; }
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
void Add (const Point<D> & p)
{
for (int i = 0; i < D; i++)
{
if (p(i) < pmin(i)) pmin(i) = p(i);
else if (p(i) > pmax(i)) pmax(i) = p(i);
}
}
2013-04-03 02:31:02 +06:00
template <typename T1, typename T2>
void Set (const IndirectArray<T1, T2> & points)
{
Set (points[points.Begin()]);
for (int i = points.Begin()+1; i < points.End(); i++)
Add (points[i]);
}
template <typename T1, typename T2>
void Add (const IndirectArray<T1, T2> & points)
{
for (int i = points.Begin(); i < points.End(); i++)
Add (points[i]);
}
2011-02-28 19:17:25 +05:00
Point<D> Center () const
{
Point<D> c;
for (int i = 0; i < D; i++)
c(i) = 0.5 * (pmin(i)+pmax(i));
return c;
}
double Diam () const { return Abs (pmax-pmin); }
Point<D> GetPointNr (int nr) const
{
Point<D> p;
for (int i = 0; i < D; i++)
{
p(i) = (nr & 1) ? pmax(i) : pmin(i);
nr >>= 1;
}
return p;
}
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
bool Intersect (const Box<D> & box2) const
{
for (int i = 0; i < D; i++)
if (pmin(i) > box2.pmax(i) ||
pmax(i) < box2.pmin(i)) return 0;
return 1;
}
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
bool IsIn (const Point<D> & p) const
{
for (int i = 0; i < D; i++)
if (p(i) < pmin(i) || p(i) > pmax(i)) return 0;
return 1;
}
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
void Increase (double dist)
{
for (int i = 0; i < D; i++)
{
pmin(i) -= dist;
pmax(i) += dist;
}
}
2009-01-13 04:40:13 +05:00
};
2011-02-28 19:17:25 +05:00
template <int D>
class BoxSphere : public Box<D>
2009-01-13 04:40:13 +05:00
{
2011-02-28 19:17:25 +05:00
protected:
///
Point<D> c;
///
double diam;
///
double inner;
public:
///
BoxSphere () { };
///
BoxSphere (const Box<D> & box)
: Box<D> (box)
{
CalcDiamCenter();
};
///
BoxSphere ( Point<D> apmin, Point<D> apmax )
: Box<D> (apmin, apmax)
{
CalcDiamCenter();
}
///
const Point<D> & Center () const { return c; }
///
double Diam () const { return diam; }
///
double Inner () const { return inner; }
///
void GetSubBox (int nr, BoxSphere & sbox) const
{
for (int i = 0; i < D; i++)
{
if (nr & 1)
{
sbox.pmin(i) = c(i);
sbox.pmax(i) = this->pmax(i);
}
else
{
sbox.pmin(i) = this->pmin(i);
sbox.pmax(i) = c(i);
}
sbox.c(i) = 0.5 * (sbox.pmin(i) + sbox.pmax(i));
nr >>= 1;
}
sbox.diam = 0.5 * diam;
sbox.inner = 0.5 * inner;
}
///
void CalcDiamCenter ()
{
c = Box<D>::Center ();
diam = Dist (this->pmin, this->pmax);
inner = this->pmax(0) - this->pmin(0);
for (int i = 1; i < D; i++)
if (this->pmax(i) - this->pmin(i) < inner)
inner = this->pmax(i) - this->pmin(i);
}
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
};
2009-01-13 04:40:13 +05:00
2011-02-28 19:17:25 +05:00
}
2009-01-13 04:40:13 +05:00
#endif