1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
*
* PostGIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* PostGIS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*
* Copyright (C) 2024 Sandro Santilli <strk@kbt.io>
*
**********************************************************************/
#include "../postgis_config.h"
//#define POSTGIS_DEBUG_LEVEL 1
#include "lwgeom_log.h"
#include "liblwgeom_internal.h"
#include "liblwgeom_topo_internal.h"
#include "lwt_edgeend_star.h"
#include "lwt_edgeend.h"
#include <inttypes.h>
LWT_EDGEEND_STAR *
lwt_edgeEndStar_init( LWT_ELEMID nodeID )
{
LWT_EDGEEND_STAR *star = lwalloc(sizeof(LWT_EDGEEND_STAR));
star->numEdgeEnds = 0;
star->edgeEndsCapacity = 0;
star->edgeEnds = NULL;
star->nodeID = nodeID;
star->sorted = 0;
return star;
}
void
lwt_edgeEndStar_release( LWT_EDGEEND_STAR *star )
{
uint64_t i;
for (i=0; i<star->edgeEndsCapacity; ++i)
{
lwfree( star->edgeEnds[i] );
}
if ( star->edgeEndsCapacity ) {
lwfree( star->edgeEnds );
}
lwfree( star );
}
void
lwt_edgeEndStar_addEdge( LWT_EDGEEND_STAR *star, const LWT_ISO_EDGE *edge )
{
int numEdgeEnds = 0;
LWT_EDGEEND *edgeEnds[2];
uint64_t newCapacity;
if ( edge->start_node == star->nodeID )
{
LWT_EDGEEND *ee = lwt_edgeEnd_fromEdge( edge, 1 );
if ( ! ee ) {
lwerror("Could not construct outgoing EdgeEnd for edge %"
LWTFMT_ELEMID, edge->edge_id);
return;
}
edgeEnds[numEdgeEnds++] = ee;
}
if ( edge->end_node == star->nodeID )
{
LWT_EDGEEND *ee = lwt_edgeEnd_fromEdge( edge, 0 );
if ( ! ee ) {
lwerror("Could not construct outgoing incoming for edge %"
LWTFMT_ELEMID, edge->edge_id);
return;
}
edgeEnds[numEdgeEnds++] = ee;
}
if ( ! numEdgeEnds )
{
lwerror("Edge %" LWTFMT_ELEMID
" doesn't start nor end on star node %" LWTFMT_ELEMID,
edge->edge_id, star->nodeID);
return;
}
LWDEBUGF(1, "Edge %" LWTFMT_ELEMID
" got %" PRIu64 " ends incident to star node %" LWTFMT_ELEMID,
edge->edge_id, numEdgeEnds, star->nodeID );
newCapacity = star->numEdgeEnds + numEdgeEnds;
LWDEBUGF(3, "Current star capacity:%d, required:%d",
star->edgeEndsCapacity, newCapacity);
if ( newCapacity > star->edgeEndsCapacity )
{
LWDEBUGF(3, "Reallocating edgeEnds from %p to new size %d", star->edgeEnds, newCapacity * sizeof(LWT_EDGEEND *));
if ( star->edgeEnds ) {
star->edgeEnds = lwrealloc(star->edgeEnds, newCapacity * sizeof(LWT_EDGEEND *));
} else {
star->edgeEnds = lwalloc(newCapacity * sizeof(LWT_EDGEEND *));
}
LWDEBUGF(3, "Reallocated edgeEnds are %p", star->edgeEnds);
star->edgeEndsCapacity = newCapacity;
LWDEBUGF(3, "New star capacity: %d", newCapacity);
}
for (int i=0; i<numEdgeEnds; ++i)
{
star->edgeEnds[ star->numEdgeEnds++ ] = edgeEnds[i];
}
star->sorted = 0;
}
static int
lwt_edgeEnd_compare(const void *i1, const void *i2)
{
const LWT_EDGEEND *ee1 = *(const LWT_EDGEEND **)i1;
const LWT_EDGEEND *ee2 = *(const LWT_EDGEEND **)i2;
int ret;
if ( ee1->azimuth < ee2->azimuth )
ret = -1;
else if ( ee1->azimuth > ee2->azimuth )
ret = 1;
else
ret = 0;
LWDEBUGF(4, "qsort comparator for %s edge %d with azimuth %g and %s edge %d with azimuth %g returning %d",
ee1->outgoing ? "outgoing" : "incoming", ee1->edge->edge_id, ee1->azimuth,
ee2->outgoing ? "outgoing" : "incoming", ee2->edge->edge_id, ee2->azimuth,
ret
);
return ret;
}
static void
lwt_edgeEndStar_ensureSorted( LWT_EDGEEND_STAR *star )
{
if ( star->sorted ) return; // nothing to do
qsort( star->edgeEnds, star->numEdgeEnds, sizeof(LWT_EDGEEND *), lwt_edgeEnd_compare);
star->sorted = 1;
}
void
lwt_EdgeEndStar_debugPrint( const LWT_EDGEEND_STAR *star )
{
lwdebug(1, "Star around node %" LWTFMT_ELEMID " has %" PRIu64 " edgeEnds, %s",
star->nodeID, star->numEdgeEnds, star->sorted ? "sorted" : "unsorted" );
for ( uint64_t i=0; i<star->numEdgeEnds; ++i )
{
LWT_EDGEEND *ee = star->edgeEnds[i];
lwdebug(1, " EdgeEnd %" PRIu64 " is %s edge %" LWTFMT_ELEMID ", azimuth=%g",
i, ee->outgoing ? "outgoing" : "incoming",
ee->edge->edge_id, ee->azimuth
);
}
}
const LWT_EDGEEND *
lwt_edgeEndStar_getNextCW( LWT_EDGEEND_STAR *star, LWT_ISO_EDGE *edge, int outgoing )
{
lwt_edgeEndStar_ensureSorted( star );
uint64_t i=0;
LWT_EDGEEND *thisEdgeEnd = NULL;
for ( i=0; i<star->numEdgeEnds; ++i )
{
LWT_EDGEEND *ee = star->edgeEnds[i];
if ( ee->edge == edge && ee->outgoing == outgoing ) {
thisEdgeEnd = ee;
break;
}
}
if ( ! thisEdgeEnd ) {
lwerror("Could not find %s edge %" LWTFMT_ELEMID " in the star",
outgoing ? "outgoing" : "incoming", edge->edge_id);
return NULL;
}
LWT_EDGEEND *nextEdgeEnd = i < star->numEdgeEnds-1 ? star->edgeEnds[i+1] : star->edgeEnds[0];
return nextEdgeEnd;
}
const LWT_EDGEEND *
lwt_edgeEndStar_getNextCCW( LWT_EDGEEND_STAR *star, LWT_ISO_EDGE *edge, int outgoing )
{
lwt_edgeEndStar_ensureSorted( star );
uint64_t i=0;
LWT_EDGEEND *thisEdgeEnd = NULL;
for ( i=0; i<star->numEdgeEnds; ++i )
{
LWT_EDGEEND *ee = star->edgeEnds[i];
if ( ee->edge == edge && ee->outgoing == outgoing ) {
thisEdgeEnd = ee;
break;
}
}
if ( ! thisEdgeEnd ) {
lwerror("Could not find %s edge %" LWTFMT_ELEMID " in the star",
outgoing ? "outgoing" : "incoming", edge->edge_id);
return NULL;
}
LWT_EDGEEND *nextEdgeEnd = i > 0 ? star->edgeEnds[i-1] : star->edgeEnds[star->numEdgeEnds-1];
return nextEdgeEnd;
}
|