1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _BGFX_POLYGON_B3DPOLYGONTOOLS_HXX
25 #define _BGFX_POLYGON_B3DPOLYGONTOOLS_HXX
26 
27 #include <basegfx/point/b3dpoint.hxx>
28 #include <basegfx/vector/b3dvector.hxx>
29 #include <basegfx/polygon/b3dpolypolygon.hxx>
30 #include <basegfx/vector/b2enums.hxx>
31 #include <vector>
32 #include <basegfx/basegfxdllapi.h>
33 
34 //////////////////////////////////////////////////////////////////////////////
35 
36 namespace basegfx
37 {
38 	// predefinitions
39 	class B3DPolygon;
40 	class B3DRange;
41 
42 	namespace tools
43 	{
44 		// B3DPolygon tools
45 
46 		/**	Check if given polygon is closed. This is kind of a
47 			'classic' method to support old polygon definitions.
48 			Those old polygon definitions define the closed state
49 			of the polygon using identical start and endpoints. This
50 			method corrects this (removes double start/end points)
51 			and sets the Closed()-state of the polygon correctly.
52 		*/
53 		BASEGFX_DLLPUBLIC void checkClosed(B3DPolygon& rCandidate);
54 
55 		// Get successor and predecessor indices. Returning the same index means there
56 		// is none. Same for successor.
57 		BASEGFX_DLLPUBLIC sal_uInt32 getIndexOfPredecessor(sal_uInt32 nIndex, const B3DPolygon& rCandidate);
58 		BASEGFX_DLLPUBLIC sal_uInt32 getIndexOfSuccessor(sal_uInt32 nIndex, const B3DPolygon& rCandidate);
59 
60 		// Get orientation of Polygon
61 		BASEGFX_DLLPUBLIC B2VectorOrientation getOrientation(const B3DPolygon& rCandidate);
62 
63 		// get size of polygon. Control vectors are included in that ranges.
64 		BASEGFX_DLLPUBLIC B3DRange getRange(const B3DPolygon& rCandidate);
65 
66 		// get normal vector of polygon
67 		BASEGFX_DLLPUBLIC B3DVector getNormal(const B3DPolygon& rCandidate);
68 
69 		// get normal vector of positive oriented polygon
70 		BASEGFX_DLLPUBLIC B3DVector getPositiveOrientedNormal(const B3DPolygon& rCandidate);
71 
72 		// get signed area of polygon
73 		BASEGFX_DLLPUBLIC double getSignedArea(const B3DPolygon& rCandidate);
74 
75 		// get area of polygon
76 		BASEGFX_DLLPUBLIC double getArea(const B3DPolygon& rCandidate);
77 
78 		// get signed area of polygon
79 		BASEGFX_DLLPUBLIC double getSignedArea(const B3DPolygon& rCandidate);
80 
81 		// get area of polygon
82 		BASEGFX_DLLPUBLIC double getArea(const ::basegfx::B3DPolygon& rCandidate);
83 
84 		// get length of polygon edge from point nIndex to nIndex + 1
85 		BASEGFX_DLLPUBLIC double getEdgeLength(const B3DPolygon& rCandidate, sal_uInt32 nIndex);
86 
87 		// get length of polygon
88 		BASEGFX_DLLPUBLIC double getLength(const B3DPolygon& rCandidate);
89 
90 		// get position on polygon for absolute given distance. If
91 		// length is given, it is assumed the correct polygon length, if 0.0 it is calculated
92 		// using getLength(...)
93 		BASEGFX_DLLPUBLIC B3DPoint getPositionAbsolute(const B3DPolygon& rCandidate, double fDistance, double fLength = 0.0);
94 
95 		// get position on polygon for relative given distance in range [0.0 .. 1.0]. If
96 		// length is given, it is assumed the correct polygon length, if 0.0 it is calculated
97 		// using getLength(...)
98 		BASEGFX_DLLPUBLIC B3DPoint getPositionRelative(const B3DPolygon& rCandidate, double fDistance, double fLength = 0.0);
99 
100 		/** Apply given LineDashing to given polygon
101 
102 			For a description see applyLineDashing in b2dpolygontoos.hxx
103 		*/
104 		BASEGFX_DLLPUBLIC void applyLineDashing(
105 			const B3DPolygon& rCandidate,
106 			const ::std::vector<double>& rDotDashArray,
107 			B3DPolyPolygon* pLineTarget,
108             B3DPolyPolygon* pGapTarget = 0,
109 			double fFullDashDotLen = 0.0);
110 
111 		/** Create/replace normals for given 3d geometry with default normals from given center to outside.
112 			rCandidate:	the 3d geometry to change
113 			rCenter:	the center of the 3d geometry
114          */
115 		BASEGFX_DLLPUBLIC B3DPolygon applyDefaultNormalsSphere( const B3DPolygon& rCandidate, const B3DPoint& rCenter);
116 
117 		/** invert normals for given 3d geometry.
118          */
119 		BASEGFX_DLLPUBLIC B3DPolygon invertNormals( const B3DPolygon& rCandidate);
120 
121 		/** Create/replace texture coordinates for given 3d geometry with parallel projected one
122 			rRange: the full range of the 3d geometry
123 			If bChangeX, x texture coordinate will be recalculated.
124 			If bChangeY, y texture coordinate will be recalculated.
125          */
126 		BASEGFX_DLLPUBLIC B3DPolygon applyDefaultTextureCoordinatesParallel( const B3DPolygon& rCandidate, const B3DRange& rRange, bool bChangeX = true, bool bChangeY = true);
127 
128 		/** Create/replace texture coordinates for given 3d geometry with spherical one
129 			rCenter: the centre of the used 3d geometry
130 			If bChangeX, x texture coordinate will be recalculated.
131 			If bChangeY, y texture coordinate will be recalculated.
132          */
133 		BASEGFX_DLLPUBLIC B3DPolygon applyDefaultTextureCoordinatesSphere( const B3DPolygon& rCandidate, const B3DPoint& rCenter, bool bChangeX = true, bool bChangeY = true);
134 
135 		// test if point is inside epsilon-range around an edge defined
136 		// by the two given points. Can be used for HitTesting. The epsilon-range
137 		// is defined to be the cylinder centered to the given edge, using radius
138 		// fDistance, and the sphere around both points with radius fDistance.
139 		BASEGFX_DLLPUBLIC bool isInEpsilonRange(const B3DPoint& rEdgeStart, const B3DPoint& rEdgeEnd, const B3DPoint& rTestPosition, double fDistance);
140 
141 		// test if point is inside epsilon-range around the given Polygon. Can be used
142 		// for HitTesting. The epsilon-range is defined to be the cylinder centered to
143         // the given edge, using radius fDistance, and the sphere around both points with radius fDistance.
144 		BASEGFX_DLLPUBLIC bool isInEpsilonRange(const B3DPolygon& rCandidate, const B3DPoint& rTestPosition, double fDistance);
145 
146 		// isInside tests for B3DPoint and other B3DPolygon. On border is not inside as long as
147 		// not true is given in bWithBorder flag.
148 		BASEGFX_DLLPUBLIC bool isInside(const B3DPolygon& rCandidate, const B3DPoint& rPoint, bool bWithBorder = false);
149 		BASEGFX_DLLPUBLIC bool isInside(const B3DPolygon& rCandidate, const B3DPolygon& rPolygon, bool bWithBorder = false);
150 
151 		// calculates if given point is on given line, taking care of the numerical epsilon
152 		BASEGFX_DLLPUBLIC bool isPointOnLine(const B3DPoint& rStart, const B3DPoint& rEnd, const B3DPoint& rCandidate, bool bWithPoints = false);
153 
154         // calculates if given point is on given polygon, taking care of the numerical epsilon. Uses
155 		// isPointOnLine internally
156 		BASEGFX_DLLPUBLIC bool isPointOnPolygon(const B3DPolygon& rCandidate, const B3DPoint& rPoint, bool bWithPoints = true);
157 
158         // helper to get a fCut position between a plane (given with normal and a point)
159         // and a line given by start and end point
160         BASEGFX_DLLPUBLIC bool getCutBetweenLineAndPlane(const B3DVector& rPlaneNormal, const B3DPoint& rPlanePoint, const B3DPoint& rEdgeStart, const B3DPoint& rEdgeEnd, double& fCut);
161 
162         // helper to get a fCut position between a 3d Polygon
163         // and a line given by start and end point
164         BASEGFX_DLLPUBLIC bool getCutBetweenLineAndPolygon(const B3DPolygon& rCandidate, const B3DPoint& rEdgeStart, const B3DPoint& rEdgeEnd, double& fCut);
165 
166 		//////////////////////////////////////////////////////////////////////
167 		// comparators with tolerance for 3D Polygons
168 		BASEGFX_DLLPUBLIC bool equal(const B3DPolygon& rCandidateA, const B3DPolygon& rCandidateB, const double& rfSmallValue);
169 		BASEGFX_DLLPUBLIC bool equal(const B3DPolygon& rCandidateA, const B3DPolygon& rCandidateB);
170 
171 		/** snap some polygon coordinates to discrete coordinates
172 
173 			This method allows to snap some polygon points to discrete (integer) values
174 			which equals e.g. a snap to discrete coordinates. It will snap points of
175 			horizontal and vertical edges
176 
177 			@param rCandidate
178 			The source polygon
179 
180 			@return
181 			The modified version of the source polygon
182 		*/
183 		BASEGFX_DLLPUBLIC B3DPolygon snapPointsOfHorizontalOrVerticalEdges(const B3DPolygon& rCandidate);
184 
185 	} // end of namespace tools
186 } // end of namespace basegfx
187 
188 #endif /* _BGFX_POLYGON_B3DPOLYGONTOOLS_HXX */
189