Developer Documentation
PolyLinePlugin_BezierSpline.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: 17392 $ *
45  * $Author: moebius $ *
46  * $Date: 2013-08-26 15:03:53 +0200 (Mon, 26 Aug 2013) $ *
47  * *
48 \*===========================================================================*/
49 
50 //=============================================================================
51 //
52 // CLASS PolyLinePlugin - IMPLEMENTATION
53 //
54 //=============================================================================
55 
56 
57 //== INCLUDES =================================================================
58 
59 #include "PolyLinePlugin.hh"
60 
64 {
65  TriMeshObject* mesh;
66  if(!PluginFunctions::getObject(_SplineData->meshIndex_, mesh))
67  return _point;
70  ACG::Vec3d nor = mesh->mesh()->normal(neigh.handle);
71  if(_nor)
72  *_nor = nor;
74  rayInt = bsp->raycollision(_point, nor);
75  if(rayInt.size())
76  return _point + nor * rayInt[0].second;
77  return _point + nor.normalize() * neigh.dist;
78 }
79 
80 void
83 {
84  PolyLineBezierSplineData* splineData = dynamic_cast<PolyLineBezierSplineData*>(_lineObject->objectData(BEZSPLINE_DATA));
85  if(!splineData)
86  return;
87  _line->clear();
88  for(unsigned int i = 0; i < splineData->handles_.size(); i++) {
90  if(_lineObject->getAdditionalNode(node, name(), "handle", i))
91  node->set_position(splineData->handles_[i]);
93  _line->add_line(control.position, splineData->handles_[i]);
94  _line->add_color(ACG::Vec4f(1,0,0,1));
95  }
96  for(unsigned int i = 0; i < splineData->points_.size(); i++) {
98  if(_lineObject->getAdditionalNode(node, name(), "control", i))
99  node->set_position(splineData->points_[i].position);
100  }
101 }
102 
103 void
105 updatePolyBezierSpline(PolyLineObject* _lineObject, unsigned int _pointCount)
106 {
107  PolyLineBezierSplineData* splineData = dynamic_cast<PolyLineBezierSplineData*>(_lineObject->objectData(BEZSPLINE_DATA));
108  TriMeshObject* mesh;
109  if(!splineData || !PluginFunctions::getObject(splineData->meshIndex_, mesh)) {
110  return;//no mesh -> do nothing
111  }
112  _lineObject->line()->clear();
113  unsigned int segCount = (splineData->points_.size() + splineData->handles_.size() - 1) / 3;
114  for(unsigned int s = 0; s < segCount; s++) {
115  const ACG::Vec3d a = splineData->points_[s].position, d = splineData->points_[s + 1].position,
116  b = splineData->handles_[s * 2], c = splineData->handles_[s * 2 + 1];
117  //the last segment will get one more point to close the spline
118  unsigned int n = (s != segCount - 1) ? _pointCount : (_pointCount + 1);
119  for(unsigned int i = 0; i < n; i++) {
120  float alpha = float(i) / float(_pointCount);
121  const ACG::Vec3d e = a + (b - a) * alpha, f = c + (d - c) * alpha;
122  ACG::Vec3d g = e + (f - e) * alpha;
123  g = getPointOnMesh(splineData, g);
124  _lineObject->line()->add_point(g);
125  }
126  }
127  emit updatedObject(_lineObject->id(), UPDATE_GEOMETRY | UPDATE_TOPOLOGY);
128 }
void updatePolyBezierHandles(PolyLineObject *_lineObject, ACG::SceneGraph::LineNode *_line)
Updates all the handles on the PolyBezier.
void add_point(const Point &_p)
Append a point to the polyline.
RayCollision raycollision(const Point &_p, const Point &_r) const
intersect mesh with ray
QString name()
Name of the Plugin.
NearestNeighbor nearest(const Point &_p) const
Return handle of the nearest neighbor face.
int id() const
Definition: BaseObject.cc:190
OMTriangleBSP * requestTriangleBsp()
bool getAdditionalNode(NodeT *&_node, QString _pluginName, QString _nodeName, int _id=0)
get an addition node from the object
ACG::Vec3d getPointOnMesh(PolyLineBezierSplineData *_SplineData, ACG::Vec3d _point, ACG::Vec3d *_nor=0)
Returns the nearest point on the mesh or if none could be found the input.
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM >>().norm())
Definition: Vector11T.hh:429
PolyLine * line()
return a pointer to the line
MeshT * mesh()
return a pointer to the mesh
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
Type for a MeshObject containing a triangle mesh.
Definition: TriangleMesh.hh:67
void add_line(const Vec3d &_v0, const Vec3d &_v1)
add line (for LineMode == LineSegmentsMode)
Definition: LineNode.cc:151
const UpdateType UPDATE_GEOMETRY(UpdateTypeSet(1)<< 2)
Geometry updated.
void clear()
Clear the current polyline.
InterpolatePoint & getInterpolatePoint(unsigned int _handleIndex)
Retrieves the interpolate point based on the handle.
size_t meshIndex_
Index of the corresponding mesh.
std::vector< std::pair< Handle, Scalar > > RayCollision
Store nearest neighbor information.
Definition: BSPImplT.hh:95
void clear()
clear points/lines and colors
Definition: LineNode.cc:101
const UpdateType UPDATE_TOPOLOGY(UpdateTypeSet(1)<< 3)
Topology updated.
void updatePolyBezierSpline(PolyLineObject *_lineObject, unsigned int _pointsPerSegment)
Generates points for the spline, updates handles.
void set_position(const Vec3d &_p, int _idx=0)
set position
PerObjectData * objectData(QString _dataName)
Returns the object data pointer.
Definition: BaseObject.cc:803
void add_color(const ACG::Vec3uc &_c)
add color (only for LineMode == LineSegmentsMode)
Definition: LineNode.cc:162