yaml-cpp read sequence in item - c++

How can I read this YAML file with yaml-cpp:
sensors:
- id: 5
hardwareId: 28-000005a32133
type: 1
- id: 6
hardwareId: 28-000005a32132
type: 4
I can't understand how can I get sensors item, to use it.
As I understand sensors is a YAML::Node. How can I get it?
Update 1:
YAML::Node config = YAML::LoadFile(config_path);
const YAML::Node& node_test1 = confg["sensors"];
for (std::size_t i = 0; i < node_test1.size(); i++) {
const YAML::Node& node_test2 = node_test1[i];
std::cout << "Id: " << node_test2["id"].as<std::string>() << std::endl;
std::cout << "hardwareId: " << node_test2["hardwareId"].as<std::string>() << std::endl << std::endl;
}
This code works, but it was writed using tutorial about old api.
I think this code could be rewrited with iterators, but I don't now how.

It looks like your code works, but if you want to rewrite it with iterators, you can:
YAML::Node config = YAML::LoadFile(config_path);
const YAML::Node& sensors = config["sensors"];
for (YAML::iterator it = sensors.begin(); it != sensors.end(); ++it) {
const YAML::Node& sensor = *it;
std::cout << "Id: " << sensor["id"].as<std::string>() << "\n";
std::cout << "hardwareId: " << sensor["hardwareId"].as<std::string>() << "\n\n";
}

Related

Not sure where the segmentation fault is

