WElevationImageOutliner.cpp 8.41 KB
Newer Older
1 2 3 4
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
5
// Copyright 2013-2014 Andreas Schwarzkopf, OpenWalnut Community
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
// 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/>.
//
//---------------------------------------------------------------------------

#include <string>

#include <fstream>  // std::ifstream
#include <iostream> // std::cout
#include <vector>

#include <osg/Geometry>
#include "core/kernel/WModule.h"

#include "core/dataHandler/WDataSetScalar.h"
#include "core/graphicsEngine/callbacks/WGELinearTranslationCallback.h"
#include "core/graphicsEngine/shaders/WGEPropertyUniform.h"
#include "core/graphicsEngine/shaders/WGEShader.h"
#include "core/graphicsEngine/WGEGeodeUtils.h"
#include "core/graphicsEngine/WGEManagedGroupNode.h"
#include "core/kernel/WKernel.h"
#include "core/kernel/WModuleInputData.h"
#include "WElevationImageOutliner.h"

WElevationImageOutliner::WElevationImageOutliner()
{
    m_minElevImageZ = 0;
    m_intensityIncreasesPerMeter = 5;
    m_vertices = new WQuadTree( 1 );
    m_printedQuadrats = new WQuadTree( 1 );
}

WElevationImageOutliner::~WElevationImageOutliner()
{
}
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
double WElevationImageOutliner::getSurfaceArea2D( WQuadTree* quadTree )
{
    return calculateSurfaceForNode2D( quadTree->getRootNode(), quadTree );
}

double WElevationImageOutliner::calculateSurfaceForNode2D( WQuadNode* node, WQuadTree* quadTree )
{
    if( node->getRadius() <= quadTree->getDetailLevel() )
    {
        return node->getRadius() * node->getRadius() * 4;
    }
    else
    {
        double area = 0.0;
        for  ( size_t child = 0; child < 4; child++ )
            if  ( node->getChild( child ) != 0 )
                area += calculateSurfaceForNode2D( node->getChild( child ), quadTree );
        return area;
    }
}

77 78 79 80 81 82 83 84
void WElevationImageOutliner::importElevationImage( WQuadTree* quadTree, size_t elevImageMode )
{
    boost::shared_ptr< WTriangleMesh > tmpMesh( new WTriangleMesh( 0, 0 ) );
    m_outputMesh = tmpMesh;
    m_vertices = new WQuadTree( quadTree->getDetailLevel() );
    m_printedQuadrats = new WQuadTree( quadTree->getDetailLevel() );
    drawNode( quadTree->getRootNode(), quadTree, elevImageMode );
}
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
void WElevationImageOutliner::drawNode(
        WQuadNode* node, WQuadTree* quadTree, size_t elevImageMode )
{
    if( node->getRadius() <= quadTree->getDetailLevel() )
    {
        double x = node->getCenter( 0 );
        double y = node->getCenter( 1 );
        double d = quadTree->getDetailLevel() * 2;

        WQuadNode* sourceNodes[][3] = { //NOLINT
            { quadTree->getLeafNode( x-d, y-d ), quadTree->getLeafNode( x, y-d ), quadTree->getLeafNode( x+d, y-d ) }, //NOLINT
            { quadTree->getLeafNode( x-d, y ), quadTree->getLeafNode( x, y ), quadTree->getLeafNode( x+d, y ) }, //NOLINT
            { quadTree->getLeafNode( x-d, y+d ), quadTree->getLeafNode( x, y+d ), quadTree->getLeafNode( x+d, y+d ) } }; //NOLINT
        for( size_t yIdx = 0; yIdx < 2; yIdx++)
            for( size_t xIdx = 0; xIdx < 2; xIdx++)
            {
                double xOff = static_cast<double>( xIdx )*d-d+x;
                double yOff = static_cast<double>( yIdx )*d-d+y;
                bool isDone = m_printedQuadrats->getLeafNode( xOff, yOff ) != 0;
                if( !isDone )
                {
                    WQuadNode* sourceQuadrat[] = {
                        sourceNodes[xIdx][yIdx], sourceNodes[xIdx][yIdx+1],
                        sourceNodes[xIdx+1][yIdx+1], sourceNodes[xIdx+1][yIdx] };
                    size_t current = 0;
                    WQuadNode* list[] = {0, 0, 0};
                    for(int index = 0; index < 4; index++ )
                    {
                        if( sourceQuadrat[index] != 0 ) list[current++] = sourceQuadrat[index];
                        if( current == 3 )
                        {
                            size_t vertID0 = getVertexID( list[0], elevImageMode );
                            size_t vertID1 = getVertexID( list[1], elevImageMode );
                            size_t vertID2 = getVertexID( list[2], elevImageMode );
                            m_outputMesh->addTriangle( vertID0, vertID1, vertID2 );
                            list[1] = list[2];
                            current--;
                        }
                    }
                    m_printedQuadrats->registerPoint( xOff, yOff, 0 );
                }
            }
    }
    else
    {
        for  ( size_t child = 0; child < 4; child++ )
            if  ( node->getChild( child ) != 0 )
                drawNode( node->getChild( child ), quadTree, elevImageMode );
    }
}
136

