Commit 339f5a31 authored by Robin Eschbach's avatar Robin Eschbach
Browse files

Merge branch 'top2021s3/disconnect_points' into 'sprint_3'

[MERGE #41] Top2021s3/disconnect points

See merge request top/21s/sivert-vis/sivert-vis-project!28
parents de629cf6 eefe3538
......@@ -22,45 +22,36 @@
//
//---------------------------------------------------------------------------
#include "WMClickHandler.h"
#include "WClickHandler.h"
WMClickHandler::WMClickHandler( WMPointConnector* connector ):
WClickHandler::WClickHandler( WMPointConnector* connector ):
m_connector( connector )
{
}
bool WMClickHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
bool WClickHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
{
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>( &aa );
osgViewer::Viewer* viewer = dynamic_cast< osgViewer::Viewer* >( &aa );
if( ea.getEventType() == osgGA::GUIEventAdapter::RELEASE )
if( ea.getEventType() == osgGA::GUIEventAdapter::RELEASE && ea.getButton() != osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON )
{
osg::Camera* camera = viewer->getCamera();
osg::Viewport* viewport = camera->getViewport();
double halfWidth = viewport->width() * 0.5;
double halfHeight = viewport->height() * 0.5;
if( !halfWidth || !halfHeight )
{
return false;
}
double mouseX = ( ea.getX() - halfWidth ) / halfWidth;
double mouseY = ( ea.getY() - halfHeight ) / halfHeight;
osg::Vec3 nearP( 0.0, 0.0, 0.0 );
osg::Vec3 farP( 0.0, 0.0, 0.0 );
mouseToWorldCoordinates( camera, mouseX, mouseY, &nearP, &farP );
mouseToWorldCoordinates( camera, ea.getXnormalized(), ea.getYnormalized(), &nearP, &farP );
osg::Vec3 direction = farP - nearP;
direction.normalize();
m_connector->handleClick( nearP, direction );
bool left = ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON;
m_connector->handleClick( nearP, direction, left );
}
return false;
}
void WMClickHandler::mouseToWorldCoordinates( osg::Camera* camera, double mouseX, double mouseY, osg::Vec3* nearP, osg::Vec3* farP )
void WClickHandler::mouseToWorldCoordinates( osg::Camera* camera, double mouseX, double mouseY, osg::Vec3* nearP, osg::Vec3* farP )
{
osg::Matrix VP = camera->getViewMatrix() * camera->getProjectionMatrix();
......
......@@ -22,8 +22,8 @@
//
//---------------------------------------------------------------------------
#ifndef WMCLICKHANDLER_H
#define WMCLICKHANDLER_H
#ifndef WCLICKHANDLER_H
#define WCLICKHANDLER_H
#include <iostream>
#include <string>
......@@ -35,17 +35,22 @@
#include "WMPointConnector.h"
/**
* Forward declartion for the WMPointConnector
*/
class WMPointConnector;
/**
* The click handler for the mouse click events of WMPointConnector
*/
class WMClickHandler : public osgGA::GUIEventHandler
class WClickHandler : public osgGA::GUIEventHandler
{
public:
/**
* Constructs one WMClickHandler.
* \param connector The WMPointConnector this handler belongs to.
*/
explicit WMClickHandler( WMPointConnector* connector );
explicit WClickHandler( WMPointConnector* connector );
/**
* The callback for the event listener.
......@@ -72,4 +77,4 @@ private:
WMPointConnector* m_connector;
};
#endif // WMCLICKHANDLER_H
#endif // WCLICKHANDLER_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut 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.
//
// OpenWalnut 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 Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <vector>
#include "WConnectorData.h"
WConnectorData::WConnectorData()
{
m_vertices = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
m_colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
}
void WConnectorData::clear()
{
m_vertices->clear();
m_colors->clear();
}
osg::ref_ptr< osg::Vec3Array > WConnectorData::getVertices()
{
return m_vertices;
}
osg::ref_ptr< osg::Vec4Array > WConnectorData::getColors()
{
return m_colors;
}
void WConnectorData::addVertex( osg::Vec3 vertex, osg::Vec4 color )
{
m_vertices->push_back( vertex );
m_colors->push_back( color );
}
void WConnectorData::deselectPoint()
{
if( m_hasSelected )
{
m_colors->operator[]( m_selectedIndex ) = m_selectedOldColor;
m_hasSelected = false;
}
}
void WConnectorData::selectPoint( size_t idx )
{
m_selectedOldColor = m_colors->operator[]( idx );
m_selectedIndex = idx;
m_colors->operator[]( idx ) = osg::Vec4( 0.0, 1.0, 0.0, 1.0 );
m_hasSelected = true;
}
void WConnectorData::selectPoint( osg::Vec3 vertex )
{
std::vector< osg::Vec3 >::iterator vertexIterator = std::find( m_vertices->begin(), m_vertices->end(), vertex );
size_t vIdx = std::distance( m_vertices->begin(), vertexIterator );
selectPoint( vIdx );
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut 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.
//
// OpenWalnut 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 Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WCONNECTORDATA_H
#define WCONNECTORDATA_H
#include <osg/Geode>
#include <boost/shared_ptr.hpp>
/**
* Holds the data of the WMPointConnector.
*/
class WConnectorData
{
public:
/**
* A shared_ptr to this class.
*/
typedef boost::shared_ptr< WConnectorData > SPtr;
/**
* Creates the internal vector arrays.
*/
WConnectorData();
/**
* Clears the internal vector arrays.
*/
void clear();
/**
* \return osg::ref_ptr< osg::Vec3Array > The vertices in this data.
*/
osg::ref_ptr< osg::Vec3Array > getVertices();
/**
* \return osg::ref_ptr< osg::Vec4Array > The colors in this data.
*/
osg::ref_ptr< osg::Vec4Array > getColors();
/**
* Adds a vertex with its color to the currently selected fiber.
* \param vertex The vertex to add.
* \param color The color of the vertex.
*/
void addVertex( osg::Vec3 vertex, osg::Vec4 color );
/**
* Selects a point by its index in m_vertices.
* \param idx The index of the point.
*/
void selectPoint( size_t idx );
/**
* Selects a poit by its coordinates.
* \param vertex The coordinates of the point.
*/
void selectPoint( osg::Vec3 vertex );
/**
* Deselects the currently selected point.
*/
void deselectPoint();
private:
/**
* The vertices that are drawn.
*/
osg::ref_ptr< osg::Vec3Array > m_vertices;
/**
* The color of the vertices that are drawn.
*/
osg::ref_ptr< osg::Vec4Array > m_colors;
/**
* The index of the selected vertex.
*/
size_t m_selectedIndex;
/**
* The color of the vertex before it has been selected.
*/
osg::Vec4 m_selectedOldColor;
/**
* Whether a selection has been done or not.
*/
bool m_hasSelected = false;
};
#endif // WCONNECTORDATA_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut 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.
//
// OpenWalnut 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 Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <string>
#include "WConnectorData.h"
#include "action/WFiberActionAddVertex.h"
#include "action/WFiberActionAddFiber.h"
#include "action/WFiberActionRemoveVertex.h"
#include "action/WFiberActionRemoveFiber.h"
#include "WFiberHandler.h"
WFiberHandler::WFiberHandler( WMPointConnector* pointConnector )
{
m_pointConnector = pointConnector;
m_actionHandler = WActionHandler::SPtr( new WActionHandler() );
m_fibers = PCFiberListSPtr( new PCFiberList() );
m_fibers->push_back( PCFiber() );
m_selectedFiber = 0;
m_fiberCount = 1;
}
void WFiberHandler::addVertexToFiber( osg::Vec3 vertex, size_t fiberIdx, bool silent )
{
m_fibers->at( fiberIdx ).push_back( vertex );
if( !silent )
{
m_actionHandler->pushAction( WFiberActionAddVertex::SPtr( new WFiberActionAddVertex( vertex, fiberIdx, this ) ) );
}
}
void WFiberHandler::addVertexToFiberAt( osg::Vec3 vertex, size_t fiberIdx, size_t position, bool silent )
{
auto fiber = m_fibers->begin() + fiberIdx;
fiber->emplace( fiber->begin() + position, vertex );
if( !silent )
{
m_actionHandler->pushAction( WFiberActionAddVertex::SPtr( new WFiberActionAddVertex( vertex, fiberIdx, this ) ) );
}
}
void WFiberHandler::removeVertexFromFiber( osg::Vec3 vertex, size_t fiberIdx, bool silent )
{
auto fiber = m_fibers->begin() + fiberIdx;
size_t position = 0;
for( auto it = fiber->begin(); it != fiber->end(); it++)
{
if( *it == vertex )
{
fiber->erase( it );
break;
}
position++;
}
if( !silent )
{
m_actionHandler->pushAction( WFiberActionRemoveVertex::SPtr( new WFiberActionRemoveVertex( vertex, fiberIdx, position, this ) ) );
}
}
void WFiberHandler::selectLastPoint()
{
PCFiber fiber = m_fibers->at( m_selectedFiber );
if( !fiber.empty() )
{
m_pointConnector->getConnectorData()->selectPoint( fiber.back() );
}
}
void WFiberHandler::createProperties( WPropertyGroup::SPtr properties )
{
WPropertyBase::PropertyChangeNotifierType notifier = boost::bind( &WFiberHandler::updateProperty, this, boost::placeholders::_1 );
m_possibleFiberSelections = WItemSelection::SPtr( new WItemSelection() );
m_possibleFiberSelections->addItem( ItemType::create( "Fiber 1", "Fiber 1", "", NULL ) );
m_fiberSelection = properties->addProperty(
"Selected Line", "The line to which the points are added", m_possibleFiberSelections->getSelectorFirst(), notifier );
WPropertyHelper::PC_SELECTONLYONE::addTo( m_fiberSelection );
WPropertyHelper::PC_NOTEMPTY::addTo( m_fiberSelection );
m_addFiber = properties->addProperty( "Add Fiber", "Add Fiber", WPVBaseTypes::PV_TRIGGER_READY, notifier );
m_removeFiber = properties->addProperty( "Remove Fiber", "Remove Fiber", WPVBaseTypes::PV_TRIGGER_READY, notifier );
m_undoTrigger = properties->addProperty( "Undo", "Undo", WPVBaseTypes::PV_TRIGGER_READY, notifier );
m_redoTrigger = properties->addProperty( "Redo", "Redo", WPVBaseTypes::PV_TRIGGER_READY, notifier );
}
static bool sortComparator( boost::shared_ptr< WItemSelectionItem > a, boost::shared_ptr< WItemSelectionItem > b )
{
return a->getName().compare( b->getName() ) < 0;
}
void WFiberHandler::addFiber( std::string name, bool silent )
{
m_fibers->push_back( PCFiber() );
m_possibleFiberSelections->addItem( ItemType::create( name, name, "", NULL ) );
m_fiberSelection->set( m_possibleFiberSelections->getSelectorLast() );
if( !silent )
{
m_actionHandler->pushAction( WFiberActionAddFiber::SPtr( new WFiberActionAddFiber( name, m_fibers->size() - 1, this ) ) );
}
}
void WFiberHandler::addFiberAt( std::string name, size_t position, bool silent, PCFiber fiber )
{
m_fibers->emplace( m_fibers->begin() + position, fiber );
m_possibleFiberSelections->addItem( ItemType::create( name, name, "", NULL ) );
m_possibleFiberSelections->stableSort( &sortComparator );
m_fiberSelection->set( m_possibleFiberSelections->getSelectorLast() );
if( !fiber.empty() )
{
m_pointConnector->updateOutput();
}
if( !silent )
{
m_actionHandler->pushAction( WFiberActionAddFiber::SPtr( new WFiberActionAddFiber( name, position, this ) ) );
}
}
void WFiberHandler::removeFiber( size_t idx, bool silent )
{
if( idx == 0 )
{
return;
}
std::string name = m_possibleFiberSelections->at( idx )->getName();
PCFiber fiber = m_fibers->at( idx );
m_fibers->erase( m_fibers->begin() + idx );
m_possibleFiberSelections->remove( m_possibleFiberSelections->at( idx ) );
m_fiberSelection->set( m_possibleFiberSelections->getSelectorLast() );
m_pointConnector->updateOutput();
if( !silent )
{
m_actionHandler->pushAction( WFiberActionRemoveFiber::SPtr( new WFiberActionRemoveFiber( name, idx, fiber , this ) ) );
}
}
void WFiberHandler::selectFiber( size_t idx )
{
m_selectedFiber = idx;
m_pointConnector->getConnectorData()->deselectPoint();
selectLastPoint();
m_pointConnector->updatePoints();
}
void WFiberHandler::updateProperty( WPropertyBase::SPtr property )
{
if( property == m_addFiber && m_addFiber->get( true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
{
m_addFiber->set( WPVBaseTypes::PV_TRIGGER_READY, false );
m_fiberCount++;
std::string name = "Fiber " + boost::lexical_cast< std::string >( m_fiberCount );
addFiber( name );
}
else if( property == m_removeFiber && m_removeFiber->get( true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
{
m_removeFiber->set( WPVBaseTypes::PV_TRIGGER_READY, false );
removeFiber( m_fiberSelection->get().getItemIndexOfSelected( 0 ) );
}
else if( property == m_undoTrigger && m_undoTrigger->get( true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
{
m_undoTrigger->set( WPVBaseTypes::PV_TRIGGER_READY, false );
m_actionHandler->undo();
}
else if( property == m_redoTrigger && m_redoTrigger->get( true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
{
m_redoTrigger->set( WPVBaseTypes::PV_TRIGGER_READY, false );
m_actionHandler->redo();
}
else if( property == m_fiberSelection )
{
selectFiber( m_fiberSelection->get().getItemIndexOfSelected( 0 ) );
}
}
WFiberHandler::PCFiberListSPtr WFiberHandler::getFibers()
{
return m_fibers;
}
WMPointConnector* WFiberHandler::getPointConnector()
{
return m_pointConnector;
}
size_t WFiberHandler::getSelectedFiber()
{
return m_selectedFiber;
}
WActionHandler::SPtr WFiberHandler::getActionHandler()
{
return m_actionHandler;
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut 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.
//
// OpenWalnut 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 Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WFIBERHANDLER_H
#define WFIBERHANDLER_H
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <osg/Geode>
#include "core/common/WItemSelectionItem.h"
#include "core/common/WItemSelectionItemTyped.h"
#include "core/common/WItemSelector.h"
#include "core/kernel/WModule.h"
#include "WMPointConnector.h"
#include "action/WActionHandler.h"
/**
* Handles the fibers of the WMPointsConnector.
*/
class WFiberHandler
{
public:
/**
* A shared_ptr to this class.
*/
typedef boost::shared_ptr< WFiberHandler > SPtr;
/**
* Type of selectable items.
*/
typedef WItemSelectionItemTyped< std::string > ItemType;
/**
* Vector of 3D vectors, representing points
*/
typedef std::vector< osg::Vec3 > PCFiber;
/**
* Vector, that could contain multiple fibers
*/