similar functions like sp.linalg.norm, sp.cross in C++ - python

In python we have library scipy containing sp.linalg.norm, sp.cross. Are there any similar functions in C++ boost library?

There doesn't seem to be.
However, OpenCV has what you need!
L2 norm: http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#norm
Cross: http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#Point_
#include <opencv2/opencv.hpp>
int main(int argc, const char *argv[])
{
cv::Point3d p1(1, 0, 0);
cv::Point3d p2(0, 1, 0);
cv::Point3d cross_prod = p1.cross(p2);
std::cout
<< "<" << cross_prod.x
<< ", " << cross_prod.y
<< ", " << cross_prod.z
<< ">"
<< std::endl;
std::cout << "L2 norm: " << cv::norm(cross_prod) << std::endl;
return 0;
}
The output:
<0, 0, 1>
L2 norm: 1

Related

Convert big-endian bytes array to int like struct.unpack in python

Recently i'm tring to convert a python serial data analyzer into c++,but i face a problem that how can i convert feff to an integer -2 in c++,and this is a part of my python code.
def Control_Message(input_):
print(input_[4:8])
print(bytes.fromhex(input_[4:8]))
print(struct.unpack('h', bytes.fromhex(input_[4:8])))
print(struct.unpack('h', bytes.fromhex(input_[4:8]))[0])
Angular_Rate_X = struct.unpack('h', bytes.fromhex(input_[0:4]))[0] * 600 * 2 ** -20
and the result is:
feff
b'\xfe\xff'
(-2,)
-2
and now i'm confusing that how can i do the same thing in c++,hope for your help,thanks!
#include <cstdint>
#include <iostream>
int main() {
uint16_t value = 0xfeff;
uint8_t firstByte = static_cast<uint8_t>((value & 0xFF00) >> 8);
uint8_t secondByte = static_cast<uint8_t>(value & 0x00FF);
std::cout << std::hex << static_cast<int>(firstByte) << "\n";
std::cout << std::hex << static_cast<int>(secondByte) << "\n";
}
Output:
fe
ff
If your goal is to get fe and ff separately and then see them as integers, do as follows.
#include <cstdint>
#include <iostream>
int main() {
uint16_t value = 0xfeff;
uint8_t firstByte = static_cast<uint8_t>((value & 0xFF00) >> 8);
uint8_t secondByte = static_cast<uint8_t>(value & 0x00FF);
std::cout << static_cast<int>(firstByte) << "\n";
std::cout << static_cast<int>(secondByte) << "\n";
}
Output:
254
255
If your goal is to find the integer value for feff then do the following
#include <cstdint>
#include <iostream>
int main() {
uint16_t value = 0xfeff;
std::cout << static_cast<int>(value) << "\n";
}
Output:
65279

specified in either feed_devices or fetch_devices was not found in the Graph tensorflow

I use tensorflow C++ API.I train model on GPU and execute this code(for predict)
#include<iostream>
using namespace tensorflow;
tensorflow::Tensor loadImage(tensorflow::string fname){
tensorflow::int32 width = 224;
tensorflow::int32 height = 224;
tensorflow::int32 nData = 1;
tensorflow::int32 nVec = width*height;
tensorflow::int32 channels = 3;
auto tensor = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, height, width, channels}));
auto mat = tensor.tensor<float, 4>();
std::ifstream fin(fname, std::ios_base::in | std::ios_base::binary);
assert(!fin.fail());
boost::iostreams::filtering_istream s;
s.push(fin);
char c;
for(int i=0;i<nData;i++){
for(int h=0;h<height;h++){
for(int w=0;w<width;w++){
for(int j=0;j<channels;j++){
s.get(c);
mat(i, h, w, j) = static_cast<float>(static_cast<uint8_t>(c)) / 255.0;
}
}
}
}std::cout << "Image Loaded" << std::endl;
return tensor;
}
int main(int argc, char* argv[]) {
Session* session;
Status status = NewSession(SessionOptions(), &session);
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
GraphDef graph_def;
status = ReadBinaryProto(Env::Default(), "graph.pb", &graph_def);
if (!status.ok()) {
std::cout << "Status Not OK" << std::endl;
std::cout << status.ToString() << "\n";
return 1;
}
else{
std::cout << "Graph Loaded" << std::endl;
}
status = session->Create(graph_def);
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
else{
std::cout << "Create End" << std::endl;
}
std::string fname = "test.jpg";
tensorflow::Tensor img = loadImage(fname);
std::vector<std::pair<tensorflow::string, tensorflow::Tensor>> inputs = {{"img0001", img }};
std::vector<tensorflow::Tensor> outputs;
std::cout << "Start Run" << std::endl;
status = session->Run(inputs, {"output_node0"}, {}, &outputs);
std::cout << "End Run" << std::endl;
if (!status.ok()) {
std::cout << status.ToString() << "\n";
return 1;
}
std::cout << outputs[0].DebugString() << "\n";
std::cout << output_c() << "\n"; // 30
session->Close();
return 0;
}
But, I got unknown error like this.
Invalid argument: Tensor img0001:0, specified in either feed_devices or fetch_devices was not found in the Graph
This error occured this code.
session->Run(inputs, {"output_node0"}, {}, &outputs);
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/common_runtime/graph_execution_state.cc
At this site explain this error occurs when node name is not equal.
I create model by keras, not tensorflow.
So, I translate model from keras to tensorflow by this code.
https://github.com/icchi-h/keras_to_tensorflow/blob/master/keras_to_tensorflow.py
I guess it has a connection with training on GPU.
https://github.com/tensorflow/tensorflow/issues/5902
But, I can't get corroboration about this.
Please teach me the solution of this problem.

