I am new to CGAL and am trying to find the length of each each edge in a mesh. I don't see any or length member functions or any way of easily getting the points on either side of the edge.
Here is where I have gotten so far. How do I get either the points on either end of the edge or the size of the edge itself?
#include <iostream>
#include <string>
#include <CGAL/Cartesian.h>
#include <CGAL/Filtered_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
typedef double Real;
typedef CGAL::Cartesian<Real> Kernel0;
// Use a filtered kernel so that all predicates are exact.
typedef CGAL::Filtered_kernel<Kernel0> Kernel;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
typedef Kernel::Point_3 Point;
void Edge_Analysis(Polyhedron mesh){
float mean = 0, min, max, length;
int count = 0; bool init = true;
for (Polyhedron::Edge_const_iterator edgeIter = mesh.edges_begin(); edgeIter != mesh.edges_end(); ++edgeIter){
Point a = edgeIter.prev()->vertex()->point();
Point b = edgeIter.vertex()->point();
length = CGAL::sqrt(CGAL::squared_distance(a, b));
++count;
if (init){
mean = min = max = length;
init = false;
}
else{
if (length < min) min = length;
if (length > max) max = length;
}
mean += length;
}
mean /= count;
std::cout << min << " " << max << " " << mean << "\n";
}
int main(int argc, char **argv){
Polyhedron mesh;
// Read the input mesh from standard input in OFF format.
if (!(std::cin >> mesh)) {
std::cerr << "Cannot read input mesh\n";
return 1;
}
Edge_Analysis(mesh);
return 0;
}
The only mistake that should be fixed in order to compile is:
const Point& a = edgeIter->prev()->vertex()->point();
const Point& b = edgeIter->vertex()->point();
You should use CGAL::Exact_predicates_inexact_constructions_kernel instead of Kernel. You should take a const ref on mesh if you want to avoid an unneeded copy.
Related
class enemy{
....
}
std::vector<std::unique_ptr<enemy> > enemies1;
for (unsigned i = 0; i < 3; ++i)
enemies1.emplace_back(...);
for (int i = 0; i < enemies1.size(); i++) {
std::cout << i <<"x: " << enemies1[i]->rect.getPosition().x << std::endl;
}
output:
100
200
400
How could I get the minimum coordinate value from multiple enemies in the vector? I want to detect the nearest enemy from the player, eg the player's coordinate is 50 and enemies are at 100, 200, 400, as you see in the above example. I want to detect the nearest enemy in the vector.
You can use min_element from
#include <algorithm>
#include <iostream>
#include <vector>
struct enemy_t
{
explicit enemy_t(const double d) :
distance{ d }
{
}
double distance;
};
int main()
{
// create a vector of enemies using the constructor with one double
std::vector<enemy_t> enemies{ 100.0,400.0,200.0,10.0 };
// the last argument to min_element is a lambda function
// it helps you define by what condition you want to find your element.
auto enemy = std::min_element(enemies.begin(), enemies.end(), [](const enemy_t& lhs, const enemy_t& rhs)
{
return lhs.distance < rhs.distance;
});
std::cout << "The minimum distance found = " << enemy->distance << "\n";
return 0;
}
For finding out the minimum you can use :
auto result = std::min_element(enemies.begin(), enemies.end(), [](auto a, auto b){return a->rect.getPosition()< b->rect.getPosition();});
std::cout<<"minimum is: "<<(**result).rect.getPosition()<<std::endl;
The above example will print out the position of the closest(minimum) enemy as you want. You just need to add the above two statements into your program.
To confirm that this works(compile and gives the expected result), below i have given an example whose output can be seen here.
#include <vector>
#include <iostream>
#include <algorithm>
struct Rectangle
{
int getPosition() const
{
return position;
}
Rectangle(int p):position(p)
{
}
int position = 0;
};
struct enemy
{
Rectangle rect;
enemy(int p): rect{p}
{
}
};
int main()
{
std::vector<enemy*> enemies;
enemy e1(600),e2(200),e3(400),e4(300), e5(100);
enemies.push_back(&e1);
enemies.push_back(&e2);
enemies.push_back(&e3);
enemies.push_back(&e4);
enemies.push_back(&e5);
auto result = std::min_element(enemies.begin(), enemies.end(), [](auto a, auto b){return a->rect.getPosition()< b->rect.getPosition();});
std::cout<<"minimum is: "<<(**result).rect.getPosition()<<std::endl;
return 0;
}
Im trying to create a adjacency representation of a graph.
I wrote a small program using vectors of vectors , however I keep getting "segmentation fault" but the compiler(clang++ version 5.0.1 on Windows) it seems wereever I try to access the vector vertex_matrix its giving a segmentation fault, why is it not being instantiated?
Here is the header:
#ifndef GRAPH_MATRIX
#define GRAPH_MATRIX
#include <vector>
//header for graph represented via adjacency matrix with minimal functionality
class graph
{
public:
graph(int);
~graph();
void add_edge(int v1, int v2, int weight);
void print_graph();
private:
std::vector<std::vector<int>> vertex_matrix;
int num_of_vertices;
int num_of_edges;
};
#endif
Here is the cpp implementation:
#include <iostream>
#include "graph_matrix.h"
#include <climits>
using namespace std;
//header for graph represented via adjacency matrix with minimal functionality
graph::graph(int _num_of_vertices) : num_of_vertices(_num_of_vertices)
{
if (_num_of_vertices==0)
{
_num_of_vertices=10;
}
for (int i = 0; i < _num_of_vertices; i++)
{
vertex_matrix[i]=(vector<int> (_num_of_vertices,INT_MAX));
}
}
graph::~graph()
{
vertex_matrix.clear();
}
void graph::add_edge(int v1, int v2, int weight)
{
//vertex_matrix[v1-1][v2-1] == INT_MAX
vector<int> columnVector = vertex_matrix[v1-1];
if (columnVector[v2-1] == INT_MAX)
{
columnVector[v2-1] = weight;
}
}
void graph::print_graph()
{
cout << "vertex_matrix size:" << vertex_matrix.size() << endl;
for (int i=0; i< num_of_vertices; i++)
{
for (int j = 0; j < num_of_vertices; j++)
{
//vertex_matrix[i][j]
std::vector<int> columnVector = vertex_matrix[i];
if (columnVector[j] != INT_MAX)
{
std::cout << columnVector[j] ;
}
else
{
std::cout << "0";
}
}
std::cout << endl;
}//end for printing
}
Here is the main entry:
#include <iostream>
#include "graph_matrix.h"
using namespace std;
int main ()
{
std::cout << " Matrix representation of graph" << std::endl;
graph _graph(4);
_graph.add_edge(1,2,1);
_graph.add_edge(2,3,1);
_graph.add_edge(3,1,1);
_graph.add_edge(3,3,1);
_graph.add_edge(3,4,1);
_graph.add_edge(4,0,0);
_graph.print_graph();
}
I edited the above code to use pass by reference, however the matrix still prints as 0's.
Please help with pass by reference, updates below:
Header:
#ifndef GRAPH_MATRIX
#define GRAPH_MATRIX
#include <vector>
//header for graph represented via adjacency matrix with minimal functionality
class graph
{
public:
graph(int);
~graph();
void add_edge(int v1, int v2, int weight,std::vector<std::vector<int>> & matrix);
void print_graph();
std::vector<std::vector<int>> vertex_matrix;
private:
int num_of_vertices;
int num_of_edges;
};
#endif
Cpp file:
#include <iostream>
#include "graph_matrix.h"
#include <climits>
using namespace std;
//header for graph represented via adjacency matrix with minimal functionality
graph::graph(int _num_of_vertices) : num_of_vertices(_num_of_vertices) {
if (num_of_vertices == 0) {
num_of_vertices = 10;
}
for (int i = 0; i < num_of_vertices; i++) {
std::vector<std::vector<int>>& matrix = vertex_matrix;
matrix.push_back(vector<int> (num_of_vertices, INT_MAX));
}
}
graph::~graph() {
std::vector<std::vector<int>>& matrix = vertex_matrix;
matrix.clear();
}
void graph::add_edge(int v1, int v2, int weight,std::vector<std::vector<int>> & _matrix) {
//vertex_matrix[v1-1][v2-1] == INT_MAX
vector<int> columnVector = _matrix[v1 - 1];
if (columnVector[v2 - 1] == INT_MAX) {
columnVector[v2 - 1] = weight;
}
}
void graph::print_graph() {
std::vector<std::vector<int>>& matrix = vertex_matrix;
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix.size(); j++) {
//vertex_matrix[i][j]
std::vector<int> columnVector = matrix[i];
if (columnVector[j] != INT_MAX) {
std::cout << columnVector[j];
} else {
std::cout << "0";
}
}
std::cout << endl;
}//end for printing
}
main:
#include <iostream>
#include "graph_matrix.h"
using namespace std;
int main ()
{
std::cout << " Matrix representation of graph" << std::endl;
graph _graph(4);
std::vector<std::vector<int>>& m = _graph.vertex_matrix;
_graph.add_edge(1,2,1,m);
_graph.add_edge(2,3,1,m);
_graph.add_edge(3,1,1,m);
_graph.add_edge(3,3,1,m);
_graph.add_edge(3,4,1,m);
_graph.add_edge(4,0,0,m);
_graph.print_graph();
}
Any help will be appreciated.
Thanks
You create an empty vector and then try to access elements in it. Change your constructor to
graph::graph(size_t _num_of_vertices) :
vertex_matrix(
std::vector<std::vector<int>>(
_num_of_vertices,std::vector<int>(_num_of_vertices)
)
)
{}
to create a correctly sized vector.
Also in case _num_vertices == 0 you set it to 10 but thats after you initialized the member num_vertices so you leave the object in an inconsistent state. There are different ways to fix that, I would probably just throw an exception when the number of vertices passed is zero, or just ignore it. User wants a zero sized matrix? Why not?
Moreover the size should be unsigned not signed, there is size_t for container sizes. Even better you shouldnt have that member at all, because a vector already knows its size, the only reason to repeat that information is to introduce mistakes ;)
i have a cpp problem and i don't know whats wrong.. maybe you can help me :).
I'm trying to implement a data structure for a graph. In this graph i will connect some nodes, which have a small euclidean distance, but at the second iteration, my iterator will point to 0x0. This case appears only, if i give the distance of those two nodes to std::cout. Here is my code:
for(vector<Node*>::iterator n1 = g->getNodes().begin(); n1 != g->getNodes().end(); ++n1)
{
for(vector<Node*>::iterator n2 = g->getNodes().begin(); n2 != g->getNodes().end(); ++n2)
{
if(*n2 == 0)
{
// This will be entered after the first iteration of n2.
cout << "n2 null" << endl;
continue;
}
double distance = (*n1)->getDistance(*n2); // just euclidean distance
if(distance <= minDistance)
{
// This works fine:
cout << "(" << *n1 << "," << *n2 << ") << endl;
// This brings me a "Segmentation fault"
cout << "(" << *n1 << " , " << *n2 << ") -> " << distance << endl;
}
}
}
Is this owed by the nested loops? Can any body tell me my fault? Thanks a lot!
EDIT: Here is some more code:
node.h
#ifndef NODE_H_
#define NODE_H_
#include <vector>
#include <iostream>
#include <limits>
#include <math.h>
using namespace std;
class Node
{
private:
int x, y, z;
public:
Node(int x, int y, int z) : x(x), y(y), z(z)
{
}
inline int getX() { return x; }
inline int getY() { return y; }
inline int getZ() { return z; }
inline double getDistance(Node* other)
{
return sqrt(pow(x-other->getX(), 2) + pow(y-other->getY(), 2) + pow(z-other->getZ(), 2));
}
};
#endif
graph.h
#ifndef GRAPH_H_
#define GRAPH_H_
#include <vector>
#include "node.h"
using namespace std;
class Graph
{
private:
vector<Node*> nodes;
public:
~Graph()
{
while(!nodes.empty())
{
delete nodes.back(), nodes.pop_back();
}
}
inline vector<Node*> getNodes() { return nodes; }
inline int getCountNodes() { return nodes.size(); }
bool createNode(int x, int y, int z)
{
nodes.push_back(new Node(x, y, z));
return true;
};
#endif
main.cc
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include "model/graph.h"
using namespace std;
int main()
{
Graph *g = new Graph();
int nodeDistance = 100;
for(int z = 0; z <= 300; z += nodeDistance)
{
for(int x = 0; x <= 500; x += nodeDistance)
{
for(int y = 0; y <= 300; y += nodeDistance)
{
g->createNode(x, y, z);
}
}
}
for(vector<Node*>::iterator n1 = g->getNodes().begin(); n1 != g->getNodes().end(); ++n1)
{
for(vector<Node*>::iterator n2 = g->getNodes().begin(); n2 != g->getNodes().end(); ++n2)
{
if(*n2 == 0)
{
// This will be entered after the first iteration of n2.
cout << "n2 null" << endl;
continue;
}
double distance = (*n1)->getDistance(*n2); // just euclidean distance
if(distance <= nodeDistance)
{
// This works fine:
cout << "(" << *n1 << "," << *n2 << ") << endl;
// This brings me a "Segmentation fault"
cout << "(" << *n1 << " , " << *n2 << ") -> " << distance << endl;
}
}
}
delete g;
return 0;
}
One major issue is that your getNodes function returns a copy of a vector, not the original vector. Therefore your iterators you use in the loops are not iterating over the same vector.
Instead, the iterators you're using in the nested loops are iterating over 4 different (but equivalent) vectors instead of the actual vector from the object in question.
There is nothing wrong in returning a copy of a vector in general. However when you do this, you have to make sure you call such a function if you really want a copy, and not the same vector. Using the getNodes function as you used it is not a valid usage in terms of what you are trying to accomplish.
The error is here:
inline vector<Node*> getNodes() { return nodes; }
The fix:
inline vector<Node*>& getNodes() { return nodes; }
The latter ensures that a reference to the actual vector in question is returned, not a copy of the actual vector. You can add an additional function that returns the vector as a copy if you want to still have the functionality available.
I am trying to figure out how I could access the values of the edges of the vertex so that I can print the graph.
This is my implementation of addEdge:
void clsGraph::add_edge(clsVertex* source, clsVertex* destination, int id, double weght)
{
clsEdge *a = new clsEdge;
a->ID = id;
a->weight = weght;
a->destination_vertex = destination;
source->edges.push_back(a);
}
This is my vertex class:
#ifndef CLSVERTEX_HPP
#define CLSVERTEX_HPP
#include <vector>
#include "clsEdge.hpp"
using namespace std; //namespace Rajkarnikar
{
class clsVertex
{
public:
int ID; //integer used to uniquely ID a vertex
vector<clsEdge*> edges; //a vector of pointers to a vertex’s edges
}; //
}
#endif
and I have this command to add my vertex:
void clsGraph::add_vertex(clsVertex* vertex)
{
verticies.push_back(vertex);
}
What I am trying to do is access the vertices and then check if it has any edges or not and output those edges as well.
can anyone please help me with this?
Thanks for the suggestion :)
This was the solution I used...
cout << ID << " " << graphType << endl;
for (int i = 0; i < verticies.size(); i++){
cout << verticies[i]->ID << "->";
for (int j = 0; j < verticies[i]->edges.size(); j++){
cout << verticies[i]->edges[j]->ID << endl;
}
Supposed I have the following dataset
double * data = (double *) malloc(sizeof(double) * 100 * 2);
for (ii = 0; ii < 100; ii++) {
data[2*ii] = ii;
data[2*ii + 1] = ii;
}
how can I create a boost polygon from this data?
thanks
A complete example
#include <iostream>
#include <boost/polygon/polygon.hpp>
#include <vector>
// Some typedefs
namespace bpl = boost::polygon;
typedef bpl::polygon_data<double> Polygon;
typedef bpl::polygon_traits<Polygon>::point_type Point;
int main() {
// Your C-style data (assumed (x,y) pairs)
double * data = (double *) malloc(sizeof(double) * 100 * 2);
for (int ii = 0; ii < 100; ii++) {
data[2*ii] = ii;
data[2*ii + 1] = ii;
}
// Convert to points
std::vector<Point> points;
for (int i=0;i<100;++i)
points.push_back(Point(data[2*i],data[2*i+1]));
// Create a polygon
Polygon polygon;
polygon.set(points.begin(),points.end());
// Do something with the polygon
std::cout << "Perimeter : " << bpl::perimeter(polygon) << std::endl;
std::cout << "Area : " << bpl::area(polygon) << std::endl;
return 0;
}
Just to illustrate the flexibility you actually have: with a bit of extra typedef work, its possible to define your own pair-of-doubles point type which can be aliased onto your data, which avoids the intermediate copy...
#include <iostream>
#include <boost/polygon/polygon.hpp>
#include <vector>
// Define a point type which can be aliased to your 'C' points
struct Pt {
double x;
double y;
};
// Some typedefs
namespace bpl = boost::polygon;
typedef bpl::polygon_data<double> Polygon;
// Add the necessary to use Pt
namespace boost {
namespace polygon {
template <> struct geometry_concept<Pt> {typedef point_concept type;};
template <> struct point_traits<Pt> {
typedef double coordinate_type;
static inline coordinate_type get(const Pt& pt,orientation_2d orient) {
return (orient == HORIZONTAL ? pt.x : pt.y);
}
};
template <> struct point_mutable_traits<Pt> {
static inline void set(Pt& pt, orientation_2d orient, int value) {
if(orient == HORIZONTAL)
pt.x = value;
else
pt.y = value;
}
static inline Pt construct(double x,double y) {
Pt r;
r.x=x;
r.y=y;
return r;
}
};
}
}
int main() {
// Your C-style data (assumed (x,y) pairs)
double * data = (double *) malloc(sizeof(double) * 100 * 2);
for (int ii = 0; ii < 100; ii++) {
data[2*ii] = ii;
data[2*ii + 1] = ii;
}
// Reinterpret your data as an array of Pt
const Pt*const pts=reinterpret_cast<const Pt*>(data);
// Create a polygon
Polygon polygon;
polygon.set(pts,pts+100);
// Do something with the polygon
std::cout << "Perimeter : " << bpl::perimeter(polygon) << std::endl;
std::cout << "Area : " << bpl::area(polygon) << std::endl;
return 0;
}
And this trend could be continued to a custom polygon class.