Skip to content
Snippets Groups Projects
Commit c129153e authored by Karsten Rink's avatar Karsten Rink
Browse files

Added basic meshlib classes for nodes, elements and meshes.

parent 2967035c
No related branches found
No related tags found
No related merge requests found
Showing
with 754 additions and 0 deletions
...@@ -33,6 +33,7 @@ ENDIF() # GCC AND GPROF_PATH ...@@ -33,6 +33,7 @@ ENDIF() # GCC AND GPROF_PATH
ADD_SUBDIRECTORY( Base ) ADD_SUBDIRECTORY( Base )
ADD_SUBDIRECTORY( GeoLib ) ADD_SUBDIRECTORY( GeoLib )
ADD_SUBDIRECTORY( MathLib ) ADD_SUBDIRECTORY( MathLib )
ADD_SUBDIRECTORY( MeshLib )
ADD_SUBDIRECTORY( SimpleTests/MatrixTests ) ADD_SUBDIRECTORY( SimpleTests/MatrixTests )
IF(NOT MSVC) IF(NOT MSVC)
ADD_SUBDIRECTORY( SimpleTests/SolverTests ) ADD_SUBDIRECTORY( SimpleTests/SolverTests )
......
...@@ -86,4 +86,28 @@ double getAngle (const double p0[3], const double p1[3], const double p2[3]) ...@@ -86,4 +86,28 @@ double getAngle (const double p0[3], const double p1[3], const double p2[3])
return acos (scpr (v0,v1,3) / (sqrt(scpr(v0,v0,3)) * sqrt(scpr (v1,v1,3)))); return acos (scpr (v0,v1,3) / (sqrt(scpr(v0,v0,3)) * sqrt(scpr (v1,v1,3))));
} }
double calcDetTriangle(const double p0[3], const double p1[3], const double p2[3])
{
const double u0 (p2[0] - p0[0]);
const double u1 (p2[1] - p0[1]);
const double u2 (p2[2] - p0[2]);
const double v0 (p1[0] - p0[0]);
const double v1 (p1[1] - p0[1]);
const double v2 (p1[2] - p0[2]);
const double z0 (u1*v2 - u2*v1);
const double z1 (u2*v0 - u0*v2);
const double z2 (u0*v1 - u1*v0);
return 0.5 * sqrt(z0*z0 + z1*z1 + z2 * z2);
}
double calcDetTetrahedron(const double* x1, const double* x2, const double* x3, const double* x4)
{
return fabs((x1[0] - x4[0]) * ((x2[1] - x4[1]) * (x3[2] - x4[2]) - (x2[2] - x4[2]) * (x3[1] - x4[1]))
- (x1[1] - x4[1]) * ((x2[0] - x4[0]) * (x3[2] - x4[2]) - (x2[2] - x4[2]) * (x3[0] - x4[0]))
+ (x1[2] - x4[2]) * ((x2[0] - x4[0]) * (x3[1] - x4[1]) - (x2[1] - x4[1]) * (x3[0] - x4[0]))) / 6.0;
}
} // namespace } // namespace
...@@ -94,6 +94,10 @@ float normalize(float min, float max, float val); ...@@ -94,6 +94,10 @@ float normalize(float min, float max, float val);
*/ */
double getAngle (const double p0[3], const double p1[3], const double p2[3]); double getAngle (const double p0[3], const double p1[3], const double p2[3]);
double calcDetTriangle(const double p0[3], const double p1[3], const double p2[3]);
double calcDetTetrahedron(const double* x1, const double* x2, const double* x3, const double* x4);
/** /**
* simple power function that takes as a second argument an integer instead of a float * simple power function that takes as a second argument an integer instead of a float
* @param base basis of the expression * @param base basis of the expression
......
# Source files
GET_SOURCE_FILES(SOURCES_MESHLIB)
SET ( SOURCES ${SOURCES_MESHLIB})
GET_SOURCE_FILES(SOURCES_ELEMENTS Elements)
SET ( SOURCES ${SOURCES} ${SOURCES_ELEMENTS})
# Create the library
ADD_LIBRARY(MeshLib STATIC ${SOURCES})
include_directories(
.
../Base
../GeoLib
../MathLib
)
target_link_libraries (MeshLib
Base
GeoLib
MathLib
)
/**
* Cell.cpp
*
* Date: 2012/05/02
* Author: KR
*/
#include "Cell.h"
namespace MeshLib {
Cell::Cell(Node** nodes, MshElemType::type type, size_t value)
: Element(nodes, type, value)
{
}
Cell::Cell(MshElemType::type type, size_t value)
: Element(type, value)
{
}
Cell::~Cell()
{
}
}
\ No newline at end of file
/**
* Cell.h
*
* Date: 2012/05/02
* Author: KR
*/
#ifndef CELL_H_
#define CELL_H_
#include "Element.h"
namespace MeshLib {
/**
* Virtual base class for 3d mesh elements.
*/
class Cell : public Element
{
public:
virtual double getVolume() const { return _volume; };
size_t getDimension() const { return 3; };
virtual ~Cell();
protected:
Cell(Node** nodes, MshElemType::type type, size_t value = 0);
Cell(MshElemType::type type, size_t value = 0);
virtual double calcVolume() = 0;
double _volume;
}; /* class */
} /* namespace */
#endif /* CELL_H_ */
/**
* Edge.cpp
*
* Date: 2012/05/02
* Author: KR
*/
#include "Edge.h"
#include "Node.h"
#include "MathTools.h"
namespace MeshLib {
Edge::Edge(Node* nodes[2], size_t value)
: Element(nodes, MshElemType::LINE, value)
{
this->_length = this->calcLength();
}
Edge::Edge(Node* n0, Node* n1, size_t value)
: Element(MshElemType::LINE, value)
{
Node* nodes[2] = { n0, n1 };
_nodes = nodes;
this->_length = this->calcLength();
}
Edge::Edge(const Edge &edge)
: Element(MshElemType::LINE, edge.getValue())
{
Node* nodes[2] = { new Node(*edge.getNode(0)), new Node(*edge.getNode(1)) };
_length = edge.getLength();
}
Edge::~Edge()
{
}
double Edge::calcLength()
{
return sqrt(MathLib::sqrDist(_nodes[0]->getData(), _nodes[1]->getData()));
}
}
\ No newline at end of file
/**
* Edge.h
*
* Date: 2012/05/02
* Author: KR
*/
#ifndef EDGE_H_
#define EDGE_H_
#include "Element.h"
namespace MeshLib {
class Node;
/**
* A 1d Edge or Line Element.
*
* Edge: o--------o
* 0 1
*/
class Edge : public Element
{
public:
Edge(Node* nodes[2], size_t value = 0);
Edge(Node* n1, Node* n2, size_t value = 0);
Edge(const Edge &edge);
virtual ~Edge();
double getLength() const { return _length; };
size_t getDimension() const { return 1; };
size_t getNNodes() const { return 2; };
protected:
double calcLength();
double _length;
}; /* class */
} /* namespace */
#endif /* EDGE_H_ */
/**
* Element.cpp
*
* Date: 2012/05/02
* Author: KR
*/
#include "Element.h"
#include "Node.h"
#include <cassert>
namespace MeshLib {
Element::Element(Node** nodes, MshElemType::type type, size_t value)
: _nodes(nodes), _type(type), _value(value)
{
}
Element::Element(MshElemType::type type, size_t value)
: _type(type), _value(value)
{
}
Element::~Element()
{
delete[] this->_nodes;
}
const Node* Element::getNode(size_t i) const
{
assert(i<getNNodes() && "Error in MeshLib::Element - Index does not exist.");
return _nodes[i];
}
size_t Element::getNodeIndex(size_t i) const
{
assert(i<getNNodes() && "Error in MeshLib::Element - Index does not exist.");
return _nodes[i]->getID();
}
}
\ No newline at end of file
/**
* Element.h
*
* Date: 2012/05/02
* Author: KR
*/
#ifndef ELEMENT_H_
#define ELEMENT_H_
#include <vector>
#include "MshEnums.h"
namespace MeshLib {
class Node;
/**
* Virtual base class for mesh elements.
*/
class Element
{
public:
const Node* getNode(size_t i) const;
Node* const* getNodes() const { return _nodes; };
virtual size_t getDimension() const = 0;
virtual size_t getNNodes() const = 0;
size_t getNodeIndex(size_t i) const;
MshElemType::type getType() const { return _type; };
size_t getValue() const { return _value; };
virtual ~Element();
protected:
Element(Node** nodes, MshElemType::type type, size_t value = 0);
Element(MshElemType::type type, size_t value = 0);
MshElemType::type _type;
size_t _value;
Node** _nodes;
std::vector<Element*> _neighbors;
private:
}; /* class */
} /* namespace */
#endif /* ELEMENT_H_ */
/**
* Face.cpp
*
* Date: 2012/05/02
* Author: KR
*/
#include "Face.h"
namespace MeshLib {
Face::Face(Node** nodes, MshElemType::type type, size_t value)
: Element(nodes, type, value)
{
}
Face::Face(MshElemType::type type, size_t value)
: Element(type, value)
{
}
Face::~Face()
{
}
}
\ No newline at end of file
/**
* Face.h
*
* Date: 2012/05/02
* Author: KR
*/
#ifndef FACE_H_
#define FACE_H_
#include "Element.h"
namespace MeshLib {
/**
* Virtual base class for 2d mesh elements.
*/
class Face : public Element
{
public:
virtual double getArea() const { return _area; };
size_t getDimension() const { return 2; };
virtual ~Face();
protected:
Face(Node** nodes, MshElemType::type type, size_t value = 0);
Face(MshElemType::type type, size_t value = 0);
virtual double calcArea() = 0;
double _area;
private:
}; /* class */
} /* namespace */
#endif /* FACE_H_ */
/**
* Hex.cpp
*
* Date: 2012/05/02
* Author: KR
*/
#include "Hex.h"
#include "Node.h"
#include "MathTools.h"
namespace MeshLib {
Hex::Hex(Node* nodes[8], size_t value)
: Cell(nodes, MshElemType::HEXAHEDRON, value)
{
this->_volume = this->calcVolume();
}
Hex::Hex(const Hex &hex)
: Cell(MshElemType::HEXAHEDRON, hex.getValue())
{
Node* nodes[8] = { new Node(*hex.getNode(0)), new Node(*hex.getNode(1)), new Node(*hex.getNode(2)), new Node(*hex.getNode(3)),
new Node(*hex.getNode(4)), new Node(*hex.getNode(5)), new Node(*hex.getNode(6)), new Node(*hex.getNode(7)) };
_volume = hex.getVolume();
}
Hex::~Hex()
{
}
double Hex::calcVolume()
{
return MathLib::calcDetTetrahedron(_nodes[4]->getData(), _nodes[7]->getData(), _nodes[5]->getData(), _nodes[0]->getData())
+ MathLib::calcDetTetrahedron(_nodes[5]->getData(), _nodes[3]->getData(), _nodes[1]->getData(), _nodes[0]->getData())
+ MathLib::calcDetTetrahedron(_nodes[5]->getData(), _nodes[7]->getData(), _nodes[3]->getData(), _nodes[0]->getData())
+ MathLib::calcDetTetrahedron(_nodes[5]->getData(), _nodes[7]->getData(), _nodes[6]->getData(), _nodes[2]->getData())
+ MathLib::calcDetTetrahedron(_nodes[1]->getData(), _nodes[3]->getData(), _nodes[5]->getData(), _nodes[2]->getData())
+ MathLib::calcDetTetrahedron(_nodes[3]->getData(), _nodes[7]->getData(), _nodes[5]->getData(), _nodes[2]->getData());
}
}
\ No newline at end of file
/**
* Hex.h
*
* Date: 2012/05/02
* Author: KR
*/
#ifndef HEX_H_
#define HEX_H_
#include "Cell.h"
namespace MeshLib {
/**
* A 3d Hexahedron Element.
*
* Hex: 7 6
* o--------o
* /: /|
* / : / |
* 4 / : 5 / |
* o--------o |
* | o....|...o 2
* | .3 | /
* | . | /
* |. |/
* o--------o
* 0 1
*/
class Hex : public Cell
{
public:
Hex(Node* nodes[8], size_t value = 0);
Hex(const Hex &hex);
virtual ~Hex();
size_t getNNodes() const { return 8; };
protected:
/// Calculates the volume of a convex hexahedron by partitioning it into six tetrahedra.
double calcVolume();
}; /* class */
} /* namespace */
#endif /* HEX_H_ */
/**
* Prism.cpp
*
* Date: 2012/05/02
* Author: KR
*/
#include "Prism.h"
#include "Node.h"
#include "MathTools.h"
namespace MeshLib {
Prism::Prism(Node* nodes[6], size_t value)
: Cell(nodes, MshElemType::PRISM, value)
{
this->_volume = this->calcVolume();
}
Prism::Prism(Node* n0, Node* n1, Node* n2, Node* n3, Node* n4, Node* n5, size_t value)
: Cell(MshElemType::PRISM, value)
{
Node* nodes[6] = { n0, n1, n2, n3, n4, n5 };
_nodes = nodes;
this->_volume = this->calcVolume();
}
Prism::Prism(const Prism &prism)
: Cell(MshElemType::PRISM, prism.getValue())
{
Node* nodes[6] = { new Node(*prism.getNode(0)), new Node(*prism.getNode(1)), new Node(*prism.getNode(2)),
new Node(*prism.getNode(3)), new Node(*prism.getNode(4)), new Node(*prism.getNode(5)) };
_volume = prism.getVolume();
}
Prism::~Prism()
{
}
double Prism::calcVolume()
{
return MathLib::calcDetTetrahedron(_nodes[0]->getData(), _nodes[1]->getData(), _nodes[2]->getData(), _nodes[3]->getData())
+ MathLib::calcDetTetrahedron(_nodes[1]->getData(), _nodes[4]->getData(), _nodes[2]->getData(), _nodes[3]->getData())
+ MathLib::calcDetTetrahedron(_nodes[2]->getData(), _nodes[4]->getData(), _nodes[5]->getData(), _nodes[3]->getData());
}
}
\ No newline at end of file
/**
* Prism.h
*
* Date: 2012/05/02
* Author: KR
*/
#ifndef PRISM_H_
#define PRISM_H_
#include "Cell.h"
namespace MeshLib {
/**
* A 3d Prism Element.
*
* Prism: 5
* o
* /:\
* / : \
* / o \
* 3 o-------o 4
* | . 2 . |
* |. .|
* o-------o
* 0 1
*/
class Prism : public Cell
{
public:
Prism(Node* nodes[6], size_t value = 0);
Prism(Node* n0, Node* n1, Node* n2, Node* n3, Node* n4, Node* n5, size_t value = 0);
Prism(const Prism &prism);
virtual ~Prism();
size_t getNNodes() const { return 6; };
protected:
/// Calculates the volume of a prism by subdividing it into three tetrahedra.
double calcVolume();
}; /* class */
} /* namespace */
#endif /* PRISM_H_ */
/**
* Pyramid.cpp
*
* Date: 2012/05/02
* Author: KR
*/
#include "Pyramid.h"
#include "Node.h"
#include "MathTools.h"
namespace MeshLib {
Pyramid::Pyramid(Node* nodes[5], size_t value)
: Cell(nodes, MshElemType::PYRAMID, value)
{
this->_volume = this->calcVolume();
}
Pyramid::Pyramid(Node* n0, Node* n1, Node* n2, Node* n3, Node* n4, size_t value)
: Cell(MshElemType::PYRAMID, value)
{
Node* nodes[5] = { n0, n1, n2, n3, n4 };
_nodes = nodes;
this->_volume = this->calcVolume();
}
Pyramid::Pyramid(const Pyramid &prism)
: Cell(MshElemType::PYRAMID, prism.getValue())
{
Node* nodes[5] = { new Node(*prism.getNode(0)), new Node(*prism.getNode(1)), new Node(*prism.getNode(2)),
new Node(*prism.getNode(3)), new Node(*prism.getNode(4)) };
_volume = prism.getVolume();
}
Pyramid::~Pyramid()
{
}
double Pyramid::calcVolume()
{
return MathLib::calcDetTetrahedron(_nodes[0]->getData(), _nodes[1]->getData(), _nodes[2]->getData(), _nodes[4]->getData())
+ MathLib::calcDetTetrahedron(_nodes[2]->getData(), _nodes[3]->getData(), _nodes[0]->getData(), _nodes[4]->getData());
}
}
\ No newline at end of file
/**
* Pyramid.h
*
* Date: 2012/05/02
* Author: KR
*/
#ifndef PYRAMID_H_
#define PYRAMID_H_
#include "Cell.h"
namespace MeshLib {
/**
* A 3d Pyramid Element.
*
* Pyramid: o 4
* //|\
* // | \
* // | \
* 3 o/...|...o 2
* ./ | /
* ./ | /
* ./ |/
* o--------o
* 0 1
*/
class Pyramid : public Cell
{
public:
Pyramid(Node* nodes[5], size_t value = 0);
Pyramid(Node* n0, Node* n1, Node* n2, Node* n3, Node* n4, size_t value = 0);
Pyramid(const Pyramid &pyramid);
virtual ~Pyramid();
size_t getNNodes() const { return 5; };
protected:
/// Calculates the volume of a prism by subdividing it into two tetrahedra.
double calcVolume();
}; /* class */
} /* namespace */
#endif /* PYRAMID_H_ */
/**
* Quad.cpp
*
* Date: 2012/05/02
* Author: KR
*/
#include "Quad.h"
#include "Node.h"
#include "MathTools.h"
namespace MeshLib {
Quad::Quad(Node* nodes[4], size_t value)
: Face(nodes, MshElemType::TRIANGLE, value)
{
this->_area = this->calcArea();
}
Quad::Quad(Node* n0, Node* n1, Node* n2, Node* n3, size_t value)
: Face(MshElemType::TRIANGLE, value)
{
Node* nodes[4] = { n0, n1, n2, n3 };
_nodes = nodes;
this->_area = this->calcArea();
}
Quad::Quad(const Quad &quad)
: Face(MshElemType::QUAD, quad.getValue())
{
Node* nodes[4] = { new Node(*quad.getNode(0)), new Node(*quad.getNode(1)), new Node(*quad.getNode(2)), new Node(*quad.getNode(3)) };
_area = quad.getArea();
}
Quad::~Quad()
{
}
double Quad::calcArea()
{
return MathLib::calcDetTriangle(_nodes[0]->getData(), _nodes[1]->getData(), _nodes[2]->getData())
+ MathLib::calcDetTriangle(_nodes[2]->getData(), _nodes[3]->getData(), _nodes[0]->getData());
}
}
\ No newline at end of file
/**
* Quad.h
*
* Date: 2012/05/02
* Author: KR
*/
#ifndef QUAD_H_
#define QUAD_H_
#include "Face.h"
namespace MeshLib {
/**
* A 2d Quadliteral Element.
*
* 3 2
* QUAD4: o-----------o
* | |
* | |
* | |
* | |
* | |
* o-----------o
* 0 1
*/
class Quad : public Face
{
public:
Quad(Node* nodes[4], size_t value = 0);
Quad(Node* n0, Node* n1, Node* n2, Node* n3, size_t value = 0);
Quad(const Quad &quad);
virtual ~Quad();
size_t getNNodes() const { return 4; };
protected:
/// Calculates the area of a convex quadliteral by dividing it into two triangles.
double calcArea();
}; /* class */
} /* namespace */
#endif /* QUAD_H_ */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment