OpenWalnut  1.5.0dev
WMReadLAS.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 <fstream>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 #include <liblas/liblas.hpp> // NOLINT: this is not a C system header as brainlint thinks
31 #include <liblas/point.hpp> // NOLINT: this is not a C system header as brainlint thinks
32 #include <liblas/reader.hpp> // NOLINT: this is not a C system header as brainlint thinks
33 
34 #include "WMReadLAS.h"
35 #include "core/common/WPathHelper.h"
36 #include "core/common/WStringUtils.h"
37 #include "core/kernel/WDataModuleInputFile.h"
38 #include "core/kernel/WDataModuleInputFilterFile.h"
39 #include "core/kernel/WKernel.h"
40 
41 // This line is needed by the module loader to actually find your module. You need to add this to your module too. Do NOT add a ";" here.
42 W_LOADABLE_MODULE( WMReadLAS )
43 
45  WDataModule(),
46  m_reload( false )
47 {
48  // Init
49 }
50 
52 {
53  // Cleanup!
54 }
55 
56 std::shared_ptr< WModule > WMReadLAS::factory() const
57 {
58  return std::shared_ptr< WModule >( new WMReadLAS() );
59 }
60 
61 const std::string WMReadLAS::getName() const
62 {
63  // Specify your module name here. This name must be UNIQUE!
64  return "Read LAS";
65 }
66 
67 const std::string WMReadLAS::getDescription() const
68 {
69  // Specify your module description here. Be detailed. This text is read by the user.
70  return "This module reads LAS files containing LiDAR point data.";
71 }
72 
74 {
75  m_output = WModuleOutputData < WDataSetPoints >::createAndAdd( shared_from_this(), "out", "The loaded dataset" );
76 
77  // call WModule's initialization
79 }
80 
82 {
84 }
85 
87 {
88  m_moduleState.setResetable( true, true );
89 
90  // Signal ready state. Now your module can be connected by the container, which owns the module.
91  ready();
92  waitRestored();
93 
94  // main loop
95  while( !m_shutdownFlag() )
96  {
98 
99  // woke up since the module is requested to finish
100  if( m_shutdownFlag() )
101  {
102  break;
103  }
104 
105  if( m_reload )
106  {
107  load();
108  }
109  }
110 }
111 
112 std::vector< WDataModuleInputFilter::ConstSPtr > WMReadLAS::getInputFilter() const
113 {
114  std::vector< WDataModuleInputFilter::ConstSPtr > filters;
115 
116  // NOTE: plain extension. No wildcards or prefixing "."!
117  filters.push_back( WDataModuleInputFilter::ConstSPtr( new WDataModuleInputFilterFile( "las", "LAS point cloud files" ) ) );
118 
119  return filters;
120 }
121 
123 {
124  // notify the module only
125  m_reload = true;
127 }
128 
130 {
131  m_reload = false;
132 
133  // open file
134  WDataModuleInputFile::SPtr inputFile = getInputAs< WDataModuleInputFile >();
135  if( !inputFile )
136  {
137  // No input? Reset output too.
138  m_output->updateData( WDataSetPoints::SPtr() );
139  return;
140  }
141  boost::filesystem::path p = inputFile->getFilename();
142 
143  std::ifstream ifs;
144  ifs.open( p.string().c_str(), std::ios::in | std::ios::binary );
145  if( !ifs || ifs.bad() )
146  {
147  errorLog() << "Could not open file \"" << p.string() << "\".";
148  return;
149  }
150 
151  liblas::ReaderFactory factory;
152  liblas::Reader reader = factory.CreateWithStream( ifs );
153 
154  size_t numPoints = reader.GetHeader().GetPointRecordsCount();
155 
156  infoLog() << "LAS Header: Point Count = " << numPoints;
157  infoLog() << "LAS Header: Compressed = " << reader.GetHeader().Compressed();
158  infoLog() << "LAS Header: File Signature = " << reader.GetHeader().GetFileSignature();
159 
160  std::shared_ptr< WProgress > progress1( new WProgress( "Loading" ) );
161  m_progress->addSubProgress( progress1 );
162 
163  // target memory
164  WDataSetPoints::VertexArray vertices( new WDataSetPoints::VertexArray::element_type() );
165  WDataSetPoints::ColorArray colors( new WDataSetPoints::ColorArray::element_type() );
166  WBoundingBox bb;
167 
168  infoLog() << "Start Loading ...";
169 
170  // interpret file
171  size_t realNumPoints = 0;
172  while( reader.ReadNextPoint() )
173  {
174  realNumPoints++;
175  liblas::Point const& coord = reader.GetPoint();
176 
177  vertices->push_back( coord.GetX() );
178  vertices->push_back( coord.GetY() );
179  vertices->push_back( coord.GetZ() );
180  colors->push_back( 1.0 );
181  colors->push_back( 1.0 );
182  colors->push_back( 1.0 );
183 
184  bb.expandBy( WVector3f( coord.GetX(), coord.GetY(), coord.GetZ() ) );
185  ++( *progress1 );
186  }
187 
188  infoLog() << "Loaded " << realNumPoints << " points from file. Done." << bb;
189 
190  // finally provide output data
191  std::shared_ptr< WDataSetPoints> newOutput( new WDataSetPoints( vertices, colors, bb ) );
192  m_output->updateData( newOutput );
193 
194  // done. close file and report finish
195  progress1->finish();
196  ifs.close();
197 }
void expandBy(const WBoundingBoxImpl< VT > &bb)
Expands this bounding box to include the given bounding box.
Definition: WBoundingBox.h:240
virtual void wait() const
Wait for the condition.
void setResetable(bool resetable=true, bool autoReset=true)
Sets the resetable flag.
virtual void notify()
Notifies all waiting threads.
std::shared_ptr< WDataModuleInputFile > SPtr
Convenience typedef for a std::shared_ptr< WDataModuleInputFile >.
Checks a given WDataModuleInput against a file type.
std::shared_ptr< const WDataModuleInputFilter > ConstSPtr
Convenience typedef for a std::shared_ptr< const WDataModuleInputFilter >.
Base for all data loader modules.
Definition: WDataModule.h:47
Dataset to store a bunch of points without order or topology.
std::shared_ptr< WDataSetPoints > SPtr
Pointer to dataset.
std::shared_ptr< std::vector< float > > ColorArray
Colors for each vertex in VertexArray.
std::shared_ptr< std::vector< float > > VertexArray
List of vertex coordinates in term of components of vertices.
This module loads LAS files (point data).
Definition: WMReadLAS.h:42
virtual void handleInputChange()
Handle a newly set input.
Definition: WMReadLAS.cpp:122
virtual std::shared_ptr< WModule > factory() const
Due to the prototype design pattern used to build modules, this method returns a new instance of this...
Definition: WMReadLAS.cpp:56
virtual void moduleMain()
Entry point after loading the module.
Definition: WMReadLAS.cpp:86
virtual void properties()
Initialize the properties for this module.
Definition: WMReadLAS.cpp:81
virtual const std::string getName() const
Gives back the name of this module.
Definition: WMReadLAS.cpp:61
virtual const std::string getDescription() const
Gives back a description of this module.
Definition: WMReadLAS.cpp:67
std::shared_ptr< WModuleOutputData< WDataSetPoints > > m_output
The output connector for the filtered data.
Definition: WMReadLAS.h:114
bool m_reload
True if the load function needs to be called.
Definition: WMReadLAS.h:119
WMReadLAS()
Default constructor.
Definition: WMReadLAS.cpp:44
virtual ~WMReadLAS()
Destructor.
Definition: WMReadLAS.cpp:51
virtual void connectors()
Initialize the connectors this module is using.
Definition: WMReadLAS.cpp:73
virtual void load()
Load data.
Definition: WMReadLAS.cpp:129
virtual std::vector< WDataModuleInputFilter::ConstSPtr > getInputFilter() const
Define a list of file filters we support.
Definition: WMReadLAS.cpp:112
A fixed size matrix class.
Definition: WMatrixFixed.h:150
Class offering an instantiate-able data connection between modules.
virtual void properties()
Initialize properties in this function.
Definition: WModule.cpp:212
void ready()
Call this whenever your module is ready and can react on property changes.
Definition: WModule.cpp:505
WConditionSet m_moduleState
The internal state of the module.
Definition: WModule.h:703
wlog::WStreamedLogger errorLog() const
Logger instance for comfortable error logging.
Definition: WModule.cpp:570
std::shared_ptr< WProgressCombiner > m_progress
Progress indicator used as parent for all progress' of this module.
Definition: WModule.h:652
wlog::WStreamedLogger infoLog() const
Logger instance for comfortable info logging.
Definition: WModule.cpp:565
virtual void connectors()
Initialize connectors in this function.
Definition: WModule.cpp:208
void waitRestored()
This method waits for the module to be restored completely.
Definition: WModule.cpp:625
Class managing progress inside of modules.
Definition: WProgress.h:42
WBoolFlag m_shutdownFlag
Condition getting fired whenever the thread should quit.