WMWriteTracts.cpp 23.5 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 25 26
//---------------------------------------------------------------------------
//
// 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/>.
//
//---------------------------------------------------------------------------

#include <string>

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

33 34
W_LOADABLE_MODULE( WMWriteTracts )

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
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
51
    return WMWriteTracts_xpm;
52 53 54 55 56 57 58 59 60
}

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

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

void WMWriteTracts::connectors()
{
66 67
    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" );
68 69 70 71 72 73

    WModule::connectors();
}

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

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

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    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." );
    //m_povrayOptions->setHidden( true );
    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 );
100 101 102 103
    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
104

105
    WModule::properties();
106 107 108 109
}

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

    ready();

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

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

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

    const char* c_file = m_savePath->get().file_string().c_str();
    std::ofstream dataFile( c_file, std::ios_base::binary );

185
    if( dataFile )
schurade's avatar
schurade committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    {
        WLogger::getLogger()->addLogMessage( "opening file", "Write Tracts", LL_DEBUG );
    }
    else
    {
        WLogger::getLogger()->addLogMessage( "open file failed" + m_savePath->get().file_string() , "Write Tracts", LL_ERROR );
        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;
205
    for( size_t i = 0; i < (verts->size() - 1 )/ 3; ++i )
schurade's avatar
schurade committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    {
        fValue = 160 - verts->at( i * 3 );
        dataFile << fValue << ",";
        fValue = 200 - verts->at( i * 3 + 1 );
        dataFile << fValue << ",";
        fValue = verts->at( i * 3 + 2 );
        dataFile << fValue << ",";
    }
    fValue = 160 - verts->at( verts->size() - 3 );
    dataFile << fValue << ",";
    fValue = 200 - verts->at( verts->size() - 2 );
    dataFile << fValue << ",";
    fValue = verts->at( verts->size() - 1 );
    dataFile << fValue << "],\n";

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

    dataFile << ( "    \"colors\" : [" );
    boost::shared_ptr<std::vector<float> > colors = ds->getGlobalColors();
233
    for( size_t i = 0; i < colors->size() - 3; i += 3 )
schurade's avatar
schurade committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    {
        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();
252
    for( size_t i = 0; i < lengths->size() - 1; ++i )
schurade's avatar
schurade committed
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
    {
        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;
    }

    const char* c_file = m_savePath->get().file_string().c_str();
    std::ofstream dataFile( c_file, std::ios_base::binary );

285
    if( dataFile )
schurade's avatar
schurade committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    {
        WLogger::getLogger()->addLogMessage( "opening file", "Write Tracts", LL_DEBUG );
    }
    else
    {
        WLogger::getLogger()->addLogMessage( "open file failed" + m_savePath->get().file_string() , "Write Tracts", LL_ERROR );
        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();
    boost::shared_ptr<std::vector<float> > colors = ds->getGlobalColors();

314
    for( size_t k = 0; k < lengths->size(); ++k )
schurade's avatar
schurade committed
315 316
    {
        size_t newLength = 0;
317
        for( size_t i = starts->at( k ); i < ( starts->at( k ) + lengths->at( k ) ); ++i )
schurade's avatar
schurade committed
318
        {
319
            if( i % 2 == 0 )
320
            {
schurade's avatar
schurade committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334
                nVertices.push_back( 160 - verts->at( i * 3 ) );
                nVertices.push_back( 200 - verts->at( i * 3 + 1 ) );
                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;
335
            }
336
        }
schurade's avatar
schurade committed
337 338 339 340 341 342 343 344 345 346 347 348
        nIndices.push_back( newLength );
    }



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

    dataFile << ( "{\n" );

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

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

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

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

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

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

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

380
    for( size_t i = 0; i < nIndices.size() - 1; ++i )
schurade's avatar
schurade committed
381 382 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 413
    {
        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;
    }

    const char* c_file = m_savePath->get().file_string().c_str();
    std::ofstream dataFile( c_file, std::ios_base::binary );

414
    if( dataFile )
schurade's avatar
schurade committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    {
        WLogger::getLogger()->addLogMessage( "opening file", "Write Tracts", LL_DEBUG );
    }
    else
    {
        WLogger::getLogger()->addLogMessage( "open file failed" + m_savePath->get().file_string() , "Write Tracts", LL_ERROR );
        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;
435
    for( size_t i = 0; i < verts->size() - 3; ++i )
schurade's avatar
schurade committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
    {
        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;
456
    for( size_t i = 0; i < lengths->size(); ++i )
schurade's avatar
schurade committed
457
    {
458
        for( size_t k = 0; k < lengths->at( i ); ++k )
schurade's avatar
schurade committed
459 460 461 462 463 464 465 466 467 468 469
        {
            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;
470
}
471 472 473 474

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

    // 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 );
491
    if( !dataFile )
492
    {
493
        errorLog() << "Opening " << fnMeshAbs << " failed.";
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
        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;
520 521 522 523 524
    size_t increment = m_povraySaveOnlyNth->get();

    boost::shared_ptr< WProgress > progress1 = boost::shared_ptr< WProgress >( new WProgress( "Converting fibers", fibStart->size() / increment) );
    m_progress->addSubProgress( progress1 );
    for( size_t fidx = 0; fidx < fibStart->size(); fidx += increment )
525 526 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
    {
        ++*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 <<
567
                        " <" << lastvert.x() << "," << lastvert.y() << "," << lastvert.z() << ">," <<
568
                          "<" << vert.x() << "," << vert.y() << "," << vert.z() << ">,Diameter" << std::endl <<
569
                        " pigment{color rgb <" << color.x() << "," << color.y() << "," << color.z() << ">}" << std::endl <<
570
                        " transform MoveToCenter" << std::endl <<
571 572
                        "}" << std::endl;
            dataFile << "sphere {" << std::endl <<
573
                        " <" << vert.x() << "," << vert.y() << "," << vert.z() << ">,Diameter" << std::endl <<
574
                        " pigment{ color rgb <" << color.x() << "," << color.y() << "," << color.z() << ">}" << std::endl <<
575
                        " transform MoveToCenter" << std::endl <<
576 577 578 579 580 581 582
                        "}" << std::endl;

            lastvert = vert;
        }
        currentStart += len;
    }

583 584 585 586 587 588 589 590 591 592 593 594 595 596
    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 );
597
    if( !dataFileScene )
598 599 600 601 602 603 604 605
    {
        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
606 607 608 609
    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;

610 611 612 613 614 615 616 617 618 619 620 621 622
    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;
    }
623

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

    // save camera and add a light
634 635 636 637 638 639
    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
640
    dataFileScene << "// Tube diameter" << std::endl;
641
    dataFileScene << "#declare Diameter = " << m_povrayTubeDiameter->get() << ";" << std::endl << std::endl;
642 643 644

    // 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
645
                "  orthographic angle 45" << std::endl <<
646 647
                "  location CamPosition" << std::endl <<
                "  right 1.33*x" << std::endl <<
Sebastian Eichelbaum's avatar
[DOC]  
Sebastian Eichelbaum committed
648 649
                "  // use this with 1280x1024" << std::endl <<
                "  // right 1.25*x" << std::endl <<
650 651 652 653 654 655 656 657 658 659 660
                "  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;
661 662

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

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