Developer Documentation
PolyLineScripting.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 #include "PolyLinePlugin.hh"
43 
44 //------------------------------------------------------------------------------
45 
55 int PolyLinePlugin::generatePolyLineFromCut( int _objectId, Vector _planePoint, Vector _planeNormal, int _polyLineId ) {
56 
57  // get object
58  BaseObjectData *obj;
59  PluginFunctions::getObject(_objectId, obj);
60 
61  if (obj == 0){
62  emit log(LOGERR,tr("Unable to get object"));
63  return -1;
64  }
65 
66  //get the intersection points
67 
68  std::vector< ACG::Vec3d > linePoints;
69  bool closed = false;
70  if ( obj->dataType(DATA_TRIANGLE_MESH) ) {
71 
72  TriMesh* mesh = PluginFunctions::triMesh(obj);
73 
74  if ( mesh == 0 ) {
75  emit log(LOGERR,tr("Unable to get mesh"));
76  return -1;
77  }
78 
79  //get an edge of the mesh that is cut by the plane
80  TriMesh::EdgeHandle eh = getCuttedEdge( *mesh, _planeNormal, _planePoint );
81 
82  if ( !eh.is_valid() ) {
83  emit log(LOGERR,tr("Unable to find initial cut edge."));
84  return -1;
85  }
86 
87  TriMesh::FaceHandle fh = mesh->face_handle( mesh->halfedge_handle(eh, 0) );
88 
89  if (!fh.is_valid())
90  fh = mesh->face_handle( mesh->halfedge_handle(eh, 1) );
91 
92  // get all intersection points
93  linePoints = getIntersectionPoints( mesh, fh.idx() , _planeNormal , _planePoint, closed);
94 
95  } else {
96 
98 
99  if ( mesh == 0 ) {
100  emit log(LOGERR,tr("Unable to get mesh"));
101  return -1;
102  }
103 
104  //get a edge of the mesh that is cut by the plane
105  PolyMesh::EdgeHandle eh = getCuttedEdge( *mesh, _planeNormal, _planePoint );
106 
107  if ( !eh.is_valid() ) {
108  emit log(LOGERR,tr("Unable to find initial cut edge."));
109  return -1;
110  }
111 
112  PolyMesh::FaceHandle fh = mesh->face_handle( mesh->halfedge_handle(eh, 0) );
113 
114  if (!fh.is_valid())
115  fh = mesh->face_handle( mesh->halfedge_handle(eh, 1) );
116 
117  // get all intersection points
118  linePoints = getIntersectionPoints( mesh, fh.idx() , _planeNormal , _planePoint, closed);
119  }
120 
121  if ( linePoints.empty() ) {
122  emit log(LOGERR,tr("No points from cut found."));
123  return -1;
124  }
125 
126  //generate a polyLine from the intersection Points
127  int polyLineId = -1;
128 
129  // add new polyline
130  if (_polyLineId == -1)
131  emit addEmptyObject(DATA_POLY_LINE,polyLineId);
132  else
133  polyLineId = _polyLineId;
134 
135  // get current polylineobject
136  BaseObjectData *polyLineObj;
137  PluginFunctions::getObject(polyLineId, polyLineObj);
138 
139  // get polyline object
140  PolyLineObject* currentPolyLine = PluginFunctions::polyLineObject(polyLineObj);
141 
142  currentPolyLine->line()->clear();
143 
144  for ( uint i = 0 ; i < linePoints.size(); ++i )
145  currentPolyLine->line()->add_point( (PolyLine::Point) linePoints[i] );
146 
147  currentPolyLine->line()->set_closed(closed);
148  currentPolyLine->target(true);
149 
150  return polyLineId;
151 }
152 
153 
154 
163 IdList PolyLinePlugin::generatePolyLinesFromCut( int _objectId, Vector _planePoint, Vector _planeNormal) {
164 
165  // List of generated lines
166  std::vector<int> lines;
167 
168 
169  // get object
170  BaseObjectData *obj;
171  PluginFunctions::getObject(_objectId, obj);
172 
173  if (obj == 0){
174  emit log(LOGERR,tr("Unable to get object"));
175  return lines;
176  }
177 
178  //get the intersection points
179 
180  std::vector< std::vector< ACG::Vec3d > > linePoints;
181  bool closed = false;
182  if ( obj->dataType(DATA_TRIANGLE_MESH) ) {
183 
184  TriMesh* mesh = PluginFunctions::triMesh(obj);
185 
186  if ( mesh == 0 ) {
187  emit log(LOGERR,tr("Unable to get mesh"));
188  return lines;
189  }
190 
191  // get all intersection points
192  linePoints = getMultipleIntersectionPoints( mesh, _planeNormal , _planePoint);
193 
194  } else {
195 
196  PolyMesh* mesh = PluginFunctions::polyMesh(obj);
197 
198  if ( mesh == 0 ) {
199  emit log(LOGERR,tr("Unable to get mesh"));
200  return lines;
201  }
202 
203  // get all intersection points
204  linePoints = getMultipleIntersectionPoints( mesh, _planeNormal , _planePoint);
205  }
206 
207 
208  // No lines found?
209  if ( linePoints.empty() ) {
210  emit log(LOGERR,tr("No cut lines found."));
211  return lines;
212  }
213 
214 
215  for ( unsigned int i = 0 ; i < linePoints.size(); ++i ) {
216 
217  if ( linePoints[i].empty() )
218  continue;
219 
220  //generate a polyLine from the intersection Points
221  int polyLineId = -1;
222 
223  // add new polyline
224  emit addEmptyObject(DATA_POLY_LINE,polyLineId);
225 
226  // get current polylineobject
227  BaseObjectData *polyLineObj;
228  PluginFunctions::getObject(polyLineId, polyLineObj);
229 
230  // get polyline object
231  PolyLineObject* currentPolyLine = PluginFunctions::polyLineObject(polyLineObj);
232 
233  currentPolyLine->line()->clear();
234 
235  for ( unsigned int j = 0 ; j < linePoints[i].size(); ++j )
236  currentPolyLine->line()->add_point( (PolyLine::Point) linePoints[i][j] );
237 
238  currentPolyLine->line()->set_closed(closed);
239  currentPolyLine->target(true);
240 
241  lines.push_back(polyLineId);
242 
243  }
244 
245 
246  return lines;
247 }
void add_point(const Point &_p)
Append a point to the polyline.
IdList generatePolyLinesFromCut(int _objectId, Vector _planePoint, Vector _planeNormal)
Generates a polyLine of a plane intersection.
void set_closed(const bool _c)
Set if the polyline should be closed and therefore forms a loop.
Definition: PolyLineT.hh:113
std::vector< std::vector< ACG::Vec3d > > getMultipleIntersectionPoints(MeshT *_mesh, ACG::Vec3d _planeNormal, ACG::Vec3d _planePoint)
get all points from the intersection between mesh and plane
PolyLine * line()
return a pointer to the line
bool dataType(DataType _type) const
Definition: BaseObject.cc:221
std::vector< ACG::Vec3d > getIntersectionPoints(MeshT *_mesh, uint _fh, ACG::Vec3d _planeNormal, ACG::Vec3d _planePoint, bool &_closed)
get the points from the closest connected intersection between mesh and plane
TriMesh * triMesh(BaseObjectData *_object)
Get a triangle mesh from an object.
bool target()
Definition: BaseObject.cc:273
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
MeshT::EdgeHandle getCuttedEdge(MeshT &_mesh, ACG::Vec3d &_planeNormal, ACG::Vec3d &_planePoint)
get an edge of the mesh that is cut by the plane
std::vector< int > IdList
Standard Type for id Lists used for scripting.
Definition: DataTypes.hh:179
void clear()
Clear the current polyline.
#define DATA_POLY_LINE
Definition: PolyLine.hh:64
int generatePolyLineFromCut(int _objectId, Vector _planePoint, Vector _planeNormal, int _polyLineId=-1)
Generates a polyLine of a plane intersection.
PolyMesh * polyMesh(BaseObjectData *_object)
Get a poly mesh from an object.
#define DATA_TRIANGLE_MESH
Definition: TriangleMesh.hh:60
PolyLineObject * polyLineObject(BaseObjectData *_object)
Cast an BaseObject to a PolyLineObject if possible.