SWIG + CMAKE: init function missing

I was provided with a CMAKE-File for a c++ Code and want to wrap its function into python. Whenever I try to import the function it's giving me an Import Error:
dynamic module does not define init function (init_main)
This is the c++ code:
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <Memory/Interface.hpp>
#include <Memory/Subscription.hpp>
using namespace memory::interface;
using namespace memory;
MemoryPtr mMem;
Subscription *s;
std::string write_document;
void subscriber_callback(const Event &event) {
std::cout << "Received an event:" << std::endl;
switch (event.getType()) {
case Event::INSERT:
std::cout << "Insert event with document ID = " << event.getID() << std::endl;
break;
case Event::REMOVE:
std::cout << "Remove event with document ID = " << event.getID() << std::endl;
break;
case Event::QUERY:
std::cout << "Query event with document ID = " << event.getID() << std::endl;
break;
case Event::REPLACE:
std::cout << "Replace event with document ID = " << event.getID() << std::endl;
break;
case Event::ALL:
std::cout << "Generic event with document ID = " << event.getID() << std::endl;
break;
default: std::cout << "Unknown event type." << std::endl;
}
std::cout << "Contained document is: " << event.getDocument() << std::endl;
}
extern "C" int main(int argc, char *argv[]) {
// validate app arguments
if(argc < 3) {
std::cerr << "Usage : " << argv[0] << " <xcf:ShortTerm> <XPATH-trigger>" << std::endl;
return 1;
}
try {
// instantiate the memory interface
mMem = MemoryInterface::getInstance(argv[1]);
std::cout << "Memory interface initialized" << std::endl;
// create the trigger
std::string xpath(argv[2]);
// create a subscriber to a specific xpath event
s = new Subscription(
Condition(Event::INSERT, xpath),
TriggeredAction(boost::bind(&subscriber_callback, _1))
);
mMem->subscribe (*s);
std::cout << "Memory interface subscriber initialized" << std::endl;
// insert command/text to memory
write_document = "<command><stop /></command>";
mMem->insert(write_document);
std::cout << "Written to Memory interface" << std::endl;
// wait for key press
std::string end;
std::cin >> end;
}
catch (const MemoryInterfaceException& e) {
std::cerr << "MemoryInterfaceException: " << e.what() << std::endl;
return 1;
}
catch(std::exception &e) {
std::cerr << std::endl << "Could not initialize memory::interface. Reason:"
<< std::endl << e.what() << std::endl;
return 1;
}
return 0;
}
This is the CMAKE File to which I added the last SWIG part:
cmake_minimum_required(VERSION 3.0.2)
project(xcf_minimal_readwrite)
find_package(PkgConfig)
IF(PKG_CONFIG_FOUND)
message(STATUS "found pkgconfig")
SET(MODULE "Memory")
IF(Memory_FIND_REQUIRED)
SET(Memory_REQUIRED "REQUIRED")
ENDIF()
PKG_CHECK_MODULES(Memory ${Memory_REQUIRED} ${MODULE})
FIND_LIBRARY(Memory_LIBRARY
NAMES ${Memory_LIBRARIES}
HINTS ${Memory_LIBRARY_DIRS}
)
SET(Memory_LIBRARIES ${Memory_LIBRARY})
SET(MODULE "xmltio")
IF(xmltio_FIND_REQUIRED)
SET(xmltio_REQUIRED "REQUIRED")
ENDIF()
PKG_CHECK_MODULES(xmltio ${xmltio_REQUIRED} ${MODULE})
FIND_LIBRARY(xmltio_LIBRARY
NAMES ${xmltio_LIBRARIES}
HINTS ${xmltio_LIBRARY_DIRS}
)
SET(xmltio_LIBRARIES ${xmltio_LIBRARY})
ENDIF()
IF(Memory_FOUND)
message(STATUS "found Memory")
include_directories(${Memory_INCLUDE_DIRS} ${xmltio_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} main.cc)
target_link_libraries(${PROJECT_NAME} ${Memory_LIBRARIES} ${xmltio_LIBRARIES})
install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin)
ENDIF()
# This is the part for Python SWIG:
FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})
FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
SET(CMAKE_SWIG_FLAGS "")
SET_SOURCE_FILES_PROPERTIES(main.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(main.i PROPERTIES SWIG_FLAGS "-includeall")
SWIG_ADD_MODULE(main python main.i main.cc)
SWIG_LINK_LIBRARIES(main ${Memory_LIBRARIES} ${xmltio_LIBRARIES} ${PYTHON_LIBRARIES})
And this is how my main.i looks like:
/* File : main.i */
%module main
%{
/* Put headers and other declarations here */
extern int main(int argc, char *argv[]);
%}
extern int main(int argc, char *argv[]);
Any clue what went wrong here?
Is "main" maybe a bad name for this?
The swig invocation generates a source file that defines the init_main function. This file is either not compiled, or its object file is not linked into the shared object that constitutes your python extension.
Yes, main is a bad name, but here you are stuck at an earlier stage than where this might become a problem.

I'm not able to triangulate a regular point cloud

I easily tessellated two parametric 3D surfaces (both convex surfaces).
These are the two tessellated parametric surfaces:
Now, my intention is to join both in a single solid. But I'm obtaining this:
enter image description here
I'm using Qhull to create the Delaunay triangulation and it seems that works well for the 1st convex surface, but not for back surface. :(
This is my current code (parts taken from ZivS )
#include "Qhull.h"
using namespace orgQhull;
void myQhull::Qhull::runQhull3D(const pcl::PCLPointCloud2& pointCloud, const char* args)
{
std::cout << "runQhull vertices" << std::endl;
numVertices = 0;
std::stringstream verticesSS;
m_externalPoints = new PointCoordinates(3,""); //3 = dimension
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::fromPCLPointCloud2(pointCloud, *cloud);
std::vector<double> allPoints;
for (unsigned int i = 0; i < cloud->size(); i++) {
allPoints.push_back(cloud->at(i).x);
allPoints.push_back(cloud->at(i).y);
allPoints.push_back(cloud->at(i).z);
verticesSS << cloud->at(i).x << " " << cloud->at(i).y << " " << cloud->at(i).z << "\n";
numVertices++;
}
vertices += verticesSS.str();
m_externalPoints->append(allPoints); //convert to vector<double>
runQhull(*m_externalPoints, args);
}
void myQhull::Qhull::runQhull(const PointCoordinates &points, const char *qhullCommand2)
{
std::string triangles;
std::stringstream ss;
numSimplices = 0;
int numFaces = 0;
std::cout << numVertices << std::endl;
std::cout << "runQhull facets" << std::endl;
orgQhull::Qhull qHull(points.comment().c_str(), points.dimension(), points.count(), &*points.coordinates(), qhullCommand2);
QhullFacetList facets = qHull.facetList();
for (QhullFacetList::iterator it = facets.begin(); it != facets.end(); ++it)
{
if (!(*it).isGood()) continue;
QhullFacet f = *it;
QhullVertexSet vSet = f.vertices();
auto coord = f.hyperplane().coordinates();
numFaces = vSet.size();
ss << numFaces;
for (QhullVertexSet::iterator vIt = vSet.begin(); vIt != vSet.end(); ++vIt)
{
QhullVertex v = *vIt;
QhullPoint p = v.point();
double * coords = p.coordinates();
ss << " " << p.id() << " ";
}
ss << "\n";
numSimplices++;
}
simplices += ss.str();
std::cout << numSimplices << std::endl;
}
void myQhull::Qhull::saveOff(std::string file)
{
std::cout << "Saving qhull.off" << std::endl;
std::ofstream offFile;
offFile.open(file);
offFile << "OFF\n";
offFile << numVertices << " " << numSimplices << " 0";
offFile << vertices;
offFile << simplices;
offFile.close();
}
void myQhull::Qhull::run(const pcl::PCLPointCloud2& pointCloud)
{
Qhull qhull;
qhull.runQhull3D(pointCloud, "Qt");
qhull.saveOff("qhull.off");
}
Also, I used greedy_projection from OpenCV but without any success. It is able only to perform the two surfaces tessellation without joining them.
Any idea why this is happening?
Finally I found the solution.
Adding "d" to qhull.runQhull3D(pointCloud, "d Qt") generates the correct Delaunay triangulation for upper and lower surface.
Therefore, as they are regular meshes I, manually, create the edge connecting vertices from the two surfaces.
Thank you.

C++ program crashing on Python script call

I am attempting to send a command that runs a specific python script: however, whenever the program reaches the execution line, this occurs:
Unhandled exception at 0x69bd1f16 in GameServer.exe: 0xC0000005: Access violation reading location 0x46f520ca.
The program stops resonding and crashes. Here is the method in question:
void ScriptManager::runScript(std::string scriptName, std::string args[])
{
std::string py = "python " + scriptName;
std::cout << py << std::endl;
for(int i = 0; i < args->length(); i++)
{
py += " " + args[i];
std::cout << py << std::endl;
}
std::cout << py << std::endl;
std::system(py.c_str());
}
This calls the above function:
void DBFactory::dbRegisterUser(std::string username, std::string password)
{
ScriptManager script;
std::string data[] = {username, password};
script.runScript("Python.py", data);
}
The script does not run, as far as I know. I can also post the script if it would help.
This is the problem:
for (int i = 0; i < args->length(); i++)
{
py += " " + args[i];
std::cout << py << std::endl;
}
args->length() is equivalent to args[0].length(); i.e. you're taking the length of the first string in the array and using that as an index. After two iterations pass, you're going to access past the end of the array. The best solutions are(all examples are UNTESTED):
Use an std::array(C++11 only):
void DBFactory::dbRegisterUser(std::string username, std::string password)
{
ScriptManager script;
script.runScript("Python.py", {username, password});
}
void ScriptManager::runScript(std::string scriptName, std::array<std::string, 2> args)
{
std::string py = "python " + scriptName;
std::cout << py << std::endl;
for (std::string s : args)
{
py += " " + s;
std::cout << py << std::endl;
}
std::cout << py << std::endl;
std::system(py.c_str());
}
Use an std::vector(the example uses C++03):
void DBFactory::dbRegisterUser(std::string username, std::string password)
{
ScriptManager script;
int tmp[2] = {username, password};
script.runScript("Python.py", std::vector<std::string>(&tmp[0], &tmp[0]+2));
}
void ScriptManager::runScript(std::string scriptName, std::vector<std::string> args)
{
std::string py = "python " + scriptName;
std::cout << py << std::endl;
for(std::vector<std::string>::iterator it = args.begin(); it != args.end(); it++)
{
py += " " + *it;
std::cout << py << std::endl;
}
std::cout << py << std::endl;
std::system(py.c_str());
}
Pass the array size as a parameter:
void DBFactory::dbRegisterUser(std::string username, std::string password)
{
ScriptManager script;
script.runScript("Python.py", {username, password}, 2);
}
void ScriptManager::runScript(std::string scriptName, std::string args[], int size)
{
std::string py = "python " + scriptName;
std::cout << py << std::endl;
for(int i=0; i<size; i++)
{
py += " " + args[i];
std::cout << py << std::endl;
}
std::cout << py << std::endl;
std::system(py.c_str());
}
I personally prefer example 1 and would avoid example 3 like the plague. Example 2 works well but probably isn't as fast as example 1.

Categories