dxf_to_graph.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, <copyright holder> <email>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the <organization> nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY <copyright holder> <email> ''AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> <email> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
30 #include <dxflib/dl_dxf.h>
32 #include <sstream>
33 
34 #include <math.h>
35 
36 namespace tuw_graph
37 {
38  bool DxfToGraph::parseGraph(const std::string &_dxfPath, const float _segLength, const float _segWidth)
39  {
40  DxfLineArcParser creationInterface;
41  DL_Dxf dxf;
42 
43  if(!dxf.in(_dxfPath, &creationInterface))
44  {
45  std::cerr << "\t" << _dxfPath << " could not be opened.\n";
46  return false;
47  }
48 
49  std::vector<Line> lineSegments;
50  const std::vector<DL_LineData> lines = creationInterface.getLines();
51 
52  for(const DL_LineData & line : lines)
53  {
54  std::vector<Line> segs = splitLine(line, _segLength);
55  lineSegments.insert(lineSegments.end(), segs.begin(), segs.end());
56  }
57 
58  const std::vector<DL_CircleData> circles = creationInterface.getCircles();
59 
60  for(const DL_CircleData & circle : circles)
61  {
62  std::vector<Line> segs = splitCircle(circle, _segLength);
63  lineSegments.insert(lineSegments.end(), segs.begin(), segs.end());
64  }
65 
66  const std::vector<DL_ArcData> arcs = creationInterface.getArcs();
67 
68  for(const DL_ArcData & arc : arcs)
69  {
70  std::vector<Line> segs = splitArc(arc, _segLength);
71  lineSegments.insert(lineSegments.end(), segs.begin(), segs.end());
72  }
73 
74 
75  std::vector<DL_ImageData> image = creationInterface.getImage();
76  if(image.size() > 1)
77  {
78  std::cerr << "\tToo many images in the dxf file" << std::endl;
79  return false;
80  }
81 
82  if(!getGraphData(image[0], scale_, offset_))
83  {
84  std::cerr << "\tInconsistant scale in image" << std::endl;
85  return false;
86  }
87 
88  graphData_ = generateGraph(lineSegments, _segWidth, scale_, offset_);
89 
90  return true;
91  }
92 
93 
94  std::vector< Line > DxfToGraph::splitLine(const DL_LineData &_line, const float _segLength) const
95  {
96  std::vector<Line> segments;
97  Eigen::Vector2d start(_line.x1, _line.y1);
98  Eigen::Vector2d end(_line.x2, _line.y2);
99 
100  float length = (end - start).norm();
101  uint32_t splits = length / _segLength;
102 
103  Eigen::Vector2d increment = (end - start) / splits;
104  Eigen::Vector2d current = start;
105 
106  for(uint32_t i = 0; i < splits; i++)
107  {
108  Line l(current, current + increment);
109  segments.push_back(l);
110  current += increment;
111  }
112 
113  if(segments.size() == 0)
114  {
115  Line l(start, end);
116  segments.push_back(l);
117  }
118 
119  return segments;
120  }
121 
122  std::vector< Line > DxfToGraph::splitCircle(const DL_CircleData &_circle, const float _segLength) const
123  {
124  DL_ArcData _arc(_circle.cx, _circle.cy, _circle.cz, _circle.radius, 0, 360);
125  return splitArc(_arc, _segLength);
126  }
127 
128 
129  std::vector< Line > DxfToGraph::splitArc(const DL_ArcData &_arc, const float _segLength) const
130  {
131  std::vector<Line> segments;
132 
133  //Take care DL_ uses Degrees
134  float angle1 = _arc.angle1 / 180 * M_PI;
135  float angle2 = _arc.angle2 / 180 * M_PI;;
136 
137  if(_arc.angle1 >= _arc.angle2)
138  angle2 += 2 * M_PI;
139 
140  float arcLength = (angle2 - angle1) * _arc.radius;
141  uint32_t splits = arcLength / _segLength;
142 
143  float angleIncrement_radians = (arcLength / (float)splits) / _arc.radius;
144  float current_angle_radians = angle1;
145  Eigen::Vector2d current_point(_arc.cx + _arc.radius * cos(current_angle_radians), _arc.cy + _arc.radius * sin(current_angle_radians));
146 
147  for(uint32_t i = 0; i < splits; i++)
148  {
149  current_angle_radians += angleIncrement_radians;
150  Eigen::Vector2d nextPoint(_arc.cx + _arc.radius * cos(current_angle_radians), _arc.cy + _arc.radius * sin(current_angle_radians));
151  Line l(current_point, nextPoint);
152  segments.push_back(l);
153 
154  current_point = nextPoint;
155  }
156 
157  return segments;
158  }
159 
160  bool DxfToGraph::getGraphData(const DL_ImageData &_image, float &_scale, Eigen::Vector2d &_offset) const
161  {
162  //Rotation doesnt matter because the lines are drawn in the right direction...
163  //Scales should be equal (u, v)
164  //Offset ip
165 
166  _offset[0] = _image.ipx;
167  _offset[1] = _image.ipy;
168 
169  float scale_u = sqrt((_image.ux * _image.ux) + (_image.uy * _image.uy));
170  float scale_v = sqrt((_image.vx * _image.vx) + (_image.vy * _image.vy));
171 
172  if(scale_u != scale_v)
173  return false;
174 
175  _scale = scale_u;
176 
177  return true;
178  }
179 
180 
181  std::vector< Segment > DxfToGraph::generateGraph(const std::vector< Line > &_lines, const float _segWidth, const float &_scale, const Eigen::Vector2d &_offset) const
182  {
183  std::vector<Segment> segments;
185 
186  //Create segments
187  for(uint32_t i = 0; i < _lines.size(); i++)
188  {
189  std::vector<Eigen::Vector2d> points({ (_lines[i].start-_offset)/scale_, (_lines[i].end-_offset)/scale_ });
190  Segment s(points , _segWidth/scale_);
191 
192  segments.push_back(s);
193  }
194 
195 
196  //Assign Neighbors
197  for(uint32_t i = 0; i < _lines.size(); i++)
198  {
199  for(uint32_t j = 0; j < _lines.size(); j++)
200  {
201  if(i != j)
202  {
203  if(((segments[i].getStart() - segments[j].getStart()).norm() < _scale) ||
204  ((segments[i].getStart() - segments[j].getEnd()).norm() < _scale))
205  {
206  segments[i].addPredecessor(j);
207  }
208  if(((segments[i].getEnd() - segments[j].getStart()).norm() < _scale) ||
209  ((segments[i].getEnd() - segments[j].getEnd()).norm() < _scale))
210  {
211  segments[i].addSuccessor(j);
212  }
213  }
214  }
215  }
216 
217  return segments;
218  }
219 
220 
221 
222 
223 
224  void DxfToGraph::serializeGraph(const std::string &_graphPath) const
225  {
226  Serializer s;
227  s.save(_graphPath, graphData_, offset_, scale_);
228  }
229 
230 }
bool getGraphData(const DL_ImageData &_image, float &_scale, Eigen::Vector2d &_offset) const
void serializeGraph(const std::string &_graphPath) const
serializes the graph and saves it to memory
std::vector< Segment > generateGraph(const std::vector< Line > &_lines, const float _segWidth, const float &_scale, const Eigen::Vector2d &_offset) const
static void resetId()
resets the id counter (which is used to generate uinique ids)
Definition: segment.cpp:182
XmlRpcServer s
std::vector< Line > splitCircle(const DL_CircleData &_circle, const float _segLength) const
bool parseGraph(const std::string &_dxfPath, const float _segLength, const float _segWidth)
reads the graph from the dx file
const std::vector< DL_ArcData > & getArcs()
const std::vector< DL_CircleData > & getCircles()
std::vector< Segment > graphData_
Definition: dxf_to_graph.h:69
const std::vector< DL_ImageData > & getImage()
void save(const std::string &_mapPath, const std::vector< Segment > &_segs, const Eigen::Vector2d &_origin, const float &_resolution)
saves the graph to a specific path in xml format
Definition: serializer.cpp:142
std::vector< Line > splitLine(const DL_LineData &_line, const float _segLength) const
TFSIMD_FORCE_INLINE tfScalar length(const Quaternion &q)
std::vector< Line > splitArc(const DL_ArcData &_arc, const float _segLength) const
const std::vector< DL_LineData > & getLines()
Eigen::Vector2d offset_
Definition: dxf_to_graph.h:71


tuw_voronoi_graph
Author(s): Benjamin Binder
autogenerated on Mon Jun 10 2019 15:42:44