Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PolyLineObject.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 //=============================================================================
51 //
52 // MyTypes
53 //
54 //=============================================================================
55 
56 #define POLYLINEOBJECT_C
57 
58 //== INCLUDES =================================================================
59 
61 #include "PolyLine.hh"
62 
63 //== DEFINES ==================================================================
64 
65 //== TYPEDEFS =================================================================
66 
67 //== CLASS DEFINITION =========================================================
68 
76  BaseObjectData( ),
77  line_(NULL),
78  lineNode_(NULL)
79 {
81  init();
82 }
83 
84 //=============================================================================
85 
86 
91  BaseObjectData(_object)
92 {
93 
94  init(_object.line_);
95 
96  setName( name() );
97 }
98 
103 {
104  // Delete the data attached to this object ( this will remove all perObject data)
105  // Not the best way to do it but it will work.
106  // This is only necessary if people use references to the line below and
107  // they do something with the polyline in the destructor of their
108  // perObjectData.
109  deleteData();
110 
111  // Delete the Mesh only, if this object contains a mesh
112  if ( line_ != NULL) {
113  delete line_;
114  line_ = NULL;
115  } else {
116  std::cerr << "Destructor error : Line already deleted" << std::endl;
117  }
118 
119  // No need to delete the scenegraph Nodes as this will be managed by baseplugin
120  lineNode_ = NULL;
121 }
122 
127  // Delete the Line only, if this object contains a line
128  if ( line_ != NULL) {
129  delete line_;
130  line_ = NULL;
131  } else {
132  std::cerr << "Cleanup error : Line already deleted" << std::endl;
133  }
134 
136 
137  lineNode_ = NULL;
138 
140 
141  init();
142 
143 }
144 
149  PolyLineObject* object = new PolyLineObject(*this);
150  return dynamic_cast< BaseObject* >(object);
151 }
152 
157 
158  if (_line == 0)
159  line_ = new PolyLine();
160  else
161  line_ = new PolyLine(*_line);
162 
163  // request default properties
164  line()->request_vertex_selections();
165  line()->request_edge_selections();
166  line()->request_vertex_vhandles();
167  line()->request_vertex_ehandles();
168  line()->request_vertex_fhandles();
169 
170 
171  if ( materialNode() == NULL)
172  std::cerr << "Error when creating Line Object! materialNode is NULL!" << std::endl;
173 
175 
176  // Set default material of the polyLine
181 
182 }
183 
184 // ===============================================================================
185 // Name/Path Handling
186 // ===============================================================================
187 
191 void PolyLineObject::setName( QString _name ) {
193 
194  std::string nodename = std::string("LineNode for Line " + _name.toUtf8() );
195  lineNode_->name( nodename );
196 }
197 
198 // ===============================================================================
199 // Content
200 // ===============================================================================
201 
206  return line_;
207 }
208 
209 
211  lineNode_->update();
212 }
213 
214 // ===============================================================================
215 // Visualization
216 // ===============================================================================
217 
219  return lineNode_;
220 }
221 
222 // ===============================================================================
223 // Object information
224 // ===============================================================================
225 
232  QString output;
233 
234  output += "========================================================================\n";
235  output += BaseObjectData::getObjectinfo();
236 
237  if ( dataType( DATA_POLY_LINE ) )
238  output += "Object Contains PolyLine : ";
239 
240  output += QString::number( line()->n_vertices() ) + " vertices, ";
241  output += QString::number( line()->n_edges() ) += " edges and is ";
242  if ( line()->is_closed() )
243  output += "closed.\n";
244  else
245  output += "open.\n";
246 
247  output += "========================================================================\n";
248  return output;
249 }
250 
251 // ===============================================================================
252 // Picking
253 // ===============================================================================
254 
261 bool PolyLineObject::picked( uint _node_idx ) {
262  return ( _node_idx == lineNode_->id() );
263 }
264 
265 void PolyLineObject::enablePicking( bool _enable ) {
266  lineNode_->enablePicking( _enable );
267 }
268 
270  return lineNode_->pickingEnabled();
271 }
272 
275  const ACG::Vec3d _hitPoint,
276  const ACG::Vec3d _start ,
277  const ACG::Vec3d _dir,
278  const unsigned int _targetIdx )
279 {
280  if ( _pickTarget == ACG::SceneGraph::PICK_FACE) {
281  //don't refine polyLine faces
282  return _hitPoint;
283  }
284 
285  if ( _pickTarget == ACG::SceneGraph::PICK_EDGE) {
286  // get picked edge handle
287 
288  int eh;
289 
290  eh = _targetIdx;
291 
292 
293  if(eh >= 0 && eh < (int)line()->n_edges())
294  {
295  //get vertices of the edge
296  ACG::Vec3d edgeStart = line()->point((eh+1)%line()->n_vertices());
297  ACG::Vec3d edgeEnd = line()->point(eh);
298 
299  //retrieve the point on the edge that is closest to the backprojected hitpoint
300  ACG::Vec3d hitPointNew;
301  ACG::Geometry::distPointLineSquared(_hitPoint,edgeStart,edgeEnd,&hitPointNew);
302 
303 
304  return hitPointNew;
305  }
306  }
307 
308  if ( _pickTarget == ACG::SceneGraph::PICK_VERTEX) {
309  // get picked vertex handle
310  int vh = _targetIdx;
311  if(vh>=0 && vh < (int)line()->n_vertices())
312  {
313  ACG::Vec3d hitpointNew = line()->point(vh);
314 
315  //just return the vertex position
316  return hitpointNew;
317  }
318  }
319 
320  return _hitPoint;
321 }
322 
323 //=============================================================================
void enablePicking(bool _enable)
Enable or disable picking for this Object.
ACG::Vec3d refinePick(ACG::SceneGraph::PickTarget _pickTarget, const ACG::Vec3d _hitPoint, const ACG::Vec3d _start, const ACG::Vec3d _dir, const unsigned int _targetIdx)
Refine picking on triangle meshes.
PickTarget
What target to use for picking.
Definition: BaseNode.hh:99
virtual QString getObjectinfo()
Get all Info for the Object as a string.
Definition: BaseObject.cc:255
ACG::PolyLineT< ACG::Vec3d > PolyLine
Simple Name for PolyLine.
#define DATA_POLY_LINE
Definition: PolyLine.hh:70
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:741
picks faces (should be implemented for all nodes)
Definition: BaseNode.hh:104
void deleteData()
Delete all data attached to this object ( calls delete on each object )
Definition: BaseObject.cc:823
Update type class.
Definition: UpdateType.hh:70
void set_random_color()
Generates a random color and sets it.
virtual void setName(QString _name)
path to the file from which the object is loaded ( defaults to "." )
virtual void cleanup()
Reset current object, including all related nodes.
ACG::SceneGraph::PolyLineNodeT< PolyLine > * lineNode_
Scenegraph Mesh Node.
void setName(QString _name)
Set the name of the Object.
MaterialNode * materialNode()
get a pointer to the materialnode
DataType dataType() const
Definition: BaseObject.cc:240
virtual void cleanup()
bool pickingEnabled()
Check if picking is enabled for this Object.
picks verices (may not be implemented for all nodes)
Definition: BaseNode.hh:108
picks edges (may not be implemented for all nodes)
Definition: BaseNode.hh:106
PolyLineObject()
constructor
bool picked(uint _node_idx)
detect if the node has been picked
virtual void init(PolyLine *_line=0)
Initialise current object, including all related nodes.
void set_point_size(float _sz)
set point size (default: 1.0)
QString getObjectinfo()
Get all Info for the Object as a string.
virtual void update(UpdateType _type=UPDATE_ALL)
This function is called to update the object.
void set_line_width(float _sz)
set line width (default: 1.0)
void setDataType(DataType _type)
Definition: BaseObject.cc:244
void set_round_points(bool _b)
set round points enabled
BaseObject * copy()
Point & point(unsigned int _i)
Get a point of the polyline.
Definition: PolyLineT.hh:148
ACG::SceneGraph::PolyLineNodeT< PolyLine > * lineNode()
Get the scenegraph Node.
PolyLine * line()
return a pointer to the line
PolyLine * line_
Pointer to the polyline.
Vec::value_type distPointLineSquared(const Vec &_p, const Vec &_v0, const Vec &_v1, Vec *_min_v)
squared distance from point _p to line segment (_v0,_v1)
Definition: Algorithms.cc:300
virtual ~PolyLineObject()
destructor