WVisiTrace.cpp 8.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2017 OpenWalnut Community
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

25
#include <string>
Alexander Wiebel's avatar
[STYLE]  
Alexander Wiebel committed
26
#include <utility>
27 28 29 30 31
#include <vector>

#include "core/common/math/linearAlgebra/WPosition.h"
#include "core/common/WAssert.h"

32 33 34 35 36
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/property_map/property_map.hpp>

37 38 39 40 41 42 43 44 45 46
#include "WVisiTrace.h"

WVisiTrace::WVisiTrace():
    m_candidatePositions(),
    m_candidateJumps(),
    m_curve3D(),
    m_dataChanged( false )
{
}

47
std::vector< WPosition > WVisiTrace::getLine()
48 49 50 51 52 53 54 55 56 57
{
    if( m_dataChanged )
    {
        performVisiTrace();
        m_dataChanged = false;
    }

    return m_curve3D;
}

58
void WVisiTrace::addCandidatesForRay(  const std::vector< std::pair< double, WPosition > > candidates )
59
{
Alexander Wiebel's avatar
Alexander Wiebel committed
60 61
    std::vector< double > opacityJumps( 0 );
    std::vector< WPosition > positions( 0 );
62 63 64 65 66 67

    for( size_t id = 0; id < candidates.size(); ++id )
    {
        opacityJumps.push_back( candidates[id].first );
        positions.push_back( candidates[id].second );
    }
68 69 70 71 72 73 74

    m_candidatePositions.push_back( positions );
    m_candidateJumps.push_back( opacityJumps );

    m_dataChanged = true;
}

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
std::vector< std::pair< int, int > > WVisiTrace::getLinearizedNodesRefs() const
{
    std::vector< std::pair< int, int > > nodeRefs( 0 );
    for( size_t outerId = 0; outerId < m_candidatePositions.size(); ++outerId )
    {
        for( size_t innerId = 0; innerId < m_candidatePositions[outerId].size(); ++innerId )
        {
            nodeRefs.push_back( std::make_pair( outerId, innerId ) );
        }
    }
    return nodeRefs;
}

std::vector< std::vector< int > > WVisiTrace::getInverseLinearizedNodesRefs() const
{
    std::vector< std::vector< int > > inverseRefs( 0 );
    size_t counter = 0;
    for( size_t outerId = 0; outerId < m_candidatePositions.size(); ++outerId )
    {
        inverseRefs.push_back( std::vector< int >( 0 ) );
        for( size_t innerId = 0; innerId < m_candidatePositions[outerId].size(); ++innerId )
        {
            inverseRefs[outerId].push_back( counter );
            ++counter;
        }
    }
    return inverseRefs;
}

