11/* *****************************************************************************
2- * Copyright (c) 2019, Simverge Software LLC. All rights reserved.
2+ * Copyright (c) 2019, Simverge Software LLC & Runette Software Ltd . All rights reserved.
33 *
44 * Redistribution and use in source and binary forms, with or without
55 * modification, are permitted provided that the following
2626 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2727 * POSSIBILITY OF SUCH DAMAGE.
2828 *****************************************************************************/
29-
29+ # define _CRT_SECURE_NO_WARNINGS
3030#include " pdalc_pipeline.h"
3131
3232#include < string>
3333
34- #include < pdal/PipelineExecutor.hpp>
35-
3634#include " pdalc_pointviewiterator.h"
3735
36+ #include < pdal/util/Utils.hpp>
37+ #include < pdal/PipelineWriter.hpp>
38+ #include < pdal/Stage.hpp>
39+
3840// TODO Address cause of std::min problems. See https://github.com/PDAL/CAPI/issues/4
3941#undef min
4042
@@ -46,46 +48,35 @@ extern "C"
4648{
4749 PDALPipelinePtr PDALCreatePipeline (const char * json)
4850 {
49- PDALPipelinePtr pipeline = nullptr ;
50-
51+ PDALPipelinePtr pipeline = new Pipeline ();
52+ Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
53+ PipelineManager *manager = ptr->manager .get ();
5154 if (json && std::strlen (json) > 0 )
5255 {
53- pdal::PipelineExecutor *executor = nullptr ;
54-
5556 try
5657 {
57- pdal::PipelineExecutor stackpipe (json);
58- executor = new pdal::PipelineExecutor (json);
58+ LogPtr log (Log::makeLog (" capi pipeline" , &ptr->logStream ));
59+ manager->setLog (log);
60+
61+ std::stringstream strm;
62+ strm << json;
63+ manager->readPipeline (strm);
5964 }
6065 catch (const std::exception &e)
6166 {
6267 printf (" Could not create pipeline: %s\n %s\n " , e.what (), json);
63- executor = nullptr ;
6468 }
6569
66- if (executor )
70+ if (manager )
6771 {
68- bool valid = false ;
69-
7072 try
7173 {
72- valid = executor-> validate ();
74+ manager-> prepare ();
7375 }
7476 catch (const std::exception &e)
7577 {
7678 printf (" Error while validating pipeline: %s\n %s\n " , e.what (), json);
7779 }
78-
79- if (valid)
80- {
81- pipeline = new Pipeline (executor);
82- }
83- else
84- {
85- delete executor;
86- executor = NULL ;
87- printf (" The pipeline is invalid:\n %s\n " , json);
88- }
8980 }
9081 }
9182
@@ -97,6 +88,7 @@ extern "C"
9788 if (pipeline)
9889 {
9990 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
91+ ptr->manager .reset ();
10092 delete ptr;
10193 }
10294 }
@@ -108,17 +100,18 @@ extern "C"
108100 if (pipeline && buffer && size > 0 )
109101 {
110102 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
111- pdal::PipelineExecutor *executor = ptr->get ();
103+ PipelineManager *manager = ptr->manager . get ();
112104 buffer[0 ] = ' \0 ' ;
113105 buffer[size - 1 ] = ' \0 ' ;
114106
115- if (executor )
107+ if (manager )
116108 {
117109 try
118110 {
119- std::string s = executor->getPipeline ();
120- std::strncpy (buffer, s.c_str (), size - 1 );
121- result = std::min (s.length (), size);
111+ std::stringstream strm;
112+ pdal::PipelineWriter::writePipeline (manager->getStage (), strm);
113+ std::strncpy (buffer, strm.str ().c_str (), size - 1 );
114+ result = std::min (strm.str ().length (), size);
122115 }
123116 catch (const std::exception &e)
124117 {
@@ -138,17 +131,19 @@ extern "C"
138131 if (pipeline && metadata && size > 0 )
139132 {
140133 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
141- pdal::PipelineExecutor *executor = ptr->get ();
134+ PipelineManager *manager = ptr->manager . get ();
142135 metadata[0 ] = ' \0 ' ;
143136 metadata[size - 1 ] = ' \0 ' ;
144137
145- if (executor )
138+ if (manager )
146139 {
147140 try
148141 {
149- std::string s = executor->getMetadata ();
150- std::strncpy (metadata, s.c_str (), size);
151- result = std::min (s.length (), size);
142+ std::stringstream strm;
143+ MetadataNode root = manager->getMetadata ().clone (" metadata" );
144+ pdal::Utils::toJSON (root, strm);
145+ std::strncpy (metadata, strm.str ().c_str (), size);
146+ result = std::min (strm.str ().length (), size);
152147 }
153148 catch (const std::exception &e)
154149 {
@@ -167,17 +162,19 @@ extern "C"
167162 if (pipeline && schema && size > 0 )
168163 {
169164 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
170- pdal::PipelineExecutor *executor = ptr->get ();
165+ PipelineManager *manager = ptr->manager . get ();
171166 schema[0 ] = ' \0 ' ;
172167 schema[size - 1 ] = ' \0 ' ;
173168
174- if (executor )
169+ if (manager )
175170 {
176171 try
177172 {
178- std::string s = executor->getSchema ();
179- std::strncpy (schema, s.c_str (), size);
180- result = std::min (s.length (), size);
173+ std::stringstream strm;
174+ MetadataNode root = manager->pointTable ().layout ()->toMetadata ().clone (" schema" );
175+ pdal::Utils::toJSON (root, strm);
176+ std::strncpy (schema, strm.str ().c_str (), size);
177+ result = std::min (strm.str ().length (), size);
181178 }
182179 catch (const std::exception &e)
183180 {
@@ -196,15 +193,15 @@ extern "C"
196193 if (pipeline && log && size > 0 )
197194 {
198195 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
199- pdal::PipelineExecutor *executor = ptr->get ();
196+ PipelineManager *manager = ptr->manager . get ();
200197 log[0 ] = ' \0 ' ;
201198 log[size - 1 ] = ' \0 ' ;
202199
203- if (executor )
200+ if (manager )
204201 {
205202 try
206203 {
207- std::string s = executor-> getLog ();
204+ std::string s = ptr-> logStream . str ();
208205 std::strncpy (log, s.c_str (), size);
209206 result = std::min (s.length (), size);
210207 }
@@ -221,36 +218,43 @@ extern "C"
221218 void PDALSetPipelineLogLevel (PDALPipelinePtr pipeline, int level)
222219 {
223220 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
221+ PipelineManager *manager = ptr->manager .get ();
224222
225- if (ptr && ptr->get ())
223+ try
224+ {
225+ if (level < 0 || level > 8 )
226+ throw pdal_error (" log level must be between 0 and 8!" );
227+
228+ ptr->logLevel = static_cast <pdal::LogLevel>(level);
229+ pdal::LogPtr lptr = manager->log ();
230+ lptr->setLevel (ptr->logLevel );
231+ }
232+ catch (const std::exception &e)
226233 {
227- try
228- {
229- ptr->get ()->setLogLevel (level);
230- }
231- catch (const std::exception &e)
232- {
233- printf (" Found error while setting log level: %s\n " , e.what ());
234- }
234+ printf (" Found error while setting log level: %s\n " , e.what ());
235235 }
236+
236237 }
237238
238239 int PDALGetPipelineLogLevel (PDALPipelinePtr pipeline)
239240 {
240241 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
241- return (ptr && ptr->get ()) ? ptr->get ()->getLogLevel () : 0 ;
242+ return (ptr && ptr->manager .get ())
243+ ? static_cast <int >(
244+ ptr->manager .get ()->log ()->getLevel ()
245+ ) : 0 ;
242246 }
243247
244248 int64_t PDALExecutePipeline (PDALPipelinePtr pipeline)
245249 {
246250 int64_t result = 0 ;
247251 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
248252
249- if (ptr && ptr-> get () )
253+ if (ptr)
250254 {
251255 try
252256 {
253- result = ptr->get ()->execute ();
257+ result = ptr->manager . get ()->execute ();
254258 }
255259 catch (const std::exception &e)
256260 {
@@ -266,11 +270,11 @@ extern "C"
266270 int64_t result = 0 ;
267271 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
268272
269- if (ptr && ptr-> get () )
273+ if (ptr)
270274 {
271275 try
272276 {
273- result = ptr->get ()->validate ();
277+ ptr->manager . get ()->prepare ();
274278 }
275279 catch (const std::exception &e)
276280 {
@@ -284,18 +288,14 @@ extern "C"
284288 PDALPointViewIteratorPtr PDALGetPointViews (PDALPipelinePtr pipeline)
285289 {
286290 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
291+ PipelineManager *manager = ptr->manager .get ();
287292 pdal::capi::PointViewIterator *views = nullptr ;
288293
289- if (ptr && ptr-> get () )
294+ if (ptr)
290295 {
291296 try
292297 {
293- auto &v = ptr->get ()->getManagerConst ().views ();
294-
295- if (!v.empty ())
296- {
297- views = new pdal::capi::PointViewIterator (v);
298- }
298+ views = new pdal::capi::PointViewIterator (manager->views ());
299299 }
300300 catch (const std::exception &e)
301301 {
@@ -305,6 +305,9 @@ extern "C"
305305
306306 return views;
307307 }
308- }
309- }
310- }
308+ } /* extern c */
309+
310+
311+
312+ } /* capi */
313+ } /* pdal */
0 commit comments