Path: blob/devel/ElmerGUI/netgen/libsrc/stlgeom/stltopology.hpp
3206 views
#ifndef FILE_STLTOPOLOGY1#define FILE_STLTOPOLOGY23/**************************************************************************/4/* File: stltopology.hpp */5/* Author: Joachim Schoeberl */6/* Author2: Johannes Gerstmayr */7/* Date: 26. Jul. 99 */8/**************************************************************************/910/*11The STLTopology contains topologic information as12triangle->point, point->triangles, triangle->edge, 2-points->edge,...13*/141516class STLGeometry;1718#define STLBASE 11920class STLPointIndex21{22int i;23public:24STLPointIndex () { ; }25STLPointIndex (int ai) : i(ai) { ; }26STLPointIndex & operator= (const STLPointIndex & ai) { i = ai.i; return *this; }27STLPointIndex & operator= (int ai) { i = ai; return *this; }28operator int () const { return i; }29STLPointIndex operator++ (int) { return i++; }30STLPointIndex operator-- (int) { return i--; }31};32333435class STLTrigIndex36{37int i;38public:39STLTrigIndex () { ; }40STLTrigIndex (int ai) : i(ai) { ; }41STLTrigIndex & operator= (const STLTrigIndex & ai) { i = ai.i; return *this; }42STLTrigIndex & operator= (int ai) { i = ai; return *this; }43operator int () const { return i; }44STLTrigIndex operator++ (int) { return i++; }45STLTrigIndex operator-- (int) { return i--; }46};474849505152// triangle structure for loading stl files53class STLReadTriangle54{55Vec<3> normal;56Point<3> pts[3];57public:58STLReadTriangle (const Point<3> * apts, const Vec<3> & anormal);59STLReadTriangle () {};60const Point<3> & operator[] (int i) const { return pts[i]; }61const Vec<3> & Normal() const { return normal; }62};63646566class STLTriangle67{68// topology edges of triangle, edge[i] opposite to point[i]69int topedges[3];70// neighbour triangles, trig[i] opposite to point[i]71int nbtrigs[2][3];72// normalized stored normal vector ??73Vec<3> normal;74// point numbers of triangle75int pts[3];76// front-side and back-side domains77int domains[2];787980public:8182Box<3> box;83Point<3> center;84double rad;85int facenum;8687struct88{89unsigned int toperror : 1;90} flags;9192939495STLTriangle (const int * apts);96STLTriangle () {pts[0]=0;pts[1]=0;pts[2]=0;}9798int operator[] (int i) const { return pts[i]; }99int & operator[] (int i) { return pts[i]; }100101int EdgeNum(int i) const { return topedges[(i-1)]; }102int & EdgeNum(int i) { return topedges[(i-1)]; }103104int NBTrig (bool side, int i) const { return nbtrigs[side][i]; }105int & NBTrig (bool side, int i) { return nbtrigs[side][i]; }106107108int Domain (bool side) const { return domains[side]; }109int & Domain (bool side) { return domains[side]; }110111112113// obsolete:114int PNum(int i) const { return pts[(i-1)]; }115int & PNum(int i) { return pts[(i-1)]; }116int PNumMod(int i) const { return pts[(i-1)%3]; }117int & PNumMod(int i) { return pts[(i-1)%3]; }118119int EdgeNumMod(int i) const { return topedges[(i-1)%3]; }120int & EdgeNumMod(int i) { return topedges[(i-1)%3]; }121122int NBTrigNum(int i) const { return nbtrigs[0][(i-1)]; }123int & NBTrigNum(int i) { return nbtrigs[0][(i-1)]; }124int NBTrigNumMod(int i) const { return nbtrigs[0][(i-1)%3]; }125int & NBTrigNumMod(int i) { return nbtrigs[0][(i-1)%3]; }126127128// consistently oriented neighbour:129int IsNeighbourFrom(const STLTriangle& t) const;130// opposite to consistently oriented neighbour:131int IsWrongNeighbourFrom(const STLTriangle& t) const;132133///Get the two points of neighbour-Triangles in orientation of this-Triangle134void GetNeighbourPoints(const STLTriangle& t, int& p1, int& p2) const;135int GetNeighbourPointsAndOpposite(const STLTriangle& t, int& p1, int& p2, int& po) const;136137138139// NON-normalized geometry - normal vector140Vec<3> GeomNormal(const ARRAY<Point<3> >& ap) const;141142// Stored normal vector, normalized143void SetNormal (const Vec<3> & n);144const Vec<3> & Normal () const { return normal; }145146147void ChangeOrientation();148149//project with a certain normal vector in plane150void ProjectInPlain(const ARRAY<Point<3> >& ap,151const Vec<3> & n, Point<3> & pp) const;152//project with the triangle's normal vector in plane153void ProjectInPlain(const ARRAY<Point<3> > & ap, Point<3> & pp) const;154155156/*157Project the point pp along the nproj into the plane of158the triangle. The triangle normal is given by ntrig to159avoid numerical instabilities.160The local coordinates lam are defined by161162pp(input) = P1 + lam1 v1 + lam2 v2 + lam3 n163164the result is165166pp(output) = P1 + lam1 v1 + lam2 v2167*/168int ProjectInPlain (const ARRAY<Point<3> >& ap,169const Vec<3> & nproj,170Point<3> & pp, Vec<3> & lam) const;171172int PointInside(const ARRAY<Point<3> >& ap, const Point<3> & pp) const;173174//get nearest point on triangle and distance to it175double GetNearestPoint(const ARRAY<Point<3> >& ap,176Point<3> & p3d) const;177178double Area(const ARRAY<Point<3> >& ap) const;179180double MinHeight(const ARRAY<Point<3> >& ap) const;181double MaxLength(const ARRAY<Point<3> >& ap) const;182//max length of a side of triangle183184int GetFaceNum() {return facenum;}185void SetFaceNum(int i) {facenum = i;}186187int HasEdge(int p1, int p2) const;188};189190191/**192Topology Edge:193Useful unside a face.194A edges sharing more than 2 faces: trigs are undefined195*/196class STLTopEdge197{198int pts[2];199int trigs[2];200double cosangle;201int status; // excluded, confirmed, candidate, undefined202public:203STLTopEdge ();204STLTopEdge (int p1, int p2, int trig1, int trig2);205206int operator[] (int i) const { return pts[i]; }207int & operator[] (int i) { return pts[i]; }208209210int PNum(int i) const { return pts[(i-1)]; }211int & PNum(int i) { return pts[(i-1)]; }212int PNumMod(int i) const { return pts[(i-1)%2]; }213int & PNumMod(int i) { return pts[(i-1)%2]; }214215int TrigNum(int i) const { return trigs[(i-1)]; }216int & TrigNum(int i) { return trigs[(i-1)]; }217int TrigNumMod(int i) const { return trigs[(i-1)%2]; }218int & TrigNumMod(int i) { return trigs[(i-1)%2]; }219220void SetCosAngle (double ca) { cosangle = ca; }221double CosAngle () const { return cosangle; }222double Angle () const { return acos (cosangle); }223224void SetStatus (int stat) { status = stat; }225int GetStatus () const { return status; }226};227228229230ostream& operator<<(ostream& os, const STLTriangle& t);231232233234235236237238class STLTopology239{240protected:241ARRAY<STLTriangle> trias;242ARRAY<STLTopEdge> topedges;243ARRAY<Point<3> > points;244245// mapping of sorted pair of points to topedge246INDEX_2_HASHTABLE<int> * ht_topedges;247// mapping of node to trigs248TABLE<int> trigsperpoint;249// mapping of node to edges250TABLE<int> topedgesperpoint;251252// searchtree for trigs and points253254Box3dTree * searchtree; // ADT255Point3dTree * pointtree;256257Box<3> boundingbox;258double pointtol;259260public:261enum STL_GEOM_STATUS { STL_GOOD, STL_WARNING, STL_ERROR };262263protected:264STL_GEOM_STATUS status;265string statustext;266267bool topology_ok;268bool orientation_ok;269270public:271STLTopology();272virtual ~STLTopology();273274static STLGeometry * LoadNaomi (istream & ist);275static STLGeometry * Load (istream & ist);276static STLGeometry * LoadBinary (istream & ist);277278void Save (const char* filename);279void SaveBinary (const char* filename, const char* aname);280void SaveSTLE (const char * filename); // stores trigs and edges281282virtual void InitSTLGeometry (const ARRAY<STLReadTriangle> & readtrigs);283284virtual void TopologyChanged() {}; //do some things, if topology changed!285286/// Generate topology tables287void FindNeighbourTrigs();288289290void GetTrianglesInBox (const Box<3> & box,291ARRAY<int> & trias) const;292293294int GetNP() const { return points.Size(); }295int AddPoint(const Point<3> & p) { return points.Append(p); }296const Point<3> & GetPoint(int nr) const { return points.Get(nr); }297int GetPointNum (const Point<3> & p);298void SetPoint(int nr, const Point<3> & p) { points.Elem(nr) = p; }299const ARRAY<Point<3> >& GetPoints() const { return points; }300301const Point<3> & operator[] (STLPointIndex i) const { return points[i]; }302Point<3> & operator[] (STLPointIndex i) { return points[i]; }303304305306307int GetNT() const { return trias.Size(); }308void AddTriangle(const STLTriangle& t);309const STLTriangle & GetTriangle (int nr) const { return trias.Get(nr); }310STLTriangle & GetTriangle (int nr) { return trias.Elem(nr); }311312const STLTriangle & operator[] (STLTrigIndex i) const { return trias[i]; }313STLTriangle & operator[] (STLTrigIndex i) { return trias[i]; }314315316int GetNTE() const { return topedges.Size(); }317const STLTopEdge & GetTopEdge (int nr) const { return topedges.Get(nr); }318STLTopEdge & GetTopEdge (int nr) { return topedges.Elem(nr); }319int GetTopEdgeNum (int pi1, int pi2) const;320321322int NOTrigsPerPoint(int pn) { return trigsperpoint.EntrySize(pn); }323int TrigPerPoint(int pn, int i) { return trigsperpoint.Get(pn, i); }324325326int NTopEdgesPerPoint (int pn) const { return topedgesperpoint.EntrySize(pn); }327int TopEdgePerPoint (int pn, int ei) const { return topedgesperpoint.Get(pn, ei); }328329330bool Topology_Ok() const { return topology_ok; }331bool Orientation_Ok() const { return orientation_ok; }332333STL_GEOM_STATUS GetStatus () const { return status; }334const string & GetStatusText () const { return statustext; }335336void InvertTrig (int trig);337void DeleteTrig (int trig);338void OrientAfterTrig (int trig);339340341// Table will be constructed, if topology is not ok342/// neighbourtrigs for surfacetrigs343TABLE<int> neighbourtrigs;344345/// get nr-th neighbour Triangle for triangle trig346int NONeighbourTrigs(int trig) const { return neighbourtrigs.EntrySize(trig); }347int NeighbourTrig(int trig, int nr) const { return neighbourtrigs.Get(trig,nr); }348int NeighbourTrigSorted(int trig, int nr) const;349void AddNeighbourTrig(int i, int nt) { neighbourtrigs.Add1(i, nt); }350351352353354int GetLeftTrig (int p1, int p2) const;355int GetRightTrig (int p1, int p2) const;356357const Box<3> & GetBoundingBox () const { return boundingbox; }358};359360361#endif362363364