OpenWalnut  1.4.0
WCreateColorArraysThread.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <vector>
26 #include <cmath>
27 
28 #include "WCreateColorArraysThread.h"
29 
30 
31 WCreateColorArraysThread::WCreateColorArraysThread( int left, int right, boost::shared_ptr< std::vector< float > >vertices,
32  boost::shared_ptr< std::vector< size_t > > lineLengths,
33  boost::shared_ptr< std::vector< float > > globalColors,
34  boost::shared_ptr< std::vector< float > > localColors,
35  boost::shared_ptr< std::vector< float > > tangents ):
37  m_myThreadFinished( false ),
38  m_left( left ),
39  m_right( right ),
40  m_vertices( vertices ),
41  m_tangents( tangents ),
42  m_globalColors( globalColors ),
43  m_localColors( localColors ),
44  m_lineLengths( lineLengths )
45 {
46 }
47 
49 {
50 }
51 
53 {
55  {
56  return;
57  }
58 
59  if( !m_vertices->size() || !m_tangents->size() || !m_globalColors->size() || !m_localColors->size() || !m_lineLengths->size() )
60  {
61  return;
62  }
63 
64  int pc = 0;
65  for( int i = 0; i < m_left; ++i )
66  {
67  pc += (*m_lineLengths)[i]*3;
68  }
69 
70  float r, g, b, rr, gg, bb;
71  float x1, x2, y1, y2, z1, z2;
72  float lastx, lasty, lastz;
73  for( int i = m_left; i <= m_right; ++i )
74  {
75  x1 = (*m_vertices)[pc];
76  y1 = (*m_vertices)[pc + 1];
77  z1 = (*m_vertices)[pc + 2];
78  x2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 3 ];
79  y2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 2 ];
80  z2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 1 ];
81  r = ( x1 ) - ( x2 );
82  g = ( y1 ) - ( y2 );
83  b = ( z1 ) - ( z2 );
84  if( r < 0.0 )
85  r *= -1.0;
86  if( g < 0.0 )
87  g *= -1.0;
88  if( b < 0.0 )
89  b *= -1.0;
90 
91  float norm = std::sqrt( r * r + g * g + b * b );
92  r *= 1.0 / norm;
93  g *= 1.0 / norm;
94  b *= 1.0 / norm;
95 
96  lastx = (*m_vertices)[pc] + ( (*m_vertices)[pc] - (*m_vertices)[pc + 3] );
97  lasty = (*m_vertices)[pc+ 1] + ( (*m_vertices)[pc + 1] - (*m_vertices)[pc + 4] );
98  lastz = (*m_vertices)[pc + 2] + ( (*m_vertices)[pc + 2] - (*m_vertices)[pc + 5] );
99 
100  for( size_t j = 0; j < m_lineLengths->at( i ); ++j )
101  {
102  rr = lastx - (*m_vertices)[pc];
103  gg = lasty - (*m_vertices)[pc + 1];
104  bb = lastz - (*m_vertices)[pc + 2];
105  lastx = (*m_vertices)[pc];
106  lasty = (*m_vertices)[pc + 1];
107  lastz = (*m_vertices)[pc + 2];
108 
109  float norm = std::sqrt( rr * rr + gg * gg + bb * bb );
110  rr *= 1.0 / norm;
111  gg *= 1.0 / norm;
112  bb *= 1.0 / norm;
113  (*m_tangents)[pc] = rr;
114  (*m_tangents)[pc+1] = gg;
115  (*m_tangents)[pc+2] = bb;
116 
117  if( rr < 0.0 )
118  rr *= -1.0;
119  if( gg < 0.0 )
120  gg *= -1.0;
121  if( bb < 0.0 )
122  bb *= -1.0;
123 
124  (*m_localColors)[pc] = rr;
125  (*m_localColors)[pc+1] = gg;
126  (*m_localColors)[pc+2] = bb;
127 
128  (*m_globalColors)[pc] = r;
129  (*m_globalColors)[pc+1] = g;
130  (*m_globalColors)[pc+2] = b;
131  pc += 3;
132  }
133  }
134 
135  m_myThreadFinished = true;
136 }
137 
139 {
140  return m_myThreadFinished;
141 }