1 /*
2 * Copyright (C) 2008-2012 Thorsten Liebig (Thorsten.Liebig@gmx.de)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <sstream>
19 #include <iostream>
20 #include <limits>
21 #include "tinyxml.h"
22 #include "stdint.h"
23
24 #include "CSPrimSphere.h"
25 #include "CSProperties.h"
26 #include "CSUseful.h"
27
CSPrimSphere(unsigned int ID,ParameterSet * paraSet,CSProperties * prop)28 CSPrimSphere::CSPrimSphere(unsigned int ID, ParameterSet* paraSet, CSProperties* prop) : CSPrimitives(ID,paraSet,prop)
29 {
30 Type=SPHERE;
31 m_Center.SetParameterSet(paraSet);
32 psRadius.SetParameterSet(paraSet);
33 PrimTypeName = std::string("Sphere");
34 }
35
CSPrimSphere(CSPrimSphere * sphere,CSProperties * prop)36 CSPrimSphere::CSPrimSphere(CSPrimSphere* sphere, CSProperties *prop) : CSPrimitives(sphere,prop)
37 {
38 Type=SPHERE;
39 m_Center.Copy(&sphere->m_Center);
40 psRadius.Copy(&sphere->psRadius);
41 PrimTypeName = std::string("Sphere");
42 }
43
CSPrimSphere(ParameterSet * paraSet,CSProperties * prop)44 CSPrimSphere::CSPrimSphere(ParameterSet* paraSet, CSProperties* prop) : CSPrimitives(paraSet,prop)
45 {
46 Type=SPHERE;
47 m_Center.SetParameterSet(paraSet);
48 psRadius.SetParameterSet(paraSet);
49 PrimTypeName = std::string("Sphere");
50 }
51
52
~CSPrimSphere()53 CSPrimSphere::~CSPrimSphere()
54 {
55 }
56
SetCenter(double x1,double x2,double x3)57 void CSPrimSphere::SetCenter(double x1, double x2, double x3)
58 {
59 SetCoord(0,x1);
60 SetCoord(1,x2);
61 SetCoord(2,x3);
62 }
63
SetCenter(double x[3])64 void CSPrimSphere::SetCenter(double x[3])
65 {
66 for (int n=0;n<3;++n)
67 SetCoord(n, x[n]);
68 }
69
SetCenter(std::string x1,std::string x2,std::string x3)70 void CSPrimSphere::SetCenter(std::string x1, std::string x2, std::string x3)
71 {
72 SetCoord(0,x1);
73 SetCoord(1,x2);
74 SetCoord(2,x3);
75 }
76
SetCenter(std::string x[3])77 void CSPrimSphere::SetCenter(std::string x[3])
78 {
79 for (int n=0;n<3;++n)
80 SetCoord(n, x[n]);
81 }
82
83
GetBoundBox(double dBoundBox[6],bool PreserveOrientation)84 bool CSPrimSphere::GetBoundBox(double dBoundBox[6], bool PreserveOrientation)
85 {
86 UNUSED(PreserveOrientation); //has no orientation or preserved anyways
87 const double* center = m_Center.GetCartesianCoords();
88 m_BoundBox_CoordSys=CARTESIAN;
89 double radius = psRadius.GetValue();
90 for (unsigned int i=0;i<3;++i)
91 {
92 dBoundBox[2*i]=center[i]-radius;
93 dBoundBox[2*i+1]=center[i]+radius;
94 }
95 if (radius>0)
96 m_Dimension=3;
97 else
98 m_Dimension=0;
99 return true;
100 }
101
IsInside(const double * Coord,double)102 bool CSPrimSphere::IsInside(const double* Coord, double /*tol*/)
103 {
104 if (Coord==NULL) return false;
105 double out[3];
106 const double* center = m_Center.GetCartesianCoords();
107 TransformCoordSystem(Coord,out,m_MeshType,CARTESIAN);
108 if (m_Transform)
109 m_Transform->InvertTransform(out,out);
110 double dist=sqrt(pow(out[0]-center[0],2)+pow(out[1]-center[1],2)+pow(out[2]-center[2],2));
111 if (dist<psRadius.GetValue())
112 return true;
113 return false;
114 }
115
Update(std::string * ErrStr)116 bool CSPrimSphere::Update(std::string *ErrStr)
117 {
118 int EC=0;
119 bool bOK=m_Center.Evaluate(ErrStr);
120 if (bOK==false)
121 {
122 std::stringstream stream;
123 stream << std::endl << "Error in " << PrimTypeName << " Center Point (ID: " << uiID << "): ";
124 ErrStr->append(stream.str());
125 }
126 m_Center.SetCoordinateSystem(m_PrimCoordSystem, m_MeshType);
127
128 EC=psRadius.Evaluate();
129 if (EC!=ParameterScalar::PS_NO_ERROR) bOK=false;
130 if ((EC!=ParameterScalar::PS_NO_ERROR) && (ErrStr!=NULL))
131 {
132 bOK=false;
133 std::stringstream stream;
134 stream << std::endl << "Error in " << PrimTypeName << " Radius (ID: " << uiID << "): ";
135 ErrStr->append(stream.str());
136 PSErrorCode2Msg(EC,ErrStr);
137 }
138
139 //update local bounding box
140 m_BoundBoxValid = GetBoundBox(m_BoundBox);
141
142 return bOK;
143 }
144
Write2XML(TiXmlElement & elem,bool parameterised)145 bool CSPrimSphere::Write2XML(TiXmlElement &elem, bool parameterised)
146 {
147 CSPrimitives::Write2XML(elem,parameterised);
148
149 WriteTerm(psRadius,elem,"Radius",parameterised);
150
151 TiXmlElement Center("Center");
152 m_Center.Write2XML(&Center,parameterised);
153 elem.InsertEndChild(Center);
154 return true;
155 }
156
ReadFromXML(TiXmlNode & root)157 bool CSPrimSphere::ReadFromXML(TiXmlNode &root)
158 {
159 if (CSPrimitives::ReadFromXML(root)==false) return false;
160
161 TiXmlElement *elem = root.ToElement();
162 if (elem==NULL) return false;
163 if (ReadTerm(psRadius,*elem,"Radius")==false) return false;
164
165 TiXmlElement* Center=root.FirstChildElement("Center");
166 if (m_Center.ReadFromXML(Center) == false) return false;
167
168 return true;
169 }
170
ShowPrimitiveStatus(std::ostream & stream)171 void CSPrimSphere::ShowPrimitiveStatus(std::ostream& stream)
172 {
173 CSPrimitives::ShowPrimitiveStatus(stream);
174 stream << " Center: " << m_Center.GetValueString(0) << "," << m_Center.GetValueString(1) << "," << m_Center.GetValueString(2) << std::endl;
175 stream << " Radius: " << psRadius.GetValueString() << std::endl;
176 }
177