Developer Documentation
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 //=============================================================================
45 //
46 // MyTypes
47 //
48 //=============================================================================
49 
50 #define POLYLINEOBJECT_C
51 
52 //== INCLUDES =================================================================
53 
55 #include "PolyLine.hh"
56 
57 //== DEFINES ==================================================================
58 
59 //== TYPEDEFS =================================================================
60 
61 //== CLASS DEFINITION =========================================================
62 
70  BaseObjectData( ),
71  line_(NULL),
72  lineNode_(NULL)
73 {
75  init();
76 }
77 
78 //=============================================================================
79 
80 
85  BaseObjectData(_object)
86 {
87 
88  init(_object.line_);
89 
90  setName( name() );
91 }
92 
97 {
98  // Delete the data attached to this object ( this will remove all perObject data)
99  // Not the best way to do it but it will work.
100  // This is only necessary if people use references to the line below and
101  // they do something with the polyline in the destructor of their
102  // perObjectData.
103  deleteData();
104 
105  // Delete the Mesh only, if this object contains a mesh
106  if ( line_ != NULL) {
107  delete line_;
108  line_ = NULL;
109  } else {
110  std::cerr << "Destructor error : Line already deleted" << std::endl;
111  }
112 
113  // No need to delete the scenegraph Nodes as this will be managed by baseplugin
114  lineNode_ = NULL;
115 }
116 
121  // Delete the Line only, if this object contains a line
122  if ( line_ != NULL) {
123  delete line_;
124  line_ = NULL;
125  } else {
126  std::cerr << "Cleanup error : Line already deleted" << std::endl;
127  }
128 
130 
131  lineNode_ = NULL;
132 
134 
135  init();
136 
137 }
138 
143  PolyLineObject* object = new PolyLineObject(*this);
144  return dynamic_cast< BaseObject* >(object);
145 }
146 
151 
152  if (_line == 0)
153  line_ = new PolyLine();
154  else
155  line_ = new PolyLine(*_line);
156 
157  // request default properties
158  line()->request_vertex_selections();
159  line()->request_edge_selections();
160  line()->request_vertex_vhandles();
161  line()->request_vertex_ehandles();
162  line()->request_vertex_fhandles();
163 
164 
165  if ( materialNode() == NULL)
166  std::cerr << "Error when creating Line Object! materialNode is NULL!" << std::endl;
167 
169 
170  // Set default material of the polyLine
175 
176 }
177 
178 // ===============================================================================
179 // Name/Path Handling
180 // ===============================================================================
181 
185 void PolyLineObject::setName( QString _name ) {
187 
188  std::string nodename = std::string("LineNode for Line " + _name.toUtf8() );
189  lineNode_->name( nodename );
190 }
191 
192 // ===============================================================================
193 // Content
194 // ===============================================================================
195 
200  return line_;
201 }
202 
203 
205  lineNode_->update();
206 }
207 
208 // ===============================================================================
209 // Visualization
210 // ===============================================================================
211 
213  return lineNode_;
214 }
215 
216 // ===============================================================================
217 // Object information
218 // ===============================================================================
219 
226  QString output;
227 
228  output += "========================================================================\n";
229  output += BaseObjectData::getObjectinfo();
230 
231  if ( dataType( DATA_POLY_LINE ) )
232  output += "Object Contains PolyLine : ";
233 
234  output += QString::number( line()->n_vertices() ) + " vertices, ";
235  output += QString::number( line()->n_edges() ) += " edges and is ";
236  if ( line()->is_closed() )
237  output += "closed.\n";
238  else
239  output += "open.\n";
240 
241  output += "========================================================================\n";
242  return output;
243 }
244 
245 // ===============================================================================
246 // Picking
247 // ===============================================================================
248 
255 bool PolyLineObject::picked( uint _node_idx ) {
256  return ( _node_idx == lineNode_->id() );
257 }
258 
259 void PolyLineObject::enablePicking( bool _enable ) {
260  lineNode_->enablePicking( _enable );
261 }
262 
264  return lineNode_->pickingEnabled();
265 }
266 
269  const ACG::Vec3d _hitPoint,
270  const ACG::Vec3d _start ,
271  const ACG::Vec3d _dir,
272  const unsigned int _targetIdx )
273 {
274  if ( _pickTarget == ACG::SceneGraph::PICK_FACE) {
275  //don't refine polyLine faces
276  return _hitPoint;
277  }
278 
279  if ( _pickTarget == ACG::SceneGraph::PICK_EDGE) {
280  // get picked edge handle
281 
282  int eh;
283 
284  eh = _targetIdx;
285 
286 
287  if(eh >= 0 && eh < (int)line()->n_edges())
288  {
289  //get vertices of the edge
290  ACG::Vec3d edgeStart = line()->point((eh+1)%line()->n_vertices());
291  ACG::Vec3d edgeEnd = line()->point(eh);
292 
293  //retrieve the point on the edge that is closest to the backprojected hitpoint
294  ACG::Vec3d hitPointNew;
295  ACG::Geometry::distPointLineSquared(_hitPoint,edgeStart,edgeEnd,&hitPointNew);
296 
297 
298  return hitPointNew;
299  }
300  }
301 
302  if ( _pickTarget == ACG::SceneGraph::PICK_VERTEX) {
303  // get picked vertex handle
304  int vh = _targetIdx;
305  if(vh>=0 && vh < (int)line()->n_vertices())
306  {
307  ACG::Vec3d hitpointNew = line()->point(vh);
308 
309  //just return the vertex position
310  return hitpointNew;
311  }
312  }
313 
314  return _hitPoint;
315 }
316 
317 //=============================================================================
virtual QString getObjectinfo()
Get all Info for the Object as a string.
Definition: BaseObject.cc:244
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:290
void deleteData()
Delete all data attached to this object ( calls delete on each object )
Definition: BaseObject.cc:812
virtual void update(UpdateType _type=UPDATE_ALL)
This function is called to update the object.
picks edges (may not be implemented for all nodes)
Definition: PickTarget.hh:80
virtual void setName(QString _name) override
path to the file from which the object is loaded ( defaults to "." )
PolyLine * line_
Pointer to the polyline.
picks faces (should be implemented for all nodes)
Definition: PickTarget.hh:78
bool pickingEnabled()
Check if picking is enabled for this Object.
void setName(QString _name)
Set the name of the Object.
virtual ~PolyLineObject()
destructor
ACG::SceneGraph::PolyLineNodeT< PolyLine > * lineNode_
Scenegraph Mesh Node.
virtual void cleanup()
Reset current object, including all related nodes.
void set_line_width(float _sz)
set line width (default: 1.0)
ACG::PolyLineT< ACG::Vec3d > PolyLine
Simple Name for PolyLine.
void set_random_color()
Generates a random color and sets it.
QString getObjectinfo()
Get all Info for the Object as a string.
#define DATA_POLY_LINE
Definition: PolyLine.hh:64
bool picked(uint _node_idx)
detect if the node has been picked
PickTarget
What target to use for picking.
Definition: PickTarget.hh:73
virtual void cleanup() override
BaseObject * copy()
Point & point(unsigned int _i)
Get a point of the polyline.
Definition: PolyLineT.hh:142
picks verices (may not be implemented for all nodes)
Definition: PickTarget.hh:82
QString name() const
return the name of the object. The name defaults to NONAME if unset.
Definition: BaseObject.cc:730
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.
Update type class.
Definition: UpdateType.hh:59
PolyLine * line()
return a pointer to the line
MaterialNode * materialNode()
get a pointer to the materialnode
void set_round_points(bool _b)
set round points enabled
virtual void init(PolyLine *_line=0)
Initialise current object, including all related nodes.
ACG::SceneGraph::PolyLineNodeT< PolyLine > * lineNode()
Get the scenegraph Node.
void enablePicking(bool _enable)
Enable or disable picking for this Object.
void setDataType(DataType _type)
Definition: BaseObject.cc:233
PolyLineObject()
constructor
DataType dataType() const
Definition: BaseObject.cc:229
void set_point_size(float _sz)
set point size (default: 1.0)