I'm getting problem with segmentation fault when trying to compile a C++ program, but not sure where the problem lies. I suspect that the problem lies with the .find() ..... could it be the iterator operator < and == which are the comparators for find() that is the issue? I hope that someone can point out to me where they think the problem lies.
The following is part of test01.cpp, where I run it to test the code and use print statements to find out where the problem is:
bool confirmEverythingMatches(const btree<long>& testContainer, const set<long>& stableContainer) {
cout << "Confirms the btree and the set "
"contain exactly the same values..." << endl;
for (long i = kMinInteger; i <= kMaxInteger; i++) {
cout << "Start of for-loop to find iterator for comparisons..." << endl;
if (stableContainer.find(i) != stableContainer.end()) {
cout << "can find i (" << i << ") in stableContainer!" << endl;
} else {
cout << "cannot find i (" << i << ") in stableContainer!" << endl;
}
cout << "In between finding i in stable and testContainers..." << endl;
if (testContainer.find(i) != testContainer.end()) {
cout << "can find i (" << i << ") in testContainer!" << endl;
} else {
cout << "cannot find i (" << i << ") in testContainer!" << endl;
}
cout << "Before assigning the find to boolean variables..." << endl;
bool foundInTree = (testContainer.find(i) != testContainer.end());
cout << "testContainer.find(i) != testContainer.end()" << endl;
bool foundInSet = (stableContainer.find(i) != stableContainer.end());
cout << "stableContainer.find(i) != stableContainer.end()" << endl;
if (foundInTree != foundInSet) {
cout << "- btree and set don't contain the same data!" << endl;
cout << "Mismatch at element: " << i << endl;
return false;
} else {cout << "foundInTree == foundInSet!!!" << i << endl;}
}
cout << "- btree checks out just fine." << endl;
return true;
}
} // namespace close
/**
* Codes for testing various bits and pieces. Most of the code is commented out
* you should uncomment it as appropriate.
**/
int main(void) {
// initialise random number generator with 'random' seed
initRandom();
cout << "after initRandom().." << endl;
// insert lots of random numbers and compare with a known correct container
btree<long> testContainer(99);
cout << "after specifying max node elements in testContainer.." << endl;
set<long> stableContainer;
cout << "after constructing stableContainer.." << endl;
insertRandomNumbers(testContainer, stableContainer, 1000000);
cout << "after inserting random numbers into testContainer and for success inserts, also into stableContainer.." << endl;
btree<long> btcpy = testContainer;
cout << "after copy assigning a copy of testContainer to btcopy.." << endl;
confirmEverythingMatches(btcpy, stableContainer);
cout << "after confirming everything internally matches between testContainer and stableContainer.." << endl;
return 0;
}
The output I get when running the program (No problem when compiling) is this:
Confirms the btree and the set contain exactly the same values...
Start of for-loop to find iterator for comparisons...
cannot find i (1000000) in stableContainer!
In between finding i in stable and testContainers...
ASAN:DEADLYSIGNAL
=================================================================
==7345==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000018 (pc 0x000108d132a8 bp 0x000000000000 sp 0x7fff56eee6f0 T0)
#0 0x108d132a7 in btree<long>::find(long const&) const (test01+0x1000022a7)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (test01+0x1000022a7) in btree<long>::find(long const&) const
==7345==ABORTING
Abort trap: 6
I also got this error when I tried to run it on another machine:
==29936==ERROR: AddressSanitizer failed to allocate 0x200000 (2097152) bytes of SizeClassAllocator32: 12
I found that when it goes into the find(), it will have the segmentation fault:
/**
* Identical in functionality to the non-const version of find,
* save the fact that what's pointed to by the returned iterator
* is deemed as const and immutable.
*
* #param elem the client element we are trying to match.
* #return an iterator to the matching element, or whatever the
* const end() returns if no such match was ever found.
*/
template<typename T> typename btree<T>::const_iterator
btree<T>::find(const T& elem) const {
std::cout << "CONST ITERATOR'S FIND" << std::endl;
Node *tmp_ = root_;
std::cout << "1" << std::endl;
while(true) {
std::cout << "2" << std::endl;
size_t i;
std::cout << "3" << std::endl;
// go through all elements from root to tail
for (i = 0; i < tmp_->__occupied_size_; ++i) {
std::cout << "4" << std::endl;
if (tmp_->__elem_[i] == elem) {
std::cout << "5" << std::endl;
// find the elem, return an iterator
return const_iterator(tmp_, i, this);
std::cout << "6" << std::endl;
} else if (tmp_->__elem_[i] > elem) {
std::cout << "7" << std::endl;
// elem is not in current Node, go to descendants
// for the elem.
if (tmp_->__descendants_ == nullptr) {
std::cout << "8" << std::endl;
return cend();
std::cout << "9" << std::endl;
} else {
std::cout << "10" << std::endl;
tmp_ = tmp_->__descendants_[i];
std::cout << "11" << std::endl;
break;
}
}
}
// handling boundaries cases
if (i == tmp_->__occupied_size_) {
std::cout << "12" << std::endl;
if (tmp_->__descendants_[i] == nullptr) {
std::cout << "13" << std::endl;
return cend();
std::cout << "14" << std::endl;
} else {
std::cout << "15" << std::endl;
tmp_ = tmp_->__descendants_[i];
}
}
}
}
The print statements for this find is:
CONST ITERATOR'S FIND
1
2
3
4
4
7
10
11
2
3
4
7
10
11
ASAN:DEADLYSIGNAL
Ok, so based on the implementation of this find function, I think the problem might be located in
if (tmp_->__descendants_ == nullptr) {
std::cout << "8" << std::endl;
return cend();
std::cout << "9" << std::endl;
} else {
std::cout << "10" << std::endl;
tmp_ = tmp_->__descendants_[i];
std::cout << "11" << std::endl;
break;
}
and then
// handling boundaries cases
if (i == tmp_->__occupied_size_) {
std::cout << "12" << std::endl;
if (tmp_->__descendants_[i] == nullptr) {
std::cout << "13" << std::endl;
return cend();
std::cout << "14" << std::endl;
} else {
std::cout << "15" << std::endl;
tmp_ = tmp_->__descendants_[i];
}
}
So, You are checking if tmp->__descendants_ is not null. If it is not, then you set tmp_ = tmp_->descendants_[i];
Note: you are just checking __descendants_ pointer to be null or not, you are not checking the __descendants_ [i] if it is null!
What if the tmp->__descendants_[i] is null (or gets out of the descendants array)?
If that value is null, then tmp_->occupied_size_ might give you segfault.
Note2: For some reason you are using same index "i" for iterating through __elem_ and __descendants_. I am not sure, how descendants are created, but it might be also a problem here.
This is why debuggers exist. Run your program in the debugger, let the program fail, and then the debugger will tell you where and why it's gone wrong.
It looks like you've got potentially a lot of code to trawl through, which no one here will really want to do as it's not a concise question.
Good luck!

Using Hbase Thrift2 API in C++