137 138 139 140 141 142 143
size_t WElevationImageOutliner::getVertexID(
        WQuadNode* node, size_t elevImageMode )
{
    double x = node->getCenter( 0 );
    double y = node->getCenter( 1 );
    WQuadNode* existingNode = m_vertices->getLeafNode( x, y );
    if( existingNode != 0 ) return existingNode->getID();
144
    double elevation = elevImageMode != 0 ?node->getValueMax() :node->getValueMin();
145 146 147 148 149
    if( elevImageMode == 2 ) elevation = node->getPointCount();
    m_vertices->registerPoint( x, y, elevation );
    existingNode = m_vertices->getLeafNode( x, y );
    size_t currentVertex = m_outputMesh->vertSize();
    existingNode->setID( currentVertex );
150
    m_outputMesh->addVertex( x, y, m_showElevationInMeshOffset ?elevation :0 );
151 152 153 154
    elevation = ( elevation - m_minElevImageZ ) * m_intensityIncreasesPerMeter;
    if( elevation < 0.0 ) elevation = 0.0;
    if( elevation > 255.0 ) elevation = 255.0;
    elevation /= 255.0;
155
    if( !m_showElevationInMeshColor ) elevation = 128;
156 157 158 159
    osg::Vec4 color = osg::Vec4( elevation, elevation, elevation, 1.0 );
    m_outputMesh->setVertexColor( currentVertex, color );
    return existingNode->getID();
}
160

161 162 163 164 165
void WElevationImageOutliner::setExportElevationImageSettings( double minElevImageZ, double intensityIncreasesPerMeter )
{
    m_minElevImageZ = minElevImageZ;
    m_intensityIncreasesPerMeter = intensityIncreasesPerMeter;
}
166

167 168 169 170
void WElevationImageOutliner::setShowElevationInMeshColor( bool showElevationInMeshColor )
{
    m_showElevationInMeshColor = showElevationInMeshColor;
}
171

172 173 174 175
void WElevationImageOutliner::setShowElevationInMeshOffset( bool showElevationInMeshOffset )
{
    m_showElevationInMeshOffset = showElevationInMeshOffset;
}
176

177 178 179 180
boost::shared_ptr< WTriangleMesh > WElevationImageOutliner::getOutputMesh()
{
    return m_outputMesh;
}
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
void WElevationImageOutliner::highlightBuildingGroups( boost::shared_ptr< WDataSetPointsGrouped >  groupedPoints )
{
    if( !groupedPoints ) return;
    WDataSetPointsGrouped::VertexArray verts = groupedPoints->getVertices();
    WDataSetPointsGrouped::GroupArray groups = groupedPoints->getGroups();
    size_t count = verts->size()/3;
    size_t nulls = 0;
    size_t outOfRanges = 0;
    for( size_t vertex = 0; vertex < count; vertex++)
    {
        float x = verts->at( vertex*3 );
        float y = verts->at( vertex*3+1 );
        WQuadNode* node = m_vertices->getLeafNode( x, y );
        if( node != 0 )
        {
            if( node->getID() < m_outputMesh->vertSize() )
            {
                size_t group = groups->at( vertex );
                osg::Vec4 color = osg::Vec4( WOctree::calcColor( group, 0 ),
                        WOctree::calcColor( group, 1 ), WOctree::calcColor( group, 2 ), 1.0f );
                m_outputMesh->setVertexColor( node->getID(), color );
            }
            else
            {
                outOfRanges++;
            }
        }
        else
        {
            nulls++;
        }
    }
    if( nulls > 0 )
        std::cout << "!!!WARNING!!! WElevationImageExporter: " << nulls << " invalid poinnt hits!" << std::endl;
    if( outOfRanges > 0 )
        std::cout << "!!!WARNING!!! WElevationImageExporter: " << outOfRanges << " out of ranges!" << std::endl;
}