summaryrefslogtreecommitdiff
path: root/CSXCAD/src/CSPrimSphere.cpp
blob: a372fe3216167d70a3365bf2622807c88657753d (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
*	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 "CSPrimSphere.h"
#include "CSProperties.h"
#include "CSUseful.h"

CSPrimSphere::CSPrimSphere(unsigned int ID, ParameterSet* paraSet, CSProperties* prop) : CSPrimitives(ID,paraSet,prop)
{
	Type=SPHERE;
	m_Center.SetParameterSet(paraSet);
	psRadius.SetParameterSet(paraSet);
	PrimTypeName = std::string("Sphere");
}

CSPrimSphere::CSPrimSphere(CSPrimSphere* sphere, CSProperties *prop) : CSPrimitives(sphere,prop)
{
	Type=SPHERE;
	m_Center.Copy(&sphere->m_Center);
	psRadius.Copy(&sphere->psRadius);
	PrimTypeName = std::string("Sphere");
}

CSPrimSphere::CSPrimSphere(ParameterSet* paraSet, CSProperties* prop) : CSPrimitives(paraSet,prop)
{
	Type=SPHERE;
	m_Center.SetParameterSet(paraSet);
	psRadius.SetParameterSet(paraSet);
	PrimTypeName = std::string("Sphere");
}


CSPrimSphere::~CSPrimSphere()
{
}

void CSPrimSphere::SetCenter(double x1, double x2, double x3)
{
	SetCoord(0,x1);
	SetCoord(1,x2);
	SetCoord(2,x3);
}

void CSPrimSphere::SetCenter(double x[3])
{
	for (int n=0;n<3;++n)
		SetCoord(n, x[n]);
}

void CSPrimSphere::SetCenter(std::string x1, std::string x2, std::string x3)
{
	SetCoord(0,x1);
	SetCoord(1,x2);
	SetCoord(2,x3);
}

void CSPrimSphere::SetCenter(std::string x[3])
{
	for (int n=0;n<3;++n)
		SetCoord(n, x[n]);
}


bool CSPrimSphere::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();
	for (unsigned int i=0;i<3;++i)
	{
		dBoundBox[2*i]=center[i]-radius;
		dBoundBox[2*i+1]=center[i]+radius;
	}
	if (radius>0)
		m_Dimension=3;
	else
		m_Dimension=0;
	return true;
}

bool CSPrimSphere::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 (dist<psRadius.GetValue())
		return true;
	return false;
}

bool CSPrimSphere::Update(std::string *ErrStr)
{
	int EC=0;
	bool bOK=m_Center.Evaluate(ErrStr);
	if (bOK==false)
	{
		std::stringstream stream;
		stream << std::endl << "Error in " << PrimTypeName << " Center Point (ID: " << uiID << "): ";
		ErrStr->append(stream.str());
	}
	m_Center.SetCoordinateSystem(m_PrimCoordSystem, m_MeshType);

	EC=psRadius.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 << " Radius (ID: " << uiID << "): ";
		ErrStr->append(stream.str());
		PSErrorCode2Msg(EC,ErrStr);
	}

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

	return bOK;
}

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

	WriteTerm(psRadius,elem,"Radius",parameterised);

	TiXmlElement Center("Center");
	m_Center.Write2XML(&Center,parameterised);
	elem.InsertEndChild(Center);
	return true;
}

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

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

	TiXmlElement* Center=root.FirstChildElement("Center");
	if (m_Center.ReadFromXML(Center) == false)	return false;

	return true;
}

void CSPrimSphere::ShowPrimitiveStatus(std::ostream& stream)
{
	CSPrimitives::ShowPrimitiveStatus(stream);
	stream << "  Center: " << m_Center.GetValueString(0) << "," << m_Center.GetValueString(1) << "," << m_Center.GetValueString(2) << std::endl;
	stream << "  Radius: " << psRadius.GetValueString() << std::endl;
}