summaryrefslogtreecommitdiff
path: root/CSXCAD/src/CSPrimSphericalShell.cpp
blob: 73f2d826f4e7d464bb606cabba79fa989a005d8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
*	Copyright (C) 2008-2012 Thorsten Liebig (Thorsten.Liebig@gmx.de)
*
*	This program is free software: you can redistribute it and/or modify
*	it under the terms of the GNU Lesser General Public License as published
*	by the Free Software Foundation, either version 3 of the License, or
*	(at your option) any later version.
*
*	This program is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU Lesser General Public License for more details.
*
*	You should have received a copy of the GNU Lesser General Public License
*	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <sstream>
#include <iostream>
#include <limits>
#include "tinyxml.h"
#include "stdint.h"

#include "CSPrimSphericalShell.h"
#include "CSProperties.h"
#include "CSUseful.h"

CSPrimSphericalShell::CSPrimSphericalShell(unsigned int ID, ParameterSet* paraSet, CSProperties* prop) : CSPrimSphere(ID,paraSet,prop)
{
	Type=SPHERICALSHELL;
	PrimTypeName = std::string("SphericalShell");
	psShellWidth.SetParameterSet(paraSet);
}

CSPrimSphericalShell::CSPrimSphericalShell(CSPrimSphericalShell* sphere, CSProperties *prop) : CSPrimSphere(sphere,prop)
{
	Type=SPHERICALSHELL;
	PrimTypeName = std::string("SphericalShell");
	psShellWidth.Copy(&sphere->psShellWidth);
}

CSPrimSphericalShell::CSPrimSphericalShell(ParameterSet* paraSet, CSProperties* prop) : CSPrimSphere(paraSet,prop)
{
	Type=SPHERICALSHELL;
	PrimTypeName = std::string("SphericalShell");
	psShellWidth.SetParameterSet(paraSet);
}


CSPrimSphericalShell::~CSPrimSphericalShell()
{
}

bool CSPrimSphericalShell::GetBoundBox(double dBoundBox[6], bool PreserveOrientation)
{
	UNUSED(PreserveOrientation); //has no orientation or preserved anyways
	const double* center = m_Center.GetCartesianCoords();
	m_BoundBox_CoordSys=CARTESIAN;
	double radius = psRadius.GetValue();
	double shellwidth = psShellWidth.GetValue();
	for (unsigned int i=0;i<3;++i)
	{
		dBoundBox[2*i]=center[i]-radius-shellwidth/2.0;
		dBoundBox[2*i+1]=center[i]+radius+shellwidth/2.0;
	}
	if (shellwidth>0)
		m_Dimension=3;
	else if (radius>0)
		m_Dimension=1;
	else
		m_Dimension=0;
	return true;
}

bool CSPrimSphericalShell::IsInside(const double* Coord, double /*tol*/)
{
	if (Coord==NULL) return false;
	double out[3];
	const double* center = m_Center.GetCartesianCoords();
	TransformCoordSystem(Coord,out,m_MeshType,CARTESIAN);
	if (m_Transform)
		m_Transform->InvertTransform(out,out);
	double dist=sqrt(pow(out[0]-center[0],2)+pow(out[1]-center[1],2)+pow(out[2]-center[2],2));
	if (fabs(dist-psRadius.GetValue())< psShellWidth.GetValue()/2.0)
		return true;
	return false;
}

bool CSPrimSphericalShell::Update(std::string *ErrStr)
{
	int EC=0;
	bool bOK=CSPrimSphere::Update(ErrStr);

	EC=psShellWidth.Evaluate();
	if (EC!=ParameterScalar::NO_ERROR) bOK=false;
	if ((EC!=ParameterScalar::NO_ERROR)  && (ErrStr!=NULL))
	{
		bOK=false;
		std::stringstream stream;
		stream << std::endl << "Error in " << PrimTypeName << " shell-width (ID: " << uiID << "): ";
		ErrStr->append(stream.str());
		PSErrorCode2Msg(EC,ErrStr);
	}

	//update local bounding box
	m_BoundBoxValid = GetBoundBox(m_BoundBox);

	return bOK;
}

bool CSPrimSphericalShell::Write2XML(TiXmlElement &elem, bool parameterised)
{
	CSPrimSphere::Write2XML(elem,parameterised);

	WriteTerm(psShellWidth,elem,"ShellWidth",parameterised);
	return true;
}

bool CSPrimSphericalShell::ReadFromXML(TiXmlNode &root)
{
	if (CSPrimSphere::ReadFromXML(root)==false) return false;

	TiXmlElement *elem = root.ToElement();
	if (elem==NULL) return false;
	if (ReadTerm(psShellWidth,*elem,"ShellWidth")==false) return false;

	return true;
}

void CSPrimSphericalShell::ShowPrimitiveStatus(std::ostream& stream)
{
	CSPrimSphere::ShowPrimitiveStatus(stream);
	stream << "  Shell width: " << psShellWidth.GetValueString() << std::endl;
}