Commit 1bbca422 authored by Sylvain Thery's avatar Sylvain Thery

add interactive operation on 2-map tutorial

parent 9ea67fa3
......@@ -53,6 +53,12 @@ add_executable( tuto5 tuto5.cpp ${tuto5_ui} ${tuto5_moc})
target_link_libraries( tuto5
${CGoGN_LIBS_D} ${CGoGN_EXT_LIBS} )
QT4_WRAP_UI( tuto_oper2_ui tuto_oper2.ui )
QT4_WRAP_CPP(tuto_oper2_moc tuto_oper2.h)
add_executable( tuto_oper2 tuto_oper2.cpp ${tuto_oper2_ui} ${tuto_oper2_moc})
target_link_libraries( tuto_oper2
${CGoGN_LIBS_D} ${CGoGN_EXT_LIBS} )
QT4_WRAP_UI( tuto_orbits_ui tuto_orbits.ui )
QT4_WRAP_CPP(tuto_orbits_moc tuto_orbits.h)
add_executable( tuto_orbits tuto_orbits.cpp ${tuto_orbits_ui} ${tuto_orbits_moc})
......
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg *
* *
* This library 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 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library 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 this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: http://cgogn.u-strasbg.fr/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#include "tuto_oper2.h"
#include "Algo/Geometry/boundingbox.h"
#include "Algo/Modelisation/polyhedron.h"
#include "Algo/Geometry/centroid.h"
using namespace CGoGN ;
int main(int argc, char **argv)
{
// // interface
QApplication app(argc, argv);
MyQT sqt;
sqt.setDock(& sqt.dock);
sqt.setCallBack( sqt.dock.listOper, SIGNAL(currentRowChanged(int)), SLOT(operation(int)) );
int n=3;
if (argc==2)
n = atoi(argv[1]);
// example code itself
sqt.createMap(n);
// set help message in menu
sqt.setHelpMsg("First Tuto: \nCreate two faces\nsew them\nand affect positions");
// final show for redraw
sqt.show();
// and wait for the end
return app.exec();
}
void MyQT::operation(int x)
{
switch(x)
{
case 0:
CGoGNout <<"split vertex"<<CGoGNendl;
if ((m_selected != NIL) && (m_selected2 != NIL))
{
dm.markAll();
myMap.splitVertex(m_selected,m_selected2);
PFP::VEC3 c1 = Algo::Geometry::faceCentroid<PFP>(myMap, m_selected, position);
PFP::VEC3 c2 = Algo::Geometry::faceCentroid<PFP>(myMap, m_selected2, position);
position[m_selected] = position[m_selected] * 0.7f + c1*0.3f;
position[m_selected2] = position[m_selected2] * 0.7f + c2*0.3f;
updateMap();
}
break;
case 1:
CGoGNout <<"delete vertex"<<CGoGNendl;
if (m_selected != NIL)
{
dm.markAll();
myMap.deleteVertex(m_selected);
updateMap();
m_selected=NIL;
}
break;
case 2:
CGoGNout <<"cut edge"<<CGoGNendl;
if (m_selected != NIL)
{
dm.markAll();
PFP::VEC3 Q = position[myMap.phi1(m_selected)];
myMap.cutEdge(m_selected);
position[myMap.phi1(m_selected)] = (position[m_selected] + Q)/2.0f;
updateMap();
}
break;
case 3:
CGoGNout <<"uncut edge"<<CGoGNendl;
if (m_selected != NIL)
{
myMap.uncutEdge(m_selected);
updateMap();
}
break;
case 4:
CGoGNout <<"collapse edge"<<CGoGNendl;
if (m_selected != NIL)
{
PFP::VEC3 M = (position[m_selected] + position[myMap.phi1(m_selected)])/2.0f;
Dart d = myMap.collapseEdge(m_selected,true);
position[d] = M;
updateMap();
m_selected=NIL;
}
break;
case 5:
CGoGNout <<"flip edge"<<CGoGNendl;
if (m_selected != NIL)
{
myMap.flipEdge(m_selected);
updateMap();
}
break;
case 6:
CGoGNout <<"flip back edge"<<CGoGNendl;
if (m_selected != NIL)
{
myMap.flipBackEdge(m_selected);
updateMap();
}
break;
case 7:
CGoGNout <<"split face"<<CGoGNendl;
if (m_selected != NIL)
{
myMap.splitFace(m_selected,m_selected2);
updateMap();
}
break;
case 8:
CGoGNout <<"merge face"<<CGoGNendl;
if (m_selected != NIL)
{
myMap.mergeFaces(m_selected);
updateMap();
m_selected=NIL;
}
break;
default:
break;
}
updateGL();
dock.listOper->setCurrentRow(-1);
}
void MyQT::createMap(int n)
{
position = myMap.addAttribute<PFP::VEC3>(VERTEX, "position");
colorDarts = myMap.addAttribute<PFP::VEC3>(DART, "color");
Algo::Modelisation::Polyhedron<PFP> grid(myMap,position);
grid.grid_topo(n,n);
grid.embedGrid(1.,1.,0.);
// bounding box of scene
Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
float lWidthObj = std::max<PFP::REAL>(std::max<PFP::REAL>(bb.size(0), bb.size(1)), bb.size(2));
Geom::Vec3f lPosObj = (bb.min() + bb.max()) / PFP::REAL(2);
// send BB info to interface for centering on GL screen
setParamObject(lWidthObj, lPosObj.data());
// first show for be sure that GL context is binded
show();
// render the topo of the map without boundary darts
dm.markAll();
m_render_topo->setDartWidth(5.0f);
m_render_topo->setInitialDartsColor(0.0f,0.0f,0.0f);
m_render_topo->updateData<PFP>(myMap, position, 0.9f, 0.9f,nb);
for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d))
{
if (dm.isMarked(d) && (!myMap.isBoundaryMarked(d)))
{
int n = random();
float r = float(n&0x7f)/255.0f + 0.25f;
float g = float((n>>8)&0x7f)/255.0f + 0.25f;
float b = float((n>>16)&0x7f)/255.0 + 0.25f;
colorDarts[d] = Geom::Vec3f(r,g,b);
m_render_topo->setDartColor(d,r,g,b);
}
}
}
void MyQT::updateMap()
{
m_render_topo->updateData<PFP>(myMap, position, 0.9f, 0.9f,nb);
for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d))
{
if (dm.isMarked(d) && (!myMap.isBoundaryMarked(d)))
{
const Geom::Vec3f& C = colorDarts[d];
if (C*C != 0.0f)
m_render_topo->setDartColor(d,C[0],C[1],C[2]);
}
}
}
// initialization GL callback
void MyQT::cb_initGL()
{
glClearColor(1.0f,1.0f,1.0f,1.0f);
m_render_topo = new Algo::Render::GL2::TopoRender() ;
}
// redraw GL callback (clear and swap already done)
void MyQT::cb_redraw()
{
glEnable( GL_POLYGON_OFFSET_FILL );
glPolygonOffset( 1.0f, 1.0f );
m_render_topo->drawTopo();
glDisable( GL_POLYGON_OFFSET_FILL );
if (m_selected != NIL)
m_render_topo->overdrawDart(m_selected, 11, 1.0f, 0.0f, 0.0f);
if (m_selected2 != NIL)
m_render_topo->overdrawDart(m_selected2, 11, 0.0f, 1.0f, 0.0f);
}
void MyQT::cb_mousePress(int button, int x, int y)
{
if (Shift())
{
Dart d = m_render_topo->picking<PFP>(myMap, x,y, nb);
if (button == Qt::LeftButton)
{
if (d != Dart::nil())
m_selected = d;
}
if (button == Qt::RightButton)
{
if (d != Dart::nil())
m_selected2 = d;
}
updateGL();
}
}
void MyQT::cb_keyPress(int keycode)
{
switch(keycode)
{
case 'c':
for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d))
{
if (!myMap.isBoundaryMarked(d))
{
int n = random();
float r = float(n&0x7f)/255.0f + 0.25f;
float g = float((n>>8)&0x7f)/255.0f + 0.25f;
float b = float((n>>16)&0x7f)/255.0 + 0.25f;
colorDarts[d] = Geom::Vec3f(r,g,b);
m_render_topo->setDartColor(d,r,g,b);
}
}
break;
case 'g':
for (Dart d=myMap.begin(); d!=myMap.end(); myMap.next(d))
{
if (!myMap.isBoundaryMarked(d))
{
colorDarts[d] = Geom::Vec3f(0.5f,0.5f,0.5f);
m_render_topo->setDartColor(d,0.5f,0.5f,0.5f);
}
}
break;
}
updateGL();
}
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg *
* *
* This library 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 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library 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 this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: http://cgogn.u-strasbg.fr/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#ifndef _TUTO_OPER2_
#define _TUTO_OPER2_
//#define USE_GMAP
#include "Topology/generic/parameters.h"
#ifdef USE_GMAP
#include "Topology/gmap/embeddedGMap2.h"
#else
#include "Topology/map/embeddedMap2.h"
#endif
#include "Algo/Render/GL2/topoRender.h"
#include "ui_tuto_oper2.h"
#include "Utils/Qt/qtui.h"
#include "Utils/Qt/qtSimple.h"
#include "Utils/cgognStream.h"
using namespace CGoGN ;
/**
* Struct that contains some informations about the types of the manipulated objects
* Mainly here to be used by the algorithms that are parameterized by it
*/
struct PFP: public PFP_STANDARD
{
// definition of the type of the map
#ifdef USE_GMAP
typedef EmbeddedGMap2 MAP;
#else
typedef EmbeddedMap2 MAP;
#endif
};
class MyQT: public Utils::QT::SimpleQT
{
Q_OBJECT
public:
MyQT():nb(myMap),m_render_topo(NULL),m_selected(NIL),m_selected2(NIL),dm(myMap) {}
void cb_redraw();
void cb_initGL();
void cb_mousePress(int button, int x, int y);
void cb_keyPress(int code);
Utils::QT::uiDockInterface dock;
protected:
// declaration of the map
PFP::MAP myMap;
PFP::TVEC3 position;
AttributeHandler<Geom::Vec3f> colorDarts;
SelectorDartNoBoundary<PFP::MAP> nb;
// render (for the topo)
Algo::Render::GL2::TopoRender* m_render_topo;
Dart m_selected;
Dart m_selected2;
DartMarker dm;
// just for more compact writing
inline Dart PHI1(Dart d) {return myMap.phi1(d);}
inline Dart PHI_1(Dart d) {return myMap.phi_1(d);}
inline Dart PHI2(Dart d) {return myMap.phi2(d);}
template<int X>
Dart PHI(Dart d) {return myMap.phi<X>(d);}
public:
// example of simple map creation
void createMap(int n);
void updateMap();
public slots:
void operation(int x);
};
#endif
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DockWidget</class>
<widget class="QDockWidget" name="DockWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>227</width>
<height>369</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>150</width>
<height>250</height>
</size>
</property>
<property name="windowTitle">
<string>Interface</string>
</property>
<widget class="QWidget" name="dockWidgetContents">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>150</width>
<height>200</height>
</size>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="margin">
<number>2</number>
</property>
<property name="spacing">
<number>4</number>
</property>
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListWidget" name="listOper">
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<item>
<property name="text">
<string>splitVertex</string>
</property>
</item>
<item>
<property name="text">
<string>deleteVertex</string>
</property>
</item>
<item>
<property name="text">
<string>cutEdge</string>
</property>
</item>
<item>
<property name="text">
<string>uncutEdge</string>
</property>
</item>
<item>
<property name="text">
<string>collapseEdge</string>
</property>
</item>
<item>
<property name="text">
<string>flipEdge</string>
</property>
</item>
<item>
<property name="text">
<string>flipBackEdge</string>
</property>
</item>
<item>
<property name="text">
<string>splitFace</string>
</property>
</item>
<item>
<property name="text">
<string>mergeFace</string>
</property>
</item>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
......@@ -33,6 +33,7 @@
#include "Topology/generic/dart.h"
#include "Topology/generic/attributeHandler.h"
#include "Topology/generic/functor.h"
#include "Geometry/vector_gen.h"
#include "Utils/vbo.h"
......@@ -102,9 +103,19 @@ protected:
*/
float m_topo_relation_width;
/**
* pointer for saved colorvbo (in picking)
*/
float *m_color_save;
/**
* initial darts color (set in update)
*/
Geom::Vec3f m_dartsColor;
/**
* attribute index to get easy correspdance dart/color
*/
AttributeHandler<unsigned int> m_attIndex;
......@@ -204,6 +215,15 @@ public:
*/
void setAllDartsColor(float r, float g, float b);
/**
* change dart initial color (used when calling updateData)
* @param d the dart
* @param r red !
* @param g green !
* @param b blue !
*/
void setInitialDartsColor(float r, float g, float b);
/**
* overdraw a dart with given width and color
* @param d the dart
......
......@@ -167,8 +167,8 @@ void Topo3Render::updateDataMap3(typename PFP::MAP& mapx, const typename PFP::TV
*positionDartBuf++ = P;
*positionDartBuf++ = Q;
*colorDartBuf++ = VEC3(1.,1.,1.0);
*colorDartBuf++ = VEC3(1.,1.,1.0);
*colorDartBuf++ = m_dartsColor;
*colorDartBuf++ = m_dartsColor;
fv1[d] = P*0.1f + Q*0.9f;
fv11[d] = P*0.9f + Q*0.1f;
......@@ -417,10 +417,10 @@ void Topo3Render::updateDataGMap3(typename PFP::MAP& mapx, const typename PFP::T
*positionDartBuf++ = PP;
*positionDartBuf++ = Q;
*positionDartBuf++ = QQ;
*colorDartBuf++ = VEC3(1.,1.,1.);
*colorDartBuf++ = VEC3(1.,1.,1.);
*colorDartBuf++ = VEC3(1.,1.,1.);
*colorDartBuf++ = VEC3(1.,1.,1.);
*colorDartBuf++ = m_dartsColor;
*colorDartBuf++ = m_dartsColor;
*colorDartBuf++ = m_dartsColor;
*colorDartBuf++ = m_dartsColor;
m_attIndex[d] = posDBI;
posDBI+=2;
......
......@@ -33,6 +33,7 @@
#include "Topology/generic/dart.h"
#include "Topology/generic/attributeHandler.h"
#include "Topology/generic/functor.h"
#include "Geometry/vector_gen.h"
#include "Utils/vbo.h"
......@@ -88,6 +89,11 @@ protected:
*/
float m_topo_relation_width;
/**
* initial darts color (set in update)
*/
Geom::Vec3f m_dartsColor;
float *m_color_save;
......@@ -178,6 +184,8 @@ public:
*/
void setAllDartsColor(float r, float g, float b);
void setInitialDartsColor(float r, float g, float b);
/**
* redraw one dart with specific width and color (not efficient use only for debug with small amount of call)
* @param d the dart
......
......@@ -139,9 +139,9 @@ void TopoRender::updateDataMap(typename PFP::MAP& mapx, const typename PFP::TVEC
m_attIndex[d] = indexDC;
indexDC+=2;
*positionDartBuf++ = P;
*colorDartBuf++ = VEC3(1.,1.,1.);
*colorDartBuf++ = m_dartsColor;
*positionDartBuf++ = Q;
*colorDartBuf++ = VEC3(1.,1.,1.);
*colorDartBuf++ = m_dartsColor;
VEC3 f = P*0.5f + Q*0.5f;
fv2[d] = f;