OpenWalnut  1.5.0dev
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 <cmath>
26 #include <memory>
27 #include <vector>
28 
29 #include "WCreateColorArraysThread.h"
30 
31 
32 WCreateColorArraysThread::WCreateColorArraysThread( int left, int right, std::shared_ptr< std::vector< float > >vertices,
33  std::shared_ptr< std::vector< size_t > > lineLengths,
34  std::shared_ptr< std::vector< float > > globalColors,
35  std::shared_ptr< std::vector< float > > localColors,
36  std::shared_ptr< std::vector< float > > tangents ):
38  m_myThreadFinished( false ),
39  m_left( left ),
40  m_right( right ),
41  m_vertices( vertices ),
42  m_tangents( tangents ),
43  m_globalColors( globalColors ),
44  m_localColors( localColors ),
45  m_lineLengths( lineLengths )
46 {
47 }
48 
50 {
51 }
52 
54 {
56  {
57  return;
58  }
59 
60  if( !m_vertices->size() || !m_tangents->size() || !m_globalColors->size() || !m_localColors->size() || !m_lineLengths->size() )
61  {
62  return;
63  }
64 
65  int pc = 0;
66  for( int i = 0; i < m_left; ++i )
67  {
68  pc += (*m_lineLengths)[i]*3;
69  }
70 
71  float r, g, b, rr, gg, bb;
72  float x1, x2, y1, y2, z1, z2;
73  float lastx, lasty, lastz;
74  for( int i = m_left; i <= m_right; ++i )
75  {
76  x1 = (*m_vertices)[pc];
77  y1 = (*m_vertices)[pc + 1];
78  z1 = (*m_vertices)[pc + 2];
79  x2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 3 ];
80  y2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 2 ];
81  z2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 1 ];
82  r = ( x1 ) - ( x2 );
83  g = ( y1 ) - ( y2 );
84  b = ( z1 ) - ( z2 );
85  if( r < 0.0 )
86  r *= -1.0;
87  if( g < 0.0 )
88  g *= -1.0;
89  if( b < 0.0 )
90  b *= -1.0;
91 
92  float norm = std::sqrt( r * r + g * g + b * b );
93  if( norm == 0.0 )
94  norm = 1.0;
95 
96  r *= 1.0 / norm;
97  g *= 1.0 / norm;
98  b *= 1.0 / norm;
99 
100  lastx = (*m_vertices)[pc] + ( (*m_vertices)[pc] - (*m_vertices)[pc + 3] );
101  lasty = (*m_vertices)[pc+ 1] + ( (*m_vertices)[pc + 1] - (*m_vertices)[pc + 4] );
102  lastz = (*m_vertices)[pc + 2] + ( (*m_vertices)[pc + 2] - (*m_vertices)[pc + 5] );
103 
104  for( size_t j = 0; j < m_lineLengths->at( i ); ++j )
105  {
106  rr = lastx - (*m_vertices)[pc];
107  gg = lasty - (*m_vertices)[pc + 1];
108  bb = lastz - (*m_vertices)[pc + 2];
109  lastx = (*m_vertices)[pc];
110  lasty = (*m_vertices)[pc + 1];
111  lastz = (*m_vertices)[pc + 2];
112 
113  float norm = std::sqrt( rr * rr + gg * gg + bb * bb );
114  if( norm == 0.0 )
115  norm = 1.0;
116 
117  rr *= 1.0 / norm;
118  gg *= 1.0 / norm;
119  bb *= 1.0 / norm;
120  (*m_tangents)[pc] = rr;
121  (*m_tangents)[pc+1] = gg;
122  (*m_tangents)[pc+2] = bb;
123 
124  if( rr < 0.0 )
125  rr *= -1.0;
126  if( gg < 0.0 )
127  gg *= -1.0;
128  if( bb < 0.0 )
129  bb *= -1.0;
130 
131  (*m_localColors)[pc] = rr;
132  (*m_localColors)[pc+1] = gg;
133  (*m_localColors)[pc+2] = bb;
134 
135  (*m_globalColors)[pc] = r;
136  (*m_globalColors)[pc+1] = g;
137  (*m_globalColors)[pc+2] = b;
138  pc += 3;
139  }
140  }
141 
142  m_myThreadFinished = true;
143 }
144 
146 {
147  return m_myThreadFinished;
148 }
149 
std::shared_ptr< const std::vector< float > > m_vertices
Point vector for all fibers.
virtual void threadMain()
entry for the run command
bool m_myThreadFinished
Has the thread finished?
std::shared_ptr< std::vector< float > > m_tangents
Point vector for tangents at each vertex, used for fake tubes.
std::shared_ptr< std::vector< float > > m_globalColors
Storing the global color value of the fibers for each point.
WCreateColorArraysThread(int left, int right, std::shared_ptr< std::vector< float > >vertices, std::shared_ptr< std::vector< size_t > > lineLengths, std::shared_ptr< std::vector< float > > globalColors, std::shared_ptr< std::vector< float > > localColors, std::shared_ptr< std::vector< float > > tangents)
default constructor
std::shared_ptr< const std::vector< size_t > > m_lineLengths
Line vector that contains the number of vertices for each line.
std::shared_ptr< std::vector< float > > m_localColors
Storing the local color value of the fibers for each point.
bool isFinished()
Return the value of the finished flag.
virtual ~WCreateColorArraysThread()
destructor
Base class for all classes needing to be executed in a separate thread.