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 
33 //////////////////////////////////////////////////////////////////////////////
34 
35 namespace basegfx
36 {
37 	// predefinitions
38 	class B3DPolygon;
39 	class B3DRange;
40 
41 	namespace tools
42 	{
43 		// B3DPolygon tools
44 
45 		/**	Check if given polygon is closed. This is kind of a
46 			'classic' method to support old polygon definitions.
47 			Those old polygon definitions define the closed state
48 			of the polygon using identical start and endpoints. This
49 			method corrects this (removes double start/end points)
50 			and sets the Closed()-state of the polygon correctly.
51 		*/
52 		void checkClosed(B3DPolygon& rCandidate);
53 
54 		// Get successor and predecessor indices. Returning the same index means there
55 		// is none. Same for successor.
56 		sal_uInt32 getIndexOfPredecessor(sal_uInt32 nIndex, const B3DPolygon& rCandidate);
57 		sal_uInt32 getIndexOfSuccessor(sal_uInt32 nIndex, const B3DPolygon& rCandidate);
58 
59 		// Get orientation of Polygon
60 		B2VectorOrientation getOrientation(const B3DPolygon& rCandidate);
61 
62 		// get size of polygon. Control vectors are included in that ranges.
63 		B3DRange getRange(const B3DPolygon& rCandidate);
64 
65 		// get normal vector of polygon
66 		B3DVector getNormal(const B3DPolygon& rCandidate);
67 
68 		// get normal vector of positive oriented polygon
69 		B3DVector getPositiveOrientedNormal(const B3DPolygon& rCandidate);
70 
71 		// get signed area of polygon
72 		double getSignedArea(const B3DPolygon& rCandidate);
73 
74 		// get area of polygon
75 		double getArea(const B3DPolygon& rCandidate);
76 
77 		// get signed area of polygon
78 		double getSignedArea(const B3DPolygon& rCandidate);
79 
80 		// get area of polygon
81 		double getArea(const ::basegfx::B3DPolygon& rCandidate);
82 
83 		// get length of polygon edge from point nIndex to nIndex + 1
84 		double getEdgeLength(const B3DPolygon& rCandidate, sal_uInt32 nIndex);
85 
86 		// get length of polygon
87 		double getLength(const B3DPolygon& rCandidate);
88 
89 		// get position on polygon for absolute given distance. If
90 		// length is given, it is assumed the correct polygon length, if 0.0 it is calculated
91 		// using getLength(...)
92 		B3DPoint getPositionAbsolute(const B3DPolygon& rCandidate, double fDistance, double fLength = 0.0);
93 
94 		// get position on polygon for relative given distance in range [0.0 .. 1.0]. If
95 		// length is given, it is assumed the correct polygon length, if 0.0 it is calculated
96 		// using getLength(...)
97 		B3DPoint getPositionRelative(const B3DPolygon& rCandidate, double fDistance, double fLength = 0.0);
98 
99 		/** Apply given LineDashing to given polygon
100 
101 			For a description see applyLineDashing in b2dpolygontoos.hxx
102 		*/
103 		void applyLineDashing(
104 			const B3DPolygon& rCandidate,
105 			const ::std::vector<double>& rDotDashArray,
106 			B3DPolyPolygon* pLineTarget,
107             B3DPolyPolygon* pGapTarget = 0,
108 			double fFullDashDotLen = 0.0);
109 
110 		/** Create/replace normals for given 3d geometry with default normals from given center to outside.
111 			rCandidate:	the 3d geometry to change
112 			rCenter:	the center of the 3d geometry
113          */
114 		B3DPolygon applyDefaultNormalsSphere( const B3DPolygon& rCandidate, const B3DPoint& rCenter);
115 
116 		/** invert normals for given 3d geometry.
117          */
118 		B3DPolygon invertNormals( const B3DPolygon& rCandidate);
119 
120 		/** Create/replace texture coordinates for given 3d geometry with parallel projected one
121 			rRange: the full range of the 3d geometry
122 			If bChangeX, x texture coordinate will be recalculated.
123 			If bChangeY, y texture coordinate will be recalculated.
124          */
125 		B3DPolygon applyDefaultTextureCoordinatesParallel( const B3DPolygon& rCandidate, const B3DRange& rRange, bool bChangeX = true, bool bChangeY = true);
126 
127 		/** Create/replace texture coordinates for given 3d geometry with spherical one
128 			rCenter: the centre of the used 3d geometry
129 			If bChangeX, x texture coordinate will be recalculated.
130 			If bChangeY, y texture coordinate will be recalculated.
131          */
132 		B3DPolygon applyDefaultTextureCoordinatesSphere( const B3DPolygon& rCandidate, const B3DPoint& rCenter, bool bChangeX = true, bool bChangeY = true);
133 
134 		// test if point is inside epsilon-range around an edge defined
135 		// by the two given points. Can be used for HitTesting. The epsilon-range
136 		// is defined to be the cylinder centered to the given edge, using radius
137 		// fDistance, and the sphere around both points with radius fDistance.
138 		bool isInEpsilonRange(const B3DPoint& rEdgeStart, const B3DPoint& rEdgeEnd, const B3DPoint& rTestPosition, double fDistance);
139 
140 		// test if point is inside epsilon-range around the given Polygon. Can be used
141 		// for HitTesting. The epsilon-range is defined to be the cylinder centered to
142         // the given edge, using radius fDistance, and the sphere around both points with radius fDistance.
143 		bool isInEpsilonRange(const B3DPolygon& rCandidate, const B3DPoint& rTestPosition, double fDistance);
144 
145 		// isInside tests for B3DPoint and other B3DPolygon. On border is not inside as long as
146 		// not true is given in bWithBorder flag.
147 		bool isInside(const B3DPolygon& rCandidate, const B3DPoint& rPoint, bool bWithBorder = false);
148 		bool isInside(const B3DPolygon& rCandidate, const B3DPolygon& rPolygon, bool bWithBorder = false);
149 
150 		// calculates if given point is on given line, taking care of the numerical epsilon
151 		bool isPointOnLine(const B3DPoint& rStart, const B3DPoint& rEnd, const B3DPoint& rCandidate, bool bWithPoints = false);
152 
153         // calculates if given point is on given polygon, taking care of the numerical epsilon. Uses
154 		// isPointOnLine internally
155 		bool isPointOnPolygon(const B3DPolygon& rCandidate, const B3DPoint& rPoint, bool bWithPoints = true);
156 
157         // helper to get a fCut position between a plane (given with normal and a point)
158         // and a line given by start and end point
159         bool getCutBetweenLineAndPlane(const B3DVector& rPlaneNormal, const B3DPoint& rPlanePoint, const B3DPoint& rEdgeStart, const B3DPoint& rEdgeEnd, double& fCut);
160 
161         // helper to get a fCut position between a 3d Polygon
162         // and a line given by start and end point
163         bool getCutBetweenLineAndPolygon(const B3DPolygon& rCandidate, const B3DPoint& rEdgeStart, const B3DPoint& rEdgeEnd, double& fCut);
164 
165 		//////////////////////////////////////////////////////////////////////
166 		// comparators with tolerance for 3D Polygons
167 		bool equal(const B3DPolygon& rCandidateA, const B3DPolygon& rCandidateB, const double& rfSmallValue);
168 		bool equal(const B3DPolygon& rCandidateA, const B3DPolygon& rCandidateB);
169 
170 		/** snap some polygon coordinates to discrete coordinates
171 
172 			This method allows to snap some polygon points to discrete (integer) values
173 			which equals e.g. a snap to discrete coordinates. It will snap points of
174 			horizontal and vertical edges
175 
176 			@param rCandidate
177 			The source polygon
178 
179 			@return
180 			The modified version of the source polygon
181 		*/
182 		B3DPolygon snapPointsOfHorizontalOrVerticalEdges(const B3DPolygon& rCandidate);
183 
184 	} // end of namespace tools
185 } // end of namespace basegfx
186 
187 #endif /* _BGFX_POLYGON_B3DPOLYGONTOOLS_HXX */
188