I've been using the Thrift API for HBase in C++ (example usage here), but now I have a need to use Thrift2 instead. I know there are examples for using Thrift2 in other languages, such as C#, Python, and Java, but I can't find documentation for C++.
Here's my current code for calling the Thrift API:
transport->open();
std::string t("demo_table");
/ Scan all tables, look for the demo table and delete it. /
std::cout << "scanning tables..." << std::endl;
StrVec tables;
client.getTableNames(tables);
for (StrVec::const_iterator it = tables.begin(); it != tables.end(); ++it) {
std::cout << " found: " << *it << std::endl;
if (t == *it) {
if (client.isTableEnabled(*it)) {
std::cout << " disabling table: " << *it << std::endl;
client.disableTable(*it);
}
std::cout << " deleting table: " << *it << std::endl;
client.deleteTable(*it);
}
}
Now this fails after switching to Thrift2. For example, client.getTableNames() no longer works (the function doesn't exist).
Some functions are removed from hbase thrift2 interface. If you just want to get list of table names in hbase, you can use get children of /hbase/table in zookeeper.
struct String_vector paths;
int ret = zoo_get_children(zkhandle,"/hbase/table", 0,&paths);//
if(ret)
{
std::cout << "zoo_get_children error " << ret << std::endl;
} else {
for(i = 0;i < paths.count;i++)
printf("/hbase/table/%s\n",paths.data[i]);
free_vector(&paths);
}

Bloomberg Equity Option Chain through API

I am currently working on a project that should help me create an implied volatility surface for a given stock. For this purpose, I am writing a script that will download all the available options for this specific stock - from what I've gathered, this is possible by sending a request through the Bloomberg API using bulks fields/overrides. Here is my current code:
d_host = "localhost";
d_port = 8194;
SessionOptions sessionOptions;
sessionOptions.setServerHost(d_host.c_str());
sessionOptions.setServerPort(d_port);
Session session(sessionOptions);
Service refDataService = session.getService("//blp/refdata");
Request request = refDataService.createRequest("ReferenceDataRequest");
request.append("securities", "MSFT US EQUITY");
request.append("fields", "CHAIN_TICKERS");
// add overrides
Element overrides = request.getElement("overrides");
Element override1 = overrides.appendElement();
override1.setElement("fieldId", "CHAIN_PUT_CALL_TYPE_OVRD");
override1.setElement("value", "C");
Element override2 = overrides.appendElement();
override2.setElement("fieldId", "CHAIN_POINTS_OVRD");
override2.setElement("value", 100);
Element override3 = overrides.appendElement();
override3.setElement("fieldId", "CHAIN_EXP_DT_OVRD");
override3.setElement("value", "20250203");
std::cout << "Sending Request: " << request << std::endl;
CorrelationId cid(this);
session.sendRequest(request, cid);
(followed by event handling)
Now I have several issues/questions:
The code compiles without problems, but when running it on the Bloomberg terminal,the following error is printed:
How would I go about fixing this problem? I assume I made a mistake somewhere in the override fields..
How would I need to adjust my code to download all options available given a specific maturity, i.e. I want to get a list of all the options until today + 15 years.
How would I then download the implied volatility for each option? Would I need to store the tickers in an array and then send a request for the field "IVOL_MID" for each option or is there some kind of way to obtain all the volatilities at once?
Edit: Here is the code of my event handler, since that seems to be the problem.
session.sendRequest(request, cid);
while (true)
{
Event event = session.nextEvent();
MessageIterator msgIter(event);
while (msgIter.next()) {
Message msg = msgIter.message();
if (msg.correlationId() == cid) {
processMessage(msg);
}
}
if (event.eventType() == Event::RESPONSE) {
break;
}
}
void processMessage(Message &msg)
{
Element securityDataArray = msg.getElement(SECURITY_DATA);
int numSecurities = securityDataArray.numValues();
for (int i = 0; i < numSecurities; ++i) {
Element securityData = securityDataArray.getValueAsElement(i);
std::cout << securityData.getElementAsString(SECURITY)
<< std::endl;
const Element fieldData = securityData.getElement(FIELD_DATA);
for (size_t j = 0; j < fieldData.numElements(); ++j) {
Element field = fieldData.getElement(j);
if (!field.isValid()) {
std::cout << field.name() << " is NULL." << std::endl;
}
else {
std::cout << field.name() << " = "
<< field.getValueAsString() << std::endl;
}
}
Element fieldExceptionArray =
securityData.getElement(FIELD_EXCEPTIONS);
for (size_t k = 0; k < fieldExceptionArray.numValues(); ++k) {
Element fieldException =
fieldExceptionArray.getValueAsElement(k);
std::cout <<
fieldException.getElement(ERROR_INFO).getElementAsString(
"category")
<< ": " << fieldException.getElementAsString(FIELD_ID);
}
std::cout << std::endl;
}
The problem is in the event handling code that you are not showing. You are probably parsing it incorrectly.
Running your query I get the following result:
MSFT US 01/20/17 C23
MSFT US 01/20/17 C25
MSFT US 01/20/17 C30
MSFT US 01/20/17 C33
MSFT US 01/20/17 C35
MSFT US 01/20/17 C38
MSFT US 01/20/17 C40
MSFT US 01/20/17 C43
MSFT US 01/20/17 C45
MSFT US 01/20/17 C47
MSFT US 01/20/17 C50
MSFT US 01/20/17 C52.5
MSFT US 01/20/17 C55
MSFT US 01/20/17 C57.5
MSFT US 01/20/17 C60
MSFT US 01/20/17 C65
MSFT US 01/20/17 C70
Note: I'm using the Java API but it is essentially the same.
UPDATE:
your code does not parse the field data array element properly: the returned data contains an array of sequences so you need to parse it in two steps. Instead of field.getValueAsString(), you should have a code that looks like this (it's in Java and not tested):
//...
for (int i = 0; i < field.numValues(); i++) {
Element sequence = field.getValueAsElement(i);
ElementIterator it = sequence.elementIterator();
while (it.hasNext()) {
Element e = it.next();
System.out.println(e.getValueAsString());
}
If that does not work I suggest you debug your code step by step and inspect the type of data you receive and handle it accordingly.
For more details you should read the Developer's guide, in particular A.2.3.
As seen in the other answer, the problem lies in the event handling so I've rewritten that part using some examples from the Bloomberg API emulator.
session.sendRequest(request, cid);
bool continueToLoop = true;
while (continueToLoop)
{
Event evt = session.nextEvent();
switch (evt.eventType())
{
case Event::RESPONSE:
continueToLoop = false; //fall through
case Event::PARTIAL_RESPONSE:
ProcessReferenceDataEvent(evt);
break;
}
}
void ProcessReferenceDataEvent(Event evt)
{
const string level1 = "";
const string level2 = "\t";
const string level3 = "\t\t";
const string level4 = "\t\t\t";
std::cout << endl << endl;
std::cout << level1 << "EventType = " << evt.eventType();
MessageIterator iter(evt);
while (iter.next())
{
Message msg = iter.message();
std::cout << endl << endl;
std::cout << level1 << "correlationID = " << msg.correlationId().asInteger() << endl;
std::cout << level1 << "messageType = " << msg.messageType().string() << endl;
std::cout << endl << endl;
Element SecurityDataArray = msg.getElement(SECURITY_DATA);
int numSecurities = SecurityDataArray.numValues();
for (int valueIndex = 0; valueIndex < numSecurities; valueIndex++)
{
Element SecurityData = SecurityDataArray.getValueAsElement(valueIndex);
string Security = SecurityData.getElementAsString(SECURITY);
std::cout << level2 << Security << endl;
bool hasFieldErrors = SecurityData.hasElement("fieldExceptions", true);
if (hasFieldErrors)
{
Element FieldErrors = SecurityData.getElement(FIELD_EXCEPTIONS);
for (size_t errorIndex = 0; errorIndex < FieldErrors.numValues(); errorIndex++)
{
Element fieldError = FieldErrors.getValueAsElement(errorIndex);
string fieldId = fieldError.getElementAsString(FIELD_ID);
Element errorInfo = fieldError.getElement(ERROR_INFO);
string source = errorInfo.getElementAsString("source");
int code = errorInfo.getElementAsInt32("code");
string category = errorInfo.getElementAsString("category");
string strMessage = errorInfo.getElementAsString("message");
string subCategory = errorInfo.getElementAsString("subcategory");
cerr << level3 << "field error:" << endl;
cerr << level4 << "fieldId = " << fieldId << endl;
cerr << level4 << "source = " << source << endl;
cerr << level4 << "code = " << code << endl;
cerr << level4 << "category = " << category << endl;
cerr << level4 << "errorMessage = " << strMessage << endl;
cerr << level4 << "subCategory = " << subCategory << endl;
}
}
bool isSecurityError = SecurityData.hasElement("securityError", true);
if (isSecurityError)
{
Element secError = SecurityData.getElement("securityError");
string source = secError.getElementAsString("source");
int code = secError.getElementAsInt32("code");
string category = secError.getElementAsString("category");
string errorMessage = secError.getElementAsString("message");
string subCategory = secError.getElementAsString("subcategory");
cerr << level3 << "security error:" << endl;
cerr << level4 << "source = " << source << endl;
cerr << level4 << "code = " << code << endl;
cerr << level4 << "category = " << category << endl;
cerr << level4 << "errorMessage = " << errorMessage << endl;
cerr << level4 << "subCategory = " << subCategory << endl;
}
else
{
Element FieldData = SecurityData.getElement(FIELD_DATA);
double pxLast = FieldData.getElementAsFloat64("PX_LAST");
double bid = FieldData.getElementAsFloat64("BID");
double ask = FieldData.getElementAsFloat64("ASK");
string ticker = FieldData.getElementAsString("TICKER");
std::cout << level3 << "fields: " << endl;
std::cout << level4 << "PX_LAST = " << pxLast << endl;
std::cout << level4 << "BID = " << bid << endl;
std::cout << level4 << "ASK = " << ask << endl;
std::cout << level4 << "TICKER = " << ticker << endl;
bool excludeNullElements = true;
if (FieldData.hasElement("CHAIN_TICKERS", excludeNullElements))
{
Element chainTickers = FieldData.getElement("CHAIN_TICKERS");
for (size_t chainTickerValueIndex = 0; chainTickerValueIndex < chainTickers.numValues(); chainTickerValueIndex++)
{
Element chainTicker = chainTickers.getValueAsElement(chainTickerValueIndex);
string strChainTicker = chainTicker.getElementAsString("Ticker");
std::cout << level4 << "CHAIN_TICKER = " << strChainTicker << endl;
}
}
else
{
std::cout << level4 << "NO CHAIN_TICKER information" << endl;
}
}
}
}
}
Regarding the second question, the Bloomberg support staff recommended me to just pick an arbitarily high number so that all options would be downloaded, i.e.
override2.setElement("fieldId", "CHAIN_POINTS_OVRD");
override2.setElement("value", 50000);
For the third question, it is possible to download the chain tickers for all maturities by setting the "CHAIN_EXP_DT_OVRD" override to 'ALL' (this part is currently untested):
Element override3 = overrides.appendElement();
override3.setElement("fieldId", "CHAIN_EXP_DT_OVRD");
override3.setElement("value", 'ALL');

failed to get values of XML elements in pugixml using node.value()

I have below simple XML template in my C++ source code. Within below code block I need to get values for <scannerID> and <subscannerID>. both elements are children of pugixml document root.
xml_document doc;
xml_parse_result r;
std::string sXml = "<inArgs><scannerID>1</scannerID><subScannerID>2</subScannerID></inArgs>";
r = doc.load_buffer(sXml.c_str(), sXml.length());
if (!r) {
return false;
}
xml_node root = doc.child("inArgs");
if (!root) {
return false;
}
std::cout << "root = " << root.name() << std::endl;
xml_node scanner_node = root.child("scannerID");
if (scanner_node) {
std::cout << "scannerID = " << scanner_node.name() << std::endl;
std::cout << "scannerID = " << scanner_node.value() << std::endl;
}
xml_node sub_scanner_node = root.child("subscannerID");
if (scanner_node) {
std::cout << "sub_scanner_node = " << sub_scanner_node.name() << std::endl;
std::cout << "sub_scanner_node = " << sub_scanner_node.value() << std::endl;
}
this code portion giving an output like below. I can get the node's names correctly but failed to retrieve the values.
Out put: values are empty strings.
root = inArgs
scannerID = scannerID
scannerID =
subscannerID = subscannerID
subscannerID =
Edited to add modification for the approach in the answer
node = root.child("scannerID");
if (!node) {
return false;
}
std::cout << "nodeName = %s" << node.name() << std::endl;
std::cout << "text value: " << node.child_value() << std::endl;
but still the output is the same. I saw something different while reading the documents in
The data is in the pcdata child of your element_nodes.
Try scanner_node.child_value()
see the Getting node data section for further examples and explanation.
see node_element
see node_pcdata

saving CGAL alpha shape surface mesh

I have never used CGAL and have got almost no C/C++ experience. But following
Google I have however managed to compile the example "Alpha_shapes_3"
(\CGAL-4.1-beta1\examples\Alpha_shapes_3) on a Windows 7 64bit machine using
visual studio 2010.
Now if we check the source code for the program "ex_alpha_shapes_3" we
notice that a data file called "bunny_1000" is red where the 3d point
cluster resides.
Now my question is how can I change the source code so that after the alpha
shape is computed for the given points, surface mesh of the alpha shape is
saved/wrote in an external file. It can be simply the list of polygons and
their respective 3D vertices. I guess these polygons will be defining the
surface mesh of the alpha shape. If I can do that I can see the output of
the alpha shape generation program in an external tool I am familiar with.
I know this is very straightforward but I could not figure this out with my
limited knowledge of CGAL.
I know you gueys have the code but I am pasting it again for completion.
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_3.h>
#include <CGAL/Alpha_shape_3.h>
#include <fstream>
#include <list>
#include <cassert>
typedef CGAL::Exact_predicates_inexact_constructions_kernel Gt;
typedef CGAL::Alpha_shape_vertex_base_3<Gt> Vb;
typedef CGAL::Alpha_shape_cell_base_3<Gt> Fb;
typedef CGAL::Triangulation_data_structure_3<Vb,Fb> Tds;
typedef CGAL::Delaunay_triangulation_3<Gt,Tds> Triangulation_3;
typedef CGAL::Alpha_shape_3<Triangulation_3> Alpha_shape_3;
typedef Gt::Point_3 Point;
typedef Alpha_shape_3::Alpha_iterator Alpha_iterator;
int main()
{
std::list<Point> lp;
//read input
std::ifstream is("./data/bunny_1000");
int n;
is >> n;
std::cout << "Reading " << n << " points " << std::endl;
Point p;
for( ; n>0 ; n--) {
is >> p;
lp.push_back(p);
}
// compute alpha shape
Alpha_shape_3 as(lp.begin(),lp.end());
std::cout << "Alpha shape computed in REGULARIZED mode by default"
<< std::endl;
// find optimal alpha value
Alpha_iterator opt = as.find_optimal_alpha(1);
std::cout << "Optimal alpha value to get one connected component is "
<< *opt << std::endl;
as.set_alpha(*opt);
assert(as.number_of_solid_components() == 1);
return 0;
}
After searching a lot in the internet I found that probably we need to use something like
std::list<Facet> facets;
alpha_shape.get_alpha_shape_facets
(
std::back_inserter(facets),Alpha_shape::REGULAR
);
But I am still completely clueless how to use this in the above code!
As documented here, a facet is a pair (Cell_handle c,int i) defined as the facet in c opposite to the vertex of index i.
On this page, you have the description of how the vertex indices of a cell are.
In the following code sample, I added a small output that prints an OFF file on cout by duplicating the vertices. To do something clean, you can either use a std::map<Alpha_shape_3::Vertex_handle,int> to associate a unique index per vertex or add an info to the vertices like in those examples.
/// collect all regular facets
std::vector<Alpha_shape_3::Facet> facets;
as.get_alpha_shape_facets(std::back_inserter(facets), Alpha_shape_3::REGULAR);
std::stringstream pts;
std::stringstream ind;
std::size_t nbf=facets.size();
for (std::size_t i=0;i<nbf;++i)
{
//To have a consistent orientation of the facet, always consider an exterior cell
if ( as.classify( facets[i].first )!=Alpha_shape_3::EXTERIOR )
facets[i]=as.mirror_facet( facets[i] );
CGAL_assertion( as.classify( facets[i].first )==Alpha_shape_3::EXTERIOR );
int indices[3]={
(facets[i].second+1)%4,
(facets[i].second+2)%4,
(facets[i].second+3)%4,
};
/// according to the encoding of vertex indices, this is needed to get
/// a consistent orienation
if ( facets[i].second%2==0 ) std::swap(indices[0], indices[1]);
pts <<
facets[i].first->vertex(indices[0])->point() << "\n" <<
facets[i].first->vertex(indices[1])->point() << "\n" <<
facets[i].first->vertex(indices[2])->point() << "\n";
ind << "3 " << 3*i << " " << 3*i+1 << " " << 3*i+2 << "\n";
}
std::cout << "OFF "<< 3*nbf << " " << nbf << " 0\n";
std::cout << pts.str();
std::cout << ind.str();
Here is my code, which outputs vtk file for visualization in Paraview. Comparing with slorior's solutions, no duplicated points are saved in the file. But my code is just for the visualization, if you need to figure out the exterior or interior simplexes, you should modify the code to get these results.
void writevtk(Alpha_shape_3 &as, const std::string &asfile) {
// http://cgal-discuss.949826.n4.nabble.com/Help-with-filtration-and-filtration-with-alpha-values-td4659524.html#a4659549
std::cout << "Information of the Alpha_Complex:\n";
std::vector<Alpha_shape_3::Cell_handle> cells;
std::vector<Alpha_shape_3::Facet> facets;
std::vector<Alpha_shape_3::Edge> edges;
// tetrahedron = cell, they should be the interior, it is inside the 3D space
as.get_alpha_shape_cells(std::back_inserter(cells), Alpha_shape_3::INTERIOR);
// triangles
// for the visualiization, don't need regular because tetrahedron will show it
//as.get_alpha_shape_facets(std::back_inserter(facets), Alpha_shape_3::REGULAR);
as.get_alpha_shape_facets(std::back_inserter(facets), Alpha_shape_3::SINGULAR);
// edges
as.get_alpha_shape_edges(std::back_inserter(edges), Alpha_shape_3::SINGULAR);
std::cout << "The alpha-complex has : " << std::endl;
std::cout << cells.size() << " cells as tetrahedrons" << std::endl;
std::cout << facets.size() << " triangles" << std::endl;
std::cout << edges.size() << " edges" << std::endl;
size_t tetra_num, tri_num, edge_num;
tetra_num = cells.size();
tri_num = facets.size();
edge_num = edges.size();
// vertices: points <-> id
std::map<Point, size_t> points;
size_t index = 0;
// finite_.. is from DT class
for (auto v_it = as.finite_vertices_begin(); v_it != as.finite_vertices_end(); v_it++) {
points[v_it->point()] = index;
index++;
}
// write
std::ofstream of(asfile);
of << "# vtk DataFile Version 2.0\n\nASCII\nDATASET UNSTRUCTURED_GRID\n\n";
of << "POINTS " << index << " float\n";
for (auto v_it = as.finite_vertices_begin(); v_it != as.finite_vertices_end(); v_it++) {
of << v_it->point() << std::endl;
}
of << std::endl;
of << "CELLS " << tetra_num + tri_num + edge_num << " " << 5 * tetra_num + 4 * tri_num + 3 * edge_num << std::endl;
for (auto cell:cells) {
size_t v0 = points.find(cell->vertex(0)->point())->second;
size_t v1 = points.find(cell->vertex(1)->point())->second;
size_t v2 = points.find(cell->vertex(2)->point())->second;
size_t v3 = points.find(cell->vertex(3)->point())->second;
of << "4 " << v0 << " " << v1 << " " << v2 << " " << v3 << std::endl;
}
// https://doc.cgal.org/latest/TDS_3/classTriangulationDataStructure__3.html#ad6a20b45e66dfb690bfcdb8438e9fcae
for (auto tri_it = facets.begin(); tri_it != facets.end(); ++tri_it) {
of << "3 ";
auto tmp_tetra = tri_it->first;
for (int i = 0; i < 4; i++) {
if (i != tri_it->second) {
of << points.find(tmp_tetra->vertex(i)->point())->second << " ";
}
}
of << std::endl;
}
// https://doc.cgal.org/latest/TDS_3/classTriangulationDataStructure__3.html#af31db7673a6d7d28c0bb90a3115ac695
for (auto e : edges) {
of << "2 ";
auto tmp_tetra = e.get<0>();
int p1, p2;
p1 = e.get<1>();
p2 = e.get<2>();
of << points.find(tmp_tetra->vertex(p1)->point())->second << " "
<< points.find(tmp_tetra->vertex(p2)->point())->second << std::endl;
}
of << std::endl;
of << "CELL_TYPES " << tetra_num + tri_num + edge_num << std::endl;
for (int i = 0; i < tetra_num; i++) {
of << "10 ";
}
for (int i = 0; i < tri_num; i++) {
of << "5 ";
}
for (int i = 0; i < edge_num; i++) {
of << "3 ";
}
of << std::endl;
of.close();
}