Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: NTF Translator
4 : * Purpose: NTF Arc to polyline stroking code. This code is really generic,
5 : * and might be moved into an OGR module at some point in the
6 : * future.
7 : * Author: Frank Warmerdam, warmerdam@pobox.com
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 2001, Frank Warmerdam
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #include <stdarg.h>
32 : #include "ntf.h"
33 : #include "cpl_conv.h"
34 : #include "cpl_string.h"
35 :
36 : #include <algorithm>
37 : #include <utility>
38 :
39 : /************************************************************************/
40 : /* NTFArcCenterFromEdgePoints() */
41 : /* */
42 : /* Compute the center of an arc/circle from three edge points. */
43 : /************************************************************************/
44 :
45 0 : int NTFArcCenterFromEdgePoints(double x_c0, double y_c0, double x_c1,
46 : double y_c1, double x_c2, double y_c2,
47 : double *x_center, double *y_center)
48 :
49 : {
50 :
51 : /* -------------------------------------------------------------------- */
52 : /* Handle a degenerate case that occurs in OSNI products by */
53 : /* making some assumptions. If the first and third points are */
54 : /* the same assume they are intended to define a full circle, */
55 : /* and that the second point is on the opposite side of the */
56 : /* circle. */
57 : /* -------------------------------------------------------------------- */
58 0 : if (x_c0 == x_c2 && y_c0 == y_c2)
59 : {
60 0 : *x_center = (x_c0 + x_c1) * 0.5;
61 0 : *y_center = (y_c0 + y_c1) * 0.5;
62 :
63 0 : return TRUE;
64 : }
65 :
66 : /* -------------------------------------------------------------------- */
67 : /* Compute the inverse of the slopes connecting the first and */
68 : /* second points. Also compute the center point of the two */
69 : /* lines ... the point our crossing line will go through. */
70 : /* -------------------------------------------------------------------- */
71 0 : const double m1 =
72 0 : (y_c1 - y_c0) != 0.0 ? (x_c0 - x_c1) / (y_c1 - y_c0) : 1e+10;
73 :
74 0 : const double x1 = (x_c0 + x_c1) * 0.5;
75 0 : const double y1 = (y_c0 + y_c1) * 0.5;
76 :
77 : /* -------------------------------------------------------------------- */
78 : /* Compute the same for the second point compared to the third */
79 : /* point. */
80 : /* -------------------------------------------------------------------- */
81 0 : const double m2 =
82 0 : (y_c2 - y_c1) != 0.0 ? (x_c1 - x_c2) / (y_c2 - y_c1) : 1e+10;
83 :
84 0 : const double x2 = (x_c1 + x_c2) * 0.5;
85 0 : const double y2 = (y_c1 + y_c2) * 0.5;
86 :
87 : /* -------------------------------------------------------------------- */
88 : /* Turn these into the Ax+By+C = 0 form of the lines. */
89 : /* -------------------------------------------------------------------- */
90 0 : const double a1 = m1;
91 0 : const double a2 = m2;
92 :
93 0 : const double b1 = -1.0;
94 0 : const double b2 = -1.0;
95 :
96 0 : const double c1 = (y1 - m1 * x1);
97 0 : const double c2 = (y2 - m2 * x2);
98 :
99 : /* -------------------------------------------------------------------- */
100 : /* Compute the intersection of the two lines through the center */
101 : /* of the circle, using Kramers rule. */
102 : /* -------------------------------------------------------------------- */
103 0 : if (a1 * b2 - a2 * b1 == 0.0)
104 0 : return FALSE;
105 :
106 0 : const double det_inv = 1 / (a1 * b2 - a2 * b1);
107 :
108 0 : *x_center = (b1 * c2 - b2 * c1) * det_inv;
109 0 : *y_center = (a2 * c1 - a1 * c2) * det_inv;
110 :
111 0 : return TRUE;
112 : }
113 :
114 : /************************************************************************/
115 : /* NTFStrokeArcToOGRGeometry_Points() */
116 : /************************************************************************/
117 :
118 0 : OGRGeometry *NTFStrokeArcToOGRGeometry_Points(double dfStartX, double dfStartY,
119 : double dfAlongX, double dfAlongY,
120 : double dfEndX, double dfEndY,
121 : int nVertexCount)
122 :
123 : {
124 0 : double dfStartAngle = 0.0;
125 0 : double dfEndAngle = 0.0;
126 0 : double dfCenterX = 0.0;
127 0 : double dfCenterY = 0.0;
128 0 : double dfRadius = 0.0;
129 :
130 0 : if (!NTFArcCenterFromEdgePoints(dfStartX, dfStartY, dfAlongX, dfAlongY,
131 : dfEndX, dfEndY, &dfCenterX, &dfCenterY))
132 0 : return nullptr;
133 :
134 0 : if (dfStartX == dfEndX && dfStartY == dfEndY)
135 : {
136 0 : dfStartAngle = 0.0;
137 0 : dfEndAngle = 360.0;
138 : }
139 : else
140 : {
141 0 : double dfDeltaX = dfStartX - dfCenterX;
142 0 : double dfDeltaY = dfStartY - dfCenterY;
143 0 : dfStartAngle = atan2(dfDeltaY, dfDeltaX) * 180.0 / M_PI;
144 :
145 0 : dfDeltaX = dfAlongX - dfCenterX;
146 0 : dfDeltaY = dfAlongY - dfCenterY;
147 0 : double dfAlongAngle = atan2(dfDeltaY, dfDeltaX) * 180.0 / M_PI;
148 :
149 0 : dfDeltaX = dfEndX - dfCenterX;
150 0 : dfDeltaY = dfEndY - dfCenterY;
151 0 : dfEndAngle = atan2(dfDeltaY, dfDeltaX) * 180.0 / M_PI;
152 :
153 0 : while (dfAlongAngle < dfStartAngle)
154 0 : dfAlongAngle += 360.0;
155 :
156 0 : while (dfEndAngle < dfAlongAngle)
157 0 : dfEndAngle += 360.0;
158 :
159 0 : if (dfEndAngle - dfStartAngle > 360.0)
160 : {
161 0 : std::swap(dfStartAngle, dfEndAngle);
162 :
163 0 : while (dfEndAngle < dfStartAngle)
164 0 : dfStartAngle -= 360.0;
165 : }
166 : }
167 :
168 0 : dfRadius = sqrt((dfCenterX - dfStartX) * (dfCenterX - dfStartX) +
169 0 : (dfCenterY - dfStartY) * (dfCenterY - dfStartY));
170 :
171 0 : return NTFStrokeArcToOGRGeometry_Angles(
172 0 : dfCenterX, dfCenterY, dfRadius, dfStartAngle, dfEndAngle, nVertexCount);
173 : }
174 :
175 : /************************************************************************/
176 : /* NTFStrokeArcToOGRGeometry_Angles() */
177 : /************************************************************************/
178 :
179 0 : OGRGeometry *NTFStrokeArcToOGRGeometry_Angles(double dfCenterX,
180 : double dfCenterY, double dfRadius,
181 : double dfStartAngle,
182 : double dfEndAngle,
183 : int nVertexCount)
184 :
185 : {
186 0 : OGRLineString *poLine = new OGRLineString;
187 :
188 0 : nVertexCount = std::max(2, nVertexCount);
189 0 : const double dfSlice = (dfEndAngle - dfStartAngle) / (nVertexCount - 1);
190 :
191 0 : poLine->setNumPoints(nVertexCount);
192 :
193 0 : for (int iPoint = 0; iPoint < nVertexCount; iPoint++)
194 : {
195 0 : const double dfAngle = (dfStartAngle + iPoint * dfSlice) * M_PI / 180.0;
196 :
197 0 : const double dfArcX = dfCenterX + cos(dfAngle) * dfRadius;
198 0 : const double dfArcY = dfCenterY + sin(dfAngle) * dfRadius;
199 :
200 0 : poLine->setPoint(iPoint, dfArcX, dfArcY);
201 : }
202 :
203 0 : return poLine;
204 : }
|