Debian Codegraph @

ogr/ogrsf_frmts/oci/ogrocistroke.cpp

raw
1 /******************************************************************************
2 *
3 * Purpose: Oracle curve to linestring stroking (approximation).
4 * Author: Frank Warmerdam, [email protected]
5 *
6 ******************************************************************************
7 * Copyright (c) 2008, Frank Warmerdam
8 *
9 * SPDX-License-Identifier: MIT
10 ****************************************************************************/
11
12 #include <stdarg.h>
13 #include "ogr_oci.h"
14 #include "cpl_conv.h"
15 #include "cpl_string.h"
16
17 #include <algorithm>
18
19 /************************************************************************/
20 /* OGROCIArcCenterFromEdgePoints() */
21 /* */
22 /* Compute the center of an arc/circle from three edge points. */
23 /************************************************************************/
24
25 static int OGROCIArcCenterFromEdgePoints(double x_c0, double y_c0, double x_c1,
26 double y_c1, double x_c2, double y_c2,
27 double *x_center, double *y_center)
28
29 {
30
31 /* -------------------------------------------------------------------- */
32 /* Handle a degenerate case that occurs in OSNI products by */
33 /* making some assumptions. If the first and third points are */
34 /* the same assume they are intended to define a full circle, */
35 /* and that the second point is on the opposite side of the */
36 /* circle. */
37 /* -------------------------------------------------------------------- */
38 if (x_c0 == x_c2 && y_c0 == y_c2)
39 {
40 *x_center = (x_c0 + x_c1) * 0.5;
41 *y_center = (y_c0 + y_c1) * 0.5;
42
43 return TRUE;
44 }
45
46 /* -------------------------------------------------------------------- */
47 /* Compute the inverse of the slopes connecting the first and */
48 /* second points. Also compute the center point of the two */
49 /* lines ... the point our crossing line will go through. */
50 /* -------------------------------------------------------------------- */
51 double m1, x1, y1;
52
53 if ((y_c1 - y_c0) != 0.0)
54 m1 = (x_c0 - x_c1) / (y_c1 - y_c0);
55 else
56 m1 = 1e+10;
57
58 x1 = (x_c0 + x_c1) * 0.5;
59 y1 = (y_c0 + y_c1) * 0.5;
60
61 /* -------------------------------------------------------------------- */
62 /* Compute the same for the second point compared to the third */
63 /* point. */
64 /* -------------------------------------------------------------------- */
65 double m2, x2, y2;
66
67 if ((y_c2 - y_c1) != 0.0)
68 m2 = (x_c1 - x_c2) / (y_c2 - y_c1);
69 else
70 m2 = 1e+10;
71
72 x2 = (x_c1 + x_c2) * 0.5;
73 y2 = (y_c1 + y_c2) * 0.5;
74
75 /* -------------------------------------------------------------------- */
76 /* Turn these into the Ax+By+C = 0 form of the lines. */
77 /* -------------------------------------------------------------------- */
78 double a1, a2, l_b1, l_b2, c1, c2;
79
80 a1 = m1;
81 a2 = m2;
82
83 l_b1 = -1.0;
84 l_b2 = -1.0;
85
86 c1 = (y1 - m1 * x1);
87 c2 = (y2 - m2 * x2);
88
89 /* -------------------------------------------------------------------- */
90 /* Compute the intersection of the two lines through the center */
91 /* of the circle, using Kramers rule. */
92 /* -------------------------------------------------------------------- */
93 double det_inv;
94
95 if (a1 * l_b2 - a2 * l_b1 == 0.0)
96 return FALSE;
97
98 det_inv = 1 / (a1 * l_b2 - a2 * l_b1);
99
100 *x_center = (l_b1 * c2 - l_b2 * c1) * det_inv;
101 *y_center = (a2 * c1 - a1 * c2) * det_inv;
102
103 return TRUE;
104 }
105
106 /************************************************************************/
107 /* OGROCIStrokeArcToOGRGeometry_Angles() */
108 /************************************************************************/
109
110 static void OGROCIStrokeArcToOGRGeometry_Angles(
111 double dfCenterX, double dfCenterY, double dfRadius, double dfStartAngle,
112 double dfEndAngle, double dfMaxAngleStepSizeDegrees, OGRLineString *poLine)
113
114 {
115 double dfArcX, dfArcY, dfSlice;
116 int iPoint, iAppendLocation, nVertexCount;
117 double dfEps = dfRadius / 100000.0;
118
119 nVertexCount =
120 (int)ceil(fabs(dfEndAngle - dfStartAngle) / dfMaxAngleStepSizeDegrees) +
121 1;
122 nVertexCount = std::max(2, nVertexCount);
123 dfSlice = (dfEndAngle - dfStartAngle) / (nVertexCount - 1);
124 iAppendLocation = poLine->getNumPoints();
125
126 for (iPoint = 0; iPoint < nVertexCount; iPoint++)
127 {
128 double dfAngle;
129
130 dfAngle = (dfStartAngle + iPoint * dfSlice) * M_PI / 180.0;
131
132 dfArcX = dfCenterX + cos(dfAngle) * dfRadius;
133 dfArcY = dfCenterY + sin(dfAngle) * dfRadius;
134
135 if (iPoint == 0)
136 {
137 if (poLine->getNumPoints() > 0 &&
138 fabs(poLine->getX(poLine->getNumPoints() - 1) - dfArcX) <
139 dfEps &&
140 fabs(poLine->getY(poLine->getNumPoints() - 1) - dfArcY) < dfEps)
141 {
142 poLine->setNumPoints(poLine->getNumPoints() + nVertexCount - 1);
143 }
144 else
145 {
146 poLine->setNumPoints(poLine->getNumPoints() + nVertexCount - 1);
147 poLine->setPoint(iAppendLocation++, dfArcX, dfArcY);
148 }
149 }
150 else
151 poLine->setPoint(iAppendLocation++, dfArcX, dfArcY);
152 }
153 }
154
155 /************************************************************************/
156 /* OGROCIStrokeArcToOGRGeometry_Points() */
157 /************************************************************************/
158
159 int OGROCIStrokeArcToOGRGeometry_Points(double dfStartX, double dfStartY,
160 double dfAlongX, double dfAlongY,
161 double dfEndX, double dfEndY,
162 double dfMaxAngleStepSizeDegrees,
163 int bForceWholeCircle,
164 OGRLineString *poLine)
165
166 {
167 double dfStartAngle, dfEndAngle, dfAlongAngle;
168 double dfCenterX, dfCenterY, dfRadius;
169
170 if (!OGROCIArcCenterFromEdgePoints(dfStartX, dfStartY, dfAlongX, dfAlongY,
171 dfEndX, dfEndY, &dfCenterX, &dfCenterY))
172 return FALSE;
173
174 if (bForceWholeCircle || (dfStartX == dfEndX && dfStartY == dfEndY))
175 {
176 dfStartAngle = 0.0;
177 dfEndAngle = 360.0;
178 }
179 else
180 {
181 double dfDeltaX, dfDeltaY;
182
183 dfDeltaX = dfStartX - dfCenterX;
184 dfDeltaY = dfStartY - dfCenterY;
185 dfStartAngle = atan2(dfDeltaY, dfDeltaX) * 180.0 / M_PI;
186
187 dfDeltaX = dfAlongX - dfCenterX;
188 dfDeltaY = dfAlongY - dfCenterY;
189 dfAlongAngle = atan2(dfDeltaY, dfDeltaX) * 180.0 / M_PI;
190
191 dfDeltaX = dfEndX - dfCenterX;
192 dfDeltaY = dfEndY - dfCenterY;
193 dfEndAngle = atan2(dfDeltaY, dfDeltaX) * 180.0 / M_PI;
194
195 // Try positive (clockwise?) winding.
196 while (dfAlongAngle < dfStartAngle)
197 dfAlongAngle += 360.0;
198
199 while (dfEndAngle < dfAlongAngle)
200 dfEndAngle += 360.0;
201
202 // If that doesn't work out, then go anticlockwise.
203 if (dfEndAngle - dfStartAngle > 360.0)
204 {
205 while (dfAlongAngle > dfStartAngle)
206 dfAlongAngle -= 360.0;
207
208 while (dfEndAngle > dfAlongAngle)
209 dfEndAngle -= 360.0;
210 }
211 }
212
213 dfRadius = sqrt((dfCenterX - dfStartX) * (dfCenterX - dfStartX) +
214 (dfCenterY - dfStartY) * (dfCenterY - dfStartY));
215
216 OGROCIStrokeArcToOGRGeometry_Angles(dfCenterX, dfCenterY, dfRadius,
217 dfStartAngle, dfEndAngle,
218 dfMaxAngleStepSizeDegrees, poLine);
219
220 /* -------------------------------------------------------------------- */
221 /* Force the points for arcs, to avoid odd rounding/math */
222 /* issues. Perhaps we should do this for the start too, but */
223 /* this is a bit tricky since it isn't obvious which point is */
224 /* the start. */
225 /* -------------------------------------------------------------------- */
226 if (!bForceWholeCircle)
227 {
228 poLine->setPoint(poLine->getNumPoints() - 1, dfEndX, dfEndY);
229 }
230
231 return TRUE;
232 }