104 105
void WVisiTrace::performDijkstra()
{
106 107 108 109 110 111
    // Check if there is something to do
    if( m_candidatePositions.size() == 0 || m_candidateJumps.size() == 0 )
    {
        return;
    }

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
    //using namespace boost;
    typedef boost::adjacency_list< boost::listS, boost::vecS, boost::directedS,
                                   boost::no_property, boost::property< boost::edge_weight_t, double > > graph_t;
    typedef boost::graph_traits< graph_t >::vertex_descriptor vertex_descriptor;
    typedef std::pair<int, int> Edge;

    std::vector< std::pair< int, int > > linearized = getLinearizedNodesRefs();
    std::vector< std::vector< int > > linearizedInverse = getInverseLinearizedNodesRefs();

    const int numVirtNodes = 2; // virtual start an end nodes
    const int startNodeId = 0;
    const int endNodeId = 1;

    const int num_nodes = linearized.size() + numVirtNodes;

    std::vector< std::string > name( 0 );
    name.push_back( "Start" );
    name.push_back( "End" );
    for( int id = 0; id < num_nodes; ++id )
    {
        name.push_back( std::to_string( id + numVirtNodes ) );
    }

    std::vector< Edge > edgeVector( 0 );
    std::vector< double > weightsV( 0 );
    // Edges from virtual start node to candidates of first ray
    for( auto candi : linearizedInverse[0] )
    {
        edgeVector.push_back( Edge( startNodeId, candi + numVirtNodes ) );
        weightsV.push_back( 1 );
    }

    // Edges from candidates of one ray to those of the next ray
    for( size_t rayId = 0; rayId < linearizedInverse.size() - 1; ++rayId )
    {
        for( size_t firstId = 0; firstId < linearizedInverse[rayId].size(); ++firstId )
        {
            for( size_t secondId = 0; secondId < linearizedInverse[rayId+1].size(); ++secondId )
            {
                edgeVector.push_back( Edge( linearizedInverse[rayId][firstId] + numVirtNodes,
                                            linearizedInverse[rayId+1][secondId] + numVirtNodes ) );
                WPosition firstPos = m_candidatePositions[rayId][firstId];
                WPosition secondPos = m_candidatePositions[rayId+1][secondId];
                double distance = length( firstPos - secondPos );
                weightsV.push_back( distance );
            }
        }
    }

    // Edges from candidates of last ray to virtual end node
    for( auto candi : linearizedInverse[linearizedInverse.size()-1] )
    {
        edgeVector.push_back( Edge( candi + numVirtNodes, endNodeId ) );
        weightsV.push_back( 1 );
    }

    Edge* edge_array = &edgeVector[0];
    double* weights = &weightsV[0];
    int num_arcs = edgeVector.size();

    graph_t g( edge_array, edge_array + num_arcs, weights, num_nodes );
    //property_map<graph_t, edge_weight_t>::type weightmap = get(edge_weight, g);
    std::vector<vertex_descriptor> p( num_vertices( g ) );
    std::vector<double> distResult( num_vertices( g ) );
    vertex_descriptor s = vertex( startNodeId, g );

    dijkstra_shortest_paths( g,
                             s,
                             predecessor_map( boost::make_iterator_property_map( p.begin(), get( boost::vertex_index, g ) ) ).
                             distance_map( boost::make_iterator_property_map( distResult.begin(), get( boost::vertex_index, g ) ) ) );

    std::vector< int > shortestPathIds( 0 );

    int parentId = endNodeId;
    shortestPathIds.push_back( parentId );
    while( parentId != startNodeId )
    {
        parentId = p[parentId];
        shortestPathIds.push_back( parentId );
    }
    std::reverse( shortestPathIds.begin(), shortestPathIds.end() );

    for( size_t id = 1; id < shortestPathIds.size() - 1; ++id )
    {
        int rayId = linearized[shortestPathIds[id]-numVirtNodes].first;
        int candidateId = linearized[shortestPathIds[id]-numVirtNodes].second;
        m_curve3D.push_back( m_candidatePositions[rayId][candidateId] );
    }

    // DEBUGGING: writing solution to file and stdout
    // {
    // #include <iostream>
    // #include <fstream>
    //     std::cout << "distances and parents:" << std::endl;
    //     std::cout << "distance(" << "END" << ") = " << distResult[endNodeId] << std::endl;
    //     std::cout << "distances and parents:" << std::endl;
    //     graph_traits < graph_t >::vertex_iterator vi, vend;
    //     for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi)
    //     {
    //         std::cout << "distance(" << name[*vi] << ") = " << distResult[*vi] << ", ";
    //         std::cout << "parent(" << name[*vi] << ") = " << name[p[*vi]] << std::endl;
    //     }
    //     std::cout << std::endl;

    //     std::ofstream dot_file("/tmp/dijkstra-eg.dot");

    //     dot_file << "digraph D {\n"
    //              << "  rankdir=LR\n"
    //              << "  size=\"20,20\"\n"
    //              << "  ratio=\"fill\"\n"
    //              << "  edge[style=\"bold\"]\n" << "  node[shape=\"circle\"]\n";

    //     graph_traits < graph_t >::edge_iterator ei, ei_end;
    //     for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
    //         graph_traits < graph_t >::edge_descriptor e = *ei;
    //         graph_traits < graph_t >::vertex_descriptor
    //             u = source(e, g), v = target(e, g);
    //         dot_file << name[u] << " -> " << name[v]
    //                  << "[label=\"" << get(weightmap, e) << "\"";
    //         if (p[v] == u)
    //             dot_file << ", color=\"black\"";
    //         else
    //             dot_file << ", color=\"grey\"";
    //         dot_file << "]";
    //     }
    //     dot_file << "}";
    // }
}

241 242
void WVisiTrace::performVisiTrace()
{
243
    performDijkstra();
244 245 246 247 248 249 250 251 252
}

void WVisiTrace::reset()
{
    m_candidatePositions.clear();
    m_candidateJumps.clear();
    m_dataChanged = true;
    m_curve3D.clear(); // not really needed because m_dataChanged is true.
}