WMWriteTracts.cpp 23.6 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 2009 OpenWalnut Community, BSV-Leipzig and CNCF-CBS
// 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 <fstream>
26 27
#include <string>
#include <vector>
28

29 30 31
#include "core/common/WPropertyHelper.h"
#include "core/dataHandler/io/WWriterFiberVTK.h"
#include "core/kernel/WKernel.h"
32
#include "WMWriteTracts.h"
33
#include "WMWriteTracts.xpm"
34

35 36
W_LOADABLE_MODULE( WMWriteTracts )

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
WMWriteTracts::WMWriteTracts():
    WModule()
{
}

WMWriteTracts::~WMWriteTracts()
{
}

boost::shared_ptr< WModule > WMWriteTracts::factory() const
{
    return boost::shared_ptr< WModule >( new WMWriteTracts() );
}

const char** WMWriteTracts::getXPMIcon() const
{
Alexander Wiebel's avatar
Alexander Wiebel committed
53
    return WMWriteTracts_xpm;
54 55 56 57 58 59 60 61 62
}

const std::string WMWriteTracts::getName() const
{
    return "Write Tracts";
}

const std::string WMWriteTracts::getDescription() const
{
63
    return "Writes tracts either from a cluster or from a WDataSetFibers to a file";
64 65 66 67
}

void WMWriteTracts::connectors()
{
68 69
    m_clusterIC = WModuleInputData< const WFiberCluster >::createAndAdd( shared_from_this(), "clusterInput", "A the tracts behind the WFiberCluster" ); // NOLINT line length
    m_tractIC = WModuleInputData< const WDataSetFibers >::createAndAdd( shared_from_this(), "tractInput", "A dataset of tracts" );
70 71 72 73 74 75

    WModule::connectors();
}

void WMWriteTracts::properties()
{
76
    m_savePath = m_properties->addProperty( "Save Path", "Where to save the result", boost::filesystem::path( "/no/such/file" ) );
77
    WPropertyHelper::PC_NOTEMPTY::addTo( m_savePath );
78
    m_run      = m_properties->addProperty( "Save", "Start saving", WPVBaseTypes::PV_TRIGGER_READY );
79

schurade's avatar
schurade committed
80
    m_fileTypeSelectionsList = boost::shared_ptr< WItemSelection >( new WItemSelection() );
81
    m_fileTypeSelectionsList->addItem( "VTK fib", "Stores the fibers in the VTK line format." );
schurade's avatar
schurade committed
82 83 84
    m_fileTypeSelectionsList->addItem( "json", "" );
    m_fileTypeSelectionsList->addItem( "json2", "" );
    m_fileTypeSelectionsList->addItem( "json triangles", "" );
85
    m_fileTypeSelectionsList->addItem( "POVRay Cylinders", "Stores the fibers as cylinders in a POVRay SDL file." );
schurade's avatar
schurade committed
86

87 88 89 90 91 92
    m_fileTypeSelection = m_properties->addProperty( "File type",  "file type.", m_fileTypeSelectionsList->getSelectorFirst(),
        boost::bind( &WMWriteTracts::fileTypeChanged, this )
    );
    WPropertyHelper::PC_SELECTONLYONE::addTo( m_fileTypeSelection );

    m_povrayOptions = m_properties->addPropertyGroup( "POVRay Options", "Options for the POVRay Exporter." );
93
    m_povrayOptions->setHidden( true );
94 95 96 97 98 99 100 101
    m_povrayTubeDiameter = m_povrayOptions->addProperty( "Tube Diameter",
                                  "The tube diameter. Each fibers is represented as a tube with spheres as connections between them",
                                  0.25 );
    m_povrayTubeDiameter->setMin( 0.001 );
    m_povrayTubeDiameter->setMax( 2.0 );
    m_povrayRadiosity = m_povrayOptions->addProperty( "Enable Radiosity",
                                  "Enable POVRay's radiosity renderer. Creates more realistic lighting but is very slow.",
                                  false );
102 103 104 105
    m_povraySaveOnlyNth = m_povrayOptions->addProperty( "Save Every n'th", "Option allows thinning the data. This is useful in cases were fast"
                                                        " rendering is needed.", 1 );
    m_povraySaveOnlyNth->setMin( 1 );
    m_povraySaveOnlyNth->setMax( 1000 );
schurade's avatar
schurade committed
106

107
    WModule::properties();
108 109 110 111
}

