$treeview $search $mathjax
Palabos  Version 1.1
$projectbrief
$projectbrief
$searchbox

triangularSurfaceMesh.h

Go to the documentation of this file.
00001 /* This file is part of the Palabos library.
00002  *
00003  * Copyright (C) 2011 FlowKit Sarl
00004  * Avenue de Chailly 23
00005  * 1012 Lausanne, Switzerland
00006  * E-mail contact: contact@flowkit.com
00007  *
00008  * The most recent release of Palabos can be downloaded at 
00009  * <http://www.palabos.org/>
00010  *
00011  * The library Palabos is free software: you can redistribute it and/or
00012  * modify it under the terms of the GNU Affero General Public License as
00013  * published by the Free Software Foundation, either version 3 of the
00014  * License, or (at your option) any later version.
00015  *
00016  * The library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Affero General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Affero General Public License
00022  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 
00025 /* Main author: Dimitrios Kontaxakis */
00026 
00027 #ifndef TRIANGULAR_SURFACE_MESH_H
00028 #define TRIANGULAR_SURFACE_MESH_H
00029 
00030 #include "core/globalDefs.h"
00031 #include "core/geometry3D.h"
00032 #include <string>
00033 #include <vector>
00034 
00035 namespace plb {
00036 
00039 
00054 template<typename T>
00055 class TriangularSurfaceMesh {
00056 public:
00058     struct Edge {
00059         plint pv;  // Pointing Vertex.
00060         plint ne;  // Neighboring Edge.
00061     };
00064     struct Lid {
00065         // Comment: if the close-holes algorithm is to be made more
00066         //   general one day, you will need to (1) assign the proper
00067         //   value to numAddedVertices, and (2) replace centerVertex
00068         //   by something else and change all codes which access
00069         //   centerVertex.
00070         Lid() : numAddedVertices(1) { }
00071         plint firstTriangle;
00072         plint numTriangles;
00073         std::vector<plint> boundaryVertices;
00074         plint centerVertex;
00075         plint numAddedVertices;
00076     };
00077 public:
00086     TriangularSurfaceMesh (
00087             std::vector<Array<T,3> >& vertexList_,
00088             std::vector<plint>& emanatingEdgeList_,
00089             std::vector<Edge>& edgeList_,
00090             plint numVertices_ = -1 );
00091 
00094     plint getNumTriangles() const {
00095         return numTriangles;
00096     }
00099     plint getNumVertices() const {
00100         return numVertices;
00101     }
00103     void replaceVertex(plint iVertex, Array<T,3> const& newPosition);
00104 
00106 
00111     void resetVertices(Array<T,3> const& defaultVertex);
00112 
00115     Array<T,3> const& getVertex(plint iTriangle, int localVertex) const;
00117     Array<T,3> const& getVertex(plint iVertex) const;
00118     bool isValidVertex(plint iTriangle, int localVertex) const;
00119     bool isValidVertex(plint iVertex) const;
00120 
00122     void computeBoundingBox (
00123             Array<T,2>& xRange,
00124             Array<T,2>& yRange,
00125             Array<T,2>& zRange ) const;
00126 
00128     void translate(Array<T,3> const& vector);
00130     void scale(T alpha);
00139     void rotate(T phi, T theta, T psi);
00140 
00149     void smooth(plint maxiter = 1, T relax = 1.0, bool isMeasureWeighted = false);
00150 
00152     plint getVertexId(plint iTriangle, plint localVertex) const;
00154     std::vector<plint> getNeighborVertexIds(plint iVertex) const;
00156     std::vector<plint> getNeighborVertexIds(plint iVertex, plint jVertex) const;
00158     std::vector<plint> getNeighborTriangleIds(plint iVertex) const;
00162     std::vector<plint> getAdjacentTriangleIds(plint iTriangle) const;
00166     std::vector<plint> getAdjacentTriangleIds(plint iVertex, plint jVertex) const;
00167 
00171     Array<T,3> computeTriangleNormal(plint iTriangle, bool isAreaWeighted = false) const;
00172     Array<T,3> computeTriangleNormal(
00173             plint iVertex, plint jVertex, plint kVertex, bool isAreaWeighted = false) const;
00174 
00177     Array<T,3> computeEdgeNormal(
00178             plint iVertex, plint jVertex, bool isAreaWeighted = false) const;
00185     Array<T,3> computeVertexNormal(plint iVertex, bool isAreaWeighted = false) const;
00186 
00195     Array<T,3> computeContinuousNormal(
00196             Array<T,3> const& p, plint iTriangle, bool isAreaWeighted = false) const;
00197 
00199     T computeTriangleArea(plint iTriangle) const;
00200     T computeTriangleArea(plint iVertex, plint jVertex, plint kVertex) const;
00203     T computeEdgeArea(plint iVertex, plint jVertex) const;
00206     T computeVertexArea(plint iVertex) const;
00207 
00209     T computeEdgeLength(plint iVertex, plint jVertex) const;
00210 
00214     T computeDihedralAngle(plint iVertex, plint jVertex) const;
00215 
00218     T computeEdgeTileSpan(plint iVertex, plint jVertex) const;
00219 
00221     void writeAsciiSTL(std::string fname) const;
00223     void writeBinarySTL(std::string fname) const;
00224 
00228     bool isBoundaryVertex(plint iVertex) const;
00232     bool isInteriorVertex(plint iVertex) const;
00233 
00237     bool isBoundaryEdge(plint iVertex, plint jVertex) const;
00241     bool isInteriorEdge(plint iVertex, plint jVertex) const;
00242 
00255     int pointOnTriangle (
00256             Array<T,3> const& point1, Array<T,3> const& point2, int flag, plint iTriangle,
00257             Array<T,3>& intersection, Array<T,3>& normal, T& distance ) const;
00258 
00265     bool segmentIntersectsTriangle (
00266             Array<T,3> const& point1, Array<T,3> const& point2, plint iTriangle ) const;
00267 
00271     void distanceToEdgeLine (
00272             Array<T,3> const& point, plint iTriangle, plint whichEdge,
00273             T& distance, bool& intersectionIsInside ) const;
00274 
00280     void distanceToTrianglePlane (
00281             Array<T,3> const& point, plint iTriangle,
00282             T& distance, bool& intersectionIsInside, bool& pointIsBehind ) const;
00283 
00295     void distanceToTriangle (
00296             Array<T,3> const& point, plint iTriangle,
00297             T& distance, bool& pointIsBehind ) const;
00298 
00300     void reverseOrientation();
00301 
00307     std::vector<Lid> closeHoles();
00309     std::vector<std::vector<plint> > detectHoles();
00310     void avoidIntegerPositions();
00311     void avoidIntegerPosition(plint iVertex);
00312     void inflate(T amount=1.e-3);
00313 public:
00315     std::vector<Array<T,3> >const& vertices() const {
00316         PLB_PRECONDITION(vertexList);
00317         return *vertexList;
00318     }
00320     std::vector<plint> const& emanatingEdges() const {
00321         PLB_PRECONDITION(emanatingEdgeList);
00322         return *emanatingEdgeList;
00323     }
00325     std::vector<Edge> const& edges() const {
00326         PLB_PRECONDITION(edgeList);
00327         return *edgeList;
00328     }
00331     Array<T,3>& getVertex(plint iTriangle, int localVertex);
00333     Array<T,3>& getVertex(plint iVertex);
00334 private:
00337     void assertVertex(Array<T,3> const& vertex) const;
00339     std::vector<Array<T,3> >& vertices() {
00340         PLB_PRECONDITION(vertexList);
00341         return *vertexList;
00342     }
00344     std::vector<plint>& emanatingEdges() {
00345         PLB_PRECONDITION(emanatingEdgeList);
00346         return *emanatingEdgeList;
00347     }
00349     std::vector<Edge>& edges() {
00350         PLB_PRECONDITION(edgeList);
00351         return *edgeList;
00352     }
00353 private:
00355     Lid closeHole(std::vector<plint> const& hole);
00356 private:
00357     plint prev(plint iEdge) const;
00358     plint next(plint iEdge) const;
00359     plint changeEdgeId(plint iEdge) const;
00360 private:
00361     std::vector<Array<T,3> >* vertexList;
00362     std::vector<plint>* emanatingEdgeList;
00363     std::vector<Edge>* edgeList;
00364     plint numTriangles, numVertices;
00365 public:
00366     static const T eps0, eps1;
00367 };
00368 
00369 template<typename T>
00370 class LidLessThan {
00371 public:
00372     LidLessThan(plint mainDirection_, TriangularSurfaceMesh<T> const& mesh_)
00373         : mainDirection(mainDirection_),
00374           mesh(mesh_)
00375     { }
00376     bool operator()(typename TriangularSurfaceMesh<T>::Lid const& lid1,
00377                     typename TriangularSurfaceMesh<T>::Lid const& lid2) const
00378     {
00379         plint dim1 = mainDirection;
00380         plint dim2 = (dim1+1)%3;
00381         plint dim3 = (dim2+1)%3;
00382         // Compare on bary-center for main coordinate, and, in the unlikely
00383         //   case that these two floats are equal, on the two subsequent
00384         //   coordinates.
00385         Array<T,3> baryCenter1 = computeBaryCenter(mesh, lid1);
00386         Array<T,3> baryCenter2 = computeBaryCenter(mesh, lid2);
00387         return
00388           (baryCenter1[dim1] < baryCenter2[dim1]) || (
00389                   equiv(baryCenter1[dim1],baryCenter2[dim1]) && (
00390                       (baryCenter1[dim2] < baryCenter2[dim2]) || (
00391                           equiv(baryCenter1[dim2],baryCenter2[dim2]) &&
00392                           (baryCenter1[dim3] < baryCenter2[dim3]) ) ) ) ;
00393     }
00394     static bool equiv(T a, T b) {
00395         return !(a<b || b<a);
00396     }
00397 private:
00398     plint mainDirection;
00399     TriangularSurfaceMesh<T> const& mesh;
00400 };
00401 
00403 template<typename T>
00404 T toLatticeUnits (
00405         TriangularSurfaceMesh<T>& mesh,
00406         plint resolution, plint referenceDirection );
00407 
00408 
00409 
00410 /* ******* Lid operations ************************************************** */
00411 
00412 template<typename T>
00413 Array<T,3> computeBaryCenter (
00414         TriangularSurfaceMesh<T> const& mesh,
00415         typename TriangularSurfaceMesh<T>::Lid const& lid );
00416 
00417 template<typename T>
00418 void computeBoundingBox (
00419         TriangularSurfaceMesh<T> const& mesh,
00420         typename TriangularSurfaceMesh<T>::Lid const& lid,
00421         Array<T,2>& xLim, Array<T,2>& yLim, Array<T,2>& zLim );
00422 
00423 template<typename T>
00424 T computeInnerRadius (
00425         TriangularSurfaceMesh<T> const& mesh,
00426         typename TriangularSurfaceMesh<T>::Lid const& lid );
00427 
00428 template<typename T>
00429 T computeOuterRadius (
00430         TriangularSurfaceMesh<T> const& mesh,
00431         typename TriangularSurfaceMesh<T>::Lid const& lid );
00432 
00433 template<typename T>
00434 T computeArea (
00435         TriangularSurfaceMesh<T> const& mesh,
00436         typename TriangularSurfaceMesh<T>::Lid const& lid );
00437 
00438 template<typename T>
00439 Array<T,3> computeNormal (
00440         TriangularSurfaceMesh<T> const& mesh,
00441         typename TriangularSurfaceMesh<T>::Lid const& lid );
00442 
00443 template<typename T>
00444 void reCenter (
00445         TriangularSurfaceMesh<T>& mesh,
00446         typename TriangularSurfaceMesh<T>::Lid const& lid );
00447 
00448 } // namespace plb
00449 
00450 #endif  // TRIANGULAR_SURFACE_MESH_H