Commit 881a88f5 authored by Jan Möbius's avatar Jan Möbius

Hannes: PolyLine Circles update. TODO: Cleanup variable names!

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@17375 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 8074fbe9
......@@ -26,6 +26,24 @@
#include <OpenFlipper/common/GlobalOptions.hh>
#include <ACG/Geometry/bsp/BSPImplT.hh>
namespace {
class GlutObjectNode : public ACG::SceneGraph::GlutPrimitiveNode{
public:
GlutObjectNode(PolyLineObject* L, BaseNode* M, std::string name)
: ACG::SceneGraph::GlutPrimitiveNode(ACG::SceneGraph::GlutPrimitiveNode::SPHERE, M, name)
{
line = L;
}
public:
PolyLineObject* line;
};
}
//== IMPLEMENTATION ==========================================================
......@@ -38,26 +56,25 @@ PolyLinePlugin::PolyLinePlugin() :
pickToolbar_(0),
pickToolBarActions_(0),
insertAction_(0),
insertCircleAction_(0),
deleteAction_(0),
moveAction_(0),
smartMoveAction_(0),
mergeAction_(0),
splitAction_(0),
cutAction_(0),
insertCircleAction_(0),
cur_insert_id_(-1),
cur_polyline_obj_(0),
cur_move_id_(-1),
move_point_ref_(0),
create_point_ref_(0),
createCircle_Active_(false),
createCircle_CurrSelIndex_(-1),
moveCircle_SelNode_(0),
cur_merge_id_(-1),
smart_move_timer_(0),
cur_smart_move_obj_(0),
planeSelect_(0)
{
}
......@@ -96,6 +113,8 @@ initializePlugin()
connect(tool_->rb_merge, SIGNAL( clicked() ), this, SLOT( slotEditModeChanged() ));
connect(tool_->rb_split, SIGNAL( clicked() ), this, SLOT( slotEditModeChanged() ));
connect(tool_->sb_CirclePointNum, SIGNAL(valueChanged(int)), this, SLOT(slot_setCirclePointNum(int)));
//add icons
tool_->rb_insert->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "polyline_insert.png") );
tool_->rb_InsertCircle->setIcon( QIcon(OpenFlipper::Options::iconDirStr() + OpenFlipper::Options::dirSeparator() + "polyline_circle.png") );
......@@ -746,9 +765,10 @@ slot_smart_move_timer()
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
PolyLinePlugin::EditMode
PolyLinePlugin::
mode()
......@@ -767,10 +787,8 @@ mode()
return PL_NONE;
}
//-----------------------------------------------------------------------------
void
PolyLinePlugin::
me_insert( QMouseEvent* _event )
......@@ -867,61 +885,91 @@ me_insert( QMouseEvent* _event )
}
}
//-----------------------------------------------------------------------------
void PolyLinePlugin::
me_insertCircle(QMouseEvent* _event)
{
ACG::GLState& gl = PluginFunctions::viewerProperties().glState();
TriMeshObject* mesh;
TriMesh::FaceHandle fh;
TriMesh::VertexHandle vh;
ACG::Vec3d hit_point;
if(!pick_triangle_mesh(_event->pos(), mesh, fh, vh, hit_point))
return;//can't generate a circle in empty space
if(_event->type() == QEvent::MouseMove && createCircle_Active_) {
if(!cur_polyline_obj_ || !cur_polyline_obj_->line()) {
createCircle_Active_ = false;
if(_event->type() == QEvent::MouseMove && createCircle_CurrSelIndex_ != -1) {
PolyLineObject* L = 0;
if(!PluginFunctions::getObject(createCircle_CurrSelIndex_, L))
return;
}
ACG::Vec3d n = createCircle_Normal, x0 = createCircle_Point_;
double t = ((n | x0) - (n | hit_point)) / (n | n);
ACG::Vec3d onPlane = hit_point + t * n, d = onPlane - x0;
double r = sqrt(d | d);
const unsigned int N = 16;
ACG::Quaterniond qRot(n, 2.0 * M_PI / double(N));
ACG::Vec3d cPos = onPlane - x0;
cur_polyline_obj_->line()->clear();
for(unsigned int i = 0; i <= N; i++) {
ACG::Vec3d screenCoords = gl.project(cPos + x0), p = cPos;
QPoint qp((int)screenCoords[0], (int)(gl.viewport_height() - screenCoords[1]));
if(pick_triangle_mesh(qp, mesh, fh, vh, p))
cur_polyline_obj_->line()->add_point((PolyLine::Point) (p ));
cPos = qRot.rotate(cPos);
}
//cur_polyline_obj_->line()->add_point((PolyLine::Point) onPlane);
emit updatedObject(cur_insert_id_, UPDATE_GEOMETRY | UPDATE_TOPOLOGY);
const ACG::Vec3d n = createCircle_Normal_, x0 = createCircle_Point_;
const double t = ((n | x0) - (n | hit_point)) / (n | n);
const ACG::Vec3d onPlane = hit_point + t * n, d = onPlane - x0;
const double r = d.norm();
const ACG::Vec3d mainAxis = (onPlane - x0).normalize(), sideAxis = (mainAxis % n).normalize();
PolyLineCircleData* LD = new PolyLineCircleData(x0, n, mainAxis, sideAxis, r, r, mesh->id());
L->setObjectData(CIRCLE_DATA, LD);
updatePolyEllipse(L, tool_->sb_CirclePointNum->value());
updateHandles(L);
}
else if(_event->type() == QEvent::MouseButtonPress) {
emit addEmptyObject(DATA_POLY_LINE, cur_insert_id_);
BaseObjectData *obj = 0;
PluginFunctions::getObject(cur_insert_id_, obj);
obj->target(true);
cur_polyline_obj_ = PluginFunctions::polyLineObject(obj);
cur_polyline_obj_->materialNode()->set_random_color();
PolyLineObject* newLine = PluginFunctions::polyLineObject(obj);
newLine->materialNode()->set_random_color();
createCircle_Point_ = hit_point;
if(!mesh->mesh()->has_face_normals())
mesh->mesh()->request_face_normals();
createCircle_Normal = PluginFunctions::viewingDirection();// mesh->mesh()->normal(fh);
createCircle_Normal_ = mesh->mesh()->normal(fh);
emit updatedObject(cur_insert_id_, UPDATE_GEOMETRY | UPDATE_TOPOLOGY);
createCircle_Active_ = true;
createCircle_CurrSelIndex_ = newLine->id();
ACG::Vec3d bbMin( FLT_MAX, FLT_MAX, FLT_MAX);
ACG::Vec3d bbMax(-FLT_MAX,-FLT_MAX,-FLT_MAX);
mesh->boundingBox(bbMin, bbMax);
const ACG::Vec3d sizeBB((bbMax-bbMin));
GlutObjectNode* handle0 = new GlutObjectNode(newLine, obj->manipulatorNode(), "N_Handle0");
handle0->get_primitive(0).color = ACG::Vec4f(1,0,0,1);
handle0->set_size(0.005*sizeBB.norm());
handle0->show();
handle0->enablePicking(false);
handle0->set_position(hit_point);
newLine->addAdditionalNode(handle0, name(), "handle0");
handle0->drawMode(ACG::SceneGraph::DrawModes::SOLID_FLAT_SHADED);
GlutObjectNode* handle1 = new GlutObjectNode(newLine, obj->manipulatorNode(), "N_Handle1");
handle1->get_primitive(0).color = ACG::Vec4f(0,1,0,1);
handle1->set_size(0.005*sizeBB.norm());
handle1->show();
handle1->enablePicking(true);
handle1->set_position(hit_point);
newLine->addAdditionalNode(handle1, name(), "handle1");
handle1->drawMode(ACG::SceneGraph::DrawModes::SOLID_FLAT_SHADED);
GlutObjectNode* cenNode = new GlutObjectNode(newLine, obj->manipulatorNode(), "N_Center");
cenNode->get_primitive(0).color = ACG::Vec4f(0,0,1,1);
cenNode->set_size(0.005*sizeBB.norm());
cenNode->show();
cenNode->enablePicking(true);
cenNode->set_position(hit_point);
newLine->addAdditionalNode(cenNode, name(), "circle");
cenNode->drawMode(ACG::SceneGraph::DrawModes::SOLID_FLAT_SHADED);
emit updatedObject(newLine->id(), UPDATE_ALL);
}
else if(_event->type() == QEvent::MouseButtonRelease) {
createCircle_Active_ = false;
PolyLineObject* L;
if(!PluginFunctions::getObject(createCircle_CurrSelIndex_, L))
return;
ACG::SceneGraph::GlutPrimitiveNode* N;
if(L->getAdditionalNode(N, name(), "handle0"))
N->enablePicking(true);
createCircle_CurrSelIndex_ = -1;
}
}
......@@ -960,7 +1008,7 @@ void
PolyLinePlugin::
me_move( QMouseEvent* _event )
{
// MousePress ? -> get eference point
// MousePress ? -> get reference point
if (_event->type() == QEvent::MouseButtonPress) {
unsigned int node_idx, target_idx;
......@@ -968,11 +1016,18 @@ me_move( QMouseEvent* _event )
// pick
if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING, _event->pos(), node_idx, target_idx, &hit_point)) {
BaseNode* node = find_node( PluginFunctions::getRootNode(), node_idx );
GlutObjectNode* glutNode = dynamic_cast<GlutObjectNode*>(node);
if(glutNode) {
moveCircle_SelNode_ = glutNode;
createCircle_CurrSelIndex_ = glutNode->line->id();
}
BaseObjectData* obj = 0;
if (PluginFunctions::getPickedObject(node_idx, obj)) {
// is picked object polyline?
PolyLineObject* cur_pol = PluginFunctions::polyLineObject(obj);
if (cur_pol) {
if (cur_pol && !cur_pol->objectData(CIRCLE_DATA)) {//no vertex dragging on circles!
// Check if we got a line segment or a vertex
if ( target_idx >= cur_pol->line()->n_vertices() )
......@@ -984,32 +1039,66 @@ me_move( QMouseEvent* _event )
move_point_ref_ = &(cur_pol->line()->point(target_idx));
}
}
}
}
// Move ? -> move reference point
if (_event->type() == QEvent::MouseMove)
if (move_point_ref_ != 0) {
if (_event->type() == QEvent::MouseMove){
unsigned int node_idx, target_idx;
ACG::Vec3d hit_point;
unsigned int node_idx, target_idx;
ACG::Vec3d hit_point;
// pick
if(moveCircle_SelNode_) {
if(PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_ANYTHING, _event->pos(), node_idx, target_idx, &hit_point) ) {
PolyLineObject* L;
if(!PluginFunctions::getObject(createCircle_CurrSelIndex_, L))
return;
PolyLineCircleData* LD = (PolyLineCircleData*)L->objectData(CIRCLE_DATA);
double cr;
const double rm = LD->circleMainRadius_, rs = LD->circleSideRadius_;
ACG::Vec3d onPlane;
const ACG::Vec3d x0 = LD->circleCenter_, n = LD->circleNormal_;
createCircle_getHitInfo(LD, hit_point, &hit_point, &cr, &onPlane);
if(!moveCircle_SelNode_->name().compare("N_Center")) {
LD->circleCenter_ = hit_point;
}
else {
ACG::Vec3d axisa = (onPlane - x0).normalize();
if(!moveCircle_SelNode_->name().compare("N_Handle0")) {
ACG::Vec3d axisb = (axisa % n).normalize();
LD->circleMainRadius_ = cr;
LD->circleMainAxis_ = axisa;
LD->circleSideAxis_ = axisb;
} else {
ACG::Vec3d axisb = (n % axisa).normalize();
LD->circleSideRadius_ = cr;
LD->circleSideAxis_ = axisa;
LD->circleMainAxis_ = axisb;
}
}
updateHandles(L);
updatePolyEllipse(L, tool_->sb_CirclePointNum->value());
}
}
else if (move_point_ref_ != 0) {
if (PluginFunctions::scenegraphPick(ACG::SceneGraph::PICK_FACE, _event->pos(), node_idx, target_idx, &hit_point)) {
(*move_point_ref_) = (PolyLine::Point) hit_point;
// update
emit updatedObject(cur_move_id_, UPDATE_GEOMETRY);
}
}
}
// Release ? -> release reference point
if (_event->type() == QEvent::MouseButtonRelease)
move_point_ref_ = 0;
if (_event->type() == QEvent::MouseButtonRelease) {
move_point_ref_ = 0;
moveCircle_SelNode_ = 0;
createCircle_CurrSelIndex_ = -1;
}
}
//-----------------------------------------------------------------------------
......@@ -1117,10 +1206,8 @@ me_split( QMouseEvent* _event )
}
//-----------------------------------------------------------------------------
void
PolyLinePlugin::
me_merge( QMouseEvent* _event )
......@@ -1320,6 +1407,8 @@ me_smart_move( QMouseEvent* _event )
PolyLineObject* cur_pol = PluginFunctions::polyLineObject(obj);
if (cur_pol) {
cur_polyline_obj_ = cur_pol;
// Check if we got a line segment or a vertex
if ( target_idx >= cur_pol->line()->n_vertices() )
return;
......@@ -1357,6 +1446,8 @@ me_smart_move( QMouseEvent* _event )
me_move( _event);
}
//-----------------------------------------------------------------------------
void
PolyLinePlugin::
slotEditModeChanged()
......@@ -1365,6 +1456,27 @@ slotEditModeChanged()
PluginFunctions::actionMode(Viewer::PickingMode);
}
//-----------------------------------------------------------------------------
void
PolyLinePlugin::
updateHandles(PolyLineObject* _lineObject)
{
PolyLineCircleData* circleData = dynamic_cast<PolyLineCircleData*>(_lineObject->objectData(CIRCLE_DATA) );
GlutObjectNode* H0, *H1, *C;
_lineObject->getAdditionalNode(C, name(), "circle");
_lineObject->getAdditionalNode(H0, name(), "handle0");
_lineObject->getAdditionalNode(H1, name(), "handle1");
if(C)
C->set_position(createCircle_getHit(circleData, circleData->circleCenter_));
if(H0)
H0->set_position(createCircle_getHit(circleData, circleData->circleCenter_ + circleData->circleMainAxis_ * circleData->circleMainRadius_));
if(H1)
H1->set_position(createCircle_getHit(circleData, circleData->circleCenter_ + circleData->circleSideAxis_ * circleData->circleSideRadius_));
}
//-----------------------------------------------------------------------------
void
PolyLinePlugin::
slotPickToolbarAction(QAction* _action) {
......
......@@ -43,6 +43,10 @@
#include <ObjectTypes/Plane/QtPlaneSelect.hh>
#include <ACG/Scenegraph/GlutPrimitiveNode.hh>
#include <ObjectTypes/PolyLine/PolyLineCircleData.hh>
#define CREATE_CUT_POLYLINE "Create Polyline"
//== CLASS DEFINITION =========================================================
......@@ -174,18 +178,20 @@ private slots:
void slot_smart_move_timer();
void slot_setCirclePointNum(int i);
private :
EditMode mode();
// mouse events
void me_insert ( QMouseEvent* _event );
void me_insertCircle(QMouseEvent* _event );
void me_delete ( QMouseEvent* _event );
void me_move ( QMouseEvent* _event );
void me_split ( QMouseEvent* _event );
void me_merge ( QMouseEvent* _event );
void me_smart_move( QMouseEvent* _event );
void me_insert ( QMouseEvent* _event );
void me_insertCircle( QMouseEvent* _event );
void me_delete ( QMouseEvent* _event );
void me_move ( QMouseEvent* _event );
void me_split ( QMouseEvent* _event );
void me_merge ( QMouseEvent* _event );
void me_smart_move ( QMouseEvent* _event );
//===========================================================================
/** @name ToolBox
......@@ -227,13 +233,13 @@ private :
QActionGroup* pickToolBarActions_;
QAction* insertAction_;
QAction* insertCircleAction_;
QAction* deleteAction_;
QAction* moveAction_;
QAction* smartMoveAction_;
QAction* mergeAction_;
QAction* splitAction_;
QAction* cutAction_;
QAction* insertCircleAction_;
private slots:
......@@ -287,9 +293,28 @@ private:
PolyLine::Point* create_point_ref_;
PolyLine::Point move_point_orig_;
bool createCircle_Active_;
int createCircle_CurrSelIndex_;
ACG::Vec3d createCircle_Point_;
ACG::Vec3d createCircle_Normal;
ACG::Vec3d createCircle_Normal_;
ACG::SceneGraph::GlutPrimitiveNode* moveCircle_SelNode_;
bool createCircle_getPointOnMesh(TriMeshObject* _triMeshObject,
ACG::Vec3d _center,
ACG::Vec3d _pOnPlane,
ACG::Vec3d _n,
ACG::Vec3d* _pOut);
bool createCircle_getHitInfo(PolyLineCircleData* _circleData,
ACG::Vec3d _hit_Point,
ACG::Vec3d* _pOut = 0,
double* _r = 0,
ACG::Vec3d* _onPlane = 0);
ACG::Vec3d createCircle_getHit(PolyLineCircleData* _circleData, ACG::Vec3d _hit_point);
void updatePolyEllipse(PolyLineObject* _lineObject, unsigned int _pointCount);
void updateHandles(PolyLineObject* _lineObject);
int cur_merge_id_;
......
/*===========================================================================*\
* *
* OpenFlipper *
* Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen *
* www.openflipper.org *
* *
*---------------------------------------------------------------------------*
* This file is part of OpenFlipper. *
* *
* OpenFlipper is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenFlipper is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenFlipper. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision$ *
* $Author$ *
* $Date$ *
* *
\*===========================================================================*/
//=============================================================================
//
// CLASS PolyLinePlugin - IMPLEMENTATION
//
//=============================================================================
//== INCLUDES =================================================================
#include "PolyLinePlugin.hh"
namespace {
struct Onb {
ACG::Vec3d x,y,z;
Onb(PolyLineCircleData* _circleData) {
x = _circleData->circleMainAxis_;
z = _circleData->circleSideAxis_;
y = _circleData->circleNormal_;
}
ACG::Vec3d toWorld(ACG::Vec3d v) const {
return ACG::Vec3d(x[0] * v[0] + y[0] * v[1] + z[0] * v[2],
x[1] * v[0] + y[1] * v[1] + z[1] * v[2],
x[2] * v[0] + y[2] * v[1] + z[2] * v[2]);
}
};
}
//-----------------------------------------------------------------------------
bool
PolyLinePlugin::
createCircle_getPointOnMesh(TriMeshObject* _triMeshObject, ACG::Vec3d _center, ACG::Vec3d _pOnPlane, ACG::Vec3d _n, ACG::Vec3d* _pOut)
{
OpenMeshTriangleBSPT<TriMesh>* bsp = _triMeshObject->requestTriangleBsp();
OpenMeshTriangleBSPT<TriMesh>::RayCollision c = bsp->raycollision(_pOnPlane, _n);
if(c.empty())
return false;
int i = -1;
double smDist = 10e10;
for(unsigned int j = 0; j < c.size(); j++) {
//ACG::Vec3d norAtInt = M->mesh()->normal(c[j].first);
ACG::Vec3d p = _pOnPlane + _n * c[j].second, dir = _center - p;
double dist = sqrt(dir | dir);
if(dist < smDist) {
smDist = dist;
if(_pOut)
*_pOut = p;
i = j;
}
}
return i != -1;
}
//-----------------------------------------------------------------------------
bool
PolyLinePlugin::
createCircle_getHitInfo(PolyLineCircleData* _circleData, ACG::Vec3d _hit_Point, ACG::Vec3d* _pOut, double* r, ACG::Vec3d* _onPlaneO)
{
ACG::Vec3d n = _circleData->circleNormal_, x0 = _circleData->circleCenter_;
double t = ((n | x0) - (n | _hit_Point)) / (n | n);
ACG::Vec3d onPlane = _hit_Point + t * n, d = onPlane - x0;
if(r)
*r = d.norm();
if(_onPlaneO)
*_onPlaneO = onPlane;
TriMeshObject* mesh;
if(PluginFunctions::getObject(_circleData->circleMeshIndex_, mesh))
return createCircle_getPointOnMesh(mesh, x0, onPlane, n, _pOut);
else return false;
}
//-----------------------------------------------------------------------------
ACG::Vec3d
PolyLinePlugin::
createCircle_getHit(PolyLineCircleData* _circleData, ACG::Vec3d _hit_point)
{
ACG::Vec3d h, p;
double r;
if(!createCircle_getHitInfo(_circleData, _hit_point, &h, &r, &p))
return p;//no point on the mesh was found...
else return h;
}
//-----------------------------------------------------------------------------
void
PolyLinePlugin::
updatePolyEllipse(PolyLineObject* _lineObject, unsigned int _pointCount)
{
PolyLineCircleData* lineData = dynamic_cast<PolyLineCircleData*>(_lineObject->objectData(CIRCLE_DATA));
const double theta = 2.0 * M_PI / double(_pointCount);
_pointCount += tool_->rb_CloseCircle->isChecked() ? 0 : -1;
const double r2 = lineData->circleMainRadius_, r1 = lineData->circleSideRadius_;
const Onb basis(lineData);
_lineObject->line()->clear();
for(unsigned int i = 0; i <= _pointCount; i++) {
const double tanTheta_i = tan(theta * i);
double x = (r1 * r2) / sqrt(r2 * r2 + r1 * r1 * tanTheta_i * tanTheta_i);
x = ((theta * i) <= M_PI_2 || (theta * i) > (3.0 * M_PI_2)) ? x : -x;
const double y = tanTheta_i * x;
ACG::Vec3d p(y,0,x);
if(createCircle_getHitInfo(lineData, basis.toWorld(p) + lineData->circleCenter_, &p))
_lineObject->line()->add_point(p);
}
emit updatedObject(_lineObject->id(), UPDATE_GEOMETRY | UPDATE_TOPOLOGY);
}
//-----------------------------------------------------------------------------
void
PolyLinePlugin::
slot_setCirclePointNum(int i)
{
PolyLineObject* _lineObject;
if(createCircle_CurrSelIndex_ != -1 && PluginFunctions::getObject(createCircle_CurrSelIndex_, _lineObject))
updatePolyEllipse(_lineObject, i);
}
......@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>388</width>
<height>713</height>
<width>363</width>
<height>741</height>
</rect>
</property>
<property name="sizePolicy">
......@@ -519,6 +519,43 @@
</layout>