| 1 | /****************************************************************************** |
| 2 | * File: ogrdxf_polyline_smooth.cpp |
| 3 | * |
| 4 | * Project: Interpolation support for smooth POLYLINE and LWPOLYLINE entities. |
| 5 | * Purpose: Implementation of classes for OGR .dxf driver. |
| 6 | * Author: TJ Snider, [email protected] |
| 7 | * Ray Gardener, Daylon Graphics Ltd. |
| 8 | * |
| 9 | ****************************************************************************** |
| 10 | * Copyright (c) 2010 Daylon Graphics Ltd. |
| 11 | * |
| 12 | * SPDX-License-Identifier: MIT |
| 13 | ****************************************************************************/ |
| 14 | |
| 15 | #include "stdlib.h" |
| 16 | #include "math.h" |
| 17 | #include "ogrdxf_polyline_smooth.h"cxx . . $ `<file>/ogr/ogrsf_frmts/dxf/ogrdxf_polyline_smooth.h`/ |
| 18 | |
| 19 | /************************************************************************/ |
| 20 | /* Local helper functions */ |
| 21 | /************************************************************************/ |
| 22 | |
| 23 | static double GetRadiuscxx . . $ GetRadius(d8e7e0733fa41c87).(double bulge, double length) |
| 24 | { |
| 25 | const double h = (bulge * length) / 2; |
| 26 | return (h / 2) + (length * length / (8 * h)); |
| 27 | } |
| 28 | |
| 29 | static double GetLengthcxx . . $ GetLength(e9fac60e61ffe37a).(const DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# &start, |
| 30 | const DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# &end) |
| 31 | { |
| 32 | return sqrtcxx . . $ sqrt(fee4f1d22bc8e617).(powcxx . . $ pow(9454dda30a4b8335).(end.xcxx . . $ DXFSmoothPolylineVertex#x. - start.xcxx . . $ DXFSmoothPolylineVertex#x., 2) + powcxx . . $ pow(9454dda30a4b8335).(end.ycxx . . $ DXFSmoothPolylineVertex#y. - start.ycxx . . $ DXFSmoothPolylineVertex#y., 2)); |
| 33 | } |
| 34 | |
| 35 | static double GetAnglecxx . . $ GetAngle(e9fac60e61ffe37a).(const DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# &start, |
| 36 | const DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# &end) |
| 37 | { |
| 38 | return atan2cxx . . $ atan2(b71928d39fde4295).((start.ycxx . . $ DXFSmoothPolylineVertex#y. - end.ycxx . . $ DXFSmoothPolylineVertex#y.), (start.xcxx . . $ DXFSmoothPolylineVertex#x. - end.xcxx . . $ DXFSmoothPolylineVertex#x.)) * 180.0 / M_PIcxx . . $ `<external>/11263917310783833665/math.h:1289:10`!; |
| 39 | } |
| 40 | |
| 41 | static double GetOGRanglecxx . . $ GetOGRangle(447c67c6676f75c3).(double angle) |
| 42 | { |
| 43 | return angle > 0.0 ? -(angle - 180.0) : -(angle + 180.0); |
| 44 | } |
| 45 | |
| 46 | /************************************************************************/ |
| 47 | /* DXFSmoothPolyline::Tessellate() */ |
| 48 | /************************************************************************/ |
| 49 | |
| 50 | OGRGeometrycxx . . $ OGRGeometry# *DXFSmoothPolylinecxx . . $ DXFSmoothPolyline#::Tessellatecxx . . $ DXFSmoothPolyline#Tessellate(9bece52a439d3243).(bool bAsPolygon) const |
| 51 | { |
| 52 | assertcxx . . $ `<external>/4398592474888995393/assert.h:137:11`!(!m_vertices.empty()); |
| 53 | |
| 54 | /* -------------------------------------------------------------------- */ |
| 55 | /* If polyline is one vertex, convert it to a point */ |
| 56 | /* -------------------------------------------------------------------- */ |
| 57 | |
| 58 | if (m_verticescxx . . $ DXFSmoothPolyline#m_vertices..size() == 1) |
| 59 | { |
| 60 | OGRPointcxx . . $ OGRPoint# *poPt = |
| 61 | new OGRPointcxx . . $ OGRPoint#OGRPoint(98dd472ac7e5c3d4).(m_verticescxx . . $ DXFSmoothPolyline#m_vertices.[0].xcxx . . $ DXFSmoothPolylineVertex#x., m_verticescxx . . $ DXFSmoothPolyline#m_vertices.[0].ycxx . . $ DXFSmoothPolylineVertex#y., m_verticescxx . . $ DXFSmoothPolyline#m_vertices.[0].zcxx . . $ DXFSmoothPolylineVertex#z.); |
| 62 | if (m_verticescxx . . $ DXFSmoothPolyline#m_vertices.[0].zcxx . . $ DXFSmoothPolylineVertex#z. == 0 || m_dimcxx . . $ DXFSmoothPolyline#m_dim. == 2) |
| 63 | poPt->flattenTo2Dcxx . . $ OGRPoint#flattenTo2D(49f6e7a06ebc5aa8).(); |
| 64 | return poPt; |
| 65 | } |
| 66 | |
| 67 | /* -------------------------------------------------------------------- */ |
| 68 | /* Otherwise, presume a line string */ |
| 69 | /* -------------------------------------------------------------------- */ |
| 70 | |
| 71 | OGRLinearRingcxx . . $ OGRLinearRing# *poLR = m_bClosedcxx . . $ DXFSmoothPolyline#m_bClosed. && bAsPolygon ? new OGRLinearRingcxx . . $ OGRLinearRing#OGRLinearRing(49f6e7a06ebc5aa8). : nullptr; |
| 72 | OGRLineStringcxx . . $ OGRLineString# *poLS = poLR ? poLR : new OGRLineStringcxx . . $ OGRLineString#OGRLineString(49f6e7a06ebc5aa8).; |
| 73 | |
| 74 | m_blinestringstartedcxx . . $ DXFSmoothPolyline#m_blinestringstarted. = false; |
| 75 | |
| 76 | stdcxx . . $ std/::vector<DXFSmoothPolylineVertex>::const_iterator oIter = |
| 77 | m_verticescxx . . $ DXFSmoothPolyline#m_vertices..begin(); |
| 78 | stdcxx . . $ std/::vector<DXFSmoothPolylineVertex>::const_iterator oEndIter = |
| 79 | m_verticescxx . . $ DXFSmoothPolyline#m_vertices..end(); |
| 80 | |
| 81 | --oEndIter; |
| 82 | |
| 83 | DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# begin = *oIter; |
| 84 | |
| 85 | while (oIter != oEndIter) |
| 86 | { |
| 87 | ++oIter; |
| 88 | DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# end = *oIter; |
| 89 | |
| 90 | const double len = GetLengthcxx . . $ GetLength(e9fac60e61ffe37a).(begin, end); |
| 91 | |
| 92 | // Don't bother handling bulge for non-constant Z |
| 93 | if (len == 0 || begin.bulgecxx . . $ DXFSmoothPolylineVertex#bulge. == 0 || begin.zcxx . . $ DXFSmoothPolylineVertex#z. != end.zcxx . . $ DXFSmoothPolylineVertex#z.) |
| 94 | { |
| 95 | EmitLinecxx . . $ DXFSmoothPolyline#EmitLine(e8d703d446b75ab2).(begin, end, poLS); |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | const double radius = GetRadiuscxx . . $ GetRadius(d8e7e0733fa41c87).(begin.bulgecxx . . $ DXFSmoothPolylineVertex#bulge., len); |
| 100 | EmitArccxx . . $ DXFSmoothPolyline#EmitArc(800a5207ed9bee9c).(begin, end, radius, len, begin.bulgecxx . . $ DXFSmoothPolylineVertex#bulge., poLS, begin.zcxx . . $ DXFSmoothPolylineVertex#z.); |
| 101 | } |
| 102 | |
| 103 | // Move to next vertex |
| 104 | begin =cxx . . $ DXFSmoothPolylineVertex#`operator=`(6dcf4e413ca539b3). end; |
| 105 | } |
| 106 | |
| 107 | /* -------------------------------------------------------------------- */ |
| 108 | /* Flatten to 2D if necessary */ |
| 109 | /* -------------------------------------------------------------------- */ |
| 110 | |
| 111 | if (m_dimcxx . . $ DXFSmoothPolyline#m_dim. == 2) |
| 112 | poLS->flattenTo2Dcxx . . $ OGRSimpleCurve#flattenTo2D(49f6e7a06ebc5aa8).(); |
| 113 | |
| 114 | if (poLR) |
| 115 | { |
| 116 | // Wrap as polygon. |
| 117 | OGRPolygoncxx . . $ OGRPolygon# *poPoly = new OGRPolygoncxx . . $ OGRPolygon#OGRPolygon(49f6e7a06ebc5aa8).(); |
| 118 | poPoly->addRingDirectlycxx . . $ OGRCurvePolygon#addRingDirectly(2f65f96c58d3f239).(poLR); |
| 119 | |
| 120 | return poPoly; |
| 121 | } |
| 122 | |
| 123 | return poLS; |
| 124 | } |
| 125 | |
| 126 | /************************************************************************/ |
| 127 | /* DXFSmoothPolyline::EmitArc() */ |
| 128 | /************************************************************************/ |
| 129 | |
| 130 | void DXFSmoothPolylinecxx . . $ DXFSmoothPolyline#::EmitArccxx . . $ DXFSmoothPolyline#EmitArc(800a5207ed9bee9c).(const DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# &start, |
| 131 | const DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# &end, |
| 132 | double radius, double len, double bulge, |
| 133 | OGRLineStringcxx . . $ OGRLineString# *poLS, double dfZ) const |
| 134 | { |
| 135 | assertcxx . . $ `<external>/4398592474888995393/assert.h:137:11`!(poLS); |
| 136 | |
| 137 | double ogrArcRotation = 0.0; |
| 138 | const double ogrArcRadius = fabscxx . . $ fabs(fee4f1d22bc8e617).(radius); |
| 139 | |
| 140 | /* -------------------------------------------------------------------- */ |
| 141 | /* Set arc's direction and keep bulge positive */ |
| 142 | /* -------------------------------------------------------------------- */ |
| 143 | |
| 144 | const bool bClockwise = (bulge < 0); |
| 145 | |
| 146 | if (bClockwise) |
| 147 | bulge *= -1; |
| 148 | |
| 149 | /* -------------------------------------------------------------------- */ |
| 150 | /* Get arc's center point */ |
| 151 | /* -------------------------------------------------------------------- */ |
| 152 | |
| 153 | const double saggita = fabscxx . . $ fabs(fee4f1d22bc8e617).(bulge * (len / 2.0)); |
| 154 | const double apo = |
| 155 | bClockwise ? -(ogrArcRadius - saggita) : -(saggita - ogrArcRadius); |
| 156 | |
| 157 | DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# vcxx . . $ DXFSmoothPolylineVertex#DXFSmoothPolylineVertex(49f6e7a06ebc5aa8).; |
| 158 | v.xcxx . . $ DXFSmoothPolylineVertex#x. = start.xcxx . . $ DXFSmoothPolylineVertex#x. - end.xcxx . . $ DXFSmoothPolylineVertex#x.; |
| 159 | v.ycxx . . $ DXFSmoothPolylineVertex#y. = start.ycxx . . $ DXFSmoothPolylineVertex#y. - end.ycxx . . $ DXFSmoothPolylineVertex#y.; |
| 160 | |
| 161 | #ifdef notdef |
| 162 | const bool bMathissue = (v.x == 0.0 || v.y == 0.0); |
| 163 | #endif |
| 164 | |
| 165 | DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# midpointcxx . . $ DXFSmoothPolylineVertex#DXFSmoothPolylineVertex(49f6e7a06ebc5aa8).; |
| 166 | midpoint.xcxx . . $ DXFSmoothPolylineVertex#x. = end.xcxx . . $ DXFSmoothPolylineVertex#x. + 0.5 * v.xcxx . . $ DXFSmoothPolylineVertex#x.; |
| 167 | midpoint.ycxx . . $ DXFSmoothPolylineVertex#y. = end.ycxx . . $ DXFSmoothPolylineVertex#y. + 0.5 * v.ycxx . . $ DXFSmoothPolylineVertex#y.; |
| 168 | |
| 169 | DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# pperpcxx . . $ DXFSmoothPolylineVertex#DXFSmoothPolylineVertex(49f6e7a06ebc5aa8).; |
| 170 | pperp.xcxx . . $ DXFSmoothPolylineVertex#x. = v.ycxx . . $ DXFSmoothPolylineVertex#y.; |
| 171 | pperp.ycxx . . $ DXFSmoothPolylineVertex#y. = -v.xcxx . . $ DXFSmoothPolylineVertex#x.; |
| 172 | pperp.normalizecxx . . $ DXFSmoothPolylineVertex#normalize(49f6e7a06ebc5aa8).(); |
| 173 | |
| 174 | DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# ogrArcCentercxx . . $ DXFSmoothPolylineVertex#DXFSmoothPolylineVertex(49f6e7a06ebc5aa8).; |
| 175 | ogrArcCenter.xcxx . . $ DXFSmoothPolylineVertex#x. = midpoint.xcxx . . $ DXFSmoothPolylineVertex#x. + (pperp.xcxx . . $ DXFSmoothPolylineVertex#x. * apo); |
| 176 | ogrArcCenter.ycxx . . $ DXFSmoothPolylineVertex#y. = midpoint.ycxx . . $ DXFSmoothPolylineVertex#y. + (pperp.ycxx . . $ DXFSmoothPolylineVertex#y. * apo); |
| 177 | |
| 178 | /* -------------------------------------------------------------------- */ |
| 179 | /* Get the line's general vertical direction (-1 = down, +1 = up) */ |
| 180 | /* -------------------------------------------------------------------- */ |
| 181 | |
| 182 | const double linedir = end.ycxx . . $ DXFSmoothPolylineVertex#y. > start.ycxx . . $ DXFSmoothPolylineVertex#y. ? 1.0 : -1.0; |
| 183 | |
| 184 | /* -------------------------------------------------------------------- */ |
| 185 | /* Get arc's starting angle. */ |
| 186 | /* -------------------------------------------------------------------- */ |
| 187 | |
| 188 | double a = GetAnglecxx . . $ GetAngle(e9fac60e61ffe37a).(ogrArcCenter, start); |
| 189 | |
| 190 | if (bClockwise && (linedir == 1.0)) |
| 191 | a += (linedir * 180.0); |
| 192 | |
| 193 | double ogrArcStartAngle = GetOGRanglecxx . . $ GetOGRangle(447c67c6676f75c3).(a); |
| 194 | |
| 195 | /* -------------------------------------------------------------------- */ |
| 196 | /* Get arc's ending angle. */ |
| 197 | /* -------------------------------------------------------------------- */ |
| 198 | |
| 199 | a = GetAnglecxx . . $ GetAngle(e9fac60e61ffe37a).(ogrArcCenter, end); |
| 200 | |
| 201 | if (bClockwise && (linedir == 1.0)) |
| 202 | a += (linedir * 180.0); |
| 203 | |
| 204 | double ogrArcEndAngle = GetOGRanglecxx . . $ GetOGRangle(447c67c6676f75c3).(a); |
| 205 | |
| 206 | if (!bClockwise && (ogrArcStartAngle < ogrArcEndAngle)) |
| 207 | ogrArcEndAngle = -180.0 + (linedir * a); |
| 208 | |
| 209 | if (bClockwise && (ogrArcStartAngle > ogrArcEndAngle)) |
| 210 | ogrArcEndAngle += 360.0; |
| 211 | |
| 212 | /* -------------------------------------------------------------------- */ |
| 213 | /* Flip arc's rotation if necessary. */ |
| 214 | /* -------------------------------------------------------------------- */ |
| 215 | |
| 216 | if (bClockwise && (linedir == 1.0)) |
| 217 | ogrArcRotation = linedir * 180.0; |
| 218 | |
| 219 | /* -------------------------------------------------------------------- */ |
| 220 | /* Tessellate the arc segment and append to the linestring. */ |
| 221 | /* -------------------------------------------------------------------- */ |
| 222 | |
| 223 | if (fabscxx . . $ fabs(fee4f1d22bc8e617).(ogrArcEndAngle - ogrArcStartAngle) <= 361.0) |
| 224 | { |
| 225 | auto poArc = stdcxx . . $ std/::unique_ptr<OGRLineStringcxx . . $ OGRLineString#>( |
| 226 | OGRGeometryFactorycxx . . $ OGRGeometryFactory#::approximateArcAnglescxx . . $ OGRGeometryFactory#approximateArcAngles(ad77857dadd20571).( |
| 227 | ogrArcCenter.xcxx . . $ DXFSmoothPolylineVertex#x., ogrArcCenter.ycxx . . $ DXFSmoothPolylineVertex#y., dfZ, ogrArcRadius, ogrArcRadius, |
| 228 | ogrArcRotation, ogrArcStartAngle, ogrArcEndAngle, 0.0, |
| 229 | m_bUseMaxGapWhenTessellatingArcscxx . . $ DXFSmoothPolyline#m_bUseMaxGapWhenTessellatingArcs.) |
| 230 | ->toLineStringcxx . . $ OGRGeometry#toLineString(67733397082bfaf6).()); |
| 231 | |
| 232 | // Make sure extremities exactly match input start and end point. |
| 233 | // This is in particular important if the polyline is closed. |
| 234 | if (poArc->getNumPointscxx . . $ OGRSimpleCurve#getNumPoints(455f465bc33b4cdf).() >= 2) |
| 235 | { |
| 236 | poArc->setPointcxx . . $ OGRSimpleCurve#setPoint(9f72e67440d8b329).(0, start.xcxx . . $ DXFSmoothPolylineVertex#x., start.ycxx . . $ DXFSmoothPolylineVertex#y.); |
| 237 | poArc->setPointcxx . . $ OGRSimpleCurve#setPoint(9f72e67440d8b329).(poArc->getNumPointscxx . . $ OGRSimpleCurve#getNumPoints(455f465bc33b4cdf).() - 1, end.xcxx . . $ DXFSmoothPolylineVertex#x., end.ycxx . . $ DXFSmoothPolylineVertex#y.); |
| 238 | } |
| 239 | |
| 240 | poLS->addSubLineStringcxx . . $ OGRSimpleCurve#addSubLineString(7c19cb04df4be82b).(poArc.get()); |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | // TODO: emit error ? |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /************************************************************************/ |
| 249 | /* DXFSmoothPolyline::EmitLine() */ |
| 250 | /************************************************************************/ |
| 251 | |
| 252 | void DXFSmoothPolylinecxx . . $ DXFSmoothPolyline#::EmitLinecxx . . $ DXFSmoothPolyline#EmitLine(e8d703d446b75ab2).(const DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# &start, |
| 253 | const DXFSmoothPolylineVertexcxx . . $ DXFSmoothPolylineVertex# &end, |
| 254 | OGRLineStringcxx . . $ OGRLineString# *poLS) const |
| 255 | { |
| 256 | assertcxx . . $ `<external>/4398592474888995393/assert.h:137:11`!(poLS); |
| 257 | |
| 258 | if (!m_blinestringstartedcxx . . $ DXFSmoothPolyline#m_blinestringstarted.) |
| 259 | { |
| 260 | poLS->addPointcxx . . $ OGRSimpleCurve#addPoint(c3c51e0645da350e).(start.xcxx . . $ DXFSmoothPolylineVertex#x., start.ycxx . . $ DXFSmoothPolylineVertex#y., start.zcxx . . $ DXFSmoothPolylineVertex#z.); |
| 261 | m_blinestringstartedcxx . . $ DXFSmoothPolyline#m_blinestringstarted. = true; |
| 262 | } |
| 263 | |
| 264 | poLS->addPointcxx . . $ OGRSimpleCurve#addPoint(c3c51e0645da350e).(end.xcxx . . $ DXFSmoothPolylineVertex#x., end.ycxx . . $ DXFSmoothPolylineVertex#y., end.zcxx . . $ DXFSmoothPolylineVertex#z.); |
| 265 | } |
| 266 | |
| 267 | /************************************************************************/ |
| 268 | /* DXFSmoothPolyline::Close() */ |
| 269 | /************************************************************************/ |
| 270 | |
| 271 | void DXFSmoothPolylinecxx . . $ DXFSmoothPolyline#::Closecxx . . $ DXFSmoothPolyline#Close(49f6e7a06ebc5aa8).() |
| 272 | { |
| 273 | assertcxx . . $ `<external>/4398592474888995393/assert.h:137:11`!(!m_bClosed); |
| 274 | |
| 275 | if (m_verticescxx . . $ DXFSmoothPolyline#m_vertices..size() >= 2) |
| 276 | { |
| 277 | const bool bVisuallyClosed = |
| 278 | (m_verticescxx . . $ DXFSmoothPolyline#m_vertices..back().shares_2D_poscxx . . $ DXFSmoothPolylineVertex#shares_2D_pos(4a1d7c13f1299738).(m_verticescxx . . $ DXFSmoothPolyline#m_vertices.[0])); |
| 279 | |
| 280 | if (!bVisuallyClosed) |
| 281 | { |
| 282 | m_verticescxx . . $ DXFSmoothPolyline#m_vertices..push_back(m_verticescxx . . $ DXFSmoothPolyline#m_vertices.[0]); |
| 283 | } |
| 284 | m_bClosedcxx . . $ DXFSmoothPolyline#m_bClosed. = true; |
| 285 | } |
| 286 | } |