OpenWalnut  1.4.0
WCreateColorArraysThread.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <vector>
00026 #include <cmath>
00027 
00028 #include "WCreateColorArraysThread.h"
00029 
00030 
00031 WCreateColorArraysThread::WCreateColorArraysThread(  int left, int right, boost::shared_ptr< std::vector< float > >vertices,
00032         boost::shared_ptr< std::vector< size_t > > lineLengths,
00033         boost::shared_ptr< std::vector< float > > globalColors,
00034         boost::shared_ptr< std::vector< float > > localColors,
00035         boost::shared_ptr< std::vector< float > > tangents ):
00036     WThreadedRunner(),
00037     m_myThreadFinished( false ),
00038     m_left( left ),
00039     m_right( right ),
00040     m_vertices( vertices ),
00041     m_tangents( tangents ),
00042     m_globalColors( globalColors ),
00043     m_localColors( localColors ),
00044     m_lineLengths( lineLengths )
00045 {
00046 }
00047 
00048 WCreateColorArraysThread::~WCreateColorArraysThread()
00049 {
00050 }
00051 
00052 void WCreateColorArraysThread::threadMain()
00053 {
00054     if( !m_vertices || !m_tangents || !m_globalColors || !m_localColors || !m_lineLengths )
00055     {
00056         return;
00057     }
00058 
00059     if( !m_vertices->size() || !m_tangents->size() || !m_globalColors->size() || !m_localColors->size() || !m_lineLengths->size() )
00060     {
00061         return;
00062     }
00063 
00064     int pc = 0;
00065     for( int i = 0; i < m_left; ++i )
00066     {
00067         pc += (*m_lineLengths)[i]*3;
00068     }
00069 
00070     float r, g, b, rr, gg, bb;
00071     float x1, x2, y1, y2, z1, z2;
00072     float lastx, lasty, lastz;
00073     for( int i = m_left; i <= m_right; ++i )
00074     {
00075         x1 = (*m_vertices)[pc];
00076         y1 = (*m_vertices)[pc + 1];
00077         z1 = (*m_vertices)[pc + 2];
00078         x2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 3 ];
00079         y2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 2 ];
00080         z2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 1 ];
00081         r = ( x1 ) - ( x2 );
00082         g = ( y1 ) - ( y2 );
00083         b = ( z1 ) - ( z2 );
00084         if( r < 0.0 )
00085             r *= -1.0;
00086         if( g < 0.0 )
00087             g *= -1.0;
00088         if( b < 0.0 )
00089             b *= -1.0;
00090 
00091         float norm = std::sqrt( r * r + g * g + b * b );
00092         r *= 1.0 / norm;
00093         g *= 1.0 / norm;
00094         b *= 1.0 / norm;
00095 
00096         lastx = (*m_vertices)[pc] + ( (*m_vertices)[pc] - (*m_vertices)[pc + 3] );
00097         lasty = (*m_vertices)[pc+ 1] + ( (*m_vertices)[pc + 1] - (*m_vertices)[pc + 4] );
00098         lastz = (*m_vertices)[pc + 2] + ( (*m_vertices)[pc + 2] - (*m_vertices)[pc + 5] );
00099 
00100         for( size_t j = 0; j < m_lineLengths->at( i ); ++j )
00101         {
00102             rr = lastx - (*m_vertices)[pc];
00103             gg = lasty - (*m_vertices)[pc + 1];
00104             bb = lastz - (*m_vertices)[pc + 2];
00105             lastx = (*m_vertices)[pc];
00106             lasty = (*m_vertices)[pc + 1];
00107             lastz = (*m_vertices)[pc + 2];
00108 
00109             float norm = std::sqrt( rr * rr + gg * gg + bb * bb );
00110             rr *= 1.0 / norm;
00111             gg *= 1.0 / norm;
00112             bb *= 1.0 / norm;
00113             (*m_tangents)[pc] = rr;
00114             (*m_tangents)[pc+1] = gg;
00115             (*m_tangents)[pc+2] = bb;
00116 
00117             if( rr < 0.0 )
00118                 rr *= -1.0;
00119             if( gg < 0.0 )
00120                 gg *= -1.0;
00121             if( bb < 0.0 )
00122                 bb *= -1.0;
00123 
00124             (*m_localColors)[pc] = rr;
00125             (*m_localColors)[pc+1] = gg;
00126             (*m_localColors)[pc+2] = bb;
00127 
00128             (*m_globalColors)[pc] = r;
00129             (*m_globalColors)[pc+1] = g;
00130             (*m_globalColors)[pc+2] = b;
00131             pc += 3;
00132         }
00133     }
00134 
00135     m_myThreadFinished = true;
00136 }
00137 
00138 bool WCreateColorArraysThread::isFinished()
00139 {
00140     return m_myThreadFinished;
00141 }