Skip to content
Snippets Groups Projects
Element.cpp 2.99 KiB
Newer Older
  • Learn to ignore specific revisions
  •  * Copyright (c) 2012, OpenGeoSys Community (http://www.opengeosys.com)
    
    Lars Bilke's avatar
    Lars Bilke committed
     *            Distributed under a Modified BSD License.
     *              See accompanying file LICENSE.txt or
    
     *              http://www.opengeosys.com/LICENSE.txt
    
    Lars Bilke's avatar
    Lars Bilke committed
     *
     *
    
    Lars Bilke's avatar
    Lars Bilke committed
     * \file Element.cpp
    
    Lars Bilke's avatar
    Lars Bilke committed
     * Created on 2012-05-02 by Karsten Rink
    
     */
    
    #include "Element.h"
    #include "Node.h"
    
    Element::Element(Node** nodes, MshElemType::type type, unsigned value)
    
    Element::Element(MshElemType::type type, unsigned value)
    
    	: _nodes(NULL), _type(type), _value(value)
    
    {
    }
    
    Element::~Element()
    {
    	delete[] this->_nodes;
    
    bool Element::addNeighbor(Element* e)
    {
    	unsigned n(0);
    	unsigned nNeighbors (this->getNNeighbors());
    	for (n=0; n<nNeighbors; n++)
    	{
    		if (this->_neighbors[n] == e)
    			return false;
    		if (this->_neighbors[n] == NULL)
    			break;
    	}
    
    	if (n<nNeighbors)
    	{
    		const unsigned nNodes (this->getNNodes());
    		const unsigned eNodes (e->getNNodes());
    		const Node* const* e_nodes = e->getNodes();
    		unsigned count(0);
    		for (unsigned i(0); i<nNodes; i++)
    			for (unsigned j(0); j<eNodes; j++)
    				if (_nodes[i] == e_nodes[j])
    					//std::cout << _nodes[i]->getID() << " == " << e_nodes[j]->getID() << std::endl;
    					// increment shared nodes counter and check if enough nodes are similar to be sure e is a neighbour of this
    					if ((++count)>=this->getDimension())
    					{
    						_neighbors[n]=e;
    						return true;
    					}
    	}
    	return false;
    }
    
    
    const Element* Element::getEdge(unsigned i) const
    {
    	if (i < getNEdges())
    	{
    		Node** nodes = new Node*[2];
    		nodes[0] = getEdgeNode(i,0);
    		nodes[1] = getEdgeNode(i,1);
    		return new Edge(nodes);
    	}
    	std::cerr << "Error in MeshLib::Element::getEdge() - Index does not exist." << std::endl;
    	return NULL;
    }
    
    
    void Element::computeSqrEdgeLengthRange(double &min, double &max) const
    {
    	min = std::numeric_limits<double>::max();
    	max = std::numeric_limits<double>::min();
    	unsigned nEdges (this->getNEdges());
    	for (unsigned i=0; i<nEdges; i++)
    	{
    
    		double dist (MathLib::sqrDist(getEdgeNode(i,0)->getCoords(), getEdgeNode(i,1)->getCoords()));
    
    		min = (dist<min) ? dist : min;
    		max = (dist>max) ? dist : max;
    	}
    }
    
    
    const Element* Element::getNeighbor(unsigned i) const
    
    	if (i < getNNeighbors())
    		return _neighbors[i];
    	std::cerr << "Error in MeshLib::Element::getNeighbor() - Index does not exist." << std::endl;
    	return NULL;
    
    const Node* Element::getNode(unsigned i) const
    
    	if (i < getNNodes())
    		return _nodes[i];
    	std::cerr << "Error in MeshLib::Element::getNode() - Index does not exist." << std::endl;
    	return NULL;
    
    Lars Bilke's avatar
    Lars Bilke committed
    unsigned Element::getNodeIndex(unsigned i) const
    
    	if (i<getNNodes())
    		return _nodes[i]->getID();
    	std::cerr << "Error in MeshLib::Element::getNodeIndex() - Index does not exist." << std::endl;
    
    	return std::numeric_limits<unsigned>::max();
    
    bool Element::hasNeighbor(Element* elem) const
    {
    
    	unsigned nNeighbors (this->getNNeighbors());
    	for (unsigned i=0; i<nNeighbors; i++)
    
    		if (this->_neighbors[i]==elem)
    			return true;
    	return false;
    }