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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
/* Copyright (c) 1997-2024
Ewgenij Gawrilow, Michael Joswig, and the polymake team
Technische Universität Berlin, Germany
https://polymake.org
This program 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, or (at your option) any
later version: http://www.gnu.org/licenses/gpl.txt.
This program 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.
--------------------------------------------------------------------------------
*/
#pragma once
#include "polymake/client.h"
#include "polymake/Graph.h"
#include "polymake/Set.h"
#include "polymake/Vector.h"
#include "polymake/Matrix.h"
#include "polymake/graph/graph_iterators.h"
#include "polymake/graph/DoublyConnectedEdgeList.h"
namespace polymake { namespace topaz {
// TODO: REMOVE! using Cone = Set<Vector<Rational>>;
// TODO: REMOVE! using Indexed_Cones = Map<Cone, Int>;
// TODO: REMOVE! using flip_sequence = std::list<Int>;
// TODO: REMOVE! using Fan_Vertices = Map<Vector<Rational>, Int>;
// TODO: REMOVE! using Fan_Max_Cells = std::list<Set<Int>>;
class FlipVisitor : public graph::NodeVisitor<> {
friend class DoublyConnectedEdgeList;
friend class SecondaryFan;
public:
using Cone = Set<Vector<Rational>>;
using Indexed_Cones = Map<Cone, Int>;
using flip_sequence = std::list<Int>;
using Fan_Vertices = Map<Vector<Rational>, Int>;
using Fan_Max_Cells = std::list<Set<Int>>;
using DoublyConnectedEdgeList = graph::dcel::DoublyConnectedEdgeList;
private:
// the graph we want to iterate through, built during iterations
Graph<Directed>& delaunay_graph;
// the base triangulation
DoublyConnectedEdgeList& dcel;
// collect all cones, IDs as its corresponding node in the delaunay_graph
Indexed_Cones cones;
// for each node of the graph we save the list of indices to flip to the corresponding dcel
Map<Int, flip_sequence> flipIds_to_node;
// a set of all vertices of the fan, each of which is labeled - we only need this to define a polyhedral complex
Fan_Vertices fan_vertices;
// a list where each entry is a Set that represents a maximal cone of the fan, e.g. {0,1,2,4} is the cone with the rays with labels 1,2,3 and 5; NOTE THE INDEX SHIFT! (we ignore the origin)
Fan_Max_Cells fan_cells;
// counter for the number vertices of the fan, equal to 1 + number of rays
Int fan_num_vert;
// number of punctures +1
Int dim;
// we store the ray indices of the facets at the coordinate hyperplane boundary for the extension to a complete fan by the all -1 vector
Fan_Max_Cells boundary_facets;
public:
// this is needed for the BFS++ to not just stop in depth one
static constexpr bool visit_all_edges = true;
FlipVisitor(Graph<Directed>& G, DoublyConnectedEdgeList& dcel_)
: delaunay_graph(G)
, dcel(dcel_)
{
// this is the dimension of the fan +1, or equivalently the number of punctures +1
dim = dcel.DelaunayInequalities().cols();
// the flip word of the first cone is obtained by finding the triangulation that is Delaunay for all weights = 1
flip_sequence start_flips = dcel.flipToDelaunayAlt( ones_vector<Rational>(dim) );
flipIds_to_node[0] = start_flips;
Cone first_cone = dcel.coneRays();
// add the first cone, from the starting dcel
cones[ first_cone ] = 0;
// the origin is always a vertex of the fan, for purposes of calculations we need to make sure that it is mapped to 0 by the Map "fan_vertices"
Vector<Rational> origin(dim);
origin[0] = 1;
fan_vertices[ origin ] = 0;
// since we just added the first cone we set the vertex-counter to 1
fan_num_vert = 1;
// updating fan_vertices, fan_num_vert and fan_cells resp. the first cone
add_cone(first_cone);
// flip back to input triangulation
dcel.flipEdges(start_flips, true);
}
bool operator()(Int n)
{
return operator()(n, n);
}
/* Preconditions:
1) the node n_to is part of the Delaunay graph,
2) the flip sequence that transforms the base DCEL into the one corresponding to n_to ("flipIds_to_node[ n_to ]") is known and
3) so is the corresponding cone in Indexed_Cones.
After this operation all the neighbors of n_to are in the graph (with the corr. edges) with their flip sequences and
cones respectively
*/
bool operator()(Int n_from, Int n_to)
{
if (visited.contains(n_to)) return false;
// we flip the start-triangulation T(0) to the triangulation T(n_to) corresponding to node n_to
dcel.flipEdges(flipIds_to_node[n_to]);
// calculate the secondary cone of triangulation n_to
BigObject p("polytope::Polytope<Rational>",
"INEQUALITIES", dcel.DelaunayInequalities());
IncidenceMatrix<> rays_in_facets = p.give("VERTICES_IN_FACETS");
Matrix<Rational> rays = p.give("VERTICES");
Matrix<Rational> facets = p.give("FACETS");
// We compute a point outside each valid facet of the n_to-cone, the parameter 0 < epsilon < 1 is the distance of the point to its corresponding facet.
// The point shall be contained in a top-dimensional cone that meets the n_to-cone in this facet.
// If this is not the case, we consider a new point with distance epsilon^2 and try again
for (Int i = 0 ; i < facets.rows() ; ++i) {
flip_sequence new_flips{};
// we store the facets at the coordinate hyperplanes for purposes of the fan estension to a complete fan
if (dcel.nonZeros(facets[i]) == 1 && facets[i][0] == 0) {
Set<Int> boundary_rays_ids;
for (const auto it : rays_in_facets[i]) {
if (rays[it][0] == 0) {
boundary_rays_ids += fan_vertices[ dcel.normalize(rays[it]) ]-1;
}
}
boundary_facets.push_back(boundary_rays_ids);
}
if (dcel.validFacet(facets[i])) {
Set<Vector<Rational>> facet_rays;
for (const auto it : rays_in_facets[i]) {
facet_rays += dcel.normalize(rays[it]);
}
Cone new_cone{};
Rational epsilon{1,10};
bool cone_is_neighbor = false;
while (!cone_is_neighbor) {
Vector<Rational> neighbor_point = neighborConePoint(facets[i], facet_rays, epsilon);
// we use the flip algorithm to determine a flip sequence that makes the triangulation Delaunay w.r.t. the weights given by neighbor_point
new_flips = dcel.flipToDelaunayAlt(neighbor_point);
// calculate cone, and check if really neighbored in facet[i]; if not take epsilon^2 and start over
new_cone = dcel.coneRays();
if (incl(facet_rays, new_cone) == -1) {
cone_is_neighbor = true;
} else {
dcel.flipEdges(new_flips, true);
epsilon = epsilon * epsilon;
}
}
// add a new node to the graph and save all the corresponding data ( flip sequence, add_cone, cones )
if (!cones.exists(new_cone) && new_cone.size() > dim-1) {
const Int new_id = delaunay_graph.add_node();
delaunay_graph.add_edge(n_to, new_id);
flip_sequence new_flipIds{ flipIds_to_node[n_to] };
new_flipIds.insert(new_flipIds.end(), new_flips.begin(), new_flips.end());
flipIds_to_node[new_id] = new_flipIds;
cones[new_cone] = new_id;
add_cone(new_cone);
}
}
// flip back to T(n_to)
dcel.flipEdges(new_flips, true);
}
// flip back to T(0)
dcel.flipEdges( flipIds_to_node[n_to] , true );
visited += n_to;
return true;
}
// when adding a cone we update the input data for the fan, namely the vertices and the maximal cells
void add_cone(Cone new_cone)
{
Set<Int> fan_cell;
for (const auto& it : new_cone) {
// case: the vertex is new
if (!fan_vertices.exists(it)) {
fan_vertices[it] = fan_num_vert;
fan_cell += fan_num_vert-1; // the -1 is an index shift, the fan_cells do not consider vertex 0 with index 0 and we relabel the verticesby -1
++fan_num_vert;
}
// case: the vertex is already known by some previous cone
else {
if (fan_vertices[it] != 0) fan_cell += fan_vertices[it]-1;
}
}
fan_cells.push_back(fan_cell);
}
Int getfan_num_vert() const
{
return fan_num_vert;
}
Int getdim() const
{
return dim;
}
const Fan_Max_Cells& getfan_cells() const
{
return fan_cells;
}
const Fan_Vertices& getfan_vertices() const
{
return fan_vertices;
}
const Fan_Max_Cells& getboundary_facets() const
{
return boundary_facets;
}
const Map<Int, flip_sequence>& getflipIds_to_node() const
{
return flipIds_to_node;
}
// Given a facet of a 0-pointed cone via its inner normal vector & a set of the rays of this facet we return a point outside the cone near to the facet
Vector<Rational> neighborConePoint(const Vector<Rational>& facet_normal, const Set<Vector<Rational>>& facet_vertices, const Rational& epsilon)
{
Rational eps(epsilon);
Vector<Rational> point(dim);
Vector<Rational> sum(dim);
for (const auto& it : facet_vertices) {
if (it[0] == 0) sum += it;
}
bool positive = false;
do {
point = 1/eps * sum;
point = point - eps * facet_normal;
positive = true;
for (Int i = 1; i < point.size(); ++i) {
if (point[i] <= 0) {
positive = false;
eps = eps*eps;
break;
}
}
} while (!positive);
return point;
}
friend std::pair<Matrix<Rational>, Array<Set<Int>>> DCEL_secondary_fan_input(DoublyConnectedEdgeList& dcel);
friend Indexed_Cones DCEL_secondary_fan(DoublyConnectedEdgeList& dcel);
friend Matrix<Rational> DCEL_secondary_fan_input_vertices(DoublyConnectedEdgeList& dcel);
friend Array<Set<Int>> DCEL_secondary_fan_input_cells(DoublyConnectedEdgeList& dcel);
}; // end class flip visitor
// the flip algorithm, we flip edges that are non-Delaunay w.r.t. the weights as long as there are some
FlipVisitor::flip_sequence flipToDelaunay(graph::DoublyConnectedEdgeList& dcel, const Vector<Rational>& weights)
{
FlipVisitor::flip_sequence flip_ids{};
Int non_delaunay = dcel.is_Delaunay(weights);
while (non_delaunay != -1) {
dcel.flipEdge(non_delaunay);
flip_ids.push_back(non_delaunay);
non_delaunay = dcel.is_Delaunay(weights);
}
return flip_ids;
}
} //end topaz namespace
} //end polymake namespace
// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
|