1 /*
2 *	Copyright (C) 2008,2009,2010 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 #pragma once
19 /*
20  * Author:	Thorsten Liebig
21  * Date:	03-12-2008
22  * Lib:		CSXCAD
23  * Version:	0.1a
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string>
29 
30 #include "ParameterObjects.h"
31 #include "ParameterCoord.h"
32 #include "CSXCAD_Global.h"
33 
34 class CSPrimPoint;
35 class CSPrimBox;
36 class CSPrimMultiBox;
37 class CSPrimSphere;
38 	class CSPrimSphericalShell;
39 class CSPrimCylinder;
40 	class CSPrimCylindricalShell;
41 class CSPrimPolygon;
42 	class CSPrimLinPoly;
43 	class CSPrimRotPoly;
44 class CSPrimPolyhedron;
45 	class CSPrimPolyhedronReader;
46 class CSPrimCurve;
47 	class CSPrimWire;
48 class CSPrimUserDefined;
49 
50 class CSProperties; //include VisualProperties
51 
52 class TiXmlNode;
53 class CSFunctionParser;
54 class CSTransform;
55 
56 /*!
57 	Calculate the distance of a point to a line (defined by start/stop coordinates).
58 	Foot will return the normalized foot-point on the line. A value between 0..1 means the foot-point is one the given line.
59 	foot == 0 --> foot-point is on start, foot == 1 --> foot-point is on stop
60 */
61 void CSXCAD_EXPORT Point_Line_Distance(const double P[], const double start[], const double stop[], double &foot, double &dist, CoordinateSystem c_system=UNDEFINED_CS);
62 
63 bool CSXCAD_EXPORT CoordInRange(const double* p, const double* start, const double* stop, CoordinateSystem cs_in);
64 
65 //! Abstract base class for different geometrical primitives.
66 /*!
67  This is an abstract base class for different geometrical primitives like boxes, spheres, cylinders etc.
68  !!! Tolerances not yet obeyed !!!
69  */
70 class CSXCAD_EXPORT CSPrimitives
71 {
72 public:
73 	virtual ~CSPrimitives();
74 
75 	virtual void Init();
76 
77 	//! Primitive type definitions.
78 	enum PrimitiveType
79 	{
80 		POINT,BOX,MULTIBOX,SPHERE,SPHERICALSHELL,CYLINDER,CYLINDRICALSHELL,POLYGON,LINPOLY,ROTPOLY,POLYHEDRON,CURVE,WIRE,USERDEFINED,
81 		POLYHEDRONREADER
82 	};
83 
84 	//! Set or change the property for this primitive.
85 	void SetProperty(CSProperties *prop);
86 	//! Get the property for this primitive.
GetProperty()87 	CSProperties* GetProperty() {return clProperty;}
88 
89 	//! Getthe unique ID for this primitive.
GetID()90 	unsigned int GetID() {return uiID;}
91 	//! Change the unique ID for this primitive. This is not recommended! Be sure what you are doing!
SetID(unsigned int ID)92 	void SetID(unsigned int ID) {uiID=ID;}
93 
94 	//! Get the type of this primitive. \sa PrimitiveType
GetType()95 	int GetType() {return Type;}
96 
GetTypeName()97 	std::string GetTypeName() {return PrimTypeName;}
98 
99 	//! Create a copy of ths primitive with different property.
100 	virtual CSPrimitives* GetCopy(CSProperties *prop=NULL) {return new CSPrimitives(this,prop);}
101 
102 	//! Get the bounding box (for the given mesh type) for this special primitive. \sa GetBoundBoxCoordSystem
103 	virtual bool GetBoundBox(double dBoundBox[6], bool PreserveOrientation=false) {UNUSED(PreserveOrientation);UNUSED(dBoundBox);return false;}
104 
GetBoundBoxCoordSystem()105 	virtual CoordinateSystem GetBoundBoxCoordSystem() const {return m_BoundBox_CoordSys;}
106 
107 	//! Get the dimension of this primitive
GetDimension()108 	virtual int GetDimension() {return m_Dimension;}
109 
110 	//! Check if given Coordinate (in the given mesh type) is inside the Primitive.
111 	virtual bool IsInside(const double* Coord, double tol=0) {UNUSED(Coord);UNUSED(tol);return false;}
112 
113 	//! Check if the primitive is inside a given box (box must be specified in the bounding box coordinate system)
114 	//! @return -1 if not, +1 if it is, 0 if unknown
115 	virtual int IsInsideBox(const double*  boundbox);
116 
117 	//! Check whether this primitive was used. (--> IsInside() return true) \sa SetPrimitiveUsed
GetPrimitiveUsed()118 	bool GetPrimitiveUsed() {return m_Primtive_Used;}
119 	//! Set the primitve uses flag. \sa GetPrimitiveUsed
SetPrimitiveUsed(bool val)120 	void SetPrimitiveUsed(bool val) {m_Primtive_Used=val;}
121 
122 	//! Set or change the priotity for this primitive.
SetPriority(int val)123 	void SetPriority(int val) {iPriority=val;}
124 	//! Get the priotity for this primitive.
GetPriority()125 	int GetPriority() {return iPriority;}
126 
127 	//! Update this primitive with respect to the parameters set.
128 	virtual bool Update(std::string *ErrStr=NULL) {UNUSED(ErrStr);return true;}
129 	//! Write this primitive to a XML-node.
130 	virtual bool Write2XML(TiXmlElement &elem, bool parameterised=true);
131 	//! Read this primitive from a XML-node.
132 	virtual bool ReadFromXML(TiXmlNode &root);
133 
134 	//! Get the corresponing Box-Primitive or NULL in case of different type.
ToBox()135 	CSPrimBox* ToBox() { return ( this && Type == BOX ) ? (CSPrimBox*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
136 	//! Get the corresponing MultiBox-Primitive or NULL in case of different type.
ToMultiBox()137 	CSPrimMultiBox* ToMultiBox() { return ( this && Type == MULTIBOX ) ? (CSPrimMultiBox*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
138 	//! Get the corresponing Sphere-Primitive or NULL in case of different type.
ToSphere()139 	CSPrimSphere* ToSphere() { return ( this && Type == SPHERE ) ? (CSPrimSphere*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
140 	//! Get the corresponing SphereicalShell-Primitive or NULL in case of different type.
ToSphericalShell()141 	CSPrimSphericalShell* ToSphericalShell() { return ( this && Type == SPHERICALSHELL ) ? (CSPrimSphericalShell*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
142 	//! Get the corresponing Cylinder-Primitive or NULL in case of different type.
ToCylinder()143 	CSPrimCylinder* ToCylinder() { return ( this && Type == CYLINDER ) ? (CSPrimCylinder*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
144 	//! Get the corresponing CylindricalShell-Primitive or NULL in case of different type.
ToCylindricalShell()145 	CSPrimCylindricalShell* ToCylindricalShell() { return ( this && Type == CYLINDRICALSHELL ) ? (CSPrimCylindricalShell*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
146 	//! Get the corresponing Polygon-Primitive or NULL in case of different type.
ToPolygon()147 	CSPrimPolygon* ToPolygon() { return ( this && Type == POLYGON ) ? (CSPrimPolygon*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
148 	//! Get the corresponing LinPoly-Primitive or NULL in case of different type.
ToLinPoly()149 	CSPrimLinPoly* ToLinPoly() { return ( this && Type == LINPOLY ) ? (CSPrimLinPoly*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
150 	//! Get the corresponing Cylinder-Primitive or NULL in case of different type.
ToRotPoly()151 	CSPrimRotPoly* ToRotPoly() { return ( this && Type == ROTPOLY ) ? (CSPrimRotPoly*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
152 	//! Get the corresponing Polyhedron-Primitive or NULL in case of different type.
ToPolyhedron()153 	CSPrimPolyhedron* ToPolyhedron() { return ( this && Type == POLYHEDRON ) ? (CSPrimPolyhedron*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
154 	//! Get the corresponing Polyhedron-Import-Primitive or NULL in case of different type.
ToPolyhedronReader()155 	CSPrimPolyhedronReader* ToPolyhedronReader() { return ( this && Type == POLYHEDRONREADER ) ? (CSPrimPolyhedronReader*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
156 	//! Get the corresponing Curve-Primitive or NULL in case of different type.
ToCurve()157 	CSPrimCurve* ToCurve() { return ( this && Type == CURVE ) ? (CSPrimCurve*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
158 	//! Get the corresponing Wire-Primitive or NULL in case of different type.
ToWire()159 	CSPrimWire* ToWire() { return ( this && Type == WIRE ) ? (CSPrimWire*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
160 	//! Get the corresponing UserDefined-Primitive or NULL in case of different type.
ToUserDefined()161 	CSPrimUserDefined* ToUserDefined() { return ( this && Type == USERDEFINED ) ? (CSPrimUserDefined*) this : 0; } /// Cast Primitive to a more defined type. Will return null if not of the requested type.
162 	//! Get the corresponing Point-Primitive or 0 in case of different type.
ToPoint()163 	CSPrimPoint* ToPoint() { return ( this && Type == POINT ) ? (CSPrimPoint*) this : 0; } //!< Cast Primitive to a more defined type. Will return 0 if not of the requested type.
164 
165 	bool operator<(CSPrimitives& vgl) { return iPriority<vgl.GetPriority();}
166 	bool operator>(CSPrimitives& vgl) { return iPriority>vgl.GetPriority();}
167 	bool operator==(CSPrimitives& vgl) { return iPriority==vgl.GetPriority();}
168 	bool operator!=(CSPrimitives& vgl) { return iPriority!=vgl.GetPriority();}
169 
170 	//! Define the input type for the weighting coordinate system 0=cartesian, 1=cylindrical, 2=spherical
171 	void SetCoordInputType(CoordinateSystem type, bool doUpdate=true) {m_MeshType=type; if (doUpdate) Update();}
172 	//! Get the input type for the weighting coordinate system 0=cartesian, 1=cylindrical, 2=spherical
GetCoordInputType()173 	CoordinateSystem GetCoordInputType() const {return m_MeshType;}
174 
175 	//! Define the coordinate system this primitive is defined in (may be different to the input mesh type) \sa SetCoordInputType
SetCoordinateSystem(CoordinateSystem cs)176 	void SetCoordinateSystem(CoordinateSystem cs) {m_PrimCoordSystem=cs;}
177 	//! Read the coordinate system for this primitive (may be different to the input mesh type) \sa GetCoordInputType
GetCoordinateSystem()178 	CoordinateSystem GetCoordinateSystem() const {return m_PrimCoordSystem;}
179 
180 	//! Get the CSTransform if it exists already or create a new one
181 	CSTransform* GetTransform();
182 
183 	//! Show status of this primitve
184 	virtual void ShowPrimitiveStatus(std::ostream& stream);
185 
186 protected:
187 	CSPrimitives(ParameterSet* paraSet, CSProperties* prop);
188 	CSPrimitives(CSPrimitives* prim, CSProperties *prop=NULL);
189 	CSPrimitives(unsigned int ID, ParameterSet* paraSet, CSProperties* prop);
190 
191 	//! Apply (invers) transformation to the given coordinate in the given coordinate system
192 	void TransformCoords(double* Coord, bool invers, CoordinateSystem cs_in) const;
193 
194 	unsigned int uiID;
195 	int iPriority;
196 	CoordinateSystem m_PrimCoordSystem;
197 	CoordinateSystem m_MeshType;
198 	PrimitiveType Type;
199 	ParameterSet* clParaSet;
200 	CSProperties* clProperty;
201 	CSTransform* m_Transform;
202 	std::string PrimTypeName;
203 	bool m_Primtive_Used;
204 
205 	//internal bounding box, updated by Update(), can be used to speedup IsInside
206 	bool m_BoundBoxValid;
207 	double m_BoundBox[6];
208 	CoordinateSystem m_BoundBox_CoordSys;
209 
210 	int m_Dimension;
211 };
212 
213 
214