OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 
27 #include "WCreateColorArraysThread.h"
28 
29 
30 WCreateColorArraysThread::WCreateColorArraysThread( int left, int right, boost::shared_ptr< std::vector< float > >vertices,
31  boost::shared_ptr< std::vector< size_t > > lineStartIndexes,
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_lineStartIndexes( lineStartIndexes ),
45  m_lineLengths( lineLengths )
46 {
47 }
48 
50 {
51 }
52 
54 {
56  {
57  return;
58  }
59 
60  if( !m_vertices->size() ||
61  !m_tangents->size() ||
62  !m_globalColors->size() ||
63  !m_localColors->size() ||
64  !m_lineStartIndexes->size() ||
65  !m_lineLengths->size() )
66  {
67  return;
68  }
69 
70  int pc = 0;
71  for( int i = 0; i < m_left; ++i )
72  {
73  pc += (*m_lineLengths)[i]*3;
74  }
75 
76  float r, g, b, rr, gg, bb;
77  float x1, x2, y1, y2, z1, z2;
78  float lastx, lasty, lastz;
79  for( int i = m_left; i <= m_right; ++i )
80  {
81  x1 = (*m_vertices)[pc];
82  y1 = (*m_vertices)[pc + 1];
83  z1 = (*m_vertices)[pc + 2];
84  x2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 3 ];
85  y2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 2 ];
86  z2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 1 ];
87  r = ( x1 ) - ( x2 );
88  g = ( y1 ) - ( y2 );
89  b = ( z1 ) - ( z2 );
90  if( r < 0.0 )
91  r *= -1.0;
92  if( g < 0.0 )
93  g *= -1.0;
94  if( b < 0.0 )
95  b *= -1.0;
96 
97  float norm = sqrt( r * r + g * g + b * b );
98  r *= 1.0 / norm;
99  g *= 1.0 / norm;
100  b *= 1.0 / norm;
101 
102  lastx = (*m_vertices)[pc] + ( (*m_vertices)[pc] - (*m_vertices)[pc + 3] );
103  lasty = (*m_vertices)[pc+ 1] + ( (*m_vertices)[pc + 1] - (*m_vertices)[pc + 4] );
104  lastz = (*m_vertices)[pc + 2] + ( (*m_vertices)[pc + 2] - (*m_vertices)[pc + 5] );
105 
106  for( size_t j = 0; j < m_lineLengths->at( i ); ++j )
107  {
108  rr = lastx - (*m_vertices)[pc];
109  gg = lasty - (*m_vertices)[pc + 1];
110  bb = lastz - (*m_vertices)[pc + 2];
111  lastx = (*m_vertices)[pc];
112  lasty = (*m_vertices)[pc + 1];
113  lastz = (*m_vertices)[pc + 2];
114 
115  float norm = sqrt( rr * rr + gg * gg + bb * bb );
116  rr *= 1.0 / norm;
117  gg *= 1.0 / norm;
118  bb *= 1.0 / norm;
119  (*m_tangents)[pc] = rr;
120  (*m_tangents)[pc+1] = gg;
121  (*m_tangents)[pc+2] = bb;
122 
123  if( rr < 0.0 )
124  rr *= -1.0;
125  if( gg < 0.0 )
126  gg *= -1.0;
127  if( bb < 0.0 )
128  bb *= -1.0;
129 
130  (*m_localColors)[pc] = rr;
131  (*m_localColors)[pc+1] = gg;
132  (*m_localColors)[pc+2] = bb;
133 
134  (*m_globalColors)[pc] = r;
135  (*m_globalColors)[pc+1] = g;
136  (*m_globalColors)[pc+2] = b;
137  pc += 3;
138  }
139  }
140 
141  m_myThreadFinished = true;
142 }
143 
145 {
146  return m_myThreadFinished;
147 }