textured_triangle.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
33 #ifndef _TEXTURED_TRIANGLE_CPP_
34 #define _TEXTURED_TRIANGLE_CPP_
35 
36 #include "textured_triangle.h"
37 
38 
39 //TEXTURED VERTEX methods
40 
41 class_textured_vertex::class_textured_vertex(float vx, float vy, float vz, float vrgb)
42 {
43  x=vx; y=vy; z=vz; rgb=vrgb;
44 }
45 
46 //TEXTURED TRIANGLE methods
48 {
49  v[0].x = v0.x; v[0].y = v0.y; v[0].z = v0.z; v[0].rgb = v0.rgb;
50  v[1].x = v1.x; v[1].y = v1.y; v[1].z = v1.z; v[1].rgb = v1.rgb;
51  v[2].x = v2.x; v[2].y = v2.y; v[2].z = v2.z; v[2].rgb = v2.rgb;
52  weight = w;
53  provenience = p;
54 }
55 
56 
58 {
59 
60  CGAL::Triangle_2<K1> t1(CGAL::Point_2<K1>(v[0].x, v[0].y), CGAL::Point_2<K1>(v[1].x, v[1].y), CGAL::Point_2<K1>(v[2].x, v[2].y));
61  CGAL::Triangle_2<K1> t2(CGAL::Point_2<K1>(t->v[0].x, t->v[0].y), CGAL::Point_2<K1>(t->v[1].x, t->v[1].y), CGAL::Point_2<K1>(t->v[2].x, t->v[2].y));
62 
63  CGAL::Object result = CGAL::intersection(t1, t2);
64  if (const CGAL::Triangle_2<K1> *itriangle = CGAL::object_cast<CGAL::Triangle_2<K1> >(&result))
65  {
66  //if intersection is a triangle then the triangles overlap
67  //ROS_WARN("intersection was a triangle");
68  return 1;
69  }
70  else if (const std::vector<CGAL::Point_2<K1> > *iptvector = CGAL::object_cast<std::vector<CGAL::Point_2<K1> > >(&result))
71  {
72  //if intersection is vector of points, i.e., a polygon, then the triangles overlap
73  //ROS_WARN("intersection was a pt vector");
74  return 1;
75  }
76  else
77  {
78  //if intersection is any of the other cases, i.e., segment or point, then there is only overlap in the boundaries
79  return 0;
80  }
81 //}
82 
83 //Segment_2
84 //Triangle_2
85 //std::vector<Point_2>
86  //CDT2 triang;
87  //for (int i=0; i<3; i++)
88  //triang.insert(CDT2::Point(v[i].x, v[i].y));
89 
90  //CDT2::Locate_type lt[3];
91  //int li;
92  //CDT2::Face_handle fh = triang.locate(CDT2::Point(t->v[0].x, t->v[0].y), lt[0], li); //locate the point where we want to insert a new vertex
93 
94 
95  //fh = triang.locate(CDT2::Point(t->v[1].x, t->v[1].y), lt[1], li); //locate the point where we want to insert a new vertex
96  //fh = triang.locate(CDT2::Point(t->v[2].x, t->v[2].y), lt[2], li); //locate the point where we want to insert a new vertex
97 
98 
99  //if (lt[0])
100 
101  //First see if any of the t vertices is inside the class triangle
102  //for (int i=0; i<3; i++)
103  //{
106  //if (point_inside_triangle(v[0].x, v[0].y, v[1].x, v[1].y,v[2].x, v[2].y, t->v[i].x, t->v[i].y))
107  //return true;
108  //}
109 
111  //for (int i=0; i<3; i++)
112  //{
113  //if (point_inside_triangle(t->v[0].x, t->v[0].y, t->v[1].x, t->v[1].y,t->v[2].x, t->v[2].y, v[i].x, v[i].y))
114  //return true;
115  //}
116 
117  //double X,Y;
118  //if (lineSegmentIntersection( t->v[0].x, t->v[0].y, t->v[1].x, t->v[1].y,
119  //v[0].x, v[0].y, v[1].x, v[1].y,
120  //&X, &Y)==DO_INTERSECT)
121  //return true;
122 
123  //if (lineSegmentIntersection( t->v[1].x, t->v[1].y, t->v[2].x, t->v[2].y,
124  //v[0].x, v[0].y, v[1].x, v[1].y,
125  //&X, &Y)==DO_INTERSECT)
126  //return true;
127 
128  //if (lineSegmentIntersection( t->v[2].x, t->v[2].y, t->v[0].x, t->v[0].y,
129  //v[0].x, v[0].y, v[1].x, v[1].y,
130  //&X, &Y)==DO_INTERSECT)
131  //return true;
132 
133  //if (lineSegmentIntersection( t->v[0].x, t->v[0].y, t->v[1].x, t->v[1].y,
134  //v[2].x, v[2].y, v[0].x, v[0].y,
135  //&X, &Y)==DO_INTERSECT)
136  //return true;
137 
138  //if (lineSegmentIntersection( t->v[1].x, t->v[1].y, t->v[2].x, t->v[2].y,
139  //v[2].x, v[2].y, v[0].x, v[0].y,
140  //&X, &Y)==DO_INTERSECT)
141  //return true;
142 
143  //if (lineSegmentIntersection( t->v[2].x, t->v[2].y, t->v[0].x, t->v[0].y,
144  //v[2].x, v[2].y, v[0].x, v[0].y,
145  //&X, &Y)==DO_INTERSECT)
146  //return true;
147 
148  //if (lineSegmentIntersection( t->v[0].x, t->v[0].y, t->v[1].x, t->v[1].y,
149  //v[1].x, v[1].y, v[2].x, v[2].y,
150  //&X, &Y)==DO_INTERSECT)
151  //return true;
152 
153  //if (lineSegmentIntersection( t->v[1].x, t->v[1].y, t->v[2].x, t->v[2].y,
154  //v[1].x, v[1].y, v[2].x, v[2].y,
155  //&X, &Y)==DO_INTERSECT)
156  //return true;
157 
158  //if (lineSegmentIntersection( t->v[2].x, t->v[2].y, t->v[0].x, t->v[0].y,
159  //v[1].x, v[1].y, v[2].x, v[2].y,
160  //&X, &Y)==DO_INTERSECT)
161  //return true;
162 
163  //return false;
164 }
165 
166 
167 //TEXTURE SET methods
168 
170 {
171 
172  if (method==FORCE)
173  {
174  boost::shared_ptr<class_textured_triangle> t(new class_textured_triangle(v0,v1,v2, weight, provenience));
175  set.push_back(t);
176  }
177  else if (method==CHECK)
178  {
179  boost::shared_ptr<class_textured_triangle> t(new class_textured_triangle(v0,v1,v2, weight, provenience));
180 
181  std::priority_queue<size_t> index_to_remove; //a list of triangles to remove on account of the insertion of the new one
182 
183  //ROS_INFO("ADDING new triangle, list size %d", (int)set.size());
184 
185  //check i the triangle to be inserted intersects any of the ones on the list
186  for (size_t i=0; i<set.size(); ++i)
187  {
188  if (t->provenience != set[i]->provenience)
189  if (set[i]->overlaps(&(*t)))
190  {
191 
192  //ROS_DEBUG("new triangle overlaps with %i from list",(int)i);
193  //printf("new:\n");
194  //t.print_info();
195  //printf("triangle %d:\n",(int)i);
196  //it->print_info();
197  //if they overlap must compare weight
198  if (t->weight > set[i]->weight) //if new triangle has larger weight
199  {
200  //ROS_INFO("LARGER: new triangle W (%f); old triangle %d (W=%f)", t->weight,(int)i, set[i]->weight);
201  index_to_remove.push(i);
202  }
203  else //if new triangle has less weight, dont add it and return
204  {
205  //ROS_INFO("SMALLER: new triangle W (%f); old triangle %d (W=%f)", t->weight,(int)i, set[i]->weight);
206  t.reset();
207  return DID_NOT_ADD_TRIANGLE;
208  }
209  }
210  }
211 
212  //if the cycle ended and reached here, no return occurred which means the new triangle must be inserted and the ones in index_to_remove must be deleted
213 
214  //ROS_INFO("must remove %d triangles before adding new",(int) key_to_remove.size());
215  //first lets delete. Must be carefull not to loose the indexes propper position, to delete from the end to the begining
216 
217  //ROS_INFO("List size before removing=%d", (int)set.size());
218  while (!index_to_remove.empty())
219  {
220  ROS_INFO("Removing triangle %d", (int)index_to_remove.top());
221  set.erase(set.begin() + index_to_remove.top());
222  index_to_remove.pop();
223  }
224 
225 
226  //for (size_t i=0; i<index_to_remove.size(); ++i)
227 
228  //ROS_INFO("AAA List size after removing=%d", (int)set.size());
229  //finnaly we add the new triangle
230  set.push_back(t);
231  //ROS_INFO("AAA List size after adding=%d", (int)set.size());
232  return ADDED_TRIANGLE;
233  }
234 
235  return -1;
236 }
237 
238 int class_texture_set::set_transform(tf::Transform* st)
239 {
240  transform.setRotation(st->getRotation());
241  transform.setOrigin(st->getOrigin());
242  return 1;
243 }
244 
246 {
247  pc.points.erase(pc.points.begin(), pc.points.end());
248  pc_proveniences.erase(pc_proveniences.begin(), pc_proveniences.end());
249 
250  for(size_t k=0; k<set.size(); k++)
251  //std::map<size_t, boost::shared_ptr<class_textured_triangle> >::iterator it;
252  //for (it=set.begin(); it!=set.end();++it)
253  {
254 
255  //printf("INSIDE TRI%d\n v0 x=%f y=%f z=%f\n v1 x=%f y=%f z=%f\n v2 x=%f y=%f z=%f\n",(int)k, set[k].v[0].x, set[k].v[0].y,set[k].v[0].z,set[k].v[1].x,set[k].v[1].y,set[k].v[1].z,set[k].v[2].x,set[k].v[2].y,set[k].v[2].z);
256  for (int u=0; u<3;u++)
257  {
258  pcl::PointCloud<pcl::PointXYZ> pc_local;
259  pcl::PointXYZ pt;
260 
261  pt.x = set[k]->v[u].x;
262  pt.y = set[k]->v[u].y;
263  pt.z = set[k]->v[u].z;
264  pc_local.points.push_back(pt);
265 
266  pcl::PointCloud<pcl::PointXYZ> pc_global;
267  pcl_ros::transformPointCloud(pc_local, pc_global, transform.inverse());
268  pcl::PointXYZRGB ptcolor;
269  ptcolor.x = pc_global.points[0].x;
270  ptcolor.y = pc_global.points[0].y;
271  ptcolor.z = pc_global.points[0].z;
272  ptcolor.rgb = set[k]->v[u].rgb;
273 
274  pc.points.push_back(ptcolor);
275  pc_proveniences.push_back(set[k]->provenience);
276 
277  pc.height = 1;
278  pc.width = pc.points.size();
279  pc.is_dense = 0;
280 
281  }
282  }
283 
284  ROS_INFO("Exported triangle set %d triangles from a list with set.size()=%d", (int)pc.points.size(), (int)set.size());
285 
286 
287  return 1;
288 
289 
290 }
291 
292 #endif
293 
int set_transform(tf::Transform *st)
std::vector< int > pc_proveniences
class_textured_vertex v[3]
bool overlaps(class_textured_triangle *t)
int add_triangle(class_textured_vertex v0, class_textured_vertex v1, class_textured_vertex v2, int provenience, float weight, t_add_method method)
#define DID_NOT_ADD_TRIANGLE
t_add_method
std::vector< boost::shared_ptr< class_textured_triangle > > set
pcl::PointCloud< pcl::PointXYZRGB > pc
#define ADDED_TRIANGLE
header for textured triangle
tf::Transform transform


polygon_primitives_extraction
Author(s): Miguel Oliveira
autogenerated on Mon Mar 2 2015 01:32:42