void WMWriteTracts::moduleMain()
{
112 113
    m_moduleState.add( m_clusterIC->getDataChangedCondition() );
    m_moduleState.add( m_tractIC->getDataChangedCondition() );
114
    m_moduleState.add( m_run->getCondition() );
115 116 117 118 119

    ready();

    while( !m_shutdownFlag() )
    {
120 121 122 123
        debugLog() << "Waiting for data ...";
        m_moduleState.wait();

        if( !m_clusterIC->getData() && !m_tractIC->getData() )
124 125 126 127
        {
            continue;
        }

128
        if( m_run->get( true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
129
        {
130
            switch( m_fileTypeSelection->get( true ).getItemIndexOfSelected( 0 ) )
131
            {
schurade's avatar
schurade committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
                case 0:
                    {
                        WWriterFiberVTK w( m_savePath->get(), true );
                        if( m_clusterIC->getData() )
                        {
                            w.writeFibs( m_clusterIC->getData()->getDataSetReference() );
                        }
                        else if( m_tractIC->getData() )
                        {
                            w.writeFibs( m_tractIC->getData() );
                        }
                    }
                    break;
                case 1:
                    saveJson();
                    break;
                case 2:
149 150 151
                    saveJson2();
                    break;
                case 3:
schurade's avatar
schurade committed
152 153
                    saveJsonTriangles();
                    break;
154 155 156 157 158 159
                case 4:
                        if( m_tractIC->getData() )
                        {
                            savePOVRay( m_tractIC->getData() );
                        }
                    break;
schurade's avatar
schurade committed
160 161 162
                default:
                    debugLog() << "this shouldn't be reached";
                    break;
163
            }
schurade's avatar
schurade committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
            m_run->set( WPVBaseTypes::PV_TRIGGER_READY, true );
        }
    }
}

bool WMWriteTracts::saveJson() const
{
    boost::shared_ptr< const WDataSetFibers > ds = m_tractIC->getData();

    if( !ds )
    {
        return false;
    }

    if( ds->getVertices()->size() == 0 )
    {
        WLogger::getLogger()->addLogMessage( "Will not write file that contains 0 vertices.", "Write Tracts", LL_ERROR );
        return false;
    }

184
    const char* c_file = m_savePath->get().string().c_str();
schurade's avatar
schurade committed
185 186
    std::ofstream dataFile( c_file, std::ios_base::binary );

187
    if( dataFile )
schurade's avatar
schurade committed
188 189 190 191 192
    {
        WLogger::getLogger()->addLogMessage( "opening file", "Write Tracts", LL_DEBUG );
    }
    else
    {
193
        WLogger::getLogger()->addLogMessage( "open file failed" + m_savePath->get().string() , "Write Tracts", LL_ERROR );
schurade's avatar
schurade committed
194 195 196 197 198 199 200 201 202 203 204 205 206
        return false;
    }

    dataFile.setf( std::ios_base::fixed );
    dataFile.precision( 3 );

    WLogger::getLogger()->addLogMessage( "start writing file", "Write Tracts", LL_DEBUG );

    dataFile << ( "{\n" );

    dataFile << ( "    \"vertices\" : [" );
    boost::shared_ptr<std::vector<float> > verts = ds->getVertices();
    float fValue;
207
    for( size_t i = 0; i < (verts->size() - 1 )/ 3; ++i )
schurade's avatar
schurade committed
208
    {
209
        fValue = verts->at( i * 3 );
schurade's avatar
schurade committed
210
        dataFile << fValue << ",";
211
        fValue = verts->at( i * 3 + 1 );
schurade's avatar
schurade committed
212 213 214 215
        dataFile << fValue << ",";
        fValue = verts->at( i * 3 + 2 );
        dataFile << fValue << ",";
    }
216
    fValue = verts->at( verts->size() - 3 );
schurade's avatar
schurade committed
217
    dataFile << fValue << ",";
218
    fValue = verts->at( verts->size() - 2 );
schurade's avatar
schurade committed
219 220 221 222 223 224
    dataFile << fValue << ",";
    fValue = verts->at( verts->size() - 1 );
    dataFile << fValue << "],\n";

    dataFile << ( "    \"normals\" : [" );
    boost::shared_ptr<std::vector<float> > tangents = ds->getTangents();
225
    for( size_t i = 0; i < tangents->size() - 1; ++i )
schurade's avatar
schurade committed
226 227 228 229 230 231 232 233
    {
        fValue = tangents->at( i );
        dataFile << fValue << ",";
    }
    fValue = tangents->at( tangents->size() - 1 );
    dataFile << fValue << "],\n";

    dataFile << ( "    \"colors\" : [" );
234
    boost::shared_ptr< std::vector< float > > colors = ds->getColorScheme( "Global Color" )->getColor();
235
    for( size_t i = 0; i < colors->size() - 3; i += 3 )
schurade's avatar
schurade committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    {
        fValue = colors->at( i );
        dataFile << fValue << ",";
        fValue = colors->at( i + 1 );
        dataFile << fValue << ",";
        fValue = colors->at( i + 2 );
        dataFile << fValue << ",1.0,";
    }
    fValue = colors->at( colors->size() - 3 );
    dataFile << fValue << ",";
    fValue = colors->at( colors->size() - 2 );
    dataFile << fValue << ",";
    fValue = colors->at( colors->size() - 1 );
    dataFile << fValue << ",1.0],\n";

    int iValue;
    dataFile << ( "    \"indices\" : [" );
    boost::shared_ptr<std::vector<size_t> > lengths = ds->getLineLengths();
254
    for( size_t i = 0; i < lengths->size() - 1; ++i )
schurade's avatar
schurade committed
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
    {
        iValue = lengths->at( i );
        dataFile << iValue << ",";
    }
    iValue = lengths->at( lengths->size() - 1 );
    dataFile << iValue << "]";

    dataFile <<  "\n}";

    dataFile.close();
    WLogger::getLogger()->addLogMessage( "saving done", "Write Tracts", LL_DEBUG );
    return true;
}

bool WMWriteTracts::saveJson2() const
{
    boost::shared_ptr< const WDataSetFibers > ds = m_tractIC->getData();

    if( !ds )
    {
        return false;
    }

    if( ds->getVertices()->size() == 0 )
    {
        WLogger::getLogger()->addLogMessage( "Will not write file that contains 0 vertices.", "Write Tracts", LL_ERROR );
        return false;
    }

284
    const char* c_file = m_savePath->get().string().c_str();
schurade's avatar
schurade committed
285 286
    std::ofstream dataFile( c_file, std::ios_base::binary );

287
    if( dataFile )
schurade's avatar
schurade committed
288 289 290 291 292
    {
        WLogger::getLogger()->addLogMessage( "opening file", "Write Tracts", LL_DEBUG );
    }
    else
    {
293
        WLogger::getLogger()->addLogMessage( "open file failed" + m_savePath->get().string() , "Write Tracts", LL_ERROR );
schurade's avatar
schurade committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        return false;
    }

    dataFile.setf( std::ios_base::fixed );
    dataFile.precision( 3 );

    //**************************************************************************************
    // create arrays;

    std::vector<float> nVertices;
    std::vector<float> nNormals;
    std::vector<float> nColors;
    std::vector<size_t> nIndices;

    boost::shared_ptr<std::vector<size_t> > starts = ds->getLineStartIndexes();
    boost::shared_ptr<std::vector<size_t> > lengths = ds->getLineLengths();


    boost::shared_ptr<std::vector<float> > verts = ds->getVertices();
    boost::shared_ptr<std::vector<float> > tangents = ds->getTangents();
314
    boost::shared_ptr< std::vector< float > > colors = ds->getColorScheme( "Global Color" )->getColor();
schurade's avatar
schurade committed
315

316
    for( size_t k = 0; k < lengths->size(); ++k )
schurade's avatar
schurade committed
317 318
    {
        size_t newLength = 0;
319
        for( size_t i = starts->at( k ); i < ( starts->at( k ) + lengths->at( k ) ); ++i )
schurade's avatar
schurade committed
320
        {
321
            if( i % 2 == 0 )
322
            {
323 324
                nVertices.push_back( verts->at( i * 3 ) );
                nVertices.push_back( verts->at( i * 3 + 1 ) );
schurade's avatar
schurade committed
325 326 327 328 329 330 331 332 333 334 335 336
                nVertices.push_back( verts->at( i * 3 + 2 ) );

                nNormals.push_back( - tangents->at( i * 3 ) );
                nNormals.push_back( - tangents->at( i * 3 + 1 ) );
                nNormals.push_back( tangents->at( i * 3 + 2 ) );

                nColors.push_back( colors->at( i * 3 ) );
                nColors.push_back( colors->at( i * 3 + 1 ) );
                nColors.push_back( colors->at( i * 3 + 2 ) );
                nColors.push_back( 1.0 );

                ++newLength;
337
            }
338
        }
schurade's avatar
schurade committed
339 340 341 342 343 344 345 346 347 348 349 350
        nIndices.push_back( newLength );
    }



    WLogger::getLogger()->addLogMessage( "start writing file", "Write Tracts", LL_DEBUG );

    dataFile << ( "{\n" );

    dataFile << ( "    \"vertices\" : [" );

    float fValue;
351
    for( size_t i = 0; i < nVertices.size() - 1 ; ++i )
schurade's avatar
schurade committed
352 353 354 355 356 357 358 359 360
    {
        fValue = nVertices[i];
        dataFile << fValue << ",";
    }
    fValue = verts->at( verts->size() - 1 );
    dataFile << fValue << "],\n";

    dataFile << ( "    \"normals\" : [" );

361
    for( size_t i = 0; i < nNormals.size() - 1; ++i )
schurade's avatar
schurade committed
362 363 364 365 366 367 368 369 370
    {
        fValue = nNormals[i];
        dataFile << fValue << ",";
    }
    fValue = nNormals[nNormals.size() - 1];
    dataFile << fValue << "],\n";

    dataFile << ( "    \"colors\" : [" );

371
    for( size_t i = 0; i < nColors.size()- 1; ++i )
schurade's avatar
schurade committed
372 373 374
    {
        fValue = nColors[i];
        dataFile << fValue << ",";
375
    }
schurade's avatar
schurade committed
376 377 378 379 380 381
    fValue = nColors[nColors.size() - 1];
    dataFile << fValue << ",1.0],\n";

    int iValue;
    dataFile << ( "    \"indices\" : [" );

382
    for( size_t i = 0; i < nIndices.size() - 1; ++i )
schurade's avatar
schurade committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
    {
        iValue = nIndices[i];
        dataFile << iValue << ",";
    }
    iValue = nIndices[nIndices.size() - 1];
    dataFile << iValue << "]";

    dataFile <<  "\n}";

    dataFile.close();
    WLogger::getLogger()->addLogMessage( "saving done", "Write Tracts", LL_DEBUG );
    return true;
}


bool WMWriteTracts::saveJsonTriangles() const
{
    boost::shared_ptr< const WDataSetFibers > ds = m_tractIC->getData();

    if( !ds )
    {
        return false;
    }

    if( ds->getVertices()->size() == 0 )
    {
        WLogger::getLogger()->addLogMessage( "Will not write file that contains 0 vertices.", "Write Tracts", LL_ERROR );
        return false;
    }

413
    const char* c_file = m_savePath->get().string().c_str();
schurade's avatar
schurade committed
414 415
    std::ofstream dataFile( c_file, std::ios_base::binary );

416
    if( dataFile )
schurade's avatar
schurade committed
417 418 419 420 421
    {
        WLogger::getLogger()->addLogMessage( "opening file", "Write Tracts", LL_DEBUG );
    }
    else
    {
422
        WLogger::getLogger()->addLogMessage( "open file failed" + m_savePath->get().string() , "Write Tracts", LL_ERROR );
schurade's avatar
schurade committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436
        return false;
    }

    dataFile.precision( 7 );

    WLogger::getLogger()->addLogMessage( "start writing file", "Write Tracts", LL_DEBUG );

    dataFile << ( "{\n" );

    dataFile << ( "    \"vertices\" : [" );
    boost::shared_ptr<std::vector<float> > verts = ds->getVertices();
    float fValue0;
    float fValue1;
    float fValue2;
437
    for( size_t i = 0; i < verts->size() - 3; ++i )
schurade's avatar
schurade committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    {
        fValue0 = verts->at( i );
        i += 1;
        fValue1 = verts->at( i );
        i += 1;
        fValue2 = verts->at( i );
        dataFile << fValue0 << "," << fValue1 << "," << fValue2 << ",";
        dataFile << fValue0 << "," << fValue1 << "," << fValue2 << ",";
    }
    fValue0 = verts->at( verts->size() - 3 );
    fValue1 = verts->at( verts->size() - 2 );
    fValue2 = verts->at( verts->size() - 1 );
    dataFile << fValue0 << "," << fValue1 << "," << fValue2 << ",";
    dataFile << fValue0 << "," << fValue1 << "," << fValue2 << "],\n";

    dataFile << ( "    \"indices\" : [" );
    boost::shared_ptr<std::vector<size_t> > starts = ds->getLineStartIndexes();
    boost::shared_ptr<std::vector<size_t> > lengths = ds->getLineLengths();

    int counter = 0;
458
    for( size_t i = 0; i < lengths->size(); ++i )
schurade's avatar
schurade committed
459
    {
460
        for( size_t k = 0; k < lengths->at( i ); ++k )
schurade's avatar
schurade committed
461 462 463 464 465 466 467 468 469 470 471
        {
            dataFile << counter << "," << counter + 1 << "," << counter + 2 << ",";
            dataFile << counter + 1 << "," << counter + 3 << "," << counter + 2 << ",";
        }
    }

    dataFile <<  "\n}";

    dataFile.close();
    WLogger::getLogger()->addLogMessage( "saving done", "Write Tracts", LL_DEBUG );
    return true;
472
}
473 474 475 476

bool WMWriteTracts::savePOVRay( boost::shared_ptr< const WDataSetFibers > fibers ) const
{
    // open file
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
477
    boost::filesystem::path meshFile( m_savePath->get() );
478
    std::string fnPath = meshFile.parent_path().string();
479 480
    std::string fnBase = meshFile.stem().string();
    std::string fnExt = meshFile.extension().string();
481 482 483 484 485 486 487 488 489 490 491 492

    // construct the filenames
    // the meshfile
    std::string fnMesh = fnBase + ".mesh" + fnExt;
    std::string fnScene = fnBase + ".scene" + fnExt;

    // absolute paths
    std::string fnMeshAbs = fnPath + "/" + fnMesh;
    std::string fnSceneAbs = fnPath + "/" + fnScene;

    debugLog() << "Opening " << fnMeshAbs << " for writing the mesh data.";
    std::ofstream dataFile( fnMeshAbs.c_str(), std::ios_base::binary );
493
    if( !dataFile )
494
    {
495
        errorLog() << "Opening " << fnMeshAbs << " failed.";
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
        return false;
    }

    // needed arrays for iterating the fibers
    WDataSetFibers::IndexArray  fibStart = fibers->getLineStartIndexes();
    WDataSetFibers::LengthArray fibLen   = fibers->getLineLengths();
    WDataSetFibers::VertexArray fibVerts = fibers->getVertices();
    WDataSetFibers::TangentArray fibTangents = fibers->getTangents();

    // get current color scheme - the mode is important as it defines the number of floats in the color array per vertex.
    WDataSetFibers::ColorScheme::ColorMode fibColorMode = fibers->getColorScheme()->getMode();
    debugLog() << "Color mode is " << fibColorMode << ".";
    WDataSetFibers::ColorArray  fibColors = fibers->getColorScheme()->getColor();

    // for each fiber:
    debugLog() << "Iterating over all fibers.";

    // find min and max
    double minX = wlimits::MAX_DOUBLE;
    double minY = wlimits::MAX_DOUBLE;
    double minZ = wlimits::MAX_DOUBLE;
    double maxX = wlimits::MIN_DOUBLE;
    double maxY = wlimits::MIN_DOUBLE;
    double maxZ = wlimits::MIN_DOUBLE;

    size_t currentStart = 0;
522 523
    size_t increment = m_povraySaveOnlyNth->get();

Sebastian Eichelbaum's avatar
[DOC]  
Sebastian Eichelbaum committed
524
    boost::shared_ptr< WProgress > progress1 = boost::shared_ptr< WProgress >( new WProgress( "Converting fibers", fibStart->size() / increment ) );
525 526
    m_progress->addSubProgress( progress1 );
    for( size_t fidx = 0; fidx < fibStart->size(); fidx += increment )
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    {
        ++*progress1;

        // the start vertex index
        size_t sidx = fibStart->at( fidx ) * 3;
        size_t csidx = fibStart->at( fidx ) * fibColorMode;

        // the length of the fiber
        size_t len = fibLen->at( fidx );

        // walk along the fiber
        WVector3d lastvert( fibVerts->at( sidx ),
                            fibVerts->at( sidx + 1 ),
                            fibVerts->at( sidx + 2 ) );
        for( size_t k = 1; k < len; ++k )
        {
            // grab vector and color
            WVector3d vert( fibVerts->at( ( 3 * k ) + sidx ),
                            fibVerts->at( ( 3 * k ) + sidx + 1 ),
                            fibVerts->at( ( 3 * k ) + sidx + 2 ) );
            WColor color( fibColors->at( ( fibColorMode * k ) + csidx + ( 0 % fibColorMode ) ),
                          fibColors->at( ( fibColorMode * k ) + csidx + ( 1 % fibColorMode ) ),
                          fibColors->at( ( fibColorMode * k ) + csidx + ( 2 % fibColorMode ) ),
                          ( fibColorMode == WDataSetFibers::ColorScheme::RGBA ) ?
                            fibColors->at( ( fibColorMode * k ) + csidx + ( 3 % fibColorMode ) ) : 1.0 );

            if( vert.x() > maxX )
                maxX = vert.x();
            if( vert.y() > maxY )
                maxY = vert.y();
            if( vert.z() > maxZ )
                maxZ = vert.z();
            if( vert.x() < minX )
                minX = vert.x();
            if( vert.y() < minY )
                minY = vert.y();
            if( vert.z() < minZ )
                minZ = vert.z();

            // write it in POVRay style
            dataFile << "cylinder" << std::endl <<
                        "{" << std::endl <<
569
                        " <" << lastvert.x() << "," << lastvert.y() << "," << lastvert.z() << ">," <<
570
                          "<" << vert.x() << "," << vert.y() << "," << vert.z() << ">,Diameter" << std::endl <<
571
                        " pigment{color rgb <" << color.x() << "," << color.y() << "," << color.z() << ">}" << std::endl <<
572
                        " transform MoveToCenter" << std::endl <<
573 574
                        "}" << std::endl;
            dataFile << "sphere {" << std::endl <<
575
                        " <" << vert.x() << "," << vert.y() << "," << vert.z() << ">,Diameter" << std::endl <<
576
                        " pigment{ color rgb <" << color.x() << "," << color.y() << "," << color.z() << ">}" << std::endl <<
577
                        " transform MoveToCenter" << std::endl <<
578 579 580 581 582 583 584
                        "}" << std::endl;

            lastvert = vert;
        }
        currentStart += len;
    }

585 586 587 588 589 590 591 592 593 594 595 596 597 598
    double sizeX = maxX - minX;
    double sizeY = maxY - minY;
    double sizeZ = maxZ - minZ;
    double mX = minX + ( sizeX / 2.0 );
    double mY = minY + ( sizeY / 2.0 );
    double mZ = minZ + ( sizeZ / 2.0 );

    // done writing mesh. Close
    infoLog() << "Done. Closing " << fnMesh << ".";
    dataFile.close();
    progress1->finish();

    debugLog() << "Opening " << fnSceneAbs << " for writing.";
    std::ofstream dataFileScene( fnSceneAbs.c_str(), std::ios_base::binary );
599
    if( !dataFileScene )
600 601 602 603 604 605 606 607
    {
        errorLog() << "Opening " << fnSceneAbs << " failed.";
        return false;
    }

    // write some head data
    dataFileScene << "#version 3.6;" << std::endl << std::endl;

Sebastian Eichelbaum's avatar
[DOC]  
Sebastian Eichelbaum committed
608 609 610 611
    dataFileScene << "// run with  povray -w800 -h600 -Q0 fibsLarge.scene.pov " << std::endl <<
                     "// * this creates a fast preview of the scene with a resolution of 800x600." << std::endl <<
                     "// * the Q parameter defines the quality Q0 means plain colors. Q11 is best, including radiosity." << std::endl << std::endl;

612 613 614 615 616 617 618 619 620 621 622 623 624
    if( m_povrayRadiosity->get() )
    {
        dataFileScene << "global_settings {" << std::endl <<
        " ambient_light 0" << std::endl << std::endl <<
        " radiosity {" << std::endl <<
        "  pretrace_start 0.08" << std::endl <<
        "  pretrace_end   0.005" << std::endl <<
        "  count 350" << std::endl <<
        "  error_bound 0.15" << std::endl <<
        "  recursion_limit 2" << std::endl <<
        " }"  << std::endl <<
        "}" << std::endl << std::endl;
    }
625

Alexander Wiebel's avatar
Alexander Wiebel committed
626
    dataFileScene << "// Enable Phong lighting for all the geometry" << std::endl <<
627 628 629 630
    dataFileScene << "#default{" << std::endl <<
                   " finish{" << std::endl <<
                   "  ambient 0" << std::endl <<
                   "  phong 1" << std::endl <<
Sebastian Eichelbaum's avatar
[DOC]  
Sebastian Eichelbaum committed
631
                   "  // reflection 0.9 " << std::endl <<
632 633
                   " }" << std::endl <<
                   "}" << std::endl << std::endl;
634 635

    // save camera and add a light
636 637 638 639 640 641
    double camX = 0;
    double camY = sizeY;
    double camZ = 0;

    dataFileScene << "#declare MoveToCenter = transform{ translate < " << -mX << ", " << -mY << ", " << -mZ << " > };" << std::endl;
    dataFileScene << "#declare CamPosition = < " << camX << ", " << camY << ", " << camZ << " >;" << std::endl << std::endl;
Sebastian Eichelbaum's avatar
[DOC]  
Sebastian Eichelbaum committed
642
    dataFileScene << "// Tube diameter" << std::endl;
643
    dataFileScene << "#declare Diameter = " << m_povrayTubeDiameter->get() << ";" << std::endl << std::endl;
644 645 646

    // this camera should produce a direct front view. The user surely needs to modify the camera
    dataFileScene << "camera {" << std::endl <<
Sebastian Eichelbaum's avatar
[DOC]  
Sebastian Eichelbaum committed
647
                "  orthographic angle 45" << std::endl <<
648 649
                "  location CamPosition" << std::endl <<
                "  right 1.33*x" << std::endl <<
Sebastian Eichelbaum's avatar
[DOC]  
Sebastian Eichelbaum committed
650 651
                "  // use this with 1280x1024" << std::endl <<
                "  // right 1.25*x" << std::endl <<
652 653 654 655 656 657 658 659 660 661 662
                "  up y " << std::endl <<
                "  look_at  < 0, 0, 0 >" << std::endl <<
                "}" << std::endl << std::endl;
    // headlight
    dataFileScene << "light_source {" << std::endl <<
                     "  CamPosition" << std::endl <<
                     "  color rgb <1.0, 1.0, 1.0>" << std::endl <<
                     "}" << std::endl << std::endl;

    // do not forget the mesh
    dataFileScene << "#include \"" << fnMesh << "\"" << std::endl;
663 664

    // done. Close
665 666
    infoLog() << "Done.";
    dataFileScene.close();
667 668
    return true;
}
669 670 671 672 673 674 675 676 677 678 679 680 681

void WMWriteTracts::fileTypeChanged()
{
    if( m_fileTypeSelection->get().getItemIndexOfSelected( 0 ) == 4 )
    {
        m_povrayOptions->setHidden( false );
    }
    else
    {
        m_povrayOptions->setHidden( true );
    }
}