OpenWalnut  1.5.0dev
WDataCreatorFiberSpiral.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 <algorithm>
26 
27 #include "core/common/math/WMath.h"
28 
29 #include "WDataCreatorFiberSpiral.h"
30 
32  WObjectNDIP< WDataSetFibersCreatorInterface >( "Spiral", "Create a spiral of fibers." )
33 {
34  // initialize members
35  m_numRotations = m_properties->addProperty( "Num Rotations", "The number of rotations.", 25 );
36  m_numRotations->setMin( 1 );
37  m_numRotations->setMax( 1000 );
38  m_tubeRadius = m_properties->addProperty( "Tube Radius", "The radius of the tube spiralling up.", 1.0 );
39  m_tubeRadius->setMin( 0.01 );
40  m_tubeRadius->setMax( 5.0 );
41 }
42 
44 {
45  // cleanup
46 }
47 
49  WProgress::SPtr progress,
50  const WColor& color,
51  size_t numFibers,
52  size_t numVertsPerFiber,
53  const WPosition& origin,
54  const WPosition& size,
58  WDataSetFibers::IndexArray fibIdxVertexMap,
60 {
61  std::srand( seed );
62 
63  WPosition originOffset = origin + size / 2.0;
64  originOffset.z() = origin.z();
65  double spiralRadius = std::min( size.x(), size.y() ) / 2.0;
66  double tubeRadius = m_tubeRadius->get();
67  double height = size.z();
68  double numRotations = m_numRotations->get();
69  WColor fibColor = color;
70 
71  // create each
72  for( size_t fidx = 0; fidx < numFibers; ++fidx )
73  {
74  size_t vertOffset = fidx * numVertsPerFiber;
75  fibIdx->push_back( vertOffset );
76  lengths->push_back( numVertsPerFiber );
77 
78  double a1 = static_cast< double >( std::rand() % 255 ) / 255.0; // NOLINT - rand_r has portability issues.
79  double a2 = static_cast< double >( std::rand() % 255 ) / 255.0; // NOLINT - rand_r has portability issues.
80 
81  double seedX = cos( 2.0 * pi() * a1 ) * tubeRadius;
82  double seedY = sin( 2.0 * pi() * a2 ) * tubeRadius;
83  double seedZ = sqrt( tubeRadius - ( seedX * seedX ) - ( seedY * seedY ) );
84  WPosition seed( seedX, seedY, seedZ );
85 
86  // create the vertices
87  for( size_t vidx = 0; vidx < numVertsPerFiber; ++vidx )
88  {
89  double v = static_cast< double >( vidx ) / static_cast< double >( numVertsPerFiber - 1 );
90  double degree = v * 2.0 * pi() * numRotations;
91 
92  double X = seed.x() + cos( degree ) * v * spiralRadius;
93  double Y = seed.y() + sin( degree ) * v * spiralRadius;
94  double Z = seed.z() + ( v * height );
95 
96  vertices->push_back( originOffset.x() + X );
97  vertices->push_back( originOffset.y() + Y );
98  vertices->push_back( originOffset.z() + Z );
99 
100  colors->push_back( fibColor.x() );
101  colors->push_back( fibColor.y() );
102  colors->push_back( fibColor.z() );
103  fibIdxVertexMap->push_back( fidx );
104  }
105 
106  ++( *progress );
107  }
108 }
WPropDouble m_tubeRadius
The radius of a tube (consisting of multiple fibers.
virtual void operator()(int seed, WProgress::SPtr progress, const WColor &color, size_t numFibers, size_t numVertsPerFiber, const WPosition &origin, const WPosition &size, WDataSetFibers::VertexArray vertices, WDataSetFibers::IndexArray fibIdx, WDataSetFibers::LengthArray lengths, WDataSetFibers::IndexArray fibIdxVertexMap, WDataSetFibers::ColorArray colors)
Create the dataset.
WDataCreatorFiberSpiral()
Default constructor.
virtual ~WDataCreatorFiberSpiral()
Destructor.
WPropInt m_numRotations
Number of rotations to do.
Define the interface which is injected into an WObjectNDIP.
std::shared_ptr< std::vector< size_t > > IndexArray
Index list indexing fibers in VertexArray in terms of vertex numbers.
std::shared_ptr< std::vector< float > > ColorArray
Colors for each vertex in VertexArray.
std::shared_ptr< std::vector< size_t > > LengthArray
Lengths of fibers in terms of vertices.
std::shared_ptr< std::vector< float > > VertexArray
List of vertex coordinates in term of components of vertices.
This is a base class for everything which has a Name,Description,Icon and Properties (=NDIP).
Definition: WObjectNDIP.h:42
WProperties::SPtr m_properties
the properties of the object.
Definition: WObjectNDIP.h:99
This only is a 3d double vector.
std::shared_ptr< WProgress > SPtr
Shared pointer on a WProgress.
Definition: WProgress.h:48